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