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