xref: /openbmc/linux/drivers/net/ethernet/marvell/mv643xx_eth.c (revision baffab28b13120694fa3ebab08d3e99667a851d2)
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  *
23527a6266SJeff Kirsher  * This program is free software; you can redistribute it and/or
24527a6266SJeff Kirsher  * modify it under the terms of the GNU General Public License
25527a6266SJeff Kirsher  * as published by the Free Software Foundation; either version 2
26527a6266SJeff Kirsher  * of the License, or (at your option) any later version.
27527a6266SJeff Kirsher  *
28527a6266SJeff Kirsher  * This program is distributed in the hope that it will be useful,
29527a6266SJeff Kirsher  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30527a6266SJeff Kirsher  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31527a6266SJeff Kirsher  * GNU General Public License for more details.
32527a6266SJeff Kirsher  *
33527a6266SJeff Kirsher  * You should have received a copy of the GNU General Public License
34527a6266SJeff Kirsher  * along with this program; if not, write to the Free Software
35527a6266SJeff Kirsher  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
36527a6266SJeff Kirsher  */
37527a6266SJeff Kirsher 
38527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
39527a6266SJeff Kirsher 
40527a6266SJeff Kirsher #include <linux/init.h>
41527a6266SJeff Kirsher #include <linux/dma-mapping.h>
42527a6266SJeff Kirsher #include <linux/in.h>
43527a6266SJeff Kirsher #include <linux/ip.h>
44527a6266SJeff Kirsher #include <linux/tcp.h>
45527a6266SJeff Kirsher #include <linux/udp.h>
46527a6266SJeff Kirsher #include <linux/etherdevice.h>
47527a6266SJeff Kirsher #include <linux/delay.h>
48527a6266SJeff Kirsher #include <linux/ethtool.h>
49527a6266SJeff Kirsher #include <linux/platform_device.h>
50527a6266SJeff Kirsher #include <linux/module.h>
51527a6266SJeff Kirsher #include <linux/kernel.h>
52527a6266SJeff Kirsher #include <linux/spinlock.h>
53527a6266SJeff Kirsher #include <linux/workqueue.h>
54527a6266SJeff Kirsher #include <linux/phy.h>
55527a6266SJeff Kirsher #include <linux/mv643xx_eth.h>
56527a6266SJeff Kirsher #include <linux/io.h>
57527a6266SJeff Kirsher #include <linux/types.h>
58527a6266SJeff Kirsher #include <linux/inet_lro.h>
59527a6266SJeff Kirsher #include <linux/slab.h>
60452503ebSAndrew Lunn #include <linux/clk.h>
61527a6266SJeff Kirsher 
62527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth";
63527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4";
64527a6266SJeff Kirsher 
65527a6266SJeff Kirsher 
66527a6266SJeff Kirsher /*
67527a6266SJeff Kirsher  * Registers shared between all ports.
68527a6266SJeff Kirsher  */
69527a6266SJeff Kirsher #define PHY_ADDR			0x0000
70527a6266SJeff Kirsher #define SMI_REG				0x0004
71527a6266SJeff Kirsher #define  SMI_BUSY			0x10000000
72527a6266SJeff Kirsher #define  SMI_READ_VALID			0x08000000
73527a6266SJeff Kirsher #define  SMI_OPCODE_READ		0x04000000
74527a6266SJeff Kirsher #define  SMI_OPCODE_WRITE		0x00000000
75527a6266SJeff Kirsher #define ERR_INT_CAUSE			0x0080
76527a6266SJeff Kirsher #define  ERR_INT_SMI_DONE		0x00000010
77527a6266SJeff Kirsher #define ERR_INT_MASK			0x0084
78527a6266SJeff Kirsher #define WINDOW_BASE(w)			(0x0200 + ((w) << 3))
79527a6266SJeff Kirsher #define WINDOW_SIZE(w)			(0x0204 + ((w) << 3))
80527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w)		(0x0280 + ((w) << 2))
81527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE		0x0290
82527a6266SJeff Kirsher #define WINDOW_PROTECT(w)		(0x0294 + ((w) << 4))
83527a6266SJeff Kirsher 
84527a6266SJeff Kirsher /*
85527a6266SJeff Kirsher  * Main per-port registers.  These live at offset 0x0400 for
86527a6266SJeff Kirsher  * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
87527a6266SJeff Kirsher  */
88527a6266SJeff Kirsher #define PORT_CONFIG			0x0000
89527a6266SJeff Kirsher #define  UNICAST_PROMISCUOUS_MODE	0x00000001
90527a6266SJeff Kirsher #define PORT_CONFIG_EXT			0x0004
91527a6266SJeff Kirsher #define MAC_ADDR_LOW			0x0014
92527a6266SJeff Kirsher #define MAC_ADDR_HIGH			0x0018
93527a6266SJeff Kirsher #define SDMA_CONFIG			0x001c
94527a6266SJeff Kirsher #define  TX_BURST_SIZE_16_64BIT		0x01000000
95527a6266SJeff Kirsher #define  TX_BURST_SIZE_4_64BIT		0x00800000
96527a6266SJeff Kirsher #define  BLM_TX_NO_SWAP			0x00000020
97527a6266SJeff Kirsher #define  BLM_RX_NO_SWAP			0x00000010
98527a6266SJeff Kirsher #define  RX_BURST_SIZE_16_64BIT		0x00000008
99527a6266SJeff Kirsher #define  RX_BURST_SIZE_4_64BIT		0x00000004
100527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL		0x003c
101527a6266SJeff Kirsher #define  SET_MII_SPEED_TO_100		0x01000000
102527a6266SJeff Kirsher #define  SET_GMII_SPEED_TO_1000		0x00800000
103527a6266SJeff Kirsher #define  SET_FULL_DUPLEX_MODE		0x00200000
104527a6266SJeff Kirsher #define  MAX_RX_PACKET_9700BYTE		0x000a0000
105527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_SPEED_GMII	0x00002000
106527a6266SJeff Kirsher #define  DO_NOT_FORCE_LINK_FAIL		0x00000400
107527a6266SJeff Kirsher #define  SERIAL_PORT_CONTROL_RESERVED	0x00000200
108527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_FLOW_CTRL	0x00000008
109527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_DUPLEX	0x00000004
110527a6266SJeff Kirsher #define  FORCE_LINK_PASS		0x00000002
111527a6266SJeff Kirsher #define  SERIAL_PORT_ENABLE		0x00000001
112527a6266SJeff Kirsher #define PORT_STATUS			0x0044
113527a6266SJeff Kirsher #define  TX_FIFO_EMPTY			0x00000400
114527a6266SJeff Kirsher #define  TX_IN_PROGRESS			0x00000080
115527a6266SJeff Kirsher #define  PORT_SPEED_MASK		0x00000030
116527a6266SJeff Kirsher #define  PORT_SPEED_1000		0x00000010
117527a6266SJeff Kirsher #define  PORT_SPEED_100			0x00000020
118527a6266SJeff Kirsher #define  PORT_SPEED_10			0x00000000
119527a6266SJeff Kirsher #define  FLOW_CONTROL_ENABLED		0x00000008
120527a6266SJeff Kirsher #define  FULL_DUPLEX			0x00000004
121527a6266SJeff Kirsher #define  LINK_UP			0x00000002
122527a6266SJeff Kirsher #define TXQ_COMMAND			0x0048
123527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF		0x004c
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
183527a6266SJeff Kirsher #define DEFAULT_TX_QUEUE_SIZE	256
184527a6266SJeff Kirsher #define SKB_DMA_REALIGN		((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
185527a6266SJeff Kirsher 
186527a6266SJeff Kirsher 
187527a6266SJeff Kirsher /*
188527a6266SJeff Kirsher  * RX/TX descriptors.
189527a6266SJeff Kirsher  */
190527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
191527a6266SJeff Kirsher struct rx_desc {
192527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
193527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
194527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
195527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
196527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
197527a6266SJeff Kirsher };
198527a6266SJeff Kirsher 
199527a6266SJeff Kirsher struct tx_desc {
200527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
201527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
202527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
203527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
204527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
205527a6266SJeff Kirsher };
206527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
207527a6266SJeff Kirsher struct rx_desc {
208527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
209527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
210527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
211527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
212527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
213527a6266SJeff Kirsher };
214527a6266SJeff Kirsher 
215527a6266SJeff Kirsher struct tx_desc {
216527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
217527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
218527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
219527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
220527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
221527a6266SJeff Kirsher };
222527a6266SJeff Kirsher #else
223527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
224527a6266SJeff Kirsher #endif
225527a6266SJeff Kirsher 
226527a6266SJeff Kirsher /* RX & TX descriptor command */
227527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA		0x80000000
228527a6266SJeff Kirsher 
229527a6266SJeff Kirsher /* RX & TX descriptor status */
230527a6266SJeff Kirsher #define ERROR_SUMMARY			0x00000001
231527a6266SJeff Kirsher 
232527a6266SJeff Kirsher /* RX descriptor status */
233527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK		0x40000000
234527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT		0x20000000
235527a6266SJeff Kirsher #define RX_FIRST_DESC			0x08000000
236527a6266SJeff Kirsher #define RX_LAST_DESC			0x04000000
237527a6266SJeff Kirsher #define RX_IP_HDR_OK			0x02000000
238527a6266SJeff Kirsher #define RX_PKT_IS_IPV4			0x01000000
239527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2		0x00800000
240527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK		0x00600000
241527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4	0x00000000
242527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED		0x00080000
243527a6266SJeff Kirsher 
244527a6266SJeff Kirsher /* TX descriptor command */
245527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT		0x00800000
246527a6266SJeff Kirsher #define GEN_CRC				0x00400000
247527a6266SJeff Kirsher #define TX_FIRST_DESC			0x00200000
248527a6266SJeff Kirsher #define TX_LAST_DESC			0x00100000
249527a6266SJeff Kirsher #define ZERO_PADDING			0x00080000
250527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM		0x00040000
251527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM		0x00020000
252527a6266SJeff Kirsher #define UDP_FRAME			0x00010000
253527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES		0x00008000
254527a6266SJeff Kirsher #define MAC_HDR_EXTRA_8_BYTES		0x00000200
255527a6266SJeff Kirsher 
256527a6266SJeff Kirsher #define TX_IHL_SHIFT			11
257527a6266SJeff Kirsher 
258527a6266SJeff Kirsher 
259527a6266SJeff Kirsher /* global *******************************************************************/
260527a6266SJeff Kirsher struct mv643xx_eth_shared_private {
261527a6266SJeff Kirsher 	/*
262527a6266SJeff Kirsher 	 * Ethernet controller base address.
263527a6266SJeff Kirsher 	 */
264527a6266SJeff Kirsher 	void __iomem *base;
265527a6266SJeff Kirsher 
266527a6266SJeff Kirsher 	/*
267527a6266SJeff Kirsher 	 * Points at the right SMI instance to use.
268527a6266SJeff Kirsher 	 */
269527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *smi;
270527a6266SJeff Kirsher 
271527a6266SJeff Kirsher 	/*
272527a6266SJeff Kirsher 	 * Provides access to local SMI interface.
273527a6266SJeff Kirsher 	 */
274527a6266SJeff Kirsher 	struct mii_bus *smi_bus;
275527a6266SJeff Kirsher 
276527a6266SJeff Kirsher 	/*
277527a6266SJeff Kirsher 	 * If we have access to the error interrupt pin (which is
278527a6266SJeff Kirsher 	 * somewhat misnamed as it not only reflects internal errors
279527a6266SJeff Kirsher 	 * but also reflects SMI completion), use that to wait for
280527a6266SJeff Kirsher 	 * SMI access completion instead of polling the SMI busy bit.
281527a6266SJeff Kirsher 	 */
282527a6266SJeff Kirsher 	int err_interrupt;
283527a6266SJeff Kirsher 	wait_queue_head_t smi_busy_wait;
284527a6266SJeff Kirsher 
285527a6266SJeff Kirsher 	/*
286527a6266SJeff Kirsher 	 * Per-port MBUS window access register value.
287527a6266SJeff Kirsher 	 */
288527a6266SJeff Kirsher 	u32 win_protect;
289527a6266SJeff Kirsher 
290527a6266SJeff Kirsher 	/*
291527a6266SJeff Kirsher 	 * Hardware-specific parameters.
292527a6266SJeff Kirsher 	 */
293527a6266SJeff Kirsher 	int extended_rx_coal_limit;
294527a6266SJeff Kirsher 	int tx_bw_control;
295527a6266SJeff Kirsher 	int tx_csum_limit;
296452503ebSAndrew Lunn 
297527a6266SJeff Kirsher };
298527a6266SJeff Kirsher 
299527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT		0
300527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT	1
301527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT	2
302527a6266SJeff Kirsher 
303527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev);
304527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev);
305527a6266SJeff Kirsher 
306527a6266SJeff Kirsher 
307527a6266SJeff Kirsher /* per-port *****************************************************************/
308527a6266SJeff Kirsher struct mib_counters {
309527a6266SJeff Kirsher 	u64 good_octets_received;
310527a6266SJeff Kirsher 	u32 bad_octets_received;
311527a6266SJeff Kirsher 	u32 internal_mac_transmit_err;
312527a6266SJeff Kirsher 	u32 good_frames_received;
313527a6266SJeff Kirsher 	u32 bad_frames_received;
314527a6266SJeff Kirsher 	u32 broadcast_frames_received;
315527a6266SJeff Kirsher 	u32 multicast_frames_received;
316527a6266SJeff Kirsher 	u32 frames_64_octets;
317527a6266SJeff Kirsher 	u32 frames_65_to_127_octets;
318527a6266SJeff Kirsher 	u32 frames_128_to_255_octets;
319527a6266SJeff Kirsher 	u32 frames_256_to_511_octets;
320527a6266SJeff Kirsher 	u32 frames_512_to_1023_octets;
321527a6266SJeff Kirsher 	u32 frames_1024_to_max_octets;
322527a6266SJeff Kirsher 	u64 good_octets_sent;
323527a6266SJeff Kirsher 	u32 good_frames_sent;
324527a6266SJeff Kirsher 	u32 excessive_collision;
325527a6266SJeff Kirsher 	u32 multicast_frames_sent;
326527a6266SJeff Kirsher 	u32 broadcast_frames_sent;
327527a6266SJeff Kirsher 	u32 unrec_mac_control_received;
328527a6266SJeff Kirsher 	u32 fc_sent;
329527a6266SJeff Kirsher 	u32 good_fc_received;
330527a6266SJeff Kirsher 	u32 bad_fc_received;
331527a6266SJeff Kirsher 	u32 undersize_received;
332527a6266SJeff Kirsher 	u32 fragments_received;
333527a6266SJeff Kirsher 	u32 oversize_received;
334527a6266SJeff Kirsher 	u32 jabber_received;
335527a6266SJeff Kirsher 	u32 mac_receive_error;
336527a6266SJeff Kirsher 	u32 bad_crc_event;
337527a6266SJeff Kirsher 	u32 collision;
338527a6266SJeff Kirsher 	u32 late_collision;
339302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
340302476c9SPaulius Zaleckas 	u32 rx_discard;
341302476c9SPaulius Zaleckas 	u32 rx_overrun;
342527a6266SJeff Kirsher };
343527a6266SJeff Kirsher 
344527a6266SJeff Kirsher struct lro_counters {
345527a6266SJeff Kirsher 	u32 lro_aggregated;
346527a6266SJeff Kirsher 	u32 lro_flushed;
347527a6266SJeff Kirsher 	u32 lro_no_desc;
348527a6266SJeff Kirsher };
349527a6266SJeff Kirsher 
350527a6266SJeff Kirsher struct rx_queue {
351527a6266SJeff Kirsher 	int index;
352527a6266SJeff Kirsher 
353527a6266SJeff Kirsher 	int rx_ring_size;
354527a6266SJeff Kirsher 
355527a6266SJeff Kirsher 	int rx_desc_count;
356527a6266SJeff Kirsher 	int rx_curr_desc;
357527a6266SJeff Kirsher 	int rx_used_desc;
358527a6266SJeff Kirsher 
359527a6266SJeff Kirsher 	struct rx_desc *rx_desc_area;
360527a6266SJeff Kirsher 	dma_addr_t rx_desc_dma;
361527a6266SJeff Kirsher 	int rx_desc_area_size;
362527a6266SJeff Kirsher 	struct sk_buff **rx_skb;
363527a6266SJeff Kirsher 
364527a6266SJeff Kirsher 	struct net_lro_mgr lro_mgr;
365527a6266SJeff Kirsher 	struct net_lro_desc lro_arr[8];
366527a6266SJeff Kirsher };
367527a6266SJeff Kirsher 
368527a6266SJeff Kirsher struct tx_queue {
369527a6266SJeff Kirsher 	int index;
370527a6266SJeff Kirsher 
371527a6266SJeff Kirsher 	int tx_ring_size;
372527a6266SJeff Kirsher 
373527a6266SJeff Kirsher 	int tx_desc_count;
374527a6266SJeff Kirsher 	int tx_curr_desc;
375527a6266SJeff Kirsher 	int tx_used_desc;
376527a6266SJeff Kirsher 
377527a6266SJeff Kirsher 	struct tx_desc *tx_desc_area;
378527a6266SJeff Kirsher 	dma_addr_t tx_desc_dma;
379527a6266SJeff Kirsher 	int tx_desc_area_size;
380527a6266SJeff Kirsher 
381527a6266SJeff Kirsher 	struct sk_buff_head tx_skb;
382527a6266SJeff Kirsher 
383527a6266SJeff Kirsher 	unsigned long tx_packets;
384527a6266SJeff Kirsher 	unsigned long tx_bytes;
385527a6266SJeff Kirsher 	unsigned long tx_dropped;
386527a6266SJeff Kirsher };
387527a6266SJeff Kirsher 
388527a6266SJeff Kirsher struct mv643xx_eth_private {
389527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *shared;
390527a6266SJeff Kirsher 	void __iomem *base;
391527a6266SJeff Kirsher 	int port_num;
392527a6266SJeff Kirsher 
393527a6266SJeff Kirsher 	struct net_device *dev;
394527a6266SJeff Kirsher 
395527a6266SJeff Kirsher 	struct phy_device *phy;
396527a6266SJeff Kirsher 
397527a6266SJeff Kirsher 	struct timer_list mib_counters_timer;
398527a6266SJeff Kirsher 	spinlock_t mib_counters_lock;
399527a6266SJeff Kirsher 	struct mib_counters mib_counters;
400527a6266SJeff Kirsher 
401527a6266SJeff Kirsher 	struct lro_counters lro_counters;
402527a6266SJeff Kirsher 
403527a6266SJeff Kirsher 	struct work_struct tx_timeout_task;
404527a6266SJeff Kirsher 
405527a6266SJeff Kirsher 	struct napi_struct napi;
406527a6266SJeff Kirsher 	u32 int_mask;
407527a6266SJeff Kirsher 	u8 oom;
408527a6266SJeff Kirsher 	u8 work_link;
409527a6266SJeff Kirsher 	u8 work_tx;
410527a6266SJeff Kirsher 	u8 work_tx_end;
411527a6266SJeff Kirsher 	u8 work_rx;
412527a6266SJeff Kirsher 	u8 work_rx_refill;
413527a6266SJeff Kirsher 
414527a6266SJeff Kirsher 	int skb_size;
415527a6266SJeff Kirsher 	struct sk_buff_head rx_recycle;
416527a6266SJeff Kirsher 
417527a6266SJeff Kirsher 	/*
418527a6266SJeff Kirsher 	 * RX state.
419527a6266SJeff Kirsher 	 */
420527a6266SJeff Kirsher 	int rx_ring_size;
421527a6266SJeff Kirsher 	unsigned long rx_desc_sram_addr;
422527a6266SJeff Kirsher 	int rx_desc_sram_size;
423527a6266SJeff Kirsher 	int rxq_count;
424527a6266SJeff Kirsher 	struct timer_list rx_oom;
425527a6266SJeff Kirsher 	struct rx_queue rxq[8];
426527a6266SJeff Kirsher 
427527a6266SJeff Kirsher 	/*
428527a6266SJeff Kirsher 	 * TX state.
429527a6266SJeff Kirsher 	 */
430527a6266SJeff Kirsher 	int tx_ring_size;
431527a6266SJeff Kirsher 	unsigned long tx_desc_sram_addr;
432527a6266SJeff Kirsher 	int tx_desc_sram_size;
433527a6266SJeff Kirsher 	int txq_count;
434527a6266SJeff Kirsher 	struct tx_queue txq[8];
435452503ebSAndrew Lunn 
436452503ebSAndrew Lunn 	/*
437452503ebSAndrew Lunn 	 * Hardware-specific parameters.
438452503ebSAndrew Lunn 	 */
4399a43a026SAndrew Lunn #if defined(CONFIG_HAVE_CLK)
440452503ebSAndrew Lunn 	struct clk *clk;
4419a43a026SAndrew Lunn #endif
442452503ebSAndrew Lunn 	unsigned int t_clk;
443527a6266SJeff Kirsher };
444527a6266SJeff Kirsher 
445527a6266SJeff Kirsher 
446527a6266SJeff Kirsher /* port register accessors **************************************************/
447527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
448527a6266SJeff Kirsher {
449527a6266SJeff Kirsher 	return readl(mp->shared->base + offset);
450527a6266SJeff Kirsher }
451527a6266SJeff Kirsher 
452527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
453527a6266SJeff Kirsher {
454527a6266SJeff Kirsher 	return readl(mp->base + offset);
455527a6266SJeff Kirsher }
456527a6266SJeff Kirsher 
457527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
458527a6266SJeff Kirsher {
459527a6266SJeff Kirsher 	writel(data, mp->shared->base + offset);
460527a6266SJeff Kirsher }
461527a6266SJeff Kirsher 
462527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
463527a6266SJeff Kirsher {
464527a6266SJeff Kirsher 	writel(data, mp->base + offset);
465527a6266SJeff Kirsher }
466527a6266SJeff Kirsher 
467527a6266SJeff Kirsher 
468527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/
469527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
470527a6266SJeff Kirsher {
471527a6266SJeff Kirsher 	return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
472527a6266SJeff Kirsher }
473527a6266SJeff Kirsher 
474527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
475527a6266SJeff Kirsher {
476527a6266SJeff Kirsher 	return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
477527a6266SJeff Kirsher }
478527a6266SJeff Kirsher 
479527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq)
480527a6266SJeff Kirsher {
481527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
482527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
483527a6266SJeff Kirsher }
484527a6266SJeff Kirsher 
485527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq)
486527a6266SJeff Kirsher {
487527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
488527a6266SJeff Kirsher 	u8 mask = 1 << rxq->index;
489527a6266SJeff Kirsher 
490527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, mask << 8);
491527a6266SJeff Kirsher 	while (rdlp(mp, RXQ_COMMAND) & mask)
492527a6266SJeff Kirsher 		udelay(10);
493527a6266SJeff Kirsher }
494527a6266SJeff Kirsher 
495527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq)
496527a6266SJeff Kirsher {
497527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
498527a6266SJeff Kirsher 	u32 addr;
499527a6266SJeff Kirsher 
500527a6266SJeff Kirsher 	addr = (u32)txq->tx_desc_dma;
501527a6266SJeff Kirsher 	addr += txq->tx_curr_desc * sizeof(struct tx_desc);
502527a6266SJeff Kirsher 	wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
503527a6266SJeff Kirsher }
504527a6266SJeff Kirsher 
505527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq)
506527a6266SJeff Kirsher {
507527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
508527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, 1 << txq->index);
509527a6266SJeff Kirsher }
510527a6266SJeff Kirsher 
511527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq)
512527a6266SJeff Kirsher {
513527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
514527a6266SJeff Kirsher 	u8 mask = 1 << txq->index;
515527a6266SJeff Kirsher 
516527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, mask << 8);
517527a6266SJeff Kirsher 	while (rdlp(mp, TXQ_COMMAND) & mask)
518527a6266SJeff Kirsher 		udelay(10);
519527a6266SJeff Kirsher }
520527a6266SJeff Kirsher 
521527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq)
522527a6266SJeff Kirsher {
523527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
524527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
525527a6266SJeff Kirsher 
526527a6266SJeff Kirsher 	if (netif_tx_queue_stopped(nq)) {
527527a6266SJeff Kirsher 		__netif_tx_lock(nq, smp_processor_id());
528527a6266SJeff Kirsher 		if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
529527a6266SJeff Kirsher 			netif_tx_wake_queue(nq);
530527a6266SJeff Kirsher 		__netif_tx_unlock(nq);
531527a6266SJeff Kirsher 	}
532527a6266SJeff Kirsher }
533527a6266SJeff Kirsher 
534527a6266SJeff Kirsher 
535527a6266SJeff Kirsher /* rx napi ******************************************************************/
536527a6266SJeff Kirsher static int
537527a6266SJeff Kirsher mv643xx_get_skb_header(struct sk_buff *skb, void **iphdr, void **tcph,
538527a6266SJeff Kirsher 		       u64 *hdr_flags, void *priv)
539527a6266SJeff Kirsher {
540527a6266SJeff Kirsher 	unsigned long cmd_sts = (unsigned long)priv;
541527a6266SJeff Kirsher 
542527a6266SJeff Kirsher 	/*
543527a6266SJeff Kirsher 	 * Make sure that this packet is Ethernet II, is not VLAN
544527a6266SJeff Kirsher 	 * tagged, is IPv4, has a valid IP header, and is TCP.
545527a6266SJeff Kirsher 	 */
546527a6266SJeff Kirsher 	if ((cmd_sts & (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
547527a6266SJeff Kirsher 		       RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_MASK |
548527a6266SJeff Kirsher 		       RX_PKT_IS_VLAN_TAGGED)) !=
549527a6266SJeff Kirsher 	    (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
550527a6266SJeff Kirsher 	     RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_TCP_IPV4))
551527a6266SJeff Kirsher 		return -1;
552527a6266SJeff Kirsher 
553527a6266SJeff Kirsher 	skb_reset_network_header(skb);
554527a6266SJeff Kirsher 	skb_set_transport_header(skb, ip_hdrlen(skb));
555527a6266SJeff Kirsher 	*iphdr = ip_hdr(skb);
556527a6266SJeff Kirsher 	*tcph = tcp_hdr(skb);
557527a6266SJeff Kirsher 	*hdr_flags = LRO_IPV4 | LRO_TCP;
558527a6266SJeff Kirsher 
559527a6266SJeff Kirsher 	return 0;
560527a6266SJeff Kirsher }
561527a6266SJeff Kirsher 
562527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget)
563527a6266SJeff Kirsher {
564527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
565527a6266SJeff Kirsher 	struct net_device_stats *stats = &mp->dev->stats;
566527a6266SJeff Kirsher 	int lro_flush_needed;
567527a6266SJeff Kirsher 	int rx;
568527a6266SJeff Kirsher 
569527a6266SJeff Kirsher 	lro_flush_needed = 0;
570527a6266SJeff Kirsher 	rx = 0;
571527a6266SJeff Kirsher 	while (rx < budget && rxq->rx_desc_count) {
572527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
573527a6266SJeff Kirsher 		unsigned int cmd_sts;
574527a6266SJeff Kirsher 		struct sk_buff *skb;
575527a6266SJeff Kirsher 		u16 byte_cnt;
576527a6266SJeff Kirsher 
577527a6266SJeff Kirsher 		rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
578527a6266SJeff Kirsher 
579527a6266SJeff Kirsher 		cmd_sts = rx_desc->cmd_sts;
580527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA)
581527a6266SJeff Kirsher 			break;
582527a6266SJeff Kirsher 		rmb();
583527a6266SJeff Kirsher 
584527a6266SJeff Kirsher 		skb = rxq->rx_skb[rxq->rx_curr_desc];
585527a6266SJeff Kirsher 		rxq->rx_skb[rxq->rx_curr_desc] = NULL;
586527a6266SJeff Kirsher 
587527a6266SJeff Kirsher 		rxq->rx_curr_desc++;
588527a6266SJeff Kirsher 		if (rxq->rx_curr_desc == rxq->rx_ring_size)
589527a6266SJeff Kirsher 			rxq->rx_curr_desc = 0;
590527a6266SJeff Kirsher 
591527a6266SJeff Kirsher 		dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
592527a6266SJeff Kirsher 				 rx_desc->buf_size, DMA_FROM_DEVICE);
593527a6266SJeff Kirsher 		rxq->rx_desc_count--;
594527a6266SJeff Kirsher 		rx++;
595527a6266SJeff Kirsher 
596527a6266SJeff Kirsher 		mp->work_rx_refill |= 1 << rxq->index;
597527a6266SJeff Kirsher 
598527a6266SJeff Kirsher 		byte_cnt = rx_desc->byte_cnt;
599527a6266SJeff Kirsher 
600527a6266SJeff Kirsher 		/*
601527a6266SJeff Kirsher 		 * Update statistics.
602527a6266SJeff Kirsher 		 *
603527a6266SJeff Kirsher 		 * Note that the descriptor byte count includes 2 dummy
604527a6266SJeff Kirsher 		 * bytes automatically inserted by the hardware at the
605527a6266SJeff Kirsher 		 * start of the packet (which we don't count), and a 4
606527a6266SJeff Kirsher 		 * byte CRC at the end of the packet (which we do count).
607527a6266SJeff Kirsher 		 */
608527a6266SJeff Kirsher 		stats->rx_packets++;
609527a6266SJeff Kirsher 		stats->rx_bytes += byte_cnt - 2;
610527a6266SJeff Kirsher 
611527a6266SJeff Kirsher 		/*
612527a6266SJeff Kirsher 		 * In case we received a packet without first / last bits
613527a6266SJeff Kirsher 		 * on, or the error summary bit is set, the packet needs
614527a6266SJeff Kirsher 		 * to be dropped.
615527a6266SJeff Kirsher 		 */
616527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
617527a6266SJeff Kirsher 			!= (RX_FIRST_DESC | RX_LAST_DESC))
618527a6266SJeff Kirsher 			goto err;
619527a6266SJeff Kirsher 
620527a6266SJeff Kirsher 		/*
621527a6266SJeff Kirsher 		 * The -4 is for the CRC in the trailer of the
622527a6266SJeff Kirsher 		 * received packet
623527a6266SJeff Kirsher 		 */
624527a6266SJeff Kirsher 		skb_put(skb, byte_cnt - 2 - 4);
625527a6266SJeff Kirsher 
626527a6266SJeff Kirsher 		if (cmd_sts & LAYER_4_CHECKSUM_OK)
627527a6266SJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
628527a6266SJeff Kirsher 		skb->protocol = eth_type_trans(skb, mp->dev);
629527a6266SJeff Kirsher 
630527a6266SJeff Kirsher 		if (skb->dev->features & NETIF_F_LRO &&
631527a6266SJeff Kirsher 		    skb->ip_summed == CHECKSUM_UNNECESSARY) {
632527a6266SJeff Kirsher 			lro_receive_skb(&rxq->lro_mgr, skb, (void *)cmd_sts);
633527a6266SJeff Kirsher 			lro_flush_needed = 1;
634527a6266SJeff Kirsher 		} else
635527a6266SJeff Kirsher 			netif_receive_skb(skb);
636527a6266SJeff Kirsher 
637527a6266SJeff Kirsher 		continue;
638527a6266SJeff Kirsher 
639527a6266SJeff Kirsher err:
640527a6266SJeff Kirsher 		stats->rx_dropped++;
641527a6266SJeff Kirsher 
642527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
643527a6266SJeff Kirsher 			(RX_FIRST_DESC | RX_LAST_DESC)) {
644527a6266SJeff Kirsher 			if (net_ratelimit())
645527a6266SJeff Kirsher 				netdev_err(mp->dev,
646527a6266SJeff Kirsher 					   "received packet spanning multiple descriptors\n");
647527a6266SJeff Kirsher 		}
648527a6266SJeff Kirsher 
649527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY)
650527a6266SJeff Kirsher 			stats->rx_errors++;
651527a6266SJeff Kirsher 
652527a6266SJeff Kirsher 		dev_kfree_skb(skb);
653527a6266SJeff Kirsher 	}
654527a6266SJeff Kirsher 
655527a6266SJeff Kirsher 	if (lro_flush_needed)
656527a6266SJeff Kirsher 		lro_flush_all(&rxq->lro_mgr);
657527a6266SJeff Kirsher 
658527a6266SJeff Kirsher 	if (rx < budget)
659527a6266SJeff Kirsher 		mp->work_rx &= ~(1 << rxq->index);
660527a6266SJeff Kirsher 
661527a6266SJeff Kirsher 	return rx;
662527a6266SJeff Kirsher }
663527a6266SJeff Kirsher 
664527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget)
665527a6266SJeff Kirsher {
666527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
667527a6266SJeff Kirsher 	int refilled;
668527a6266SJeff Kirsher 
669527a6266SJeff Kirsher 	refilled = 0;
670527a6266SJeff Kirsher 	while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
671527a6266SJeff Kirsher 		struct sk_buff *skb;
672527a6266SJeff Kirsher 		int rx;
673527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
674527a6266SJeff Kirsher 		int size;
675527a6266SJeff Kirsher 
676527a6266SJeff Kirsher 		skb = __skb_dequeue(&mp->rx_recycle);
677527a6266SJeff Kirsher 		if (skb == NULL)
678c056b734SPradeep A Dalvi 			skb = netdev_alloc_skb(mp->dev, mp->skb_size);
679527a6266SJeff Kirsher 
680527a6266SJeff Kirsher 		if (skb == NULL) {
681527a6266SJeff Kirsher 			mp->oom = 1;
682527a6266SJeff Kirsher 			goto oom;
683527a6266SJeff Kirsher 		}
684527a6266SJeff Kirsher 
685527a6266SJeff Kirsher 		if (SKB_DMA_REALIGN)
686527a6266SJeff Kirsher 			skb_reserve(skb, SKB_DMA_REALIGN);
687527a6266SJeff Kirsher 
688527a6266SJeff Kirsher 		refilled++;
689527a6266SJeff Kirsher 		rxq->rx_desc_count++;
690527a6266SJeff Kirsher 
691527a6266SJeff Kirsher 		rx = rxq->rx_used_desc++;
692527a6266SJeff Kirsher 		if (rxq->rx_used_desc == rxq->rx_ring_size)
693527a6266SJeff Kirsher 			rxq->rx_used_desc = 0;
694527a6266SJeff Kirsher 
695527a6266SJeff Kirsher 		rx_desc = rxq->rx_desc_area + rx;
696527a6266SJeff Kirsher 
697527a6266SJeff Kirsher 		size = skb->end - skb->data;
698527a6266SJeff Kirsher 		rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
699527a6266SJeff Kirsher 						  skb->data, size,
700527a6266SJeff Kirsher 						  DMA_FROM_DEVICE);
701527a6266SJeff Kirsher 		rx_desc->buf_size = size;
702527a6266SJeff Kirsher 		rxq->rx_skb[rx] = skb;
703527a6266SJeff Kirsher 		wmb();
704527a6266SJeff Kirsher 		rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
705527a6266SJeff Kirsher 		wmb();
706527a6266SJeff Kirsher 
707527a6266SJeff Kirsher 		/*
708527a6266SJeff Kirsher 		 * The hardware automatically prepends 2 bytes of
709527a6266SJeff Kirsher 		 * dummy data to each received packet, so that the
710527a6266SJeff Kirsher 		 * IP header ends up 16-byte aligned.
711527a6266SJeff Kirsher 		 */
712527a6266SJeff Kirsher 		skb_reserve(skb, 2);
713527a6266SJeff Kirsher 	}
714527a6266SJeff Kirsher 
715527a6266SJeff Kirsher 	if (refilled < budget)
716527a6266SJeff Kirsher 		mp->work_rx_refill &= ~(1 << rxq->index);
717527a6266SJeff Kirsher 
718527a6266SJeff Kirsher oom:
719527a6266SJeff Kirsher 	return refilled;
720527a6266SJeff Kirsher }
721527a6266SJeff Kirsher 
722527a6266SJeff Kirsher 
723527a6266SJeff Kirsher /* tx ***********************************************************************/
724527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
725527a6266SJeff Kirsher {
726527a6266SJeff Kirsher 	int frag;
727527a6266SJeff Kirsher 
728527a6266SJeff Kirsher 	for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
7299e903e08SEric Dumazet 		const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
7309e903e08SEric Dumazet 
7319e903e08SEric Dumazet 		if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
732527a6266SJeff Kirsher 			return 1;
733527a6266SJeff Kirsher 	}
734527a6266SJeff Kirsher 
735527a6266SJeff Kirsher 	return 0;
736527a6266SJeff Kirsher }
737527a6266SJeff Kirsher 
738527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
739527a6266SJeff Kirsher {
740527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
741527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
742527a6266SJeff Kirsher 	int frag;
743527a6266SJeff Kirsher 
744527a6266SJeff Kirsher 	for (frag = 0; frag < nr_frags; frag++) {
745527a6266SJeff Kirsher 		skb_frag_t *this_frag;
746527a6266SJeff Kirsher 		int tx_index;
747527a6266SJeff Kirsher 		struct tx_desc *desc;
748527a6266SJeff Kirsher 
749527a6266SJeff Kirsher 		this_frag = &skb_shinfo(skb)->frags[frag];
750527a6266SJeff Kirsher 		tx_index = txq->tx_curr_desc++;
751527a6266SJeff Kirsher 		if (txq->tx_curr_desc == txq->tx_ring_size)
752527a6266SJeff Kirsher 			txq->tx_curr_desc = 0;
753527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
754527a6266SJeff Kirsher 
755527a6266SJeff Kirsher 		/*
756527a6266SJeff Kirsher 		 * The last fragment will generate an interrupt
757527a6266SJeff Kirsher 		 * which will free the skb on TX completion.
758527a6266SJeff Kirsher 		 */
759527a6266SJeff Kirsher 		if (frag == nr_frags - 1) {
760527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA |
761527a6266SJeff Kirsher 					ZERO_PADDING | TX_LAST_DESC |
762527a6266SJeff Kirsher 					TX_ENABLE_INTERRUPT;
763527a6266SJeff Kirsher 		} else {
764527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA;
765527a6266SJeff Kirsher 		}
766527a6266SJeff Kirsher 
767527a6266SJeff Kirsher 		desc->l4i_chk = 0;
7689e903e08SEric Dumazet 		desc->byte_cnt = skb_frag_size(this_frag);
769f106358bSIan Campbell 		desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
770f106358bSIan Campbell 						 this_frag, 0,
7719e903e08SEric Dumazet 						 skb_frag_size(this_frag),
772f106358bSIan Campbell 						 DMA_TO_DEVICE);
773527a6266SJeff Kirsher 	}
774527a6266SJeff Kirsher }
775527a6266SJeff Kirsher 
776527a6266SJeff Kirsher static inline __be16 sum16_as_be(__sum16 sum)
777527a6266SJeff Kirsher {
778527a6266SJeff Kirsher 	return (__force __be16)sum;
779527a6266SJeff Kirsher }
780527a6266SJeff Kirsher 
781527a6266SJeff Kirsher static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
782527a6266SJeff Kirsher {
783527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
784527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
785527a6266SJeff Kirsher 	int tx_index;
786527a6266SJeff Kirsher 	struct tx_desc *desc;
787527a6266SJeff Kirsher 	u32 cmd_sts;
788527a6266SJeff Kirsher 	u16 l4i_chk;
789527a6266SJeff Kirsher 	int length;
790527a6266SJeff Kirsher 
791527a6266SJeff Kirsher 	cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
792527a6266SJeff Kirsher 	l4i_chk = 0;
793527a6266SJeff Kirsher 
794527a6266SJeff Kirsher 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
795527a6266SJeff Kirsher 		int hdr_len;
796527a6266SJeff Kirsher 		int tag_bytes;
797527a6266SJeff Kirsher 
798527a6266SJeff Kirsher 		BUG_ON(skb->protocol != htons(ETH_P_IP) &&
799527a6266SJeff Kirsher 		       skb->protocol != htons(ETH_P_8021Q));
800527a6266SJeff Kirsher 
801527a6266SJeff Kirsher 		hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
802527a6266SJeff Kirsher 		tag_bytes = hdr_len - ETH_HLEN;
803527a6266SJeff Kirsher 		if (skb->len - hdr_len > mp->shared->tx_csum_limit ||
804527a6266SJeff Kirsher 		    unlikely(tag_bytes & ~12)) {
805527a6266SJeff Kirsher 			if (skb_checksum_help(skb) == 0)
806527a6266SJeff Kirsher 				goto no_csum;
807527a6266SJeff Kirsher 			kfree_skb(skb);
808527a6266SJeff Kirsher 			return 1;
809527a6266SJeff Kirsher 		}
810527a6266SJeff Kirsher 
811527a6266SJeff Kirsher 		if (tag_bytes & 4)
812527a6266SJeff Kirsher 			cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
813527a6266SJeff Kirsher 		if (tag_bytes & 8)
814527a6266SJeff Kirsher 			cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
815527a6266SJeff Kirsher 
816527a6266SJeff Kirsher 		cmd_sts |= GEN_TCP_UDP_CHECKSUM |
817527a6266SJeff Kirsher 			   GEN_IP_V4_CHECKSUM   |
818527a6266SJeff Kirsher 			   ip_hdr(skb)->ihl << TX_IHL_SHIFT;
819527a6266SJeff Kirsher 
820527a6266SJeff Kirsher 		switch (ip_hdr(skb)->protocol) {
821527a6266SJeff Kirsher 		case IPPROTO_UDP:
822527a6266SJeff Kirsher 			cmd_sts |= UDP_FRAME;
823527a6266SJeff Kirsher 			l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
824527a6266SJeff Kirsher 			break;
825527a6266SJeff Kirsher 		case IPPROTO_TCP:
826527a6266SJeff Kirsher 			l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
827527a6266SJeff Kirsher 			break;
828527a6266SJeff Kirsher 		default:
829527a6266SJeff Kirsher 			BUG();
830527a6266SJeff Kirsher 		}
831527a6266SJeff Kirsher 	} else {
832527a6266SJeff Kirsher no_csum:
833527a6266SJeff Kirsher 		/* Errata BTS #50, IHL must be 5 if no HW checksum */
834527a6266SJeff Kirsher 		cmd_sts |= 5 << TX_IHL_SHIFT;
835527a6266SJeff Kirsher 	}
836527a6266SJeff Kirsher 
837527a6266SJeff Kirsher 	tx_index = txq->tx_curr_desc++;
838527a6266SJeff Kirsher 	if (txq->tx_curr_desc == txq->tx_ring_size)
839527a6266SJeff Kirsher 		txq->tx_curr_desc = 0;
840527a6266SJeff Kirsher 	desc = &txq->tx_desc_area[tx_index];
841527a6266SJeff Kirsher 
842527a6266SJeff Kirsher 	if (nr_frags) {
843527a6266SJeff Kirsher 		txq_submit_frag_skb(txq, skb);
844527a6266SJeff Kirsher 		length = skb_headlen(skb);
845527a6266SJeff Kirsher 	} else {
846527a6266SJeff Kirsher 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
847527a6266SJeff Kirsher 		length = skb->len;
848527a6266SJeff Kirsher 	}
849527a6266SJeff Kirsher 
850527a6266SJeff Kirsher 	desc->l4i_chk = l4i_chk;
851527a6266SJeff Kirsher 	desc->byte_cnt = length;
852527a6266SJeff Kirsher 	desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
853527a6266SJeff Kirsher 				       length, DMA_TO_DEVICE);
854527a6266SJeff Kirsher 
855527a6266SJeff Kirsher 	__skb_queue_tail(&txq->tx_skb, skb);
856527a6266SJeff Kirsher 
857527a6266SJeff Kirsher 	skb_tx_timestamp(skb);
858527a6266SJeff Kirsher 
859527a6266SJeff Kirsher 	/* ensure all other descriptors are written before first cmd_sts */
860527a6266SJeff Kirsher 	wmb();
861527a6266SJeff Kirsher 	desc->cmd_sts = cmd_sts;
862527a6266SJeff Kirsher 
863527a6266SJeff Kirsher 	/* clear TX_END status */
864527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
865527a6266SJeff Kirsher 
866527a6266SJeff Kirsher 	/* ensure all descriptors are written before poking hardware */
867527a6266SJeff Kirsher 	wmb();
868527a6266SJeff Kirsher 	txq_enable(txq);
869527a6266SJeff Kirsher 
870527a6266SJeff Kirsher 	txq->tx_desc_count += nr_frags + 1;
871527a6266SJeff Kirsher 
872527a6266SJeff Kirsher 	return 0;
873527a6266SJeff Kirsher }
874527a6266SJeff Kirsher 
875527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
876527a6266SJeff Kirsher {
877527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
878527a6266SJeff Kirsher 	int length, queue;
879527a6266SJeff Kirsher 	struct tx_queue *txq;
880527a6266SJeff Kirsher 	struct netdev_queue *nq;
881527a6266SJeff Kirsher 
882527a6266SJeff Kirsher 	queue = skb_get_queue_mapping(skb);
883527a6266SJeff Kirsher 	txq = mp->txq + queue;
884527a6266SJeff Kirsher 	nq = netdev_get_tx_queue(dev, queue);
885527a6266SJeff Kirsher 
886527a6266SJeff Kirsher 	if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
887527a6266SJeff Kirsher 		txq->tx_dropped++;
888527a6266SJeff Kirsher 		netdev_printk(KERN_DEBUG, dev,
889527a6266SJeff Kirsher 			      "failed to linearize skb with tiny unaligned fragment\n");
890527a6266SJeff Kirsher 		return NETDEV_TX_BUSY;
891527a6266SJeff Kirsher 	}
892527a6266SJeff Kirsher 
893527a6266SJeff Kirsher 	if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
894527a6266SJeff Kirsher 		if (net_ratelimit())
895527a6266SJeff Kirsher 			netdev_err(dev, "tx queue full?!\n");
896527a6266SJeff Kirsher 		kfree_skb(skb);
897527a6266SJeff Kirsher 		return NETDEV_TX_OK;
898527a6266SJeff Kirsher 	}
899527a6266SJeff Kirsher 
900527a6266SJeff Kirsher 	length = skb->len;
901527a6266SJeff Kirsher 
902527a6266SJeff Kirsher 	if (!txq_submit_skb(txq, skb)) {
903527a6266SJeff Kirsher 		int entries_left;
904527a6266SJeff Kirsher 
905527a6266SJeff Kirsher 		txq->tx_bytes += length;
906527a6266SJeff Kirsher 		txq->tx_packets++;
907527a6266SJeff Kirsher 
908527a6266SJeff Kirsher 		entries_left = txq->tx_ring_size - txq->tx_desc_count;
909527a6266SJeff Kirsher 		if (entries_left < MAX_SKB_FRAGS + 1)
910527a6266SJeff Kirsher 			netif_tx_stop_queue(nq);
911527a6266SJeff Kirsher 	}
912527a6266SJeff Kirsher 
913527a6266SJeff Kirsher 	return NETDEV_TX_OK;
914527a6266SJeff Kirsher }
915527a6266SJeff Kirsher 
916527a6266SJeff Kirsher 
917527a6266SJeff Kirsher /* tx napi ******************************************************************/
918527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq)
919527a6266SJeff Kirsher {
920527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
921527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
922527a6266SJeff Kirsher 	u32 hw_desc_ptr;
923527a6266SJeff Kirsher 	u32 expected_ptr;
924527a6266SJeff Kirsher 
925527a6266SJeff Kirsher 	__netif_tx_lock(nq, smp_processor_id());
926527a6266SJeff Kirsher 
927527a6266SJeff Kirsher 	if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
928527a6266SJeff Kirsher 		goto out;
929527a6266SJeff Kirsher 
930527a6266SJeff Kirsher 	hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
931527a6266SJeff Kirsher 	expected_ptr = (u32)txq->tx_desc_dma +
932527a6266SJeff Kirsher 				txq->tx_curr_desc * sizeof(struct tx_desc);
933527a6266SJeff Kirsher 
934527a6266SJeff Kirsher 	if (hw_desc_ptr != expected_ptr)
935527a6266SJeff Kirsher 		txq_enable(txq);
936527a6266SJeff Kirsher 
937527a6266SJeff Kirsher out:
938527a6266SJeff Kirsher 	__netif_tx_unlock(nq);
939527a6266SJeff Kirsher 
940527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
941527a6266SJeff Kirsher }
942527a6266SJeff Kirsher 
943527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force)
944527a6266SJeff Kirsher {
945527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
946527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
947527a6266SJeff Kirsher 	int reclaimed;
948527a6266SJeff Kirsher 
949527a6266SJeff Kirsher 	__netif_tx_lock(nq, smp_processor_id());
950527a6266SJeff Kirsher 
951527a6266SJeff Kirsher 	reclaimed = 0;
952527a6266SJeff Kirsher 	while (reclaimed < budget && txq->tx_desc_count > 0) {
953527a6266SJeff Kirsher 		int tx_index;
954527a6266SJeff Kirsher 		struct tx_desc *desc;
955527a6266SJeff Kirsher 		u32 cmd_sts;
956527a6266SJeff Kirsher 		struct sk_buff *skb;
957527a6266SJeff Kirsher 
958527a6266SJeff Kirsher 		tx_index = txq->tx_used_desc;
959527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
960527a6266SJeff Kirsher 		cmd_sts = desc->cmd_sts;
961527a6266SJeff Kirsher 
962527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA) {
963527a6266SJeff Kirsher 			if (!force)
964527a6266SJeff Kirsher 				break;
965527a6266SJeff Kirsher 			desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
966527a6266SJeff Kirsher 		}
967527a6266SJeff Kirsher 
968527a6266SJeff Kirsher 		txq->tx_used_desc = tx_index + 1;
969527a6266SJeff Kirsher 		if (txq->tx_used_desc == txq->tx_ring_size)
970527a6266SJeff Kirsher 			txq->tx_used_desc = 0;
971527a6266SJeff Kirsher 
972527a6266SJeff Kirsher 		reclaimed++;
973527a6266SJeff Kirsher 		txq->tx_desc_count--;
974527a6266SJeff Kirsher 
975527a6266SJeff Kirsher 		skb = NULL;
976527a6266SJeff Kirsher 		if (cmd_sts & TX_LAST_DESC)
977527a6266SJeff Kirsher 			skb = __skb_dequeue(&txq->tx_skb);
978527a6266SJeff Kirsher 
979527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY) {
980527a6266SJeff Kirsher 			netdev_info(mp->dev, "tx error\n");
981527a6266SJeff Kirsher 			mp->dev->stats.tx_errors++;
982527a6266SJeff Kirsher 		}
983527a6266SJeff Kirsher 
984527a6266SJeff Kirsher 		if (cmd_sts & TX_FIRST_DESC) {
985527a6266SJeff Kirsher 			dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
986527a6266SJeff Kirsher 					 desc->byte_cnt, DMA_TO_DEVICE);
987527a6266SJeff Kirsher 		} else {
988527a6266SJeff Kirsher 			dma_unmap_page(mp->dev->dev.parent, desc->buf_ptr,
989527a6266SJeff Kirsher 				       desc->byte_cnt, DMA_TO_DEVICE);
990527a6266SJeff Kirsher 		}
991527a6266SJeff Kirsher 
992527a6266SJeff Kirsher 		if (skb != NULL) {
993527a6266SJeff Kirsher 			if (skb_queue_len(&mp->rx_recycle) <
994527a6266SJeff Kirsher 					mp->rx_ring_size &&
995527a6266SJeff Kirsher 			    skb_recycle_check(skb, mp->skb_size))
996527a6266SJeff Kirsher 				__skb_queue_head(&mp->rx_recycle, skb);
997527a6266SJeff Kirsher 			else
998527a6266SJeff Kirsher 				dev_kfree_skb(skb);
999527a6266SJeff Kirsher 		}
1000527a6266SJeff Kirsher 	}
1001527a6266SJeff Kirsher 
1002527a6266SJeff Kirsher 	__netif_tx_unlock(nq);
1003527a6266SJeff Kirsher 
1004527a6266SJeff Kirsher 	if (reclaimed < budget)
1005527a6266SJeff Kirsher 		mp->work_tx &= ~(1 << txq->index);
1006527a6266SJeff Kirsher 
1007527a6266SJeff Kirsher 	return reclaimed;
1008527a6266SJeff Kirsher }
1009527a6266SJeff Kirsher 
1010527a6266SJeff Kirsher 
1011527a6266SJeff Kirsher /* tx rate control **********************************************************/
1012527a6266SJeff Kirsher /*
1013527a6266SJeff Kirsher  * Set total maximum TX rate (shared by all TX queues for this port)
1014527a6266SJeff Kirsher  * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
1015527a6266SJeff Kirsher  */
1016527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
1017527a6266SJeff Kirsher {
1018527a6266SJeff Kirsher 	int token_rate;
1019527a6266SJeff Kirsher 	int mtu;
1020527a6266SJeff Kirsher 	int bucket_size;
1021527a6266SJeff Kirsher 
1022452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1023527a6266SJeff Kirsher 	if (token_rate > 1023)
1024527a6266SJeff Kirsher 		token_rate = 1023;
1025527a6266SJeff Kirsher 
1026527a6266SJeff Kirsher 	mtu = (mp->dev->mtu + 255) >> 8;
1027527a6266SJeff Kirsher 	if (mtu > 63)
1028527a6266SJeff Kirsher 		mtu = 63;
1029527a6266SJeff Kirsher 
1030527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1031527a6266SJeff Kirsher 	if (bucket_size > 65535)
1032527a6266SJeff Kirsher 		bucket_size = 65535;
1033527a6266SJeff Kirsher 
1034527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1035527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1036527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE, token_rate);
1037527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU, mtu);
1038527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST, bucket_size);
1039527a6266SJeff Kirsher 		break;
1040527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1041527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE_MOVED, token_rate);
1042527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU_MOVED, mtu);
1043527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
1044527a6266SJeff Kirsher 		break;
1045527a6266SJeff Kirsher 	}
1046527a6266SJeff Kirsher }
1047527a6266SJeff Kirsher 
1048527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
1049527a6266SJeff Kirsher {
1050527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1051527a6266SJeff Kirsher 	int token_rate;
1052527a6266SJeff Kirsher 	int bucket_size;
1053527a6266SJeff Kirsher 
1054452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1055527a6266SJeff Kirsher 	if (token_rate > 1023)
1056527a6266SJeff Kirsher 		token_rate = 1023;
1057527a6266SJeff Kirsher 
1058527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1059527a6266SJeff Kirsher 	if (bucket_size > 65535)
1060527a6266SJeff Kirsher 		bucket_size = 65535;
1061527a6266SJeff Kirsher 
1062527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
1063527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
1064527a6266SJeff Kirsher }
1065527a6266SJeff Kirsher 
1066527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq)
1067527a6266SJeff Kirsher {
1068527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1069527a6266SJeff Kirsher 	int off;
1070527a6266SJeff Kirsher 	u32 val;
1071527a6266SJeff Kirsher 
1072527a6266SJeff Kirsher 	/*
1073527a6266SJeff Kirsher 	 * Turn on fixed priority mode.
1074527a6266SJeff Kirsher 	 */
1075527a6266SJeff Kirsher 	off = 0;
1076527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1077527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1078527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF;
1079527a6266SJeff Kirsher 		break;
1080527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1081527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF_MOVED;
1082527a6266SJeff Kirsher 		break;
1083527a6266SJeff Kirsher 	}
1084527a6266SJeff Kirsher 
1085527a6266SJeff Kirsher 	if (off) {
1086527a6266SJeff Kirsher 		val = rdlp(mp, off);
1087527a6266SJeff Kirsher 		val |= 1 << txq->index;
1088527a6266SJeff Kirsher 		wrlp(mp, off, val);
1089527a6266SJeff Kirsher 	}
1090527a6266SJeff Kirsher }
1091527a6266SJeff Kirsher 
1092527a6266SJeff Kirsher 
1093527a6266SJeff Kirsher /* mii management interface *************************************************/
1094527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_err_irq(int irq, void *dev_id)
1095527a6266SJeff Kirsher {
1096527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = dev_id;
1097527a6266SJeff Kirsher 
1098527a6266SJeff Kirsher 	if (readl(msp->base + ERR_INT_CAUSE) & ERR_INT_SMI_DONE) {
1099527a6266SJeff Kirsher 		writel(~ERR_INT_SMI_DONE, msp->base + ERR_INT_CAUSE);
1100527a6266SJeff Kirsher 		wake_up(&msp->smi_busy_wait);
1101527a6266SJeff Kirsher 		return IRQ_HANDLED;
1102527a6266SJeff Kirsher 	}
1103527a6266SJeff Kirsher 
1104527a6266SJeff Kirsher 	return IRQ_NONE;
1105527a6266SJeff Kirsher }
1106527a6266SJeff Kirsher 
1107527a6266SJeff Kirsher static int smi_is_done(struct mv643xx_eth_shared_private *msp)
1108527a6266SJeff Kirsher {
1109527a6266SJeff Kirsher 	return !(readl(msp->base + SMI_REG) & SMI_BUSY);
1110527a6266SJeff Kirsher }
1111527a6266SJeff Kirsher 
1112527a6266SJeff Kirsher static int smi_wait_ready(struct mv643xx_eth_shared_private *msp)
1113527a6266SJeff Kirsher {
1114527a6266SJeff Kirsher 	if (msp->err_interrupt == NO_IRQ) {
1115527a6266SJeff Kirsher 		int i;
1116527a6266SJeff Kirsher 
1117527a6266SJeff Kirsher 		for (i = 0; !smi_is_done(msp); i++) {
1118527a6266SJeff Kirsher 			if (i == 10)
1119527a6266SJeff Kirsher 				return -ETIMEDOUT;
1120527a6266SJeff Kirsher 			msleep(10);
1121527a6266SJeff Kirsher 		}
1122527a6266SJeff Kirsher 
1123527a6266SJeff Kirsher 		return 0;
1124527a6266SJeff Kirsher 	}
1125527a6266SJeff Kirsher 
1126527a6266SJeff Kirsher 	if (!smi_is_done(msp)) {
1127527a6266SJeff Kirsher 		wait_event_timeout(msp->smi_busy_wait, smi_is_done(msp),
1128527a6266SJeff Kirsher 				   msecs_to_jiffies(100));
1129527a6266SJeff Kirsher 		if (!smi_is_done(msp))
1130527a6266SJeff Kirsher 			return -ETIMEDOUT;
1131527a6266SJeff Kirsher 	}
1132527a6266SJeff Kirsher 
1133527a6266SJeff Kirsher 	return 0;
1134527a6266SJeff Kirsher }
1135527a6266SJeff Kirsher 
1136527a6266SJeff Kirsher static int smi_bus_read(struct mii_bus *bus, int addr, int reg)
1137527a6266SJeff Kirsher {
1138527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = bus->priv;
1139527a6266SJeff Kirsher 	void __iomem *smi_reg = msp->base + SMI_REG;
1140527a6266SJeff Kirsher 	int ret;
1141527a6266SJeff Kirsher 
1142527a6266SJeff Kirsher 	if (smi_wait_ready(msp)) {
1143527a6266SJeff Kirsher 		pr_warn("SMI bus busy timeout\n");
1144527a6266SJeff Kirsher 		return -ETIMEDOUT;
1145527a6266SJeff Kirsher 	}
1146527a6266SJeff Kirsher 
1147527a6266SJeff Kirsher 	writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
1148527a6266SJeff Kirsher 
1149527a6266SJeff Kirsher 	if (smi_wait_ready(msp)) {
1150527a6266SJeff Kirsher 		pr_warn("SMI bus busy timeout\n");
1151527a6266SJeff Kirsher 		return -ETIMEDOUT;
1152527a6266SJeff Kirsher 	}
1153527a6266SJeff Kirsher 
1154527a6266SJeff Kirsher 	ret = readl(smi_reg);
1155527a6266SJeff Kirsher 	if (!(ret & SMI_READ_VALID)) {
1156527a6266SJeff Kirsher 		pr_warn("SMI bus read not valid\n");
1157527a6266SJeff Kirsher 		return -ENODEV;
1158527a6266SJeff Kirsher 	}
1159527a6266SJeff Kirsher 
1160527a6266SJeff Kirsher 	return ret & 0xffff;
1161527a6266SJeff Kirsher }
1162527a6266SJeff Kirsher 
1163527a6266SJeff Kirsher static int smi_bus_write(struct mii_bus *bus, int addr, int reg, u16 val)
1164527a6266SJeff Kirsher {
1165527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = bus->priv;
1166527a6266SJeff Kirsher 	void __iomem *smi_reg = msp->base + SMI_REG;
1167527a6266SJeff Kirsher 
1168527a6266SJeff Kirsher 	if (smi_wait_ready(msp)) {
1169527a6266SJeff Kirsher 		pr_warn("SMI bus busy timeout\n");
1170527a6266SJeff Kirsher 		return -ETIMEDOUT;
1171527a6266SJeff Kirsher 	}
1172527a6266SJeff Kirsher 
1173527a6266SJeff Kirsher 	writel(SMI_OPCODE_WRITE | (reg << 21) |
1174527a6266SJeff Kirsher 		(addr << 16) | (val & 0xffff), smi_reg);
1175527a6266SJeff Kirsher 
1176527a6266SJeff Kirsher 	if (smi_wait_ready(msp)) {
1177527a6266SJeff Kirsher 		pr_warn("SMI bus busy timeout\n");
1178527a6266SJeff Kirsher 		return -ETIMEDOUT;
1179527a6266SJeff Kirsher 	}
1180527a6266SJeff Kirsher 
1181527a6266SJeff Kirsher 	return 0;
1182527a6266SJeff Kirsher }
1183527a6266SJeff Kirsher 
1184527a6266SJeff Kirsher 
1185527a6266SJeff Kirsher /* statistics ***************************************************************/
1186527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1187527a6266SJeff Kirsher {
1188527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1189527a6266SJeff Kirsher 	struct net_device_stats *stats = &dev->stats;
1190527a6266SJeff Kirsher 	unsigned long tx_packets = 0;
1191527a6266SJeff Kirsher 	unsigned long tx_bytes = 0;
1192527a6266SJeff Kirsher 	unsigned long tx_dropped = 0;
1193527a6266SJeff Kirsher 	int i;
1194527a6266SJeff Kirsher 
1195527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
1196527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
1197527a6266SJeff Kirsher 
1198527a6266SJeff Kirsher 		tx_packets += txq->tx_packets;
1199527a6266SJeff Kirsher 		tx_bytes += txq->tx_bytes;
1200527a6266SJeff Kirsher 		tx_dropped += txq->tx_dropped;
1201527a6266SJeff Kirsher 	}
1202527a6266SJeff Kirsher 
1203527a6266SJeff Kirsher 	stats->tx_packets = tx_packets;
1204527a6266SJeff Kirsher 	stats->tx_bytes = tx_bytes;
1205527a6266SJeff Kirsher 	stats->tx_dropped = tx_dropped;
1206527a6266SJeff Kirsher 
1207527a6266SJeff Kirsher 	return stats;
1208527a6266SJeff Kirsher }
1209527a6266SJeff Kirsher 
1210527a6266SJeff Kirsher static void mv643xx_eth_grab_lro_stats(struct mv643xx_eth_private *mp)
1211527a6266SJeff Kirsher {
1212527a6266SJeff Kirsher 	u32 lro_aggregated = 0;
1213527a6266SJeff Kirsher 	u32 lro_flushed = 0;
1214527a6266SJeff Kirsher 	u32 lro_no_desc = 0;
1215527a6266SJeff Kirsher 	int i;
1216527a6266SJeff Kirsher 
1217527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
1218527a6266SJeff Kirsher 		struct rx_queue *rxq = mp->rxq + i;
1219527a6266SJeff Kirsher 
1220527a6266SJeff Kirsher 		lro_aggregated += rxq->lro_mgr.stats.aggregated;
1221527a6266SJeff Kirsher 		lro_flushed += rxq->lro_mgr.stats.flushed;
1222527a6266SJeff Kirsher 		lro_no_desc += rxq->lro_mgr.stats.no_desc;
1223527a6266SJeff Kirsher 	}
1224527a6266SJeff Kirsher 
1225527a6266SJeff Kirsher 	mp->lro_counters.lro_aggregated = lro_aggregated;
1226527a6266SJeff Kirsher 	mp->lro_counters.lro_flushed = lro_flushed;
1227527a6266SJeff Kirsher 	mp->lro_counters.lro_no_desc = lro_no_desc;
1228527a6266SJeff Kirsher }
1229527a6266SJeff Kirsher 
1230527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
1231527a6266SJeff Kirsher {
1232527a6266SJeff Kirsher 	return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
1233527a6266SJeff Kirsher }
1234527a6266SJeff Kirsher 
1235527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp)
1236527a6266SJeff Kirsher {
1237527a6266SJeff Kirsher 	int i;
1238527a6266SJeff Kirsher 
1239527a6266SJeff Kirsher 	for (i = 0; i < 0x80; i += 4)
1240527a6266SJeff Kirsher 		mib_read(mp, i);
1241302476c9SPaulius Zaleckas 
1242302476c9SPaulius Zaleckas 	/* Clear non MIB hw counters also */
1243302476c9SPaulius Zaleckas 	rdlp(mp, RX_DISCARD_FRAME_CNT);
1244302476c9SPaulius Zaleckas 	rdlp(mp, RX_OVERRUN_FRAME_CNT);
1245527a6266SJeff Kirsher }
1246527a6266SJeff Kirsher 
1247527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp)
1248527a6266SJeff Kirsher {
1249527a6266SJeff Kirsher 	struct mib_counters *p = &mp->mib_counters;
1250527a6266SJeff Kirsher 
1251527a6266SJeff Kirsher 	spin_lock_bh(&mp->mib_counters_lock);
1252527a6266SJeff Kirsher 	p->good_octets_received += mib_read(mp, 0x00);
1253527a6266SJeff Kirsher 	p->bad_octets_received += mib_read(mp, 0x08);
1254527a6266SJeff Kirsher 	p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1255527a6266SJeff Kirsher 	p->good_frames_received += mib_read(mp, 0x10);
1256527a6266SJeff Kirsher 	p->bad_frames_received += mib_read(mp, 0x14);
1257527a6266SJeff Kirsher 	p->broadcast_frames_received += mib_read(mp, 0x18);
1258527a6266SJeff Kirsher 	p->multicast_frames_received += mib_read(mp, 0x1c);
1259527a6266SJeff Kirsher 	p->frames_64_octets += mib_read(mp, 0x20);
1260527a6266SJeff Kirsher 	p->frames_65_to_127_octets += mib_read(mp, 0x24);
1261527a6266SJeff Kirsher 	p->frames_128_to_255_octets += mib_read(mp, 0x28);
1262527a6266SJeff Kirsher 	p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1263527a6266SJeff Kirsher 	p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1264527a6266SJeff Kirsher 	p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1265527a6266SJeff Kirsher 	p->good_octets_sent += mib_read(mp, 0x38);
1266527a6266SJeff Kirsher 	p->good_frames_sent += mib_read(mp, 0x40);
1267527a6266SJeff Kirsher 	p->excessive_collision += mib_read(mp, 0x44);
1268527a6266SJeff Kirsher 	p->multicast_frames_sent += mib_read(mp, 0x48);
1269527a6266SJeff Kirsher 	p->broadcast_frames_sent += mib_read(mp, 0x4c);
1270527a6266SJeff Kirsher 	p->unrec_mac_control_received += mib_read(mp, 0x50);
1271527a6266SJeff Kirsher 	p->fc_sent += mib_read(mp, 0x54);
1272527a6266SJeff Kirsher 	p->good_fc_received += mib_read(mp, 0x58);
1273527a6266SJeff Kirsher 	p->bad_fc_received += mib_read(mp, 0x5c);
1274527a6266SJeff Kirsher 	p->undersize_received += mib_read(mp, 0x60);
1275527a6266SJeff Kirsher 	p->fragments_received += mib_read(mp, 0x64);
1276527a6266SJeff Kirsher 	p->oversize_received += mib_read(mp, 0x68);
1277527a6266SJeff Kirsher 	p->jabber_received += mib_read(mp, 0x6c);
1278527a6266SJeff Kirsher 	p->mac_receive_error += mib_read(mp, 0x70);
1279527a6266SJeff Kirsher 	p->bad_crc_event += mib_read(mp, 0x74);
1280527a6266SJeff Kirsher 	p->collision += mib_read(mp, 0x78);
1281527a6266SJeff Kirsher 	p->late_collision += mib_read(mp, 0x7c);
1282302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
1283302476c9SPaulius Zaleckas 	p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1284302476c9SPaulius Zaleckas 	p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
1285527a6266SJeff Kirsher 	spin_unlock_bh(&mp->mib_counters_lock);
1286527a6266SJeff Kirsher 
1287527a6266SJeff Kirsher 	mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1288527a6266SJeff Kirsher }
1289527a6266SJeff Kirsher 
1290527a6266SJeff Kirsher static void mib_counters_timer_wrapper(unsigned long _mp)
1291527a6266SJeff Kirsher {
1292527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = (void *)_mp;
1293527a6266SJeff Kirsher 
1294527a6266SJeff Kirsher 	mib_counters_update(mp);
1295527a6266SJeff Kirsher }
1296527a6266SJeff Kirsher 
1297527a6266SJeff Kirsher 
1298527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/
1299527a6266SJeff Kirsher /*
1300527a6266SJeff Kirsher  * Hardware coalescing parameters are set in units of 64 t_clk
1301527a6266SJeff Kirsher  * cycles.  I.e.:
1302527a6266SJeff Kirsher  *
1303527a6266SJeff Kirsher  *	coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1304527a6266SJeff Kirsher  *
1305527a6266SJeff Kirsher  *	register_value = coal_delay_in_usec * t_clk_rate / 64000000
1306527a6266SJeff Kirsher  *
1307527a6266SJeff Kirsher  * In the ->set*() methods, we round the computed register value
1308527a6266SJeff Kirsher  * to the nearest integer.
1309527a6266SJeff Kirsher  */
1310527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1311527a6266SJeff Kirsher {
1312527a6266SJeff Kirsher 	u32 val = rdlp(mp, SDMA_CONFIG);
1313527a6266SJeff Kirsher 	u64 temp;
1314527a6266SJeff Kirsher 
1315527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit)
1316527a6266SJeff Kirsher 		temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1317527a6266SJeff Kirsher 	else
1318527a6266SJeff Kirsher 		temp = (val & 0x003fff00) >> 8;
1319527a6266SJeff Kirsher 
1320527a6266SJeff Kirsher 	temp *= 64000000;
1321452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1322527a6266SJeff Kirsher 
1323527a6266SJeff Kirsher 	return (unsigned int)temp;
1324527a6266SJeff Kirsher }
1325527a6266SJeff Kirsher 
1326527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1327527a6266SJeff Kirsher {
1328527a6266SJeff Kirsher 	u64 temp;
1329527a6266SJeff Kirsher 	u32 val;
1330527a6266SJeff Kirsher 
1331452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1332527a6266SJeff Kirsher 	temp += 31999999;
1333527a6266SJeff Kirsher 	do_div(temp, 64000000);
1334527a6266SJeff Kirsher 
1335527a6266SJeff Kirsher 	val = rdlp(mp, SDMA_CONFIG);
1336527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit) {
1337527a6266SJeff Kirsher 		if (temp > 0xffff)
1338527a6266SJeff Kirsher 			temp = 0xffff;
1339527a6266SJeff Kirsher 		val &= ~0x023fff80;
1340527a6266SJeff Kirsher 		val |= (temp & 0x8000) << 10;
1341527a6266SJeff Kirsher 		val |= (temp & 0x7fff) << 7;
1342527a6266SJeff Kirsher 	} else {
1343527a6266SJeff Kirsher 		if (temp > 0x3fff)
1344527a6266SJeff Kirsher 			temp = 0x3fff;
1345527a6266SJeff Kirsher 		val &= ~0x003fff00;
1346527a6266SJeff Kirsher 		val |= (temp & 0x3fff) << 8;
1347527a6266SJeff Kirsher 	}
1348527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, val);
1349527a6266SJeff Kirsher }
1350527a6266SJeff Kirsher 
1351527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1352527a6266SJeff Kirsher {
1353527a6266SJeff Kirsher 	u64 temp;
1354527a6266SJeff Kirsher 
1355527a6266SJeff Kirsher 	temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1356527a6266SJeff Kirsher 	temp *= 64000000;
1357452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1358527a6266SJeff Kirsher 
1359527a6266SJeff Kirsher 	return (unsigned int)temp;
1360527a6266SJeff Kirsher }
1361527a6266SJeff Kirsher 
1362527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1363527a6266SJeff Kirsher {
1364527a6266SJeff Kirsher 	u64 temp;
1365527a6266SJeff Kirsher 
1366452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1367527a6266SJeff Kirsher 	temp += 31999999;
1368527a6266SJeff Kirsher 	do_div(temp, 64000000);
1369527a6266SJeff Kirsher 
1370527a6266SJeff Kirsher 	if (temp > 0x3fff)
1371527a6266SJeff Kirsher 		temp = 0x3fff;
1372527a6266SJeff Kirsher 
1373527a6266SJeff Kirsher 	wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1374527a6266SJeff Kirsher }
1375527a6266SJeff Kirsher 
1376527a6266SJeff Kirsher 
1377527a6266SJeff Kirsher /* ethtool ******************************************************************/
1378527a6266SJeff Kirsher struct mv643xx_eth_stats {
1379527a6266SJeff Kirsher 	char stat_string[ETH_GSTRING_LEN];
1380527a6266SJeff Kirsher 	int sizeof_stat;
1381527a6266SJeff Kirsher 	int netdev_off;
1382527a6266SJeff Kirsher 	int mp_off;
1383527a6266SJeff Kirsher };
1384527a6266SJeff Kirsher 
1385527a6266SJeff Kirsher #define SSTAT(m)						\
1386527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct net_device_stats, m),		\
1387527a6266SJeff Kirsher 	  offsetof(struct net_device, stats.m), -1 }
1388527a6266SJeff Kirsher 
1389527a6266SJeff Kirsher #define MIBSTAT(m)						\
1390527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct mib_counters, m),		\
1391527a6266SJeff Kirsher 	  -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1392527a6266SJeff Kirsher 
1393527a6266SJeff Kirsher #define LROSTAT(m)						\
1394527a6266SJeff Kirsher 	{ #m, FIELD_SIZEOF(struct lro_counters, m),		\
1395527a6266SJeff Kirsher 	  -1, offsetof(struct mv643xx_eth_private, lro_counters.m) }
1396527a6266SJeff Kirsher 
1397527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1398527a6266SJeff Kirsher 	SSTAT(rx_packets),
1399527a6266SJeff Kirsher 	SSTAT(tx_packets),
1400527a6266SJeff Kirsher 	SSTAT(rx_bytes),
1401527a6266SJeff Kirsher 	SSTAT(tx_bytes),
1402527a6266SJeff Kirsher 	SSTAT(rx_errors),
1403527a6266SJeff Kirsher 	SSTAT(tx_errors),
1404527a6266SJeff Kirsher 	SSTAT(rx_dropped),
1405527a6266SJeff Kirsher 	SSTAT(tx_dropped),
1406527a6266SJeff Kirsher 	MIBSTAT(good_octets_received),
1407527a6266SJeff Kirsher 	MIBSTAT(bad_octets_received),
1408527a6266SJeff Kirsher 	MIBSTAT(internal_mac_transmit_err),
1409527a6266SJeff Kirsher 	MIBSTAT(good_frames_received),
1410527a6266SJeff Kirsher 	MIBSTAT(bad_frames_received),
1411527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_received),
1412527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_received),
1413527a6266SJeff Kirsher 	MIBSTAT(frames_64_octets),
1414527a6266SJeff Kirsher 	MIBSTAT(frames_65_to_127_octets),
1415527a6266SJeff Kirsher 	MIBSTAT(frames_128_to_255_octets),
1416527a6266SJeff Kirsher 	MIBSTAT(frames_256_to_511_octets),
1417527a6266SJeff Kirsher 	MIBSTAT(frames_512_to_1023_octets),
1418527a6266SJeff Kirsher 	MIBSTAT(frames_1024_to_max_octets),
1419527a6266SJeff Kirsher 	MIBSTAT(good_octets_sent),
1420527a6266SJeff Kirsher 	MIBSTAT(good_frames_sent),
1421527a6266SJeff Kirsher 	MIBSTAT(excessive_collision),
1422527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_sent),
1423527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_sent),
1424527a6266SJeff Kirsher 	MIBSTAT(unrec_mac_control_received),
1425527a6266SJeff Kirsher 	MIBSTAT(fc_sent),
1426527a6266SJeff Kirsher 	MIBSTAT(good_fc_received),
1427527a6266SJeff Kirsher 	MIBSTAT(bad_fc_received),
1428527a6266SJeff Kirsher 	MIBSTAT(undersize_received),
1429527a6266SJeff Kirsher 	MIBSTAT(fragments_received),
1430527a6266SJeff Kirsher 	MIBSTAT(oversize_received),
1431527a6266SJeff Kirsher 	MIBSTAT(jabber_received),
1432527a6266SJeff Kirsher 	MIBSTAT(mac_receive_error),
1433527a6266SJeff Kirsher 	MIBSTAT(bad_crc_event),
1434527a6266SJeff Kirsher 	MIBSTAT(collision),
1435527a6266SJeff Kirsher 	MIBSTAT(late_collision),
1436302476c9SPaulius Zaleckas 	MIBSTAT(rx_discard),
1437302476c9SPaulius Zaleckas 	MIBSTAT(rx_overrun),
1438527a6266SJeff Kirsher 	LROSTAT(lro_aggregated),
1439527a6266SJeff Kirsher 	LROSTAT(lro_flushed),
1440527a6266SJeff Kirsher 	LROSTAT(lro_no_desc),
1441527a6266SJeff Kirsher };
1442527a6266SJeff Kirsher 
1443527a6266SJeff Kirsher static int
1444527a6266SJeff Kirsher mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
1445527a6266SJeff Kirsher 			     struct ethtool_cmd *cmd)
1446527a6266SJeff Kirsher {
1447527a6266SJeff Kirsher 	int err;
1448527a6266SJeff Kirsher 
1449527a6266SJeff Kirsher 	err = phy_read_status(mp->phy);
1450527a6266SJeff Kirsher 	if (err == 0)
1451527a6266SJeff Kirsher 		err = phy_ethtool_gset(mp->phy, cmd);
1452527a6266SJeff Kirsher 
1453527a6266SJeff Kirsher 	/*
1454527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1455527a6266SJeff Kirsher 	 */
1456527a6266SJeff Kirsher 	cmd->supported &= ~SUPPORTED_1000baseT_Half;
1457527a6266SJeff Kirsher 	cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1458527a6266SJeff Kirsher 
1459527a6266SJeff Kirsher 	return err;
1460527a6266SJeff Kirsher }
1461527a6266SJeff Kirsher 
1462527a6266SJeff Kirsher static int
1463527a6266SJeff Kirsher mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
1464527a6266SJeff Kirsher 				 struct ethtool_cmd *cmd)
1465527a6266SJeff Kirsher {
1466527a6266SJeff Kirsher 	u32 port_status;
1467527a6266SJeff Kirsher 
1468527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
1469527a6266SJeff Kirsher 
1470527a6266SJeff Kirsher 	cmd->supported = SUPPORTED_MII;
1471527a6266SJeff Kirsher 	cmd->advertising = ADVERTISED_MII;
1472527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
1473527a6266SJeff Kirsher 	case PORT_SPEED_10:
1474527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_10);
1475527a6266SJeff Kirsher 		break;
1476527a6266SJeff Kirsher 	case PORT_SPEED_100:
1477527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_100);
1478527a6266SJeff Kirsher 		break;
1479527a6266SJeff Kirsher 	case PORT_SPEED_1000:
1480527a6266SJeff Kirsher 		ethtool_cmd_speed_set(cmd, SPEED_1000);
1481527a6266SJeff Kirsher 		break;
1482527a6266SJeff Kirsher 	default:
1483527a6266SJeff Kirsher 		cmd->speed = -1;
1484527a6266SJeff Kirsher 		break;
1485527a6266SJeff Kirsher 	}
1486527a6266SJeff Kirsher 	cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
1487527a6266SJeff Kirsher 	cmd->port = PORT_MII;
1488527a6266SJeff Kirsher 	cmd->phy_address = 0;
1489527a6266SJeff Kirsher 	cmd->transceiver = XCVR_INTERNAL;
1490527a6266SJeff Kirsher 	cmd->autoneg = AUTONEG_DISABLE;
1491527a6266SJeff Kirsher 	cmd->maxtxpkt = 1;
1492527a6266SJeff Kirsher 	cmd->maxrxpkt = 1;
1493527a6266SJeff Kirsher 
1494527a6266SJeff Kirsher 	return 0;
1495527a6266SJeff Kirsher }
1496527a6266SJeff Kirsher 
1497527a6266SJeff Kirsher static int
1498527a6266SJeff Kirsher mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1499527a6266SJeff Kirsher {
1500527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1501527a6266SJeff Kirsher 
1502527a6266SJeff Kirsher 	if (mp->phy != NULL)
1503527a6266SJeff Kirsher 		return mv643xx_eth_get_settings_phy(mp, cmd);
1504527a6266SJeff Kirsher 	else
1505527a6266SJeff Kirsher 		return mv643xx_eth_get_settings_phyless(mp, cmd);
1506527a6266SJeff Kirsher }
1507527a6266SJeff Kirsher 
1508527a6266SJeff Kirsher static int
1509527a6266SJeff Kirsher mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1510527a6266SJeff Kirsher {
1511527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1512527a6266SJeff Kirsher 
1513527a6266SJeff Kirsher 	if (mp->phy == NULL)
1514527a6266SJeff Kirsher 		return -EINVAL;
1515527a6266SJeff Kirsher 
1516527a6266SJeff Kirsher 	/*
1517527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1518527a6266SJeff Kirsher 	 */
1519527a6266SJeff Kirsher 	cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1520527a6266SJeff Kirsher 
1521527a6266SJeff Kirsher 	return phy_ethtool_sset(mp->phy, cmd);
1522527a6266SJeff Kirsher }
1523527a6266SJeff Kirsher 
1524527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1525527a6266SJeff Kirsher 				    struct ethtool_drvinfo *drvinfo)
1526527a6266SJeff Kirsher {
15276f39da2cSAxel Lin 	strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
15286f39da2cSAxel Lin 		sizeof(drvinfo->driver));
152968aad78cSRick Jones 	strlcpy(drvinfo->version, mv643xx_eth_driver_version,
15306f39da2cSAxel Lin 		sizeof(drvinfo->version));
15316f39da2cSAxel Lin 	strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
15326f39da2cSAxel Lin 	strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
1533527a6266SJeff Kirsher 	drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
1534527a6266SJeff Kirsher }
1535527a6266SJeff Kirsher 
1536527a6266SJeff Kirsher static int mv643xx_eth_nway_reset(struct net_device *dev)
1537527a6266SJeff Kirsher {
1538527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1539527a6266SJeff Kirsher 
1540527a6266SJeff Kirsher 	if (mp->phy == NULL)
1541527a6266SJeff Kirsher 		return -EINVAL;
1542527a6266SJeff Kirsher 
1543527a6266SJeff Kirsher 	return genphy_restart_aneg(mp->phy);
1544527a6266SJeff Kirsher }
1545527a6266SJeff Kirsher 
1546527a6266SJeff Kirsher static int
1547527a6266SJeff Kirsher mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1548527a6266SJeff Kirsher {
1549527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1550527a6266SJeff Kirsher 
1551527a6266SJeff Kirsher 	ec->rx_coalesce_usecs = get_rx_coal(mp);
1552527a6266SJeff Kirsher 	ec->tx_coalesce_usecs = get_tx_coal(mp);
1553527a6266SJeff Kirsher 
1554527a6266SJeff Kirsher 	return 0;
1555527a6266SJeff Kirsher }
1556527a6266SJeff Kirsher 
1557527a6266SJeff Kirsher static int
1558527a6266SJeff Kirsher mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1559527a6266SJeff Kirsher {
1560527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1561527a6266SJeff Kirsher 
1562527a6266SJeff Kirsher 	set_rx_coal(mp, ec->rx_coalesce_usecs);
1563527a6266SJeff Kirsher 	set_tx_coal(mp, ec->tx_coalesce_usecs);
1564527a6266SJeff Kirsher 
1565527a6266SJeff Kirsher 	return 0;
1566527a6266SJeff Kirsher }
1567527a6266SJeff Kirsher 
1568527a6266SJeff Kirsher static void
1569527a6266SJeff Kirsher mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1570527a6266SJeff Kirsher {
1571527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1572527a6266SJeff Kirsher 
1573527a6266SJeff Kirsher 	er->rx_max_pending = 4096;
1574527a6266SJeff Kirsher 	er->tx_max_pending = 4096;
1575527a6266SJeff Kirsher 
1576527a6266SJeff Kirsher 	er->rx_pending = mp->rx_ring_size;
1577527a6266SJeff Kirsher 	er->tx_pending = mp->tx_ring_size;
1578527a6266SJeff Kirsher }
1579527a6266SJeff Kirsher 
1580527a6266SJeff Kirsher static int
1581527a6266SJeff Kirsher mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1582527a6266SJeff Kirsher {
1583527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1584527a6266SJeff Kirsher 
1585527a6266SJeff Kirsher 	if (er->rx_mini_pending || er->rx_jumbo_pending)
1586527a6266SJeff Kirsher 		return -EINVAL;
1587527a6266SJeff Kirsher 
1588527a6266SJeff Kirsher 	mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
1589527a6266SJeff Kirsher 	mp->tx_ring_size = er->tx_pending < 4096 ? er->tx_pending : 4096;
1590527a6266SJeff Kirsher 
1591527a6266SJeff Kirsher 	if (netif_running(dev)) {
1592527a6266SJeff Kirsher 		mv643xx_eth_stop(dev);
1593527a6266SJeff Kirsher 		if (mv643xx_eth_open(dev)) {
1594527a6266SJeff Kirsher 			netdev_err(dev,
1595527a6266SJeff Kirsher 				   "fatal error on re-opening device after ring param change\n");
1596527a6266SJeff Kirsher 			return -ENOMEM;
1597527a6266SJeff Kirsher 		}
1598527a6266SJeff Kirsher 	}
1599527a6266SJeff Kirsher 
1600527a6266SJeff Kirsher 	return 0;
1601527a6266SJeff Kirsher }
1602527a6266SJeff Kirsher 
1603527a6266SJeff Kirsher 
1604527a6266SJeff Kirsher static int
1605c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
1606527a6266SJeff Kirsher {
1607527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
16083ad9b358SMichał Mirosław 	bool rx_csum = features & NETIF_F_RXCSUM;
1609527a6266SJeff Kirsher 
1610527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1611527a6266SJeff Kirsher 
1612527a6266SJeff Kirsher 	return 0;
1613527a6266SJeff Kirsher }
1614527a6266SJeff Kirsher 
1615527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev,
1616527a6266SJeff Kirsher 				    uint32_t stringset, uint8_t *data)
1617527a6266SJeff Kirsher {
1618527a6266SJeff Kirsher 	int i;
1619527a6266SJeff Kirsher 
1620527a6266SJeff Kirsher 	if (stringset == ETH_SS_STATS) {
1621527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1622527a6266SJeff Kirsher 			memcpy(data + i * ETH_GSTRING_LEN,
1623527a6266SJeff Kirsher 				mv643xx_eth_stats[i].stat_string,
1624527a6266SJeff Kirsher 				ETH_GSTRING_LEN);
1625527a6266SJeff Kirsher 		}
1626527a6266SJeff Kirsher 	}
1627527a6266SJeff Kirsher }
1628527a6266SJeff Kirsher 
1629527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1630527a6266SJeff Kirsher 					  struct ethtool_stats *stats,
1631527a6266SJeff Kirsher 					  uint64_t *data)
1632527a6266SJeff Kirsher {
1633527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1634527a6266SJeff Kirsher 	int i;
1635527a6266SJeff Kirsher 
1636527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
1637527a6266SJeff Kirsher 	mib_counters_update(mp);
1638527a6266SJeff Kirsher 	mv643xx_eth_grab_lro_stats(mp);
1639527a6266SJeff Kirsher 
1640527a6266SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1641527a6266SJeff Kirsher 		const struct mv643xx_eth_stats *stat;
1642527a6266SJeff Kirsher 		void *p;
1643527a6266SJeff Kirsher 
1644527a6266SJeff Kirsher 		stat = mv643xx_eth_stats + i;
1645527a6266SJeff Kirsher 
1646527a6266SJeff Kirsher 		if (stat->netdev_off >= 0)
1647527a6266SJeff Kirsher 			p = ((void *)mp->dev) + stat->netdev_off;
1648527a6266SJeff Kirsher 		else
1649527a6266SJeff Kirsher 			p = ((void *)mp) + stat->mp_off;
1650527a6266SJeff Kirsher 
1651527a6266SJeff Kirsher 		data[i] = (stat->sizeof_stat == 8) ?
1652527a6266SJeff Kirsher 				*(uint64_t *)p : *(uint32_t *)p;
1653527a6266SJeff Kirsher 	}
1654527a6266SJeff Kirsher }
1655527a6266SJeff Kirsher 
1656527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
1657527a6266SJeff Kirsher {
1658527a6266SJeff Kirsher 	if (sset == ETH_SS_STATS)
1659527a6266SJeff Kirsher 		return ARRAY_SIZE(mv643xx_eth_stats);
1660527a6266SJeff Kirsher 
1661527a6266SJeff Kirsher 	return -EOPNOTSUPP;
1662527a6266SJeff Kirsher }
1663527a6266SJeff Kirsher 
1664527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1665527a6266SJeff Kirsher 	.get_settings		= mv643xx_eth_get_settings,
1666527a6266SJeff Kirsher 	.set_settings		= mv643xx_eth_set_settings,
1667527a6266SJeff Kirsher 	.get_drvinfo		= mv643xx_eth_get_drvinfo,
1668527a6266SJeff Kirsher 	.nway_reset		= mv643xx_eth_nway_reset,
1669527a6266SJeff Kirsher 	.get_link		= ethtool_op_get_link,
1670527a6266SJeff Kirsher 	.get_coalesce		= mv643xx_eth_get_coalesce,
1671527a6266SJeff Kirsher 	.set_coalesce		= mv643xx_eth_set_coalesce,
1672527a6266SJeff Kirsher 	.get_ringparam		= mv643xx_eth_get_ringparam,
1673527a6266SJeff Kirsher 	.set_ringparam		= mv643xx_eth_set_ringparam,
1674527a6266SJeff Kirsher 	.get_strings		= mv643xx_eth_get_strings,
1675527a6266SJeff Kirsher 	.get_ethtool_stats	= mv643xx_eth_get_ethtool_stats,
1676527a6266SJeff Kirsher 	.get_sset_count		= mv643xx_eth_get_sset_count,
1677ebad0a8dSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
1678527a6266SJeff Kirsher };
1679527a6266SJeff Kirsher 
1680527a6266SJeff Kirsher 
1681527a6266SJeff Kirsher /* address handling *********************************************************/
1682527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
1683527a6266SJeff Kirsher {
1684527a6266SJeff Kirsher 	unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1685527a6266SJeff Kirsher 	unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
1686527a6266SJeff Kirsher 
1687527a6266SJeff Kirsher 	addr[0] = (mac_h >> 24) & 0xff;
1688527a6266SJeff Kirsher 	addr[1] = (mac_h >> 16) & 0xff;
1689527a6266SJeff Kirsher 	addr[2] = (mac_h >> 8) & 0xff;
1690527a6266SJeff Kirsher 	addr[3] = mac_h & 0xff;
1691527a6266SJeff Kirsher 	addr[4] = (mac_l >> 8) & 0xff;
1692527a6266SJeff Kirsher 	addr[5] = mac_l & 0xff;
1693527a6266SJeff Kirsher }
1694527a6266SJeff Kirsher 
1695527a6266SJeff Kirsher static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
1696527a6266SJeff Kirsher {
1697527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_HIGH,
1698527a6266SJeff Kirsher 		(addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1699527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
1700527a6266SJeff Kirsher }
1701527a6266SJeff Kirsher 
1702527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev)
1703527a6266SJeff Kirsher {
1704527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1705527a6266SJeff Kirsher 	u32 nibbles;
1706527a6266SJeff Kirsher 
1707527a6266SJeff Kirsher 	if (dev->flags & IFF_PROMISC)
1708527a6266SJeff Kirsher 		return 0;
1709527a6266SJeff Kirsher 
1710527a6266SJeff Kirsher 	nibbles = 1 << (dev->dev_addr[5] & 0x0f);
1711527a6266SJeff Kirsher 	netdev_for_each_uc_addr(ha, dev) {
1712527a6266SJeff Kirsher 		if (memcmp(dev->dev_addr, ha->addr, 5))
1713527a6266SJeff Kirsher 			return 0;
1714527a6266SJeff Kirsher 		if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
1715527a6266SJeff Kirsher 			return 0;
1716527a6266SJeff Kirsher 
1717527a6266SJeff Kirsher 		nibbles |= 1 << (ha->addr[5] & 0x0f);
1718527a6266SJeff Kirsher 	}
1719527a6266SJeff Kirsher 
1720527a6266SJeff Kirsher 	return nibbles;
1721527a6266SJeff Kirsher }
1722527a6266SJeff Kirsher 
1723527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
1724527a6266SJeff Kirsher {
1725527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1726527a6266SJeff Kirsher 	u32 port_config;
1727527a6266SJeff Kirsher 	u32 nibbles;
1728527a6266SJeff Kirsher 	int i;
1729527a6266SJeff Kirsher 
1730527a6266SJeff Kirsher 	uc_addr_set(mp, dev->dev_addr);
1731527a6266SJeff Kirsher 
1732527a6266SJeff Kirsher 	port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1733527a6266SJeff Kirsher 
1734527a6266SJeff Kirsher 	nibbles = uc_addr_filter_mask(dev);
1735527a6266SJeff Kirsher 	if (!nibbles) {
1736527a6266SJeff Kirsher 		port_config |= UNICAST_PROMISCUOUS_MODE;
1737527a6266SJeff Kirsher 		nibbles = 0xffff;
1738527a6266SJeff Kirsher 	}
1739527a6266SJeff Kirsher 
1740527a6266SJeff Kirsher 	for (i = 0; i < 16; i += 4) {
1741527a6266SJeff Kirsher 		int off = UNICAST_TABLE(mp->port_num) + i;
1742527a6266SJeff Kirsher 		u32 v;
1743527a6266SJeff Kirsher 
1744527a6266SJeff Kirsher 		v = 0;
1745527a6266SJeff Kirsher 		if (nibbles & 1)
1746527a6266SJeff Kirsher 			v |= 0x00000001;
1747527a6266SJeff Kirsher 		if (nibbles & 2)
1748527a6266SJeff Kirsher 			v |= 0x00000100;
1749527a6266SJeff Kirsher 		if (nibbles & 4)
1750527a6266SJeff Kirsher 			v |= 0x00010000;
1751527a6266SJeff Kirsher 		if (nibbles & 8)
1752527a6266SJeff Kirsher 			v |= 0x01000000;
1753527a6266SJeff Kirsher 		nibbles >>= 4;
1754527a6266SJeff Kirsher 
1755527a6266SJeff Kirsher 		wrl(mp, off, v);
1756527a6266SJeff Kirsher 	}
1757527a6266SJeff Kirsher 
1758527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, port_config);
1759527a6266SJeff Kirsher }
1760527a6266SJeff Kirsher 
1761527a6266SJeff Kirsher static int addr_crc(unsigned char *addr)
1762527a6266SJeff Kirsher {
1763527a6266SJeff Kirsher 	int crc = 0;
1764527a6266SJeff Kirsher 	int i;
1765527a6266SJeff Kirsher 
1766527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
1767527a6266SJeff Kirsher 		int j;
1768527a6266SJeff Kirsher 
1769527a6266SJeff Kirsher 		crc = (crc ^ addr[i]) << 8;
1770527a6266SJeff Kirsher 		for (j = 7; j >= 0; j--) {
1771527a6266SJeff Kirsher 			if (crc & (0x100 << j))
1772527a6266SJeff Kirsher 				crc ^= 0x107 << j;
1773527a6266SJeff Kirsher 		}
1774527a6266SJeff Kirsher 	}
1775527a6266SJeff Kirsher 
1776527a6266SJeff Kirsher 	return crc;
1777527a6266SJeff Kirsher }
1778527a6266SJeff Kirsher 
1779527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
1780527a6266SJeff Kirsher {
1781527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1782527a6266SJeff Kirsher 	u32 *mc_spec;
1783527a6266SJeff Kirsher 	u32 *mc_other;
1784527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1785527a6266SJeff Kirsher 	int i;
1786527a6266SJeff Kirsher 
1787527a6266SJeff Kirsher 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1788527a6266SJeff Kirsher 		int port_num;
1789527a6266SJeff Kirsher 		u32 accept;
1790527a6266SJeff Kirsher 
1791527a6266SJeff Kirsher oom:
1792527a6266SJeff Kirsher 		port_num = mp->port_num;
1793527a6266SJeff Kirsher 		accept = 0x01010101;
1794527a6266SJeff Kirsher 		for (i = 0; i < 0x100; i += 4) {
1795527a6266SJeff Kirsher 			wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1796527a6266SJeff Kirsher 			wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
1797527a6266SJeff Kirsher 		}
1798527a6266SJeff Kirsher 		return;
1799527a6266SJeff Kirsher 	}
1800527a6266SJeff Kirsher 
1801527a6266SJeff Kirsher 	mc_spec = kmalloc(0x200, GFP_ATOMIC);
1802527a6266SJeff Kirsher 	if (mc_spec == NULL)
1803527a6266SJeff Kirsher 		goto oom;
1804527a6266SJeff Kirsher 	mc_other = mc_spec + (0x100 >> 2);
1805527a6266SJeff Kirsher 
1806527a6266SJeff Kirsher 	memset(mc_spec, 0, 0x100);
1807527a6266SJeff Kirsher 	memset(mc_other, 0, 0x100);
1808527a6266SJeff Kirsher 
1809527a6266SJeff Kirsher 	netdev_for_each_mc_addr(ha, dev) {
1810527a6266SJeff Kirsher 		u8 *a = ha->addr;
1811527a6266SJeff Kirsher 		u32 *table;
1812527a6266SJeff Kirsher 		int entry;
1813527a6266SJeff Kirsher 
1814527a6266SJeff Kirsher 		if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1815527a6266SJeff Kirsher 			table = mc_spec;
1816527a6266SJeff Kirsher 			entry = a[5];
1817527a6266SJeff Kirsher 		} else {
1818527a6266SJeff Kirsher 			table = mc_other;
1819527a6266SJeff Kirsher 			entry = addr_crc(a);
1820527a6266SJeff Kirsher 		}
1821527a6266SJeff Kirsher 
1822527a6266SJeff Kirsher 		table[entry >> 2] |= 1 << (8 * (entry & 3));
1823527a6266SJeff Kirsher 	}
1824527a6266SJeff Kirsher 
1825527a6266SJeff Kirsher 	for (i = 0; i < 0x100; i += 4) {
1826527a6266SJeff Kirsher 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
1827527a6266SJeff Kirsher 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
1828527a6266SJeff Kirsher 	}
1829527a6266SJeff Kirsher 
1830527a6266SJeff Kirsher 	kfree(mc_spec);
1831527a6266SJeff Kirsher }
1832527a6266SJeff Kirsher 
1833527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1834527a6266SJeff Kirsher {
1835527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1836527a6266SJeff Kirsher 	mv643xx_eth_program_multicast_filter(dev);
1837527a6266SJeff Kirsher }
1838527a6266SJeff Kirsher 
1839527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1840527a6266SJeff Kirsher {
1841527a6266SJeff Kirsher 	struct sockaddr *sa = addr;
1842527a6266SJeff Kirsher 
1843527a6266SJeff Kirsher 	if (!is_valid_ether_addr(sa->sa_data))
1844504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
1845527a6266SJeff Kirsher 
1846527a6266SJeff Kirsher 	memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1847527a6266SJeff Kirsher 
1848527a6266SJeff Kirsher 	netif_addr_lock_bh(dev);
1849527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1850527a6266SJeff Kirsher 	netif_addr_unlock_bh(dev);
1851527a6266SJeff Kirsher 
1852527a6266SJeff Kirsher 	return 0;
1853527a6266SJeff Kirsher }
1854527a6266SJeff Kirsher 
1855527a6266SJeff Kirsher 
1856527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/
1857527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index)
1858527a6266SJeff Kirsher {
1859527a6266SJeff Kirsher 	struct rx_queue *rxq = mp->rxq + index;
1860527a6266SJeff Kirsher 	struct rx_desc *rx_desc;
1861527a6266SJeff Kirsher 	int size;
1862527a6266SJeff Kirsher 	int i;
1863527a6266SJeff Kirsher 
1864527a6266SJeff Kirsher 	rxq->index = index;
1865527a6266SJeff Kirsher 
1866527a6266SJeff Kirsher 	rxq->rx_ring_size = mp->rx_ring_size;
1867527a6266SJeff Kirsher 
1868527a6266SJeff Kirsher 	rxq->rx_desc_count = 0;
1869527a6266SJeff Kirsher 	rxq->rx_curr_desc = 0;
1870527a6266SJeff Kirsher 	rxq->rx_used_desc = 0;
1871527a6266SJeff Kirsher 
1872527a6266SJeff Kirsher 	size = rxq->rx_ring_size * sizeof(struct rx_desc);
1873527a6266SJeff Kirsher 
1874527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size) {
1875527a6266SJeff Kirsher 		rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1876527a6266SJeff Kirsher 						mp->rx_desc_sram_size);
1877527a6266SJeff Kirsher 		rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1878527a6266SJeff Kirsher 	} else {
1879527a6266SJeff Kirsher 		rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1880527a6266SJeff Kirsher 						       size, &rxq->rx_desc_dma,
1881527a6266SJeff Kirsher 						       GFP_KERNEL);
1882527a6266SJeff Kirsher 	}
1883527a6266SJeff Kirsher 
1884527a6266SJeff Kirsher 	if (rxq->rx_desc_area == NULL) {
1885527a6266SJeff Kirsher 		netdev_err(mp->dev,
1886527a6266SJeff Kirsher 			   "can't allocate rx ring (%d bytes)\n", size);
1887527a6266SJeff Kirsher 		goto out;
1888527a6266SJeff Kirsher 	}
1889527a6266SJeff Kirsher 	memset(rxq->rx_desc_area, 0, size);
1890527a6266SJeff Kirsher 
1891527a6266SJeff Kirsher 	rxq->rx_desc_area_size = size;
1892527a6266SJeff Kirsher 	rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
1893527a6266SJeff Kirsher 								GFP_KERNEL);
1894527a6266SJeff Kirsher 	if (rxq->rx_skb == NULL) {
1895527a6266SJeff Kirsher 		netdev_err(mp->dev, "can't allocate rx skb ring\n");
1896527a6266SJeff Kirsher 		goto out_free;
1897527a6266SJeff Kirsher 	}
1898527a6266SJeff Kirsher 
1899527a6266SJeff Kirsher 	rx_desc = (struct rx_desc *)rxq->rx_desc_area;
1900527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1901527a6266SJeff Kirsher 		int nexti;
1902527a6266SJeff Kirsher 
1903527a6266SJeff Kirsher 		nexti = i + 1;
1904527a6266SJeff Kirsher 		if (nexti == rxq->rx_ring_size)
1905527a6266SJeff Kirsher 			nexti = 0;
1906527a6266SJeff Kirsher 
1907527a6266SJeff Kirsher 		rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1908527a6266SJeff Kirsher 					nexti * sizeof(struct rx_desc);
1909527a6266SJeff Kirsher 	}
1910527a6266SJeff Kirsher 
1911527a6266SJeff Kirsher 	rxq->lro_mgr.dev = mp->dev;
1912527a6266SJeff Kirsher 	memset(&rxq->lro_mgr.stats, 0, sizeof(rxq->lro_mgr.stats));
1913527a6266SJeff Kirsher 	rxq->lro_mgr.features = LRO_F_NAPI;
1914527a6266SJeff Kirsher 	rxq->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1915527a6266SJeff Kirsher 	rxq->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1916527a6266SJeff Kirsher 	rxq->lro_mgr.max_desc = ARRAY_SIZE(rxq->lro_arr);
1917527a6266SJeff Kirsher 	rxq->lro_mgr.max_aggr = 32;
1918527a6266SJeff Kirsher 	rxq->lro_mgr.frag_align_pad = 0;
1919527a6266SJeff Kirsher 	rxq->lro_mgr.lro_arr = rxq->lro_arr;
1920527a6266SJeff Kirsher 	rxq->lro_mgr.get_skb_header = mv643xx_get_skb_header;
1921527a6266SJeff Kirsher 
1922527a6266SJeff Kirsher 	memset(&rxq->lro_arr, 0, sizeof(rxq->lro_arr));
1923527a6266SJeff Kirsher 
1924527a6266SJeff Kirsher 	return 0;
1925527a6266SJeff Kirsher 
1926527a6266SJeff Kirsher 
1927527a6266SJeff Kirsher out_free:
1928527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size)
1929527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1930527a6266SJeff Kirsher 	else
1931527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, size,
1932527a6266SJeff Kirsher 				  rxq->rx_desc_area,
1933527a6266SJeff Kirsher 				  rxq->rx_desc_dma);
1934527a6266SJeff Kirsher 
1935527a6266SJeff Kirsher out:
1936527a6266SJeff Kirsher 	return -ENOMEM;
1937527a6266SJeff Kirsher }
1938527a6266SJeff Kirsher 
1939527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq)
1940527a6266SJeff Kirsher {
1941527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1942527a6266SJeff Kirsher 	int i;
1943527a6266SJeff Kirsher 
1944527a6266SJeff Kirsher 	rxq_disable(rxq);
1945527a6266SJeff Kirsher 
1946527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1947527a6266SJeff Kirsher 		if (rxq->rx_skb[i]) {
1948527a6266SJeff Kirsher 			dev_kfree_skb(rxq->rx_skb[i]);
1949527a6266SJeff Kirsher 			rxq->rx_desc_count--;
1950527a6266SJeff Kirsher 		}
1951527a6266SJeff Kirsher 	}
1952527a6266SJeff Kirsher 
1953527a6266SJeff Kirsher 	if (rxq->rx_desc_count) {
1954527a6266SJeff Kirsher 		netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
1955527a6266SJeff Kirsher 			   rxq->rx_desc_count);
1956527a6266SJeff Kirsher 	}
1957527a6266SJeff Kirsher 
1958527a6266SJeff Kirsher 	if (rxq->index == 0 &&
1959527a6266SJeff Kirsher 	    rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
1960527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1961527a6266SJeff Kirsher 	else
1962527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
1963527a6266SJeff Kirsher 				  rxq->rx_desc_area, rxq->rx_desc_dma);
1964527a6266SJeff Kirsher 
1965527a6266SJeff Kirsher 	kfree(rxq->rx_skb);
1966527a6266SJeff Kirsher }
1967527a6266SJeff Kirsher 
1968527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index)
1969527a6266SJeff Kirsher {
1970527a6266SJeff Kirsher 	struct tx_queue *txq = mp->txq + index;
1971527a6266SJeff Kirsher 	struct tx_desc *tx_desc;
1972527a6266SJeff Kirsher 	int size;
1973527a6266SJeff Kirsher 	int i;
1974527a6266SJeff Kirsher 
1975527a6266SJeff Kirsher 	txq->index = index;
1976527a6266SJeff Kirsher 
1977527a6266SJeff Kirsher 	txq->tx_ring_size = mp->tx_ring_size;
1978527a6266SJeff Kirsher 
1979527a6266SJeff Kirsher 	txq->tx_desc_count = 0;
1980527a6266SJeff Kirsher 	txq->tx_curr_desc = 0;
1981527a6266SJeff Kirsher 	txq->tx_used_desc = 0;
1982527a6266SJeff Kirsher 
1983527a6266SJeff Kirsher 	size = txq->tx_ring_size * sizeof(struct tx_desc);
1984527a6266SJeff Kirsher 
1985527a6266SJeff Kirsher 	if (index == 0 && size <= mp->tx_desc_sram_size) {
1986527a6266SJeff Kirsher 		txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1987527a6266SJeff Kirsher 						mp->tx_desc_sram_size);
1988527a6266SJeff Kirsher 		txq->tx_desc_dma = mp->tx_desc_sram_addr;
1989527a6266SJeff Kirsher 	} else {
1990527a6266SJeff Kirsher 		txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1991527a6266SJeff Kirsher 						       size, &txq->tx_desc_dma,
1992527a6266SJeff Kirsher 						       GFP_KERNEL);
1993527a6266SJeff Kirsher 	}
1994527a6266SJeff Kirsher 
1995527a6266SJeff Kirsher 	if (txq->tx_desc_area == NULL) {
1996527a6266SJeff Kirsher 		netdev_err(mp->dev,
1997527a6266SJeff Kirsher 			   "can't allocate tx ring (%d bytes)\n", size);
1998527a6266SJeff Kirsher 		return -ENOMEM;
1999527a6266SJeff Kirsher 	}
2000527a6266SJeff Kirsher 	memset(txq->tx_desc_area, 0, size);
2001527a6266SJeff Kirsher 
2002527a6266SJeff Kirsher 	txq->tx_desc_area_size = size;
2003527a6266SJeff Kirsher 
2004527a6266SJeff Kirsher 	tx_desc = (struct tx_desc *)txq->tx_desc_area;
2005527a6266SJeff Kirsher 	for (i = 0; i < txq->tx_ring_size; i++) {
2006527a6266SJeff Kirsher 		struct tx_desc *txd = tx_desc + i;
2007527a6266SJeff Kirsher 		int nexti;
2008527a6266SJeff Kirsher 
2009527a6266SJeff Kirsher 		nexti = i + 1;
2010527a6266SJeff Kirsher 		if (nexti == txq->tx_ring_size)
2011527a6266SJeff Kirsher 			nexti = 0;
2012527a6266SJeff Kirsher 
2013527a6266SJeff Kirsher 		txd->cmd_sts = 0;
2014527a6266SJeff Kirsher 		txd->next_desc_ptr = txq->tx_desc_dma +
2015527a6266SJeff Kirsher 					nexti * sizeof(struct tx_desc);
2016527a6266SJeff Kirsher 	}
2017527a6266SJeff Kirsher 
2018527a6266SJeff Kirsher 	skb_queue_head_init(&txq->tx_skb);
2019527a6266SJeff Kirsher 
2020527a6266SJeff Kirsher 	return 0;
2021527a6266SJeff Kirsher }
2022527a6266SJeff Kirsher 
2023527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq)
2024527a6266SJeff Kirsher {
2025527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
2026527a6266SJeff Kirsher 
2027527a6266SJeff Kirsher 	txq_disable(txq);
2028527a6266SJeff Kirsher 	txq_reclaim(txq, txq->tx_ring_size, 1);
2029527a6266SJeff Kirsher 
2030527a6266SJeff Kirsher 	BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
2031527a6266SJeff Kirsher 
2032527a6266SJeff Kirsher 	if (txq->index == 0 &&
2033527a6266SJeff Kirsher 	    txq->tx_desc_area_size <= mp->tx_desc_sram_size)
2034527a6266SJeff Kirsher 		iounmap(txq->tx_desc_area);
2035527a6266SJeff Kirsher 	else
2036527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
2037527a6266SJeff Kirsher 				  txq->tx_desc_area, txq->tx_desc_dma);
2038527a6266SJeff Kirsher }
2039527a6266SJeff Kirsher 
2040527a6266SJeff Kirsher 
2041527a6266SJeff Kirsher /* netdev ops and related ***************************************************/
2042527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
2043527a6266SJeff Kirsher {
2044527a6266SJeff Kirsher 	u32 int_cause;
2045527a6266SJeff Kirsher 	u32 int_cause_ext;
2046527a6266SJeff Kirsher 
2047527a6266SJeff Kirsher 	int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
2048527a6266SJeff Kirsher 	if (int_cause == 0)
2049527a6266SJeff Kirsher 		return 0;
2050527a6266SJeff Kirsher 
2051527a6266SJeff Kirsher 	int_cause_ext = 0;
2052527a6266SJeff Kirsher 	if (int_cause & INT_EXT) {
2053527a6266SJeff Kirsher 		int_cause &= ~INT_EXT;
2054527a6266SJeff Kirsher 		int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
2055527a6266SJeff Kirsher 	}
2056527a6266SJeff Kirsher 
2057527a6266SJeff Kirsher 	if (int_cause) {
2058527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE, ~int_cause);
2059527a6266SJeff Kirsher 		mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
2060527a6266SJeff Kirsher 				~(rdlp(mp, TXQ_COMMAND) & 0xff);
2061527a6266SJeff Kirsher 		mp->work_rx |= (int_cause & INT_RX) >> 2;
2062527a6266SJeff Kirsher 	}
2063527a6266SJeff Kirsher 
2064527a6266SJeff Kirsher 	int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
2065527a6266SJeff Kirsher 	if (int_cause_ext) {
2066527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
2067527a6266SJeff Kirsher 		if (int_cause_ext & INT_EXT_LINK_PHY)
2068527a6266SJeff Kirsher 			mp->work_link = 1;
2069527a6266SJeff Kirsher 		mp->work_tx |= int_cause_ext & INT_EXT_TX;
2070527a6266SJeff Kirsher 	}
2071527a6266SJeff Kirsher 
2072527a6266SJeff Kirsher 	return 1;
2073527a6266SJeff Kirsher }
2074527a6266SJeff Kirsher 
2075527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
2076527a6266SJeff Kirsher {
2077527a6266SJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_id;
2078527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2079527a6266SJeff Kirsher 
2080527a6266SJeff Kirsher 	if (unlikely(!mv643xx_eth_collect_events(mp)))
2081527a6266SJeff Kirsher 		return IRQ_NONE;
2082527a6266SJeff Kirsher 
2083527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
2084527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2085527a6266SJeff Kirsher 
2086527a6266SJeff Kirsher 	return IRQ_HANDLED;
2087527a6266SJeff Kirsher }
2088527a6266SJeff Kirsher 
2089527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp)
2090527a6266SJeff Kirsher {
2091527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2092527a6266SJeff Kirsher 	u32 port_status;
2093527a6266SJeff Kirsher 	int speed;
2094527a6266SJeff Kirsher 	int duplex;
2095527a6266SJeff Kirsher 	int fc;
2096527a6266SJeff Kirsher 
2097527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
2098527a6266SJeff Kirsher 	if (!(port_status & LINK_UP)) {
2099527a6266SJeff Kirsher 		if (netif_carrier_ok(dev)) {
2100527a6266SJeff Kirsher 			int i;
2101527a6266SJeff Kirsher 
2102527a6266SJeff Kirsher 			netdev_info(dev, "link down\n");
2103527a6266SJeff Kirsher 
2104527a6266SJeff Kirsher 			netif_carrier_off(dev);
2105527a6266SJeff Kirsher 
2106527a6266SJeff Kirsher 			for (i = 0; i < mp->txq_count; i++) {
2107527a6266SJeff Kirsher 				struct tx_queue *txq = mp->txq + i;
2108527a6266SJeff Kirsher 
2109527a6266SJeff Kirsher 				txq_reclaim(txq, txq->tx_ring_size, 1);
2110527a6266SJeff Kirsher 				txq_reset_hw_ptr(txq);
2111527a6266SJeff Kirsher 			}
2112527a6266SJeff Kirsher 		}
2113527a6266SJeff Kirsher 		return;
2114527a6266SJeff Kirsher 	}
2115527a6266SJeff Kirsher 
2116527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
2117527a6266SJeff Kirsher 	case PORT_SPEED_10:
2118527a6266SJeff Kirsher 		speed = 10;
2119527a6266SJeff Kirsher 		break;
2120527a6266SJeff Kirsher 	case PORT_SPEED_100:
2121527a6266SJeff Kirsher 		speed = 100;
2122527a6266SJeff Kirsher 		break;
2123527a6266SJeff Kirsher 	case PORT_SPEED_1000:
2124527a6266SJeff Kirsher 		speed = 1000;
2125527a6266SJeff Kirsher 		break;
2126527a6266SJeff Kirsher 	default:
2127527a6266SJeff Kirsher 		speed = -1;
2128527a6266SJeff Kirsher 		break;
2129527a6266SJeff Kirsher 	}
2130527a6266SJeff Kirsher 	duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
2131527a6266SJeff Kirsher 	fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
2132527a6266SJeff Kirsher 
2133527a6266SJeff Kirsher 	netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
2134527a6266SJeff Kirsher 		    speed, duplex ? "full" : "half", fc ? "en" : "dis");
2135527a6266SJeff Kirsher 
2136527a6266SJeff Kirsher 	if (!netif_carrier_ok(dev))
2137527a6266SJeff Kirsher 		netif_carrier_on(dev);
2138527a6266SJeff Kirsher }
2139527a6266SJeff Kirsher 
2140527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
2141527a6266SJeff Kirsher {
2142527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2143527a6266SJeff Kirsher 	int work_done;
2144527a6266SJeff Kirsher 
2145527a6266SJeff Kirsher 	mp = container_of(napi, struct mv643xx_eth_private, napi);
2146527a6266SJeff Kirsher 
2147527a6266SJeff Kirsher 	if (unlikely(mp->oom)) {
2148527a6266SJeff Kirsher 		mp->oom = 0;
2149527a6266SJeff Kirsher 		del_timer(&mp->rx_oom);
2150527a6266SJeff Kirsher 	}
2151527a6266SJeff Kirsher 
2152527a6266SJeff Kirsher 	work_done = 0;
2153527a6266SJeff Kirsher 	while (work_done < budget) {
2154527a6266SJeff Kirsher 		u8 queue_mask;
2155527a6266SJeff Kirsher 		int queue;
2156527a6266SJeff Kirsher 		int work_tbd;
2157527a6266SJeff Kirsher 
2158527a6266SJeff Kirsher 		if (mp->work_link) {
2159527a6266SJeff Kirsher 			mp->work_link = 0;
2160527a6266SJeff Kirsher 			handle_link_event(mp);
2161527a6266SJeff Kirsher 			work_done++;
2162527a6266SJeff Kirsher 			continue;
2163527a6266SJeff Kirsher 		}
2164527a6266SJeff Kirsher 
2165527a6266SJeff Kirsher 		queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2166527a6266SJeff Kirsher 		if (likely(!mp->oom))
2167527a6266SJeff Kirsher 			queue_mask |= mp->work_rx_refill;
2168527a6266SJeff Kirsher 
2169527a6266SJeff Kirsher 		if (!queue_mask) {
2170527a6266SJeff Kirsher 			if (mv643xx_eth_collect_events(mp))
2171527a6266SJeff Kirsher 				continue;
2172527a6266SJeff Kirsher 			break;
2173527a6266SJeff Kirsher 		}
2174527a6266SJeff Kirsher 
2175527a6266SJeff Kirsher 		queue = fls(queue_mask) - 1;
2176527a6266SJeff Kirsher 		queue_mask = 1 << queue;
2177527a6266SJeff Kirsher 
2178527a6266SJeff Kirsher 		work_tbd = budget - work_done;
2179527a6266SJeff Kirsher 		if (work_tbd > 16)
2180527a6266SJeff Kirsher 			work_tbd = 16;
2181527a6266SJeff Kirsher 
2182527a6266SJeff Kirsher 		if (mp->work_tx_end & queue_mask) {
2183527a6266SJeff Kirsher 			txq_kick(mp->txq + queue);
2184527a6266SJeff Kirsher 		} else if (mp->work_tx & queue_mask) {
2185527a6266SJeff Kirsher 			work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2186527a6266SJeff Kirsher 			txq_maybe_wake(mp->txq + queue);
2187527a6266SJeff Kirsher 		} else if (mp->work_rx & queue_mask) {
2188527a6266SJeff Kirsher 			work_done += rxq_process(mp->rxq + queue, work_tbd);
2189527a6266SJeff Kirsher 		} else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
2190527a6266SJeff Kirsher 			work_done += rxq_refill(mp->rxq + queue, work_tbd);
2191527a6266SJeff Kirsher 		} else {
2192527a6266SJeff Kirsher 			BUG();
2193527a6266SJeff Kirsher 		}
2194527a6266SJeff Kirsher 	}
2195527a6266SJeff Kirsher 
2196527a6266SJeff Kirsher 	if (work_done < budget) {
2197527a6266SJeff Kirsher 		if (mp->oom)
2198527a6266SJeff Kirsher 			mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
2199527a6266SJeff Kirsher 		napi_complete(napi);
2200527a6266SJeff Kirsher 		wrlp(mp, INT_MASK, mp->int_mask);
2201527a6266SJeff Kirsher 	}
2202527a6266SJeff Kirsher 
2203527a6266SJeff Kirsher 	return work_done;
2204527a6266SJeff Kirsher }
2205527a6266SJeff Kirsher 
2206527a6266SJeff Kirsher static inline void oom_timer_wrapper(unsigned long data)
2207527a6266SJeff Kirsher {
2208527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = (void *)data;
2209527a6266SJeff Kirsher 
2210527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2211527a6266SJeff Kirsher }
2212527a6266SJeff Kirsher 
2213527a6266SJeff Kirsher static void phy_reset(struct mv643xx_eth_private *mp)
2214527a6266SJeff Kirsher {
2215527a6266SJeff Kirsher 	int data;
2216527a6266SJeff Kirsher 
2217527a6266SJeff Kirsher 	data = phy_read(mp->phy, MII_BMCR);
2218527a6266SJeff Kirsher 	if (data < 0)
2219527a6266SJeff Kirsher 		return;
2220527a6266SJeff Kirsher 
2221527a6266SJeff Kirsher 	data |= BMCR_RESET;
2222527a6266SJeff Kirsher 	if (phy_write(mp->phy, MII_BMCR, data) < 0)
2223527a6266SJeff Kirsher 		return;
2224527a6266SJeff Kirsher 
2225527a6266SJeff Kirsher 	do {
2226527a6266SJeff Kirsher 		data = phy_read(mp->phy, MII_BMCR);
2227527a6266SJeff Kirsher 	} while (data >= 0 && data & BMCR_RESET);
2228527a6266SJeff Kirsher }
2229527a6266SJeff Kirsher 
2230527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp)
2231527a6266SJeff Kirsher {
2232527a6266SJeff Kirsher 	u32 pscr;
2233527a6266SJeff Kirsher 	int i;
2234527a6266SJeff Kirsher 
2235527a6266SJeff Kirsher 	/*
2236527a6266SJeff Kirsher 	 * Perform PHY reset, if there is a PHY.
2237527a6266SJeff Kirsher 	 */
2238527a6266SJeff Kirsher 	if (mp->phy != NULL) {
2239527a6266SJeff Kirsher 		struct ethtool_cmd cmd;
2240527a6266SJeff Kirsher 
2241527a6266SJeff Kirsher 		mv643xx_eth_get_settings(mp->dev, &cmd);
2242527a6266SJeff Kirsher 		phy_reset(mp);
2243527a6266SJeff Kirsher 		mv643xx_eth_set_settings(mp->dev, &cmd);
2244527a6266SJeff Kirsher 	}
2245527a6266SJeff Kirsher 
2246527a6266SJeff Kirsher 	/*
2247527a6266SJeff Kirsher 	 * Configure basic link parameters.
2248527a6266SJeff Kirsher 	 */
2249527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2250527a6266SJeff Kirsher 
2251527a6266SJeff Kirsher 	pscr |= SERIAL_PORT_ENABLE;
2252527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2253527a6266SJeff Kirsher 
2254527a6266SJeff Kirsher 	pscr |= DO_NOT_FORCE_LINK_FAIL;
2255527a6266SJeff Kirsher 	if (mp->phy == NULL)
2256527a6266SJeff Kirsher 		pscr |= FORCE_LINK_PASS;
2257527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2258527a6266SJeff Kirsher 
2259527a6266SJeff Kirsher 	/*
2260527a6266SJeff Kirsher 	 * Configure TX path and queues.
2261527a6266SJeff Kirsher 	 */
2262527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2263527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2264527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
2265527a6266SJeff Kirsher 
2266527a6266SJeff Kirsher 		txq_reset_hw_ptr(txq);
2267527a6266SJeff Kirsher 		txq_set_rate(txq, 1000000000, 16777216);
2268527a6266SJeff Kirsher 		txq_set_fixed_prio_mode(txq);
2269527a6266SJeff Kirsher 	}
2270527a6266SJeff Kirsher 
2271527a6266SJeff Kirsher 	/*
2272527a6266SJeff Kirsher 	 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2273527a6266SJeff Kirsher 	 * frames to RX queue #0, and include the pseudo-header when
2274527a6266SJeff Kirsher 	 * calculating receive checksums.
2275527a6266SJeff Kirsher 	 */
2276527a6266SJeff Kirsher 	mv643xx_eth_set_features(mp->dev, mp->dev->features);
2277527a6266SJeff Kirsher 
2278527a6266SJeff Kirsher 	/*
2279527a6266SJeff Kirsher 	 * Treat BPDUs as normal multicasts, and disable partition mode.
2280527a6266SJeff Kirsher 	 */
2281527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
2282527a6266SJeff Kirsher 
2283527a6266SJeff Kirsher 	/*
2284527a6266SJeff Kirsher 	 * Add configured unicast addresses to address filter table.
2285527a6266SJeff Kirsher 	 */
2286527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(mp->dev);
2287527a6266SJeff Kirsher 
2288527a6266SJeff Kirsher 	/*
2289527a6266SJeff Kirsher 	 * Enable the receive queues.
2290527a6266SJeff Kirsher 	 */
2291527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2292527a6266SJeff Kirsher 		struct rx_queue *rxq = mp->rxq + i;
2293527a6266SJeff Kirsher 		u32 addr;
2294527a6266SJeff Kirsher 
2295527a6266SJeff Kirsher 		addr = (u32)rxq->rx_desc_dma;
2296527a6266SJeff Kirsher 		addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
2297527a6266SJeff Kirsher 		wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
2298527a6266SJeff Kirsher 
2299527a6266SJeff Kirsher 		rxq_enable(rxq);
2300527a6266SJeff Kirsher 	}
2301527a6266SJeff Kirsher }
2302527a6266SJeff Kirsher 
2303527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2304527a6266SJeff Kirsher {
2305527a6266SJeff Kirsher 	int skb_size;
2306527a6266SJeff Kirsher 
2307527a6266SJeff Kirsher 	/*
2308527a6266SJeff Kirsher 	 * Reserve 2+14 bytes for an ethernet header (the hardware
2309527a6266SJeff Kirsher 	 * automatically prepends 2 bytes of dummy data to each
2310527a6266SJeff Kirsher 	 * received packet), 16 bytes for up to four VLAN tags, and
2311527a6266SJeff Kirsher 	 * 4 bytes for the trailing FCS -- 36 bytes total.
2312527a6266SJeff Kirsher 	 */
2313527a6266SJeff Kirsher 	skb_size = mp->dev->mtu + 36;
2314527a6266SJeff Kirsher 
2315527a6266SJeff Kirsher 	/*
2316527a6266SJeff Kirsher 	 * Make sure that the skb size is a multiple of 8 bytes, as
2317527a6266SJeff Kirsher 	 * the lower three bits of the receive descriptor's buffer
2318527a6266SJeff Kirsher 	 * size field are ignored by the hardware.
2319527a6266SJeff Kirsher 	 */
2320527a6266SJeff Kirsher 	mp->skb_size = (skb_size + 7) & ~7;
2321527a6266SJeff Kirsher 
2322527a6266SJeff Kirsher 	/*
2323527a6266SJeff Kirsher 	 * If NET_SKB_PAD is smaller than a cache line,
2324527a6266SJeff Kirsher 	 * netdev_alloc_skb() will cause skb->data to be misaligned
2325527a6266SJeff Kirsher 	 * to a cache line boundary.  If this is the case, include
2326527a6266SJeff Kirsher 	 * some extra space to allow re-aligning the data area.
2327527a6266SJeff Kirsher 	 */
2328527a6266SJeff Kirsher 	mp->skb_size += SKB_DMA_REALIGN;
2329527a6266SJeff Kirsher }
2330527a6266SJeff Kirsher 
2331527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev)
2332527a6266SJeff Kirsher {
2333527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2334527a6266SJeff Kirsher 	int err;
2335527a6266SJeff Kirsher 	int i;
2336527a6266SJeff Kirsher 
2337527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE, 0);
2338527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE_EXT, 0);
2339527a6266SJeff Kirsher 	rdlp(mp, INT_CAUSE_EXT);
2340527a6266SJeff Kirsher 
2341527a6266SJeff Kirsher 	err = request_irq(dev->irq, mv643xx_eth_irq,
2342527a6266SJeff Kirsher 			  IRQF_SHARED, dev->name, dev);
2343527a6266SJeff Kirsher 	if (err) {
2344527a6266SJeff Kirsher 		netdev_err(dev, "can't assign irq\n");
2345527a6266SJeff Kirsher 		return -EAGAIN;
2346527a6266SJeff Kirsher 	}
2347527a6266SJeff Kirsher 
2348527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2349527a6266SJeff Kirsher 
2350527a6266SJeff Kirsher 	napi_enable(&mp->napi);
2351527a6266SJeff Kirsher 
2352527a6266SJeff Kirsher 	skb_queue_head_init(&mp->rx_recycle);
2353527a6266SJeff Kirsher 
2354527a6266SJeff Kirsher 	mp->int_mask = INT_EXT;
2355527a6266SJeff Kirsher 
2356527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2357527a6266SJeff Kirsher 		err = rxq_init(mp, i);
2358527a6266SJeff Kirsher 		if (err) {
2359527a6266SJeff Kirsher 			while (--i >= 0)
2360527a6266SJeff Kirsher 				rxq_deinit(mp->rxq + i);
2361527a6266SJeff Kirsher 			goto out;
2362527a6266SJeff Kirsher 		}
2363527a6266SJeff Kirsher 
2364527a6266SJeff Kirsher 		rxq_refill(mp->rxq + i, INT_MAX);
2365527a6266SJeff Kirsher 		mp->int_mask |= INT_RX_0 << i;
2366527a6266SJeff Kirsher 	}
2367527a6266SJeff Kirsher 
2368527a6266SJeff Kirsher 	if (mp->oom) {
2369527a6266SJeff Kirsher 		mp->rx_oom.expires = jiffies + (HZ / 10);
2370527a6266SJeff Kirsher 		add_timer(&mp->rx_oom);
2371527a6266SJeff Kirsher 	}
2372527a6266SJeff Kirsher 
2373527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2374527a6266SJeff Kirsher 		err = txq_init(mp, i);
2375527a6266SJeff Kirsher 		if (err) {
2376527a6266SJeff Kirsher 			while (--i >= 0)
2377527a6266SJeff Kirsher 				txq_deinit(mp->txq + i);
2378527a6266SJeff Kirsher 			goto out_free;
2379527a6266SJeff Kirsher 		}
2380527a6266SJeff Kirsher 		mp->int_mask |= INT_TX_END_0 << i;
2381527a6266SJeff Kirsher 	}
2382527a6266SJeff Kirsher 
2383527a6266SJeff Kirsher 	port_start(mp);
2384527a6266SJeff Kirsher 
2385527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
2386527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2387527a6266SJeff Kirsher 
2388527a6266SJeff Kirsher 	return 0;
2389527a6266SJeff Kirsher 
2390527a6266SJeff Kirsher 
2391527a6266SJeff Kirsher out_free:
2392527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2393527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2394527a6266SJeff Kirsher out:
2395527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2396527a6266SJeff Kirsher 
2397527a6266SJeff Kirsher 	return err;
2398527a6266SJeff Kirsher }
2399527a6266SJeff Kirsher 
2400527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp)
2401527a6266SJeff Kirsher {
2402527a6266SJeff Kirsher 	unsigned int data;
2403527a6266SJeff Kirsher 	int i;
2404527a6266SJeff Kirsher 
2405527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2406527a6266SJeff Kirsher 		rxq_disable(mp->rxq + i);
2407527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2408527a6266SJeff Kirsher 		txq_disable(mp->txq + i);
2409527a6266SJeff Kirsher 
2410527a6266SJeff Kirsher 	while (1) {
2411527a6266SJeff Kirsher 		u32 ps = rdlp(mp, PORT_STATUS);
2412527a6266SJeff Kirsher 
2413527a6266SJeff Kirsher 		if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2414527a6266SJeff Kirsher 			break;
2415527a6266SJeff Kirsher 		udelay(10);
2416527a6266SJeff Kirsher 	}
2417527a6266SJeff Kirsher 
2418527a6266SJeff Kirsher 	/* Reset the Enable bit in the Configuration Register */
2419527a6266SJeff Kirsher 	data = rdlp(mp, PORT_SERIAL_CONTROL);
2420527a6266SJeff Kirsher 	data &= ~(SERIAL_PORT_ENABLE		|
2421527a6266SJeff Kirsher 		  DO_NOT_FORCE_LINK_FAIL	|
2422527a6266SJeff Kirsher 		  FORCE_LINK_PASS);
2423527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, data);
2424527a6266SJeff Kirsher }
2425527a6266SJeff Kirsher 
2426527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev)
2427527a6266SJeff Kirsher {
2428527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2429527a6266SJeff Kirsher 	int i;
2430527a6266SJeff Kirsher 
2431527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, 0x00000000);
2432527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2433527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2434527a6266SJeff Kirsher 
2435527a6266SJeff Kirsher 	napi_disable(&mp->napi);
2436527a6266SJeff Kirsher 
2437527a6266SJeff Kirsher 	del_timer_sync(&mp->rx_oom);
2438527a6266SJeff Kirsher 
2439527a6266SJeff Kirsher 	netif_carrier_off(dev);
2440527a6266SJeff Kirsher 
2441527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2442527a6266SJeff Kirsher 
2443527a6266SJeff Kirsher 	port_reset(mp);
2444527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
2445527a6266SJeff Kirsher 	mib_counters_update(mp);
2446527a6266SJeff Kirsher 	del_timer_sync(&mp->mib_counters_timer);
2447527a6266SJeff Kirsher 
2448527a6266SJeff Kirsher 	skb_queue_purge(&mp->rx_recycle);
2449527a6266SJeff Kirsher 
2450527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2451527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2452527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2453527a6266SJeff Kirsher 		txq_deinit(mp->txq + i);
2454527a6266SJeff Kirsher 
2455527a6266SJeff Kirsher 	return 0;
2456527a6266SJeff Kirsher }
2457527a6266SJeff Kirsher 
2458527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2459527a6266SJeff Kirsher {
2460527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2461527a6266SJeff Kirsher 
2462527a6266SJeff Kirsher 	if (mp->phy != NULL)
2463527a6266SJeff Kirsher 		return phy_mii_ioctl(mp->phy, ifr, cmd);
2464527a6266SJeff Kirsher 
2465527a6266SJeff Kirsher 	return -EOPNOTSUPP;
2466527a6266SJeff Kirsher }
2467527a6266SJeff Kirsher 
2468527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2469527a6266SJeff Kirsher {
2470527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2471527a6266SJeff Kirsher 
2472527a6266SJeff Kirsher 	if (new_mtu < 64 || new_mtu > 9500)
2473527a6266SJeff Kirsher 		return -EINVAL;
2474527a6266SJeff Kirsher 
2475527a6266SJeff Kirsher 	dev->mtu = new_mtu;
2476527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2477527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2478527a6266SJeff Kirsher 
2479527a6266SJeff Kirsher 	if (!netif_running(dev))
2480527a6266SJeff Kirsher 		return 0;
2481527a6266SJeff Kirsher 
2482527a6266SJeff Kirsher 	/*
2483527a6266SJeff Kirsher 	 * Stop and then re-open the interface. This will allocate RX
2484527a6266SJeff Kirsher 	 * skbs of the new MTU.
2485527a6266SJeff Kirsher 	 * There is a possible danger that the open will not succeed,
2486527a6266SJeff Kirsher 	 * due to memory being full.
2487527a6266SJeff Kirsher 	 */
2488527a6266SJeff Kirsher 	mv643xx_eth_stop(dev);
2489527a6266SJeff Kirsher 	if (mv643xx_eth_open(dev)) {
2490527a6266SJeff Kirsher 		netdev_err(dev,
2491527a6266SJeff Kirsher 			   "fatal error on re-opening device after MTU change\n");
2492527a6266SJeff Kirsher 	}
2493527a6266SJeff Kirsher 
2494527a6266SJeff Kirsher 	return 0;
2495527a6266SJeff Kirsher }
2496527a6266SJeff Kirsher 
2497527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly)
2498527a6266SJeff Kirsher {
2499527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2500527a6266SJeff Kirsher 
2501527a6266SJeff Kirsher 	mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2502527a6266SJeff Kirsher 	if (netif_running(mp->dev)) {
2503527a6266SJeff Kirsher 		netif_tx_stop_all_queues(mp->dev);
2504527a6266SJeff Kirsher 		port_reset(mp);
2505527a6266SJeff Kirsher 		port_start(mp);
2506527a6266SJeff Kirsher 		netif_tx_wake_all_queues(mp->dev);
2507527a6266SJeff Kirsher 	}
2508527a6266SJeff Kirsher }
2509527a6266SJeff Kirsher 
2510527a6266SJeff Kirsher static void mv643xx_eth_tx_timeout(struct net_device *dev)
2511527a6266SJeff Kirsher {
2512527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2513527a6266SJeff Kirsher 
2514527a6266SJeff Kirsher 	netdev_info(dev, "tx timeout\n");
2515527a6266SJeff Kirsher 
2516527a6266SJeff Kirsher 	schedule_work(&mp->tx_timeout_task);
2517527a6266SJeff Kirsher }
2518527a6266SJeff Kirsher 
2519527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2520527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev)
2521527a6266SJeff Kirsher {
2522527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2523527a6266SJeff Kirsher 
2524527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2525527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2526527a6266SJeff Kirsher 
2527527a6266SJeff Kirsher 	mv643xx_eth_irq(dev->irq, dev);
2528527a6266SJeff Kirsher 
2529527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2530527a6266SJeff Kirsher }
2531527a6266SJeff Kirsher #endif
2532527a6266SJeff Kirsher 
2533527a6266SJeff Kirsher 
2534527a6266SJeff Kirsher /* platform glue ************************************************************/
2535527a6266SJeff Kirsher static void
2536527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
253763a9332bSAndrew Lunn 			      const struct mbus_dram_target_info *dram)
2538527a6266SJeff Kirsher {
2539527a6266SJeff Kirsher 	void __iomem *base = msp->base;
2540527a6266SJeff Kirsher 	u32 win_enable;
2541527a6266SJeff Kirsher 	u32 win_protect;
2542527a6266SJeff Kirsher 	int i;
2543527a6266SJeff Kirsher 
2544527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
2545527a6266SJeff Kirsher 		writel(0, base + WINDOW_BASE(i));
2546527a6266SJeff Kirsher 		writel(0, base + WINDOW_SIZE(i));
2547527a6266SJeff Kirsher 		if (i < 4)
2548527a6266SJeff Kirsher 			writel(0, base + WINDOW_REMAP_HIGH(i));
2549527a6266SJeff Kirsher 	}
2550527a6266SJeff Kirsher 
2551527a6266SJeff Kirsher 	win_enable = 0x3f;
2552527a6266SJeff Kirsher 	win_protect = 0;
2553527a6266SJeff Kirsher 
2554527a6266SJeff Kirsher 	for (i = 0; i < dram->num_cs; i++) {
255563a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
2556527a6266SJeff Kirsher 
2557527a6266SJeff Kirsher 		writel((cs->base & 0xffff0000) |
2558527a6266SJeff Kirsher 			(cs->mbus_attr << 8) |
2559527a6266SJeff Kirsher 			dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2560527a6266SJeff Kirsher 		writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2561527a6266SJeff Kirsher 
2562527a6266SJeff Kirsher 		win_enable &= ~(1 << i);
2563527a6266SJeff Kirsher 		win_protect |= 3 << (2 * i);
2564527a6266SJeff Kirsher 	}
2565527a6266SJeff Kirsher 
2566527a6266SJeff Kirsher 	writel(win_enable, base + WINDOW_BAR_ENABLE);
2567527a6266SJeff Kirsher 	msp->win_protect = win_protect;
2568527a6266SJeff Kirsher }
2569527a6266SJeff Kirsher 
2570527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2571527a6266SJeff Kirsher {
2572527a6266SJeff Kirsher 	/*
2573527a6266SJeff Kirsher 	 * Check whether we have a 14-bit coal limit field in bits
2574527a6266SJeff Kirsher 	 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2575527a6266SJeff Kirsher 	 * SDMA config register.
2576527a6266SJeff Kirsher 	 */
2577527a6266SJeff Kirsher 	writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2578527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
2579527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 1;
2580527a6266SJeff Kirsher 	else
2581527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 0;
2582527a6266SJeff Kirsher 
2583527a6266SJeff Kirsher 	/*
2584527a6266SJeff Kirsher 	 * Check whether the MAC supports TX rate control, and if
2585527a6266SJeff Kirsher 	 * yes, whether its associated registers are in the old or
2586527a6266SJeff Kirsher 	 * the new place.
2587527a6266SJeff Kirsher 	 */
2588527a6266SJeff Kirsher 	writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2589527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
2590527a6266SJeff Kirsher 		msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2591527a6266SJeff Kirsher 	} else {
2592527a6266SJeff Kirsher 		writel(7, msp->base + 0x0400 + TX_BW_RATE);
2593527a6266SJeff Kirsher 		if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
2594527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2595527a6266SJeff Kirsher 		else
2596527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2597527a6266SJeff Kirsher 	}
2598527a6266SJeff Kirsher }
2599527a6266SJeff Kirsher 
2600527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2601527a6266SJeff Kirsher {
2602527a6266SJeff Kirsher 	static int mv643xx_eth_version_printed;
2603527a6266SJeff Kirsher 	struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2604527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp;
260563a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
2606527a6266SJeff Kirsher 	struct resource *res;
2607527a6266SJeff Kirsher 	int ret;
2608527a6266SJeff Kirsher 
2609527a6266SJeff Kirsher 	if (!mv643xx_eth_version_printed++)
2610527a6266SJeff Kirsher 		pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2611527a6266SJeff Kirsher 			  mv643xx_eth_driver_version);
2612527a6266SJeff Kirsher 
2613527a6266SJeff Kirsher 	ret = -EINVAL;
2614527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2615527a6266SJeff Kirsher 	if (res == NULL)
2616527a6266SJeff Kirsher 		goto out;
2617527a6266SJeff Kirsher 
2618527a6266SJeff Kirsher 	ret = -ENOMEM;
2619527a6266SJeff Kirsher 	msp = kzalloc(sizeof(*msp), GFP_KERNEL);
2620527a6266SJeff Kirsher 	if (msp == NULL)
2621527a6266SJeff Kirsher 		goto out;
2622527a6266SJeff Kirsher 
2623527a6266SJeff Kirsher 	msp->base = ioremap(res->start, resource_size(res));
2624527a6266SJeff Kirsher 	if (msp->base == NULL)
2625527a6266SJeff Kirsher 		goto out_free;
2626527a6266SJeff Kirsher 
2627527a6266SJeff Kirsher 	/*
2628527a6266SJeff Kirsher 	 * Set up and register SMI bus.
2629527a6266SJeff Kirsher 	 */
2630527a6266SJeff Kirsher 	if (pd == NULL || pd->shared_smi == NULL) {
2631527a6266SJeff Kirsher 		msp->smi_bus = mdiobus_alloc();
2632527a6266SJeff Kirsher 		if (msp->smi_bus == NULL)
2633527a6266SJeff Kirsher 			goto out_unmap;
2634527a6266SJeff Kirsher 
2635527a6266SJeff Kirsher 		msp->smi_bus->priv = msp;
2636527a6266SJeff Kirsher 		msp->smi_bus->name = "mv643xx_eth smi";
2637527a6266SJeff Kirsher 		msp->smi_bus->read = smi_bus_read;
2638527a6266SJeff Kirsher 		msp->smi_bus->write = smi_bus_write,
263912322431SFlorian Fainelli 		snprintf(msp->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
264012322431SFlorian Fainelli 			pdev->name, pdev->id);
2641527a6266SJeff Kirsher 		msp->smi_bus->parent = &pdev->dev;
2642527a6266SJeff Kirsher 		msp->smi_bus->phy_mask = 0xffffffff;
2643527a6266SJeff Kirsher 		if (mdiobus_register(msp->smi_bus) < 0)
2644527a6266SJeff Kirsher 			goto out_free_mii_bus;
2645527a6266SJeff Kirsher 		msp->smi = msp;
2646527a6266SJeff Kirsher 	} else {
2647527a6266SJeff Kirsher 		msp->smi = platform_get_drvdata(pd->shared_smi);
2648527a6266SJeff Kirsher 	}
2649527a6266SJeff Kirsher 
2650527a6266SJeff Kirsher 	msp->err_interrupt = NO_IRQ;
2651527a6266SJeff Kirsher 	init_waitqueue_head(&msp->smi_busy_wait);
2652527a6266SJeff Kirsher 
2653527a6266SJeff Kirsher 	/*
2654527a6266SJeff Kirsher 	 * Check whether the error interrupt is hooked up.
2655527a6266SJeff Kirsher 	 */
2656527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2657527a6266SJeff Kirsher 	if (res != NULL) {
2658527a6266SJeff Kirsher 		int err;
2659527a6266SJeff Kirsher 
2660527a6266SJeff Kirsher 		err = request_irq(res->start, mv643xx_eth_err_irq,
2661527a6266SJeff Kirsher 				  IRQF_SHARED, "mv643xx_eth", msp);
2662527a6266SJeff Kirsher 		if (!err) {
2663527a6266SJeff Kirsher 			writel(ERR_INT_SMI_DONE, msp->base + ERR_INT_MASK);
2664527a6266SJeff Kirsher 			msp->err_interrupt = res->start;
2665527a6266SJeff Kirsher 		}
2666527a6266SJeff Kirsher 	}
2667527a6266SJeff Kirsher 
2668527a6266SJeff Kirsher 	/*
2669527a6266SJeff Kirsher 	 * (Re-)program MBUS remapping windows if we are asked to.
2670527a6266SJeff Kirsher 	 */
267163a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
267263a9332bSAndrew Lunn 	if (dram)
267363a9332bSAndrew Lunn 		mv643xx_eth_conf_mbus_windows(msp, dram);
2674527a6266SJeff Kirsher 
2675527a6266SJeff Kirsher 	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2676527a6266SJeff Kirsher 					pd->tx_csum_limit : 9 * 1024;
2677527a6266SJeff Kirsher 	infer_hw_params(msp);
2678527a6266SJeff Kirsher 
2679527a6266SJeff Kirsher 	platform_set_drvdata(pdev, msp);
2680527a6266SJeff Kirsher 
2681527a6266SJeff Kirsher 	return 0;
2682527a6266SJeff Kirsher 
2683527a6266SJeff Kirsher out_free_mii_bus:
2684527a6266SJeff Kirsher 	mdiobus_free(msp->smi_bus);
2685527a6266SJeff Kirsher out_unmap:
2686527a6266SJeff Kirsher 	iounmap(msp->base);
2687527a6266SJeff Kirsher out_free:
2688527a6266SJeff Kirsher 	kfree(msp);
2689527a6266SJeff Kirsher out:
2690527a6266SJeff Kirsher 	return ret;
2691527a6266SJeff Kirsher }
2692527a6266SJeff Kirsher 
2693527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2694527a6266SJeff Kirsher {
2695527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
2696527a6266SJeff Kirsher 	struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2697527a6266SJeff Kirsher 
2698527a6266SJeff Kirsher 	if (pd == NULL || pd->shared_smi == NULL) {
2699527a6266SJeff Kirsher 		mdiobus_unregister(msp->smi_bus);
2700527a6266SJeff Kirsher 		mdiobus_free(msp->smi_bus);
2701527a6266SJeff Kirsher 	}
2702527a6266SJeff Kirsher 	if (msp->err_interrupt != NO_IRQ)
2703527a6266SJeff Kirsher 		free_irq(msp->err_interrupt, msp);
2704527a6266SJeff Kirsher 	iounmap(msp->base);
2705527a6266SJeff Kirsher 	kfree(msp);
2706527a6266SJeff Kirsher 
2707527a6266SJeff Kirsher 	return 0;
2708527a6266SJeff Kirsher }
2709527a6266SJeff Kirsher 
2710527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = {
2711527a6266SJeff Kirsher 	.probe		= mv643xx_eth_shared_probe,
2712527a6266SJeff Kirsher 	.remove		= mv643xx_eth_shared_remove,
2713527a6266SJeff Kirsher 	.driver = {
2714527a6266SJeff Kirsher 		.name	= MV643XX_ETH_SHARED_NAME,
2715527a6266SJeff Kirsher 		.owner	= THIS_MODULE,
2716527a6266SJeff Kirsher 	},
2717527a6266SJeff Kirsher };
2718527a6266SJeff Kirsher 
2719527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
2720527a6266SJeff Kirsher {
2721527a6266SJeff Kirsher 	int addr_shift = 5 * mp->port_num;
2722527a6266SJeff Kirsher 	u32 data;
2723527a6266SJeff Kirsher 
2724527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2725527a6266SJeff Kirsher 	data &= ~(0x1f << addr_shift);
2726527a6266SJeff Kirsher 	data |= (phy_addr & 0x1f) << addr_shift;
2727527a6266SJeff Kirsher 	wrl(mp, PHY_ADDR, data);
2728527a6266SJeff Kirsher }
2729527a6266SJeff Kirsher 
2730527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp)
2731527a6266SJeff Kirsher {
2732527a6266SJeff Kirsher 	unsigned int data;
2733527a6266SJeff Kirsher 
2734527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2735527a6266SJeff Kirsher 
2736527a6266SJeff Kirsher 	return (data >> (5 * mp->port_num)) & 0x1f;
2737527a6266SJeff Kirsher }
2738527a6266SJeff Kirsher 
2739527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp,
2740527a6266SJeff Kirsher 		       struct mv643xx_eth_platform_data *pd)
2741527a6266SJeff Kirsher {
2742527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2743527a6266SJeff Kirsher 
2744527a6266SJeff Kirsher 	if (is_valid_ether_addr(pd->mac_addr))
2745527a6266SJeff Kirsher 		memcpy(dev->dev_addr, pd->mac_addr, 6);
2746527a6266SJeff Kirsher 	else
2747527a6266SJeff Kirsher 		uc_addr_get(mp, dev->dev_addr);
2748527a6266SJeff Kirsher 
2749527a6266SJeff Kirsher 	mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2750527a6266SJeff Kirsher 	if (pd->rx_queue_size)
2751527a6266SJeff Kirsher 		mp->rx_ring_size = pd->rx_queue_size;
2752527a6266SJeff Kirsher 	mp->rx_desc_sram_addr = pd->rx_sram_addr;
2753527a6266SJeff Kirsher 	mp->rx_desc_sram_size = pd->rx_sram_size;
2754527a6266SJeff Kirsher 
2755527a6266SJeff Kirsher 	mp->rxq_count = pd->rx_queue_count ? : 1;
2756527a6266SJeff Kirsher 
2757527a6266SJeff Kirsher 	mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2758527a6266SJeff Kirsher 	if (pd->tx_queue_size)
2759527a6266SJeff Kirsher 		mp->tx_ring_size = pd->tx_queue_size;
2760527a6266SJeff Kirsher 	mp->tx_desc_sram_addr = pd->tx_sram_addr;
2761527a6266SJeff Kirsher 	mp->tx_desc_sram_size = pd->tx_sram_size;
2762527a6266SJeff Kirsher 
2763527a6266SJeff Kirsher 	mp->txq_count = pd->tx_queue_count ? : 1;
2764527a6266SJeff Kirsher }
2765527a6266SJeff Kirsher 
2766527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2767527a6266SJeff Kirsher 				   int phy_addr)
2768527a6266SJeff Kirsher {
2769527a6266SJeff Kirsher 	struct mii_bus *bus = mp->shared->smi->smi_bus;
2770527a6266SJeff Kirsher 	struct phy_device *phydev;
2771527a6266SJeff Kirsher 	int start;
2772527a6266SJeff Kirsher 	int num;
2773527a6266SJeff Kirsher 	int i;
2774527a6266SJeff Kirsher 
2775527a6266SJeff Kirsher 	if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2776527a6266SJeff Kirsher 		start = phy_addr_get(mp) & 0x1f;
2777527a6266SJeff Kirsher 		num = 32;
2778527a6266SJeff Kirsher 	} else {
2779527a6266SJeff Kirsher 		start = phy_addr & 0x1f;
2780527a6266SJeff Kirsher 		num = 1;
2781527a6266SJeff Kirsher 	}
2782527a6266SJeff Kirsher 
2783527a6266SJeff Kirsher 	phydev = NULL;
2784527a6266SJeff Kirsher 	for (i = 0; i < num; i++) {
2785527a6266SJeff Kirsher 		int addr = (start + i) & 0x1f;
2786527a6266SJeff Kirsher 
2787527a6266SJeff Kirsher 		if (bus->phy_map[addr] == NULL)
2788527a6266SJeff Kirsher 			mdiobus_scan(bus, addr);
2789527a6266SJeff Kirsher 
2790527a6266SJeff Kirsher 		if (phydev == NULL) {
2791527a6266SJeff Kirsher 			phydev = bus->phy_map[addr];
2792527a6266SJeff Kirsher 			if (phydev != NULL)
2793527a6266SJeff Kirsher 				phy_addr_set(mp, addr);
2794527a6266SJeff Kirsher 		}
2795527a6266SJeff Kirsher 	}
2796527a6266SJeff Kirsher 
2797527a6266SJeff Kirsher 	return phydev;
2798527a6266SJeff Kirsher }
2799527a6266SJeff Kirsher 
2800527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
2801527a6266SJeff Kirsher {
2802527a6266SJeff Kirsher 	struct phy_device *phy = mp->phy;
2803527a6266SJeff Kirsher 
2804527a6266SJeff Kirsher 	phy_reset(mp);
2805527a6266SJeff Kirsher 
2806527a6266SJeff Kirsher 	phy_attach(mp->dev, dev_name(&phy->dev), 0, PHY_INTERFACE_MODE_GMII);
2807527a6266SJeff Kirsher 
2808527a6266SJeff Kirsher 	if (speed == 0) {
2809527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_ENABLE;
2810527a6266SJeff Kirsher 		phy->speed = 0;
2811527a6266SJeff Kirsher 		phy->duplex = 0;
2812527a6266SJeff Kirsher 		phy->advertising = phy->supported | ADVERTISED_Autoneg;
2813527a6266SJeff Kirsher 	} else {
2814527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_DISABLE;
2815527a6266SJeff Kirsher 		phy->advertising = 0;
2816527a6266SJeff Kirsher 		phy->speed = speed;
2817527a6266SJeff Kirsher 		phy->duplex = duplex;
2818527a6266SJeff Kirsher 	}
2819527a6266SJeff Kirsher 	phy_start_aneg(phy);
2820527a6266SJeff Kirsher }
2821527a6266SJeff Kirsher 
2822527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2823527a6266SJeff Kirsher {
2824527a6266SJeff Kirsher 	u32 pscr;
2825527a6266SJeff Kirsher 
2826527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2827527a6266SJeff Kirsher 	if (pscr & SERIAL_PORT_ENABLE) {
2828527a6266SJeff Kirsher 		pscr &= ~SERIAL_PORT_ENABLE;
2829527a6266SJeff Kirsher 		wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2830527a6266SJeff Kirsher 	}
2831527a6266SJeff Kirsher 
2832527a6266SJeff Kirsher 	pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
2833527a6266SJeff Kirsher 	if (mp->phy == NULL) {
2834527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2835527a6266SJeff Kirsher 		if (speed == SPEED_1000)
2836527a6266SJeff Kirsher 			pscr |= SET_GMII_SPEED_TO_1000;
2837527a6266SJeff Kirsher 		else if (speed == SPEED_100)
2838527a6266SJeff Kirsher 			pscr |= SET_MII_SPEED_TO_100;
2839527a6266SJeff Kirsher 
2840527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2841527a6266SJeff Kirsher 
2842527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2843527a6266SJeff Kirsher 		if (duplex == DUPLEX_FULL)
2844527a6266SJeff Kirsher 			pscr |= SET_FULL_DUPLEX_MODE;
2845527a6266SJeff Kirsher 	}
2846527a6266SJeff Kirsher 
2847527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2848527a6266SJeff Kirsher }
2849527a6266SJeff Kirsher 
2850527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = {
2851527a6266SJeff Kirsher 	.ndo_open		= mv643xx_eth_open,
2852527a6266SJeff Kirsher 	.ndo_stop		= mv643xx_eth_stop,
2853527a6266SJeff Kirsher 	.ndo_start_xmit		= mv643xx_eth_xmit,
2854527a6266SJeff Kirsher 	.ndo_set_rx_mode	= mv643xx_eth_set_rx_mode,
2855527a6266SJeff Kirsher 	.ndo_set_mac_address	= mv643xx_eth_set_mac_address,
2856527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
2857527a6266SJeff Kirsher 	.ndo_do_ioctl		= mv643xx_eth_ioctl,
2858527a6266SJeff Kirsher 	.ndo_change_mtu		= mv643xx_eth_change_mtu,
2859527a6266SJeff Kirsher 	.ndo_set_features	= mv643xx_eth_set_features,
2860527a6266SJeff Kirsher 	.ndo_tx_timeout		= mv643xx_eth_tx_timeout,
2861527a6266SJeff Kirsher 	.ndo_get_stats		= mv643xx_eth_get_stats,
2862527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
2863527a6266SJeff Kirsher 	.ndo_poll_controller	= mv643xx_eth_netpoll,
2864527a6266SJeff Kirsher #endif
2865527a6266SJeff Kirsher };
2866527a6266SJeff Kirsher 
2867527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev)
2868527a6266SJeff Kirsher {
2869527a6266SJeff Kirsher 	struct mv643xx_eth_platform_data *pd;
2870527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2871527a6266SJeff Kirsher 	struct net_device *dev;
2872527a6266SJeff Kirsher 	struct resource *res;
2873527a6266SJeff Kirsher 	int err;
2874527a6266SJeff Kirsher 
2875527a6266SJeff Kirsher 	pd = pdev->dev.platform_data;
2876527a6266SJeff Kirsher 	if (pd == NULL) {
2877527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
2878527a6266SJeff Kirsher 		return -ENODEV;
2879527a6266SJeff Kirsher 	}
2880527a6266SJeff Kirsher 
2881527a6266SJeff Kirsher 	if (pd->shared == NULL) {
2882527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
2883527a6266SJeff Kirsher 		return -ENODEV;
2884527a6266SJeff Kirsher 	}
2885527a6266SJeff Kirsher 
2886527a6266SJeff Kirsher 	dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
2887527a6266SJeff Kirsher 	if (!dev)
2888527a6266SJeff Kirsher 		return -ENOMEM;
2889527a6266SJeff Kirsher 
2890527a6266SJeff Kirsher 	mp = netdev_priv(dev);
2891527a6266SJeff Kirsher 	platform_set_drvdata(pdev, mp);
2892527a6266SJeff Kirsher 
2893527a6266SJeff Kirsher 	mp->shared = platform_get_drvdata(pd->shared);
2894527a6266SJeff Kirsher 	mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
2895527a6266SJeff Kirsher 	mp->port_num = pd->port_number;
2896527a6266SJeff Kirsher 
2897527a6266SJeff Kirsher 	mp->dev = dev;
2898527a6266SJeff Kirsher 
2899452503ebSAndrew Lunn 	/*
29009a43a026SAndrew Lunn 	 * Start with a default rate, and if there is a clock, allow
29019a43a026SAndrew Lunn 	 * it to override the default.
2902452503ebSAndrew Lunn 	 */
29039a43a026SAndrew Lunn 	mp->t_clk = 133000000;
29049a43a026SAndrew Lunn #if defined(CONFIG_HAVE_CLK)
2905452503ebSAndrew Lunn 	mp->clk = clk_get(&pdev->dev, (pdev->id ? "1" : "0"));
2906452503ebSAndrew Lunn 	if (!IS_ERR(mp->clk)) {
2907452503ebSAndrew Lunn 		clk_prepare_enable(mp->clk);
2908452503ebSAndrew Lunn 		mp->t_clk = clk_get_rate(mp->clk);
2909452503ebSAndrew Lunn 	}
29109a43a026SAndrew Lunn #endif
2911527a6266SJeff Kirsher 	set_params(mp, pd);
2912527a6266SJeff Kirsher 	netif_set_real_num_tx_queues(dev, mp->txq_count);
2913527a6266SJeff Kirsher 	netif_set_real_num_rx_queues(dev, mp->rxq_count);
2914527a6266SJeff Kirsher 
2915527a6266SJeff Kirsher 	if (pd->phy_addr != MV643XX_ETH_PHY_NONE)
2916527a6266SJeff Kirsher 		mp->phy = phy_scan(mp, pd->phy_addr);
2917527a6266SJeff Kirsher 
2918527a6266SJeff Kirsher 	if (mp->phy != NULL)
2919527a6266SJeff Kirsher 		phy_init(mp, pd->speed, pd->duplex);
2920527a6266SJeff Kirsher 
2921527a6266SJeff Kirsher 	SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
2922527a6266SJeff Kirsher 
2923527a6266SJeff Kirsher 	init_pscr(mp, pd->speed, pd->duplex);
2924527a6266SJeff Kirsher 
2925527a6266SJeff Kirsher 
2926527a6266SJeff Kirsher 	mib_counters_clear(mp);
2927527a6266SJeff Kirsher 
2928527a6266SJeff Kirsher 	init_timer(&mp->mib_counters_timer);
2929527a6266SJeff Kirsher 	mp->mib_counters_timer.data = (unsigned long)mp;
2930527a6266SJeff Kirsher 	mp->mib_counters_timer.function = mib_counters_timer_wrapper;
2931527a6266SJeff Kirsher 	mp->mib_counters_timer.expires = jiffies + 30 * HZ;
2932527a6266SJeff Kirsher 	add_timer(&mp->mib_counters_timer);
2933527a6266SJeff Kirsher 
2934527a6266SJeff Kirsher 	spin_lock_init(&mp->mib_counters_lock);
2935527a6266SJeff Kirsher 
2936527a6266SJeff Kirsher 	INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
2937527a6266SJeff Kirsher 
2938527a6266SJeff Kirsher 	netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
2939527a6266SJeff Kirsher 
2940527a6266SJeff Kirsher 	init_timer(&mp->rx_oom);
2941527a6266SJeff Kirsher 	mp->rx_oom.data = (unsigned long)mp;
2942527a6266SJeff Kirsher 	mp->rx_oom.function = oom_timer_wrapper;
2943527a6266SJeff Kirsher 
2944527a6266SJeff Kirsher 
2945527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2946527a6266SJeff Kirsher 	BUG_ON(!res);
2947527a6266SJeff Kirsher 	dev->irq = res->start;
2948527a6266SJeff Kirsher 
2949527a6266SJeff Kirsher 	dev->netdev_ops = &mv643xx_eth_netdev_ops;
2950527a6266SJeff Kirsher 
2951527a6266SJeff Kirsher 	dev->watchdog_timeo = 2 * HZ;
2952527a6266SJeff Kirsher 	dev->base_addr = 0;
2953527a6266SJeff Kirsher 
2954527a6266SJeff Kirsher 	dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
2955527a6266SJeff Kirsher 		NETIF_F_RXCSUM | NETIF_F_LRO;
2956527a6266SJeff Kirsher 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
2957527a6266SJeff Kirsher 	dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
2958527a6266SJeff Kirsher 
295901789349SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT;
296001789349SJiri Pirko 
2961527a6266SJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
2962527a6266SJeff Kirsher 
2963527a6266SJeff Kirsher 	if (mp->shared->win_protect)
2964527a6266SJeff Kirsher 		wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
2965527a6266SJeff Kirsher 
2966527a6266SJeff Kirsher 	netif_carrier_off(dev);
2967527a6266SJeff Kirsher 
2968527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
2969527a6266SJeff Kirsher 
2970527a6266SJeff Kirsher 	set_rx_coal(mp, 250);
2971527a6266SJeff Kirsher 	set_tx_coal(mp, 0);
2972527a6266SJeff Kirsher 
2973527a6266SJeff Kirsher 	err = register_netdev(dev);
2974527a6266SJeff Kirsher 	if (err)
2975527a6266SJeff Kirsher 		goto out;
2976527a6266SJeff Kirsher 
2977527a6266SJeff Kirsher 	netdev_notice(dev, "port %d with MAC address %pM\n",
2978527a6266SJeff Kirsher 		      mp->port_num, dev->dev_addr);
2979527a6266SJeff Kirsher 
2980527a6266SJeff Kirsher 	if (mp->tx_desc_sram_size > 0)
2981527a6266SJeff Kirsher 		netdev_notice(dev, "configured with sram\n");
2982527a6266SJeff Kirsher 
2983527a6266SJeff Kirsher 	return 0;
2984527a6266SJeff Kirsher 
2985527a6266SJeff Kirsher out:
2986*baffab28SSimon Baatz #if defined(CONFIG_HAVE_CLK)
2987*baffab28SSimon Baatz 	if (!IS_ERR(mp->clk)) {
2988*baffab28SSimon Baatz 		clk_disable_unprepare(mp->clk);
2989*baffab28SSimon Baatz 		clk_put(mp->clk);
2990*baffab28SSimon Baatz 	}
2991*baffab28SSimon Baatz #endif
2992527a6266SJeff Kirsher 	free_netdev(dev);
2993527a6266SJeff Kirsher 
2994527a6266SJeff Kirsher 	return err;
2995527a6266SJeff Kirsher }
2996527a6266SJeff Kirsher 
2997527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev)
2998527a6266SJeff Kirsher {
2999527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
3000527a6266SJeff Kirsher 
3001527a6266SJeff Kirsher 	unregister_netdev(mp->dev);
3002527a6266SJeff Kirsher 	if (mp->phy != NULL)
3003527a6266SJeff Kirsher 		phy_detach(mp->phy);
3004527a6266SJeff Kirsher 	cancel_work_sync(&mp->tx_timeout_task);
3005452503ebSAndrew Lunn 
30069a43a026SAndrew Lunn #if defined(CONFIG_HAVE_CLK)
3007452503ebSAndrew Lunn 	if (!IS_ERR(mp->clk)) {
3008452503ebSAndrew Lunn 		clk_disable_unprepare(mp->clk);
3009452503ebSAndrew Lunn 		clk_put(mp->clk);
3010452503ebSAndrew Lunn 	}
30119a43a026SAndrew Lunn #endif
30129a43a026SAndrew Lunn 
3013527a6266SJeff Kirsher 	free_netdev(mp->dev);
3014527a6266SJeff Kirsher 
3015527a6266SJeff Kirsher 	platform_set_drvdata(pdev, NULL);
3016527a6266SJeff Kirsher 
3017527a6266SJeff Kirsher 	return 0;
3018527a6266SJeff Kirsher }
3019527a6266SJeff Kirsher 
3020527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev)
3021527a6266SJeff Kirsher {
3022527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
3023527a6266SJeff Kirsher 
3024527a6266SJeff Kirsher 	/* Mask all interrupts on ethernet port */
3025527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
3026527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
3027527a6266SJeff Kirsher 
3028527a6266SJeff Kirsher 	if (netif_running(mp->dev))
3029527a6266SJeff Kirsher 		port_reset(mp);
3030527a6266SJeff Kirsher }
3031527a6266SJeff Kirsher 
3032527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = {
3033527a6266SJeff Kirsher 	.probe		= mv643xx_eth_probe,
3034527a6266SJeff Kirsher 	.remove		= mv643xx_eth_remove,
3035527a6266SJeff Kirsher 	.shutdown	= mv643xx_eth_shutdown,
3036527a6266SJeff Kirsher 	.driver = {
3037527a6266SJeff Kirsher 		.name	= MV643XX_ETH_NAME,
3038527a6266SJeff Kirsher 		.owner	= THIS_MODULE,
3039527a6266SJeff Kirsher 	},
3040527a6266SJeff Kirsher };
3041527a6266SJeff Kirsher 
3042527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void)
3043527a6266SJeff Kirsher {
3044527a6266SJeff Kirsher 	int rc;
3045527a6266SJeff Kirsher 
3046527a6266SJeff Kirsher 	rc = platform_driver_register(&mv643xx_eth_shared_driver);
3047527a6266SJeff Kirsher 	if (!rc) {
3048527a6266SJeff Kirsher 		rc = platform_driver_register(&mv643xx_eth_driver);
3049527a6266SJeff Kirsher 		if (rc)
3050527a6266SJeff Kirsher 			platform_driver_unregister(&mv643xx_eth_shared_driver);
3051527a6266SJeff Kirsher 	}
3052527a6266SJeff Kirsher 
3053527a6266SJeff Kirsher 	return rc;
3054527a6266SJeff Kirsher }
3055527a6266SJeff Kirsher module_init(mv643xx_eth_init_module);
3056527a6266SJeff Kirsher 
3057527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void)
3058527a6266SJeff Kirsher {
3059527a6266SJeff Kirsher 	platform_driver_unregister(&mv643xx_eth_driver);
3060527a6266SJeff Kirsher 	platform_driver_unregister(&mv643xx_eth_shared_driver);
3061527a6266SJeff Kirsher }
3062527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module);
3063527a6266SJeff Kirsher 
3064527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
3065527a6266SJeff Kirsher 	      "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
3066527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
3067527a6266SJeff Kirsher MODULE_LICENSE("GPL");
3068527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
3069527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3070