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