xref: /openbmc/linux/drivers/net/ethernet/marvell/mv643xx_eth.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2527a6266SJeff Kirsher /*
3527a6266SJeff Kirsher  * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
4527a6266SJeff Kirsher  * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
5527a6266SJeff Kirsher  *
6527a6266SJeff Kirsher  * Based on the 64360 driver from:
7527a6266SJeff Kirsher  * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
8527a6266SJeff Kirsher  *		      Rabeeh Khoury <rabeeh@marvell.com>
9527a6266SJeff Kirsher  *
10527a6266SJeff Kirsher  * Copyright (C) 2003 PMC-Sierra, Inc.,
11527a6266SJeff Kirsher  *	written by Manish Lachwani
12527a6266SJeff Kirsher  *
13527a6266SJeff Kirsher  * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
14527a6266SJeff Kirsher  *
15527a6266SJeff Kirsher  * Copyright (C) 2004-2006 MontaVista Software, Inc.
16527a6266SJeff Kirsher  *			   Dale Farnsworth <dale@farnsworth.org>
17527a6266SJeff Kirsher  *
18527a6266SJeff Kirsher  * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
19527a6266SJeff Kirsher  *				     <sjhill@realitydiluted.com>
20527a6266SJeff Kirsher  *
21527a6266SJeff Kirsher  * Copyright (C) 2007-2008 Marvell Semiconductor
22527a6266SJeff Kirsher  *			   Lennert Buytenhek <buytenh@marvell.com>
23527a6266SJeff Kirsher  *
243871c387SMichael Stapelberg  * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
25527a6266SJeff Kirsher  */
26527a6266SJeff Kirsher 
27527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28527a6266SJeff Kirsher 
29527a6266SJeff Kirsher #include <linux/init.h>
30527a6266SJeff Kirsher #include <linux/dma-mapping.h>
31527a6266SJeff Kirsher #include <linux/in.h>
32527a6266SJeff Kirsher #include <linux/ip.h>
333ae8f4e0SEzequiel Garcia #include <net/tso.h>
34527a6266SJeff Kirsher #include <linux/tcp.h>
35527a6266SJeff Kirsher #include <linux/udp.h>
36527a6266SJeff Kirsher #include <linux/etherdevice.h>
37527a6266SJeff Kirsher #include <linux/delay.h>
38527a6266SJeff Kirsher #include <linux/ethtool.h>
39527a6266SJeff Kirsher #include <linux/platform_device.h>
40527a6266SJeff Kirsher #include <linux/module.h>
41527a6266SJeff Kirsher #include <linux/kernel.h>
42527a6266SJeff Kirsher #include <linux/spinlock.h>
43527a6266SJeff Kirsher #include <linux/workqueue.h>
44527a6266SJeff Kirsher #include <linux/phy.h>
45527a6266SJeff Kirsher #include <linux/mv643xx_eth.h>
46527a6266SJeff Kirsher #include <linux/io.h>
473619eb85SSebastian Hesselbarth #include <linux/interrupt.h>
48527a6266SJeff Kirsher #include <linux/types.h>
49527a6266SJeff Kirsher #include <linux/slab.h>
50452503ebSAndrew Lunn #include <linux/clk.h>
5176723bcaSSebastian Hesselbarth #include <linux/of.h>
5276723bcaSSebastian Hesselbarth #include <linux/of_irq.h>
5376723bcaSSebastian Hesselbarth #include <linux/of_net.h>
54cc9d4598SSebastian Hesselbarth #include <linux/of_mdio.h>
55527a6266SJeff Kirsher 
56527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth";
57527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4";
58527a6266SJeff Kirsher 
59527a6266SJeff Kirsher 
60527a6266SJeff Kirsher /*
61527a6266SJeff Kirsher  * Registers shared between all ports.
62527a6266SJeff Kirsher  */
63527a6266SJeff Kirsher #define PHY_ADDR			0x0000
64527a6266SJeff Kirsher #define WINDOW_BASE(w)			(0x0200 + ((w) << 3))
65527a6266SJeff Kirsher #define WINDOW_SIZE(w)			(0x0204 + ((w) << 3))
66527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w)		(0x0280 + ((w) << 2))
67527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE		0x0290
68527a6266SJeff Kirsher #define WINDOW_PROTECT(w)		(0x0294 + ((w) << 4))
69527a6266SJeff Kirsher 
70527a6266SJeff Kirsher /*
71527a6266SJeff Kirsher  * Main per-port registers.  These live at offset 0x0400 for
72527a6266SJeff Kirsher  * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
73527a6266SJeff Kirsher  */
74527a6266SJeff Kirsher #define PORT_CONFIG			0x0000
75527a6266SJeff Kirsher #define  UNICAST_PROMISCUOUS_MODE	0x00000001
76527a6266SJeff Kirsher #define PORT_CONFIG_EXT			0x0004
77527a6266SJeff Kirsher #define MAC_ADDR_LOW			0x0014
78527a6266SJeff Kirsher #define MAC_ADDR_HIGH			0x0018
79527a6266SJeff Kirsher #define SDMA_CONFIG			0x001c
80527a6266SJeff Kirsher #define  TX_BURST_SIZE_16_64BIT		0x01000000
81527a6266SJeff Kirsher #define  TX_BURST_SIZE_4_64BIT		0x00800000
82527a6266SJeff Kirsher #define  BLM_TX_NO_SWAP			0x00000020
83527a6266SJeff Kirsher #define  BLM_RX_NO_SWAP			0x00000010
84527a6266SJeff Kirsher #define  RX_BURST_SIZE_16_64BIT		0x00000008
85527a6266SJeff Kirsher #define  RX_BURST_SIZE_4_64BIT		0x00000004
86527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL		0x003c
87527a6266SJeff Kirsher #define  SET_MII_SPEED_TO_100		0x01000000
88527a6266SJeff Kirsher #define  SET_GMII_SPEED_TO_1000		0x00800000
89527a6266SJeff Kirsher #define  SET_FULL_DUPLEX_MODE		0x00200000
90527a6266SJeff Kirsher #define  MAX_RX_PACKET_9700BYTE		0x000a0000
91527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_SPEED_GMII	0x00002000
92527a6266SJeff Kirsher #define  DO_NOT_FORCE_LINK_FAIL		0x00000400
93527a6266SJeff Kirsher #define  SERIAL_PORT_CONTROL_RESERVED	0x00000200
94527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_FLOW_CTRL	0x00000008
95527a6266SJeff Kirsher #define  DISABLE_AUTO_NEG_FOR_DUPLEX	0x00000004
96527a6266SJeff Kirsher #define  FORCE_LINK_PASS		0x00000002
97527a6266SJeff Kirsher #define  SERIAL_PORT_ENABLE		0x00000001
98527a6266SJeff Kirsher #define PORT_STATUS			0x0044
99527a6266SJeff Kirsher #define  TX_FIFO_EMPTY			0x00000400
100527a6266SJeff Kirsher #define  TX_IN_PROGRESS			0x00000080
101527a6266SJeff Kirsher #define  PORT_SPEED_MASK		0x00000030
102527a6266SJeff Kirsher #define  PORT_SPEED_1000		0x00000010
103527a6266SJeff Kirsher #define  PORT_SPEED_100			0x00000020
104527a6266SJeff Kirsher #define  PORT_SPEED_10			0x00000000
105527a6266SJeff Kirsher #define  FLOW_CONTROL_ENABLED		0x00000008
106527a6266SJeff Kirsher #define  FULL_DUPLEX			0x00000004
107527a6266SJeff Kirsher #define  LINK_UP			0x00000002
108527a6266SJeff Kirsher #define TXQ_COMMAND			0x0048
109527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF		0x004c
110cb85215fSSebastian Hesselbarth #define PORT_SERIAL_CONTROL1		0x004c
111d08cb255SDavid Yang #define  RGMII_EN			0x00000008
112cb85215fSSebastian Hesselbarth #define  CLK125_BYPASS_EN		0x00000010
113527a6266SJeff Kirsher #define TX_BW_RATE			0x0050
114527a6266SJeff Kirsher #define TX_BW_MTU			0x0058
115527a6266SJeff Kirsher #define TX_BW_BURST			0x005c
116527a6266SJeff Kirsher #define INT_CAUSE			0x0060
117527a6266SJeff Kirsher #define  INT_TX_END			0x07f80000
118527a6266SJeff Kirsher #define  INT_TX_END_0			0x00080000
119527a6266SJeff Kirsher #define  INT_RX				0x000003fc
120527a6266SJeff Kirsher #define  INT_RX_0			0x00000004
121527a6266SJeff Kirsher #define  INT_EXT			0x00000002
122527a6266SJeff Kirsher #define INT_CAUSE_EXT			0x0064
123527a6266SJeff Kirsher #define  INT_EXT_LINK_PHY		0x00110000
124527a6266SJeff Kirsher #define  INT_EXT_TX			0x000000ff
125527a6266SJeff Kirsher #define INT_MASK			0x0068
126527a6266SJeff Kirsher #define INT_MASK_EXT			0x006c
127527a6266SJeff Kirsher #define TX_FIFO_URGENT_THRESHOLD	0x0074
128302476c9SPaulius Zaleckas #define RX_DISCARD_FRAME_CNT		0x0084
129302476c9SPaulius Zaleckas #define RX_OVERRUN_FRAME_CNT		0x0088
130527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF_MOVED		0x00dc
131527a6266SJeff Kirsher #define TX_BW_RATE_MOVED		0x00e0
132527a6266SJeff Kirsher #define TX_BW_MTU_MOVED			0x00e8
133527a6266SJeff Kirsher #define TX_BW_BURST_MOVED		0x00ec
134527a6266SJeff Kirsher #define RXQ_CURRENT_DESC_PTR(q)		(0x020c + ((q) << 4))
135527a6266SJeff Kirsher #define RXQ_COMMAND			0x0280
136527a6266SJeff Kirsher #define TXQ_CURRENT_DESC_PTR(q)		(0x02c0 + ((q) << 2))
137527a6266SJeff Kirsher #define TXQ_BW_TOKENS(q)		(0x0300 + ((q) << 4))
138527a6266SJeff Kirsher #define TXQ_BW_CONF(q)			(0x0304 + ((q) << 4))
139527a6266SJeff Kirsher #define TXQ_BW_WRR_CONF(q)		(0x0308 + ((q) << 4))
140527a6266SJeff Kirsher 
141527a6266SJeff Kirsher /*
142527a6266SJeff Kirsher  * Misc per-port registers.
143527a6266SJeff Kirsher  */
144527a6266SJeff Kirsher #define MIB_COUNTERS(p)			(0x1000 + ((p) << 7))
145527a6266SJeff Kirsher #define SPECIAL_MCAST_TABLE(p)		(0x1400 + ((p) << 10))
146527a6266SJeff Kirsher #define OTHER_MCAST_TABLE(p)		(0x1500 + ((p) << 10))
147527a6266SJeff Kirsher #define UNICAST_TABLE(p)		(0x1600 + ((p) << 10))
148527a6266SJeff Kirsher 
149527a6266SJeff Kirsher 
150527a6266SJeff Kirsher /*
151527a6266SJeff Kirsher  * SDMA configuration register default value.
152527a6266SJeff Kirsher  */
153527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
154527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
155527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
156527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
157527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
158527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE		\
159527a6266SJeff Kirsher 		(RX_BURST_SIZE_4_64BIT	|	\
160527a6266SJeff Kirsher 		 BLM_RX_NO_SWAP		|	\
161527a6266SJeff Kirsher 		 BLM_TX_NO_SWAP		|	\
162527a6266SJeff Kirsher 		 TX_BURST_SIZE_4_64BIT)
163527a6266SJeff Kirsher #else
164527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
165527a6266SJeff Kirsher #endif
166527a6266SJeff Kirsher 
167527a6266SJeff Kirsher 
168527a6266SJeff Kirsher /*
169527a6266SJeff Kirsher  * Misc definitions.
170527a6266SJeff Kirsher  */
171527a6266SJeff Kirsher #define DEFAULT_RX_QUEUE_SIZE	128
1723ae8f4e0SEzequiel Garcia #define DEFAULT_TX_QUEUE_SIZE	512
173527a6266SJeff Kirsher #define SKB_DMA_REALIGN		((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
174527a6266SJeff Kirsher 
175ee9e4956SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */
176ee9e4956SEzequiel Garcia #define MV643XX_MAX_TSO_SEGS 100
177ee9e4956SEzequiel Garcia #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS)
178ee9e4956SEzequiel Garcia 
179b926260cSEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \
180b926260cSEzequiel Garcia 	((addr >= txq->tso_hdrs_dma) && \
181b926260cSEzequiel Garcia 	 (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE))
1829e911414SEzequiel Garcia 
1839e911414SEzequiel Garcia #define DESC_DMA_MAP_SINGLE 0
1849e911414SEzequiel Garcia #define DESC_DMA_MAP_PAGE 1
1859e911414SEzequiel Garcia 
186527a6266SJeff Kirsher /*
187527a6266SJeff Kirsher  * RX/TX descriptors.
188527a6266SJeff Kirsher  */
189527a6266SJeff Kirsher #if defined(__BIG_ENDIAN)
190527a6266SJeff Kirsher struct rx_desc {
191527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
192527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
193527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
194527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
195527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
196527a6266SJeff Kirsher };
197527a6266SJeff Kirsher 
198527a6266SJeff Kirsher struct tx_desc {
199527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
200527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
201527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
202527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
203527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
204527a6266SJeff Kirsher };
205527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN)
206527a6266SJeff Kirsher struct rx_desc {
207527a6266SJeff Kirsher 	u32 cmd_sts;		/* Descriptor command status		*/
208527a6266SJeff Kirsher 	u16 buf_size;		/* Buffer size				*/
209527a6266SJeff Kirsher 	u16 byte_cnt;		/* Descriptor buffer byte count		*/
210527a6266SJeff Kirsher 	u32 buf_ptr;		/* Descriptor buffer pointer		*/
211527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Next descriptor pointer		*/
212527a6266SJeff Kirsher };
213527a6266SJeff Kirsher 
214527a6266SJeff Kirsher struct tx_desc {
215527a6266SJeff Kirsher 	u32 cmd_sts;		/* Command/status field			*/
216527a6266SJeff Kirsher 	u16 l4i_chk;		/* CPU provided TCP checksum		*/
217527a6266SJeff Kirsher 	u16 byte_cnt;		/* buffer byte count			*/
218527a6266SJeff Kirsher 	u32 buf_ptr;		/* pointer to buffer for this descriptor*/
219527a6266SJeff Kirsher 	u32 next_desc_ptr;	/* Pointer to next descriptor		*/
220527a6266SJeff Kirsher };
221527a6266SJeff Kirsher #else
222527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
223527a6266SJeff Kirsher #endif
224527a6266SJeff Kirsher 
225527a6266SJeff Kirsher /* RX & TX descriptor command */
226527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA		0x80000000
227527a6266SJeff Kirsher 
228527a6266SJeff Kirsher /* RX & TX descriptor status */
229527a6266SJeff Kirsher #define ERROR_SUMMARY			0x00000001
230527a6266SJeff Kirsher 
231527a6266SJeff Kirsher /* RX descriptor status */
232527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK		0x40000000
233527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT		0x20000000
234527a6266SJeff Kirsher #define RX_FIRST_DESC			0x08000000
235527a6266SJeff Kirsher #define RX_LAST_DESC			0x04000000
236527a6266SJeff Kirsher #define RX_IP_HDR_OK			0x02000000
237527a6266SJeff Kirsher #define RX_PKT_IS_IPV4			0x01000000
238527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2		0x00800000
239527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK		0x00600000
240527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4	0x00000000
241527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED		0x00080000
242527a6266SJeff Kirsher 
243527a6266SJeff Kirsher /* TX descriptor command */
244527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT		0x00800000
245527a6266SJeff Kirsher #define GEN_CRC				0x00400000
246527a6266SJeff Kirsher #define TX_FIRST_DESC			0x00200000
247527a6266SJeff Kirsher #define TX_LAST_DESC			0x00100000
248527a6266SJeff Kirsher #define ZERO_PADDING			0x00080000
249527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM		0x00040000
250527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM		0x00020000
251527a6266SJeff Kirsher #define UDP_FRAME			0x00010000
252527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES		0x00008000
25384411f73SEzequiel Garcia #define GEN_TCP_UDP_CHK_FULL		0x00000400
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 	 * Per-port MBUS window access register value.
268527a6266SJeff Kirsher 	 */
269527a6266SJeff Kirsher 	u32 win_protect;
270527a6266SJeff Kirsher 
271527a6266SJeff Kirsher 	/*
272527a6266SJeff Kirsher 	 * Hardware-specific parameters.
273527a6266SJeff Kirsher 	 */
274527a6266SJeff Kirsher 	int extended_rx_coal_limit;
275527a6266SJeff Kirsher 	int tx_bw_control;
276527a6266SJeff Kirsher 	int tx_csum_limit;
27720922486SSebastian Hesselbarth 	struct clk *clk;
278527a6266SJeff Kirsher };
279527a6266SJeff Kirsher 
280527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT		0
281527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT	1
282527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT	2
283527a6266SJeff Kirsher 
284527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev);
285527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev);
286527a6266SJeff Kirsher 
287527a6266SJeff Kirsher 
288527a6266SJeff Kirsher /* per-port *****************************************************************/
289527a6266SJeff Kirsher struct mib_counters {
290527a6266SJeff Kirsher 	u64 good_octets_received;
291527a6266SJeff Kirsher 	u32 bad_octets_received;
292527a6266SJeff Kirsher 	u32 internal_mac_transmit_err;
293527a6266SJeff Kirsher 	u32 good_frames_received;
294527a6266SJeff Kirsher 	u32 bad_frames_received;
295527a6266SJeff Kirsher 	u32 broadcast_frames_received;
296527a6266SJeff Kirsher 	u32 multicast_frames_received;
297527a6266SJeff Kirsher 	u32 frames_64_octets;
298527a6266SJeff Kirsher 	u32 frames_65_to_127_octets;
299527a6266SJeff Kirsher 	u32 frames_128_to_255_octets;
300527a6266SJeff Kirsher 	u32 frames_256_to_511_octets;
301527a6266SJeff Kirsher 	u32 frames_512_to_1023_octets;
302527a6266SJeff Kirsher 	u32 frames_1024_to_max_octets;
303527a6266SJeff Kirsher 	u64 good_octets_sent;
304527a6266SJeff Kirsher 	u32 good_frames_sent;
305527a6266SJeff Kirsher 	u32 excessive_collision;
306527a6266SJeff Kirsher 	u32 multicast_frames_sent;
307527a6266SJeff Kirsher 	u32 broadcast_frames_sent;
308527a6266SJeff Kirsher 	u32 unrec_mac_control_received;
309527a6266SJeff Kirsher 	u32 fc_sent;
310527a6266SJeff Kirsher 	u32 good_fc_received;
311527a6266SJeff Kirsher 	u32 bad_fc_received;
312527a6266SJeff Kirsher 	u32 undersize_received;
313527a6266SJeff Kirsher 	u32 fragments_received;
314527a6266SJeff Kirsher 	u32 oversize_received;
315527a6266SJeff Kirsher 	u32 jabber_received;
316527a6266SJeff Kirsher 	u32 mac_receive_error;
317527a6266SJeff Kirsher 	u32 bad_crc_event;
318527a6266SJeff Kirsher 	u32 collision;
319527a6266SJeff Kirsher 	u32 late_collision;
320302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
321302476c9SPaulius Zaleckas 	u32 rx_discard;
322302476c9SPaulius Zaleckas 	u32 rx_overrun;
323527a6266SJeff Kirsher };
324527a6266SJeff Kirsher 
325527a6266SJeff Kirsher struct rx_queue {
326527a6266SJeff Kirsher 	int index;
327527a6266SJeff Kirsher 
328527a6266SJeff Kirsher 	int rx_ring_size;
329527a6266SJeff Kirsher 
330527a6266SJeff Kirsher 	int rx_desc_count;
331527a6266SJeff Kirsher 	int rx_curr_desc;
332527a6266SJeff Kirsher 	int rx_used_desc;
333527a6266SJeff Kirsher 
334527a6266SJeff Kirsher 	struct rx_desc *rx_desc_area;
335527a6266SJeff Kirsher 	dma_addr_t rx_desc_dma;
336527a6266SJeff Kirsher 	int rx_desc_area_size;
337527a6266SJeff Kirsher 	struct sk_buff **rx_skb;
338527a6266SJeff Kirsher };
339527a6266SJeff Kirsher 
340527a6266SJeff Kirsher struct tx_queue {
341527a6266SJeff Kirsher 	int index;
342527a6266SJeff Kirsher 
343527a6266SJeff Kirsher 	int tx_ring_size;
344527a6266SJeff Kirsher 
345527a6266SJeff Kirsher 	int tx_desc_count;
346527a6266SJeff Kirsher 	int tx_curr_desc;
347527a6266SJeff Kirsher 	int tx_used_desc;
348527a6266SJeff Kirsher 
349ee9e4956SEzequiel Garcia 	int tx_stop_threshold;
350ee9e4956SEzequiel Garcia 	int tx_wake_threshold;
351ee9e4956SEzequiel Garcia 
3523ae8f4e0SEzequiel Garcia 	char *tso_hdrs;
3533ae8f4e0SEzequiel Garcia 	dma_addr_t tso_hdrs_dma;
3543ae8f4e0SEzequiel Garcia 
355527a6266SJeff Kirsher 	struct tx_desc *tx_desc_area;
3569e911414SEzequiel Garcia 	char *tx_desc_mapping; /* array to track the type of the dma mapping */
357527a6266SJeff Kirsher 	dma_addr_t tx_desc_dma;
358527a6266SJeff Kirsher 	int tx_desc_area_size;
359527a6266SJeff Kirsher 
360527a6266SJeff Kirsher 	struct sk_buff_head tx_skb;
361527a6266SJeff Kirsher 
362527a6266SJeff Kirsher 	unsigned long tx_packets;
363527a6266SJeff Kirsher 	unsigned long tx_bytes;
364527a6266SJeff Kirsher 	unsigned long tx_dropped;
365527a6266SJeff Kirsher };
366527a6266SJeff Kirsher 
367527a6266SJeff Kirsher struct mv643xx_eth_private {
368527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *shared;
369527a6266SJeff Kirsher 	void __iomem *base;
370527a6266SJeff Kirsher 	int port_num;
371527a6266SJeff Kirsher 
372527a6266SJeff Kirsher 	struct net_device *dev;
373527a6266SJeff Kirsher 
374527a6266SJeff Kirsher 	struct timer_list mib_counters_timer;
375527a6266SJeff Kirsher 	spinlock_t mib_counters_lock;
376527a6266SJeff Kirsher 	struct mib_counters mib_counters;
377527a6266SJeff Kirsher 
378527a6266SJeff Kirsher 	struct work_struct tx_timeout_task;
379527a6266SJeff Kirsher 
380527a6266SJeff Kirsher 	struct napi_struct napi;
381527a6266SJeff Kirsher 	u32 int_mask;
382527a6266SJeff Kirsher 	u8 oom;
383527a6266SJeff Kirsher 	u8 work_link;
384527a6266SJeff Kirsher 	u8 work_tx;
385527a6266SJeff Kirsher 	u8 work_tx_end;
386527a6266SJeff Kirsher 	u8 work_rx;
387527a6266SJeff Kirsher 	u8 work_rx_refill;
388527a6266SJeff Kirsher 
389527a6266SJeff Kirsher 	int skb_size;
390527a6266SJeff Kirsher 
391527a6266SJeff Kirsher 	/*
392527a6266SJeff Kirsher 	 * RX state.
393527a6266SJeff Kirsher 	 */
394527a6266SJeff Kirsher 	int rx_ring_size;
395527a6266SJeff Kirsher 	unsigned long rx_desc_sram_addr;
396527a6266SJeff Kirsher 	int rx_desc_sram_size;
397527a6266SJeff Kirsher 	int rxq_count;
398527a6266SJeff Kirsher 	struct timer_list rx_oom;
399527a6266SJeff Kirsher 	struct rx_queue rxq[8];
400527a6266SJeff Kirsher 
401527a6266SJeff Kirsher 	/*
402527a6266SJeff Kirsher 	 * TX state.
403527a6266SJeff Kirsher 	 */
404527a6266SJeff Kirsher 	int tx_ring_size;
405527a6266SJeff Kirsher 	unsigned long tx_desc_sram_addr;
406527a6266SJeff Kirsher 	int tx_desc_sram_size;
407527a6266SJeff Kirsher 	int txq_count;
408527a6266SJeff Kirsher 	struct tx_queue txq[8];
409452503ebSAndrew Lunn 
410452503ebSAndrew Lunn 	/*
411452503ebSAndrew Lunn 	 * Hardware-specific parameters.
412452503ebSAndrew Lunn 	 */
413452503ebSAndrew Lunn 	struct clk *clk;
414452503ebSAndrew Lunn 	unsigned int t_clk;
415527a6266SJeff Kirsher };
416527a6266SJeff Kirsher 
417527a6266SJeff Kirsher 
418527a6266SJeff Kirsher /* port register accessors **************************************************/
rdl(struct mv643xx_eth_private * mp,int offset)419527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
420527a6266SJeff Kirsher {
421527a6266SJeff Kirsher 	return readl(mp->shared->base + offset);
422527a6266SJeff Kirsher }
423527a6266SJeff Kirsher 
rdlp(struct mv643xx_eth_private * mp,int offset)424527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
425527a6266SJeff Kirsher {
426527a6266SJeff Kirsher 	return readl(mp->base + offset);
427527a6266SJeff Kirsher }
428527a6266SJeff Kirsher 
wrl(struct mv643xx_eth_private * mp,int offset,u32 data)429527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
430527a6266SJeff Kirsher {
431527a6266SJeff Kirsher 	writel(data, mp->shared->base + offset);
432527a6266SJeff Kirsher }
433527a6266SJeff Kirsher 
wrlp(struct mv643xx_eth_private * mp,int offset,u32 data)434527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
435527a6266SJeff Kirsher {
436527a6266SJeff Kirsher 	writel(data, mp->base + offset);
437527a6266SJeff Kirsher }
438527a6266SJeff Kirsher 
439527a6266SJeff Kirsher 
440527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/
rxq_to_mp(struct rx_queue * rxq)441527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
442527a6266SJeff Kirsher {
443527a6266SJeff Kirsher 	return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
444527a6266SJeff Kirsher }
445527a6266SJeff Kirsher 
txq_to_mp(struct tx_queue * txq)446527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
447527a6266SJeff Kirsher {
448527a6266SJeff Kirsher 	return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
449527a6266SJeff Kirsher }
450527a6266SJeff Kirsher 
rxq_enable(struct rx_queue * rxq)451527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq)
452527a6266SJeff Kirsher {
453527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
454527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
455527a6266SJeff Kirsher }
456527a6266SJeff Kirsher 
rxq_disable(struct rx_queue * rxq)457527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq)
458527a6266SJeff Kirsher {
459527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
460527a6266SJeff Kirsher 	u8 mask = 1 << rxq->index;
461527a6266SJeff Kirsher 
462527a6266SJeff Kirsher 	wrlp(mp, RXQ_COMMAND, mask << 8);
463527a6266SJeff Kirsher 	while (rdlp(mp, RXQ_COMMAND) & mask)
464527a6266SJeff Kirsher 		udelay(10);
465527a6266SJeff Kirsher }
466527a6266SJeff Kirsher 
txq_reset_hw_ptr(struct tx_queue * txq)467527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq)
468527a6266SJeff Kirsher {
469527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
470527a6266SJeff Kirsher 	u32 addr;
471527a6266SJeff Kirsher 
472527a6266SJeff Kirsher 	addr = (u32)txq->tx_desc_dma;
473527a6266SJeff Kirsher 	addr += txq->tx_curr_desc * sizeof(struct tx_desc);
474527a6266SJeff Kirsher 	wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
475527a6266SJeff Kirsher }
476527a6266SJeff Kirsher 
txq_enable(struct tx_queue * txq)477527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq)
478527a6266SJeff Kirsher {
479527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
480527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, 1 << txq->index);
481527a6266SJeff Kirsher }
482527a6266SJeff Kirsher 
txq_disable(struct tx_queue * txq)483527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq)
484527a6266SJeff Kirsher {
485527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
486527a6266SJeff Kirsher 	u8 mask = 1 << txq->index;
487527a6266SJeff Kirsher 
488527a6266SJeff Kirsher 	wrlp(mp, TXQ_COMMAND, mask << 8);
489527a6266SJeff Kirsher 	while (rdlp(mp, TXQ_COMMAND) & mask)
490527a6266SJeff Kirsher 		udelay(10);
491527a6266SJeff Kirsher }
492527a6266SJeff Kirsher 
txq_maybe_wake(struct tx_queue * txq)493527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq)
494527a6266SJeff Kirsher {
495527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
496527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
497527a6266SJeff Kirsher 
498527a6266SJeff Kirsher 	if (netif_tx_queue_stopped(nq)) {
499527a6266SJeff Kirsher 		__netif_tx_lock(nq, smp_processor_id());
500ee9e4956SEzequiel Garcia 		if (txq->tx_desc_count <= txq->tx_wake_threshold)
501527a6266SJeff Kirsher 			netif_tx_wake_queue(nq);
502527a6266SJeff Kirsher 		__netif_tx_unlock(nq);
503527a6266SJeff Kirsher 	}
504527a6266SJeff Kirsher }
505527a6266SJeff Kirsher 
rxq_process(struct rx_queue * rxq,int budget)506527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget)
507527a6266SJeff Kirsher {
508527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
509527a6266SJeff Kirsher 	struct net_device_stats *stats = &mp->dev->stats;
510527a6266SJeff Kirsher 	int rx;
511527a6266SJeff Kirsher 
512527a6266SJeff Kirsher 	rx = 0;
513527a6266SJeff Kirsher 	while (rx < budget && rxq->rx_desc_count) {
514527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
515527a6266SJeff Kirsher 		unsigned int cmd_sts;
516527a6266SJeff Kirsher 		struct sk_buff *skb;
517527a6266SJeff Kirsher 		u16 byte_cnt;
518527a6266SJeff Kirsher 
519527a6266SJeff Kirsher 		rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
520527a6266SJeff Kirsher 
521527a6266SJeff Kirsher 		cmd_sts = rx_desc->cmd_sts;
522527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA)
523527a6266SJeff Kirsher 			break;
524527a6266SJeff Kirsher 		rmb();
525527a6266SJeff Kirsher 
526527a6266SJeff Kirsher 		skb = rxq->rx_skb[rxq->rx_curr_desc];
527527a6266SJeff Kirsher 		rxq->rx_skb[rxq->rx_curr_desc] = NULL;
528527a6266SJeff Kirsher 
529527a6266SJeff Kirsher 		rxq->rx_curr_desc++;
530527a6266SJeff Kirsher 		if (rxq->rx_curr_desc == rxq->rx_ring_size)
531527a6266SJeff Kirsher 			rxq->rx_curr_desc = 0;
532527a6266SJeff Kirsher 
533527a6266SJeff Kirsher 		dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
534527a6266SJeff Kirsher 				 rx_desc->buf_size, DMA_FROM_DEVICE);
535527a6266SJeff Kirsher 		rxq->rx_desc_count--;
536527a6266SJeff Kirsher 		rx++;
537527a6266SJeff Kirsher 
538527a6266SJeff Kirsher 		mp->work_rx_refill |= 1 << rxq->index;
539527a6266SJeff Kirsher 
540527a6266SJeff Kirsher 		byte_cnt = rx_desc->byte_cnt;
541527a6266SJeff Kirsher 
542527a6266SJeff Kirsher 		/*
543527a6266SJeff Kirsher 		 * Update statistics.
544527a6266SJeff Kirsher 		 *
545527a6266SJeff Kirsher 		 * Note that the descriptor byte count includes 2 dummy
546527a6266SJeff Kirsher 		 * bytes automatically inserted by the hardware at the
547527a6266SJeff Kirsher 		 * start of the packet (which we don't count), and a 4
548527a6266SJeff Kirsher 		 * byte CRC at the end of the packet (which we do count).
549527a6266SJeff Kirsher 		 */
550527a6266SJeff Kirsher 		stats->rx_packets++;
551527a6266SJeff Kirsher 		stats->rx_bytes += byte_cnt - 2;
552527a6266SJeff Kirsher 
553527a6266SJeff Kirsher 		/*
554527a6266SJeff Kirsher 		 * In case we received a packet without first / last bits
555527a6266SJeff Kirsher 		 * on, or the error summary bit is set, the packet needs
556527a6266SJeff Kirsher 		 * to be dropped.
557527a6266SJeff Kirsher 		 */
558527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
559527a6266SJeff Kirsher 			!= (RX_FIRST_DESC | RX_LAST_DESC))
560527a6266SJeff Kirsher 			goto err;
561527a6266SJeff Kirsher 
562527a6266SJeff Kirsher 		/*
563527a6266SJeff Kirsher 		 * The -4 is for the CRC in the trailer of the
564527a6266SJeff Kirsher 		 * received packet
565527a6266SJeff Kirsher 		 */
566527a6266SJeff Kirsher 		skb_put(skb, byte_cnt - 2 - 4);
567527a6266SJeff Kirsher 
568527a6266SJeff Kirsher 		if (cmd_sts & LAYER_4_CHECKSUM_OK)
569527a6266SJeff Kirsher 			skb->ip_summed = CHECKSUM_UNNECESSARY;
570527a6266SJeff Kirsher 		skb->protocol = eth_type_trans(skb, mp->dev);
571527a6266SJeff Kirsher 
57209bf1c10SSebastian Hesselbarth 		napi_gro_receive(&mp->napi, skb);
573527a6266SJeff Kirsher 
574527a6266SJeff Kirsher 		continue;
575527a6266SJeff Kirsher 
576527a6266SJeff Kirsher err:
577527a6266SJeff Kirsher 		stats->rx_dropped++;
578527a6266SJeff Kirsher 
579527a6266SJeff Kirsher 		if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
580527a6266SJeff Kirsher 			(RX_FIRST_DESC | RX_LAST_DESC)) {
581527a6266SJeff Kirsher 			if (net_ratelimit())
582527a6266SJeff Kirsher 				netdev_err(mp->dev,
583527a6266SJeff Kirsher 					   "received packet spanning multiple descriptors\n");
584527a6266SJeff Kirsher 		}
585527a6266SJeff Kirsher 
586527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY)
587527a6266SJeff Kirsher 			stats->rx_errors++;
588527a6266SJeff Kirsher 
589527a6266SJeff Kirsher 		dev_kfree_skb(skb);
590527a6266SJeff Kirsher 	}
591527a6266SJeff Kirsher 
592527a6266SJeff Kirsher 	if (rx < budget)
593527a6266SJeff Kirsher 		mp->work_rx &= ~(1 << rxq->index);
594527a6266SJeff Kirsher 
595527a6266SJeff Kirsher 	return rx;
596527a6266SJeff Kirsher }
597527a6266SJeff Kirsher 
rxq_refill(struct rx_queue * rxq,int budget)598527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget)
599527a6266SJeff Kirsher {
600527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
601527a6266SJeff Kirsher 	int refilled;
602527a6266SJeff Kirsher 
603527a6266SJeff Kirsher 	refilled = 0;
604527a6266SJeff Kirsher 	while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
605527a6266SJeff Kirsher 		struct sk_buff *skb;
606527a6266SJeff Kirsher 		int rx;
607527a6266SJeff Kirsher 		struct rx_desc *rx_desc;
608527a6266SJeff Kirsher 		int size;
609527a6266SJeff Kirsher 
610c056b734SPradeep A Dalvi 		skb = netdev_alloc_skb(mp->dev, mp->skb_size);
611527a6266SJeff Kirsher 
612527a6266SJeff Kirsher 		if (skb == NULL) {
613527a6266SJeff Kirsher 			mp->oom = 1;
614527a6266SJeff Kirsher 			goto oom;
615527a6266SJeff Kirsher 		}
616527a6266SJeff Kirsher 
617527a6266SJeff Kirsher 		if (SKB_DMA_REALIGN)
618527a6266SJeff Kirsher 			skb_reserve(skb, SKB_DMA_REALIGN);
619527a6266SJeff Kirsher 
620527a6266SJeff Kirsher 		refilled++;
621527a6266SJeff Kirsher 		rxq->rx_desc_count++;
622527a6266SJeff Kirsher 
623527a6266SJeff Kirsher 		rx = rxq->rx_used_desc++;
624527a6266SJeff Kirsher 		if (rxq->rx_used_desc == rxq->rx_ring_size)
625527a6266SJeff Kirsher 			rxq->rx_used_desc = 0;
626527a6266SJeff Kirsher 
627527a6266SJeff Kirsher 		rx_desc = rxq->rx_desc_area + rx;
628527a6266SJeff Kirsher 
62918f1d054SIsaku Yamahata 		size = skb_end_pointer(skb) - skb->data;
630527a6266SJeff Kirsher 		rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
631527a6266SJeff Kirsher 						  skb->data, size,
632527a6266SJeff Kirsher 						  DMA_FROM_DEVICE);
633527a6266SJeff Kirsher 		rx_desc->buf_size = size;
634527a6266SJeff Kirsher 		rxq->rx_skb[rx] = skb;
635527a6266SJeff Kirsher 		wmb();
636527a6266SJeff Kirsher 		rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
637527a6266SJeff Kirsher 		wmb();
638527a6266SJeff Kirsher 
639527a6266SJeff Kirsher 		/*
640527a6266SJeff Kirsher 		 * The hardware automatically prepends 2 bytes of
641527a6266SJeff Kirsher 		 * dummy data to each received packet, so that the
642527a6266SJeff Kirsher 		 * IP header ends up 16-byte aligned.
643527a6266SJeff Kirsher 		 */
644527a6266SJeff Kirsher 		skb_reserve(skb, 2);
645527a6266SJeff Kirsher 	}
646527a6266SJeff Kirsher 
647527a6266SJeff Kirsher 	if (refilled < budget)
648527a6266SJeff Kirsher 		mp->work_rx_refill &= ~(1 << rxq->index);
649527a6266SJeff Kirsher 
650527a6266SJeff Kirsher oom:
651527a6266SJeff Kirsher 	return refilled;
652527a6266SJeff Kirsher }
653527a6266SJeff Kirsher 
654527a6266SJeff Kirsher 
655527a6266SJeff Kirsher /* tx ***********************************************************************/
has_tiny_unaligned_frags(struct sk_buff * skb)656527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
657527a6266SJeff Kirsher {
658527a6266SJeff Kirsher 	int frag;
659527a6266SJeff Kirsher 
660527a6266SJeff Kirsher 	for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
6619e903e08SEric Dumazet 		const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
6629e903e08SEric Dumazet 
663b54c9d5bSJonathan Lemon 		if (skb_frag_size(fragp) <= 8 && skb_frag_off(fragp) & 7)
664527a6266SJeff Kirsher 			return 1;
665527a6266SJeff Kirsher 	}
666527a6266SJeff Kirsher 
667527a6266SJeff Kirsher 	return 0;
668527a6266SJeff Kirsher }
669527a6266SJeff Kirsher 
skb_tx_csum(struct mv643xx_eth_private * mp,struct sk_buff * skb,u16 * l4i_chk,u32 * command,int length)6700a8fa933SEzequiel Garcia static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb,
6710a8fa933SEzequiel Garcia 		       u16 *l4i_chk, u32 *command, int length)
6720a8fa933SEzequiel Garcia {
6730a8fa933SEzequiel Garcia 	int ret;
6740a8fa933SEzequiel Garcia 	u32 cmd = 0;
6750a8fa933SEzequiel Garcia 
6760a8fa933SEzequiel Garcia 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
6770a8fa933SEzequiel Garcia 		int hdr_len;
6780a8fa933SEzequiel Garcia 		int tag_bytes;
6790a8fa933SEzequiel Garcia 
6800a8fa933SEzequiel Garcia 		BUG_ON(skb->protocol != htons(ETH_P_IP) &&
6810a8fa933SEzequiel Garcia 		       skb->protocol != htons(ETH_P_8021Q));
6820a8fa933SEzequiel Garcia 
6830a8fa933SEzequiel Garcia 		hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
6840a8fa933SEzequiel Garcia 		tag_bytes = hdr_len - ETH_HLEN;
6850a8fa933SEzequiel Garcia 
6860a8fa933SEzequiel Garcia 		if (length - hdr_len > mp->shared->tx_csum_limit ||
6870a8fa933SEzequiel Garcia 		    unlikely(tag_bytes & ~12)) {
6880a8fa933SEzequiel Garcia 			ret = skb_checksum_help(skb);
6890a8fa933SEzequiel Garcia 			if (!ret)
6900a8fa933SEzequiel Garcia 				goto no_csum;
6910a8fa933SEzequiel Garcia 			return ret;
6920a8fa933SEzequiel Garcia 		}
6930a8fa933SEzequiel Garcia 
6940a8fa933SEzequiel Garcia 		if (tag_bytes & 4)
6950a8fa933SEzequiel Garcia 			cmd |= MAC_HDR_EXTRA_4_BYTES;
6960a8fa933SEzequiel Garcia 		if (tag_bytes & 8)
6970a8fa933SEzequiel Garcia 			cmd |= MAC_HDR_EXTRA_8_BYTES;
6980a8fa933SEzequiel Garcia 
69984411f73SEzequiel Garcia 		cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL |
7000a8fa933SEzequiel Garcia 			   GEN_IP_V4_CHECKSUM   |
7010a8fa933SEzequiel Garcia 			   ip_hdr(skb)->ihl << TX_IHL_SHIFT;
7020a8fa933SEzequiel Garcia 
70384411f73SEzequiel Garcia 		/* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL
704df4a17a9SYangyang Li 		 * it seems we don't need to pass the initial checksum.
705df4a17a9SYangyang Li 		 */
7060a8fa933SEzequiel Garcia 		switch (ip_hdr(skb)->protocol) {
7070a8fa933SEzequiel Garcia 		case IPPROTO_UDP:
7080a8fa933SEzequiel Garcia 			cmd |= UDP_FRAME;
70984411f73SEzequiel Garcia 			*l4i_chk = 0;
7100a8fa933SEzequiel Garcia 			break;
7110a8fa933SEzequiel Garcia 		case IPPROTO_TCP:
71284411f73SEzequiel Garcia 			*l4i_chk = 0;
7130a8fa933SEzequiel Garcia 			break;
7140a8fa933SEzequiel Garcia 		default:
7150a8fa933SEzequiel Garcia 			WARN(1, "protocol not supported");
7160a8fa933SEzequiel Garcia 		}
7170a8fa933SEzequiel Garcia 	} else {
7180a8fa933SEzequiel Garcia no_csum:
7190a8fa933SEzequiel Garcia 		/* Errata BTS #50, IHL must be 5 if no HW checksum */
7200a8fa933SEzequiel Garcia 		cmd |= 5 << TX_IHL_SHIFT;
7210a8fa933SEzequiel Garcia 	}
7220a8fa933SEzequiel Garcia 	*command = cmd;
7230a8fa933SEzequiel Garcia 	return 0;
7240a8fa933SEzequiel Garcia }
7250a8fa933SEzequiel Garcia 
7263ae8f4e0SEzequiel Garcia static inline int
txq_put_data_tso(struct net_device * dev,struct tx_queue * txq,struct sk_buff * skb,char * data,int length,bool last_tcp,bool is_last)7273ae8f4e0SEzequiel Garcia txq_put_data_tso(struct net_device *dev, struct tx_queue *txq,
7283ae8f4e0SEzequiel Garcia 		 struct sk_buff *skb, char *data, int length,
7293ae8f4e0SEzequiel Garcia 		 bool last_tcp, bool is_last)
7303ae8f4e0SEzequiel Garcia {
7313ae8f4e0SEzequiel Garcia 	int tx_index;
7323ae8f4e0SEzequiel Garcia 	u32 cmd_sts;
7333ae8f4e0SEzequiel Garcia 	struct tx_desc *desc;
7343ae8f4e0SEzequiel Garcia 
7353ae8f4e0SEzequiel Garcia 	tx_index = txq->tx_curr_desc++;
7363ae8f4e0SEzequiel Garcia 	if (txq->tx_curr_desc == txq->tx_ring_size)
7373ae8f4e0SEzequiel Garcia 		txq->tx_curr_desc = 0;
7383ae8f4e0SEzequiel Garcia 	desc = &txq->tx_desc_area[tx_index];
7399e911414SEzequiel Garcia 	txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
7403ae8f4e0SEzequiel Garcia 
7413ae8f4e0SEzequiel Garcia 	desc->l4i_chk = 0;
7423ae8f4e0SEzequiel Garcia 	desc->byte_cnt = length;
74391986fd3SPhilipp Kirchhofer 
74491986fd3SPhilipp Kirchhofer 	if (length <= 8 && (uintptr_t)data & 0x7) {
74591986fd3SPhilipp Kirchhofer 		/* Copy unaligned small data fragment to TSO header data area */
7463b89624aSNicolas Schichan 		memcpy(txq->tso_hdrs + tx_index * TSO_HEADER_SIZE,
74791986fd3SPhilipp Kirchhofer 		       data, length);
74891986fd3SPhilipp Kirchhofer 		desc->buf_ptr = txq->tso_hdrs_dma
7493b89624aSNicolas Schichan 			+ tx_index * TSO_HEADER_SIZE;
75091986fd3SPhilipp Kirchhofer 	} else {
75191986fd3SPhilipp Kirchhofer 		/* Alignment is okay, map buffer and hand off to hardware */
75291986fd3SPhilipp Kirchhofer 		txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
7533ae8f4e0SEzequiel Garcia 		desc->buf_ptr = dma_map_single(dev->dev.parent, data,
7543ae8f4e0SEzequiel Garcia 			length, DMA_TO_DEVICE);
75591986fd3SPhilipp Kirchhofer 		if (unlikely(dma_mapping_error(dev->dev.parent,
75691986fd3SPhilipp Kirchhofer 					       desc->buf_ptr))) {
7573ae8f4e0SEzequiel Garcia 			WARN(1, "dma_map_single failed!\n");
7583ae8f4e0SEzequiel Garcia 			return -ENOMEM;
7593ae8f4e0SEzequiel Garcia 		}
76091986fd3SPhilipp Kirchhofer 	}
7613ae8f4e0SEzequiel Garcia 
7623ae8f4e0SEzequiel Garcia 	cmd_sts = BUFFER_OWNED_BY_DMA;
7633ae8f4e0SEzequiel Garcia 	if (last_tcp) {
7643ae8f4e0SEzequiel Garcia 		/* last descriptor in the TCP packet */
7653ae8f4e0SEzequiel Garcia 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC;
7663ae8f4e0SEzequiel Garcia 		/* last descriptor in SKB */
7673ae8f4e0SEzequiel Garcia 		if (is_last)
7683ae8f4e0SEzequiel Garcia 			cmd_sts |= TX_ENABLE_INTERRUPT;
7693ae8f4e0SEzequiel Garcia 	}
7703ae8f4e0SEzequiel Garcia 	desc->cmd_sts = cmd_sts;
7713ae8f4e0SEzequiel Garcia 	return 0;
7723ae8f4e0SEzequiel Garcia }
7733ae8f4e0SEzequiel Garcia 
7743ae8f4e0SEzequiel Garcia static inline void
txq_put_hdr_tso(struct sk_buff * skb,struct tx_queue * txq,int length,u32 * first_cmd_sts,bool first_desc)775968200f3SPhilipp Kirchhofer txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length,
776968200f3SPhilipp Kirchhofer 		u32 *first_cmd_sts, bool first_desc)
7773ae8f4e0SEzequiel Garcia {
7783ae8f4e0SEzequiel Garcia 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
779504148feSEric Dumazet 	int hdr_len = skb_tcp_all_headers(skb);
7803ae8f4e0SEzequiel Garcia 	int tx_index;
7813ae8f4e0SEzequiel Garcia 	struct tx_desc *desc;
7823ae8f4e0SEzequiel Garcia 	int ret;
7833ae8f4e0SEzequiel Garcia 	u32 cmd_csum = 0;
7843ae8f4e0SEzequiel Garcia 	u16 l4i_chk = 0;
785968200f3SPhilipp Kirchhofer 	u32 cmd_sts;
7863ae8f4e0SEzequiel Garcia 
7873ae8f4e0SEzequiel Garcia 	tx_index = txq->tx_curr_desc;
7883ae8f4e0SEzequiel Garcia 	desc = &txq->tx_desc_area[tx_index];
7893ae8f4e0SEzequiel Garcia 
7903ae8f4e0SEzequiel Garcia 	ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_csum, length);
7913ae8f4e0SEzequiel Garcia 	if (ret)
7923ae8f4e0SEzequiel Garcia 		WARN(1, "failed to prepare checksum!");
7933ae8f4e0SEzequiel Garcia 
7943ae8f4e0SEzequiel Garcia 	/* Should we set this? Can't use the value from skb_tx_csum()
795df4a17a9SYangyang Li 	 * as it's not the correct initial L4 checksum to use.
796df4a17a9SYangyang Li 	 */
7973ae8f4e0SEzequiel Garcia 	desc->l4i_chk = 0;
7983ae8f4e0SEzequiel Garcia 
7993ae8f4e0SEzequiel Garcia 	desc->byte_cnt = hdr_len;
8003ae8f4e0SEzequiel Garcia 	desc->buf_ptr = txq->tso_hdrs_dma +
8013ae8f4e0SEzequiel Garcia 			txq->tx_curr_desc * TSO_HEADER_SIZE;
802968200f3SPhilipp Kirchhofer 	cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA  | TX_FIRST_DESC |
8033ae8f4e0SEzequiel Garcia 				   GEN_CRC;
8043ae8f4e0SEzequiel Garcia 
805968200f3SPhilipp Kirchhofer 	/* Defer updating the first command descriptor until all
806968200f3SPhilipp Kirchhofer 	 * following descriptors have been written.
807968200f3SPhilipp Kirchhofer 	 */
808968200f3SPhilipp Kirchhofer 	if (first_desc)
809968200f3SPhilipp Kirchhofer 		*first_cmd_sts = cmd_sts;
810968200f3SPhilipp Kirchhofer 	else
811968200f3SPhilipp Kirchhofer 		desc->cmd_sts = cmd_sts;
812968200f3SPhilipp Kirchhofer 
8133ae8f4e0SEzequiel Garcia 	txq->tx_curr_desc++;
8143ae8f4e0SEzequiel Garcia 	if (txq->tx_curr_desc == txq->tx_ring_size)
8153ae8f4e0SEzequiel Garcia 		txq->tx_curr_desc = 0;
8163ae8f4e0SEzequiel Garcia }
8173ae8f4e0SEzequiel Garcia 
txq_submit_tso(struct tx_queue * txq,struct sk_buff * skb,struct net_device * dev)8183ae8f4e0SEzequiel Garcia static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb,
8193ae8f4e0SEzequiel Garcia 			  struct net_device *dev)
8203ae8f4e0SEzequiel Garcia {
8213ae8f4e0SEzequiel Garcia 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
822761b331cSEric Dumazet 	int hdr_len, total_len, data_left, ret;
8233ae8f4e0SEzequiel Garcia 	int desc_count = 0;
8243ae8f4e0SEzequiel Garcia 	struct tso_t tso;
825968200f3SPhilipp Kirchhofer 	struct tx_desc *first_tx_desc;
826968200f3SPhilipp Kirchhofer 	u32 first_cmd_sts = 0;
8273ae8f4e0SEzequiel Garcia 
8283ae8f4e0SEzequiel Garcia 	/* Count needed descriptors */
8293ae8f4e0SEzequiel Garcia 	if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) {
8303ae8f4e0SEzequiel Garcia 		netdev_dbg(dev, "not enough descriptors for TSO!\n");
8313ae8f4e0SEzequiel Garcia 		return -EBUSY;
8323ae8f4e0SEzequiel Garcia 	}
8333ae8f4e0SEzequiel Garcia 
834968200f3SPhilipp Kirchhofer 	first_tx_desc = &txq->tx_desc_area[txq->tx_curr_desc];
835968200f3SPhilipp Kirchhofer 
8363ae8f4e0SEzequiel Garcia 	/* Initialize the TSO handler, and prepare the first payload */
837761b331cSEric Dumazet 	hdr_len = tso_start(skb, &tso);
8383ae8f4e0SEzequiel Garcia 
8393ae8f4e0SEzequiel Garcia 	total_len = skb->len - hdr_len;
8403ae8f4e0SEzequiel Garcia 	while (total_len > 0) {
841968200f3SPhilipp Kirchhofer 		bool first_desc = (desc_count == 0);
8423ae8f4e0SEzequiel Garcia 		char *hdr;
8433ae8f4e0SEzequiel Garcia 
8443ae8f4e0SEzequiel Garcia 		data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len);
8453ae8f4e0SEzequiel Garcia 		total_len -= data_left;
8463ae8f4e0SEzequiel Garcia 		desc_count++;
8473ae8f4e0SEzequiel Garcia 
8483ae8f4e0SEzequiel Garcia 		/* prepare packet headers: MAC + IP + TCP */
8493ae8f4e0SEzequiel Garcia 		hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE;
8503ae8f4e0SEzequiel Garcia 		tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0);
851968200f3SPhilipp Kirchhofer 		txq_put_hdr_tso(skb, txq, data_left, &first_cmd_sts,
852968200f3SPhilipp Kirchhofer 				first_desc);
8533ae8f4e0SEzequiel Garcia 
8543ae8f4e0SEzequiel Garcia 		while (data_left > 0) {
8553ae8f4e0SEzequiel Garcia 			int size;
8563ae8f4e0SEzequiel Garcia 			desc_count++;
8573ae8f4e0SEzequiel Garcia 
8583ae8f4e0SEzequiel Garcia 			size = min_t(int, tso.size, data_left);
8593ae8f4e0SEzequiel Garcia 			ret = txq_put_data_tso(dev, txq, skb, tso.data, size,
8603ae8f4e0SEzequiel Garcia 					       size == data_left,
8613ae8f4e0SEzequiel Garcia 					       total_len == 0);
8623ae8f4e0SEzequiel Garcia 			if (ret)
8633ae8f4e0SEzequiel Garcia 				goto err_release;
8643ae8f4e0SEzequiel Garcia 			data_left -= size;
8653ae8f4e0SEzequiel Garcia 			tso_build_data(skb, &tso, size);
8663ae8f4e0SEzequiel Garcia 		}
8673ae8f4e0SEzequiel Garcia 	}
8683ae8f4e0SEzequiel Garcia 
8693ae8f4e0SEzequiel Garcia 	__skb_queue_tail(&txq->tx_skb, skb);
8703ae8f4e0SEzequiel Garcia 	skb_tx_timestamp(skb);
8713ae8f4e0SEzequiel Garcia 
872968200f3SPhilipp Kirchhofer 	/* ensure all other descriptors are written before first cmd_sts */
873968200f3SPhilipp Kirchhofer 	wmb();
874968200f3SPhilipp Kirchhofer 	first_tx_desc->cmd_sts = first_cmd_sts;
875968200f3SPhilipp Kirchhofer 
8763ae8f4e0SEzequiel Garcia 	/* clear TX_END status */
8773ae8f4e0SEzequiel Garcia 	mp->work_tx_end &= ~(1 << txq->index);
8783ae8f4e0SEzequiel Garcia 
8793ae8f4e0SEzequiel Garcia 	/* ensure all descriptors are written before poking hardware */
8803ae8f4e0SEzequiel Garcia 	wmb();
8813ae8f4e0SEzequiel Garcia 	txq_enable(txq);
8823ae8f4e0SEzequiel Garcia 	txq->tx_desc_count += desc_count;
8833ae8f4e0SEzequiel Garcia 	return 0;
8843ae8f4e0SEzequiel Garcia err_release:
8853ae8f4e0SEzequiel Garcia 	/* TODO: Release all used data descriptors; header descriptors must not
8863ae8f4e0SEzequiel Garcia 	 * be DMA-unmapped.
8873ae8f4e0SEzequiel Garcia 	 */
8883ae8f4e0SEzequiel Garcia 	return ret;
8893ae8f4e0SEzequiel Garcia }
8903ae8f4e0SEzequiel Garcia 
txq_submit_frag_skb(struct tx_queue * txq,struct sk_buff * skb)891527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
892527a6266SJeff Kirsher {
893527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
894527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
895527a6266SJeff Kirsher 	int frag;
896527a6266SJeff Kirsher 
897527a6266SJeff Kirsher 	for (frag = 0; frag < nr_frags; frag++) {
898527a6266SJeff Kirsher 		skb_frag_t *this_frag;
899527a6266SJeff Kirsher 		int tx_index;
900527a6266SJeff Kirsher 		struct tx_desc *desc;
901527a6266SJeff Kirsher 
902527a6266SJeff Kirsher 		this_frag = &skb_shinfo(skb)->frags[frag];
903527a6266SJeff Kirsher 		tx_index = txq->tx_curr_desc++;
904527a6266SJeff Kirsher 		if (txq->tx_curr_desc == txq->tx_ring_size)
905527a6266SJeff Kirsher 			txq->tx_curr_desc = 0;
906527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
9079e911414SEzequiel Garcia 		txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_PAGE;
908527a6266SJeff Kirsher 
909527a6266SJeff Kirsher 		/*
910527a6266SJeff Kirsher 		 * The last fragment will generate an interrupt
911527a6266SJeff Kirsher 		 * which will free the skb on TX completion.
912527a6266SJeff Kirsher 		 */
913527a6266SJeff Kirsher 		if (frag == nr_frags - 1) {
914527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA |
915527a6266SJeff Kirsher 					ZERO_PADDING | TX_LAST_DESC |
916527a6266SJeff Kirsher 					TX_ENABLE_INTERRUPT;
917527a6266SJeff Kirsher 		} else {
918527a6266SJeff Kirsher 			desc->cmd_sts = BUFFER_OWNED_BY_DMA;
919527a6266SJeff Kirsher 		}
920527a6266SJeff Kirsher 
921527a6266SJeff Kirsher 		desc->l4i_chk = 0;
9229e903e08SEric Dumazet 		desc->byte_cnt = skb_frag_size(this_frag);
9239e911414SEzequiel Garcia 		desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
9249e911414SEzequiel Garcia 						 this_frag, 0, desc->byte_cnt,
9259e911414SEzequiel Garcia 						 DMA_TO_DEVICE);
926527a6266SJeff Kirsher 	}
927527a6266SJeff Kirsher }
928527a6266SJeff Kirsher 
txq_submit_skb(struct tx_queue * txq,struct sk_buff * skb,struct net_device * dev)929ee9e4956SEzequiel Garcia static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb,
930ee9e4956SEzequiel Garcia 			  struct net_device *dev)
931527a6266SJeff Kirsher {
932527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
933527a6266SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
934527a6266SJeff Kirsher 	int tx_index;
935527a6266SJeff Kirsher 	struct tx_desc *desc;
936527a6266SJeff Kirsher 	u32 cmd_sts;
937527a6266SJeff Kirsher 	u16 l4i_chk;
9380a8fa933SEzequiel Garcia 	int length, ret;
939527a6266SJeff Kirsher 
9400a8fa933SEzequiel Garcia 	cmd_sts = 0;
941527a6266SJeff Kirsher 	l4i_chk = 0;
942527a6266SJeff Kirsher 
943ee9e4956SEzequiel Garcia 	if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
944ee9e4956SEzequiel Garcia 		if (net_ratelimit())
945ee9e4956SEzequiel Garcia 			netdev_err(dev, "tx queue full?!\n");
946ee9e4956SEzequiel Garcia 		return -EBUSY;
947527a6266SJeff Kirsher 	}
948ee9e4956SEzequiel Garcia 
949ee9e4956SEzequiel Garcia 	ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len);
950ee9e4956SEzequiel Garcia 	if (ret)
951ee9e4956SEzequiel Garcia 		return ret;
9520a8fa933SEzequiel Garcia 	cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
953527a6266SJeff Kirsher 
954527a6266SJeff Kirsher 	tx_index = txq->tx_curr_desc++;
955527a6266SJeff Kirsher 	if (txq->tx_curr_desc == txq->tx_ring_size)
956527a6266SJeff Kirsher 		txq->tx_curr_desc = 0;
957527a6266SJeff Kirsher 	desc = &txq->tx_desc_area[tx_index];
9589e911414SEzequiel Garcia 	txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE;
959527a6266SJeff Kirsher 
960527a6266SJeff Kirsher 	if (nr_frags) {
961527a6266SJeff Kirsher 		txq_submit_frag_skb(txq, skb);
962527a6266SJeff Kirsher 		length = skb_headlen(skb);
963527a6266SJeff Kirsher 	} else {
964527a6266SJeff Kirsher 		cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
965527a6266SJeff Kirsher 		length = skb->len;
966527a6266SJeff Kirsher 	}
967527a6266SJeff Kirsher 
968527a6266SJeff Kirsher 	desc->l4i_chk = l4i_chk;
969527a6266SJeff Kirsher 	desc->byte_cnt = length;
970527a6266SJeff Kirsher 	desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
971527a6266SJeff Kirsher 				       length, DMA_TO_DEVICE);
972527a6266SJeff Kirsher 
973527a6266SJeff Kirsher 	__skb_queue_tail(&txq->tx_skb, skb);
974527a6266SJeff Kirsher 
975527a6266SJeff Kirsher 	skb_tx_timestamp(skb);
976527a6266SJeff Kirsher 
977527a6266SJeff Kirsher 	/* ensure all other descriptors are written before first cmd_sts */
978527a6266SJeff Kirsher 	wmb();
979527a6266SJeff Kirsher 	desc->cmd_sts = cmd_sts;
980527a6266SJeff Kirsher 
981527a6266SJeff Kirsher 	/* clear TX_END status */
982527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
983527a6266SJeff Kirsher 
984527a6266SJeff Kirsher 	/* ensure all descriptors are written before poking hardware */
985527a6266SJeff Kirsher 	wmb();
986527a6266SJeff Kirsher 	txq_enable(txq);
987527a6266SJeff Kirsher 
988527a6266SJeff Kirsher 	txq->tx_desc_count += nr_frags + 1;
989527a6266SJeff Kirsher 
990527a6266SJeff Kirsher 	return 0;
991527a6266SJeff Kirsher }
992527a6266SJeff Kirsher 
mv643xx_eth_xmit(struct sk_buff * skb,struct net_device * dev)993527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
994527a6266SJeff Kirsher {
995527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
9963ae8f4e0SEzequiel Garcia 	int length, queue, ret;
997527a6266SJeff Kirsher 	struct tx_queue *txq;
998527a6266SJeff Kirsher 	struct netdev_queue *nq;
999527a6266SJeff Kirsher 
1000527a6266SJeff Kirsher 	queue = skb_get_queue_mapping(skb);
1001527a6266SJeff Kirsher 	txq = mp->txq + queue;
1002527a6266SJeff Kirsher 	nq = netdev_get_tx_queue(dev, queue);
1003527a6266SJeff Kirsher 
1004527a6266SJeff Kirsher 	if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1005527a6266SJeff Kirsher 		netdev_printk(KERN_DEBUG, dev,
1006527a6266SJeff Kirsher 			      "failed to linearize skb with tiny unaligned fragment\n");
1007527a6266SJeff Kirsher 		return NETDEV_TX_BUSY;
1008527a6266SJeff Kirsher 	}
1009527a6266SJeff Kirsher 
1010527a6266SJeff Kirsher 	length = skb->len;
1011527a6266SJeff Kirsher 
10123ae8f4e0SEzequiel Garcia 	if (skb_is_gso(skb))
10133ae8f4e0SEzequiel Garcia 		ret = txq_submit_tso(txq, skb, dev);
10143ae8f4e0SEzequiel Garcia 	else
1015ee9e4956SEzequiel Garcia 		ret = txq_submit_skb(txq, skb, dev);
10163ae8f4e0SEzequiel Garcia 	if (!ret) {
1017527a6266SJeff Kirsher 		txq->tx_bytes += length;
1018527a6266SJeff Kirsher 		txq->tx_packets++;
1019527a6266SJeff Kirsher 
1020ee9e4956SEzequiel Garcia 		if (txq->tx_desc_count >= txq->tx_stop_threshold)
1021527a6266SJeff Kirsher 			netif_tx_stop_queue(nq);
1022dd11680dSEzequiel Garcia 	} else {
1023dd11680dSEzequiel Garcia 		txq->tx_dropped++;
1024dd11680dSEzequiel Garcia 		dev_kfree_skb_any(skb);
1025527a6266SJeff Kirsher 	}
1026527a6266SJeff Kirsher 
1027527a6266SJeff Kirsher 	return NETDEV_TX_OK;
1028527a6266SJeff Kirsher }
1029527a6266SJeff Kirsher 
1030527a6266SJeff Kirsher 
1031527a6266SJeff Kirsher /* tx napi ******************************************************************/
txq_kick(struct tx_queue * txq)1032527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq)
1033527a6266SJeff Kirsher {
1034527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1035527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
1036527a6266SJeff Kirsher 	u32 hw_desc_ptr;
1037527a6266SJeff Kirsher 	u32 expected_ptr;
1038527a6266SJeff Kirsher 
1039527a6266SJeff Kirsher 	__netif_tx_lock(nq, smp_processor_id());
1040527a6266SJeff Kirsher 
1041527a6266SJeff Kirsher 	if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
1042527a6266SJeff Kirsher 		goto out;
1043527a6266SJeff Kirsher 
1044527a6266SJeff Kirsher 	hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
1045527a6266SJeff Kirsher 	expected_ptr = (u32)txq->tx_desc_dma +
1046527a6266SJeff Kirsher 				txq->tx_curr_desc * sizeof(struct tx_desc);
1047527a6266SJeff Kirsher 
1048527a6266SJeff Kirsher 	if (hw_desc_ptr != expected_ptr)
1049527a6266SJeff Kirsher 		txq_enable(txq);
1050527a6266SJeff Kirsher 
1051527a6266SJeff Kirsher out:
1052527a6266SJeff Kirsher 	__netif_tx_unlock(nq);
1053527a6266SJeff Kirsher 
1054527a6266SJeff Kirsher 	mp->work_tx_end &= ~(1 << txq->index);
1055527a6266SJeff Kirsher }
1056527a6266SJeff Kirsher 
txq_reclaim(struct tx_queue * txq,int budget,int force)1057527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force)
1058527a6266SJeff Kirsher {
1059527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1060527a6266SJeff Kirsher 	struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
1061527a6266SJeff Kirsher 	int reclaimed;
1062527a6266SJeff Kirsher 
10633aefe2b4SRussell King 	__netif_tx_lock_bh(nq);
1064527a6266SJeff Kirsher 
1065527a6266SJeff Kirsher 	reclaimed = 0;
1066527a6266SJeff Kirsher 	while (reclaimed < budget && txq->tx_desc_count > 0) {
1067527a6266SJeff Kirsher 		int tx_index;
1068527a6266SJeff Kirsher 		struct tx_desc *desc;
1069527a6266SJeff Kirsher 		u32 cmd_sts;
10709e911414SEzequiel Garcia 		char desc_dma_map;
1071527a6266SJeff Kirsher 
1072527a6266SJeff Kirsher 		tx_index = txq->tx_used_desc;
1073527a6266SJeff Kirsher 		desc = &txq->tx_desc_area[tx_index];
10749e911414SEzequiel Garcia 		desc_dma_map = txq->tx_desc_mapping[tx_index];
10759e911414SEzequiel Garcia 
1076527a6266SJeff Kirsher 		cmd_sts = desc->cmd_sts;
1077527a6266SJeff Kirsher 
1078527a6266SJeff Kirsher 		if (cmd_sts & BUFFER_OWNED_BY_DMA) {
1079527a6266SJeff Kirsher 			if (!force)
1080527a6266SJeff Kirsher 				break;
1081527a6266SJeff Kirsher 			desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
1082527a6266SJeff Kirsher 		}
1083527a6266SJeff Kirsher 
1084527a6266SJeff Kirsher 		txq->tx_used_desc = tx_index + 1;
1085527a6266SJeff Kirsher 		if (txq->tx_used_desc == txq->tx_ring_size)
1086527a6266SJeff Kirsher 			txq->tx_used_desc = 0;
1087527a6266SJeff Kirsher 
1088527a6266SJeff Kirsher 		reclaimed++;
1089527a6266SJeff Kirsher 		txq->tx_desc_count--;
1090527a6266SJeff Kirsher 
10919e911414SEzequiel Garcia 		if (!IS_TSO_HEADER(txq, desc->buf_ptr)) {
10929e911414SEzequiel Garcia 
10939e911414SEzequiel Garcia 			if (desc_dma_map == DESC_DMA_MAP_PAGE)
10949e911414SEzequiel Garcia 				dma_unmap_page(mp->dev->dev.parent,
10959e911414SEzequiel Garcia 					       desc->buf_ptr,
10969e911414SEzequiel Garcia 					       desc->byte_cnt,
10979e911414SEzequiel Garcia 					       DMA_TO_DEVICE);
10989e911414SEzequiel Garcia 			else
10999e911414SEzequiel Garcia 				dma_unmap_single(mp->dev->dev.parent,
11009e911414SEzequiel Garcia 						 desc->buf_ptr,
11019e911414SEzequiel Garcia 						 desc->byte_cnt,
11029e911414SEzequiel Garcia 						 DMA_TO_DEVICE);
11039e911414SEzequiel Garcia 		}
11042c2a9cbdSKarl Beldan 
11052c2a9cbdSKarl Beldan 		if (cmd_sts & TX_ENABLE_INTERRUPT) {
11062c2a9cbdSKarl Beldan 			struct sk_buff *skb = __skb_dequeue(&txq->tx_skb);
11072c2a9cbdSKarl Beldan 
11082c2a9cbdSKarl Beldan 			if (!WARN_ON(!skb))
110943cee2d2SFlorian Fainelli 				dev_consume_skb_any(skb);
11102c2a9cbdSKarl Beldan 		}
1111527a6266SJeff Kirsher 
1112527a6266SJeff Kirsher 		if (cmd_sts & ERROR_SUMMARY) {
1113527a6266SJeff Kirsher 			netdev_info(mp->dev, "tx error\n");
1114527a6266SJeff Kirsher 			mp->dev->stats.tx_errors++;
1115527a6266SJeff Kirsher 		}
1116527a6266SJeff Kirsher 
1117527a6266SJeff Kirsher 	}
1118527a6266SJeff Kirsher 
11193aefe2b4SRussell King 	__netif_tx_unlock_bh(nq);
1120527a6266SJeff Kirsher 
1121527a6266SJeff Kirsher 	if (reclaimed < budget)
1122527a6266SJeff Kirsher 		mp->work_tx &= ~(1 << txq->index);
1123527a6266SJeff Kirsher 
1124527a6266SJeff Kirsher 	return reclaimed;
1125527a6266SJeff Kirsher }
1126527a6266SJeff Kirsher 
1127527a6266SJeff Kirsher 
1128527a6266SJeff Kirsher /* tx rate control **********************************************************/
1129527a6266SJeff Kirsher /*
1130527a6266SJeff Kirsher  * Set total maximum TX rate (shared by all TX queues for this port)
1131527a6266SJeff Kirsher  * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
1132527a6266SJeff Kirsher  */
tx_set_rate(struct mv643xx_eth_private * mp,int rate,int burst)1133527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
1134527a6266SJeff Kirsher {
1135527a6266SJeff Kirsher 	int token_rate;
1136527a6266SJeff Kirsher 	int mtu;
1137527a6266SJeff Kirsher 	int bucket_size;
1138527a6266SJeff Kirsher 
1139452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1140527a6266SJeff Kirsher 	if (token_rate > 1023)
1141527a6266SJeff Kirsher 		token_rate = 1023;
1142527a6266SJeff Kirsher 
1143527a6266SJeff Kirsher 	mtu = (mp->dev->mtu + 255) >> 8;
1144527a6266SJeff Kirsher 	if (mtu > 63)
1145527a6266SJeff Kirsher 		mtu = 63;
1146527a6266SJeff Kirsher 
1147527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1148527a6266SJeff Kirsher 	if (bucket_size > 65535)
1149527a6266SJeff Kirsher 		bucket_size = 65535;
1150527a6266SJeff Kirsher 
1151527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1152527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1153527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE, token_rate);
1154527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU, mtu);
1155527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST, bucket_size);
1156527a6266SJeff Kirsher 		break;
1157527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1158527a6266SJeff Kirsher 		wrlp(mp, TX_BW_RATE_MOVED, token_rate);
1159527a6266SJeff Kirsher 		wrlp(mp, TX_BW_MTU_MOVED, mtu);
1160527a6266SJeff Kirsher 		wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
1161527a6266SJeff Kirsher 		break;
1162527a6266SJeff Kirsher 	}
1163527a6266SJeff Kirsher }
1164527a6266SJeff Kirsher 
txq_set_rate(struct tx_queue * txq,int rate,int burst)1165527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
1166527a6266SJeff Kirsher {
1167527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1168527a6266SJeff Kirsher 	int token_rate;
1169527a6266SJeff Kirsher 	int bucket_size;
1170527a6266SJeff Kirsher 
1171452503ebSAndrew Lunn 	token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
1172527a6266SJeff Kirsher 	if (token_rate > 1023)
1173527a6266SJeff Kirsher 		token_rate = 1023;
1174527a6266SJeff Kirsher 
1175527a6266SJeff Kirsher 	bucket_size = (burst + 255) >> 8;
1176527a6266SJeff Kirsher 	if (bucket_size > 65535)
1177527a6266SJeff Kirsher 		bucket_size = 65535;
1178527a6266SJeff Kirsher 
1179527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
1180527a6266SJeff Kirsher 	wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
1181527a6266SJeff Kirsher }
1182527a6266SJeff Kirsher 
txq_set_fixed_prio_mode(struct tx_queue * txq)1183527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq)
1184527a6266SJeff Kirsher {
1185527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
1186527a6266SJeff Kirsher 	int off;
1187527a6266SJeff Kirsher 	u32 val;
1188527a6266SJeff Kirsher 
1189527a6266SJeff Kirsher 	/*
1190527a6266SJeff Kirsher 	 * Turn on fixed priority mode.
1191527a6266SJeff Kirsher 	 */
1192527a6266SJeff Kirsher 	off = 0;
1193527a6266SJeff Kirsher 	switch (mp->shared->tx_bw_control) {
1194527a6266SJeff Kirsher 	case TX_BW_CONTROL_OLD_LAYOUT:
1195527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF;
1196527a6266SJeff Kirsher 		break;
1197527a6266SJeff Kirsher 	case TX_BW_CONTROL_NEW_LAYOUT:
1198527a6266SJeff Kirsher 		off = TXQ_FIX_PRIO_CONF_MOVED;
1199527a6266SJeff Kirsher 		break;
1200527a6266SJeff Kirsher 	}
1201527a6266SJeff Kirsher 
1202527a6266SJeff Kirsher 	if (off) {
1203527a6266SJeff Kirsher 		val = rdlp(mp, off);
1204527a6266SJeff Kirsher 		val |= 1 << txq->index;
1205527a6266SJeff Kirsher 		wrlp(mp, off, val);
1206527a6266SJeff Kirsher 	}
1207527a6266SJeff Kirsher }
1208527a6266SJeff Kirsher 
1209527a6266SJeff Kirsher 
1210527a6266SJeff Kirsher /* mii management interface *************************************************/
mv643xx_eth_adjust_link(struct net_device * dev)12110a9e413bSEzequiel Garcia static void mv643xx_eth_adjust_link(struct net_device *dev)
1212260055bbSPhil Sutter {
12130a9e413bSEzequiel Garcia 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1214260055bbSPhil Sutter 	u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1215260055bbSPhil Sutter 	u32 autoneg_disable = FORCE_LINK_PASS |
1216260055bbSPhil Sutter 	             DISABLE_AUTO_NEG_SPEED_GMII |
1217260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1218260055bbSPhil Sutter 		     DISABLE_AUTO_NEG_FOR_DUPLEX;
1219260055bbSPhil Sutter 
12201e8a655dSPhilippe Reynes 	if (dev->phydev->autoneg == AUTONEG_ENABLE) {
1221260055bbSPhil Sutter 		/* enable auto negotiation */
1222260055bbSPhil Sutter 		pscr &= ~autoneg_disable;
1223260055bbSPhil Sutter 		goto out_write;
1224260055bbSPhil Sutter 	}
1225260055bbSPhil Sutter 
1226260055bbSPhil Sutter 	pscr |= autoneg_disable;
1227260055bbSPhil Sutter 
12281e8a655dSPhilippe Reynes 	if (dev->phydev->speed == SPEED_1000) {
1229260055bbSPhil Sutter 		/* force gigabit, half duplex not supported */
1230260055bbSPhil Sutter 		pscr |= SET_GMII_SPEED_TO_1000;
1231260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1232260055bbSPhil Sutter 		goto out_write;
1233260055bbSPhil Sutter 	}
1234260055bbSPhil Sutter 
1235260055bbSPhil Sutter 	pscr &= ~SET_GMII_SPEED_TO_1000;
1236260055bbSPhil Sutter 
12371e8a655dSPhilippe Reynes 	if (dev->phydev->speed == SPEED_100)
1238260055bbSPhil Sutter 		pscr |= SET_MII_SPEED_TO_100;
1239260055bbSPhil Sutter 	else
1240260055bbSPhil Sutter 		pscr &= ~SET_MII_SPEED_TO_100;
1241260055bbSPhil Sutter 
12421e8a655dSPhilippe Reynes 	if (dev->phydev->duplex == DUPLEX_FULL)
1243260055bbSPhil Sutter 		pscr |= SET_FULL_DUPLEX_MODE;
1244260055bbSPhil Sutter 	else
1245260055bbSPhil Sutter 		pscr &= ~SET_FULL_DUPLEX_MODE;
1246260055bbSPhil Sutter 
1247260055bbSPhil Sutter out_write:
1248260055bbSPhil Sutter 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1249260055bbSPhil Sutter }
1250260055bbSPhil Sutter 
1251527a6266SJeff Kirsher /* statistics ***************************************************************/
mv643xx_eth_get_stats(struct net_device * dev)1252527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1253527a6266SJeff Kirsher {
1254527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1255527a6266SJeff Kirsher 	struct net_device_stats *stats = &dev->stats;
1256527a6266SJeff Kirsher 	unsigned long tx_packets = 0;
1257527a6266SJeff Kirsher 	unsigned long tx_bytes = 0;
1258527a6266SJeff Kirsher 	unsigned long tx_dropped = 0;
1259527a6266SJeff Kirsher 	int i;
1260527a6266SJeff Kirsher 
1261527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
1262527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
1263527a6266SJeff Kirsher 
1264527a6266SJeff Kirsher 		tx_packets += txq->tx_packets;
1265527a6266SJeff Kirsher 		tx_bytes += txq->tx_bytes;
1266527a6266SJeff Kirsher 		tx_dropped += txq->tx_dropped;
1267527a6266SJeff Kirsher 	}
1268527a6266SJeff Kirsher 
1269527a6266SJeff Kirsher 	stats->tx_packets = tx_packets;
1270527a6266SJeff Kirsher 	stats->tx_bytes = tx_bytes;
1271527a6266SJeff Kirsher 	stats->tx_dropped = tx_dropped;
1272527a6266SJeff Kirsher 
1273527a6266SJeff Kirsher 	return stats;
1274527a6266SJeff Kirsher }
1275527a6266SJeff Kirsher 
mib_read(struct mv643xx_eth_private * mp,int offset)1276527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
1277527a6266SJeff Kirsher {
1278527a6266SJeff Kirsher 	return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
1279527a6266SJeff Kirsher }
1280527a6266SJeff Kirsher 
mib_counters_clear(struct mv643xx_eth_private * mp)1281527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp)
1282527a6266SJeff Kirsher {
1283527a6266SJeff Kirsher 	int i;
1284527a6266SJeff Kirsher 
1285527a6266SJeff Kirsher 	for (i = 0; i < 0x80; i += 4)
1286527a6266SJeff Kirsher 		mib_read(mp, i);
1287302476c9SPaulius Zaleckas 
1288302476c9SPaulius Zaleckas 	/* Clear non MIB hw counters also */
1289302476c9SPaulius Zaleckas 	rdlp(mp, RX_DISCARD_FRAME_CNT);
1290302476c9SPaulius Zaleckas 	rdlp(mp, RX_OVERRUN_FRAME_CNT);
1291527a6266SJeff Kirsher }
1292527a6266SJeff Kirsher 
mib_counters_update(struct mv643xx_eth_private * mp)1293527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp)
1294527a6266SJeff Kirsher {
1295527a6266SJeff Kirsher 	struct mib_counters *p = &mp->mib_counters;
1296527a6266SJeff Kirsher 
1297527a6266SJeff Kirsher 	spin_lock_bh(&mp->mib_counters_lock);
1298527a6266SJeff Kirsher 	p->good_octets_received += mib_read(mp, 0x00);
1299527a6266SJeff Kirsher 	p->bad_octets_received += mib_read(mp, 0x08);
1300527a6266SJeff Kirsher 	p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1301527a6266SJeff Kirsher 	p->good_frames_received += mib_read(mp, 0x10);
1302527a6266SJeff Kirsher 	p->bad_frames_received += mib_read(mp, 0x14);
1303527a6266SJeff Kirsher 	p->broadcast_frames_received += mib_read(mp, 0x18);
1304527a6266SJeff Kirsher 	p->multicast_frames_received += mib_read(mp, 0x1c);
1305527a6266SJeff Kirsher 	p->frames_64_octets += mib_read(mp, 0x20);
1306527a6266SJeff Kirsher 	p->frames_65_to_127_octets += mib_read(mp, 0x24);
1307527a6266SJeff Kirsher 	p->frames_128_to_255_octets += mib_read(mp, 0x28);
1308527a6266SJeff Kirsher 	p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1309527a6266SJeff Kirsher 	p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1310527a6266SJeff Kirsher 	p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1311527a6266SJeff Kirsher 	p->good_octets_sent += mib_read(mp, 0x38);
1312527a6266SJeff Kirsher 	p->good_frames_sent += mib_read(mp, 0x40);
1313527a6266SJeff Kirsher 	p->excessive_collision += mib_read(mp, 0x44);
1314527a6266SJeff Kirsher 	p->multicast_frames_sent += mib_read(mp, 0x48);
1315527a6266SJeff Kirsher 	p->broadcast_frames_sent += mib_read(mp, 0x4c);
1316527a6266SJeff Kirsher 	p->unrec_mac_control_received += mib_read(mp, 0x50);
1317527a6266SJeff Kirsher 	p->fc_sent += mib_read(mp, 0x54);
1318527a6266SJeff Kirsher 	p->good_fc_received += mib_read(mp, 0x58);
1319527a6266SJeff Kirsher 	p->bad_fc_received += mib_read(mp, 0x5c);
1320527a6266SJeff Kirsher 	p->undersize_received += mib_read(mp, 0x60);
1321527a6266SJeff Kirsher 	p->fragments_received += mib_read(mp, 0x64);
1322527a6266SJeff Kirsher 	p->oversize_received += mib_read(mp, 0x68);
1323527a6266SJeff Kirsher 	p->jabber_received += mib_read(mp, 0x6c);
1324527a6266SJeff Kirsher 	p->mac_receive_error += mib_read(mp, 0x70);
1325527a6266SJeff Kirsher 	p->bad_crc_event += mib_read(mp, 0x74);
1326527a6266SJeff Kirsher 	p->collision += mib_read(mp, 0x78);
1327527a6266SJeff Kirsher 	p->late_collision += mib_read(mp, 0x7c);
1328302476c9SPaulius Zaleckas 	/* Non MIB hardware counters */
1329302476c9SPaulius Zaleckas 	p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1330302476c9SPaulius Zaleckas 	p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
1331527a6266SJeff Kirsher 	spin_unlock_bh(&mp->mib_counters_lock);
1332527a6266SJeff Kirsher }
1333527a6266SJeff Kirsher 
mib_counters_timer_wrapper(struct timer_list * t)1334e99e88a9SKees Cook static void mib_counters_timer_wrapper(struct timer_list *t)
1335527a6266SJeff Kirsher {
1336e99e88a9SKees Cook 	struct mv643xx_eth_private *mp = from_timer(mp, t, mib_counters_timer);
1337527a6266SJeff Kirsher 	mib_counters_update(mp);
1338041b4ddbSSebastian Hesselbarth 	mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1339527a6266SJeff Kirsher }
1340527a6266SJeff Kirsher 
1341527a6266SJeff Kirsher 
1342527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/
1343527a6266SJeff Kirsher /*
1344527a6266SJeff Kirsher  * Hardware coalescing parameters are set in units of 64 t_clk
1345527a6266SJeff Kirsher  * cycles.  I.e.:
1346527a6266SJeff Kirsher  *
1347527a6266SJeff Kirsher  *	coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1348527a6266SJeff Kirsher  *
1349527a6266SJeff Kirsher  *	register_value = coal_delay_in_usec * t_clk_rate / 64000000
1350527a6266SJeff Kirsher  *
1351527a6266SJeff Kirsher  * In the ->set*() methods, we round the computed register value
1352527a6266SJeff Kirsher  * to the nearest integer.
1353527a6266SJeff Kirsher  */
get_rx_coal(struct mv643xx_eth_private * mp)1354527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1355527a6266SJeff Kirsher {
1356527a6266SJeff Kirsher 	u32 val = rdlp(mp, SDMA_CONFIG);
1357527a6266SJeff Kirsher 	u64 temp;
1358527a6266SJeff Kirsher 
1359527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit)
1360527a6266SJeff Kirsher 		temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1361527a6266SJeff Kirsher 	else
1362527a6266SJeff Kirsher 		temp = (val & 0x003fff00) >> 8;
1363527a6266SJeff Kirsher 
1364527a6266SJeff Kirsher 	temp *= 64000000;
1365b9b84fc0SRussell King 	temp += mp->t_clk / 2;
1366452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1367527a6266SJeff Kirsher 
1368527a6266SJeff Kirsher 	return (unsigned int)temp;
1369527a6266SJeff Kirsher }
1370527a6266SJeff Kirsher 
set_rx_coal(struct mv643xx_eth_private * mp,unsigned int usec)1371527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1372527a6266SJeff Kirsher {
1373527a6266SJeff Kirsher 	u64 temp;
1374527a6266SJeff Kirsher 	u32 val;
1375527a6266SJeff Kirsher 
1376452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1377527a6266SJeff Kirsher 	temp += 31999999;
1378527a6266SJeff Kirsher 	do_div(temp, 64000000);
1379527a6266SJeff Kirsher 
1380527a6266SJeff Kirsher 	val = rdlp(mp, SDMA_CONFIG);
1381527a6266SJeff Kirsher 	if (mp->shared->extended_rx_coal_limit) {
1382527a6266SJeff Kirsher 		if (temp > 0xffff)
1383527a6266SJeff Kirsher 			temp = 0xffff;
1384527a6266SJeff Kirsher 		val &= ~0x023fff80;
1385527a6266SJeff Kirsher 		val |= (temp & 0x8000) << 10;
1386527a6266SJeff Kirsher 		val |= (temp & 0x7fff) << 7;
1387527a6266SJeff Kirsher 	} else {
1388527a6266SJeff Kirsher 		if (temp > 0x3fff)
1389527a6266SJeff Kirsher 			temp = 0x3fff;
1390527a6266SJeff Kirsher 		val &= ~0x003fff00;
1391527a6266SJeff Kirsher 		val |= (temp & 0x3fff) << 8;
1392527a6266SJeff Kirsher 	}
1393527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, val);
1394527a6266SJeff Kirsher }
1395527a6266SJeff Kirsher 
get_tx_coal(struct mv643xx_eth_private * mp)1396527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1397527a6266SJeff Kirsher {
1398527a6266SJeff Kirsher 	u64 temp;
1399527a6266SJeff Kirsher 
1400527a6266SJeff Kirsher 	temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1401527a6266SJeff Kirsher 	temp *= 64000000;
1402b9b84fc0SRussell King 	temp += mp->t_clk / 2;
1403452503ebSAndrew Lunn 	do_div(temp, mp->t_clk);
1404527a6266SJeff Kirsher 
1405527a6266SJeff Kirsher 	return (unsigned int)temp;
1406527a6266SJeff Kirsher }
1407527a6266SJeff Kirsher 
set_tx_coal(struct mv643xx_eth_private * mp,unsigned int usec)1408527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1409527a6266SJeff Kirsher {
1410527a6266SJeff Kirsher 	u64 temp;
1411527a6266SJeff Kirsher 
1412452503ebSAndrew Lunn 	temp = (u64)usec * mp->t_clk;
1413527a6266SJeff Kirsher 	temp += 31999999;
1414527a6266SJeff Kirsher 	do_div(temp, 64000000);
1415527a6266SJeff Kirsher 
1416527a6266SJeff Kirsher 	if (temp > 0x3fff)
1417527a6266SJeff Kirsher 		temp = 0x3fff;
1418527a6266SJeff Kirsher 
1419527a6266SJeff Kirsher 	wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1420527a6266SJeff Kirsher }
1421527a6266SJeff Kirsher 
1422527a6266SJeff Kirsher 
1423527a6266SJeff Kirsher /* ethtool ******************************************************************/
1424527a6266SJeff Kirsher struct mv643xx_eth_stats {
1425527a6266SJeff Kirsher 	char stat_string[ETH_GSTRING_LEN];
1426527a6266SJeff Kirsher 	int sizeof_stat;
1427527a6266SJeff Kirsher 	int netdev_off;
1428527a6266SJeff Kirsher 	int mp_off;
1429527a6266SJeff Kirsher };
1430527a6266SJeff Kirsher 
1431527a6266SJeff Kirsher #define SSTAT(m)						\
1432c593642cSPankaj Bharadiya 	{ #m, sizeof_field(struct net_device_stats, m),		\
1433527a6266SJeff Kirsher 	  offsetof(struct net_device, stats.m), -1 }
1434527a6266SJeff Kirsher 
1435527a6266SJeff Kirsher #define MIBSTAT(m)						\
1436c593642cSPankaj Bharadiya 	{ #m, sizeof_field(struct mib_counters, m),		\
1437527a6266SJeff Kirsher 	  -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1438527a6266SJeff Kirsher 
1439527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1440527a6266SJeff Kirsher 	SSTAT(rx_packets),
1441527a6266SJeff Kirsher 	SSTAT(tx_packets),
1442527a6266SJeff Kirsher 	SSTAT(rx_bytes),
1443527a6266SJeff Kirsher 	SSTAT(tx_bytes),
1444527a6266SJeff Kirsher 	SSTAT(rx_errors),
1445527a6266SJeff Kirsher 	SSTAT(tx_errors),
1446527a6266SJeff Kirsher 	SSTAT(rx_dropped),
1447527a6266SJeff Kirsher 	SSTAT(tx_dropped),
1448527a6266SJeff Kirsher 	MIBSTAT(good_octets_received),
1449527a6266SJeff Kirsher 	MIBSTAT(bad_octets_received),
1450527a6266SJeff Kirsher 	MIBSTAT(internal_mac_transmit_err),
1451527a6266SJeff Kirsher 	MIBSTAT(good_frames_received),
1452527a6266SJeff Kirsher 	MIBSTAT(bad_frames_received),
1453527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_received),
1454527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_received),
1455527a6266SJeff Kirsher 	MIBSTAT(frames_64_octets),
1456527a6266SJeff Kirsher 	MIBSTAT(frames_65_to_127_octets),
1457527a6266SJeff Kirsher 	MIBSTAT(frames_128_to_255_octets),
1458527a6266SJeff Kirsher 	MIBSTAT(frames_256_to_511_octets),
1459527a6266SJeff Kirsher 	MIBSTAT(frames_512_to_1023_octets),
1460527a6266SJeff Kirsher 	MIBSTAT(frames_1024_to_max_octets),
1461527a6266SJeff Kirsher 	MIBSTAT(good_octets_sent),
1462527a6266SJeff Kirsher 	MIBSTAT(good_frames_sent),
1463527a6266SJeff Kirsher 	MIBSTAT(excessive_collision),
1464527a6266SJeff Kirsher 	MIBSTAT(multicast_frames_sent),
1465527a6266SJeff Kirsher 	MIBSTAT(broadcast_frames_sent),
1466527a6266SJeff Kirsher 	MIBSTAT(unrec_mac_control_received),
1467527a6266SJeff Kirsher 	MIBSTAT(fc_sent),
1468527a6266SJeff Kirsher 	MIBSTAT(good_fc_received),
1469527a6266SJeff Kirsher 	MIBSTAT(bad_fc_received),
1470527a6266SJeff Kirsher 	MIBSTAT(undersize_received),
1471527a6266SJeff Kirsher 	MIBSTAT(fragments_received),
1472527a6266SJeff Kirsher 	MIBSTAT(oversize_received),
1473527a6266SJeff Kirsher 	MIBSTAT(jabber_received),
1474527a6266SJeff Kirsher 	MIBSTAT(mac_receive_error),
1475527a6266SJeff Kirsher 	MIBSTAT(bad_crc_event),
1476527a6266SJeff Kirsher 	MIBSTAT(collision),
1477527a6266SJeff Kirsher 	MIBSTAT(late_collision),
1478302476c9SPaulius Zaleckas 	MIBSTAT(rx_discard),
1479302476c9SPaulius Zaleckas 	MIBSTAT(rx_overrun),
1480527a6266SJeff Kirsher };
1481527a6266SJeff Kirsher 
1482527a6266SJeff Kirsher static int
mv643xx_eth_get_link_ksettings_phy(struct mv643xx_eth_private * mp,struct ethtool_link_ksettings * cmd)1483a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings_phy(struct mv643xx_eth_private *mp,
1484a54e1612SPhilippe Reynes 				   struct ethtool_link_ksettings *cmd)
1485527a6266SJeff Kirsher {
14861e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
1487527a6266SJeff Kirsher 
14885514174fSyuval.shaia@oracle.com 	phy_ethtool_ksettings_get(dev->phydev, cmd);
1489527a6266SJeff Kirsher 
1490527a6266SJeff Kirsher 	/*
1491527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1492527a6266SJeff Kirsher 	 */
14933c1bcc86SAndrew Lunn 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1494a54e1612SPhilippe Reynes 			   cmd->link_modes.supported);
14953c1bcc86SAndrew Lunn 	linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT,
1496a54e1612SPhilippe Reynes 			   cmd->link_modes.advertising);
1497527a6266SJeff Kirsher 
14985514174fSyuval.shaia@oracle.com 	return 0;
1499527a6266SJeff Kirsher }
1500527a6266SJeff Kirsher 
1501527a6266SJeff Kirsher static int
mv643xx_eth_get_link_ksettings_phyless(struct mv643xx_eth_private * mp,struct ethtool_link_ksettings * cmd)1502a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings_phyless(struct mv643xx_eth_private *mp,
1503a54e1612SPhilippe Reynes 				       struct ethtool_link_ksettings *cmd)
1504527a6266SJeff Kirsher {
1505527a6266SJeff Kirsher 	u32 port_status;
1506a54e1612SPhilippe Reynes 	u32 supported, advertising;
1507527a6266SJeff Kirsher 
1508527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
1509527a6266SJeff Kirsher 
1510a54e1612SPhilippe Reynes 	supported = SUPPORTED_MII;
1511a54e1612SPhilippe Reynes 	advertising = ADVERTISED_MII;
1512527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
1513527a6266SJeff Kirsher 	case PORT_SPEED_10:
1514a54e1612SPhilippe Reynes 		cmd->base.speed = SPEED_10;
1515527a6266SJeff Kirsher 		break;
1516527a6266SJeff Kirsher 	case PORT_SPEED_100:
1517a54e1612SPhilippe Reynes 		cmd->base.speed = SPEED_100;
1518527a6266SJeff Kirsher 		break;
1519527a6266SJeff Kirsher 	case PORT_SPEED_1000:
1520a54e1612SPhilippe Reynes 		cmd->base.speed = SPEED_1000;
1521527a6266SJeff Kirsher 		break;
1522527a6266SJeff Kirsher 	default:
1523a54e1612SPhilippe Reynes 		cmd->base.speed = -1;
1524527a6266SJeff Kirsher 		break;
1525527a6266SJeff Kirsher 	}
1526a54e1612SPhilippe Reynes 	cmd->base.duplex = (port_status & FULL_DUPLEX) ?
1527a54e1612SPhilippe Reynes 		DUPLEX_FULL : DUPLEX_HALF;
1528a54e1612SPhilippe Reynes 	cmd->base.port = PORT_MII;
1529a54e1612SPhilippe Reynes 	cmd->base.phy_address = 0;
1530a54e1612SPhilippe Reynes 	cmd->base.autoneg = AUTONEG_DISABLE;
1531a54e1612SPhilippe Reynes 
1532a54e1612SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
1533a54e1612SPhilippe Reynes 						supported);
1534a54e1612SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
1535a54e1612SPhilippe Reynes 						advertising);
1536527a6266SJeff Kirsher 
1537527a6266SJeff Kirsher 	return 0;
1538527a6266SJeff Kirsher }
1539527a6266SJeff Kirsher 
15403871c387SMichael Stapelberg static void
mv643xx_eth_get_wol(struct net_device * dev,struct ethtool_wolinfo * wol)15413871c387SMichael Stapelberg mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
15423871c387SMichael Stapelberg {
15433871c387SMichael Stapelberg 	wol->supported = 0;
15443871c387SMichael Stapelberg 	wol->wolopts = 0;
15451e8a655dSPhilippe Reynes 	if (dev->phydev)
15461e8a655dSPhilippe Reynes 		phy_ethtool_get_wol(dev->phydev, wol);
15473871c387SMichael Stapelberg }
15483871c387SMichael Stapelberg 
15493871c387SMichael Stapelberg static int
mv643xx_eth_set_wol(struct net_device * dev,struct ethtool_wolinfo * wol)15503871c387SMichael Stapelberg mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
15513871c387SMichael Stapelberg {
15523871c387SMichael Stapelberg 	int err;
15533871c387SMichael Stapelberg 
15541e8a655dSPhilippe Reynes 	if (!dev->phydev)
15553871c387SMichael Stapelberg 		return -EOPNOTSUPP;
15563871c387SMichael Stapelberg 
15571e8a655dSPhilippe Reynes 	err = phy_ethtool_set_wol(dev->phydev, wol);
15583871c387SMichael Stapelberg 	/* Given that mv643xx_eth works without the marvell-specific PHY driver,
15593871c387SMichael Stapelberg 	 * this debugging hint is useful to have.
15603871c387SMichael Stapelberg 	 */
15613871c387SMichael Stapelberg 	if (err == -EOPNOTSUPP)
15623871c387SMichael Stapelberg 		netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
15633871c387SMichael Stapelberg 	return err;
15643871c387SMichael Stapelberg }
15653871c387SMichael Stapelberg 
1566527a6266SJeff Kirsher static int
mv643xx_eth_get_link_ksettings(struct net_device * dev,struct ethtool_link_ksettings * cmd)1567a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings(struct net_device *dev,
1568a54e1612SPhilippe Reynes 			       struct ethtool_link_ksettings *cmd)
1569527a6266SJeff Kirsher {
1570527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1571527a6266SJeff Kirsher 
15721e8a655dSPhilippe Reynes 	if (dev->phydev)
1573a54e1612SPhilippe Reynes 		return mv643xx_eth_get_link_ksettings_phy(mp, cmd);
1574527a6266SJeff Kirsher 	else
1575a54e1612SPhilippe Reynes 		return mv643xx_eth_get_link_ksettings_phyless(mp, cmd);
1576527a6266SJeff Kirsher }
1577527a6266SJeff Kirsher 
1578527a6266SJeff Kirsher static int
mv643xx_eth_set_link_ksettings(struct net_device * dev,const struct ethtool_link_ksettings * cmd)1579a54e1612SPhilippe Reynes mv643xx_eth_set_link_ksettings(struct net_device *dev,
1580a54e1612SPhilippe Reynes 			       const struct ethtool_link_ksettings *cmd)
1581527a6266SJeff Kirsher {
1582a54e1612SPhilippe Reynes 	struct ethtool_link_ksettings c = *cmd;
1583a54e1612SPhilippe Reynes 	u32 advertising;
1584260055bbSPhil Sutter 	int ret;
1585527a6266SJeff Kirsher 
15861e8a655dSPhilippe Reynes 	if (!dev->phydev)
1587527a6266SJeff Kirsher 		return -EINVAL;
1588527a6266SJeff Kirsher 
1589527a6266SJeff Kirsher 	/*
1590527a6266SJeff Kirsher 	 * The MAC does not support 1000baseT_Half.
1591527a6266SJeff Kirsher 	 */
1592a54e1612SPhilippe Reynes 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
1593a54e1612SPhilippe Reynes 						c.link_modes.advertising);
1594a54e1612SPhilippe Reynes 	advertising &= ~ADVERTISED_1000baseT_Half;
1595a54e1612SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(c.link_modes.advertising,
1596a54e1612SPhilippe Reynes 						advertising);
1597527a6266SJeff Kirsher 
1598a54e1612SPhilippe Reynes 	ret = phy_ethtool_ksettings_set(dev->phydev, &c);
1599260055bbSPhil Sutter 	if (!ret)
16000a9e413bSEzequiel Garcia 		mv643xx_eth_adjust_link(dev);
1601260055bbSPhil Sutter 	return ret;
1602527a6266SJeff Kirsher }
1603527a6266SJeff Kirsher 
mv643xx_eth_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)1604527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1605527a6266SJeff Kirsher 				    struct ethtool_drvinfo *drvinfo)
1606527a6266SJeff Kirsher {
1607f029c781SWolfram Sang 	strscpy(drvinfo->driver, mv643xx_eth_driver_name,
16086f39da2cSAxel Lin 		sizeof(drvinfo->driver));
1609f029c781SWolfram Sang 	strscpy(drvinfo->version, mv643xx_eth_driver_version,
16106f39da2cSAxel Lin 		sizeof(drvinfo->version));
1611f029c781SWolfram Sang 	strscpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1612f029c781SWolfram Sang 	strscpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
1613527a6266SJeff Kirsher }
1614527a6266SJeff Kirsher 
mv643xx_eth_get_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1615f3ccfda1SYufeng Mo static int mv643xx_eth_get_coalesce(struct net_device *dev,
1616f3ccfda1SYufeng Mo 				    struct ethtool_coalesce *ec,
1617f3ccfda1SYufeng Mo 				    struct kernel_ethtool_coalesce *kernel_coal,
1618f3ccfda1SYufeng Mo 				    struct netlink_ext_ack *extack)
1619527a6266SJeff Kirsher {
1620527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1621527a6266SJeff Kirsher 
1622527a6266SJeff Kirsher 	ec->rx_coalesce_usecs = get_rx_coal(mp);
1623527a6266SJeff Kirsher 	ec->tx_coalesce_usecs = get_tx_coal(mp);
1624527a6266SJeff Kirsher 
1625527a6266SJeff Kirsher 	return 0;
1626527a6266SJeff Kirsher }
1627527a6266SJeff Kirsher 
mv643xx_eth_set_coalesce(struct net_device * dev,struct ethtool_coalesce * ec,struct kernel_ethtool_coalesce * kernel_coal,struct netlink_ext_ack * extack)1628f3ccfda1SYufeng Mo static int mv643xx_eth_set_coalesce(struct net_device *dev,
1629f3ccfda1SYufeng Mo 				    struct ethtool_coalesce *ec,
1630f3ccfda1SYufeng Mo 				    struct kernel_ethtool_coalesce *kernel_coal,
1631f3ccfda1SYufeng Mo 				    struct netlink_ext_ack *extack)
1632527a6266SJeff Kirsher {
1633527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1634527a6266SJeff Kirsher 
1635527a6266SJeff Kirsher 	set_rx_coal(mp, ec->rx_coalesce_usecs);
1636527a6266SJeff Kirsher 	set_tx_coal(mp, ec->tx_coalesce_usecs);
1637527a6266SJeff Kirsher 
1638527a6266SJeff Kirsher 	return 0;
1639527a6266SJeff Kirsher }
1640527a6266SJeff Kirsher 
1641527a6266SJeff Kirsher static void
mv643xx_eth_get_ringparam(struct net_device * dev,struct ethtool_ringparam * er,struct kernel_ethtool_ringparam * kernel_er,struct netlink_ext_ack * extack)164274624944SHao Chen mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er,
164374624944SHao Chen 			  struct kernel_ethtool_ringparam *kernel_er,
164474624944SHao Chen 			  struct netlink_ext_ack *extack)
1645527a6266SJeff Kirsher {
1646527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1647527a6266SJeff Kirsher 
1648527a6266SJeff Kirsher 	er->rx_max_pending = 4096;
1649527a6266SJeff Kirsher 	er->tx_max_pending = 4096;
1650527a6266SJeff Kirsher 
1651527a6266SJeff Kirsher 	er->rx_pending = mp->rx_ring_size;
1652527a6266SJeff Kirsher 	er->tx_pending = mp->tx_ring_size;
1653527a6266SJeff Kirsher }
1654527a6266SJeff Kirsher 
1655527a6266SJeff Kirsher static int
mv643xx_eth_set_ringparam(struct net_device * dev,struct ethtool_ringparam * er,struct kernel_ethtool_ringparam * kernel_er,struct netlink_ext_ack * extack)165674624944SHao Chen mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er,
165774624944SHao Chen 			  struct kernel_ethtool_ringparam *kernel_er,
165874624944SHao Chen 			  struct netlink_ext_ack *extack)
1659527a6266SJeff Kirsher {
1660527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1661527a6266SJeff Kirsher 
1662527a6266SJeff Kirsher 	if (er->rx_mini_pending || er->rx_jumbo_pending)
1663527a6266SJeff Kirsher 		return -EINVAL;
1664527a6266SJeff Kirsher 
16652f5e65deSHaowen Bai 	mp->rx_ring_size = min(er->rx_pending, 4096U);
1666ee9e4956SEzequiel Garcia 	mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending,
1667ee9e4956SEzequiel Garcia 				   MV643XX_MAX_SKB_DESCS * 2, 4096);
1668ee9e4956SEzequiel Garcia 	if (mp->tx_ring_size != er->tx_pending)
1669ee9e4956SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
1670ee9e4956SEzequiel Garcia 			    mp->tx_ring_size, er->tx_pending);
1671527a6266SJeff Kirsher 
1672527a6266SJeff Kirsher 	if (netif_running(dev)) {
1673527a6266SJeff Kirsher 		mv643xx_eth_stop(dev);
1674527a6266SJeff Kirsher 		if (mv643xx_eth_open(dev)) {
1675527a6266SJeff Kirsher 			netdev_err(dev,
1676527a6266SJeff Kirsher 				   "fatal error on re-opening device after ring param change\n");
1677527a6266SJeff Kirsher 			return -ENOMEM;
1678527a6266SJeff Kirsher 		}
1679527a6266SJeff Kirsher 	}
1680527a6266SJeff Kirsher 
1681527a6266SJeff Kirsher 	return 0;
1682527a6266SJeff Kirsher }
1683527a6266SJeff Kirsher 
1684527a6266SJeff Kirsher 
1685527a6266SJeff Kirsher static int
mv643xx_eth_set_features(struct net_device * dev,netdev_features_t features)1686c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
1687527a6266SJeff Kirsher {
1688527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
16893ad9b358SMichał Mirosław 	bool rx_csum = features & NETIF_F_RXCSUM;
1690527a6266SJeff Kirsher 
1691527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1692527a6266SJeff Kirsher 
1693527a6266SJeff Kirsher 	return 0;
1694527a6266SJeff Kirsher }
1695527a6266SJeff Kirsher 
mv643xx_eth_get_strings(struct net_device * dev,uint32_t stringset,uint8_t * data)1696527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev,
1697527a6266SJeff Kirsher 				    uint32_t stringset, uint8_t *data)
1698527a6266SJeff Kirsher {
1699527a6266SJeff Kirsher 	int i;
1700527a6266SJeff Kirsher 
1701527a6266SJeff Kirsher 	if (stringset == ETH_SS_STATS) {
1702527a6266SJeff Kirsher 		for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1703527a6266SJeff Kirsher 			memcpy(data + i * ETH_GSTRING_LEN,
1704527a6266SJeff Kirsher 				mv643xx_eth_stats[i].stat_string,
1705527a6266SJeff Kirsher 				ETH_GSTRING_LEN);
1706527a6266SJeff Kirsher 		}
1707527a6266SJeff Kirsher 	}
1708527a6266SJeff Kirsher }
1709527a6266SJeff Kirsher 
mv643xx_eth_get_ethtool_stats(struct net_device * dev,struct ethtool_stats * stats,uint64_t * data)1710527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1711527a6266SJeff Kirsher 					  struct ethtool_stats *stats,
1712527a6266SJeff Kirsher 					  uint64_t *data)
1713527a6266SJeff Kirsher {
1714527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1715527a6266SJeff Kirsher 	int i;
1716527a6266SJeff Kirsher 
1717527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
1718527a6266SJeff Kirsher 	mib_counters_update(mp);
1719527a6266SJeff Kirsher 
1720527a6266SJeff Kirsher 	for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1721527a6266SJeff Kirsher 		const struct mv643xx_eth_stats *stat;
1722527a6266SJeff Kirsher 		void *p;
1723527a6266SJeff Kirsher 
1724527a6266SJeff Kirsher 		stat = mv643xx_eth_stats + i;
1725527a6266SJeff Kirsher 
1726527a6266SJeff Kirsher 		if (stat->netdev_off >= 0)
1727527a6266SJeff Kirsher 			p = ((void *)mp->dev) + stat->netdev_off;
1728527a6266SJeff Kirsher 		else
1729527a6266SJeff Kirsher 			p = ((void *)mp) + stat->mp_off;
1730527a6266SJeff Kirsher 
1731527a6266SJeff Kirsher 		data[i] = (stat->sizeof_stat == 8) ?
1732527a6266SJeff Kirsher 				*(uint64_t *)p : *(uint32_t *)p;
1733527a6266SJeff Kirsher 	}
1734527a6266SJeff Kirsher }
1735527a6266SJeff Kirsher 
mv643xx_eth_get_sset_count(struct net_device * dev,int sset)1736527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
1737527a6266SJeff Kirsher {
1738527a6266SJeff Kirsher 	if (sset == ETH_SS_STATS)
1739527a6266SJeff Kirsher 		return ARRAY_SIZE(mv643xx_eth_stats);
1740527a6266SJeff Kirsher 
1741527a6266SJeff Kirsher 	return -EOPNOTSUPP;
1742527a6266SJeff Kirsher }
1743527a6266SJeff Kirsher 
1744527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1745f99db1d4SJakub Kicinski 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS,
1746527a6266SJeff Kirsher 	.get_drvinfo		= mv643xx_eth_get_drvinfo,
1747fc5e353cSFlorian Fainelli 	.nway_reset		= phy_ethtool_nway_reset,
1748527a6266SJeff Kirsher 	.get_link		= ethtool_op_get_link,
1749527a6266SJeff Kirsher 	.get_coalesce		= mv643xx_eth_get_coalesce,
1750527a6266SJeff Kirsher 	.set_coalesce		= mv643xx_eth_set_coalesce,
1751527a6266SJeff Kirsher 	.get_ringparam		= mv643xx_eth_get_ringparam,
1752527a6266SJeff Kirsher 	.set_ringparam		= mv643xx_eth_set_ringparam,
1753527a6266SJeff Kirsher 	.get_strings		= mv643xx_eth_get_strings,
1754527a6266SJeff Kirsher 	.get_ethtool_stats	= mv643xx_eth_get_ethtool_stats,
1755527a6266SJeff Kirsher 	.get_sset_count		= mv643xx_eth_get_sset_count,
1756ebad0a8dSRichard Cochran 	.get_ts_info		= ethtool_op_get_ts_info,
17573871c387SMichael Stapelberg 	.get_wol                = mv643xx_eth_get_wol,
17583871c387SMichael Stapelberg 	.set_wol                = mv643xx_eth_set_wol,
1759a54e1612SPhilippe Reynes 	.get_link_ksettings	= mv643xx_eth_get_link_ksettings,
1760a54e1612SPhilippe Reynes 	.set_link_ksettings	= mv643xx_eth_set_link_ksettings,
1761527a6266SJeff Kirsher };
1762527a6266SJeff Kirsher 
1763527a6266SJeff Kirsher 
1764527a6266SJeff Kirsher /* address handling *********************************************************/
uc_addr_get(struct mv643xx_eth_private * mp,unsigned char * addr)1765527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
1766527a6266SJeff Kirsher {
1767527a6266SJeff Kirsher 	unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1768527a6266SJeff Kirsher 	unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
1769527a6266SJeff Kirsher 
1770527a6266SJeff Kirsher 	addr[0] = (mac_h >> 24) & 0xff;
1771527a6266SJeff Kirsher 	addr[1] = (mac_h >> 16) & 0xff;
1772527a6266SJeff Kirsher 	addr[2] = (mac_h >> 8) & 0xff;
1773527a6266SJeff Kirsher 	addr[3] = mac_h & 0xff;
1774527a6266SJeff Kirsher 	addr[4] = (mac_l >> 8) & 0xff;
1775527a6266SJeff Kirsher 	addr[5] = mac_l & 0xff;
1776527a6266SJeff Kirsher }
1777527a6266SJeff Kirsher 
uc_addr_set(struct mv643xx_eth_private * mp,const u8 * addr)177876660757SJakub Kicinski static void uc_addr_set(struct mv643xx_eth_private *mp, const u8 *addr)
1779527a6266SJeff Kirsher {
1780527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_HIGH,
1781527a6266SJeff Kirsher 		(addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1782527a6266SJeff Kirsher 	wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
1783527a6266SJeff Kirsher }
1784527a6266SJeff Kirsher 
uc_addr_filter_mask(struct net_device * dev)1785527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev)
1786527a6266SJeff Kirsher {
1787527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1788527a6266SJeff Kirsher 	u32 nibbles;
1789527a6266SJeff Kirsher 
1790527a6266SJeff Kirsher 	if (dev->flags & IFF_PROMISC)
1791527a6266SJeff Kirsher 		return 0;
1792527a6266SJeff Kirsher 
1793527a6266SJeff Kirsher 	nibbles = 1 << (dev->dev_addr[5] & 0x0f);
1794527a6266SJeff Kirsher 	netdev_for_each_uc_addr(ha, dev) {
1795527a6266SJeff Kirsher 		if (memcmp(dev->dev_addr, ha->addr, 5))
1796527a6266SJeff Kirsher 			return 0;
1797527a6266SJeff Kirsher 		if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
1798527a6266SJeff Kirsher 			return 0;
1799527a6266SJeff Kirsher 
1800527a6266SJeff Kirsher 		nibbles |= 1 << (ha->addr[5] & 0x0f);
1801527a6266SJeff Kirsher 	}
1802527a6266SJeff Kirsher 
1803527a6266SJeff Kirsher 	return nibbles;
1804527a6266SJeff Kirsher }
1805527a6266SJeff Kirsher 
mv643xx_eth_program_unicast_filter(struct net_device * dev)1806527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
1807527a6266SJeff Kirsher {
1808527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1809527a6266SJeff Kirsher 	u32 port_config;
1810527a6266SJeff Kirsher 	u32 nibbles;
1811527a6266SJeff Kirsher 	int i;
1812527a6266SJeff Kirsher 
1813527a6266SJeff Kirsher 	uc_addr_set(mp, dev->dev_addr);
1814527a6266SJeff Kirsher 
1815527a6266SJeff Kirsher 	port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1816527a6266SJeff Kirsher 
1817527a6266SJeff Kirsher 	nibbles = uc_addr_filter_mask(dev);
1818527a6266SJeff Kirsher 	if (!nibbles) {
1819527a6266SJeff Kirsher 		port_config |= UNICAST_PROMISCUOUS_MODE;
1820527a6266SJeff Kirsher 		nibbles = 0xffff;
1821527a6266SJeff Kirsher 	}
1822527a6266SJeff Kirsher 
1823527a6266SJeff Kirsher 	for (i = 0; i < 16; i += 4) {
1824527a6266SJeff Kirsher 		int off = UNICAST_TABLE(mp->port_num) + i;
1825527a6266SJeff Kirsher 		u32 v;
1826527a6266SJeff Kirsher 
1827527a6266SJeff Kirsher 		v = 0;
1828527a6266SJeff Kirsher 		if (nibbles & 1)
1829527a6266SJeff Kirsher 			v |= 0x00000001;
1830527a6266SJeff Kirsher 		if (nibbles & 2)
1831527a6266SJeff Kirsher 			v |= 0x00000100;
1832527a6266SJeff Kirsher 		if (nibbles & 4)
1833527a6266SJeff Kirsher 			v |= 0x00010000;
1834527a6266SJeff Kirsher 		if (nibbles & 8)
1835527a6266SJeff Kirsher 			v |= 0x01000000;
1836527a6266SJeff Kirsher 		nibbles >>= 4;
1837527a6266SJeff Kirsher 
1838527a6266SJeff Kirsher 		wrl(mp, off, v);
1839527a6266SJeff Kirsher 	}
1840527a6266SJeff Kirsher 
1841527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG, port_config);
1842527a6266SJeff Kirsher }
1843527a6266SJeff Kirsher 
addr_crc(unsigned char * addr)1844527a6266SJeff Kirsher static int addr_crc(unsigned char *addr)
1845527a6266SJeff Kirsher {
1846527a6266SJeff Kirsher 	int crc = 0;
1847527a6266SJeff Kirsher 	int i;
1848527a6266SJeff Kirsher 
1849527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
1850527a6266SJeff Kirsher 		int j;
1851527a6266SJeff Kirsher 
1852527a6266SJeff Kirsher 		crc = (crc ^ addr[i]) << 8;
1853527a6266SJeff Kirsher 		for (j = 7; j >= 0; j--) {
1854527a6266SJeff Kirsher 			if (crc & (0x100 << j))
1855527a6266SJeff Kirsher 				crc ^= 0x107 << j;
1856527a6266SJeff Kirsher 		}
1857527a6266SJeff Kirsher 	}
1858527a6266SJeff Kirsher 
1859527a6266SJeff Kirsher 	return crc;
1860527a6266SJeff Kirsher }
1861527a6266SJeff Kirsher 
mv643xx_eth_program_multicast_filter(struct net_device * dev)1862527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
1863527a6266SJeff Kirsher {
1864527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
1865527a6266SJeff Kirsher 	u32 *mc_spec;
1866527a6266SJeff Kirsher 	u32 *mc_other;
1867527a6266SJeff Kirsher 	struct netdev_hw_addr *ha;
1868527a6266SJeff Kirsher 	int i;
1869527a6266SJeff Kirsher 
18708b711d6dSJoe Perches 	if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI))
18718b711d6dSJoe Perches 		goto promiscuous;
1872527a6266SJeff Kirsher 
18738b711d6dSJoe Perches 	/* Allocate both mc_spec and mc_other tables */
18748b711d6dSJoe Perches 	mc_spec = kcalloc(128, sizeof(u32), GFP_ATOMIC);
18758b711d6dSJoe Perches 	if (!mc_spec)
18768b711d6dSJoe Perches 		goto promiscuous;
18778b711d6dSJoe Perches 	mc_other = &mc_spec[64];
1878527a6266SJeff Kirsher 
1879527a6266SJeff Kirsher 	netdev_for_each_mc_addr(ha, dev) {
1880527a6266SJeff Kirsher 		u8 *a = ha->addr;
1881527a6266SJeff Kirsher 		u32 *table;
18828b711d6dSJoe Perches 		u8 entry;
1883527a6266SJeff Kirsher 
1884527a6266SJeff Kirsher 		if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1885527a6266SJeff Kirsher 			table = mc_spec;
1886527a6266SJeff Kirsher 			entry = a[5];
1887527a6266SJeff Kirsher 		} else {
1888527a6266SJeff Kirsher 			table = mc_other;
1889527a6266SJeff Kirsher 			entry = addr_crc(a);
1890527a6266SJeff Kirsher 		}
1891527a6266SJeff Kirsher 
1892527a6266SJeff Kirsher 		table[entry >> 2] |= 1 << (8 * (entry & 3));
1893527a6266SJeff Kirsher 	}
1894527a6266SJeff Kirsher 
18958b711d6dSJoe Perches 	for (i = 0; i < 64; i++) {
18968b711d6dSJoe Perches 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
18978b711d6dSJoe Perches 		    mc_spec[i]);
18988b711d6dSJoe Perches 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
18998b711d6dSJoe Perches 		    mc_other[i]);
1900527a6266SJeff Kirsher 	}
1901527a6266SJeff Kirsher 
1902527a6266SJeff Kirsher 	kfree(mc_spec);
19038b711d6dSJoe Perches 	return;
19048b711d6dSJoe Perches 
19058b711d6dSJoe Perches promiscuous:
19068b711d6dSJoe Perches 	for (i = 0; i < 64; i++) {
19078b711d6dSJoe Perches 		wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
19088b711d6dSJoe Perches 		    0x01010101u);
19098b711d6dSJoe Perches 		wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32),
19108b711d6dSJoe Perches 		    0x01010101u);
19118b711d6dSJoe Perches 	}
1912527a6266SJeff Kirsher }
1913527a6266SJeff Kirsher 
mv643xx_eth_set_rx_mode(struct net_device * dev)1914527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1915527a6266SJeff Kirsher {
1916527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1917527a6266SJeff Kirsher 	mv643xx_eth_program_multicast_filter(dev);
1918527a6266SJeff Kirsher }
1919527a6266SJeff Kirsher 
mv643xx_eth_set_mac_address(struct net_device * dev,void * addr)1920527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1921527a6266SJeff Kirsher {
1922527a6266SJeff Kirsher 	struct sockaddr *sa = addr;
1923527a6266SJeff Kirsher 
1924527a6266SJeff Kirsher 	if (!is_valid_ether_addr(sa->sa_data))
1925504f9b5aSDanny Kukawka 		return -EADDRNOTAVAIL;
1926527a6266SJeff Kirsher 
1927a96d317fSJakub Kicinski 	eth_hw_addr_set(dev, sa->sa_data);
1928527a6266SJeff Kirsher 
1929527a6266SJeff Kirsher 	netif_addr_lock_bh(dev);
1930527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(dev);
1931527a6266SJeff Kirsher 	netif_addr_unlock_bh(dev);
1932527a6266SJeff Kirsher 
1933527a6266SJeff Kirsher 	return 0;
1934527a6266SJeff Kirsher }
1935527a6266SJeff Kirsher 
1936527a6266SJeff Kirsher 
1937527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/
rxq_init(struct mv643xx_eth_private * mp,int index)1938527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index)
1939527a6266SJeff Kirsher {
1940527a6266SJeff Kirsher 	struct rx_queue *rxq = mp->rxq + index;
1941527a6266SJeff Kirsher 	struct rx_desc *rx_desc;
1942527a6266SJeff Kirsher 	int size;
1943527a6266SJeff Kirsher 	int i;
1944527a6266SJeff Kirsher 
1945527a6266SJeff Kirsher 	rxq->index = index;
1946527a6266SJeff Kirsher 
1947527a6266SJeff Kirsher 	rxq->rx_ring_size = mp->rx_ring_size;
1948527a6266SJeff Kirsher 
1949527a6266SJeff Kirsher 	rxq->rx_desc_count = 0;
1950527a6266SJeff Kirsher 	rxq->rx_curr_desc = 0;
1951527a6266SJeff Kirsher 	rxq->rx_used_desc = 0;
1952527a6266SJeff Kirsher 
1953527a6266SJeff Kirsher 	size = rxq->rx_ring_size * sizeof(struct rx_desc);
1954527a6266SJeff Kirsher 
1955527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size) {
1956527a6266SJeff Kirsher 		rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1957527a6266SJeff Kirsher 						mp->rx_desc_sram_size);
1958527a6266SJeff Kirsher 		rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1959527a6266SJeff Kirsher 	} else {
1960527a6266SJeff Kirsher 		rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1961527a6266SJeff Kirsher 						       size, &rxq->rx_desc_dma,
1962527a6266SJeff Kirsher 						       GFP_KERNEL);
1963527a6266SJeff Kirsher 	}
1964527a6266SJeff Kirsher 
1965527a6266SJeff Kirsher 	if (rxq->rx_desc_area == NULL) {
1966527a6266SJeff Kirsher 		netdev_err(mp->dev,
1967527a6266SJeff Kirsher 			   "can't allocate rx ring (%d bytes)\n", size);
1968527a6266SJeff Kirsher 		goto out;
1969527a6266SJeff Kirsher 	}
1970527a6266SJeff Kirsher 	memset(rxq->rx_desc_area, 0, size);
1971527a6266SJeff Kirsher 
1972527a6266SJeff Kirsher 	rxq->rx_desc_area_size = size;
19739fa8e980SLubomir Rintel 	rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1974527a6266SJeff Kirsher 				    GFP_KERNEL);
1975b2adaca9SJoe Perches 	if (rxq->rx_skb == NULL)
1976527a6266SJeff Kirsher 		goto out_free;
1977527a6266SJeff Kirsher 
197864699336SJoe Perches 	rx_desc = rxq->rx_desc_area;
1979527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
1980527a6266SJeff Kirsher 		int nexti;
1981527a6266SJeff Kirsher 
1982527a6266SJeff Kirsher 		nexti = i + 1;
1983527a6266SJeff Kirsher 		if (nexti == rxq->rx_ring_size)
1984527a6266SJeff Kirsher 			nexti = 0;
1985527a6266SJeff Kirsher 
1986527a6266SJeff Kirsher 		rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1987527a6266SJeff Kirsher 					nexti * sizeof(struct rx_desc);
1988527a6266SJeff Kirsher 	}
1989527a6266SJeff Kirsher 
1990527a6266SJeff Kirsher 	return 0;
1991527a6266SJeff Kirsher 
1992527a6266SJeff Kirsher 
1993527a6266SJeff Kirsher out_free:
1994527a6266SJeff Kirsher 	if (index == 0 && size <= mp->rx_desc_sram_size)
1995527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
1996527a6266SJeff Kirsher 	else
1997527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, size,
1998527a6266SJeff Kirsher 				  rxq->rx_desc_area,
1999527a6266SJeff Kirsher 				  rxq->rx_desc_dma);
2000527a6266SJeff Kirsher 
2001527a6266SJeff Kirsher out:
2002527a6266SJeff Kirsher 	return -ENOMEM;
2003527a6266SJeff Kirsher }
2004527a6266SJeff Kirsher 
rxq_deinit(struct rx_queue * rxq)2005527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq)
2006527a6266SJeff Kirsher {
2007527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
2008527a6266SJeff Kirsher 	int i;
2009527a6266SJeff Kirsher 
2010527a6266SJeff Kirsher 	rxq_disable(rxq);
2011527a6266SJeff Kirsher 
2012527a6266SJeff Kirsher 	for (i = 0; i < rxq->rx_ring_size; i++) {
2013527a6266SJeff Kirsher 		if (rxq->rx_skb[i]) {
201443cee2d2SFlorian Fainelli 			dev_consume_skb_any(rxq->rx_skb[i]);
2015527a6266SJeff Kirsher 			rxq->rx_desc_count--;
2016527a6266SJeff Kirsher 		}
2017527a6266SJeff Kirsher 	}
2018527a6266SJeff Kirsher 
2019527a6266SJeff Kirsher 	if (rxq->rx_desc_count) {
2020527a6266SJeff Kirsher 		netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
2021527a6266SJeff Kirsher 			   rxq->rx_desc_count);
2022527a6266SJeff Kirsher 	}
2023527a6266SJeff Kirsher 
2024527a6266SJeff Kirsher 	if (rxq->index == 0 &&
2025527a6266SJeff Kirsher 	    rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
2026527a6266SJeff Kirsher 		iounmap(rxq->rx_desc_area);
2027527a6266SJeff Kirsher 	else
2028527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
2029527a6266SJeff Kirsher 				  rxq->rx_desc_area, rxq->rx_desc_dma);
2030527a6266SJeff Kirsher 
2031527a6266SJeff Kirsher 	kfree(rxq->rx_skb);
2032527a6266SJeff Kirsher }
2033527a6266SJeff Kirsher 
txq_init(struct mv643xx_eth_private * mp,int index)2034527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index)
2035527a6266SJeff Kirsher {
2036527a6266SJeff Kirsher 	struct tx_queue *txq = mp->txq + index;
2037527a6266SJeff Kirsher 	struct tx_desc *tx_desc;
2038527a6266SJeff Kirsher 	int size;
20399e911414SEzequiel Garcia 	int ret;
2040527a6266SJeff Kirsher 	int i;
2041527a6266SJeff Kirsher 
2042527a6266SJeff Kirsher 	txq->index = index;
2043527a6266SJeff Kirsher 
2044527a6266SJeff Kirsher 	txq->tx_ring_size = mp->tx_ring_size;
2045527a6266SJeff Kirsher 
2046ee9e4956SEzequiel Garcia 	/* A queue must always have room for at least one skb.
2047ee9e4956SEzequiel Garcia 	 * Therefore, stop the queue when the free entries reaches
2048ee9e4956SEzequiel Garcia 	 * the maximum number of descriptors per skb.
2049ee9e4956SEzequiel Garcia 	 */
2050ee9e4956SEzequiel Garcia 	txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS;
2051ee9e4956SEzequiel Garcia 	txq->tx_wake_threshold = txq->tx_stop_threshold / 2;
2052ee9e4956SEzequiel Garcia 
2053527a6266SJeff Kirsher 	txq->tx_desc_count = 0;
2054527a6266SJeff Kirsher 	txq->tx_curr_desc = 0;
2055527a6266SJeff Kirsher 	txq->tx_used_desc = 0;
2056527a6266SJeff Kirsher 
2057527a6266SJeff Kirsher 	size = txq->tx_ring_size * sizeof(struct tx_desc);
2058527a6266SJeff Kirsher 
2059527a6266SJeff Kirsher 	if (index == 0 && size <= mp->tx_desc_sram_size) {
2060527a6266SJeff Kirsher 		txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
2061527a6266SJeff Kirsher 						mp->tx_desc_sram_size);
2062527a6266SJeff Kirsher 		txq->tx_desc_dma = mp->tx_desc_sram_addr;
2063527a6266SJeff Kirsher 	} else {
2064527a6266SJeff Kirsher 		txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
2065527a6266SJeff Kirsher 						       size, &txq->tx_desc_dma,
2066527a6266SJeff Kirsher 						       GFP_KERNEL);
2067527a6266SJeff Kirsher 	}
2068527a6266SJeff Kirsher 
2069527a6266SJeff Kirsher 	if (txq->tx_desc_area == NULL) {
2070527a6266SJeff Kirsher 		netdev_err(mp->dev,
2071527a6266SJeff Kirsher 			   "can't allocate tx ring (%d bytes)\n", size);
2072527a6266SJeff Kirsher 		return -ENOMEM;
2073527a6266SJeff Kirsher 	}
2074527a6266SJeff Kirsher 	memset(txq->tx_desc_area, 0, size);
2075527a6266SJeff Kirsher 
2076527a6266SJeff Kirsher 	txq->tx_desc_area_size = size;
2077527a6266SJeff Kirsher 
207864699336SJoe Perches 	tx_desc = txq->tx_desc_area;
2079527a6266SJeff Kirsher 	for (i = 0; i < txq->tx_ring_size; i++) {
2080527a6266SJeff Kirsher 		struct tx_desc *txd = tx_desc + i;
2081527a6266SJeff Kirsher 		int nexti;
2082527a6266SJeff Kirsher 
2083527a6266SJeff Kirsher 		nexti = i + 1;
2084527a6266SJeff Kirsher 		if (nexti == txq->tx_ring_size)
2085527a6266SJeff Kirsher 			nexti = 0;
2086527a6266SJeff Kirsher 
2087527a6266SJeff Kirsher 		txd->cmd_sts = 0;
2088527a6266SJeff Kirsher 		txd->next_desc_ptr = txq->tx_desc_dma +
2089527a6266SJeff Kirsher 					nexti * sizeof(struct tx_desc);
2090527a6266SJeff Kirsher 	}
2091527a6266SJeff Kirsher 
20929e911414SEzequiel Garcia 	txq->tx_desc_mapping = kcalloc(txq->tx_ring_size, sizeof(char),
20939e911414SEzequiel Garcia 				       GFP_KERNEL);
20949e911414SEzequiel Garcia 	if (!txq->tx_desc_mapping) {
20959e911414SEzequiel Garcia 		ret = -ENOMEM;
20969e911414SEzequiel Garcia 		goto err_free_desc_area;
20979e911414SEzequiel Garcia 	}
20989e911414SEzequiel Garcia 
20993ae8f4e0SEzequiel Garcia 	/* Allocate DMA buffers for TSO MAC/IP/TCP headers */
21003ae8f4e0SEzequiel Garcia 	txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent,
21013ae8f4e0SEzequiel Garcia 					   txq->tx_ring_size * TSO_HEADER_SIZE,
21023ae8f4e0SEzequiel Garcia 					   &txq->tso_hdrs_dma, GFP_KERNEL);
21033ae8f4e0SEzequiel Garcia 	if (txq->tso_hdrs == NULL) {
21049e911414SEzequiel Garcia 		ret = -ENOMEM;
21059e911414SEzequiel Garcia 		goto err_free_desc_mapping;
21063ae8f4e0SEzequiel Garcia 	}
2107527a6266SJeff Kirsher 	skb_queue_head_init(&txq->tx_skb);
2108527a6266SJeff Kirsher 
2109527a6266SJeff Kirsher 	return 0;
21109e911414SEzequiel Garcia 
21119e911414SEzequiel Garcia err_free_desc_mapping:
21129e911414SEzequiel Garcia 	kfree(txq->tx_desc_mapping);
21139e911414SEzequiel Garcia err_free_desc_area:
21149e911414SEzequiel Garcia 	if (index == 0 && size <= mp->tx_desc_sram_size)
21159e911414SEzequiel Garcia 		iounmap(txq->tx_desc_area);
21169e911414SEzequiel Garcia 	else
21179e911414SEzequiel Garcia 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
21189e911414SEzequiel Garcia 				  txq->tx_desc_area, txq->tx_desc_dma);
21199e911414SEzequiel Garcia 	return ret;
2120527a6266SJeff Kirsher }
2121527a6266SJeff Kirsher 
txq_deinit(struct tx_queue * txq)2122527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq)
2123527a6266SJeff Kirsher {
2124527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = txq_to_mp(txq);
2125527a6266SJeff Kirsher 
2126527a6266SJeff Kirsher 	txq_disable(txq);
2127527a6266SJeff Kirsher 	txq_reclaim(txq, txq->tx_ring_size, 1);
2128527a6266SJeff Kirsher 
2129527a6266SJeff Kirsher 	BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
2130527a6266SJeff Kirsher 
2131527a6266SJeff Kirsher 	if (txq->index == 0 &&
2132527a6266SJeff Kirsher 	    txq->tx_desc_area_size <= mp->tx_desc_sram_size)
2133527a6266SJeff Kirsher 		iounmap(txq->tx_desc_area);
2134527a6266SJeff Kirsher 	else
2135527a6266SJeff Kirsher 		dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
2136527a6266SJeff Kirsher 				  txq->tx_desc_area, txq->tx_desc_dma);
21379e911414SEzequiel Garcia 	kfree(txq->tx_desc_mapping);
21389e911414SEzequiel Garcia 
21393ae8f4e0SEzequiel Garcia 	if (txq->tso_hdrs)
21403ae8f4e0SEzequiel Garcia 		dma_free_coherent(mp->dev->dev.parent,
21413ae8f4e0SEzequiel Garcia 				  txq->tx_ring_size * TSO_HEADER_SIZE,
21423ae8f4e0SEzequiel Garcia 				  txq->tso_hdrs, txq->tso_hdrs_dma);
2143527a6266SJeff Kirsher }
2144527a6266SJeff Kirsher 
2145527a6266SJeff Kirsher 
2146527a6266SJeff Kirsher /* netdev ops and related ***************************************************/
mv643xx_eth_collect_events(struct mv643xx_eth_private * mp)2147527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
2148527a6266SJeff Kirsher {
2149527a6266SJeff Kirsher 	u32 int_cause;
2150527a6266SJeff Kirsher 	u32 int_cause_ext;
2151527a6266SJeff Kirsher 
2152527a6266SJeff Kirsher 	int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
2153527a6266SJeff Kirsher 	if (int_cause == 0)
2154527a6266SJeff Kirsher 		return 0;
2155527a6266SJeff Kirsher 
2156527a6266SJeff Kirsher 	int_cause_ext = 0;
2157527a6266SJeff Kirsher 	if (int_cause & INT_EXT) {
2158527a6266SJeff Kirsher 		int_cause &= ~INT_EXT;
2159527a6266SJeff Kirsher 		int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
2160527a6266SJeff Kirsher 	}
2161527a6266SJeff Kirsher 
2162527a6266SJeff Kirsher 	if (int_cause) {
2163527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE, ~int_cause);
2164527a6266SJeff Kirsher 		mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
2165527a6266SJeff Kirsher 				~(rdlp(mp, TXQ_COMMAND) & 0xff);
2166527a6266SJeff Kirsher 		mp->work_rx |= (int_cause & INT_RX) >> 2;
2167527a6266SJeff Kirsher 	}
2168527a6266SJeff Kirsher 
2169527a6266SJeff Kirsher 	int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
2170527a6266SJeff Kirsher 	if (int_cause_ext) {
2171527a6266SJeff Kirsher 		wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
2172527a6266SJeff Kirsher 		if (int_cause_ext & INT_EXT_LINK_PHY)
2173527a6266SJeff Kirsher 			mp->work_link = 1;
2174527a6266SJeff Kirsher 		mp->work_tx |= int_cause_ext & INT_EXT_TX;
2175527a6266SJeff Kirsher 	}
2176527a6266SJeff Kirsher 
2177527a6266SJeff Kirsher 	return 1;
2178527a6266SJeff Kirsher }
2179527a6266SJeff Kirsher 
mv643xx_eth_irq(int irq,void * dev_id)2180527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
2181527a6266SJeff Kirsher {
2182527a6266SJeff Kirsher 	struct net_device *dev = (struct net_device *)dev_id;
2183527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2184527a6266SJeff Kirsher 
2185527a6266SJeff Kirsher 	if (unlikely(!mv643xx_eth_collect_events(mp)))
2186527a6266SJeff Kirsher 		return IRQ_NONE;
2187527a6266SJeff Kirsher 
2188527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
2189527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2190527a6266SJeff Kirsher 
2191527a6266SJeff Kirsher 	return IRQ_HANDLED;
2192527a6266SJeff Kirsher }
2193527a6266SJeff Kirsher 
handle_link_event(struct mv643xx_eth_private * mp)2194527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp)
2195527a6266SJeff Kirsher {
2196527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2197527a6266SJeff Kirsher 	u32 port_status;
2198527a6266SJeff Kirsher 	int speed;
2199527a6266SJeff Kirsher 	int duplex;
2200527a6266SJeff Kirsher 	int fc;
2201527a6266SJeff Kirsher 
2202527a6266SJeff Kirsher 	port_status = rdlp(mp, PORT_STATUS);
2203527a6266SJeff Kirsher 	if (!(port_status & LINK_UP)) {
2204527a6266SJeff Kirsher 		if (netif_carrier_ok(dev)) {
2205527a6266SJeff Kirsher 			int i;
2206527a6266SJeff Kirsher 
2207527a6266SJeff Kirsher 			netdev_info(dev, "link down\n");
2208527a6266SJeff Kirsher 
2209527a6266SJeff Kirsher 			netif_carrier_off(dev);
2210527a6266SJeff Kirsher 
2211527a6266SJeff Kirsher 			for (i = 0; i < mp->txq_count; i++) {
2212527a6266SJeff Kirsher 				struct tx_queue *txq = mp->txq + i;
2213527a6266SJeff Kirsher 
2214527a6266SJeff Kirsher 				txq_reclaim(txq, txq->tx_ring_size, 1);
2215527a6266SJeff Kirsher 				txq_reset_hw_ptr(txq);
2216527a6266SJeff Kirsher 			}
2217527a6266SJeff Kirsher 		}
2218527a6266SJeff Kirsher 		return;
2219527a6266SJeff Kirsher 	}
2220527a6266SJeff Kirsher 
2221527a6266SJeff Kirsher 	switch (port_status & PORT_SPEED_MASK) {
2222527a6266SJeff Kirsher 	case PORT_SPEED_10:
2223527a6266SJeff Kirsher 		speed = 10;
2224527a6266SJeff Kirsher 		break;
2225527a6266SJeff Kirsher 	case PORT_SPEED_100:
2226527a6266SJeff Kirsher 		speed = 100;
2227527a6266SJeff Kirsher 		break;
2228527a6266SJeff Kirsher 	case PORT_SPEED_1000:
2229527a6266SJeff Kirsher 		speed = 1000;
2230527a6266SJeff Kirsher 		break;
2231527a6266SJeff Kirsher 	default:
2232527a6266SJeff Kirsher 		speed = -1;
2233527a6266SJeff Kirsher 		break;
2234527a6266SJeff Kirsher 	}
2235527a6266SJeff Kirsher 	duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
2236527a6266SJeff Kirsher 	fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
2237527a6266SJeff Kirsher 
2238527a6266SJeff Kirsher 	netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
2239527a6266SJeff Kirsher 		    speed, duplex ? "full" : "half", fc ? "en" : "dis");
2240527a6266SJeff Kirsher 
2241527a6266SJeff Kirsher 	if (!netif_carrier_ok(dev))
2242527a6266SJeff Kirsher 		netif_carrier_on(dev);
2243527a6266SJeff Kirsher }
2244527a6266SJeff Kirsher 
mv643xx_eth_poll(struct napi_struct * napi,int budget)2245527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
2246527a6266SJeff Kirsher {
2247527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2248527a6266SJeff Kirsher 	int work_done;
2249527a6266SJeff Kirsher 
2250527a6266SJeff Kirsher 	mp = container_of(napi, struct mv643xx_eth_private, napi);
2251527a6266SJeff Kirsher 
2252527a6266SJeff Kirsher 	if (unlikely(mp->oom)) {
2253527a6266SJeff Kirsher 		mp->oom = 0;
2254527a6266SJeff Kirsher 		del_timer(&mp->rx_oom);
2255527a6266SJeff Kirsher 	}
2256527a6266SJeff Kirsher 
2257527a6266SJeff Kirsher 	work_done = 0;
2258527a6266SJeff Kirsher 	while (work_done < budget) {
2259527a6266SJeff Kirsher 		u8 queue_mask;
2260527a6266SJeff Kirsher 		int queue;
2261527a6266SJeff Kirsher 		int work_tbd;
2262527a6266SJeff Kirsher 
2263527a6266SJeff Kirsher 		if (mp->work_link) {
2264527a6266SJeff Kirsher 			mp->work_link = 0;
2265527a6266SJeff Kirsher 			handle_link_event(mp);
2266527a6266SJeff Kirsher 			work_done++;
2267527a6266SJeff Kirsher 			continue;
2268527a6266SJeff Kirsher 		}
2269527a6266SJeff Kirsher 
2270527a6266SJeff Kirsher 		queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2271527a6266SJeff Kirsher 		if (likely(!mp->oom))
2272527a6266SJeff Kirsher 			queue_mask |= mp->work_rx_refill;
2273527a6266SJeff Kirsher 
2274527a6266SJeff Kirsher 		if (!queue_mask) {
2275527a6266SJeff Kirsher 			if (mv643xx_eth_collect_events(mp))
2276527a6266SJeff Kirsher 				continue;
2277527a6266SJeff Kirsher 			break;
2278527a6266SJeff Kirsher 		}
2279527a6266SJeff Kirsher 
2280527a6266SJeff Kirsher 		queue = fls(queue_mask) - 1;
2281527a6266SJeff Kirsher 		queue_mask = 1 << queue;
2282527a6266SJeff Kirsher 
2283527a6266SJeff Kirsher 		work_tbd = budget - work_done;
2284527a6266SJeff Kirsher 		if (work_tbd > 16)
2285527a6266SJeff Kirsher 			work_tbd = 16;
2286527a6266SJeff Kirsher 
2287527a6266SJeff Kirsher 		if (mp->work_tx_end & queue_mask) {
2288527a6266SJeff Kirsher 			txq_kick(mp->txq + queue);
2289527a6266SJeff Kirsher 		} else if (mp->work_tx & queue_mask) {
2290527a6266SJeff Kirsher 			work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2291527a6266SJeff Kirsher 			txq_maybe_wake(mp->txq + queue);
2292527a6266SJeff Kirsher 		} else if (mp->work_rx & queue_mask) {
2293527a6266SJeff Kirsher 			work_done += rxq_process(mp->rxq + queue, work_tbd);
2294527a6266SJeff Kirsher 		} else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
2295527a6266SJeff Kirsher 			work_done += rxq_refill(mp->rxq + queue, work_tbd);
2296527a6266SJeff Kirsher 		} else {
2297527a6266SJeff Kirsher 			BUG();
2298527a6266SJeff Kirsher 		}
2299527a6266SJeff Kirsher 	}
2300527a6266SJeff Kirsher 
2301527a6266SJeff Kirsher 	if (work_done < budget) {
2302527a6266SJeff Kirsher 		if (mp->oom)
2303527a6266SJeff Kirsher 			mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
23046ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
2305527a6266SJeff Kirsher 		wrlp(mp, INT_MASK, mp->int_mask);
2306527a6266SJeff Kirsher 	}
2307527a6266SJeff Kirsher 
2308527a6266SJeff Kirsher 	return work_done;
2309527a6266SJeff Kirsher }
2310527a6266SJeff Kirsher 
oom_timer_wrapper(struct timer_list * t)2311e99e88a9SKees Cook static inline void oom_timer_wrapper(struct timer_list *t)
2312527a6266SJeff Kirsher {
2313e99e88a9SKees Cook 	struct mv643xx_eth_private *mp = from_timer(mp, t, rx_oom);
2314527a6266SJeff Kirsher 
2315527a6266SJeff Kirsher 	napi_schedule(&mp->napi);
2316527a6266SJeff Kirsher }
2317527a6266SJeff Kirsher 
port_start(struct mv643xx_eth_private * mp)2318527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp)
2319527a6266SJeff Kirsher {
23201e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
2321527a6266SJeff Kirsher 	u32 pscr;
2322527a6266SJeff Kirsher 	int i;
2323527a6266SJeff Kirsher 
2324527a6266SJeff Kirsher 	/*
2325527a6266SJeff Kirsher 	 * Perform PHY reset, if there is a PHY.
2326527a6266SJeff Kirsher 	 */
23271e8a655dSPhilippe Reynes 	if (dev->phydev) {
2328a54e1612SPhilippe Reynes 		struct ethtool_link_ksettings cmd;
2329527a6266SJeff Kirsher 
2330a54e1612SPhilippe Reynes 		mv643xx_eth_get_link_ksettings(dev, &cmd);
23311e8a655dSPhilippe Reynes 		phy_init_hw(dev->phydev);
2332a54e1612SPhilippe Reynes 		mv643xx_eth_set_link_ksettings(
2333a54e1612SPhilippe Reynes 			dev, (const struct ethtool_link_ksettings *)&cmd);
23341e8a655dSPhilippe Reynes 		phy_start(dev->phydev);
2335527a6266SJeff Kirsher 	}
2336527a6266SJeff Kirsher 
2337527a6266SJeff Kirsher 	/*
2338527a6266SJeff Kirsher 	 * Configure basic link parameters.
2339527a6266SJeff Kirsher 	 */
2340527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
2341527a6266SJeff Kirsher 
2342527a6266SJeff Kirsher 	pscr |= SERIAL_PORT_ENABLE;
2343527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2344527a6266SJeff Kirsher 
2345527a6266SJeff Kirsher 	pscr |= DO_NOT_FORCE_LINK_FAIL;
23461e8a655dSPhilippe Reynes 	if (!dev->phydev)
2347527a6266SJeff Kirsher 		pscr |= FORCE_LINK_PASS;
2348527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
2349527a6266SJeff Kirsher 
2350527a6266SJeff Kirsher 	/*
2351527a6266SJeff Kirsher 	 * Configure TX path and queues.
2352527a6266SJeff Kirsher 	 */
2353527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2354527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2355527a6266SJeff Kirsher 		struct tx_queue *txq = mp->txq + i;
2356527a6266SJeff Kirsher 
2357527a6266SJeff Kirsher 		txq_reset_hw_ptr(txq);
2358527a6266SJeff Kirsher 		txq_set_rate(txq, 1000000000, 16777216);
2359527a6266SJeff Kirsher 		txq_set_fixed_prio_mode(txq);
2360527a6266SJeff Kirsher 	}
2361527a6266SJeff Kirsher 
2362527a6266SJeff Kirsher 	/*
2363527a6266SJeff Kirsher 	 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2364527a6266SJeff Kirsher 	 * frames to RX queue #0, and include the pseudo-header when
2365527a6266SJeff Kirsher 	 * calculating receive checksums.
2366527a6266SJeff Kirsher 	 */
2367527a6266SJeff Kirsher 	mv643xx_eth_set_features(mp->dev, mp->dev->features);
2368527a6266SJeff Kirsher 
2369527a6266SJeff Kirsher 	/*
2370527a6266SJeff Kirsher 	 * Treat BPDUs as normal multicasts, and disable partition mode.
2371527a6266SJeff Kirsher 	 */
2372527a6266SJeff Kirsher 	wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
2373527a6266SJeff Kirsher 
2374527a6266SJeff Kirsher 	/*
2375527a6266SJeff Kirsher 	 * Add configured unicast addresses to address filter table.
2376527a6266SJeff Kirsher 	 */
2377527a6266SJeff Kirsher 	mv643xx_eth_program_unicast_filter(mp->dev);
2378527a6266SJeff Kirsher 
2379527a6266SJeff Kirsher 	/*
2380527a6266SJeff Kirsher 	 * Enable the receive queues.
2381527a6266SJeff Kirsher 	 */
2382527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2383527a6266SJeff Kirsher 		struct rx_queue *rxq = mp->rxq + i;
2384527a6266SJeff Kirsher 		u32 addr;
2385527a6266SJeff Kirsher 
2386527a6266SJeff Kirsher 		addr = (u32)rxq->rx_desc_dma;
2387527a6266SJeff Kirsher 		addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
2388527a6266SJeff Kirsher 		wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
2389527a6266SJeff Kirsher 
2390527a6266SJeff Kirsher 		rxq_enable(rxq);
2391527a6266SJeff Kirsher 	}
2392527a6266SJeff Kirsher }
2393527a6266SJeff Kirsher 
mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private * mp)2394527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2395527a6266SJeff Kirsher {
2396527a6266SJeff Kirsher 	int skb_size;
2397527a6266SJeff Kirsher 
2398527a6266SJeff Kirsher 	/*
2399527a6266SJeff Kirsher 	 * Reserve 2+14 bytes for an ethernet header (the hardware
2400527a6266SJeff Kirsher 	 * automatically prepends 2 bytes of dummy data to each
2401527a6266SJeff Kirsher 	 * received packet), 16 bytes for up to four VLAN tags, and
2402527a6266SJeff Kirsher 	 * 4 bytes for the trailing FCS -- 36 bytes total.
2403527a6266SJeff Kirsher 	 */
2404527a6266SJeff Kirsher 	skb_size = mp->dev->mtu + 36;
2405527a6266SJeff Kirsher 
2406527a6266SJeff Kirsher 	/*
2407527a6266SJeff Kirsher 	 * Make sure that the skb size is a multiple of 8 bytes, as
2408527a6266SJeff Kirsher 	 * the lower three bits of the receive descriptor's buffer
2409527a6266SJeff Kirsher 	 * size field are ignored by the hardware.
2410527a6266SJeff Kirsher 	 */
2411527a6266SJeff Kirsher 	mp->skb_size = (skb_size + 7) & ~7;
2412527a6266SJeff Kirsher 
2413527a6266SJeff Kirsher 	/*
2414527a6266SJeff Kirsher 	 * If NET_SKB_PAD is smaller than a cache line,
2415527a6266SJeff Kirsher 	 * netdev_alloc_skb() will cause skb->data to be misaligned
2416527a6266SJeff Kirsher 	 * to a cache line boundary.  If this is the case, include
2417527a6266SJeff Kirsher 	 * some extra space to allow re-aligning the data area.
2418527a6266SJeff Kirsher 	 */
2419527a6266SJeff Kirsher 	mp->skb_size += SKB_DMA_REALIGN;
2420527a6266SJeff Kirsher }
2421527a6266SJeff Kirsher 
mv643xx_eth_open(struct net_device * dev)2422527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev)
2423527a6266SJeff Kirsher {
2424527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2425527a6266SJeff Kirsher 	int err;
2426527a6266SJeff Kirsher 	int i;
2427527a6266SJeff Kirsher 
2428527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE, 0);
2429527a6266SJeff Kirsher 	wrlp(mp, INT_CAUSE_EXT, 0);
2430527a6266SJeff Kirsher 	rdlp(mp, INT_CAUSE_EXT);
2431527a6266SJeff Kirsher 
2432527a6266SJeff Kirsher 	err = request_irq(dev->irq, mv643xx_eth_irq,
2433527a6266SJeff Kirsher 			  IRQF_SHARED, dev->name, dev);
2434527a6266SJeff Kirsher 	if (err) {
2435527a6266SJeff Kirsher 		netdev_err(dev, "can't assign irq\n");
2436527a6266SJeff Kirsher 		return -EAGAIN;
2437527a6266SJeff Kirsher 	}
2438527a6266SJeff Kirsher 
2439527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2440527a6266SJeff Kirsher 
2441527a6266SJeff Kirsher 	napi_enable(&mp->napi);
2442527a6266SJeff Kirsher 
2443527a6266SJeff Kirsher 	mp->int_mask = INT_EXT;
2444527a6266SJeff Kirsher 
2445527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++) {
2446527a6266SJeff Kirsher 		err = rxq_init(mp, i);
2447527a6266SJeff Kirsher 		if (err) {
2448527a6266SJeff Kirsher 			while (--i >= 0)
2449527a6266SJeff Kirsher 				rxq_deinit(mp->rxq + i);
2450527a6266SJeff Kirsher 			goto out;
2451527a6266SJeff Kirsher 		}
2452527a6266SJeff Kirsher 
2453527a6266SJeff Kirsher 		rxq_refill(mp->rxq + i, INT_MAX);
2454527a6266SJeff Kirsher 		mp->int_mask |= INT_RX_0 << i;
2455527a6266SJeff Kirsher 	}
2456527a6266SJeff Kirsher 
2457527a6266SJeff Kirsher 	if (mp->oom) {
2458527a6266SJeff Kirsher 		mp->rx_oom.expires = jiffies + (HZ / 10);
2459527a6266SJeff Kirsher 		add_timer(&mp->rx_oom);
2460527a6266SJeff Kirsher 	}
2461527a6266SJeff Kirsher 
2462527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++) {
2463527a6266SJeff Kirsher 		err = txq_init(mp, i);
2464527a6266SJeff Kirsher 		if (err) {
2465527a6266SJeff Kirsher 			while (--i >= 0)
2466527a6266SJeff Kirsher 				txq_deinit(mp->txq + i);
2467527a6266SJeff Kirsher 			goto out_free;
2468527a6266SJeff Kirsher 		}
2469527a6266SJeff Kirsher 		mp->int_mask |= INT_TX_END_0 << i;
2470527a6266SJeff Kirsher 	}
2471527a6266SJeff Kirsher 
2472f564412cSSebastian Hesselbarth 	add_timer(&mp->mib_counters_timer);
2473527a6266SJeff Kirsher 	port_start(mp);
2474527a6266SJeff Kirsher 
2475527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
2476527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2477527a6266SJeff Kirsher 
2478527a6266SJeff Kirsher 	return 0;
2479527a6266SJeff Kirsher 
2480527a6266SJeff Kirsher 
2481527a6266SJeff Kirsher out_free:
2482527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2483527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2484527a6266SJeff Kirsher out:
2485*f111606bSZhengchao Shao 	napi_disable(&mp->napi);
2486527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2487527a6266SJeff Kirsher 
2488527a6266SJeff Kirsher 	return err;
2489527a6266SJeff Kirsher }
2490527a6266SJeff Kirsher 
port_reset(struct mv643xx_eth_private * mp)2491527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp)
2492527a6266SJeff Kirsher {
2493527a6266SJeff Kirsher 	unsigned int data;
2494527a6266SJeff Kirsher 	int i;
2495527a6266SJeff Kirsher 
2496527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2497527a6266SJeff Kirsher 		rxq_disable(mp->rxq + i);
2498527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2499527a6266SJeff Kirsher 		txq_disable(mp->txq + i);
2500527a6266SJeff Kirsher 
2501527a6266SJeff Kirsher 	while (1) {
2502527a6266SJeff Kirsher 		u32 ps = rdlp(mp, PORT_STATUS);
2503527a6266SJeff Kirsher 
2504527a6266SJeff Kirsher 		if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2505527a6266SJeff Kirsher 			break;
2506527a6266SJeff Kirsher 		udelay(10);
2507527a6266SJeff Kirsher 	}
2508527a6266SJeff Kirsher 
2509527a6266SJeff Kirsher 	/* Reset the Enable bit in the Configuration Register */
2510527a6266SJeff Kirsher 	data = rdlp(mp, PORT_SERIAL_CONTROL);
2511527a6266SJeff Kirsher 	data &= ~(SERIAL_PORT_ENABLE		|
2512527a6266SJeff Kirsher 		  DO_NOT_FORCE_LINK_FAIL	|
2513527a6266SJeff Kirsher 		  FORCE_LINK_PASS);
2514527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, data);
2515527a6266SJeff Kirsher }
2516527a6266SJeff Kirsher 
mv643xx_eth_stop(struct net_device * dev)2517527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev)
2518527a6266SJeff Kirsher {
2519527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2520527a6266SJeff Kirsher 	int i;
2521527a6266SJeff Kirsher 
2522527a6266SJeff Kirsher 	wrlp(mp, INT_MASK_EXT, 0x00000000);
2523527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2524527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2525527a6266SJeff Kirsher 
2526527a6266SJeff Kirsher 	napi_disable(&mp->napi);
2527527a6266SJeff Kirsher 
2528527a6266SJeff Kirsher 	del_timer_sync(&mp->rx_oom);
2529527a6266SJeff Kirsher 
2530527a6266SJeff Kirsher 	netif_carrier_off(dev);
25311e8a655dSPhilippe Reynes 	if (dev->phydev)
25321e8a655dSPhilippe Reynes 		phy_stop(dev->phydev);
2533527a6266SJeff Kirsher 	free_irq(dev->irq, dev);
2534527a6266SJeff Kirsher 
2535527a6266SJeff Kirsher 	port_reset(mp);
2536527a6266SJeff Kirsher 	mv643xx_eth_get_stats(dev);
2537527a6266SJeff Kirsher 	mib_counters_update(mp);
2538527a6266SJeff Kirsher 	del_timer_sync(&mp->mib_counters_timer);
2539527a6266SJeff Kirsher 
2540527a6266SJeff Kirsher 	for (i = 0; i < mp->rxq_count; i++)
2541527a6266SJeff Kirsher 		rxq_deinit(mp->rxq + i);
2542527a6266SJeff Kirsher 	for (i = 0; i < mp->txq_count; i++)
2543527a6266SJeff Kirsher 		txq_deinit(mp->txq + i);
2544527a6266SJeff Kirsher 
2545527a6266SJeff Kirsher 	return 0;
2546527a6266SJeff Kirsher }
2547527a6266SJeff Kirsher 
mv643xx_eth_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)2548527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2549527a6266SJeff Kirsher {
2550260055bbSPhil Sutter 	int ret;
2551527a6266SJeff Kirsher 
25521e8a655dSPhilippe Reynes 	if (!dev->phydev)
2553260055bbSPhil Sutter 		return -ENOTSUPP;
2554527a6266SJeff Kirsher 
25551e8a655dSPhilippe Reynes 	ret = phy_mii_ioctl(dev->phydev, ifr, cmd);
2556260055bbSPhil Sutter 	if (!ret)
25570a9e413bSEzequiel Garcia 		mv643xx_eth_adjust_link(dev);
2558260055bbSPhil Sutter 	return ret;
2559527a6266SJeff Kirsher }
2560527a6266SJeff Kirsher 
mv643xx_eth_change_mtu(struct net_device * dev,int new_mtu)2561527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2562527a6266SJeff Kirsher {
2563527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2564527a6266SJeff Kirsher 
2565527a6266SJeff Kirsher 	dev->mtu = new_mtu;
2566527a6266SJeff Kirsher 	mv643xx_eth_recalc_skb_size(mp);
2567527a6266SJeff Kirsher 	tx_set_rate(mp, 1000000000, 16777216);
2568527a6266SJeff Kirsher 
2569527a6266SJeff Kirsher 	if (!netif_running(dev))
2570527a6266SJeff Kirsher 		return 0;
2571527a6266SJeff Kirsher 
2572527a6266SJeff Kirsher 	/*
2573527a6266SJeff Kirsher 	 * Stop and then re-open the interface. This will allocate RX
2574527a6266SJeff Kirsher 	 * skbs of the new MTU.
2575527a6266SJeff Kirsher 	 * There is a possible danger that the open will not succeed,
2576527a6266SJeff Kirsher 	 * due to memory being full.
2577527a6266SJeff Kirsher 	 */
2578527a6266SJeff Kirsher 	mv643xx_eth_stop(dev);
2579527a6266SJeff Kirsher 	if (mv643xx_eth_open(dev)) {
2580527a6266SJeff Kirsher 		netdev_err(dev,
2581527a6266SJeff Kirsher 			   "fatal error on re-opening device after MTU change\n");
2582527a6266SJeff Kirsher 	}
2583527a6266SJeff Kirsher 
2584527a6266SJeff Kirsher 	return 0;
2585527a6266SJeff Kirsher }
2586527a6266SJeff Kirsher 
tx_timeout_task(struct work_struct * ugly)2587527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly)
2588527a6266SJeff Kirsher {
2589527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
2590527a6266SJeff Kirsher 
2591527a6266SJeff Kirsher 	mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2592527a6266SJeff Kirsher 	if (netif_running(mp->dev)) {
2593527a6266SJeff Kirsher 		netif_tx_stop_all_queues(mp->dev);
2594527a6266SJeff Kirsher 		port_reset(mp);
2595527a6266SJeff Kirsher 		port_start(mp);
2596527a6266SJeff Kirsher 		netif_tx_wake_all_queues(mp->dev);
2597527a6266SJeff Kirsher 	}
2598527a6266SJeff Kirsher }
2599527a6266SJeff Kirsher 
mv643xx_eth_tx_timeout(struct net_device * dev,unsigned int txqueue)26000290bd29SMichael S. Tsirkin static void mv643xx_eth_tx_timeout(struct net_device *dev, unsigned int txqueue)
2601527a6266SJeff Kirsher {
2602527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2603527a6266SJeff Kirsher 
2604527a6266SJeff Kirsher 	netdev_info(dev, "tx timeout\n");
2605527a6266SJeff Kirsher 
2606527a6266SJeff Kirsher 	schedule_work(&mp->tx_timeout_task);
2607527a6266SJeff Kirsher }
2608527a6266SJeff Kirsher 
2609527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
mv643xx_eth_netpoll(struct net_device * dev)2610527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev)
2611527a6266SJeff Kirsher {
2612527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = netdev_priv(dev);
2613527a6266SJeff Kirsher 
2614527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0x00000000);
2615527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
2616527a6266SJeff Kirsher 
2617527a6266SJeff Kirsher 	mv643xx_eth_irq(dev->irq, dev);
2618527a6266SJeff Kirsher 
2619527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, mp->int_mask);
2620527a6266SJeff Kirsher }
2621527a6266SJeff Kirsher #endif
2622527a6266SJeff Kirsher 
2623527a6266SJeff Kirsher 
2624527a6266SJeff Kirsher /* platform glue ************************************************************/
2625527a6266SJeff Kirsher static void
mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private * msp,const struct mbus_dram_target_info * dram)2626527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
262763a9332bSAndrew Lunn 			      const struct mbus_dram_target_info *dram)
2628527a6266SJeff Kirsher {
2629527a6266SJeff Kirsher 	void __iomem *base = msp->base;
2630527a6266SJeff Kirsher 	u32 win_enable;
2631527a6266SJeff Kirsher 	u32 win_protect;
2632527a6266SJeff Kirsher 	int i;
2633527a6266SJeff Kirsher 
2634527a6266SJeff Kirsher 	for (i = 0; i < 6; i++) {
2635527a6266SJeff Kirsher 		writel(0, base + WINDOW_BASE(i));
2636527a6266SJeff Kirsher 		writel(0, base + WINDOW_SIZE(i));
2637527a6266SJeff Kirsher 		if (i < 4)
2638527a6266SJeff Kirsher 			writel(0, base + WINDOW_REMAP_HIGH(i));
2639527a6266SJeff Kirsher 	}
2640527a6266SJeff Kirsher 
2641527a6266SJeff Kirsher 	win_enable = 0x3f;
2642527a6266SJeff Kirsher 	win_protect = 0;
2643527a6266SJeff Kirsher 
2644527a6266SJeff Kirsher 	for (i = 0; i < dram->num_cs; i++) {
264563a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
2646527a6266SJeff Kirsher 
2647527a6266SJeff Kirsher 		writel((cs->base & 0xffff0000) |
2648527a6266SJeff Kirsher 			(cs->mbus_attr << 8) |
2649527a6266SJeff Kirsher 			dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2650527a6266SJeff Kirsher 		writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2651527a6266SJeff Kirsher 
2652527a6266SJeff Kirsher 		win_enable &= ~(1 << i);
2653527a6266SJeff Kirsher 		win_protect |= 3 << (2 * i);
2654527a6266SJeff Kirsher 	}
2655527a6266SJeff Kirsher 
2656527a6266SJeff Kirsher 	writel(win_enable, base + WINDOW_BAR_ENABLE);
2657527a6266SJeff Kirsher 	msp->win_protect = win_protect;
2658527a6266SJeff Kirsher }
2659527a6266SJeff Kirsher 
infer_hw_params(struct mv643xx_eth_shared_private * msp)2660527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2661527a6266SJeff Kirsher {
2662527a6266SJeff Kirsher 	/*
2663527a6266SJeff Kirsher 	 * Check whether we have a 14-bit coal limit field in bits
2664527a6266SJeff Kirsher 	 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2665527a6266SJeff Kirsher 	 * SDMA config register.
2666527a6266SJeff Kirsher 	 */
2667527a6266SJeff Kirsher 	writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2668527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
2669527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 1;
2670527a6266SJeff Kirsher 	else
2671527a6266SJeff Kirsher 		msp->extended_rx_coal_limit = 0;
2672527a6266SJeff Kirsher 
2673527a6266SJeff Kirsher 	/*
2674527a6266SJeff Kirsher 	 * Check whether the MAC supports TX rate control, and if
2675527a6266SJeff Kirsher 	 * yes, whether its associated registers are in the old or
2676527a6266SJeff Kirsher 	 * the new place.
2677527a6266SJeff Kirsher 	 */
2678527a6266SJeff Kirsher 	writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2679527a6266SJeff Kirsher 	if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
2680527a6266SJeff Kirsher 		msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2681527a6266SJeff Kirsher 	} else {
2682527a6266SJeff Kirsher 		writel(7, msp->base + 0x0400 + TX_BW_RATE);
2683527a6266SJeff Kirsher 		if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
2684527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2685527a6266SJeff Kirsher 		else
2686527a6266SJeff Kirsher 			msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2687527a6266SJeff Kirsher 	}
2688527a6266SJeff Kirsher }
2689527a6266SJeff Kirsher 
269076723bcaSSebastian Hesselbarth #if defined(CONFIG_OF)
269176723bcaSSebastian Hesselbarth static const struct of_device_id mv643xx_eth_shared_ids[] = {
269276723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,orion-eth", },
269376723bcaSSebastian Hesselbarth 	{ .compatible = "marvell,kirkwood-eth", },
269476723bcaSSebastian Hesselbarth 	{ }
269576723bcaSSebastian Hesselbarth };
269676723bcaSSebastian Hesselbarth MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids);
269776723bcaSSebastian Hesselbarth #endif
269876723bcaSSebastian Hesselbarth 
2699600cc3c9SChristophe Leroy #ifdef CONFIG_OF_IRQ
270076723bcaSSebastian Hesselbarth #define mv643xx_eth_property(_np, _name, _v)				\
270176723bcaSSebastian Hesselbarth 	do {								\
270276723bcaSSebastian Hesselbarth 		u32 tmp;						\
270376723bcaSSebastian Hesselbarth 		if (!of_property_read_u32(_np, "marvell," _name, &tmp))	\
270476723bcaSSebastian Hesselbarth 			_v = tmp;					\
270576723bcaSSebastian Hesselbarth 	} while (0)
270676723bcaSSebastian Hesselbarth 
270776723bcaSSebastian Hesselbarth static struct platform_device *port_platdev[3];
270876723bcaSSebastian Hesselbarth 
mv643xx_eth_shared_of_remove(void)270942404d8fSMauri Sandberg static void mv643xx_eth_shared_of_remove(void)
271042404d8fSMauri Sandberg {
271142404d8fSMauri Sandberg 	int n;
271242404d8fSMauri Sandberg 
271342404d8fSMauri Sandberg 	for (n = 0; n < 3; n++) {
271442404d8fSMauri Sandberg 		platform_device_del(port_platdev[n]);
271542404d8fSMauri Sandberg 		port_platdev[n] = NULL;
271642404d8fSMauri Sandberg 	}
271742404d8fSMauri Sandberg }
271842404d8fSMauri Sandberg 
mv643xx_eth_shared_of_add_port(struct platform_device * pdev,struct device_node * pnp)271976723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev,
272076723bcaSSebastian Hesselbarth 					  struct device_node *pnp)
272176723bcaSSebastian Hesselbarth {
272276723bcaSSebastian Hesselbarth 	struct platform_device *ppdev;
272376723bcaSSebastian Hesselbarth 	struct mv643xx_eth_platform_data ppd;
272476723bcaSSebastian Hesselbarth 	struct resource res;
272576723bcaSSebastian Hesselbarth 	int ret;
2726785bf6f7SJonas Gorski 	int dev_num = 0;
272776723bcaSSebastian Hesselbarth 
272876723bcaSSebastian Hesselbarth 	memset(&ppd, 0, sizeof(ppd));
272976723bcaSSebastian Hesselbarth 	ppd.shared = pdev;
273076723bcaSSebastian Hesselbarth 
273176723bcaSSebastian Hesselbarth 	memset(&res, 0, sizeof(res));
2732cfbcb61fSSergei Shtylyov 	if (of_irq_to_resource(pnp, 0, &res) <= 0) {
273321c328dcSRob Herring 		dev_err(&pdev->dev, "missing interrupt on %pOFn\n", pnp);
273476723bcaSSebastian Hesselbarth 		return -EINVAL;
273576723bcaSSebastian Hesselbarth 	}
273676723bcaSSebastian Hesselbarth 
273776723bcaSSebastian Hesselbarth 	if (of_property_read_u32(pnp, "reg", &ppd.port_number)) {
273821c328dcSRob Herring 		dev_err(&pdev->dev, "missing reg property on %pOFn\n", pnp);
273976723bcaSSebastian Hesselbarth 		return -EINVAL;
274076723bcaSSebastian Hesselbarth 	}
274176723bcaSSebastian Hesselbarth 
274276723bcaSSebastian Hesselbarth 	if (ppd.port_number >= 3) {
274321c328dcSRob Herring 		dev_err(&pdev->dev, "invalid reg property on %pOFn\n", pnp);
274476723bcaSSebastian Hesselbarth 		return -EINVAL;
274576723bcaSSebastian Hesselbarth 	}
274676723bcaSSebastian Hesselbarth 
2747785bf6f7SJonas Gorski 	while (dev_num < 3 && port_platdev[dev_num])
2748785bf6f7SJonas Gorski 		dev_num++;
2749785bf6f7SJonas Gorski 
2750785bf6f7SJonas Gorski 	if (dev_num == 3) {
2751785bf6f7SJonas Gorski 		dev_err(&pdev->dev, "too many ports registered\n");
2752785bf6f7SJonas Gorski 		return -EINVAL;
2753785bf6f7SJonas Gorski 	}
2754785bf6f7SJonas Gorski 
275542404d8fSMauri Sandberg 	ret = of_get_mac_address(pnp, ppd.mac_addr);
275611f8e7c1SAndrew Lunn 	if (ret == -EPROBE_DEFER)
275742404d8fSMauri Sandberg 		return ret;
275876723bcaSSebastian Hesselbarth 
275976723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size);
276076723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr);
276176723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size);
276276723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size);
276376723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr);
276476723bcaSSebastian Hesselbarth 	mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size);
276576723bcaSSebastian Hesselbarth 
2766d08cb255SDavid Yang 	of_get_phy_mode(pnp, &ppd.interface);
2767d08cb255SDavid Yang 
276876723bcaSSebastian Hesselbarth 	ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0);
276976723bcaSSebastian Hesselbarth 	if (!ppd.phy_node) {
277076723bcaSSebastian Hesselbarth 		ppd.phy_addr = MV643XX_ETH_PHY_NONE;
277176723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "speed", &ppd.speed);
277276723bcaSSebastian Hesselbarth 		of_property_read_u32(pnp, "duplex", &ppd.duplex);
277376723bcaSSebastian Hesselbarth 	}
277476723bcaSSebastian Hesselbarth 
2775785bf6f7SJonas Gorski 	ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num);
277676723bcaSSebastian Hesselbarth 	if (!ppdev)
277776723bcaSSebastian Hesselbarth 		return -ENOMEM;
277876723bcaSSebastian Hesselbarth 	ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
2779b5d82db8SSebastian Hesselbarth 	ppdev->dev.of_node = pnp;
278076723bcaSSebastian Hesselbarth 
278176723bcaSSebastian Hesselbarth 	ret = platform_device_add_resources(ppdev, &res, 1);
278276723bcaSSebastian Hesselbarth 	if (ret)
278376723bcaSSebastian Hesselbarth 		goto port_err;
278476723bcaSSebastian Hesselbarth 
278576723bcaSSebastian Hesselbarth 	ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd));
278676723bcaSSebastian Hesselbarth 	if (ret)
278776723bcaSSebastian Hesselbarth 		goto port_err;
278876723bcaSSebastian Hesselbarth 
278976723bcaSSebastian Hesselbarth 	ret = platform_device_add(ppdev);
279076723bcaSSebastian Hesselbarth 	if (ret)
279176723bcaSSebastian Hesselbarth 		goto port_err;
279276723bcaSSebastian Hesselbarth 
2793785bf6f7SJonas Gorski 	port_platdev[dev_num] = ppdev;
279476723bcaSSebastian Hesselbarth 
279576723bcaSSebastian Hesselbarth 	return 0;
279676723bcaSSebastian Hesselbarth 
279776723bcaSSebastian Hesselbarth port_err:
279876723bcaSSebastian Hesselbarth 	platform_device_put(ppdev);
279976723bcaSSebastian Hesselbarth 	return ret;
280076723bcaSSebastian Hesselbarth }
280176723bcaSSebastian Hesselbarth 
mv643xx_eth_shared_of_probe(struct platform_device * pdev)280276723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
280376723bcaSSebastian Hesselbarth {
280476723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
280576723bcaSSebastian Hesselbarth 	struct device_node *pnp, *np = pdev->dev.of_node;
280676723bcaSSebastian Hesselbarth 	int ret;
280776723bcaSSebastian Hesselbarth 
280876723bcaSSebastian Hesselbarth 	/* bail out if not registered from DT */
280976723bcaSSebastian Hesselbarth 	if (!np)
281076723bcaSSebastian Hesselbarth 		return 0;
281176723bcaSSebastian Hesselbarth 
281276723bcaSSebastian Hesselbarth 	pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL);
281376723bcaSSebastian Hesselbarth 	if (!pd)
281476723bcaSSebastian Hesselbarth 		return -ENOMEM;
281576723bcaSSebastian Hesselbarth 	pdev->dev.platform_data = pd;
281676723bcaSSebastian Hesselbarth 
281776723bcaSSebastian Hesselbarth 	mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit);
281876723bcaSSebastian Hesselbarth 
281976723bcaSSebastian Hesselbarth 	for_each_available_child_of_node(np, pnp) {
282076723bcaSSebastian Hesselbarth 		ret = mv643xx_eth_shared_of_add_port(pdev, pnp);
282126b7974dSJulia Lawall 		if (ret) {
282226b7974dSJulia Lawall 			of_node_put(pnp);
282342404d8fSMauri Sandberg 			mv643xx_eth_shared_of_remove();
282476723bcaSSebastian Hesselbarth 			return ret;
282576723bcaSSebastian Hesselbarth 		}
282626b7974dSJulia Lawall 	}
282776723bcaSSebastian Hesselbarth 	return 0;
282876723bcaSSebastian Hesselbarth }
282976723bcaSSebastian Hesselbarth 
283076723bcaSSebastian Hesselbarth #else
mv643xx_eth_shared_of_probe(struct platform_device * pdev)2831ff20877aSArnd Bergmann static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev)
283276723bcaSSebastian Hesselbarth {
2833ff20877aSArnd Bergmann 	return 0;
283476723bcaSSebastian Hesselbarth }
283576723bcaSSebastian Hesselbarth 
mv643xx_eth_shared_of_remove(void)2836ff20877aSArnd Bergmann static inline void mv643xx_eth_shared_of_remove(void)
2837ff20877aSArnd Bergmann {
2838ff20877aSArnd Bergmann }
283976723bcaSSebastian Hesselbarth #endif
284076723bcaSSebastian Hesselbarth 
mv643xx_eth_shared_probe(struct platform_device * pdev)2841527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2842527a6266SJeff Kirsher {
2843527a6266SJeff Kirsher 	static int mv643xx_eth_version_printed;
284476723bcaSSebastian Hesselbarth 	struct mv643xx_eth_shared_platform_data *pd;
2845527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp;
284663a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
2847527a6266SJeff Kirsher 	struct resource *res;
284876723bcaSSebastian Hesselbarth 	int ret;
2849527a6266SJeff Kirsher 
2850527a6266SJeff Kirsher 	if (!mv643xx_eth_version_printed++)
2851527a6266SJeff Kirsher 		pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2852527a6266SJeff Kirsher 			  mv643xx_eth_driver_version);
2853527a6266SJeff Kirsher 
2854527a6266SJeff Kirsher 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2855527a6266SJeff Kirsher 	if (res == NULL)
2856727f957aSSebastian Hesselbarth 		return -EINVAL;
2857527a6266SJeff Kirsher 
2858727f957aSSebastian Hesselbarth 	msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
2859527a6266SJeff Kirsher 	if (msp == NULL)
2860727f957aSSebastian Hesselbarth 		return -ENOMEM;
286176723bcaSSebastian Hesselbarth 	platform_set_drvdata(pdev, msp);
2862527a6266SJeff Kirsher 
286365a6f969SSebastian Hesselbarth 	msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res));
2864527a6266SJeff Kirsher 	if (msp->base == NULL)
2865727f957aSSebastian Hesselbarth 		return -ENOMEM;
2866527a6266SJeff Kirsher 
286720922486SSebastian Hesselbarth 	msp->clk = devm_clk_get(&pdev->dev, NULL);
286820922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
286920922486SSebastian Hesselbarth 		clk_prepare_enable(msp->clk);
287020922486SSebastian Hesselbarth 
2871527a6266SJeff Kirsher 	/*
2872527a6266SJeff Kirsher 	 * (Re-)program MBUS remapping windows if we are asked to.
2873527a6266SJeff Kirsher 	 */
287463a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
287563a9332bSAndrew Lunn 	if (dram)
287663a9332bSAndrew Lunn 		mv643xx_eth_conf_mbus_windows(msp, dram);
2877527a6266SJeff Kirsher 
287876723bcaSSebastian Hesselbarth 	ret = mv643xx_eth_shared_of_probe(pdev);
287976723bcaSSebastian Hesselbarth 	if (ret)
2880e928b5d6SAlexey Khoroshilov 		goto err_put_clk;
2881bbfa6d0aSJingoo Han 	pd = dev_get_platdata(&pdev->dev);
288276723bcaSSebastian Hesselbarth 
2883527a6266SJeff Kirsher 	msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2884527a6266SJeff Kirsher 					pd->tx_csum_limit : 9 * 1024;
2885527a6266SJeff Kirsher 	infer_hw_params(msp);
2886527a6266SJeff Kirsher 
2887527a6266SJeff Kirsher 	return 0;
2888e928b5d6SAlexey Khoroshilov 
2889e928b5d6SAlexey Khoroshilov err_put_clk:
2890e928b5d6SAlexey Khoroshilov 	if (!IS_ERR(msp->clk))
2891e928b5d6SAlexey Khoroshilov 		clk_disable_unprepare(msp->clk);
2892e928b5d6SAlexey Khoroshilov 	return ret;
2893527a6266SJeff Kirsher }
2894527a6266SJeff Kirsher 
mv643xx_eth_shared_remove(struct platform_device * pdev)2895527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2896527a6266SJeff Kirsher {
2897527a6266SJeff Kirsher 	struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
2898527a6266SJeff Kirsher 
289976723bcaSSebastian Hesselbarth 	mv643xx_eth_shared_of_remove();
290020922486SSebastian Hesselbarth 	if (!IS_ERR(msp->clk))
290120922486SSebastian Hesselbarth 		clk_disable_unprepare(msp->clk);
2902527a6266SJeff Kirsher 	return 0;
2903527a6266SJeff Kirsher }
2904527a6266SJeff Kirsher 
2905527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = {
2906527a6266SJeff Kirsher 	.probe		= mv643xx_eth_shared_probe,
2907527a6266SJeff Kirsher 	.remove		= mv643xx_eth_shared_remove,
2908527a6266SJeff Kirsher 	.driver = {
2909527a6266SJeff Kirsher 		.name	= MV643XX_ETH_SHARED_NAME,
291076723bcaSSebastian Hesselbarth 		.of_match_table = of_match_ptr(mv643xx_eth_shared_ids),
2911527a6266SJeff Kirsher 	},
2912527a6266SJeff Kirsher };
2913527a6266SJeff Kirsher 
phy_addr_set(struct mv643xx_eth_private * mp,int phy_addr)2914527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
2915527a6266SJeff Kirsher {
2916527a6266SJeff Kirsher 	int addr_shift = 5 * mp->port_num;
2917527a6266SJeff Kirsher 	u32 data;
2918527a6266SJeff Kirsher 
2919527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2920527a6266SJeff Kirsher 	data &= ~(0x1f << addr_shift);
2921527a6266SJeff Kirsher 	data |= (phy_addr & 0x1f) << addr_shift;
2922527a6266SJeff Kirsher 	wrl(mp, PHY_ADDR, data);
2923527a6266SJeff Kirsher }
2924527a6266SJeff Kirsher 
phy_addr_get(struct mv643xx_eth_private * mp)2925527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp)
2926527a6266SJeff Kirsher {
2927527a6266SJeff Kirsher 	unsigned int data;
2928527a6266SJeff Kirsher 
2929527a6266SJeff Kirsher 	data = rdl(mp, PHY_ADDR);
2930527a6266SJeff Kirsher 
2931527a6266SJeff Kirsher 	return (data >> (5 * mp->port_num)) & 0x1f;
2932527a6266SJeff Kirsher }
2933527a6266SJeff Kirsher 
set_params(struct mv643xx_eth_private * mp,struct mv643xx_eth_platform_data * pd)2934527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp,
2935527a6266SJeff Kirsher 		       struct mv643xx_eth_platform_data *pd)
2936527a6266SJeff Kirsher {
2937527a6266SJeff Kirsher 	struct net_device *dev = mp->dev;
2938ee9e4956SEzequiel Garcia 	unsigned int tx_ring_size;
2939527a6266SJeff Kirsher 
294015c343ebSJakub Kicinski 	if (is_valid_ether_addr(pd->mac_addr)) {
2941a96d317fSJakub Kicinski 		eth_hw_addr_set(dev, pd->mac_addr);
294215c343ebSJakub Kicinski 	} else {
294315c343ebSJakub Kicinski 		u8 addr[ETH_ALEN];
294415c343ebSJakub Kicinski 
294515c343ebSJakub Kicinski 		uc_addr_get(mp, addr);
294615c343ebSJakub Kicinski 		eth_hw_addr_set(dev, addr);
294715c343ebSJakub Kicinski 	}
2948527a6266SJeff Kirsher 
2949527a6266SJeff Kirsher 	mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2950527a6266SJeff Kirsher 	if (pd->rx_queue_size)
2951527a6266SJeff Kirsher 		mp->rx_ring_size = pd->rx_queue_size;
2952527a6266SJeff Kirsher 	mp->rx_desc_sram_addr = pd->rx_sram_addr;
2953527a6266SJeff Kirsher 	mp->rx_desc_sram_size = pd->rx_sram_size;
2954527a6266SJeff Kirsher 
2955527a6266SJeff Kirsher 	mp->rxq_count = pd->rx_queue_count ? : 1;
2956527a6266SJeff Kirsher 
2957ee9e4956SEzequiel Garcia 	tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2958527a6266SJeff Kirsher 	if (pd->tx_queue_size)
2959ee9e4956SEzequiel Garcia 		tx_ring_size = pd->tx_queue_size;
2960ee9e4956SEzequiel Garcia 
2961ee9e4956SEzequiel Garcia 	mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size,
2962ee9e4956SEzequiel Garcia 				   MV643XX_MAX_SKB_DESCS * 2, 4096);
2963ee9e4956SEzequiel Garcia 	if (mp->tx_ring_size != tx_ring_size)
2964ee9e4956SEzequiel Garcia 		netdev_warn(dev, "TX queue size set to %u (requested %u)\n",
2965ee9e4956SEzequiel Garcia 			    mp->tx_ring_size, tx_ring_size);
2966ee9e4956SEzequiel Garcia 
2967527a6266SJeff Kirsher 	mp->tx_desc_sram_addr = pd->tx_sram_addr;
2968527a6266SJeff Kirsher 	mp->tx_desc_sram_size = pd->tx_sram_size;
2969527a6266SJeff Kirsher 
2970527a6266SJeff Kirsher 	mp->txq_count = pd->tx_queue_count ? : 1;
2971527a6266SJeff Kirsher }
2972527a6266SJeff Kirsher 
get_phy_mode(struct mv643xx_eth_private * mp)2973fd33b244SJason Gunthorpe static int get_phy_mode(struct mv643xx_eth_private *mp)
2974fd33b244SJason Gunthorpe {
2975fd33b244SJason Gunthorpe 	struct device *dev = mp->dev->dev.parent;
29760c65b2b9SAndrew Lunn 	phy_interface_t iface;
29770c65b2b9SAndrew Lunn 	int err;
2978fd33b244SJason Gunthorpe 
2979fd33b244SJason Gunthorpe 	if (dev->of_node)
29800c65b2b9SAndrew Lunn 		err = of_get_phy_mode(dev->of_node, &iface);
2981fd33b244SJason Gunthorpe 
2982fd33b244SJason Gunthorpe 	/* Historical default if unspecified. We could also read/write
2983fd33b244SJason Gunthorpe 	 * the interface state in the PSC1
2984fd33b244SJason Gunthorpe 	 */
29850c65b2b9SAndrew Lunn 	if (!dev->of_node || err)
2986fd33b244SJason Gunthorpe 		iface = PHY_INTERFACE_MODE_GMII;
2987fd33b244SJason Gunthorpe 	return iface;
2988fd33b244SJason Gunthorpe }
2989fd33b244SJason Gunthorpe 
phy_scan(struct mv643xx_eth_private * mp,int phy_addr)2990527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2991527a6266SJeff Kirsher 				   int phy_addr)
2992527a6266SJeff Kirsher {
2993527a6266SJeff Kirsher 	struct phy_device *phydev;
2994527a6266SJeff Kirsher 	int start;
2995527a6266SJeff Kirsher 	int num;
2996527a6266SJeff Kirsher 	int i;
2997c3a07134SFlorian Fainelli 	char phy_id[MII_BUS_ID_SIZE + 3];
2998527a6266SJeff Kirsher 
2999527a6266SJeff Kirsher 	if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
3000527a6266SJeff Kirsher 		start = phy_addr_get(mp) & 0x1f;
3001527a6266SJeff Kirsher 		num = 32;
3002527a6266SJeff Kirsher 	} else {
3003527a6266SJeff Kirsher 		start = phy_addr & 0x1f;
3004527a6266SJeff Kirsher 		num = 1;
3005527a6266SJeff Kirsher 	}
3006527a6266SJeff Kirsher 
3007c3a07134SFlorian Fainelli 	/* Attempt to connect to the PHY using orion-mdio */
3008976c90b9SSimon Baatz 	phydev = ERR_PTR(-ENODEV);
3009527a6266SJeff Kirsher 	for (i = 0; i < num; i++) {
3010527a6266SJeff Kirsher 		int addr = (start + i) & 0x1f;
3011527a6266SJeff Kirsher 
3012c3a07134SFlorian Fainelli 		snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
3013c3a07134SFlorian Fainelli 				"orion-mdio-mii", addr);
3014527a6266SJeff Kirsher 
3015c3a07134SFlorian Fainelli 		phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
3016fd33b244SJason Gunthorpe 				     get_phy_mode(mp));
3017c3a07134SFlorian Fainelli 		if (!IS_ERR(phydev)) {
3018527a6266SJeff Kirsher 			phy_addr_set(mp, addr);
3019c3a07134SFlorian Fainelli 			break;
3020527a6266SJeff Kirsher 		}
3021527a6266SJeff Kirsher 	}
3022527a6266SJeff Kirsher 
3023527a6266SJeff Kirsher 	return phydev;
3024527a6266SJeff Kirsher }
3025527a6266SJeff Kirsher 
phy_init(struct mv643xx_eth_private * mp,int speed,int duplex)3026527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
3027527a6266SJeff Kirsher {
30281e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
30291e8a655dSPhilippe Reynes 	struct phy_device *phy = dev->phydev;
3030527a6266SJeff Kirsher 
3031527a6266SJeff Kirsher 	if (speed == 0) {
3032527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_ENABLE;
3033527a6266SJeff Kirsher 		phy->speed = 0;
3034527a6266SJeff Kirsher 		phy->duplex = 0;
30353c1bcc86SAndrew Lunn 		linkmode_copy(phy->advertising, phy->supported);
30363c1bcc86SAndrew Lunn 		linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT,
30373c1bcc86SAndrew Lunn 				 phy->advertising);
3038527a6266SJeff Kirsher 	} else {
3039527a6266SJeff Kirsher 		phy->autoneg = AUTONEG_DISABLE;
30403c1bcc86SAndrew Lunn 		linkmode_zero(phy->advertising);
3041527a6266SJeff Kirsher 		phy->speed = speed;
3042527a6266SJeff Kirsher 		phy->duplex = duplex;
3043527a6266SJeff Kirsher 	}
3044527a6266SJeff Kirsher 	phy_start_aneg(phy);
3045527a6266SJeff Kirsher }
3046527a6266SJeff Kirsher 
init_pscr(struct mv643xx_eth_private * mp,int speed,int duplex)3047527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
3048527a6266SJeff Kirsher {
30491e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
3050527a6266SJeff Kirsher 	u32 pscr;
3051527a6266SJeff Kirsher 
3052527a6266SJeff Kirsher 	pscr = rdlp(mp, PORT_SERIAL_CONTROL);
3053527a6266SJeff Kirsher 	if (pscr & SERIAL_PORT_ENABLE) {
3054527a6266SJeff Kirsher 		pscr &= ~SERIAL_PORT_ENABLE;
3055527a6266SJeff Kirsher 		wrlp(mp, PORT_SERIAL_CONTROL, pscr);
3056527a6266SJeff Kirsher 	}
3057527a6266SJeff Kirsher 
3058527a6266SJeff Kirsher 	pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
30591e8a655dSPhilippe Reynes 	if (!dev->phydev) {
3060527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
3061527a6266SJeff Kirsher 		if (speed == SPEED_1000)
3062527a6266SJeff Kirsher 			pscr |= SET_GMII_SPEED_TO_1000;
3063527a6266SJeff Kirsher 		else if (speed == SPEED_100)
3064527a6266SJeff Kirsher 			pscr |= SET_MII_SPEED_TO_100;
3065527a6266SJeff Kirsher 
3066527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
3067527a6266SJeff Kirsher 
3068527a6266SJeff Kirsher 		pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
3069527a6266SJeff Kirsher 		if (duplex == DUPLEX_FULL)
3070527a6266SJeff Kirsher 			pscr |= SET_FULL_DUPLEX_MODE;
3071527a6266SJeff Kirsher 	}
3072527a6266SJeff Kirsher 
3073527a6266SJeff Kirsher 	wrlp(mp, PORT_SERIAL_CONTROL, pscr);
3074527a6266SJeff Kirsher }
3075527a6266SJeff Kirsher 
3076527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = {
3077527a6266SJeff Kirsher 	.ndo_open		= mv643xx_eth_open,
3078527a6266SJeff Kirsher 	.ndo_stop		= mv643xx_eth_stop,
3079527a6266SJeff Kirsher 	.ndo_start_xmit		= mv643xx_eth_xmit,
3080527a6266SJeff Kirsher 	.ndo_set_rx_mode	= mv643xx_eth_set_rx_mode,
3081527a6266SJeff Kirsher 	.ndo_set_mac_address	= mv643xx_eth_set_mac_address,
3082527a6266SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
3083a7605370SArnd Bergmann 	.ndo_eth_ioctl		= mv643xx_eth_ioctl,
3084527a6266SJeff Kirsher 	.ndo_change_mtu		= mv643xx_eth_change_mtu,
3085527a6266SJeff Kirsher 	.ndo_set_features	= mv643xx_eth_set_features,
3086527a6266SJeff Kirsher 	.ndo_tx_timeout		= mv643xx_eth_tx_timeout,
3087527a6266SJeff Kirsher 	.ndo_get_stats		= mv643xx_eth_get_stats,
3088527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
3089527a6266SJeff Kirsher 	.ndo_poll_controller	= mv643xx_eth_netpoll,
3090527a6266SJeff Kirsher #endif
3091527a6266SJeff Kirsher };
3092527a6266SJeff Kirsher 
mv643xx_eth_probe(struct platform_device * pdev)3093527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev)
3094527a6266SJeff Kirsher {
3095527a6266SJeff Kirsher 	struct mv643xx_eth_platform_data *pd;
3096527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp;
3097527a6266SJeff Kirsher 	struct net_device *dev;
30981e8a655dSPhilippe Reynes 	struct phy_device *phydev = NULL;
3099d08cb255SDavid Yang 	u32 psc1r;
3100bf2b8342SMinghao Chi 	int err, irq;
3101527a6266SJeff Kirsher 
3102bbfa6d0aSJingoo Han 	pd = dev_get_platdata(&pdev->dev);
3103527a6266SJeff Kirsher 	if (pd == NULL) {
3104527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
3105527a6266SJeff Kirsher 		return -ENODEV;
3106527a6266SJeff Kirsher 	}
3107527a6266SJeff Kirsher 
3108527a6266SJeff Kirsher 	if (pd->shared == NULL) {
3109527a6266SJeff Kirsher 		dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
3110527a6266SJeff Kirsher 		return -ENODEV;
3111527a6266SJeff Kirsher 	}
3112527a6266SJeff Kirsher 
3113527a6266SJeff Kirsher 	dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
3114527a6266SJeff Kirsher 	if (!dev)
3115527a6266SJeff Kirsher 		return -ENOMEM;
3116527a6266SJeff Kirsher 
3117fd33b244SJason Gunthorpe 	SET_NETDEV_DEV(dev, &pdev->dev);
3118527a6266SJeff Kirsher 	mp = netdev_priv(dev);
3119527a6266SJeff Kirsher 	platform_set_drvdata(pdev, mp);
3120527a6266SJeff Kirsher 
3121527a6266SJeff Kirsher 	mp->shared = platform_get_drvdata(pd->shared);
3122527a6266SJeff Kirsher 	mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
3123527a6266SJeff Kirsher 	mp->port_num = pd->port_number;
3124527a6266SJeff Kirsher 
3125527a6266SJeff Kirsher 	mp->dev = dev;
3126527a6266SJeff Kirsher 
3127d08cb255SDavid Yang 	if (of_device_is_compatible(pdev->dev.of_node,
3128d08cb255SDavid Yang 				    "marvell,kirkwood-eth-port")) {
3129d08cb255SDavid Yang 		psc1r = rdlp(mp, PORT_SERIAL_CONTROL1);
3130d08cb255SDavid Yang 
3131cb85215fSSebastian Hesselbarth 		/* Kirkwood resets some registers on gated clocks. Especially
3132cb85215fSSebastian Hesselbarth 		 * CLK125_BYPASS_EN must be cleared but is not available on
3133cb85215fSSebastian Hesselbarth 		 * all other SoCs/System Controllers using this driver.
3134cb85215fSSebastian Hesselbarth 		 */
3135d08cb255SDavid Yang 		psc1r &= ~CLK125_BYPASS_EN;
3136d08cb255SDavid Yang 
3137d08cb255SDavid Yang 		/* On Kirkwood with two Ethernet controllers, if both of them
3138d08cb255SDavid Yang 		 * have RGMII_EN disabled, the first controller will be in GMII
3139d08cb255SDavid Yang 		 * mode and the second one is effectively disabled, instead of
3140d08cb255SDavid Yang 		 * two MII interfaces.
3141d08cb255SDavid Yang 		 *
3142d08cb255SDavid Yang 		 * To enable GMII in the first controller, the second one must
3143d08cb255SDavid Yang 		 * also be configured (and may be enabled) with RGMII_EN
3144d08cb255SDavid Yang 		 * disabled too, even though it cannot be used at all.
3145d08cb255SDavid Yang 		 */
3146d08cb255SDavid Yang 		switch (pd->interface) {
3147d08cb255SDavid Yang 		/* Use internal to denote second controller being disabled */
3148d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_INTERNAL:
3149d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_MII:
3150d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_GMII:
3151d08cb255SDavid Yang 			psc1r &= ~RGMII_EN;
3152d08cb255SDavid Yang 			break;
3153d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_RGMII:
3154d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_RGMII_ID:
3155d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_RGMII_RXID:
3156d08cb255SDavid Yang 		case PHY_INTERFACE_MODE_RGMII_TXID:
3157d08cb255SDavid Yang 			psc1r |= RGMII_EN;
3158d08cb255SDavid Yang 			break;
3159d08cb255SDavid Yang 		default:
3160d08cb255SDavid Yang 			/* Unknown; don't touch */
3161d08cb255SDavid Yang 			break;
3162d08cb255SDavid Yang 		}
3163d08cb255SDavid Yang 
3164d08cb255SDavid Yang 		wrlp(mp, PORT_SERIAL_CONTROL1, psc1r);
3165d08cb255SDavid Yang 	}
3166cb85215fSSebastian Hesselbarth 
3167452503ebSAndrew Lunn 	/*
31689a43a026SAndrew Lunn 	 * Start with a default rate, and if there is a clock, allow
31699a43a026SAndrew Lunn 	 * it to override the default.
3170452503ebSAndrew Lunn 	 */
31719a43a026SAndrew Lunn 	mp->t_clk = 133000000;
317220922486SSebastian Hesselbarth 	mp->clk = devm_clk_get(&pdev->dev, NULL);
3173452503ebSAndrew Lunn 	if (!IS_ERR(mp->clk)) {
3174452503ebSAndrew Lunn 		clk_prepare_enable(mp->clk);
3175452503ebSAndrew Lunn 		mp->t_clk = clk_get_rate(mp->clk);
317676723bcaSSebastian Hesselbarth 	} else if (!IS_ERR(mp->shared->clk)) {
317776723bcaSSebastian Hesselbarth 		mp->t_clk = clk_get_rate(mp->shared->clk);
3178452503ebSAndrew Lunn 	}
317920922486SSebastian Hesselbarth 
3180527a6266SJeff Kirsher 	set_params(mp, pd);
3181527a6266SJeff Kirsher 	netif_set_real_num_tx_queues(dev, mp->txq_count);
3182527a6266SJeff Kirsher 	netif_set_real_num_rx_queues(dev, mp->rxq_count);
3183527a6266SJeff Kirsher 
3184cc9d4598SSebastian Hesselbarth 	err = 0;
3185cc9d4598SSebastian Hesselbarth 	if (pd->phy_node) {
31861e8a655dSPhilippe Reynes 		phydev = of_phy_connect(mp->dev, pd->phy_node,
3187cc9d4598SSebastian Hesselbarth 					mv643xx_eth_adjust_link, 0,
3188fd33b244SJason Gunthorpe 					get_phy_mode(mp));
31891e8a655dSPhilippe Reynes 		if (!phydev)
3190cc9d4598SSebastian Hesselbarth 			err = -ENODEV;
31916115c11fSDan Carpenter 		else
31921e8a655dSPhilippe Reynes 			phy_addr_set(mp, phydev->mdio.addr);
3193cc9d4598SSebastian Hesselbarth 	} else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
31941e8a655dSPhilippe Reynes 		phydev = phy_scan(mp, pd->phy_addr);
3195527a6266SJeff Kirsher 
31961e8a655dSPhilippe Reynes 		if (IS_ERR(phydev))
31971e8a655dSPhilippe Reynes 			err = PTR_ERR(phydev);
3198cc9d4598SSebastian Hesselbarth 		else
3199cc9d4598SSebastian Hesselbarth 			phy_init(mp, pd->speed, pd->duplex);
3200cc9d4598SSebastian Hesselbarth 	}
3201cc9d4598SSebastian Hesselbarth 	if (err == -ENODEV) {
3202976c90b9SSimon Baatz 		err = -EPROBE_DEFER;
3203976c90b9SSimon Baatz 		goto out;
3204976c90b9SSimon Baatz 	}
3205cc9d4598SSebastian Hesselbarth 	if (err)
3206cc9d4598SSebastian Hesselbarth 		goto out;
3207527a6266SJeff Kirsher 
32087ad24ea4SWilfried Klaebe 	dev->ethtool_ops = &mv643xx_eth_ethtool_ops;
3209527a6266SJeff Kirsher 
3210527a6266SJeff Kirsher 	init_pscr(mp, pd->speed, pd->duplex);
3211527a6266SJeff Kirsher 
3212527a6266SJeff Kirsher 
3213527a6266SJeff Kirsher 	mib_counters_clear(mp);
3214527a6266SJeff Kirsher 
3215e99e88a9SKees Cook 	timer_setup(&mp->mib_counters_timer, mib_counters_timer_wrapper, 0);
3216527a6266SJeff Kirsher 	mp->mib_counters_timer.expires = jiffies + 30 * HZ;
3217527a6266SJeff Kirsher 
3218527a6266SJeff Kirsher 	spin_lock_init(&mp->mib_counters_lock);
3219527a6266SJeff Kirsher 
3220527a6266SJeff Kirsher 	INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
3221527a6266SJeff Kirsher 
3222b48b89f9SJakub Kicinski 	netif_napi_add(dev, &mp->napi, mv643xx_eth_poll);
3223527a6266SJeff Kirsher 
3224e99e88a9SKees Cook 	timer_setup(&mp->rx_oom, oom_timer_wrapper, 0);
3225527a6266SJeff Kirsher 
3226527a6266SJeff Kirsher 
3227bf2b8342SMinghao Chi 	irq = platform_get_irq(pdev, 0);
3228571703ffSMinghao Chi 	if (WARN_ON(irq < 0)) {
3229571703ffSMinghao Chi 		err = irq;
3230571703ffSMinghao Chi 		goto out;
3231571703ffSMinghao Chi 	}
3232bf2b8342SMinghao Chi 	dev->irq = irq;
3233527a6266SJeff Kirsher 
3234527a6266SJeff Kirsher 	dev->netdev_ops = &mv643xx_eth_netdev_ops;
3235527a6266SJeff Kirsher 
3236527a6266SJeff Kirsher 	dev->watchdog_timeo = 2 * HZ;
3237527a6266SJeff Kirsher 	dev->base_addr = 0;
3238527a6266SJeff Kirsher 
32393ae8f4e0SEzequiel Garcia 	dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
32404d48d589SEzequiel Garcia 	dev->vlan_features = dev->features;
32414d48d589SEzequiel Garcia 
32424d48d589SEzequiel Garcia 	dev->features |= NETIF_F_RXCSUM;
32434d48d589SEzequiel Garcia 	dev->hw_features = dev->features;
3244527a6266SJeff Kirsher 
324501789349SJiri Pirko 	dev->priv_flags |= IFF_UNICAST_FLT;
3246ee8b7a11SJakub Kicinski 	netif_set_tso_max_segs(dev, MV643XX_MAX_TSO_SEGS);
324701789349SJiri Pirko 
3248d894be57SJarod Wilson 	/* MTU range: 64 - 9500 */
3249d894be57SJarod Wilson 	dev->min_mtu = 64;
3250d894be57SJarod Wilson 	dev->max_mtu = 9500;
3251d894be57SJarod Wilson 
3252527a6266SJeff Kirsher 	if (mp->shared->win_protect)
3253527a6266SJeff Kirsher 		wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
3254527a6266SJeff Kirsher 
3255527a6266SJeff Kirsher 	netif_carrier_off(dev);
3256527a6266SJeff Kirsher 
3257527a6266SJeff Kirsher 	wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
3258527a6266SJeff Kirsher 
3259527a6266SJeff Kirsher 	set_rx_coal(mp, 250);
3260527a6266SJeff Kirsher 	set_tx_coal(mp, 0);
3261527a6266SJeff Kirsher 
3262527a6266SJeff Kirsher 	err = register_netdev(dev);
3263527a6266SJeff Kirsher 	if (err)
3264527a6266SJeff Kirsher 		goto out;
3265527a6266SJeff Kirsher 
3266527a6266SJeff Kirsher 	netdev_notice(dev, "port %d with MAC address %pM\n",
3267527a6266SJeff Kirsher 		      mp->port_num, dev->dev_addr);
3268527a6266SJeff Kirsher 
3269527a6266SJeff Kirsher 	if (mp->tx_desc_sram_size > 0)
3270527a6266SJeff Kirsher 		netdev_notice(dev, "configured with sram\n");
3271527a6266SJeff Kirsher 
3272527a6266SJeff Kirsher 	return 0;
3273527a6266SJeff Kirsher 
3274527a6266SJeff Kirsher out:
327520922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
3276baffab28SSimon Baatz 		clk_disable_unprepare(mp->clk);
3277527a6266SJeff Kirsher 	free_netdev(dev);
3278527a6266SJeff Kirsher 
3279527a6266SJeff Kirsher 	return err;
3280527a6266SJeff Kirsher }
3281527a6266SJeff Kirsher 
mv643xx_eth_remove(struct platform_device * pdev)3282527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev)
3283527a6266SJeff Kirsher {
3284527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
32851e8a655dSPhilippe Reynes 	struct net_device *dev = mp->dev;
3286527a6266SJeff Kirsher 
3287527a6266SJeff Kirsher 	unregister_netdev(mp->dev);
32881e8a655dSPhilippe Reynes 	if (dev->phydev)
32891e8a655dSPhilippe Reynes 		phy_disconnect(dev->phydev);
3290527a6266SJeff Kirsher 	cancel_work_sync(&mp->tx_timeout_task);
3291452503ebSAndrew Lunn 
329220922486SSebastian Hesselbarth 	if (!IS_ERR(mp->clk))
3293452503ebSAndrew Lunn 		clk_disable_unprepare(mp->clk);
32949a43a026SAndrew Lunn 
3295527a6266SJeff Kirsher 	free_netdev(mp->dev);
3296527a6266SJeff Kirsher 
3297527a6266SJeff Kirsher 	return 0;
3298527a6266SJeff Kirsher }
3299527a6266SJeff Kirsher 
mv643xx_eth_shutdown(struct platform_device * pdev)3300527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev)
3301527a6266SJeff Kirsher {
3302527a6266SJeff Kirsher 	struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
3303527a6266SJeff Kirsher 
3304527a6266SJeff Kirsher 	/* Mask all interrupts on ethernet port */
3305527a6266SJeff Kirsher 	wrlp(mp, INT_MASK, 0);
3306527a6266SJeff Kirsher 	rdlp(mp, INT_MASK);
3307527a6266SJeff Kirsher 
3308527a6266SJeff Kirsher 	if (netif_running(mp->dev))
3309527a6266SJeff Kirsher 		port_reset(mp);
3310527a6266SJeff Kirsher }
3311527a6266SJeff Kirsher 
3312527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = {
3313527a6266SJeff Kirsher 	.probe		= mv643xx_eth_probe,
3314527a6266SJeff Kirsher 	.remove		= mv643xx_eth_remove,
3315527a6266SJeff Kirsher 	.shutdown	= mv643xx_eth_shutdown,
3316527a6266SJeff Kirsher 	.driver = {
3317527a6266SJeff Kirsher 		.name	= MV643XX_ETH_NAME,
3318527a6266SJeff Kirsher 	},
3319527a6266SJeff Kirsher };
3320527a6266SJeff Kirsher 
33213b5dde70SThierry Reding static struct platform_driver * const drivers[] = {
33223b5dde70SThierry Reding 	&mv643xx_eth_shared_driver,
33233b5dde70SThierry Reding 	&mv643xx_eth_driver,
33243b5dde70SThierry Reding };
33253b5dde70SThierry Reding 
mv643xx_eth_init_module(void)3326527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void)
3327527a6266SJeff Kirsher {
33283b5dde70SThierry Reding 	return platform_register_drivers(drivers, ARRAY_SIZE(drivers));
3329527a6266SJeff Kirsher }
3330527a6266SJeff Kirsher module_init(mv643xx_eth_init_module);
3331527a6266SJeff Kirsher 
mv643xx_eth_cleanup_module(void)3332527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void)
3333527a6266SJeff Kirsher {
33343b5dde70SThierry Reding 	platform_unregister_drivers(drivers, ARRAY_SIZE(drivers));
3335527a6266SJeff Kirsher }
3336527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module);
3337527a6266SJeff Kirsher 
3338527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
3339527a6266SJeff Kirsher 	      "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
3340527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
3341527a6266SJeff Kirsher MODULE_LICENSE("GPL");
3342527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
3343527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3344