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