xref: /openbmc/linux/drivers/net/ethernet/marvell/mv643xx_eth.c (revision 2c2a9cbd64387d6b70ac5db013e9bfe9412c7354)
1527a6266SJeff Kirsher /*
2527a6266SJeff Kirsher  * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
3527a6266SJeff Kirsher  * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4527a6266SJeff Kirsher  *
5527a6266SJeff Kirsher  * Based on the 64360 driver from:
6527a6266SJeff Kirsher  * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7527a6266SJeff Kirsher  *		      Rabeeh Khoury <rabeeh@marvell.com>
8527a6266SJeff Kirsher  *
9527a6266SJeff Kirsher  * Copyright (C) 2003 PMC-Sierra, Inc.,
10527a6266SJeff Kirsher  *	written by Manish Lachwani
11527a6266SJeff Kirsher  *
12527a6266SJeff Kirsher  * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13527a6266SJeff Kirsher  *
14527a6266SJeff Kirsher  * Copyright (C) 2004-2006 MontaVista Software, Inc.
15527a6266SJeff Kirsher  *			   Dale Farnsworth <dale@farnsworth.org>
16527a6266SJeff Kirsher  *
17527a6266SJeff Kirsher  * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18527a6266SJeff Kirsher  *				     <sjhill@realitydiluted.com>
19527a6266SJeff Kirsher  *
20527a6266SJeff Kirsher  * Copyright (C) 2007-2008 Marvell Semiconductor
21527a6266SJeff Kirsher  *			   Lennert Buytenhek <buytenh@marvell.com>
22527a6266SJeff Kirsher  *
233871c387SMichael Stapelberg  * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
243871c387SMichael Stapelberg  *
25527a6266SJeff Kirsher  * This program is free software; you can redistribute it and/or
26527a6266SJeff Kirsher  * modify it under the terms of the GNU General Public License
27527a6266SJeff Kirsher  * as published by the Free Software Foundation; either version 2
28527a6266SJeff Kirsher  * of the License, or (at your option) any later version.
29527a6266SJeff Kirsher  *
30527a6266SJeff Kirsher  * This program is distributed in the hope that it will be useful,
31527a6266SJeff Kirsher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
32527a6266SJeff Kirsher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33527a6266SJeff Kirsher  * GNU General Public License for more details.
34527a6266SJeff Kirsher  *
35527a6266SJeff Kirsher  * You should have received a copy of the GNU General Public License
360ab75ae8SJeff Kirsher  * along with this program; if not, see <http://www.gnu.org/licenses/>.
37527a6266SJeff Kirsher  */
38527a6266SJeff Kirsher 
39527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
40527a6266SJeff Kirsher 
41527a6266SJeff Kirsher #include <linux/init.h>
42527a6266SJeff Kirsher #include <linux/dma-mapping.h>
43527a6266SJeff Kirsher #include <linux/in.h>
44527a6266SJeff Kirsher #include <linux/ip.h>
453ae8f4e0SEzequiel Garcia #include <net/tso.h>
46527a6266SJeff Kirsher #include <linux/tcp.h>
47527a6266SJeff Kirsher #include <linux/udp.h>
48527a6266SJeff Kirsher #include <linux/etherdevice.h>
49527a6266SJeff Kirsher #include <linux/delay.h>
50527a6266SJeff Kirsher #include <linux/ethtool.h>
51527a6266SJeff Kirsher #include <linux/platform_device.h>
52527a6266SJeff Kirsher #include <linux/module.h>
53527a6266SJeff Kirsher #include <linux/kernel.h>
54527a6266SJeff Kirsher #include <linux/spinlock.h>
55527a6266SJeff Kirsher #include <linux/workqueue.h>
56527a6266SJeff Kirsher #include <linux/phy.h>
57527a6266SJeff Kirsher #include <linux/mv643xx_eth.h>
58527a6266SJeff Kirsher #include <linux/io.h>
593619eb85SSebastian Hesselbarth #include <linux/interrupt.h>
60527a6266SJeff Kirsher #include <linux/types.h>
61527a6266SJeff Kirsher #include <linux/slab.h>
62452503ebSAndrew Lunn #include <linux/clk.h>
6376723bcaSSebastian Hesselbarth #include <linux/of.h>
6476723bcaSSebastian Hesselbarth #include <linux/of_irq.h>
6576723bcaSSebastian Hesselbarth #include <linux/of_net.h>
66cc9d4598SSebastian Hesselbarth #include <linux/of_mdio.h>
67527a6266SJeff Kirsher 
68527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth";
69527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4";
70527a6266SJeff Kirsher 
71527a6266SJeff Kirsher 
72527a6266SJeff Kirsher /*
73527a6266SJeff Kirsher  * Registers shared between all ports.
74527a6266SJeff Kirsher  */
75527a6266SJeff Kirsher #define PHY_ADDR			0x0000
76527a6266SJeff Kirsher #define WINDOW_BASE(w)			(0x0200 + ((w) << 3))
77527a6266SJeff Kirsher #define WINDOW_SIZE(w)			(0x0204 + ((w) << 3))
78527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w)		(0x0280 + ((w) << 2))
79527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE		0x0290
80527a6266SJeff Kirsher #define WINDOW_PROTECT(w)		(0x0294 + ((w) << 4))
81527a6266SJeff Kirsher 
82527a6266SJeff Kirsher /*
83527a6266SJeff Kirsher  * Main per-port registers.  These live at offset 0x0400 for
84527a6266SJeff Kirsher  * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
85527a6266SJeff Kirsher  */
86527a6266SJeff Kirsher #define PORT_CONFIG			0x0000
87527a6266SJeff Kirsher #define  UNICAST_PROMISCUOUS_MODE	0x00000001
88527a6266SJeff Kirsher #define PORT_CONFIG_EXT			0x0004
89527a6266SJeff Kirsher #define MAC_ADDR_LOW			0x0014
90527a6266SJeff Kirsher #define MAC_ADDR_HIGH			0x0018
91527a6266SJeff Kirsher #define SDMA_CONFIG			0x001c
92527a6266SJeff Kirsher #define  TX_BURST_SIZE_16_64BIT		0x01000000
93527a6266SJeff Kirsher #define  TX_BURST_SIZE_4_64BIT		0x00800000
94527a6266SJeff Kirsher #define  BLM_TX_NO_SWAP			0x00000020
95527a6266SJeff Kirsher #define  BLM_RX_NO_SWAP			0x00000010
96527a6266SJeff Kirsher #define  RX_BURST_SIZE_16_64BIT		0x00000008
97527a6266SJeff Kirsher #define  RX_BURST_SIZE_4_64BIT		0x00000004
98527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL		0x003c
99527a6266SJeff Kirsher #define  SET_MII_SPEED_TO_100		0x01000000
100527a6266SJeff Kirsher #define  SET_GMII_SPEED_TO_1000		0x00800000
101527a6266SJeff Kirsher #define  SET_FULL_DUPLEX_MODE		0x00200000
102527a6266SJeff Kirsher #define  MAX_RX_PACKET_9700BYTE		0x000a0000
103527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_SPEED_GMII	0x00002000
104527a6266SJeff Kirsher #define  DO_NOT_FORCE_LINK_FAIL		0x00000400
105527a6266SJeff Kirsher #define  SERIAL_PORT_CONTROL_RESERVED	0x00000200
106527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_FLOW_CTRL	0x00000008
107527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_DUPLEX	0x00000004
108527a6266SJeff Kirsher #define  FORCE_LINK_PASS		0x00000002
109527a6266SJeff Kirsher #define  SERIAL_PORT_ENABLE		0x00000001
110527a6266SJeff Kirsher #define PORT_STATUS			0x0044
111527a6266SJeff Kirsher #define  TX_FIFO_EMPTY			0x00000400
112527a6266SJeff Kirsher #define  TX_IN_PROGRESS			0x00000080
113527a6266SJeff Kirsher #define  PORT_SPEED_MASK		0x00000030
114527a6266SJeff Kirsher #define  PORT_SPEED_1000		0x00000010
115527a6266SJeff Kirsher #define  PORT_SPEED_100			0x00000020
116527a6266SJeff Kirsher #define  PORT_SPEED_10			0x00000000
117527a6266SJeff Kirsher #define  FLOW_CONTROL_ENABLED		0x00000008
118527a6266SJeff Kirsher #define  FULL_DUPLEX			0x00000004
119527a6266SJeff Kirsher #define  LINK_UP			0x00000002
120527a6266SJeff Kirsher #define TXQ_COMMAND			0x0048
121527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF		0x004c
122cb85215fSSebastian Hesselbarth #define PORT_SERIAL_CONTROL1		0x004c
123cb85215fSSebastian Hesselbarth #define  CLK125_BYPASS_EN		0x00000010
124527a6266SJeff Kirsher #define TX_BW_RATE			0x0050
125527a6266SJeff Kirsher #define TX_BW_MTU			0x0058
126527a6266SJeff Kirsher #define TX_BW_BURST			0x005c
127527a6266SJeff Kirsher #define INT_CAUSE			0x0060
128527a6266SJeff Kirsher #define  INT_TX_END			0x07f80000
129527a6266SJeff Kirsher #define  INT_TX_END_0			0x00080000
130527a6266SJeff Kirsher #define  INT_RX				0x000003fc
131527a6266SJeff Kirsher #define  INT_RX_0			0x00000004
132527a6266SJeff Kirsher #define  INT_EXT			0x00000002
133527a6266SJeff Kirsher #define INT_CAUSE_EXT			0x0064
134527a6266SJeff Kirsher #define  INT_EXT_LINK_PHY		0x00110000
135527a6266SJeff Kirsher #define  INT_EXT_TX			0x000000ff
136527a6266SJeff Kirsher #define INT_MASK			0x0068
137527a6266SJeff Kirsher #define INT_MASK_EXT			0x006c
138527a6266SJeff Kirsher #define TX_FIFO_URGENT_THRESHOLD	0x0074
139302476c9SPaulius Zaleckas #define RX_DISCARD_FRAME_CNT		0x0084
140302476c9SPaulius Zaleckas #define RX_OVERRUN_FRAME_CNT		0x0088
141527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF_MOVED		0x00dc
142527a6266SJeff Kirsher #define TX_BW_RATE_MOVED		0x00e0
143527a6266SJeff Kirsher #define TX_BW_MTU_MOVED			0x00e8
144527a6266SJeff Kirsher #define TX_BW_BURST_MOVED		0x00ec
145527a6266SJeff Kirsher #define RXQ_CURRENT_DESC_PTR(q)		(0x020c + ((q) << 4))
146527a6266SJeff Kirsher #define RXQ_COMMAND			0x0280
147527a6266SJeff Kirsher #define TXQ_CURRENT_DESC_PTR(q)		(0x02c0 + ((q) << 2))
148527a6266SJeff Kirsher #define TXQ_BW_TOKENS(q)		(0x0300 + ((q) << 4))
149527a6266SJeff Kirsher #define TXQ_BW_CONF(q)			(0x0304 + ((q) << 4))
150527a6266SJeff Kirsher #define TXQ_BW_WRR_CONF(q)		(0x0308 + ((q) << 4))
151527a6266SJeff Kirsher 
152527a6266SJeff Kirsher /*
153527a6266SJeff Kirsher  * Misc per-port registers.
154527a6266SJeff Kirsher  */
155527a6266SJeff Kirsher #define MIB_COUNTERS(p)			(0x1000 + ((p) << 7))
156527a6266SJeff Kirsher #define SPECIAL_MCAST_TABLE(p)		(0x1400 + ((p) << 10))
157527a6266SJeff Kirsher #define OTHER_MCAST_TABLE(p)		(0x1500 + ((p) << 10))
158527a6266SJeff Kirsher #define UNICAST_TABLE(p)		(0x1600 + ((p) << 10))
159527a6266SJeff Kirsher 
160527a6266SJeff Kirsher 
161527a6266SJeff Kirsher /*
162527a6266SJeff Kirsher  * SDMA configuration register default value.
163527a6266SJeff Kirsher  */
164527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
165527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
166527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
167527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
168527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
169527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
170527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
171527a6266SJeff Kirsher 		 BLM_RX_NO_SWAP		|	\
172527a6266SJeff Kirsher 		 BLM_TX_NO_SWAP		|	\
173527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
174527a6266SJeff Kirsher #else
175527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
176527a6266SJeff Kirsher #endif
177527a6266SJeff Kirsher 
178527a6266SJeff Kirsher 
179527a6266SJeff Kirsher /*
180527a6266SJeff Kirsher  * Misc definitions.
181527a6266SJeff Kirsher  */
182527a6266SJeff Kirsher #define DEFAULT_RX_QUEUE_SIZE	128
1833ae8f4e0SEzequiel Garcia #define DEFAULT_TX_QUEUE_SIZE	512
184527a6266SJeff Kirsher #define SKB_DMA_REALIGN		((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
185527a6266SJeff Kirsher 
1863ae8f4e0SEzequiel Garcia #define TSO_HEADER_SIZE		128
187527a6266SJeff Kirsher 
188ee9e4956SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */
189ee9e4956SEzequiel Garcia #define MV643XX_MAX_TSO_SEGS 100
190ee9e4956SEzequiel Garcia #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
191ee9e4956SEzequiel Garcia 
192b926260cSEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \
193b926260cSEzequiel Garcia 	((addr >= txq->tso_hdrs_dma) && \
194b926260cSEzequiel Garcia 	 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
195527a6266SJeff Kirsher /*
196527a6266SJeff Kirsher  * RX/TX descriptors.
197527a6266SJeff Kirsher  */
198527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
199527a6266SJeff Kirsher struct rx_desc {
200527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
201527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
202527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
203527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
204527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
205527a6266SJeff Kirsher };
206527a6266SJeff Kirsher 
207527a6266SJeff Kirsher struct tx_desc {
208527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
209527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
210527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
211527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
212527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
213527a6266SJeff Kirsher };
214527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
215527a6266SJeff Kirsher struct rx_desc {
216527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
217527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
218527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
219527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
220527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
221527a6266SJeff Kirsher };
222527a6266SJeff Kirsher 
223527a6266SJeff Kirsher struct tx_desc {
224527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
225527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
226527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
227527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
228527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
229527a6266SJeff Kirsher };
230527a6266SJeff Kirsher #else
231527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
232527a6266SJeff Kirsher #endif
233527a6266SJeff Kirsher 
234527a6266SJeff Kirsher /* RX & TX descriptor command */
235527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA		0x80000000
236527a6266SJeff Kirsher 
237527a6266SJeff Kirsher /* RX & TX descriptor status */
238527a6266SJeff Kirsher #define ERROR_SUMMARY			0x00000001
239527a6266SJeff Kirsher 
240527a6266SJeff Kirsher /* RX descriptor status */
241527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK		0x40000000
242527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT		0x20000000
243527a6266SJeff Kirsher #define RX_FIRST_DESC			0x08000000
244527a6266SJeff Kirsher #define RX_LAST_DESC			0x04000000
245527a6266SJeff Kirsher #define RX_IP_HDR_OK			0x02000000
246527a6266SJeff Kirsher #define RX_PKT_IS_IPV4			0x01000000
247527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2		0x00800000
248527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK		0x00600000
249527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4	0x00000000
250527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED		0x00080000
251527a6266SJeff Kirsher 
252527a6266SJeff Kirsher /* TX descriptor command */
253527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT		0x00800000
254527a6266SJeff Kirsher #define GEN_CRC				0x00400000
255527a6266SJeff Kirsher #define TX_FIRST_DESC			0x00200000
256527a6266SJeff Kirsher #define TX_LAST_DESC			0x00100000
257527a6266SJeff Kirsher #define ZERO_PADDING			0x00080000
258527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM		0x00040000
259527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM		0x00020000
260527a6266SJeff Kirsher #define UDP_FRAME			0x00010000
261527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES		0x00008000
26284411f73SEzequiel Garcia #define GEN_TCP_UDP_CHK_FULL		0x00000400
263527a6266SJeff Kirsher #define MAC_HDR_EXTRA_8_BYTES		0x00000200
264527a6266SJeff Kirsher 
265527a6266SJeff Kirsher #define TX_IHL_SHIFT			11
266527a6266SJeff Kirsher 
267527a6266SJeff Kirsher 
268527a6266SJeff Kirsher /* global *******************************************************************/
269527a6266SJeff Kirsher struct mv643xx_eth_shared_private {
270527a6266SJeff Kirsher 	/*
271527a6266SJeff Kirsher 	 * Ethernet controller base address.
272527a6266SJeff Kirsher 	 */
273527a6266SJeff Kirsher 	void __iomem *base;
274527a6266SJeff Kirsher 
275527a6266SJeff Kirsher 	/*
276527a6266SJeff Kirsher 	 * Per-port MBUS window access register value.
277527a6266SJeff Kirsher 	 */
278527a6266SJeff Kirsher 	u32 win_protect;
279527a6266SJeff Kirsher 
280527a6266SJeff Kirsher 	/*
281527a6266SJeff Kirsher 	 * Hardware-specific parameters.
282527a6266SJeff Kirsher 	 */
283527a6266SJeff Kirsher 	int extended_rx_coal_limit;
284527a6266SJeff Kirsher 	int tx_bw_control;
285527a6266SJeff Kirsher 	int tx_csum_limit;
28620922486SSebastian Hesselbarth 	struct clk *clk;
287527a6266SJeff Kirsher };
288527a6266SJeff Kirsher 
289527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT		0
290527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT	1
291527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT	2
292527a6266SJeff Kirsher 
293527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev);
294527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev);
295527a6266SJeff Kirsher 
296527a6266SJeff Kirsher 
297527a6266SJeff Kirsher /* per-port *****************************************************************/
298527a6266SJeff Kirsher struct mib_counters {
299527a6266SJeff Kirsher 	u64 good_octets_received;
300527a6266SJeff Kirsher 	u32 bad_octets_received;
301527a6266SJeff Kirsher 	u32 internal_mac_transmit_err;
302527a6266SJeff Kirsher 	u32 good_frames_received;
303527a6266SJeff Kirsher 	u32 bad_frames_received;
304527a6266SJeff Kirsher 	u32 broadcast_frames_received;
305527a6266SJeff Kirsher 	u32 multicast_frames_received;
306527a6266SJeff Kirsher 	u32 frames_64_octets;
307527a6266SJeff Kirsher 	u32 frames_65_to_127_octets;
308527a6266SJeff Kirsher 	u32 frames_128_to_255_octets;
309527a6266SJeff Kirsher 	u32 frames_256_to_511_octets;
310527a6266SJeff Kirsher 	u32 frames_512_to_1023_octets;
311527a6266SJeff Kirsher 	u32 frames_1024_to_max_octets;
312527a6266SJeff Kirsher 	u64 good_octets_sent;
313527a6266SJeff Kirsher 	u32 good_frames_sent;
314527a6266SJeff Kirsher 	u32 excessive_collision;
315527a6266SJeff Kirsher 	u32 multicast_frames_sent;
316527a6266SJeff Kirsher 	u32 broadcast_frames_sent;
317527a6266SJeff Kirsher 	u32 unrec_mac_control_received;
318527a6266SJeff Kirsher 	u32 fc_sent;
319527a6266SJeff Kirsher 	u32 good_fc_received;
320527a6266SJeff Kirsher 	u32 bad_fc_received;
321527a6266SJeff Kirsher 	u32 undersize_received;
322527a6266SJeff Kirsher 	u32 fragments_received;
323527a6266SJeff Kirsher 	u32 oversize_received;
324527a6266SJeff Kirsher 	u32 jabber_received;
325527a6266SJeff Kirsher 	u32 mac_receive_error;
326527a6266SJeff Kirsher 	u32 bad_crc_event;
327527a6266SJeff Kirsher 	u32 collision;
328527a6266SJeff Kirsher 	u32 late_collision;
329302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
330302476c9SPaulius Zaleckas 	u32 rx_discard;
331302476c9SPaulius Zaleckas 	u32 rx_overrun;
332527a6266SJeff Kirsher };
333527a6266SJeff Kirsher 
334527a6266SJeff Kirsher struct rx_queue {
335527a6266SJeff Kirsher 	int index;
336527a6266SJeff Kirsher 
337527a6266SJeff Kirsher 	int rx_ring_size;
338527a6266SJeff Kirsher 
339527a6266SJeff Kirsher 	int rx_desc_count;
340527a6266SJeff Kirsher 	int rx_curr_desc;
341527a6266SJeff Kirsher 	int rx_used_desc;
342527a6266SJeff Kirsher 
343527a6266SJeff Kirsher 	struct rx_desc *rx_desc_area;
344527a6266SJeff Kirsher 	dma_addr_t rx_desc_dma;
345527a6266SJeff Kirsher 	int rx_desc_area_size;
346527a6266SJeff Kirsher 	struct sk_buff **rx_skb;
347527a6266SJeff Kirsher };
348527a6266SJeff Kirsher 
349527a6266SJeff Kirsher struct tx_queue {
350527a6266SJeff Kirsher 	int index;
351527a6266SJeff Kirsher 
352527a6266SJeff Kirsher 	int tx_ring_size;
353527a6266SJeff Kirsher 
354527a6266SJeff Kirsher 	int tx_desc_count;
355527a6266SJeff Kirsher 	int tx_curr_desc;
356527a6266SJeff Kirsher 	int tx_used_desc;
357527a6266SJeff Kirsher 
358ee9e4956SEzequiel Garcia 	int tx_stop_threshold;
359ee9e4956SEzequiel Garcia 	int tx_wake_threshold;
360ee9e4956SEzequiel Garcia 
3613ae8f4e0SEzequiel Garcia 	char *tso_hdrs;
3623ae8f4e0SEzequiel Garcia 	dma_addr_t tso_hdrs_dma;
3633ae8f4e0SEzequiel Garcia 
364527a6266SJeff Kirsher 	struct tx_desc *tx_desc_area;
365527a6266SJeff Kirsher 	dma_addr_t tx_desc_dma;
366527a6266SJeff Kirsher 	int tx_desc_area_size;
367527a6266SJeff Kirsher 
368527a6266SJeff Kirsher 	struct sk_buff_head tx_skb;
369527a6266SJeff Kirsher 
370527a6266SJeff Kirsher 	unsigned long tx_packets;
371527a6266SJeff Kirsher 	unsigned long tx_bytes;
372527a6266SJeff Kirsher 	unsigned long tx_dropped;
373527a6266SJeff Kirsher };
374527a6266SJeff Kirsher 
375527a6266SJeff Kirsher struct mv643xx_eth_private {
376527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *shared;
377527a6266SJeff Kirsher 	void __iomem *base;
378527a6266SJeff Kirsher 	int port_num;
379527a6266SJeff Kirsher 
380527a6266SJeff Kirsher 	struct net_device *dev;
381527a6266SJeff Kirsher 
382527a6266SJeff Kirsher 	struct phy_device *phy;
383527a6266SJeff Kirsher 
384527a6266SJeff Kirsher 	struct timer_list mib_counters_timer;
385527a6266SJeff Kirsher 	spinlock_t mib_counters_lock;
386527a6266SJeff Kirsher 	struct mib_counters mib_counters;
387527a6266SJeff Kirsher 
388527a6266SJeff Kirsher 	struct work_struct tx_timeout_task;
389527a6266SJeff Kirsher 
390527a6266SJeff Kirsher 	struct napi_struct napi;
391527a6266SJeff Kirsher 	u32 int_mask;
392527a6266SJeff Kirsher 	u8 oom;
393527a6266SJeff Kirsher 	u8 work_link;
394527a6266SJeff Kirsher 	u8 work_tx;
395527a6266SJeff Kirsher 	u8 work_tx_end;
396527a6266SJeff Kirsher 	u8 work_rx;
397527a6266SJeff Kirsher 	u8 work_rx_refill;
398527a6266SJeff Kirsher 
399527a6266SJeff Kirsher 	int skb_size;
400527a6266SJeff Kirsher 
401527a6266SJeff Kirsher 	/*
402527a6266SJeff Kirsher 	 * RX state.
403527a6266SJeff Kirsher 	 */
404527a6266SJeff Kirsher 	int rx_ring_size;
405527a6266SJeff Kirsher 	unsigned long rx_desc_sram_addr;
406527a6266SJeff Kirsher 	int rx_desc_sram_size;
407527a6266SJeff Kirsher 	int rxq_count;
408527a6266SJeff Kirsher 	struct timer_list rx_oom;
409527a6266SJeff Kirsher 	struct rx_queue rxq[8];
410527a6266SJeff Kirsher 
411527a6266SJeff Kirsher 	/*
412527a6266SJeff Kirsher 	 * TX state.
413527a6266SJeff Kirsher 	 */
414527a6266SJeff Kirsher 	int tx_ring_size;
415527a6266SJeff Kirsher 	unsigned long tx_desc_sram_addr;
416527a6266SJeff Kirsher 	int tx_desc_sram_size;
417527a6266SJeff Kirsher 	int txq_count;
418527a6266SJeff Kirsher 	struct tx_queue txq[8];
419452503ebSAndrew Lunn 
420452503ebSAndrew Lunn 	/*
421452503ebSAndrew Lunn 	 * Hardware-specific parameters.
422452503ebSAndrew Lunn 	 */
423452503ebSAndrew Lunn 	struct clk *clk;
424452503ebSAndrew Lunn 	unsigned int t_clk;
425527a6266SJeff Kirsher };
426527a6266SJeff Kirsher 
427527a6266SJeff Kirsher 
428527a6266SJeff Kirsher /* port register accessors **************************************************/
429527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
430527a6266SJeff Kirsher {
431527a6266SJeff Kirsher 	return readl(mp->shared->base + offset);
432527a6266SJeff Kirsher }
433527a6266SJeff Kirsher 
434527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
435527a6266SJeff Kirsher {
436527a6266SJeff Kirsher 	return readl(mp->base + offset);
437527a6266SJeff Kirsher }
438527a6266SJeff Kirsher 
439527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
440527a6266SJeff Kirsher {
441527a6266SJeff Kirsher 	writel(data, mp->shared->base + offset);
442527a6266SJeff Kirsher }
443527a6266SJeff Kirsher 
444527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
445527a6266SJeff Kirsher {
446527a6266SJeff Kirsher 	writel(data, mp->base + offset);
447527a6266SJeff Kirsher }
448527a6266SJeff Kirsher 
449527a6266SJeff Kirsher 
450527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/
451527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
452527a6266SJeff Kirsher {
453527a6266SJeff Kirsher 	return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
454527a6266SJeff Kirsher }
455527a6266SJeff Kirsher 
456527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
457527a6266SJeff Kirsher {
458527a6266SJeff Kirsher 	return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
459527a6266SJeff Kirsher }
460527a6266SJeff Kirsher 
461527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq)
462527a6266SJeff Kirsher {
463527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
464527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
465527a6266SJeff Kirsher }
466527a6266SJeff Kirsher 
467527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq)
468527a6266SJeff Kirsher {
469527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
470527a6266SJeff Kirsher 	u8 mask = 1 << rxq->index;
471527a6266SJeff Kirsher 
472527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, mask << 8);
473527a6266SJeff Kirsher 	while (rdlp(mp, RXQ_COMMAND) & mask)
474527a6266SJeff Kirsher 		udelay(10);
475527a6266SJeff Kirsher }
476527a6266SJeff Kirsher 
477527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq)
478527a6266SJeff Kirsher {
479527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
480527a6266SJeff Kirsher 	u32 addr;
481527a6266SJeff Kirsher 
482527a6266SJeff Kirsher 	addr = (u32)txq->tx_desc_dma;
483527a6266SJeff Kirsher 	addr += txq->tx_curr_desc * sizeof(struct tx_desc);
484527a6266SJeff Kirsher 	wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
485527a6266SJeff Kirsher }
486527a6266SJeff Kirsher 
487527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq)
488527a6266SJeff Kirsher {
489527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
490527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, 1 << txq->index);
491527a6266SJeff Kirsher }
492527a6266SJeff Kirsher 
493527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq)
494527a6266SJeff Kirsher {
495527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
496527a6266SJeff Kirsher 	u8 mask = 1 << txq->index;
497527a6266SJeff Kirsher 
498527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, mask << 8);
499527a6266SJeff Kirsher 	while (rdlp(mp, TXQ_COMMAND) & mask)
500527a6266SJeff Kirsher 		udelay(10);
501527a6266SJeff Kirsher }
502527a6266SJeff Kirsher 
503527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq)
504527a6266SJeff Kirsher {
505527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
506527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
507527a6266SJeff Kirsher 
508527a6266SJeff Kirsher 	if (netif_tx_queue_stopped(nq)) {
509527a6266SJeff Kirsher 		__netif_tx_lock(nq, smp_processor_id());
510ee9e4956SEzequiel Garcia 		if (txq->tx_desc_count <= txq->tx_wake_threshold)
511527a6266SJeff Kirsher 			netif_tx_wake_queue(nq);
512527a6266SJeff Kirsher 		__netif_tx_unlock(nq);
513527a6266SJeff Kirsher 	}
514527a6266SJeff Kirsher }
515527a6266SJeff Kirsher 
516527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget)
517527a6266SJeff Kirsher {
518527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
519527a6266SJeff Kirsher 	struct net_device_stats *stats = &mp->dev->stats;
520527a6266SJeff Kirsher 	int rx;
521527a6266SJeff Kirsher 
522527a6266SJeff Kirsher 	rx = 0;
523527a6266SJeff Kirsher 	while (rx < budget && rxq->rx_desc_count) {
524527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
525527a6266SJeff Kirsher 		unsigned int cmd_sts;
526527a6266SJeff Kirsher 		struct sk_buff *skb;
527527a6266SJeff Kirsher 		u16 byte_cnt;
528527a6266SJeff Kirsher 
529527a6266SJeff Kirsher 		rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
530527a6266SJeff Kirsher 
531527a6266SJeff Kirsher 		cmd_sts = rx_desc->cmd_sts;
532527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA)
533527a6266SJeff Kirsher 			break;
534527a6266SJeff Kirsher 		rmb();
535527a6266SJeff Kirsher 
536527a6266SJeff Kirsher 		skb = rxq->rx_skb[rxq->rx_curr_desc];
537527a6266SJeff Kirsher 		rxq->rx_skb[rxq->rx_curr_desc] = NULL;
538527a6266SJeff Kirsher 
539527a6266SJeff Kirsher 		rxq->rx_curr_desc++;
540527a6266SJeff Kirsher 		if (rxq->rx_curr_desc == rxq->rx_ring_size)
541527a6266SJeff Kirsher 			rxq->rx_curr_desc = 0;
542527a6266SJeff Kirsher 
543527a6266SJeff Kirsher 		dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
544527a6266SJeff Kirsher 				 rx_desc->buf_size, DMA_FROM_DEVICE);
545527a6266SJeff Kirsher 		rxq->rx_desc_count--;
546527a6266SJeff Kirsher 		rx++;
547527a6266SJeff Kirsher 
548527a6266SJeff Kirsher 		mp->work_rx_refill |= 1 << rxq->index;
549527a6266SJeff Kirsher 
550527a6266SJeff Kirsher 		byte_cnt = rx_desc->byte_cnt;
551527a6266SJeff Kirsher 
552527a6266SJeff Kirsher 		/*
553527a6266SJeff Kirsher 		 * Update statistics.
554527a6266SJeff Kirsher 		 *
555527a6266SJeff Kirsher 		 * Note that the descriptor byte count includes 2 dummy
556527a6266SJeff Kirsher 		 * bytes automatically inserted by the hardware at the
557527a6266SJeff Kirsher 		 * start of the packet (which we don't count), and a 4
558527a6266SJeff Kirsher 		 * byte CRC at the end of the packet (which we do count).
559527a6266SJeff Kirsher 		 */
560527a6266SJeff Kirsher 		stats->rx_packets++;
561527a6266SJeff Kirsher 		stats->rx_bytes += byte_cnt - 2;
562527a6266SJeff Kirsher 
563527a6266SJeff Kirsher 		/*
564527a6266SJeff Kirsher 		 * In case we received a packet without first / last bits
565527a6266SJeff Kirsher 		 * on, or the error summary bit is set, the packet needs
566527a6266SJeff Kirsher 		 * to be dropped.
567527a6266SJeff Kirsher 		 */
568527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
569527a6266SJeff Kirsher 			!= (RX_FIRST_DESC | RX_LAST_DESC))
570527a6266SJeff Kirsher 			goto err;
571527a6266SJeff Kirsher 
572527a6266SJeff Kirsher 		/*
573527a6266SJeff Kirsher 		 * The -4 is for the CRC in the trailer of the
574527a6266SJeff Kirsher 		 * received packet
575527a6266SJeff Kirsher 		 */
576527a6266SJeff Kirsher 		skb_put(skb, byte_cnt - 2 - 4);
577527a6266SJeff Kirsher 
578527a6266SJeff Kirsher 		if (cmd_sts & LAYER_4_CHECKSUM_OK)
579527a6266SJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
580527a6266SJeff Kirsher 		skb->protocol = eth_type_trans(skb, mp->dev);
581527a6266SJeff Kirsher 
58209bf1c10SSebastian Hesselbarth 		napi_gro_receive(&mp->napi, skb);
583527a6266SJeff Kirsher 
584527a6266SJeff Kirsher 		continue;
585527a6266SJeff Kirsher 
586527a6266SJeff Kirsher err:
587527a6266SJeff Kirsher 		stats->rx_dropped++;
588527a6266SJeff Kirsher 
589527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
590527a6266SJeff Kirsher 			(RX_FIRST_DESC | RX_LAST_DESC)) {
591527a6266SJeff Kirsher 			if (net_ratelimit())
592527a6266SJeff Kirsher 				netdev_err(mp->dev,
593527a6266SJeff Kirsher 					   "received packet spanning multiple descriptors\n");
594527a6266SJeff Kirsher 		}
595527a6266SJeff Kirsher 
596527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY)
597527a6266SJeff Kirsher 			stats->rx_errors++;
598527a6266SJeff Kirsher 
599527a6266SJeff Kirsher 		dev_kfree_skb(skb);
600527a6266SJeff Kirsher 	}
601527a6266SJeff Kirsher 
602527a6266SJeff Kirsher 	if (rx < budget)
603527a6266SJeff Kirsher 		mp->work_rx &= ~(1 << rxq->index);
604527a6266SJeff Kirsher 
605527a6266SJeff Kirsher 	return rx;
606527a6266SJeff Kirsher }
607527a6266SJeff Kirsher 
608527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget)
609527a6266SJeff Kirsher {
610527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
611527a6266SJeff Kirsher 	int refilled;
612527a6266SJeff Kirsher 
613527a6266SJeff Kirsher 	refilled = 0;
614527a6266SJeff Kirsher 	while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
615527a6266SJeff Kirsher 		struct sk_buff *skb;
616527a6266SJeff Kirsher 		int rx;
617527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
618527a6266SJeff Kirsher 		int size;
619527a6266SJeff Kirsher 
620c056b734SPradeep A Dalvi 		skb = netdev_alloc_skb(mp->dev, mp->skb_size);
621527a6266SJeff Kirsher 
622527a6266SJeff Kirsher 		if (skb == NULL) {
623527a6266SJeff Kirsher 			mp->oom = 1;
624527a6266SJeff Kirsher 			goto oom;
625527a6266SJeff Kirsher 		}
626527a6266SJeff Kirsher 
627527a6266SJeff Kirsher 		if (SKB_DMA_REALIGN)
628527a6266SJeff Kirsher 			skb_reserve(skb, SKB_DMA_REALIGN);
629527a6266SJeff Kirsher 
630527a6266SJeff Kirsher 		refilled++;
631527a6266SJeff Kirsher 		rxq->rx_desc_count++;
632527a6266SJeff Kirsher 
633527a6266SJeff Kirsher 		rx = rxq->rx_used_desc++;
634527a6266SJeff Kirsher 		if (rxq->rx_used_desc == rxq->rx_ring_size)
635527a6266SJeff Kirsher 			rxq->rx_used_desc = 0;
636527a6266SJeff Kirsher 
637527a6266SJeff Kirsher 		rx_desc = rxq->rx_desc_area + rx;
638527a6266SJeff Kirsher 
63918f1d054SIsaku Yamahata 		size = skb_end_pointer(skb) - skb->data;
640527a6266SJeff Kirsher 		rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
641527a6266SJeff Kirsher 						  skb->data, size,
642527a6266SJeff Kirsher 						  DMA_FROM_DEVICE);
643527a6266SJeff Kirsher 		rx_desc->buf_size = size;
644527a6266SJeff Kirsher 		rxq->rx_skb[rx] = skb;
645527a6266SJeff Kirsher 		wmb();
646527a6266SJeff Kirsher 		rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
647527a6266SJeff Kirsher 		wmb();
648527a6266SJeff Kirsher 
649527a6266SJeff Kirsher 		/*
650527a6266SJeff Kirsher 		 * The hardware automatically prepends 2 bytes of
651527a6266SJeff Kirsher 		 * dummy data to each received packet, so that the
652527a6266SJeff Kirsher 		 * IP header ends up 16-byte aligned.
653527a6266SJeff Kirsher 		 */
654527a6266SJeff Kirsher 		skb_reserve(skb, 2);
655527a6266SJeff Kirsher 	}
656527a6266SJeff Kirsher 
657527a6266SJeff Kirsher 	if (refilled < budget)
658527a6266SJeff Kirsher 		mp->work_rx_refill &= ~(1 << rxq->index);
659527a6266SJeff Kirsher 
660527a6266SJeff Kirsher oom:
661527a6266SJeff Kirsher 	return refilled;
662527a6266SJeff Kirsher }
663527a6266SJeff Kirsher 
664527a6266SJeff Kirsher 
665527a6266SJeff Kirsher /* tx ***********************************************************************/
666527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
667527a6266SJeff Kirsher {
668527a6266SJeff Kirsher 	int frag;
669527a6266SJeff Kirsher 
670527a6266SJeff Kirsher 	for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
6719e903e08SEric Dumazet 		const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
6729e903e08SEric Dumazet 
6739e903e08SEric Dumazet 		if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
674527a6266SJeff Kirsher 			return 1;
675527a6266SJeff Kirsher 	}
676527a6266SJeff Kirsher 
677527a6266SJeff Kirsher 	return 0;
678527a6266SJeff Kirsher }
679527a6266SJeff Kirsher 
6800a8fa933SEzequiel Garcia static inline __be16 sum16_as_be(__sum16 sum)
6810a8fa933SEzequiel Garcia {
6820a8fa933SEzequiel Garcia 	return (__force __be16)sum;
6830a8fa933SEzequiel Garcia }
6840a8fa933SEzequiel Garcia 
6850a8fa933SEzequiel Garcia static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb,
6860a8fa933SEzequiel Garcia 		       u16 *l4i_chk, u32 *command, int length)
6870a8fa933SEzequiel Garcia {
6880a8fa933SEzequiel Garcia 	int ret;
6890a8fa933SEzequiel Garcia 	u32 cmd = 0;
6900a8fa933SEzequiel Garcia 
6910a8fa933SEzequiel Garcia 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
6920a8fa933SEzequiel Garcia 		int hdr_len;
6930a8fa933SEzequiel Garcia 		int tag_bytes;
6940a8fa933SEzequiel Garcia 
6950a8fa933SEzequiel Garcia 		BUG_ON(skb->protocol != htons(ETH_P_IP) &&
6960a8fa933SEzequiel Garcia 		       skb->protocol != htons(ETH_P_8021Q));
6970a8fa933SEzequiel Garcia 
6980a8fa933SEzequiel Garcia 		hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
6990a8fa933SEzequiel Garcia 		tag_bytes = hdr_len - ETH_HLEN;
7000a8fa933SEzequiel Garcia 
7010a8fa933SEzequiel Garcia 		if (length - hdr_len > mp->shared->tx_csum_limit ||
7020a8fa933SEzequiel Garcia 		    unlikely(tag_bytes & ~12)) {
7030a8fa933SEzequiel Garcia 			ret = skb_checksum_help(skb);
7040a8fa933SEzequiel Garcia 			if (!ret)
7050a8fa933SEzequiel Garcia 				goto no_csum;
7060a8fa933SEzequiel Garcia 			return ret;
7070a8fa933SEzequiel Garcia 		}
7080a8fa933SEzequiel Garcia 
7090a8fa933SEzequiel Garcia 		if (tag_bytes & 4)
7100a8fa933SEzequiel Garcia 			cmd |= MAC_HDR_EXTRA_4_BYTES;
7110a8fa933SEzequiel Garcia 		if (tag_bytes & 8)
7120a8fa933SEzequiel Garcia 			cmd |= MAC_HDR_EXTRA_8_BYTES;
7130a8fa933SEzequiel Garcia 
71484411f73SEzequiel Garcia 		cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL |
7150a8fa933SEzequiel Garcia 			   GEN_IP_V4_CHECKSUM   |
7160a8fa933SEzequiel Garcia 			   ip_hdr(skb)->ihl << TX_IHL_SHIFT;
7170a8fa933SEzequiel Garcia 
71884411f73SEzequiel Garcia 		/* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL
71984411f73SEzequiel Garcia 		 * it seems we don't need to pass the initial checksum. */
7200a8fa933SEzequiel Garcia 		switch (ip_hdr(skb)->protocol) {
7210a8fa933SEzequiel Garcia 		case IPPROTO_UDP:
7220a8fa933SEzequiel Garcia 			cmd |= UDP_FRAME;
72384411f73SEzequiel Garcia 			*l4i_chk = 0;
7240a8fa933SEzequiel Garcia 			break;
7250a8fa933SEzequiel Garcia 		case IPPROTO_TCP:
72684411f73SEzequiel Garcia 			*l4i_chk = 0;
7270a8fa933SEzequiel Garcia 			break;
7280a8fa933SEzequiel Garcia 		default:
7290a8fa933SEzequiel Garcia 			WARN(1, "protocol not supported");
7300a8fa933SEzequiel Garcia 		}
7310a8fa933SEzequiel Garcia 	} else {
7320a8fa933SEzequiel Garcia no_csum:
7330a8fa933SEzequiel Garcia 		/* Errata BTS #50, IHL must be 5 if no HW checksum */
7340a8fa933SEzequiel Garcia 		cmd |= 5 << TX_IHL_SHIFT;
7350a8fa933SEzequiel Garcia 	}
7360a8fa933SEzequiel Garcia 	*command = cmd;
7370a8fa933SEzequiel Garcia 	return 0;
7380a8fa933SEzequiel Garcia }
7390a8fa933SEzequiel Garcia 
7403ae8f4e0SEzequiel Garcia static inline int
7413ae8f4e0SEzequiel Garcia txq_put_data_tso(struct net_device *dev, struct tx_queue *txq,
7423ae8f4e0SEzequiel Garcia 		 struct sk_buff *skb, char *data, int length,
7433ae8f4e0SEzequiel Garcia 		 bool last_tcp, bool is_last)
7443ae8f4e0SEzequiel Garcia {
7453ae8f4e0SEzequiel Garcia 	int tx_index;
7463ae8f4e0SEzequiel Garcia 	u32 cmd_sts;
7473ae8f4e0SEzequiel Garcia 	struct tx_desc *desc;
7483ae8f4e0SEzequiel Garcia 
7493ae8f4e0SEzequiel Garcia 	tx_index = txq->tx_curr_desc++;
7503ae8f4e0SEzequiel Garcia 	if (txq->tx_curr_desc == txq->tx_ring_size)
7513ae8f4e0SEzequiel Garcia 		txq->tx_curr_desc = 0;
7523ae8f4e0SEzequiel Garcia 	desc = &txq->tx_desc_area[tx_index];
7533ae8f4e0SEzequiel Garcia 
7543ae8f4e0SEzequiel Garcia 	desc->l4i_chk = 0;
7553ae8f4e0SEzequiel Garcia 	desc->byte_cnt = length;
7563ae8f4e0SEzequiel Garcia 	desc->buf_ptr = dma_map_single(dev->dev.parent, data,
7573ae8f4e0SEzequiel Garcia 				       length, DMA_TO_DEVICE);
7583ae8f4e0SEzequiel Garcia 	if (unlikely(dma_mapping_error(dev->dev.parent, desc->buf_ptr))) {
7593ae8f4e0SEzequiel Garcia 		WARN(1, "dma_map_single failed!\n");
7603ae8f4e0SEzequiel Garcia 		return -ENOMEM;
7613ae8f4e0SEzequiel Garcia 	}
7623ae8f4e0SEzequiel Garcia 
7633ae8f4e0SEzequiel Garcia 	cmd_sts = BUFFER_OWNED_BY_DMA;
7643ae8f4e0SEzequiel Garcia 	if (last_tcp) {
7653ae8f4e0SEzequiel Garcia 		/* last descriptor in the TCP packet */
7663ae8f4e0SEzequiel Garcia 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC;
7673ae8f4e0SEzequiel Garcia 		/* last descriptor in SKB */
7683ae8f4e0SEzequiel Garcia 		if (is_last)
7693ae8f4e0SEzequiel Garcia 			cmd_sts |= TX_ENABLE_INTERRUPT;
7703ae8f4e0SEzequiel Garcia 	}
7713ae8f4e0SEzequiel Garcia 	desc->cmd_sts = cmd_sts;
7723ae8f4e0SEzequiel Garcia 	return 0;
7733ae8f4e0SEzequiel Garcia }
7743ae8f4e0SEzequiel Garcia 
7753ae8f4e0SEzequiel Garcia static inline void
7763ae8f4e0SEzequiel Garcia txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length)
7773ae8f4e0SEzequiel Garcia {
7783ae8f4e0SEzequiel Garcia 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
7793ae8f4e0SEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
7803ae8f4e0SEzequiel Garcia 	int tx_index;
7813ae8f4e0SEzequiel Garcia 	struct tx_desc *desc;
7823ae8f4e0SEzequiel Garcia 	int ret;
7833ae8f4e0SEzequiel Garcia 	u32 cmd_csum = 0;
7843ae8f4e0SEzequiel Garcia 	u16 l4i_chk = 0;
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()
7943ae8f4e0SEzequiel Garcia 	 * as it's not the correct initial L4 checksum to use. */
7953ae8f4e0SEzequiel Garcia 	desc->l4i_chk = 0;
7963ae8f4e0SEzequiel Garcia 
7973ae8f4e0SEzequiel Garcia 	desc->byte_cnt = hdr_len;
7983ae8f4e0SEzequiel Garcia 	desc->buf_ptr = txq->tso_hdrs_dma +
7993ae8f4e0SEzequiel Garcia 			txq->tx_curr_desc * TSO_HEADER_SIZE;
8003ae8f4e0SEzequiel Garcia 	desc->cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA  | TX_FIRST_DESC |
8013ae8f4e0SEzequiel Garcia 				   GEN_CRC;
8023ae8f4e0SEzequiel Garcia 
8033ae8f4e0SEzequiel Garcia 	txq->tx_curr_desc++;
8043ae8f4e0SEzequiel Garcia 	if (txq->tx_curr_desc == txq->tx_ring_size)
8053ae8f4e0SEzequiel Garcia 		txq->tx_curr_desc = 0;
8063ae8f4e0SEzequiel Garcia }
8073ae8f4e0SEzequiel Garcia 
8083ae8f4e0SEzequiel Garcia static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb,
8093ae8f4e0SEzequiel Garcia 			  struct net_device *dev)
8103ae8f4e0SEzequiel Garcia {
8113ae8f4e0SEzequiel Garcia 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
8123ae8f4e0SEzequiel Garcia 	int total_len, data_left, ret;
8133ae8f4e0SEzequiel Garcia 	int desc_count = 0;
8143ae8f4e0SEzequiel Garcia 	struct tso_t tso;
8153ae8f4e0SEzequiel Garcia 	int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
8163ae8f4e0SEzequiel Garcia 
8173ae8f4e0SEzequiel Garcia 	/* Count needed descriptors */
8183ae8f4e0SEzequiel Garcia 	if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) {
8193ae8f4e0SEzequiel Garcia 		netdev_dbg(dev, "not enough descriptors for TSO!\n");
8203ae8f4e0SEzequiel Garcia 		return -EBUSY;
8213ae8f4e0SEzequiel Garcia 	}
8223ae8f4e0SEzequiel Garcia 
8233ae8f4e0SEzequiel Garcia 	/* Initialize the TSO handler, and prepare the first payload */
8243ae8f4e0SEzequiel Garcia 	tso_start(skb, &tso);
8253ae8f4e0SEzequiel Garcia 
8263ae8f4e0SEzequiel Garcia 	total_len = skb->len - hdr_len;
8273ae8f4e0SEzequiel Garcia 	while (total_len > 0) {
8283ae8f4e0SEzequiel Garcia 		char *hdr;
8293ae8f4e0SEzequiel Garcia 
8303ae8f4e0SEzequiel Garcia 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
8313ae8f4e0SEzequiel Garcia 		total_len -= data_left;
8323ae8f4e0SEzequiel Garcia 		desc_count++;
8333ae8f4e0SEzequiel Garcia 
8343ae8f4e0SEzequiel Garcia 		/* prepare packet headers: MAC + IP + TCP */
8353ae8f4e0SEzequiel Garcia 		hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE;
8363ae8f4e0SEzequiel Garcia 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
8373ae8f4e0SEzequiel Garcia 		txq_put_hdr_tso(skb, txq, data_left);
8383ae8f4e0SEzequiel Garcia 
8393ae8f4e0SEzequiel Garcia 		while (data_left > 0) {
8403ae8f4e0SEzequiel Garcia 			int size;
8413ae8f4e0SEzequiel Garcia 			desc_count++;
8423ae8f4e0SEzequiel Garcia 
8433ae8f4e0SEzequiel Garcia 			size = min_t(int, tso.size, data_left);
8443ae8f4e0SEzequiel Garcia 			ret = txq_put_data_tso(dev, txq, skb, tso.data, size,
8453ae8f4e0SEzequiel Garcia 					       size == data_left,
8463ae8f4e0SEzequiel Garcia 					       total_len == 0);
8473ae8f4e0SEzequiel Garcia 			if (ret)
8483ae8f4e0SEzequiel Garcia 				goto err_release;
8493ae8f4e0SEzequiel Garcia 			data_left -= size;
8503ae8f4e0SEzequiel Garcia 			tso_build_data(skb, &tso, size);
8513ae8f4e0SEzequiel Garcia 		}
8523ae8f4e0SEzequiel Garcia 	}
8533ae8f4e0SEzequiel Garcia 
8543ae8f4e0SEzequiel Garcia 	__skb_queue_tail(&txq->tx_skb, skb);
8553ae8f4e0SEzequiel Garcia 	skb_tx_timestamp(skb);
8563ae8f4e0SEzequiel Garcia 
8573ae8f4e0SEzequiel Garcia 	/* clear TX_END status */
8583ae8f4e0SEzequiel Garcia 	mp->work_tx_end &= ~(1 << txq->index);
8593ae8f4e0SEzequiel Garcia 
8603ae8f4e0SEzequiel Garcia 	/* ensure all descriptors are written before poking hardware */
8613ae8f4e0SEzequiel Garcia 	wmb();
8623ae8f4e0SEzequiel Garcia 	txq_enable(txq);
8633ae8f4e0SEzequiel Garcia 	txq->tx_desc_count += desc_count;
8643ae8f4e0SEzequiel Garcia 	return 0;
8653ae8f4e0SEzequiel Garcia err_release:
8663ae8f4e0SEzequiel Garcia 	/* TODO: Release all used data descriptors; header descriptors must not
8673ae8f4e0SEzequiel Garcia 	 * be DMA-unmapped.
8683ae8f4e0SEzequiel Garcia 	 */
8693ae8f4e0SEzequiel Garcia 	return ret;
8703ae8f4e0SEzequiel Garcia }
8713ae8f4e0SEzequiel Garcia 
872527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
873527a6266SJeff Kirsher {
874527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
875527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
876527a6266SJeff Kirsher 	int frag;
877527a6266SJeff Kirsher 
878527a6266SJeff Kirsher 	for (frag = 0; frag < nr_frags; frag++) {
879527a6266SJeff Kirsher 		skb_frag_t *this_frag;
880527a6266SJeff Kirsher 		int tx_index;
881527a6266SJeff Kirsher 		struct tx_desc *desc;
88269ad0dd7SEzequiel Garcia 		void *addr;
883527a6266SJeff Kirsher 
884527a6266SJeff Kirsher 		this_frag = &skb_shinfo(skb)->frags[frag];
88569ad0dd7SEzequiel Garcia 		addr = page_address(this_frag->page.p) + this_frag->page_offset;
886527a6266SJeff Kirsher 		tx_index = txq->tx_curr_desc++;
887527a6266SJeff Kirsher 		if (txq->tx_curr_desc == txq->tx_ring_size)
888527a6266SJeff Kirsher 			txq->tx_curr_desc = 0;
889527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
890527a6266SJeff Kirsher 
891527a6266SJeff Kirsher 		/*
892527a6266SJeff Kirsher 		 * The last fragment will generate an interrupt
893527a6266SJeff Kirsher 		 * which will free the skb on TX completion.
894527a6266SJeff Kirsher 		 */
895527a6266SJeff Kirsher 		if (frag == nr_frags - 1) {
896527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA |
897527a6266SJeff Kirsher 					ZERO_PADDING | TX_LAST_DESC |
898527a6266SJeff Kirsher 					TX_ENABLE_INTERRUPT;
899527a6266SJeff Kirsher 		} else {
900527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA;
901527a6266SJeff Kirsher 		}
902527a6266SJeff Kirsher 
903527a6266SJeff Kirsher 		desc->l4i_chk = 0;
9049e903e08SEric Dumazet 		desc->byte_cnt = skb_frag_size(this_frag);
90569ad0dd7SEzequiel Garcia 		desc->buf_ptr = dma_map_single(mp->dev->dev.parent, addr,
90669ad0dd7SEzequiel Garcia 					       desc->byte_cnt, DMA_TO_DEVICE);
907527a6266SJeff Kirsher 	}
908527a6266SJeff Kirsher }
909527a6266SJeff Kirsher 
910ee9e4956SEzequiel Garcia static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb,
911ee9e4956SEzequiel Garcia 			  struct net_device *dev)
912527a6266SJeff Kirsher {
913527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
914527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
915527a6266SJeff Kirsher 	int tx_index;
916527a6266SJeff Kirsher 	struct tx_desc *desc;
917527a6266SJeff Kirsher 	u32 cmd_sts;
918527a6266SJeff Kirsher 	u16 l4i_chk;
9190a8fa933SEzequiel Garcia 	int length, ret;
920527a6266SJeff Kirsher 
9210a8fa933SEzequiel Garcia 	cmd_sts = 0;
922527a6266SJeff Kirsher 	l4i_chk = 0;
923527a6266SJeff Kirsher 
924ee9e4956SEzequiel Garcia 	if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
925ee9e4956SEzequiel Garcia 		if (net_ratelimit())
926ee9e4956SEzequiel Garcia 			netdev_err(dev, "tx queue full?!\n");
927ee9e4956SEzequiel Garcia 		return -EBUSY;
928527a6266SJeff Kirsher 	}
929ee9e4956SEzequiel Garcia 
930ee9e4956SEzequiel Garcia 	ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len);
931ee9e4956SEzequiel Garcia 	if (ret)
932ee9e4956SEzequiel Garcia 		return ret;
9330a8fa933SEzequiel Garcia 	cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
934527a6266SJeff Kirsher 
935527a6266SJeff Kirsher 	tx_index = txq->tx_curr_desc++;
936527a6266SJeff Kirsher 	if (txq->tx_curr_desc == txq->tx_ring_size)
937527a6266SJeff Kirsher 		txq->tx_curr_desc = 0;
938527a6266SJeff Kirsher 	desc = &txq->tx_desc_area[tx_index];
939527a6266SJeff Kirsher 
940527a6266SJeff Kirsher 	if (nr_frags) {
941527a6266SJeff Kirsher 		txq_submit_frag_skb(txq, skb);
942527a6266SJeff Kirsher 		length = skb_headlen(skb);
943527a6266SJeff Kirsher 	} else {
944527a6266SJeff Kirsher 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
945527a6266SJeff Kirsher 		length = skb->len;
946527a6266SJeff Kirsher 	}
947527a6266SJeff Kirsher 
948527a6266SJeff Kirsher 	desc->l4i_chk = l4i_chk;
949527a6266SJeff Kirsher 	desc->byte_cnt = length;
950527a6266SJeff Kirsher 	desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
951527a6266SJeff Kirsher 				       length, DMA_TO_DEVICE);
952527a6266SJeff Kirsher 
953527a6266SJeff Kirsher 	__skb_queue_tail(&txq->tx_skb, skb);
954527a6266SJeff Kirsher 
955527a6266SJeff Kirsher 	skb_tx_timestamp(skb);
956527a6266SJeff Kirsher 
957527a6266SJeff Kirsher 	/* ensure all other descriptors are written before first cmd_sts */
958527a6266SJeff Kirsher 	wmb();
959527a6266SJeff Kirsher 	desc->cmd_sts = cmd_sts;
960527a6266SJeff Kirsher 
961527a6266SJeff Kirsher 	/* clear TX_END status */
962527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
963527a6266SJeff Kirsher 
964527a6266SJeff Kirsher 	/* ensure all descriptors are written before poking hardware */
965527a6266SJeff Kirsher 	wmb();
966527a6266SJeff Kirsher 	txq_enable(txq);
967527a6266SJeff Kirsher 
968527a6266SJeff Kirsher 	txq->tx_desc_count += nr_frags + 1;
969527a6266SJeff Kirsher 
970527a6266SJeff Kirsher 	return 0;
971527a6266SJeff Kirsher }
972527a6266SJeff Kirsher 
973527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
974527a6266SJeff Kirsher {
975527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
9763ae8f4e0SEzequiel Garcia 	int length, queue, ret;
977527a6266SJeff Kirsher 	struct tx_queue *txq;
978527a6266SJeff Kirsher 	struct netdev_queue *nq;
979527a6266SJeff Kirsher 
980527a6266SJeff Kirsher 	queue = skb_get_queue_mapping(skb);
981527a6266SJeff Kirsher 	txq = mp->txq + queue;
982527a6266SJeff Kirsher 	nq = netdev_get_tx_queue(dev, queue);
983527a6266SJeff Kirsher 
984527a6266SJeff Kirsher 	if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
985527a6266SJeff Kirsher 		netdev_printk(KERN_DEBUG, dev,
986527a6266SJeff Kirsher 			      "failed to linearize skb with tiny unaligned fragment\n");
987527a6266SJeff Kirsher 		return NETDEV_TX_BUSY;
988527a6266SJeff Kirsher 	}
989527a6266SJeff Kirsher 
990527a6266SJeff Kirsher 	length = skb->len;
991527a6266SJeff Kirsher 
9923ae8f4e0SEzequiel Garcia 	if (skb_is_gso(skb))
9933ae8f4e0SEzequiel Garcia 		ret = txq_submit_tso(txq, skb, dev);
9943ae8f4e0SEzequiel Garcia 	else
995ee9e4956SEzequiel Garcia 		ret = txq_submit_skb(txq, skb, dev);
9963ae8f4e0SEzequiel Garcia 	if (!ret) {
997527a6266SJeff Kirsher 		txq->tx_bytes += length;
998527a6266SJeff Kirsher 		txq->tx_packets++;
999527a6266SJeff Kirsher 
1000ee9e4956SEzequiel Garcia 		if (txq->tx_desc_count >= txq->tx_stop_threshold)
1001527a6266SJeff Kirsher 			netif_tx_stop_queue(nq);
1002dd11680dSEzequiel Garcia 	} else {
1003dd11680dSEzequiel Garcia 		txq->tx_dropped++;
1004dd11680dSEzequiel Garcia 		dev_kfree_skb_any(skb);
1005527a6266SJeff Kirsher 	}
1006527a6266SJeff Kirsher 
1007527a6266SJeff Kirsher 	return NETDEV_TX_OK;
1008527a6266SJeff Kirsher }
1009527a6266SJeff Kirsher 
1010527a6266SJeff Kirsher 
1011527a6266SJeff Kirsher /* tx napi ******************************************************************/
1012527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq)
1013527a6266SJeff Kirsher {
1014527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1015527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
1016527a6266SJeff Kirsher 	u32 hw_desc_ptr;
1017527a6266SJeff Kirsher 	u32 expected_ptr;
1018527a6266SJeff Kirsher 
1019527a6266SJeff Kirsher 	__netif_tx_lock(nq, smp_processor_id());
1020527a6266SJeff Kirsher 
1021527a6266SJeff Kirsher 	if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
1022527a6266SJeff Kirsher 		goto out;
1023527a6266SJeff Kirsher 
1024527a6266SJeff Kirsher 	hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
1025527a6266SJeff Kirsher 	expected_ptr = (u32)txq->tx_desc_dma +
1026527a6266SJeff Kirsher 				txq->tx_curr_desc * sizeof(struct tx_desc);
1027527a6266SJeff Kirsher 
1028527a6266SJeff Kirsher 	if (hw_desc_ptr != expected_ptr)
1029527a6266SJeff Kirsher 		txq_enable(txq);
1030527a6266SJeff Kirsher 
1031527a6266SJeff Kirsher out:
1032527a6266SJeff Kirsher 	__netif_tx_unlock(nq);
1033527a6266SJeff Kirsher 
1034527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
1035527a6266SJeff Kirsher }
1036527a6266SJeff Kirsher 
1037527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force)
1038527a6266SJeff Kirsher {
1039527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1040527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
1041527a6266SJeff Kirsher 	int reclaimed;
1042527a6266SJeff Kirsher 
10433aefe2b4SRussell King 	__netif_tx_lock_bh(nq);
1044527a6266SJeff Kirsher 
1045527a6266SJeff Kirsher 	reclaimed = 0;
1046527a6266SJeff Kirsher 	while (reclaimed < budget && txq->tx_desc_count > 0) {
1047527a6266SJeff Kirsher 		int tx_index;
1048527a6266SJeff Kirsher 		struct tx_desc *desc;
1049527a6266SJeff Kirsher 		u32 cmd_sts;
1050527a6266SJeff Kirsher 
1051527a6266SJeff Kirsher 		tx_index = txq->tx_used_desc;
1052527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
1053527a6266SJeff Kirsher 		cmd_sts = desc->cmd_sts;
1054527a6266SJeff Kirsher 
1055527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA) {
1056527a6266SJeff Kirsher 			if (!force)
1057527a6266SJeff Kirsher 				break;
1058527a6266SJeff Kirsher 			desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
1059527a6266SJeff Kirsher 		}
1060527a6266SJeff Kirsher 
1061527a6266SJeff Kirsher 		txq->tx_used_desc = tx_index + 1;
1062527a6266SJeff Kirsher 		if (txq->tx_used_desc == txq->tx_ring_size)
1063527a6266SJeff Kirsher 			txq->tx_used_desc = 0;
1064527a6266SJeff Kirsher 
1065527a6266SJeff Kirsher 		reclaimed++;
1066527a6266SJeff Kirsher 		txq->tx_desc_count--;
1067527a6266SJeff Kirsher 
1068*2c2a9cbdSKarl Beldan 		if (!IS_TSO_HEADER(txq, desc->buf_ptr))
1069*2c2a9cbdSKarl Beldan 			dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
1070*2c2a9cbdSKarl Beldan 					 desc->byte_cnt, DMA_TO_DEVICE);
1071*2c2a9cbdSKarl Beldan 
1072*2c2a9cbdSKarl Beldan 		if (cmd_sts & TX_ENABLE_INTERRUPT) {
1073*2c2a9cbdSKarl Beldan 			struct sk_buff *skb = __skb_dequeue(&txq->tx_skb);
1074*2c2a9cbdSKarl Beldan 
1075*2c2a9cbdSKarl Beldan 			if (!WARN_ON(!skb))
1076*2c2a9cbdSKarl Beldan 				dev_kfree_skb(skb);
1077*2c2a9cbdSKarl Beldan 		}
1078527a6266SJeff Kirsher 
1079527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY) {
1080527a6266SJeff Kirsher 			netdev_info(mp->dev, "tx error\n");
1081527a6266SJeff Kirsher 			mp->dev->stats.tx_errors++;
1082527a6266SJeff Kirsher 		}
1083527a6266SJeff Kirsher 
1084527a6266SJeff Kirsher 	}
1085527a6266SJeff Kirsher 
10863aefe2b4SRussell King 	__netif_tx_unlock_bh(nq);
1087527a6266SJeff Kirsher 
1088527a6266SJeff Kirsher 	if (reclaimed < budget)
1089527a6266SJeff Kirsher 		mp->work_tx &= ~(1 << txq->index);
1090527a6266SJeff Kirsher 
1091527a6266SJeff Kirsher 	return reclaimed;
1092527a6266SJeff Kirsher }
1093527a6266SJeff Kirsher 
1094527a6266SJeff Kirsher 
1095527a6266SJeff Kirsher /* tx rate control **********************************************************/
1096527a6266SJeff Kirsher /*
1097527a6266SJeff Kirsher  * Set total maximum TX rate (shared by all TX queues for this port)
1098527a6266SJeff Kirsher  * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
1099527a6266SJeff Kirsher  */
1100527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
1101527a6266SJeff Kirsher {
1102527a6266SJeff Kirsher 	int token_rate;
1103527a6266SJeff Kirsher 	int mtu;
1104527a6266SJeff Kirsher 	int bucket_size;
1105527a6266SJeff Kirsher 
1106452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1107527a6266SJeff Kirsher 	if (token_rate > 1023)
1108527a6266SJeff Kirsher 		token_rate = 1023;
1109527a6266SJeff Kirsher 
1110527a6266SJeff Kirsher 	mtu = (mp->dev->mtu + 255) >> 8;
1111527a6266SJeff Kirsher 	if (mtu > 63)
1112527a6266SJeff Kirsher 		mtu = 63;
1113527a6266SJeff Kirsher 
1114527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1115527a6266SJeff Kirsher 	if (bucket_size > 65535)
1116527a6266SJeff Kirsher 		bucket_size = 65535;
1117527a6266SJeff Kirsher 
1118527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1119527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1120527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE, token_rate);
1121527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU, mtu);
1122527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST, bucket_size);
1123527a6266SJeff Kirsher 		break;
1124527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1125527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE_MOVED, token_rate);
1126527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU_MOVED, mtu);
1127527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
1128527a6266SJeff Kirsher 		break;
1129527a6266SJeff Kirsher 	}
1130527a6266SJeff Kirsher }
1131527a6266SJeff Kirsher 
1132527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
1133527a6266SJeff Kirsher {
1134527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1135527a6266SJeff Kirsher 	int token_rate;
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 	bucket_size = (burst + 255) >> 8;
1143527a6266SJeff Kirsher 	if (bucket_size > 65535)
1144527a6266SJeff Kirsher 		bucket_size = 65535;
1145527a6266SJeff Kirsher 
1146527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
1147527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
1148527a6266SJeff Kirsher }
1149527a6266SJeff Kirsher 
1150527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq)
1151527a6266SJeff Kirsher {
1152527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1153527a6266SJeff Kirsher 	int off;
1154527a6266SJeff Kirsher 	u32 val;
1155527a6266SJeff Kirsher 
1156527a6266SJeff Kirsher 	/*
1157527a6266SJeff Kirsher 	 * Turn on fixed priority mode.
1158527a6266SJeff Kirsher 	 */
1159527a6266SJeff Kirsher 	off = 0;
1160527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1161527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1162527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF;
1163527a6266SJeff Kirsher 		break;
1164527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1165527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF_MOVED;
1166527a6266SJeff Kirsher 		break;
1167527a6266SJeff Kirsher 	}
1168527a6266SJeff Kirsher 
1169527a6266SJeff Kirsher 	if (off) {
1170527a6266SJeff Kirsher 		val = rdlp(mp, off);
1171527a6266SJeff Kirsher 		val |= 1 << txq->index;
1172527a6266SJeff Kirsher 		wrlp(mp, off, val);
1173527a6266SJeff Kirsher 	}
1174527a6266SJeff Kirsher }
1175527a6266SJeff Kirsher 
1176527a6266SJeff Kirsher 
1177527a6266SJeff Kirsher /* mii management interface *************************************************/
11780a9e413bSEzequiel Garcia static void mv643xx_eth_adjust_link(struct net_device *dev)
1179260055bbSPhil Sutter {
11800a9e413bSEzequiel Garcia 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1181260055bbSPhil Sutter 	u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1182260055bbSPhil Sutter 	u32 autoneg_disable = FORCE_LINK_PASS |
1183260055bbSPhil Sutter 	             DISABLE_AUTO_NEG_SPEED_GMII |
1184260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1185260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_DUPLEX;
1186260055bbSPhil Sutter 
1187260055bbSPhil Sutter 	if (mp->phy->autoneg == AUTONEG_ENABLE) {
1188260055bbSPhil Sutter 		/* enable auto negotiation */
1189260055bbSPhil Sutter 		pscr &= ~autoneg_disable;
1190260055bbSPhil Sutter 		goto out_write;
1191260055bbSPhil Sutter 	}
1192260055bbSPhil Sutter 
1193260055bbSPhil Sutter 	pscr |= autoneg_disable;
1194260055bbSPhil Sutter 
1195260055bbSPhil Sutter 	if (mp->phy->speed == SPEED_1000) {
1196260055bbSPhil Sutter 		/* force gigabit, half duplex not supported */
1197260055bbSPhil Sutter 		pscr |= SET_GMII_SPEED_TO_1000;
1198260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1199260055bbSPhil Sutter 		goto out_write;
1200260055bbSPhil Sutter 	}
1201260055bbSPhil Sutter 
1202260055bbSPhil Sutter 	pscr &= ~SET_GMII_SPEED_TO_1000;
1203260055bbSPhil Sutter 
1204260055bbSPhil Sutter 	if (mp->phy->speed == SPEED_100)
1205260055bbSPhil Sutter 		pscr |= SET_MII_SPEED_TO_100;
1206260055bbSPhil Sutter 	else
1207260055bbSPhil Sutter 		pscr &= ~SET_MII_SPEED_TO_100;
1208260055bbSPhil Sutter 
1209260055bbSPhil Sutter 	if (mp->phy->duplex == DUPLEX_FULL)
1210260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1211260055bbSPhil Sutter 	else
1212260055bbSPhil Sutter 		pscr &= ~SET_FULL_DUPLEX_MODE;
1213260055bbSPhil Sutter 
1214260055bbSPhil Sutter out_write:
1215260055bbSPhil Sutter 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1216260055bbSPhil Sutter }
1217260055bbSPhil Sutter 
1218527a6266SJeff Kirsher /* statistics ***************************************************************/
1219527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1220527a6266SJeff Kirsher {
1221527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1222527a6266SJeff Kirsher 	struct net_device_stats *stats = &dev->stats;
1223527a6266SJeff Kirsher 	unsigned long tx_packets = 0;
1224527a6266SJeff Kirsher 	unsigned long tx_bytes = 0;
1225527a6266SJeff Kirsher 	unsigned long tx_dropped = 0;
1226527a6266SJeff Kirsher 	int i;
1227527a6266SJeff Kirsher 
1228527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
1229527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
1230527a6266SJeff Kirsher 
1231527a6266SJeff Kirsher 		tx_packets += txq->tx_packets;
1232527a6266SJeff Kirsher 		tx_bytes += txq->tx_bytes;
1233527a6266SJeff Kirsher 		tx_dropped += txq->tx_dropped;
1234527a6266SJeff Kirsher 	}
1235527a6266SJeff Kirsher 
1236527a6266SJeff Kirsher 	stats->tx_packets = tx_packets;
1237527a6266SJeff Kirsher 	stats->tx_bytes = tx_bytes;
1238527a6266SJeff Kirsher 	stats->tx_dropped = tx_dropped;
1239527a6266SJeff Kirsher 
1240527a6266SJeff Kirsher 	return stats;
1241527a6266SJeff Kirsher }
1242527a6266SJeff Kirsher 
1243527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
1244527a6266SJeff Kirsher {
1245527a6266SJeff Kirsher 	return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
1246527a6266SJeff Kirsher }
1247527a6266SJeff Kirsher 
1248527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp)
1249527a6266SJeff Kirsher {
1250527a6266SJeff Kirsher 	int i;
1251527a6266SJeff Kirsher 
1252527a6266SJeff Kirsher 	for (i = 0; i < 0x80; i += 4)
1253527a6266SJeff Kirsher 		mib_read(mp, i);
1254302476c9SPaulius Zaleckas 
1255302476c9SPaulius Zaleckas 	/* Clear non MIB hw counters also */
1256302476c9SPaulius Zaleckas 	rdlp(mp, RX_DISCARD_FRAME_CNT);
1257302476c9SPaulius Zaleckas 	rdlp(mp, RX_OVERRUN_FRAME_CNT);
1258527a6266SJeff Kirsher }
1259527a6266SJeff Kirsher 
1260527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp)
1261527a6266SJeff Kirsher {
1262527a6266SJeff Kirsher 	struct mib_counters *p = &mp->mib_counters;
1263527a6266SJeff Kirsher 
1264527a6266SJeff Kirsher 	spin_lock_bh(&mp->mib_counters_lock);
1265527a6266SJeff Kirsher 	p->good_octets_received += mib_read(mp, 0x00);
1266527a6266SJeff Kirsher 	p->bad_octets_received += mib_read(mp, 0x08);
1267527a6266SJeff Kirsher 	p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1268527a6266SJeff Kirsher 	p->good_frames_received += mib_read(mp, 0x10);
1269527a6266SJeff Kirsher 	p->bad_frames_received += mib_read(mp, 0x14);
1270527a6266SJeff Kirsher 	p->broadcast_frames_received += mib_read(mp, 0x18);
1271527a6266SJeff Kirsher 	p->multicast_frames_received += mib_read(mp, 0x1c);
1272527a6266SJeff Kirsher 	p->frames_64_octets += mib_read(mp, 0x20);
1273527a6266SJeff Kirsher 	p->frames_65_to_127_octets += mib_read(mp, 0x24);
1274527a6266SJeff Kirsher 	p->frames_128_to_255_octets += mib_read(mp, 0x28);
1275527a6266SJeff Kirsher 	p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1276527a6266SJeff Kirsher 	p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1277527a6266SJeff Kirsher 	p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1278527a6266SJeff Kirsher 	p->good_octets_sent += mib_read(mp, 0x38);
1279527a6266SJeff Kirsher 	p->good_frames_sent += mib_read(mp, 0x40);
1280527a6266SJeff Kirsher 	p->excessive_collision += mib_read(mp, 0x44);
1281527a6266SJeff Kirsher 	p->multicast_frames_sent += mib_read(mp, 0x48);
1282527a6266SJeff Kirsher 	p->broadcast_frames_sent += mib_read(mp, 0x4c);
1283527a6266SJeff Kirsher 	p->unrec_mac_control_received += mib_read(mp, 0x50);
1284527a6266SJeff Kirsher 	p->fc_sent += mib_read(mp, 0x54);
1285527a6266SJeff Kirsher 	p->good_fc_received += mib_read(mp, 0x58);
1286527a6266SJeff Kirsher 	p->bad_fc_received += mib_read(mp, 0x5c);
1287527a6266SJeff Kirsher 	p->undersize_received += mib_read(mp, 0x60);
1288527a6266SJeff Kirsher 	p->fragments_received += mib_read(mp, 0x64);
1289527a6266SJeff Kirsher 	p->oversize_received += mib_read(mp, 0x68);
1290527a6266SJeff Kirsher 	p->jabber_received += mib_read(mp, 0x6c);
1291527a6266SJeff Kirsher 	p->mac_receive_error += mib_read(mp, 0x70);
1292527a6266SJeff Kirsher 	p->bad_crc_event += mib_read(mp, 0x74);
1293527a6266SJeff Kirsher 	p->collision += mib_read(mp, 0x78);
1294527a6266SJeff Kirsher 	p->late_collision += mib_read(mp, 0x7c);
1295302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
1296302476c9SPaulius Zaleckas 	p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1297302476c9SPaulius Zaleckas 	p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
1298527a6266SJeff Kirsher 	spin_unlock_bh(&mp->mib_counters_lock);
1299527a6266SJeff Kirsher }
1300527a6266SJeff Kirsher 
1301527a6266SJeff Kirsher static void mib_counters_timer_wrapper(unsigned long _mp)
1302527a6266SJeff Kirsher {
1303527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = (void *)_mp;
1304527a6266SJeff Kirsher 	mib_counters_update(mp);
1305041b4ddbSSebastian Hesselbarth 	mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1306527a6266SJeff Kirsher }
1307527a6266SJeff Kirsher 
1308527a6266SJeff Kirsher 
1309527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/
1310527a6266SJeff Kirsher /*
1311527a6266SJeff Kirsher  * Hardware coalescing parameters are set in units of 64 t_clk
1312527a6266SJeff Kirsher  * cycles.  I.e.:
1313527a6266SJeff Kirsher  *
1314527a6266SJeff Kirsher  *	coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1315527a6266SJeff Kirsher  *
1316527a6266SJeff Kirsher  *	register_value = coal_delay_in_usec * t_clk_rate / 64000000
1317527a6266SJeff Kirsher  *
1318527a6266SJeff Kirsher  * In the ->set*() methods, we round the computed register value
1319527a6266SJeff Kirsher  * to the nearest integer.
1320527a6266SJeff Kirsher  */
1321527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1322527a6266SJeff Kirsher {
1323527a6266SJeff Kirsher 	u32 val = rdlp(mp, SDMA_CONFIG);
1324527a6266SJeff Kirsher 	u64 temp;
1325527a6266SJeff Kirsher 
1326527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit)
1327527a6266SJeff Kirsher 		temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1328527a6266SJeff Kirsher 	else
1329527a6266SJeff Kirsher 		temp = (val & 0x003fff00) >> 8;
1330527a6266SJeff Kirsher 
1331527a6266SJeff Kirsher 	temp *= 64000000;
1332452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1333527a6266SJeff Kirsher 
1334527a6266SJeff Kirsher 	return (unsigned int)temp;
1335527a6266SJeff Kirsher }
1336527a6266SJeff Kirsher 
1337527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1338527a6266SJeff Kirsher {
1339527a6266SJeff Kirsher 	u64 temp;
1340527a6266SJeff Kirsher 	u32 val;
1341527a6266SJeff Kirsher 
1342452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1343527a6266SJeff Kirsher 	temp += 31999999;
1344527a6266SJeff Kirsher 	do_div(temp, 64000000);
1345527a6266SJeff Kirsher 
1346527a6266SJeff Kirsher 	val = rdlp(mp, SDMA_CONFIG);
1347527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit) {
1348527a6266SJeff Kirsher 		if (temp > 0xffff)
1349527a6266SJeff Kirsher 			temp = 0xffff;
1350527a6266SJeff Kirsher 		val &= ~0x023fff80;
1351527a6266SJeff Kirsher 		val |= (temp & 0x8000) << 10;
1352527a6266SJeff Kirsher 		val |= (temp & 0x7fff) << 7;
1353527a6266SJeff Kirsher 	} else {
1354527a6266SJeff Kirsher 		if (temp > 0x3fff)
1355527a6266SJeff Kirsher 			temp = 0x3fff;
1356527a6266SJeff Kirsher 		val &= ~0x003fff00;
1357527a6266SJeff Kirsher 		val |= (temp & 0x3fff) << 8;
1358527a6266SJeff Kirsher 	}
1359527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, val);
1360527a6266SJeff Kirsher }
1361527a6266SJeff Kirsher 
1362527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1363527a6266SJeff Kirsher {
1364527a6266SJeff Kirsher 	u64 temp;
1365527a6266SJeff Kirsher 
1366527a6266SJeff Kirsher 	temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1367527a6266SJeff Kirsher 	temp *= 64000000;
1368452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1369527a6266SJeff Kirsher 
1370527a6266SJeff Kirsher 	return (unsigned int)temp;
1371527a6266SJeff Kirsher }
1372527a6266SJeff Kirsher 
1373527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1374527a6266SJeff Kirsher {
1375527a6266SJeff Kirsher 	u64 temp;
1376527a6266SJeff Kirsher 
1377452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1378527a6266SJeff Kirsher 	temp += 31999999;
1379527a6266SJeff Kirsher 	do_div(temp, 64000000);
1380527a6266SJeff Kirsher 
1381527a6266SJeff Kirsher 	if (temp > 0x3fff)
1382527a6266SJeff Kirsher 		temp = 0x3fff;
1383527a6266SJeff Kirsher 
1384527a6266SJeff Kirsher 	wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1385527a6266SJeff Kirsher }
1386527a6266SJeff Kirsher 
1387527a6266SJeff Kirsher 
1388527a6266SJeff Kirsher /* ethtool ******************************************************************/
1389527a6266SJeff Kirsher struct mv643xx_eth_stats {
1390527a6266SJeff Kirsher 	char stat_string[ETH_GSTRING_LEN];
1391527a6266SJeff Kirsher 	int sizeof_stat;
1392527a6266SJeff Kirsher 	int netdev_off;
1393527a6266SJeff Kirsher 	int mp_off;
1394527a6266SJeff Kirsher };
1395527a6266SJeff Kirsher 
1396527a6266SJeff Kirsher #define SSTAT(m)						\
1397527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct net_device_stats, m),		\
1398527a6266SJeff Kirsher 	  offsetof(struct net_device, stats.m), -1 }
1399527a6266SJeff Kirsher 
1400527a6266SJeff Kirsher #define MIBSTAT(m)						\
1401527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct mib_counters, m),		\
1402527a6266SJeff Kirsher 	  -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1403527a6266SJeff Kirsher 
1404527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1405527a6266SJeff Kirsher 	SSTAT(rx_packets),
1406527a6266SJeff Kirsher 	SSTAT(tx_packets),
1407527a6266SJeff Kirsher 	SSTAT(rx_bytes),
1408527a6266SJeff Kirsher 	SSTAT(tx_bytes),
1409527a6266SJeff Kirsher 	SSTAT(rx_errors),
1410527a6266SJeff Kirsher 	SSTAT(tx_errors),
1411527a6266SJeff Kirsher 	SSTAT(rx_dropped),
1412527a6266SJeff Kirsher 	SSTAT(tx_dropped),
1413527a6266SJeff Kirsher 	MIBSTAT(good_octets_received),
1414527a6266SJeff Kirsher 	MIBSTAT(bad_octets_received),
1415527a6266SJeff Kirsher 	MIBSTAT(internal_mac_transmit_err),
1416527a6266SJeff Kirsher 	MIBSTAT(good_frames_received),
1417527a6266SJeff Kirsher 	MIBSTAT(bad_frames_received),
1418527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_received),
1419527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_received),
1420527a6266SJeff Kirsher 	MIBSTAT(frames_64_octets),
1421527a6266SJeff Kirsher 	MIBSTAT(frames_65_to_127_octets),
1422527a6266SJeff Kirsher 	MIBSTAT(frames_128_to_255_octets),
1423527a6266SJeff Kirsher 	MIBSTAT(frames_256_to_511_octets),
1424527a6266SJeff Kirsher 	MIBSTAT(frames_512_to_1023_octets),
1425527a6266SJeff Kirsher 	MIBSTAT(frames_1024_to_max_octets),
1426527a6266SJeff Kirsher 	MIBSTAT(good_octets_sent),
1427527a6266SJeff Kirsher 	MIBSTAT(good_frames_sent),
1428527a6266SJeff Kirsher 	MIBSTAT(excessive_collision),
1429527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_sent),
1430527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_sent),
1431527a6266SJeff Kirsher 	MIBSTAT(unrec_mac_control_received),
1432527a6266SJeff Kirsher 	MIBSTAT(fc_sent),
1433527a6266SJeff Kirsher 	MIBSTAT(good_fc_received),
1434527a6266SJeff Kirsher 	MIBSTAT(bad_fc_received),
1435527a6266SJeff Kirsher 	MIBSTAT(undersize_received),
1436527a6266SJeff Kirsher 	MIBSTAT(fragments_received),
1437527a6266SJeff Kirsher 	MIBSTAT(oversize_received),
1438527a6266SJeff Kirsher 	MIBSTAT(jabber_received),
1439527a6266SJeff Kirsher 	MIBSTAT(mac_receive_error),
1440527a6266SJeff Kirsher 	MIBSTAT(bad_crc_event),
1441527a6266SJeff Kirsher 	MIBSTAT(collision),
1442527a6266SJeff Kirsher 	MIBSTAT(late_collision),
1443302476c9SPaulius Zaleckas 	MIBSTAT(rx_discard),
1444302476c9SPaulius Zaleckas 	MIBSTAT(rx_overrun),
1445527a6266SJeff Kirsher };
1446527a6266SJeff Kirsher 
1447527a6266SJeff Kirsher static int
1448527a6266SJeff Kirsher mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
1449527a6266SJeff Kirsher 			     struct ethtool_cmd *cmd)
1450527a6266SJeff Kirsher {
1451527a6266SJeff Kirsher 	int err;
1452527a6266SJeff Kirsher 
1453527a6266SJeff Kirsher 	err = phy_read_status(mp->phy);
1454527a6266SJeff Kirsher 	if (err == 0)
1455527a6266SJeff Kirsher 		err = phy_ethtool_gset(mp->phy, cmd);
1456527a6266SJeff Kirsher 
1457527a6266SJeff Kirsher 	/*
1458527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1459527a6266SJeff Kirsher 	 */
1460527a6266SJeff Kirsher 	cmd->supported &= ~SUPPORTED_1000baseT_Half;
1461527a6266SJeff Kirsher 	cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1462527a6266SJeff Kirsher 
1463527a6266SJeff Kirsher 	return err;
1464527a6266SJeff Kirsher }
1465527a6266SJeff Kirsher 
1466527a6266SJeff Kirsher static int
1467527a6266SJeff Kirsher mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
1468527a6266SJeff Kirsher 				 struct ethtool_cmd *cmd)
1469527a6266SJeff Kirsher {
1470527a6266SJeff Kirsher 	u32 port_status;
1471527a6266SJeff Kirsher 
1472527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
1473527a6266SJeff Kirsher 
1474527a6266SJeff Kirsher 	cmd->supported = SUPPORTED_MII;
1475527a6266SJeff Kirsher 	cmd->advertising = ADVERTISED_MII;
1476527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
1477527a6266SJeff Kirsher 	case PORT_SPEED_10:
1478527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_10);
1479527a6266SJeff Kirsher 		break;
1480527a6266SJeff Kirsher 	case PORT_SPEED_100:
1481527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_100);
1482527a6266SJeff Kirsher 		break;
1483527a6266SJeff Kirsher 	case PORT_SPEED_1000:
1484527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_1000);
1485527a6266SJeff Kirsher 		break;
1486527a6266SJeff Kirsher 	default:
1487527a6266SJeff Kirsher 		cmd->speed = -1;
1488527a6266SJeff Kirsher 		break;
1489527a6266SJeff Kirsher 	}
1490527a6266SJeff Kirsher 	cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
1491527a6266SJeff Kirsher 	cmd->port = PORT_MII;
1492527a6266SJeff Kirsher 	cmd->phy_address = 0;
1493527a6266SJeff Kirsher 	cmd->transceiver = XCVR_INTERNAL;
1494527a6266SJeff Kirsher 	cmd->autoneg = AUTONEG_DISABLE;
1495527a6266SJeff Kirsher 	cmd->maxtxpkt = 1;
1496527a6266SJeff Kirsher 	cmd->maxrxpkt = 1;
1497527a6266SJeff Kirsher 
1498527a6266SJeff Kirsher 	return 0;
1499527a6266SJeff Kirsher }
1500527a6266SJeff Kirsher 
15013871c387SMichael Stapelberg static void
15023871c387SMichael Stapelberg mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
15033871c387SMichael Stapelberg {
15043871c387SMichael Stapelberg 	struct mv643xx_eth_private *mp = netdev_priv(dev);
15053871c387SMichael Stapelberg 	wol->supported = 0;
15063871c387SMichael Stapelberg 	wol->wolopts = 0;
15073871c387SMichael Stapelberg 	if (mp->phy)
15083871c387SMichael Stapelberg 		phy_ethtool_get_wol(mp->phy, wol);
15093871c387SMichael Stapelberg }
15103871c387SMichael Stapelberg 
15113871c387SMichael Stapelberg static int
15123871c387SMichael Stapelberg mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
15133871c387SMichael Stapelberg {
15143871c387SMichael Stapelberg 	struct mv643xx_eth_private *mp = netdev_priv(dev);
15153871c387SMichael Stapelberg 	int err;
15163871c387SMichael Stapelberg 
15173871c387SMichael Stapelberg 	if (mp->phy == NULL)
15183871c387SMichael Stapelberg 		return -EOPNOTSUPP;
15193871c387SMichael Stapelberg 
15203871c387SMichael Stapelberg 	err = phy_ethtool_set_wol(mp->phy, wol);
15213871c387SMichael Stapelberg 	/* Given that mv643xx_eth works without the marvell-specific PHY driver,
15223871c387SMichael Stapelberg 	 * this debugging hint is useful to have.
15233871c387SMichael Stapelberg 	 */
15243871c387SMichael Stapelberg 	if (err == -EOPNOTSUPP)
15253871c387SMichael Stapelberg 		netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
15263871c387SMichael Stapelberg 	return err;
15273871c387SMichael Stapelberg }
15283871c387SMichael Stapelberg 
1529527a6266SJeff Kirsher static int
1530527a6266SJeff Kirsher mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1531527a6266SJeff Kirsher {
1532527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1533527a6266SJeff Kirsher 
1534527a6266SJeff Kirsher 	if (mp->phy != NULL)
1535527a6266SJeff Kirsher 		return mv643xx_eth_get_settings_phy(mp, cmd);
1536527a6266SJeff Kirsher 	else
1537527a6266SJeff Kirsher 		return mv643xx_eth_get_settings_phyless(mp, cmd);
1538527a6266SJeff Kirsher }
1539527a6266SJeff Kirsher 
1540527a6266SJeff Kirsher static int
1541527a6266SJeff Kirsher mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1542527a6266SJeff Kirsher {
1543527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1544260055bbSPhil Sutter 	int ret;
1545527a6266SJeff Kirsher 
1546527a6266SJeff Kirsher 	if (mp->phy == NULL)
1547527a6266SJeff Kirsher 		return -EINVAL;
1548527a6266SJeff Kirsher 
1549527a6266SJeff Kirsher 	/*
1550527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1551527a6266SJeff Kirsher 	 */
1552527a6266SJeff Kirsher 	cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1553527a6266SJeff Kirsher 
1554260055bbSPhil Sutter 	ret = phy_ethtool_sset(mp->phy, cmd);
1555260055bbSPhil Sutter 	if (!ret)
15560a9e413bSEzequiel Garcia 		mv643xx_eth_adjust_link(dev);
1557260055bbSPhil Sutter 	return ret;
1558527a6266SJeff Kirsher }
1559527a6266SJeff Kirsher 
1560527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1561527a6266SJeff Kirsher 				    struct ethtool_drvinfo *drvinfo)
1562527a6266SJeff Kirsher {
15636f39da2cSAxel Lin 	strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
15646f39da2cSAxel Lin 		sizeof(drvinfo->driver));
156568aad78cSRick Jones 	strlcpy(drvinfo->version, mv643xx_eth_driver_version,
15666f39da2cSAxel Lin 		sizeof(drvinfo->version));
15676f39da2cSAxel Lin 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
15686f39da2cSAxel Lin 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
1569527a6266SJeff Kirsher 	drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
1570527a6266SJeff Kirsher }
1571527a6266SJeff Kirsher 
1572527a6266SJeff Kirsher static int mv643xx_eth_nway_reset(struct net_device *dev)
1573527a6266SJeff Kirsher {
1574527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1575527a6266SJeff Kirsher 
1576527a6266SJeff Kirsher 	if (mp->phy == NULL)
1577527a6266SJeff Kirsher 		return -EINVAL;
1578527a6266SJeff Kirsher 
1579527a6266SJeff Kirsher 	return genphy_restart_aneg(mp->phy);
1580527a6266SJeff Kirsher }
1581527a6266SJeff Kirsher 
1582527a6266SJeff Kirsher static int
1583527a6266SJeff Kirsher mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1584527a6266SJeff Kirsher {
1585527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1586527a6266SJeff Kirsher 
1587527a6266SJeff Kirsher 	ec->rx_coalesce_usecs = get_rx_coal(mp);
1588527a6266SJeff Kirsher 	ec->tx_coalesce_usecs = get_tx_coal(mp);
1589527a6266SJeff Kirsher 
1590527a6266SJeff Kirsher 	return 0;
1591527a6266SJeff Kirsher }
1592527a6266SJeff Kirsher 
1593527a6266SJeff Kirsher static int
1594527a6266SJeff Kirsher mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1595527a6266SJeff Kirsher {
1596527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1597527a6266SJeff Kirsher 
1598527a6266SJeff Kirsher 	set_rx_coal(mp, ec->rx_coalesce_usecs);
1599527a6266SJeff Kirsher 	set_tx_coal(mp, ec->tx_coalesce_usecs);
1600527a6266SJeff Kirsher 
1601527a6266SJeff Kirsher 	return 0;
1602527a6266SJeff Kirsher }
1603527a6266SJeff Kirsher 
1604527a6266SJeff Kirsher static void
1605527a6266SJeff Kirsher mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1606527a6266SJeff Kirsher {
1607527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1608527a6266SJeff Kirsher 
1609527a6266SJeff Kirsher 	er->rx_max_pending = 4096;
1610527a6266SJeff Kirsher 	er->tx_max_pending = 4096;
1611527a6266SJeff Kirsher 
1612527a6266SJeff Kirsher 	er->rx_pending = mp->rx_ring_size;
1613527a6266SJeff Kirsher 	er->tx_pending = mp->tx_ring_size;
1614527a6266SJeff Kirsher }
1615527a6266SJeff Kirsher 
1616527a6266SJeff Kirsher static int
1617527a6266SJeff Kirsher mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1618527a6266SJeff Kirsher {
1619527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1620527a6266SJeff Kirsher 
1621527a6266SJeff Kirsher 	if (er->rx_mini_pending || er->rx_jumbo_pending)
1622527a6266SJeff Kirsher 		return -EINVAL;
1623527a6266SJeff Kirsher 
1624527a6266SJeff Kirsher 	mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
1625ee9e4956SEzequiel Garcia 	mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending,
1626ee9e4956SEzequiel Garcia 				   MV643XX_MAX_SKB_DESCS * 2, 4096);
1627ee9e4956SEzequiel Garcia 	if (mp->tx_ring_size != er->tx_pending)
1628ee9e4956SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
1629ee9e4956SEzequiel Garcia 			    mp->tx_ring_size, er->tx_pending);
1630527a6266SJeff Kirsher 
1631527a6266SJeff Kirsher 	if (netif_running(dev)) {
1632527a6266SJeff Kirsher 		mv643xx_eth_stop(dev);
1633527a6266SJeff Kirsher 		if (mv643xx_eth_open(dev)) {
1634527a6266SJeff Kirsher 			netdev_err(dev,
1635527a6266SJeff Kirsher 				   "fatal error on re-opening device after ring param change\n");
1636527a6266SJeff Kirsher 			return -ENOMEM;
1637527a6266SJeff Kirsher 		}
1638527a6266SJeff Kirsher 	}
1639527a6266SJeff Kirsher 
1640527a6266SJeff Kirsher 	return 0;
1641527a6266SJeff Kirsher }
1642527a6266SJeff Kirsher 
1643527a6266SJeff Kirsher 
1644527a6266SJeff Kirsher static int
1645c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
1646527a6266SJeff Kirsher {
1647527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
16483ad9b358SMichał Mirosław 	bool rx_csum = features & NETIF_F_RXCSUM;
1649527a6266SJeff Kirsher 
1650527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1651527a6266SJeff Kirsher 
1652527a6266SJeff Kirsher 	return 0;
1653527a6266SJeff Kirsher }
1654527a6266SJeff Kirsher 
1655527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev,
1656527a6266SJeff Kirsher 				    uint32_t stringset, uint8_t *data)
1657527a6266SJeff Kirsher {
1658527a6266SJeff Kirsher 	int i;
1659527a6266SJeff Kirsher 
1660527a6266SJeff Kirsher 	if (stringset == ETH_SS_STATS) {
1661527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1662527a6266SJeff Kirsher 			memcpy(data + i * ETH_GSTRING_LEN,
1663527a6266SJeff Kirsher 				mv643xx_eth_stats[i].stat_string,
1664527a6266SJeff Kirsher 				ETH_GSTRING_LEN);
1665527a6266SJeff Kirsher 		}
1666527a6266SJeff Kirsher 	}
1667527a6266SJeff Kirsher }
1668527a6266SJeff Kirsher 
1669527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1670527a6266SJeff Kirsher 					  struct ethtool_stats *stats,
1671527a6266SJeff Kirsher 					  uint64_t *data)
1672527a6266SJeff Kirsher {
1673527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1674527a6266SJeff Kirsher 	int i;
1675527a6266SJeff Kirsher 
1676527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
1677527a6266SJeff Kirsher 	mib_counters_update(mp);
1678527a6266SJeff Kirsher 
1679527a6266SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1680527a6266SJeff Kirsher 		const struct mv643xx_eth_stats *stat;
1681527a6266SJeff Kirsher 		void *p;
1682527a6266SJeff Kirsher 
1683527a6266SJeff Kirsher 		stat = mv643xx_eth_stats + i;
1684527a6266SJeff Kirsher 
1685527a6266SJeff Kirsher 		if (stat->netdev_off >= 0)
1686527a6266SJeff Kirsher 			p = ((void *)mp->dev) + stat->netdev_off;
1687527a6266SJeff Kirsher 		else
1688527a6266SJeff Kirsher 			p = ((void *)mp) + stat->mp_off;
1689527a6266SJeff Kirsher 
1690527a6266SJeff Kirsher 		data[i] = (stat->sizeof_stat == 8) ?
1691527a6266SJeff Kirsher 				*(uint64_t *)p : *(uint32_t *)p;
1692527a6266SJeff Kirsher 	}
1693527a6266SJeff Kirsher }
1694527a6266SJeff Kirsher 
1695527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
1696527a6266SJeff Kirsher {
1697527a6266SJeff Kirsher 	if (sset == ETH_SS_STATS)
1698527a6266SJeff Kirsher 		return ARRAY_SIZE(mv643xx_eth_stats);
1699527a6266SJeff Kirsher 
1700527a6266SJeff Kirsher 	return -EOPNOTSUPP;
1701527a6266SJeff Kirsher }
1702527a6266SJeff Kirsher 
1703527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1704527a6266SJeff Kirsher 	.get_settings		= mv643xx_eth_get_settings,
1705527a6266SJeff Kirsher 	.set_settings		= mv643xx_eth_set_settings,
1706527a6266SJeff Kirsher 	.get_drvinfo		= mv643xx_eth_get_drvinfo,
1707527a6266SJeff Kirsher 	.nway_reset		= mv643xx_eth_nway_reset,
1708527a6266SJeff Kirsher 	.get_link		= ethtool_op_get_link,
1709527a6266SJeff Kirsher 	.get_coalesce		= mv643xx_eth_get_coalesce,
1710527a6266SJeff Kirsher 	.set_coalesce		= mv643xx_eth_set_coalesce,
1711527a6266SJeff Kirsher 	.get_ringparam		= mv643xx_eth_get_ringparam,
1712527a6266SJeff Kirsher 	.set_ringparam		= mv643xx_eth_set_ringparam,
1713527a6266SJeff Kirsher 	.get_strings		= mv643xx_eth_get_strings,
1714527a6266SJeff Kirsher 	.get_ethtool_stats	= mv643xx_eth_get_ethtool_stats,
1715527a6266SJeff Kirsher 	.get_sset_count		= mv643xx_eth_get_sset_count,
1716ebad0a8dSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
17173871c387SMichael Stapelberg 	.get_wol                = mv643xx_eth_get_wol,
17183871c387SMichael Stapelberg 	.set_wol                = mv643xx_eth_set_wol,
1719527a6266SJeff Kirsher };
1720527a6266SJeff Kirsher 
1721527a6266SJeff Kirsher 
1722527a6266SJeff Kirsher /* address handling *********************************************************/
1723527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
1724527a6266SJeff Kirsher {
1725527a6266SJeff Kirsher 	unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1726527a6266SJeff Kirsher 	unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
1727527a6266SJeff Kirsher 
1728527a6266SJeff Kirsher 	addr[0] = (mac_h >> 24) & 0xff;
1729527a6266SJeff Kirsher 	addr[1] = (mac_h >> 16) & 0xff;
1730527a6266SJeff Kirsher 	addr[2] = (mac_h >> 8) & 0xff;
1731527a6266SJeff Kirsher 	addr[3] = mac_h & 0xff;
1732527a6266SJeff Kirsher 	addr[4] = (mac_l >> 8) & 0xff;
1733527a6266SJeff Kirsher 	addr[5] = mac_l & 0xff;
1734527a6266SJeff Kirsher }
1735527a6266SJeff Kirsher 
1736527a6266SJeff Kirsher static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
1737527a6266SJeff Kirsher {
1738527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_HIGH,
1739527a6266SJeff Kirsher 		(addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1740527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
1741527a6266SJeff Kirsher }
1742527a6266SJeff Kirsher 
1743527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev)
1744527a6266SJeff Kirsher {
1745527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1746527a6266SJeff Kirsher 	u32 nibbles;
1747527a6266SJeff Kirsher 
1748527a6266SJeff Kirsher 	if (dev->flags & IFF_PROMISC)
1749527a6266SJeff Kirsher 		return 0;
1750527a6266SJeff Kirsher 
1751527a6266SJeff Kirsher 	nibbles = 1 << (dev->dev_addr[5] & 0x0f);
1752527a6266SJeff Kirsher 	netdev_for_each_uc_addr(ha, dev) {
1753527a6266SJeff Kirsher 		if (memcmp(dev->dev_addr, ha->addr, 5))
1754527a6266SJeff Kirsher 			return 0;
1755527a6266SJeff Kirsher 		if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
1756527a6266SJeff Kirsher 			return 0;
1757527a6266SJeff Kirsher 
1758527a6266SJeff Kirsher 		nibbles |= 1 << (ha->addr[5] & 0x0f);
1759527a6266SJeff Kirsher 	}
1760527a6266SJeff Kirsher 
1761527a6266SJeff Kirsher 	return nibbles;
1762527a6266SJeff Kirsher }
1763527a6266SJeff Kirsher 
1764527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
1765527a6266SJeff Kirsher {
1766527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1767527a6266SJeff Kirsher 	u32 port_config;
1768527a6266SJeff Kirsher 	u32 nibbles;
1769527a6266SJeff Kirsher 	int i;
1770527a6266SJeff Kirsher 
1771527a6266SJeff Kirsher 	uc_addr_set(mp, dev->dev_addr);
1772527a6266SJeff Kirsher 
1773527a6266SJeff Kirsher 	port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1774527a6266SJeff Kirsher 
1775527a6266SJeff Kirsher 	nibbles = uc_addr_filter_mask(dev);
1776527a6266SJeff Kirsher 	if (!nibbles) {
1777527a6266SJeff Kirsher 		port_config |= UNICAST_PROMISCUOUS_MODE;
1778527a6266SJeff Kirsher 		nibbles = 0xffff;
1779527a6266SJeff Kirsher 	}
1780527a6266SJeff Kirsher 
1781527a6266SJeff Kirsher 	for (i = 0; i < 16; i += 4) {
1782527a6266SJeff Kirsher 		int off = UNICAST_TABLE(mp->port_num) + i;
1783527a6266SJeff Kirsher 		u32 v;
1784527a6266SJeff Kirsher 
1785527a6266SJeff Kirsher 		v = 0;
1786527a6266SJeff Kirsher 		if (nibbles & 1)
1787527a6266SJeff Kirsher 			v |= 0x00000001;
1788527a6266SJeff Kirsher 		if (nibbles & 2)
1789527a6266SJeff Kirsher 			v |= 0x00000100;
1790527a6266SJeff Kirsher 		if (nibbles & 4)
1791527a6266SJeff Kirsher 			v |= 0x00010000;
1792527a6266SJeff Kirsher 		if (nibbles & 8)
1793527a6266SJeff Kirsher 			v |= 0x01000000;
1794527a6266SJeff Kirsher 		nibbles >>= 4;
1795527a6266SJeff Kirsher 
1796527a6266SJeff Kirsher 		wrl(mp, off, v);
1797527a6266SJeff Kirsher 	}
1798527a6266SJeff Kirsher 
1799527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, port_config);
1800527a6266SJeff Kirsher }
1801527a6266SJeff Kirsher 
1802527a6266SJeff Kirsher static int addr_crc(unsigned char *addr)
1803527a6266SJeff Kirsher {
1804527a6266SJeff Kirsher 	int crc = 0;
1805527a6266SJeff Kirsher 	int i;
1806527a6266SJeff Kirsher 
1807527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
1808527a6266SJeff Kirsher 		int j;
1809527a6266SJeff Kirsher 
1810527a6266SJeff Kirsher 		crc = (crc ^ addr[i]) << 8;
1811527a6266SJeff Kirsher 		for (j = 7; j >= 0; j--) {
1812527a6266SJeff Kirsher 			if (crc & (0x100 << j))
1813527a6266SJeff Kirsher 				crc ^= 0x107 << j;
1814527a6266SJeff Kirsher 		}
1815527a6266SJeff Kirsher 	}
1816527a6266SJeff Kirsher 
1817527a6266SJeff Kirsher 	return crc;
1818527a6266SJeff Kirsher }
1819527a6266SJeff Kirsher 
1820527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
1821527a6266SJeff Kirsher {
1822527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1823527a6266SJeff Kirsher 	u32 *mc_spec;
1824527a6266SJeff Kirsher 	u32 *mc_other;
1825527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1826527a6266SJeff Kirsher 	int i;
1827527a6266SJeff Kirsher 
1828527a6266SJeff Kirsher 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1829527a6266SJeff Kirsher 		int port_num;
1830527a6266SJeff Kirsher 		u32 accept;
1831527a6266SJeff Kirsher 
1832527a6266SJeff Kirsher oom:
1833527a6266SJeff Kirsher 		port_num = mp->port_num;
1834527a6266SJeff Kirsher 		accept = 0x01010101;
1835527a6266SJeff Kirsher 		for (i = 0; i < 0x100; i += 4) {
1836527a6266SJeff Kirsher 			wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1837527a6266SJeff Kirsher 			wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
1838527a6266SJeff Kirsher 		}
1839527a6266SJeff Kirsher 		return;
1840527a6266SJeff Kirsher 	}
1841527a6266SJeff Kirsher 
1842527a6266SJeff Kirsher 	mc_spec = kmalloc(0x200, GFP_ATOMIC);
1843527a6266SJeff Kirsher 	if (mc_spec == NULL)
1844527a6266SJeff Kirsher 		goto oom;
1845527a6266SJeff Kirsher 	mc_other = mc_spec + (0x100 >> 2);
1846527a6266SJeff Kirsher 
1847527a6266SJeff Kirsher 	memset(mc_spec, 0, 0x100);
1848527a6266SJeff Kirsher 	memset(mc_other, 0, 0x100);
1849527a6266SJeff Kirsher 
1850527a6266SJeff Kirsher 	netdev_for_each_mc_addr(ha, dev) {
1851527a6266SJeff Kirsher 		u8 *a = ha->addr;
1852527a6266SJeff Kirsher 		u32 *table;
1853527a6266SJeff Kirsher 		int entry;
1854527a6266SJeff Kirsher 
1855527a6266SJeff Kirsher 		if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1856527a6266SJeff Kirsher 			table = mc_spec;
1857527a6266SJeff Kirsher 			entry = a[5];
1858527a6266SJeff Kirsher 		} else {
1859527a6266SJeff Kirsher 			table = mc_other;
1860527a6266SJeff Kirsher 			entry = addr_crc(a);
1861527a6266SJeff Kirsher 		}
1862527a6266SJeff Kirsher 
1863527a6266SJeff Kirsher 		table[entry >> 2] |= 1 << (8 * (entry & 3));
1864527a6266SJeff Kirsher 	}
1865527a6266SJeff Kirsher 
1866527a6266SJeff Kirsher 	for (i = 0; i < 0x100; i += 4) {
1867527a6266SJeff Kirsher 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
1868527a6266SJeff Kirsher 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
1869527a6266SJeff Kirsher 	}
1870527a6266SJeff Kirsher 
1871527a6266SJeff Kirsher 	kfree(mc_spec);
1872527a6266SJeff Kirsher }
1873527a6266SJeff Kirsher 
1874527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1875527a6266SJeff Kirsher {
1876527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1877527a6266SJeff Kirsher 	mv643xx_eth_program_multicast_filter(dev);
1878527a6266SJeff Kirsher }
1879527a6266SJeff Kirsher 
1880527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1881527a6266SJeff Kirsher {
1882527a6266SJeff Kirsher 	struct sockaddr *sa = addr;
1883527a6266SJeff Kirsher 
1884527a6266SJeff Kirsher 	if (!is_valid_ether_addr(sa->sa_data))
1885504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
1886527a6266SJeff Kirsher 
1887527a6266SJeff Kirsher 	memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1888527a6266SJeff Kirsher 
1889527a6266SJeff Kirsher 	netif_addr_lock_bh(dev);
1890527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1891527a6266SJeff Kirsher 	netif_addr_unlock_bh(dev);
1892527a6266SJeff Kirsher 
1893527a6266SJeff Kirsher 	return 0;
1894527a6266SJeff Kirsher }
1895527a6266SJeff Kirsher 
1896527a6266SJeff Kirsher 
1897527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/
1898527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index)
1899527a6266SJeff Kirsher {
1900527a6266SJeff Kirsher 	struct rx_queue *rxq = mp->rxq + index;
1901527a6266SJeff Kirsher 	struct rx_desc *rx_desc;
1902527a6266SJeff Kirsher 	int size;
1903527a6266SJeff Kirsher 	int i;
1904527a6266SJeff Kirsher 
1905527a6266SJeff Kirsher 	rxq->index = index;
1906527a6266SJeff Kirsher 
1907527a6266SJeff Kirsher 	rxq->rx_ring_size = mp->rx_ring_size;
1908527a6266SJeff Kirsher 
1909527a6266SJeff Kirsher 	rxq->rx_desc_count = 0;
1910527a6266SJeff Kirsher 	rxq->rx_curr_desc = 0;
1911527a6266SJeff Kirsher 	rxq->rx_used_desc = 0;
1912527a6266SJeff Kirsher 
1913527a6266SJeff Kirsher 	size = rxq->rx_ring_size * sizeof(struct rx_desc);
1914527a6266SJeff Kirsher 
1915527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size) {
1916527a6266SJeff Kirsher 		rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1917527a6266SJeff Kirsher 						mp->rx_desc_sram_size);
1918527a6266SJeff Kirsher 		rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1919527a6266SJeff Kirsher 	} else {
1920527a6266SJeff Kirsher 		rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1921527a6266SJeff Kirsher 						       size, &rxq->rx_desc_dma,
1922527a6266SJeff Kirsher 						       GFP_KERNEL);
1923527a6266SJeff Kirsher 	}
1924527a6266SJeff Kirsher 
1925527a6266SJeff Kirsher 	if (rxq->rx_desc_area == NULL) {
1926527a6266SJeff Kirsher 		netdev_err(mp->dev,
1927527a6266SJeff Kirsher 			   "can't allocate rx ring (%d bytes)\n", size);
1928527a6266SJeff Kirsher 		goto out;
1929527a6266SJeff Kirsher 	}
1930527a6266SJeff Kirsher 	memset(rxq->rx_desc_area, 0, size);
1931527a6266SJeff Kirsher 
1932527a6266SJeff Kirsher 	rxq->rx_desc_area_size = size;
19339fa8e980SLubomir Rintel 	rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1934527a6266SJeff Kirsher 				    GFP_KERNEL);
1935b2adaca9SJoe Perches 	if (rxq->rx_skb == NULL)
1936527a6266SJeff Kirsher 		goto out_free;
1937527a6266SJeff Kirsher 
193864699336SJoe Perches 	rx_desc = rxq->rx_desc_area;
1939527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1940527a6266SJeff Kirsher 		int nexti;
1941527a6266SJeff Kirsher 
1942527a6266SJeff Kirsher 		nexti = i + 1;
1943527a6266SJeff Kirsher 		if (nexti == rxq->rx_ring_size)
1944527a6266SJeff Kirsher 			nexti = 0;
1945527a6266SJeff Kirsher 
1946527a6266SJeff Kirsher 		rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1947527a6266SJeff Kirsher 					nexti * sizeof(struct rx_desc);
1948527a6266SJeff Kirsher 	}
1949527a6266SJeff Kirsher 
1950527a6266SJeff Kirsher 	return 0;
1951527a6266SJeff Kirsher 
1952527a6266SJeff Kirsher 
1953527a6266SJeff Kirsher out_free:
1954527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size)
1955527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1956527a6266SJeff Kirsher 	else
1957527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, size,
1958527a6266SJeff Kirsher 				  rxq->rx_desc_area,
1959527a6266SJeff Kirsher 				  rxq->rx_desc_dma);
1960527a6266SJeff Kirsher 
1961527a6266SJeff Kirsher out:
1962527a6266SJeff Kirsher 	return -ENOMEM;
1963527a6266SJeff Kirsher }
1964527a6266SJeff Kirsher 
1965527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq)
1966527a6266SJeff Kirsher {
1967527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1968527a6266SJeff Kirsher 	int i;
1969527a6266SJeff Kirsher 
1970527a6266SJeff Kirsher 	rxq_disable(rxq);
1971527a6266SJeff Kirsher 
1972527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1973527a6266SJeff Kirsher 		if (rxq->rx_skb[i]) {
1974527a6266SJeff Kirsher 			dev_kfree_skb(rxq->rx_skb[i]);
1975527a6266SJeff Kirsher 			rxq->rx_desc_count--;
1976527a6266SJeff Kirsher 		}
1977527a6266SJeff Kirsher 	}
1978527a6266SJeff Kirsher 
1979527a6266SJeff Kirsher 	if (rxq->rx_desc_count) {
1980527a6266SJeff Kirsher 		netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
1981527a6266SJeff Kirsher 			   rxq->rx_desc_count);
1982527a6266SJeff Kirsher 	}
1983527a6266SJeff Kirsher 
1984527a6266SJeff Kirsher 	if (rxq->index == 0 &&
1985527a6266SJeff Kirsher 	    rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
1986527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1987527a6266SJeff Kirsher 	else
1988527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
1989527a6266SJeff Kirsher 				  rxq->rx_desc_area, rxq->rx_desc_dma);
1990527a6266SJeff Kirsher 
1991527a6266SJeff Kirsher 	kfree(rxq->rx_skb);
1992527a6266SJeff Kirsher }
1993527a6266SJeff Kirsher 
1994527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index)
1995527a6266SJeff Kirsher {
1996527a6266SJeff Kirsher 	struct tx_queue *txq = mp->txq + index;
1997527a6266SJeff Kirsher 	struct tx_desc *tx_desc;
1998527a6266SJeff Kirsher 	int size;
1999527a6266SJeff Kirsher 	int i;
2000527a6266SJeff Kirsher 
2001527a6266SJeff Kirsher 	txq->index = index;
2002527a6266SJeff Kirsher 
2003527a6266SJeff Kirsher 	txq->tx_ring_size = mp->tx_ring_size;
2004527a6266SJeff Kirsher 
2005ee9e4956SEzequiel Garcia 	/* A queue must always have room for at least one skb.
2006ee9e4956SEzequiel Garcia 	 * Therefore, stop the queue when the free entries reaches
2007ee9e4956SEzequiel Garcia 	 * the maximum number of descriptors per skb.
2008ee9e4956SEzequiel Garcia 	 */
2009ee9e4956SEzequiel Garcia 	txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS;
2010ee9e4956SEzequiel Garcia 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2011ee9e4956SEzequiel Garcia 
2012527a6266SJeff Kirsher 	txq->tx_desc_count = 0;
2013527a6266SJeff Kirsher 	txq->tx_curr_desc = 0;
2014527a6266SJeff Kirsher 	txq->tx_used_desc = 0;
2015527a6266SJeff Kirsher 
2016527a6266SJeff Kirsher 	size = txq->tx_ring_size * sizeof(struct tx_desc);
2017527a6266SJeff Kirsher 
2018527a6266SJeff Kirsher 	if (index == 0 && size <= mp->tx_desc_sram_size) {
2019527a6266SJeff Kirsher 		txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
2020527a6266SJeff Kirsher 						mp->tx_desc_sram_size);
2021527a6266SJeff Kirsher 		txq->tx_desc_dma = mp->tx_desc_sram_addr;
2022527a6266SJeff Kirsher 	} else {
2023527a6266SJeff Kirsher 		txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
2024527a6266SJeff Kirsher 						       size, &txq->tx_desc_dma,
2025527a6266SJeff Kirsher 						       GFP_KERNEL);
2026527a6266SJeff Kirsher 	}
2027527a6266SJeff Kirsher 
2028527a6266SJeff Kirsher 	if (txq->tx_desc_area == NULL) {
2029527a6266SJeff Kirsher 		netdev_err(mp->dev,
2030527a6266SJeff Kirsher 			   "can't allocate tx ring (%d bytes)\n", size);
2031527a6266SJeff Kirsher 		return -ENOMEM;
2032527a6266SJeff Kirsher 	}
2033527a6266SJeff Kirsher 	memset(txq->tx_desc_area, 0, size);
2034527a6266SJeff Kirsher 
2035527a6266SJeff Kirsher 	txq->tx_desc_area_size = size;
2036527a6266SJeff Kirsher 
203764699336SJoe Perches 	tx_desc = txq->tx_desc_area;
2038527a6266SJeff Kirsher 	for (i = 0; i < txq->tx_ring_size; i++) {
2039527a6266SJeff Kirsher 		struct tx_desc *txd = tx_desc + i;
2040527a6266SJeff Kirsher 		int nexti;
2041527a6266SJeff Kirsher 
2042527a6266SJeff Kirsher 		nexti = i + 1;
2043527a6266SJeff Kirsher 		if (nexti == txq->tx_ring_size)
2044527a6266SJeff Kirsher 			nexti = 0;
2045527a6266SJeff Kirsher 
2046527a6266SJeff Kirsher 		txd->cmd_sts = 0;
2047527a6266SJeff Kirsher 		txd->next_desc_ptr = txq->tx_desc_dma +
2048527a6266SJeff Kirsher 					nexti * sizeof(struct tx_desc);
2049527a6266SJeff Kirsher 	}
2050527a6266SJeff Kirsher 
20513ae8f4e0SEzequiel Garcia 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
20523ae8f4e0SEzequiel Garcia 	txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent,
20533ae8f4e0SEzequiel Garcia 					   txq->tx_ring_size * TSO_HEADER_SIZE,
20543ae8f4e0SEzequiel Garcia 					   &txq->tso_hdrs_dma, GFP_KERNEL);
20553ae8f4e0SEzequiel Garcia 	if (txq->tso_hdrs == NULL) {
20563ae8f4e0SEzequiel Garcia 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
20573ae8f4e0SEzequiel Garcia 				  txq->tx_desc_area, txq->tx_desc_dma);
20583ae8f4e0SEzequiel Garcia 		return -ENOMEM;
20593ae8f4e0SEzequiel Garcia 	}
2060527a6266SJeff Kirsher 	skb_queue_head_init(&txq->tx_skb);
2061527a6266SJeff Kirsher 
2062527a6266SJeff Kirsher 	return 0;
2063527a6266SJeff Kirsher }
2064527a6266SJeff Kirsher 
2065527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq)
2066527a6266SJeff Kirsher {
2067527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
2068527a6266SJeff Kirsher 
2069527a6266SJeff Kirsher 	txq_disable(txq);
2070527a6266SJeff Kirsher 	txq_reclaim(txq, txq->tx_ring_size, 1);
2071527a6266SJeff Kirsher 
2072527a6266SJeff Kirsher 	BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
2073527a6266SJeff Kirsher 
2074527a6266SJeff Kirsher 	if (txq->index == 0 &&
2075527a6266SJeff Kirsher 	    txq->tx_desc_area_size <= mp->tx_desc_sram_size)
2076527a6266SJeff Kirsher 		iounmap(txq->tx_desc_area);
2077527a6266SJeff Kirsher 	else
2078527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
2079527a6266SJeff Kirsher 				  txq->tx_desc_area, txq->tx_desc_dma);
20803ae8f4e0SEzequiel Garcia 	if (txq->tso_hdrs)
20813ae8f4e0SEzequiel Garcia 		dma_free_coherent(mp->dev->dev.parent,
20823ae8f4e0SEzequiel Garcia 				  txq->tx_ring_size * TSO_HEADER_SIZE,
20833ae8f4e0SEzequiel Garcia 				  txq->tso_hdrs, txq->tso_hdrs_dma);
2084527a6266SJeff Kirsher }
2085527a6266SJeff Kirsher 
2086527a6266SJeff Kirsher 
2087527a6266SJeff Kirsher /* netdev ops and related ***************************************************/
2088527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
2089527a6266SJeff Kirsher {
2090527a6266SJeff Kirsher 	u32 int_cause;
2091527a6266SJeff Kirsher 	u32 int_cause_ext;
2092527a6266SJeff Kirsher 
2093527a6266SJeff Kirsher 	int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
2094527a6266SJeff Kirsher 	if (int_cause == 0)
2095527a6266SJeff Kirsher 		return 0;
2096527a6266SJeff Kirsher 
2097527a6266SJeff Kirsher 	int_cause_ext = 0;
2098527a6266SJeff Kirsher 	if (int_cause & INT_EXT) {
2099527a6266SJeff Kirsher 		int_cause &= ~INT_EXT;
2100527a6266SJeff Kirsher 		int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
2101527a6266SJeff Kirsher 	}
2102527a6266SJeff Kirsher 
2103527a6266SJeff Kirsher 	if (int_cause) {
2104527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE, ~int_cause);
2105527a6266SJeff Kirsher 		mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
2106527a6266SJeff Kirsher 				~(rdlp(mp, TXQ_COMMAND) & 0xff);
2107527a6266SJeff Kirsher 		mp->work_rx |= (int_cause & INT_RX) >> 2;
2108527a6266SJeff Kirsher 	}
2109527a6266SJeff Kirsher 
2110527a6266SJeff Kirsher 	int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
2111527a6266SJeff Kirsher 	if (int_cause_ext) {
2112527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
2113527a6266SJeff Kirsher 		if (int_cause_ext & INT_EXT_LINK_PHY)
2114527a6266SJeff Kirsher 			mp->work_link = 1;
2115527a6266SJeff Kirsher 		mp->work_tx |= int_cause_ext & INT_EXT_TX;
2116527a6266SJeff Kirsher 	}
2117527a6266SJeff Kirsher 
2118527a6266SJeff Kirsher 	return 1;
2119527a6266SJeff Kirsher }
2120527a6266SJeff Kirsher 
2121527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
2122527a6266SJeff Kirsher {
2123527a6266SJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_id;
2124527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2125527a6266SJeff Kirsher 
2126527a6266SJeff Kirsher 	if (unlikely(!mv643xx_eth_collect_events(mp)))
2127527a6266SJeff Kirsher 		return IRQ_NONE;
2128527a6266SJeff Kirsher 
2129527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
2130527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2131527a6266SJeff Kirsher 
2132527a6266SJeff Kirsher 	return IRQ_HANDLED;
2133527a6266SJeff Kirsher }
2134527a6266SJeff Kirsher 
2135527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp)
2136527a6266SJeff Kirsher {
2137527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2138527a6266SJeff Kirsher 	u32 port_status;
2139527a6266SJeff Kirsher 	int speed;
2140527a6266SJeff Kirsher 	int duplex;
2141527a6266SJeff Kirsher 	int fc;
2142527a6266SJeff Kirsher 
2143527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
2144527a6266SJeff Kirsher 	if (!(port_status & LINK_UP)) {
2145527a6266SJeff Kirsher 		if (netif_carrier_ok(dev)) {
2146527a6266SJeff Kirsher 			int i;
2147527a6266SJeff Kirsher 
2148527a6266SJeff Kirsher 			netdev_info(dev, "link down\n");
2149527a6266SJeff Kirsher 
2150527a6266SJeff Kirsher 			netif_carrier_off(dev);
2151527a6266SJeff Kirsher 
2152527a6266SJeff Kirsher 			for (i = 0; i < mp->txq_count; i++) {
2153527a6266SJeff Kirsher 				struct tx_queue *txq = mp->txq + i;
2154527a6266SJeff Kirsher 
2155527a6266SJeff Kirsher 				txq_reclaim(txq, txq->tx_ring_size, 1);
2156527a6266SJeff Kirsher 				txq_reset_hw_ptr(txq);
2157527a6266SJeff Kirsher 			}
2158527a6266SJeff Kirsher 		}
2159527a6266SJeff Kirsher 		return;
2160527a6266SJeff Kirsher 	}
2161527a6266SJeff Kirsher 
2162527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
2163527a6266SJeff Kirsher 	case PORT_SPEED_10:
2164527a6266SJeff Kirsher 		speed = 10;
2165527a6266SJeff Kirsher 		break;
2166527a6266SJeff Kirsher 	case PORT_SPEED_100:
2167527a6266SJeff Kirsher 		speed = 100;
2168527a6266SJeff Kirsher 		break;
2169527a6266SJeff Kirsher 	case PORT_SPEED_1000:
2170527a6266SJeff Kirsher 		speed = 1000;
2171527a6266SJeff Kirsher 		break;
2172527a6266SJeff Kirsher 	default:
2173527a6266SJeff Kirsher 		speed = -1;
2174527a6266SJeff Kirsher 		break;
2175527a6266SJeff Kirsher 	}
2176527a6266SJeff Kirsher 	duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
2177527a6266SJeff Kirsher 	fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
2178527a6266SJeff Kirsher 
2179527a6266SJeff Kirsher 	netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
2180527a6266SJeff Kirsher 		    speed, duplex ? "full" : "half", fc ? "en" : "dis");
2181527a6266SJeff Kirsher 
2182527a6266SJeff Kirsher 	if (!netif_carrier_ok(dev))
2183527a6266SJeff Kirsher 		netif_carrier_on(dev);
2184527a6266SJeff Kirsher }
2185527a6266SJeff Kirsher 
2186527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
2187527a6266SJeff Kirsher {
2188527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2189527a6266SJeff Kirsher 	int work_done;
2190527a6266SJeff Kirsher 
2191527a6266SJeff Kirsher 	mp = container_of(napi, struct mv643xx_eth_private, napi);
2192527a6266SJeff Kirsher 
2193527a6266SJeff Kirsher 	if (unlikely(mp->oom)) {
2194527a6266SJeff Kirsher 		mp->oom = 0;
2195527a6266SJeff Kirsher 		del_timer(&mp->rx_oom);
2196527a6266SJeff Kirsher 	}
2197527a6266SJeff Kirsher 
2198527a6266SJeff Kirsher 	work_done = 0;
2199527a6266SJeff Kirsher 	while (work_done < budget) {
2200527a6266SJeff Kirsher 		u8 queue_mask;
2201527a6266SJeff Kirsher 		int queue;
2202527a6266SJeff Kirsher 		int work_tbd;
2203527a6266SJeff Kirsher 
2204527a6266SJeff Kirsher 		if (mp->work_link) {
2205527a6266SJeff Kirsher 			mp->work_link = 0;
2206527a6266SJeff Kirsher 			handle_link_event(mp);
2207527a6266SJeff Kirsher 			work_done++;
2208527a6266SJeff Kirsher 			continue;
2209527a6266SJeff Kirsher 		}
2210527a6266SJeff Kirsher 
2211527a6266SJeff Kirsher 		queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2212527a6266SJeff Kirsher 		if (likely(!mp->oom))
2213527a6266SJeff Kirsher 			queue_mask |= mp->work_rx_refill;
2214527a6266SJeff Kirsher 
2215527a6266SJeff Kirsher 		if (!queue_mask) {
2216527a6266SJeff Kirsher 			if (mv643xx_eth_collect_events(mp))
2217527a6266SJeff Kirsher 				continue;
2218527a6266SJeff Kirsher 			break;
2219527a6266SJeff Kirsher 		}
2220527a6266SJeff Kirsher 
2221527a6266SJeff Kirsher 		queue = fls(queue_mask) - 1;
2222527a6266SJeff Kirsher 		queue_mask = 1 << queue;
2223527a6266SJeff Kirsher 
2224527a6266SJeff Kirsher 		work_tbd = budget - work_done;
2225527a6266SJeff Kirsher 		if (work_tbd > 16)
2226527a6266SJeff Kirsher 			work_tbd = 16;
2227527a6266SJeff Kirsher 
2228527a6266SJeff Kirsher 		if (mp->work_tx_end & queue_mask) {
2229527a6266SJeff Kirsher 			txq_kick(mp->txq + queue);
2230527a6266SJeff Kirsher 		} else if (mp->work_tx & queue_mask) {
2231527a6266SJeff Kirsher 			work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2232527a6266SJeff Kirsher 			txq_maybe_wake(mp->txq + queue);
2233527a6266SJeff Kirsher 		} else if (mp->work_rx & queue_mask) {
2234527a6266SJeff Kirsher 			work_done += rxq_process(mp->rxq + queue, work_tbd);
2235527a6266SJeff Kirsher 		} else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
2236527a6266SJeff Kirsher 			work_done += rxq_refill(mp->rxq + queue, work_tbd);
2237527a6266SJeff Kirsher 		} else {
2238527a6266SJeff Kirsher 			BUG();
2239527a6266SJeff Kirsher 		}
2240527a6266SJeff Kirsher 	}
2241527a6266SJeff Kirsher 
2242527a6266SJeff Kirsher 	if (work_done < budget) {
2243527a6266SJeff Kirsher 		if (mp->oom)
2244527a6266SJeff Kirsher 			mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
2245527a6266SJeff Kirsher 		napi_complete(napi);
2246527a6266SJeff Kirsher 		wrlp(mp, INT_MASK, mp->int_mask);
2247527a6266SJeff Kirsher 	}
2248527a6266SJeff Kirsher 
2249527a6266SJeff Kirsher 	return work_done;
2250527a6266SJeff Kirsher }
2251527a6266SJeff Kirsher 
2252527a6266SJeff Kirsher static inline void oom_timer_wrapper(unsigned long data)
2253527a6266SJeff Kirsher {
2254527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = (void *)data;
2255527a6266SJeff Kirsher 
2256527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2257527a6266SJeff Kirsher }
2258527a6266SJeff Kirsher 
2259527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp)
2260527a6266SJeff Kirsher {
2261527a6266SJeff Kirsher 	u32 pscr;
2262527a6266SJeff Kirsher 	int i;
2263527a6266SJeff Kirsher 
2264527a6266SJeff Kirsher 	/*
2265527a6266SJeff Kirsher 	 * Perform PHY reset, if there is a PHY.
2266527a6266SJeff Kirsher 	 */
2267527a6266SJeff Kirsher 	if (mp->phy != NULL) {
2268527a6266SJeff Kirsher 		struct ethtool_cmd cmd;
2269527a6266SJeff Kirsher 
2270527a6266SJeff Kirsher 		mv643xx_eth_get_settings(mp->dev, &cmd);
22717cd14636SFlorian Fainelli 		phy_init_hw(mp->phy);
2272527a6266SJeff Kirsher 		mv643xx_eth_set_settings(mp->dev, &cmd);
227358911151SSebastian Hesselbarth 		phy_start(mp->phy);
2274527a6266SJeff Kirsher 	}
2275527a6266SJeff Kirsher 
2276527a6266SJeff Kirsher 	/*
2277527a6266SJeff Kirsher 	 * Configure basic link parameters.
2278527a6266SJeff Kirsher 	 */
2279527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2280527a6266SJeff Kirsher 
2281527a6266SJeff Kirsher 	pscr |= SERIAL_PORT_ENABLE;
2282527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2283527a6266SJeff Kirsher 
2284527a6266SJeff Kirsher 	pscr |= DO_NOT_FORCE_LINK_FAIL;
2285527a6266SJeff Kirsher 	if (mp->phy == NULL)
2286527a6266SJeff Kirsher 		pscr |= FORCE_LINK_PASS;
2287527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2288527a6266SJeff Kirsher 
2289527a6266SJeff Kirsher 	/*
2290527a6266SJeff Kirsher 	 * Configure TX path and queues.
2291527a6266SJeff Kirsher 	 */
2292527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2293527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2294527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
2295527a6266SJeff Kirsher 
2296527a6266SJeff Kirsher 		txq_reset_hw_ptr(txq);
2297527a6266SJeff Kirsher 		txq_set_rate(txq, 1000000000, 16777216);
2298527a6266SJeff Kirsher 		txq_set_fixed_prio_mode(txq);
2299527a6266SJeff Kirsher 	}
2300527a6266SJeff Kirsher 
2301527a6266SJeff Kirsher 	/*
2302527a6266SJeff Kirsher 	 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2303527a6266SJeff Kirsher 	 * frames to RX queue #0, and include the pseudo-header when
2304527a6266SJeff Kirsher 	 * calculating receive checksums.
2305527a6266SJeff Kirsher 	 */
2306527a6266SJeff Kirsher 	mv643xx_eth_set_features(mp->dev, mp->dev->features);
2307527a6266SJeff Kirsher 
2308527a6266SJeff Kirsher 	/*
2309527a6266SJeff Kirsher 	 * Treat BPDUs as normal multicasts, and disable partition mode.
2310527a6266SJeff Kirsher 	 */
2311527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
2312527a6266SJeff Kirsher 
2313527a6266SJeff Kirsher 	/*
2314527a6266SJeff Kirsher 	 * Add configured unicast addresses to address filter table.
2315527a6266SJeff Kirsher 	 */
2316527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(mp->dev);
2317527a6266SJeff Kirsher 
2318527a6266SJeff Kirsher 	/*
2319527a6266SJeff Kirsher 	 * Enable the receive queues.
2320527a6266SJeff Kirsher 	 */
2321527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2322527a6266SJeff Kirsher 		struct rx_queue *rxq = mp->rxq + i;
2323527a6266SJeff Kirsher 		u32 addr;
2324527a6266SJeff Kirsher 
2325527a6266SJeff Kirsher 		addr = (u32)rxq->rx_desc_dma;
2326527a6266SJeff Kirsher 		addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
2327527a6266SJeff Kirsher 		wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
2328527a6266SJeff Kirsher 
2329527a6266SJeff Kirsher 		rxq_enable(rxq);
2330527a6266SJeff Kirsher 	}
2331527a6266SJeff Kirsher }
2332527a6266SJeff Kirsher 
2333527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2334527a6266SJeff Kirsher {
2335527a6266SJeff Kirsher 	int skb_size;
2336527a6266SJeff Kirsher 
2337527a6266SJeff Kirsher 	/*
2338527a6266SJeff Kirsher 	 * Reserve 2+14 bytes for an ethernet header (the hardware
2339527a6266SJeff Kirsher 	 * automatically prepends 2 bytes of dummy data to each
2340527a6266SJeff Kirsher 	 * received packet), 16 bytes for up to four VLAN tags, and
2341527a6266SJeff Kirsher 	 * 4 bytes for the trailing FCS -- 36 bytes total.
2342527a6266SJeff Kirsher 	 */
2343527a6266SJeff Kirsher 	skb_size = mp->dev->mtu + 36;
2344527a6266SJeff Kirsher 
2345527a6266SJeff Kirsher 	/*
2346527a6266SJeff Kirsher 	 * Make sure that the skb size is a multiple of 8 bytes, as
2347527a6266SJeff Kirsher 	 * the lower three bits of the receive descriptor's buffer
2348527a6266SJeff Kirsher 	 * size field are ignored by the hardware.
2349527a6266SJeff Kirsher 	 */
2350527a6266SJeff Kirsher 	mp->skb_size = (skb_size + 7) & ~7;
2351527a6266SJeff Kirsher 
2352527a6266SJeff Kirsher 	/*
2353527a6266SJeff Kirsher 	 * If NET_SKB_PAD is smaller than a cache line,
2354527a6266SJeff Kirsher 	 * netdev_alloc_skb() will cause skb->data to be misaligned
2355527a6266SJeff Kirsher 	 * to a cache line boundary.  If this is the case, include
2356527a6266SJeff Kirsher 	 * some extra space to allow re-aligning the data area.
2357527a6266SJeff Kirsher 	 */
2358527a6266SJeff Kirsher 	mp->skb_size += SKB_DMA_REALIGN;
2359527a6266SJeff Kirsher }
2360527a6266SJeff Kirsher 
2361527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev)
2362527a6266SJeff Kirsher {
2363527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2364527a6266SJeff Kirsher 	int err;
2365527a6266SJeff Kirsher 	int i;
2366527a6266SJeff Kirsher 
2367527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE, 0);
2368527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE_EXT, 0);
2369527a6266SJeff Kirsher 	rdlp(mp, INT_CAUSE_EXT);
2370527a6266SJeff Kirsher 
2371527a6266SJeff Kirsher 	err = request_irq(dev->irq, mv643xx_eth_irq,
2372527a6266SJeff Kirsher 			  IRQF_SHARED, dev->name, dev);
2373527a6266SJeff Kirsher 	if (err) {
2374527a6266SJeff Kirsher 		netdev_err(dev, "can't assign irq\n");
2375527a6266SJeff Kirsher 		return -EAGAIN;
2376527a6266SJeff Kirsher 	}
2377527a6266SJeff Kirsher 
2378527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2379527a6266SJeff Kirsher 
2380527a6266SJeff Kirsher 	napi_enable(&mp->napi);
2381527a6266SJeff Kirsher 
2382527a6266SJeff Kirsher 	mp->int_mask = INT_EXT;
2383527a6266SJeff Kirsher 
2384527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2385527a6266SJeff Kirsher 		err = rxq_init(mp, i);
2386527a6266SJeff Kirsher 		if (err) {
2387527a6266SJeff Kirsher 			while (--i >= 0)
2388527a6266SJeff Kirsher 				rxq_deinit(mp->rxq + i);
2389527a6266SJeff Kirsher 			goto out;
2390527a6266SJeff Kirsher 		}
2391527a6266SJeff Kirsher 
2392527a6266SJeff Kirsher 		rxq_refill(mp->rxq + i, INT_MAX);
2393527a6266SJeff Kirsher 		mp->int_mask |= INT_RX_0 << i;
2394527a6266SJeff Kirsher 	}
2395527a6266SJeff Kirsher 
2396527a6266SJeff Kirsher 	if (mp->oom) {
2397527a6266SJeff Kirsher 		mp->rx_oom.expires = jiffies + (HZ / 10);
2398527a6266SJeff Kirsher 		add_timer(&mp->rx_oom);
2399527a6266SJeff Kirsher 	}
2400527a6266SJeff Kirsher 
2401527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2402527a6266SJeff Kirsher 		err = txq_init(mp, i);
2403527a6266SJeff Kirsher 		if (err) {
2404527a6266SJeff Kirsher 			while (--i >= 0)
2405527a6266SJeff Kirsher 				txq_deinit(mp->txq + i);
2406527a6266SJeff Kirsher 			goto out_free;
2407527a6266SJeff Kirsher 		}
2408527a6266SJeff Kirsher 		mp->int_mask |= INT_TX_END_0 << i;
2409527a6266SJeff Kirsher 	}
2410527a6266SJeff Kirsher 
2411f564412cSSebastian Hesselbarth 	add_timer(&mp->mib_counters_timer);
2412527a6266SJeff Kirsher 	port_start(mp);
2413527a6266SJeff Kirsher 
2414527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
2415527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2416527a6266SJeff Kirsher 
2417527a6266SJeff Kirsher 	return 0;
2418527a6266SJeff Kirsher 
2419527a6266SJeff Kirsher 
2420527a6266SJeff Kirsher out_free:
2421527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2422527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2423527a6266SJeff Kirsher out:
2424527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2425527a6266SJeff Kirsher 
2426527a6266SJeff Kirsher 	return err;
2427527a6266SJeff Kirsher }
2428527a6266SJeff Kirsher 
2429527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp)
2430527a6266SJeff Kirsher {
2431527a6266SJeff Kirsher 	unsigned int data;
2432527a6266SJeff Kirsher 	int i;
2433527a6266SJeff Kirsher 
2434527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2435527a6266SJeff Kirsher 		rxq_disable(mp->rxq + i);
2436527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2437527a6266SJeff Kirsher 		txq_disable(mp->txq + i);
2438527a6266SJeff Kirsher 
2439527a6266SJeff Kirsher 	while (1) {
2440527a6266SJeff Kirsher 		u32 ps = rdlp(mp, PORT_STATUS);
2441527a6266SJeff Kirsher 
2442527a6266SJeff Kirsher 		if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2443527a6266SJeff Kirsher 			break;
2444527a6266SJeff Kirsher 		udelay(10);
2445527a6266SJeff Kirsher 	}
2446527a6266SJeff Kirsher 
2447527a6266SJeff Kirsher 	/* Reset the Enable bit in the Configuration Register */
2448527a6266SJeff Kirsher 	data = rdlp(mp, PORT_SERIAL_CONTROL);
2449527a6266SJeff Kirsher 	data &= ~(SERIAL_PORT_ENABLE		|
2450527a6266SJeff Kirsher 		  DO_NOT_FORCE_LINK_FAIL	|
2451527a6266SJeff Kirsher 		  FORCE_LINK_PASS);
2452527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, data);
2453527a6266SJeff Kirsher }
2454527a6266SJeff Kirsher 
2455527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev)
2456527a6266SJeff Kirsher {
2457527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2458527a6266SJeff Kirsher 	int i;
2459527a6266SJeff Kirsher 
2460527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, 0x00000000);
2461527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2462527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2463527a6266SJeff Kirsher 
2464527a6266SJeff Kirsher 	napi_disable(&mp->napi);
2465527a6266SJeff Kirsher 
2466527a6266SJeff Kirsher 	del_timer_sync(&mp->rx_oom);
2467527a6266SJeff Kirsher 
2468527a6266SJeff Kirsher 	netif_carrier_off(dev);
246958911151SSebastian Hesselbarth 	if (mp->phy)
247058911151SSebastian Hesselbarth 		phy_stop(mp->phy);
2471527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2472527a6266SJeff Kirsher 
2473527a6266SJeff Kirsher 	port_reset(mp);
2474527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
2475527a6266SJeff Kirsher 	mib_counters_update(mp);
2476527a6266SJeff Kirsher 	del_timer_sync(&mp->mib_counters_timer);
2477527a6266SJeff Kirsher 
2478527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2479527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2480527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2481527a6266SJeff Kirsher 		txq_deinit(mp->txq + i);
2482527a6266SJeff Kirsher 
2483527a6266SJeff Kirsher 	return 0;
2484527a6266SJeff Kirsher }
2485527a6266SJeff Kirsher 
2486527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2487527a6266SJeff Kirsher {
2488527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2489260055bbSPhil Sutter 	int ret;
2490527a6266SJeff Kirsher 
2491260055bbSPhil Sutter 	if (mp->phy == NULL)
2492260055bbSPhil Sutter 		return -ENOTSUPP;
2493527a6266SJeff Kirsher 
2494260055bbSPhil Sutter 	ret = phy_mii_ioctl(mp->phy, ifr, cmd);
2495260055bbSPhil Sutter 	if (!ret)
24960a9e413bSEzequiel Garcia 		mv643xx_eth_adjust_link(dev);
2497260055bbSPhil Sutter 	return ret;
2498527a6266SJeff Kirsher }
2499527a6266SJeff Kirsher 
2500527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2501527a6266SJeff Kirsher {
2502527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2503527a6266SJeff Kirsher 
2504527a6266SJeff Kirsher 	if (new_mtu < 64 || new_mtu > 9500)
2505527a6266SJeff Kirsher 		return -EINVAL;
2506527a6266SJeff Kirsher 
2507527a6266SJeff Kirsher 	dev->mtu = new_mtu;
2508527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2509527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2510527a6266SJeff Kirsher 
2511527a6266SJeff Kirsher 	if (!netif_running(dev))
2512527a6266SJeff Kirsher 		return 0;
2513527a6266SJeff Kirsher 
2514527a6266SJeff Kirsher 	/*
2515527a6266SJeff Kirsher 	 * Stop and then re-open the interface. This will allocate RX
2516527a6266SJeff Kirsher 	 * skbs of the new MTU.
2517527a6266SJeff Kirsher 	 * There is a possible danger that the open will not succeed,
2518527a6266SJeff Kirsher 	 * due to memory being full.
2519527a6266SJeff Kirsher 	 */
2520527a6266SJeff Kirsher 	mv643xx_eth_stop(dev);
2521527a6266SJeff Kirsher 	if (mv643xx_eth_open(dev)) {
2522527a6266SJeff Kirsher 		netdev_err(dev,
2523527a6266SJeff Kirsher 			   "fatal error on re-opening device after MTU change\n");
2524527a6266SJeff Kirsher 	}
2525527a6266SJeff Kirsher 
2526527a6266SJeff Kirsher 	return 0;
2527527a6266SJeff Kirsher }
2528527a6266SJeff Kirsher 
2529527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly)
2530527a6266SJeff Kirsher {
2531527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2532527a6266SJeff Kirsher 
2533527a6266SJeff Kirsher 	mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2534527a6266SJeff Kirsher 	if (netif_running(mp->dev)) {
2535527a6266SJeff Kirsher 		netif_tx_stop_all_queues(mp->dev);
2536527a6266SJeff Kirsher 		port_reset(mp);
2537527a6266SJeff Kirsher 		port_start(mp);
2538527a6266SJeff Kirsher 		netif_tx_wake_all_queues(mp->dev);
2539527a6266SJeff Kirsher 	}
2540527a6266SJeff Kirsher }
2541527a6266SJeff Kirsher 
2542527a6266SJeff Kirsher static void mv643xx_eth_tx_timeout(struct net_device *dev)
2543527a6266SJeff Kirsher {
2544527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2545527a6266SJeff Kirsher 
2546527a6266SJeff Kirsher 	netdev_info(dev, "tx timeout\n");
2547527a6266SJeff Kirsher 
2548527a6266SJeff Kirsher 	schedule_work(&mp->tx_timeout_task);
2549527a6266SJeff Kirsher }
2550527a6266SJeff Kirsher 
2551527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2552527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev)
2553527a6266SJeff Kirsher {
2554527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2555527a6266SJeff Kirsher 
2556527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2557527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2558527a6266SJeff Kirsher 
2559527a6266SJeff Kirsher 	mv643xx_eth_irq(dev->irq, dev);
2560527a6266SJeff Kirsher 
2561527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2562527a6266SJeff Kirsher }
2563527a6266SJeff Kirsher #endif
2564527a6266SJeff Kirsher 
2565527a6266SJeff Kirsher 
2566527a6266SJeff Kirsher /* platform glue ************************************************************/
2567527a6266SJeff Kirsher static void
2568527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
256963a9332bSAndrew Lunn 			      const struct mbus_dram_target_info *dram)
2570527a6266SJeff Kirsher {
2571527a6266SJeff Kirsher 	void __iomem *base = msp->base;
2572527a6266SJeff Kirsher 	u32 win_enable;
2573527a6266SJeff Kirsher 	u32 win_protect;
2574527a6266SJeff Kirsher 	int i;
2575527a6266SJeff Kirsher 
2576527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
2577527a6266SJeff Kirsher 		writel(0, base + WINDOW_BASE(i));
2578527a6266SJeff Kirsher 		writel(0, base + WINDOW_SIZE(i));
2579527a6266SJeff Kirsher 		if (i < 4)
2580527a6266SJeff Kirsher 			writel(0, base + WINDOW_REMAP_HIGH(i));
2581527a6266SJeff Kirsher 	}
2582527a6266SJeff Kirsher 
2583527a6266SJeff Kirsher 	win_enable = 0x3f;
2584527a6266SJeff Kirsher 	win_protect = 0;
2585527a6266SJeff Kirsher 
2586527a6266SJeff Kirsher 	for (i = 0; i < dram->num_cs; i++) {
258763a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
2588527a6266SJeff Kirsher 
2589527a6266SJeff Kirsher 		writel((cs->base & 0xffff0000) |
2590527a6266SJeff Kirsher 			(cs->mbus_attr << 8) |
2591527a6266SJeff Kirsher 			dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2592527a6266SJeff Kirsher 		writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2593527a6266SJeff Kirsher 
2594527a6266SJeff Kirsher 		win_enable &= ~(1 << i);
2595527a6266SJeff Kirsher 		win_protect |= 3 << (2 * i);
2596527a6266SJeff Kirsher 	}
2597527a6266SJeff Kirsher 
2598527a6266SJeff Kirsher 	writel(win_enable, base + WINDOW_BAR_ENABLE);
2599527a6266SJeff Kirsher 	msp->win_protect = win_protect;
2600527a6266SJeff Kirsher }
2601527a6266SJeff Kirsher 
2602527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2603527a6266SJeff Kirsher {
2604527a6266SJeff Kirsher 	/*
2605527a6266SJeff Kirsher 	 * Check whether we have a 14-bit coal limit field in bits
2606527a6266SJeff Kirsher 	 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2607527a6266SJeff Kirsher 	 * SDMA config register.
2608527a6266SJeff Kirsher 	 */
2609527a6266SJeff Kirsher 	writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2610527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
2611527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 1;
2612527a6266SJeff Kirsher 	else
2613527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 0;
2614527a6266SJeff Kirsher 
2615527a6266SJeff Kirsher 	/*
2616527a6266SJeff Kirsher 	 * Check whether the MAC supports TX rate control, and if
2617527a6266SJeff Kirsher 	 * yes, whether its associated registers are in the old or
2618527a6266SJeff Kirsher 	 * the new place.
2619527a6266SJeff Kirsher 	 */
2620527a6266SJeff Kirsher 	writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2621527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
2622527a6266SJeff Kirsher 		msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2623527a6266SJeff Kirsher 	} else {
2624527a6266SJeff Kirsher 		writel(7, msp->base + 0x0400 + TX_BW_RATE);
2625527a6266SJeff Kirsher 		if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
2626527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2627527a6266SJeff Kirsher 		else
2628527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2629527a6266SJeff Kirsher 	}
2630527a6266SJeff Kirsher }
2631527a6266SJeff Kirsher 
263276723bcaSSebastian Hesselbarth #if defined(CONFIG_OF)
263376723bcaSSebastian Hesselbarth static const struct of_device_id mv643xx_eth_shared_ids[] = {
263476723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,orion-eth", },
263576723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,kirkwood-eth", },
263676723bcaSSebastian Hesselbarth 	{ }
263776723bcaSSebastian Hesselbarth };
263876723bcaSSebastian Hesselbarth MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids);
263976723bcaSSebastian Hesselbarth #endif
264076723bcaSSebastian Hesselbarth 
264176723bcaSSebastian Hesselbarth #if defined(CONFIG_OF) && !defined(CONFIG_MV64X60)
264276723bcaSSebastian Hesselbarth #define mv643xx_eth_property(_np, _name, _v)				\
264376723bcaSSebastian Hesselbarth 	do {								\
264476723bcaSSebastian Hesselbarth 		u32 tmp;						\
264576723bcaSSebastian Hesselbarth 		if (!of_property_read_u32(_np, "marvell," _name, &tmp))	\
264676723bcaSSebastian Hesselbarth 			_v = tmp;					\
264776723bcaSSebastian Hesselbarth 	} while (0)
264876723bcaSSebastian Hesselbarth 
264976723bcaSSebastian Hesselbarth static struct platform_device *port_platdev[3];
265076723bcaSSebastian Hesselbarth 
265176723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
265276723bcaSSebastian Hesselbarth 					  struct device_node *pnp)
265376723bcaSSebastian Hesselbarth {
265476723bcaSSebastian Hesselbarth 	struct platform_device *ppdev;
265576723bcaSSebastian Hesselbarth 	struct mv643xx_eth_platform_data ppd;
265676723bcaSSebastian Hesselbarth 	struct resource res;
265776723bcaSSebastian Hesselbarth 	const char *mac_addr;
265876723bcaSSebastian Hesselbarth 	int ret;
2659785bf6f7SJonas Gorski 	int dev_num = 0;
266076723bcaSSebastian Hesselbarth 
266176723bcaSSebastian Hesselbarth 	memset(&ppd, 0, sizeof(ppd));
266276723bcaSSebastian Hesselbarth 	ppd.shared = pdev;
266376723bcaSSebastian Hesselbarth 
266476723bcaSSebastian Hesselbarth 	memset(&res, 0, sizeof(res));
266576723bcaSSebastian Hesselbarth 	if (!of_irq_to_resource(pnp, 0, &res)) {
266676723bcaSSebastian Hesselbarth 		dev_err(&pdev->dev, "missing interrupt on %s\n", pnp->name);
266776723bcaSSebastian Hesselbarth 		return -EINVAL;
266876723bcaSSebastian Hesselbarth 	}
266976723bcaSSebastian Hesselbarth 
267076723bcaSSebastian Hesselbarth 	if (of_property_read_u32(pnp, "reg", &ppd.port_number)) {
267176723bcaSSebastian Hesselbarth 		dev_err(&pdev->dev, "missing reg property on %s\n", pnp->name);
267276723bcaSSebastian Hesselbarth 		return -EINVAL;
267376723bcaSSebastian Hesselbarth 	}
267476723bcaSSebastian Hesselbarth 
267576723bcaSSebastian Hesselbarth 	if (ppd.port_number >= 3) {
267676723bcaSSebastian Hesselbarth 		dev_err(&pdev->dev, "invalid reg property on %s\n", pnp->name);
267776723bcaSSebastian Hesselbarth 		return -EINVAL;
267876723bcaSSebastian Hesselbarth 	}
267976723bcaSSebastian Hesselbarth 
2680785bf6f7SJonas Gorski 	while (dev_num < 3 && port_platdev[dev_num])
2681785bf6f7SJonas Gorski 		dev_num++;
2682785bf6f7SJonas Gorski 
2683785bf6f7SJonas Gorski 	if (dev_num == 3) {
2684785bf6f7SJonas Gorski 		dev_err(&pdev->dev, "too many ports registered\n");
2685785bf6f7SJonas Gorski 		return -EINVAL;
2686785bf6f7SJonas Gorski 	}
2687785bf6f7SJonas Gorski 
268876723bcaSSebastian Hesselbarth 	mac_addr = of_get_mac_address(pnp);
268976723bcaSSebastian Hesselbarth 	if (mac_addr)
2690d458cdf7SJoe Perches 		memcpy(ppd.mac_addr, mac_addr, ETH_ALEN);
269176723bcaSSebastian Hesselbarth 
269276723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size);
269376723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr);
269476723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size);
269576723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size);
269676723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr);
269776723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size);
269876723bcaSSebastian Hesselbarth 
269976723bcaSSebastian Hesselbarth 	ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0);
270076723bcaSSebastian Hesselbarth 	if (!ppd.phy_node) {
270176723bcaSSebastian Hesselbarth 		ppd.phy_addr = MV643XX_ETH_PHY_NONE;
270276723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "speed", &ppd.speed);
270376723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "duplex", &ppd.duplex);
270476723bcaSSebastian Hesselbarth 	}
270576723bcaSSebastian Hesselbarth 
2706785bf6f7SJonas Gorski 	ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num);
270776723bcaSSebastian Hesselbarth 	if (!ppdev)
270876723bcaSSebastian Hesselbarth 		return -ENOMEM;
270976723bcaSSebastian Hesselbarth 	ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2710b5d82db8SSebastian Hesselbarth 	ppdev->dev.of_node = pnp;
271176723bcaSSebastian Hesselbarth 
271276723bcaSSebastian Hesselbarth 	ret = platform_device_add_resources(ppdev, &res, 1);
271376723bcaSSebastian Hesselbarth 	if (ret)
271476723bcaSSebastian Hesselbarth 		goto port_err;
271576723bcaSSebastian Hesselbarth 
271676723bcaSSebastian Hesselbarth 	ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
271776723bcaSSebastian Hesselbarth 	if (ret)
271876723bcaSSebastian Hesselbarth 		goto port_err;
271976723bcaSSebastian Hesselbarth 
272076723bcaSSebastian Hesselbarth 	ret = platform_device_add(ppdev);
272176723bcaSSebastian Hesselbarth 	if (ret)
272276723bcaSSebastian Hesselbarth 		goto port_err;
272376723bcaSSebastian Hesselbarth 
2724785bf6f7SJonas Gorski 	port_platdev[dev_num] = ppdev;
272576723bcaSSebastian Hesselbarth 
272676723bcaSSebastian Hesselbarth 	return 0;
272776723bcaSSebastian Hesselbarth 
272876723bcaSSebastian Hesselbarth port_err:
272976723bcaSSebastian Hesselbarth 	platform_device_put(ppdev);
273076723bcaSSebastian Hesselbarth 	return ret;
273176723bcaSSebastian Hesselbarth }
273276723bcaSSebastian Hesselbarth 
273376723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
273476723bcaSSebastian Hesselbarth {
273576723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
273676723bcaSSebastian Hesselbarth 	struct device_node *pnp, *np = pdev->dev.of_node;
273776723bcaSSebastian Hesselbarth 	int ret;
273876723bcaSSebastian Hesselbarth 
273976723bcaSSebastian Hesselbarth 	/* bail out if not registered from DT */
274076723bcaSSebastian Hesselbarth 	if (!np)
274176723bcaSSebastian Hesselbarth 		return 0;
274276723bcaSSebastian Hesselbarth 
274376723bcaSSebastian Hesselbarth 	pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
274476723bcaSSebastian Hesselbarth 	if (!pd)
274576723bcaSSebastian Hesselbarth 		return -ENOMEM;
274676723bcaSSebastian Hesselbarth 	pdev->dev.platform_data = pd;
274776723bcaSSebastian Hesselbarth 
274876723bcaSSebastian Hesselbarth 	mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
274976723bcaSSebastian Hesselbarth 
275076723bcaSSebastian Hesselbarth 	for_each_available_child_of_node(np, pnp) {
275176723bcaSSebastian Hesselbarth 		ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
275276723bcaSSebastian Hesselbarth 		if (ret)
275376723bcaSSebastian Hesselbarth 			return ret;
275476723bcaSSebastian Hesselbarth 	}
275576723bcaSSebastian Hesselbarth 	return 0;
275676723bcaSSebastian Hesselbarth }
275776723bcaSSebastian Hesselbarth 
275876723bcaSSebastian Hesselbarth static void mv643xx_eth_shared_of_remove(void)
275976723bcaSSebastian Hesselbarth {
276076723bcaSSebastian Hesselbarth 	int n;
276176723bcaSSebastian Hesselbarth 
276276723bcaSSebastian Hesselbarth 	for (n = 0; n < 3; n++) {
276376723bcaSSebastian Hesselbarth 		platform_device_del(port_platdev[n]);
276476723bcaSSebastian Hesselbarth 		port_platdev[n] = NULL;
276576723bcaSSebastian Hesselbarth 	}
276676723bcaSSebastian Hesselbarth }
276776723bcaSSebastian Hesselbarth #else
2768ff20877aSArnd Bergmann static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
276976723bcaSSebastian Hesselbarth {
2770ff20877aSArnd Bergmann 	return 0;
277176723bcaSSebastian Hesselbarth }
277276723bcaSSebastian Hesselbarth 
2773ff20877aSArnd Bergmann static inline void mv643xx_eth_shared_of_remove(void)
2774ff20877aSArnd Bergmann {
2775ff20877aSArnd Bergmann }
277676723bcaSSebastian Hesselbarth #endif
277776723bcaSSebastian Hesselbarth 
2778527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2779527a6266SJeff Kirsher {
2780527a6266SJeff Kirsher 	static int mv643xx_eth_version_printed;
278176723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
2782527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp;
278363a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
2784527a6266SJeff Kirsher 	struct resource *res;
278576723bcaSSebastian Hesselbarth 	int ret;
2786527a6266SJeff Kirsher 
2787527a6266SJeff Kirsher 	if (!mv643xx_eth_version_printed++)
2788527a6266SJeff Kirsher 		pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2789527a6266SJeff Kirsher 			  mv643xx_eth_driver_version);
2790527a6266SJeff Kirsher 
2791527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2792527a6266SJeff Kirsher 	if (res == NULL)
2793727f957aSSebastian Hesselbarth 		return -EINVAL;
2794527a6266SJeff Kirsher 
2795727f957aSSebastian Hesselbarth 	msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
2796527a6266SJeff Kirsher 	if (msp == NULL)
2797727f957aSSebastian Hesselbarth 		return -ENOMEM;
279876723bcaSSebastian Hesselbarth 	platform_set_drvdata(pdev, msp);
2799527a6266SJeff Kirsher 
280065a6f969SSebastian Hesselbarth 	msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
2801527a6266SJeff Kirsher 	if (msp->base == NULL)
2802727f957aSSebastian Hesselbarth 		return -ENOMEM;
2803527a6266SJeff Kirsher 
280420922486SSebastian Hesselbarth 	msp->clk = devm_clk_get(&pdev->dev, NULL);
280520922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
280620922486SSebastian Hesselbarth 		clk_prepare_enable(msp->clk);
280720922486SSebastian Hesselbarth 
2808527a6266SJeff Kirsher 	/*
2809527a6266SJeff Kirsher 	 * (Re-)program MBUS remapping windows if we are asked to.
2810527a6266SJeff Kirsher 	 */
281163a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
281263a9332bSAndrew Lunn 	if (dram)
281363a9332bSAndrew Lunn 		mv643xx_eth_conf_mbus_windows(msp, dram);
2814527a6266SJeff Kirsher 
281576723bcaSSebastian Hesselbarth 	ret = mv643xx_eth_shared_of_probe(pdev);
281676723bcaSSebastian Hesselbarth 	if (ret)
281776723bcaSSebastian Hesselbarth 		return ret;
2818bbfa6d0aSJingoo Han 	pd = dev_get_platdata(&pdev->dev);
281976723bcaSSebastian Hesselbarth 
2820527a6266SJeff Kirsher 	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2821527a6266SJeff Kirsher 					pd->tx_csum_limit : 9 * 1024;
2822527a6266SJeff Kirsher 	infer_hw_params(msp);
2823527a6266SJeff Kirsher 
2824527a6266SJeff Kirsher 	return 0;
2825527a6266SJeff Kirsher }
2826527a6266SJeff Kirsher 
2827527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2828527a6266SJeff Kirsher {
2829527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
2830527a6266SJeff Kirsher 
283176723bcaSSebastian Hesselbarth 	mv643xx_eth_shared_of_remove();
283220922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
283320922486SSebastian Hesselbarth 		clk_disable_unprepare(msp->clk);
2834527a6266SJeff Kirsher 	return 0;
2835527a6266SJeff Kirsher }
2836527a6266SJeff Kirsher 
2837527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = {
2838527a6266SJeff Kirsher 	.probe		= mv643xx_eth_shared_probe,
2839527a6266SJeff Kirsher 	.remove		= mv643xx_eth_shared_remove,
2840527a6266SJeff Kirsher 	.driver = {
2841527a6266SJeff Kirsher 		.name	= MV643XX_ETH_SHARED_NAME,
2842527a6266SJeff Kirsher 		.owner	= THIS_MODULE,
284376723bcaSSebastian Hesselbarth 		.of_match_table = of_match_ptr(mv643xx_eth_shared_ids),
2844527a6266SJeff Kirsher 	},
2845527a6266SJeff Kirsher };
2846527a6266SJeff Kirsher 
2847527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
2848527a6266SJeff Kirsher {
2849527a6266SJeff Kirsher 	int addr_shift = 5 * mp->port_num;
2850527a6266SJeff Kirsher 	u32 data;
2851527a6266SJeff Kirsher 
2852527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2853527a6266SJeff Kirsher 	data &= ~(0x1f << addr_shift);
2854527a6266SJeff Kirsher 	data |= (phy_addr & 0x1f) << addr_shift;
2855527a6266SJeff Kirsher 	wrl(mp, PHY_ADDR, data);
2856527a6266SJeff Kirsher }
2857527a6266SJeff Kirsher 
2858527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp)
2859527a6266SJeff Kirsher {
2860527a6266SJeff Kirsher 	unsigned int data;
2861527a6266SJeff Kirsher 
2862527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2863527a6266SJeff Kirsher 
2864527a6266SJeff Kirsher 	return (data >> (5 * mp->port_num)) & 0x1f;
2865527a6266SJeff Kirsher }
2866527a6266SJeff Kirsher 
2867527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp,
2868527a6266SJeff Kirsher 		       struct mv643xx_eth_platform_data *pd)
2869527a6266SJeff Kirsher {
2870527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2871ee9e4956SEzequiel Garcia 	unsigned int tx_ring_size;
2872527a6266SJeff Kirsher 
2873527a6266SJeff Kirsher 	if (is_valid_ether_addr(pd->mac_addr))
2874d458cdf7SJoe Perches 		memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN);
2875527a6266SJeff Kirsher 	else
2876527a6266SJeff Kirsher 		uc_addr_get(mp, dev->dev_addr);
2877527a6266SJeff Kirsher 
2878527a6266SJeff Kirsher 	mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2879527a6266SJeff Kirsher 	if (pd->rx_queue_size)
2880527a6266SJeff Kirsher 		mp->rx_ring_size = pd->rx_queue_size;
2881527a6266SJeff Kirsher 	mp->rx_desc_sram_addr = pd->rx_sram_addr;
2882527a6266SJeff Kirsher 	mp->rx_desc_sram_size = pd->rx_sram_size;
2883527a6266SJeff Kirsher 
2884527a6266SJeff Kirsher 	mp->rxq_count = pd->rx_queue_count ? : 1;
2885527a6266SJeff Kirsher 
2886ee9e4956SEzequiel Garcia 	tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2887527a6266SJeff Kirsher 	if (pd->tx_queue_size)
2888ee9e4956SEzequiel Garcia 		tx_ring_size = pd->tx_queue_size;
2889ee9e4956SEzequiel Garcia 
2890ee9e4956SEzequiel Garcia 	mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size,
2891ee9e4956SEzequiel Garcia 				   MV643XX_MAX_SKB_DESCS * 2, 4096);
2892ee9e4956SEzequiel Garcia 	if (mp->tx_ring_size != tx_ring_size)
2893ee9e4956SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
2894ee9e4956SEzequiel Garcia 			    mp->tx_ring_size, tx_ring_size);
2895ee9e4956SEzequiel Garcia 
2896527a6266SJeff Kirsher 	mp->tx_desc_sram_addr = pd->tx_sram_addr;
2897527a6266SJeff Kirsher 	mp->tx_desc_sram_size = pd->tx_sram_size;
2898527a6266SJeff Kirsher 
2899527a6266SJeff Kirsher 	mp->txq_count = pd->tx_queue_count ? : 1;
2900527a6266SJeff Kirsher }
2901527a6266SJeff Kirsher 
2902527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2903527a6266SJeff Kirsher 				   int phy_addr)
2904527a6266SJeff Kirsher {
2905527a6266SJeff Kirsher 	struct phy_device *phydev;
2906527a6266SJeff Kirsher 	int start;
2907527a6266SJeff Kirsher 	int num;
2908527a6266SJeff Kirsher 	int i;
2909c3a07134SFlorian Fainelli 	char phy_id[MII_BUS_ID_SIZE + 3];
2910527a6266SJeff Kirsher 
2911527a6266SJeff Kirsher 	if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2912527a6266SJeff Kirsher 		start = phy_addr_get(mp) & 0x1f;
2913527a6266SJeff Kirsher 		num = 32;
2914527a6266SJeff Kirsher 	} else {
2915527a6266SJeff Kirsher 		start = phy_addr & 0x1f;
2916527a6266SJeff Kirsher 		num = 1;
2917527a6266SJeff Kirsher 	}
2918527a6266SJeff Kirsher 
2919c3a07134SFlorian Fainelli 	/* Attempt to connect to the PHY using orion-mdio */
2920976c90b9SSimon Baatz 	phydev = ERR_PTR(-ENODEV);
2921527a6266SJeff Kirsher 	for (i = 0; i < num; i++) {
2922527a6266SJeff Kirsher 		int addr = (start + i) & 0x1f;
2923527a6266SJeff Kirsher 
2924c3a07134SFlorian Fainelli 		snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
2925c3a07134SFlorian Fainelli 				"orion-mdio-mii", addr);
2926527a6266SJeff Kirsher 
2927c3a07134SFlorian Fainelli 		phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
2928c3a07134SFlorian Fainelli 				PHY_INTERFACE_MODE_GMII);
2929c3a07134SFlorian Fainelli 		if (!IS_ERR(phydev)) {
2930527a6266SJeff Kirsher 			phy_addr_set(mp, addr);
2931c3a07134SFlorian Fainelli 			break;
2932527a6266SJeff Kirsher 		}
2933527a6266SJeff Kirsher 	}
2934527a6266SJeff Kirsher 
2935527a6266SJeff Kirsher 	return phydev;
2936527a6266SJeff Kirsher }
2937527a6266SJeff Kirsher 
2938527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
2939527a6266SJeff Kirsher {
2940527a6266SJeff Kirsher 	struct phy_device *phy = mp->phy;
2941527a6266SJeff Kirsher 
2942527a6266SJeff Kirsher 	if (speed == 0) {
2943527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_ENABLE;
2944527a6266SJeff Kirsher 		phy->speed = 0;
2945527a6266SJeff Kirsher 		phy->duplex = 0;
2946527a6266SJeff Kirsher 		phy->advertising = phy->supported | ADVERTISED_Autoneg;
2947527a6266SJeff Kirsher 	} else {
2948527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_DISABLE;
2949527a6266SJeff Kirsher 		phy->advertising = 0;
2950527a6266SJeff Kirsher 		phy->speed = speed;
2951527a6266SJeff Kirsher 		phy->duplex = duplex;
2952527a6266SJeff Kirsher 	}
2953527a6266SJeff Kirsher 	phy_start_aneg(phy);
2954527a6266SJeff Kirsher }
2955527a6266SJeff Kirsher 
2956527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2957527a6266SJeff Kirsher {
2958527a6266SJeff Kirsher 	u32 pscr;
2959527a6266SJeff Kirsher 
2960527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2961527a6266SJeff Kirsher 	if (pscr & SERIAL_PORT_ENABLE) {
2962527a6266SJeff Kirsher 		pscr &= ~SERIAL_PORT_ENABLE;
2963527a6266SJeff Kirsher 		wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2964527a6266SJeff Kirsher 	}
2965527a6266SJeff Kirsher 
2966527a6266SJeff Kirsher 	pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
2967527a6266SJeff Kirsher 	if (mp->phy == NULL) {
2968527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2969527a6266SJeff Kirsher 		if (speed == SPEED_1000)
2970527a6266SJeff Kirsher 			pscr |= SET_GMII_SPEED_TO_1000;
2971527a6266SJeff Kirsher 		else if (speed == SPEED_100)
2972527a6266SJeff Kirsher 			pscr |= SET_MII_SPEED_TO_100;
2973527a6266SJeff Kirsher 
2974527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2975527a6266SJeff Kirsher 
2976527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2977527a6266SJeff Kirsher 		if (duplex == DUPLEX_FULL)
2978527a6266SJeff Kirsher 			pscr |= SET_FULL_DUPLEX_MODE;
2979527a6266SJeff Kirsher 	}
2980527a6266SJeff Kirsher 
2981527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2982527a6266SJeff Kirsher }
2983527a6266SJeff Kirsher 
2984527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = {
2985527a6266SJeff Kirsher 	.ndo_open		= mv643xx_eth_open,
2986527a6266SJeff Kirsher 	.ndo_stop		= mv643xx_eth_stop,
2987527a6266SJeff Kirsher 	.ndo_start_xmit		= mv643xx_eth_xmit,
2988527a6266SJeff Kirsher 	.ndo_set_rx_mode	= mv643xx_eth_set_rx_mode,
2989527a6266SJeff Kirsher 	.ndo_set_mac_address	= mv643xx_eth_set_mac_address,
2990527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2991527a6266SJeff Kirsher 	.ndo_do_ioctl		= mv643xx_eth_ioctl,
2992527a6266SJeff Kirsher 	.ndo_change_mtu		= mv643xx_eth_change_mtu,
2993527a6266SJeff Kirsher 	.ndo_set_features	= mv643xx_eth_set_features,
2994527a6266SJeff Kirsher 	.ndo_tx_timeout		= mv643xx_eth_tx_timeout,
2995527a6266SJeff Kirsher 	.ndo_get_stats		= mv643xx_eth_get_stats,
2996527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2997527a6266SJeff Kirsher 	.ndo_poll_controller	= mv643xx_eth_netpoll,
2998527a6266SJeff Kirsher #endif
2999527a6266SJeff Kirsher };
3000527a6266SJeff Kirsher 
3001527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev)
3002527a6266SJeff Kirsher {
3003527a6266SJeff Kirsher 	struct mv643xx_eth_platform_data *pd;
3004527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
3005527a6266SJeff Kirsher 	struct net_device *dev;
3006527a6266SJeff Kirsher 	struct resource *res;
3007527a6266SJeff Kirsher 	int err;
3008527a6266SJeff Kirsher 
3009bbfa6d0aSJingoo Han 	pd = dev_get_platdata(&pdev->dev);
3010527a6266SJeff Kirsher 	if (pd == NULL) {
3011527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
3012527a6266SJeff Kirsher 		return -ENODEV;
3013527a6266SJeff Kirsher 	}
3014527a6266SJeff Kirsher 
3015527a6266SJeff Kirsher 	if (pd->shared == NULL) {
3016527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
3017527a6266SJeff Kirsher 		return -ENODEV;
3018527a6266SJeff Kirsher 	}
3019527a6266SJeff Kirsher 
3020527a6266SJeff Kirsher 	dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
3021527a6266SJeff Kirsher 	if (!dev)
3022527a6266SJeff Kirsher 		return -ENOMEM;
3023527a6266SJeff Kirsher 
3024527a6266SJeff Kirsher 	mp = netdev_priv(dev);
3025527a6266SJeff Kirsher 	platform_set_drvdata(pdev, mp);
3026527a6266SJeff Kirsher 
3027527a6266SJeff Kirsher 	mp->shared = platform_get_drvdata(pd->shared);
3028527a6266SJeff Kirsher 	mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
3029527a6266SJeff Kirsher 	mp->port_num = pd->port_number;
3030527a6266SJeff Kirsher 
3031527a6266SJeff Kirsher 	mp->dev = dev;
3032527a6266SJeff Kirsher 
3033cb85215fSSebastian Hesselbarth 	/* Kirkwood resets some registers on gated clocks. Especially
3034cb85215fSSebastian Hesselbarth 	 * CLK125_BYPASS_EN must be cleared but is not available on
3035cb85215fSSebastian Hesselbarth 	 * all other SoCs/System Controllers using this driver.
3036cb85215fSSebastian Hesselbarth 	 */
3037cb85215fSSebastian Hesselbarth 	if (of_device_is_compatible(pdev->dev.of_node,
3038cb85215fSSebastian Hesselbarth 				    "marvell,kirkwood-eth-port"))
3039cb85215fSSebastian Hesselbarth 		wrlp(mp, PORT_SERIAL_CONTROL1,
3040cb85215fSSebastian Hesselbarth 		     rdlp(mp, PORT_SERIAL_CONTROL1) & ~CLK125_BYPASS_EN);
3041cb85215fSSebastian Hesselbarth 
3042452503ebSAndrew Lunn 	/*
30439a43a026SAndrew Lunn 	 * Start with a default rate, and if there is a clock, allow
30449a43a026SAndrew Lunn 	 * it to override the default.
3045452503ebSAndrew Lunn 	 */
30469a43a026SAndrew Lunn 	mp->t_clk = 133000000;
304720922486SSebastian Hesselbarth 	mp->clk = devm_clk_get(&pdev->dev, NULL);
3048452503ebSAndrew Lunn 	if (!IS_ERR(mp->clk)) {
3049452503ebSAndrew Lunn 		clk_prepare_enable(mp->clk);
3050452503ebSAndrew Lunn 		mp->t_clk = clk_get_rate(mp->clk);
305176723bcaSSebastian Hesselbarth 	} else if (!IS_ERR(mp->shared->clk)) {
305276723bcaSSebastian Hesselbarth 		mp->t_clk = clk_get_rate(mp->shared->clk);
3053452503ebSAndrew Lunn 	}
305420922486SSebastian Hesselbarth 
3055527a6266SJeff Kirsher 	set_params(mp, pd);
3056527a6266SJeff Kirsher 	netif_set_real_num_tx_queues(dev, mp->txq_count);
3057527a6266SJeff Kirsher 	netif_set_real_num_rx_queues(dev, mp->rxq_count);
3058527a6266SJeff Kirsher 
3059cc9d4598SSebastian Hesselbarth 	err = 0;
3060cc9d4598SSebastian Hesselbarth 	if (pd->phy_node) {
3061cc9d4598SSebastian Hesselbarth 		mp->phy = of_phy_connect(mp->dev, pd->phy_node,
3062cc9d4598SSebastian Hesselbarth 					 mv643xx_eth_adjust_link, 0,
3063cc9d4598SSebastian Hesselbarth 					 PHY_INTERFACE_MODE_GMII);
3064cc9d4598SSebastian Hesselbarth 		if (!mp->phy)
3065cc9d4598SSebastian Hesselbarth 			err = -ENODEV;
30666115c11fSDan Carpenter 		else
30671cce16d3SJason Gunthorpe 			phy_addr_set(mp, mp->phy->addr);
3068cc9d4598SSebastian Hesselbarth 	} else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
3069527a6266SJeff Kirsher 		mp->phy = phy_scan(mp, pd->phy_addr);
3070527a6266SJeff Kirsher 
3071cc9d4598SSebastian Hesselbarth 		if (IS_ERR(mp->phy))
3072976c90b9SSimon Baatz 			err = PTR_ERR(mp->phy);
3073cc9d4598SSebastian Hesselbarth 		else
3074cc9d4598SSebastian Hesselbarth 			phy_init(mp, pd->speed, pd->duplex);
3075cc9d4598SSebastian Hesselbarth 	}
3076cc9d4598SSebastian Hesselbarth 	if (err == -ENODEV) {
3077976c90b9SSimon Baatz 		err = -EPROBE_DEFER;
3078976c90b9SSimon Baatz 		goto out;
3079976c90b9SSimon Baatz 	}
3080cc9d4598SSebastian Hesselbarth 	if (err)
3081cc9d4598SSebastian Hesselbarth 		goto out;
3082527a6266SJeff Kirsher 
30837ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &mv643xx_eth_ethtool_ops;
3084527a6266SJeff Kirsher 
3085527a6266SJeff Kirsher 	init_pscr(mp, pd->speed, pd->duplex);
3086527a6266SJeff Kirsher 
3087527a6266SJeff Kirsher 
3088527a6266SJeff Kirsher 	mib_counters_clear(mp);
3089527a6266SJeff Kirsher 
3090527a6266SJeff Kirsher 	init_timer(&mp->mib_counters_timer);
3091527a6266SJeff Kirsher 	mp->mib_counters_timer.data = (unsigned long)mp;
3092527a6266SJeff Kirsher 	mp->mib_counters_timer.function = mib_counters_timer_wrapper;
3093527a6266SJeff Kirsher 	mp->mib_counters_timer.expires = jiffies + 30 * HZ;
3094527a6266SJeff Kirsher 
3095527a6266SJeff Kirsher 	spin_lock_init(&mp->mib_counters_lock);
3096527a6266SJeff Kirsher 
3097527a6266SJeff Kirsher 	INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
3098527a6266SJeff Kirsher 
3099a3659aa0SAndrew Lunn 	netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, NAPI_POLL_WEIGHT);
3100527a6266SJeff Kirsher 
3101527a6266SJeff Kirsher 	init_timer(&mp->rx_oom);
3102527a6266SJeff Kirsher 	mp->rx_oom.data = (unsigned long)mp;
3103527a6266SJeff Kirsher 	mp->rx_oom.function = oom_timer_wrapper;
3104527a6266SJeff Kirsher 
3105527a6266SJeff Kirsher 
3106527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
3107527a6266SJeff Kirsher 	BUG_ON(!res);
3108527a6266SJeff Kirsher 	dev->irq = res->start;
3109527a6266SJeff Kirsher 
3110527a6266SJeff Kirsher 	dev->netdev_ops = &mv643xx_eth_netdev_ops;
3111527a6266SJeff Kirsher 
3112527a6266SJeff Kirsher 	dev->watchdog_timeo = 2 * HZ;
3113527a6266SJeff Kirsher 	dev->base_addr = 0;
3114527a6266SJeff Kirsher 
31153ae8f4e0SEzequiel Garcia 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
31164d48d589SEzequiel Garcia 	dev->vlan_features = dev->features;
31174d48d589SEzequiel Garcia 
31184d48d589SEzequiel Garcia 	dev->features |= NETIF_F_RXCSUM;
31194d48d589SEzequiel Garcia 	dev->hw_features = dev->features;
3120527a6266SJeff Kirsher 
312101789349SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT;
3122ee9e4956SEzequiel Garcia 	dev->gso_max_segs = MV643XX_MAX_TSO_SEGS;
312301789349SJiri Pirko 
3124527a6266SJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
3125527a6266SJeff Kirsher 
3126527a6266SJeff Kirsher 	if (mp->shared->win_protect)
3127527a6266SJeff Kirsher 		wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
3128527a6266SJeff Kirsher 
3129527a6266SJeff Kirsher 	netif_carrier_off(dev);
3130527a6266SJeff Kirsher 
3131527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
3132527a6266SJeff Kirsher 
3133527a6266SJeff Kirsher 	set_rx_coal(mp, 250);
3134527a6266SJeff Kirsher 	set_tx_coal(mp, 0);
3135527a6266SJeff Kirsher 
3136527a6266SJeff Kirsher 	err = register_netdev(dev);
3137527a6266SJeff Kirsher 	if (err)
3138527a6266SJeff Kirsher 		goto out;
3139527a6266SJeff Kirsher 
3140527a6266SJeff Kirsher 	netdev_notice(dev, "port %d with MAC address %pM\n",
3141527a6266SJeff Kirsher 		      mp->port_num, dev->dev_addr);
3142527a6266SJeff Kirsher 
3143527a6266SJeff Kirsher 	if (mp->tx_desc_sram_size > 0)
3144527a6266SJeff Kirsher 		netdev_notice(dev, "configured with sram\n");
3145527a6266SJeff Kirsher 
3146527a6266SJeff Kirsher 	return 0;
3147527a6266SJeff Kirsher 
3148527a6266SJeff Kirsher out:
314920922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
3150baffab28SSimon Baatz 		clk_disable_unprepare(mp->clk);
3151527a6266SJeff Kirsher 	free_netdev(dev);
3152527a6266SJeff Kirsher 
3153527a6266SJeff Kirsher 	return err;
3154527a6266SJeff Kirsher }
3155527a6266SJeff Kirsher 
3156527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev)
3157527a6266SJeff Kirsher {
3158527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
3159527a6266SJeff Kirsher 
3160527a6266SJeff Kirsher 	unregister_netdev(mp->dev);
3161527a6266SJeff Kirsher 	if (mp->phy != NULL)
3162cec753f5SSebastian Hesselbarth 		phy_disconnect(mp->phy);
3163527a6266SJeff Kirsher 	cancel_work_sync(&mp->tx_timeout_task);
3164452503ebSAndrew Lunn 
316520922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
3166452503ebSAndrew Lunn 		clk_disable_unprepare(mp->clk);
31679a43a026SAndrew Lunn 
3168527a6266SJeff Kirsher 	free_netdev(mp->dev);
3169527a6266SJeff Kirsher 
3170527a6266SJeff Kirsher 	return 0;
3171527a6266SJeff Kirsher }
3172527a6266SJeff Kirsher 
3173527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev)
3174527a6266SJeff Kirsher {
3175527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
3176527a6266SJeff Kirsher 
3177527a6266SJeff Kirsher 	/* Mask all interrupts on ethernet port */
3178527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
3179527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
3180527a6266SJeff Kirsher 
3181527a6266SJeff Kirsher 	if (netif_running(mp->dev))
3182527a6266SJeff Kirsher 		port_reset(mp);
3183527a6266SJeff Kirsher }
3184527a6266SJeff Kirsher 
3185527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = {
3186527a6266SJeff Kirsher 	.probe		= mv643xx_eth_probe,
3187527a6266SJeff Kirsher 	.remove		= mv643xx_eth_remove,
3188527a6266SJeff Kirsher 	.shutdown	= mv643xx_eth_shutdown,
3189527a6266SJeff Kirsher 	.driver = {
3190527a6266SJeff Kirsher 		.name	= MV643XX_ETH_NAME,
3191527a6266SJeff Kirsher 		.owner	= THIS_MODULE,
3192527a6266SJeff Kirsher 	},
3193527a6266SJeff Kirsher };
3194527a6266SJeff Kirsher 
3195527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void)
3196527a6266SJeff Kirsher {
3197527a6266SJeff Kirsher 	int rc;
3198527a6266SJeff Kirsher 
3199527a6266SJeff Kirsher 	rc = platform_driver_register(&mv643xx_eth_shared_driver);
3200527a6266SJeff Kirsher 	if (!rc) {
3201527a6266SJeff Kirsher 		rc = platform_driver_register(&mv643xx_eth_driver);
3202527a6266SJeff Kirsher 		if (rc)
3203527a6266SJeff Kirsher 			platform_driver_unregister(&mv643xx_eth_shared_driver);
3204527a6266SJeff Kirsher 	}
3205527a6266SJeff Kirsher 
3206527a6266SJeff Kirsher 	return rc;
3207527a6266SJeff Kirsher }
3208527a6266SJeff Kirsher module_init(mv643xx_eth_init_module);
3209527a6266SJeff Kirsher 
3210527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void)
3211527a6266SJeff Kirsher {
3212527a6266SJeff Kirsher 	platform_driver_unregister(&mv643xx_eth_driver);
3213527a6266SJeff Kirsher 	platform_driver_unregister(&mv643xx_eth_shared_driver);
3214527a6266SJeff Kirsher }
3215527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module);
3216527a6266SJeff Kirsher 
3217527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
3218527a6266SJeff Kirsher 	      "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
3219527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
3220527a6266SJeff Kirsher MODULE_LICENSE("GPL");
3221527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
3222527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3223