xref: /linux/drivers/net/ethernet/marvell/mvneta.c (revision e3617433c3da3d0859a4bc67f3f975e87f650ebf)
1 /*
2  * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3  *
4  * Copyright (C) 2012 Marvell
5  *
6  * Rami Rosen <rosenr@marvell.com>
7  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
8  *
9  * This file is licensed under the terms of the GNU General Public
10  * License version 2. This program is licensed "as is" without any
11  * warranty of any kind, whether express or implied.
12  */
13 
14 #include <linux/clk.h>
15 #include <linux/cpu.h>
16 #include <linux/etherdevice.h>
17 #include <linux/if_vlan.h>
18 #include <linux/inetdevice.h>
19 #include <linux/interrupt.h>
20 #include <linux/io.h>
21 #include <linux/kernel.h>
22 #include <linux/mbus.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/of.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_mdio.h>
29 #include <linux/of_net.h>
30 #include <linux/phy/phy.h>
31 #include <linux/phy.h>
32 #include <linux/phylink.h>
33 #include <linux/platform_device.h>
34 #include <linux/skbuff.h>
35 #include <net/hwbm.h>
36 #include "mvneta_bm.h"
37 #include <net/ip.h>
38 #include <net/ipv6.h>
39 #include <net/tso.h>
40 #include <net/page_pool.h>
41 #include <linux/bpf_trace.h>
42 
43 /* Registers */
44 #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
45 #define      MVNETA_RXQ_HW_BUF_ALLOC            BIT(0)
46 #define      MVNETA_RXQ_SHORT_POOL_ID_SHIFT	4
47 #define      MVNETA_RXQ_SHORT_POOL_ID_MASK	0x30
48 #define      MVNETA_RXQ_LONG_POOL_ID_SHIFT	6
49 #define      MVNETA_RXQ_LONG_POOL_ID_MASK	0xc0
50 #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
51 #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
52 #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
53 #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
54 #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
55 #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
56 #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
57 #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
58 #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
59 #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
60 #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
61 #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
62 #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
63 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool)	(0x1700 + ((pool) << 2))
64 #define      MVNETA_PORT_POOL_BUFFER_SZ_SHIFT	3
65 #define      MVNETA_PORT_POOL_BUFFER_SZ_MASK	0xfff8
66 #define MVNETA_PORT_RX_RESET                    0x1cc0
67 #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
68 #define MVNETA_PHY_ADDR                         0x2000
69 #define      MVNETA_PHY_ADDR_MASK               0x1f
70 #define MVNETA_MBUS_RETRY                       0x2010
71 #define MVNETA_UNIT_INTR_CAUSE                  0x2080
72 #define MVNETA_UNIT_CONTROL                     0x20B0
73 #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
74 #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
75 #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
76 #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
77 #define MVNETA_BASE_ADDR_ENABLE                 0x2290
78 #define MVNETA_ACCESS_PROTECT_ENABLE            0x2294
79 #define MVNETA_PORT_CONFIG                      0x2400
80 #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
81 #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
82 #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
83 #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
84 #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
85 #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
86 #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
87 #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
88 #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
89 						 MVNETA_DEF_RXQ_ARP(q)	 | \
90 						 MVNETA_DEF_RXQ_TCP(q)	 | \
91 						 MVNETA_DEF_RXQ_UDP(q)	 | \
92 						 MVNETA_DEF_RXQ_BPDU(q)	 | \
93 						 MVNETA_TX_UNSET_ERR_SUM | \
94 						 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
95 #define MVNETA_PORT_CONFIG_EXTEND                0x2404
96 #define MVNETA_MAC_ADDR_LOW                      0x2414
97 #define MVNETA_MAC_ADDR_HIGH                     0x2418
98 #define MVNETA_SDMA_CONFIG                       0x241c
99 #define      MVNETA_SDMA_BRST_SIZE_16            4
100 #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
101 #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
102 #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
103 #define      MVNETA_DESC_SWAP                    BIT(6)
104 #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
105 #define	MVNETA_VLAN_PRIO_TO_RXQ			 0x2440
106 #define      MVNETA_VLAN_PRIO_RXQ_MAP(prio, rxq) ((rxq) << ((prio) * 3))
107 #define MVNETA_PORT_STATUS                       0x2444
108 #define      MVNETA_TX_IN_PRGRS                  BIT(0)
109 #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
110 #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
111 /* Only exists on Armada XP and Armada 370 */
112 #define MVNETA_SERDES_CFG			 0x24A0
113 #define      MVNETA_SGMII_SERDES_PROTO		 0x0cc7
114 #define      MVNETA_QSGMII_SERDES_PROTO		 0x0667
115 #define      MVNETA_HSGMII_SERDES_PROTO		 0x1107
116 #define MVNETA_TYPE_PRIO                         0x24bc
117 #define      MVNETA_FORCE_UNI                    BIT(21)
118 #define MVNETA_TXQ_CMD_1                         0x24e4
119 #define MVNETA_TXQ_CMD                           0x2448
120 #define      MVNETA_TXQ_DISABLE_SHIFT            8
121 #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
122 #define MVNETA_RX_DISCARD_FRAME_COUNT		 0x2484
123 #define MVNETA_OVERRUN_FRAME_COUNT		 0x2488
124 #define MVNETA_GMAC_CLOCK_DIVIDER                0x24f4
125 #define      MVNETA_GMAC_1MS_CLOCK_ENABLE        BIT(31)
126 #define MVNETA_ACC_MODE                          0x2500
127 #define MVNETA_BM_ADDRESS                        0x2504
128 #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
129 #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
130 #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
131 #define      MVNETA_CPU_RXQ_ACCESS(rxq)		 BIT(rxq)
132 #define      MVNETA_CPU_TXQ_ACCESS(txq)		 BIT(txq + 8)
133 #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
134 
135 /* Exception Interrupt Port/Queue Cause register
136  *
137  * Their behavior depend of the mapping done using the PCPX2Q
138  * registers. For a given CPU if the bit associated to a queue is not
139  * set, then for the register a read from this CPU will always return
140  * 0 and a write won't do anything
141  */
142 
143 #define MVNETA_INTR_NEW_CAUSE                    0x25a0
144 #define MVNETA_INTR_NEW_MASK                     0x25a4
145 
146 /* bits  0..7  = TXQ SENT, one bit per queue.
147  * bits  8..15 = RXQ OCCUP, one bit per queue.
148  * bits 16..23 = RXQ FREE, one bit per queue.
149  * bit  29 = OLD_REG_SUM, see old reg ?
150  * bit  30 = TX_ERR_SUM, one bit for 4 ports
151  * bit  31 = MISC_SUM,   one bit for 4 ports
152  */
153 #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
154 #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
155 #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
156 #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
157 #define      MVNETA_MISCINTR_INTR_MASK           BIT(31)
158 
159 #define MVNETA_INTR_OLD_CAUSE                    0x25a8
160 #define MVNETA_INTR_OLD_MASK                     0x25ac
161 
162 /* Data Path Port/Queue Cause Register */
163 #define MVNETA_INTR_MISC_CAUSE                   0x25b0
164 #define MVNETA_INTR_MISC_MASK                    0x25b4
165 
166 #define      MVNETA_CAUSE_PHY_STATUS_CHANGE      BIT(0)
167 #define      MVNETA_CAUSE_LINK_CHANGE            BIT(1)
168 #define      MVNETA_CAUSE_PTP                    BIT(4)
169 
170 #define      MVNETA_CAUSE_INTERNAL_ADDR_ERR      BIT(7)
171 #define      MVNETA_CAUSE_RX_OVERRUN             BIT(8)
172 #define      MVNETA_CAUSE_RX_CRC_ERROR           BIT(9)
173 #define      MVNETA_CAUSE_RX_LARGE_PKT           BIT(10)
174 #define      MVNETA_CAUSE_TX_UNDERUN             BIT(11)
175 #define      MVNETA_CAUSE_PRBS_ERR               BIT(12)
176 #define      MVNETA_CAUSE_PSC_SYNC_CHANGE        BIT(13)
177 #define      MVNETA_CAUSE_SERDES_SYNC_ERR        BIT(14)
178 
179 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT    16
180 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK   (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
181 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
182 
183 #define      MVNETA_CAUSE_TXQ_ERROR_SHIFT        24
184 #define      MVNETA_CAUSE_TXQ_ERROR_ALL_MASK     (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
185 #define      MVNETA_CAUSE_TXQ_ERROR_MASK(q)      (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
186 
187 #define MVNETA_INTR_ENABLE                       0x25b8
188 #define      MVNETA_TXQ_INTR_ENABLE_ALL_MASK     0x0000ff00
189 #define      MVNETA_RXQ_INTR_ENABLE_ALL_MASK     0x000000ff
190 
191 #define MVNETA_RXQ_CMD                           0x2680
192 #define      MVNETA_RXQ_DISABLE_SHIFT            8
193 #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
194 #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
195 #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
196 #define MVNETA_GMAC_CTRL_0                       0x2c00
197 #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
198 #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
199 #define      MVNETA_GMAC0_PORT_1000BASE_X        BIT(1)
200 #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
201 #define MVNETA_GMAC_CTRL_2                       0x2c08
202 #define      MVNETA_GMAC2_INBAND_AN_ENABLE       BIT(0)
203 #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
204 #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
205 #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
206 #define MVNETA_GMAC_STATUS                       0x2c10
207 #define      MVNETA_GMAC_LINK_UP                 BIT(0)
208 #define      MVNETA_GMAC_SPEED_1000              BIT(1)
209 #define      MVNETA_GMAC_SPEED_100               BIT(2)
210 #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
211 #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
212 #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
213 #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
214 #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
215 #define      MVNETA_GMAC_AN_COMPLETE             BIT(11)
216 #define      MVNETA_GMAC_SYNC_OK                 BIT(14)
217 #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
218 #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
219 #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
220 #define      MVNETA_GMAC_INBAND_AN_ENABLE        BIT(2)
221 #define      MVNETA_GMAC_AN_BYPASS_ENABLE        BIT(3)
222 #define      MVNETA_GMAC_INBAND_RESTART_AN       BIT(4)
223 #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
224 #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
225 #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
226 #define      MVNETA_GMAC_CONFIG_FLOW_CTRL        BIT(8)
227 #define      MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL    BIT(9)
228 #define      MVNETA_GMAC_AN_FLOW_CTRL_EN         BIT(11)
229 #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
230 #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
231 #define MVNETA_GMAC_CTRL_4                       0x2c90
232 #define      MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE  BIT(1)
233 #define MVNETA_MIB_COUNTERS_BASE                 0x3000
234 #define      MVNETA_MIB_LATE_COLLISION           0x7c
235 #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
236 #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
237 #define MVNETA_DA_FILT_UCAST_BASE                0x3600
238 #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
239 #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
240 #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
241 #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
242 #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
243 #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
244 #define      MVNETA_TXQ_DEC_SENT_MASK            0xff
245 #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
246 #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
247 #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
248 #define MVNETA_PORT_TX_RESET                     0x3cf0
249 #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
250 #define MVNETA_TX_MTU                            0x3e0c
251 #define MVNETA_TX_TOKEN_SIZE                     0x3e14
252 #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
253 #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
254 #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
255 
256 #define MVNETA_LPI_CTRL_0                        0x2cc0
257 #define MVNETA_LPI_CTRL_1                        0x2cc4
258 #define      MVNETA_LPI_REQUEST_ENABLE           BIT(0)
259 #define MVNETA_LPI_CTRL_2                        0x2cc8
260 #define MVNETA_LPI_STATUS                        0x2ccc
261 
262 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK	 0xff
263 
264 /* Descriptor ring Macros */
265 #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
266 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
267 
268 /* Various constants */
269 
270 /* Coalescing */
271 #define MVNETA_TXDONE_COAL_PKTS		0	/* interrupt per packet */
272 #define MVNETA_RX_COAL_PKTS		32
273 #define MVNETA_RX_COAL_USEC		100
274 
275 /* The two bytes Marvell header. Either contains a special value used
276  * by Marvell switches when a specific hardware mode is enabled (not
277  * supported by this driver) or is filled automatically by zeroes on
278  * the RX side. Those two bytes being at the front of the Ethernet
279  * header, they allow to have the IP header aligned on a 4 bytes
280  * boundary automatically: the hardware skips those two bytes on its
281  * own.
282  */
283 #define MVNETA_MH_SIZE			2
284 
285 #define MVNETA_VLAN_TAG_LEN             4
286 
287 #define MVNETA_TX_CSUM_DEF_SIZE		1600
288 #define MVNETA_TX_CSUM_MAX_SIZE		9800
289 #define MVNETA_ACC_MODE_EXT1		1
290 #define MVNETA_ACC_MODE_EXT2		2
291 
292 #define MVNETA_MAX_DECODE_WIN		6
293 
294 /* Timeout constants */
295 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
296 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
297 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
298 
299 #define MVNETA_TX_MTU_MAX		0x3ffff
300 
301 /* The RSS lookup table actually has 256 entries but we do not use
302  * them yet
303  */
304 #define MVNETA_RSS_LU_TABLE_SIZE	1
305 
306 /* Max number of Rx descriptors */
307 #define MVNETA_MAX_RXD 512
308 
309 /* Max number of Tx descriptors */
310 #define MVNETA_MAX_TXD 1024
311 
312 /* Max number of allowed TCP segments for software TSO */
313 #define MVNETA_MAX_TSO_SEGS 100
314 
315 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
316 
317 /* descriptor aligned size */
318 #define MVNETA_DESC_ALIGNED_SIZE	32
319 
320 /* Number of bytes to be taken into account by HW when putting incoming data
321  * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
322  * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
323  */
324 #define MVNETA_RX_PKT_OFFSET_CORRECTION		64
325 
326 #define MVNETA_RX_PKT_SIZE(mtu) \
327 	ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
328 	      ETH_HLEN + ETH_FCS_LEN,			     \
329 	      cache_line_size())
330 
331 /* Driver assumes that the last 3 bits are 0 */
332 #define MVNETA_SKB_HEADROOM	ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8)
333 #define MVNETA_SKB_PAD	(SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \
334 			 MVNETA_SKB_HEADROOM))
335 #define MVNETA_MAX_RX_BUF_SIZE	(PAGE_SIZE - MVNETA_SKB_PAD)
336 
337 #define IS_TSO_HEADER(txq, addr) \
338 	((addr >= txq->tso_hdrs_phys) && \
339 	 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
340 
341 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
342 	(((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
343 
344 enum {
345 	ETHTOOL_STAT_EEE_WAKEUP,
346 	ETHTOOL_STAT_SKB_ALLOC_ERR,
347 	ETHTOOL_STAT_REFILL_ERR,
348 	ETHTOOL_XDP_REDIRECT,
349 	ETHTOOL_XDP_PASS,
350 	ETHTOOL_XDP_DROP,
351 	ETHTOOL_XDP_TX,
352 	ETHTOOL_XDP_TX_ERR,
353 	ETHTOOL_XDP_XMIT,
354 	ETHTOOL_XDP_XMIT_ERR,
355 	ETHTOOL_MAX_STATS,
356 };
357 
358 struct mvneta_statistic {
359 	unsigned short offset;
360 	unsigned short type;
361 	const char name[ETH_GSTRING_LEN];
362 };
363 
364 #define T_REG_32	32
365 #define T_REG_64	64
366 #define T_SW		1
367 
368 #define MVNETA_XDP_PASS		0
369 #define MVNETA_XDP_DROPPED	BIT(0)
370 #define MVNETA_XDP_TX		BIT(1)
371 #define MVNETA_XDP_REDIR	BIT(2)
372 
373 static const struct mvneta_statistic mvneta_statistics[] = {
374 	{ 0x3000, T_REG_64, "good_octets_received", },
375 	{ 0x3010, T_REG_32, "good_frames_received", },
376 	{ 0x3008, T_REG_32, "bad_octets_received", },
377 	{ 0x3014, T_REG_32, "bad_frames_received", },
378 	{ 0x3018, T_REG_32, "broadcast_frames_received", },
379 	{ 0x301c, T_REG_32, "multicast_frames_received", },
380 	{ 0x3050, T_REG_32, "unrec_mac_control_received", },
381 	{ 0x3058, T_REG_32, "good_fc_received", },
382 	{ 0x305c, T_REG_32, "bad_fc_received", },
383 	{ 0x3060, T_REG_32, "undersize_received", },
384 	{ 0x3064, T_REG_32, "fragments_received", },
385 	{ 0x3068, T_REG_32, "oversize_received", },
386 	{ 0x306c, T_REG_32, "jabber_received", },
387 	{ 0x3070, T_REG_32, "mac_receive_error", },
388 	{ 0x3074, T_REG_32, "bad_crc_event", },
389 	{ 0x3078, T_REG_32, "collision", },
390 	{ 0x307c, T_REG_32, "late_collision", },
391 	{ 0x2484, T_REG_32, "rx_discard", },
392 	{ 0x2488, T_REG_32, "rx_overrun", },
393 	{ 0x3020, T_REG_32, "frames_64_octets", },
394 	{ 0x3024, T_REG_32, "frames_65_to_127_octets", },
395 	{ 0x3028, T_REG_32, "frames_128_to_255_octets", },
396 	{ 0x302c, T_REG_32, "frames_256_to_511_octets", },
397 	{ 0x3030, T_REG_32, "frames_512_to_1023_octets", },
398 	{ 0x3034, T_REG_32, "frames_1024_to_max_octets", },
399 	{ 0x3038, T_REG_64, "good_octets_sent", },
400 	{ 0x3040, T_REG_32, "good_frames_sent", },
401 	{ 0x3044, T_REG_32, "excessive_collision", },
402 	{ 0x3048, T_REG_32, "multicast_frames_sent", },
403 	{ 0x304c, T_REG_32, "broadcast_frames_sent", },
404 	{ 0x3054, T_REG_32, "fc_sent", },
405 	{ 0x300c, T_REG_32, "internal_mac_transmit_err", },
406 	{ ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
407 	{ ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
408 	{ ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
409 	{ ETHTOOL_XDP_REDIRECT, T_SW, "rx_xdp_redirect", },
410 	{ ETHTOOL_XDP_PASS, T_SW, "rx_xdp_pass", },
411 	{ ETHTOOL_XDP_DROP, T_SW, "rx_xdp_drop", },
412 	{ ETHTOOL_XDP_TX, T_SW, "rx_xdp_tx", },
413 	{ ETHTOOL_XDP_TX_ERR, T_SW, "rx_xdp_tx_errors", },
414 	{ ETHTOOL_XDP_XMIT, T_SW, "tx_xdp_xmit", },
415 	{ ETHTOOL_XDP_XMIT_ERR, T_SW, "tx_xdp_xmit_errors", },
416 };
417 
418 struct mvneta_stats {
419 	u64	rx_packets;
420 	u64	rx_bytes;
421 	u64	tx_packets;
422 	u64	tx_bytes;
423 	/* xdp */
424 	u64	xdp_redirect;
425 	u64	xdp_pass;
426 	u64	xdp_drop;
427 	u64	xdp_xmit;
428 	u64	xdp_xmit_err;
429 	u64	xdp_tx;
430 	u64	xdp_tx_err;
431 };
432 
433 struct mvneta_ethtool_stats {
434 	struct mvneta_stats ps;
435 	u64	skb_alloc_error;
436 	u64	refill_error;
437 };
438 
439 struct mvneta_pcpu_stats {
440 	struct u64_stats_sync syncp;
441 
442 	struct mvneta_ethtool_stats es;
443 	u64	rx_dropped;
444 	u64	rx_errors;
445 };
446 
447 struct mvneta_pcpu_port {
448 	/* Pointer to the shared port */
449 	struct mvneta_port	*pp;
450 
451 	/* Pointer to the CPU-local NAPI struct */
452 	struct napi_struct	napi;
453 
454 	/* Cause of the previous interrupt */
455 	u32			cause_rx_tx;
456 };
457 
458 enum {
459 	__MVNETA_DOWN,
460 };
461 
462 struct mvneta_port {
463 	u8 id;
464 	struct mvneta_pcpu_port __percpu	*ports;
465 	struct mvneta_pcpu_stats __percpu	*stats;
466 
467 	unsigned long state;
468 
469 	int pkt_size;
470 	void __iomem *base;
471 	struct mvneta_rx_queue *rxqs;
472 	struct mvneta_tx_queue *txqs;
473 	struct net_device *dev;
474 	struct hlist_node node_online;
475 	struct hlist_node node_dead;
476 	int rxq_def;
477 	/* Protect the access to the percpu interrupt registers,
478 	 * ensuring that the configuration remains coherent.
479 	 */
480 	spinlock_t lock;
481 	bool is_stopped;
482 
483 	u32 cause_rx_tx;
484 	struct napi_struct napi;
485 
486 	struct bpf_prog *xdp_prog;
487 
488 	/* Core clock */
489 	struct clk *clk;
490 	/* AXI clock */
491 	struct clk *clk_bus;
492 	u8 mcast_count[256];
493 	u16 tx_ring_size;
494 	u16 rx_ring_size;
495 	u8 prio_tc_map[8];
496 
497 	phy_interface_t phy_interface;
498 	struct device_node *dn;
499 	unsigned int tx_csum_limit;
500 	struct phylink *phylink;
501 	struct phylink_config phylink_config;
502 	struct phy *comphy;
503 
504 	struct mvneta_bm *bm_priv;
505 	struct mvneta_bm_pool *pool_long;
506 	struct mvneta_bm_pool *pool_short;
507 	int bm_win_id;
508 
509 	bool eee_enabled;
510 	bool eee_active;
511 	bool tx_lpi_enabled;
512 
513 	u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
514 
515 	u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
516 
517 	/* Flags for special SoC configurations */
518 	bool neta_armada3700;
519 	u16 rx_offset_correction;
520 	const struct mbus_dram_target_info *dram_target_info;
521 };
522 
523 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
524  * layout of the transmit and reception DMA descriptors, and their
525  * layout is therefore defined by the hardware design
526  */
527 
528 #define MVNETA_TX_L3_OFF_SHIFT	0
529 #define MVNETA_TX_IP_HLEN_SHIFT	8
530 #define MVNETA_TX_L4_UDP	BIT(16)
531 #define MVNETA_TX_L3_IP6	BIT(17)
532 #define MVNETA_TXD_IP_CSUM	BIT(18)
533 #define MVNETA_TXD_Z_PAD	BIT(19)
534 #define MVNETA_TXD_L_DESC	BIT(20)
535 #define MVNETA_TXD_F_DESC	BIT(21)
536 #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
537 				 MVNETA_TXD_L_DESC | \
538 				 MVNETA_TXD_F_DESC)
539 #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
540 #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
541 
542 #define MVNETA_RXD_ERR_CRC		0x0
543 #define MVNETA_RXD_BM_POOL_SHIFT	13
544 #define MVNETA_RXD_BM_POOL_MASK		(BIT(13) | BIT(14))
545 #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
546 #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
547 #define MVNETA_RXD_ERR_LEN		BIT(18)
548 #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
549 #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
550 #define MVNETA_RXD_L3_IP4		BIT(25)
551 #define MVNETA_RXD_LAST_DESC		BIT(26)
552 #define MVNETA_RXD_FIRST_DESC		BIT(27)
553 #define MVNETA_RXD_FIRST_LAST_DESC	(MVNETA_RXD_FIRST_DESC | \
554 					 MVNETA_RXD_LAST_DESC)
555 #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
556 
557 #if defined(__LITTLE_ENDIAN)
558 struct mvneta_tx_desc {
559 	u32  command;		/* Options used by HW for packet transmitting.*/
560 	u16  reserved1;		/* csum_l4 (for future use)		*/
561 	u16  data_size;		/* Data size of transmitted packet in bytes */
562 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
563 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
564 	u32  reserved3[4];	/* Reserved - (for future use)		*/
565 };
566 
567 struct mvneta_rx_desc {
568 	u32  status;		/* Info about received packet		*/
569 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
570 	u16  data_size;		/* Size of received packet in bytes	*/
571 
572 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
573 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
574 
575 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
576 	u16  reserved3;		/* prefetch_cmd, for future use		*/
577 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
578 
579 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
580 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
581 };
582 #else
583 struct mvneta_tx_desc {
584 	u16  data_size;		/* Data size of transmitted packet in bytes */
585 	u16  reserved1;		/* csum_l4 (for future use)		*/
586 	u32  command;		/* Options used by HW for packet transmitting.*/
587 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
588 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
589 	u32  reserved3[4];	/* Reserved - (for future use)		*/
590 };
591 
592 struct mvneta_rx_desc {
593 	u16  data_size;		/* Size of received packet in bytes	*/
594 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
595 	u32  status;		/* Info about received packet		*/
596 
597 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
598 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
599 
600 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
601 	u16  reserved3;		/* prefetch_cmd, for future use		*/
602 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
603 
604 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
605 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
606 };
607 #endif
608 
609 enum mvneta_tx_buf_type {
610 	MVNETA_TYPE_SKB,
611 	MVNETA_TYPE_XDP_TX,
612 	MVNETA_TYPE_XDP_NDO,
613 };
614 
615 struct mvneta_tx_buf {
616 	enum mvneta_tx_buf_type type;
617 	union {
618 		struct xdp_frame *xdpf;
619 		struct sk_buff *skb;
620 	};
621 };
622 
623 struct mvneta_tx_queue {
624 	/* Number of this TX queue, in the range 0-7 */
625 	u8 id;
626 
627 	/* Number of TX DMA descriptors in the descriptor ring */
628 	int size;
629 
630 	/* Number of currently used TX DMA descriptor in the
631 	 * descriptor ring
632 	 */
633 	int count;
634 	int pending;
635 	int tx_stop_threshold;
636 	int tx_wake_threshold;
637 
638 	/* Array of transmitted buffers */
639 	struct mvneta_tx_buf *buf;
640 
641 	/* Index of last TX DMA descriptor that was inserted */
642 	int txq_put_index;
643 
644 	/* Index of the TX DMA descriptor to be cleaned up */
645 	int txq_get_index;
646 
647 	u32 done_pkts_coal;
648 
649 	/* Virtual address of the TX DMA descriptors array */
650 	struct mvneta_tx_desc *descs;
651 
652 	/* DMA address of the TX DMA descriptors array */
653 	dma_addr_t descs_phys;
654 
655 	/* Index of the last TX DMA descriptor */
656 	int last_desc;
657 
658 	/* Index of the next TX DMA descriptor to process */
659 	int next_desc_to_proc;
660 
661 	/* DMA buffers for TSO headers */
662 	char *tso_hdrs;
663 
664 	/* DMA address of TSO headers */
665 	dma_addr_t tso_hdrs_phys;
666 
667 	/* Affinity mask for CPUs*/
668 	cpumask_t affinity_mask;
669 };
670 
671 struct mvneta_rx_queue {
672 	/* rx queue number, in the range 0-7 */
673 	u8 id;
674 
675 	/* num of rx descriptors in the rx descriptor ring */
676 	int size;
677 
678 	u32 pkts_coal;
679 	u32 time_coal;
680 
681 	/* page_pool */
682 	struct page_pool *page_pool;
683 	struct xdp_rxq_info xdp_rxq;
684 
685 	/* Virtual address of the RX buffer */
686 	void  **buf_virt_addr;
687 
688 	/* Virtual address of the RX DMA descriptors array */
689 	struct mvneta_rx_desc *descs;
690 
691 	/* DMA address of the RX DMA descriptors array */
692 	dma_addr_t descs_phys;
693 
694 	/* Index of the last RX DMA descriptor */
695 	int last_desc;
696 
697 	/* Index of the next RX DMA descriptor to process */
698 	int next_desc_to_proc;
699 
700 	/* Index of first RX DMA descriptor to refill */
701 	int first_to_refill;
702 	u32 refill_num;
703 };
704 
705 static enum cpuhp_state online_hpstate;
706 /* The hardware supports eight (8) rx queues, but we are only allowing
707  * the first one to be used. Therefore, let's just allocate one queue.
708  */
709 static int rxq_number = 8;
710 static int txq_number = 8;
711 
712 static int rxq_def;
713 
714 static int rx_copybreak __read_mostly = 256;
715 
716 /* HW BM need that each port be identify by a unique ID */
717 static int global_port_id;
718 
719 #define MVNETA_DRIVER_NAME "mvneta"
720 #define MVNETA_DRIVER_VERSION "1.0"
721 
722 /* Utility/helper methods */
723 
724 /* Write helper method */
725 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
726 {
727 	writel(data, pp->base + offset);
728 }
729 
730 /* Read helper method */
731 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
732 {
733 	return readl(pp->base + offset);
734 }
735 
736 /* Increment txq get counter */
737 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
738 {
739 	txq->txq_get_index++;
740 	if (txq->txq_get_index == txq->size)
741 		txq->txq_get_index = 0;
742 }
743 
744 /* Increment txq put counter */
745 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
746 {
747 	txq->txq_put_index++;
748 	if (txq->txq_put_index == txq->size)
749 		txq->txq_put_index = 0;
750 }
751 
752 
753 /* Clear all MIB counters */
754 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
755 {
756 	int i;
757 
758 	/* Perform dummy reads from MIB counters */
759 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
760 		mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
761 	mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
762 	mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
763 }
764 
765 /* Get System Network Statistics */
766 static void
767 mvneta_get_stats64(struct net_device *dev,
768 		   struct rtnl_link_stats64 *stats)
769 {
770 	struct mvneta_port *pp = netdev_priv(dev);
771 	unsigned int start;
772 	int cpu;
773 
774 	for_each_possible_cpu(cpu) {
775 		struct mvneta_pcpu_stats *cpu_stats;
776 		u64 rx_packets;
777 		u64 rx_bytes;
778 		u64 rx_dropped;
779 		u64 rx_errors;
780 		u64 tx_packets;
781 		u64 tx_bytes;
782 
783 		cpu_stats = per_cpu_ptr(pp->stats, cpu);
784 		do {
785 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
786 			rx_packets = cpu_stats->es.ps.rx_packets;
787 			rx_bytes   = cpu_stats->es.ps.rx_bytes;
788 			rx_dropped = cpu_stats->rx_dropped;
789 			rx_errors  = cpu_stats->rx_errors;
790 			tx_packets = cpu_stats->es.ps.tx_packets;
791 			tx_bytes   = cpu_stats->es.ps.tx_bytes;
792 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
793 
794 		stats->rx_packets += rx_packets;
795 		stats->rx_bytes   += rx_bytes;
796 		stats->rx_dropped += rx_dropped;
797 		stats->rx_errors  += rx_errors;
798 		stats->tx_packets += tx_packets;
799 		stats->tx_bytes   += tx_bytes;
800 	}
801 
802 	stats->tx_dropped	= dev->stats.tx_dropped;
803 }
804 
805 /* Rx descriptors helper methods */
806 
807 /* Checks whether the RX descriptor having this status is both the first
808  * and the last descriptor for the RX packet. Each RX packet is currently
809  * received through a single RX descriptor, so not having each RX
810  * descriptor with its first and last bits set is an error
811  */
812 static int mvneta_rxq_desc_is_first_last(u32 status)
813 {
814 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
815 		MVNETA_RXD_FIRST_LAST_DESC;
816 }
817 
818 /* Add number of descriptors ready to receive new packets */
819 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
820 					  struct mvneta_rx_queue *rxq,
821 					  int ndescs)
822 {
823 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
824 	 * be added at once
825 	 */
826 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
827 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
828 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
829 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
830 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
831 	}
832 
833 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
834 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
835 }
836 
837 /* Get number of RX descriptors occupied by received packets */
838 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
839 					struct mvneta_rx_queue *rxq)
840 {
841 	u32 val;
842 
843 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
844 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
845 }
846 
847 /* Update num of rx desc called upon return from rx path or
848  * from mvneta_rxq_drop_pkts().
849  */
850 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
851 				       struct mvneta_rx_queue *rxq,
852 				       int rx_done, int rx_filled)
853 {
854 	u32 val;
855 
856 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
857 		val = rx_done |
858 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
859 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
860 		return;
861 	}
862 
863 	/* Only 255 descriptors can be added at once */
864 	while ((rx_done > 0) || (rx_filled > 0)) {
865 		if (rx_done <= 0xff) {
866 			val = rx_done;
867 			rx_done = 0;
868 		} else {
869 			val = 0xff;
870 			rx_done -= 0xff;
871 		}
872 		if (rx_filled <= 0xff) {
873 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
874 			rx_filled = 0;
875 		} else {
876 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
877 			rx_filled -= 0xff;
878 		}
879 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
880 	}
881 }
882 
883 /* Get pointer to next RX descriptor to be processed by SW */
884 static struct mvneta_rx_desc *
885 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
886 {
887 	int rx_desc = rxq->next_desc_to_proc;
888 
889 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
890 	prefetch(rxq->descs + rxq->next_desc_to_proc);
891 	return rxq->descs + rx_desc;
892 }
893 
894 /* Change maximum receive size of the port. */
895 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
896 {
897 	u32 val;
898 
899 	val =  mvreg_read(pp, MVNETA_GMAC_CTRL_0);
900 	val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
901 	val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
902 		MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
903 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
904 }
905 
906 
907 /* Set rx queue offset */
908 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
909 				  struct mvneta_rx_queue *rxq,
910 				  int offset)
911 {
912 	u32 val;
913 
914 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
915 	val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
916 
917 	/* Offset is in */
918 	val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
919 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
920 }
921 
922 
923 /* Tx descriptors helper methods */
924 
925 /* Update HW with number of TX descriptors to be sent */
926 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
927 				     struct mvneta_tx_queue *txq,
928 				     int pend_desc)
929 {
930 	u32 val;
931 
932 	pend_desc += txq->pending;
933 
934 	/* Only 255 Tx descriptors can be added at once */
935 	do {
936 		val = min(pend_desc, 255);
937 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
938 		pend_desc -= val;
939 	} while (pend_desc > 0);
940 	txq->pending = 0;
941 }
942 
943 /* Get pointer to next TX descriptor to be processed (send) by HW */
944 static struct mvneta_tx_desc *
945 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
946 {
947 	int tx_desc = txq->next_desc_to_proc;
948 
949 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
950 	return txq->descs + tx_desc;
951 }
952 
953 /* Release the last allocated TX descriptor. Useful to handle DMA
954  * mapping failures in the TX path.
955  */
956 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
957 {
958 	if (txq->next_desc_to_proc == 0)
959 		txq->next_desc_to_proc = txq->last_desc - 1;
960 	else
961 		txq->next_desc_to_proc--;
962 }
963 
964 /* Set rxq buf size */
965 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
966 				    struct mvneta_rx_queue *rxq,
967 				    int buf_size)
968 {
969 	u32 val;
970 
971 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
972 
973 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
974 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
975 
976 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
977 }
978 
979 /* Disable buffer management (BM) */
980 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
981 				  struct mvneta_rx_queue *rxq)
982 {
983 	u32 val;
984 
985 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
986 	val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
987 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
988 }
989 
990 /* Enable buffer management (BM) */
991 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
992 				 struct mvneta_rx_queue *rxq)
993 {
994 	u32 val;
995 
996 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
997 	val |= MVNETA_RXQ_HW_BUF_ALLOC;
998 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
999 }
1000 
1001 /* Notify HW about port's assignment of pool for bigger packets */
1002 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
1003 				     struct mvneta_rx_queue *rxq)
1004 {
1005 	u32 val;
1006 
1007 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1008 	val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
1009 	val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
1010 
1011 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1012 }
1013 
1014 /* Notify HW about port's assignment of pool for smaller packets */
1015 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
1016 				      struct mvneta_rx_queue *rxq)
1017 {
1018 	u32 val;
1019 
1020 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
1021 	val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
1022 	val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
1023 
1024 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
1025 }
1026 
1027 /* Set port's receive buffer size for assigned BM pool */
1028 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
1029 					      int buf_size,
1030 					      u8 pool_id)
1031 {
1032 	u32 val;
1033 
1034 	if (!IS_ALIGNED(buf_size, 8)) {
1035 		dev_warn(pp->dev->dev.parent,
1036 			 "illegal buf_size value %d, round to %d\n",
1037 			 buf_size, ALIGN(buf_size, 8));
1038 		buf_size = ALIGN(buf_size, 8);
1039 	}
1040 
1041 	val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
1042 	val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
1043 	mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
1044 }
1045 
1046 /* Configure MBUS window in order to enable access BM internal SRAM */
1047 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
1048 				  u8 target, u8 attr)
1049 {
1050 	u32 win_enable, win_protect;
1051 	int i;
1052 
1053 	win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
1054 
1055 	if (pp->bm_win_id < 0) {
1056 		/* Find first not occupied window */
1057 		for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
1058 			if (win_enable & (1 << i)) {
1059 				pp->bm_win_id = i;
1060 				break;
1061 			}
1062 		}
1063 		if (i == MVNETA_MAX_DECODE_WIN)
1064 			return -ENOMEM;
1065 	} else {
1066 		i = pp->bm_win_id;
1067 	}
1068 
1069 	mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1070 	mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1071 
1072 	if (i < 4)
1073 		mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1074 
1075 	mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
1076 		    (attr << 8) | target);
1077 
1078 	mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
1079 
1080 	win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
1081 	win_protect |= 3 << (2 * i);
1082 	mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
1083 
1084 	win_enable &= ~(1 << i);
1085 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1086 
1087 	return 0;
1088 }
1089 
1090 static int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
1091 {
1092 	u32 wsize;
1093 	u8 target, attr;
1094 	int err;
1095 
1096 	/* Get BM window information */
1097 	err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
1098 					 &target, &attr);
1099 	if (err < 0)
1100 		return err;
1101 
1102 	pp->bm_win_id = -1;
1103 
1104 	/* Open NETA -> BM window */
1105 	err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
1106 				     target, attr);
1107 	if (err < 0) {
1108 		netdev_info(pp->dev, "fail to configure mbus window to BM\n");
1109 		return err;
1110 	}
1111 	return 0;
1112 }
1113 
1114 /* Assign and initialize pools for port. In case of fail
1115  * buffer manager will remain disabled for current port.
1116  */
1117 static int mvneta_bm_port_init(struct platform_device *pdev,
1118 			       struct mvneta_port *pp)
1119 {
1120 	struct device_node *dn = pdev->dev.of_node;
1121 	u32 long_pool_id, short_pool_id;
1122 
1123 	if (!pp->neta_armada3700) {
1124 		int ret;
1125 
1126 		ret = mvneta_bm_port_mbus_init(pp);
1127 		if (ret)
1128 			return ret;
1129 	}
1130 
1131 	if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1132 		netdev_info(pp->dev, "missing long pool id\n");
1133 		return -EINVAL;
1134 	}
1135 
1136 	/* Create port's long pool depending on mtu */
1137 	pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1138 					   MVNETA_BM_LONG, pp->id,
1139 					   MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1140 	if (!pp->pool_long) {
1141 		netdev_info(pp->dev, "fail to obtain long pool for port\n");
1142 		return -ENOMEM;
1143 	}
1144 
1145 	pp->pool_long->port_map |= 1 << pp->id;
1146 
1147 	mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1148 				   pp->pool_long->id);
1149 
1150 	/* If short pool id is not defined, assume using single pool */
1151 	if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1152 		short_pool_id = long_pool_id;
1153 
1154 	/* Create port's short pool */
1155 	pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1156 					    MVNETA_BM_SHORT, pp->id,
1157 					    MVNETA_BM_SHORT_PKT_SIZE);
1158 	if (!pp->pool_short) {
1159 		netdev_info(pp->dev, "fail to obtain short pool for port\n");
1160 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1161 		return -ENOMEM;
1162 	}
1163 
1164 	if (short_pool_id != long_pool_id) {
1165 		pp->pool_short->port_map |= 1 << pp->id;
1166 		mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1167 					   pp->pool_short->id);
1168 	}
1169 
1170 	return 0;
1171 }
1172 
1173 /* Update settings of a pool for bigger packets */
1174 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1175 {
1176 	struct mvneta_bm_pool *bm_pool = pp->pool_long;
1177 	struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1178 	int num;
1179 
1180 	/* Release all buffers from long pool */
1181 	mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1182 	if (hwbm_pool->buf_num) {
1183 		WARN(1, "cannot free all buffers in pool %d\n",
1184 		     bm_pool->id);
1185 		goto bm_mtu_err;
1186 	}
1187 
1188 	bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1189 	bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1190 	hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1191 			SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1192 
1193 	/* Fill entire long pool */
1194 	num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
1195 	if (num != hwbm_pool->size) {
1196 		WARN(1, "pool %d: %d of %d allocated\n",
1197 		     bm_pool->id, num, hwbm_pool->size);
1198 		goto bm_mtu_err;
1199 	}
1200 	mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1201 
1202 	return;
1203 
1204 bm_mtu_err:
1205 	mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1206 	mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1207 
1208 	pp->bm_priv = NULL;
1209 	pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
1210 	mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1211 	netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1212 }
1213 
1214 /* Start the Ethernet port RX and TX activity */
1215 static void mvneta_port_up(struct mvneta_port *pp)
1216 {
1217 	int queue;
1218 	u32 q_map;
1219 
1220 	/* Enable all initialized TXs. */
1221 	q_map = 0;
1222 	for (queue = 0; queue < txq_number; queue++) {
1223 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
1224 		if (txq->descs)
1225 			q_map |= (1 << queue);
1226 	}
1227 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1228 
1229 	q_map = 0;
1230 	/* Enable all initialized RXQs. */
1231 	for (queue = 0; queue < rxq_number; queue++) {
1232 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1233 
1234 		if (rxq->descs)
1235 			q_map |= (1 << queue);
1236 	}
1237 	mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1238 }
1239 
1240 /* Stop the Ethernet port activity */
1241 static void mvneta_port_down(struct mvneta_port *pp)
1242 {
1243 	u32 val;
1244 	int count;
1245 
1246 	/* Stop Rx port activity. Check port Rx activity. */
1247 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1248 
1249 	/* Issue stop command for active channels only */
1250 	if (val != 0)
1251 		mvreg_write(pp, MVNETA_RXQ_CMD,
1252 			    val << MVNETA_RXQ_DISABLE_SHIFT);
1253 
1254 	/* Wait for all Rx activity to terminate. */
1255 	count = 0;
1256 	do {
1257 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1258 			netdev_warn(pp->dev,
1259 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1260 				    val);
1261 			break;
1262 		}
1263 		mdelay(1);
1264 
1265 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
1266 	} while (val & MVNETA_RXQ_ENABLE_MASK);
1267 
1268 	/* Stop Tx port activity. Check port Tx activity. Issue stop
1269 	 * command for active channels only
1270 	 */
1271 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1272 
1273 	if (val != 0)
1274 		mvreg_write(pp, MVNETA_TXQ_CMD,
1275 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
1276 
1277 	/* Wait for all Tx activity to terminate. */
1278 	count = 0;
1279 	do {
1280 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1281 			netdev_warn(pp->dev,
1282 				    "TIMEOUT for TX stopped status=0x%08x\n",
1283 				    val);
1284 			break;
1285 		}
1286 		mdelay(1);
1287 
1288 		/* Check TX Command reg that all Txqs are stopped */
1289 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
1290 
1291 	} while (val & MVNETA_TXQ_ENABLE_MASK);
1292 
1293 	/* Double check to verify that TX FIFO is empty */
1294 	count = 0;
1295 	do {
1296 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1297 			netdev_warn(pp->dev,
1298 				    "TX FIFO empty timeout status=0x%08x\n",
1299 				    val);
1300 			break;
1301 		}
1302 		mdelay(1);
1303 
1304 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
1305 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1306 		 (val & MVNETA_TX_IN_PRGRS));
1307 
1308 	udelay(200);
1309 }
1310 
1311 /* Enable the port by setting the port enable bit of the MAC control register */
1312 static void mvneta_port_enable(struct mvneta_port *pp)
1313 {
1314 	u32 val;
1315 
1316 	/* Enable port */
1317 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1318 	val |= MVNETA_GMAC0_PORT_ENABLE;
1319 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1320 }
1321 
1322 /* Disable the port and wait for about 200 usec before retuning */
1323 static void mvneta_port_disable(struct mvneta_port *pp)
1324 {
1325 	u32 val;
1326 
1327 	/* Reset the Enable bit in the Serial Control Register */
1328 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1329 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
1330 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1331 
1332 	udelay(200);
1333 }
1334 
1335 /* Multicast tables methods */
1336 
1337 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1338 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1339 {
1340 	int offset;
1341 	u32 val;
1342 
1343 	if (queue == -1) {
1344 		val = 0;
1345 	} else {
1346 		val = 0x1 | (queue << 1);
1347 		val |= (val << 24) | (val << 16) | (val << 8);
1348 	}
1349 
1350 	for (offset = 0; offset <= 0xc; offset += 4)
1351 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1352 }
1353 
1354 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1355 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1356 {
1357 	int offset;
1358 	u32 val;
1359 
1360 	if (queue == -1) {
1361 		val = 0;
1362 	} else {
1363 		val = 0x1 | (queue << 1);
1364 		val |= (val << 24) | (val << 16) | (val << 8);
1365 	}
1366 
1367 	for (offset = 0; offset <= 0xfc; offset += 4)
1368 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1369 
1370 }
1371 
1372 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1373 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1374 {
1375 	int offset;
1376 	u32 val;
1377 
1378 	if (queue == -1) {
1379 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1380 		val = 0;
1381 	} else {
1382 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1383 		val = 0x1 | (queue << 1);
1384 		val |= (val << 24) | (val << 16) | (val << 8);
1385 	}
1386 
1387 	for (offset = 0; offset <= 0xfc; offset += 4)
1388 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1389 }
1390 
1391 static void mvneta_percpu_unmask_interrupt(void *arg)
1392 {
1393 	struct mvneta_port *pp = arg;
1394 
1395 	/* All the queue are unmasked, but actually only the ones
1396 	 * mapped to this CPU will be unmasked
1397 	 */
1398 	mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1399 		    MVNETA_RX_INTR_MASK_ALL |
1400 		    MVNETA_TX_INTR_MASK_ALL |
1401 		    MVNETA_MISCINTR_INTR_MASK);
1402 }
1403 
1404 static void mvneta_percpu_mask_interrupt(void *arg)
1405 {
1406 	struct mvneta_port *pp = arg;
1407 
1408 	/* All the queue are masked, but actually only the ones
1409 	 * mapped to this CPU will be masked
1410 	 */
1411 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1412 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1413 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1414 }
1415 
1416 static void mvneta_percpu_clear_intr_cause(void *arg)
1417 {
1418 	struct mvneta_port *pp = arg;
1419 
1420 	/* All the queue are cleared, but actually only the ones
1421 	 * mapped to this CPU will be cleared
1422 	 */
1423 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1424 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1425 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1426 }
1427 
1428 /* This method sets defaults to the NETA port:
1429  *	Clears interrupt Cause and Mask registers.
1430  *	Clears all MAC tables.
1431  *	Sets defaults to all registers.
1432  *	Resets RX and TX descriptor rings.
1433  *	Resets PHY.
1434  * This method can be called after mvneta_port_down() to return the port
1435  *	settings to defaults.
1436  */
1437 static void mvneta_defaults_set(struct mvneta_port *pp)
1438 {
1439 	int cpu;
1440 	int queue;
1441 	u32 val;
1442 	int max_cpu = num_present_cpus();
1443 
1444 	/* Clear all Cause registers */
1445 	on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1446 
1447 	/* Mask all interrupts */
1448 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1449 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1450 
1451 	/* Enable MBUS Retry bit16 */
1452 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1453 
1454 	/* Set CPU queue access map. CPUs are assigned to the RX and
1455 	 * TX queues modulo their number. If there is only one TX
1456 	 * queue then it is assigned to the CPU associated to the
1457 	 * default RX queue.
1458 	 */
1459 	for_each_present_cpu(cpu) {
1460 		int rxq_map = 0, txq_map = 0;
1461 		int rxq, txq;
1462 		if (!pp->neta_armada3700) {
1463 			for (rxq = 0; rxq < rxq_number; rxq++)
1464 				if ((rxq % max_cpu) == cpu)
1465 					rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1466 
1467 			for (txq = 0; txq < txq_number; txq++)
1468 				if ((txq % max_cpu) == cpu)
1469 					txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1470 
1471 			/* With only one TX queue we configure a special case
1472 			 * which will allow to get all the irq on a single
1473 			 * CPU
1474 			 */
1475 			if (txq_number == 1)
1476 				txq_map = (cpu == pp->rxq_def) ?
1477 					MVNETA_CPU_TXQ_ACCESS(1) : 0;
1478 
1479 		} else {
1480 			txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1481 			rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1482 		}
1483 
1484 		mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1485 	}
1486 
1487 	/* Reset RX and TX DMAs */
1488 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1489 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1490 
1491 	/* Disable Legacy WRR, Disable EJP, Release from reset */
1492 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1493 	for (queue = 0; queue < txq_number; queue++) {
1494 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1495 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1496 	}
1497 
1498 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1499 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1500 
1501 	/* Set Port Acceleration Mode */
1502 	if (pp->bm_priv)
1503 		/* HW buffer management + legacy parser */
1504 		val = MVNETA_ACC_MODE_EXT2;
1505 	else
1506 		/* SW buffer management + legacy parser */
1507 		val = MVNETA_ACC_MODE_EXT1;
1508 	mvreg_write(pp, MVNETA_ACC_MODE, val);
1509 
1510 	if (pp->bm_priv)
1511 		mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1512 
1513 	/* Update val of portCfg register accordingly with all RxQueue types */
1514 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1515 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1516 
1517 	val = 0;
1518 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1519 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1520 
1521 	/* Build PORT_SDMA_CONFIG_REG */
1522 	val = 0;
1523 
1524 	/* Default burst size */
1525 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1526 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1527 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1528 
1529 #if defined(__BIG_ENDIAN)
1530 	val |= MVNETA_DESC_SWAP;
1531 #endif
1532 
1533 	/* Assign port SDMA configuration */
1534 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1535 
1536 	/* Disable PHY polling in hardware, since we're using the
1537 	 * kernel phylib to do this.
1538 	 */
1539 	val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1540 	val &= ~MVNETA_PHY_POLLING_ENABLE;
1541 	mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1542 
1543 	mvneta_set_ucast_table(pp, -1);
1544 	mvneta_set_special_mcast_table(pp, -1);
1545 	mvneta_set_other_mcast_table(pp, -1);
1546 
1547 	/* Set port interrupt enable register - default enable all */
1548 	mvreg_write(pp, MVNETA_INTR_ENABLE,
1549 		    (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1550 		     | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1551 
1552 	mvneta_mib_counters_clear(pp);
1553 }
1554 
1555 /* Set max sizes for tx queues */
1556 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1557 
1558 {
1559 	u32 val, size, mtu;
1560 	int queue;
1561 
1562 	mtu = max_tx_size * 8;
1563 	if (mtu > MVNETA_TX_MTU_MAX)
1564 		mtu = MVNETA_TX_MTU_MAX;
1565 
1566 	/* Set MTU */
1567 	val = mvreg_read(pp, MVNETA_TX_MTU);
1568 	val &= ~MVNETA_TX_MTU_MAX;
1569 	val |= mtu;
1570 	mvreg_write(pp, MVNETA_TX_MTU, val);
1571 
1572 	/* TX token size and all TXQs token size must be larger that MTU */
1573 	val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1574 
1575 	size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1576 	if (size < mtu) {
1577 		size = mtu;
1578 		val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1579 		val |= size;
1580 		mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1581 	}
1582 	for (queue = 0; queue < txq_number; queue++) {
1583 		val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1584 
1585 		size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1586 		if (size < mtu) {
1587 			size = mtu;
1588 			val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1589 			val |= size;
1590 			mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1591 		}
1592 	}
1593 }
1594 
1595 /* Set unicast address */
1596 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1597 				  int queue)
1598 {
1599 	unsigned int unicast_reg;
1600 	unsigned int tbl_offset;
1601 	unsigned int reg_offset;
1602 
1603 	/* Locate the Unicast table entry */
1604 	last_nibble = (0xf & last_nibble);
1605 
1606 	/* offset from unicast tbl base */
1607 	tbl_offset = (last_nibble / 4) * 4;
1608 
1609 	/* offset within the above reg  */
1610 	reg_offset = last_nibble % 4;
1611 
1612 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1613 
1614 	if (queue == -1) {
1615 		/* Clear accepts frame bit at specified unicast DA tbl entry */
1616 		unicast_reg &= ~(0xff << (8 * reg_offset));
1617 	} else {
1618 		unicast_reg &= ~(0xff << (8 * reg_offset));
1619 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1620 	}
1621 
1622 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1623 }
1624 
1625 /* Set mac address */
1626 static void mvneta_mac_addr_set(struct mvneta_port *pp,
1627 				const unsigned char *addr, int queue)
1628 {
1629 	unsigned int mac_h;
1630 	unsigned int mac_l;
1631 
1632 	if (queue != -1) {
1633 		mac_l = (addr[4] << 8) | (addr[5]);
1634 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
1635 			(addr[2] << 8) | (addr[3] << 0);
1636 
1637 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1638 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1639 	}
1640 
1641 	/* Accept frames of this address */
1642 	mvneta_set_ucast_addr(pp, addr[5], queue);
1643 }
1644 
1645 /* Set the number of packets that will be received before RX interrupt
1646  * will be generated by HW.
1647  */
1648 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1649 				    struct mvneta_rx_queue *rxq, u32 value)
1650 {
1651 	mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1652 		    value | MVNETA_RXQ_NON_OCCUPIED(0));
1653 }
1654 
1655 /* Set the time delay in usec before RX interrupt will be generated by
1656  * HW.
1657  */
1658 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1659 				    struct mvneta_rx_queue *rxq, u32 value)
1660 {
1661 	u32 val;
1662 	unsigned long clk_rate;
1663 
1664 	clk_rate = clk_get_rate(pp->clk);
1665 	val = (clk_rate / 1000000) * value;
1666 
1667 	mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1668 }
1669 
1670 /* Set threshold for TX_DONE pkts coalescing */
1671 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1672 					 struct mvneta_tx_queue *txq, u32 value)
1673 {
1674 	u32 val;
1675 
1676 	val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1677 
1678 	val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1679 	val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1680 
1681 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1682 }
1683 
1684 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1685 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1686 				u32 phys_addr, void *virt_addr,
1687 				struct mvneta_rx_queue *rxq)
1688 {
1689 	int i;
1690 
1691 	rx_desc->buf_phys_addr = phys_addr;
1692 	i = rx_desc - rxq->descs;
1693 	rxq->buf_virt_addr[i] = virt_addr;
1694 }
1695 
1696 /* Decrement sent descriptors counter */
1697 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1698 				     struct mvneta_tx_queue *txq,
1699 				     int sent_desc)
1700 {
1701 	u32 val;
1702 
1703 	/* Only 255 TX descriptors can be updated at once */
1704 	while (sent_desc > 0xff) {
1705 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1706 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1707 		sent_desc = sent_desc - 0xff;
1708 	}
1709 
1710 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1711 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1712 }
1713 
1714 /* Get number of TX descriptors already sent by HW */
1715 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1716 					struct mvneta_tx_queue *txq)
1717 {
1718 	u32 val;
1719 	int sent_desc;
1720 
1721 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1722 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1723 		MVNETA_TXQ_SENT_DESC_SHIFT;
1724 
1725 	return sent_desc;
1726 }
1727 
1728 /* Get number of sent descriptors and decrement counter.
1729  *  The number of sent descriptors is returned.
1730  */
1731 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1732 				     struct mvneta_tx_queue *txq)
1733 {
1734 	int sent_desc;
1735 
1736 	/* Get number of sent descriptors */
1737 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1738 
1739 	/* Decrement sent descriptors counter */
1740 	if (sent_desc)
1741 		mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1742 
1743 	return sent_desc;
1744 }
1745 
1746 /* Set TXQ descriptors fields relevant for CSUM calculation */
1747 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1748 				int ip_hdr_len, int l4_proto)
1749 {
1750 	u32 command;
1751 
1752 	/* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1753 	 * G_L4_chk, L4_type; required only for checksum
1754 	 * calculation
1755 	 */
1756 	command =  l3_offs    << MVNETA_TX_L3_OFF_SHIFT;
1757 	command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1758 
1759 	if (l3_proto == htons(ETH_P_IP))
1760 		command |= MVNETA_TXD_IP_CSUM;
1761 	else
1762 		command |= MVNETA_TX_L3_IP6;
1763 
1764 	if (l4_proto == IPPROTO_TCP)
1765 		command |=  MVNETA_TX_L4_CSUM_FULL;
1766 	else if (l4_proto == IPPROTO_UDP)
1767 		command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1768 	else
1769 		command |= MVNETA_TX_L4_CSUM_NOT;
1770 
1771 	return command;
1772 }
1773 
1774 
1775 /* Display more error info */
1776 static void mvneta_rx_error(struct mvneta_port *pp,
1777 			    struct mvneta_rx_desc *rx_desc)
1778 {
1779 	struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1780 	u32 status = rx_desc->status;
1781 
1782 	/* update per-cpu counter */
1783 	u64_stats_update_begin(&stats->syncp);
1784 	stats->rx_errors++;
1785 	u64_stats_update_end(&stats->syncp);
1786 
1787 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1788 	case MVNETA_RXD_ERR_CRC:
1789 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1790 			   status, rx_desc->data_size);
1791 		break;
1792 	case MVNETA_RXD_ERR_OVERRUN:
1793 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1794 			   status, rx_desc->data_size);
1795 		break;
1796 	case MVNETA_RXD_ERR_LEN:
1797 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1798 			   status, rx_desc->data_size);
1799 		break;
1800 	case MVNETA_RXD_ERR_RESOURCE:
1801 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1802 			   status, rx_desc->data_size);
1803 		break;
1804 	}
1805 }
1806 
1807 /* Handle RX checksum offload based on the descriptor's status */
1808 static int mvneta_rx_csum(struct mvneta_port *pp, u32 status)
1809 {
1810 	if ((pp->dev->features & NETIF_F_RXCSUM) &&
1811 	    (status & MVNETA_RXD_L3_IP4) &&
1812 	    (status & MVNETA_RXD_L4_CSUM_OK))
1813 		return CHECKSUM_UNNECESSARY;
1814 
1815 	return CHECKSUM_NONE;
1816 }
1817 
1818 /* Return tx queue pointer (find last set bit) according to <cause> returned
1819  * form tx_done reg. <cause> must not be null. The return value is always a
1820  * valid queue for matching the first one found in <cause>.
1821  */
1822 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1823 						     u32 cause)
1824 {
1825 	int queue = fls(cause) - 1;
1826 
1827 	return &pp->txqs[queue];
1828 }
1829 
1830 /* Free tx queue skbuffs */
1831 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1832 				 struct mvneta_tx_queue *txq, int num,
1833 				 struct netdev_queue *nq, bool napi)
1834 {
1835 	unsigned int bytes_compl = 0, pkts_compl = 0;
1836 	struct xdp_frame_bulk bq;
1837 	int i;
1838 
1839 	xdp_frame_bulk_init(&bq);
1840 
1841 	rcu_read_lock(); /* need for xdp_return_frame_bulk */
1842 
1843 	for (i = 0; i < num; i++) {
1844 		struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
1845 		struct mvneta_tx_desc *tx_desc = txq->descs +
1846 			txq->txq_get_index;
1847 
1848 		mvneta_txq_inc_get(txq);
1849 
1850 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr) &&
1851 		    buf->type != MVNETA_TYPE_XDP_TX)
1852 			dma_unmap_single(pp->dev->dev.parent,
1853 					 tx_desc->buf_phys_addr,
1854 					 tx_desc->data_size, DMA_TO_DEVICE);
1855 		if (buf->type == MVNETA_TYPE_SKB && buf->skb) {
1856 			bytes_compl += buf->skb->len;
1857 			pkts_compl++;
1858 			dev_kfree_skb_any(buf->skb);
1859 		} else if (buf->type == MVNETA_TYPE_XDP_TX ||
1860 			   buf->type == MVNETA_TYPE_XDP_NDO) {
1861 			if (napi && buf->type == MVNETA_TYPE_XDP_TX)
1862 				xdp_return_frame_rx_napi(buf->xdpf);
1863 			else
1864 				xdp_return_frame_bulk(buf->xdpf, &bq);
1865 		}
1866 	}
1867 	xdp_flush_frame_bulk(&bq);
1868 
1869 	rcu_read_unlock();
1870 
1871 	netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1872 }
1873 
1874 /* Handle end of transmission */
1875 static void mvneta_txq_done(struct mvneta_port *pp,
1876 			   struct mvneta_tx_queue *txq)
1877 {
1878 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1879 	int tx_done;
1880 
1881 	tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1882 	if (!tx_done)
1883 		return;
1884 
1885 	mvneta_txq_bufs_free(pp, txq, tx_done, nq, true);
1886 
1887 	txq->count -= tx_done;
1888 
1889 	if (netif_tx_queue_stopped(nq)) {
1890 		if (txq->count <= txq->tx_wake_threshold)
1891 			netif_tx_wake_queue(nq);
1892 	}
1893 }
1894 
1895 /* Refill processing for SW buffer management */
1896 /* Allocate page per descriptor */
1897 static int mvneta_rx_refill(struct mvneta_port *pp,
1898 			    struct mvneta_rx_desc *rx_desc,
1899 			    struct mvneta_rx_queue *rxq,
1900 			    gfp_t gfp_mask)
1901 {
1902 	dma_addr_t phys_addr;
1903 	struct page *page;
1904 
1905 	page = page_pool_alloc_pages(rxq->page_pool,
1906 				     gfp_mask | __GFP_NOWARN);
1907 	if (!page)
1908 		return -ENOMEM;
1909 
1910 	phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction;
1911 	mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
1912 
1913 	return 0;
1914 }
1915 
1916 /* Handle tx checksum */
1917 static u32 mvneta_skb_tx_csum(struct sk_buff *skb)
1918 {
1919 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1920 		int ip_hdr_len = 0;
1921 		__be16 l3_proto = vlan_get_protocol(skb);
1922 		u8 l4_proto;
1923 
1924 		if (l3_proto == htons(ETH_P_IP)) {
1925 			struct iphdr *ip4h = ip_hdr(skb);
1926 
1927 			/* Calculate IPv4 checksum and L4 checksum */
1928 			ip_hdr_len = ip4h->ihl;
1929 			l4_proto = ip4h->protocol;
1930 		} else if (l3_proto == htons(ETH_P_IPV6)) {
1931 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
1932 
1933 			/* Read l4_protocol from one of IPv6 extra headers */
1934 			if (skb_network_header_len(skb) > 0)
1935 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
1936 			l4_proto = ip6h->nexthdr;
1937 		} else
1938 			return MVNETA_TX_L4_CSUM_NOT;
1939 
1940 		return mvneta_txq_desc_csum(skb_network_offset(skb),
1941 					    l3_proto, ip_hdr_len, l4_proto);
1942 	}
1943 
1944 	return MVNETA_TX_L4_CSUM_NOT;
1945 }
1946 
1947 /* Drop packets received by the RXQ and free buffers */
1948 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1949 				 struct mvneta_rx_queue *rxq)
1950 {
1951 	int rx_done, i;
1952 
1953 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1954 	if (rx_done)
1955 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1956 
1957 	if (pp->bm_priv) {
1958 		for (i = 0; i < rx_done; i++) {
1959 			struct mvneta_rx_desc *rx_desc =
1960 						  mvneta_rxq_next_desc_get(rxq);
1961 			u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1962 			struct mvneta_bm_pool *bm_pool;
1963 
1964 			bm_pool = &pp->bm_priv->bm_pools[pool_id];
1965 			/* Return dropped buffer to the pool */
1966 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1967 					      rx_desc->buf_phys_addr);
1968 		}
1969 		return;
1970 	}
1971 
1972 	for (i = 0; i < rxq->size; i++) {
1973 		struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1974 		void *data = rxq->buf_virt_addr[i];
1975 		if (!data || !(rx_desc->buf_phys_addr))
1976 			continue;
1977 
1978 		page_pool_put_full_page(rxq->page_pool, data, false);
1979 	}
1980 	if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
1981 		xdp_rxq_info_unreg(&rxq->xdp_rxq);
1982 	page_pool_destroy(rxq->page_pool);
1983 	rxq->page_pool = NULL;
1984 }
1985 
1986 static void
1987 mvneta_update_stats(struct mvneta_port *pp,
1988 		    struct mvneta_stats *ps)
1989 {
1990 	struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1991 
1992 	u64_stats_update_begin(&stats->syncp);
1993 	stats->es.ps.rx_packets += ps->rx_packets;
1994 	stats->es.ps.rx_bytes += ps->rx_bytes;
1995 	/* xdp */
1996 	stats->es.ps.xdp_redirect += ps->xdp_redirect;
1997 	stats->es.ps.xdp_pass += ps->xdp_pass;
1998 	stats->es.ps.xdp_drop += ps->xdp_drop;
1999 	u64_stats_update_end(&stats->syncp);
2000 }
2001 
2002 static inline
2003 int mvneta_rx_refill_queue(struct mvneta_port *pp, struct mvneta_rx_queue *rxq)
2004 {
2005 	struct mvneta_rx_desc *rx_desc;
2006 	int curr_desc = rxq->first_to_refill;
2007 	int i;
2008 
2009 	for (i = 0; (i < rxq->refill_num) && (i < 64); i++) {
2010 		rx_desc = rxq->descs + curr_desc;
2011 		if (!(rx_desc->buf_phys_addr)) {
2012 			if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
2013 				struct mvneta_pcpu_stats *stats;
2014 
2015 				pr_err("Can't refill queue %d. Done %d from %d\n",
2016 				       rxq->id, i, rxq->refill_num);
2017 
2018 				stats = this_cpu_ptr(pp->stats);
2019 				u64_stats_update_begin(&stats->syncp);
2020 				stats->es.refill_error++;
2021 				u64_stats_update_end(&stats->syncp);
2022 				break;
2023 			}
2024 		}
2025 		curr_desc = MVNETA_QUEUE_NEXT_DESC(rxq, curr_desc);
2026 	}
2027 	rxq->refill_num -= i;
2028 	rxq->first_to_refill = curr_desc;
2029 
2030 	return i;
2031 }
2032 
2033 static void
2034 mvneta_xdp_put_buff(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2035 		    struct xdp_buff *xdp, struct skb_shared_info *sinfo,
2036 		    int sync_len)
2037 {
2038 	int i;
2039 
2040 	for (i = 0; i < sinfo->nr_frags; i++)
2041 		page_pool_put_full_page(rxq->page_pool,
2042 					skb_frag_page(&sinfo->frags[i]), true);
2043 	page_pool_put_page(rxq->page_pool, virt_to_head_page(xdp->data),
2044 			   sync_len, true);
2045 }
2046 
2047 static int
2048 mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq,
2049 			struct xdp_frame *xdpf, bool dma_map)
2050 {
2051 	struct mvneta_tx_desc *tx_desc;
2052 	struct mvneta_tx_buf *buf;
2053 	dma_addr_t dma_addr;
2054 
2055 	if (txq->count >= txq->tx_stop_threshold)
2056 		return MVNETA_XDP_DROPPED;
2057 
2058 	tx_desc = mvneta_txq_next_desc_get(txq);
2059 
2060 	buf = &txq->buf[txq->txq_put_index];
2061 	if (dma_map) {
2062 		/* ndo_xdp_xmit */
2063 		dma_addr = dma_map_single(pp->dev->dev.parent, xdpf->data,
2064 					  xdpf->len, DMA_TO_DEVICE);
2065 		if (dma_mapping_error(pp->dev->dev.parent, dma_addr)) {
2066 			mvneta_txq_desc_put(txq);
2067 			return MVNETA_XDP_DROPPED;
2068 		}
2069 		buf->type = MVNETA_TYPE_XDP_NDO;
2070 	} else {
2071 		struct page *page = virt_to_page(xdpf->data);
2072 
2073 		dma_addr = page_pool_get_dma_addr(page) +
2074 			   sizeof(*xdpf) + xdpf->headroom;
2075 		dma_sync_single_for_device(pp->dev->dev.parent, dma_addr,
2076 					   xdpf->len, DMA_BIDIRECTIONAL);
2077 		buf->type = MVNETA_TYPE_XDP_TX;
2078 	}
2079 	buf->xdpf = xdpf;
2080 
2081 	tx_desc->command = MVNETA_TXD_FLZ_DESC;
2082 	tx_desc->buf_phys_addr = dma_addr;
2083 	tx_desc->data_size = xdpf->len;
2084 
2085 	mvneta_txq_inc_put(txq);
2086 	txq->pending++;
2087 	txq->count++;
2088 
2089 	return MVNETA_XDP_TX;
2090 }
2091 
2092 static int
2093 mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp)
2094 {
2095 	struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2096 	struct mvneta_tx_queue *txq;
2097 	struct netdev_queue *nq;
2098 	struct xdp_frame *xdpf;
2099 	int cpu;
2100 	u32 ret;
2101 
2102 	xdpf = xdp_convert_buff_to_frame(xdp);
2103 	if (unlikely(!xdpf))
2104 		return MVNETA_XDP_DROPPED;
2105 
2106 	cpu = smp_processor_id();
2107 	txq = &pp->txqs[cpu % txq_number];
2108 	nq = netdev_get_tx_queue(pp->dev, txq->id);
2109 
2110 	__netif_tx_lock(nq, cpu);
2111 	ret = mvneta_xdp_submit_frame(pp, txq, xdpf, false);
2112 	if (ret == MVNETA_XDP_TX) {
2113 		u64_stats_update_begin(&stats->syncp);
2114 		stats->es.ps.tx_bytes += xdpf->len;
2115 		stats->es.ps.tx_packets++;
2116 		stats->es.ps.xdp_tx++;
2117 		u64_stats_update_end(&stats->syncp);
2118 
2119 		mvneta_txq_pend_desc_add(pp, txq, 0);
2120 	} else {
2121 		u64_stats_update_begin(&stats->syncp);
2122 		stats->es.ps.xdp_tx_err++;
2123 		u64_stats_update_end(&stats->syncp);
2124 	}
2125 	__netif_tx_unlock(nq);
2126 
2127 	return ret;
2128 }
2129 
2130 static int
2131 mvneta_xdp_xmit(struct net_device *dev, int num_frame,
2132 		struct xdp_frame **frames, u32 flags)
2133 {
2134 	struct mvneta_port *pp = netdev_priv(dev);
2135 	struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2136 	int i, nxmit_byte = 0, nxmit = 0;
2137 	int cpu = smp_processor_id();
2138 	struct mvneta_tx_queue *txq;
2139 	struct netdev_queue *nq;
2140 	u32 ret;
2141 
2142 	if (unlikely(test_bit(__MVNETA_DOWN, &pp->state)))
2143 		return -ENETDOWN;
2144 
2145 	if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2146 		return -EINVAL;
2147 
2148 	txq = &pp->txqs[cpu % txq_number];
2149 	nq = netdev_get_tx_queue(pp->dev, txq->id);
2150 
2151 	__netif_tx_lock(nq, cpu);
2152 	for (i = 0; i < num_frame; i++) {
2153 		ret = mvneta_xdp_submit_frame(pp, txq, frames[i], true);
2154 		if (ret != MVNETA_XDP_TX)
2155 			break;
2156 
2157 		nxmit_byte += frames[i]->len;
2158 		nxmit++;
2159 	}
2160 
2161 	if (unlikely(flags & XDP_XMIT_FLUSH))
2162 		mvneta_txq_pend_desc_add(pp, txq, 0);
2163 	__netif_tx_unlock(nq);
2164 
2165 	u64_stats_update_begin(&stats->syncp);
2166 	stats->es.ps.tx_bytes += nxmit_byte;
2167 	stats->es.ps.tx_packets += nxmit;
2168 	stats->es.ps.xdp_xmit += nxmit;
2169 	stats->es.ps.xdp_xmit_err += num_frame - nxmit;
2170 	u64_stats_update_end(&stats->syncp);
2171 
2172 	return nxmit;
2173 }
2174 
2175 static int
2176 mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2177 	       struct bpf_prog *prog, struct xdp_buff *xdp,
2178 	       u32 frame_sz, struct mvneta_stats *stats)
2179 {
2180 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2181 	unsigned int len, data_len, sync;
2182 	u32 ret, act;
2183 
2184 	len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2185 	data_len = xdp->data_end - xdp->data;
2186 	act = bpf_prog_run_xdp(prog, xdp);
2187 
2188 	/* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
2189 	sync = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2190 	sync = max(sync, len);
2191 
2192 	switch (act) {
2193 	case XDP_PASS:
2194 		stats->xdp_pass++;
2195 		return MVNETA_XDP_PASS;
2196 	case XDP_REDIRECT: {
2197 		int err;
2198 
2199 		err = xdp_do_redirect(pp->dev, xdp, prog);
2200 		if (unlikely(err)) {
2201 			mvneta_xdp_put_buff(pp, rxq, xdp, sinfo, sync);
2202 			ret = MVNETA_XDP_DROPPED;
2203 		} else {
2204 			ret = MVNETA_XDP_REDIR;
2205 			stats->xdp_redirect++;
2206 		}
2207 		break;
2208 	}
2209 	case XDP_TX:
2210 		ret = mvneta_xdp_xmit_back(pp, xdp);
2211 		if (ret != MVNETA_XDP_TX)
2212 			mvneta_xdp_put_buff(pp, rxq, xdp, sinfo, sync);
2213 		break;
2214 	default:
2215 		bpf_warn_invalid_xdp_action(act);
2216 		fallthrough;
2217 	case XDP_ABORTED:
2218 		trace_xdp_exception(pp->dev, prog, act);
2219 		fallthrough;
2220 	case XDP_DROP:
2221 		mvneta_xdp_put_buff(pp, rxq, xdp, sinfo, sync);
2222 		ret = MVNETA_XDP_DROPPED;
2223 		stats->xdp_drop++;
2224 		break;
2225 	}
2226 
2227 	stats->rx_bytes += frame_sz + xdp->data_end - xdp->data - data_len;
2228 	stats->rx_packets++;
2229 
2230 	return ret;
2231 }
2232 
2233 static void
2234 mvneta_swbm_rx_frame(struct mvneta_port *pp,
2235 		     struct mvneta_rx_desc *rx_desc,
2236 		     struct mvneta_rx_queue *rxq,
2237 		     struct xdp_buff *xdp, int *size,
2238 		     struct page *page)
2239 {
2240 	unsigned char *data = page_address(page);
2241 	int data_len = -MVNETA_MH_SIZE, len;
2242 	struct net_device *dev = pp->dev;
2243 	enum dma_data_direction dma_dir;
2244 	struct skb_shared_info *sinfo;
2245 
2246 	if (*size > MVNETA_MAX_RX_BUF_SIZE) {
2247 		len = MVNETA_MAX_RX_BUF_SIZE;
2248 		data_len += len;
2249 	} else {
2250 		len = *size;
2251 		data_len += len - ETH_FCS_LEN;
2252 	}
2253 	*size = *size - len;
2254 
2255 	dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2256 	dma_sync_single_for_cpu(dev->dev.parent,
2257 				rx_desc->buf_phys_addr,
2258 				len, dma_dir);
2259 
2260 	rx_desc->buf_phys_addr = 0;
2261 
2262 	/* Prefetch header */
2263 	prefetch(data);
2264 	xdp_prepare_buff(xdp, data, pp->rx_offset_correction + MVNETA_MH_SIZE,
2265 			 data_len, false);
2266 
2267 	sinfo = xdp_get_shared_info_from_buff(xdp);
2268 	sinfo->nr_frags = 0;
2269 }
2270 
2271 static void
2272 mvneta_swbm_add_rx_fragment(struct mvneta_port *pp,
2273 			    struct mvneta_rx_desc *rx_desc,
2274 			    struct mvneta_rx_queue *rxq,
2275 			    struct xdp_buff *xdp, int *size,
2276 			    struct skb_shared_info *xdp_sinfo,
2277 			    struct page *page)
2278 {
2279 	struct net_device *dev = pp->dev;
2280 	enum dma_data_direction dma_dir;
2281 	int data_len, len;
2282 
2283 	if (*size > MVNETA_MAX_RX_BUF_SIZE) {
2284 		len = MVNETA_MAX_RX_BUF_SIZE;
2285 		data_len = len;
2286 	} else {
2287 		len = *size;
2288 		data_len = len - ETH_FCS_LEN;
2289 	}
2290 	dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2291 	dma_sync_single_for_cpu(dev->dev.parent,
2292 				rx_desc->buf_phys_addr,
2293 				len, dma_dir);
2294 	rx_desc->buf_phys_addr = 0;
2295 
2296 	if (data_len > 0 && xdp_sinfo->nr_frags < MAX_SKB_FRAGS) {
2297 		skb_frag_t *frag = &xdp_sinfo->frags[xdp_sinfo->nr_frags++];
2298 
2299 		skb_frag_off_set(frag, pp->rx_offset_correction);
2300 		skb_frag_size_set(frag, data_len);
2301 		__skb_frag_set_page(frag, page);
2302 	} else {
2303 		page_pool_put_full_page(rxq->page_pool, page, true);
2304 	}
2305 
2306 	/* last fragment */
2307 	if (len == *size) {
2308 		struct skb_shared_info *sinfo;
2309 
2310 		sinfo = xdp_get_shared_info_from_buff(xdp);
2311 		sinfo->nr_frags = xdp_sinfo->nr_frags;
2312 		memcpy(sinfo->frags, xdp_sinfo->frags,
2313 		       sinfo->nr_frags * sizeof(skb_frag_t));
2314 	}
2315 	*size -= len;
2316 }
2317 
2318 static struct sk_buff *
2319 mvneta_swbm_build_skb(struct mvneta_port *pp, struct page_pool *pool,
2320 		      struct xdp_buff *xdp, u32 desc_status)
2321 {
2322 	struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2323 	int i, num_frags = sinfo->nr_frags;
2324 	struct sk_buff *skb;
2325 
2326 	skb = build_skb(xdp->data_hard_start, PAGE_SIZE);
2327 	if (!skb)
2328 		return ERR_PTR(-ENOMEM);
2329 
2330 	skb_mark_for_recycle(skb);
2331 
2332 	skb_reserve(skb, xdp->data - xdp->data_hard_start);
2333 	skb_put(skb, xdp->data_end - xdp->data);
2334 	skb->ip_summed = mvneta_rx_csum(pp, desc_status);
2335 
2336 	for (i = 0; i < num_frags; i++) {
2337 		skb_frag_t *frag = &sinfo->frags[i];
2338 
2339 		skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2340 				skb_frag_page(frag), skb_frag_off(frag),
2341 				skb_frag_size(frag), PAGE_SIZE);
2342 	}
2343 
2344 	return skb;
2345 }
2346 
2347 /* Main rx processing when using software buffer management */
2348 static int mvneta_rx_swbm(struct napi_struct *napi,
2349 			  struct mvneta_port *pp, int budget,
2350 			  struct mvneta_rx_queue *rxq)
2351 {
2352 	int rx_proc = 0, rx_todo, refill, size = 0;
2353 	struct net_device *dev = pp->dev;
2354 	struct skb_shared_info sinfo;
2355 	struct mvneta_stats ps = {};
2356 	struct bpf_prog *xdp_prog;
2357 	u32 desc_status, frame_sz;
2358 	struct xdp_buff xdp_buf;
2359 
2360 	xdp_init_buff(&xdp_buf, PAGE_SIZE, &rxq->xdp_rxq);
2361 	xdp_buf.data_hard_start = NULL;
2362 
2363 	sinfo.nr_frags = 0;
2364 
2365 	/* Get number of received packets */
2366 	rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
2367 
2368 	xdp_prog = READ_ONCE(pp->xdp_prog);
2369 
2370 	/* Fairness NAPI loop */
2371 	while (rx_proc < budget && rx_proc < rx_todo) {
2372 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2373 		u32 rx_status, index;
2374 		struct sk_buff *skb;
2375 		struct page *page;
2376 
2377 		index = rx_desc - rxq->descs;
2378 		page = (struct page *)rxq->buf_virt_addr[index];
2379 
2380 		rx_status = rx_desc->status;
2381 		rx_proc++;
2382 		rxq->refill_num++;
2383 
2384 		if (rx_status & MVNETA_RXD_FIRST_DESC) {
2385 			/* Check errors only for FIRST descriptor */
2386 			if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
2387 				mvneta_rx_error(pp, rx_desc);
2388 				goto next;
2389 			}
2390 
2391 			size = rx_desc->data_size;
2392 			frame_sz = size - ETH_FCS_LEN;
2393 			desc_status = rx_status;
2394 
2395 			mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf,
2396 					     &size, page);
2397 		} else {
2398 			if (unlikely(!xdp_buf.data_hard_start)) {
2399 				rx_desc->buf_phys_addr = 0;
2400 				page_pool_put_full_page(rxq->page_pool, page,
2401 							true);
2402 				goto next;
2403 			}
2404 
2405 			mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, &xdp_buf,
2406 						    &size, &sinfo, page);
2407 		} /* Middle or Last descriptor */
2408 
2409 		if (!(rx_status & MVNETA_RXD_LAST_DESC))
2410 			/* no last descriptor this time */
2411 			continue;
2412 
2413 		if (size) {
2414 			mvneta_xdp_put_buff(pp, rxq, &xdp_buf, &sinfo, -1);
2415 			goto next;
2416 		}
2417 
2418 		if (xdp_prog &&
2419 		    mvneta_run_xdp(pp, rxq, xdp_prog, &xdp_buf, frame_sz, &ps))
2420 			goto next;
2421 
2422 		skb = mvneta_swbm_build_skb(pp, rxq->page_pool, &xdp_buf, desc_status);
2423 		if (IS_ERR(skb)) {
2424 			struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2425 
2426 			mvneta_xdp_put_buff(pp, rxq, &xdp_buf, &sinfo, -1);
2427 
2428 			u64_stats_update_begin(&stats->syncp);
2429 			stats->es.skb_alloc_error++;
2430 			stats->rx_dropped++;
2431 			u64_stats_update_end(&stats->syncp);
2432 
2433 			goto next;
2434 		}
2435 
2436 		ps.rx_bytes += skb->len;
2437 		ps.rx_packets++;
2438 
2439 		skb->protocol = eth_type_trans(skb, dev);
2440 		napi_gro_receive(napi, skb);
2441 next:
2442 		xdp_buf.data_hard_start = NULL;
2443 		sinfo.nr_frags = 0;
2444 	}
2445 
2446 	if (xdp_buf.data_hard_start)
2447 		mvneta_xdp_put_buff(pp, rxq, &xdp_buf, &sinfo, -1);
2448 
2449 	if (ps.xdp_redirect)
2450 		xdp_do_flush_map();
2451 
2452 	if (ps.rx_packets)
2453 		mvneta_update_stats(pp, &ps);
2454 
2455 	/* return some buffers to hardware queue, one at a time is too slow */
2456 	refill = mvneta_rx_refill_queue(pp, rxq);
2457 
2458 	/* Update rxq management counters */
2459 	mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
2460 
2461 	return ps.rx_packets;
2462 }
2463 
2464 /* Main rx processing when using hardware buffer management */
2465 static int mvneta_rx_hwbm(struct napi_struct *napi,
2466 			  struct mvneta_port *pp, int rx_todo,
2467 			  struct mvneta_rx_queue *rxq)
2468 {
2469 	struct net_device *dev = pp->dev;
2470 	int rx_done;
2471 	u32 rcvd_pkts = 0;
2472 	u32 rcvd_bytes = 0;
2473 
2474 	/* Get number of received packets */
2475 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2476 
2477 	if (rx_todo > rx_done)
2478 		rx_todo = rx_done;
2479 
2480 	rx_done = 0;
2481 
2482 	/* Fairness NAPI loop */
2483 	while (rx_done < rx_todo) {
2484 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2485 		struct mvneta_bm_pool *bm_pool = NULL;
2486 		struct sk_buff *skb;
2487 		unsigned char *data;
2488 		dma_addr_t phys_addr;
2489 		u32 rx_status, frag_size;
2490 		int rx_bytes, err;
2491 		u8 pool_id;
2492 
2493 		rx_done++;
2494 		rx_status = rx_desc->status;
2495 		rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2496 		data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2497 		phys_addr = rx_desc->buf_phys_addr;
2498 		pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2499 		bm_pool = &pp->bm_priv->bm_pools[pool_id];
2500 
2501 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2502 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2503 err_drop_frame_ret_pool:
2504 			/* Return the buffer to the pool */
2505 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2506 					      rx_desc->buf_phys_addr);
2507 err_drop_frame:
2508 			mvneta_rx_error(pp, rx_desc);
2509 			/* leave the descriptor untouched */
2510 			continue;
2511 		}
2512 
2513 		if (rx_bytes <= rx_copybreak) {
2514 			/* better copy a small frame and not unmap the DMA region */
2515 			skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2516 			if (unlikely(!skb))
2517 				goto err_drop_frame_ret_pool;
2518 
2519 			dma_sync_single_range_for_cpu(&pp->bm_priv->pdev->dev,
2520 			                              rx_desc->buf_phys_addr,
2521 			                              MVNETA_MH_SIZE + NET_SKB_PAD,
2522 			                              rx_bytes,
2523 			                              DMA_FROM_DEVICE);
2524 			skb_put_data(skb, data + MVNETA_MH_SIZE + NET_SKB_PAD,
2525 				     rx_bytes);
2526 
2527 			skb->protocol = eth_type_trans(skb, dev);
2528 			skb->ip_summed = mvneta_rx_csum(pp, rx_status);
2529 			napi_gro_receive(napi, skb);
2530 
2531 			rcvd_pkts++;
2532 			rcvd_bytes += rx_bytes;
2533 
2534 			/* Return the buffer to the pool */
2535 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2536 					      rx_desc->buf_phys_addr);
2537 
2538 			/* leave the descriptor and buffer untouched */
2539 			continue;
2540 		}
2541 
2542 		/* Refill processing */
2543 		err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2544 		if (err) {
2545 			struct mvneta_pcpu_stats *stats;
2546 
2547 			netdev_err(dev, "Linux processing - Can't refill\n");
2548 
2549 			stats = this_cpu_ptr(pp->stats);
2550 			u64_stats_update_begin(&stats->syncp);
2551 			stats->es.refill_error++;
2552 			u64_stats_update_end(&stats->syncp);
2553 
2554 			goto err_drop_frame_ret_pool;
2555 		}
2556 
2557 		frag_size = bm_pool->hwbm_pool.frag_size;
2558 
2559 		skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2560 
2561 		/* After refill old buffer has to be unmapped regardless
2562 		 * the skb is successfully built or not.
2563 		 */
2564 		dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2565 				 bm_pool->buf_size, DMA_FROM_DEVICE);
2566 		if (!skb)
2567 			goto err_drop_frame;
2568 
2569 		rcvd_pkts++;
2570 		rcvd_bytes += rx_bytes;
2571 
2572 		/* Linux processing */
2573 		skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2574 		skb_put(skb, rx_bytes);
2575 
2576 		skb->protocol = eth_type_trans(skb, dev);
2577 		skb->ip_summed = mvneta_rx_csum(pp, rx_status);
2578 
2579 		napi_gro_receive(napi, skb);
2580 	}
2581 
2582 	if (rcvd_pkts) {
2583 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2584 
2585 		u64_stats_update_begin(&stats->syncp);
2586 		stats->es.ps.rx_packets += rcvd_pkts;
2587 		stats->es.ps.rx_bytes += rcvd_bytes;
2588 		u64_stats_update_end(&stats->syncp);
2589 	}
2590 
2591 	/* Update rxq management counters */
2592 	mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2593 
2594 	return rx_done;
2595 }
2596 
2597 static inline void
2598 mvneta_tso_put_hdr(struct sk_buff *skb, struct mvneta_tx_queue *txq)
2599 {
2600 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2601 	struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2602 	struct mvneta_tx_desc *tx_desc;
2603 
2604 	tx_desc = mvneta_txq_next_desc_get(txq);
2605 	tx_desc->data_size = hdr_len;
2606 	tx_desc->command = mvneta_skb_tx_csum(skb);
2607 	tx_desc->command |= MVNETA_TXD_F_DESC;
2608 	tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2609 				 txq->txq_put_index * TSO_HEADER_SIZE;
2610 	buf->type = MVNETA_TYPE_SKB;
2611 	buf->skb = NULL;
2612 
2613 	mvneta_txq_inc_put(txq);
2614 }
2615 
2616 static inline int
2617 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2618 		    struct sk_buff *skb, char *data, int size,
2619 		    bool last_tcp, bool is_last)
2620 {
2621 	struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2622 	struct mvneta_tx_desc *tx_desc;
2623 
2624 	tx_desc = mvneta_txq_next_desc_get(txq);
2625 	tx_desc->data_size = size;
2626 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2627 						size, DMA_TO_DEVICE);
2628 	if (unlikely(dma_mapping_error(dev->dev.parent,
2629 		     tx_desc->buf_phys_addr))) {
2630 		mvneta_txq_desc_put(txq);
2631 		return -ENOMEM;
2632 	}
2633 
2634 	tx_desc->command = 0;
2635 	buf->type = MVNETA_TYPE_SKB;
2636 	buf->skb = NULL;
2637 
2638 	if (last_tcp) {
2639 		/* last descriptor in the TCP packet */
2640 		tx_desc->command = MVNETA_TXD_L_DESC;
2641 
2642 		/* last descriptor in SKB */
2643 		if (is_last)
2644 			buf->skb = skb;
2645 	}
2646 	mvneta_txq_inc_put(txq);
2647 	return 0;
2648 }
2649 
2650 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2651 			 struct mvneta_tx_queue *txq)
2652 {
2653 	int hdr_len, total_len, data_left;
2654 	int desc_count = 0;
2655 	struct mvneta_port *pp = netdev_priv(dev);
2656 	struct tso_t tso;
2657 	int i;
2658 
2659 	/* Count needed descriptors */
2660 	if ((txq->count + tso_count_descs(skb)) >= txq->size)
2661 		return 0;
2662 
2663 	if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2664 		pr_info("*** Is this even possible?\n");
2665 		return 0;
2666 	}
2667 
2668 	/* Initialize the TSO handler, and prepare the first payload */
2669 	hdr_len = tso_start(skb, &tso);
2670 
2671 	total_len = skb->len - hdr_len;
2672 	while (total_len > 0) {
2673 		char *hdr;
2674 
2675 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2676 		total_len -= data_left;
2677 		desc_count++;
2678 
2679 		/* prepare packet headers: MAC + IP + TCP */
2680 		hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2681 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2682 
2683 		mvneta_tso_put_hdr(skb, txq);
2684 
2685 		while (data_left > 0) {
2686 			int size;
2687 			desc_count++;
2688 
2689 			size = min_t(int, tso.size, data_left);
2690 
2691 			if (mvneta_tso_put_data(dev, txq, skb,
2692 						 tso.data, size,
2693 						 size == data_left,
2694 						 total_len == 0))
2695 				goto err_release;
2696 			data_left -= size;
2697 
2698 			tso_build_data(skb, &tso, size);
2699 		}
2700 	}
2701 
2702 	return desc_count;
2703 
2704 err_release:
2705 	/* Release all used data descriptors; header descriptors must not
2706 	 * be DMA-unmapped.
2707 	 */
2708 	for (i = desc_count - 1; i >= 0; i--) {
2709 		struct mvneta_tx_desc *tx_desc = txq->descs + i;
2710 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2711 			dma_unmap_single(pp->dev->dev.parent,
2712 					 tx_desc->buf_phys_addr,
2713 					 tx_desc->data_size,
2714 					 DMA_TO_DEVICE);
2715 		mvneta_txq_desc_put(txq);
2716 	}
2717 	return 0;
2718 }
2719 
2720 /* Handle tx fragmentation processing */
2721 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2722 				  struct mvneta_tx_queue *txq)
2723 {
2724 	struct mvneta_tx_desc *tx_desc;
2725 	int i, nr_frags = skb_shinfo(skb)->nr_frags;
2726 
2727 	for (i = 0; i < nr_frags; i++) {
2728 		struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2729 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2730 		void *addr = skb_frag_address(frag);
2731 
2732 		tx_desc = mvneta_txq_next_desc_get(txq);
2733 		tx_desc->data_size = skb_frag_size(frag);
2734 
2735 		tx_desc->buf_phys_addr =
2736 			dma_map_single(pp->dev->dev.parent, addr,
2737 				       tx_desc->data_size, DMA_TO_DEVICE);
2738 
2739 		if (dma_mapping_error(pp->dev->dev.parent,
2740 				      tx_desc->buf_phys_addr)) {
2741 			mvneta_txq_desc_put(txq);
2742 			goto error;
2743 		}
2744 
2745 		if (i == nr_frags - 1) {
2746 			/* Last descriptor */
2747 			tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2748 			buf->skb = skb;
2749 		} else {
2750 			/* Descriptor in the middle: Not First, Not Last */
2751 			tx_desc->command = 0;
2752 			buf->skb = NULL;
2753 		}
2754 		buf->type = MVNETA_TYPE_SKB;
2755 		mvneta_txq_inc_put(txq);
2756 	}
2757 
2758 	return 0;
2759 
2760 error:
2761 	/* Release all descriptors that were used to map fragments of
2762 	 * this packet, as well as the corresponding DMA mappings
2763 	 */
2764 	for (i = i - 1; i >= 0; i--) {
2765 		tx_desc = txq->descs + i;
2766 		dma_unmap_single(pp->dev->dev.parent,
2767 				 tx_desc->buf_phys_addr,
2768 				 tx_desc->data_size,
2769 				 DMA_TO_DEVICE);
2770 		mvneta_txq_desc_put(txq);
2771 	}
2772 
2773 	return -ENOMEM;
2774 }
2775 
2776 /* Main tx processing */
2777 static netdev_tx_t mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2778 {
2779 	struct mvneta_port *pp = netdev_priv(dev);
2780 	u16 txq_id = skb_get_queue_mapping(skb);
2781 	struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2782 	struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2783 	struct mvneta_tx_desc *tx_desc;
2784 	int len = skb->len;
2785 	int frags = 0;
2786 	u32 tx_cmd;
2787 
2788 	if (!netif_running(dev))
2789 		goto out;
2790 
2791 	if (skb_is_gso(skb)) {
2792 		frags = mvneta_tx_tso(skb, dev, txq);
2793 		goto out;
2794 	}
2795 
2796 	frags = skb_shinfo(skb)->nr_frags + 1;
2797 
2798 	/* Get a descriptor for the first part of the packet */
2799 	tx_desc = mvneta_txq_next_desc_get(txq);
2800 
2801 	tx_cmd = mvneta_skb_tx_csum(skb);
2802 
2803 	tx_desc->data_size = skb_headlen(skb);
2804 
2805 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2806 						tx_desc->data_size,
2807 						DMA_TO_DEVICE);
2808 	if (unlikely(dma_mapping_error(dev->dev.parent,
2809 				       tx_desc->buf_phys_addr))) {
2810 		mvneta_txq_desc_put(txq);
2811 		frags = 0;
2812 		goto out;
2813 	}
2814 
2815 	buf->type = MVNETA_TYPE_SKB;
2816 	if (frags == 1) {
2817 		/* First and Last descriptor */
2818 		tx_cmd |= MVNETA_TXD_FLZ_DESC;
2819 		tx_desc->command = tx_cmd;
2820 		buf->skb = skb;
2821 		mvneta_txq_inc_put(txq);
2822 	} else {
2823 		/* First but not Last */
2824 		tx_cmd |= MVNETA_TXD_F_DESC;
2825 		buf->skb = NULL;
2826 		mvneta_txq_inc_put(txq);
2827 		tx_desc->command = tx_cmd;
2828 		/* Continue with other skb fragments */
2829 		if (mvneta_tx_frag_process(pp, skb, txq)) {
2830 			dma_unmap_single(dev->dev.parent,
2831 					 tx_desc->buf_phys_addr,
2832 					 tx_desc->data_size,
2833 					 DMA_TO_DEVICE);
2834 			mvneta_txq_desc_put(txq);
2835 			frags = 0;
2836 			goto out;
2837 		}
2838 	}
2839 
2840 out:
2841 	if (frags > 0) {
2842 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2843 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2844 
2845 		netdev_tx_sent_queue(nq, len);
2846 
2847 		txq->count += frags;
2848 		if (txq->count >= txq->tx_stop_threshold)
2849 			netif_tx_stop_queue(nq);
2850 
2851 		if (!netdev_xmit_more() || netif_xmit_stopped(nq) ||
2852 		    txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2853 			mvneta_txq_pend_desc_add(pp, txq, frags);
2854 		else
2855 			txq->pending += frags;
2856 
2857 		u64_stats_update_begin(&stats->syncp);
2858 		stats->es.ps.tx_bytes += len;
2859 		stats->es.ps.tx_packets++;
2860 		u64_stats_update_end(&stats->syncp);
2861 	} else {
2862 		dev->stats.tx_dropped++;
2863 		dev_kfree_skb_any(skb);
2864 	}
2865 
2866 	return NETDEV_TX_OK;
2867 }
2868 
2869 
2870 /* Free tx resources, when resetting a port */
2871 static void mvneta_txq_done_force(struct mvneta_port *pp,
2872 				  struct mvneta_tx_queue *txq)
2873 
2874 {
2875 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2876 	int tx_done = txq->count;
2877 
2878 	mvneta_txq_bufs_free(pp, txq, tx_done, nq, false);
2879 
2880 	/* reset txq */
2881 	txq->count = 0;
2882 	txq->txq_put_index = 0;
2883 	txq->txq_get_index = 0;
2884 }
2885 
2886 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2887  * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2888  */
2889 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2890 {
2891 	struct mvneta_tx_queue *txq;
2892 	struct netdev_queue *nq;
2893 	int cpu = smp_processor_id();
2894 
2895 	while (cause_tx_done) {
2896 		txq = mvneta_tx_done_policy(pp, cause_tx_done);
2897 
2898 		nq = netdev_get_tx_queue(pp->dev, txq->id);
2899 		__netif_tx_lock(nq, cpu);
2900 
2901 		if (txq->count)
2902 			mvneta_txq_done(pp, txq);
2903 
2904 		__netif_tx_unlock(nq);
2905 		cause_tx_done &= ~((1 << txq->id));
2906 	}
2907 }
2908 
2909 /* Compute crc8 of the specified address, using a unique algorithm ,
2910  * according to hw spec, different than generic crc8 algorithm
2911  */
2912 static int mvneta_addr_crc(unsigned char *addr)
2913 {
2914 	int crc = 0;
2915 	int i;
2916 
2917 	for (i = 0; i < ETH_ALEN; i++) {
2918 		int j;
2919 
2920 		crc = (crc ^ addr[i]) << 8;
2921 		for (j = 7; j >= 0; j--) {
2922 			if (crc & (0x100 << j))
2923 				crc ^= 0x107 << j;
2924 		}
2925 	}
2926 
2927 	return crc;
2928 }
2929 
2930 /* This method controls the net device special MAC multicast support.
2931  * The Special Multicast Table for MAC addresses supports MAC of the form
2932  * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2933  * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2934  * Table entries in the DA-Filter table. This method set the Special
2935  * Multicast Table appropriate entry.
2936  */
2937 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2938 					  unsigned char last_byte,
2939 					  int queue)
2940 {
2941 	unsigned int smc_table_reg;
2942 	unsigned int tbl_offset;
2943 	unsigned int reg_offset;
2944 
2945 	/* Register offset from SMC table base    */
2946 	tbl_offset = (last_byte / 4);
2947 	/* Entry offset within the above reg */
2948 	reg_offset = last_byte % 4;
2949 
2950 	smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2951 					+ tbl_offset * 4));
2952 
2953 	if (queue == -1)
2954 		smc_table_reg &= ~(0xff << (8 * reg_offset));
2955 	else {
2956 		smc_table_reg &= ~(0xff << (8 * reg_offset));
2957 		smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2958 	}
2959 
2960 	mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2961 		    smc_table_reg);
2962 }
2963 
2964 /* This method controls the network device Other MAC multicast support.
2965  * The Other Multicast Table is used for multicast of another type.
2966  * A CRC-8 is used as an index to the Other Multicast Table entries
2967  * in the DA-Filter table.
2968  * The method gets the CRC-8 value from the calling routine and
2969  * sets the Other Multicast Table appropriate entry according to the
2970  * specified CRC-8 .
2971  */
2972 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2973 					unsigned char crc8,
2974 					int queue)
2975 {
2976 	unsigned int omc_table_reg;
2977 	unsigned int tbl_offset;
2978 	unsigned int reg_offset;
2979 
2980 	tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2981 	reg_offset = crc8 % 4;	     /* Entry offset within the above reg   */
2982 
2983 	omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2984 
2985 	if (queue == -1) {
2986 		/* Clear accepts frame bit at specified Other DA table entry */
2987 		omc_table_reg &= ~(0xff << (8 * reg_offset));
2988 	} else {
2989 		omc_table_reg &= ~(0xff << (8 * reg_offset));
2990 		omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2991 	}
2992 
2993 	mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2994 }
2995 
2996 /* The network device supports multicast using two tables:
2997  *    1) Special Multicast Table for MAC addresses of the form
2998  *       0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2999  *       The MAC DA[7:0] bits are used as a pointer to the Special Multicast
3000  *       Table entries in the DA-Filter table.
3001  *    2) Other Multicast Table for multicast of another type. A CRC-8 value
3002  *       is used as an index to the Other Multicast Table entries in the
3003  *       DA-Filter table.
3004  */
3005 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
3006 				 int queue)
3007 {
3008 	unsigned char crc_result = 0;
3009 
3010 	if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
3011 		mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
3012 		return 0;
3013 	}
3014 
3015 	crc_result = mvneta_addr_crc(p_addr);
3016 	if (queue == -1) {
3017 		if (pp->mcast_count[crc_result] == 0) {
3018 			netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
3019 				    crc_result);
3020 			return -EINVAL;
3021 		}
3022 
3023 		pp->mcast_count[crc_result]--;
3024 		if (pp->mcast_count[crc_result] != 0) {
3025 			netdev_info(pp->dev,
3026 				    "After delete there are %d valid Mcast for crc8=0x%02x\n",
3027 				    pp->mcast_count[crc_result], crc_result);
3028 			return -EINVAL;
3029 		}
3030 	} else
3031 		pp->mcast_count[crc_result]++;
3032 
3033 	mvneta_set_other_mcast_addr(pp, crc_result, queue);
3034 
3035 	return 0;
3036 }
3037 
3038 /* Configure Fitering mode of Ethernet port */
3039 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
3040 					  int is_promisc)
3041 {
3042 	u32 port_cfg_reg, val;
3043 
3044 	port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
3045 
3046 	val = mvreg_read(pp, MVNETA_TYPE_PRIO);
3047 
3048 	/* Set / Clear UPM bit in port configuration register */
3049 	if (is_promisc) {
3050 		/* Accept all Unicast addresses */
3051 		port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
3052 		val |= MVNETA_FORCE_UNI;
3053 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
3054 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
3055 	} else {
3056 		/* Reject all Unicast addresses */
3057 		port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
3058 		val &= ~MVNETA_FORCE_UNI;
3059 	}
3060 
3061 	mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
3062 	mvreg_write(pp, MVNETA_TYPE_PRIO, val);
3063 }
3064 
3065 /* register unicast and multicast addresses */
3066 static void mvneta_set_rx_mode(struct net_device *dev)
3067 {
3068 	struct mvneta_port *pp = netdev_priv(dev);
3069 	struct netdev_hw_addr *ha;
3070 
3071 	if (dev->flags & IFF_PROMISC) {
3072 		/* Accept all: Multicast + Unicast */
3073 		mvneta_rx_unicast_promisc_set(pp, 1);
3074 		mvneta_set_ucast_table(pp, pp->rxq_def);
3075 		mvneta_set_special_mcast_table(pp, pp->rxq_def);
3076 		mvneta_set_other_mcast_table(pp, pp->rxq_def);
3077 	} else {
3078 		/* Accept single Unicast */
3079 		mvneta_rx_unicast_promisc_set(pp, 0);
3080 		mvneta_set_ucast_table(pp, -1);
3081 		mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
3082 
3083 		if (dev->flags & IFF_ALLMULTI) {
3084 			/* Accept all multicast */
3085 			mvneta_set_special_mcast_table(pp, pp->rxq_def);
3086 			mvneta_set_other_mcast_table(pp, pp->rxq_def);
3087 		} else {
3088 			/* Accept only initialized multicast */
3089 			mvneta_set_special_mcast_table(pp, -1);
3090 			mvneta_set_other_mcast_table(pp, -1);
3091 
3092 			if (!netdev_mc_empty(dev)) {
3093 				netdev_for_each_mc_addr(ha, dev) {
3094 					mvneta_mcast_addr_set(pp, ha->addr,
3095 							      pp->rxq_def);
3096 				}
3097 			}
3098 		}
3099 	}
3100 }
3101 
3102 /* Interrupt handling - the callback for request_irq() */
3103 static irqreturn_t mvneta_isr(int irq, void *dev_id)
3104 {
3105 	struct mvneta_port *pp = (struct mvneta_port *)dev_id;
3106 
3107 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
3108 	napi_schedule(&pp->napi);
3109 
3110 	return IRQ_HANDLED;
3111 }
3112 
3113 /* Interrupt handling - the callback for request_percpu_irq() */
3114 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
3115 {
3116 	struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
3117 
3118 	disable_percpu_irq(port->pp->dev->irq);
3119 	napi_schedule(&port->napi);
3120 
3121 	return IRQ_HANDLED;
3122 }
3123 
3124 static void mvneta_link_change(struct mvneta_port *pp)
3125 {
3126 	u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3127 
3128 	phylink_mac_change(pp->phylink, !!(gmac_stat & MVNETA_GMAC_LINK_UP));
3129 }
3130 
3131 /* NAPI handler
3132  * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
3133  * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
3134  * Bits 8 -15 of the cause Rx Tx register indicate that are received
3135  * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
3136  * Each CPU has its own causeRxTx register
3137  */
3138 static int mvneta_poll(struct napi_struct *napi, int budget)
3139 {
3140 	int rx_done = 0;
3141 	u32 cause_rx_tx;
3142 	int rx_queue;
3143 	struct mvneta_port *pp = netdev_priv(napi->dev);
3144 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
3145 
3146 	if (!netif_running(pp->dev)) {
3147 		napi_complete(napi);
3148 		return rx_done;
3149 	}
3150 
3151 	/* Read cause register */
3152 	cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
3153 	if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
3154 		u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
3155 
3156 		mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
3157 
3158 		if (cause_misc & (MVNETA_CAUSE_PHY_STATUS_CHANGE |
3159 				  MVNETA_CAUSE_LINK_CHANGE))
3160 			mvneta_link_change(pp);
3161 	}
3162 
3163 	/* Release Tx descriptors */
3164 	if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
3165 		mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
3166 		cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
3167 	}
3168 
3169 	/* For the case where the last mvneta_poll did not process all
3170 	 * RX packets
3171 	 */
3172 	cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
3173 		port->cause_rx_tx;
3174 
3175 	rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
3176 	if (rx_queue) {
3177 		rx_queue = rx_queue - 1;
3178 		if (pp->bm_priv)
3179 			rx_done = mvneta_rx_hwbm(napi, pp, budget,
3180 						 &pp->rxqs[rx_queue]);
3181 		else
3182 			rx_done = mvneta_rx_swbm(napi, pp, budget,
3183 						 &pp->rxqs[rx_queue]);
3184 	}
3185 
3186 	if (rx_done < budget) {
3187 		cause_rx_tx = 0;
3188 		napi_complete_done(napi, rx_done);
3189 
3190 		if (pp->neta_armada3700) {
3191 			unsigned long flags;
3192 
3193 			local_irq_save(flags);
3194 			mvreg_write(pp, MVNETA_INTR_NEW_MASK,
3195 				    MVNETA_RX_INTR_MASK(rxq_number) |
3196 				    MVNETA_TX_INTR_MASK(txq_number) |
3197 				    MVNETA_MISCINTR_INTR_MASK);
3198 			local_irq_restore(flags);
3199 		} else {
3200 			enable_percpu_irq(pp->dev->irq, 0);
3201 		}
3202 	}
3203 
3204 	if (pp->neta_armada3700)
3205 		pp->cause_rx_tx = cause_rx_tx;
3206 	else
3207 		port->cause_rx_tx = cause_rx_tx;
3208 
3209 	return rx_done;
3210 }
3211 
3212 static int mvneta_create_page_pool(struct mvneta_port *pp,
3213 				   struct mvneta_rx_queue *rxq, int size)
3214 {
3215 	struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog);
3216 	struct page_pool_params pp_params = {
3217 		.order = 0,
3218 		.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
3219 		.pool_size = size,
3220 		.nid = NUMA_NO_NODE,
3221 		.dev = pp->dev->dev.parent,
3222 		.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
3223 		.offset = pp->rx_offset_correction,
3224 		.max_len = MVNETA_MAX_RX_BUF_SIZE,
3225 	};
3226 	int err;
3227 
3228 	rxq->page_pool = page_pool_create(&pp_params);
3229 	if (IS_ERR(rxq->page_pool)) {
3230 		err = PTR_ERR(rxq->page_pool);
3231 		rxq->page_pool = NULL;
3232 		return err;
3233 	}
3234 
3235 	err = xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id, 0);
3236 	if (err < 0)
3237 		goto err_free_pp;
3238 
3239 	err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
3240 					 rxq->page_pool);
3241 	if (err)
3242 		goto err_unregister_rxq;
3243 
3244 	return 0;
3245 
3246 err_unregister_rxq:
3247 	xdp_rxq_info_unreg(&rxq->xdp_rxq);
3248 err_free_pp:
3249 	page_pool_destroy(rxq->page_pool);
3250 	rxq->page_pool = NULL;
3251 	return err;
3252 }
3253 
3254 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
3255 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
3256 			   int num)
3257 {
3258 	int i, err;
3259 
3260 	err = mvneta_create_page_pool(pp, rxq, num);
3261 	if (err < 0)
3262 		return err;
3263 
3264 	for (i = 0; i < num; i++) {
3265 		memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
3266 		if (mvneta_rx_refill(pp, rxq->descs + i, rxq,
3267 				     GFP_KERNEL) != 0) {
3268 			netdev_err(pp->dev,
3269 				   "%s:rxq %d, %d of %d buffs  filled\n",
3270 				   __func__, rxq->id, i, num);
3271 			break;
3272 		}
3273 	}
3274 
3275 	/* Add this number of RX descriptors as non occupied (ready to
3276 	 * get packets)
3277 	 */
3278 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
3279 
3280 	return i;
3281 }
3282 
3283 /* Free all packets pending transmit from all TXQs and reset TX port */
3284 static void mvneta_tx_reset(struct mvneta_port *pp)
3285 {
3286 	int queue;
3287 
3288 	/* free the skb's in the tx ring */
3289 	for (queue = 0; queue < txq_number; queue++)
3290 		mvneta_txq_done_force(pp, &pp->txqs[queue]);
3291 
3292 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
3293 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
3294 }
3295 
3296 static void mvneta_rx_reset(struct mvneta_port *pp)
3297 {
3298 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
3299 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
3300 }
3301 
3302 /* Rx/Tx queue initialization/cleanup methods */
3303 
3304 static int mvneta_rxq_sw_init(struct mvneta_port *pp,
3305 			      struct mvneta_rx_queue *rxq)
3306 {
3307 	rxq->size = pp->rx_ring_size;
3308 
3309 	/* Allocate memory for RX descriptors */
3310 	rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3311 					rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3312 					&rxq->descs_phys, GFP_KERNEL);
3313 	if (!rxq->descs)
3314 		return -ENOMEM;
3315 
3316 	rxq->last_desc = rxq->size - 1;
3317 
3318 	return 0;
3319 }
3320 
3321 static void mvneta_rxq_hw_init(struct mvneta_port *pp,
3322 			       struct mvneta_rx_queue *rxq)
3323 {
3324 	/* Set Rx descriptors queue starting address */
3325 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
3326 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
3327 
3328 	/* Set coalescing pkts and time */
3329 	mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3330 	mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3331 
3332 	if (!pp->bm_priv) {
3333 		/* Set Offset */
3334 		mvneta_rxq_offset_set(pp, rxq, 0);
3335 		mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
3336 					MVNETA_MAX_RX_BUF_SIZE :
3337 					MVNETA_RX_BUF_SIZE(pp->pkt_size));
3338 		mvneta_rxq_bm_disable(pp, rxq);
3339 		mvneta_rxq_fill(pp, rxq, rxq->size);
3340 	} else {
3341 		/* Set Offset */
3342 		mvneta_rxq_offset_set(pp, rxq,
3343 				      NET_SKB_PAD - pp->rx_offset_correction);
3344 
3345 		mvneta_rxq_bm_enable(pp, rxq);
3346 		/* Fill RXQ with buffers from RX pool */
3347 		mvneta_rxq_long_pool_set(pp, rxq);
3348 		mvneta_rxq_short_pool_set(pp, rxq);
3349 		mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
3350 	}
3351 }
3352 
3353 /* Create a specified RX queue */
3354 static int mvneta_rxq_init(struct mvneta_port *pp,
3355 			   struct mvneta_rx_queue *rxq)
3356 
3357 {
3358 	int ret;
3359 
3360 	ret = mvneta_rxq_sw_init(pp, rxq);
3361 	if (ret < 0)
3362 		return ret;
3363 
3364 	mvneta_rxq_hw_init(pp, rxq);
3365 
3366 	return 0;
3367 }
3368 
3369 /* Cleanup Rx queue */
3370 static void mvneta_rxq_deinit(struct mvneta_port *pp,
3371 			      struct mvneta_rx_queue *rxq)
3372 {
3373 	mvneta_rxq_drop_pkts(pp, rxq);
3374 
3375 	if (rxq->descs)
3376 		dma_free_coherent(pp->dev->dev.parent,
3377 				  rxq->size * MVNETA_DESC_ALIGNED_SIZE,
3378 				  rxq->descs,
3379 				  rxq->descs_phys);
3380 
3381 	rxq->descs             = NULL;
3382 	rxq->last_desc         = 0;
3383 	rxq->next_desc_to_proc = 0;
3384 	rxq->descs_phys        = 0;
3385 	rxq->first_to_refill   = 0;
3386 	rxq->refill_num        = 0;
3387 }
3388 
3389 static int mvneta_txq_sw_init(struct mvneta_port *pp,
3390 			      struct mvneta_tx_queue *txq)
3391 {
3392 	int cpu;
3393 
3394 	txq->size = pp->tx_ring_size;
3395 
3396 	/* A queue must always have room for at least one skb.
3397 	 * Therefore, stop the queue when the free entries reaches
3398 	 * the maximum number of descriptors per skb.
3399 	 */
3400 	txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
3401 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
3402 
3403 	/* Allocate memory for TX descriptors */
3404 	txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
3405 					txq->size * MVNETA_DESC_ALIGNED_SIZE,
3406 					&txq->descs_phys, GFP_KERNEL);
3407 	if (!txq->descs)
3408 		return -ENOMEM;
3409 
3410 	txq->last_desc = txq->size - 1;
3411 
3412 	txq->buf = kmalloc_array(txq->size, sizeof(*txq->buf), GFP_KERNEL);
3413 	if (!txq->buf)
3414 		return -ENOMEM;
3415 
3416 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
3417 	txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
3418 					   txq->size * TSO_HEADER_SIZE,
3419 					   &txq->tso_hdrs_phys, GFP_KERNEL);
3420 	if (!txq->tso_hdrs)
3421 		return -ENOMEM;
3422 
3423 	/* Setup XPS mapping */
3424 	if (pp->neta_armada3700)
3425 		cpu = 0;
3426 	else if (txq_number > 1)
3427 		cpu = txq->id % num_present_cpus();
3428 	else
3429 		cpu = pp->rxq_def % num_present_cpus();
3430 	cpumask_set_cpu(cpu, &txq->affinity_mask);
3431 	netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
3432 
3433 	return 0;
3434 }
3435 
3436 static void mvneta_txq_hw_init(struct mvneta_port *pp,
3437 			       struct mvneta_tx_queue *txq)
3438 {
3439 	/* Set maximum bandwidth for enabled TXQs */
3440 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
3441 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
3442 
3443 	/* Set Tx descriptors queue starting address */
3444 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
3445 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
3446 
3447 	mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3448 }
3449 
3450 /* Create and initialize a tx queue */
3451 static int mvneta_txq_init(struct mvneta_port *pp,
3452 			   struct mvneta_tx_queue *txq)
3453 {
3454 	int ret;
3455 
3456 	ret = mvneta_txq_sw_init(pp, txq);
3457 	if (ret < 0)
3458 		return ret;
3459 
3460 	mvneta_txq_hw_init(pp, txq);
3461 
3462 	return 0;
3463 }
3464 
3465 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
3466 static void mvneta_txq_sw_deinit(struct mvneta_port *pp,
3467 				 struct mvneta_tx_queue *txq)
3468 {
3469 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
3470 
3471 	kfree(txq->buf);
3472 
3473 	if (txq->tso_hdrs)
3474 		dma_free_coherent(pp->dev->dev.parent,
3475 				  txq->size * TSO_HEADER_SIZE,
3476 				  txq->tso_hdrs, txq->tso_hdrs_phys);
3477 	if (txq->descs)
3478 		dma_free_coherent(pp->dev->dev.parent,
3479 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
3480 				  txq->descs, txq->descs_phys);
3481 
3482 	netdev_tx_reset_queue(nq);
3483 
3484 	txq->descs             = NULL;
3485 	txq->last_desc         = 0;
3486 	txq->next_desc_to_proc = 0;
3487 	txq->descs_phys        = 0;
3488 }
3489 
3490 static void mvneta_txq_hw_deinit(struct mvneta_port *pp,
3491 				 struct mvneta_tx_queue *txq)
3492 {
3493 	/* Set minimum bandwidth for disabled TXQs */
3494 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
3495 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
3496 
3497 	/* Set Tx descriptors queue starting address and size */
3498 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
3499 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
3500 }
3501 
3502 static void mvneta_txq_deinit(struct mvneta_port *pp,
3503 			      struct mvneta_tx_queue *txq)
3504 {
3505 	mvneta_txq_sw_deinit(pp, txq);
3506 	mvneta_txq_hw_deinit(pp, txq);
3507 }
3508 
3509 /* Cleanup all Tx queues */
3510 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3511 {
3512 	int queue;
3513 
3514 	for (queue = 0; queue < txq_number; queue++)
3515 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
3516 }
3517 
3518 /* Cleanup all Rx queues */
3519 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3520 {
3521 	int queue;
3522 
3523 	for (queue = 0; queue < rxq_number; queue++)
3524 		mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3525 }
3526 
3527 
3528 /* Init all Rx queues */
3529 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3530 {
3531 	int queue;
3532 
3533 	for (queue = 0; queue < rxq_number; queue++) {
3534 		int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3535 
3536 		if (err) {
3537 			netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3538 				   __func__, queue);
3539 			mvneta_cleanup_rxqs(pp);
3540 			return err;
3541 		}
3542 	}
3543 
3544 	return 0;
3545 }
3546 
3547 /* Init all tx queues */
3548 static int mvneta_setup_txqs(struct mvneta_port *pp)
3549 {
3550 	int queue;
3551 
3552 	for (queue = 0; queue < txq_number; queue++) {
3553 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3554 		if (err) {
3555 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
3556 				   __func__, queue);
3557 			mvneta_cleanup_txqs(pp);
3558 			return err;
3559 		}
3560 	}
3561 
3562 	return 0;
3563 }
3564 
3565 static int mvneta_comphy_init(struct mvneta_port *pp, phy_interface_t interface)
3566 {
3567 	int ret;
3568 
3569 	ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET, interface);
3570 	if (ret)
3571 		return ret;
3572 
3573 	return phy_power_on(pp->comphy);
3574 }
3575 
3576 static int mvneta_config_interface(struct mvneta_port *pp,
3577 				   phy_interface_t interface)
3578 {
3579 	int ret = 0;
3580 
3581 	if (pp->comphy) {
3582 		if (interface == PHY_INTERFACE_MODE_SGMII ||
3583 		    interface == PHY_INTERFACE_MODE_1000BASEX ||
3584 		    interface == PHY_INTERFACE_MODE_2500BASEX) {
3585 			ret = mvneta_comphy_init(pp, interface);
3586 		}
3587 	} else {
3588 		switch (interface) {
3589 		case PHY_INTERFACE_MODE_QSGMII:
3590 			mvreg_write(pp, MVNETA_SERDES_CFG,
3591 				    MVNETA_QSGMII_SERDES_PROTO);
3592 			break;
3593 
3594 		case PHY_INTERFACE_MODE_SGMII:
3595 		case PHY_INTERFACE_MODE_1000BASEX:
3596 			mvreg_write(pp, MVNETA_SERDES_CFG,
3597 				    MVNETA_SGMII_SERDES_PROTO);
3598 			break;
3599 
3600 		case PHY_INTERFACE_MODE_2500BASEX:
3601 			mvreg_write(pp, MVNETA_SERDES_CFG,
3602 				    MVNETA_HSGMII_SERDES_PROTO);
3603 			break;
3604 		default:
3605 			break;
3606 		}
3607 	}
3608 
3609 	pp->phy_interface = interface;
3610 
3611 	return ret;
3612 }
3613 
3614 static void mvneta_start_dev(struct mvneta_port *pp)
3615 {
3616 	int cpu;
3617 
3618 	WARN_ON(mvneta_config_interface(pp, pp->phy_interface));
3619 
3620 	mvneta_max_rx_size_set(pp, pp->pkt_size);
3621 	mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3622 
3623 	/* start the Rx/Tx activity */
3624 	mvneta_port_enable(pp);
3625 
3626 	if (!pp->neta_armada3700) {
3627 		/* Enable polling on the port */
3628 		for_each_online_cpu(cpu) {
3629 			struct mvneta_pcpu_port *port =
3630 				per_cpu_ptr(pp->ports, cpu);
3631 
3632 			napi_enable(&port->napi);
3633 		}
3634 	} else {
3635 		napi_enable(&pp->napi);
3636 	}
3637 
3638 	/* Unmask interrupts. It has to be done from each CPU */
3639 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3640 
3641 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3642 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3643 		    MVNETA_CAUSE_LINK_CHANGE);
3644 
3645 	phylink_start(pp->phylink);
3646 
3647 	/* We may have called phylink_speed_down before */
3648 	phylink_speed_up(pp->phylink);
3649 
3650 	netif_tx_start_all_queues(pp->dev);
3651 
3652 	clear_bit(__MVNETA_DOWN, &pp->state);
3653 }
3654 
3655 static void mvneta_stop_dev(struct mvneta_port *pp)
3656 {
3657 	unsigned int cpu;
3658 
3659 	set_bit(__MVNETA_DOWN, &pp->state);
3660 
3661 	if (device_may_wakeup(&pp->dev->dev))
3662 		phylink_speed_down(pp->phylink, false);
3663 
3664 	phylink_stop(pp->phylink);
3665 
3666 	if (!pp->neta_armada3700) {
3667 		for_each_online_cpu(cpu) {
3668 			struct mvneta_pcpu_port *port =
3669 				per_cpu_ptr(pp->ports, cpu);
3670 
3671 			napi_disable(&port->napi);
3672 		}
3673 	} else {
3674 		napi_disable(&pp->napi);
3675 	}
3676 
3677 	netif_carrier_off(pp->dev);
3678 
3679 	mvneta_port_down(pp);
3680 	netif_tx_stop_all_queues(pp->dev);
3681 
3682 	/* Stop the port activity */
3683 	mvneta_port_disable(pp);
3684 
3685 	/* Clear all ethernet port interrupts */
3686 	on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3687 
3688 	/* Mask all ethernet port interrupts */
3689 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3690 
3691 	mvneta_tx_reset(pp);
3692 	mvneta_rx_reset(pp);
3693 
3694 	WARN_ON(phy_power_off(pp->comphy));
3695 }
3696 
3697 static void mvneta_percpu_enable(void *arg)
3698 {
3699 	struct mvneta_port *pp = arg;
3700 
3701 	enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3702 }
3703 
3704 static void mvneta_percpu_disable(void *arg)
3705 {
3706 	struct mvneta_port *pp = arg;
3707 
3708 	disable_percpu_irq(pp->dev->irq);
3709 }
3710 
3711 /* Change the device mtu */
3712 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3713 {
3714 	struct mvneta_port *pp = netdev_priv(dev);
3715 	int ret;
3716 
3717 	if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3718 		netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3719 			    mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3720 		mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3721 	}
3722 
3723 	if (pp->xdp_prog && mtu > MVNETA_MAX_RX_BUF_SIZE) {
3724 		netdev_info(dev, "Illegal MTU value %d for XDP mode\n", mtu);
3725 		return -EINVAL;
3726 	}
3727 
3728 	dev->mtu = mtu;
3729 
3730 	if (!netif_running(dev)) {
3731 		if (pp->bm_priv)
3732 			mvneta_bm_update_mtu(pp, mtu);
3733 
3734 		netdev_update_features(dev);
3735 		return 0;
3736 	}
3737 
3738 	/* The interface is running, so we have to force a
3739 	 * reallocation of the queues
3740 	 */
3741 	mvneta_stop_dev(pp);
3742 	on_each_cpu(mvneta_percpu_disable, pp, true);
3743 
3744 	mvneta_cleanup_txqs(pp);
3745 	mvneta_cleanup_rxqs(pp);
3746 
3747 	if (pp->bm_priv)
3748 		mvneta_bm_update_mtu(pp, mtu);
3749 
3750 	pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3751 
3752 	ret = mvneta_setup_rxqs(pp);
3753 	if (ret) {
3754 		netdev_err(dev, "unable to setup rxqs after MTU change\n");
3755 		return ret;
3756 	}
3757 
3758 	ret = mvneta_setup_txqs(pp);
3759 	if (ret) {
3760 		netdev_err(dev, "unable to setup txqs after MTU change\n");
3761 		return ret;
3762 	}
3763 
3764 	on_each_cpu(mvneta_percpu_enable, pp, true);
3765 	mvneta_start_dev(pp);
3766 
3767 	netdev_update_features(dev);
3768 
3769 	return 0;
3770 }
3771 
3772 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3773 					     netdev_features_t features)
3774 {
3775 	struct mvneta_port *pp = netdev_priv(dev);
3776 
3777 	if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3778 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3779 		netdev_info(dev,
3780 			    "Disable IP checksum for MTU greater than %dB\n",
3781 			    pp->tx_csum_limit);
3782 	}
3783 
3784 	return features;
3785 }
3786 
3787 /* Get mac address */
3788 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3789 {
3790 	u32 mac_addr_l, mac_addr_h;
3791 
3792 	mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3793 	mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3794 	addr[0] = (mac_addr_h >> 24) & 0xFF;
3795 	addr[1] = (mac_addr_h >> 16) & 0xFF;
3796 	addr[2] = (mac_addr_h >> 8) & 0xFF;
3797 	addr[3] = mac_addr_h & 0xFF;
3798 	addr[4] = (mac_addr_l >> 8) & 0xFF;
3799 	addr[5] = mac_addr_l & 0xFF;
3800 }
3801 
3802 /* Handle setting mac address */
3803 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3804 {
3805 	struct mvneta_port *pp = netdev_priv(dev);
3806 	struct sockaddr *sockaddr = addr;
3807 	int ret;
3808 
3809 	ret = eth_prepare_mac_addr_change(dev, addr);
3810 	if (ret < 0)
3811 		return ret;
3812 	/* Remove previous address table entry */
3813 	mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3814 
3815 	/* Set new addr in hw */
3816 	mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3817 
3818 	eth_commit_mac_addr_change(dev, addr);
3819 	return 0;
3820 }
3821 
3822 static void mvneta_validate(struct phylink_config *config,
3823 			    unsigned long *supported,
3824 			    struct phylink_link_state *state)
3825 {
3826 	/* We only support QSGMII, SGMII, 802.3z and RGMII modes.
3827 	 * When in 802.3z mode, we must have AN enabled:
3828 	 * "Bit 2 Field InBandAnEn In-band Auto-Negotiation enable. ...
3829 	 * When <PortType> = 1 (1000BASE-X) this field must be set to 1."
3830 	 */
3831 	if (phy_interface_mode_is_8023z(state->interface) &&
3832 	    !phylink_test(state->advertising, Autoneg)) {
3833 		linkmode_zero(supported);
3834 		return;
3835 	}
3836 
3837 	phylink_generic_validate(config, supported, state);
3838 }
3839 
3840 static void mvneta_mac_pcs_get_state(struct phylink_config *config,
3841 				     struct phylink_link_state *state)
3842 {
3843 	struct net_device *ndev = to_net_dev(config->dev);
3844 	struct mvneta_port *pp = netdev_priv(ndev);
3845 	u32 gmac_stat;
3846 
3847 	gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
3848 
3849 	if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3850 		state->speed =
3851 			state->interface == PHY_INTERFACE_MODE_2500BASEX ?
3852 			SPEED_2500 : SPEED_1000;
3853 	else if (gmac_stat & MVNETA_GMAC_SPEED_100)
3854 		state->speed = SPEED_100;
3855 	else
3856 		state->speed = SPEED_10;
3857 
3858 	state->an_complete = !!(gmac_stat & MVNETA_GMAC_AN_COMPLETE);
3859 	state->link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
3860 	state->duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
3861 
3862 	state->pause = 0;
3863 	if (gmac_stat & MVNETA_GMAC_RX_FLOW_CTRL_ENABLE)
3864 		state->pause |= MLO_PAUSE_RX;
3865 	if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
3866 		state->pause |= MLO_PAUSE_TX;
3867 }
3868 
3869 static void mvneta_mac_an_restart(struct phylink_config *config)
3870 {
3871 	struct net_device *ndev = to_net_dev(config->dev);
3872 	struct mvneta_port *pp = netdev_priv(ndev);
3873 	u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3874 
3875 	mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3876 		    gmac_an | MVNETA_GMAC_INBAND_RESTART_AN);
3877 	mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3878 		    gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
3879 }
3880 
3881 static void mvneta_mac_config(struct phylink_config *config, unsigned int mode,
3882 			      const struct phylink_link_state *state)
3883 {
3884 	struct net_device *ndev = to_net_dev(config->dev);
3885 	struct mvneta_port *pp = netdev_priv(ndev);
3886 	u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
3887 	u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3888 	u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
3889 	u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
3890 	u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3891 
3892 	new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
3893 	new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
3894 				   MVNETA_GMAC2_PORT_RESET);
3895 	new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
3896 	new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
3897 	new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
3898 			     MVNETA_GMAC_INBAND_RESTART_AN |
3899 			     MVNETA_GMAC_AN_SPEED_EN |
3900 			     MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3901 			     MVNETA_GMAC_AN_FLOW_CTRL_EN |
3902 			     MVNETA_GMAC_AN_DUPLEX_EN);
3903 
3904 	/* Even though it might look weird, when we're configured in
3905 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
3906 	 */
3907 	new_ctrl2 |= MVNETA_GMAC2_PORT_RGMII;
3908 
3909 	if (state->interface == PHY_INTERFACE_MODE_QSGMII ||
3910 	    state->interface == PHY_INTERFACE_MODE_SGMII ||
3911 	    phy_interface_mode_is_8023z(state->interface))
3912 		new_ctrl2 |= MVNETA_GMAC2_PCS_ENABLE;
3913 
3914 	if (phylink_test(state->advertising, Pause))
3915 		new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3916 
3917 	if (!phylink_autoneg_inband(mode)) {
3918 		/* Phy or fixed speed - nothing to do, leave the
3919 		 * configured speed, duplex and flow control as-is.
3920 		 */
3921 	} else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
3922 		/* SGMII mode receives the state from the PHY */
3923 		new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
3924 		new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3925 		new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3926 				     MVNETA_GMAC_FORCE_LINK_PASS |
3927 				     MVNETA_GMAC_CONFIG_MII_SPEED |
3928 				     MVNETA_GMAC_CONFIG_GMII_SPEED |
3929 				     MVNETA_GMAC_CONFIG_FULL_DUPLEX)) |
3930 			 MVNETA_GMAC_INBAND_AN_ENABLE |
3931 			 MVNETA_GMAC_AN_SPEED_EN |
3932 			 MVNETA_GMAC_AN_DUPLEX_EN;
3933 	} else {
3934 		/* 802.3z negotiation - only 1000base-X */
3935 		new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
3936 		new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
3937 		new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3938 				     MVNETA_GMAC_FORCE_LINK_PASS |
3939 				     MVNETA_GMAC_CONFIG_MII_SPEED)) |
3940 			 MVNETA_GMAC_INBAND_AN_ENABLE |
3941 			 MVNETA_GMAC_CONFIG_GMII_SPEED |
3942 			 /* The MAC only supports FD mode */
3943 			 MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3944 
3945 		if (state->pause & MLO_PAUSE_AN && state->an_enabled)
3946 			new_an |= MVNETA_GMAC_AN_FLOW_CTRL_EN;
3947 	}
3948 
3949 	/* Armada 370 documentation says we can only change the port mode
3950 	 * and in-band enable when the link is down, so force it down
3951 	 * while making these changes. We also do this for GMAC_CTRL2
3952 	 */
3953 	if ((new_ctrl0 ^ gmac_ctrl0) & MVNETA_GMAC0_PORT_1000BASE_X ||
3954 	    (new_ctrl2 ^ gmac_ctrl2) & MVNETA_GMAC2_INBAND_AN_ENABLE ||
3955 	    (new_an  ^ gmac_an) & MVNETA_GMAC_INBAND_AN_ENABLE) {
3956 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3957 			    (gmac_an & ~MVNETA_GMAC_FORCE_LINK_PASS) |
3958 			    MVNETA_GMAC_FORCE_LINK_DOWN);
3959 	}
3960 
3961 
3962 	/* When at 2.5G, the link partner can send frames with shortened
3963 	 * preambles.
3964 	 */
3965 	if (state->interface == PHY_INTERFACE_MODE_2500BASEX)
3966 		new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
3967 
3968 	if (pp->phy_interface != state->interface) {
3969 		if (pp->comphy)
3970 			WARN_ON(phy_power_off(pp->comphy));
3971 		WARN_ON(mvneta_config_interface(pp, state->interface));
3972 	}
3973 
3974 	if (new_ctrl0 != gmac_ctrl0)
3975 		mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
3976 	if (new_ctrl2 != gmac_ctrl2)
3977 		mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
3978 	if (new_ctrl4 != gmac_ctrl4)
3979 		mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
3980 	if (new_clk != gmac_clk)
3981 		mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
3982 	if (new_an != gmac_an)
3983 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, new_an);
3984 
3985 	if (gmac_ctrl2 & MVNETA_GMAC2_PORT_RESET) {
3986 		while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
3987 			MVNETA_GMAC2_PORT_RESET) != 0)
3988 			continue;
3989 	}
3990 }
3991 
3992 static void mvneta_set_eee(struct mvneta_port *pp, bool enable)
3993 {
3994 	u32 lpi_ctl1;
3995 
3996 	lpi_ctl1 = mvreg_read(pp, MVNETA_LPI_CTRL_1);
3997 	if (enable)
3998 		lpi_ctl1 |= MVNETA_LPI_REQUEST_ENABLE;
3999 	else
4000 		lpi_ctl1 &= ~MVNETA_LPI_REQUEST_ENABLE;
4001 	mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
4002 }
4003 
4004 static void mvneta_mac_link_down(struct phylink_config *config,
4005 				 unsigned int mode, phy_interface_t interface)
4006 {
4007 	struct net_device *ndev = to_net_dev(config->dev);
4008 	struct mvneta_port *pp = netdev_priv(ndev);
4009 	u32 val;
4010 
4011 	mvneta_port_down(pp);
4012 
4013 	if (!phylink_autoneg_inband(mode)) {
4014 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4015 		val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
4016 		val |= MVNETA_GMAC_FORCE_LINK_DOWN;
4017 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4018 	}
4019 
4020 	pp->eee_active = false;
4021 	mvneta_set_eee(pp, false);
4022 }
4023 
4024 static void mvneta_mac_link_up(struct phylink_config *config,
4025 			       struct phy_device *phy,
4026 			       unsigned int mode, phy_interface_t interface,
4027 			       int speed, int duplex,
4028 			       bool tx_pause, bool rx_pause)
4029 {
4030 	struct net_device *ndev = to_net_dev(config->dev);
4031 	struct mvneta_port *pp = netdev_priv(ndev);
4032 	u32 val;
4033 
4034 	if (!phylink_autoneg_inband(mode)) {
4035 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4036 		val &= ~(MVNETA_GMAC_FORCE_LINK_DOWN |
4037 			 MVNETA_GMAC_CONFIG_MII_SPEED |
4038 			 MVNETA_GMAC_CONFIG_GMII_SPEED |
4039 			 MVNETA_GMAC_CONFIG_FLOW_CTRL |
4040 			 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
4041 		val |= MVNETA_GMAC_FORCE_LINK_PASS;
4042 
4043 		if (speed == SPEED_1000 || speed == SPEED_2500)
4044 			val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
4045 		else if (speed == SPEED_100)
4046 			val |= MVNETA_GMAC_CONFIG_MII_SPEED;
4047 
4048 		if (duplex == DUPLEX_FULL)
4049 			val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
4050 
4051 		if (tx_pause || rx_pause)
4052 			val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4053 
4054 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4055 	} else {
4056 		/* When inband doesn't cover flow control or flow control is
4057 		 * disabled, we need to manually configure it. This bit will
4058 		 * only have effect if MVNETA_GMAC_AN_FLOW_CTRL_EN is unset.
4059 		 */
4060 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4061 		val &= ~MVNETA_GMAC_CONFIG_FLOW_CTRL;
4062 
4063 		if (tx_pause || rx_pause)
4064 			val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4065 
4066 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4067 	}
4068 
4069 	mvneta_port_up(pp);
4070 
4071 	if (phy && pp->eee_enabled) {
4072 		pp->eee_active = phy_init_eee(phy, 0) >= 0;
4073 		mvneta_set_eee(pp, pp->eee_active && pp->tx_lpi_enabled);
4074 	}
4075 }
4076 
4077 static const struct phylink_mac_ops mvneta_phylink_ops = {
4078 	.validate = mvneta_validate,
4079 	.mac_pcs_get_state = mvneta_mac_pcs_get_state,
4080 	.mac_an_restart = mvneta_mac_an_restart,
4081 	.mac_config = mvneta_mac_config,
4082 	.mac_link_down = mvneta_mac_link_down,
4083 	.mac_link_up = mvneta_mac_link_up,
4084 };
4085 
4086 static int mvneta_mdio_probe(struct mvneta_port *pp)
4087 {
4088 	struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL };
4089 	int err = phylink_of_phy_connect(pp->phylink, pp->dn, 0);
4090 
4091 	if (err)
4092 		netdev_err(pp->dev, "could not attach PHY: %d\n", err);
4093 
4094 	phylink_ethtool_get_wol(pp->phylink, &wol);
4095 	device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
4096 
4097 	/* PHY WoL may be enabled but device wakeup disabled */
4098 	if (wol.supported)
4099 		device_set_wakeup_enable(&pp->dev->dev, !!wol.wolopts);
4100 
4101 	return err;
4102 }
4103 
4104 static void mvneta_mdio_remove(struct mvneta_port *pp)
4105 {
4106 	phylink_disconnect_phy(pp->phylink);
4107 }
4108 
4109 /* Electing a CPU must be done in an atomic way: it should be done
4110  * after or before the removal/insertion of a CPU and this function is
4111  * not reentrant.
4112  */
4113 static void mvneta_percpu_elect(struct mvneta_port *pp)
4114 {
4115 	int elected_cpu = 0, max_cpu, cpu, i = 0;
4116 
4117 	/* Use the cpu associated to the rxq when it is online, in all
4118 	 * the other cases, use the cpu 0 which can't be offline.
4119 	 */
4120 	if (cpu_online(pp->rxq_def))
4121 		elected_cpu = pp->rxq_def;
4122 
4123 	max_cpu = num_present_cpus();
4124 
4125 	for_each_online_cpu(cpu) {
4126 		int rxq_map = 0, txq_map = 0;
4127 		int rxq;
4128 
4129 		for (rxq = 0; rxq < rxq_number; rxq++)
4130 			if ((rxq % max_cpu) == cpu)
4131 				rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
4132 
4133 		if (cpu == elected_cpu)
4134 			/* Map the default receive queue to the elected CPU */
4135 			rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
4136 
4137 		/* We update the TX queue map only if we have one
4138 		 * queue. In this case we associate the TX queue to
4139 		 * the CPU bound to the default RX queue
4140 		 */
4141 		if (txq_number == 1)
4142 			txq_map = (cpu == elected_cpu) ?
4143 				MVNETA_CPU_TXQ_ACCESS(1) : 0;
4144 		else
4145 			txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
4146 				MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
4147 
4148 		mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
4149 
4150 		/* Update the interrupt mask on each CPU according the
4151 		 * new mapping
4152 		 */
4153 		smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
4154 					 pp, true);
4155 		i++;
4156 
4157 	}
4158 };
4159 
4160 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
4161 {
4162 	int other_cpu;
4163 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4164 						  node_online);
4165 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4166 
4167 	/* Armada 3700's per-cpu interrupt for mvneta is broken, all interrupts
4168 	 * are routed to CPU 0, so we don't need all the cpu-hotplug support
4169 	 */
4170 	if (pp->neta_armada3700)
4171 		return 0;
4172 
4173 	spin_lock(&pp->lock);
4174 	/*
4175 	 * Configuring the driver for a new CPU while the driver is
4176 	 * stopping is racy, so just avoid it.
4177 	 */
4178 	if (pp->is_stopped) {
4179 		spin_unlock(&pp->lock);
4180 		return 0;
4181 	}
4182 	netif_tx_stop_all_queues(pp->dev);
4183 
4184 	/*
4185 	 * We have to synchronise on tha napi of each CPU except the one
4186 	 * just being woken up
4187 	 */
4188 	for_each_online_cpu(other_cpu) {
4189 		if (other_cpu != cpu) {
4190 			struct mvneta_pcpu_port *other_port =
4191 				per_cpu_ptr(pp->ports, other_cpu);
4192 
4193 			napi_synchronize(&other_port->napi);
4194 		}
4195 	}
4196 
4197 	/* Mask all ethernet port interrupts */
4198 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4199 	napi_enable(&port->napi);
4200 
4201 	/*
4202 	 * Enable per-CPU interrupts on the CPU that is
4203 	 * brought up.
4204 	 */
4205 	mvneta_percpu_enable(pp);
4206 
4207 	/*
4208 	 * Enable per-CPU interrupt on the one CPU we care
4209 	 * about.
4210 	 */
4211 	mvneta_percpu_elect(pp);
4212 
4213 	/* Unmask all ethernet port interrupts */
4214 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4215 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4216 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
4217 		    MVNETA_CAUSE_LINK_CHANGE);
4218 	netif_tx_start_all_queues(pp->dev);
4219 	spin_unlock(&pp->lock);
4220 	return 0;
4221 }
4222 
4223 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
4224 {
4225 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4226 						  node_online);
4227 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
4228 
4229 	/*
4230 	 * Thanks to this lock we are sure that any pending cpu election is
4231 	 * done.
4232 	 */
4233 	spin_lock(&pp->lock);
4234 	/* Mask all ethernet port interrupts */
4235 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4236 	spin_unlock(&pp->lock);
4237 
4238 	napi_synchronize(&port->napi);
4239 	napi_disable(&port->napi);
4240 	/* Disable per-CPU interrupts on the CPU that is brought down. */
4241 	mvneta_percpu_disable(pp);
4242 	return 0;
4243 }
4244 
4245 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
4246 {
4247 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
4248 						  node_dead);
4249 
4250 	/* Check if a new CPU must be elected now this on is down */
4251 	spin_lock(&pp->lock);
4252 	mvneta_percpu_elect(pp);
4253 	spin_unlock(&pp->lock);
4254 	/* Unmask all ethernet port interrupts */
4255 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
4256 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
4257 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
4258 		    MVNETA_CAUSE_LINK_CHANGE);
4259 	netif_tx_start_all_queues(pp->dev);
4260 	return 0;
4261 }
4262 
4263 static int mvneta_open(struct net_device *dev)
4264 {
4265 	struct mvneta_port *pp = netdev_priv(dev);
4266 	int ret;
4267 
4268 	pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
4269 
4270 	ret = mvneta_setup_rxqs(pp);
4271 	if (ret)
4272 		return ret;
4273 
4274 	ret = mvneta_setup_txqs(pp);
4275 	if (ret)
4276 		goto err_cleanup_rxqs;
4277 
4278 	/* Connect to port interrupt line */
4279 	if (pp->neta_armada3700)
4280 		ret = request_irq(pp->dev->irq, mvneta_isr, 0,
4281 				  dev->name, pp);
4282 	else
4283 		ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
4284 					 dev->name, pp->ports);
4285 	if (ret) {
4286 		netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
4287 		goto err_cleanup_txqs;
4288 	}
4289 
4290 	if (!pp->neta_armada3700) {
4291 		/* Enable per-CPU interrupt on all the CPU to handle our RX
4292 		 * queue interrupts
4293 		 */
4294 		on_each_cpu(mvneta_percpu_enable, pp, true);
4295 
4296 		pp->is_stopped = false;
4297 		/* Register a CPU notifier to handle the case where our CPU
4298 		 * might be taken offline.
4299 		 */
4300 		ret = cpuhp_state_add_instance_nocalls(online_hpstate,
4301 						       &pp->node_online);
4302 		if (ret)
4303 			goto err_free_irq;
4304 
4305 		ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4306 						       &pp->node_dead);
4307 		if (ret)
4308 			goto err_free_online_hp;
4309 	}
4310 
4311 	ret = mvneta_mdio_probe(pp);
4312 	if (ret < 0) {
4313 		netdev_err(dev, "cannot probe MDIO bus\n");
4314 		goto err_free_dead_hp;
4315 	}
4316 
4317 	mvneta_start_dev(pp);
4318 
4319 	return 0;
4320 
4321 err_free_dead_hp:
4322 	if (!pp->neta_armada3700)
4323 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4324 						    &pp->node_dead);
4325 err_free_online_hp:
4326 	if (!pp->neta_armada3700)
4327 		cpuhp_state_remove_instance_nocalls(online_hpstate,
4328 						    &pp->node_online);
4329 err_free_irq:
4330 	if (pp->neta_armada3700) {
4331 		free_irq(pp->dev->irq, pp);
4332 	} else {
4333 		on_each_cpu(mvneta_percpu_disable, pp, true);
4334 		free_percpu_irq(pp->dev->irq, pp->ports);
4335 	}
4336 err_cleanup_txqs:
4337 	mvneta_cleanup_txqs(pp);
4338 err_cleanup_rxqs:
4339 	mvneta_cleanup_rxqs(pp);
4340 	return ret;
4341 }
4342 
4343 /* Stop the port, free port interrupt line */
4344 static int mvneta_stop(struct net_device *dev)
4345 {
4346 	struct mvneta_port *pp = netdev_priv(dev);
4347 
4348 	if (!pp->neta_armada3700) {
4349 		/* Inform that we are stopping so we don't want to setup the
4350 		 * driver for new CPUs in the notifiers. The code of the
4351 		 * notifier for CPU online is protected by the same spinlock,
4352 		 * so when we get the lock, the notifer work is done.
4353 		 */
4354 		spin_lock(&pp->lock);
4355 		pp->is_stopped = true;
4356 		spin_unlock(&pp->lock);
4357 
4358 		mvneta_stop_dev(pp);
4359 		mvneta_mdio_remove(pp);
4360 
4361 		cpuhp_state_remove_instance_nocalls(online_hpstate,
4362 						    &pp->node_online);
4363 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
4364 						    &pp->node_dead);
4365 		on_each_cpu(mvneta_percpu_disable, pp, true);
4366 		free_percpu_irq(dev->irq, pp->ports);
4367 	} else {
4368 		mvneta_stop_dev(pp);
4369 		mvneta_mdio_remove(pp);
4370 		free_irq(dev->irq, pp);
4371 	}
4372 
4373 	mvneta_cleanup_rxqs(pp);
4374 	mvneta_cleanup_txqs(pp);
4375 
4376 	return 0;
4377 }
4378 
4379 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
4380 {
4381 	struct mvneta_port *pp = netdev_priv(dev);
4382 
4383 	return phylink_mii_ioctl(pp->phylink, ifr, cmd);
4384 }
4385 
4386 static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
4387 			    struct netlink_ext_ack *extack)
4388 {
4389 	bool need_update, running = netif_running(dev);
4390 	struct mvneta_port *pp = netdev_priv(dev);
4391 	struct bpf_prog *old_prog;
4392 
4393 	if (prog && dev->mtu > MVNETA_MAX_RX_BUF_SIZE) {
4394 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for XDP");
4395 		return -EOPNOTSUPP;
4396 	}
4397 
4398 	if (pp->bm_priv) {
4399 		NL_SET_ERR_MSG_MOD(extack,
4400 				   "Hardware Buffer Management not supported on XDP");
4401 		return -EOPNOTSUPP;
4402 	}
4403 
4404 	need_update = !!pp->xdp_prog != !!prog;
4405 	if (running && need_update)
4406 		mvneta_stop(dev);
4407 
4408 	old_prog = xchg(&pp->xdp_prog, prog);
4409 	if (old_prog)
4410 		bpf_prog_put(old_prog);
4411 
4412 	if (running && need_update)
4413 		return mvneta_open(dev);
4414 
4415 	return 0;
4416 }
4417 
4418 static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4419 {
4420 	switch (xdp->command) {
4421 	case XDP_SETUP_PROG:
4422 		return mvneta_xdp_setup(dev, xdp->prog, xdp->extack);
4423 	default:
4424 		return -EINVAL;
4425 	}
4426 }
4427 
4428 /* Ethtool methods */
4429 
4430 /* Set link ksettings (phy address, speed) for ethtools */
4431 static int
4432 mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
4433 				  const struct ethtool_link_ksettings *cmd)
4434 {
4435 	struct mvneta_port *pp = netdev_priv(ndev);
4436 
4437 	return phylink_ethtool_ksettings_set(pp->phylink, cmd);
4438 }
4439 
4440 /* Get link ksettings for ethtools */
4441 static int
4442 mvneta_ethtool_get_link_ksettings(struct net_device *ndev,
4443 				  struct ethtool_link_ksettings *cmd)
4444 {
4445 	struct mvneta_port *pp = netdev_priv(ndev);
4446 
4447 	return phylink_ethtool_ksettings_get(pp->phylink, cmd);
4448 }
4449 
4450 static int mvneta_ethtool_nway_reset(struct net_device *dev)
4451 {
4452 	struct mvneta_port *pp = netdev_priv(dev);
4453 
4454 	return phylink_ethtool_nway_reset(pp->phylink);
4455 }
4456 
4457 /* Set interrupt coalescing for ethtools */
4458 static int
4459 mvneta_ethtool_set_coalesce(struct net_device *dev,
4460 			    struct ethtool_coalesce *c,
4461 			    struct kernel_ethtool_coalesce *kernel_coal,
4462 			    struct netlink_ext_ack *extack)
4463 {
4464 	struct mvneta_port *pp = netdev_priv(dev);
4465 	int queue;
4466 
4467 	for (queue = 0; queue < rxq_number; queue++) {
4468 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4469 		rxq->time_coal = c->rx_coalesce_usecs;
4470 		rxq->pkts_coal = c->rx_max_coalesced_frames;
4471 		mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
4472 		mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
4473 	}
4474 
4475 	for (queue = 0; queue < txq_number; queue++) {
4476 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
4477 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
4478 		mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
4479 	}
4480 
4481 	return 0;
4482 }
4483 
4484 /* get coalescing for ethtools */
4485 static int
4486 mvneta_ethtool_get_coalesce(struct net_device *dev,
4487 			    struct ethtool_coalesce *c,
4488 			    struct kernel_ethtool_coalesce *kernel_coal,
4489 			    struct netlink_ext_ack *extack)
4490 {
4491 	struct mvneta_port *pp = netdev_priv(dev);
4492 
4493 	c->rx_coalesce_usecs        = pp->rxqs[0].time_coal;
4494 	c->rx_max_coalesced_frames  = pp->rxqs[0].pkts_coal;
4495 
4496 	c->tx_max_coalesced_frames =  pp->txqs[0].done_pkts_coal;
4497 	return 0;
4498 }
4499 
4500 
4501 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
4502 				    struct ethtool_drvinfo *drvinfo)
4503 {
4504 	strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
4505 		sizeof(drvinfo->driver));
4506 	strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
4507 		sizeof(drvinfo->version));
4508 	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
4509 		sizeof(drvinfo->bus_info));
4510 }
4511 
4512 
4513 static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
4514 					 struct ethtool_ringparam *ring)
4515 {
4516 	struct mvneta_port *pp = netdev_priv(netdev);
4517 
4518 	ring->rx_max_pending = MVNETA_MAX_RXD;
4519 	ring->tx_max_pending = MVNETA_MAX_TXD;
4520 	ring->rx_pending = pp->rx_ring_size;
4521 	ring->tx_pending = pp->tx_ring_size;
4522 }
4523 
4524 static int mvneta_ethtool_set_ringparam(struct net_device *dev,
4525 					struct ethtool_ringparam *ring)
4526 {
4527 	struct mvneta_port *pp = netdev_priv(dev);
4528 
4529 	if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
4530 		return -EINVAL;
4531 	pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
4532 		ring->rx_pending : MVNETA_MAX_RXD;
4533 
4534 	pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
4535 				   MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
4536 	if (pp->tx_ring_size != ring->tx_pending)
4537 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
4538 			    pp->tx_ring_size, ring->tx_pending);
4539 
4540 	if (netif_running(dev)) {
4541 		mvneta_stop(dev);
4542 		if (mvneta_open(dev)) {
4543 			netdev_err(dev,
4544 				   "error on opening device after ring param change\n");
4545 			return -ENOMEM;
4546 		}
4547 	}
4548 
4549 	return 0;
4550 }
4551 
4552 static void mvneta_ethtool_get_pauseparam(struct net_device *dev,
4553 					  struct ethtool_pauseparam *pause)
4554 {
4555 	struct mvneta_port *pp = netdev_priv(dev);
4556 
4557 	phylink_ethtool_get_pauseparam(pp->phylink, pause);
4558 }
4559 
4560 static int mvneta_ethtool_set_pauseparam(struct net_device *dev,
4561 					 struct ethtool_pauseparam *pause)
4562 {
4563 	struct mvneta_port *pp = netdev_priv(dev);
4564 
4565 	return phylink_ethtool_set_pauseparam(pp->phylink, pause);
4566 }
4567 
4568 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
4569 				       u8 *data)
4570 {
4571 	if (sset == ETH_SS_STATS) {
4572 		int i;
4573 
4574 		for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4575 			memcpy(data + i * ETH_GSTRING_LEN,
4576 			       mvneta_statistics[i].name, ETH_GSTRING_LEN);
4577 	}
4578 }
4579 
4580 static void
4581 mvneta_ethtool_update_pcpu_stats(struct mvneta_port *pp,
4582 				 struct mvneta_ethtool_stats *es)
4583 {
4584 	unsigned int start;
4585 	int cpu;
4586 
4587 	for_each_possible_cpu(cpu) {
4588 		struct mvneta_pcpu_stats *stats;
4589 		u64 skb_alloc_error;
4590 		u64 refill_error;
4591 		u64 xdp_redirect;
4592 		u64 xdp_xmit_err;
4593 		u64 xdp_tx_err;
4594 		u64 xdp_pass;
4595 		u64 xdp_drop;
4596 		u64 xdp_xmit;
4597 		u64 xdp_tx;
4598 
4599 		stats = per_cpu_ptr(pp->stats, cpu);
4600 		do {
4601 			start = u64_stats_fetch_begin_irq(&stats->syncp);
4602 			skb_alloc_error = stats->es.skb_alloc_error;
4603 			refill_error = stats->es.refill_error;
4604 			xdp_redirect = stats->es.ps.xdp_redirect;
4605 			xdp_pass = stats->es.ps.xdp_pass;
4606 			xdp_drop = stats->es.ps.xdp_drop;
4607 			xdp_xmit = stats->es.ps.xdp_xmit;
4608 			xdp_xmit_err = stats->es.ps.xdp_xmit_err;
4609 			xdp_tx = stats->es.ps.xdp_tx;
4610 			xdp_tx_err = stats->es.ps.xdp_tx_err;
4611 		} while (u64_stats_fetch_retry_irq(&stats->syncp, start));
4612 
4613 		es->skb_alloc_error += skb_alloc_error;
4614 		es->refill_error += refill_error;
4615 		es->ps.xdp_redirect += xdp_redirect;
4616 		es->ps.xdp_pass += xdp_pass;
4617 		es->ps.xdp_drop += xdp_drop;
4618 		es->ps.xdp_xmit += xdp_xmit;
4619 		es->ps.xdp_xmit_err += xdp_xmit_err;
4620 		es->ps.xdp_tx += xdp_tx;
4621 		es->ps.xdp_tx_err += xdp_tx_err;
4622 	}
4623 }
4624 
4625 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
4626 {
4627 	struct mvneta_ethtool_stats stats = {};
4628 	const struct mvneta_statistic *s;
4629 	void __iomem *base = pp->base;
4630 	u32 high, low;
4631 	u64 val;
4632 	int i;
4633 
4634 	mvneta_ethtool_update_pcpu_stats(pp, &stats);
4635 	for (i = 0, s = mvneta_statistics;
4636 	     s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
4637 	     s++, i++) {
4638 		switch (s->type) {
4639 		case T_REG_32:
4640 			val = readl_relaxed(base + s->offset);
4641 			pp->ethtool_stats[i] += val;
4642 			break;
4643 		case T_REG_64:
4644 			/* Docs say to read low 32-bit then high */
4645 			low = readl_relaxed(base + s->offset);
4646 			high = readl_relaxed(base + s->offset + 4);
4647 			val = (u64)high << 32 | low;
4648 			pp->ethtool_stats[i] += val;
4649 			break;
4650 		case T_SW:
4651 			switch (s->offset) {
4652 			case ETHTOOL_STAT_EEE_WAKEUP:
4653 				val = phylink_get_eee_err(pp->phylink);
4654 				pp->ethtool_stats[i] += val;
4655 				break;
4656 			case ETHTOOL_STAT_SKB_ALLOC_ERR:
4657 				pp->ethtool_stats[i] = stats.skb_alloc_error;
4658 				break;
4659 			case ETHTOOL_STAT_REFILL_ERR:
4660 				pp->ethtool_stats[i] = stats.refill_error;
4661 				break;
4662 			case ETHTOOL_XDP_REDIRECT:
4663 				pp->ethtool_stats[i] = stats.ps.xdp_redirect;
4664 				break;
4665 			case ETHTOOL_XDP_PASS:
4666 				pp->ethtool_stats[i] = stats.ps.xdp_pass;
4667 				break;
4668 			case ETHTOOL_XDP_DROP:
4669 				pp->ethtool_stats[i] = stats.ps.xdp_drop;
4670 				break;
4671 			case ETHTOOL_XDP_TX:
4672 				pp->ethtool_stats[i] = stats.ps.xdp_tx;
4673 				break;
4674 			case ETHTOOL_XDP_TX_ERR:
4675 				pp->ethtool_stats[i] = stats.ps.xdp_tx_err;
4676 				break;
4677 			case ETHTOOL_XDP_XMIT:
4678 				pp->ethtool_stats[i] = stats.ps.xdp_xmit;
4679 				break;
4680 			case ETHTOOL_XDP_XMIT_ERR:
4681 				pp->ethtool_stats[i] = stats.ps.xdp_xmit_err;
4682 				break;
4683 			}
4684 			break;
4685 		}
4686 	}
4687 }
4688 
4689 static void mvneta_ethtool_get_stats(struct net_device *dev,
4690 				     struct ethtool_stats *stats, u64 *data)
4691 {
4692 	struct mvneta_port *pp = netdev_priv(dev);
4693 	int i;
4694 
4695 	mvneta_ethtool_update_stats(pp);
4696 
4697 	for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
4698 		*data++ = pp->ethtool_stats[i];
4699 }
4700 
4701 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
4702 {
4703 	if (sset == ETH_SS_STATS)
4704 		return ARRAY_SIZE(mvneta_statistics);
4705 	return -EOPNOTSUPP;
4706 }
4707 
4708 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
4709 {
4710 	return MVNETA_RSS_LU_TABLE_SIZE;
4711 }
4712 
4713 static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
4714 				    struct ethtool_rxnfc *info,
4715 				    u32 *rules __always_unused)
4716 {
4717 	switch (info->cmd) {
4718 	case ETHTOOL_GRXRINGS:
4719 		info->data =  rxq_number;
4720 		return 0;
4721 	case ETHTOOL_GRXFH:
4722 		return -EOPNOTSUPP;
4723 	default:
4724 		return -EOPNOTSUPP;
4725 	}
4726 }
4727 
4728 static int  mvneta_config_rss(struct mvneta_port *pp)
4729 {
4730 	int cpu;
4731 	u32 val;
4732 
4733 	netif_tx_stop_all_queues(pp->dev);
4734 
4735 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
4736 
4737 	if (!pp->neta_armada3700) {
4738 		/* We have to synchronise on the napi of each CPU */
4739 		for_each_online_cpu(cpu) {
4740 			struct mvneta_pcpu_port *pcpu_port =
4741 				per_cpu_ptr(pp->ports, cpu);
4742 
4743 			napi_synchronize(&pcpu_port->napi);
4744 			napi_disable(&pcpu_port->napi);
4745 		}
4746 	} else {
4747 		napi_synchronize(&pp->napi);
4748 		napi_disable(&pp->napi);
4749 	}
4750 
4751 	pp->rxq_def = pp->indir[0];
4752 
4753 	/* Update unicast mapping */
4754 	mvneta_set_rx_mode(pp->dev);
4755 
4756 	/* Update val of portCfg register accordingly with all RxQueue types */
4757 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
4758 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
4759 
4760 	/* Update the elected CPU matching the new rxq_def */
4761 	spin_lock(&pp->lock);
4762 	mvneta_percpu_elect(pp);
4763 	spin_unlock(&pp->lock);
4764 
4765 	if (!pp->neta_armada3700) {
4766 		/* We have to synchronise on the napi of each CPU */
4767 		for_each_online_cpu(cpu) {
4768 			struct mvneta_pcpu_port *pcpu_port =
4769 				per_cpu_ptr(pp->ports, cpu);
4770 
4771 			napi_enable(&pcpu_port->napi);
4772 		}
4773 	} else {
4774 		napi_enable(&pp->napi);
4775 	}
4776 
4777 	netif_tx_start_all_queues(pp->dev);
4778 
4779 	return 0;
4780 }
4781 
4782 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
4783 				   const u8 *key, const u8 hfunc)
4784 {
4785 	struct mvneta_port *pp = netdev_priv(dev);
4786 
4787 	/* Current code for Armada 3700 doesn't support RSS features yet */
4788 	if (pp->neta_armada3700)
4789 		return -EOPNOTSUPP;
4790 
4791 	/* We require at least one supported parameter to be changed
4792 	 * and no change in any of the unsupported parameters
4793 	 */
4794 	if (key ||
4795 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
4796 		return -EOPNOTSUPP;
4797 
4798 	if (!indir)
4799 		return 0;
4800 
4801 	memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
4802 
4803 	return mvneta_config_rss(pp);
4804 }
4805 
4806 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
4807 				   u8 *hfunc)
4808 {
4809 	struct mvneta_port *pp = netdev_priv(dev);
4810 
4811 	/* Current code for Armada 3700 doesn't support RSS features yet */
4812 	if (pp->neta_armada3700)
4813 		return -EOPNOTSUPP;
4814 
4815 	if (hfunc)
4816 		*hfunc = ETH_RSS_HASH_TOP;
4817 
4818 	if (!indir)
4819 		return 0;
4820 
4821 	memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
4822 
4823 	return 0;
4824 }
4825 
4826 static void mvneta_ethtool_get_wol(struct net_device *dev,
4827 				   struct ethtool_wolinfo *wol)
4828 {
4829 	struct mvneta_port *pp = netdev_priv(dev);
4830 
4831 	phylink_ethtool_get_wol(pp->phylink, wol);
4832 }
4833 
4834 static int mvneta_ethtool_set_wol(struct net_device *dev,
4835 				  struct ethtool_wolinfo *wol)
4836 {
4837 	struct mvneta_port *pp = netdev_priv(dev);
4838 	int ret;
4839 
4840 	ret = phylink_ethtool_set_wol(pp->phylink, wol);
4841 	if (!ret)
4842 		device_set_wakeup_enable(&dev->dev, !!wol->wolopts);
4843 
4844 	return ret;
4845 }
4846 
4847 static int mvneta_ethtool_get_eee(struct net_device *dev,
4848 				  struct ethtool_eee *eee)
4849 {
4850 	struct mvneta_port *pp = netdev_priv(dev);
4851 	u32 lpi_ctl0;
4852 
4853 	lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4854 
4855 	eee->eee_enabled = pp->eee_enabled;
4856 	eee->eee_active = pp->eee_active;
4857 	eee->tx_lpi_enabled = pp->tx_lpi_enabled;
4858 	eee->tx_lpi_timer = (lpi_ctl0) >> 8; // * scale;
4859 
4860 	return phylink_ethtool_get_eee(pp->phylink, eee);
4861 }
4862 
4863 static int mvneta_ethtool_set_eee(struct net_device *dev,
4864 				  struct ethtool_eee *eee)
4865 {
4866 	struct mvneta_port *pp = netdev_priv(dev);
4867 	u32 lpi_ctl0;
4868 
4869 	/* The Armada 37x documents do not give limits for this other than
4870 	 * it being an 8-bit register.
4871 	 */
4872 	if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255)
4873 		return -EINVAL;
4874 
4875 	lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
4876 	lpi_ctl0 &= ~(0xff << 8);
4877 	lpi_ctl0 |= eee->tx_lpi_timer << 8;
4878 	mvreg_write(pp, MVNETA_LPI_CTRL_0, lpi_ctl0);
4879 
4880 	pp->eee_enabled = eee->eee_enabled;
4881 	pp->tx_lpi_enabled = eee->tx_lpi_enabled;
4882 
4883 	mvneta_set_eee(pp, eee->tx_lpi_enabled && eee->eee_enabled);
4884 
4885 	return phylink_ethtool_set_eee(pp->phylink, eee);
4886 }
4887 
4888 static void mvneta_clear_rx_prio_map(struct mvneta_port *pp)
4889 {
4890 	mvreg_write(pp, MVNETA_VLAN_PRIO_TO_RXQ, 0);
4891 }
4892 
4893 static void mvneta_setup_rx_prio_map(struct mvneta_port *pp)
4894 {
4895 	u32 val = 0;
4896 	int i;
4897 
4898 	for (i = 0; i < rxq_number; i++)
4899 		val |= MVNETA_VLAN_PRIO_RXQ_MAP(i, pp->prio_tc_map[i]);
4900 
4901 	mvreg_write(pp, MVNETA_VLAN_PRIO_TO_RXQ, val);
4902 }
4903 
4904 static int mvneta_setup_mqprio(struct net_device *dev,
4905 			       struct tc_mqprio_qopt *qopt)
4906 {
4907 	struct mvneta_port *pp = netdev_priv(dev);
4908 	u8 num_tc;
4909 	int i;
4910 
4911 	qopt->hw = TC_MQPRIO_HW_OFFLOAD_TCS;
4912 	num_tc = qopt->num_tc;
4913 
4914 	if (num_tc > rxq_number)
4915 		return -EINVAL;
4916 
4917 	if (!num_tc) {
4918 		mvneta_clear_rx_prio_map(pp);
4919 		netdev_reset_tc(dev);
4920 		return 0;
4921 	}
4922 
4923 	memcpy(pp->prio_tc_map, qopt->prio_tc_map, sizeof(pp->prio_tc_map));
4924 
4925 	mvneta_setup_rx_prio_map(pp);
4926 
4927 	netdev_set_num_tc(dev, qopt->num_tc);
4928 	for (i = 0; i < qopt->num_tc; i++)
4929 		netdev_set_tc_queue(dev, i, qopt->count[i], qopt->offset[i]);
4930 
4931 	return 0;
4932 }
4933 
4934 static int mvneta_setup_tc(struct net_device *dev, enum tc_setup_type type,
4935 			   void *type_data)
4936 {
4937 	switch (type) {
4938 	case TC_SETUP_QDISC_MQPRIO:
4939 		return mvneta_setup_mqprio(dev, type_data);
4940 	default:
4941 		return -EOPNOTSUPP;
4942 	}
4943 }
4944 
4945 static const struct net_device_ops mvneta_netdev_ops = {
4946 	.ndo_open            = mvneta_open,
4947 	.ndo_stop            = mvneta_stop,
4948 	.ndo_start_xmit      = mvneta_tx,
4949 	.ndo_set_rx_mode     = mvneta_set_rx_mode,
4950 	.ndo_set_mac_address = mvneta_set_mac_addr,
4951 	.ndo_change_mtu      = mvneta_change_mtu,
4952 	.ndo_fix_features    = mvneta_fix_features,
4953 	.ndo_get_stats64     = mvneta_get_stats64,
4954 	.ndo_eth_ioctl        = mvneta_ioctl,
4955 	.ndo_bpf	     = mvneta_xdp,
4956 	.ndo_xdp_xmit        = mvneta_xdp_xmit,
4957 	.ndo_setup_tc	     = mvneta_setup_tc,
4958 };
4959 
4960 static const struct ethtool_ops mvneta_eth_tool_ops = {
4961 	.supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
4962 				     ETHTOOL_COALESCE_MAX_FRAMES,
4963 	.nway_reset	= mvneta_ethtool_nway_reset,
4964 	.get_link       = ethtool_op_get_link,
4965 	.set_coalesce   = mvneta_ethtool_set_coalesce,
4966 	.get_coalesce   = mvneta_ethtool_get_coalesce,
4967 	.get_drvinfo    = mvneta_ethtool_get_drvinfo,
4968 	.get_ringparam  = mvneta_ethtool_get_ringparam,
4969 	.set_ringparam	= mvneta_ethtool_set_ringparam,
4970 	.get_pauseparam	= mvneta_ethtool_get_pauseparam,
4971 	.set_pauseparam	= mvneta_ethtool_set_pauseparam,
4972 	.get_strings	= mvneta_ethtool_get_strings,
4973 	.get_ethtool_stats = mvneta_ethtool_get_stats,
4974 	.get_sset_count	= mvneta_ethtool_get_sset_count,
4975 	.get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
4976 	.get_rxnfc	= mvneta_ethtool_get_rxnfc,
4977 	.get_rxfh	= mvneta_ethtool_get_rxfh,
4978 	.set_rxfh	= mvneta_ethtool_set_rxfh,
4979 	.get_link_ksettings = mvneta_ethtool_get_link_ksettings,
4980 	.set_link_ksettings = mvneta_ethtool_set_link_ksettings,
4981 	.get_wol        = mvneta_ethtool_get_wol,
4982 	.set_wol        = mvneta_ethtool_set_wol,
4983 	.get_eee	= mvneta_ethtool_get_eee,
4984 	.set_eee	= mvneta_ethtool_set_eee,
4985 };
4986 
4987 /* Initialize hw */
4988 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
4989 {
4990 	int queue;
4991 
4992 	/* Disable port */
4993 	mvneta_port_disable(pp);
4994 
4995 	/* Set port default values */
4996 	mvneta_defaults_set(pp);
4997 
4998 	pp->txqs = devm_kcalloc(dev, txq_number, sizeof(*pp->txqs), GFP_KERNEL);
4999 	if (!pp->txqs)
5000 		return -ENOMEM;
5001 
5002 	/* Initialize TX descriptor rings */
5003 	for (queue = 0; queue < txq_number; queue++) {
5004 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
5005 		txq->id = queue;
5006 		txq->size = pp->tx_ring_size;
5007 		txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
5008 	}
5009 
5010 	pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(*pp->rxqs), GFP_KERNEL);
5011 	if (!pp->rxqs)
5012 		return -ENOMEM;
5013 
5014 	/* Create Rx descriptor rings */
5015 	for (queue = 0; queue < rxq_number; queue++) {
5016 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5017 		rxq->id = queue;
5018 		rxq->size = pp->rx_ring_size;
5019 		rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
5020 		rxq->time_coal = MVNETA_RX_COAL_USEC;
5021 		rxq->buf_virt_addr
5022 			= devm_kmalloc_array(pp->dev->dev.parent,
5023 					     rxq->size,
5024 					     sizeof(*rxq->buf_virt_addr),
5025 					     GFP_KERNEL);
5026 		if (!rxq->buf_virt_addr)
5027 			return -ENOMEM;
5028 	}
5029 
5030 	return 0;
5031 }
5032 
5033 /* platform glue : initialize decoding windows */
5034 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
5035 				     const struct mbus_dram_target_info *dram)
5036 {
5037 	u32 win_enable;
5038 	u32 win_protect;
5039 	int i;
5040 
5041 	for (i = 0; i < 6; i++) {
5042 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
5043 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
5044 
5045 		if (i < 4)
5046 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
5047 	}
5048 
5049 	win_enable = 0x3f;
5050 	win_protect = 0;
5051 
5052 	if (dram) {
5053 		for (i = 0; i < dram->num_cs; i++) {
5054 			const struct mbus_dram_window *cs = dram->cs + i;
5055 
5056 			mvreg_write(pp, MVNETA_WIN_BASE(i),
5057 				    (cs->base & 0xffff0000) |
5058 				    (cs->mbus_attr << 8) |
5059 				    dram->mbus_dram_target_id);
5060 
5061 			mvreg_write(pp, MVNETA_WIN_SIZE(i),
5062 				    (cs->size - 1) & 0xffff0000);
5063 
5064 			win_enable &= ~(1 << i);
5065 			win_protect |= 3 << (2 * i);
5066 		}
5067 	} else {
5068 		/* For Armada3700 open default 4GB Mbus window, leaving
5069 		 * arbitration of target/attribute to a different layer
5070 		 * of configuration.
5071 		 */
5072 		mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
5073 		win_enable &= ~BIT(0);
5074 		win_protect = 3;
5075 	}
5076 
5077 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
5078 	mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
5079 }
5080 
5081 /* Power up the port */
5082 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
5083 {
5084 	/* MAC Cause register should be cleared */
5085 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
5086 
5087 	if (phy_mode != PHY_INTERFACE_MODE_QSGMII &&
5088 	    phy_mode != PHY_INTERFACE_MODE_SGMII &&
5089 	    !phy_interface_mode_is_8023z(phy_mode) &&
5090 	    !phy_interface_mode_is_rgmii(phy_mode))
5091 		return -EINVAL;
5092 
5093 	return 0;
5094 }
5095 
5096 /* Device initialization routine */
5097 static int mvneta_probe(struct platform_device *pdev)
5098 {
5099 	struct device_node *dn = pdev->dev.of_node;
5100 	struct device_node *bm_node;
5101 	struct mvneta_port *pp;
5102 	struct net_device *dev;
5103 	struct phylink *phylink;
5104 	struct phy *comphy;
5105 	char hw_mac_addr[ETH_ALEN];
5106 	phy_interface_t phy_mode;
5107 	const char *mac_from;
5108 	int tx_csum_limit;
5109 	int err;
5110 	int cpu;
5111 
5112 	dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port),
5113 				      txq_number, rxq_number);
5114 	if (!dev)
5115 		return -ENOMEM;
5116 
5117 	dev->irq = irq_of_parse_and_map(dn, 0);
5118 	if (dev->irq == 0)
5119 		return -EINVAL;
5120 
5121 	err = of_get_phy_mode(dn, &phy_mode);
5122 	if (err) {
5123 		dev_err(&pdev->dev, "incorrect phy-mode\n");
5124 		goto err_free_irq;
5125 	}
5126 
5127 	comphy = devm_of_phy_get(&pdev->dev, dn, NULL);
5128 	if (comphy == ERR_PTR(-EPROBE_DEFER)) {
5129 		err = -EPROBE_DEFER;
5130 		goto err_free_irq;
5131 	} else if (IS_ERR(comphy)) {
5132 		comphy = NULL;
5133 	}
5134 
5135 	pp = netdev_priv(dev);
5136 	spin_lock_init(&pp->lock);
5137 
5138 	pp->phylink_config.dev = &dev->dev;
5139 	pp->phylink_config.type = PHYLINK_NETDEV;
5140 	pp->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 |
5141 		MAC_100 | MAC_1000FD | MAC_2500FD;
5142 
5143 	phy_interface_set_rgmii(pp->phylink_config.supported_interfaces);
5144 	__set_bit(PHY_INTERFACE_MODE_QSGMII,
5145 		  pp->phylink_config.supported_interfaces);
5146 	if (comphy) {
5147 		/* If a COMPHY is present, we can support any of the serdes
5148 		 * modes and switch between them.
5149 		 */
5150 		__set_bit(PHY_INTERFACE_MODE_SGMII,
5151 			  pp->phylink_config.supported_interfaces);
5152 		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
5153 			  pp->phylink_config.supported_interfaces);
5154 		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
5155 			  pp->phylink_config.supported_interfaces);
5156 	} else if (phy_mode == PHY_INTERFACE_MODE_2500BASEX) {
5157 		/* No COMPHY, with only 2500BASE-X mode supported */
5158 		__set_bit(PHY_INTERFACE_MODE_2500BASEX,
5159 			  pp->phylink_config.supported_interfaces);
5160 	} else if (phy_mode == PHY_INTERFACE_MODE_1000BASEX ||
5161 		   phy_mode == PHY_INTERFACE_MODE_SGMII) {
5162 		/* No COMPHY, we can switch between 1000BASE-X and SGMII */
5163 		__set_bit(PHY_INTERFACE_MODE_1000BASEX,
5164 			  pp->phylink_config.supported_interfaces);
5165 		__set_bit(PHY_INTERFACE_MODE_SGMII,
5166 			  pp->phylink_config.supported_interfaces);
5167 	}
5168 
5169 	phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode,
5170 				 phy_mode, &mvneta_phylink_ops);
5171 	if (IS_ERR(phylink)) {
5172 		err = PTR_ERR(phylink);
5173 		goto err_free_irq;
5174 	}
5175 
5176 	dev->tx_queue_len = MVNETA_MAX_TXD;
5177 	dev->watchdog_timeo = 5 * HZ;
5178 	dev->netdev_ops = &mvneta_netdev_ops;
5179 
5180 	dev->ethtool_ops = &mvneta_eth_tool_ops;
5181 
5182 	pp->phylink = phylink;
5183 	pp->comphy = comphy;
5184 	pp->phy_interface = phy_mode;
5185 	pp->dn = dn;
5186 
5187 	pp->rxq_def = rxq_def;
5188 	pp->indir[0] = rxq_def;
5189 
5190 	/* Get special SoC configurations */
5191 	if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
5192 		pp->neta_armada3700 = true;
5193 
5194 	pp->clk = devm_clk_get(&pdev->dev, "core");
5195 	if (IS_ERR(pp->clk))
5196 		pp->clk = devm_clk_get(&pdev->dev, NULL);
5197 	if (IS_ERR(pp->clk)) {
5198 		err = PTR_ERR(pp->clk);
5199 		goto err_free_phylink;
5200 	}
5201 
5202 	clk_prepare_enable(pp->clk);
5203 
5204 	pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
5205 	if (!IS_ERR(pp->clk_bus))
5206 		clk_prepare_enable(pp->clk_bus);
5207 
5208 	pp->base = devm_platform_ioremap_resource(pdev, 0);
5209 	if (IS_ERR(pp->base)) {
5210 		err = PTR_ERR(pp->base);
5211 		goto err_clk;
5212 	}
5213 
5214 	/* Alloc per-cpu port structure */
5215 	pp->ports = alloc_percpu(struct mvneta_pcpu_port);
5216 	if (!pp->ports) {
5217 		err = -ENOMEM;
5218 		goto err_clk;
5219 	}
5220 
5221 	/* Alloc per-cpu stats */
5222 	pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
5223 	if (!pp->stats) {
5224 		err = -ENOMEM;
5225 		goto err_free_ports;
5226 	}
5227 
5228 	err = of_get_ethdev_address(dn, dev);
5229 	if (!err) {
5230 		mac_from = "device tree";
5231 	} else {
5232 		mvneta_get_mac_addr(pp, hw_mac_addr);
5233 		if (is_valid_ether_addr(hw_mac_addr)) {
5234 			mac_from = "hardware";
5235 			eth_hw_addr_set(dev, hw_mac_addr);
5236 		} else {
5237 			mac_from = "random";
5238 			eth_hw_addr_random(dev);
5239 		}
5240 	}
5241 
5242 	if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
5243 		if (tx_csum_limit < 0 ||
5244 		    tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
5245 			tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
5246 			dev_info(&pdev->dev,
5247 				 "Wrong TX csum limit in DT, set to %dB\n",
5248 				 MVNETA_TX_CSUM_DEF_SIZE);
5249 		}
5250 	} else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
5251 		tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
5252 	} else {
5253 		tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
5254 	}
5255 
5256 	pp->tx_csum_limit = tx_csum_limit;
5257 
5258 	pp->dram_target_info = mv_mbus_dram_info();
5259 	/* Armada3700 requires setting default configuration of Mbus
5260 	 * windows, however without using filled mbus_dram_target_info
5261 	 * structure.
5262 	 */
5263 	if (pp->dram_target_info || pp->neta_armada3700)
5264 		mvneta_conf_mbus_windows(pp, pp->dram_target_info);
5265 
5266 	pp->tx_ring_size = MVNETA_MAX_TXD;
5267 	pp->rx_ring_size = MVNETA_MAX_RXD;
5268 
5269 	pp->dev = dev;
5270 	SET_NETDEV_DEV(dev, &pdev->dev);
5271 
5272 	pp->id = global_port_id++;
5273 
5274 	/* Obtain access to BM resources if enabled and already initialized */
5275 	bm_node = of_parse_phandle(dn, "buffer-manager", 0);
5276 	if (bm_node) {
5277 		pp->bm_priv = mvneta_bm_get(bm_node);
5278 		if (pp->bm_priv) {
5279 			err = mvneta_bm_port_init(pdev, pp);
5280 			if (err < 0) {
5281 				dev_info(&pdev->dev,
5282 					 "use SW buffer management\n");
5283 				mvneta_bm_put(pp->bm_priv);
5284 				pp->bm_priv = NULL;
5285 			}
5286 		}
5287 		/* Set RX packet offset correction for platforms, whose
5288 		 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
5289 		 * platforms and 0B for 32-bit ones.
5290 		 */
5291 		pp->rx_offset_correction = max(0,
5292 					       NET_SKB_PAD -
5293 					       MVNETA_RX_PKT_OFFSET_CORRECTION);
5294 	}
5295 	of_node_put(bm_node);
5296 
5297 	/* sw buffer management */
5298 	if (!pp->bm_priv)
5299 		pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5300 
5301 	err = mvneta_init(&pdev->dev, pp);
5302 	if (err < 0)
5303 		goto err_netdev;
5304 
5305 	err = mvneta_port_power_up(pp, pp->phy_interface);
5306 	if (err < 0) {
5307 		dev_err(&pdev->dev, "can't power up port\n");
5308 		goto err_netdev;
5309 	}
5310 
5311 	/* Armada3700 network controller does not support per-cpu
5312 	 * operation, so only single NAPI should be initialized.
5313 	 */
5314 	if (pp->neta_armada3700) {
5315 		netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
5316 	} else {
5317 		for_each_present_cpu(cpu) {
5318 			struct mvneta_pcpu_port *port =
5319 				per_cpu_ptr(pp->ports, cpu);
5320 
5321 			netif_napi_add(dev, &port->napi, mvneta_poll,
5322 				       NAPI_POLL_WEIGHT);
5323 			port->pp = pp;
5324 		}
5325 	}
5326 
5327 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5328 			NETIF_F_TSO | NETIF_F_RXCSUM;
5329 	dev->hw_features |= dev->features;
5330 	dev->vlan_features |= dev->features;
5331 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
5332 	dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
5333 
5334 	/* MTU range: 68 - 9676 */
5335 	dev->min_mtu = ETH_MIN_MTU;
5336 	/* 9676 == 9700 - 20 and rounding to 8 */
5337 	dev->max_mtu = 9676;
5338 
5339 	err = register_netdev(dev);
5340 	if (err < 0) {
5341 		dev_err(&pdev->dev, "failed to register\n");
5342 		goto err_netdev;
5343 	}
5344 
5345 	netdev_info(dev, "Using %s mac address %pM\n", mac_from,
5346 		    dev->dev_addr);
5347 
5348 	platform_set_drvdata(pdev, pp->dev);
5349 
5350 	return 0;
5351 
5352 err_netdev:
5353 	if (pp->bm_priv) {
5354 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5355 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5356 				       1 << pp->id);
5357 		mvneta_bm_put(pp->bm_priv);
5358 	}
5359 	free_percpu(pp->stats);
5360 err_free_ports:
5361 	free_percpu(pp->ports);
5362 err_clk:
5363 	clk_disable_unprepare(pp->clk_bus);
5364 	clk_disable_unprepare(pp->clk);
5365 err_free_phylink:
5366 	if (pp->phylink)
5367 		phylink_destroy(pp->phylink);
5368 err_free_irq:
5369 	irq_dispose_mapping(dev->irq);
5370 	return err;
5371 }
5372 
5373 /* Device removal routine */
5374 static int mvneta_remove(struct platform_device *pdev)
5375 {
5376 	struct net_device  *dev = platform_get_drvdata(pdev);
5377 	struct mvneta_port *pp = netdev_priv(dev);
5378 
5379 	unregister_netdev(dev);
5380 	clk_disable_unprepare(pp->clk_bus);
5381 	clk_disable_unprepare(pp->clk);
5382 	free_percpu(pp->ports);
5383 	free_percpu(pp->stats);
5384 	irq_dispose_mapping(dev->irq);
5385 	phylink_destroy(pp->phylink);
5386 
5387 	if (pp->bm_priv) {
5388 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
5389 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
5390 				       1 << pp->id);
5391 		mvneta_bm_put(pp->bm_priv);
5392 	}
5393 
5394 	return 0;
5395 }
5396 
5397 #ifdef CONFIG_PM_SLEEP
5398 static int mvneta_suspend(struct device *device)
5399 {
5400 	int queue;
5401 	struct net_device *dev = dev_get_drvdata(device);
5402 	struct mvneta_port *pp = netdev_priv(dev);
5403 
5404 	if (!netif_running(dev))
5405 		goto clean_exit;
5406 
5407 	if (!pp->neta_armada3700) {
5408 		spin_lock(&pp->lock);
5409 		pp->is_stopped = true;
5410 		spin_unlock(&pp->lock);
5411 
5412 		cpuhp_state_remove_instance_nocalls(online_hpstate,
5413 						    &pp->node_online);
5414 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5415 						    &pp->node_dead);
5416 	}
5417 
5418 	rtnl_lock();
5419 	mvneta_stop_dev(pp);
5420 	rtnl_unlock();
5421 
5422 	for (queue = 0; queue < rxq_number; queue++) {
5423 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5424 
5425 		mvneta_rxq_drop_pkts(pp, rxq);
5426 	}
5427 
5428 	for (queue = 0; queue < txq_number; queue++) {
5429 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
5430 
5431 		mvneta_txq_hw_deinit(pp, txq);
5432 	}
5433 
5434 clean_exit:
5435 	netif_device_detach(dev);
5436 	clk_disable_unprepare(pp->clk_bus);
5437 	clk_disable_unprepare(pp->clk);
5438 
5439 	return 0;
5440 }
5441 
5442 static int mvneta_resume(struct device *device)
5443 {
5444 	struct platform_device *pdev = to_platform_device(device);
5445 	struct net_device *dev = dev_get_drvdata(device);
5446 	struct mvneta_port *pp = netdev_priv(dev);
5447 	int err, queue;
5448 
5449 	clk_prepare_enable(pp->clk);
5450 	if (!IS_ERR(pp->clk_bus))
5451 		clk_prepare_enable(pp->clk_bus);
5452 	if (pp->dram_target_info || pp->neta_armada3700)
5453 		mvneta_conf_mbus_windows(pp, pp->dram_target_info);
5454 	if (pp->bm_priv) {
5455 		err = mvneta_bm_port_init(pdev, pp);
5456 		if (err < 0) {
5457 			dev_info(&pdev->dev, "use SW buffer management\n");
5458 			pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5459 			pp->bm_priv = NULL;
5460 		}
5461 	}
5462 	mvneta_defaults_set(pp);
5463 	err = mvneta_port_power_up(pp, pp->phy_interface);
5464 	if (err < 0) {
5465 		dev_err(device, "can't power up port\n");
5466 		return err;
5467 	}
5468 
5469 	netif_device_attach(dev);
5470 
5471 	if (!netif_running(dev))
5472 		return 0;
5473 
5474 	for (queue = 0; queue < rxq_number; queue++) {
5475 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
5476 
5477 		rxq->next_desc_to_proc = 0;
5478 		mvneta_rxq_hw_init(pp, rxq);
5479 	}
5480 
5481 	for (queue = 0; queue < txq_number; queue++) {
5482 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
5483 
5484 		txq->next_desc_to_proc = 0;
5485 		mvneta_txq_hw_init(pp, txq);
5486 	}
5487 
5488 	if (!pp->neta_armada3700) {
5489 		spin_lock(&pp->lock);
5490 		pp->is_stopped = false;
5491 		spin_unlock(&pp->lock);
5492 		cpuhp_state_add_instance_nocalls(online_hpstate,
5493 						 &pp->node_online);
5494 		cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
5495 						 &pp->node_dead);
5496 	}
5497 
5498 	rtnl_lock();
5499 	mvneta_start_dev(pp);
5500 	rtnl_unlock();
5501 	mvneta_set_rx_mode(dev);
5502 
5503 	return 0;
5504 }
5505 #endif
5506 
5507 static SIMPLE_DEV_PM_OPS(mvneta_pm_ops, mvneta_suspend, mvneta_resume);
5508 
5509 static const struct of_device_id mvneta_match[] = {
5510 	{ .compatible = "marvell,armada-370-neta" },
5511 	{ .compatible = "marvell,armada-xp-neta" },
5512 	{ .compatible = "marvell,armada-3700-neta" },
5513 	{ }
5514 };
5515 MODULE_DEVICE_TABLE(of, mvneta_match);
5516 
5517 static struct platform_driver mvneta_driver = {
5518 	.probe = mvneta_probe,
5519 	.remove = mvneta_remove,
5520 	.driver = {
5521 		.name = MVNETA_DRIVER_NAME,
5522 		.of_match_table = mvneta_match,
5523 		.pm = &mvneta_pm_ops,
5524 	},
5525 };
5526 
5527 static int __init mvneta_driver_init(void)
5528 {
5529 	int ret;
5530 
5531 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvneta:online",
5532 				      mvneta_cpu_online,
5533 				      mvneta_cpu_down_prepare);
5534 	if (ret < 0)
5535 		goto out;
5536 	online_hpstate = ret;
5537 	ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
5538 				      NULL, mvneta_cpu_dead);
5539 	if (ret)
5540 		goto err_dead;
5541 
5542 	ret = platform_driver_register(&mvneta_driver);
5543 	if (ret)
5544 		goto err;
5545 	return 0;
5546 
5547 err:
5548 	cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5549 err_dead:
5550 	cpuhp_remove_multi_state(online_hpstate);
5551 out:
5552 	return ret;
5553 }
5554 module_init(mvneta_driver_init);
5555 
5556 static void __exit mvneta_driver_exit(void)
5557 {
5558 	platform_driver_unregister(&mvneta_driver);
5559 	cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
5560 	cpuhp_remove_multi_state(online_hpstate);
5561 }
5562 module_exit(mvneta_driver_exit);
5563 
5564 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
5565 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
5566 MODULE_LICENSE("GPL");
5567 
5568 module_param(rxq_number, int, 0444);
5569 module_param(txq_number, int, 0444);
5570 
5571 module_param(rxq_def, int, 0444);
5572 module_param(rx_copybreak, int, 0644);
5573