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.h>
31 #include <linux/phy_fixed.h>
32 #include <linux/platform_device.h>
33 #include <linux/skbuff.h>
34 #include <net/hwbm.h>
35 #include "mvneta_bm.h"
36 #include <net/ip.h>
37 #include <net/ipv6.h>
38 #include <net/tso.h>
39 
40 /* Registers */
41 #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
42 #define      MVNETA_RXQ_HW_BUF_ALLOC            BIT(0)
43 #define      MVNETA_RXQ_SHORT_POOL_ID_SHIFT	4
44 #define      MVNETA_RXQ_SHORT_POOL_ID_MASK	0x30
45 #define      MVNETA_RXQ_LONG_POOL_ID_SHIFT	6
46 #define      MVNETA_RXQ_LONG_POOL_ID_MASK	0xc0
47 #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
48 #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
49 #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
50 #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
51 #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
52 #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
53 #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
54 #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
55 #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
56 #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
57 #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
58 #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
59 #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
60 #define MVNETA_PORT_POOL_BUFFER_SZ_REG(pool)	(0x1700 + ((pool) << 2))
61 #define      MVNETA_PORT_POOL_BUFFER_SZ_SHIFT	3
62 #define      MVNETA_PORT_POOL_BUFFER_SZ_MASK	0xfff8
63 #define MVNETA_PORT_RX_RESET                    0x1cc0
64 #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
65 #define MVNETA_PHY_ADDR                         0x2000
66 #define      MVNETA_PHY_ADDR_MASK               0x1f
67 #define MVNETA_MBUS_RETRY                       0x2010
68 #define MVNETA_UNIT_INTR_CAUSE                  0x2080
69 #define MVNETA_UNIT_CONTROL                     0x20B0
70 #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
71 #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
72 #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
73 #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
74 #define MVNETA_BASE_ADDR_ENABLE                 0x2290
75 #define MVNETA_ACCESS_PROTECT_ENABLE            0x2294
76 #define MVNETA_PORT_CONFIG                      0x2400
77 #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
78 #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
79 #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
80 #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
81 #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
82 #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
83 #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
84 #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
85 #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
86 						 MVNETA_DEF_RXQ_ARP(q)	 | \
87 						 MVNETA_DEF_RXQ_TCP(q)	 | \
88 						 MVNETA_DEF_RXQ_UDP(q)	 | \
89 						 MVNETA_DEF_RXQ_BPDU(q)	 | \
90 						 MVNETA_TX_UNSET_ERR_SUM | \
91 						 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
92 #define MVNETA_PORT_CONFIG_EXTEND                0x2404
93 #define MVNETA_MAC_ADDR_LOW                      0x2414
94 #define MVNETA_MAC_ADDR_HIGH                     0x2418
95 #define MVNETA_SDMA_CONFIG                       0x241c
96 #define      MVNETA_SDMA_BRST_SIZE_16            4
97 #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
98 #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
99 #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
100 #define      MVNETA_DESC_SWAP                    BIT(6)
101 #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
102 #define MVNETA_PORT_STATUS                       0x2444
103 #define      MVNETA_TX_IN_PRGRS                  BIT(1)
104 #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
105 #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
106 #define MVNETA_SERDES_CFG			 0x24A0
107 #define      MVNETA_SGMII_SERDES_PROTO		 0x0cc7
108 #define      MVNETA_QSGMII_SERDES_PROTO		 0x0667
109 #define MVNETA_TYPE_PRIO                         0x24bc
110 #define      MVNETA_FORCE_UNI                    BIT(21)
111 #define MVNETA_TXQ_CMD_1                         0x24e4
112 #define MVNETA_TXQ_CMD                           0x2448
113 #define      MVNETA_TXQ_DISABLE_SHIFT            8
114 #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
115 #define MVNETA_RX_DISCARD_FRAME_COUNT		 0x2484
116 #define MVNETA_OVERRUN_FRAME_COUNT		 0x2488
117 #define MVNETA_GMAC_CLOCK_DIVIDER                0x24f4
118 #define      MVNETA_GMAC_1MS_CLOCK_ENABLE        BIT(31)
119 #define MVNETA_ACC_MODE                          0x2500
120 #define MVNETA_BM_ADDRESS                        0x2504
121 #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
122 #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
123 #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
124 #define      MVNETA_CPU_RXQ_ACCESS(rxq)		 BIT(rxq)
125 #define      MVNETA_CPU_TXQ_ACCESS(txq)		 BIT(txq + 8)
126 #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
127 
128 /* Exception Interrupt Port/Queue Cause register
129  *
130  * Their behavior depend of the mapping done using the PCPX2Q
131  * registers. For a given CPU if the bit associated to a queue is not
132  * set, then for the register a read from this CPU will always return
133  * 0 and a write won't do anything
134  */
135 
136 #define MVNETA_INTR_NEW_CAUSE                    0x25a0
137 #define MVNETA_INTR_NEW_MASK                     0x25a4
138 
139 /* bits  0..7  = TXQ SENT, one bit per queue.
140  * bits  8..15 = RXQ OCCUP, one bit per queue.
141  * bits 16..23 = RXQ FREE, one bit per queue.
142  * bit  29 = OLD_REG_SUM, see old reg ?
143  * bit  30 = TX_ERR_SUM, one bit for 4 ports
144  * bit  31 = MISC_SUM,   one bit for 4 ports
145  */
146 #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
147 #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
148 #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
149 #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
150 #define      MVNETA_MISCINTR_INTR_MASK           BIT(31)
151 
152 #define MVNETA_INTR_OLD_CAUSE                    0x25a8
153 #define MVNETA_INTR_OLD_MASK                     0x25ac
154 
155 /* Data Path Port/Queue Cause Register */
156 #define MVNETA_INTR_MISC_CAUSE                   0x25b0
157 #define MVNETA_INTR_MISC_MASK                    0x25b4
158 
159 #define      MVNETA_CAUSE_PHY_STATUS_CHANGE      BIT(0)
160 #define      MVNETA_CAUSE_LINK_CHANGE            BIT(1)
161 #define      MVNETA_CAUSE_PTP                    BIT(4)
162 
163 #define      MVNETA_CAUSE_INTERNAL_ADDR_ERR      BIT(7)
164 #define      MVNETA_CAUSE_RX_OVERRUN             BIT(8)
165 #define      MVNETA_CAUSE_RX_CRC_ERROR           BIT(9)
166 #define      MVNETA_CAUSE_RX_LARGE_PKT           BIT(10)
167 #define      MVNETA_CAUSE_TX_UNDERUN             BIT(11)
168 #define      MVNETA_CAUSE_PRBS_ERR               BIT(12)
169 #define      MVNETA_CAUSE_PSC_SYNC_CHANGE        BIT(13)
170 #define      MVNETA_CAUSE_SERDES_SYNC_ERR        BIT(14)
171 
172 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT    16
173 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_ALL_MASK   (0xF << MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT)
174 #define      MVNETA_CAUSE_BMU_ALLOC_ERR_MASK(pool) (1 << (MVNETA_CAUSE_BMU_ALLOC_ERR_SHIFT + (pool)))
175 
176 #define      MVNETA_CAUSE_TXQ_ERROR_SHIFT        24
177 #define      MVNETA_CAUSE_TXQ_ERROR_ALL_MASK     (0xFF << MVNETA_CAUSE_TXQ_ERROR_SHIFT)
178 #define      MVNETA_CAUSE_TXQ_ERROR_MASK(q)      (1 << (MVNETA_CAUSE_TXQ_ERROR_SHIFT + (q)))
179 
180 #define MVNETA_INTR_ENABLE                       0x25b8
181 #define      MVNETA_TXQ_INTR_ENABLE_ALL_MASK     0x0000ff00
182 #define      MVNETA_RXQ_INTR_ENABLE_ALL_MASK     0x000000ff
183 
184 #define MVNETA_RXQ_CMD                           0x2680
185 #define      MVNETA_RXQ_DISABLE_SHIFT            8
186 #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
187 #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
188 #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
189 #define MVNETA_GMAC_CTRL_0                       0x2c00
190 #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
191 #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
192 #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
193 #define MVNETA_GMAC_CTRL_2                       0x2c08
194 #define      MVNETA_GMAC2_INBAND_AN_ENABLE       BIT(0)
195 #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
196 #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
197 #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
198 #define MVNETA_GMAC_STATUS                       0x2c10
199 #define      MVNETA_GMAC_LINK_UP                 BIT(0)
200 #define      MVNETA_GMAC_SPEED_1000              BIT(1)
201 #define      MVNETA_GMAC_SPEED_100               BIT(2)
202 #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
203 #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
204 #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
205 #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
206 #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
207 #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
208 #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
209 #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
210 #define      MVNETA_GMAC_INBAND_AN_ENABLE        BIT(2)
211 #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
212 #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
213 #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
214 #define      MVNETA_GMAC_AN_FLOW_CTRL_EN         BIT(11)
215 #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
216 #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
217 #define MVNETA_MIB_COUNTERS_BASE                 0x3000
218 #define      MVNETA_MIB_LATE_COLLISION           0x7c
219 #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
220 #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
221 #define MVNETA_DA_FILT_UCAST_BASE                0x3600
222 #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
223 #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
224 #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
225 #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
226 #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
227 #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
228 #define      MVNETA_TXQ_DEC_SENT_MASK            0xff
229 #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
230 #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
231 #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
232 #define MVNETA_PORT_TX_RESET                     0x3cf0
233 #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
234 #define MVNETA_TX_MTU                            0x3e0c
235 #define MVNETA_TX_TOKEN_SIZE                     0x3e14
236 #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
237 #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
238 #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
239 
240 #define MVNETA_CAUSE_TXQ_SENT_DESC_ALL_MASK	 0xff
241 
242 /* Descriptor ring Macros */
243 #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
244 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
245 
246 /* Various constants */
247 
248 /* Coalescing */
249 #define MVNETA_TXDONE_COAL_PKTS		0	/* interrupt per packet */
250 #define MVNETA_RX_COAL_PKTS		32
251 #define MVNETA_RX_COAL_USEC		100
252 
253 /* The two bytes Marvell header. Either contains a special value used
254  * by Marvell switches when a specific hardware mode is enabled (not
255  * supported by this driver) or is filled automatically by zeroes on
256  * the RX side. Those two bytes being at the front of the Ethernet
257  * header, they allow to have the IP header aligned on a 4 bytes
258  * boundary automatically: the hardware skips those two bytes on its
259  * own.
260  */
261 #define MVNETA_MH_SIZE			2
262 
263 #define MVNETA_VLAN_TAG_LEN             4
264 
265 #define MVNETA_TX_CSUM_DEF_SIZE		1600
266 #define MVNETA_TX_CSUM_MAX_SIZE		9800
267 #define MVNETA_ACC_MODE_EXT1		1
268 #define MVNETA_ACC_MODE_EXT2		2
269 
270 #define MVNETA_MAX_DECODE_WIN		6
271 
272 /* Timeout constants */
273 #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
274 #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
275 #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
276 
277 #define MVNETA_TX_MTU_MAX		0x3ffff
278 
279 /* The RSS lookup table actually has 256 entries but we do not use
280  * them yet
281  */
282 #define MVNETA_RSS_LU_TABLE_SIZE	1
283 
284 /* TSO header size */
285 #define TSO_HEADER_SIZE 128
286 
287 /* Max number of Rx descriptors */
288 #define MVNETA_MAX_RXD 128
289 
290 /* Max number of Tx descriptors */
291 #define MVNETA_MAX_TXD 532
292 
293 /* Max number of allowed TCP segments for software TSO */
294 #define MVNETA_MAX_TSO_SEGS 100
295 
296 #define MVNETA_MAX_SKB_DESCS (MVNETA_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
297 
298 /* descriptor aligned size */
299 #define MVNETA_DESC_ALIGNED_SIZE	32
300 
301 /* Number of bytes to be taken into account by HW when putting incoming data
302  * to the buffers. It is needed in case NET_SKB_PAD exceeds maximum packet
303  * offset supported in MVNETA_RXQ_CONFIG_REG(q) registers.
304  */
305 #define MVNETA_RX_PKT_OFFSET_CORRECTION		64
306 
307 #define MVNETA_RX_PKT_SIZE(mtu) \
308 	ALIGN((mtu) + MVNETA_MH_SIZE + MVNETA_VLAN_TAG_LEN + \
309 	      ETH_HLEN + ETH_FCS_LEN,			     \
310 	      cache_line_size())
311 
312 #define IS_TSO_HEADER(txq, addr) \
313 	((addr >= txq->tso_hdrs_phys) && \
314 	 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
315 
316 #define MVNETA_RX_GET_BM_POOL_ID(rxd) \
317 	(((rxd)->status & MVNETA_RXD_BM_POOL_MASK) >> MVNETA_RXD_BM_POOL_SHIFT)
318 
319 struct mvneta_statistic {
320 	unsigned short offset;
321 	unsigned short type;
322 	const char name[ETH_GSTRING_LEN];
323 };
324 
325 #define T_REG_32	32
326 #define T_REG_64	64
327 
328 static const struct mvneta_statistic mvneta_statistics[] = {
329 	{ 0x3000, T_REG_64, "good_octets_received", },
330 	{ 0x3010, T_REG_32, "good_frames_received", },
331 	{ 0x3008, T_REG_32, "bad_octets_received", },
332 	{ 0x3014, T_REG_32, "bad_frames_received", },
333 	{ 0x3018, T_REG_32, "broadcast_frames_received", },
334 	{ 0x301c, T_REG_32, "multicast_frames_received", },
335 	{ 0x3050, T_REG_32, "unrec_mac_control_received", },
336 	{ 0x3058, T_REG_32, "good_fc_received", },
337 	{ 0x305c, T_REG_32, "bad_fc_received", },
338 	{ 0x3060, T_REG_32, "undersize_received", },
339 	{ 0x3064, T_REG_32, "fragments_received", },
340 	{ 0x3068, T_REG_32, "oversize_received", },
341 	{ 0x306c, T_REG_32, "jabber_received", },
342 	{ 0x3070, T_REG_32, "mac_receive_error", },
343 	{ 0x3074, T_REG_32, "bad_crc_event", },
344 	{ 0x3078, T_REG_32, "collision", },
345 	{ 0x307c, T_REG_32, "late_collision", },
346 	{ 0x2484, T_REG_32, "rx_discard", },
347 	{ 0x2488, T_REG_32, "rx_overrun", },
348 	{ 0x3020, T_REG_32, "frames_64_octets", },
349 	{ 0x3024, T_REG_32, "frames_65_to_127_octets", },
350 	{ 0x3028, T_REG_32, "frames_128_to_255_octets", },
351 	{ 0x302c, T_REG_32, "frames_256_to_511_octets", },
352 	{ 0x3030, T_REG_32, "frames_512_to_1023_octets", },
353 	{ 0x3034, T_REG_32, "frames_1024_to_max_octets", },
354 	{ 0x3038, T_REG_64, "good_octets_sent", },
355 	{ 0x3040, T_REG_32, "good_frames_sent", },
356 	{ 0x3044, T_REG_32, "excessive_collision", },
357 	{ 0x3048, T_REG_32, "multicast_frames_sent", },
358 	{ 0x304c, T_REG_32, "broadcast_frames_sent", },
359 	{ 0x3054, T_REG_32, "fc_sent", },
360 	{ 0x300c, T_REG_32, "internal_mac_transmit_err", },
361 };
362 
363 struct mvneta_pcpu_stats {
364 	struct	u64_stats_sync syncp;
365 	u64	rx_packets;
366 	u64	rx_bytes;
367 	u64	tx_packets;
368 	u64	tx_bytes;
369 };
370 
371 struct mvneta_pcpu_port {
372 	/* Pointer to the shared port */
373 	struct mvneta_port	*pp;
374 
375 	/* Pointer to the CPU-local NAPI struct */
376 	struct napi_struct	napi;
377 
378 	/* Cause of the previous interrupt */
379 	u32			cause_rx_tx;
380 };
381 
382 struct mvneta_port {
383 	u8 id;
384 	struct mvneta_pcpu_port __percpu	*ports;
385 	struct mvneta_pcpu_stats __percpu	*stats;
386 
387 	int pkt_size;
388 	unsigned int frag_size;
389 	void __iomem *base;
390 	struct mvneta_rx_queue *rxqs;
391 	struct mvneta_tx_queue *txqs;
392 	struct net_device *dev;
393 	struct hlist_node node_online;
394 	struct hlist_node node_dead;
395 	int rxq_def;
396 	/* Protect the access to the percpu interrupt registers,
397 	 * ensuring that the configuration remains coherent.
398 	 */
399 	spinlock_t lock;
400 	bool is_stopped;
401 
402 	u32 cause_rx_tx;
403 	struct napi_struct napi;
404 
405 	/* Core clock */
406 	struct clk *clk;
407 	/* AXI clock */
408 	struct clk *clk_bus;
409 	u8 mcast_count[256];
410 	u16 tx_ring_size;
411 	u16 rx_ring_size;
412 
413 	struct mii_bus *mii_bus;
414 	phy_interface_t phy_interface;
415 	struct device_node *phy_node;
416 	unsigned int link;
417 	unsigned int duplex;
418 	unsigned int speed;
419 	unsigned int tx_csum_limit;
420 	unsigned int use_inband_status:1;
421 
422 	struct mvneta_bm *bm_priv;
423 	struct mvneta_bm_pool *pool_long;
424 	struct mvneta_bm_pool *pool_short;
425 	int bm_win_id;
426 
427 	u64 ethtool_stats[ARRAY_SIZE(mvneta_statistics)];
428 
429 	u32 indir[MVNETA_RSS_LU_TABLE_SIZE];
430 
431 	/* Flags for special SoC configurations */
432 	bool neta_armada3700;
433 	u16 rx_offset_correction;
434 };
435 
436 /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
437  * layout of the transmit and reception DMA descriptors, and their
438  * layout is therefore defined by the hardware design
439  */
440 
441 #define MVNETA_TX_L3_OFF_SHIFT	0
442 #define MVNETA_TX_IP_HLEN_SHIFT	8
443 #define MVNETA_TX_L4_UDP	BIT(16)
444 #define MVNETA_TX_L3_IP6	BIT(17)
445 #define MVNETA_TXD_IP_CSUM	BIT(18)
446 #define MVNETA_TXD_Z_PAD	BIT(19)
447 #define MVNETA_TXD_L_DESC	BIT(20)
448 #define MVNETA_TXD_F_DESC	BIT(21)
449 #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
450 				 MVNETA_TXD_L_DESC | \
451 				 MVNETA_TXD_F_DESC)
452 #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
453 #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
454 
455 #define MVNETA_RXD_ERR_CRC		0x0
456 #define MVNETA_RXD_BM_POOL_SHIFT	13
457 #define MVNETA_RXD_BM_POOL_MASK		(BIT(13) | BIT(14))
458 #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
459 #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
460 #define MVNETA_RXD_ERR_LEN		BIT(18)
461 #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
462 #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
463 #define MVNETA_RXD_L3_IP4		BIT(25)
464 #define MVNETA_RXD_FIRST_LAST_DESC	(BIT(26) | BIT(27))
465 #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
466 
467 #if defined(__LITTLE_ENDIAN)
468 struct mvneta_tx_desc {
469 	u32  command;		/* Options used by HW for packet transmitting.*/
470 	u16  reserverd1;	/* csum_l4 (for future use)		*/
471 	u16  data_size;		/* Data size of transmitted packet in bytes */
472 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
473 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
474 	u32  reserved3[4];	/* Reserved - (for future use)		*/
475 };
476 
477 struct mvneta_rx_desc {
478 	u32  status;		/* Info about received packet		*/
479 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
480 	u16  data_size;		/* Size of received packet in bytes	*/
481 
482 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
483 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
484 
485 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
486 	u16  reserved3;		/* prefetch_cmd, for future use		*/
487 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
488 
489 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
490 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
491 };
492 #else
493 struct mvneta_tx_desc {
494 	u16  data_size;		/* Data size of transmitted packet in bytes */
495 	u16  reserverd1;	/* csum_l4 (for future use)		*/
496 	u32  command;		/* Options used by HW for packet transmitting.*/
497 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
498 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
499 	u32  reserved3[4];	/* Reserved - (for future use)		*/
500 };
501 
502 struct mvneta_rx_desc {
503 	u16  data_size;		/* Size of received packet in bytes	*/
504 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
505 	u32  status;		/* Info about received packet		*/
506 
507 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
508 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
509 
510 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
511 	u16  reserved3;		/* prefetch_cmd, for future use		*/
512 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
513 
514 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
515 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
516 };
517 #endif
518 
519 struct mvneta_tx_queue {
520 	/* Number of this TX queue, in the range 0-7 */
521 	u8 id;
522 
523 	/* Number of TX DMA descriptors in the descriptor ring */
524 	int size;
525 
526 	/* Number of currently used TX DMA descriptor in the
527 	 * descriptor ring
528 	 */
529 	int count;
530 	int pending;
531 	int tx_stop_threshold;
532 	int tx_wake_threshold;
533 
534 	/* Array of transmitted skb */
535 	struct sk_buff **tx_skb;
536 
537 	/* Index of last TX DMA descriptor that was inserted */
538 	int txq_put_index;
539 
540 	/* Index of the TX DMA descriptor to be cleaned up */
541 	int txq_get_index;
542 
543 	u32 done_pkts_coal;
544 
545 	/* Virtual address of the TX DMA descriptors array */
546 	struct mvneta_tx_desc *descs;
547 
548 	/* DMA address of the TX DMA descriptors array */
549 	dma_addr_t descs_phys;
550 
551 	/* Index of the last TX DMA descriptor */
552 	int last_desc;
553 
554 	/* Index of the next TX DMA descriptor to process */
555 	int next_desc_to_proc;
556 
557 	/* DMA buffers for TSO headers */
558 	char *tso_hdrs;
559 
560 	/* DMA address of TSO headers */
561 	dma_addr_t tso_hdrs_phys;
562 
563 	/* Affinity mask for CPUs*/
564 	cpumask_t affinity_mask;
565 };
566 
567 struct mvneta_rx_queue {
568 	/* rx queue number, in the range 0-7 */
569 	u8 id;
570 
571 	/* num of rx descriptors in the rx descriptor ring */
572 	int size;
573 
574 	/* counter of times when mvneta_refill() failed */
575 	int missed;
576 
577 	u32 pkts_coal;
578 	u32 time_coal;
579 
580 	/* Virtual address of the RX buffer */
581 	void  **buf_virt_addr;
582 
583 	/* Virtual address of the RX DMA descriptors array */
584 	struct mvneta_rx_desc *descs;
585 
586 	/* DMA address of the RX DMA descriptors array */
587 	dma_addr_t descs_phys;
588 
589 	/* Index of the last RX DMA descriptor */
590 	int last_desc;
591 
592 	/* Index of the next RX DMA descriptor to process */
593 	int next_desc_to_proc;
594 };
595 
596 static enum cpuhp_state online_hpstate;
597 /* The hardware supports eight (8) rx queues, but we are only allowing
598  * the first one to be used. Therefore, let's just allocate one queue.
599  */
600 static int rxq_number = 8;
601 static int txq_number = 8;
602 
603 static int rxq_def;
604 
605 static int rx_copybreak __read_mostly = 256;
606 
607 /* HW BM need that each port be identify by a unique ID */
608 static int global_port_id;
609 
610 #define MVNETA_DRIVER_NAME "mvneta"
611 #define MVNETA_DRIVER_VERSION "1.0"
612 
613 /* Utility/helper methods */
614 
615 /* Write helper method */
616 static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
617 {
618 	writel(data, pp->base + offset);
619 }
620 
621 /* Read helper method */
622 static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
623 {
624 	return readl(pp->base + offset);
625 }
626 
627 /* Increment txq get counter */
628 static void mvneta_txq_inc_get(struct mvneta_tx_queue *txq)
629 {
630 	txq->txq_get_index++;
631 	if (txq->txq_get_index == txq->size)
632 		txq->txq_get_index = 0;
633 }
634 
635 /* Increment txq put counter */
636 static void mvneta_txq_inc_put(struct mvneta_tx_queue *txq)
637 {
638 	txq->txq_put_index++;
639 	if (txq->txq_put_index == txq->size)
640 		txq->txq_put_index = 0;
641 }
642 
643 
644 /* Clear all MIB counters */
645 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
646 {
647 	int i;
648 	u32 dummy;
649 
650 	/* Perform dummy reads from MIB counters */
651 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
652 		dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
653 	dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
654 	dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
655 }
656 
657 /* Get System Network Statistics */
658 static void
659 mvneta_get_stats64(struct net_device *dev,
660 		   struct rtnl_link_stats64 *stats)
661 {
662 	struct mvneta_port *pp = netdev_priv(dev);
663 	unsigned int start;
664 	int cpu;
665 
666 	for_each_possible_cpu(cpu) {
667 		struct mvneta_pcpu_stats *cpu_stats;
668 		u64 rx_packets;
669 		u64 rx_bytes;
670 		u64 tx_packets;
671 		u64 tx_bytes;
672 
673 		cpu_stats = per_cpu_ptr(pp->stats, cpu);
674 		do {
675 			start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
676 			rx_packets = cpu_stats->rx_packets;
677 			rx_bytes   = cpu_stats->rx_bytes;
678 			tx_packets = cpu_stats->tx_packets;
679 			tx_bytes   = cpu_stats->tx_bytes;
680 		} while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
681 
682 		stats->rx_packets += rx_packets;
683 		stats->rx_bytes   += rx_bytes;
684 		stats->tx_packets += tx_packets;
685 		stats->tx_bytes   += tx_bytes;
686 	}
687 
688 	stats->rx_errors	= dev->stats.rx_errors;
689 	stats->rx_dropped	= dev->stats.rx_dropped;
690 
691 	stats->tx_dropped	= dev->stats.tx_dropped;
692 }
693 
694 /* Rx descriptors helper methods */
695 
696 /* Checks whether the RX descriptor having this status is both the first
697  * and the last descriptor for the RX packet. Each RX packet is currently
698  * received through a single RX descriptor, so not having each RX
699  * descriptor with its first and last bits set is an error
700  */
701 static int mvneta_rxq_desc_is_first_last(u32 status)
702 {
703 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
704 		MVNETA_RXD_FIRST_LAST_DESC;
705 }
706 
707 /* Add number of descriptors ready to receive new packets */
708 static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
709 					  struct mvneta_rx_queue *rxq,
710 					  int ndescs)
711 {
712 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
713 	 * be added at once
714 	 */
715 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
716 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
717 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
718 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
719 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
720 	}
721 
722 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
723 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
724 }
725 
726 /* Get number of RX descriptors occupied by received packets */
727 static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
728 					struct mvneta_rx_queue *rxq)
729 {
730 	u32 val;
731 
732 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
733 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
734 }
735 
736 /* Update num of rx desc called upon return from rx path or
737  * from mvneta_rxq_drop_pkts().
738  */
739 static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
740 				       struct mvneta_rx_queue *rxq,
741 				       int rx_done, int rx_filled)
742 {
743 	u32 val;
744 
745 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
746 		val = rx_done |
747 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
748 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
749 		return;
750 	}
751 
752 	/* Only 255 descriptors can be added at once */
753 	while ((rx_done > 0) || (rx_filled > 0)) {
754 		if (rx_done <= 0xff) {
755 			val = rx_done;
756 			rx_done = 0;
757 		} else {
758 			val = 0xff;
759 			rx_done -= 0xff;
760 		}
761 		if (rx_filled <= 0xff) {
762 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
763 			rx_filled = 0;
764 		} else {
765 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
766 			rx_filled -= 0xff;
767 		}
768 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
769 	}
770 }
771 
772 /* Get pointer to next RX descriptor to be processed by SW */
773 static struct mvneta_rx_desc *
774 mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
775 {
776 	int rx_desc = rxq->next_desc_to_proc;
777 
778 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
779 	prefetch(rxq->descs + rxq->next_desc_to_proc);
780 	return rxq->descs + rx_desc;
781 }
782 
783 /* Change maximum receive size of the port. */
784 static void mvneta_max_rx_size_set(struct mvneta_port *pp, int max_rx_size)
785 {
786 	u32 val;
787 
788 	val =  mvreg_read(pp, MVNETA_GMAC_CTRL_0);
789 	val &= ~MVNETA_GMAC_MAX_RX_SIZE_MASK;
790 	val |= ((max_rx_size - MVNETA_MH_SIZE) / 2) <<
791 		MVNETA_GMAC_MAX_RX_SIZE_SHIFT;
792 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
793 }
794 
795 
796 /* Set rx queue offset */
797 static void mvneta_rxq_offset_set(struct mvneta_port *pp,
798 				  struct mvneta_rx_queue *rxq,
799 				  int offset)
800 {
801 	u32 val;
802 
803 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
804 	val &= ~MVNETA_RXQ_PKT_OFFSET_ALL_MASK;
805 
806 	/* Offset is in */
807 	val |= MVNETA_RXQ_PKT_OFFSET_MASK(offset >> 3);
808 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
809 }
810 
811 
812 /* Tx descriptors helper methods */
813 
814 /* Update HW with number of TX descriptors to be sent */
815 static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
816 				     struct mvneta_tx_queue *txq,
817 				     int pend_desc)
818 {
819 	u32 val;
820 
821 	/* Only 255 descriptors can be added at once ; Assume caller
822 	 * process TX desriptors in quanta less than 256
823 	 */
824 	val = pend_desc + txq->pending;
825 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
826 	txq->pending = 0;
827 }
828 
829 /* Get pointer to next TX descriptor to be processed (send) by HW */
830 static struct mvneta_tx_desc *
831 mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
832 {
833 	int tx_desc = txq->next_desc_to_proc;
834 
835 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
836 	return txq->descs + tx_desc;
837 }
838 
839 /* Release the last allocated TX descriptor. Useful to handle DMA
840  * mapping failures in the TX path.
841  */
842 static void mvneta_txq_desc_put(struct mvneta_tx_queue *txq)
843 {
844 	if (txq->next_desc_to_proc == 0)
845 		txq->next_desc_to_proc = txq->last_desc - 1;
846 	else
847 		txq->next_desc_to_proc--;
848 }
849 
850 /* Set rxq buf size */
851 static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
852 				    struct mvneta_rx_queue *rxq,
853 				    int buf_size)
854 {
855 	u32 val;
856 
857 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
858 
859 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
860 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
861 
862 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
863 }
864 
865 /* Disable buffer management (BM) */
866 static void mvneta_rxq_bm_disable(struct mvneta_port *pp,
867 				  struct mvneta_rx_queue *rxq)
868 {
869 	u32 val;
870 
871 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
872 	val &= ~MVNETA_RXQ_HW_BUF_ALLOC;
873 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
874 }
875 
876 /* Enable buffer management (BM) */
877 static void mvneta_rxq_bm_enable(struct mvneta_port *pp,
878 				 struct mvneta_rx_queue *rxq)
879 {
880 	u32 val;
881 
882 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
883 	val |= MVNETA_RXQ_HW_BUF_ALLOC;
884 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
885 }
886 
887 /* Notify HW about port's assignment of pool for bigger packets */
888 static void mvneta_rxq_long_pool_set(struct mvneta_port *pp,
889 				     struct mvneta_rx_queue *rxq)
890 {
891 	u32 val;
892 
893 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
894 	val &= ~MVNETA_RXQ_LONG_POOL_ID_MASK;
895 	val |= (pp->pool_long->id << MVNETA_RXQ_LONG_POOL_ID_SHIFT);
896 
897 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
898 }
899 
900 /* Notify HW about port's assignment of pool for smaller packets */
901 static void mvneta_rxq_short_pool_set(struct mvneta_port *pp,
902 				      struct mvneta_rx_queue *rxq)
903 {
904 	u32 val;
905 
906 	val = mvreg_read(pp, MVNETA_RXQ_CONFIG_REG(rxq->id));
907 	val &= ~MVNETA_RXQ_SHORT_POOL_ID_MASK;
908 	val |= (pp->pool_short->id << MVNETA_RXQ_SHORT_POOL_ID_SHIFT);
909 
910 	mvreg_write(pp, MVNETA_RXQ_CONFIG_REG(rxq->id), val);
911 }
912 
913 /* Set port's receive buffer size for assigned BM pool */
914 static inline void mvneta_bm_pool_bufsize_set(struct mvneta_port *pp,
915 					      int buf_size,
916 					      u8 pool_id)
917 {
918 	u32 val;
919 
920 	if (!IS_ALIGNED(buf_size, 8)) {
921 		dev_warn(pp->dev->dev.parent,
922 			 "illegal buf_size value %d, round to %d\n",
923 			 buf_size, ALIGN(buf_size, 8));
924 		buf_size = ALIGN(buf_size, 8);
925 	}
926 
927 	val = mvreg_read(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id));
928 	val |= buf_size & MVNETA_PORT_POOL_BUFFER_SZ_MASK;
929 	mvreg_write(pp, MVNETA_PORT_POOL_BUFFER_SZ_REG(pool_id), val);
930 }
931 
932 /* Configure MBUS window in order to enable access BM internal SRAM */
933 static int mvneta_mbus_io_win_set(struct mvneta_port *pp, u32 base, u32 wsize,
934 				  u8 target, u8 attr)
935 {
936 	u32 win_enable, win_protect;
937 	int i;
938 
939 	win_enable = mvreg_read(pp, MVNETA_BASE_ADDR_ENABLE);
940 
941 	if (pp->bm_win_id < 0) {
942 		/* Find first not occupied window */
943 		for (i = 0; i < MVNETA_MAX_DECODE_WIN; i++) {
944 			if (win_enable & (1 << i)) {
945 				pp->bm_win_id = i;
946 				break;
947 			}
948 		}
949 		if (i == MVNETA_MAX_DECODE_WIN)
950 			return -ENOMEM;
951 	} else {
952 		i = pp->bm_win_id;
953 	}
954 
955 	mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
956 	mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
957 
958 	if (i < 4)
959 		mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
960 
961 	mvreg_write(pp, MVNETA_WIN_BASE(i), (base & 0xffff0000) |
962 		    (attr << 8) | target);
963 
964 	mvreg_write(pp, MVNETA_WIN_SIZE(i), (wsize - 1) & 0xffff0000);
965 
966 	win_protect = mvreg_read(pp, MVNETA_ACCESS_PROTECT_ENABLE);
967 	win_protect |= 3 << (2 * i);
968 	mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
969 
970 	win_enable &= ~(1 << i);
971 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
972 
973 	return 0;
974 }
975 
976 static  int mvneta_bm_port_mbus_init(struct mvneta_port *pp)
977 {
978 	u32 wsize;
979 	u8 target, attr;
980 	int err;
981 
982 	/* Get BM window information */
983 	err = mvebu_mbus_get_io_win_info(pp->bm_priv->bppi_phys_addr, &wsize,
984 					 &target, &attr);
985 	if (err < 0)
986 		return err;
987 
988 	pp->bm_win_id = -1;
989 
990 	/* Open NETA -> BM window */
991 	err = mvneta_mbus_io_win_set(pp, pp->bm_priv->bppi_phys_addr, wsize,
992 				     target, attr);
993 	if (err < 0) {
994 		netdev_info(pp->dev, "fail to configure mbus window to BM\n");
995 		return err;
996 	}
997 	return 0;
998 }
999 
1000 /* Assign and initialize pools for port. In case of fail
1001  * buffer manager will remain disabled for current port.
1002  */
1003 static int mvneta_bm_port_init(struct platform_device *pdev,
1004 			       struct mvneta_port *pp)
1005 {
1006 	struct device_node *dn = pdev->dev.of_node;
1007 	u32 long_pool_id, short_pool_id;
1008 
1009 	if (!pp->neta_armada3700) {
1010 		int ret;
1011 
1012 		ret = mvneta_bm_port_mbus_init(pp);
1013 		if (ret)
1014 			return ret;
1015 	}
1016 
1017 	if (of_property_read_u32(dn, "bm,pool-long", &long_pool_id)) {
1018 		netdev_info(pp->dev, "missing long pool id\n");
1019 		return -EINVAL;
1020 	}
1021 
1022 	/* Create port's long pool depending on mtu */
1023 	pp->pool_long = mvneta_bm_pool_use(pp->bm_priv, long_pool_id,
1024 					   MVNETA_BM_LONG, pp->id,
1025 					   MVNETA_RX_PKT_SIZE(pp->dev->mtu));
1026 	if (!pp->pool_long) {
1027 		netdev_info(pp->dev, "fail to obtain long pool for port\n");
1028 		return -ENOMEM;
1029 	}
1030 
1031 	pp->pool_long->port_map |= 1 << pp->id;
1032 
1033 	mvneta_bm_pool_bufsize_set(pp, pp->pool_long->buf_size,
1034 				   pp->pool_long->id);
1035 
1036 	/* If short pool id is not defined, assume using single pool */
1037 	if (of_property_read_u32(dn, "bm,pool-short", &short_pool_id))
1038 		short_pool_id = long_pool_id;
1039 
1040 	/* Create port's short pool */
1041 	pp->pool_short = mvneta_bm_pool_use(pp->bm_priv, short_pool_id,
1042 					    MVNETA_BM_SHORT, pp->id,
1043 					    MVNETA_BM_SHORT_PKT_SIZE);
1044 	if (!pp->pool_short) {
1045 		netdev_info(pp->dev, "fail to obtain short pool for port\n");
1046 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1047 		return -ENOMEM;
1048 	}
1049 
1050 	if (short_pool_id != long_pool_id) {
1051 		pp->pool_short->port_map |= 1 << pp->id;
1052 		mvneta_bm_pool_bufsize_set(pp, pp->pool_short->buf_size,
1053 					   pp->pool_short->id);
1054 	}
1055 
1056 	return 0;
1057 }
1058 
1059 /* Update settings of a pool for bigger packets */
1060 static void mvneta_bm_update_mtu(struct mvneta_port *pp, int mtu)
1061 {
1062 	struct mvneta_bm_pool *bm_pool = pp->pool_long;
1063 	struct hwbm_pool *hwbm_pool = &bm_pool->hwbm_pool;
1064 	int num;
1065 
1066 	/* Release all buffers from long pool */
1067 	mvneta_bm_bufs_free(pp->bm_priv, bm_pool, 1 << pp->id);
1068 	if (hwbm_pool->buf_num) {
1069 		WARN(1, "cannot free all buffers in pool %d\n",
1070 		     bm_pool->id);
1071 		goto bm_mtu_err;
1072 	}
1073 
1074 	bm_pool->pkt_size = MVNETA_RX_PKT_SIZE(mtu);
1075 	bm_pool->buf_size = MVNETA_RX_BUF_SIZE(bm_pool->pkt_size);
1076 	hwbm_pool->frag_size = SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) +
1077 			SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
1078 
1079 	/* Fill entire long pool */
1080 	num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1081 	if (num != hwbm_pool->size) {
1082 		WARN(1, "pool %d: %d of %d allocated\n",
1083 		     bm_pool->id, num, hwbm_pool->size);
1084 		goto bm_mtu_err;
1085 	}
1086 	mvneta_bm_pool_bufsize_set(pp, bm_pool->buf_size, bm_pool->id);
1087 
1088 	return;
1089 
1090 bm_mtu_err:
1091 	mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
1092 	mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
1093 
1094 	pp->bm_priv = NULL;
1095 	mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
1096 	netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
1097 }
1098 
1099 /* Start the Ethernet port RX and TX activity */
1100 static void mvneta_port_up(struct mvneta_port *pp)
1101 {
1102 	int queue;
1103 	u32 q_map;
1104 
1105 	/* Enable all initialized TXs. */
1106 	q_map = 0;
1107 	for (queue = 0; queue < txq_number; queue++) {
1108 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
1109 		if (txq->descs != NULL)
1110 			q_map |= (1 << queue);
1111 	}
1112 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
1113 
1114 	/* Enable all initialized RXQs. */
1115 	for (queue = 0; queue < rxq_number; queue++) {
1116 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1117 
1118 		if (rxq->descs != NULL)
1119 			q_map |= (1 << queue);
1120 	}
1121 	mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
1122 }
1123 
1124 /* Stop the Ethernet port activity */
1125 static void mvneta_port_down(struct mvneta_port *pp)
1126 {
1127 	u32 val;
1128 	int count;
1129 
1130 	/* Stop Rx port activity. Check port Rx activity. */
1131 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
1132 
1133 	/* Issue stop command for active channels only */
1134 	if (val != 0)
1135 		mvreg_write(pp, MVNETA_RXQ_CMD,
1136 			    val << MVNETA_RXQ_DISABLE_SHIFT);
1137 
1138 	/* Wait for all Rx activity to terminate. */
1139 	count = 0;
1140 	do {
1141 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
1142 			netdev_warn(pp->dev,
1143 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x%08x\n",
1144 				    val);
1145 			break;
1146 		}
1147 		mdelay(1);
1148 
1149 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
1150 	} while (val & MVNETA_RXQ_ENABLE_MASK);
1151 
1152 	/* Stop Tx port activity. Check port Tx activity. Issue stop
1153 	 * command for active channels only
1154 	 */
1155 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
1156 
1157 	if (val != 0)
1158 		mvreg_write(pp, MVNETA_TXQ_CMD,
1159 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
1160 
1161 	/* Wait for all Tx activity to terminate. */
1162 	count = 0;
1163 	do {
1164 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
1165 			netdev_warn(pp->dev,
1166 				    "TIMEOUT for TX stopped status=0x%08x\n",
1167 				    val);
1168 			break;
1169 		}
1170 		mdelay(1);
1171 
1172 		/* Check TX Command reg that all Txqs are stopped */
1173 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
1174 
1175 	} while (val & MVNETA_TXQ_ENABLE_MASK);
1176 
1177 	/* Double check to verify that TX FIFO is empty */
1178 	count = 0;
1179 	do {
1180 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
1181 			netdev_warn(pp->dev,
1182 				    "TX FIFO empty timeout status=0x%08x\n",
1183 				    val);
1184 			break;
1185 		}
1186 		mdelay(1);
1187 
1188 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
1189 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
1190 		 (val & MVNETA_TX_IN_PRGRS));
1191 
1192 	udelay(200);
1193 }
1194 
1195 /* Enable the port by setting the port enable bit of the MAC control register */
1196 static void mvneta_port_enable(struct mvneta_port *pp)
1197 {
1198 	u32 val;
1199 
1200 	/* Enable port */
1201 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1202 	val |= MVNETA_GMAC0_PORT_ENABLE;
1203 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1204 }
1205 
1206 /* Disable the port and wait for about 200 usec before retuning */
1207 static void mvneta_port_disable(struct mvneta_port *pp)
1208 {
1209 	u32 val;
1210 
1211 	/* Reset the Enable bit in the Serial Control Register */
1212 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
1213 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
1214 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
1215 
1216 	udelay(200);
1217 }
1218 
1219 /* Multicast tables methods */
1220 
1221 /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
1222 static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
1223 {
1224 	int offset;
1225 	u32 val;
1226 
1227 	if (queue == -1) {
1228 		val = 0;
1229 	} else {
1230 		val = 0x1 | (queue << 1);
1231 		val |= (val << 24) | (val << 16) | (val << 8);
1232 	}
1233 
1234 	for (offset = 0; offset <= 0xc; offset += 4)
1235 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
1236 }
1237 
1238 /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
1239 static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
1240 {
1241 	int offset;
1242 	u32 val;
1243 
1244 	if (queue == -1) {
1245 		val = 0;
1246 	} else {
1247 		val = 0x1 | (queue << 1);
1248 		val |= (val << 24) | (val << 16) | (val << 8);
1249 	}
1250 
1251 	for (offset = 0; offset <= 0xfc; offset += 4)
1252 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
1253 
1254 }
1255 
1256 /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
1257 static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
1258 {
1259 	int offset;
1260 	u32 val;
1261 
1262 	if (queue == -1) {
1263 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
1264 		val = 0;
1265 	} else {
1266 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
1267 		val = 0x1 | (queue << 1);
1268 		val |= (val << 24) | (val << 16) | (val << 8);
1269 	}
1270 
1271 	for (offset = 0; offset <= 0xfc; offset += 4)
1272 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
1273 }
1274 
1275 static void mvneta_set_autoneg(struct mvneta_port *pp, int enable)
1276 {
1277 	u32 val;
1278 
1279 	if (enable) {
1280 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1281 		val &= ~(MVNETA_GMAC_FORCE_LINK_PASS |
1282 			 MVNETA_GMAC_FORCE_LINK_DOWN |
1283 			 MVNETA_GMAC_AN_FLOW_CTRL_EN);
1284 		val |= MVNETA_GMAC_INBAND_AN_ENABLE |
1285 		       MVNETA_GMAC_AN_SPEED_EN |
1286 		       MVNETA_GMAC_AN_DUPLEX_EN;
1287 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1288 
1289 		val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1290 		val |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
1291 		mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1292 
1293 		val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1294 		val |= MVNETA_GMAC2_INBAND_AN_ENABLE;
1295 		mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1296 	} else {
1297 		val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1298 		val &= ~(MVNETA_GMAC_INBAND_AN_ENABLE |
1299 		       MVNETA_GMAC_AN_SPEED_EN |
1300 		       MVNETA_GMAC_AN_DUPLEX_EN);
1301 		mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1302 
1303 		val = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
1304 		val &= ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
1305 		mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, val);
1306 
1307 		val = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1308 		val &= ~MVNETA_GMAC2_INBAND_AN_ENABLE;
1309 		mvreg_write(pp, MVNETA_GMAC_CTRL_2, val);
1310 	}
1311 }
1312 
1313 static void mvneta_percpu_unmask_interrupt(void *arg)
1314 {
1315 	struct mvneta_port *pp = arg;
1316 
1317 	/* All the queue are unmasked, but actually only the ones
1318 	 * mapped to this CPU will be unmasked
1319 	 */
1320 	mvreg_write(pp, MVNETA_INTR_NEW_MASK,
1321 		    MVNETA_RX_INTR_MASK_ALL |
1322 		    MVNETA_TX_INTR_MASK_ALL |
1323 		    MVNETA_MISCINTR_INTR_MASK);
1324 }
1325 
1326 static void mvneta_percpu_mask_interrupt(void *arg)
1327 {
1328 	struct mvneta_port *pp = arg;
1329 
1330 	/* All the queue are masked, but actually only the ones
1331 	 * mapped to this CPU will be masked
1332 	 */
1333 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
1334 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
1335 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
1336 }
1337 
1338 static void mvneta_percpu_clear_intr_cause(void *arg)
1339 {
1340 	struct mvneta_port *pp = arg;
1341 
1342 	/* All the queue are cleared, but actually only the ones
1343 	 * mapped to this CPU will be cleared
1344 	 */
1345 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
1346 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
1347 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
1348 }
1349 
1350 /* This method sets defaults to the NETA port:
1351  *	Clears interrupt Cause and Mask registers.
1352  *	Clears all MAC tables.
1353  *	Sets defaults to all registers.
1354  *	Resets RX and TX descriptor rings.
1355  *	Resets PHY.
1356  * This method can be called after mvneta_port_down() to return the port
1357  *	settings to defaults.
1358  */
1359 static void mvneta_defaults_set(struct mvneta_port *pp)
1360 {
1361 	int cpu;
1362 	int queue;
1363 	u32 val;
1364 	int max_cpu = num_present_cpus();
1365 
1366 	/* Clear all Cause registers */
1367 	on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
1368 
1369 	/* Mask all interrupts */
1370 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
1371 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
1372 
1373 	/* Enable MBUS Retry bit16 */
1374 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
1375 
1376 	/* Set CPU queue access map. CPUs are assigned to the RX and
1377 	 * TX queues modulo their number. If there is only one TX
1378 	 * queue then it is assigned to the CPU associated to the
1379 	 * default RX queue.
1380 	 */
1381 	for_each_present_cpu(cpu) {
1382 		int rxq_map = 0, txq_map = 0;
1383 		int rxq, txq;
1384 		if (!pp->neta_armada3700) {
1385 			for (rxq = 0; rxq < rxq_number; rxq++)
1386 				if ((rxq % max_cpu) == cpu)
1387 					rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
1388 
1389 			for (txq = 0; txq < txq_number; txq++)
1390 				if ((txq % max_cpu) == cpu)
1391 					txq_map |= MVNETA_CPU_TXQ_ACCESS(txq);
1392 
1393 			/* With only one TX queue we configure a special case
1394 			 * which will allow to get all the irq on a single
1395 			 * CPU
1396 			 */
1397 			if (txq_number == 1)
1398 				txq_map = (cpu == pp->rxq_def) ?
1399 					MVNETA_CPU_TXQ_ACCESS(1) : 0;
1400 
1401 		} else {
1402 			txq_map = MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
1403 			rxq_map = MVNETA_CPU_RXQ_ACCESS_ALL_MASK;
1404 		}
1405 
1406 		mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
1407 	}
1408 
1409 	/* Reset RX and TX DMAs */
1410 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
1411 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
1412 
1413 	/* Disable Legacy WRR, Disable EJP, Release from reset */
1414 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
1415 	for (queue = 0; queue < txq_number; queue++) {
1416 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
1417 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
1418 	}
1419 
1420 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
1421 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
1422 
1423 	/* Set Port Acceleration Mode */
1424 	if (pp->bm_priv)
1425 		/* HW buffer management + legacy parser */
1426 		val = MVNETA_ACC_MODE_EXT2;
1427 	else
1428 		/* SW buffer management + legacy parser */
1429 		val = MVNETA_ACC_MODE_EXT1;
1430 	mvreg_write(pp, MVNETA_ACC_MODE, val);
1431 
1432 	if (pp->bm_priv)
1433 		mvreg_write(pp, MVNETA_BM_ADDRESS, pp->bm_priv->bppi_phys_addr);
1434 
1435 	/* Update val of portCfg register accordingly with all RxQueue types */
1436 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
1437 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
1438 
1439 	val = 0;
1440 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
1441 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
1442 
1443 	/* Build PORT_SDMA_CONFIG_REG */
1444 	val = 0;
1445 
1446 	/* Default burst size */
1447 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1448 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
1449 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
1450 
1451 #if defined(__BIG_ENDIAN)
1452 	val |= MVNETA_DESC_SWAP;
1453 #endif
1454 
1455 	/* Assign port SDMA configuration */
1456 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
1457 
1458 	/* Disable PHY polling in hardware, since we're using the
1459 	 * kernel phylib to do this.
1460 	 */
1461 	val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
1462 	val &= ~MVNETA_PHY_POLLING_ENABLE;
1463 	mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
1464 
1465 	mvneta_set_autoneg(pp, pp->use_inband_status);
1466 	mvneta_set_ucast_table(pp, -1);
1467 	mvneta_set_special_mcast_table(pp, -1);
1468 	mvneta_set_other_mcast_table(pp, -1);
1469 
1470 	/* Set port interrupt enable register - default enable all */
1471 	mvreg_write(pp, MVNETA_INTR_ENABLE,
1472 		    (MVNETA_RXQ_INTR_ENABLE_ALL_MASK
1473 		     | MVNETA_TXQ_INTR_ENABLE_ALL_MASK));
1474 
1475 	mvneta_mib_counters_clear(pp);
1476 }
1477 
1478 /* Set max sizes for tx queues */
1479 static void mvneta_txq_max_tx_size_set(struct mvneta_port *pp, int max_tx_size)
1480 
1481 {
1482 	u32 val, size, mtu;
1483 	int queue;
1484 
1485 	mtu = max_tx_size * 8;
1486 	if (mtu > MVNETA_TX_MTU_MAX)
1487 		mtu = MVNETA_TX_MTU_MAX;
1488 
1489 	/* Set MTU */
1490 	val = mvreg_read(pp, MVNETA_TX_MTU);
1491 	val &= ~MVNETA_TX_MTU_MAX;
1492 	val |= mtu;
1493 	mvreg_write(pp, MVNETA_TX_MTU, val);
1494 
1495 	/* TX token size and all TXQs token size must be larger that MTU */
1496 	val = mvreg_read(pp, MVNETA_TX_TOKEN_SIZE);
1497 
1498 	size = val & MVNETA_TX_TOKEN_SIZE_MAX;
1499 	if (size < mtu) {
1500 		size = mtu;
1501 		val &= ~MVNETA_TX_TOKEN_SIZE_MAX;
1502 		val |= size;
1503 		mvreg_write(pp, MVNETA_TX_TOKEN_SIZE, val);
1504 	}
1505 	for (queue = 0; queue < txq_number; queue++) {
1506 		val = mvreg_read(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue));
1507 
1508 		size = val & MVNETA_TXQ_TOKEN_SIZE_MAX;
1509 		if (size < mtu) {
1510 			size = mtu;
1511 			val &= ~MVNETA_TXQ_TOKEN_SIZE_MAX;
1512 			val |= size;
1513 			mvreg_write(pp, MVNETA_TXQ_TOKEN_SIZE_REG(queue), val);
1514 		}
1515 	}
1516 }
1517 
1518 /* Set unicast address */
1519 static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
1520 				  int queue)
1521 {
1522 	unsigned int unicast_reg;
1523 	unsigned int tbl_offset;
1524 	unsigned int reg_offset;
1525 
1526 	/* Locate the Unicast table entry */
1527 	last_nibble = (0xf & last_nibble);
1528 
1529 	/* offset from unicast tbl base */
1530 	tbl_offset = (last_nibble / 4) * 4;
1531 
1532 	/* offset within the above reg  */
1533 	reg_offset = last_nibble % 4;
1534 
1535 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
1536 
1537 	if (queue == -1) {
1538 		/* Clear accepts frame bit at specified unicast DA tbl entry */
1539 		unicast_reg &= ~(0xff << (8 * reg_offset));
1540 	} else {
1541 		unicast_reg &= ~(0xff << (8 * reg_offset));
1542 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
1543 	}
1544 
1545 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
1546 }
1547 
1548 /* Set mac address */
1549 static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
1550 				int queue)
1551 {
1552 	unsigned int mac_h;
1553 	unsigned int mac_l;
1554 
1555 	if (queue != -1) {
1556 		mac_l = (addr[4] << 8) | (addr[5]);
1557 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
1558 			(addr[2] << 8) | (addr[3] << 0);
1559 
1560 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
1561 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
1562 	}
1563 
1564 	/* Accept frames of this address */
1565 	mvneta_set_ucast_addr(pp, addr[5], queue);
1566 }
1567 
1568 /* Set the number of packets that will be received before RX interrupt
1569  * will be generated by HW.
1570  */
1571 static void mvneta_rx_pkts_coal_set(struct mvneta_port *pp,
1572 				    struct mvneta_rx_queue *rxq, u32 value)
1573 {
1574 	mvreg_write(pp, MVNETA_RXQ_THRESHOLD_REG(rxq->id),
1575 		    value | MVNETA_RXQ_NON_OCCUPIED(0));
1576 	rxq->pkts_coal = value;
1577 }
1578 
1579 /* Set the time delay in usec before RX interrupt will be generated by
1580  * HW.
1581  */
1582 static void mvneta_rx_time_coal_set(struct mvneta_port *pp,
1583 				    struct mvneta_rx_queue *rxq, u32 value)
1584 {
1585 	u32 val;
1586 	unsigned long clk_rate;
1587 
1588 	clk_rate = clk_get_rate(pp->clk);
1589 	val = (clk_rate / 1000000) * value;
1590 
1591 	mvreg_write(pp, MVNETA_RXQ_TIME_COAL_REG(rxq->id), val);
1592 	rxq->time_coal = value;
1593 }
1594 
1595 /* Set threshold for TX_DONE pkts coalescing */
1596 static void mvneta_tx_done_pkts_coal_set(struct mvneta_port *pp,
1597 					 struct mvneta_tx_queue *txq, u32 value)
1598 {
1599 	u32 val;
1600 
1601 	val = mvreg_read(pp, MVNETA_TXQ_SIZE_REG(txq->id));
1602 
1603 	val &= ~MVNETA_TXQ_SENT_THRESH_ALL_MASK;
1604 	val |= MVNETA_TXQ_SENT_THRESH_MASK(value);
1605 
1606 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), val);
1607 
1608 	txq->done_pkts_coal = value;
1609 }
1610 
1611 /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
1612 static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
1613 				u32 phys_addr, void *virt_addr,
1614 				struct mvneta_rx_queue *rxq)
1615 {
1616 	int i;
1617 
1618 	rx_desc->buf_phys_addr = phys_addr;
1619 	i = rx_desc - rxq->descs;
1620 	rxq->buf_virt_addr[i] = virt_addr;
1621 }
1622 
1623 /* Decrement sent descriptors counter */
1624 static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
1625 				     struct mvneta_tx_queue *txq,
1626 				     int sent_desc)
1627 {
1628 	u32 val;
1629 
1630 	/* Only 255 TX descriptors can be updated at once */
1631 	while (sent_desc > 0xff) {
1632 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
1633 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1634 		sent_desc = sent_desc - 0xff;
1635 	}
1636 
1637 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
1638 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
1639 }
1640 
1641 /* Get number of TX descriptors already sent by HW */
1642 static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
1643 					struct mvneta_tx_queue *txq)
1644 {
1645 	u32 val;
1646 	int sent_desc;
1647 
1648 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
1649 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
1650 		MVNETA_TXQ_SENT_DESC_SHIFT;
1651 
1652 	return sent_desc;
1653 }
1654 
1655 /* Get number of sent descriptors and decrement counter.
1656  *  The number of sent descriptors is returned.
1657  */
1658 static int mvneta_txq_sent_desc_proc(struct mvneta_port *pp,
1659 				     struct mvneta_tx_queue *txq)
1660 {
1661 	int sent_desc;
1662 
1663 	/* Get number of sent descriptors */
1664 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1665 
1666 	/* Decrement sent descriptors counter */
1667 	if (sent_desc)
1668 		mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1669 
1670 	return sent_desc;
1671 }
1672 
1673 /* Set TXQ descriptors fields relevant for CSUM calculation */
1674 static u32 mvneta_txq_desc_csum(int l3_offs, int l3_proto,
1675 				int ip_hdr_len, int l4_proto)
1676 {
1677 	u32 command;
1678 
1679 	/* Fields: L3_offset, IP_hdrlen, L3_type, G_IPv4_chk,
1680 	 * G_L4_chk, L4_type; required only for checksum
1681 	 * calculation
1682 	 */
1683 	command =  l3_offs    << MVNETA_TX_L3_OFF_SHIFT;
1684 	command |= ip_hdr_len << MVNETA_TX_IP_HLEN_SHIFT;
1685 
1686 	if (l3_proto == htons(ETH_P_IP))
1687 		command |= MVNETA_TXD_IP_CSUM;
1688 	else
1689 		command |= MVNETA_TX_L3_IP6;
1690 
1691 	if (l4_proto == IPPROTO_TCP)
1692 		command |=  MVNETA_TX_L4_CSUM_FULL;
1693 	else if (l4_proto == IPPROTO_UDP)
1694 		command |= MVNETA_TX_L4_UDP | MVNETA_TX_L4_CSUM_FULL;
1695 	else
1696 		command |= MVNETA_TX_L4_CSUM_NOT;
1697 
1698 	return command;
1699 }
1700 
1701 
1702 /* Display more error info */
1703 static void mvneta_rx_error(struct mvneta_port *pp,
1704 			    struct mvneta_rx_desc *rx_desc)
1705 {
1706 	u32 status = rx_desc->status;
1707 
1708 	if (!mvneta_rxq_desc_is_first_last(status)) {
1709 		netdev_err(pp->dev,
1710 			   "bad rx status %08x (buffer oversize), size=%d\n",
1711 			   status, rx_desc->data_size);
1712 		return;
1713 	}
1714 
1715 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
1716 	case MVNETA_RXD_ERR_CRC:
1717 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
1718 			   status, rx_desc->data_size);
1719 		break;
1720 	case MVNETA_RXD_ERR_OVERRUN:
1721 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
1722 			   status, rx_desc->data_size);
1723 		break;
1724 	case MVNETA_RXD_ERR_LEN:
1725 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
1726 			   status, rx_desc->data_size);
1727 		break;
1728 	case MVNETA_RXD_ERR_RESOURCE:
1729 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
1730 			   status, rx_desc->data_size);
1731 		break;
1732 	}
1733 }
1734 
1735 /* Handle RX checksum offload based on the descriptor's status */
1736 static void mvneta_rx_csum(struct mvneta_port *pp, u32 status,
1737 			   struct sk_buff *skb)
1738 {
1739 	if ((status & MVNETA_RXD_L3_IP4) &&
1740 	    (status & MVNETA_RXD_L4_CSUM_OK)) {
1741 		skb->csum = 0;
1742 		skb->ip_summed = CHECKSUM_UNNECESSARY;
1743 		return;
1744 	}
1745 
1746 	skb->ip_summed = CHECKSUM_NONE;
1747 }
1748 
1749 /* Return tx queue pointer (find last set bit) according to <cause> returned
1750  * form tx_done reg. <cause> must not be null. The return value is always a
1751  * valid queue for matching the first one found in <cause>.
1752  */
1753 static struct mvneta_tx_queue *mvneta_tx_done_policy(struct mvneta_port *pp,
1754 						     u32 cause)
1755 {
1756 	int queue = fls(cause) - 1;
1757 
1758 	return &pp->txqs[queue];
1759 }
1760 
1761 /* Free tx queue skbuffs */
1762 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
1763 				 struct mvneta_tx_queue *txq, int num,
1764 				 struct netdev_queue *nq)
1765 {
1766 	unsigned int bytes_compl = 0, pkts_compl = 0;
1767 	int i;
1768 
1769 	for (i = 0; i < num; i++) {
1770 		struct mvneta_tx_desc *tx_desc = txq->descs +
1771 			txq->txq_get_index;
1772 		struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1773 
1774 		if (skb) {
1775 			bytes_compl += skb->len;
1776 			pkts_compl++;
1777 		}
1778 
1779 		mvneta_txq_inc_get(txq);
1780 
1781 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1782 			dma_unmap_single(pp->dev->dev.parent,
1783 					 tx_desc->buf_phys_addr,
1784 					 tx_desc->data_size, DMA_TO_DEVICE);
1785 		if (!skb)
1786 			continue;
1787 		dev_kfree_skb_any(skb);
1788 	}
1789 
1790 	netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
1791 }
1792 
1793 /* Handle end of transmission */
1794 static void mvneta_txq_done(struct mvneta_port *pp,
1795 			   struct mvneta_tx_queue *txq)
1796 {
1797 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
1798 	int tx_done;
1799 
1800 	tx_done = mvneta_txq_sent_desc_proc(pp, txq);
1801 	if (!tx_done)
1802 		return;
1803 
1804 	mvneta_txq_bufs_free(pp, txq, tx_done, nq);
1805 
1806 	txq->count -= tx_done;
1807 
1808 	if (netif_tx_queue_stopped(nq)) {
1809 		if (txq->count <= txq->tx_wake_threshold)
1810 			netif_tx_wake_queue(nq);
1811 	}
1812 }
1813 
1814 void *mvneta_frag_alloc(unsigned int frag_size)
1815 {
1816 	if (likely(frag_size <= PAGE_SIZE))
1817 		return netdev_alloc_frag(frag_size);
1818 	else
1819 		return kmalloc(frag_size, GFP_ATOMIC);
1820 }
1821 EXPORT_SYMBOL_GPL(mvneta_frag_alloc);
1822 
1823 void mvneta_frag_free(unsigned int frag_size, void *data)
1824 {
1825 	if (likely(frag_size <= PAGE_SIZE))
1826 		skb_free_frag(data);
1827 	else
1828 		kfree(data);
1829 }
1830 EXPORT_SYMBOL_GPL(mvneta_frag_free);
1831 
1832 /* Refill processing for SW buffer management */
1833 static int mvneta_rx_refill(struct mvneta_port *pp,
1834 			    struct mvneta_rx_desc *rx_desc,
1835 			    struct mvneta_rx_queue *rxq)
1836 
1837 {
1838 	dma_addr_t phys_addr;
1839 	void *data;
1840 
1841 	data = mvneta_frag_alloc(pp->frag_size);
1842 	if (!data)
1843 		return -ENOMEM;
1844 
1845 	phys_addr = dma_map_single(pp->dev->dev.parent, data,
1846 				   MVNETA_RX_BUF_SIZE(pp->pkt_size),
1847 				   DMA_FROM_DEVICE);
1848 	if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1849 		mvneta_frag_free(pp->frag_size, data);
1850 		return -ENOMEM;
1851 	}
1852 
1853 	phys_addr += pp->rx_offset_correction;
1854 	mvneta_rx_desc_fill(rx_desc, phys_addr, data, rxq);
1855 	return 0;
1856 }
1857 
1858 /* Handle tx checksum */
1859 static u32 mvneta_skb_tx_csum(struct mvneta_port *pp, struct sk_buff *skb)
1860 {
1861 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1862 		int ip_hdr_len = 0;
1863 		__be16 l3_proto = vlan_get_protocol(skb);
1864 		u8 l4_proto;
1865 
1866 		if (l3_proto == htons(ETH_P_IP)) {
1867 			struct iphdr *ip4h = ip_hdr(skb);
1868 
1869 			/* Calculate IPv4 checksum and L4 checksum */
1870 			ip_hdr_len = ip4h->ihl;
1871 			l4_proto = ip4h->protocol;
1872 		} else if (l3_proto == htons(ETH_P_IPV6)) {
1873 			struct ipv6hdr *ip6h = ipv6_hdr(skb);
1874 
1875 			/* Read l4_protocol from one of IPv6 extra headers */
1876 			if (skb_network_header_len(skb) > 0)
1877 				ip_hdr_len = (skb_network_header_len(skb) >> 2);
1878 			l4_proto = ip6h->nexthdr;
1879 		} else
1880 			return MVNETA_TX_L4_CSUM_NOT;
1881 
1882 		return mvneta_txq_desc_csum(skb_network_offset(skb),
1883 					    l3_proto, ip_hdr_len, l4_proto);
1884 	}
1885 
1886 	return MVNETA_TX_L4_CSUM_NOT;
1887 }
1888 
1889 /* Drop packets received by the RXQ and free buffers */
1890 static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
1891 				 struct mvneta_rx_queue *rxq)
1892 {
1893 	int rx_done, i;
1894 
1895 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1896 	if (rx_done)
1897 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1898 
1899 	if (pp->bm_priv) {
1900 		for (i = 0; i < rx_done; i++) {
1901 			struct mvneta_rx_desc *rx_desc =
1902 						  mvneta_rxq_next_desc_get(rxq);
1903 			u8 pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
1904 			struct mvneta_bm_pool *bm_pool;
1905 
1906 			bm_pool = &pp->bm_priv->bm_pools[pool_id];
1907 			/* Return dropped buffer to the pool */
1908 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
1909 					      rx_desc->buf_phys_addr);
1910 		}
1911 		return;
1912 	}
1913 
1914 	for (i = 0; i < rxq->size; i++) {
1915 		struct mvneta_rx_desc *rx_desc = rxq->descs + i;
1916 		void *data = rxq->buf_virt_addr[i];
1917 
1918 		dma_unmap_single(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1919 				 MVNETA_RX_BUF_SIZE(pp->pkt_size), DMA_FROM_DEVICE);
1920 		mvneta_frag_free(pp->frag_size, data);
1921 	}
1922 }
1923 
1924 /* Main rx processing when using software buffer management */
1925 static int mvneta_rx_swbm(struct mvneta_port *pp, int rx_todo,
1926 			  struct mvneta_rx_queue *rxq)
1927 {
1928 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
1929 	struct net_device *dev = pp->dev;
1930 	int rx_done;
1931 	u32 rcvd_pkts = 0;
1932 	u32 rcvd_bytes = 0;
1933 
1934 	/* Get number of received packets */
1935 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1936 
1937 	if (rx_todo > rx_done)
1938 		rx_todo = rx_done;
1939 
1940 	rx_done = 0;
1941 
1942 	/* Fairness NAPI loop */
1943 	while (rx_done < rx_todo) {
1944 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1945 		struct sk_buff *skb;
1946 		unsigned char *data;
1947 		dma_addr_t phys_addr;
1948 		u32 rx_status, frag_size;
1949 		int rx_bytes, err, index;
1950 
1951 		rx_done++;
1952 		rx_status = rx_desc->status;
1953 		rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
1954 		index = rx_desc - rxq->descs;
1955 		data = rxq->buf_virt_addr[index];
1956 		phys_addr = rx_desc->buf_phys_addr;
1957 
1958 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1959 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1960 err_drop_frame:
1961 			dev->stats.rx_errors++;
1962 			mvneta_rx_error(pp, rx_desc);
1963 			/* leave the descriptor untouched */
1964 			continue;
1965 		}
1966 
1967 		if (rx_bytes <= rx_copybreak) {
1968 		/* better copy a small frame and not unmap the DMA region */
1969 			skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
1970 			if (unlikely(!skb))
1971 				goto err_drop_frame;
1972 
1973 			dma_sync_single_range_for_cpu(dev->dev.parent,
1974 						      phys_addr,
1975 						      MVNETA_MH_SIZE + NET_SKB_PAD,
1976 						      rx_bytes,
1977 						      DMA_FROM_DEVICE);
1978 			memcpy(skb_put(skb, rx_bytes),
1979 			       data + MVNETA_MH_SIZE + NET_SKB_PAD,
1980 			       rx_bytes);
1981 
1982 			skb->protocol = eth_type_trans(skb, dev);
1983 			mvneta_rx_csum(pp, rx_status, skb);
1984 			napi_gro_receive(&port->napi, skb);
1985 
1986 			rcvd_pkts++;
1987 			rcvd_bytes += rx_bytes;
1988 
1989 			/* leave the descriptor and buffer untouched */
1990 			continue;
1991 		}
1992 
1993 		/* Refill processing */
1994 		err = mvneta_rx_refill(pp, rx_desc, rxq);
1995 		if (err) {
1996 			netdev_err(dev, "Linux processing - Can't refill\n");
1997 			rxq->missed++;
1998 			goto err_drop_frame;
1999 		}
2000 
2001 		frag_size = pp->frag_size;
2002 
2003 		skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2004 
2005 		/* After refill old buffer has to be unmapped regardless
2006 		 * the skb is successfully built or not.
2007 		 */
2008 		dma_unmap_single(dev->dev.parent, phys_addr,
2009 				 MVNETA_RX_BUF_SIZE(pp->pkt_size),
2010 				 DMA_FROM_DEVICE);
2011 
2012 		if (!skb)
2013 			goto err_drop_frame;
2014 
2015 		rcvd_pkts++;
2016 		rcvd_bytes += rx_bytes;
2017 
2018 		/* Linux processing */
2019 		skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2020 		skb_put(skb, rx_bytes);
2021 
2022 		skb->protocol = eth_type_trans(skb, dev);
2023 
2024 		mvneta_rx_csum(pp, rx_status, skb);
2025 
2026 		napi_gro_receive(&port->napi, skb);
2027 	}
2028 
2029 	if (rcvd_pkts) {
2030 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2031 
2032 		u64_stats_update_begin(&stats->syncp);
2033 		stats->rx_packets += rcvd_pkts;
2034 		stats->rx_bytes   += rcvd_bytes;
2035 		u64_stats_update_end(&stats->syncp);
2036 	}
2037 
2038 	/* Update rxq management counters */
2039 	mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2040 
2041 	return rx_done;
2042 }
2043 
2044 /* Main rx processing when using hardware buffer management */
2045 static int mvneta_rx_hwbm(struct mvneta_port *pp, int rx_todo,
2046 			  struct mvneta_rx_queue *rxq)
2047 {
2048 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2049 	struct net_device *dev = pp->dev;
2050 	int rx_done;
2051 	u32 rcvd_pkts = 0;
2052 	u32 rcvd_bytes = 0;
2053 
2054 	/* Get number of received packets */
2055 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
2056 
2057 	if (rx_todo > rx_done)
2058 		rx_todo = rx_done;
2059 
2060 	rx_done = 0;
2061 
2062 	/* Fairness NAPI loop */
2063 	while (rx_done < rx_todo) {
2064 		struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
2065 		struct mvneta_bm_pool *bm_pool = NULL;
2066 		struct sk_buff *skb;
2067 		unsigned char *data;
2068 		dma_addr_t phys_addr;
2069 		u32 rx_status, frag_size;
2070 		int rx_bytes, err;
2071 		u8 pool_id;
2072 
2073 		rx_done++;
2074 		rx_status = rx_desc->status;
2075 		rx_bytes = rx_desc->data_size - (ETH_FCS_LEN + MVNETA_MH_SIZE);
2076 		data = (u8 *)(uintptr_t)rx_desc->buf_cookie;
2077 		phys_addr = rx_desc->buf_phys_addr;
2078 		pool_id = MVNETA_RX_GET_BM_POOL_ID(rx_desc);
2079 		bm_pool = &pp->bm_priv->bm_pools[pool_id];
2080 
2081 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
2082 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
2083 err_drop_frame_ret_pool:
2084 			/* Return the buffer to the pool */
2085 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2086 					      rx_desc->buf_phys_addr);
2087 err_drop_frame:
2088 			dev->stats.rx_errors++;
2089 			mvneta_rx_error(pp, rx_desc);
2090 			/* leave the descriptor untouched */
2091 			continue;
2092 		}
2093 
2094 		if (rx_bytes <= rx_copybreak) {
2095 			/* better copy a small frame and not unmap the DMA region */
2096 			skb = netdev_alloc_skb_ip_align(dev, rx_bytes);
2097 			if (unlikely(!skb))
2098 				goto err_drop_frame_ret_pool;
2099 
2100 			dma_sync_single_range_for_cpu(dev->dev.parent,
2101 			                              rx_desc->buf_phys_addr,
2102 			                              MVNETA_MH_SIZE + NET_SKB_PAD,
2103 			                              rx_bytes,
2104 			                              DMA_FROM_DEVICE);
2105 			memcpy(skb_put(skb, rx_bytes),
2106 			       data + MVNETA_MH_SIZE + NET_SKB_PAD,
2107 			       rx_bytes);
2108 
2109 			skb->protocol = eth_type_trans(skb, dev);
2110 			mvneta_rx_csum(pp, rx_status, skb);
2111 			napi_gro_receive(&port->napi, skb);
2112 
2113 			rcvd_pkts++;
2114 			rcvd_bytes += rx_bytes;
2115 
2116 			/* Return the buffer to the pool */
2117 			mvneta_bm_pool_put_bp(pp->bm_priv, bm_pool,
2118 					      rx_desc->buf_phys_addr);
2119 
2120 			/* leave the descriptor and buffer untouched */
2121 			continue;
2122 		}
2123 
2124 		/* Refill processing */
2125 		err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
2126 		if (err) {
2127 			netdev_err(dev, "Linux processing - Can't refill\n");
2128 			rxq->missed++;
2129 			goto err_drop_frame_ret_pool;
2130 		}
2131 
2132 		frag_size = bm_pool->hwbm_pool.frag_size;
2133 
2134 		skb = build_skb(data, frag_size > PAGE_SIZE ? 0 : frag_size);
2135 
2136 		/* After refill old buffer has to be unmapped regardless
2137 		 * the skb is successfully built or not.
2138 		 */
2139 		dma_unmap_single(&pp->bm_priv->pdev->dev, phys_addr,
2140 				 bm_pool->buf_size, DMA_FROM_DEVICE);
2141 		if (!skb)
2142 			goto err_drop_frame;
2143 
2144 		rcvd_pkts++;
2145 		rcvd_bytes += rx_bytes;
2146 
2147 		/* Linux processing */
2148 		skb_reserve(skb, MVNETA_MH_SIZE + NET_SKB_PAD);
2149 		skb_put(skb, rx_bytes);
2150 
2151 		skb->protocol = eth_type_trans(skb, dev);
2152 
2153 		mvneta_rx_csum(pp, rx_status, skb);
2154 
2155 		napi_gro_receive(&port->napi, skb);
2156 	}
2157 
2158 	if (rcvd_pkts) {
2159 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2160 
2161 		u64_stats_update_begin(&stats->syncp);
2162 		stats->rx_packets += rcvd_pkts;
2163 		stats->rx_bytes   += rcvd_bytes;
2164 		u64_stats_update_end(&stats->syncp);
2165 	}
2166 
2167 	/* Update rxq management counters */
2168 	mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
2169 
2170 	return rx_done;
2171 }
2172 
2173 static inline void
2174 mvneta_tso_put_hdr(struct sk_buff *skb,
2175 		   struct mvneta_port *pp, struct mvneta_tx_queue *txq)
2176 {
2177 	struct mvneta_tx_desc *tx_desc;
2178 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2179 
2180 	txq->tx_skb[txq->txq_put_index] = NULL;
2181 	tx_desc = mvneta_txq_next_desc_get(txq);
2182 	tx_desc->data_size = hdr_len;
2183 	tx_desc->command = mvneta_skb_tx_csum(pp, skb);
2184 	tx_desc->command |= MVNETA_TXD_F_DESC;
2185 	tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
2186 				 txq->txq_put_index * TSO_HEADER_SIZE;
2187 	mvneta_txq_inc_put(txq);
2188 }
2189 
2190 static inline int
2191 mvneta_tso_put_data(struct net_device *dev, struct mvneta_tx_queue *txq,
2192 		    struct sk_buff *skb, char *data, int size,
2193 		    bool last_tcp, bool is_last)
2194 {
2195 	struct mvneta_tx_desc *tx_desc;
2196 
2197 	tx_desc = mvneta_txq_next_desc_get(txq);
2198 	tx_desc->data_size = size;
2199 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, data,
2200 						size, DMA_TO_DEVICE);
2201 	if (unlikely(dma_mapping_error(dev->dev.parent,
2202 		     tx_desc->buf_phys_addr))) {
2203 		mvneta_txq_desc_put(txq);
2204 		return -ENOMEM;
2205 	}
2206 
2207 	tx_desc->command = 0;
2208 	txq->tx_skb[txq->txq_put_index] = NULL;
2209 
2210 	if (last_tcp) {
2211 		/* last descriptor in the TCP packet */
2212 		tx_desc->command = MVNETA_TXD_L_DESC;
2213 
2214 		/* last descriptor in SKB */
2215 		if (is_last)
2216 			txq->tx_skb[txq->txq_put_index] = skb;
2217 	}
2218 	mvneta_txq_inc_put(txq);
2219 	return 0;
2220 }
2221 
2222 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
2223 			 struct mvneta_tx_queue *txq)
2224 {
2225 	int total_len, data_left;
2226 	int desc_count = 0;
2227 	struct mvneta_port *pp = netdev_priv(dev);
2228 	struct tso_t tso;
2229 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2230 	int i;
2231 
2232 	/* Count needed descriptors */
2233 	if ((txq->count + tso_count_descs(skb)) >= txq->size)
2234 		return 0;
2235 
2236 	if (skb_headlen(skb) < (skb_transport_offset(skb) + tcp_hdrlen(skb))) {
2237 		pr_info("*** Is this even  possible???!?!?\n");
2238 		return 0;
2239 	}
2240 
2241 	/* Initialize the TSO handler, and prepare the first payload */
2242 	tso_start(skb, &tso);
2243 
2244 	total_len = skb->len - hdr_len;
2245 	while (total_len > 0) {
2246 		char *hdr;
2247 
2248 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
2249 		total_len -= data_left;
2250 		desc_count++;
2251 
2252 		/* prepare packet headers: MAC + IP + TCP */
2253 		hdr = txq->tso_hdrs + txq->txq_put_index * TSO_HEADER_SIZE;
2254 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
2255 
2256 		mvneta_tso_put_hdr(skb, pp, txq);
2257 
2258 		while (data_left > 0) {
2259 			int size;
2260 			desc_count++;
2261 
2262 			size = min_t(int, tso.size, data_left);
2263 
2264 			if (mvneta_tso_put_data(dev, txq, skb,
2265 						 tso.data, size,
2266 						 size == data_left,
2267 						 total_len == 0))
2268 				goto err_release;
2269 			data_left -= size;
2270 
2271 			tso_build_data(skb, &tso, size);
2272 		}
2273 	}
2274 
2275 	return desc_count;
2276 
2277 err_release:
2278 	/* Release all used data descriptors; header descriptors must not
2279 	 * be DMA-unmapped.
2280 	 */
2281 	for (i = desc_count - 1; i >= 0; i--) {
2282 		struct mvneta_tx_desc *tx_desc = txq->descs + i;
2283 		if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
2284 			dma_unmap_single(pp->dev->dev.parent,
2285 					 tx_desc->buf_phys_addr,
2286 					 tx_desc->data_size,
2287 					 DMA_TO_DEVICE);
2288 		mvneta_txq_desc_put(txq);
2289 	}
2290 	return 0;
2291 }
2292 
2293 /* Handle tx fragmentation processing */
2294 static int mvneta_tx_frag_process(struct mvneta_port *pp, struct sk_buff *skb,
2295 				  struct mvneta_tx_queue *txq)
2296 {
2297 	struct mvneta_tx_desc *tx_desc;
2298 	int i, nr_frags = skb_shinfo(skb)->nr_frags;
2299 
2300 	for (i = 0; i < nr_frags; i++) {
2301 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2302 		void *addr = page_address(frag->page.p) + frag->page_offset;
2303 
2304 		tx_desc = mvneta_txq_next_desc_get(txq);
2305 		tx_desc->data_size = frag->size;
2306 
2307 		tx_desc->buf_phys_addr =
2308 			dma_map_single(pp->dev->dev.parent, addr,
2309 				       tx_desc->data_size, DMA_TO_DEVICE);
2310 
2311 		if (dma_mapping_error(pp->dev->dev.parent,
2312 				      tx_desc->buf_phys_addr)) {
2313 			mvneta_txq_desc_put(txq);
2314 			goto error;
2315 		}
2316 
2317 		if (i == nr_frags - 1) {
2318 			/* Last descriptor */
2319 			tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2320 			txq->tx_skb[txq->txq_put_index] = skb;
2321 		} else {
2322 			/* Descriptor in the middle: Not First, Not Last */
2323 			tx_desc->command = 0;
2324 			txq->tx_skb[txq->txq_put_index] = NULL;
2325 		}
2326 		mvneta_txq_inc_put(txq);
2327 	}
2328 
2329 	return 0;
2330 
2331 error:
2332 	/* Release all descriptors that were used to map fragments of
2333 	 * this packet, as well as the corresponding DMA mappings
2334 	 */
2335 	for (i = i - 1; i >= 0; i--) {
2336 		tx_desc = txq->descs + i;
2337 		dma_unmap_single(pp->dev->dev.parent,
2338 				 tx_desc->buf_phys_addr,
2339 				 tx_desc->data_size,
2340 				 DMA_TO_DEVICE);
2341 		mvneta_txq_desc_put(txq);
2342 	}
2343 
2344 	return -ENOMEM;
2345 }
2346 
2347 /* Main tx processing */
2348 static int mvneta_tx(struct sk_buff *skb, struct net_device *dev)
2349 {
2350 	struct mvneta_port *pp = netdev_priv(dev);
2351 	u16 txq_id = skb_get_queue_mapping(skb);
2352 	struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2353 	struct mvneta_tx_desc *tx_desc;
2354 	int len = skb->len;
2355 	int frags = 0;
2356 	u32 tx_cmd;
2357 
2358 	if (!netif_running(dev))
2359 		goto out;
2360 
2361 	if (skb_is_gso(skb)) {
2362 		frags = mvneta_tx_tso(skb, dev, txq);
2363 		goto out;
2364 	}
2365 
2366 	frags = skb_shinfo(skb)->nr_frags + 1;
2367 
2368 	/* Get a descriptor for the first part of the packet */
2369 	tx_desc = mvneta_txq_next_desc_get(txq);
2370 
2371 	tx_cmd = mvneta_skb_tx_csum(pp, skb);
2372 
2373 	tx_desc->data_size = skb_headlen(skb);
2374 
2375 	tx_desc->buf_phys_addr = dma_map_single(dev->dev.parent, skb->data,
2376 						tx_desc->data_size,
2377 						DMA_TO_DEVICE);
2378 	if (unlikely(dma_mapping_error(dev->dev.parent,
2379 				       tx_desc->buf_phys_addr))) {
2380 		mvneta_txq_desc_put(txq);
2381 		frags = 0;
2382 		goto out;
2383 	}
2384 
2385 	if (frags == 1) {
2386 		/* First and Last descriptor */
2387 		tx_cmd |= MVNETA_TXD_FLZ_DESC;
2388 		tx_desc->command = tx_cmd;
2389 		txq->tx_skb[txq->txq_put_index] = skb;
2390 		mvneta_txq_inc_put(txq);
2391 	} else {
2392 		/* First but not Last */
2393 		tx_cmd |= MVNETA_TXD_F_DESC;
2394 		txq->tx_skb[txq->txq_put_index] = NULL;
2395 		mvneta_txq_inc_put(txq);
2396 		tx_desc->command = tx_cmd;
2397 		/* Continue with other skb fragments */
2398 		if (mvneta_tx_frag_process(pp, skb, txq)) {
2399 			dma_unmap_single(dev->dev.parent,
2400 					 tx_desc->buf_phys_addr,
2401 					 tx_desc->data_size,
2402 					 DMA_TO_DEVICE);
2403 			mvneta_txq_desc_put(txq);
2404 			frags = 0;
2405 			goto out;
2406 		}
2407 	}
2408 
2409 out:
2410 	if (frags > 0) {
2411 		struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2412 		struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2413 
2414 		netdev_tx_sent_queue(nq, len);
2415 
2416 		txq->count += frags;
2417 		if (txq->count >= txq->tx_stop_threshold)
2418 			netif_tx_stop_queue(nq);
2419 
2420 		if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2421 		    txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
2422 			mvneta_txq_pend_desc_add(pp, txq, frags);
2423 		else
2424 			txq->pending += frags;
2425 
2426 		u64_stats_update_begin(&stats->syncp);
2427 		stats->tx_packets++;
2428 		stats->tx_bytes  += len;
2429 		u64_stats_update_end(&stats->syncp);
2430 	} else {
2431 		dev->stats.tx_dropped++;
2432 		dev_kfree_skb_any(skb);
2433 	}
2434 
2435 	return NETDEV_TX_OK;
2436 }
2437 
2438 
2439 /* Free tx resources, when resetting a port */
2440 static void mvneta_txq_done_force(struct mvneta_port *pp,
2441 				  struct mvneta_tx_queue *txq)
2442 
2443 {
2444 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2445 	int tx_done = txq->count;
2446 
2447 	mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2448 
2449 	/* reset txq */
2450 	txq->count = 0;
2451 	txq->txq_put_index = 0;
2452 	txq->txq_get_index = 0;
2453 }
2454 
2455 /* Handle tx done - called in softirq context. The <cause_tx_done> argument
2456  * must be a valid cause according to MVNETA_TXQ_INTR_MASK_ALL.
2457  */
2458 static void mvneta_tx_done_gbe(struct mvneta_port *pp, u32 cause_tx_done)
2459 {
2460 	struct mvneta_tx_queue *txq;
2461 	struct netdev_queue *nq;
2462 
2463 	while (cause_tx_done) {
2464 		txq = mvneta_tx_done_policy(pp, cause_tx_done);
2465 
2466 		nq = netdev_get_tx_queue(pp->dev, txq->id);
2467 		__netif_tx_lock(nq, smp_processor_id());
2468 
2469 		if (txq->count)
2470 			mvneta_txq_done(pp, txq);
2471 
2472 		__netif_tx_unlock(nq);
2473 		cause_tx_done &= ~((1 << txq->id));
2474 	}
2475 }
2476 
2477 /* Compute crc8 of the specified address, using a unique algorithm ,
2478  * according to hw spec, different than generic crc8 algorithm
2479  */
2480 static int mvneta_addr_crc(unsigned char *addr)
2481 {
2482 	int crc = 0;
2483 	int i;
2484 
2485 	for (i = 0; i < ETH_ALEN; i++) {
2486 		int j;
2487 
2488 		crc = (crc ^ addr[i]) << 8;
2489 		for (j = 7; j >= 0; j--) {
2490 			if (crc & (0x100 << j))
2491 				crc ^= 0x107 << j;
2492 		}
2493 	}
2494 
2495 	return crc;
2496 }
2497 
2498 /* This method controls the net device special MAC multicast support.
2499  * The Special Multicast Table for MAC addresses supports MAC of the form
2500  * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2501  * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2502  * Table entries in the DA-Filter table. This method set the Special
2503  * Multicast Table appropriate entry.
2504  */
2505 static void mvneta_set_special_mcast_addr(struct mvneta_port *pp,
2506 					  unsigned char last_byte,
2507 					  int queue)
2508 {
2509 	unsigned int smc_table_reg;
2510 	unsigned int tbl_offset;
2511 	unsigned int reg_offset;
2512 
2513 	/* Register offset from SMC table base    */
2514 	tbl_offset = (last_byte / 4);
2515 	/* Entry offset within the above reg */
2516 	reg_offset = last_byte % 4;
2517 
2518 	smc_table_reg = mvreg_read(pp, (MVNETA_DA_FILT_SPEC_MCAST
2519 					+ tbl_offset * 4));
2520 
2521 	if (queue == -1)
2522 		smc_table_reg &= ~(0xff << (8 * reg_offset));
2523 	else {
2524 		smc_table_reg &= ~(0xff << (8 * reg_offset));
2525 		smc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2526 	}
2527 
2528 	mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + tbl_offset * 4,
2529 		    smc_table_reg);
2530 }
2531 
2532 /* This method controls the network device Other MAC multicast support.
2533  * The Other Multicast Table is used for multicast of another type.
2534  * A CRC-8 is used as an index to the Other Multicast Table entries
2535  * in the DA-Filter table.
2536  * The method gets the CRC-8 value from the calling routine and
2537  * sets the Other Multicast Table appropriate entry according to the
2538  * specified CRC-8 .
2539  */
2540 static void mvneta_set_other_mcast_addr(struct mvneta_port *pp,
2541 					unsigned char crc8,
2542 					int queue)
2543 {
2544 	unsigned int omc_table_reg;
2545 	unsigned int tbl_offset;
2546 	unsigned int reg_offset;
2547 
2548 	tbl_offset = (crc8 / 4) * 4; /* Register offset from OMC table base */
2549 	reg_offset = crc8 % 4;	     /* Entry offset within the above reg   */
2550 
2551 	omc_table_reg = mvreg_read(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset);
2552 
2553 	if (queue == -1) {
2554 		/* Clear accepts frame bit at specified Other DA table entry */
2555 		omc_table_reg &= ~(0xff << (8 * reg_offset));
2556 	} else {
2557 		omc_table_reg &= ~(0xff << (8 * reg_offset));
2558 		omc_table_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
2559 	}
2560 
2561 	mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + tbl_offset, omc_table_reg);
2562 }
2563 
2564 /* The network device supports multicast using two tables:
2565  *    1) Special Multicast Table for MAC addresses of the form
2566  *       0x01-00-5E-00-00-XX (where XX is between 0x00 and 0xFF).
2567  *       The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2568  *       Table entries in the DA-Filter table.
2569  *    2) Other Multicast Table for multicast of another type. A CRC-8 value
2570  *       is used as an index to the Other Multicast Table entries in the
2571  *       DA-Filter table.
2572  */
2573 static int mvneta_mcast_addr_set(struct mvneta_port *pp, unsigned char *p_addr,
2574 				 int queue)
2575 {
2576 	unsigned char crc_result = 0;
2577 
2578 	if (memcmp(p_addr, "\x01\x00\x5e\x00\x00", 5) == 0) {
2579 		mvneta_set_special_mcast_addr(pp, p_addr[5], queue);
2580 		return 0;
2581 	}
2582 
2583 	crc_result = mvneta_addr_crc(p_addr);
2584 	if (queue == -1) {
2585 		if (pp->mcast_count[crc_result] == 0) {
2586 			netdev_info(pp->dev, "No valid Mcast for crc8=0x%02x\n",
2587 				    crc_result);
2588 			return -EINVAL;
2589 		}
2590 
2591 		pp->mcast_count[crc_result]--;
2592 		if (pp->mcast_count[crc_result] != 0) {
2593 			netdev_info(pp->dev,
2594 				    "After delete there are %d valid Mcast for crc8=0x%02x\n",
2595 				    pp->mcast_count[crc_result], crc_result);
2596 			return -EINVAL;
2597 		}
2598 	} else
2599 		pp->mcast_count[crc_result]++;
2600 
2601 	mvneta_set_other_mcast_addr(pp, crc_result, queue);
2602 
2603 	return 0;
2604 }
2605 
2606 /* Configure Fitering mode of Ethernet port */
2607 static void mvneta_rx_unicast_promisc_set(struct mvneta_port *pp,
2608 					  int is_promisc)
2609 {
2610 	u32 port_cfg_reg, val;
2611 
2612 	port_cfg_reg = mvreg_read(pp, MVNETA_PORT_CONFIG);
2613 
2614 	val = mvreg_read(pp, MVNETA_TYPE_PRIO);
2615 
2616 	/* Set / Clear UPM bit in port configuration register */
2617 	if (is_promisc) {
2618 		/* Accept all Unicast addresses */
2619 		port_cfg_reg |= MVNETA_UNI_PROMISC_MODE;
2620 		val |= MVNETA_FORCE_UNI;
2621 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, 0xffff);
2622 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, 0xffffffff);
2623 	} else {
2624 		/* Reject all Unicast addresses */
2625 		port_cfg_reg &= ~MVNETA_UNI_PROMISC_MODE;
2626 		val &= ~MVNETA_FORCE_UNI;
2627 	}
2628 
2629 	mvreg_write(pp, MVNETA_PORT_CONFIG, port_cfg_reg);
2630 	mvreg_write(pp, MVNETA_TYPE_PRIO, val);
2631 }
2632 
2633 /* register unicast and multicast addresses */
2634 static void mvneta_set_rx_mode(struct net_device *dev)
2635 {
2636 	struct mvneta_port *pp = netdev_priv(dev);
2637 	struct netdev_hw_addr *ha;
2638 
2639 	if (dev->flags & IFF_PROMISC) {
2640 		/* Accept all: Multicast + Unicast */
2641 		mvneta_rx_unicast_promisc_set(pp, 1);
2642 		mvneta_set_ucast_table(pp, pp->rxq_def);
2643 		mvneta_set_special_mcast_table(pp, pp->rxq_def);
2644 		mvneta_set_other_mcast_table(pp, pp->rxq_def);
2645 	} else {
2646 		/* Accept single Unicast */
2647 		mvneta_rx_unicast_promisc_set(pp, 0);
2648 		mvneta_set_ucast_table(pp, -1);
2649 		mvneta_mac_addr_set(pp, dev->dev_addr, pp->rxq_def);
2650 
2651 		if (dev->flags & IFF_ALLMULTI) {
2652 			/* Accept all multicast */
2653 			mvneta_set_special_mcast_table(pp, pp->rxq_def);
2654 			mvneta_set_other_mcast_table(pp, pp->rxq_def);
2655 		} else {
2656 			/* Accept only initialized multicast */
2657 			mvneta_set_special_mcast_table(pp, -1);
2658 			mvneta_set_other_mcast_table(pp, -1);
2659 
2660 			if (!netdev_mc_empty(dev)) {
2661 				netdev_for_each_mc_addr(ha, dev) {
2662 					mvneta_mcast_addr_set(pp, ha->addr,
2663 							      pp->rxq_def);
2664 				}
2665 			}
2666 		}
2667 	}
2668 }
2669 
2670 /* Interrupt handling - the callback for request_irq() */
2671 static irqreturn_t mvneta_isr(int irq, void *dev_id)
2672 {
2673 	struct mvneta_port *pp = (struct mvneta_port *)dev_id;
2674 
2675 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
2676 	napi_schedule(&pp->napi);
2677 
2678 	return IRQ_HANDLED;
2679 }
2680 
2681 /* Interrupt handling - the callback for request_percpu_irq() */
2682 static irqreturn_t mvneta_percpu_isr(int irq, void *dev_id)
2683 {
2684 	struct mvneta_pcpu_port *port = (struct mvneta_pcpu_port *)dev_id;
2685 
2686 	disable_percpu_irq(port->pp->dev->irq);
2687 	napi_schedule(&port->napi);
2688 
2689 	return IRQ_HANDLED;
2690 }
2691 
2692 static int mvneta_fixed_link_update(struct mvneta_port *pp,
2693 				    struct phy_device *phy)
2694 {
2695 	struct fixed_phy_status status;
2696 	struct fixed_phy_status changed = {};
2697 	u32 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
2698 
2699 	status.link = !!(gmac_stat & MVNETA_GMAC_LINK_UP);
2700 	if (gmac_stat & MVNETA_GMAC_SPEED_1000)
2701 		status.speed = SPEED_1000;
2702 	else if (gmac_stat & MVNETA_GMAC_SPEED_100)
2703 		status.speed = SPEED_100;
2704 	else
2705 		status.speed = SPEED_10;
2706 	status.duplex = !!(gmac_stat & MVNETA_GMAC_FULL_DUPLEX);
2707 	changed.link = 1;
2708 	changed.speed = 1;
2709 	changed.duplex = 1;
2710 	fixed_phy_update_state(phy, &status, &changed);
2711 	return 0;
2712 }
2713 
2714 /* NAPI handler
2715  * Bits 0 - 7 of the causeRxTx register indicate that are transmitted
2716  * packets on the corresponding TXQ (Bit 0 is for TX queue 1).
2717  * Bits 8 -15 of the cause Rx Tx register indicate that are received
2718  * packets on the corresponding RXQ (Bit 8 is for RX queue 0).
2719  * Each CPU has its own causeRxTx register
2720  */
2721 static int mvneta_poll(struct napi_struct *napi, int budget)
2722 {
2723 	int rx_done = 0;
2724 	u32 cause_rx_tx;
2725 	int rx_queue;
2726 	struct mvneta_port *pp = netdev_priv(napi->dev);
2727 	struct net_device *ndev = pp->dev;
2728 	struct mvneta_pcpu_port *port = this_cpu_ptr(pp->ports);
2729 
2730 	if (!netif_running(pp->dev)) {
2731 		napi_complete(napi);
2732 		return rx_done;
2733 	}
2734 
2735 	/* Read cause register */
2736 	cause_rx_tx = mvreg_read(pp, MVNETA_INTR_NEW_CAUSE);
2737 	if (cause_rx_tx & MVNETA_MISCINTR_INTR_MASK) {
2738 		u32 cause_misc = mvreg_read(pp, MVNETA_INTR_MISC_CAUSE);
2739 
2740 		mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
2741 		if (pp->use_inband_status && (cause_misc &
2742 				(MVNETA_CAUSE_PHY_STATUS_CHANGE |
2743 				 MVNETA_CAUSE_LINK_CHANGE |
2744 				 MVNETA_CAUSE_PSC_SYNC_CHANGE))) {
2745 			mvneta_fixed_link_update(pp, ndev->phydev);
2746 		}
2747 	}
2748 
2749 	/* Release Tx descriptors */
2750 	if (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL) {
2751 		mvneta_tx_done_gbe(pp, (cause_rx_tx & MVNETA_TX_INTR_MASK_ALL));
2752 		cause_rx_tx &= ~MVNETA_TX_INTR_MASK_ALL;
2753 	}
2754 
2755 	/* For the case where the last mvneta_poll did not process all
2756 	 * RX packets
2757 	 */
2758 	rx_queue = fls(((cause_rx_tx >> 8) & 0xff));
2759 
2760 	cause_rx_tx |= pp->neta_armada3700 ? pp->cause_rx_tx :
2761 		port->cause_rx_tx;
2762 
2763 	if (rx_queue) {
2764 		rx_queue = rx_queue - 1;
2765 		if (pp->bm_priv)
2766 			rx_done = mvneta_rx_hwbm(pp, budget, &pp->rxqs[rx_queue]);
2767 		else
2768 			rx_done = mvneta_rx_swbm(pp, budget, &pp->rxqs[rx_queue]);
2769 	}
2770 
2771 	if (rx_done < budget) {
2772 		cause_rx_tx = 0;
2773 		napi_complete_done(napi, rx_done);
2774 
2775 		if (pp->neta_armada3700) {
2776 			unsigned long flags;
2777 
2778 			local_irq_save(flags);
2779 			mvreg_write(pp, MVNETA_INTR_NEW_MASK,
2780 				    MVNETA_RX_INTR_MASK(rxq_number) |
2781 				    MVNETA_TX_INTR_MASK(txq_number) |
2782 				    MVNETA_MISCINTR_INTR_MASK);
2783 			local_irq_restore(flags);
2784 		} else {
2785 			enable_percpu_irq(pp->dev->irq, 0);
2786 		}
2787 	}
2788 
2789 	if (pp->neta_armada3700)
2790 		pp->cause_rx_tx = cause_rx_tx;
2791 	else
2792 		port->cause_rx_tx = cause_rx_tx;
2793 
2794 	return rx_done;
2795 }
2796 
2797 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
2798 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2799 			   int num)
2800 {
2801 	int i;
2802 
2803 	for (i = 0; i < num; i++) {
2804 		memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
2805 		if (mvneta_rx_refill(pp, rxq->descs + i, rxq) != 0) {
2806 			netdev_err(pp->dev, "%s:rxq %d, %d of %d buffs  filled\n",
2807 				__func__, rxq->id, i, num);
2808 			break;
2809 		}
2810 	}
2811 
2812 	/* Add this number of RX descriptors as non occupied (ready to
2813 	 * get packets)
2814 	 */
2815 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
2816 
2817 	return i;
2818 }
2819 
2820 /* Free all packets pending transmit from all TXQs and reset TX port */
2821 static void mvneta_tx_reset(struct mvneta_port *pp)
2822 {
2823 	int queue;
2824 
2825 	/* free the skb's in the tx ring */
2826 	for (queue = 0; queue < txq_number; queue++)
2827 		mvneta_txq_done_force(pp, &pp->txqs[queue]);
2828 
2829 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
2830 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
2831 }
2832 
2833 static void mvneta_rx_reset(struct mvneta_port *pp)
2834 {
2835 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
2836 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
2837 }
2838 
2839 /* Rx/Tx queue initialization/cleanup methods */
2840 
2841 /* Create a specified RX queue */
2842 static int mvneta_rxq_init(struct mvneta_port *pp,
2843 			   struct mvneta_rx_queue *rxq)
2844 
2845 {
2846 	rxq->size = pp->rx_ring_size;
2847 
2848 	/* Allocate memory for RX descriptors */
2849 	rxq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2850 					rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2851 					&rxq->descs_phys, GFP_KERNEL);
2852 	if (rxq->descs == NULL)
2853 		return -ENOMEM;
2854 
2855 	rxq->last_desc = rxq->size - 1;
2856 
2857 	/* Set Rx descriptors queue starting address */
2858 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
2859 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
2860 
2861 	/* Set Offset */
2862 	mvneta_rxq_offset_set(pp, rxq, NET_SKB_PAD - pp->rx_offset_correction);
2863 
2864 	/* Set coalescing pkts and time */
2865 	mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
2866 	mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
2867 
2868 	if (!pp->bm_priv) {
2869 		/* Fill RXQ with buffers from RX pool */
2870 		mvneta_rxq_buf_size_set(pp, rxq,
2871 					MVNETA_RX_BUF_SIZE(pp->pkt_size));
2872 		mvneta_rxq_bm_disable(pp, rxq);
2873 		mvneta_rxq_fill(pp, rxq, rxq->size);
2874 	} else {
2875 		mvneta_rxq_bm_enable(pp, rxq);
2876 		mvneta_rxq_long_pool_set(pp, rxq);
2877 		mvneta_rxq_short_pool_set(pp, rxq);
2878 		mvneta_rxq_non_occup_desc_add(pp, rxq, rxq->size);
2879 	}
2880 
2881 	return 0;
2882 }
2883 
2884 /* Cleanup Rx queue */
2885 static void mvneta_rxq_deinit(struct mvneta_port *pp,
2886 			      struct mvneta_rx_queue *rxq)
2887 {
2888 	mvneta_rxq_drop_pkts(pp, rxq);
2889 
2890 	if (rxq->descs)
2891 		dma_free_coherent(pp->dev->dev.parent,
2892 				  rxq->size * MVNETA_DESC_ALIGNED_SIZE,
2893 				  rxq->descs,
2894 				  rxq->descs_phys);
2895 
2896 	rxq->descs             = NULL;
2897 	rxq->last_desc         = 0;
2898 	rxq->next_desc_to_proc = 0;
2899 	rxq->descs_phys        = 0;
2900 }
2901 
2902 /* Create and initialize a tx queue */
2903 static int mvneta_txq_init(struct mvneta_port *pp,
2904 			   struct mvneta_tx_queue *txq)
2905 {
2906 	int cpu;
2907 
2908 	txq->size = pp->tx_ring_size;
2909 
2910 	/* A queue must always have room for at least one skb.
2911 	 * Therefore, stop the queue when the free entries reaches
2912 	 * the maximum number of descriptors per skb.
2913 	 */
2914 	txq->tx_stop_threshold = txq->size - MVNETA_MAX_SKB_DESCS;
2915 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2916 
2917 
2918 	/* Allocate memory for TX descriptors */
2919 	txq->descs = dma_alloc_coherent(pp->dev->dev.parent,
2920 					txq->size * MVNETA_DESC_ALIGNED_SIZE,
2921 					&txq->descs_phys, GFP_KERNEL);
2922 	if (txq->descs == NULL)
2923 		return -ENOMEM;
2924 
2925 	txq->last_desc = txq->size - 1;
2926 
2927 	/* Set maximum bandwidth for enabled TXQs */
2928 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
2929 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
2930 
2931 	/* Set Tx descriptors queue starting address */
2932 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
2933 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
2934 
2935 	txq->tx_skb = kmalloc(txq->size * sizeof(*txq->tx_skb), GFP_KERNEL);
2936 	if (txq->tx_skb == NULL) {
2937 		dma_free_coherent(pp->dev->dev.parent,
2938 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2939 				  txq->descs, txq->descs_phys);
2940 		return -ENOMEM;
2941 	}
2942 
2943 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
2944 	txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
2945 					   txq->size * TSO_HEADER_SIZE,
2946 					   &txq->tso_hdrs_phys, GFP_KERNEL);
2947 	if (txq->tso_hdrs == NULL) {
2948 		kfree(txq->tx_skb);
2949 		dma_free_coherent(pp->dev->dev.parent,
2950 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2951 				  txq->descs, txq->descs_phys);
2952 		return -ENOMEM;
2953 	}
2954 	mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
2955 
2956 	/* Setup XPS mapping */
2957 	if (txq_number > 1)
2958 		cpu = txq->id % num_present_cpus();
2959 	else
2960 		cpu = pp->rxq_def % num_present_cpus();
2961 	cpumask_set_cpu(cpu, &txq->affinity_mask);
2962 	netif_set_xps_queue(pp->dev, &txq->affinity_mask, txq->id);
2963 
2964 	return 0;
2965 }
2966 
2967 /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
2968 static void mvneta_txq_deinit(struct mvneta_port *pp,
2969 			      struct mvneta_tx_queue *txq)
2970 {
2971 	struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
2972 
2973 	kfree(txq->tx_skb);
2974 
2975 	if (txq->tso_hdrs)
2976 		dma_free_coherent(pp->dev->dev.parent,
2977 				  txq->size * TSO_HEADER_SIZE,
2978 				  txq->tso_hdrs, txq->tso_hdrs_phys);
2979 	if (txq->descs)
2980 		dma_free_coherent(pp->dev->dev.parent,
2981 				  txq->size * MVNETA_DESC_ALIGNED_SIZE,
2982 				  txq->descs, txq->descs_phys);
2983 
2984 	netdev_tx_reset_queue(nq);
2985 
2986 	txq->descs             = NULL;
2987 	txq->last_desc         = 0;
2988 	txq->next_desc_to_proc = 0;
2989 	txq->descs_phys        = 0;
2990 
2991 	/* Set minimum bandwidth for disabled TXQs */
2992 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
2993 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
2994 
2995 	/* Set Tx descriptors queue starting address and size */
2996 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
2997 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
2998 }
2999 
3000 /* Cleanup all Tx queues */
3001 static void mvneta_cleanup_txqs(struct mvneta_port *pp)
3002 {
3003 	int queue;
3004 
3005 	for (queue = 0; queue < txq_number; queue++)
3006 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
3007 }
3008 
3009 /* Cleanup all Rx queues */
3010 static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
3011 {
3012 	int queue;
3013 
3014 	for (queue = 0; queue < txq_number; queue++)
3015 		mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
3016 }
3017 
3018 
3019 /* Init all Rx queues */
3020 static int mvneta_setup_rxqs(struct mvneta_port *pp)
3021 {
3022 	int queue;
3023 
3024 	for (queue = 0; queue < rxq_number; queue++) {
3025 		int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
3026 
3027 		if (err) {
3028 			netdev_err(pp->dev, "%s: can't create rxq=%d\n",
3029 				   __func__, queue);
3030 			mvneta_cleanup_rxqs(pp);
3031 			return err;
3032 		}
3033 	}
3034 
3035 	return 0;
3036 }
3037 
3038 /* Init all tx queues */
3039 static int mvneta_setup_txqs(struct mvneta_port *pp)
3040 {
3041 	int queue;
3042 
3043 	for (queue = 0; queue < txq_number; queue++) {
3044 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
3045 		if (err) {
3046 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
3047 				   __func__, queue);
3048 			mvneta_cleanup_txqs(pp);
3049 			return err;
3050 		}
3051 	}
3052 
3053 	return 0;
3054 }
3055 
3056 static void mvneta_start_dev(struct mvneta_port *pp)
3057 {
3058 	int cpu;
3059 	struct net_device *ndev = pp->dev;
3060 
3061 	mvneta_max_rx_size_set(pp, pp->pkt_size);
3062 	mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
3063 
3064 	/* start the Rx/Tx activity */
3065 	mvneta_port_enable(pp);
3066 
3067 	if (!pp->neta_armada3700) {
3068 		/* Enable polling on the port */
3069 		for_each_online_cpu(cpu) {
3070 			struct mvneta_pcpu_port *port =
3071 				per_cpu_ptr(pp->ports, cpu);
3072 
3073 			napi_enable(&port->napi);
3074 		}
3075 	} else {
3076 		napi_enable(&pp->napi);
3077 	}
3078 
3079 	/* Unmask interrupts. It has to be done from each CPU */
3080 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3081 
3082 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3083 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3084 		    MVNETA_CAUSE_LINK_CHANGE |
3085 		    MVNETA_CAUSE_PSC_SYNC_CHANGE);
3086 
3087 	phy_start(ndev->phydev);
3088 	netif_tx_start_all_queues(pp->dev);
3089 }
3090 
3091 static void mvneta_stop_dev(struct mvneta_port *pp)
3092 {
3093 	unsigned int cpu;
3094 	struct net_device *ndev = pp->dev;
3095 
3096 	phy_stop(ndev->phydev);
3097 
3098 	if (!pp->neta_armada3700) {
3099 		for_each_online_cpu(cpu) {
3100 			struct mvneta_pcpu_port *port =
3101 				per_cpu_ptr(pp->ports, cpu);
3102 
3103 			napi_disable(&port->napi);
3104 		}
3105 	} else {
3106 		napi_disable(&pp->napi);
3107 	}
3108 
3109 	netif_carrier_off(pp->dev);
3110 
3111 	mvneta_port_down(pp);
3112 	netif_tx_stop_all_queues(pp->dev);
3113 
3114 	/* Stop the port activity */
3115 	mvneta_port_disable(pp);
3116 
3117 	/* Clear all ethernet port interrupts */
3118 	on_each_cpu(mvneta_percpu_clear_intr_cause, pp, true);
3119 
3120 	/* Mask all ethernet port interrupts */
3121 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3122 
3123 	mvneta_tx_reset(pp);
3124 	mvneta_rx_reset(pp);
3125 }
3126 
3127 static void mvneta_percpu_enable(void *arg)
3128 {
3129 	struct mvneta_port *pp = arg;
3130 
3131 	enable_percpu_irq(pp->dev->irq, IRQ_TYPE_NONE);
3132 }
3133 
3134 static void mvneta_percpu_disable(void *arg)
3135 {
3136 	struct mvneta_port *pp = arg;
3137 
3138 	disable_percpu_irq(pp->dev->irq);
3139 }
3140 
3141 /* Change the device mtu */
3142 static int mvneta_change_mtu(struct net_device *dev, int mtu)
3143 {
3144 	struct mvneta_port *pp = netdev_priv(dev);
3145 	int ret;
3146 
3147 	if (!IS_ALIGNED(MVNETA_RX_PKT_SIZE(mtu), 8)) {
3148 		netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
3149 			    mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
3150 		mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3151 	}
3152 
3153 	dev->mtu = mtu;
3154 
3155 	if (!netif_running(dev)) {
3156 		if (pp->bm_priv)
3157 			mvneta_bm_update_mtu(pp, mtu);
3158 
3159 		netdev_update_features(dev);
3160 		return 0;
3161 	}
3162 
3163 	/* The interface is running, so we have to force a
3164 	 * reallocation of the queues
3165 	 */
3166 	mvneta_stop_dev(pp);
3167 	on_each_cpu(mvneta_percpu_disable, pp, true);
3168 
3169 	mvneta_cleanup_txqs(pp);
3170 	mvneta_cleanup_rxqs(pp);
3171 
3172 	if (pp->bm_priv)
3173 		mvneta_bm_update_mtu(pp, mtu);
3174 
3175 	pp->pkt_size = MVNETA_RX_PKT_SIZE(dev->mtu);
3176 	pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3177 	                SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3178 
3179 	ret = mvneta_setup_rxqs(pp);
3180 	if (ret) {
3181 		netdev_err(dev, "unable to setup rxqs after MTU change\n");
3182 		return ret;
3183 	}
3184 
3185 	ret = mvneta_setup_txqs(pp);
3186 	if (ret) {
3187 		netdev_err(dev, "unable to setup txqs after MTU change\n");
3188 		return ret;
3189 	}
3190 
3191 	on_each_cpu(mvneta_percpu_enable, pp, true);
3192 	mvneta_start_dev(pp);
3193 	mvneta_port_up(pp);
3194 
3195 	netdev_update_features(dev);
3196 
3197 	return 0;
3198 }
3199 
3200 static netdev_features_t mvneta_fix_features(struct net_device *dev,
3201 					     netdev_features_t features)
3202 {
3203 	struct mvneta_port *pp = netdev_priv(dev);
3204 
3205 	if (pp->tx_csum_limit && dev->mtu > pp->tx_csum_limit) {
3206 		features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3207 		netdev_info(dev,
3208 			    "Disable IP checksum for MTU greater than %dB\n",
3209 			    pp->tx_csum_limit);
3210 	}
3211 
3212 	return features;
3213 }
3214 
3215 /* Get mac address */
3216 static void mvneta_get_mac_addr(struct mvneta_port *pp, unsigned char *addr)
3217 {
3218 	u32 mac_addr_l, mac_addr_h;
3219 
3220 	mac_addr_l = mvreg_read(pp, MVNETA_MAC_ADDR_LOW);
3221 	mac_addr_h = mvreg_read(pp, MVNETA_MAC_ADDR_HIGH);
3222 	addr[0] = (mac_addr_h >> 24) & 0xFF;
3223 	addr[1] = (mac_addr_h >> 16) & 0xFF;
3224 	addr[2] = (mac_addr_h >> 8) & 0xFF;
3225 	addr[3] = mac_addr_h & 0xFF;
3226 	addr[4] = (mac_addr_l >> 8) & 0xFF;
3227 	addr[5] = mac_addr_l & 0xFF;
3228 }
3229 
3230 /* Handle setting mac address */
3231 static int mvneta_set_mac_addr(struct net_device *dev, void *addr)
3232 {
3233 	struct mvneta_port *pp = netdev_priv(dev);
3234 	struct sockaddr *sockaddr = addr;
3235 	int ret;
3236 
3237 	ret = eth_prepare_mac_addr_change(dev, addr);
3238 	if (ret < 0)
3239 		return ret;
3240 	/* Remove previous address table entry */
3241 	mvneta_mac_addr_set(pp, dev->dev_addr, -1);
3242 
3243 	/* Set new addr in hw */
3244 	mvneta_mac_addr_set(pp, sockaddr->sa_data, pp->rxq_def);
3245 
3246 	eth_commit_mac_addr_change(dev, addr);
3247 	return 0;
3248 }
3249 
3250 static void mvneta_adjust_link(struct net_device *ndev)
3251 {
3252 	struct mvneta_port *pp = netdev_priv(ndev);
3253 	struct phy_device *phydev = ndev->phydev;
3254 	int status_change = 0;
3255 
3256 	if (phydev->link) {
3257 		if ((pp->speed != phydev->speed) ||
3258 		    (pp->duplex != phydev->duplex)) {
3259 			u32 val;
3260 
3261 			val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3262 			val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3263 				 MVNETA_GMAC_CONFIG_GMII_SPEED |
3264 				 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3265 
3266 			if (phydev->duplex)
3267 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3268 
3269 			if (phydev->speed == SPEED_1000)
3270 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3271 			else if (phydev->speed == SPEED_100)
3272 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3273 
3274 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3275 
3276 			pp->duplex = phydev->duplex;
3277 			pp->speed  = phydev->speed;
3278 		}
3279 	}
3280 
3281 	if (phydev->link != pp->link) {
3282 		if (!phydev->link) {
3283 			pp->duplex = -1;
3284 			pp->speed = 0;
3285 		}
3286 
3287 		pp->link = phydev->link;
3288 		status_change = 1;
3289 	}
3290 
3291 	if (status_change) {
3292 		if (phydev->link) {
3293 			if (!pp->use_inband_status) {
3294 				u32 val = mvreg_read(pp,
3295 						  MVNETA_GMAC_AUTONEG_CONFIG);
3296 				val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
3297 				val |= MVNETA_GMAC_FORCE_LINK_PASS;
3298 				mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3299 					    val);
3300 			}
3301 			mvneta_port_up(pp);
3302 		} else {
3303 			if (!pp->use_inband_status) {
3304 				u32 val = mvreg_read(pp,
3305 						  MVNETA_GMAC_AUTONEG_CONFIG);
3306 				val &= ~MVNETA_GMAC_FORCE_LINK_PASS;
3307 				val |= MVNETA_GMAC_FORCE_LINK_DOWN;
3308 				mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG,
3309 					    val);
3310 			}
3311 			mvneta_port_down(pp);
3312 		}
3313 		phy_print_status(phydev);
3314 	}
3315 }
3316 
3317 static int mvneta_mdio_probe(struct mvneta_port *pp)
3318 {
3319 	struct phy_device *phy_dev;
3320 
3321 	phy_dev = of_phy_connect(pp->dev, pp->phy_node, mvneta_adjust_link, 0,
3322 				 pp->phy_interface);
3323 	if (!phy_dev) {
3324 		netdev_err(pp->dev, "could not find the PHY\n");
3325 		return -ENODEV;
3326 	}
3327 
3328 	phy_dev->supported &= PHY_GBIT_FEATURES;
3329 	phy_dev->advertising = phy_dev->supported;
3330 
3331 	pp->link    = 0;
3332 	pp->duplex  = 0;
3333 	pp->speed   = 0;
3334 
3335 	return 0;
3336 }
3337 
3338 static void mvneta_mdio_remove(struct mvneta_port *pp)
3339 {
3340 	struct net_device *ndev = pp->dev;
3341 
3342 	phy_disconnect(ndev->phydev);
3343 }
3344 
3345 /* Electing a CPU must be done in an atomic way: it should be done
3346  * after or before the removal/insertion of a CPU and this function is
3347  * not reentrant.
3348  */
3349 static void mvneta_percpu_elect(struct mvneta_port *pp)
3350 {
3351 	int elected_cpu = 0, max_cpu, cpu, i = 0;
3352 
3353 	/* Use the cpu associated to the rxq when it is online, in all
3354 	 * the other cases, use the cpu 0 which can't be offline.
3355 	 */
3356 	if (cpu_online(pp->rxq_def))
3357 		elected_cpu = pp->rxq_def;
3358 
3359 	max_cpu = num_present_cpus();
3360 
3361 	for_each_online_cpu(cpu) {
3362 		int rxq_map = 0, txq_map = 0;
3363 		int rxq;
3364 
3365 		for (rxq = 0; rxq < rxq_number; rxq++)
3366 			if ((rxq % max_cpu) == cpu)
3367 				rxq_map |= MVNETA_CPU_RXQ_ACCESS(rxq);
3368 
3369 		if (cpu == elected_cpu)
3370 			/* Map the default receive queue queue to the
3371 			 * elected CPU
3372 			 */
3373 			rxq_map |= MVNETA_CPU_RXQ_ACCESS(pp->rxq_def);
3374 
3375 		/* We update the TX queue map only if we have one
3376 		 * queue. In this case we associate the TX queue to
3377 		 * the CPU bound to the default RX queue
3378 		 */
3379 		if (txq_number == 1)
3380 			txq_map = (cpu == elected_cpu) ?
3381 				MVNETA_CPU_TXQ_ACCESS(1) : 0;
3382 		else
3383 			txq_map = mvreg_read(pp, MVNETA_CPU_MAP(cpu)) &
3384 				MVNETA_CPU_TXQ_ACCESS_ALL_MASK;
3385 
3386 		mvreg_write(pp, MVNETA_CPU_MAP(cpu), rxq_map | txq_map);
3387 
3388 		/* Update the interrupt mask on each CPU according the
3389 		 * new mapping
3390 		 */
3391 		smp_call_function_single(cpu, mvneta_percpu_unmask_interrupt,
3392 					 pp, true);
3393 		i++;
3394 
3395 	}
3396 };
3397 
3398 static int mvneta_cpu_online(unsigned int cpu, struct hlist_node *node)
3399 {
3400 	int other_cpu;
3401 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3402 						  node_online);
3403 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3404 
3405 
3406 	spin_lock(&pp->lock);
3407 	/*
3408 	 * Configuring the driver for a new CPU while the driver is
3409 	 * stopping is racy, so just avoid it.
3410 	 */
3411 	if (pp->is_stopped) {
3412 		spin_unlock(&pp->lock);
3413 		return 0;
3414 	}
3415 	netif_tx_stop_all_queues(pp->dev);
3416 
3417 	/*
3418 	 * We have to synchronise on tha napi of each CPU except the one
3419 	 * just being woken up
3420 	 */
3421 	for_each_online_cpu(other_cpu) {
3422 		if (other_cpu != cpu) {
3423 			struct mvneta_pcpu_port *other_port =
3424 				per_cpu_ptr(pp->ports, other_cpu);
3425 
3426 			napi_synchronize(&other_port->napi);
3427 		}
3428 	}
3429 
3430 	/* Mask all ethernet port interrupts */
3431 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3432 	napi_enable(&port->napi);
3433 
3434 	/*
3435 	 * Enable per-CPU interrupts on the CPU that is
3436 	 * brought up.
3437 	 */
3438 	mvneta_percpu_enable(pp);
3439 
3440 	/*
3441 	 * Enable per-CPU interrupt on the one CPU we care
3442 	 * about.
3443 	 */
3444 	mvneta_percpu_elect(pp);
3445 
3446 	/* Unmask all ethernet port interrupts */
3447 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3448 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3449 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3450 		    MVNETA_CAUSE_LINK_CHANGE |
3451 		    MVNETA_CAUSE_PSC_SYNC_CHANGE);
3452 	netif_tx_start_all_queues(pp->dev);
3453 	spin_unlock(&pp->lock);
3454 	return 0;
3455 }
3456 
3457 static int mvneta_cpu_down_prepare(unsigned int cpu, struct hlist_node *node)
3458 {
3459 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3460 						  node_online);
3461 	struct mvneta_pcpu_port *port = per_cpu_ptr(pp->ports, cpu);
3462 
3463 	/*
3464 	 * Thanks to this lock we are sure that any pending cpu election is
3465 	 * done.
3466 	 */
3467 	spin_lock(&pp->lock);
3468 	/* Mask all ethernet port interrupts */
3469 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3470 	spin_unlock(&pp->lock);
3471 
3472 	napi_synchronize(&port->napi);
3473 	napi_disable(&port->napi);
3474 	/* Disable per-CPU interrupts on the CPU that is brought down. */
3475 	mvneta_percpu_disable(pp);
3476 	return 0;
3477 }
3478 
3479 static int mvneta_cpu_dead(unsigned int cpu, struct hlist_node *node)
3480 {
3481 	struct mvneta_port *pp = hlist_entry_safe(node, struct mvneta_port,
3482 						  node_dead);
3483 
3484 	/* Check if a new CPU must be elected now this on is down */
3485 	spin_lock(&pp->lock);
3486 	mvneta_percpu_elect(pp);
3487 	spin_unlock(&pp->lock);
3488 	/* Unmask all ethernet port interrupts */
3489 	on_each_cpu(mvneta_percpu_unmask_interrupt, pp, true);
3490 	mvreg_write(pp, MVNETA_INTR_MISC_MASK,
3491 		    MVNETA_CAUSE_PHY_STATUS_CHANGE |
3492 		    MVNETA_CAUSE_LINK_CHANGE |
3493 		    MVNETA_CAUSE_PSC_SYNC_CHANGE);
3494 	netif_tx_start_all_queues(pp->dev);
3495 	return 0;
3496 }
3497 
3498 static int mvneta_open(struct net_device *dev)
3499 {
3500 	struct mvneta_port *pp = netdev_priv(dev);
3501 	int ret;
3502 
3503 	pp->pkt_size = MVNETA_RX_PKT_SIZE(pp->dev->mtu);
3504 	pp->frag_size = SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(pp->pkt_size)) +
3505 	                SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3506 
3507 	ret = mvneta_setup_rxqs(pp);
3508 	if (ret)
3509 		return ret;
3510 
3511 	ret = mvneta_setup_txqs(pp);
3512 	if (ret)
3513 		goto err_cleanup_rxqs;
3514 
3515 	/* Connect to port interrupt line */
3516 	if (pp->neta_armada3700)
3517 		ret = request_irq(pp->dev->irq, mvneta_isr, 0,
3518 				  dev->name, pp);
3519 	else
3520 		ret = request_percpu_irq(pp->dev->irq, mvneta_percpu_isr,
3521 					 dev->name, pp->ports);
3522 	if (ret) {
3523 		netdev_err(pp->dev, "cannot request irq %d\n", pp->dev->irq);
3524 		goto err_cleanup_txqs;
3525 	}
3526 
3527 	if (!pp->neta_armada3700) {
3528 		/* Enable per-CPU interrupt on all the CPU to handle our RX
3529 		 * queue interrupts
3530 		 */
3531 		on_each_cpu(mvneta_percpu_enable, pp, true);
3532 
3533 		pp->is_stopped = false;
3534 		/* Register a CPU notifier to handle the case where our CPU
3535 		 * might be taken offline.
3536 		 */
3537 		ret = cpuhp_state_add_instance_nocalls(online_hpstate,
3538 						       &pp->node_online);
3539 		if (ret)
3540 			goto err_free_irq;
3541 
3542 		ret = cpuhp_state_add_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3543 						       &pp->node_dead);
3544 		if (ret)
3545 			goto err_free_online_hp;
3546 	}
3547 
3548 	/* In default link is down */
3549 	netif_carrier_off(pp->dev);
3550 
3551 	ret = mvneta_mdio_probe(pp);
3552 	if (ret < 0) {
3553 		netdev_err(dev, "cannot probe MDIO bus\n");
3554 		goto err_free_dead_hp;
3555 	}
3556 
3557 	mvneta_start_dev(pp);
3558 
3559 	return 0;
3560 
3561 err_free_dead_hp:
3562 	if (!pp->neta_armada3700)
3563 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3564 						    &pp->node_dead);
3565 err_free_online_hp:
3566 	if (!pp->neta_armada3700)
3567 		cpuhp_state_remove_instance_nocalls(online_hpstate,
3568 						    &pp->node_online);
3569 err_free_irq:
3570 	if (pp->neta_armada3700) {
3571 		free_irq(pp->dev->irq, pp);
3572 	} else {
3573 		on_each_cpu(mvneta_percpu_disable, pp, true);
3574 		free_percpu_irq(pp->dev->irq, pp->ports);
3575 	}
3576 err_cleanup_txqs:
3577 	mvneta_cleanup_txqs(pp);
3578 err_cleanup_rxqs:
3579 	mvneta_cleanup_rxqs(pp);
3580 	return ret;
3581 }
3582 
3583 /* Stop the port, free port interrupt line */
3584 static int mvneta_stop(struct net_device *dev)
3585 {
3586 	struct mvneta_port *pp = netdev_priv(dev);
3587 
3588 	if (!pp->neta_armada3700) {
3589 		/* Inform that we are stopping so we don't want to setup the
3590 		 * driver for new CPUs in the notifiers. The code of the
3591 		 * notifier for CPU online is protected by the same spinlock,
3592 		 * so when we get the lock, the notifer work is done.
3593 		 */
3594 		spin_lock(&pp->lock);
3595 		pp->is_stopped = true;
3596 		spin_unlock(&pp->lock);
3597 
3598 		mvneta_stop_dev(pp);
3599 		mvneta_mdio_remove(pp);
3600 
3601 		cpuhp_state_remove_instance_nocalls(online_hpstate,
3602 						    &pp->node_online);
3603 		cpuhp_state_remove_instance_nocalls(CPUHP_NET_MVNETA_DEAD,
3604 						    &pp->node_dead);
3605 		on_each_cpu(mvneta_percpu_disable, pp, true);
3606 		free_percpu_irq(dev->irq, pp->ports);
3607 	} else {
3608 		mvneta_stop_dev(pp);
3609 		mvneta_mdio_remove(pp);
3610 		free_irq(dev->irq, pp);
3611 	}
3612 
3613 	mvneta_cleanup_rxqs(pp);
3614 	mvneta_cleanup_txqs(pp);
3615 
3616 	return 0;
3617 }
3618 
3619 static int mvneta_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3620 {
3621 	if (!dev->phydev)
3622 		return -ENOTSUPP;
3623 
3624 	return phy_mii_ioctl(dev->phydev, ifr, cmd);
3625 }
3626 
3627 /* Ethtool methods */
3628 
3629 /* Set link ksettings (phy address, speed) for ethtools */
3630 static int
3631 mvneta_ethtool_set_link_ksettings(struct net_device *ndev,
3632 				  const struct ethtool_link_ksettings *cmd)
3633 {
3634 	struct mvneta_port *pp = netdev_priv(ndev);
3635 	struct phy_device *phydev = ndev->phydev;
3636 
3637 	if (!phydev)
3638 		return -ENODEV;
3639 
3640 	if ((cmd->base.autoneg == AUTONEG_ENABLE) != pp->use_inband_status) {
3641 		u32 val;
3642 
3643 		mvneta_set_autoneg(pp, cmd->base.autoneg == AUTONEG_ENABLE);
3644 
3645 		if (cmd->base.autoneg == AUTONEG_DISABLE) {
3646 			val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3647 			val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
3648 				 MVNETA_GMAC_CONFIG_GMII_SPEED |
3649 				 MVNETA_GMAC_CONFIG_FULL_DUPLEX);
3650 
3651 			if (phydev->duplex)
3652 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3653 
3654 			if (phydev->speed == SPEED_1000)
3655 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3656 			else if (phydev->speed == SPEED_100)
3657 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
3658 
3659 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
3660 		}
3661 
3662 		pp->use_inband_status = (cmd->base.autoneg == AUTONEG_ENABLE);
3663 		netdev_info(pp->dev, "autoneg status set to %i\n",
3664 			    pp->use_inband_status);
3665 
3666 		if (netif_running(ndev)) {
3667 			mvneta_port_down(pp);
3668 			mvneta_port_up(pp);
3669 		}
3670 	}
3671 
3672 	return phy_ethtool_ksettings_set(ndev->phydev, cmd);
3673 }
3674 
3675 /* Set interrupt coalescing for ethtools */
3676 static int mvneta_ethtool_set_coalesce(struct net_device *dev,
3677 				       struct ethtool_coalesce *c)
3678 {
3679 	struct mvneta_port *pp = netdev_priv(dev);
3680 	int queue;
3681 
3682 	for (queue = 0; queue < rxq_number; queue++) {
3683 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
3684 		rxq->time_coal = c->rx_coalesce_usecs;
3685 		rxq->pkts_coal = c->rx_max_coalesced_frames;
3686 		mvneta_rx_pkts_coal_set(pp, rxq, rxq->pkts_coal);
3687 		mvneta_rx_time_coal_set(pp, rxq, rxq->time_coal);
3688 	}
3689 
3690 	for (queue = 0; queue < txq_number; queue++) {
3691 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
3692 		txq->done_pkts_coal = c->tx_max_coalesced_frames;
3693 		mvneta_tx_done_pkts_coal_set(pp, txq, txq->done_pkts_coal);
3694 	}
3695 
3696 	return 0;
3697 }
3698 
3699 /* get coalescing for ethtools */
3700 static int mvneta_ethtool_get_coalesce(struct net_device *dev,
3701 				       struct ethtool_coalesce *c)
3702 {
3703 	struct mvneta_port *pp = netdev_priv(dev);
3704 
3705 	c->rx_coalesce_usecs        = pp->rxqs[0].time_coal;
3706 	c->rx_max_coalesced_frames  = pp->rxqs[0].pkts_coal;
3707 
3708 	c->tx_max_coalesced_frames =  pp->txqs[0].done_pkts_coal;
3709 	return 0;
3710 }
3711 
3712 
3713 static void mvneta_ethtool_get_drvinfo(struct net_device *dev,
3714 				    struct ethtool_drvinfo *drvinfo)
3715 {
3716 	strlcpy(drvinfo->driver, MVNETA_DRIVER_NAME,
3717 		sizeof(drvinfo->driver));
3718 	strlcpy(drvinfo->version, MVNETA_DRIVER_VERSION,
3719 		sizeof(drvinfo->version));
3720 	strlcpy(drvinfo->bus_info, dev_name(&dev->dev),
3721 		sizeof(drvinfo->bus_info));
3722 }
3723 
3724 
3725 static void mvneta_ethtool_get_ringparam(struct net_device *netdev,
3726 					 struct ethtool_ringparam *ring)
3727 {
3728 	struct mvneta_port *pp = netdev_priv(netdev);
3729 
3730 	ring->rx_max_pending = MVNETA_MAX_RXD;
3731 	ring->tx_max_pending = MVNETA_MAX_TXD;
3732 	ring->rx_pending = pp->rx_ring_size;
3733 	ring->tx_pending = pp->tx_ring_size;
3734 }
3735 
3736 static int mvneta_ethtool_set_ringparam(struct net_device *dev,
3737 					struct ethtool_ringparam *ring)
3738 {
3739 	struct mvneta_port *pp = netdev_priv(dev);
3740 
3741 	if ((ring->rx_pending == 0) || (ring->tx_pending == 0))
3742 		return -EINVAL;
3743 	pp->rx_ring_size = ring->rx_pending < MVNETA_MAX_RXD ?
3744 		ring->rx_pending : MVNETA_MAX_RXD;
3745 
3746 	pp->tx_ring_size = clamp_t(u16, ring->tx_pending,
3747 				   MVNETA_MAX_SKB_DESCS * 2, MVNETA_MAX_TXD);
3748 	if (pp->tx_ring_size != ring->tx_pending)
3749 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
3750 			    pp->tx_ring_size, ring->tx_pending);
3751 
3752 	if (netif_running(dev)) {
3753 		mvneta_stop(dev);
3754 		if (mvneta_open(dev)) {
3755 			netdev_err(dev,
3756 				   "error on opening device after ring param change\n");
3757 			return -ENOMEM;
3758 		}
3759 	}
3760 
3761 	return 0;
3762 }
3763 
3764 static void mvneta_ethtool_get_strings(struct net_device *netdev, u32 sset,
3765 				       u8 *data)
3766 {
3767 	if (sset == ETH_SS_STATS) {
3768 		int i;
3769 
3770 		for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3771 			memcpy(data + i * ETH_GSTRING_LEN,
3772 			       mvneta_statistics[i].name, ETH_GSTRING_LEN);
3773 	}
3774 }
3775 
3776 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
3777 {
3778 	const struct mvneta_statistic *s;
3779 	void __iomem *base = pp->base;
3780 	u32 high, low, val;
3781 	u64 val64;
3782 	int i;
3783 
3784 	for (i = 0, s = mvneta_statistics;
3785 	     s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
3786 	     s++, i++) {
3787 		switch (s->type) {
3788 		case T_REG_32:
3789 			val = readl_relaxed(base + s->offset);
3790 			pp->ethtool_stats[i] += val;
3791 			break;
3792 		case T_REG_64:
3793 			/* Docs say to read low 32-bit then high */
3794 			low = readl_relaxed(base + s->offset);
3795 			high = readl_relaxed(base + s->offset + 4);
3796 			val64 = (u64)high << 32 | low;
3797 			pp->ethtool_stats[i] += val64;
3798 			break;
3799 		}
3800 	}
3801 }
3802 
3803 static void mvneta_ethtool_get_stats(struct net_device *dev,
3804 				     struct ethtool_stats *stats, u64 *data)
3805 {
3806 	struct mvneta_port *pp = netdev_priv(dev);
3807 	int i;
3808 
3809 	mvneta_ethtool_update_stats(pp);
3810 
3811 	for (i = 0; i < ARRAY_SIZE(mvneta_statistics); i++)
3812 		*data++ = pp->ethtool_stats[i];
3813 }
3814 
3815 static int mvneta_ethtool_get_sset_count(struct net_device *dev, int sset)
3816 {
3817 	if (sset == ETH_SS_STATS)
3818 		return ARRAY_SIZE(mvneta_statistics);
3819 	return -EOPNOTSUPP;
3820 }
3821 
3822 static u32 mvneta_ethtool_get_rxfh_indir_size(struct net_device *dev)
3823 {
3824 	return MVNETA_RSS_LU_TABLE_SIZE;
3825 }
3826 
3827 static int mvneta_ethtool_get_rxnfc(struct net_device *dev,
3828 				    struct ethtool_rxnfc *info,
3829 				    u32 *rules __always_unused)
3830 {
3831 	switch (info->cmd) {
3832 	case ETHTOOL_GRXRINGS:
3833 		info->data =  rxq_number;
3834 		return 0;
3835 	case ETHTOOL_GRXFH:
3836 		return -EOPNOTSUPP;
3837 	default:
3838 		return -EOPNOTSUPP;
3839 	}
3840 }
3841 
3842 static int  mvneta_config_rss(struct mvneta_port *pp)
3843 {
3844 	int cpu;
3845 	u32 val;
3846 
3847 	netif_tx_stop_all_queues(pp->dev);
3848 
3849 	on_each_cpu(mvneta_percpu_mask_interrupt, pp, true);
3850 
3851 	/* We have to synchronise on the napi of each CPU */
3852 	for_each_online_cpu(cpu) {
3853 		struct mvneta_pcpu_port *pcpu_port =
3854 			per_cpu_ptr(pp->ports, cpu);
3855 
3856 		napi_synchronize(&pcpu_port->napi);
3857 		napi_disable(&pcpu_port->napi);
3858 	}
3859 
3860 	pp->rxq_def = pp->indir[0];
3861 
3862 	/* Update unicast mapping */
3863 	mvneta_set_rx_mode(pp->dev);
3864 
3865 	/* Update val of portCfg register accordingly with all RxQueue types */
3866 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(pp->rxq_def);
3867 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
3868 
3869 	/* Update the elected CPU matching the new rxq_def */
3870 	spin_lock(&pp->lock);
3871 	mvneta_percpu_elect(pp);
3872 	spin_unlock(&pp->lock);
3873 
3874 	/* We have to synchronise on the napi of each CPU */
3875 	for_each_online_cpu(cpu) {
3876 		struct mvneta_pcpu_port *pcpu_port =
3877 			per_cpu_ptr(pp->ports, cpu);
3878 
3879 		napi_enable(&pcpu_port->napi);
3880 	}
3881 
3882 	netif_tx_start_all_queues(pp->dev);
3883 
3884 	return 0;
3885 }
3886 
3887 static int mvneta_ethtool_set_rxfh(struct net_device *dev, const u32 *indir,
3888 				   const u8 *key, const u8 hfunc)
3889 {
3890 	struct mvneta_port *pp = netdev_priv(dev);
3891 
3892 	/* Current code for Armada 3700 doesn't support RSS features yet */
3893 	if (pp->neta_armada3700)
3894 		return -EOPNOTSUPP;
3895 
3896 	/* We require at least one supported parameter to be changed
3897 	 * and no change in any of the unsupported parameters
3898 	 */
3899 	if (key ||
3900 	    (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP))
3901 		return -EOPNOTSUPP;
3902 
3903 	if (!indir)
3904 		return 0;
3905 
3906 	memcpy(pp->indir, indir, MVNETA_RSS_LU_TABLE_SIZE);
3907 
3908 	return mvneta_config_rss(pp);
3909 }
3910 
3911 static int mvneta_ethtool_get_rxfh(struct net_device *dev, u32 *indir, u8 *key,
3912 				   u8 *hfunc)
3913 {
3914 	struct mvneta_port *pp = netdev_priv(dev);
3915 
3916 	/* Current code for Armada 3700 doesn't support RSS features yet */
3917 	if (pp->neta_armada3700)
3918 		return -EOPNOTSUPP;
3919 
3920 	if (hfunc)
3921 		*hfunc = ETH_RSS_HASH_TOP;
3922 
3923 	if (!indir)
3924 		return 0;
3925 
3926 	memcpy(indir, pp->indir, MVNETA_RSS_LU_TABLE_SIZE);
3927 
3928 	return 0;
3929 }
3930 
3931 static void mvneta_ethtool_get_wol(struct net_device *dev,
3932 				   struct ethtool_wolinfo *wol)
3933 {
3934 	wol->supported = 0;
3935 	wol->wolopts = 0;
3936 
3937 	if (dev->phydev)
3938 		phy_ethtool_get_wol(dev->phydev, wol);
3939 }
3940 
3941 static int mvneta_ethtool_set_wol(struct net_device *dev,
3942 				  struct ethtool_wolinfo *wol)
3943 {
3944 	if (!dev->phydev)
3945 		return -EOPNOTSUPP;
3946 
3947 	return phy_ethtool_set_wol(dev->phydev, wol);
3948 }
3949 
3950 static const struct net_device_ops mvneta_netdev_ops = {
3951 	.ndo_open            = mvneta_open,
3952 	.ndo_stop            = mvneta_stop,
3953 	.ndo_start_xmit      = mvneta_tx,
3954 	.ndo_set_rx_mode     = mvneta_set_rx_mode,
3955 	.ndo_set_mac_address = mvneta_set_mac_addr,
3956 	.ndo_change_mtu      = mvneta_change_mtu,
3957 	.ndo_fix_features    = mvneta_fix_features,
3958 	.ndo_get_stats64     = mvneta_get_stats64,
3959 	.ndo_do_ioctl        = mvneta_ioctl,
3960 };
3961 
3962 static const struct ethtool_ops mvneta_eth_tool_ops = {
3963 	.nway_reset	= phy_ethtool_nway_reset,
3964 	.get_link       = ethtool_op_get_link,
3965 	.set_coalesce   = mvneta_ethtool_set_coalesce,
3966 	.get_coalesce   = mvneta_ethtool_get_coalesce,
3967 	.get_drvinfo    = mvneta_ethtool_get_drvinfo,
3968 	.get_ringparam  = mvneta_ethtool_get_ringparam,
3969 	.set_ringparam	= mvneta_ethtool_set_ringparam,
3970 	.get_strings	= mvneta_ethtool_get_strings,
3971 	.get_ethtool_stats = mvneta_ethtool_get_stats,
3972 	.get_sset_count	= mvneta_ethtool_get_sset_count,
3973 	.get_rxfh_indir_size = mvneta_ethtool_get_rxfh_indir_size,
3974 	.get_rxnfc	= mvneta_ethtool_get_rxnfc,
3975 	.get_rxfh	= mvneta_ethtool_get_rxfh,
3976 	.set_rxfh	= mvneta_ethtool_set_rxfh,
3977 	.get_link_ksettings = phy_ethtool_get_link_ksettings,
3978 	.set_link_ksettings = mvneta_ethtool_set_link_ksettings,
3979 	.get_wol        = mvneta_ethtool_get_wol,
3980 	.set_wol        = mvneta_ethtool_set_wol,
3981 };
3982 
3983 /* Initialize hw */
3984 static int mvneta_init(struct device *dev, struct mvneta_port *pp)
3985 {
3986 	int queue;
3987 
3988 	/* Disable port */
3989 	mvneta_port_disable(pp);
3990 
3991 	/* Set port default values */
3992 	mvneta_defaults_set(pp);
3993 
3994 	pp->txqs = devm_kcalloc(dev, txq_number, sizeof(struct mvneta_tx_queue),
3995 				GFP_KERNEL);
3996 	if (!pp->txqs)
3997 		return -ENOMEM;
3998 
3999 	/* Initialize TX descriptor rings */
4000 	for (queue = 0; queue < txq_number; queue++) {
4001 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
4002 		txq->id = queue;
4003 		txq->size = pp->tx_ring_size;
4004 		txq->done_pkts_coal = MVNETA_TXDONE_COAL_PKTS;
4005 	}
4006 
4007 	pp->rxqs = devm_kcalloc(dev, rxq_number, sizeof(struct mvneta_rx_queue),
4008 				GFP_KERNEL);
4009 	if (!pp->rxqs)
4010 		return -ENOMEM;
4011 
4012 	/* Create Rx descriptor rings */
4013 	for (queue = 0; queue < rxq_number; queue++) {
4014 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
4015 		rxq->id = queue;
4016 		rxq->size = pp->rx_ring_size;
4017 		rxq->pkts_coal = MVNETA_RX_COAL_PKTS;
4018 		rxq->time_coal = MVNETA_RX_COAL_USEC;
4019 		rxq->buf_virt_addr = devm_kmalloc(pp->dev->dev.parent,
4020 						  rxq->size * sizeof(void *),
4021 						  GFP_KERNEL);
4022 		if (!rxq->buf_virt_addr)
4023 			return -ENOMEM;
4024 	}
4025 
4026 	return 0;
4027 }
4028 
4029 /* platform glue : initialize decoding windows */
4030 static void mvneta_conf_mbus_windows(struct mvneta_port *pp,
4031 				     const struct mbus_dram_target_info *dram)
4032 {
4033 	u32 win_enable;
4034 	u32 win_protect;
4035 	int i;
4036 
4037 	for (i = 0; i < 6; i++) {
4038 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
4039 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
4040 
4041 		if (i < 4)
4042 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
4043 	}
4044 
4045 	win_enable = 0x3f;
4046 	win_protect = 0;
4047 
4048 	if (dram) {
4049 		for (i = 0; i < dram->num_cs; i++) {
4050 			const struct mbus_dram_window *cs = dram->cs + i;
4051 
4052 			mvreg_write(pp, MVNETA_WIN_BASE(i),
4053 				    (cs->base & 0xffff0000) |
4054 				    (cs->mbus_attr << 8) |
4055 				    dram->mbus_dram_target_id);
4056 
4057 			mvreg_write(pp, MVNETA_WIN_SIZE(i),
4058 				    (cs->size - 1) & 0xffff0000);
4059 
4060 			win_enable &= ~(1 << i);
4061 			win_protect |= 3 << (2 * i);
4062 		}
4063 	} else {
4064 		/* For Armada3700 open default 4GB Mbus window, leaving
4065 		 * arbitration of target/attribute to a different layer
4066 		 * of configuration.
4067 		 */
4068 		mvreg_write(pp, MVNETA_WIN_SIZE(0), 0xffff0000);
4069 		win_enable &= ~BIT(0);
4070 		win_protect = 3;
4071 	}
4072 
4073 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
4074 	mvreg_write(pp, MVNETA_ACCESS_PROTECT_ENABLE, win_protect);
4075 }
4076 
4077 /* Power up the port */
4078 static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
4079 {
4080 	u32 ctrl;
4081 
4082 	/* MAC Cause register should be cleared */
4083 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
4084 
4085 	ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
4086 
4087 	/* Even though it might look weird, when we're configured in
4088 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
4089 	 */
4090 	switch(phy_mode) {
4091 	case PHY_INTERFACE_MODE_QSGMII:
4092 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4093 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4094 		break;
4095 	case PHY_INTERFACE_MODE_SGMII:
4096 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4097 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
4098 		break;
4099 	case PHY_INTERFACE_MODE_RGMII:
4100 	case PHY_INTERFACE_MODE_RGMII_ID:
4101 		ctrl |= MVNETA_GMAC2_PORT_RGMII;
4102 		break;
4103 	default:
4104 		return -EINVAL;
4105 	}
4106 
4107 	/* Cancel Port Reset */
4108 	ctrl &= ~MVNETA_GMAC2_PORT_RESET;
4109 	mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
4110 
4111 	while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
4112 		MVNETA_GMAC2_PORT_RESET) != 0)
4113 		continue;
4114 
4115 	return 0;
4116 }
4117 
4118 /* Device initialization routine */
4119 static int mvneta_probe(struct platform_device *pdev)
4120 {
4121 	const struct mbus_dram_target_info *dram_target_info;
4122 	struct resource *res;
4123 	struct device_node *dn = pdev->dev.of_node;
4124 	struct device_node *phy_node;
4125 	struct device_node *bm_node;
4126 	struct mvneta_port *pp;
4127 	struct net_device *dev;
4128 	const char *dt_mac_addr;
4129 	char hw_mac_addr[ETH_ALEN];
4130 	const char *mac_from;
4131 	const char *managed;
4132 	int tx_csum_limit;
4133 	int phy_mode;
4134 	int err;
4135 	int cpu;
4136 
4137 	dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
4138 	if (!dev)
4139 		return -ENOMEM;
4140 
4141 	dev->irq = irq_of_parse_and_map(dn, 0);
4142 	if (dev->irq == 0) {
4143 		err = -EINVAL;
4144 		goto err_free_netdev;
4145 	}
4146 
4147 	phy_node = of_parse_phandle(dn, "phy", 0);
4148 	if (!phy_node) {
4149 		if (!of_phy_is_fixed_link(dn)) {
4150 			dev_err(&pdev->dev, "no PHY specified\n");
4151 			err = -ENODEV;
4152 			goto err_free_irq;
4153 		}
4154 
4155 		err = of_phy_register_fixed_link(dn);
4156 		if (err < 0) {
4157 			dev_err(&pdev->dev, "cannot register fixed PHY\n");
4158 			goto err_free_irq;
4159 		}
4160 
4161 		/* In the case of a fixed PHY, the DT node associated
4162 		 * to the PHY is the Ethernet MAC DT node.
4163 		 */
4164 		phy_node = of_node_get(dn);
4165 	}
4166 
4167 	phy_mode = of_get_phy_mode(dn);
4168 	if (phy_mode < 0) {
4169 		dev_err(&pdev->dev, "incorrect phy-mode\n");
4170 		err = -EINVAL;
4171 		goto err_put_phy_node;
4172 	}
4173 
4174 	dev->tx_queue_len = MVNETA_MAX_TXD;
4175 	dev->watchdog_timeo = 5 * HZ;
4176 	dev->netdev_ops = &mvneta_netdev_ops;
4177 
4178 	dev->ethtool_ops = &mvneta_eth_tool_ops;
4179 
4180 	pp = netdev_priv(dev);
4181 	spin_lock_init(&pp->lock);
4182 	pp->phy_node = phy_node;
4183 	pp->phy_interface = phy_mode;
4184 
4185 	err = of_property_read_string(dn, "managed", &managed);
4186 	pp->use_inband_status = (err == 0 &&
4187 				 strcmp(managed, "in-band-status") == 0);
4188 
4189 	pp->rxq_def = rxq_def;
4190 
4191 	/* Set RX packet offset correction for platforms, whose
4192 	 * NET_SKB_PAD, exceeds 64B. It should be 64B for 64-bit
4193 	 * platforms and 0B for 32-bit ones.
4194 	 */
4195 	pp->rx_offset_correction =
4196 		max(0, NET_SKB_PAD - MVNETA_RX_PKT_OFFSET_CORRECTION);
4197 
4198 	pp->indir[0] = rxq_def;
4199 
4200 	/* Get special SoC configurations */
4201 	if (of_device_is_compatible(dn, "marvell,armada-3700-neta"))
4202 		pp->neta_armada3700 = true;
4203 
4204 	pp->clk = devm_clk_get(&pdev->dev, "core");
4205 	if (IS_ERR(pp->clk))
4206 		pp->clk = devm_clk_get(&pdev->dev, NULL);
4207 	if (IS_ERR(pp->clk)) {
4208 		err = PTR_ERR(pp->clk);
4209 		goto err_put_phy_node;
4210 	}
4211 
4212 	clk_prepare_enable(pp->clk);
4213 
4214 	pp->clk_bus = devm_clk_get(&pdev->dev, "bus");
4215 	if (!IS_ERR(pp->clk_bus))
4216 		clk_prepare_enable(pp->clk_bus);
4217 
4218 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4219 	pp->base = devm_ioremap_resource(&pdev->dev, res);
4220 	if (IS_ERR(pp->base)) {
4221 		err = PTR_ERR(pp->base);
4222 		goto err_clk;
4223 	}
4224 
4225 	/* Alloc per-cpu port structure */
4226 	pp->ports = alloc_percpu(struct mvneta_pcpu_port);
4227 	if (!pp->ports) {
4228 		err = -ENOMEM;
4229 		goto err_clk;
4230 	}
4231 
4232 	/* Alloc per-cpu stats */
4233 	pp->stats = netdev_alloc_pcpu_stats(struct mvneta_pcpu_stats);
4234 	if (!pp->stats) {
4235 		err = -ENOMEM;
4236 		goto err_free_ports;
4237 	}
4238 
4239 	dt_mac_addr = of_get_mac_address(dn);
4240 	if (dt_mac_addr) {
4241 		mac_from = "device tree";
4242 		memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
4243 	} else {
4244 		mvneta_get_mac_addr(pp, hw_mac_addr);
4245 		if (is_valid_ether_addr(hw_mac_addr)) {
4246 			mac_from = "hardware";
4247 			memcpy(dev->dev_addr, hw_mac_addr, ETH_ALEN);
4248 		} else {
4249 			mac_from = "random";
4250 			eth_hw_addr_random(dev);
4251 		}
4252 	}
4253 
4254 	if (!of_property_read_u32(dn, "tx-csum-limit", &tx_csum_limit)) {
4255 		if (tx_csum_limit < 0 ||
4256 		    tx_csum_limit > MVNETA_TX_CSUM_MAX_SIZE) {
4257 			tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4258 			dev_info(&pdev->dev,
4259 				 "Wrong TX csum limit in DT, set to %dB\n",
4260 				 MVNETA_TX_CSUM_DEF_SIZE);
4261 		}
4262 	} else if (of_device_is_compatible(dn, "marvell,armada-370-neta")) {
4263 		tx_csum_limit = MVNETA_TX_CSUM_DEF_SIZE;
4264 	} else {
4265 		tx_csum_limit = MVNETA_TX_CSUM_MAX_SIZE;
4266 	}
4267 
4268 	pp->tx_csum_limit = tx_csum_limit;
4269 
4270 	dram_target_info = mv_mbus_dram_info();
4271 	/* Armada3700 requires setting default configuration of Mbus
4272 	 * windows, however without using filled mbus_dram_target_info
4273 	 * structure.
4274 	 */
4275 	if (dram_target_info || pp->neta_armada3700)
4276 		mvneta_conf_mbus_windows(pp, dram_target_info);
4277 
4278 	pp->tx_ring_size = MVNETA_MAX_TXD;
4279 	pp->rx_ring_size = MVNETA_MAX_RXD;
4280 
4281 	pp->dev = dev;
4282 	SET_NETDEV_DEV(dev, &pdev->dev);
4283 
4284 	pp->id = global_port_id++;
4285 
4286 	/* Obtain access to BM resources if enabled and already initialized */
4287 	bm_node = of_parse_phandle(dn, "buffer-manager", 0);
4288 	if (bm_node && bm_node->data) {
4289 		pp->bm_priv = bm_node->data;
4290 		err = mvneta_bm_port_init(pdev, pp);
4291 		if (err < 0) {
4292 			dev_info(&pdev->dev, "use SW buffer management\n");
4293 			pp->bm_priv = NULL;
4294 		}
4295 	}
4296 	of_node_put(bm_node);
4297 
4298 	err = mvneta_init(&pdev->dev, pp);
4299 	if (err < 0)
4300 		goto err_netdev;
4301 
4302 	err = mvneta_port_power_up(pp, phy_mode);
4303 	if (err < 0) {
4304 		dev_err(&pdev->dev, "can't power up port\n");
4305 		goto err_netdev;
4306 	}
4307 
4308 	/* Armada3700 network controller does not support per-cpu
4309 	 * operation, so only single NAPI should be initialized.
4310 	 */
4311 	if (pp->neta_armada3700) {
4312 		netif_napi_add(dev, &pp->napi, mvneta_poll, NAPI_POLL_WEIGHT);
4313 	} else {
4314 		for_each_present_cpu(cpu) {
4315 			struct mvneta_pcpu_port *port =
4316 				per_cpu_ptr(pp->ports, cpu);
4317 
4318 			netif_napi_add(dev, &port->napi, mvneta_poll,
4319 				       NAPI_POLL_WEIGHT);
4320 			port->pp = pp;
4321 		}
4322 	}
4323 
4324 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
4325 	dev->hw_features |= dev->features;
4326 	dev->vlan_features |= dev->features;
4327 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
4328 	dev->gso_max_segs = MVNETA_MAX_TSO_SEGS;
4329 
4330 	/* MTU range: 68 - 9676 */
4331 	dev->min_mtu = ETH_MIN_MTU;
4332 	/* 9676 == 9700 - 20 and rounding to 8 */
4333 	dev->max_mtu = 9676;
4334 
4335 	err = register_netdev(dev);
4336 	if (err < 0) {
4337 		dev_err(&pdev->dev, "failed to register\n");
4338 		goto err_free_stats;
4339 	}
4340 
4341 	netdev_info(dev, "Using %s mac address %pM\n", mac_from,
4342 		    dev->dev_addr);
4343 
4344 	platform_set_drvdata(pdev, pp->dev);
4345 
4346 	if (pp->use_inband_status) {
4347 		struct phy_device *phy = of_phy_find_device(dn);
4348 
4349 		mvneta_fixed_link_update(pp, phy);
4350 
4351 		put_device(&phy->mdio.dev);
4352 	}
4353 
4354 	return 0;
4355 
4356 err_netdev:
4357 	unregister_netdev(dev);
4358 	if (pp->bm_priv) {
4359 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4360 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4361 				       1 << pp->id);
4362 	}
4363 err_free_stats:
4364 	free_percpu(pp->stats);
4365 err_free_ports:
4366 	free_percpu(pp->ports);
4367 err_clk:
4368 	clk_disable_unprepare(pp->clk_bus);
4369 	clk_disable_unprepare(pp->clk);
4370 err_put_phy_node:
4371 	of_node_put(phy_node);
4372 	if (of_phy_is_fixed_link(dn))
4373 		of_phy_deregister_fixed_link(dn);
4374 err_free_irq:
4375 	irq_dispose_mapping(dev->irq);
4376 err_free_netdev:
4377 	free_netdev(dev);
4378 	return err;
4379 }
4380 
4381 /* Device removal routine */
4382 static int mvneta_remove(struct platform_device *pdev)
4383 {
4384 	struct net_device  *dev = platform_get_drvdata(pdev);
4385 	struct device_node *dn = pdev->dev.of_node;
4386 	struct mvneta_port *pp = netdev_priv(dev);
4387 
4388 	unregister_netdev(dev);
4389 	clk_disable_unprepare(pp->clk_bus);
4390 	clk_disable_unprepare(pp->clk);
4391 	free_percpu(pp->ports);
4392 	free_percpu(pp->stats);
4393 	if (of_phy_is_fixed_link(dn))
4394 		of_phy_deregister_fixed_link(dn);
4395 	irq_dispose_mapping(dev->irq);
4396 	of_node_put(pp->phy_node);
4397 	free_netdev(dev);
4398 
4399 	if (pp->bm_priv) {
4400 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
4401 		mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short,
4402 				       1 << pp->id);
4403 	}
4404 
4405 	return 0;
4406 }
4407 
4408 static const struct of_device_id mvneta_match[] = {
4409 	{ .compatible = "marvell,armada-370-neta" },
4410 	{ .compatible = "marvell,armada-xp-neta" },
4411 	{ .compatible = "marvell,armada-3700-neta" },
4412 	{ }
4413 };
4414 MODULE_DEVICE_TABLE(of, mvneta_match);
4415 
4416 static struct platform_driver mvneta_driver = {
4417 	.probe = mvneta_probe,
4418 	.remove = mvneta_remove,
4419 	.driver = {
4420 		.name = MVNETA_DRIVER_NAME,
4421 		.of_match_table = mvneta_match,
4422 	},
4423 };
4424 
4425 static int __init mvneta_driver_init(void)
4426 {
4427 	int ret;
4428 
4429 	ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
4430 				      mvneta_cpu_online,
4431 				      mvneta_cpu_down_prepare);
4432 	if (ret < 0)
4433 		goto out;
4434 	online_hpstate = ret;
4435 	ret = cpuhp_setup_state_multi(CPUHP_NET_MVNETA_DEAD, "net/mvneta:dead",
4436 				      NULL, mvneta_cpu_dead);
4437 	if (ret)
4438 		goto err_dead;
4439 
4440 	ret = platform_driver_register(&mvneta_driver);
4441 	if (ret)
4442 		goto err;
4443 	return 0;
4444 
4445 err:
4446 	cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4447 err_dead:
4448 	cpuhp_remove_multi_state(online_hpstate);
4449 out:
4450 	return ret;
4451 }
4452 module_init(mvneta_driver_init);
4453 
4454 static void __exit mvneta_driver_exit(void)
4455 {
4456 	platform_driver_unregister(&mvneta_driver);
4457 	cpuhp_remove_multi_state(CPUHP_NET_MVNETA_DEAD);
4458 	cpuhp_remove_multi_state(online_hpstate);
4459 }
4460 module_exit(mvneta_driver_exit);
4461 
4462 MODULE_DESCRIPTION("Marvell NETA Ethernet Driver - www.marvell.com");
4463 MODULE_AUTHOR("Rami Rosen <rosenr@marvell.com>, Thomas Petazzoni <thomas.petazzoni@free-electrons.com>");
4464 MODULE_LICENSE("GPL");
4465 
4466 module_param(rxq_number, int, S_IRUGO);
4467 module_param(txq_number, int, S_IRUGO);
4468 
4469 module_param(rxq_def, int, S_IRUGO);
4470 module_param(rx_copybreak, int, S_IRUGO | S_IWUSR);
4471