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