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