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