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