xref: /openbmc/linux/drivers/net/ethernet/marvell/mv643xx_eth.c (revision 2f5e65de0496c2e46c91afd93be2006cb9ace1d6)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2527a6266SJeff Kirsher /*
3527a6266SJeff Kirsher  * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
4527a6266SJeff Kirsher  * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
5527a6266SJeff Kirsher  *
6527a6266SJeff Kirsher  * Based on the 64360 driver from:
7527a6266SJeff Kirsher  * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
8527a6266SJeff Kirsher  *		      Rabeeh Khoury <rabeeh@marvell.com>
9527a6266SJeff Kirsher  *
10527a6266SJeff Kirsher  * Copyright (C) 2003 PMC-Sierra, Inc.,
11527a6266SJeff Kirsher  *	written by Manish Lachwani
12527a6266SJeff Kirsher  *
13527a6266SJeff Kirsher  * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
14527a6266SJeff Kirsher  *
15527a6266SJeff Kirsher  * Copyright (C) 2004-2006 MontaVista Software, Inc.
16527a6266SJeff Kirsher  *			   Dale Farnsworth <dale@farnsworth.org>
17527a6266SJeff Kirsher  *
18527a6266SJeff Kirsher  * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
19527a6266SJeff Kirsher  *				     <sjhill@realitydiluted.com>
20527a6266SJeff Kirsher  *
21527a6266SJeff Kirsher  * Copyright (C) 2007-2008 Marvell Semiconductor
22527a6266SJeff Kirsher  *			   Lennert Buytenhek <buytenh@marvell.com>
23527a6266SJeff Kirsher  *
243871c387SMichael Stapelberg  * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
25527a6266SJeff Kirsher  */
26527a6266SJeff Kirsher 
27527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28527a6266SJeff Kirsher 
29527a6266SJeff Kirsher #include <linux/init.h>
30527a6266SJeff Kirsher #include <linux/dma-mapping.h>
31527a6266SJeff Kirsher #include <linux/in.h>
32527a6266SJeff Kirsher #include <linux/ip.h>
333ae8f4e0SEzequiel Garcia #include <net/tso.h>
34527a6266SJeff Kirsher #include <linux/tcp.h>
35527a6266SJeff Kirsher #include <linux/udp.h>
36527a6266SJeff Kirsher #include <linux/etherdevice.h>
37527a6266SJeff Kirsher #include <linux/delay.h>
38527a6266SJeff Kirsher #include <linux/ethtool.h>
39527a6266SJeff Kirsher #include <linux/platform_device.h>
40527a6266SJeff Kirsher #include <linux/module.h>
41527a6266SJeff Kirsher #include <linux/kernel.h>
42527a6266SJeff Kirsher #include <linux/spinlock.h>
43527a6266SJeff Kirsher #include <linux/workqueue.h>
44527a6266SJeff Kirsher #include <linux/phy.h>
45527a6266SJeff Kirsher #include <linux/mv643xx_eth.h>
46527a6266SJeff Kirsher #include <linux/io.h>
473619eb85SSebastian Hesselbarth #include <linux/interrupt.h>
48527a6266SJeff Kirsher #include <linux/types.h>
49527a6266SJeff Kirsher #include <linux/slab.h>
50452503ebSAndrew Lunn #include <linux/clk.h>
5176723bcaSSebastian Hesselbarth #include <linux/of.h>
5276723bcaSSebastian Hesselbarth #include <linux/of_irq.h>
5376723bcaSSebastian Hesselbarth #include <linux/of_net.h>
54cc9d4598SSebastian Hesselbarth #include <linux/of_mdio.h>
55527a6266SJeff Kirsher 
56527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth";
57527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4";
58527a6266SJeff Kirsher 
59527a6266SJeff Kirsher 
60527a6266SJeff Kirsher /*
61527a6266SJeff Kirsher  * Registers shared between all ports.
62527a6266SJeff Kirsher  */
63527a6266SJeff Kirsher #define PHY_ADDR			0x0000
64527a6266SJeff Kirsher #define WINDOW_BASE(w)			(0x0200 + ((w) << 3))
65527a6266SJeff Kirsher #define WINDOW_SIZE(w)			(0x0204 + ((w) << 3))
66527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w)		(0x0280 + ((w) << 2))
67527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE		0x0290
68527a6266SJeff Kirsher #define WINDOW_PROTECT(w)		(0x0294 + ((w) << 4))
69527a6266SJeff Kirsher 
70527a6266SJeff Kirsher /*
71527a6266SJeff Kirsher  * Main per-port registers.  These live at offset 0x0400 for
72527a6266SJeff Kirsher  * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
73527a6266SJeff Kirsher  */
74527a6266SJeff Kirsher #define PORT_CONFIG			0x0000
75527a6266SJeff Kirsher #define  UNICAST_PROMISCUOUS_MODE	0x00000001
76527a6266SJeff Kirsher #define PORT_CONFIG_EXT			0x0004
77527a6266SJeff Kirsher #define MAC_ADDR_LOW			0x0014
78527a6266SJeff Kirsher #define MAC_ADDR_HIGH			0x0018
79527a6266SJeff Kirsher #define SDMA_CONFIG			0x001c
80527a6266SJeff Kirsher #define  TX_BURST_SIZE_16_64BIT		0x01000000
81527a6266SJeff Kirsher #define  TX_BURST_SIZE_4_64BIT		0x00800000
82527a6266SJeff Kirsher #define  BLM_TX_NO_SWAP			0x00000020
83527a6266SJeff Kirsher #define  BLM_RX_NO_SWAP			0x00000010
84527a6266SJeff Kirsher #define  RX_BURST_SIZE_16_64BIT		0x00000008
85527a6266SJeff Kirsher #define  RX_BURST_SIZE_4_64BIT		0x00000004
86527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL		0x003c
87527a6266SJeff Kirsher #define  SET_MII_SPEED_TO_100		0x01000000
88527a6266SJeff Kirsher #define  SET_GMII_SPEED_TO_1000		0x00800000
89527a6266SJeff Kirsher #define  SET_FULL_DUPLEX_MODE		0x00200000
90527a6266SJeff Kirsher #define  MAX_RX_PACKET_9700BYTE		0x000a0000
91527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_SPEED_GMII	0x00002000
92527a6266SJeff Kirsher #define  DO_NOT_FORCE_LINK_FAIL		0x00000400
93527a6266SJeff Kirsher #define  SERIAL_PORT_CONTROL_RESERVED	0x00000200
94527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_FLOW_CTRL	0x00000008
95527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_DUPLEX	0x00000004
96527a6266SJeff Kirsher #define  FORCE_LINK_PASS		0x00000002
97527a6266SJeff Kirsher #define  SERIAL_PORT_ENABLE		0x00000001
98527a6266SJeff Kirsher #define PORT_STATUS			0x0044
99527a6266SJeff Kirsher #define  TX_FIFO_EMPTY			0x00000400
100527a6266SJeff Kirsher #define  TX_IN_PROGRESS			0x00000080
101527a6266SJeff Kirsher #define  PORT_SPEED_MASK		0x00000030
102527a6266SJeff Kirsher #define  PORT_SPEED_1000		0x00000010
103527a6266SJeff Kirsher #define  PORT_SPEED_100			0x00000020
104527a6266SJeff Kirsher #define  PORT_SPEED_10			0x00000000
105527a6266SJeff Kirsher #define  FLOW_CONTROL_ENABLED		0x00000008
106527a6266SJeff Kirsher #define  FULL_DUPLEX			0x00000004
107527a6266SJeff Kirsher #define  LINK_UP			0x00000002
108527a6266SJeff Kirsher #define TXQ_COMMAND			0x0048
109527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF		0x004c
110cb85215fSSebastian Hesselbarth #define PORT_SERIAL_CONTROL1		0x004c
111cb85215fSSebastian Hesselbarth #define  CLK125_BYPASS_EN		0x00000010
112527a6266SJeff Kirsher #define TX_BW_RATE			0x0050
113527a6266SJeff Kirsher #define TX_BW_MTU			0x0058
114527a6266SJeff Kirsher #define TX_BW_BURST			0x005c
115527a6266SJeff Kirsher #define INT_CAUSE			0x0060
116527a6266SJeff Kirsher #define  INT_TX_END			0x07f80000
117527a6266SJeff Kirsher #define  INT_TX_END_0			0x00080000
118527a6266SJeff Kirsher #define  INT_RX				0x000003fc
119527a6266SJeff Kirsher #define  INT_RX_0			0x00000004
120527a6266SJeff Kirsher #define  INT_EXT			0x00000002
121527a6266SJeff Kirsher #define INT_CAUSE_EXT			0x0064
122527a6266SJeff Kirsher #define  INT_EXT_LINK_PHY		0x00110000
123527a6266SJeff Kirsher #define  INT_EXT_TX			0x000000ff
124527a6266SJeff Kirsher #define INT_MASK			0x0068
125527a6266SJeff Kirsher #define INT_MASK_EXT			0x006c
126527a6266SJeff Kirsher #define TX_FIFO_URGENT_THRESHOLD	0x0074
127302476c9SPaulius Zaleckas #define RX_DISCARD_FRAME_CNT		0x0084
128302476c9SPaulius Zaleckas #define RX_OVERRUN_FRAME_CNT		0x0088
129527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF_MOVED		0x00dc
130527a6266SJeff Kirsher #define TX_BW_RATE_MOVED		0x00e0
131527a6266SJeff Kirsher #define TX_BW_MTU_MOVED			0x00e8
132527a6266SJeff Kirsher #define TX_BW_BURST_MOVED		0x00ec
133527a6266SJeff Kirsher #define RXQ_CURRENT_DESC_PTR(q)		(0x020c + ((q) << 4))
134527a6266SJeff Kirsher #define RXQ_COMMAND			0x0280
135527a6266SJeff Kirsher #define TXQ_CURRENT_DESC_PTR(q)		(0x02c0 + ((q) << 2))
136527a6266SJeff Kirsher #define TXQ_BW_TOKENS(q)		(0x0300 + ((q) << 4))
137527a6266SJeff Kirsher #define TXQ_BW_CONF(q)			(0x0304 + ((q) << 4))
138527a6266SJeff Kirsher #define TXQ_BW_WRR_CONF(q)		(0x0308 + ((q) << 4))
139527a6266SJeff Kirsher 
140527a6266SJeff Kirsher /*
141527a6266SJeff Kirsher  * Misc per-port registers.
142527a6266SJeff Kirsher  */
143527a6266SJeff Kirsher #define MIB_COUNTERS(p)			(0x1000 + ((p) << 7))
144527a6266SJeff Kirsher #define SPECIAL_MCAST_TABLE(p)		(0x1400 + ((p) << 10))
145527a6266SJeff Kirsher #define OTHER_MCAST_TABLE(p)		(0x1500 + ((p) << 10))
146527a6266SJeff Kirsher #define UNICAST_TABLE(p)		(0x1600 + ((p) << 10))
147527a6266SJeff Kirsher 
148527a6266SJeff Kirsher 
149527a6266SJeff Kirsher /*
150527a6266SJeff Kirsher  * SDMA configuration register default value.
151527a6266SJeff Kirsher  */
152527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
153527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
154527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
155527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
156527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
157527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
158527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
159527a6266SJeff Kirsher 		 BLM_RX_NO_SWAP		|	\
160527a6266SJeff Kirsher 		 BLM_TX_NO_SWAP		|	\
161527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
162527a6266SJeff Kirsher #else
163527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
164527a6266SJeff Kirsher #endif
165527a6266SJeff Kirsher 
166527a6266SJeff Kirsher 
167527a6266SJeff Kirsher /*
168527a6266SJeff Kirsher  * Misc definitions.
169527a6266SJeff Kirsher  */
170527a6266SJeff Kirsher #define DEFAULT_RX_QUEUE_SIZE	128
1713ae8f4e0SEzequiel Garcia #define DEFAULT_TX_QUEUE_SIZE	512
172527a6266SJeff Kirsher #define SKB_DMA_REALIGN		((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
173527a6266SJeff Kirsher 
174ee9e4956SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */
175ee9e4956SEzequiel Garcia #define MV643XX_MAX_TSO_SEGS 100
176ee9e4956SEzequiel Garcia #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
177ee9e4956SEzequiel Garcia 
178b926260cSEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \
179b926260cSEzequiel Garcia 	((addr >= txq->tso_hdrs_dma) && \
180b926260cSEzequiel Garcia 	 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
1819e911414SEzequiel Garcia 
1829e911414SEzequiel Garcia #define DESC_DMA_MAP_SINGLE 0
1839e911414SEzequiel Garcia #define DESC_DMA_MAP_PAGE 1
1849e911414SEzequiel Garcia 
185527a6266SJeff Kirsher /*
186527a6266SJeff Kirsher  * RX/TX descriptors.
187527a6266SJeff Kirsher  */
188527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
189527a6266SJeff Kirsher struct rx_desc {
190527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
191527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
192527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
193527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
194527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
195527a6266SJeff Kirsher };
196527a6266SJeff Kirsher 
197527a6266SJeff Kirsher struct tx_desc {
198527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
199527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
200527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
201527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
202527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
203527a6266SJeff Kirsher };
204527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
205527a6266SJeff Kirsher struct rx_desc {
206527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
207527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
208527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
209527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
210527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
211527a6266SJeff Kirsher };
212527a6266SJeff Kirsher 
213527a6266SJeff Kirsher struct tx_desc {
214527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
215527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
216527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
217527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
218527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
219527a6266SJeff Kirsher };
220527a6266SJeff Kirsher #else
221527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
222527a6266SJeff Kirsher #endif
223527a6266SJeff Kirsher 
224527a6266SJeff Kirsher /* RX & TX descriptor command */
225527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA		0x80000000
226527a6266SJeff Kirsher 
227527a6266SJeff Kirsher /* RX & TX descriptor status */
228527a6266SJeff Kirsher #define ERROR_SUMMARY			0x00000001
229527a6266SJeff Kirsher 
230527a6266SJeff Kirsher /* RX descriptor status */
231527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK		0x40000000
232527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT		0x20000000
233527a6266SJeff Kirsher #define RX_FIRST_DESC			0x08000000
234527a6266SJeff Kirsher #define RX_LAST_DESC			0x04000000
235527a6266SJeff Kirsher #define RX_IP_HDR_OK			0x02000000
236527a6266SJeff Kirsher #define RX_PKT_IS_IPV4			0x01000000
237527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2		0x00800000
238527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK		0x00600000
239527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4	0x00000000
240527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED		0x00080000
241527a6266SJeff Kirsher 
242527a6266SJeff Kirsher /* TX descriptor command */
243527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT		0x00800000
244527a6266SJeff Kirsher #define GEN_CRC				0x00400000
245527a6266SJeff Kirsher #define TX_FIRST_DESC			0x00200000
246527a6266SJeff Kirsher #define TX_LAST_DESC			0x00100000
247527a6266SJeff Kirsher #define ZERO_PADDING			0x00080000
248527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM		0x00040000
249527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM		0x00020000
250527a6266SJeff Kirsher #define UDP_FRAME			0x00010000
251527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES		0x00008000
25284411f73SEzequiel Garcia #define GEN_TCP_UDP_CHK_FULL		0x00000400
253527a6266SJeff Kirsher #define MAC_HDR_EXTRA_8_BYTES		0x00000200
254527a6266SJeff Kirsher 
255527a6266SJeff Kirsher #define TX_IHL_SHIFT			11
256527a6266SJeff Kirsher 
257527a6266SJeff Kirsher 
258527a6266SJeff Kirsher /* global *******************************************************************/
259527a6266SJeff Kirsher struct mv643xx_eth_shared_private {
260527a6266SJeff Kirsher 	/*
261527a6266SJeff Kirsher 	 * Ethernet controller base address.
262527a6266SJeff Kirsher 	 */
263527a6266SJeff Kirsher 	void __iomem *base;
264527a6266SJeff Kirsher 
265527a6266SJeff Kirsher 	/*
266527a6266SJeff Kirsher 	 * Per-port MBUS window access register value.
267527a6266SJeff Kirsher 	 */
268527a6266SJeff Kirsher 	u32 win_protect;
269527a6266SJeff Kirsher 
270527a6266SJeff Kirsher 	/*
271527a6266SJeff Kirsher 	 * Hardware-specific parameters.
272527a6266SJeff Kirsher 	 */
273527a6266SJeff Kirsher 	int extended_rx_coal_limit;
274527a6266SJeff Kirsher 	int tx_bw_control;
275527a6266SJeff Kirsher 	int tx_csum_limit;
27620922486SSebastian Hesselbarth 	struct clk *clk;
277527a6266SJeff Kirsher };
278527a6266SJeff Kirsher 
279527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT		0
280527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT	1
281527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT	2
282527a6266SJeff Kirsher 
283527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev);
284527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev);
285527a6266SJeff Kirsher 
286527a6266SJeff Kirsher 
287527a6266SJeff Kirsher /* per-port *****************************************************************/
288527a6266SJeff Kirsher struct mib_counters {
289527a6266SJeff Kirsher 	u64 good_octets_received;
290527a6266SJeff Kirsher 	u32 bad_octets_received;
291527a6266SJeff Kirsher 	u32 internal_mac_transmit_err;
292527a6266SJeff Kirsher 	u32 good_frames_received;
293527a6266SJeff Kirsher 	u32 bad_frames_received;
294527a6266SJeff Kirsher 	u32 broadcast_frames_received;
295527a6266SJeff Kirsher 	u32 multicast_frames_received;
296527a6266SJeff Kirsher 	u32 frames_64_octets;
297527a6266SJeff Kirsher 	u32 frames_65_to_127_octets;
298527a6266SJeff Kirsher 	u32 frames_128_to_255_octets;
299527a6266SJeff Kirsher 	u32 frames_256_to_511_octets;
300527a6266SJeff Kirsher 	u32 frames_512_to_1023_octets;
301527a6266SJeff Kirsher 	u32 frames_1024_to_max_octets;
302527a6266SJeff Kirsher 	u64 good_octets_sent;
303527a6266SJeff Kirsher 	u32 good_frames_sent;
304527a6266SJeff Kirsher 	u32 excessive_collision;
305527a6266SJeff Kirsher 	u32 multicast_frames_sent;
306527a6266SJeff Kirsher 	u32 broadcast_frames_sent;
307527a6266SJeff Kirsher 	u32 unrec_mac_control_received;
308527a6266SJeff Kirsher 	u32 fc_sent;
309527a6266SJeff Kirsher 	u32 good_fc_received;
310527a6266SJeff Kirsher 	u32 bad_fc_received;
311527a6266SJeff Kirsher 	u32 undersize_received;
312527a6266SJeff Kirsher 	u32 fragments_received;
313527a6266SJeff Kirsher 	u32 oversize_received;
314527a6266SJeff Kirsher 	u32 jabber_received;
315527a6266SJeff Kirsher 	u32 mac_receive_error;
316527a6266SJeff Kirsher 	u32 bad_crc_event;
317527a6266SJeff Kirsher 	u32 collision;
318527a6266SJeff Kirsher 	u32 late_collision;
319302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
320302476c9SPaulius Zaleckas 	u32 rx_discard;
321302476c9SPaulius Zaleckas 	u32 rx_overrun;
322527a6266SJeff Kirsher };
323527a6266SJeff Kirsher 
324527a6266SJeff Kirsher struct rx_queue {
325527a6266SJeff Kirsher 	int index;
326527a6266SJeff Kirsher 
327527a6266SJeff Kirsher 	int rx_ring_size;
328527a6266SJeff Kirsher 
329527a6266SJeff Kirsher 	int rx_desc_count;
330527a6266SJeff Kirsher 	int rx_curr_desc;
331527a6266SJeff Kirsher 	int rx_used_desc;
332527a6266SJeff Kirsher 
333527a6266SJeff Kirsher 	struct rx_desc *rx_desc_area;
334527a6266SJeff Kirsher 	dma_addr_t rx_desc_dma;
335527a6266SJeff Kirsher 	int rx_desc_area_size;
336527a6266SJeff Kirsher 	struct sk_buff **rx_skb;
337527a6266SJeff Kirsher };
338527a6266SJeff Kirsher 
339527a6266SJeff Kirsher struct tx_queue {
340527a6266SJeff Kirsher 	int index;
341527a6266SJeff Kirsher 
342527a6266SJeff Kirsher 	int tx_ring_size;
343527a6266SJeff Kirsher 
344527a6266SJeff Kirsher 	int tx_desc_count;
345527a6266SJeff Kirsher 	int tx_curr_desc;
346527a6266SJeff Kirsher 	int tx_used_desc;
347527a6266SJeff Kirsher 
348ee9e4956SEzequiel Garcia 	int tx_stop_threshold;
349ee9e4956SEzequiel Garcia 	int tx_wake_threshold;
350ee9e4956SEzequiel Garcia 
3513ae8f4e0SEzequiel Garcia 	char *tso_hdrs;
3523ae8f4e0SEzequiel Garcia 	dma_addr_t tso_hdrs_dma;
3533ae8f4e0SEzequiel Garcia 
354527a6266SJeff Kirsher 	struct tx_desc *tx_desc_area;
3559e911414SEzequiel Garcia 	char *tx_desc_mapping; /* array to track the type of the dma mapping */
356527a6266SJeff Kirsher 	dma_addr_t tx_desc_dma;
357527a6266SJeff Kirsher 	int tx_desc_area_size;
358527a6266SJeff Kirsher 
359527a6266SJeff Kirsher 	struct sk_buff_head tx_skb;
360527a6266SJeff Kirsher 
361527a6266SJeff Kirsher 	unsigned long tx_packets;
362527a6266SJeff Kirsher 	unsigned long tx_bytes;
363527a6266SJeff Kirsher 	unsigned long tx_dropped;
364527a6266SJeff Kirsher };
365527a6266SJeff Kirsher 
366527a6266SJeff Kirsher struct mv643xx_eth_private {
367527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *shared;
368527a6266SJeff Kirsher 	void __iomem *base;
369527a6266SJeff Kirsher 	int port_num;
370527a6266SJeff Kirsher 
371527a6266SJeff Kirsher 	struct net_device *dev;
372527a6266SJeff Kirsher 
373527a6266SJeff Kirsher 	struct timer_list mib_counters_timer;
374527a6266SJeff Kirsher 	spinlock_t mib_counters_lock;
375527a6266SJeff Kirsher 	struct mib_counters mib_counters;
376527a6266SJeff Kirsher 
377527a6266SJeff Kirsher 	struct work_struct tx_timeout_task;
378527a6266SJeff Kirsher 
379527a6266SJeff Kirsher 	struct napi_struct napi;
380527a6266SJeff Kirsher 	u32 int_mask;
381527a6266SJeff Kirsher 	u8 oom;
382527a6266SJeff Kirsher 	u8 work_link;
383527a6266SJeff Kirsher 	u8 work_tx;
384527a6266SJeff Kirsher 	u8 work_tx_end;
385527a6266SJeff Kirsher 	u8 work_rx;
386527a6266SJeff Kirsher 	u8 work_rx_refill;
387527a6266SJeff Kirsher 
388527a6266SJeff Kirsher 	int skb_size;
389527a6266SJeff Kirsher 
390527a6266SJeff Kirsher 	/*
391527a6266SJeff Kirsher 	 * RX state.
392527a6266SJeff Kirsher 	 */
393527a6266SJeff Kirsher 	int rx_ring_size;
394527a6266SJeff Kirsher 	unsigned long rx_desc_sram_addr;
395527a6266SJeff Kirsher 	int rx_desc_sram_size;
396527a6266SJeff Kirsher 	int rxq_count;
397527a6266SJeff Kirsher 	struct timer_list rx_oom;
398527a6266SJeff Kirsher 	struct rx_queue rxq[8];
399527a6266SJeff Kirsher 
400527a6266SJeff Kirsher 	/*
401527a6266SJeff Kirsher 	 * TX state.
402527a6266SJeff Kirsher 	 */
403527a6266SJeff Kirsher 	int tx_ring_size;
404527a6266SJeff Kirsher 	unsigned long tx_desc_sram_addr;
405527a6266SJeff Kirsher 	int tx_desc_sram_size;
406527a6266SJeff Kirsher 	int txq_count;
407527a6266SJeff Kirsher 	struct tx_queue txq[8];
408452503ebSAndrew Lunn 
409452503ebSAndrew Lunn 	/*
410452503ebSAndrew Lunn 	 * Hardware-specific parameters.
411452503ebSAndrew Lunn 	 */
412452503ebSAndrew Lunn 	struct clk *clk;
413452503ebSAndrew Lunn 	unsigned int t_clk;
414527a6266SJeff Kirsher };
415527a6266SJeff Kirsher 
416527a6266SJeff Kirsher 
417527a6266SJeff Kirsher /* port register accessors **************************************************/
418527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
419527a6266SJeff Kirsher {
420527a6266SJeff Kirsher 	return readl(mp->shared->base + offset);
421527a6266SJeff Kirsher }
422527a6266SJeff Kirsher 
423527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
424527a6266SJeff Kirsher {
425527a6266SJeff Kirsher 	return readl(mp->base + offset);
426527a6266SJeff Kirsher }
427527a6266SJeff Kirsher 
428527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
429527a6266SJeff Kirsher {
430527a6266SJeff Kirsher 	writel(data, mp->shared->base + offset);
431527a6266SJeff Kirsher }
432527a6266SJeff Kirsher 
433527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
434527a6266SJeff Kirsher {
435527a6266SJeff Kirsher 	writel(data, mp->base + offset);
436527a6266SJeff Kirsher }
437527a6266SJeff Kirsher 
438527a6266SJeff Kirsher 
439527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/
440527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
441527a6266SJeff Kirsher {
442527a6266SJeff Kirsher 	return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
443527a6266SJeff Kirsher }
444527a6266SJeff Kirsher 
445527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
446527a6266SJeff Kirsher {
447527a6266SJeff Kirsher 	return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
448527a6266SJeff Kirsher }
449527a6266SJeff Kirsher 
450527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq)
451527a6266SJeff Kirsher {
452527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
453527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
454527a6266SJeff Kirsher }
455527a6266SJeff Kirsher 
456527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq)
457527a6266SJeff Kirsher {
458527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
459527a6266SJeff Kirsher 	u8 mask = 1 << rxq->index;
460527a6266SJeff Kirsher 
461527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, mask << 8);
462527a6266SJeff Kirsher 	while (rdlp(mp, RXQ_COMMAND) & mask)
463527a6266SJeff Kirsher 		udelay(10);
464527a6266SJeff Kirsher }
465527a6266SJeff Kirsher 
466527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq)
467527a6266SJeff Kirsher {
468527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
469527a6266SJeff Kirsher 	u32 addr;
470527a6266SJeff Kirsher 
471527a6266SJeff Kirsher 	addr = (u32)txq->tx_desc_dma;
472527a6266SJeff Kirsher 	addr += txq->tx_curr_desc * sizeof(struct tx_desc);
473527a6266SJeff Kirsher 	wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
474527a6266SJeff Kirsher }
475527a6266SJeff Kirsher 
476527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq)
477527a6266SJeff Kirsher {
478527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
479527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, 1 << txq->index);
480527a6266SJeff Kirsher }
481527a6266SJeff Kirsher 
482527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq)
483527a6266SJeff Kirsher {
484527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
485527a6266SJeff Kirsher 	u8 mask = 1 << txq->index;
486527a6266SJeff Kirsher 
487527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, mask << 8);
488527a6266SJeff Kirsher 	while (rdlp(mp, TXQ_COMMAND) & mask)
489527a6266SJeff Kirsher 		udelay(10);
490527a6266SJeff Kirsher }
491527a6266SJeff Kirsher 
492527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq)
493527a6266SJeff Kirsher {
494527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
495527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
496527a6266SJeff Kirsher 
497527a6266SJeff Kirsher 	if (netif_tx_queue_stopped(nq)) {
498527a6266SJeff Kirsher 		__netif_tx_lock(nq, smp_processor_id());
499ee9e4956SEzequiel Garcia 		if (txq->tx_desc_count <= txq->tx_wake_threshold)
500527a6266SJeff Kirsher 			netif_tx_wake_queue(nq);
501527a6266SJeff Kirsher 		__netif_tx_unlock(nq);
502527a6266SJeff Kirsher 	}
503527a6266SJeff Kirsher }
504527a6266SJeff Kirsher 
505527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget)
506527a6266SJeff Kirsher {
507527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
508527a6266SJeff Kirsher 	struct net_device_stats *stats = &mp->dev->stats;
509527a6266SJeff Kirsher 	int rx;
510527a6266SJeff Kirsher 
511527a6266SJeff Kirsher 	rx = 0;
512527a6266SJeff Kirsher 	while (rx < budget && rxq->rx_desc_count) {
513527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
514527a6266SJeff Kirsher 		unsigned int cmd_sts;
515527a6266SJeff Kirsher 		struct sk_buff *skb;
516527a6266SJeff Kirsher 		u16 byte_cnt;
517527a6266SJeff Kirsher 
518527a6266SJeff Kirsher 		rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
519527a6266SJeff Kirsher 
520527a6266SJeff Kirsher 		cmd_sts = rx_desc->cmd_sts;
521527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA)
522527a6266SJeff Kirsher 			break;
523527a6266SJeff Kirsher 		rmb();
524527a6266SJeff Kirsher 
525527a6266SJeff Kirsher 		skb = rxq->rx_skb[rxq->rx_curr_desc];
526527a6266SJeff Kirsher 		rxq->rx_skb[rxq->rx_curr_desc] = NULL;
527527a6266SJeff Kirsher 
528527a6266SJeff Kirsher 		rxq->rx_curr_desc++;
529527a6266SJeff Kirsher 		if (rxq->rx_curr_desc == rxq->rx_ring_size)
530527a6266SJeff Kirsher 			rxq->rx_curr_desc = 0;
531527a6266SJeff Kirsher 
532527a6266SJeff Kirsher 		dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
533527a6266SJeff Kirsher 				 rx_desc->buf_size, DMA_FROM_DEVICE);
534527a6266SJeff Kirsher 		rxq->rx_desc_count--;
535527a6266SJeff Kirsher 		rx++;
536527a6266SJeff Kirsher 
537527a6266SJeff Kirsher 		mp->work_rx_refill |= 1 << rxq->index;
538527a6266SJeff Kirsher 
539527a6266SJeff Kirsher 		byte_cnt = rx_desc->byte_cnt;
540527a6266SJeff Kirsher 
541527a6266SJeff Kirsher 		/*
542527a6266SJeff Kirsher 		 * Update statistics.
543527a6266SJeff Kirsher 		 *
544527a6266SJeff Kirsher 		 * Note that the descriptor byte count includes 2 dummy
545527a6266SJeff Kirsher 		 * bytes automatically inserted by the hardware at the
546527a6266SJeff Kirsher 		 * start of the packet (which we don't count), and a 4
547527a6266SJeff Kirsher 		 * byte CRC at the end of the packet (which we do count).
548527a6266SJeff Kirsher 		 */
549527a6266SJeff Kirsher 		stats->rx_packets++;
550527a6266SJeff Kirsher 		stats->rx_bytes += byte_cnt - 2;
551527a6266SJeff Kirsher 
552527a6266SJeff Kirsher 		/*
553527a6266SJeff Kirsher 		 * In case we received a packet without first / last bits
554527a6266SJeff Kirsher 		 * on, or the error summary bit is set, the packet needs
555527a6266SJeff Kirsher 		 * to be dropped.
556527a6266SJeff Kirsher 		 */
557527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
558527a6266SJeff Kirsher 			!= (RX_FIRST_DESC | RX_LAST_DESC))
559527a6266SJeff Kirsher 			goto err;
560527a6266SJeff Kirsher 
561527a6266SJeff Kirsher 		/*
562527a6266SJeff Kirsher 		 * The -4 is for the CRC in the trailer of the
563527a6266SJeff Kirsher 		 * received packet
564527a6266SJeff Kirsher 		 */
565527a6266SJeff Kirsher 		skb_put(skb, byte_cnt - 2 - 4);
566527a6266SJeff Kirsher 
567527a6266SJeff Kirsher 		if (cmd_sts & LAYER_4_CHECKSUM_OK)
568527a6266SJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
569527a6266SJeff Kirsher 		skb->protocol = eth_type_trans(skb, mp->dev);
570527a6266SJeff Kirsher 
57109bf1c10SSebastian Hesselbarth 		napi_gro_receive(&mp->napi, skb);
572527a6266SJeff Kirsher 
573527a6266SJeff Kirsher 		continue;
574527a6266SJeff Kirsher 
575527a6266SJeff Kirsher err:
576527a6266SJeff Kirsher 		stats->rx_dropped++;
577527a6266SJeff Kirsher 
578527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
579527a6266SJeff Kirsher 			(RX_FIRST_DESC | RX_LAST_DESC)) {
580527a6266SJeff Kirsher 			if (net_ratelimit())
581527a6266SJeff Kirsher 				netdev_err(mp->dev,
582527a6266SJeff Kirsher 					   "received packet spanning multiple descriptors\n");
583527a6266SJeff Kirsher 		}
584527a6266SJeff Kirsher 
585527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY)
586527a6266SJeff Kirsher 			stats->rx_errors++;
587527a6266SJeff Kirsher 
588527a6266SJeff Kirsher 		dev_kfree_skb(skb);
589527a6266SJeff Kirsher 	}
590527a6266SJeff Kirsher 
591527a6266SJeff Kirsher 	if (rx < budget)
592527a6266SJeff Kirsher 		mp->work_rx &= ~(1 << rxq->index);
593527a6266SJeff Kirsher 
594527a6266SJeff Kirsher 	return rx;
595527a6266SJeff Kirsher }
596527a6266SJeff Kirsher 
597527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget)
598527a6266SJeff Kirsher {
599527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
600527a6266SJeff Kirsher 	int refilled;
601527a6266SJeff Kirsher 
602527a6266SJeff Kirsher 	refilled = 0;
603527a6266SJeff Kirsher 	while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
604527a6266SJeff Kirsher 		struct sk_buff *skb;
605527a6266SJeff Kirsher 		int rx;
606527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
607527a6266SJeff Kirsher 		int size;
608527a6266SJeff Kirsher 
609c056b734SPradeep A Dalvi 		skb = netdev_alloc_skb(mp->dev, mp->skb_size);
610527a6266SJeff Kirsher 
611527a6266SJeff Kirsher 		if (skb == NULL) {
612527a6266SJeff Kirsher 			mp->oom = 1;
613527a6266SJeff Kirsher 			goto oom;
614527a6266SJeff Kirsher 		}
615527a6266SJeff Kirsher 
616527a6266SJeff Kirsher 		if (SKB_DMA_REALIGN)
617527a6266SJeff Kirsher 			skb_reserve(skb, SKB_DMA_REALIGN);
618527a6266SJeff Kirsher 
619527a6266SJeff Kirsher 		refilled++;
620527a6266SJeff Kirsher 		rxq->rx_desc_count++;
621527a6266SJeff Kirsher 
622527a6266SJeff Kirsher 		rx = rxq->rx_used_desc++;
623527a6266SJeff Kirsher 		if (rxq->rx_used_desc == rxq->rx_ring_size)
624527a6266SJeff Kirsher 			rxq->rx_used_desc = 0;
625527a6266SJeff Kirsher 
626527a6266SJeff Kirsher 		rx_desc = rxq->rx_desc_area + rx;
627527a6266SJeff Kirsher 
62818f1d054SIsaku Yamahata 		size = skb_end_pointer(skb) - skb->data;
629527a6266SJeff Kirsher 		rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
630527a6266SJeff Kirsher 						  skb->data, size,
631527a6266SJeff Kirsher 						  DMA_FROM_DEVICE);
632527a6266SJeff Kirsher 		rx_desc->buf_size = size;
633527a6266SJeff Kirsher 		rxq->rx_skb[rx] = skb;
634527a6266SJeff Kirsher 		wmb();
635527a6266SJeff Kirsher 		rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
636527a6266SJeff Kirsher 		wmb();
637527a6266SJeff Kirsher 
638527a6266SJeff Kirsher 		/*
639527a6266SJeff Kirsher 		 * The hardware automatically prepends 2 bytes of
640527a6266SJeff Kirsher 		 * dummy data to each received packet, so that the
641527a6266SJeff Kirsher 		 * IP header ends up 16-byte aligned.
642527a6266SJeff Kirsher 		 */
643527a6266SJeff Kirsher 		skb_reserve(skb, 2);
644527a6266SJeff Kirsher 	}
645527a6266SJeff Kirsher 
646527a6266SJeff Kirsher 	if (refilled < budget)
647527a6266SJeff Kirsher 		mp->work_rx_refill &= ~(1 << rxq->index);
648527a6266SJeff Kirsher 
649527a6266SJeff Kirsher oom:
650527a6266SJeff Kirsher 	return refilled;
651527a6266SJeff Kirsher }
652527a6266SJeff Kirsher 
653527a6266SJeff Kirsher 
654527a6266SJeff Kirsher /* tx ***********************************************************************/
655527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
656527a6266SJeff Kirsher {
657527a6266SJeff Kirsher 	int frag;
658527a6266SJeff Kirsher 
659527a6266SJeff Kirsher 	for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
6609e903e08SEric Dumazet 		const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
6619e903e08SEric Dumazet 
662b54c9d5bSJonathan Lemon 		if (skb_frag_size(fragp) <= 8 && skb_frag_off(fragp) & 7)
663527a6266SJeff Kirsher 			return 1;
664527a6266SJeff Kirsher 	}
665527a6266SJeff Kirsher 
666527a6266SJeff Kirsher 	return 0;
667527a6266SJeff Kirsher }
668527a6266SJeff Kirsher 
6690a8fa933SEzequiel Garcia static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb,
6700a8fa933SEzequiel Garcia 		       u16 *l4i_chk, u32 *command, int length)
6710a8fa933SEzequiel Garcia {
6720a8fa933SEzequiel Garcia 	int ret;
6730a8fa933SEzequiel Garcia 	u32 cmd = 0;
6740a8fa933SEzequiel Garcia 
6750a8fa933SEzequiel Garcia 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
6760a8fa933SEzequiel Garcia 		int hdr_len;
6770a8fa933SEzequiel Garcia 		int tag_bytes;
6780a8fa933SEzequiel Garcia 
6790a8fa933SEzequiel Garcia 		BUG_ON(skb->protocol != htons(ETH_P_IP) &&
6800a8fa933SEzequiel Garcia 		       skb->protocol != htons(ETH_P_8021Q));
6810a8fa933SEzequiel Garcia 
6820a8fa933SEzequiel Garcia 		hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
6830a8fa933SEzequiel Garcia 		tag_bytes = hdr_len - ETH_HLEN;
6840a8fa933SEzequiel Garcia 
6850a8fa933SEzequiel Garcia 		if (length - hdr_len > mp->shared->tx_csum_limit ||
6860a8fa933SEzequiel Garcia 		    unlikely(tag_bytes & ~12)) {
6870a8fa933SEzequiel Garcia 			ret = skb_checksum_help(skb);
6880a8fa933SEzequiel Garcia 			if (!ret)
6890a8fa933SEzequiel Garcia 				goto no_csum;
6900a8fa933SEzequiel Garcia 			return ret;
6910a8fa933SEzequiel Garcia 		}
6920a8fa933SEzequiel Garcia 
6930a8fa933SEzequiel Garcia 		if (tag_bytes & 4)
6940a8fa933SEzequiel Garcia 			cmd |= MAC_HDR_EXTRA_4_BYTES;
6950a8fa933SEzequiel Garcia 		if (tag_bytes & 8)
6960a8fa933SEzequiel Garcia 			cmd |= MAC_HDR_EXTRA_8_BYTES;
6970a8fa933SEzequiel Garcia 
69884411f73SEzequiel Garcia 		cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL |
6990a8fa933SEzequiel Garcia 			   GEN_IP_V4_CHECKSUM   |
7000a8fa933SEzequiel Garcia 			   ip_hdr(skb)->ihl << TX_IHL_SHIFT;
7010a8fa933SEzequiel Garcia 
70284411f73SEzequiel Garcia 		/* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL
703df4a17a9SYangyang Li 		 * it seems we don't need to pass the initial checksum.
704df4a17a9SYangyang Li 		 */
7050a8fa933SEzequiel Garcia 		switch (ip_hdr(skb)->protocol) {
7060a8fa933SEzequiel Garcia 		case IPPROTO_UDP:
7070a8fa933SEzequiel Garcia 			cmd |= UDP_FRAME;
70884411f73SEzequiel Garcia 			*l4i_chk = 0;
7090a8fa933SEzequiel Garcia 			break;
7100a8fa933SEzequiel Garcia 		case IPPROTO_TCP:
71184411f73SEzequiel Garcia 			*l4i_chk = 0;
7120a8fa933SEzequiel Garcia 			break;
7130a8fa933SEzequiel Garcia 		default:
7140a8fa933SEzequiel Garcia 			WARN(1, "protocol not supported");
7150a8fa933SEzequiel Garcia 		}
7160a8fa933SEzequiel Garcia 	} else {
7170a8fa933SEzequiel Garcia no_csum:
7180a8fa933SEzequiel Garcia 		/* Errata BTS #50, IHL must be 5 if no HW checksum */
7190a8fa933SEzequiel Garcia 		cmd |= 5 << TX_IHL_SHIFT;
7200a8fa933SEzequiel Garcia 	}
7210a8fa933SEzequiel Garcia 	*command = cmd;
7220a8fa933SEzequiel Garcia 	return 0;
7230a8fa933SEzequiel Garcia }
7240a8fa933SEzequiel Garcia 
7253ae8f4e0SEzequiel Garcia static inline int
7263ae8f4e0SEzequiel Garcia txq_put_data_tso(struct net_device *dev, struct tx_queue *txq,
7273ae8f4e0SEzequiel Garcia 		 struct sk_buff *skb, char *data, int length,
7283ae8f4e0SEzequiel Garcia 		 bool last_tcp, bool is_last)
7293ae8f4e0SEzequiel Garcia {
7303ae8f4e0SEzequiel Garcia 	int tx_index;
7313ae8f4e0SEzequiel Garcia 	u32 cmd_sts;
7323ae8f4e0SEzequiel Garcia 	struct tx_desc *desc;
7333ae8f4e0SEzequiel Garcia 
7343ae8f4e0SEzequiel Garcia 	tx_index = txq->tx_curr_desc++;
7353ae8f4e0SEzequiel Garcia 	if (txq->tx_curr_desc == txq->tx_ring_size)
7363ae8f4e0SEzequiel Garcia 		txq->tx_curr_desc = 0;
7373ae8f4e0SEzequiel Garcia 	desc = &txq->tx_desc_area[tx_index];
7389e911414SEzequiel Garcia 	txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
7393ae8f4e0SEzequiel Garcia 
7403ae8f4e0SEzequiel Garcia 	desc->l4i_chk = 0;
7413ae8f4e0SEzequiel Garcia 	desc->byte_cnt = length;
74291986fd3SPhilipp Kirchhofer 
74391986fd3SPhilipp Kirchhofer 	if (length <= 8 && (uintptr_t)data & 0x7) {
74491986fd3SPhilipp Kirchhofer 		/* Copy unaligned small data fragment to TSO header data area */
7453b89624aSNicolas Schichan 		memcpy(txq->tso_hdrs + tx_index * TSO_HEADER_SIZE,
74691986fd3SPhilipp Kirchhofer 		       data, length);
74791986fd3SPhilipp Kirchhofer 		desc->buf_ptr = txq->tso_hdrs_dma
7483b89624aSNicolas Schichan 			+ tx_index * TSO_HEADER_SIZE;
74991986fd3SPhilipp Kirchhofer 	} else {
75091986fd3SPhilipp Kirchhofer 		/* Alignment is okay, map buffer and hand off to hardware */
75191986fd3SPhilipp Kirchhofer 		txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
7523ae8f4e0SEzequiel Garcia 		desc->buf_ptr = dma_map_single(dev->dev.parent, data,
7533ae8f4e0SEzequiel Garcia 			length, DMA_TO_DEVICE);
75491986fd3SPhilipp Kirchhofer 		if (unlikely(dma_mapping_error(dev->dev.parent,
75591986fd3SPhilipp Kirchhofer 					       desc->buf_ptr))) {
7563ae8f4e0SEzequiel Garcia 			WARN(1, "dma_map_single failed!\n");
7573ae8f4e0SEzequiel Garcia 			return -ENOMEM;
7583ae8f4e0SEzequiel Garcia 		}
75991986fd3SPhilipp Kirchhofer 	}
7603ae8f4e0SEzequiel Garcia 
7613ae8f4e0SEzequiel Garcia 	cmd_sts = BUFFER_OWNED_BY_DMA;
7623ae8f4e0SEzequiel Garcia 	if (last_tcp) {
7633ae8f4e0SEzequiel Garcia 		/* last descriptor in the TCP packet */
7643ae8f4e0SEzequiel Garcia 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC;
7653ae8f4e0SEzequiel Garcia 		/* last descriptor in SKB */
7663ae8f4e0SEzequiel Garcia 		if (is_last)
7673ae8f4e0SEzequiel Garcia 			cmd_sts |= TX_ENABLE_INTERRUPT;
7683ae8f4e0SEzequiel Garcia 	}
7693ae8f4e0SEzequiel Garcia 	desc->cmd_sts = cmd_sts;
7703ae8f4e0SEzequiel Garcia 	return 0;
7713ae8f4e0SEzequiel Garcia }
7723ae8f4e0SEzequiel Garcia 
7733ae8f4e0SEzequiel Garcia static inline void
774968200f3SPhilipp Kirchhofer txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length,
775968200f3SPhilipp Kirchhofer 		u32 *first_cmd_sts, bool first_desc)
7763ae8f4e0SEzequiel Garcia {
7773ae8f4e0SEzequiel Garcia 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
7783ae8f4e0SEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
7793ae8f4e0SEzequiel Garcia 	int tx_index;
7803ae8f4e0SEzequiel Garcia 	struct tx_desc *desc;
7813ae8f4e0SEzequiel Garcia 	int ret;
7823ae8f4e0SEzequiel Garcia 	u32 cmd_csum = 0;
7833ae8f4e0SEzequiel Garcia 	u16 l4i_chk = 0;
784968200f3SPhilipp Kirchhofer 	u32 cmd_sts;
7853ae8f4e0SEzequiel Garcia 
7863ae8f4e0SEzequiel Garcia 	tx_index = txq->tx_curr_desc;
7873ae8f4e0SEzequiel Garcia 	desc = &txq->tx_desc_area[tx_index];
7883ae8f4e0SEzequiel Garcia 
7893ae8f4e0SEzequiel Garcia 	ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_csum, length);
7903ae8f4e0SEzequiel Garcia 	if (ret)
7913ae8f4e0SEzequiel Garcia 		WARN(1, "failed to prepare checksum!");
7923ae8f4e0SEzequiel Garcia 
7933ae8f4e0SEzequiel Garcia 	/* Should we set this? Can't use the value from skb_tx_csum()
794df4a17a9SYangyang Li 	 * as it's not the correct initial L4 checksum to use.
795df4a17a9SYangyang Li 	 */
7963ae8f4e0SEzequiel Garcia 	desc->l4i_chk = 0;
7973ae8f4e0SEzequiel Garcia 
7983ae8f4e0SEzequiel Garcia 	desc->byte_cnt = hdr_len;
7993ae8f4e0SEzequiel Garcia 	desc->buf_ptr = txq->tso_hdrs_dma +
8003ae8f4e0SEzequiel Garcia 			txq->tx_curr_desc * TSO_HEADER_SIZE;
801968200f3SPhilipp Kirchhofer 	cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA  | TX_FIRST_DESC |
8023ae8f4e0SEzequiel Garcia 				   GEN_CRC;
8033ae8f4e0SEzequiel Garcia 
804968200f3SPhilipp Kirchhofer 	/* Defer updating the first command descriptor until all
805968200f3SPhilipp Kirchhofer 	 * following descriptors have been written.
806968200f3SPhilipp Kirchhofer 	 */
807968200f3SPhilipp Kirchhofer 	if (first_desc)
808968200f3SPhilipp Kirchhofer 		*first_cmd_sts = cmd_sts;
809968200f3SPhilipp Kirchhofer 	else
810968200f3SPhilipp Kirchhofer 		desc->cmd_sts = cmd_sts;
811968200f3SPhilipp Kirchhofer 
8123ae8f4e0SEzequiel Garcia 	txq->tx_curr_desc++;
8133ae8f4e0SEzequiel Garcia 	if (txq->tx_curr_desc == txq->tx_ring_size)
8143ae8f4e0SEzequiel Garcia 		txq->tx_curr_desc = 0;
8153ae8f4e0SEzequiel Garcia }
8163ae8f4e0SEzequiel Garcia 
8173ae8f4e0SEzequiel Garcia static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb,
8183ae8f4e0SEzequiel Garcia 			  struct net_device *dev)
8193ae8f4e0SEzequiel Garcia {
8203ae8f4e0SEzequiel Garcia 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
821761b331cSEric Dumazet 	int hdr_len, total_len, data_left, ret;
8223ae8f4e0SEzequiel Garcia 	int desc_count = 0;
8233ae8f4e0SEzequiel Garcia 	struct tso_t tso;
824968200f3SPhilipp Kirchhofer 	struct tx_desc *first_tx_desc;
825968200f3SPhilipp Kirchhofer 	u32 first_cmd_sts = 0;
8263ae8f4e0SEzequiel Garcia 
8273ae8f4e0SEzequiel Garcia 	/* Count needed descriptors */
8283ae8f4e0SEzequiel Garcia 	if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) {
8293ae8f4e0SEzequiel Garcia 		netdev_dbg(dev, "not enough descriptors for TSO!\n");
8303ae8f4e0SEzequiel Garcia 		return -EBUSY;
8313ae8f4e0SEzequiel Garcia 	}
8323ae8f4e0SEzequiel Garcia 
833968200f3SPhilipp Kirchhofer 	first_tx_desc = &txq->tx_desc_area[txq->tx_curr_desc];
834968200f3SPhilipp Kirchhofer 
8353ae8f4e0SEzequiel Garcia 	/* Initialize the TSO handler, and prepare the first payload */
836761b331cSEric Dumazet 	hdr_len = tso_start(skb, &tso);
8373ae8f4e0SEzequiel Garcia 
8383ae8f4e0SEzequiel Garcia 	total_len = skb->len - hdr_len;
8393ae8f4e0SEzequiel Garcia 	while (total_len > 0) {
840968200f3SPhilipp Kirchhofer 		bool first_desc = (desc_count == 0);
8413ae8f4e0SEzequiel Garcia 		char *hdr;
8423ae8f4e0SEzequiel Garcia 
8433ae8f4e0SEzequiel Garcia 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
8443ae8f4e0SEzequiel Garcia 		total_len -= data_left;
8453ae8f4e0SEzequiel Garcia 		desc_count++;
8463ae8f4e0SEzequiel Garcia 
8473ae8f4e0SEzequiel Garcia 		/* prepare packet headers: MAC + IP + TCP */
8483ae8f4e0SEzequiel Garcia 		hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE;
8493ae8f4e0SEzequiel Garcia 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
850968200f3SPhilipp Kirchhofer 		txq_put_hdr_tso(skb, txq, data_left, &first_cmd_sts,
851968200f3SPhilipp Kirchhofer 				first_desc);
8523ae8f4e0SEzequiel Garcia 
8533ae8f4e0SEzequiel Garcia 		while (data_left > 0) {
8543ae8f4e0SEzequiel Garcia 			int size;
8553ae8f4e0SEzequiel Garcia 			desc_count++;
8563ae8f4e0SEzequiel Garcia 
8573ae8f4e0SEzequiel Garcia 			size = min_t(int, tso.size, data_left);
8583ae8f4e0SEzequiel Garcia 			ret = txq_put_data_tso(dev, txq, skb, tso.data, size,
8593ae8f4e0SEzequiel Garcia 					       size == data_left,
8603ae8f4e0SEzequiel Garcia 					       total_len == 0);
8613ae8f4e0SEzequiel Garcia 			if (ret)
8623ae8f4e0SEzequiel Garcia 				goto err_release;
8633ae8f4e0SEzequiel Garcia 			data_left -= size;
8643ae8f4e0SEzequiel Garcia 			tso_build_data(skb, &tso, size);
8653ae8f4e0SEzequiel Garcia 		}
8663ae8f4e0SEzequiel Garcia 	}
8673ae8f4e0SEzequiel Garcia 
8683ae8f4e0SEzequiel Garcia 	__skb_queue_tail(&txq->tx_skb, skb);
8693ae8f4e0SEzequiel Garcia 	skb_tx_timestamp(skb);
8703ae8f4e0SEzequiel Garcia 
871968200f3SPhilipp Kirchhofer 	/* ensure all other descriptors are written before first cmd_sts */
872968200f3SPhilipp Kirchhofer 	wmb();
873968200f3SPhilipp Kirchhofer 	first_tx_desc->cmd_sts = first_cmd_sts;
874968200f3SPhilipp Kirchhofer 
8753ae8f4e0SEzequiel Garcia 	/* clear TX_END status */
8763ae8f4e0SEzequiel Garcia 	mp->work_tx_end &= ~(1 << txq->index);
8773ae8f4e0SEzequiel Garcia 
8783ae8f4e0SEzequiel Garcia 	/* ensure all descriptors are written before poking hardware */
8793ae8f4e0SEzequiel Garcia 	wmb();
8803ae8f4e0SEzequiel Garcia 	txq_enable(txq);
8813ae8f4e0SEzequiel Garcia 	txq->tx_desc_count += desc_count;
8823ae8f4e0SEzequiel Garcia 	return 0;
8833ae8f4e0SEzequiel Garcia err_release:
8843ae8f4e0SEzequiel Garcia 	/* TODO: Release all used data descriptors; header descriptors must not
8853ae8f4e0SEzequiel Garcia 	 * be DMA-unmapped.
8863ae8f4e0SEzequiel Garcia 	 */
8873ae8f4e0SEzequiel Garcia 	return ret;
8883ae8f4e0SEzequiel Garcia }
8893ae8f4e0SEzequiel Garcia 
890527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
891527a6266SJeff Kirsher {
892527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
893527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
894527a6266SJeff Kirsher 	int frag;
895527a6266SJeff Kirsher 
896527a6266SJeff Kirsher 	for (frag = 0; frag < nr_frags; frag++) {
897527a6266SJeff Kirsher 		skb_frag_t *this_frag;
898527a6266SJeff Kirsher 		int tx_index;
899527a6266SJeff Kirsher 		struct tx_desc *desc;
900527a6266SJeff Kirsher 
901527a6266SJeff Kirsher 		this_frag = &skb_shinfo(skb)->frags[frag];
902527a6266SJeff Kirsher 		tx_index = txq->tx_curr_desc++;
903527a6266SJeff Kirsher 		if (txq->tx_curr_desc == txq->tx_ring_size)
904527a6266SJeff Kirsher 			txq->tx_curr_desc = 0;
905527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
9069e911414SEzequiel Garcia 		txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_PAGE;
907527a6266SJeff Kirsher 
908527a6266SJeff Kirsher 		/*
909527a6266SJeff Kirsher 		 * The last fragment will generate an interrupt
910527a6266SJeff Kirsher 		 * which will free the skb on TX completion.
911527a6266SJeff Kirsher 		 */
912527a6266SJeff Kirsher 		if (frag == nr_frags - 1) {
913527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA |
914527a6266SJeff Kirsher 					ZERO_PADDING | TX_LAST_DESC |
915527a6266SJeff Kirsher 					TX_ENABLE_INTERRUPT;
916527a6266SJeff Kirsher 		} else {
917527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA;
918527a6266SJeff Kirsher 		}
919527a6266SJeff Kirsher 
920527a6266SJeff Kirsher 		desc->l4i_chk = 0;
9219e903e08SEric Dumazet 		desc->byte_cnt = skb_frag_size(this_frag);
9229e911414SEzequiel Garcia 		desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
9239e911414SEzequiel Garcia 						 this_frag, 0, desc->byte_cnt,
9249e911414SEzequiel Garcia 						 DMA_TO_DEVICE);
925527a6266SJeff Kirsher 	}
926527a6266SJeff Kirsher }
927527a6266SJeff Kirsher 
928ee9e4956SEzequiel Garcia static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb,
929ee9e4956SEzequiel Garcia 			  struct net_device *dev)
930527a6266SJeff Kirsher {
931527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
932527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
933527a6266SJeff Kirsher 	int tx_index;
934527a6266SJeff Kirsher 	struct tx_desc *desc;
935527a6266SJeff Kirsher 	u32 cmd_sts;
936527a6266SJeff Kirsher 	u16 l4i_chk;
9370a8fa933SEzequiel Garcia 	int length, ret;
938527a6266SJeff Kirsher 
9390a8fa933SEzequiel Garcia 	cmd_sts = 0;
940527a6266SJeff Kirsher 	l4i_chk = 0;
941527a6266SJeff Kirsher 
942ee9e4956SEzequiel Garcia 	if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
943ee9e4956SEzequiel Garcia 		if (net_ratelimit())
944ee9e4956SEzequiel Garcia 			netdev_err(dev, "tx queue full?!\n");
945ee9e4956SEzequiel Garcia 		return -EBUSY;
946527a6266SJeff Kirsher 	}
947ee9e4956SEzequiel Garcia 
948ee9e4956SEzequiel Garcia 	ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len);
949ee9e4956SEzequiel Garcia 	if (ret)
950ee9e4956SEzequiel Garcia 		return ret;
9510a8fa933SEzequiel Garcia 	cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
952527a6266SJeff Kirsher 
953527a6266SJeff Kirsher 	tx_index = txq->tx_curr_desc++;
954527a6266SJeff Kirsher 	if (txq->tx_curr_desc == txq->tx_ring_size)
955527a6266SJeff Kirsher 		txq->tx_curr_desc = 0;
956527a6266SJeff Kirsher 	desc = &txq->tx_desc_area[tx_index];
9579e911414SEzequiel Garcia 	txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
958527a6266SJeff Kirsher 
959527a6266SJeff Kirsher 	if (nr_frags) {
960527a6266SJeff Kirsher 		txq_submit_frag_skb(txq, skb);
961527a6266SJeff Kirsher 		length = skb_headlen(skb);
962527a6266SJeff Kirsher 	} else {
963527a6266SJeff Kirsher 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
964527a6266SJeff Kirsher 		length = skb->len;
965527a6266SJeff Kirsher 	}
966527a6266SJeff Kirsher 
967527a6266SJeff Kirsher 	desc->l4i_chk = l4i_chk;
968527a6266SJeff Kirsher 	desc->byte_cnt = length;
969527a6266SJeff Kirsher 	desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
970527a6266SJeff Kirsher 				       length, DMA_TO_DEVICE);
971527a6266SJeff Kirsher 
972527a6266SJeff Kirsher 	__skb_queue_tail(&txq->tx_skb, skb);
973527a6266SJeff Kirsher 
974527a6266SJeff Kirsher 	skb_tx_timestamp(skb);
975527a6266SJeff Kirsher 
976527a6266SJeff Kirsher 	/* ensure all other descriptors are written before first cmd_sts */
977527a6266SJeff Kirsher 	wmb();
978527a6266SJeff Kirsher 	desc->cmd_sts = cmd_sts;
979527a6266SJeff Kirsher 
980527a6266SJeff Kirsher 	/* clear TX_END status */
981527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
982527a6266SJeff Kirsher 
983527a6266SJeff Kirsher 	/* ensure all descriptors are written before poking hardware */
984527a6266SJeff Kirsher 	wmb();
985527a6266SJeff Kirsher 	txq_enable(txq);
986527a6266SJeff Kirsher 
987527a6266SJeff Kirsher 	txq->tx_desc_count += nr_frags + 1;
988527a6266SJeff Kirsher 
989527a6266SJeff Kirsher 	return 0;
990527a6266SJeff Kirsher }
991527a6266SJeff Kirsher 
992527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
993527a6266SJeff Kirsher {
994527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
9953ae8f4e0SEzequiel Garcia 	int length, queue, ret;
996527a6266SJeff Kirsher 	struct tx_queue *txq;
997527a6266SJeff Kirsher 	struct netdev_queue *nq;
998527a6266SJeff Kirsher 
999527a6266SJeff Kirsher 	queue = skb_get_queue_mapping(skb);
1000527a6266SJeff Kirsher 	txq = mp->txq + queue;
1001527a6266SJeff Kirsher 	nq = netdev_get_tx_queue(dev, queue);
1002527a6266SJeff Kirsher 
1003527a6266SJeff Kirsher 	if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1004527a6266SJeff Kirsher 		netdev_printk(KERN_DEBUG, dev,
1005527a6266SJeff Kirsher 			      "failed to linearize skb with tiny unaligned fragment\n");
1006527a6266SJeff Kirsher 		return NETDEV_TX_BUSY;
1007527a6266SJeff Kirsher 	}
1008527a6266SJeff Kirsher 
1009527a6266SJeff Kirsher 	length = skb->len;
1010527a6266SJeff Kirsher 
10113ae8f4e0SEzequiel Garcia 	if (skb_is_gso(skb))
10123ae8f4e0SEzequiel Garcia 		ret = txq_submit_tso(txq, skb, dev);
10133ae8f4e0SEzequiel Garcia 	else
1014ee9e4956SEzequiel Garcia 		ret = txq_submit_skb(txq, skb, dev);
10153ae8f4e0SEzequiel Garcia 	if (!ret) {
1016527a6266SJeff Kirsher 		txq->tx_bytes += length;
1017527a6266SJeff Kirsher 		txq->tx_packets++;
1018527a6266SJeff Kirsher 
1019ee9e4956SEzequiel Garcia 		if (txq->tx_desc_count >= txq->tx_stop_threshold)
1020527a6266SJeff Kirsher 			netif_tx_stop_queue(nq);
1021dd11680dSEzequiel Garcia 	} else {
1022dd11680dSEzequiel Garcia 		txq->tx_dropped++;
1023dd11680dSEzequiel Garcia 		dev_kfree_skb_any(skb);
1024527a6266SJeff Kirsher 	}
1025527a6266SJeff Kirsher 
1026527a6266SJeff Kirsher 	return NETDEV_TX_OK;
1027527a6266SJeff Kirsher }
1028527a6266SJeff Kirsher 
1029527a6266SJeff Kirsher 
1030527a6266SJeff Kirsher /* tx napi ******************************************************************/
1031527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq)
1032527a6266SJeff Kirsher {
1033527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1034527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
1035527a6266SJeff Kirsher 	u32 hw_desc_ptr;
1036527a6266SJeff Kirsher 	u32 expected_ptr;
1037527a6266SJeff Kirsher 
1038527a6266SJeff Kirsher 	__netif_tx_lock(nq, smp_processor_id());
1039527a6266SJeff Kirsher 
1040527a6266SJeff Kirsher 	if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
1041527a6266SJeff Kirsher 		goto out;
1042527a6266SJeff Kirsher 
1043527a6266SJeff Kirsher 	hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
1044527a6266SJeff Kirsher 	expected_ptr = (u32)txq->tx_desc_dma +
1045527a6266SJeff Kirsher 				txq->tx_curr_desc * sizeof(struct tx_desc);
1046527a6266SJeff Kirsher 
1047527a6266SJeff Kirsher 	if (hw_desc_ptr != expected_ptr)
1048527a6266SJeff Kirsher 		txq_enable(txq);
1049527a6266SJeff Kirsher 
1050527a6266SJeff Kirsher out:
1051527a6266SJeff Kirsher 	__netif_tx_unlock(nq);
1052527a6266SJeff Kirsher 
1053527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
1054527a6266SJeff Kirsher }
1055527a6266SJeff Kirsher 
1056527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force)
1057527a6266SJeff Kirsher {
1058527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1059527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
1060527a6266SJeff Kirsher 	int reclaimed;
1061527a6266SJeff Kirsher 
10623aefe2b4SRussell King 	__netif_tx_lock_bh(nq);
1063527a6266SJeff Kirsher 
1064527a6266SJeff Kirsher 	reclaimed = 0;
1065527a6266SJeff Kirsher 	while (reclaimed < budget && txq->tx_desc_count > 0) {
1066527a6266SJeff Kirsher 		int tx_index;
1067527a6266SJeff Kirsher 		struct tx_desc *desc;
1068527a6266SJeff Kirsher 		u32 cmd_sts;
10699e911414SEzequiel Garcia 		char desc_dma_map;
1070527a6266SJeff Kirsher 
1071527a6266SJeff Kirsher 		tx_index = txq->tx_used_desc;
1072527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
10739e911414SEzequiel Garcia 		desc_dma_map = txq->tx_desc_mapping[tx_index];
10749e911414SEzequiel Garcia 
1075527a6266SJeff Kirsher 		cmd_sts = desc->cmd_sts;
1076527a6266SJeff Kirsher 
1077527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA) {
1078527a6266SJeff Kirsher 			if (!force)
1079527a6266SJeff Kirsher 				break;
1080527a6266SJeff Kirsher 			desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
1081527a6266SJeff Kirsher 		}
1082527a6266SJeff Kirsher 
1083527a6266SJeff Kirsher 		txq->tx_used_desc = tx_index + 1;
1084527a6266SJeff Kirsher 		if (txq->tx_used_desc == txq->tx_ring_size)
1085527a6266SJeff Kirsher 			txq->tx_used_desc = 0;
1086527a6266SJeff Kirsher 
1087527a6266SJeff Kirsher 		reclaimed++;
1088527a6266SJeff Kirsher 		txq->tx_desc_count--;
1089527a6266SJeff Kirsher 
10909e911414SEzequiel Garcia 		if (!IS_TSO_HEADER(txq, desc->buf_ptr)) {
10919e911414SEzequiel Garcia 
10929e911414SEzequiel Garcia 			if (desc_dma_map == DESC_DMA_MAP_PAGE)
10939e911414SEzequiel Garcia 				dma_unmap_page(mp->dev->dev.parent,
10949e911414SEzequiel Garcia 					       desc->buf_ptr,
10959e911414SEzequiel Garcia 					       desc->byte_cnt,
10969e911414SEzequiel Garcia 					       DMA_TO_DEVICE);
10979e911414SEzequiel Garcia 			else
10989e911414SEzequiel Garcia 				dma_unmap_single(mp->dev->dev.parent,
10999e911414SEzequiel Garcia 						 desc->buf_ptr,
11009e911414SEzequiel Garcia 						 desc->byte_cnt,
11019e911414SEzequiel Garcia 						 DMA_TO_DEVICE);
11029e911414SEzequiel Garcia 		}
11032c2a9cbdSKarl Beldan 
11042c2a9cbdSKarl Beldan 		if (cmd_sts & TX_ENABLE_INTERRUPT) {
11052c2a9cbdSKarl Beldan 			struct sk_buff *skb = __skb_dequeue(&txq->tx_skb);
11062c2a9cbdSKarl Beldan 
11072c2a9cbdSKarl Beldan 			if (!WARN_ON(!skb))
110843cee2d2SFlorian Fainelli 				dev_consume_skb_any(skb);
11092c2a9cbdSKarl Beldan 		}
1110527a6266SJeff Kirsher 
1111527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY) {
1112527a6266SJeff Kirsher 			netdev_info(mp->dev, "tx error\n");
1113527a6266SJeff Kirsher 			mp->dev->stats.tx_errors++;
1114527a6266SJeff Kirsher 		}
1115527a6266SJeff Kirsher 
1116527a6266SJeff Kirsher 	}
1117527a6266SJeff Kirsher 
11183aefe2b4SRussell King 	__netif_tx_unlock_bh(nq);
1119527a6266SJeff Kirsher 
1120527a6266SJeff Kirsher 	if (reclaimed < budget)
1121527a6266SJeff Kirsher 		mp->work_tx &= ~(1 << txq->index);
1122527a6266SJeff Kirsher 
1123527a6266SJeff Kirsher 	return reclaimed;
1124527a6266SJeff Kirsher }
1125527a6266SJeff Kirsher 
1126527a6266SJeff Kirsher 
1127527a6266SJeff Kirsher /* tx rate control **********************************************************/
1128527a6266SJeff Kirsher /*
1129527a6266SJeff Kirsher  * Set total maximum TX rate (shared by all TX queues for this port)
1130527a6266SJeff Kirsher  * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
1131527a6266SJeff Kirsher  */
1132527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
1133527a6266SJeff Kirsher {
1134527a6266SJeff Kirsher 	int token_rate;
1135527a6266SJeff Kirsher 	int mtu;
1136527a6266SJeff Kirsher 	int bucket_size;
1137527a6266SJeff Kirsher 
1138452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1139527a6266SJeff Kirsher 	if (token_rate > 1023)
1140527a6266SJeff Kirsher 		token_rate = 1023;
1141527a6266SJeff Kirsher 
1142527a6266SJeff Kirsher 	mtu = (mp->dev->mtu + 255) >> 8;
1143527a6266SJeff Kirsher 	if (mtu > 63)
1144527a6266SJeff Kirsher 		mtu = 63;
1145527a6266SJeff Kirsher 
1146527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1147527a6266SJeff Kirsher 	if (bucket_size > 65535)
1148527a6266SJeff Kirsher 		bucket_size = 65535;
1149527a6266SJeff Kirsher 
1150527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1151527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1152527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE, token_rate);
1153527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU, mtu);
1154527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST, bucket_size);
1155527a6266SJeff Kirsher 		break;
1156527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1157527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE_MOVED, token_rate);
1158527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU_MOVED, mtu);
1159527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
1160527a6266SJeff Kirsher 		break;
1161527a6266SJeff Kirsher 	}
1162527a6266SJeff Kirsher }
1163527a6266SJeff Kirsher 
1164527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
1165527a6266SJeff Kirsher {
1166527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1167527a6266SJeff Kirsher 	int token_rate;
1168527a6266SJeff Kirsher 	int bucket_size;
1169527a6266SJeff Kirsher 
1170452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1171527a6266SJeff Kirsher 	if (token_rate > 1023)
1172527a6266SJeff Kirsher 		token_rate = 1023;
1173527a6266SJeff Kirsher 
1174527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1175527a6266SJeff Kirsher 	if (bucket_size > 65535)
1176527a6266SJeff Kirsher 		bucket_size = 65535;
1177527a6266SJeff Kirsher 
1178527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
1179527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
1180527a6266SJeff Kirsher }
1181527a6266SJeff Kirsher 
1182527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq)
1183527a6266SJeff Kirsher {
1184527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1185527a6266SJeff Kirsher 	int off;
1186527a6266SJeff Kirsher 	u32 val;
1187527a6266SJeff Kirsher 
1188527a6266SJeff Kirsher 	/*
1189527a6266SJeff Kirsher 	 * Turn on fixed priority mode.
1190527a6266SJeff Kirsher 	 */
1191527a6266SJeff Kirsher 	off = 0;
1192527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1193527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1194527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF;
1195527a6266SJeff Kirsher 		break;
1196527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1197527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF_MOVED;
1198527a6266SJeff Kirsher 		break;
1199527a6266SJeff Kirsher 	}
1200527a6266SJeff Kirsher 
1201527a6266SJeff Kirsher 	if (off) {
1202527a6266SJeff Kirsher 		val = rdlp(mp, off);
1203527a6266SJeff Kirsher 		val |= 1 << txq->index;
1204527a6266SJeff Kirsher 		wrlp(mp, off, val);
1205527a6266SJeff Kirsher 	}
1206527a6266SJeff Kirsher }
1207527a6266SJeff Kirsher 
1208527a6266SJeff Kirsher 
1209527a6266SJeff Kirsher /* mii management interface *************************************************/
12100a9e413bSEzequiel Garcia static void mv643xx_eth_adjust_link(struct net_device *dev)
1211260055bbSPhil Sutter {
12120a9e413bSEzequiel Garcia 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1213260055bbSPhil Sutter 	u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1214260055bbSPhil Sutter 	u32 autoneg_disable = FORCE_LINK_PASS |
1215260055bbSPhil Sutter 	             DISABLE_AUTO_NEG_SPEED_GMII |
1216260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1217260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_DUPLEX;
1218260055bbSPhil Sutter 
12191e8a655dSPhilippe Reynes 	if (dev->phydev->autoneg == AUTONEG_ENABLE) {
1220260055bbSPhil Sutter 		/* enable auto negotiation */
1221260055bbSPhil Sutter 		pscr &= ~autoneg_disable;
1222260055bbSPhil Sutter 		goto out_write;
1223260055bbSPhil Sutter 	}
1224260055bbSPhil Sutter 
1225260055bbSPhil Sutter 	pscr |= autoneg_disable;
1226260055bbSPhil Sutter 
12271e8a655dSPhilippe Reynes 	if (dev->phydev->speed == SPEED_1000) {
1228260055bbSPhil Sutter 		/* force gigabit, half duplex not supported */
1229260055bbSPhil Sutter 		pscr |= SET_GMII_SPEED_TO_1000;
1230260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1231260055bbSPhil Sutter 		goto out_write;
1232260055bbSPhil Sutter 	}
1233260055bbSPhil Sutter 
1234260055bbSPhil Sutter 	pscr &= ~SET_GMII_SPEED_TO_1000;
1235260055bbSPhil Sutter 
12361e8a655dSPhilippe Reynes 	if (dev->phydev->speed == SPEED_100)
1237260055bbSPhil Sutter 		pscr |= SET_MII_SPEED_TO_100;
1238260055bbSPhil Sutter 	else
1239260055bbSPhil Sutter 		pscr &= ~SET_MII_SPEED_TO_100;
1240260055bbSPhil Sutter 
12411e8a655dSPhilippe Reynes 	if (dev->phydev->duplex == DUPLEX_FULL)
1242260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1243260055bbSPhil Sutter 	else
1244260055bbSPhil Sutter 		pscr &= ~SET_FULL_DUPLEX_MODE;
1245260055bbSPhil Sutter 
1246260055bbSPhil Sutter out_write:
1247260055bbSPhil Sutter 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1248260055bbSPhil Sutter }
1249260055bbSPhil Sutter 
1250527a6266SJeff Kirsher /* statistics ***************************************************************/
1251527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1252527a6266SJeff Kirsher {
1253527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1254527a6266SJeff Kirsher 	struct net_device_stats *stats = &dev->stats;
1255527a6266SJeff Kirsher 	unsigned long tx_packets = 0;
1256527a6266SJeff Kirsher 	unsigned long tx_bytes = 0;
1257527a6266SJeff Kirsher 	unsigned long tx_dropped = 0;
1258527a6266SJeff Kirsher 	int i;
1259527a6266SJeff Kirsher 
1260527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
1261527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
1262527a6266SJeff Kirsher 
1263527a6266SJeff Kirsher 		tx_packets += txq->tx_packets;
1264527a6266SJeff Kirsher 		tx_bytes += txq->tx_bytes;
1265527a6266SJeff Kirsher 		tx_dropped += txq->tx_dropped;
1266527a6266SJeff Kirsher 	}
1267527a6266SJeff Kirsher 
1268527a6266SJeff Kirsher 	stats->tx_packets = tx_packets;
1269527a6266SJeff Kirsher 	stats->tx_bytes = tx_bytes;
1270527a6266SJeff Kirsher 	stats->tx_dropped = tx_dropped;
1271527a6266SJeff Kirsher 
1272527a6266SJeff Kirsher 	return stats;
1273527a6266SJeff Kirsher }
1274527a6266SJeff Kirsher 
1275527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
1276527a6266SJeff Kirsher {
1277527a6266SJeff Kirsher 	return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
1278527a6266SJeff Kirsher }
1279527a6266SJeff Kirsher 
1280527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp)
1281527a6266SJeff Kirsher {
1282527a6266SJeff Kirsher 	int i;
1283527a6266SJeff Kirsher 
1284527a6266SJeff Kirsher 	for (i = 0; i < 0x80; i += 4)
1285527a6266SJeff Kirsher 		mib_read(mp, i);
1286302476c9SPaulius Zaleckas 
1287302476c9SPaulius Zaleckas 	/* Clear non MIB hw counters also */
1288302476c9SPaulius Zaleckas 	rdlp(mp, RX_DISCARD_FRAME_CNT);
1289302476c9SPaulius Zaleckas 	rdlp(mp, RX_OVERRUN_FRAME_CNT);
1290527a6266SJeff Kirsher }
1291527a6266SJeff Kirsher 
1292527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp)
1293527a6266SJeff Kirsher {
1294527a6266SJeff Kirsher 	struct mib_counters *p = &mp->mib_counters;
1295527a6266SJeff Kirsher 
1296527a6266SJeff Kirsher 	spin_lock_bh(&mp->mib_counters_lock);
1297527a6266SJeff Kirsher 	p->good_octets_received += mib_read(mp, 0x00);
1298527a6266SJeff Kirsher 	p->bad_octets_received += mib_read(mp, 0x08);
1299527a6266SJeff Kirsher 	p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1300527a6266SJeff Kirsher 	p->good_frames_received += mib_read(mp, 0x10);
1301527a6266SJeff Kirsher 	p->bad_frames_received += mib_read(mp, 0x14);
1302527a6266SJeff Kirsher 	p->broadcast_frames_received += mib_read(mp, 0x18);
1303527a6266SJeff Kirsher 	p->multicast_frames_received += mib_read(mp, 0x1c);
1304527a6266SJeff Kirsher 	p->frames_64_octets += mib_read(mp, 0x20);
1305527a6266SJeff Kirsher 	p->frames_65_to_127_octets += mib_read(mp, 0x24);
1306527a6266SJeff Kirsher 	p->frames_128_to_255_octets += mib_read(mp, 0x28);
1307527a6266SJeff Kirsher 	p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1308527a6266SJeff Kirsher 	p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1309527a6266SJeff Kirsher 	p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1310527a6266SJeff Kirsher 	p->good_octets_sent += mib_read(mp, 0x38);
1311527a6266SJeff Kirsher 	p->good_frames_sent += mib_read(mp, 0x40);
1312527a6266SJeff Kirsher 	p->excessive_collision += mib_read(mp, 0x44);
1313527a6266SJeff Kirsher 	p->multicast_frames_sent += mib_read(mp, 0x48);
1314527a6266SJeff Kirsher 	p->broadcast_frames_sent += mib_read(mp, 0x4c);
1315527a6266SJeff Kirsher 	p->unrec_mac_control_received += mib_read(mp, 0x50);
1316527a6266SJeff Kirsher 	p->fc_sent += mib_read(mp, 0x54);
1317527a6266SJeff Kirsher 	p->good_fc_received += mib_read(mp, 0x58);
1318527a6266SJeff Kirsher 	p->bad_fc_received += mib_read(mp, 0x5c);
1319527a6266SJeff Kirsher 	p->undersize_received += mib_read(mp, 0x60);
1320527a6266SJeff Kirsher 	p->fragments_received += mib_read(mp, 0x64);
1321527a6266SJeff Kirsher 	p->oversize_received += mib_read(mp, 0x68);
1322527a6266SJeff Kirsher 	p->jabber_received += mib_read(mp, 0x6c);
1323527a6266SJeff Kirsher 	p->mac_receive_error += mib_read(mp, 0x70);
1324527a6266SJeff Kirsher 	p->bad_crc_event += mib_read(mp, 0x74);
1325527a6266SJeff Kirsher 	p->collision += mib_read(mp, 0x78);
1326527a6266SJeff Kirsher 	p->late_collision += mib_read(mp, 0x7c);
1327302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
1328302476c9SPaulius Zaleckas 	p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1329302476c9SPaulius Zaleckas 	p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
1330527a6266SJeff Kirsher 	spin_unlock_bh(&mp->mib_counters_lock);
1331527a6266SJeff Kirsher }
1332527a6266SJeff Kirsher 
1333e99e88a9SKees Cook static void mib_counters_timer_wrapper(struct timer_list *t)
1334527a6266SJeff Kirsher {
1335e99e88a9SKees Cook 	struct mv643xx_eth_private *mp = from_timer(mp, t, mib_counters_timer);
1336527a6266SJeff Kirsher 	mib_counters_update(mp);
1337041b4ddbSSebastian Hesselbarth 	mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1338527a6266SJeff Kirsher }
1339527a6266SJeff Kirsher 
1340527a6266SJeff Kirsher 
1341527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/
1342527a6266SJeff Kirsher /*
1343527a6266SJeff Kirsher  * Hardware coalescing parameters are set in units of 64 t_clk
1344527a6266SJeff Kirsher  * cycles.  I.e.:
1345527a6266SJeff Kirsher  *
1346527a6266SJeff Kirsher  *	coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1347527a6266SJeff Kirsher  *
1348527a6266SJeff Kirsher  *	register_value = coal_delay_in_usec * t_clk_rate / 64000000
1349527a6266SJeff Kirsher  *
1350527a6266SJeff Kirsher  * In the ->set*() methods, we round the computed register value
1351527a6266SJeff Kirsher  * to the nearest integer.
1352527a6266SJeff Kirsher  */
1353527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1354527a6266SJeff Kirsher {
1355527a6266SJeff Kirsher 	u32 val = rdlp(mp, SDMA_CONFIG);
1356527a6266SJeff Kirsher 	u64 temp;
1357527a6266SJeff Kirsher 
1358527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit)
1359527a6266SJeff Kirsher 		temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1360527a6266SJeff Kirsher 	else
1361527a6266SJeff Kirsher 		temp = (val & 0x003fff00) >> 8;
1362527a6266SJeff Kirsher 
1363527a6266SJeff Kirsher 	temp *= 64000000;
1364b9b84fc0SRussell King 	temp += mp->t_clk / 2;
1365452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1366527a6266SJeff Kirsher 
1367527a6266SJeff Kirsher 	return (unsigned int)temp;
1368527a6266SJeff Kirsher }
1369527a6266SJeff Kirsher 
1370527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1371527a6266SJeff Kirsher {
1372527a6266SJeff Kirsher 	u64 temp;
1373527a6266SJeff Kirsher 	u32 val;
1374527a6266SJeff Kirsher 
1375452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1376527a6266SJeff Kirsher 	temp += 31999999;
1377527a6266SJeff Kirsher 	do_div(temp, 64000000);
1378527a6266SJeff Kirsher 
1379527a6266SJeff Kirsher 	val = rdlp(mp, SDMA_CONFIG);
1380527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit) {
1381527a6266SJeff Kirsher 		if (temp > 0xffff)
1382527a6266SJeff Kirsher 			temp = 0xffff;
1383527a6266SJeff Kirsher 		val &= ~0x023fff80;
1384527a6266SJeff Kirsher 		val |= (temp & 0x8000) << 10;
1385527a6266SJeff Kirsher 		val |= (temp & 0x7fff) << 7;
1386527a6266SJeff Kirsher 	} else {
1387527a6266SJeff Kirsher 		if (temp > 0x3fff)
1388527a6266SJeff Kirsher 			temp = 0x3fff;
1389527a6266SJeff Kirsher 		val &= ~0x003fff00;
1390527a6266SJeff Kirsher 		val |= (temp & 0x3fff) << 8;
1391527a6266SJeff Kirsher 	}
1392527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, val);
1393527a6266SJeff Kirsher }
1394527a6266SJeff Kirsher 
1395527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1396527a6266SJeff Kirsher {
1397527a6266SJeff Kirsher 	u64 temp;
1398527a6266SJeff Kirsher 
1399527a6266SJeff Kirsher 	temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1400527a6266SJeff Kirsher 	temp *= 64000000;
1401b9b84fc0SRussell King 	temp += mp->t_clk / 2;
1402452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1403527a6266SJeff Kirsher 
1404527a6266SJeff Kirsher 	return (unsigned int)temp;
1405527a6266SJeff Kirsher }
1406527a6266SJeff Kirsher 
1407527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1408527a6266SJeff Kirsher {
1409527a6266SJeff Kirsher 	u64 temp;
1410527a6266SJeff Kirsher 
1411452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1412527a6266SJeff Kirsher 	temp += 31999999;
1413527a6266SJeff Kirsher 	do_div(temp, 64000000);
1414527a6266SJeff Kirsher 
1415527a6266SJeff Kirsher 	if (temp > 0x3fff)
1416527a6266SJeff Kirsher 		temp = 0x3fff;
1417527a6266SJeff Kirsher 
1418527a6266SJeff Kirsher 	wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1419527a6266SJeff Kirsher }
1420527a6266SJeff Kirsher 
1421527a6266SJeff Kirsher 
1422527a6266SJeff Kirsher /* ethtool ******************************************************************/
1423527a6266SJeff Kirsher struct mv643xx_eth_stats {
1424527a6266SJeff Kirsher 	char stat_string[ETH_GSTRING_LEN];
1425527a6266SJeff Kirsher 	int sizeof_stat;
1426527a6266SJeff Kirsher 	int netdev_off;
1427527a6266SJeff Kirsher 	int mp_off;
1428527a6266SJeff Kirsher };
1429527a6266SJeff Kirsher 
1430527a6266SJeff Kirsher #define SSTAT(m)						\
1431c593642cSPankaj Bharadiya 	{ #m, sizeof_field(struct net_device_stats, m),		\
1432527a6266SJeff Kirsher 	  offsetof(struct net_device, stats.m), -1 }
1433527a6266SJeff Kirsher 
1434527a6266SJeff Kirsher #define MIBSTAT(m)						\
1435c593642cSPankaj Bharadiya 	{ #m, sizeof_field(struct mib_counters, m),		\
1436527a6266SJeff Kirsher 	  -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1437527a6266SJeff Kirsher 
1438527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1439527a6266SJeff Kirsher 	SSTAT(rx_packets),
1440527a6266SJeff Kirsher 	SSTAT(tx_packets),
1441527a6266SJeff Kirsher 	SSTAT(rx_bytes),
1442527a6266SJeff Kirsher 	SSTAT(tx_bytes),
1443527a6266SJeff Kirsher 	SSTAT(rx_errors),
1444527a6266SJeff Kirsher 	SSTAT(tx_errors),
1445527a6266SJeff Kirsher 	SSTAT(rx_dropped),
1446527a6266SJeff Kirsher 	SSTAT(tx_dropped),
1447527a6266SJeff Kirsher 	MIBSTAT(good_octets_received),
1448527a6266SJeff Kirsher 	MIBSTAT(bad_octets_received),
1449527a6266SJeff Kirsher 	MIBSTAT(internal_mac_transmit_err),
1450527a6266SJeff Kirsher 	MIBSTAT(good_frames_received),
1451527a6266SJeff Kirsher 	MIBSTAT(bad_frames_received),
1452527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_received),
1453527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_received),
1454527a6266SJeff Kirsher 	MIBSTAT(frames_64_octets),
1455527a6266SJeff Kirsher 	MIBSTAT(frames_65_to_127_octets),
1456527a6266SJeff Kirsher 	MIBSTAT(frames_128_to_255_octets),
1457527a6266SJeff Kirsher 	MIBSTAT(frames_256_to_511_octets),
1458527a6266SJeff Kirsher 	MIBSTAT(frames_512_to_1023_octets),
1459527a6266SJeff Kirsher 	MIBSTAT(frames_1024_to_max_octets),
1460527a6266SJeff Kirsher 	MIBSTAT(good_octets_sent),
1461527a6266SJeff Kirsher 	MIBSTAT(good_frames_sent),
1462527a6266SJeff Kirsher 	MIBSTAT(excessive_collision),
1463527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_sent),
1464527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_sent),
1465527a6266SJeff Kirsher 	MIBSTAT(unrec_mac_control_received),
1466527a6266SJeff Kirsher 	MIBSTAT(fc_sent),
1467527a6266SJeff Kirsher 	MIBSTAT(good_fc_received),
1468527a6266SJeff Kirsher 	MIBSTAT(bad_fc_received),
1469527a6266SJeff Kirsher 	MIBSTAT(undersize_received),
1470527a6266SJeff Kirsher 	MIBSTAT(fragments_received),
1471527a6266SJeff Kirsher 	MIBSTAT(oversize_received),
1472527a6266SJeff Kirsher 	MIBSTAT(jabber_received),
1473527a6266SJeff Kirsher 	MIBSTAT(mac_receive_error),
1474527a6266SJeff Kirsher 	MIBSTAT(bad_crc_event),
1475527a6266SJeff Kirsher 	MIBSTAT(collision),
1476527a6266SJeff Kirsher 	MIBSTAT(late_collision),
1477302476c9SPaulius Zaleckas 	MIBSTAT(rx_discard),
1478302476c9SPaulius Zaleckas 	MIBSTAT(rx_overrun),
1479527a6266SJeff Kirsher };
1480527a6266SJeff Kirsher 
1481527a6266SJeff Kirsher static int
1482a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings_phy(struct mv643xx_eth_private *mp,
1483a54e1612SPhilippe Reynes 				   struct ethtool_link_ksettings *cmd)
1484527a6266SJeff Kirsher {
14851e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
1486527a6266SJeff Kirsher 
14875514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(dev->phydev, cmd);
1488527a6266SJeff Kirsher 
1489527a6266SJeff Kirsher 	/*
1490527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1491527a6266SJeff Kirsher 	 */
14923c1bcc86SAndrew Lunn 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1493a54e1612SPhilippe Reynes 			   cmd->link_modes.supported);
14943c1bcc86SAndrew Lunn 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1495a54e1612SPhilippe Reynes 			   cmd->link_modes.advertising);
1496527a6266SJeff Kirsher 
14975514174fSyuval.shaia@oracle.com 	return 0;
1498527a6266SJeff Kirsher }
1499527a6266SJeff Kirsher 
1500527a6266SJeff Kirsher static int
1501a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings_phyless(struct mv643xx_eth_private *mp,
1502a54e1612SPhilippe Reynes 				       struct ethtool_link_ksettings *cmd)
1503527a6266SJeff Kirsher {
1504527a6266SJeff Kirsher 	u32 port_status;
1505a54e1612SPhilippe Reynes 	u32 supported, advertising;
1506527a6266SJeff Kirsher 
1507527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
1508527a6266SJeff Kirsher 
1509a54e1612SPhilippe Reynes 	supported = SUPPORTED_MII;
1510a54e1612SPhilippe Reynes 	advertising = ADVERTISED_MII;
1511527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
1512527a6266SJeff Kirsher 	case PORT_SPEED_10:
1513a54e1612SPhilippe Reynes 		cmd->base.speed = SPEED_10;
1514527a6266SJeff Kirsher 		break;
1515527a6266SJeff Kirsher 	case PORT_SPEED_100:
1516a54e1612SPhilippe Reynes 		cmd->base.speed = SPEED_100;
1517527a6266SJeff Kirsher 		break;
1518527a6266SJeff Kirsher 	case PORT_SPEED_1000:
1519a54e1612SPhilippe Reynes 		cmd->base.speed = SPEED_1000;
1520527a6266SJeff Kirsher 		break;
1521527a6266SJeff Kirsher 	default:
1522a54e1612SPhilippe Reynes 		cmd->base.speed = -1;
1523527a6266SJeff Kirsher 		break;
1524527a6266SJeff Kirsher 	}
1525a54e1612SPhilippe Reynes 	cmd->base.duplex = (port_status & FULL_DUPLEX) ?
1526a54e1612SPhilippe Reynes 		DUPLEX_FULL : DUPLEX_HALF;
1527a54e1612SPhilippe Reynes 	cmd->base.port = PORT_MII;
1528a54e1612SPhilippe Reynes 	cmd->base.phy_address = 0;
1529a54e1612SPhilippe Reynes 	cmd->base.autoneg = AUTONEG_DISABLE;
1530a54e1612SPhilippe Reynes 
1531a54e1612SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1532a54e1612SPhilippe Reynes 						supported);
1533a54e1612SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1534a54e1612SPhilippe Reynes 						advertising);
1535527a6266SJeff Kirsher 
1536527a6266SJeff Kirsher 	return 0;
1537527a6266SJeff Kirsher }
1538527a6266SJeff Kirsher 
15393871c387SMichael Stapelberg static void
15403871c387SMichael Stapelberg mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
15413871c387SMichael Stapelberg {
15423871c387SMichael Stapelberg 	wol->supported = 0;
15433871c387SMichael Stapelberg 	wol->wolopts = 0;
15441e8a655dSPhilippe Reynes 	if (dev->phydev)
15451e8a655dSPhilippe Reynes 		phy_ethtool_get_wol(dev->phydev, wol);
15463871c387SMichael Stapelberg }
15473871c387SMichael Stapelberg 
15483871c387SMichael Stapelberg static int
15493871c387SMichael Stapelberg mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
15503871c387SMichael Stapelberg {
15513871c387SMichael Stapelberg 	int err;
15523871c387SMichael Stapelberg 
15531e8a655dSPhilippe Reynes 	if (!dev->phydev)
15543871c387SMichael Stapelberg 		return -EOPNOTSUPP;
15553871c387SMichael Stapelberg 
15561e8a655dSPhilippe Reynes 	err = phy_ethtool_set_wol(dev->phydev, wol);
15573871c387SMichael Stapelberg 	/* Given that mv643xx_eth works without the marvell-specific PHY driver,
15583871c387SMichael Stapelberg 	 * this debugging hint is useful to have.
15593871c387SMichael Stapelberg 	 */
15603871c387SMichael Stapelberg 	if (err == -EOPNOTSUPP)
15613871c387SMichael Stapelberg 		netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
15623871c387SMichael Stapelberg 	return err;
15633871c387SMichael Stapelberg }
15643871c387SMichael Stapelberg 
1565527a6266SJeff Kirsher static int
1566a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings(struct net_device *dev,
1567a54e1612SPhilippe Reynes 			       struct ethtool_link_ksettings *cmd)
1568527a6266SJeff Kirsher {
1569527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1570527a6266SJeff Kirsher 
15711e8a655dSPhilippe Reynes 	if (dev->phydev)
1572a54e1612SPhilippe Reynes 		return mv643xx_eth_get_link_ksettings_phy(mp, cmd);
1573527a6266SJeff Kirsher 	else
1574a54e1612SPhilippe Reynes 		return mv643xx_eth_get_link_ksettings_phyless(mp, cmd);
1575527a6266SJeff Kirsher }
1576527a6266SJeff Kirsher 
1577527a6266SJeff Kirsher static int
1578a54e1612SPhilippe Reynes mv643xx_eth_set_link_ksettings(struct net_device *dev,
1579a54e1612SPhilippe Reynes 			       const struct ethtool_link_ksettings *cmd)
1580527a6266SJeff Kirsher {
1581a54e1612SPhilippe Reynes 	struct ethtool_link_ksettings c = *cmd;
1582a54e1612SPhilippe Reynes 	u32 advertising;
1583260055bbSPhil Sutter 	int ret;
1584527a6266SJeff Kirsher 
15851e8a655dSPhilippe Reynes 	if (!dev->phydev)
1586527a6266SJeff Kirsher 		return -EINVAL;
1587527a6266SJeff Kirsher 
1588527a6266SJeff Kirsher 	/*
1589527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1590527a6266SJeff Kirsher 	 */
1591a54e1612SPhilippe Reynes 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
1592a54e1612SPhilippe Reynes 						c.link_modes.advertising);
1593a54e1612SPhilippe Reynes 	advertising &= ~ADVERTISED_1000baseT_Half;
1594a54e1612SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(c.link_modes.advertising,
1595a54e1612SPhilippe Reynes 						advertising);
1596527a6266SJeff Kirsher 
1597a54e1612SPhilippe Reynes 	ret = phy_ethtool_ksettings_set(dev->phydev, &c);
1598260055bbSPhil Sutter 	if (!ret)
15990a9e413bSEzequiel Garcia 		mv643xx_eth_adjust_link(dev);
1600260055bbSPhil Sutter 	return ret;
1601527a6266SJeff Kirsher }
1602527a6266SJeff Kirsher 
1603527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1604527a6266SJeff Kirsher 				    struct ethtool_drvinfo *drvinfo)
1605527a6266SJeff Kirsher {
16066f39da2cSAxel Lin 	strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
16076f39da2cSAxel Lin 		sizeof(drvinfo->driver));
160868aad78cSRick Jones 	strlcpy(drvinfo->version, mv643xx_eth_driver_version,
16096f39da2cSAxel Lin 		sizeof(drvinfo->version));
16106f39da2cSAxel Lin 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
16116f39da2cSAxel Lin 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
1612527a6266SJeff Kirsher }
1613527a6266SJeff Kirsher 
1614f3ccfda1SYufeng Mo static int mv643xx_eth_get_coalesce(struct net_device *dev,
1615f3ccfda1SYufeng Mo 				    struct ethtool_coalesce *ec,
1616f3ccfda1SYufeng Mo 				    struct kernel_ethtool_coalesce *kernel_coal,
1617f3ccfda1SYufeng Mo 				    struct netlink_ext_ack *extack)
1618527a6266SJeff Kirsher {
1619527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1620527a6266SJeff Kirsher 
1621527a6266SJeff Kirsher 	ec->rx_coalesce_usecs = get_rx_coal(mp);
1622527a6266SJeff Kirsher 	ec->tx_coalesce_usecs = get_tx_coal(mp);
1623527a6266SJeff Kirsher 
1624527a6266SJeff Kirsher 	return 0;
1625527a6266SJeff Kirsher }
1626527a6266SJeff Kirsher 
1627f3ccfda1SYufeng Mo static int mv643xx_eth_set_coalesce(struct net_device *dev,
1628f3ccfda1SYufeng Mo 				    struct ethtool_coalesce *ec,
1629f3ccfda1SYufeng Mo 				    struct kernel_ethtool_coalesce *kernel_coal,
1630f3ccfda1SYufeng Mo 				    struct netlink_ext_ack *extack)
1631527a6266SJeff Kirsher {
1632527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1633527a6266SJeff Kirsher 
1634527a6266SJeff Kirsher 	set_rx_coal(mp, ec->rx_coalesce_usecs);
1635527a6266SJeff Kirsher 	set_tx_coal(mp, ec->tx_coalesce_usecs);
1636527a6266SJeff Kirsher 
1637527a6266SJeff Kirsher 	return 0;
1638527a6266SJeff Kirsher }
1639527a6266SJeff Kirsher 
1640527a6266SJeff Kirsher static void
164174624944SHao Chen mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er,
164274624944SHao Chen 			  struct kernel_ethtool_ringparam *kernel_er,
164374624944SHao Chen 			  struct netlink_ext_ack *extack)
1644527a6266SJeff Kirsher {
1645527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1646527a6266SJeff Kirsher 
1647527a6266SJeff Kirsher 	er->rx_max_pending = 4096;
1648527a6266SJeff Kirsher 	er->tx_max_pending = 4096;
1649527a6266SJeff Kirsher 
1650527a6266SJeff Kirsher 	er->rx_pending = mp->rx_ring_size;
1651527a6266SJeff Kirsher 	er->tx_pending = mp->tx_ring_size;
1652527a6266SJeff Kirsher }
1653527a6266SJeff Kirsher 
1654527a6266SJeff Kirsher static int
165574624944SHao Chen mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er,
165674624944SHao Chen 			  struct kernel_ethtool_ringparam *kernel_er,
165774624944SHao Chen 			  struct netlink_ext_ack *extack)
1658527a6266SJeff Kirsher {
1659527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1660527a6266SJeff Kirsher 
1661527a6266SJeff Kirsher 	if (er->rx_mini_pending || er->rx_jumbo_pending)
1662527a6266SJeff Kirsher 		return -EINVAL;
1663527a6266SJeff Kirsher 
1664*2f5e65deSHaowen Bai 	mp->rx_ring_size = min(er->rx_pending, 4096U);
1665ee9e4956SEzequiel Garcia 	mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending,
1666ee9e4956SEzequiel Garcia 				   MV643XX_MAX_SKB_DESCS * 2, 4096);
1667ee9e4956SEzequiel Garcia 	if (mp->tx_ring_size != er->tx_pending)
1668ee9e4956SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
1669ee9e4956SEzequiel Garcia 			    mp->tx_ring_size, er->tx_pending);
1670527a6266SJeff Kirsher 
1671527a6266SJeff Kirsher 	if (netif_running(dev)) {
1672527a6266SJeff Kirsher 		mv643xx_eth_stop(dev);
1673527a6266SJeff Kirsher 		if (mv643xx_eth_open(dev)) {
1674527a6266SJeff Kirsher 			netdev_err(dev,
1675527a6266SJeff Kirsher 				   "fatal error on re-opening device after ring param change\n");
1676527a6266SJeff Kirsher 			return -ENOMEM;
1677527a6266SJeff Kirsher 		}
1678527a6266SJeff Kirsher 	}
1679527a6266SJeff Kirsher 
1680527a6266SJeff Kirsher 	return 0;
1681527a6266SJeff Kirsher }
1682527a6266SJeff Kirsher 
1683527a6266SJeff Kirsher 
1684527a6266SJeff Kirsher static int
1685c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
1686527a6266SJeff Kirsher {
1687527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
16883ad9b358SMichał Mirosław 	bool rx_csum = features & NETIF_F_RXCSUM;
1689527a6266SJeff Kirsher 
1690527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1691527a6266SJeff Kirsher 
1692527a6266SJeff Kirsher 	return 0;
1693527a6266SJeff Kirsher }
1694527a6266SJeff Kirsher 
1695527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev,
1696527a6266SJeff Kirsher 				    uint32_t stringset, uint8_t *data)
1697527a6266SJeff Kirsher {
1698527a6266SJeff Kirsher 	int i;
1699527a6266SJeff Kirsher 
1700527a6266SJeff Kirsher 	if (stringset == ETH_SS_STATS) {
1701527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1702527a6266SJeff Kirsher 			memcpy(data + i * ETH_GSTRING_LEN,
1703527a6266SJeff Kirsher 				mv643xx_eth_stats[i].stat_string,
1704527a6266SJeff Kirsher 				ETH_GSTRING_LEN);
1705527a6266SJeff Kirsher 		}
1706527a6266SJeff Kirsher 	}
1707527a6266SJeff Kirsher }
1708527a6266SJeff Kirsher 
1709527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1710527a6266SJeff Kirsher 					  struct ethtool_stats *stats,
1711527a6266SJeff Kirsher 					  uint64_t *data)
1712527a6266SJeff Kirsher {
1713527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1714527a6266SJeff Kirsher 	int i;
1715527a6266SJeff Kirsher 
1716527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
1717527a6266SJeff Kirsher 	mib_counters_update(mp);
1718527a6266SJeff Kirsher 
1719527a6266SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1720527a6266SJeff Kirsher 		const struct mv643xx_eth_stats *stat;
1721527a6266SJeff Kirsher 		void *p;
1722527a6266SJeff Kirsher 
1723527a6266SJeff Kirsher 		stat = mv643xx_eth_stats + i;
1724527a6266SJeff Kirsher 
1725527a6266SJeff Kirsher 		if (stat->netdev_off >= 0)
1726527a6266SJeff Kirsher 			p = ((void *)mp->dev) + stat->netdev_off;
1727527a6266SJeff Kirsher 		else
1728527a6266SJeff Kirsher 			p = ((void *)mp) + stat->mp_off;
1729527a6266SJeff Kirsher 
1730527a6266SJeff Kirsher 		data[i] = (stat->sizeof_stat == 8) ?
1731527a6266SJeff Kirsher 				*(uint64_t *)p : *(uint32_t *)p;
1732527a6266SJeff Kirsher 	}
1733527a6266SJeff Kirsher }
1734527a6266SJeff Kirsher 
1735527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
1736527a6266SJeff Kirsher {
1737527a6266SJeff Kirsher 	if (sset == ETH_SS_STATS)
1738527a6266SJeff Kirsher 		return ARRAY_SIZE(mv643xx_eth_stats);
1739527a6266SJeff Kirsher 
1740527a6266SJeff Kirsher 	return -EOPNOTSUPP;
1741527a6266SJeff Kirsher }
1742527a6266SJeff Kirsher 
1743527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1744f99db1d4SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
1745527a6266SJeff Kirsher 	.get_drvinfo		= mv643xx_eth_get_drvinfo,
1746fc5e353cSFlorian Fainelli 	.nway_reset		= phy_ethtool_nway_reset,
1747527a6266SJeff Kirsher 	.get_link		= ethtool_op_get_link,
1748527a6266SJeff Kirsher 	.get_coalesce		= mv643xx_eth_get_coalesce,
1749527a6266SJeff Kirsher 	.set_coalesce		= mv643xx_eth_set_coalesce,
1750527a6266SJeff Kirsher 	.get_ringparam		= mv643xx_eth_get_ringparam,
1751527a6266SJeff Kirsher 	.set_ringparam		= mv643xx_eth_set_ringparam,
1752527a6266SJeff Kirsher 	.get_strings		= mv643xx_eth_get_strings,
1753527a6266SJeff Kirsher 	.get_ethtool_stats	= mv643xx_eth_get_ethtool_stats,
1754527a6266SJeff Kirsher 	.get_sset_count		= mv643xx_eth_get_sset_count,
1755ebad0a8dSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
17563871c387SMichael Stapelberg 	.get_wol                = mv643xx_eth_get_wol,
17573871c387SMichael Stapelberg 	.set_wol                = mv643xx_eth_set_wol,
1758a54e1612SPhilippe Reynes 	.get_link_ksettings	= mv643xx_eth_get_link_ksettings,
1759a54e1612SPhilippe Reynes 	.set_link_ksettings	= mv643xx_eth_set_link_ksettings,
1760527a6266SJeff Kirsher };
1761527a6266SJeff Kirsher 
1762527a6266SJeff Kirsher 
1763527a6266SJeff Kirsher /* address handling *********************************************************/
1764527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
1765527a6266SJeff Kirsher {
1766527a6266SJeff Kirsher 	unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1767527a6266SJeff Kirsher 	unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
1768527a6266SJeff Kirsher 
1769527a6266SJeff Kirsher 	addr[0] = (mac_h >> 24) & 0xff;
1770527a6266SJeff Kirsher 	addr[1] = (mac_h >> 16) & 0xff;
1771527a6266SJeff Kirsher 	addr[2] = (mac_h >> 8) & 0xff;
1772527a6266SJeff Kirsher 	addr[3] = mac_h & 0xff;
1773527a6266SJeff Kirsher 	addr[4] = (mac_l >> 8) & 0xff;
1774527a6266SJeff Kirsher 	addr[5] = mac_l & 0xff;
1775527a6266SJeff Kirsher }
1776527a6266SJeff Kirsher 
177776660757SJakub Kicinski static void uc_addr_set(struct mv643xx_eth_private *mp, const u8 *addr)
1778527a6266SJeff Kirsher {
1779527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_HIGH,
1780527a6266SJeff Kirsher 		(addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1781527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
1782527a6266SJeff Kirsher }
1783527a6266SJeff Kirsher 
1784527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev)
1785527a6266SJeff Kirsher {
1786527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1787527a6266SJeff Kirsher 	u32 nibbles;
1788527a6266SJeff Kirsher 
1789527a6266SJeff Kirsher 	if (dev->flags & IFF_PROMISC)
1790527a6266SJeff Kirsher 		return 0;
1791527a6266SJeff Kirsher 
1792527a6266SJeff Kirsher 	nibbles = 1 << (dev->dev_addr[5] & 0x0f);
1793527a6266SJeff Kirsher 	netdev_for_each_uc_addr(ha, dev) {
1794527a6266SJeff Kirsher 		if (memcmp(dev->dev_addr, ha->addr, 5))
1795527a6266SJeff Kirsher 			return 0;
1796527a6266SJeff Kirsher 		if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
1797527a6266SJeff Kirsher 			return 0;
1798527a6266SJeff Kirsher 
1799527a6266SJeff Kirsher 		nibbles |= 1 << (ha->addr[5] & 0x0f);
1800527a6266SJeff Kirsher 	}
1801527a6266SJeff Kirsher 
1802527a6266SJeff Kirsher 	return nibbles;
1803527a6266SJeff Kirsher }
1804527a6266SJeff Kirsher 
1805527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
1806527a6266SJeff Kirsher {
1807527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1808527a6266SJeff Kirsher 	u32 port_config;
1809527a6266SJeff Kirsher 	u32 nibbles;
1810527a6266SJeff Kirsher 	int i;
1811527a6266SJeff Kirsher 
1812527a6266SJeff Kirsher 	uc_addr_set(mp, dev->dev_addr);
1813527a6266SJeff Kirsher 
1814527a6266SJeff Kirsher 	port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1815527a6266SJeff Kirsher 
1816527a6266SJeff Kirsher 	nibbles = uc_addr_filter_mask(dev);
1817527a6266SJeff Kirsher 	if (!nibbles) {
1818527a6266SJeff Kirsher 		port_config |= UNICAST_PROMISCUOUS_MODE;
1819527a6266SJeff Kirsher 		nibbles = 0xffff;
1820527a6266SJeff Kirsher 	}
1821527a6266SJeff Kirsher 
1822527a6266SJeff Kirsher 	for (i = 0; i < 16; i += 4) {
1823527a6266SJeff Kirsher 		int off = UNICAST_TABLE(mp->port_num) + i;
1824527a6266SJeff Kirsher 		u32 v;
1825527a6266SJeff Kirsher 
1826527a6266SJeff Kirsher 		v = 0;
1827527a6266SJeff Kirsher 		if (nibbles & 1)
1828527a6266SJeff Kirsher 			v |= 0x00000001;
1829527a6266SJeff Kirsher 		if (nibbles & 2)
1830527a6266SJeff Kirsher 			v |= 0x00000100;
1831527a6266SJeff Kirsher 		if (nibbles & 4)
1832527a6266SJeff Kirsher 			v |= 0x00010000;
1833527a6266SJeff Kirsher 		if (nibbles & 8)
1834527a6266SJeff Kirsher 			v |= 0x01000000;
1835527a6266SJeff Kirsher 		nibbles >>= 4;
1836527a6266SJeff Kirsher 
1837527a6266SJeff Kirsher 		wrl(mp, off, v);
1838527a6266SJeff Kirsher 	}
1839527a6266SJeff Kirsher 
1840527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, port_config);
1841527a6266SJeff Kirsher }
1842527a6266SJeff Kirsher 
1843527a6266SJeff Kirsher static int addr_crc(unsigned char *addr)
1844527a6266SJeff Kirsher {
1845527a6266SJeff Kirsher 	int crc = 0;
1846527a6266SJeff Kirsher 	int i;
1847527a6266SJeff Kirsher 
1848527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
1849527a6266SJeff Kirsher 		int j;
1850527a6266SJeff Kirsher 
1851527a6266SJeff Kirsher 		crc = (crc ^ addr[i]) << 8;
1852527a6266SJeff Kirsher 		for (j = 7; j >= 0; j--) {
1853527a6266SJeff Kirsher 			if (crc & (0x100 << j))
1854527a6266SJeff Kirsher 				crc ^= 0x107 << j;
1855527a6266SJeff Kirsher 		}
1856527a6266SJeff Kirsher 	}
1857527a6266SJeff Kirsher 
1858527a6266SJeff Kirsher 	return crc;
1859527a6266SJeff Kirsher }
1860527a6266SJeff Kirsher 
1861527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
1862527a6266SJeff Kirsher {
1863527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1864527a6266SJeff Kirsher 	u32 *mc_spec;
1865527a6266SJeff Kirsher 	u32 *mc_other;
1866527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1867527a6266SJeff Kirsher 	int i;
1868527a6266SJeff Kirsher 
18698b711d6dSJoe Perches 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI))
18708b711d6dSJoe Perches 		goto promiscuous;
1871527a6266SJeff Kirsher 
18728b711d6dSJoe Perches 	/* Allocate both mc_spec and mc_other tables */
18738b711d6dSJoe Perches 	mc_spec = kcalloc(128, sizeof(u32), GFP_ATOMIC);
18748b711d6dSJoe Perches 	if (!mc_spec)
18758b711d6dSJoe Perches 		goto promiscuous;
18768b711d6dSJoe Perches 	mc_other = &mc_spec[64];
1877527a6266SJeff Kirsher 
1878527a6266SJeff Kirsher 	netdev_for_each_mc_addr(ha, dev) {
1879527a6266SJeff Kirsher 		u8 *a = ha->addr;
1880527a6266SJeff Kirsher 		u32 *table;
18818b711d6dSJoe Perches 		u8 entry;
1882527a6266SJeff Kirsher 
1883527a6266SJeff Kirsher 		if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1884527a6266SJeff Kirsher 			table = mc_spec;
1885527a6266SJeff Kirsher 			entry = a[5];
1886527a6266SJeff Kirsher 		} else {
1887527a6266SJeff Kirsher 			table = mc_other;
1888527a6266SJeff Kirsher 			entry = addr_crc(a);
1889527a6266SJeff Kirsher 		}
1890527a6266SJeff Kirsher 
1891527a6266SJeff Kirsher 		table[entry >> 2] |= 1 << (8 * (entry & 3));
1892527a6266SJeff Kirsher 	}
1893527a6266SJeff Kirsher 
18948b711d6dSJoe Perches 	for (i = 0; i < 64; i++) {
18958b711d6dSJoe Perches 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
18968b711d6dSJoe Perches 		    mc_spec[i]);
18978b711d6dSJoe Perches 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
18988b711d6dSJoe Perches 		    mc_other[i]);
1899527a6266SJeff Kirsher 	}
1900527a6266SJeff Kirsher 
1901527a6266SJeff Kirsher 	kfree(mc_spec);
19028b711d6dSJoe Perches 	return;
19038b711d6dSJoe Perches 
19048b711d6dSJoe Perches promiscuous:
19058b711d6dSJoe Perches 	for (i = 0; i < 64; i++) {
19068b711d6dSJoe Perches 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
19078b711d6dSJoe Perches 		    0x01010101u);
19088b711d6dSJoe Perches 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
19098b711d6dSJoe Perches 		    0x01010101u);
19108b711d6dSJoe Perches 	}
1911527a6266SJeff Kirsher }
1912527a6266SJeff Kirsher 
1913527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1914527a6266SJeff Kirsher {
1915527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1916527a6266SJeff Kirsher 	mv643xx_eth_program_multicast_filter(dev);
1917527a6266SJeff Kirsher }
1918527a6266SJeff Kirsher 
1919527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1920527a6266SJeff Kirsher {
1921527a6266SJeff Kirsher 	struct sockaddr *sa = addr;
1922527a6266SJeff Kirsher 
1923527a6266SJeff Kirsher 	if (!is_valid_ether_addr(sa->sa_data))
1924504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
1925527a6266SJeff Kirsher 
1926a96d317fSJakub Kicinski 	eth_hw_addr_set(dev, sa->sa_data);
1927527a6266SJeff Kirsher 
1928527a6266SJeff Kirsher 	netif_addr_lock_bh(dev);
1929527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1930527a6266SJeff Kirsher 	netif_addr_unlock_bh(dev);
1931527a6266SJeff Kirsher 
1932527a6266SJeff Kirsher 	return 0;
1933527a6266SJeff Kirsher }
1934527a6266SJeff Kirsher 
1935527a6266SJeff Kirsher 
1936527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/
1937527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index)
1938527a6266SJeff Kirsher {
1939527a6266SJeff Kirsher 	struct rx_queue *rxq = mp->rxq + index;
1940527a6266SJeff Kirsher 	struct rx_desc *rx_desc;
1941527a6266SJeff Kirsher 	int size;
1942527a6266SJeff Kirsher 	int i;
1943527a6266SJeff Kirsher 
1944527a6266SJeff Kirsher 	rxq->index = index;
1945527a6266SJeff Kirsher 
1946527a6266SJeff Kirsher 	rxq->rx_ring_size = mp->rx_ring_size;
1947527a6266SJeff Kirsher 
1948527a6266SJeff Kirsher 	rxq->rx_desc_count = 0;
1949527a6266SJeff Kirsher 	rxq->rx_curr_desc = 0;
1950527a6266SJeff Kirsher 	rxq->rx_used_desc = 0;
1951527a6266SJeff Kirsher 
1952527a6266SJeff Kirsher 	size = rxq->rx_ring_size * sizeof(struct rx_desc);
1953527a6266SJeff Kirsher 
1954527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size) {
1955527a6266SJeff Kirsher 		rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1956527a6266SJeff Kirsher 						mp->rx_desc_sram_size);
1957527a6266SJeff Kirsher 		rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1958527a6266SJeff Kirsher 	} else {
1959527a6266SJeff Kirsher 		rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1960527a6266SJeff Kirsher 						       size, &rxq->rx_desc_dma,
1961527a6266SJeff Kirsher 						       GFP_KERNEL);
1962527a6266SJeff Kirsher 	}
1963527a6266SJeff Kirsher 
1964527a6266SJeff Kirsher 	if (rxq->rx_desc_area == NULL) {
1965527a6266SJeff Kirsher 		netdev_err(mp->dev,
1966527a6266SJeff Kirsher 			   "can't allocate rx ring (%d bytes)\n", size);
1967527a6266SJeff Kirsher 		goto out;
1968527a6266SJeff Kirsher 	}
1969527a6266SJeff Kirsher 	memset(rxq->rx_desc_area, 0, size);
1970527a6266SJeff Kirsher 
1971527a6266SJeff Kirsher 	rxq->rx_desc_area_size = size;
19729fa8e980SLubomir Rintel 	rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1973527a6266SJeff Kirsher 				    GFP_KERNEL);
1974b2adaca9SJoe Perches 	if (rxq->rx_skb == NULL)
1975527a6266SJeff Kirsher 		goto out_free;
1976527a6266SJeff Kirsher 
197764699336SJoe Perches 	rx_desc = rxq->rx_desc_area;
1978527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1979527a6266SJeff Kirsher 		int nexti;
1980527a6266SJeff Kirsher 
1981527a6266SJeff Kirsher 		nexti = i + 1;
1982527a6266SJeff Kirsher 		if (nexti == rxq->rx_ring_size)
1983527a6266SJeff Kirsher 			nexti = 0;
1984527a6266SJeff Kirsher 
1985527a6266SJeff Kirsher 		rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1986527a6266SJeff Kirsher 					nexti * sizeof(struct rx_desc);
1987527a6266SJeff Kirsher 	}
1988527a6266SJeff Kirsher 
1989527a6266SJeff Kirsher 	return 0;
1990527a6266SJeff Kirsher 
1991527a6266SJeff Kirsher 
1992527a6266SJeff Kirsher out_free:
1993527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size)
1994527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1995527a6266SJeff Kirsher 	else
1996527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, size,
1997527a6266SJeff Kirsher 				  rxq->rx_desc_area,
1998527a6266SJeff Kirsher 				  rxq->rx_desc_dma);
1999527a6266SJeff Kirsher 
2000527a6266SJeff Kirsher out:
2001527a6266SJeff Kirsher 	return -ENOMEM;
2002527a6266SJeff Kirsher }
2003527a6266SJeff Kirsher 
2004527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq)
2005527a6266SJeff Kirsher {
2006527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
2007527a6266SJeff Kirsher 	int i;
2008527a6266SJeff Kirsher 
2009527a6266SJeff Kirsher 	rxq_disable(rxq);
2010527a6266SJeff Kirsher 
2011527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
2012527a6266SJeff Kirsher 		if (rxq->rx_skb[i]) {
201343cee2d2SFlorian Fainelli 			dev_consume_skb_any(rxq->rx_skb[i]);
2014527a6266SJeff Kirsher 			rxq->rx_desc_count--;
2015527a6266SJeff Kirsher 		}
2016527a6266SJeff Kirsher 	}
2017527a6266SJeff Kirsher 
2018527a6266SJeff Kirsher 	if (rxq->rx_desc_count) {
2019527a6266SJeff Kirsher 		netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
2020527a6266SJeff Kirsher 			   rxq->rx_desc_count);
2021527a6266SJeff Kirsher 	}
2022527a6266SJeff Kirsher 
2023527a6266SJeff Kirsher 	if (rxq->index == 0 &&
2024527a6266SJeff Kirsher 	    rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
2025527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
2026527a6266SJeff Kirsher 	else
2027527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
2028527a6266SJeff Kirsher 				  rxq->rx_desc_area, rxq->rx_desc_dma);
2029527a6266SJeff Kirsher 
2030527a6266SJeff Kirsher 	kfree(rxq->rx_skb);
2031527a6266SJeff Kirsher }
2032527a6266SJeff Kirsher 
2033527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index)
2034527a6266SJeff Kirsher {
2035527a6266SJeff Kirsher 	struct tx_queue *txq = mp->txq + index;
2036527a6266SJeff Kirsher 	struct tx_desc *tx_desc;
2037527a6266SJeff Kirsher 	int size;
20389e911414SEzequiel Garcia 	int ret;
2039527a6266SJeff Kirsher 	int i;
2040527a6266SJeff Kirsher 
2041527a6266SJeff Kirsher 	txq->index = index;
2042527a6266SJeff Kirsher 
2043527a6266SJeff Kirsher 	txq->tx_ring_size = mp->tx_ring_size;
2044527a6266SJeff Kirsher 
2045ee9e4956SEzequiel Garcia 	/* A queue must always have room for at least one skb.
2046ee9e4956SEzequiel Garcia 	 * Therefore, stop the queue when the free entries reaches
2047ee9e4956SEzequiel Garcia 	 * the maximum number of descriptors per skb.
2048ee9e4956SEzequiel Garcia 	 */
2049ee9e4956SEzequiel Garcia 	txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS;
2050ee9e4956SEzequiel Garcia 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2051ee9e4956SEzequiel Garcia 
2052527a6266SJeff Kirsher 	txq->tx_desc_count = 0;
2053527a6266SJeff Kirsher 	txq->tx_curr_desc = 0;
2054527a6266SJeff Kirsher 	txq->tx_used_desc = 0;
2055527a6266SJeff Kirsher 
2056527a6266SJeff Kirsher 	size = txq->tx_ring_size * sizeof(struct tx_desc);
2057527a6266SJeff Kirsher 
2058527a6266SJeff Kirsher 	if (index == 0 && size <= mp->tx_desc_sram_size) {
2059527a6266SJeff Kirsher 		txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
2060527a6266SJeff Kirsher 						mp->tx_desc_sram_size);
2061527a6266SJeff Kirsher 		txq->tx_desc_dma = mp->tx_desc_sram_addr;
2062527a6266SJeff Kirsher 	} else {
2063527a6266SJeff Kirsher 		txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
2064527a6266SJeff Kirsher 						       size, &txq->tx_desc_dma,
2065527a6266SJeff Kirsher 						       GFP_KERNEL);
2066527a6266SJeff Kirsher 	}
2067527a6266SJeff Kirsher 
2068527a6266SJeff Kirsher 	if (txq->tx_desc_area == NULL) {
2069527a6266SJeff Kirsher 		netdev_err(mp->dev,
2070527a6266SJeff Kirsher 			   "can't allocate tx ring (%d bytes)\n", size);
2071527a6266SJeff Kirsher 		return -ENOMEM;
2072527a6266SJeff Kirsher 	}
2073527a6266SJeff Kirsher 	memset(txq->tx_desc_area, 0, size);
2074527a6266SJeff Kirsher 
2075527a6266SJeff Kirsher 	txq->tx_desc_area_size = size;
2076527a6266SJeff Kirsher 
207764699336SJoe Perches 	tx_desc = txq->tx_desc_area;
2078527a6266SJeff Kirsher 	for (i = 0; i < txq->tx_ring_size; i++) {
2079527a6266SJeff Kirsher 		struct tx_desc *txd = tx_desc + i;
2080527a6266SJeff Kirsher 		int nexti;
2081527a6266SJeff Kirsher 
2082527a6266SJeff Kirsher 		nexti = i + 1;
2083527a6266SJeff Kirsher 		if (nexti == txq->tx_ring_size)
2084527a6266SJeff Kirsher 			nexti = 0;
2085527a6266SJeff Kirsher 
2086527a6266SJeff Kirsher 		txd->cmd_sts = 0;
2087527a6266SJeff Kirsher 		txd->next_desc_ptr = txq->tx_desc_dma +
2088527a6266SJeff Kirsher 					nexti * sizeof(struct tx_desc);
2089527a6266SJeff Kirsher 	}
2090527a6266SJeff Kirsher 
20919e911414SEzequiel Garcia 	txq->tx_desc_mapping = kcalloc(txq->tx_ring_size, sizeof(char),
20929e911414SEzequiel Garcia 				       GFP_KERNEL);
20939e911414SEzequiel Garcia 	if (!txq->tx_desc_mapping) {
20949e911414SEzequiel Garcia 		ret = -ENOMEM;
20959e911414SEzequiel Garcia 		goto err_free_desc_area;
20969e911414SEzequiel Garcia 	}
20979e911414SEzequiel Garcia 
20983ae8f4e0SEzequiel Garcia 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
20993ae8f4e0SEzequiel Garcia 	txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent,
21003ae8f4e0SEzequiel Garcia 					   txq->tx_ring_size * TSO_HEADER_SIZE,
21013ae8f4e0SEzequiel Garcia 					   &txq->tso_hdrs_dma, GFP_KERNEL);
21023ae8f4e0SEzequiel Garcia 	if (txq->tso_hdrs == NULL) {
21039e911414SEzequiel Garcia 		ret = -ENOMEM;
21049e911414SEzequiel Garcia 		goto err_free_desc_mapping;
21053ae8f4e0SEzequiel Garcia 	}
2106527a6266SJeff Kirsher 	skb_queue_head_init(&txq->tx_skb);
2107527a6266SJeff Kirsher 
2108527a6266SJeff Kirsher 	return 0;
21099e911414SEzequiel Garcia 
21109e911414SEzequiel Garcia err_free_desc_mapping:
21119e911414SEzequiel Garcia 	kfree(txq->tx_desc_mapping);
21129e911414SEzequiel Garcia err_free_desc_area:
21139e911414SEzequiel Garcia 	if (index == 0 && size <= mp->tx_desc_sram_size)
21149e911414SEzequiel Garcia 		iounmap(txq->tx_desc_area);
21159e911414SEzequiel Garcia 	else
21169e911414SEzequiel Garcia 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
21179e911414SEzequiel Garcia 				  txq->tx_desc_area, txq->tx_desc_dma);
21189e911414SEzequiel Garcia 	return ret;
2119527a6266SJeff Kirsher }
2120527a6266SJeff Kirsher 
2121527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq)
2122527a6266SJeff Kirsher {
2123527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
2124527a6266SJeff Kirsher 
2125527a6266SJeff Kirsher 	txq_disable(txq);
2126527a6266SJeff Kirsher 	txq_reclaim(txq, txq->tx_ring_size, 1);
2127527a6266SJeff Kirsher 
2128527a6266SJeff Kirsher 	BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
2129527a6266SJeff Kirsher 
2130527a6266SJeff Kirsher 	if (txq->index == 0 &&
2131527a6266SJeff Kirsher 	    txq->tx_desc_area_size <= mp->tx_desc_sram_size)
2132527a6266SJeff Kirsher 		iounmap(txq->tx_desc_area);
2133527a6266SJeff Kirsher 	else
2134527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
2135527a6266SJeff Kirsher 				  txq->tx_desc_area, txq->tx_desc_dma);
21369e911414SEzequiel Garcia 	kfree(txq->tx_desc_mapping);
21379e911414SEzequiel Garcia 
21383ae8f4e0SEzequiel Garcia 	if (txq->tso_hdrs)
21393ae8f4e0SEzequiel Garcia 		dma_free_coherent(mp->dev->dev.parent,
21403ae8f4e0SEzequiel Garcia 				  txq->tx_ring_size * TSO_HEADER_SIZE,
21413ae8f4e0SEzequiel Garcia 				  txq->tso_hdrs, txq->tso_hdrs_dma);
2142527a6266SJeff Kirsher }
2143527a6266SJeff Kirsher 
2144527a6266SJeff Kirsher 
2145527a6266SJeff Kirsher /* netdev ops and related ***************************************************/
2146527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
2147527a6266SJeff Kirsher {
2148527a6266SJeff Kirsher 	u32 int_cause;
2149527a6266SJeff Kirsher 	u32 int_cause_ext;
2150527a6266SJeff Kirsher 
2151527a6266SJeff Kirsher 	int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
2152527a6266SJeff Kirsher 	if (int_cause == 0)
2153527a6266SJeff Kirsher 		return 0;
2154527a6266SJeff Kirsher 
2155527a6266SJeff Kirsher 	int_cause_ext = 0;
2156527a6266SJeff Kirsher 	if (int_cause & INT_EXT) {
2157527a6266SJeff Kirsher 		int_cause &= ~INT_EXT;
2158527a6266SJeff Kirsher 		int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
2159527a6266SJeff Kirsher 	}
2160527a6266SJeff Kirsher 
2161527a6266SJeff Kirsher 	if (int_cause) {
2162527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE, ~int_cause);
2163527a6266SJeff Kirsher 		mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
2164527a6266SJeff Kirsher 				~(rdlp(mp, TXQ_COMMAND) & 0xff);
2165527a6266SJeff Kirsher 		mp->work_rx |= (int_cause & INT_RX) >> 2;
2166527a6266SJeff Kirsher 	}
2167527a6266SJeff Kirsher 
2168527a6266SJeff Kirsher 	int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
2169527a6266SJeff Kirsher 	if (int_cause_ext) {
2170527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
2171527a6266SJeff Kirsher 		if (int_cause_ext & INT_EXT_LINK_PHY)
2172527a6266SJeff Kirsher 			mp->work_link = 1;
2173527a6266SJeff Kirsher 		mp->work_tx |= int_cause_ext & INT_EXT_TX;
2174527a6266SJeff Kirsher 	}
2175527a6266SJeff Kirsher 
2176527a6266SJeff Kirsher 	return 1;
2177527a6266SJeff Kirsher }
2178527a6266SJeff Kirsher 
2179527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
2180527a6266SJeff Kirsher {
2181527a6266SJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_id;
2182527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2183527a6266SJeff Kirsher 
2184527a6266SJeff Kirsher 	if (unlikely(!mv643xx_eth_collect_events(mp)))
2185527a6266SJeff Kirsher 		return IRQ_NONE;
2186527a6266SJeff Kirsher 
2187527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
2188527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2189527a6266SJeff Kirsher 
2190527a6266SJeff Kirsher 	return IRQ_HANDLED;
2191527a6266SJeff Kirsher }
2192527a6266SJeff Kirsher 
2193527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp)
2194527a6266SJeff Kirsher {
2195527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2196527a6266SJeff Kirsher 	u32 port_status;
2197527a6266SJeff Kirsher 	int speed;
2198527a6266SJeff Kirsher 	int duplex;
2199527a6266SJeff Kirsher 	int fc;
2200527a6266SJeff Kirsher 
2201527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
2202527a6266SJeff Kirsher 	if (!(port_status & LINK_UP)) {
2203527a6266SJeff Kirsher 		if (netif_carrier_ok(dev)) {
2204527a6266SJeff Kirsher 			int i;
2205527a6266SJeff Kirsher 
2206527a6266SJeff Kirsher 			netdev_info(dev, "link down\n");
2207527a6266SJeff Kirsher 
2208527a6266SJeff Kirsher 			netif_carrier_off(dev);
2209527a6266SJeff Kirsher 
2210527a6266SJeff Kirsher 			for (i = 0; i < mp->txq_count; i++) {
2211527a6266SJeff Kirsher 				struct tx_queue *txq = mp->txq + i;
2212527a6266SJeff Kirsher 
2213527a6266SJeff Kirsher 				txq_reclaim(txq, txq->tx_ring_size, 1);
2214527a6266SJeff Kirsher 				txq_reset_hw_ptr(txq);
2215527a6266SJeff Kirsher 			}
2216527a6266SJeff Kirsher 		}
2217527a6266SJeff Kirsher 		return;
2218527a6266SJeff Kirsher 	}
2219527a6266SJeff Kirsher 
2220527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
2221527a6266SJeff Kirsher 	case PORT_SPEED_10:
2222527a6266SJeff Kirsher 		speed = 10;
2223527a6266SJeff Kirsher 		break;
2224527a6266SJeff Kirsher 	case PORT_SPEED_100:
2225527a6266SJeff Kirsher 		speed = 100;
2226527a6266SJeff Kirsher 		break;
2227527a6266SJeff Kirsher 	case PORT_SPEED_1000:
2228527a6266SJeff Kirsher 		speed = 1000;
2229527a6266SJeff Kirsher 		break;
2230527a6266SJeff Kirsher 	default:
2231527a6266SJeff Kirsher 		speed = -1;
2232527a6266SJeff Kirsher 		break;
2233527a6266SJeff Kirsher 	}
2234527a6266SJeff Kirsher 	duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
2235527a6266SJeff Kirsher 	fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
2236527a6266SJeff Kirsher 
2237527a6266SJeff Kirsher 	netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
2238527a6266SJeff Kirsher 		    speed, duplex ? "full" : "half", fc ? "en" : "dis");
2239527a6266SJeff Kirsher 
2240527a6266SJeff Kirsher 	if (!netif_carrier_ok(dev))
2241527a6266SJeff Kirsher 		netif_carrier_on(dev);
2242527a6266SJeff Kirsher }
2243527a6266SJeff Kirsher 
2244527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
2245527a6266SJeff Kirsher {
2246527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2247527a6266SJeff Kirsher 	int work_done;
2248527a6266SJeff Kirsher 
2249527a6266SJeff Kirsher 	mp = container_of(napi, struct mv643xx_eth_private, napi);
2250527a6266SJeff Kirsher 
2251527a6266SJeff Kirsher 	if (unlikely(mp->oom)) {
2252527a6266SJeff Kirsher 		mp->oom = 0;
2253527a6266SJeff Kirsher 		del_timer(&mp->rx_oom);
2254527a6266SJeff Kirsher 	}
2255527a6266SJeff Kirsher 
2256527a6266SJeff Kirsher 	work_done = 0;
2257527a6266SJeff Kirsher 	while (work_done < budget) {
2258527a6266SJeff Kirsher 		u8 queue_mask;
2259527a6266SJeff Kirsher 		int queue;
2260527a6266SJeff Kirsher 		int work_tbd;
2261527a6266SJeff Kirsher 
2262527a6266SJeff Kirsher 		if (mp->work_link) {
2263527a6266SJeff Kirsher 			mp->work_link = 0;
2264527a6266SJeff Kirsher 			handle_link_event(mp);
2265527a6266SJeff Kirsher 			work_done++;
2266527a6266SJeff Kirsher 			continue;
2267527a6266SJeff Kirsher 		}
2268527a6266SJeff Kirsher 
2269527a6266SJeff Kirsher 		queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2270527a6266SJeff Kirsher 		if (likely(!mp->oom))
2271527a6266SJeff Kirsher 			queue_mask |= mp->work_rx_refill;
2272527a6266SJeff Kirsher 
2273527a6266SJeff Kirsher 		if (!queue_mask) {
2274527a6266SJeff Kirsher 			if (mv643xx_eth_collect_events(mp))
2275527a6266SJeff Kirsher 				continue;
2276527a6266SJeff Kirsher 			break;
2277527a6266SJeff Kirsher 		}
2278527a6266SJeff Kirsher 
2279527a6266SJeff Kirsher 		queue = fls(queue_mask) - 1;
2280527a6266SJeff Kirsher 		queue_mask = 1 << queue;
2281527a6266SJeff Kirsher 
2282527a6266SJeff Kirsher 		work_tbd = budget - work_done;
2283527a6266SJeff Kirsher 		if (work_tbd > 16)
2284527a6266SJeff Kirsher 			work_tbd = 16;
2285527a6266SJeff Kirsher 
2286527a6266SJeff Kirsher 		if (mp->work_tx_end & queue_mask) {
2287527a6266SJeff Kirsher 			txq_kick(mp->txq + queue);
2288527a6266SJeff Kirsher 		} else if (mp->work_tx & queue_mask) {
2289527a6266SJeff Kirsher 			work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2290527a6266SJeff Kirsher 			txq_maybe_wake(mp->txq + queue);
2291527a6266SJeff Kirsher 		} else if (mp->work_rx & queue_mask) {
2292527a6266SJeff Kirsher 			work_done += rxq_process(mp->rxq + queue, work_tbd);
2293527a6266SJeff Kirsher 		} else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
2294527a6266SJeff Kirsher 			work_done += rxq_refill(mp->rxq + queue, work_tbd);
2295527a6266SJeff Kirsher 		} else {
2296527a6266SJeff Kirsher 			BUG();
2297527a6266SJeff Kirsher 		}
2298527a6266SJeff Kirsher 	}
2299527a6266SJeff Kirsher 
2300527a6266SJeff Kirsher 	if (work_done < budget) {
2301527a6266SJeff Kirsher 		if (mp->oom)
2302527a6266SJeff Kirsher 			mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
23036ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
2304527a6266SJeff Kirsher 		wrlp(mp, INT_MASK, mp->int_mask);
2305527a6266SJeff Kirsher 	}
2306527a6266SJeff Kirsher 
2307527a6266SJeff Kirsher 	return work_done;
2308527a6266SJeff Kirsher }
2309527a6266SJeff Kirsher 
2310e99e88a9SKees Cook static inline void oom_timer_wrapper(struct timer_list *t)
2311527a6266SJeff Kirsher {
2312e99e88a9SKees Cook 	struct mv643xx_eth_private *mp = from_timer(mp, t, rx_oom);
2313527a6266SJeff Kirsher 
2314527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2315527a6266SJeff Kirsher }
2316527a6266SJeff Kirsher 
2317527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp)
2318527a6266SJeff Kirsher {
23191e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
2320527a6266SJeff Kirsher 	u32 pscr;
2321527a6266SJeff Kirsher 	int i;
2322527a6266SJeff Kirsher 
2323527a6266SJeff Kirsher 	/*
2324527a6266SJeff Kirsher 	 * Perform PHY reset, if there is a PHY.
2325527a6266SJeff Kirsher 	 */
23261e8a655dSPhilippe Reynes 	if (dev->phydev) {
2327a54e1612SPhilippe Reynes 		struct ethtool_link_ksettings cmd;
2328527a6266SJeff Kirsher 
2329a54e1612SPhilippe Reynes 		mv643xx_eth_get_link_ksettings(dev, &cmd);
23301e8a655dSPhilippe Reynes 		phy_init_hw(dev->phydev);
2331a54e1612SPhilippe Reynes 		mv643xx_eth_set_link_ksettings(
2332a54e1612SPhilippe Reynes 			dev, (const struct ethtool_link_ksettings *)&cmd);
23331e8a655dSPhilippe Reynes 		phy_start(dev->phydev);
2334527a6266SJeff Kirsher 	}
2335527a6266SJeff Kirsher 
2336527a6266SJeff Kirsher 	/*
2337527a6266SJeff Kirsher 	 * Configure basic link parameters.
2338527a6266SJeff Kirsher 	 */
2339527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2340527a6266SJeff Kirsher 
2341527a6266SJeff Kirsher 	pscr |= SERIAL_PORT_ENABLE;
2342527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2343527a6266SJeff Kirsher 
2344527a6266SJeff Kirsher 	pscr |= DO_NOT_FORCE_LINK_FAIL;
23451e8a655dSPhilippe Reynes 	if (!dev->phydev)
2346527a6266SJeff Kirsher 		pscr |= FORCE_LINK_PASS;
2347527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2348527a6266SJeff Kirsher 
2349527a6266SJeff Kirsher 	/*
2350527a6266SJeff Kirsher 	 * Configure TX path and queues.
2351527a6266SJeff Kirsher 	 */
2352527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2353527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2354527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
2355527a6266SJeff Kirsher 
2356527a6266SJeff Kirsher 		txq_reset_hw_ptr(txq);
2357527a6266SJeff Kirsher 		txq_set_rate(txq, 1000000000, 16777216);
2358527a6266SJeff Kirsher 		txq_set_fixed_prio_mode(txq);
2359527a6266SJeff Kirsher 	}
2360527a6266SJeff Kirsher 
2361527a6266SJeff Kirsher 	/*
2362527a6266SJeff Kirsher 	 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2363527a6266SJeff Kirsher 	 * frames to RX queue #0, and include the pseudo-header when
2364527a6266SJeff Kirsher 	 * calculating receive checksums.
2365527a6266SJeff Kirsher 	 */
2366527a6266SJeff Kirsher 	mv643xx_eth_set_features(mp->dev, mp->dev->features);
2367527a6266SJeff Kirsher 
2368527a6266SJeff Kirsher 	/*
2369527a6266SJeff Kirsher 	 * Treat BPDUs as normal multicasts, and disable partition mode.
2370527a6266SJeff Kirsher 	 */
2371527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
2372527a6266SJeff Kirsher 
2373527a6266SJeff Kirsher 	/*
2374527a6266SJeff Kirsher 	 * Add configured unicast addresses to address filter table.
2375527a6266SJeff Kirsher 	 */
2376527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(mp->dev);
2377527a6266SJeff Kirsher 
2378527a6266SJeff Kirsher 	/*
2379527a6266SJeff Kirsher 	 * Enable the receive queues.
2380527a6266SJeff Kirsher 	 */
2381527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2382527a6266SJeff Kirsher 		struct rx_queue *rxq = mp->rxq + i;
2383527a6266SJeff Kirsher 		u32 addr;
2384527a6266SJeff Kirsher 
2385527a6266SJeff Kirsher 		addr = (u32)rxq->rx_desc_dma;
2386527a6266SJeff Kirsher 		addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
2387527a6266SJeff Kirsher 		wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
2388527a6266SJeff Kirsher 
2389527a6266SJeff Kirsher 		rxq_enable(rxq);
2390527a6266SJeff Kirsher 	}
2391527a6266SJeff Kirsher }
2392527a6266SJeff Kirsher 
2393527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2394527a6266SJeff Kirsher {
2395527a6266SJeff Kirsher 	int skb_size;
2396527a6266SJeff Kirsher 
2397527a6266SJeff Kirsher 	/*
2398527a6266SJeff Kirsher 	 * Reserve 2+14 bytes for an ethernet header (the hardware
2399527a6266SJeff Kirsher 	 * automatically prepends 2 bytes of dummy data to each
2400527a6266SJeff Kirsher 	 * received packet), 16 bytes for up to four VLAN tags, and
2401527a6266SJeff Kirsher 	 * 4 bytes for the trailing FCS -- 36 bytes total.
2402527a6266SJeff Kirsher 	 */
2403527a6266SJeff Kirsher 	skb_size = mp->dev->mtu + 36;
2404527a6266SJeff Kirsher 
2405527a6266SJeff Kirsher 	/*
2406527a6266SJeff Kirsher 	 * Make sure that the skb size is a multiple of 8 bytes, as
2407527a6266SJeff Kirsher 	 * the lower three bits of the receive descriptor's buffer
2408527a6266SJeff Kirsher 	 * size field are ignored by the hardware.
2409527a6266SJeff Kirsher 	 */
2410527a6266SJeff Kirsher 	mp->skb_size = (skb_size + 7) & ~7;
2411527a6266SJeff Kirsher 
2412527a6266SJeff Kirsher 	/*
2413527a6266SJeff Kirsher 	 * If NET_SKB_PAD is smaller than a cache line,
2414527a6266SJeff Kirsher 	 * netdev_alloc_skb() will cause skb->data to be misaligned
2415527a6266SJeff Kirsher 	 * to a cache line boundary.  If this is the case, include
2416527a6266SJeff Kirsher 	 * some extra space to allow re-aligning the data area.
2417527a6266SJeff Kirsher 	 */
2418527a6266SJeff Kirsher 	mp->skb_size += SKB_DMA_REALIGN;
2419527a6266SJeff Kirsher }
2420527a6266SJeff Kirsher 
2421527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev)
2422527a6266SJeff Kirsher {
2423527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2424527a6266SJeff Kirsher 	int err;
2425527a6266SJeff Kirsher 	int i;
2426527a6266SJeff Kirsher 
2427527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE, 0);
2428527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE_EXT, 0);
2429527a6266SJeff Kirsher 	rdlp(mp, INT_CAUSE_EXT);
2430527a6266SJeff Kirsher 
2431527a6266SJeff Kirsher 	err = request_irq(dev->irq, mv643xx_eth_irq,
2432527a6266SJeff Kirsher 			  IRQF_SHARED, dev->name, dev);
2433527a6266SJeff Kirsher 	if (err) {
2434527a6266SJeff Kirsher 		netdev_err(dev, "can't assign irq\n");
2435527a6266SJeff Kirsher 		return -EAGAIN;
2436527a6266SJeff Kirsher 	}
2437527a6266SJeff Kirsher 
2438527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2439527a6266SJeff Kirsher 
2440527a6266SJeff Kirsher 	napi_enable(&mp->napi);
2441527a6266SJeff Kirsher 
2442527a6266SJeff Kirsher 	mp->int_mask = INT_EXT;
2443527a6266SJeff Kirsher 
2444527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2445527a6266SJeff Kirsher 		err = rxq_init(mp, i);
2446527a6266SJeff Kirsher 		if (err) {
2447527a6266SJeff Kirsher 			while (--i >= 0)
2448527a6266SJeff Kirsher 				rxq_deinit(mp->rxq + i);
2449527a6266SJeff Kirsher 			goto out;
2450527a6266SJeff Kirsher 		}
2451527a6266SJeff Kirsher 
2452527a6266SJeff Kirsher 		rxq_refill(mp->rxq + i, INT_MAX);
2453527a6266SJeff Kirsher 		mp->int_mask |= INT_RX_0 << i;
2454527a6266SJeff Kirsher 	}
2455527a6266SJeff Kirsher 
2456527a6266SJeff Kirsher 	if (mp->oom) {
2457527a6266SJeff Kirsher 		mp->rx_oom.expires = jiffies + (HZ / 10);
2458527a6266SJeff Kirsher 		add_timer(&mp->rx_oom);
2459527a6266SJeff Kirsher 	}
2460527a6266SJeff Kirsher 
2461527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2462527a6266SJeff Kirsher 		err = txq_init(mp, i);
2463527a6266SJeff Kirsher 		if (err) {
2464527a6266SJeff Kirsher 			while (--i >= 0)
2465527a6266SJeff Kirsher 				txq_deinit(mp->txq + i);
2466527a6266SJeff Kirsher 			goto out_free;
2467527a6266SJeff Kirsher 		}
2468527a6266SJeff Kirsher 		mp->int_mask |= INT_TX_END_0 << i;
2469527a6266SJeff Kirsher 	}
2470527a6266SJeff Kirsher 
2471f564412cSSebastian Hesselbarth 	add_timer(&mp->mib_counters_timer);
2472527a6266SJeff Kirsher 	port_start(mp);
2473527a6266SJeff Kirsher 
2474527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
2475527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2476527a6266SJeff Kirsher 
2477527a6266SJeff Kirsher 	return 0;
2478527a6266SJeff Kirsher 
2479527a6266SJeff Kirsher 
2480527a6266SJeff Kirsher out_free:
2481527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2482527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2483527a6266SJeff Kirsher out:
2484527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2485527a6266SJeff Kirsher 
2486527a6266SJeff Kirsher 	return err;
2487527a6266SJeff Kirsher }
2488527a6266SJeff Kirsher 
2489527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp)
2490527a6266SJeff Kirsher {
2491527a6266SJeff Kirsher 	unsigned int data;
2492527a6266SJeff Kirsher 	int i;
2493527a6266SJeff Kirsher 
2494527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2495527a6266SJeff Kirsher 		rxq_disable(mp->rxq + i);
2496527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2497527a6266SJeff Kirsher 		txq_disable(mp->txq + i);
2498527a6266SJeff Kirsher 
2499527a6266SJeff Kirsher 	while (1) {
2500527a6266SJeff Kirsher 		u32 ps = rdlp(mp, PORT_STATUS);
2501527a6266SJeff Kirsher 
2502527a6266SJeff Kirsher 		if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2503527a6266SJeff Kirsher 			break;
2504527a6266SJeff Kirsher 		udelay(10);
2505527a6266SJeff Kirsher 	}
2506527a6266SJeff Kirsher 
2507527a6266SJeff Kirsher 	/* Reset the Enable bit in the Configuration Register */
2508527a6266SJeff Kirsher 	data = rdlp(mp, PORT_SERIAL_CONTROL);
2509527a6266SJeff Kirsher 	data &= ~(SERIAL_PORT_ENABLE		|
2510527a6266SJeff Kirsher 		  DO_NOT_FORCE_LINK_FAIL	|
2511527a6266SJeff Kirsher 		  FORCE_LINK_PASS);
2512527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, data);
2513527a6266SJeff Kirsher }
2514527a6266SJeff Kirsher 
2515527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev)
2516527a6266SJeff Kirsher {
2517527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2518527a6266SJeff Kirsher 	int i;
2519527a6266SJeff Kirsher 
2520527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, 0x00000000);
2521527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2522527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2523527a6266SJeff Kirsher 
2524527a6266SJeff Kirsher 	napi_disable(&mp->napi);
2525527a6266SJeff Kirsher 
2526527a6266SJeff Kirsher 	del_timer_sync(&mp->rx_oom);
2527527a6266SJeff Kirsher 
2528527a6266SJeff Kirsher 	netif_carrier_off(dev);
25291e8a655dSPhilippe Reynes 	if (dev->phydev)
25301e8a655dSPhilippe Reynes 		phy_stop(dev->phydev);
2531527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2532527a6266SJeff Kirsher 
2533527a6266SJeff Kirsher 	port_reset(mp);
2534527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
2535527a6266SJeff Kirsher 	mib_counters_update(mp);
2536527a6266SJeff Kirsher 	del_timer_sync(&mp->mib_counters_timer);
2537527a6266SJeff Kirsher 
2538527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2539527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2540527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2541527a6266SJeff Kirsher 		txq_deinit(mp->txq + i);
2542527a6266SJeff Kirsher 
2543527a6266SJeff Kirsher 	return 0;
2544527a6266SJeff Kirsher }
2545527a6266SJeff Kirsher 
2546527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2547527a6266SJeff Kirsher {
2548260055bbSPhil Sutter 	int ret;
2549527a6266SJeff Kirsher 
25501e8a655dSPhilippe Reynes 	if (!dev->phydev)
2551260055bbSPhil Sutter 		return -ENOTSUPP;
2552527a6266SJeff Kirsher 
25531e8a655dSPhilippe Reynes 	ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
2554260055bbSPhil Sutter 	if (!ret)
25550a9e413bSEzequiel Garcia 		mv643xx_eth_adjust_link(dev);
2556260055bbSPhil Sutter 	return ret;
2557527a6266SJeff Kirsher }
2558527a6266SJeff Kirsher 
2559527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2560527a6266SJeff Kirsher {
2561527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2562527a6266SJeff Kirsher 
2563527a6266SJeff Kirsher 	dev->mtu = new_mtu;
2564527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2565527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2566527a6266SJeff Kirsher 
2567527a6266SJeff Kirsher 	if (!netif_running(dev))
2568527a6266SJeff Kirsher 		return 0;
2569527a6266SJeff Kirsher 
2570527a6266SJeff Kirsher 	/*
2571527a6266SJeff Kirsher 	 * Stop and then re-open the interface. This will allocate RX
2572527a6266SJeff Kirsher 	 * skbs of the new MTU.
2573527a6266SJeff Kirsher 	 * There is a possible danger that the open will not succeed,
2574527a6266SJeff Kirsher 	 * due to memory being full.
2575527a6266SJeff Kirsher 	 */
2576527a6266SJeff Kirsher 	mv643xx_eth_stop(dev);
2577527a6266SJeff Kirsher 	if (mv643xx_eth_open(dev)) {
2578527a6266SJeff Kirsher 		netdev_err(dev,
2579527a6266SJeff Kirsher 			   "fatal error on re-opening device after MTU change\n");
2580527a6266SJeff Kirsher 	}
2581527a6266SJeff Kirsher 
2582527a6266SJeff Kirsher 	return 0;
2583527a6266SJeff Kirsher }
2584527a6266SJeff Kirsher 
2585527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly)
2586527a6266SJeff Kirsher {
2587527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2588527a6266SJeff Kirsher 
2589527a6266SJeff Kirsher 	mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2590527a6266SJeff Kirsher 	if (netif_running(mp->dev)) {
2591527a6266SJeff Kirsher 		netif_tx_stop_all_queues(mp->dev);
2592527a6266SJeff Kirsher 		port_reset(mp);
2593527a6266SJeff Kirsher 		port_start(mp);
2594527a6266SJeff Kirsher 		netif_tx_wake_all_queues(mp->dev);
2595527a6266SJeff Kirsher 	}
2596527a6266SJeff Kirsher }
2597527a6266SJeff Kirsher 
25980290bd29SMichael S. Tsirkin static void mv643xx_eth_tx_timeout(struct net_device *dev, unsigned int txqueue)
2599527a6266SJeff Kirsher {
2600527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2601527a6266SJeff Kirsher 
2602527a6266SJeff Kirsher 	netdev_info(dev, "tx timeout\n");
2603527a6266SJeff Kirsher 
2604527a6266SJeff Kirsher 	schedule_work(&mp->tx_timeout_task);
2605527a6266SJeff Kirsher }
2606527a6266SJeff Kirsher 
2607527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2608527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev)
2609527a6266SJeff Kirsher {
2610527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2611527a6266SJeff Kirsher 
2612527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2613527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2614527a6266SJeff Kirsher 
2615527a6266SJeff Kirsher 	mv643xx_eth_irq(dev->irq, dev);
2616527a6266SJeff Kirsher 
2617527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2618527a6266SJeff Kirsher }
2619527a6266SJeff Kirsher #endif
2620527a6266SJeff Kirsher 
2621527a6266SJeff Kirsher 
2622527a6266SJeff Kirsher /* platform glue ************************************************************/
2623527a6266SJeff Kirsher static void
2624527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
262563a9332bSAndrew Lunn 			      const struct mbus_dram_target_info *dram)
2626527a6266SJeff Kirsher {
2627527a6266SJeff Kirsher 	void __iomem *base = msp->base;
2628527a6266SJeff Kirsher 	u32 win_enable;
2629527a6266SJeff Kirsher 	u32 win_protect;
2630527a6266SJeff Kirsher 	int i;
2631527a6266SJeff Kirsher 
2632527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
2633527a6266SJeff Kirsher 		writel(0, base + WINDOW_BASE(i));
2634527a6266SJeff Kirsher 		writel(0, base + WINDOW_SIZE(i));
2635527a6266SJeff Kirsher 		if (i < 4)
2636527a6266SJeff Kirsher 			writel(0, base + WINDOW_REMAP_HIGH(i));
2637527a6266SJeff Kirsher 	}
2638527a6266SJeff Kirsher 
2639527a6266SJeff Kirsher 	win_enable = 0x3f;
2640527a6266SJeff Kirsher 	win_protect = 0;
2641527a6266SJeff Kirsher 
2642527a6266SJeff Kirsher 	for (i = 0; i < dram->num_cs; i++) {
264363a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
2644527a6266SJeff Kirsher 
2645527a6266SJeff Kirsher 		writel((cs->base & 0xffff0000) |
2646527a6266SJeff Kirsher 			(cs->mbus_attr << 8) |
2647527a6266SJeff Kirsher 			dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2648527a6266SJeff Kirsher 		writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2649527a6266SJeff Kirsher 
2650527a6266SJeff Kirsher 		win_enable &= ~(1 << i);
2651527a6266SJeff Kirsher 		win_protect |= 3 << (2 * i);
2652527a6266SJeff Kirsher 	}
2653527a6266SJeff Kirsher 
2654527a6266SJeff Kirsher 	writel(win_enable, base + WINDOW_BAR_ENABLE);
2655527a6266SJeff Kirsher 	msp->win_protect = win_protect;
2656527a6266SJeff Kirsher }
2657527a6266SJeff Kirsher 
2658527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2659527a6266SJeff Kirsher {
2660527a6266SJeff Kirsher 	/*
2661527a6266SJeff Kirsher 	 * Check whether we have a 14-bit coal limit field in bits
2662527a6266SJeff Kirsher 	 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2663527a6266SJeff Kirsher 	 * SDMA config register.
2664527a6266SJeff Kirsher 	 */
2665527a6266SJeff Kirsher 	writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2666527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
2667527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 1;
2668527a6266SJeff Kirsher 	else
2669527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 0;
2670527a6266SJeff Kirsher 
2671527a6266SJeff Kirsher 	/*
2672527a6266SJeff Kirsher 	 * Check whether the MAC supports TX rate control, and if
2673527a6266SJeff Kirsher 	 * yes, whether its associated registers are in the old or
2674527a6266SJeff Kirsher 	 * the new place.
2675527a6266SJeff Kirsher 	 */
2676527a6266SJeff Kirsher 	writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2677527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
2678527a6266SJeff Kirsher 		msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2679527a6266SJeff Kirsher 	} else {
2680527a6266SJeff Kirsher 		writel(7, msp->base + 0x0400 + TX_BW_RATE);
2681527a6266SJeff Kirsher 		if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
2682527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2683527a6266SJeff Kirsher 		else
2684527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2685527a6266SJeff Kirsher 	}
2686527a6266SJeff Kirsher }
2687527a6266SJeff Kirsher 
268876723bcaSSebastian Hesselbarth #if defined(CONFIG_OF)
268976723bcaSSebastian Hesselbarth static const struct of_device_id mv643xx_eth_shared_ids[] = {
269076723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,orion-eth", },
269176723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,kirkwood-eth", },
269276723bcaSSebastian Hesselbarth 	{ }
269376723bcaSSebastian Hesselbarth };
269476723bcaSSebastian Hesselbarth MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids);
269576723bcaSSebastian Hesselbarth #endif
269676723bcaSSebastian Hesselbarth 
2697600cc3c9SChristophe Leroy #ifdef CONFIG_OF_IRQ
269876723bcaSSebastian Hesselbarth #define mv643xx_eth_property(_np, _name, _v)				\
269976723bcaSSebastian Hesselbarth 	do {								\
270076723bcaSSebastian Hesselbarth 		u32 tmp;						\
270176723bcaSSebastian Hesselbarth 		if (!of_property_read_u32(_np, "marvell," _name, &tmp))	\
270276723bcaSSebastian Hesselbarth 			_v = tmp;					\
270376723bcaSSebastian Hesselbarth 	} while (0)
270476723bcaSSebastian Hesselbarth 
270576723bcaSSebastian Hesselbarth static struct platform_device *port_platdev[3];
270676723bcaSSebastian Hesselbarth 
270742404d8fSMauri Sandberg static void mv643xx_eth_shared_of_remove(void)
270842404d8fSMauri Sandberg {
270942404d8fSMauri Sandberg 	int n;
271042404d8fSMauri Sandberg 
271142404d8fSMauri Sandberg 	for (n = 0; n < 3; n++) {
271242404d8fSMauri Sandberg 		platform_device_del(port_platdev[n]);
271342404d8fSMauri Sandberg 		port_platdev[n] = NULL;
271442404d8fSMauri Sandberg 	}
271542404d8fSMauri Sandberg }
271642404d8fSMauri Sandberg 
271776723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
271876723bcaSSebastian Hesselbarth 					  struct device_node *pnp)
271976723bcaSSebastian Hesselbarth {
272076723bcaSSebastian Hesselbarth 	struct platform_device *ppdev;
272176723bcaSSebastian Hesselbarth 	struct mv643xx_eth_platform_data ppd;
272276723bcaSSebastian Hesselbarth 	struct resource res;
272376723bcaSSebastian Hesselbarth 	int ret;
2724785bf6f7SJonas Gorski 	int dev_num = 0;
272576723bcaSSebastian Hesselbarth 
272676723bcaSSebastian Hesselbarth 	memset(&ppd, 0, sizeof(ppd));
272776723bcaSSebastian Hesselbarth 	ppd.shared = pdev;
272876723bcaSSebastian Hesselbarth 
272976723bcaSSebastian Hesselbarth 	memset(&res, 0, sizeof(res));
2730cfbcb61fSSergei Shtylyov 	if (of_irq_to_resource(pnp, 0, &res) <= 0) {
273121c328dcSRob Herring 		dev_err(&pdev->dev, "missing interrupt on %pOFn\n", pnp);
273276723bcaSSebastian Hesselbarth 		return -EINVAL;
273376723bcaSSebastian Hesselbarth 	}
273476723bcaSSebastian Hesselbarth 
273576723bcaSSebastian Hesselbarth 	if (of_property_read_u32(pnp, "reg", &ppd.port_number)) {
273621c328dcSRob Herring 		dev_err(&pdev->dev, "missing reg property on %pOFn\n", pnp);
273776723bcaSSebastian Hesselbarth 		return -EINVAL;
273876723bcaSSebastian Hesselbarth 	}
273976723bcaSSebastian Hesselbarth 
274076723bcaSSebastian Hesselbarth 	if (ppd.port_number >= 3) {
274121c328dcSRob Herring 		dev_err(&pdev->dev, "invalid reg property on %pOFn\n", pnp);
274276723bcaSSebastian Hesselbarth 		return -EINVAL;
274376723bcaSSebastian Hesselbarth 	}
274476723bcaSSebastian Hesselbarth 
2745785bf6f7SJonas Gorski 	while (dev_num < 3 && port_platdev[dev_num])
2746785bf6f7SJonas Gorski 		dev_num++;
2747785bf6f7SJonas Gorski 
2748785bf6f7SJonas Gorski 	if (dev_num == 3) {
2749785bf6f7SJonas Gorski 		dev_err(&pdev->dev, "too many ports registered\n");
2750785bf6f7SJonas Gorski 		return -EINVAL;
2751785bf6f7SJonas Gorski 	}
2752785bf6f7SJonas Gorski 
275342404d8fSMauri Sandberg 	ret = of_get_mac_address(pnp, ppd.mac_addr);
275442404d8fSMauri Sandberg 	if (ret)
275542404d8fSMauri Sandberg 		return ret;
275676723bcaSSebastian Hesselbarth 
275776723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size);
275876723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr);
275976723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size);
276076723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size);
276176723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr);
276276723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size);
276376723bcaSSebastian Hesselbarth 
276476723bcaSSebastian Hesselbarth 	ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0);
276576723bcaSSebastian Hesselbarth 	if (!ppd.phy_node) {
276676723bcaSSebastian Hesselbarth 		ppd.phy_addr = MV643XX_ETH_PHY_NONE;
276776723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "speed", &ppd.speed);
276876723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "duplex", &ppd.duplex);
276976723bcaSSebastian Hesselbarth 	}
277076723bcaSSebastian Hesselbarth 
2771785bf6f7SJonas Gorski 	ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num);
277276723bcaSSebastian Hesselbarth 	if (!ppdev)
277376723bcaSSebastian Hesselbarth 		return -ENOMEM;
277476723bcaSSebastian Hesselbarth 	ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2775b5d82db8SSebastian Hesselbarth 	ppdev->dev.of_node = pnp;
277676723bcaSSebastian Hesselbarth 
277776723bcaSSebastian Hesselbarth 	ret = platform_device_add_resources(ppdev, &res, 1);
277876723bcaSSebastian Hesselbarth 	if (ret)
277976723bcaSSebastian Hesselbarth 		goto port_err;
278076723bcaSSebastian Hesselbarth 
278176723bcaSSebastian Hesselbarth 	ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
278276723bcaSSebastian Hesselbarth 	if (ret)
278376723bcaSSebastian Hesselbarth 		goto port_err;
278476723bcaSSebastian Hesselbarth 
278576723bcaSSebastian Hesselbarth 	ret = platform_device_add(ppdev);
278676723bcaSSebastian Hesselbarth 	if (ret)
278776723bcaSSebastian Hesselbarth 		goto port_err;
278876723bcaSSebastian Hesselbarth 
2789785bf6f7SJonas Gorski 	port_platdev[dev_num] = ppdev;
279076723bcaSSebastian Hesselbarth 
279176723bcaSSebastian Hesselbarth 	return 0;
279276723bcaSSebastian Hesselbarth 
279376723bcaSSebastian Hesselbarth port_err:
279476723bcaSSebastian Hesselbarth 	platform_device_put(ppdev);
279576723bcaSSebastian Hesselbarth 	return ret;
279676723bcaSSebastian Hesselbarth }
279776723bcaSSebastian Hesselbarth 
279876723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
279976723bcaSSebastian Hesselbarth {
280076723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
280176723bcaSSebastian Hesselbarth 	struct device_node *pnp, *np = pdev->dev.of_node;
280276723bcaSSebastian Hesselbarth 	int ret;
280376723bcaSSebastian Hesselbarth 
280476723bcaSSebastian Hesselbarth 	/* bail out if not registered from DT */
280576723bcaSSebastian Hesselbarth 	if (!np)
280676723bcaSSebastian Hesselbarth 		return 0;
280776723bcaSSebastian Hesselbarth 
280876723bcaSSebastian Hesselbarth 	pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
280976723bcaSSebastian Hesselbarth 	if (!pd)
281076723bcaSSebastian Hesselbarth 		return -ENOMEM;
281176723bcaSSebastian Hesselbarth 	pdev->dev.platform_data = pd;
281276723bcaSSebastian Hesselbarth 
281376723bcaSSebastian Hesselbarth 	mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
281476723bcaSSebastian Hesselbarth 
281576723bcaSSebastian Hesselbarth 	for_each_available_child_of_node(np, pnp) {
281676723bcaSSebastian Hesselbarth 		ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
281726b7974dSJulia Lawall 		if (ret) {
281826b7974dSJulia Lawall 			of_node_put(pnp);
281942404d8fSMauri Sandberg 			mv643xx_eth_shared_of_remove();
282076723bcaSSebastian Hesselbarth 			return ret;
282176723bcaSSebastian Hesselbarth 		}
282226b7974dSJulia Lawall 	}
282376723bcaSSebastian Hesselbarth 	return 0;
282476723bcaSSebastian Hesselbarth }
282576723bcaSSebastian Hesselbarth 
282676723bcaSSebastian Hesselbarth #else
2827ff20877aSArnd Bergmann static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
282876723bcaSSebastian Hesselbarth {
2829ff20877aSArnd Bergmann 	return 0;
283076723bcaSSebastian Hesselbarth }
283176723bcaSSebastian Hesselbarth 
2832ff20877aSArnd Bergmann static inline void mv643xx_eth_shared_of_remove(void)
2833ff20877aSArnd Bergmann {
2834ff20877aSArnd Bergmann }
283576723bcaSSebastian Hesselbarth #endif
283676723bcaSSebastian Hesselbarth 
2837527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2838527a6266SJeff Kirsher {
2839527a6266SJeff Kirsher 	static int mv643xx_eth_version_printed;
284076723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
2841527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp;
284263a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
2843527a6266SJeff Kirsher 	struct resource *res;
284476723bcaSSebastian Hesselbarth 	int ret;
2845527a6266SJeff Kirsher 
2846527a6266SJeff Kirsher 	if (!mv643xx_eth_version_printed++)
2847527a6266SJeff Kirsher 		pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2848527a6266SJeff Kirsher 			  mv643xx_eth_driver_version);
2849527a6266SJeff Kirsher 
2850527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2851527a6266SJeff Kirsher 	if (res == NULL)
2852727f957aSSebastian Hesselbarth 		return -EINVAL;
2853527a6266SJeff Kirsher 
2854727f957aSSebastian Hesselbarth 	msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
2855527a6266SJeff Kirsher 	if (msp == NULL)
2856727f957aSSebastian Hesselbarth 		return -ENOMEM;
285776723bcaSSebastian Hesselbarth 	platform_set_drvdata(pdev, msp);
2858527a6266SJeff Kirsher 
285965a6f969SSebastian Hesselbarth 	msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
2860527a6266SJeff Kirsher 	if (msp->base == NULL)
2861727f957aSSebastian Hesselbarth 		return -ENOMEM;
2862527a6266SJeff Kirsher 
286320922486SSebastian Hesselbarth 	msp->clk = devm_clk_get(&pdev->dev, NULL);
286420922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
286520922486SSebastian Hesselbarth 		clk_prepare_enable(msp->clk);
286620922486SSebastian Hesselbarth 
2867527a6266SJeff Kirsher 	/*
2868527a6266SJeff Kirsher 	 * (Re-)program MBUS remapping windows if we are asked to.
2869527a6266SJeff Kirsher 	 */
287063a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
287163a9332bSAndrew Lunn 	if (dram)
287263a9332bSAndrew Lunn 		mv643xx_eth_conf_mbus_windows(msp, dram);
2873527a6266SJeff Kirsher 
287476723bcaSSebastian Hesselbarth 	ret = mv643xx_eth_shared_of_probe(pdev);
287576723bcaSSebastian Hesselbarth 	if (ret)
2876e928b5d6SAlexey Khoroshilov 		goto err_put_clk;
2877bbfa6d0aSJingoo Han 	pd = dev_get_platdata(&pdev->dev);
287876723bcaSSebastian Hesselbarth 
2879527a6266SJeff Kirsher 	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2880527a6266SJeff Kirsher 					pd->tx_csum_limit : 9 * 1024;
2881527a6266SJeff Kirsher 	infer_hw_params(msp);
2882527a6266SJeff Kirsher 
2883527a6266SJeff Kirsher 	return 0;
2884e928b5d6SAlexey Khoroshilov 
2885e928b5d6SAlexey Khoroshilov err_put_clk:
2886e928b5d6SAlexey Khoroshilov 	if (!IS_ERR(msp->clk))
2887e928b5d6SAlexey Khoroshilov 		clk_disable_unprepare(msp->clk);
2888e928b5d6SAlexey Khoroshilov 	return ret;
2889527a6266SJeff Kirsher }
2890527a6266SJeff Kirsher 
2891527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2892527a6266SJeff Kirsher {
2893527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
2894527a6266SJeff Kirsher 
289576723bcaSSebastian Hesselbarth 	mv643xx_eth_shared_of_remove();
289620922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
289720922486SSebastian Hesselbarth 		clk_disable_unprepare(msp->clk);
2898527a6266SJeff Kirsher 	return 0;
2899527a6266SJeff Kirsher }
2900527a6266SJeff Kirsher 
2901527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = {
2902527a6266SJeff Kirsher 	.probe		= mv643xx_eth_shared_probe,
2903527a6266SJeff Kirsher 	.remove		= mv643xx_eth_shared_remove,
2904527a6266SJeff Kirsher 	.driver = {
2905527a6266SJeff Kirsher 		.name	= MV643XX_ETH_SHARED_NAME,
290676723bcaSSebastian Hesselbarth 		.of_match_table = of_match_ptr(mv643xx_eth_shared_ids),
2907527a6266SJeff Kirsher 	},
2908527a6266SJeff Kirsher };
2909527a6266SJeff Kirsher 
2910527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
2911527a6266SJeff Kirsher {
2912527a6266SJeff Kirsher 	int addr_shift = 5 * mp->port_num;
2913527a6266SJeff Kirsher 	u32 data;
2914527a6266SJeff Kirsher 
2915527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2916527a6266SJeff Kirsher 	data &= ~(0x1f << addr_shift);
2917527a6266SJeff Kirsher 	data |= (phy_addr & 0x1f) << addr_shift;
2918527a6266SJeff Kirsher 	wrl(mp, PHY_ADDR, data);
2919527a6266SJeff Kirsher }
2920527a6266SJeff Kirsher 
2921527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp)
2922527a6266SJeff Kirsher {
2923527a6266SJeff Kirsher 	unsigned int data;
2924527a6266SJeff Kirsher 
2925527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2926527a6266SJeff Kirsher 
2927527a6266SJeff Kirsher 	return (data >> (5 * mp->port_num)) & 0x1f;
2928527a6266SJeff Kirsher }
2929527a6266SJeff Kirsher 
2930527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp,
2931527a6266SJeff Kirsher 		       struct mv643xx_eth_platform_data *pd)
2932527a6266SJeff Kirsher {
2933527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2934ee9e4956SEzequiel Garcia 	unsigned int tx_ring_size;
2935527a6266SJeff Kirsher 
293615c343ebSJakub Kicinski 	if (is_valid_ether_addr(pd->mac_addr)) {
2937a96d317fSJakub Kicinski 		eth_hw_addr_set(dev, pd->mac_addr);
293815c343ebSJakub Kicinski 	} else {
293915c343ebSJakub Kicinski 		u8 addr[ETH_ALEN];
294015c343ebSJakub Kicinski 
294115c343ebSJakub Kicinski 		uc_addr_get(mp, addr);
294215c343ebSJakub Kicinski 		eth_hw_addr_set(dev, addr);
294315c343ebSJakub Kicinski 	}
2944527a6266SJeff Kirsher 
2945527a6266SJeff Kirsher 	mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2946527a6266SJeff Kirsher 	if (pd->rx_queue_size)
2947527a6266SJeff Kirsher 		mp->rx_ring_size = pd->rx_queue_size;
2948527a6266SJeff Kirsher 	mp->rx_desc_sram_addr = pd->rx_sram_addr;
2949527a6266SJeff Kirsher 	mp->rx_desc_sram_size = pd->rx_sram_size;
2950527a6266SJeff Kirsher 
2951527a6266SJeff Kirsher 	mp->rxq_count = pd->rx_queue_count ? : 1;
2952527a6266SJeff Kirsher 
2953ee9e4956SEzequiel Garcia 	tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2954527a6266SJeff Kirsher 	if (pd->tx_queue_size)
2955ee9e4956SEzequiel Garcia 		tx_ring_size = pd->tx_queue_size;
2956ee9e4956SEzequiel Garcia 
2957ee9e4956SEzequiel Garcia 	mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size,
2958ee9e4956SEzequiel Garcia 				   MV643XX_MAX_SKB_DESCS * 2, 4096);
2959ee9e4956SEzequiel Garcia 	if (mp->tx_ring_size != tx_ring_size)
2960ee9e4956SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
2961ee9e4956SEzequiel Garcia 			    mp->tx_ring_size, tx_ring_size);
2962ee9e4956SEzequiel Garcia 
2963527a6266SJeff Kirsher 	mp->tx_desc_sram_addr = pd->tx_sram_addr;
2964527a6266SJeff Kirsher 	mp->tx_desc_sram_size = pd->tx_sram_size;
2965527a6266SJeff Kirsher 
2966527a6266SJeff Kirsher 	mp->txq_count = pd->tx_queue_count ? : 1;
2967527a6266SJeff Kirsher }
2968527a6266SJeff Kirsher 
2969fd33b244SJason Gunthorpe static int get_phy_mode(struct mv643xx_eth_private *mp)
2970fd33b244SJason Gunthorpe {
2971fd33b244SJason Gunthorpe 	struct device *dev = mp->dev->dev.parent;
29720c65b2b9SAndrew Lunn 	phy_interface_t iface;
29730c65b2b9SAndrew Lunn 	int err;
2974fd33b244SJason Gunthorpe 
2975fd33b244SJason Gunthorpe 	if (dev->of_node)
29760c65b2b9SAndrew Lunn 		err = of_get_phy_mode(dev->of_node, &iface);
2977fd33b244SJason Gunthorpe 
2978fd33b244SJason Gunthorpe 	/* Historical default if unspecified. We could also read/write
2979fd33b244SJason Gunthorpe 	 * the interface state in the PSC1
2980fd33b244SJason Gunthorpe 	 */
29810c65b2b9SAndrew Lunn 	if (!dev->of_node || err)
2982fd33b244SJason Gunthorpe 		iface = PHY_INTERFACE_MODE_GMII;
2983fd33b244SJason Gunthorpe 	return iface;
2984fd33b244SJason Gunthorpe }
2985fd33b244SJason Gunthorpe 
2986527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2987527a6266SJeff Kirsher 				   int phy_addr)
2988527a6266SJeff Kirsher {
2989527a6266SJeff Kirsher 	struct phy_device *phydev;
2990527a6266SJeff Kirsher 	int start;
2991527a6266SJeff Kirsher 	int num;
2992527a6266SJeff Kirsher 	int i;
2993c3a07134SFlorian Fainelli 	char phy_id[MII_BUS_ID_SIZE + 3];
2994527a6266SJeff Kirsher 
2995527a6266SJeff Kirsher 	if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2996527a6266SJeff Kirsher 		start = phy_addr_get(mp) & 0x1f;
2997527a6266SJeff Kirsher 		num = 32;
2998527a6266SJeff Kirsher 	} else {
2999527a6266SJeff Kirsher 		start = phy_addr & 0x1f;
3000527a6266SJeff Kirsher 		num = 1;
3001527a6266SJeff Kirsher 	}
3002527a6266SJeff Kirsher 
3003c3a07134SFlorian Fainelli 	/* Attempt to connect to the PHY using orion-mdio */
3004976c90b9SSimon Baatz 	phydev = ERR_PTR(-ENODEV);
3005527a6266SJeff Kirsher 	for (i = 0; i < num; i++) {
3006527a6266SJeff Kirsher 		int addr = (start + i) & 0x1f;
3007527a6266SJeff Kirsher 
3008c3a07134SFlorian Fainelli 		snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
3009c3a07134SFlorian Fainelli 				"orion-mdio-mii", addr);
3010527a6266SJeff Kirsher 
3011c3a07134SFlorian Fainelli 		phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
3012fd33b244SJason Gunthorpe 				     get_phy_mode(mp));
3013c3a07134SFlorian Fainelli 		if (!IS_ERR(phydev)) {
3014527a6266SJeff Kirsher 			phy_addr_set(mp, addr);
3015c3a07134SFlorian Fainelli 			break;
3016527a6266SJeff Kirsher 		}
3017527a6266SJeff Kirsher 	}
3018527a6266SJeff Kirsher 
3019527a6266SJeff Kirsher 	return phydev;
3020527a6266SJeff Kirsher }
3021527a6266SJeff Kirsher 
3022527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
3023527a6266SJeff Kirsher {
30241e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
30251e8a655dSPhilippe Reynes 	struct phy_device *phy = dev->phydev;
3026527a6266SJeff Kirsher 
3027527a6266SJeff Kirsher 	if (speed == 0) {
3028527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_ENABLE;
3029527a6266SJeff Kirsher 		phy->speed = 0;
3030527a6266SJeff Kirsher 		phy->duplex = 0;
30313c1bcc86SAndrew Lunn 		linkmode_copy(phy->advertising, phy->supported);
30323c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
30333c1bcc86SAndrew Lunn 				 phy->advertising);
3034527a6266SJeff Kirsher 	} else {
3035527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_DISABLE;
30363c1bcc86SAndrew Lunn 		linkmode_zero(phy->advertising);
3037527a6266SJeff Kirsher 		phy->speed = speed;
3038527a6266SJeff Kirsher 		phy->duplex = duplex;
3039527a6266SJeff Kirsher 	}
3040527a6266SJeff Kirsher 	phy_start_aneg(phy);
3041527a6266SJeff Kirsher }
3042527a6266SJeff Kirsher 
3043527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
3044527a6266SJeff Kirsher {
30451e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
3046527a6266SJeff Kirsher 	u32 pscr;
3047527a6266SJeff Kirsher 
3048527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
3049527a6266SJeff Kirsher 	if (pscr & SERIAL_PORT_ENABLE) {
3050527a6266SJeff Kirsher 		pscr &= ~SERIAL_PORT_ENABLE;
3051527a6266SJeff Kirsher 		wrlp(mp, PORT_SERIAL_CONTROL, pscr);
3052527a6266SJeff Kirsher 	}
3053527a6266SJeff Kirsher 
3054527a6266SJeff Kirsher 	pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
30551e8a655dSPhilippe Reynes 	if (!dev->phydev) {
3056527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
3057527a6266SJeff Kirsher 		if (speed == SPEED_1000)
3058527a6266SJeff Kirsher 			pscr |= SET_GMII_SPEED_TO_1000;
3059527a6266SJeff Kirsher 		else if (speed == SPEED_100)
3060527a6266SJeff Kirsher 			pscr |= SET_MII_SPEED_TO_100;
3061527a6266SJeff Kirsher 
3062527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
3063527a6266SJeff Kirsher 
3064527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
3065527a6266SJeff Kirsher 		if (duplex == DUPLEX_FULL)
3066527a6266SJeff Kirsher 			pscr |= SET_FULL_DUPLEX_MODE;
3067527a6266SJeff Kirsher 	}
3068527a6266SJeff Kirsher 
3069527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
3070527a6266SJeff Kirsher }
3071527a6266SJeff Kirsher 
3072527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = {
3073527a6266SJeff Kirsher 	.ndo_open		= mv643xx_eth_open,
3074527a6266SJeff Kirsher 	.ndo_stop		= mv643xx_eth_stop,
3075527a6266SJeff Kirsher 	.ndo_start_xmit		= mv643xx_eth_xmit,
3076527a6266SJeff Kirsher 	.ndo_set_rx_mode	= mv643xx_eth_set_rx_mode,
3077527a6266SJeff Kirsher 	.ndo_set_mac_address	= mv643xx_eth_set_mac_address,
3078527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
3079a7605370SArnd Bergmann 	.ndo_eth_ioctl		= mv643xx_eth_ioctl,
3080527a6266SJeff Kirsher 	.ndo_change_mtu		= mv643xx_eth_change_mtu,
3081527a6266SJeff Kirsher 	.ndo_set_features	= mv643xx_eth_set_features,
3082527a6266SJeff Kirsher 	.ndo_tx_timeout		= mv643xx_eth_tx_timeout,
3083527a6266SJeff Kirsher 	.ndo_get_stats		= mv643xx_eth_get_stats,
3084527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
3085527a6266SJeff Kirsher 	.ndo_poll_controller	= mv643xx_eth_netpoll,
3086527a6266SJeff Kirsher #endif
3087527a6266SJeff Kirsher };
3088527a6266SJeff Kirsher 
3089527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev)
3090527a6266SJeff Kirsher {
3091527a6266SJeff Kirsher 	struct mv643xx_eth_platform_data *pd;
3092527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
3093527a6266SJeff Kirsher 	struct net_device *dev;
30941e8a655dSPhilippe Reynes 	struct phy_device *phydev = NULL;
3095527a6266SJeff Kirsher 	struct resource *res;
3096527a6266SJeff Kirsher 	int err;
3097527a6266SJeff Kirsher 
3098bbfa6d0aSJingoo Han 	pd = dev_get_platdata(&pdev->dev);
3099527a6266SJeff Kirsher 	if (pd == NULL) {
3100527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
3101527a6266SJeff Kirsher 		return -ENODEV;
3102527a6266SJeff Kirsher 	}
3103527a6266SJeff Kirsher 
3104527a6266SJeff Kirsher 	if (pd->shared == NULL) {
3105527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
3106527a6266SJeff Kirsher 		return -ENODEV;
3107527a6266SJeff Kirsher 	}
3108527a6266SJeff Kirsher 
3109527a6266SJeff Kirsher 	dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
3110527a6266SJeff Kirsher 	if (!dev)
3111527a6266SJeff Kirsher 		return -ENOMEM;
3112527a6266SJeff Kirsher 
3113fd33b244SJason Gunthorpe 	SET_NETDEV_DEV(dev, &pdev->dev);
3114527a6266SJeff Kirsher 	mp = netdev_priv(dev);
3115527a6266SJeff Kirsher 	platform_set_drvdata(pdev, mp);
3116527a6266SJeff Kirsher 
3117527a6266SJeff Kirsher 	mp->shared = platform_get_drvdata(pd->shared);
3118527a6266SJeff Kirsher 	mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
3119527a6266SJeff Kirsher 	mp->port_num = pd->port_number;
3120527a6266SJeff Kirsher 
3121527a6266SJeff Kirsher 	mp->dev = dev;
3122527a6266SJeff Kirsher 
3123cb85215fSSebastian Hesselbarth 	/* Kirkwood resets some registers on gated clocks. Especially
3124cb85215fSSebastian Hesselbarth 	 * CLK125_BYPASS_EN must be cleared but is not available on
3125cb85215fSSebastian Hesselbarth 	 * all other SoCs/System Controllers using this driver.
3126cb85215fSSebastian Hesselbarth 	 */
3127cb85215fSSebastian Hesselbarth 	if (of_device_is_compatible(pdev->dev.of_node,
3128cb85215fSSebastian Hesselbarth 				    "marvell,kirkwood-eth-port"))
3129cb85215fSSebastian Hesselbarth 		wrlp(mp, PORT_SERIAL_CONTROL1,
3130cb85215fSSebastian Hesselbarth 		     rdlp(mp, PORT_SERIAL_CONTROL1) & ~CLK125_BYPASS_EN);
3131cb85215fSSebastian Hesselbarth 
3132452503ebSAndrew Lunn 	/*
31339a43a026SAndrew Lunn 	 * Start with a default rate, and if there is a clock, allow
31349a43a026SAndrew Lunn 	 * it to override the default.
3135452503ebSAndrew Lunn 	 */
31369a43a026SAndrew Lunn 	mp->t_clk = 133000000;
313720922486SSebastian Hesselbarth 	mp->clk = devm_clk_get(&pdev->dev, NULL);
3138452503ebSAndrew Lunn 	if (!IS_ERR(mp->clk)) {
3139452503ebSAndrew Lunn 		clk_prepare_enable(mp->clk);
3140452503ebSAndrew Lunn 		mp->t_clk = clk_get_rate(mp->clk);
314176723bcaSSebastian Hesselbarth 	} else if (!IS_ERR(mp->shared->clk)) {
314276723bcaSSebastian Hesselbarth 		mp->t_clk = clk_get_rate(mp->shared->clk);
3143452503ebSAndrew Lunn 	}
314420922486SSebastian Hesselbarth 
3145527a6266SJeff Kirsher 	set_params(mp, pd);
3146527a6266SJeff Kirsher 	netif_set_real_num_tx_queues(dev, mp->txq_count);
3147527a6266SJeff Kirsher 	netif_set_real_num_rx_queues(dev, mp->rxq_count);
3148527a6266SJeff Kirsher 
3149cc9d4598SSebastian Hesselbarth 	err = 0;
3150cc9d4598SSebastian Hesselbarth 	if (pd->phy_node) {
31511e8a655dSPhilippe Reynes 		phydev = of_phy_connect(mp->dev, pd->phy_node,
3152cc9d4598SSebastian Hesselbarth 					mv643xx_eth_adjust_link, 0,
3153fd33b244SJason Gunthorpe 					get_phy_mode(mp));
31541e8a655dSPhilippe Reynes 		if (!phydev)
3155cc9d4598SSebastian Hesselbarth 			err = -ENODEV;
31566115c11fSDan Carpenter 		else
31571e8a655dSPhilippe Reynes 			phy_addr_set(mp, phydev->mdio.addr);
3158cc9d4598SSebastian Hesselbarth 	} else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
31591e8a655dSPhilippe Reynes 		phydev = phy_scan(mp, pd->phy_addr);
3160527a6266SJeff Kirsher 
31611e8a655dSPhilippe Reynes 		if (IS_ERR(phydev))
31621e8a655dSPhilippe Reynes 			err = PTR_ERR(phydev);
3163cc9d4598SSebastian Hesselbarth 		else
3164cc9d4598SSebastian Hesselbarth 			phy_init(mp, pd->speed, pd->duplex);
3165cc9d4598SSebastian Hesselbarth 	}
3166cc9d4598SSebastian Hesselbarth 	if (err == -ENODEV) {
3167976c90b9SSimon Baatz 		err = -EPROBE_DEFER;
3168976c90b9SSimon Baatz 		goto out;
3169976c90b9SSimon Baatz 	}
3170cc9d4598SSebastian Hesselbarth 	if (err)
3171cc9d4598SSebastian Hesselbarth 		goto out;
3172527a6266SJeff Kirsher 
31737ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &mv643xx_eth_ethtool_ops;
3174527a6266SJeff Kirsher 
3175527a6266SJeff Kirsher 	init_pscr(mp, pd->speed, pd->duplex);
3176527a6266SJeff Kirsher 
3177527a6266SJeff Kirsher 
3178527a6266SJeff Kirsher 	mib_counters_clear(mp);
3179527a6266SJeff Kirsher 
3180e99e88a9SKees Cook 	timer_setup(&mp->mib_counters_timer, mib_counters_timer_wrapper, 0);
3181527a6266SJeff Kirsher 	mp->mib_counters_timer.expires = jiffies + 30 * HZ;
3182527a6266SJeff Kirsher 
3183527a6266SJeff Kirsher 	spin_lock_init(&mp->mib_counters_lock);
3184527a6266SJeff Kirsher 
3185527a6266SJeff Kirsher 	INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
3186527a6266SJeff Kirsher 
3187a3659aa0SAndrew Lunn 	netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, NAPI_POLL_WEIGHT);
3188527a6266SJeff Kirsher 
3189e99e88a9SKees Cook 	timer_setup(&mp->rx_oom, oom_timer_wrapper, 0);
3190527a6266SJeff Kirsher 
3191527a6266SJeff Kirsher 
3192527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
3193527a6266SJeff Kirsher 	BUG_ON(!res);
3194527a6266SJeff Kirsher 	dev->irq = res->start;
3195527a6266SJeff Kirsher 
3196527a6266SJeff Kirsher 	dev->netdev_ops = &mv643xx_eth_netdev_ops;
3197527a6266SJeff Kirsher 
3198527a6266SJeff Kirsher 	dev->watchdog_timeo = 2 * HZ;
3199527a6266SJeff Kirsher 	dev->base_addr = 0;
3200527a6266SJeff Kirsher 
32013ae8f4e0SEzequiel Garcia 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
32024d48d589SEzequiel Garcia 	dev->vlan_features = dev->features;
32034d48d589SEzequiel Garcia 
32044d48d589SEzequiel Garcia 	dev->features |= NETIF_F_RXCSUM;
32054d48d589SEzequiel Garcia 	dev->hw_features = dev->features;
3206527a6266SJeff Kirsher 
320701789349SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT;
32086d872df3SEric Dumazet 	netif_set_gso_max_segs(dev, MV643XX_MAX_TSO_SEGS);
320901789349SJiri Pirko 
3210d894be57SJarod Wilson 	/* MTU range: 64 - 9500 */
3211d894be57SJarod Wilson 	dev->min_mtu = 64;
3212d894be57SJarod Wilson 	dev->max_mtu = 9500;
3213d894be57SJarod Wilson 
3214527a6266SJeff Kirsher 	if (mp->shared->win_protect)
3215527a6266SJeff Kirsher 		wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
3216527a6266SJeff Kirsher 
3217527a6266SJeff Kirsher 	netif_carrier_off(dev);
3218527a6266SJeff Kirsher 
3219527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
3220527a6266SJeff Kirsher 
3221527a6266SJeff Kirsher 	set_rx_coal(mp, 250);
3222527a6266SJeff Kirsher 	set_tx_coal(mp, 0);
3223527a6266SJeff Kirsher 
3224527a6266SJeff Kirsher 	err = register_netdev(dev);
3225527a6266SJeff Kirsher 	if (err)
3226527a6266SJeff Kirsher 		goto out;
3227527a6266SJeff Kirsher 
3228527a6266SJeff Kirsher 	netdev_notice(dev, "port %d with MAC address %pM\n",
3229527a6266SJeff Kirsher 		      mp->port_num, dev->dev_addr);
3230527a6266SJeff Kirsher 
3231527a6266SJeff Kirsher 	if (mp->tx_desc_sram_size > 0)
3232527a6266SJeff Kirsher 		netdev_notice(dev, "configured with sram\n");
3233527a6266SJeff Kirsher 
3234527a6266SJeff Kirsher 	return 0;
3235527a6266SJeff Kirsher 
3236527a6266SJeff Kirsher out:
323720922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
3238baffab28SSimon Baatz 		clk_disable_unprepare(mp->clk);
3239527a6266SJeff Kirsher 	free_netdev(dev);
3240527a6266SJeff Kirsher 
3241527a6266SJeff Kirsher 	return err;
3242527a6266SJeff Kirsher }
3243527a6266SJeff Kirsher 
3244527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev)
3245527a6266SJeff Kirsher {
3246527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
32471e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
3248527a6266SJeff Kirsher 
3249527a6266SJeff Kirsher 	unregister_netdev(mp->dev);
32501e8a655dSPhilippe Reynes 	if (dev->phydev)
32511e8a655dSPhilippe Reynes 		phy_disconnect(dev->phydev);
3252527a6266SJeff Kirsher 	cancel_work_sync(&mp->tx_timeout_task);
3253452503ebSAndrew Lunn 
325420922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
3255452503ebSAndrew Lunn 		clk_disable_unprepare(mp->clk);
32569a43a026SAndrew Lunn 
3257527a6266SJeff Kirsher 	free_netdev(mp->dev);
3258527a6266SJeff Kirsher 
3259527a6266SJeff Kirsher 	return 0;
3260527a6266SJeff Kirsher }
3261527a6266SJeff Kirsher 
3262527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev)
3263527a6266SJeff Kirsher {
3264527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
3265527a6266SJeff Kirsher 
3266527a6266SJeff Kirsher 	/* Mask all interrupts on ethernet port */
3267527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
3268527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
3269527a6266SJeff Kirsher 
3270527a6266SJeff Kirsher 	if (netif_running(mp->dev))
3271527a6266SJeff Kirsher 		port_reset(mp);
3272527a6266SJeff Kirsher }
3273527a6266SJeff Kirsher 
3274527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = {
3275527a6266SJeff Kirsher 	.probe		= mv643xx_eth_probe,
3276527a6266SJeff Kirsher 	.remove		= mv643xx_eth_remove,
3277527a6266SJeff Kirsher 	.shutdown	= mv643xx_eth_shutdown,
3278527a6266SJeff Kirsher 	.driver = {
3279527a6266SJeff Kirsher 		.name	= MV643XX_ETH_NAME,
3280527a6266SJeff Kirsher 	},
3281527a6266SJeff Kirsher };
3282527a6266SJeff Kirsher 
32833b5dde70SThierry Reding static struct platform_driver * const drivers[] = {
32843b5dde70SThierry Reding 	&mv643xx_eth_shared_driver,
32853b5dde70SThierry Reding 	&mv643xx_eth_driver,
32863b5dde70SThierry Reding };
32873b5dde70SThierry Reding 
3288527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void)
3289527a6266SJeff Kirsher {
32903b5dde70SThierry Reding 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
3291527a6266SJeff Kirsher }
3292527a6266SJeff Kirsher module_init(mv643xx_eth_init_module);
3293527a6266SJeff Kirsher 
3294527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void)
3295527a6266SJeff Kirsher {
32963b5dde70SThierry Reding 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
3297527a6266SJeff Kirsher }
3298527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module);
3299527a6266SJeff Kirsher 
3300527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
3301527a6266SJeff Kirsher 	      "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
3302527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
3303527a6266SJeff Kirsher MODULE_LICENSE("GPL");
3304527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
3305527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3306