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