blob: aedbd8256ad1bffee06a8a6fe6fa2cbf69686c14 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a18a2006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Michael Stapelberg3871c382013-03-11 13:56:45 +000023 * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de>
24 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version 2
28 * of the License, or (at your option) any later version.
29 *
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
34 *
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020039
Joe Perches7542db82011-03-02 17:50:35 +000040#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/init.h>
43#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050044#include <linux/in.h>
Lennert Buytenhekc3efab82008-10-02 00:52:39 +000045#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/tcp.h>
47#include <linux/udp.h>
48#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/delay.h>
50#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010051#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/module.h>
53#include <linux/kernel.h>
54#include <linux/spinlock.h>
55#include <linux/workqueue.h>
Lennert Buytenheked944932008-08-26 13:34:19 +020056#include <linux/phy.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020057#include <linux/mv643xx_eth.h>
Lennert Buytenhek10a99482008-11-20 03:57:16 -080058#include <linux/io.h>
59#include <linux/types.h>
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +000060#include <linux/inet_lro.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Andrew Lunn452503e2011-12-24 01:24:24 +010062#include <linux/clk.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020063
Lennert Buytenheke5371492008-06-01 02:18:13 +020064static char mv643xx_eth_driver_name[] = "mv643xx_eth";
Lennert Buytenhek042af532008-08-24 09:01:57 +020065static char mv643xx_eth_driver_version[] = "1.4";
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020066
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020067
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020068/*
69 * Registers shared between all ports.
70 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020071#define PHY_ADDR 0x0000
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020072#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
73#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
74#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
75#define WINDOW_BAR_ENABLE 0x0290
76#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020077
78/*
Lennert Buytenhek37a60842008-11-20 03:57:36 -080079 * Main per-port registers. These live at offset 0x0400 for
80 * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020081 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -080082#define PORT_CONFIG 0x0000
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020083#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek37a60842008-11-20 03:57:36 -080084#define PORT_CONFIG_EXT 0x0004
85#define MAC_ADDR_LOW 0x0014
86#define MAC_ADDR_HIGH 0x0018
87#define SDMA_CONFIG 0x001c
Lennert Buytenhekbecfad92009-05-06 03:01:17 +000088#define TX_BURST_SIZE_16_64BIT 0x01000000
89#define TX_BURST_SIZE_4_64BIT 0x00800000
90#define BLM_TX_NO_SWAP 0x00000020
91#define BLM_RX_NO_SWAP 0x00000010
92#define RX_BURST_SIZE_16_64BIT 0x00000008
93#define RX_BURST_SIZE_4_64BIT 0x00000004
Lennert Buytenhek37a60842008-11-20 03:57:36 -080094#define PORT_SERIAL_CONTROL 0x003c
Lennert Buytenhekbecfad92009-05-06 03:01:17 +000095#define SET_MII_SPEED_TO_100 0x01000000
96#define SET_GMII_SPEED_TO_1000 0x00800000
97#define SET_FULL_DUPLEX_MODE 0x00200000
98#define MAX_RX_PACKET_9700BYTE 0x000a0000
99#define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000
100#define DO_NOT_FORCE_LINK_FAIL 0x00000400
101#define SERIAL_PORT_CONTROL_RESERVED 0x00000200
102#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008
103#define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004
104#define FORCE_LINK_PASS 0x00000002
105#define SERIAL_PORT_ENABLE 0x00000001
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800106#define PORT_STATUS 0x0044
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200107#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhekae9ae062008-07-15 02:15:24 +0200108#define TX_IN_PROGRESS 0x00000080
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +0200109#define PORT_SPEED_MASK 0x00000030
110#define PORT_SPEED_1000 0x00000010
111#define PORT_SPEED_100 0x00000020
112#define PORT_SPEED_10 0x00000000
113#define FLOW_CONTROL_ENABLED 0x00000008
114#define FULL_DUPLEX 0x00000004
Lennert Buytenhek81600eea92008-07-14 14:29:40 +0200115#define LINK_UP 0x00000002
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800116#define TXQ_COMMAND 0x0048
117#define TXQ_FIX_PRIO_CONF 0x004c
118#define TX_BW_RATE 0x0050
119#define TX_BW_MTU 0x0058
120#define TX_BW_BURST 0x005c
121#define INT_CAUSE 0x0060
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200122#define INT_TX_END 0x07f80000
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000123#define INT_TX_END_0 0x00080000
Lennert Buytenhekbefefe22008-08-28 06:10:04 +0200124#define INT_RX 0x000003fc
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000125#define INT_RX_0 0x00000004
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200126#define INT_EXT 0x00000002
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800127#define INT_CAUSE_EXT 0x0064
Lennert Buytenhekbefefe22008-08-28 06:10:04 +0200128#define INT_EXT_LINK_PHY 0x00110000
129#define INT_EXT_TX 0x000000ff
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800130#define INT_MASK 0x0068
131#define INT_MASK_EXT 0x006c
132#define TX_FIFO_URGENT_THRESHOLD 0x0074
Paulius Zaleckas302476c2012-01-23 01:16:35 +0000133#define RX_DISCARD_FRAME_CNT 0x0084
134#define RX_OVERRUN_FRAME_CNT 0x0088
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800135#define TXQ_FIX_PRIO_CONF_MOVED 0x00dc
136#define TX_BW_RATE_MOVED 0x00e0
137#define TX_BW_MTU_MOVED 0x00e8
138#define TX_BW_BURST_MOVED 0x00ec
139#define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4))
140#define RXQ_COMMAND 0x0280
141#define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2))
142#define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4))
143#define TXQ_BW_CONF(q) (0x0304 + ((q) << 4))
144#define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4))
145
146/*
147 * Misc per-port registers.
148 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200149#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
150#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
151#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
152#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200153
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200154
155/*
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000156 * SDMA configuration register default value.
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200157 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200158#if defined(__BIG_ENDIAN)
159#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenheke0c6ef92009-01-19 17:16:00 -0800160 (RX_BURST_SIZE_4_64BIT | \
161 TX_BURST_SIZE_4_64BIT)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200162#elif defined(__LITTLE_ENDIAN)
163#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenheke0c6ef92009-01-19 17:16:00 -0800164 (RX_BURST_SIZE_4_64BIT | \
165 BLM_RX_NO_SWAP | \
166 BLM_TX_NO_SWAP | \
167 TX_BURST_SIZE_4_64BIT)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200168#else
169#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
170#endif
171
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200172
173/*
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000174 * Misc definitions.
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200175 */
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000176#define DEFAULT_RX_QUEUE_SIZE 128
177#define DEFAULT_TX_QUEUE_SIZE 256
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +0000178#define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200180
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200181/*
182 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200183 */
184#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200185struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200186 u16 byte_cnt; /* Descriptor buffer byte count */
187 u16 buf_size; /* Buffer size */
188 u32 cmd_sts; /* Descriptor command status */
189 u32 next_desc_ptr; /* Next descriptor pointer */
190 u32 buf_ptr; /* Descriptor buffer pointer */
191};
192
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200193struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200194 u16 byte_cnt; /* buffer byte count */
195 u16 l4i_chk; /* CPU provided TCP checksum */
196 u32 cmd_sts; /* Command/status field */
197 u32 next_desc_ptr; /* Pointer to next descriptor */
198 u32 buf_ptr; /* pointer to buffer for this descriptor*/
199};
200#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200201struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200202 u32 cmd_sts; /* Descriptor command status */
203 u16 buf_size; /* Buffer size */
204 u16 byte_cnt; /* Descriptor buffer byte count */
205 u32 buf_ptr; /* Descriptor buffer pointer */
206 u32 next_desc_ptr; /* Next descriptor pointer */
207};
208
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200209struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200210 u32 cmd_sts; /* Command/status field */
211 u16 l4i_chk; /* CPU provided TCP checksum */
212 u16 byte_cnt; /* buffer byte count */
213 u32 buf_ptr; /* pointer to buffer for this descriptor*/
214 u32 next_desc_ptr; /* Pointer to next descriptor */
215};
216#else
217#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
218#endif
219
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200220/* RX & TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200221#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200222
223/* RX & TX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200224#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200225
226/* RX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200227#define LAYER_4_CHECKSUM_OK 0x40000000
228#define RX_ENABLE_INTERRUPT 0x20000000
229#define RX_FIRST_DESC 0x08000000
230#define RX_LAST_DESC 0x04000000
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000231#define RX_IP_HDR_OK 0x02000000
232#define RX_PKT_IS_IPV4 0x01000000
233#define RX_PKT_IS_ETHERNETV2 0x00800000
234#define RX_PKT_LAYER4_TYPE_MASK 0x00600000
235#define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000
236#define RX_PKT_IS_VLAN_TAGGED 0x00080000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200237
238/* TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200239#define TX_ENABLE_INTERRUPT 0x00800000
240#define GEN_CRC 0x00400000
241#define TX_FIRST_DESC 0x00200000
242#define TX_LAST_DESC 0x00100000
243#define ZERO_PADDING 0x00080000
244#define GEN_IP_V4_CHECKSUM 0x00040000
245#define GEN_TCP_UDP_CHECKSUM 0x00020000
246#define UDP_FRAME 0x00010000
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200247#define MAC_HDR_EXTRA_4_BYTES 0x00008000
248#define MAC_HDR_EXTRA_8_BYTES 0x00000200
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200249
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200250#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200251
252
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200253/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200254struct mv643xx_eth_shared_private {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200255 /*
256 * Ethernet controller base address.
257 */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200258 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200259
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200260 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200261 * Per-port MBUS window access register value.
262 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200263 u32 win_protect;
264
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200265 /*
266 * Hardware-specific parameters.
267 */
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +0200268 int extended_rx_coal_limit;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200269 int tx_bw_control;
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000270 int tx_csum_limit;
Andrew Lunn452503e2011-12-24 01:24:24 +0100271
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200272};
273
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200274#define TX_BW_CONTROL_ABSENT 0
275#define TX_BW_CONTROL_OLD_LAYOUT 1
276#define TX_BW_CONTROL_NEW_LAYOUT 2
277
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000278static int mv643xx_eth_open(struct net_device *dev);
279static int mv643xx_eth_stop(struct net_device *dev);
280
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200281
282/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200283struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200284 u64 good_octets_received;
285 u32 bad_octets_received;
286 u32 internal_mac_transmit_err;
287 u32 good_frames_received;
288 u32 bad_frames_received;
289 u32 broadcast_frames_received;
290 u32 multicast_frames_received;
291 u32 frames_64_octets;
292 u32 frames_65_to_127_octets;
293 u32 frames_128_to_255_octets;
294 u32 frames_256_to_511_octets;
295 u32 frames_512_to_1023_octets;
296 u32 frames_1024_to_max_octets;
297 u64 good_octets_sent;
298 u32 good_frames_sent;
299 u32 excessive_collision;
300 u32 multicast_frames_sent;
301 u32 broadcast_frames_sent;
302 u32 unrec_mac_control_received;
303 u32 fc_sent;
304 u32 good_fc_received;
305 u32 bad_fc_received;
306 u32 undersize_received;
307 u32 fragments_received;
308 u32 oversize_received;
309 u32 jabber_received;
310 u32 mac_receive_error;
311 u32 bad_crc_event;
312 u32 collision;
313 u32 late_collision;
Paulius Zaleckas302476c2012-01-23 01:16:35 +0000314 /* Non MIB hardware counters */
315 u32 rx_discard;
316 u32 rx_overrun;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200317};
318
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000319struct lro_counters {
320 u32 lro_aggregated;
321 u32 lro_flushed;
322 u32 lro_no_desc;
323};
324
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200325struct rx_queue {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200326 int index;
327
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200328 int rx_ring_size;
329
330 int rx_desc_count;
331 int rx_curr_desc;
332 int rx_used_desc;
333
334 struct rx_desc *rx_desc_area;
335 dma_addr_t rx_desc_dma;
336 int rx_desc_area_size;
337 struct sk_buff **rx_skb;
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000338
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000339 struct net_lro_mgr lro_mgr;
340 struct net_lro_desc lro_arr[8];
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200341};
342
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200343struct tx_queue {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200344 int index;
345
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200346 int tx_ring_size;
347
348 int tx_desc_count;
349 int tx_curr_desc;
350 int tx_used_desc;
351
352 struct tx_desc *tx_desc_area;
353 dma_addr_t tx_desc_dma;
354 int tx_desc_area_size;
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200355
356 struct sk_buff_head tx_skb;
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200357
358 unsigned long tx_packets;
359 unsigned long tx_bytes;
360 unsigned long tx_dropped;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200361};
362
Lennert Buytenheke5371492008-06-01 02:18:13 +0200363struct mv643xx_eth_private {
364 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800365 void __iomem *base;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200366 int port_num;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200367
368 struct net_device *dev;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200369
Lennert Buytenheked944932008-08-26 13:34:19 +0200370 struct phy_device *phy;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200371
Lennert Buytenhek4ff34952008-09-19 05:04:57 +0200372 struct timer_list mib_counters_timer;
373 spinlock_t mib_counters_lock;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200374 struct mib_counters mib_counters;
Lennert Buytenhek4ff34952008-09-19 05:04:57 +0200375
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000376 struct lro_counters lro_counters;
377
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200378 struct work_struct tx_timeout_task;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200379
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200380 struct napi_struct napi;
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000381 u32 int_mask;
Lennert Buytenhek1319eba2009-04-29 11:57:34 +0000382 u8 oom;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200383 u8 work_link;
384 u8 work_tx;
385 u8 work_tx_end;
386 u8 work_rx;
387 u8 work_rx_refill;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200388
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700389 int skb_size;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700390
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200391 /*
392 * RX state.
393 */
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000394 int rx_ring_size;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200395 unsigned long rx_desc_sram_addr;
396 int rx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200397 int rxq_count;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200398 struct timer_list rx_oom;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200399 struct rx_queue rxq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200400
401 /*
402 * TX state.
403 */
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000404 int tx_ring_size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200405 unsigned long tx_desc_sram_addr;
406 int tx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200407 int txq_count;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200408 struct tx_queue txq[8];
Andrew Lunn452503e2011-12-24 01:24:24 +0100409
410 /*
411 * Hardware-specific parameters.
412 */
Andrew Lunn9a43a022012-06-06 06:40:43 +0000413#if defined(CONFIG_HAVE_CLK)
Andrew Lunn452503e2011-12-24 01:24:24 +0100414 struct clk *clk;
Andrew Lunn9a43a022012-06-06 06:40:43 +0000415#endif
Andrew Lunn452503e2011-12-24 01:24:24 +0100416 unsigned int t_clk;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200417};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200419
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200420/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200421static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200423 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800426static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
427{
428 return readl(mp->base + offset);
429}
430
Lennert Buytenheke5371492008-06-01 02:18:13 +0200431static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200433 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800436static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
437{
438 writel(data, mp->base + offset);
439}
440
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200441
442/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200443static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200445 return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200446}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200448static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
449{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200450 return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200451}
452
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200453static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200454{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200455 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800456 wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200457}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700458
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200459static void rxq_disable(struct rx_queue *rxq)
460{
461 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200462 u8 mask = 1 << rxq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200463
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800464 wrlp(mp, RXQ_COMMAND, mask << 8);
465 while (rdlp(mp, RXQ_COMMAND) & mask)
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200466 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200467}
468
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200469static void txq_reset_hw_ptr(struct tx_queue *txq)
470{
471 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200472 u32 addr;
473
474 addr = (u32)txq->tx_desc_dma;
475 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800476 wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200477}
478
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200479static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200480{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200481 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800482 wrlp(mp, TXQ_COMMAND, 1 << txq->index);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200483}
484
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200485static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200486{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200487 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200488 u8 mask = 1 << txq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200489
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800490 wrlp(mp, TXQ_COMMAND, mask << 8);
491 while (rdlp(mp, TXQ_COMMAND) & mask)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200492 udelay(10);
493}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200494
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200495static void txq_maybe_wake(struct tx_queue *txq)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200496{
497 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200498 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200499
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200500 if (netif_tx_queue_stopped(nq)) {
501 __netif_tx_lock(nq, smp_processor_id());
502 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
503 netif_tx_wake_queue(nq);
504 __netif_tx_unlock(nq);
505 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200506}
507
508
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200509/* rx napi ******************************************************************/
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000510static int
511mv643xx_get_skb_header(struct sk_buff *skb, void **iphdr, void **tcph,
512 u64 *hdr_flags, void *priv)
513{
514 unsigned long cmd_sts = (unsigned long)priv;
515
516 /*
517 * Make sure that this packet is Ethernet II, is not VLAN
518 * tagged, is IPv4, has a valid IP header, and is TCP.
519 */
520 if ((cmd_sts & (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
521 RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_MASK |
522 RX_PKT_IS_VLAN_TAGGED)) !=
523 (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
524 RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_TCP_IPV4))
525 return -1;
526
527 skb_reset_network_header(skb);
528 skb_set_transport_header(skb, ip_hdrlen(skb));
529 *iphdr = ip_hdr(skb);
530 *tcph = tcp_hdr(skb);
531 *hdr_flags = LRO_IPV4 | LRO_TCP;
532
533 return 0;
534}
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000535
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200536static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200538 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
539 struct net_device_stats *stats = &mp->dev->stats;
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000540 int lro_flush_needed;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200541 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000543 lro_flush_needed = 0;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200544 rx = 0;
Lennert Buytenhek9e1f3772008-08-24 02:33:47 +0200545 while (rx < budget && rxq->rx_desc_count) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200546 struct rx_desc *rx_desc;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200547 unsigned int cmd_sts;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200548 struct sk_buff *skb;
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200549 u16 byte_cnt;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200550
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200551 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200552
553 cmd_sts = rx_desc->cmd_sts;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200554 if (cmd_sts & BUFFER_OWNED_BY_DMA)
Lennert Buytenhek96587662008-06-01 10:31:56 +0200555 break;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200556 rmb();
557
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200558 skb = rxq->rx_skb[rxq->rx_curr_desc];
559 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200560
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200561 rxq->rx_curr_desc++;
562 if (rxq->rx_curr_desc == rxq->rx_ring_size)
563 rxq->rx_curr_desc = 0;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200564
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700565 dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200566 rx_desc->buf_size, DMA_FROM_DEVICE);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200567 rxq->rx_desc_count--;
568 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700569
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200570 mp->work_rx_refill |= 1 << rxq->index;
571
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200572 byte_cnt = rx_desc->byte_cnt;
573
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700574 /*
575 * Update statistics.
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200576 *
577 * Note that the descriptor byte count includes 2 dummy
578 * bytes automatically inserted by the hardware at the
579 * start of the packet (which we don't count), and a 4
580 * byte CRC at the end of the packet (which we do count).
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700581 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 stats->rx_packets++;
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200583 stats->rx_bytes += byte_cnt - 2;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200586 * In case we received a packet without first / last bits
587 * on, or the error summary bit is set, the packet needs
588 * to be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800590 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
591 != (RX_FIRST_DESC | RX_LAST_DESC))
592 goto err;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200593
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800594 /*
595 * The -4 is for the CRC in the trailer of the
596 * received packet
597 */
598 skb_put(skb, byte_cnt - 2 - 4);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200599
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800600 if (cmd_sts & LAYER_4_CHECKSUM_OK)
601 skb->ip_summed = CHECKSUM_UNNECESSARY;
602 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000603
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000604 if (skb->dev->features & NETIF_F_LRO &&
605 skb->ip_summed == CHECKSUM_UNNECESSARY) {
606 lro_receive_skb(&rxq->lro_mgr, skb, (void *)cmd_sts);
607 lro_flush_needed = 1;
608 } else
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000609 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800611 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800613err:
614 stats->rx_dropped++;
615
616 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
617 (RX_FIRST_DESC | RX_LAST_DESC)) {
618 if (net_ratelimit())
Joe Perches7542db82011-03-02 17:50:35 +0000619 netdev_err(mp->dev,
620 "received packet spanning multiple descriptors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800622
623 if (cmd_sts & ERROR_SUMMARY)
624 stats->rx_errors++;
625
626 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200628
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000629 if (lro_flush_needed)
630 lro_flush_all(&rxq->lro_mgr);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000631
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200632 if (rx < budget)
633 mp->work_rx &= ~(1 << rxq->index);
634
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200635 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200638static int rxq_refill(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200640 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200641 int refilled;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200642
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200643 refilled = 0;
644 while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
645 struct sk_buff *skb;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200646 int rx;
Lennert Buytenhek53771522008-11-20 03:59:04 -0800647 struct rx_desc *rx_desc;
Saeed Bishara530e5572010-01-05 09:15:32 +0000648 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Dumazetacb600d2012-10-05 06:23:55 +0000650 skb = netdev_alloc_skb(mp->dev, mp->skb_size);
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700651
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200652 if (skb == NULL) {
Lennert Buytenhek1319eba2009-04-29 11:57:34 +0000653 mp->oom = 1;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200654 goto oom;
655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +0000657 if (SKB_DMA_REALIGN)
658 skb_reserve(skb, SKB_DMA_REALIGN);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200659
660 refilled++;
661 rxq->rx_desc_count++;
662
663 rx = rxq->rx_used_desc++;
664 if (rxq->rx_used_desc == rxq->rx_ring_size)
665 rxq->rx_used_desc = 0;
666
Lennert Buytenhek53771522008-11-20 03:59:04 -0800667 rx_desc = rxq->rx_desc_area + rx;
668
Saeed Bishara530e5572010-01-05 09:15:32 +0000669 size = skb->end - skb->data;
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700670 rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
Saeed Bishara530e5572010-01-05 09:15:32 +0000671 skb->data, size,
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700672 DMA_FROM_DEVICE);
Saeed Bishara530e5572010-01-05 09:15:32 +0000673 rx_desc->buf_size = size;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200674 rxq->rx_skb[rx] = skb;
675 wmb();
Lennert Buytenhek53771522008-11-20 03:59:04 -0800676 rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200677 wmb();
678
679 /*
680 * The hardware automatically prepends 2 bytes of
681 * dummy data to each received packet, so that the
682 * IP header ends up 16-byte aligned.
683 */
684 skb_reserve(skb, 2);
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200685 }
686
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200687 if (refilled < budget)
688 mp->work_rx_refill &= ~(1 << rxq->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200690oom:
691 return refilled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200694
695/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700696static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
697{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200698 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700699
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700700 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000701 const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
702
703 if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700704 return 1;
705 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200706
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700707 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700708}
709
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200710static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700711{
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700712 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200713 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700714 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700715
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200716 for (frag = 0; frag < nr_frags; frag++) {
717 skb_frag_t *this_frag;
718 int tx_index;
719 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700720
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200721 this_frag = &skb_shinfo(skb)->frags[frag];
Lennert Buytenhek66823b92008-11-20 03:58:09 -0800722 tx_index = txq->tx_curr_desc++;
723 if (txq->tx_curr_desc == txq->tx_ring_size)
724 txq->tx_curr_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200725 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700726
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200727 /*
728 * The last fragment will generate an interrupt
729 * which will free the skb on TX completion.
730 */
731 if (frag == nr_frags - 1) {
732 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
733 ZERO_PADDING | TX_LAST_DESC |
734 TX_ENABLE_INTERRUPT;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200735 } else {
736 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200737 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700738
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700739 desc->l4i_chk = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000740 desc->byte_cnt = skb_frag_size(this_frag);
Ian Campbellf1063582011-08-31 00:46:57 +0000741 desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
742 this_frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000743 skb_frag_size(this_frag),
Ian Campbellf1063582011-08-31 00:46:57 +0000744 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700745 }
746}
747
Byron Bradley324ff2c2008-02-04 23:47:15 -0800748static inline __be16 sum16_as_be(__sum16 sum)
749{
750 return (__force __be16)sum;
751}
752
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200753static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700754{
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200755 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200756 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700757 int tx_index;
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200758 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700759 u32 cmd_sts;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200760 u16 l4i_chk;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700761 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700762
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200763 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200764 l4i_chk = 0;
765
766 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000767 int hdr_len;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200768 int tag_bytes;
769
770 BUG_ON(skb->protocol != htons(ETH_P_IP) &&
771 skb->protocol != htons(ETH_P_8021Q));
772
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000773 hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
774 tag_bytes = hdr_len - ETH_HLEN;
775 if (skb->len - hdr_len > mp->shared->tx_csum_limit ||
776 unlikely(tag_bytes & ~12)) {
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200777 if (skb_checksum_help(skb) == 0)
778 goto no_csum;
779 kfree_skb(skb);
780 return 1;
781 }
782
783 if (tag_bytes & 4)
784 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
785 if (tag_bytes & 8)
786 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
787
788 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
789 GEN_IP_V4_CHECKSUM |
790 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
791
792 switch (ip_hdr(skb)->protocol) {
793 case IPPROTO_UDP:
794 cmd_sts |= UDP_FRAME;
795 l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
796 break;
797 case IPPROTO_TCP:
798 l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
799 break;
800 default:
801 BUG();
802 }
803 } else {
804no_csum:
805 /* Errata BTS #50, IHL must be 5 if no HW checksum */
806 cmd_sts |= 5 << TX_IHL_SHIFT;
807 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700808
Lennert Buytenhek66823b92008-11-20 03:58:09 -0800809 tx_index = txq->tx_curr_desc++;
810 if (txq->tx_curr_desc == txq->tx_ring_size)
811 txq->tx_curr_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200812 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700813
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700814 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200815 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700816 length = skb_headlen(skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700817 } else {
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200818 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700819 length = skb->len;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700820 }
821
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200822 desc->l4i_chk = l4i_chk;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700823 desc->byte_cnt = length;
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700824 desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
825 length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700826
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200827 __skb_queue_tail(&txq->tx_skb, skb);
828
Richard Cochran3b182d72011-06-21 16:01:11 -0700829 skb_tx_timestamp(skb);
830
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700831 /* ensure all other descriptors are written before first cmd_sts */
832 wmb();
833 desc->cmd_sts = cmd_sts;
834
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200835 /* clear TX_END status */
836 mp->work_tx_end &= ~(1 << txq->index);
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200837
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700838 /* ensure all descriptors are written before poking hardware */
839 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200840 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700841
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200842 txq->tx_desc_count += nr_frags + 1;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200843
844 return 0;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700845}
846
Denis Kirjanov0ccfe642009-11-29 17:04:31 -0800847static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200849 struct mv643xx_eth_private *mp = netdev_priv(dev);
Richard Cochran73151ce2011-06-21 16:00:24 -0700850 int length, queue;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200851 struct tx_queue *txq;
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200852 struct netdev_queue *nq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200854 queue = skb_get_queue_mapping(skb);
855 txq = mp->txq + queue;
856 nq = netdev_get_tx_queue(dev, queue);
857
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700858 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200859 txq->tx_dropped++;
Joe Perches7542db82011-03-02 17:50:35 +0000860 netdev_printk(KERN_DEBUG, dev,
861 "failed to linearize skb with tiny unaligned fragment\n");
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700862 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Lennert Buytenhek17cd0a52008-08-24 05:33:55 +0200865 if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200866 if (net_ratelimit())
Joe Perches7542db82011-03-02 17:50:35 +0000867 netdev_err(dev, "tx queue full?!\n");
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200868 kfree_skb(skb);
869 return NETDEV_TX_OK;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700870 }
871
Richard Cochran73151ce2011-06-21 16:00:24 -0700872 length = skb->len;
873
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200874 if (!txq_submit_skb(txq, skb)) {
875 int entries_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Richard Cochran73151ce2011-06-21 16:00:24 -0700877 txq->tx_bytes += length;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200878 txq->tx_packets++;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200879
880 entries_left = txq->tx_ring_size - txq->tx_desc_count;
881 if (entries_left < MAX_SKB_FRAGS + 1)
882 netif_tx_stop_queue(nq);
883 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700884
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700885 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886}
887
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200888
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200889/* tx napi ******************************************************************/
890static void txq_kick(struct tx_queue *txq)
891{
892 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200893 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200894 u32 hw_desc_ptr;
895 u32 expected_ptr;
896
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200897 __netif_tx_lock(nq, smp_processor_id());
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200898
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800899 if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200900 goto out;
901
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800902 hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200903 expected_ptr = (u32)txq->tx_desc_dma +
904 txq->tx_curr_desc * sizeof(struct tx_desc);
905
906 if (hw_desc_ptr != expected_ptr)
907 txq_enable(txq);
908
909out:
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200910 __netif_tx_unlock(nq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200911
912 mp->work_tx_end &= ~(1 << txq->index);
913}
914
915static int txq_reclaim(struct tx_queue *txq, int budget, int force)
916{
917 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200918 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200919 int reclaimed;
920
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200921 __netif_tx_lock(nq, smp_processor_id());
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200922
923 reclaimed = 0;
924 while (reclaimed < budget && txq->tx_desc_count > 0) {
925 int tx_index;
926 struct tx_desc *desc;
927 u32 cmd_sts;
928 struct sk_buff *skb;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200929
930 tx_index = txq->tx_used_desc;
931 desc = &txq->tx_desc_area[tx_index];
932 cmd_sts = desc->cmd_sts;
933
934 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
935 if (!force)
936 break;
937 desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
938 }
939
940 txq->tx_used_desc = tx_index + 1;
941 if (txq->tx_used_desc == txq->tx_ring_size)
942 txq->tx_used_desc = 0;
943
944 reclaimed++;
945 txq->tx_desc_count--;
946
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200947 skb = NULL;
948 if (cmd_sts & TX_LAST_DESC)
949 skb = __skb_dequeue(&txq->tx_skb);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200950
951 if (cmd_sts & ERROR_SUMMARY) {
Joe Perches7542db82011-03-02 17:50:35 +0000952 netdev_info(mp->dev, "tx error\n");
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200953 mp->dev->stats.tx_errors++;
954 }
955
Lennert Buytenheka4189502008-09-13 04:16:15 +0200956 if (cmd_sts & TX_FIRST_DESC) {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700957 dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
Lennert Buytenheka4189502008-09-13 04:16:15 +0200958 desc->byte_cnt, DMA_TO_DEVICE);
959 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700960 dma_unmap_page(mp->dev->dev.parent, desc->buf_ptr,
Lennert Buytenheka4189502008-09-13 04:16:15 +0200961 desc->byte_cnt, DMA_TO_DEVICE);
962 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200963
Eric Dumazetacb600d2012-10-05 06:23:55 +0000964 dev_kfree_skb(skb);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200965 }
966
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200967 __netif_tx_unlock(nq);
968
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200969 if (reclaimed < budget)
970 mp->work_tx &= ~(1 << txq->index);
971
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200972 return reclaimed;
973}
974
975
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200976/* tx rate control **********************************************************/
977/*
978 * Set total maximum TX rate (shared by all TX queues for this port)
979 * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
980 */
981static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
982{
983 int token_rate;
984 int mtu;
985 int bucket_size;
986
Andrew Lunn452503e2011-12-24 01:24:24 +0100987 token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200988 if (token_rate > 1023)
989 token_rate = 1023;
990
991 mtu = (mp->dev->mtu + 255) >> 8;
992 if (mtu > 63)
993 mtu = 63;
994
995 bucket_size = (burst + 255) >> 8;
996 if (bucket_size > 65535)
997 bucket_size = 65535;
998
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200999 switch (mp->shared->tx_bw_control) {
1000 case TX_BW_CONTROL_OLD_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001001 wrlp(mp, TX_BW_RATE, token_rate);
1002 wrlp(mp, TX_BW_MTU, mtu);
1003 wrlp(mp, TX_BW_BURST, bucket_size);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001004 break;
1005 case TX_BW_CONTROL_NEW_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001006 wrlp(mp, TX_BW_RATE_MOVED, token_rate);
1007 wrlp(mp, TX_BW_MTU_MOVED, mtu);
1008 wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001009 break;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02001010 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001011}
1012
1013static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
1014{
1015 struct mv643xx_eth_private *mp = txq_to_mp(txq);
1016 int token_rate;
1017 int bucket_size;
1018
Andrew Lunn452503e2011-12-24 01:24:24 +01001019 token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001020 if (token_rate > 1023)
1021 token_rate = 1023;
1022
1023 bucket_size = (burst + 255) >> 8;
1024 if (bucket_size > 65535)
1025 bucket_size = 65535;
1026
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001027 wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
1028 wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001029}
1030
1031static void txq_set_fixed_prio_mode(struct tx_queue *txq)
1032{
1033 struct mv643xx_eth_private *mp = txq_to_mp(txq);
1034 int off;
1035 u32 val;
1036
1037 /*
1038 * Turn on fixed priority mode.
1039 */
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001040 off = 0;
1041 switch (mp->shared->tx_bw_control) {
1042 case TX_BW_CONTROL_OLD_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001043 off = TXQ_FIX_PRIO_CONF;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001044 break;
1045 case TX_BW_CONTROL_NEW_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001046 off = TXQ_FIX_PRIO_CONF_MOVED;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001047 break;
1048 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001049
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001050 if (off) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001051 val = rdlp(mp, off);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001052 val |= 1 << txq->index;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001053 wrlp(mp, off, val);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001054 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001055}
1056
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001057
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001058/* mii management interface *************************************************/
Phil Sutter260055b2013-03-06 07:49:02 +00001059static void mv643xx_adjust_pscr(struct mv643xx_eth_private *mp)
1060{
1061 u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1062 u32 autoneg_disable = FORCE_LINK_PASS |
1063 DISABLE_AUTO_NEG_SPEED_GMII |
1064 DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1065 DISABLE_AUTO_NEG_FOR_DUPLEX;
1066
1067 if (mp->phy->autoneg == AUTONEG_ENABLE) {
1068 /* enable auto negotiation */
1069 pscr &= ~autoneg_disable;
1070 goto out_write;
1071 }
1072
1073 pscr |= autoneg_disable;
1074
1075 if (mp->phy->speed == SPEED_1000) {
1076 /* force gigabit, half duplex not supported */
1077 pscr |= SET_GMII_SPEED_TO_1000;
1078 pscr |= SET_FULL_DUPLEX_MODE;
1079 goto out_write;
1080 }
1081
1082 pscr &= ~SET_GMII_SPEED_TO_1000;
1083
1084 if (mp->phy->speed == SPEED_100)
1085 pscr |= SET_MII_SPEED_TO_100;
1086 else
1087 pscr &= ~SET_MII_SPEED_TO_100;
1088
1089 if (mp->phy->duplex == DUPLEX_FULL)
1090 pscr |= SET_FULL_DUPLEX_MODE;
1091 else
1092 pscr &= ~SET_FULL_DUPLEX_MODE;
1093
1094out_write:
1095 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1096}
1097
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02001098/* statistics ***************************************************************/
1099static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1100{
1101 struct mv643xx_eth_private *mp = netdev_priv(dev);
1102 struct net_device_stats *stats = &dev->stats;
1103 unsigned long tx_packets = 0;
1104 unsigned long tx_bytes = 0;
1105 unsigned long tx_dropped = 0;
1106 int i;
1107
1108 for (i = 0; i < mp->txq_count; i++) {
1109 struct tx_queue *txq = mp->txq + i;
1110
1111 tx_packets += txq->tx_packets;
1112 tx_bytes += txq->tx_bytes;
1113 tx_dropped += txq->tx_dropped;
1114 }
1115
1116 stats->tx_packets = tx_packets;
1117 stats->tx_bytes = tx_bytes;
1118 stats->tx_dropped = tx_dropped;
1119
1120 return stats;
1121}
1122
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001123static void mv643xx_eth_grab_lro_stats(struct mv643xx_eth_private *mp)
1124{
1125 u32 lro_aggregated = 0;
1126 u32 lro_flushed = 0;
1127 u32 lro_no_desc = 0;
1128 int i;
1129
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001130 for (i = 0; i < mp->rxq_count; i++) {
1131 struct rx_queue *rxq = mp->rxq + i;
1132
1133 lro_aggregated += rxq->lro_mgr.stats.aggregated;
1134 lro_flushed += rxq->lro_mgr.stats.flushed;
1135 lro_no_desc += rxq->lro_mgr.stats.no_desc;
1136 }
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001137
1138 mp->lro_counters.lro_aggregated = lro_aggregated;
1139 mp->lro_counters.lro_flushed = lro_flushed;
1140 mp->lro_counters.lro_no_desc = lro_no_desc;
1141}
1142
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001143static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001144{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001145 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001146}
1147
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001148static void mib_counters_clear(struct mv643xx_eth_private *mp)
1149{
1150 int i;
1151
1152 for (i = 0; i < 0x80; i += 4)
1153 mib_read(mp, i);
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001154
1155 /* Clear non MIB hw counters also */
1156 rdlp(mp, RX_DISCARD_FRAME_CNT);
1157 rdlp(mp, RX_OVERRUN_FRAME_CNT);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001158}
1159
1160static void mib_counters_update(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001161{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001162 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001163
Sebastian Siewior57e8f262009-02-16 11:28:15 +00001164 spin_lock_bh(&mp->mib_counters_lock);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001165 p->good_octets_received += mib_read(mp, 0x00);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001166 p->bad_octets_received += mib_read(mp, 0x08);
1167 p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1168 p->good_frames_received += mib_read(mp, 0x10);
1169 p->bad_frames_received += mib_read(mp, 0x14);
1170 p->broadcast_frames_received += mib_read(mp, 0x18);
1171 p->multicast_frames_received += mib_read(mp, 0x1c);
1172 p->frames_64_octets += mib_read(mp, 0x20);
1173 p->frames_65_to_127_octets += mib_read(mp, 0x24);
1174 p->frames_128_to_255_octets += mib_read(mp, 0x28);
1175 p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1176 p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1177 p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1178 p->good_octets_sent += mib_read(mp, 0x38);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001179 p->good_frames_sent += mib_read(mp, 0x40);
1180 p->excessive_collision += mib_read(mp, 0x44);
1181 p->multicast_frames_sent += mib_read(mp, 0x48);
1182 p->broadcast_frames_sent += mib_read(mp, 0x4c);
1183 p->unrec_mac_control_received += mib_read(mp, 0x50);
1184 p->fc_sent += mib_read(mp, 0x54);
1185 p->good_fc_received += mib_read(mp, 0x58);
1186 p->bad_fc_received += mib_read(mp, 0x5c);
1187 p->undersize_received += mib_read(mp, 0x60);
1188 p->fragments_received += mib_read(mp, 0x64);
1189 p->oversize_received += mib_read(mp, 0x68);
1190 p->jabber_received += mib_read(mp, 0x6c);
1191 p->mac_receive_error += mib_read(mp, 0x70);
1192 p->bad_crc_event += mib_read(mp, 0x74);
1193 p->collision += mib_read(mp, 0x78);
1194 p->late_collision += mib_read(mp, 0x7c);
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001195 /* Non MIB hardware counters */
1196 p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1197 p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
Sebastian Siewior57e8f262009-02-16 11:28:15 +00001198 spin_unlock_bh(&mp->mib_counters_lock);
Lennert Buytenhek4ff34952008-09-19 05:04:57 +02001199
1200 mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1201}
1202
1203static void mib_counters_timer_wrapper(unsigned long _mp)
1204{
1205 struct mv643xx_eth_private *mp = (void *)_mp;
1206
1207 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001208}
1209
1210
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001211/* interrupt coalescing *****************************************************/
1212/*
1213 * Hardware coalescing parameters are set in units of 64 t_clk
1214 * cycles. I.e.:
1215 *
1216 * coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1217 *
1218 * register_value = coal_delay_in_usec * t_clk_rate / 64000000
1219 *
1220 * In the ->set*() methods, we round the computed register value
1221 * to the nearest integer.
1222 */
1223static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1224{
1225 u32 val = rdlp(mp, SDMA_CONFIG);
1226 u64 temp;
1227
1228 if (mp->shared->extended_rx_coal_limit)
1229 temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1230 else
1231 temp = (val & 0x003fff00) >> 8;
1232
1233 temp *= 64000000;
Andrew Lunn452503e2011-12-24 01:24:24 +01001234 do_div(temp, mp->t_clk);
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001235
1236 return (unsigned int)temp;
1237}
1238
1239static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1240{
1241 u64 temp;
1242 u32 val;
1243
Andrew Lunn452503e2011-12-24 01:24:24 +01001244 temp = (u64)usec * mp->t_clk;
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001245 temp += 31999999;
1246 do_div(temp, 64000000);
1247
1248 val = rdlp(mp, SDMA_CONFIG);
1249 if (mp->shared->extended_rx_coal_limit) {
1250 if (temp > 0xffff)
1251 temp = 0xffff;
1252 val &= ~0x023fff80;
1253 val |= (temp & 0x8000) << 10;
1254 val |= (temp & 0x7fff) << 7;
1255 } else {
1256 if (temp > 0x3fff)
1257 temp = 0x3fff;
1258 val &= ~0x003fff00;
1259 val |= (temp & 0x3fff) << 8;
1260 }
1261 wrlp(mp, SDMA_CONFIG, val);
1262}
1263
1264static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1265{
1266 u64 temp;
1267
1268 temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1269 temp *= 64000000;
Andrew Lunn452503e2011-12-24 01:24:24 +01001270 do_div(temp, mp->t_clk);
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001271
1272 return (unsigned int)temp;
1273}
1274
1275static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1276{
1277 u64 temp;
1278
Andrew Lunn452503e2011-12-24 01:24:24 +01001279 temp = (u64)usec * mp->t_clk;
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001280 temp += 31999999;
1281 do_div(temp, 64000000);
1282
1283 if (temp > 0x3fff)
1284 temp = 0x3fff;
1285
1286 wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1287}
1288
1289
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001290/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001291struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001292 char stat_string[ETH_GSTRING_LEN];
1293 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +02001294 int netdev_off;
1295 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001296};
1297
Lennert Buytenhek16820052008-06-01 11:40:29 +02001298#define SSTAT(m) \
1299 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
1300 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001301
Lennert Buytenhek16820052008-06-01 11:40:29 +02001302#define MIBSTAT(m) \
1303 { #m, FIELD_SIZEOF(struct mib_counters, m), \
1304 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1305
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001306#define LROSTAT(m) \
1307 { #m, FIELD_SIZEOF(struct lro_counters, m), \
1308 -1, offsetof(struct mv643xx_eth_private, lro_counters.m) }
1309
Lennert Buytenhek16820052008-06-01 11:40:29 +02001310static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1311 SSTAT(rx_packets),
1312 SSTAT(tx_packets),
1313 SSTAT(rx_bytes),
1314 SSTAT(tx_bytes),
1315 SSTAT(rx_errors),
1316 SSTAT(tx_errors),
1317 SSTAT(rx_dropped),
1318 SSTAT(tx_dropped),
1319 MIBSTAT(good_octets_received),
1320 MIBSTAT(bad_octets_received),
1321 MIBSTAT(internal_mac_transmit_err),
1322 MIBSTAT(good_frames_received),
1323 MIBSTAT(bad_frames_received),
1324 MIBSTAT(broadcast_frames_received),
1325 MIBSTAT(multicast_frames_received),
1326 MIBSTAT(frames_64_octets),
1327 MIBSTAT(frames_65_to_127_octets),
1328 MIBSTAT(frames_128_to_255_octets),
1329 MIBSTAT(frames_256_to_511_octets),
1330 MIBSTAT(frames_512_to_1023_octets),
1331 MIBSTAT(frames_1024_to_max_octets),
1332 MIBSTAT(good_octets_sent),
1333 MIBSTAT(good_frames_sent),
1334 MIBSTAT(excessive_collision),
1335 MIBSTAT(multicast_frames_sent),
1336 MIBSTAT(broadcast_frames_sent),
1337 MIBSTAT(unrec_mac_control_received),
1338 MIBSTAT(fc_sent),
1339 MIBSTAT(good_fc_received),
1340 MIBSTAT(bad_fc_received),
1341 MIBSTAT(undersize_received),
1342 MIBSTAT(fragments_received),
1343 MIBSTAT(oversize_received),
1344 MIBSTAT(jabber_received),
1345 MIBSTAT(mac_receive_error),
1346 MIBSTAT(bad_crc_event),
1347 MIBSTAT(collision),
1348 MIBSTAT(late_collision),
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001349 MIBSTAT(rx_discard),
1350 MIBSTAT(rx_overrun),
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001351 LROSTAT(lro_aggregated),
1352 LROSTAT(lro_flushed),
1353 LROSTAT(lro_no_desc),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001354};
1355
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001356static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001357mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
1358 struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001359{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001360 int err;
1361
Lennert Buytenheked944932008-08-26 13:34:19 +02001362 err = phy_read_status(mp->phy);
1363 if (err == 0)
1364 err = phy_ethtool_gset(mp->phy, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001365
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001366 /*
1367 * The MAC does not support 1000baseT_Half.
1368 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001369 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1370 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1371
1372 return err;
1373}
1374
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001375static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001376mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001377 struct ethtool_cmd *cmd)
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001378{
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001379 u32 port_status;
1380
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001381 port_status = rdlp(mp, PORT_STATUS);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001382
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001383 cmd->supported = SUPPORTED_MII;
1384 cmd->advertising = ADVERTISED_MII;
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001385 switch (port_status & PORT_SPEED_MASK) {
1386 case PORT_SPEED_10:
David Decotigny70739492011-04-27 18:32:40 +00001387 ethtool_cmd_speed_set(cmd, SPEED_10);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001388 break;
1389 case PORT_SPEED_100:
David Decotigny70739492011-04-27 18:32:40 +00001390 ethtool_cmd_speed_set(cmd, SPEED_100);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001391 break;
1392 case PORT_SPEED_1000:
David Decotigny70739492011-04-27 18:32:40 +00001393 ethtool_cmd_speed_set(cmd, SPEED_1000);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001394 break;
1395 default:
1396 cmd->speed = -1;
1397 break;
1398 }
1399 cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001400 cmd->port = PORT_MII;
1401 cmd->phy_address = 0;
1402 cmd->transceiver = XCVR_INTERNAL;
1403 cmd->autoneg = AUTONEG_DISABLE;
1404 cmd->maxtxpkt = 1;
1405 cmd->maxrxpkt = 1;
1406
1407 return 0;
1408}
1409
Michael Stapelberg3871c382013-03-11 13:56:45 +00001410static void
1411mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1412{
1413 struct mv643xx_eth_private *mp = netdev_priv(dev);
1414 wol->supported = 0;
1415 wol->wolopts = 0;
1416 if (mp->phy)
1417 phy_ethtool_get_wol(mp->phy, wol);
1418}
1419
1420static int
1421mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1422{
1423 struct mv643xx_eth_private *mp = netdev_priv(dev);
1424 int err;
1425
1426 if (mp->phy == NULL)
1427 return -EOPNOTSUPP;
1428
1429 err = phy_ethtool_set_wol(mp->phy, wol);
1430 /* Given that mv643xx_eth works without the marvell-specific PHY driver,
1431 * this debugging hint is useful to have.
1432 */
1433 if (err == -EOPNOTSUPP)
1434 netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
1435 return err;
1436}
1437
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001438static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001439mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1440{
1441 struct mv643xx_eth_private *mp = netdev_priv(dev);
1442
1443 if (mp->phy != NULL)
1444 return mv643xx_eth_get_settings_phy(mp, cmd);
1445 else
1446 return mv643xx_eth_get_settings_phyless(mp, cmd);
1447}
1448
1449static int
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001450mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001451{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001452 struct mv643xx_eth_private *mp = netdev_priv(dev);
Phil Sutter260055b2013-03-06 07:49:02 +00001453 int ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001454
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001455 if (mp->phy == NULL)
1456 return -EINVAL;
1457
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001458 /*
1459 * The MAC does not support 1000baseT_Half.
1460 */
1461 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1462
Phil Sutter260055b2013-03-06 07:49:02 +00001463 ret = phy_ethtool_sset(mp->phy, cmd);
1464 if (!ret)
1465 mv643xx_adjust_pscr(mp);
1466 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001467}
1468
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001469static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1470 struct ethtool_drvinfo *drvinfo)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001471{
Axel Lin6f39da22011-11-24 00:41:55 -05001472 strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
1473 sizeof(drvinfo->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001474 strlcpy(drvinfo->version, mv643xx_eth_driver_version,
Axel Lin6f39da22011-11-24 00:41:55 -05001475 sizeof(drvinfo->version));
1476 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1477 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek16820052008-06-01 11:40:29 +02001478 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001479}
1480
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001481static int mv643xx_eth_nway_reset(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001482{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001483 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001484
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001485 if (mp->phy == NULL)
1486 return -EINVAL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001487
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001488 return genphy_restart_aneg(mp->phy);
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001489}
1490
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001491static int
1492mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1493{
1494 struct mv643xx_eth_private *mp = netdev_priv(dev);
1495
1496 ec->rx_coalesce_usecs = get_rx_coal(mp);
1497 ec->tx_coalesce_usecs = get_tx_coal(mp);
1498
1499 return 0;
1500}
1501
1502static int
1503mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1504{
1505 struct mv643xx_eth_private *mp = netdev_priv(dev);
1506
1507 set_rx_coal(mp, ec->rx_coalesce_usecs);
1508 set_tx_coal(mp, ec->tx_coalesce_usecs);
1509
1510 return 0;
1511}
1512
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001513static void
1514mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1515{
1516 struct mv643xx_eth_private *mp = netdev_priv(dev);
1517
1518 er->rx_max_pending = 4096;
1519 er->tx_max_pending = 4096;
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001520
1521 er->rx_pending = mp->rx_ring_size;
1522 er->tx_pending = mp->tx_ring_size;
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001523}
1524
1525static int
1526mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1527{
1528 struct mv643xx_eth_private *mp = netdev_priv(dev);
1529
1530 if (er->rx_mini_pending || er->rx_jumbo_pending)
1531 return -EINVAL;
1532
1533 mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
1534 mp->tx_ring_size = er->tx_pending < 4096 ? er->tx_pending : 4096;
1535
1536 if (netif_running(dev)) {
1537 mv643xx_eth_stop(dev);
1538 if (mv643xx_eth_open(dev)) {
Joe Perches7542db82011-03-02 17:50:35 +00001539 netdev_err(dev,
1540 "fatal error on re-opening device after ring param change\n");
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001541 return -ENOMEM;
1542 }
1543 }
1544
1545 return 0;
1546}
1547
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001548
1549static int
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001550mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001551{
1552 struct mv643xx_eth_private *mp = netdev_priv(dev);
Michał Mirosław3ad9b352011-11-16 14:05:33 +00001553 bool rx_csum = features & NETIF_F_RXCSUM;
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001554
1555 wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1556
1557 return 0;
1558}
1559
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001560static void mv643xx_eth_get_strings(struct net_device *dev,
1561 uint32_t stringset, uint8_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001562{
1563 int i;
1564
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001565 if (stringset == ETH_SS_STATS) {
1566 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001567 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001568 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001569 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001570 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001571 }
1572}
1573
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001574static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1575 struct ethtool_stats *stats,
1576 uint64_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001577{
Lennert Buytenhekb9873842008-08-24 05:59:16 +02001578 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001579 int i;
1580
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02001581 mv643xx_eth_get_stats(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001582 mib_counters_update(mp);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001583 mv643xx_eth_grab_lro_stats(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001584
Lennert Buytenhek16820052008-06-01 11:40:29 +02001585 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1586 const struct mv643xx_eth_stats *stat;
1587 void *p;
1588
1589 stat = mv643xx_eth_stats + i;
1590
1591 if (stat->netdev_off >= 0)
1592 p = ((void *)mp->dev) + stat->netdev_off;
1593 else
1594 p = ((void *)mp) + stat->mp_off;
1595
1596 data[i] = (stat->sizeof_stat == 8) ?
1597 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001598 }
1599}
1600
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001601static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001602{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001603 if (sset == ETH_SS_STATS)
Lennert Buytenhek16820052008-06-01 11:40:29 +02001604 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001605
1606 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001607}
1608
Lennert Buytenheke5371492008-06-01 02:18:13 +02001609static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001610 .get_settings = mv643xx_eth_get_settings,
1611 .set_settings = mv643xx_eth_set_settings,
1612 .get_drvinfo = mv643xx_eth_get_drvinfo,
1613 .nway_reset = mv643xx_eth_nway_reset,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +00001614 .get_link = ethtool_op_get_link,
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001615 .get_coalesce = mv643xx_eth_get_coalesce,
1616 .set_coalesce = mv643xx_eth_set_coalesce,
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001617 .get_ringparam = mv643xx_eth_get_ringparam,
1618 .set_ringparam = mv643xx_eth_set_ringparam,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001619 .get_strings = mv643xx_eth_get_strings,
1620 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001621 .get_sset_count = mv643xx_eth_get_sset_count,
Richard Cochranebad0a82012-04-03 22:59:32 +00001622 .get_ts_info = ethtool_op_get_ts_info,
Michael Stapelberg3871c382013-03-11 13:56:45 +00001623 .get_wol = mv643xx_eth_get_wol,
1624 .set_wol = mv643xx_eth_set_wol,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001625};
1626
1627
1628/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001629static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001630{
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001631 unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1632 unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001633
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001634 addr[0] = (mac_h >> 24) & 0xff;
1635 addr[1] = (mac_h >> 16) & 0xff;
1636 addr[2] = (mac_h >> 8) & 0xff;
1637 addr[3] = mac_h & 0xff;
1638 addr[4] = (mac_l >> 8) & 0xff;
1639 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001640}
1641
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001642static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001643{
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001644 wrlp(mp, MAC_ADDR_HIGH,
1645 (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1646 wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001647}
1648
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001649static u32 uc_addr_filter_mask(struct net_device *dev)
1650{
Jiri Pirkoccffad252009-05-22 23:22:17 +00001651 struct netdev_hw_addr *ha;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001652 u32 nibbles;
1653
1654 if (dev->flags & IFF_PROMISC)
1655 return 0;
1656
1657 nibbles = 1 << (dev->dev_addr[5] & 0x0f);
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001658 netdev_for_each_uc_addr(ha, dev) {
Jiri Pirkoccffad252009-05-22 23:22:17 +00001659 if (memcmp(dev->dev_addr, ha->addr, 5))
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001660 return 0;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001661 if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001662 return 0;
1663
Jiri Pirkoccffad252009-05-22 23:22:17 +00001664 nibbles |= 1 << (ha->addr[5] & 0x0f);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001665 }
1666
1667 return nibbles;
1668}
1669
1670static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001671{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001672 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001673 u32 port_config;
1674 u32 nibbles;
1675 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001676
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001677 uc_addr_set(mp, dev->dev_addr);
1678
Prabhanjan Sarnaik6877f542009-06-18 11:35:02 +00001679 port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1680
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001681 nibbles = uc_addr_filter_mask(dev);
1682 if (!nibbles) {
1683 port_config |= UNICAST_PROMISCUOUS_MODE;
Prabhanjan Sarnaik6877f542009-06-18 11:35:02 +00001684 nibbles = 0xffff;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001685 }
1686
1687 for (i = 0; i < 16; i += 4) {
1688 int off = UNICAST_TABLE(mp->port_num) + i;
1689 u32 v;
1690
1691 v = 0;
1692 if (nibbles & 1)
1693 v |= 0x00000001;
1694 if (nibbles & 2)
1695 v |= 0x00000100;
1696 if (nibbles & 4)
1697 v |= 0x00010000;
1698 if (nibbles & 8)
1699 v |= 0x01000000;
1700 nibbles >>= 4;
1701
1702 wrl(mp, off, v);
1703 }
1704
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001705 wrlp(mp, PORT_CONFIG, port_config);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001706}
1707
Lennert Buytenhek69876562008-06-01 11:43:32 +02001708static int addr_crc(unsigned char *addr)
1709{
1710 int crc = 0;
1711 int i;
1712
1713 for (i = 0; i < 6; i++) {
1714 int j;
1715
1716 crc = (crc ^ addr[i]) << 8;
1717 for (j = 7; j >= 0; j--) {
1718 if (crc & (0x100 << j))
1719 crc ^= 0x107 << j;
1720 }
1721 }
1722
1723 return crc;
1724}
1725
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001726static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001727{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001728 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001729 u32 *mc_spec;
1730 u32 *mc_other;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001731 struct netdev_hw_addr *ha;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001732 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001733
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001734 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001735 int port_num;
1736 u32 accept;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001737
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001738oom:
1739 port_num = mp->port_num;
1740 accept = 0x01010101;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001741 for (i = 0; i < 0x100; i += 4) {
1742 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1743 wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001744 }
1745 return;
1746 }
1747
Sebastian Siewior82a5bd62009-02-14 23:26:18 +00001748 mc_spec = kmalloc(0x200, GFP_ATOMIC);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001749 if (mc_spec == NULL)
1750 goto oom;
1751 mc_other = mc_spec + (0x100 >> 2);
1752
1753 memset(mc_spec, 0, 0x100);
1754 memset(mc_other, 0, 0x100);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001755
Jiri Pirko22bedad32010-04-01 21:22:57 +00001756 netdev_for_each_mc_addr(ha, dev) {
1757 u8 *a = ha->addr;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001758 u32 *table;
1759 int entry;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001760
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001761 if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001762 table = mc_spec;
1763 entry = a[5];
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001764 } else {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001765 table = mc_other;
1766 entry = addr_crc(a);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001767 }
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001768
Lennert Buytenhek2b448332009-01-19 17:17:18 -08001769 table[entry >> 2] |= 1 << (8 * (entry & 3));
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001770 }
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001771
1772 for (i = 0; i < 0x100; i += 4) {
1773 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
1774 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
1775 }
1776
1777 kfree(mc_spec);
1778}
1779
1780static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1781{
1782 mv643xx_eth_program_unicast_filter(dev);
1783 mv643xx_eth_program_multicast_filter(dev);
1784}
1785
1786static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1787{
1788 struct sockaddr *sa = addr;
1789
Denis Kirjanova29ec082009-12-08 20:36:00 -08001790 if (!is_valid_ether_addr(sa->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00001791 return -EADDRNOTAVAIL;
Denis Kirjanova29ec082009-12-08 20:36:00 -08001792
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001793 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1794
1795 netif_addr_lock_bh(dev);
1796 mv643xx_eth_program_unicast_filter(dev);
1797 netif_addr_unlock_bh(dev);
1798
1799 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001800}
1801
1802
1803/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001804static int rxq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001805{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001806 struct rx_queue *rxq = mp->rxq + index;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001807 struct rx_desc *rx_desc;
1808 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001809 int i;
1810
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001811 rxq->index = index;
1812
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001813 rxq->rx_ring_size = mp->rx_ring_size;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001814
1815 rxq->rx_desc_count = 0;
1816 rxq->rx_curr_desc = 0;
1817 rxq->rx_used_desc = 0;
1818
1819 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1820
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001821 if (index == 0 && size <= mp->rx_desc_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001822 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1823 mp->rx_desc_sram_size);
1824 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1825 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001826 rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1827 size, &rxq->rx_desc_dma,
1828 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001829 }
1830
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001831 if (rxq->rx_desc_area == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00001832 netdev_err(mp->dev,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001833 "can't allocate rx ring (%d bytes)\n", size);
1834 goto out;
1835 }
1836 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001837
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001838 rxq->rx_desc_area_size = size;
Joe Perchesb2adaca2013-02-03 17:43:58 +00001839 rxq->rx_skb = kmalloc_array(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1840 GFP_KERNEL);
1841 if (rxq->rx_skb == NULL)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001842 goto out_free;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001843
Joe Perches64699332012-06-04 12:44:16 +00001844 rx_desc = rxq->rx_desc_area;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001845 for (i = 0; i < rxq->rx_ring_size; i++) {
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001846 int nexti;
1847
1848 nexti = i + 1;
1849 if (nexti == rxq->rx_ring_size)
1850 nexti = 0;
1851
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001852 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1853 nexti * sizeof(struct rx_desc);
1854 }
1855
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001856 rxq->lro_mgr.dev = mp->dev;
1857 memset(&rxq->lro_mgr.stats, 0, sizeof(rxq->lro_mgr.stats));
1858 rxq->lro_mgr.features = LRO_F_NAPI;
1859 rxq->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1860 rxq->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1861 rxq->lro_mgr.max_desc = ARRAY_SIZE(rxq->lro_arr);
1862 rxq->lro_mgr.max_aggr = 32;
1863 rxq->lro_mgr.frag_align_pad = 0;
1864 rxq->lro_mgr.lro_arr = rxq->lro_arr;
1865 rxq->lro_mgr.get_skb_header = mv643xx_get_skb_header;
1866
1867 memset(&rxq->lro_arr, 0, sizeof(rxq->lro_arr));
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001868
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001869 return 0;
1870
1871
1872out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001873 if (index == 0 && size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001874 iounmap(rxq->rx_desc_area);
1875 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001876 dma_free_coherent(mp->dev->dev.parent, size,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001877 rxq->rx_desc_area,
1878 rxq->rx_desc_dma);
1879
1880out:
1881 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001882}
1883
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001884static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001885{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001886 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1887 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001888
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001889 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001890
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001891 for (i = 0; i < rxq->rx_ring_size; i++) {
1892 if (rxq->rx_skb[i]) {
1893 dev_kfree_skb(rxq->rx_skb[i]);
1894 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001895 }
1896 }
1897
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001898 if (rxq->rx_desc_count) {
Joe Perches7542db82011-03-02 17:50:35 +00001899 netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001900 rxq->rx_desc_count);
1901 }
1902
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001903 if (rxq->index == 0 &&
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001904 rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001905 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001906 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001907 dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001908 rxq->rx_desc_area, rxq->rx_desc_dma);
1909
1910 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001911}
1912
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001913static int txq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001914{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001915 struct tx_queue *txq = mp->txq + index;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001916 struct tx_desc *tx_desc;
1917 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001918 int i;
1919
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001920 txq->index = index;
1921
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001922 txq->tx_ring_size = mp->tx_ring_size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001923
1924 txq->tx_desc_count = 0;
1925 txq->tx_curr_desc = 0;
1926 txq->tx_used_desc = 0;
1927
1928 size = txq->tx_ring_size * sizeof(struct tx_desc);
1929
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001930 if (index == 0 && size <= mp->tx_desc_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001931 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1932 mp->tx_desc_sram_size);
1933 txq->tx_desc_dma = mp->tx_desc_sram_addr;
1934 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001935 txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1936 size, &txq->tx_desc_dma,
1937 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001938 }
1939
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001940 if (txq->tx_desc_area == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00001941 netdev_err(mp->dev,
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001942 "can't allocate tx ring (%d bytes)\n", size);
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +02001943 return -ENOMEM;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001944 }
1945 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001946
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001947 txq->tx_desc_area_size = size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001948
Joe Perches64699332012-06-04 12:44:16 +00001949 tx_desc = txq->tx_desc_area;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001950 for (i = 0; i < txq->tx_ring_size; i++) {
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001951 struct tx_desc *txd = tx_desc + i;
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001952 int nexti;
1953
1954 nexti = i + 1;
1955 if (nexti == txq->tx_ring_size)
1956 nexti = 0;
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001957
1958 txd->cmd_sts = 0;
1959 txd->next_desc_ptr = txq->tx_desc_dma +
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001960 nexti * sizeof(struct tx_desc);
1961 }
1962
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +02001963 skb_queue_head_init(&txq->tx_skb);
1964
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001965 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001966}
1967
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001968static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001969{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001970 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001971
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001972 txq_disable(txq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001973 txq_reclaim(txq, txq->tx_ring_size, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001974
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001975 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001976
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001977 if (txq->index == 0 &&
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001978 txq->tx_desc_area_size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001979 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001980 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001981 dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001982 txq->tx_desc_area, txq->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001983}
1984
1985
1986/* netdev ops and related ***************************************************/
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001987static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
1988{
1989 u32 int_cause;
1990 u32 int_cause_ext;
1991
Saeed Bisharae0ca8412009-05-06 03:02:01 +00001992 int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001993 if (int_cause == 0)
1994 return 0;
1995
1996 int_cause_ext = 0;
Saeed Bisharae0ca8412009-05-06 03:02:01 +00001997 if (int_cause & INT_EXT) {
1998 int_cause &= ~INT_EXT;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001999 int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002000 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002001
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002002 if (int_cause) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002003 wrlp(mp, INT_CAUSE, ~int_cause);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002004 mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002005 ~(rdlp(mp, TXQ_COMMAND) & 0xff);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002006 mp->work_rx |= (int_cause & INT_RX) >> 2;
2007 }
2008
2009 int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
2010 if (int_cause_ext) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002011 wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002012 if (int_cause_ext & INT_EXT_LINK_PHY)
2013 mp->work_link = 1;
2014 mp->work_tx |= int_cause_ext & INT_EXT_TX;
2015 }
2016
2017 return 1;
2018}
2019
2020static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
2021{
2022 struct net_device *dev = (struct net_device *)dev_id;
2023 struct mv643xx_eth_private *mp = netdev_priv(dev);
2024
2025 if (unlikely(!mv643xx_eth_collect_events(mp)))
2026 return IRQ_NONE;
2027
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002028 wrlp(mp, INT_MASK, 0);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002029 napi_schedule(&mp->napi);
2030
2031 return IRQ_HANDLED;
2032}
2033
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002034static void handle_link_event(struct mv643xx_eth_private *mp)
2035{
2036 struct net_device *dev = mp->dev;
2037 u32 port_status;
2038 int speed;
2039 int duplex;
2040 int fc;
2041
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002042 port_status = rdlp(mp, PORT_STATUS);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002043 if (!(port_status & LINK_UP)) {
2044 if (netif_carrier_ok(dev)) {
2045 int i;
2046
Joe Perches7542db82011-03-02 17:50:35 +00002047 netdev_info(dev, "link down\n");
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002048
2049 netif_carrier_off(dev);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002050
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002051 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002052 struct tx_queue *txq = mp->txq + i;
2053
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002054 txq_reclaim(txq, txq->tx_ring_size, 1);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002055 txq_reset_hw_ptr(txq);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002056 }
2057 }
2058 return;
2059 }
2060
2061 switch (port_status & PORT_SPEED_MASK) {
2062 case PORT_SPEED_10:
2063 speed = 10;
2064 break;
2065 case PORT_SPEED_100:
2066 speed = 100;
2067 break;
2068 case PORT_SPEED_1000:
2069 speed = 1000;
2070 break;
2071 default:
2072 speed = -1;
2073 break;
2074 }
2075 duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
2076 fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
2077
Joe Perches7542db82011-03-02 17:50:35 +00002078 netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
2079 speed, duplex ? "full" : "half", fc ? "en" : "dis");
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002080
Lennert Buytenhek4fdeca32008-08-28 05:39:13 +02002081 if (!netif_carrier_ok(dev))
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002082 netif_carrier_on(dev);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002083}
2084
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002085static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002086{
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002087 struct mv643xx_eth_private *mp;
2088 int work_done;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002089
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002090 mp = container_of(napi, struct mv643xx_eth_private, napi);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002091
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002092 if (unlikely(mp->oom)) {
2093 mp->oom = 0;
2094 del_timer(&mp->rx_oom);
2095 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002096
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002097 work_done = 0;
2098 while (work_done < budget) {
2099 u8 queue_mask;
2100 int queue;
2101 int work_tbd;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002102
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002103 if (mp->work_link) {
2104 mp->work_link = 0;
2105 handle_link_event(mp);
Lennert Buytenhek26ef1f12009-05-06 03:01:10 +00002106 work_done++;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002107 continue;
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02002108 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002109
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002110 queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2111 if (likely(!mp->oom))
2112 queue_mask |= mp->work_rx_refill;
2113
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002114 if (!queue_mask) {
2115 if (mv643xx_eth_collect_events(mp))
2116 continue;
2117 break;
2118 }
2119
2120 queue = fls(queue_mask) - 1;
2121 queue_mask = 1 << queue;
2122
2123 work_tbd = budget - work_done;
2124 if (work_tbd > 16)
2125 work_tbd = 16;
2126
2127 if (mp->work_tx_end & queue_mask) {
2128 txq_kick(mp->txq + queue);
2129 } else if (mp->work_tx & queue_mask) {
2130 work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2131 txq_maybe_wake(mp->txq + queue);
2132 } else if (mp->work_rx & queue_mask) {
2133 work_done += rxq_process(mp->rxq + queue, work_tbd);
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002134 } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002135 work_done += rxq_refill(mp->rxq + queue, work_tbd);
2136 } else {
2137 BUG();
2138 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002139 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002140
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002141 if (work_done < budget) {
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002142 if (mp->oom)
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002143 mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
2144 napi_complete(napi);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002145 wrlp(mp, INT_MASK, mp->int_mask);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002146 }
2147
2148 return work_done;
2149}
2150
2151static inline void oom_timer_wrapper(unsigned long data)
2152{
2153 struct mv643xx_eth_private *mp = (void *)data;
2154
2155 napi_schedule(&mp->napi);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002156}
2157
Lennert Buytenheke5371492008-06-01 02:18:13 +02002158static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002159{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002160 int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002161
Lennert Buytenheked944932008-08-26 13:34:19 +02002162 data = phy_read(mp->phy, MII_BMCR);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002163 if (data < 0)
2164 return;
2165
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002166 data |= BMCR_RESET;
Lennert Buytenheked944932008-08-26 13:34:19 +02002167 if (phy_write(mp->phy, MII_BMCR, data) < 0)
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002168 return;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002169
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002170 do {
Lennert Buytenheked944932008-08-26 13:34:19 +02002171 data = phy_read(mp->phy, MII_BMCR);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002172 } while (data >= 0 && data & BMCR_RESET);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002173}
2174
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002175static void port_start(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002176{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002177 u32 pscr;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002178 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002179
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002180 /*
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002181 * Perform PHY reset, if there is a PHY.
2182 */
Lennert Buytenheked944932008-08-26 13:34:19 +02002183 if (mp->phy != NULL) {
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002184 struct ethtool_cmd cmd;
2185
2186 mv643xx_eth_get_settings(mp->dev, &cmd);
2187 phy_reset(mp);
2188 mv643xx_eth_set_settings(mp->dev, &cmd);
2189 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002190
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002191 /*
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002192 * Configure basic link parameters.
2193 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002194 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002195
2196 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002197 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002198
2199 pscr |= DO_NOT_FORCE_LINK_FAIL;
Lennert Buytenheked944932008-08-26 13:34:19 +02002200 if (mp->phy == NULL)
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002201 pscr |= FORCE_LINK_PASS;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002202 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002203
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002204 /*
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002205 * Configure TX path and queues.
2206 */
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002207 tx_set_rate(mp, 1000000000, 16777216);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002208 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002209 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002210
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02002211 txq_reset_hw_ptr(txq);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002212 txq_set_rate(txq, 1000000000, 16777216);
2213 txq_set_fixed_prio_mode(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002214 }
2215
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002216 /*
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002217 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
Lennert Buytenhek170e7102008-09-19 02:58:50 +02002218 * frames to RX queue #0, and include the pseudo-header when
2219 * calculating receive checksums.
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002220 */
David S. Millere138f962011-04-21 15:19:02 -07002221 mv643xx_eth_set_features(mp->dev, mp->dev->features);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002222
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002223 /*
2224 * Treat BPDUs as normal multicasts, and disable partition mode.
2225 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002226 wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002227
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002228 /*
Lennert Buytenhek5a893922009-03-13 15:48:02 -07002229 * Add configured unicast addresses to address filter table.
2230 */
2231 mv643xx_eth_program_unicast_filter(mp->dev);
2232
2233 /*
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002234 * Enable the receive queues.
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002235 */
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002236 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002237 struct rx_queue *rxq = mp->rxq + i;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002238 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002239
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002240 addr = (u32)rxq->rx_desc_dma;
2241 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002242 wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002243
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002244 rxq_enable(rxq);
2245 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002246}
2247
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002248static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2249{
2250 int skb_size;
2251
2252 /*
2253 * Reserve 2+14 bytes for an ethernet header (the hardware
2254 * automatically prepends 2 bytes of dummy data to each
2255 * received packet), 16 bytes for up to four VLAN tags, and
2256 * 4 bytes for the trailing FCS -- 36 bytes total.
2257 */
2258 skb_size = mp->dev->mtu + 36;
2259
2260 /*
2261 * Make sure that the skb size is a multiple of 8 bytes, as
2262 * the lower three bits of the receive descriptor's buffer
2263 * size field are ignored by the hardware.
2264 */
2265 mp->skb_size = (skb_size + 7) & ~7;
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +00002266
2267 /*
2268 * If NET_SKB_PAD is smaller than a cache line,
2269 * netdev_alloc_skb() will cause skb->data to be misaligned
2270 * to a cache line boundary. If this is the case, include
2271 * some extra space to allow re-aligning the data area.
2272 */
2273 mp->skb_size += SKB_DMA_REALIGN;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002274}
2275
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002276static int mv643xx_eth_open(struct net_device *dev)
2277{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002278 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002279 int err;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002280 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002281
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002282 wrlp(mp, INT_CAUSE, 0);
2283 wrlp(mp, INT_CAUSE_EXT, 0);
2284 rdlp(mp, INT_CAUSE_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002285
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002286 err = request_irq(dev->irq, mv643xx_eth_irq,
Lennert Buytenhek2a1867a2008-08-23 23:43:38 +02002287 IRQF_SHARED, dev->name, dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002288 if (err) {
Joe Perches7542db82011-03-02 17:50:35 +00002289 netdev_err(dev, "can't assign irq\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002290 return -EAGAIN;
2291 }
2292
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002293 mv643xx_eth_recalc_skb_size(mp);
2294
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002295 napi_enable(&mp->napi);
2296
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002297 mp->int_mask = INT_EXT;
2298
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002299 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002300 err = rxq_init(mp, i);
2301 if (err) {
2302 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002303 rxq_deinit(mp->rxq + i);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002304 goto out;
2305 }
2306
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002307 rxq_refill(mp->rxq + i, INT_MAX);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002308 mp->int_mask |= INT_RX_0 << i;
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002309 }
2310
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002311 if (mp->oom) {
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002312 mp->rx_oom.expires = jiffies + (HZ / 10);
2313 add_timer(&mp->rx_oom);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002314 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002315
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002316 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002317 err = txq_init(mp, i);
2318 if (err) {
2319 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002320 txq_deinit(mp->txq + i);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002321 goto out_free;
2322 }
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002323 mp->int_mask |= INT_TX_END_0 << i;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002324 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002325
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002326 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002327
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002328 wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002329 wrlp(mp, INT_MASK, mp->int_mask);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002330
2331 return 0;
2332
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002333
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002334out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002335 for (i = 0; i < mp->rxq_count; i++)
2336 rxq_deinit(mp->rxq + i);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002337out:
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002338 free_irq(dev->irq, dev);
2339
2340 return err;
2341}
2342
Lennert Buytenheke5371492008-06-01 02:18:13 +02002343static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002344{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002345 unsigned int data;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002346 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002347
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002348 for (i = 0; i < mp->rxq_count; i++)
2349 rxq_disable(mp->rxq + i);
2350 for (i = 0; i < mp->txq_count; i++)
2351 txq_disable(mp->txq + i);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002352
2353 while (1) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002354 u32 ps = rdlp(mp, PORT_STATUS);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002355
2356 if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2357 break;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002358 udelay(10);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002359 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002360
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002361 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002362 data = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002363 data &= ~(SERIAL_PORT_ENABLE |
2364 DO_NOT_FORCE_LINK_FAIL |
2365 FORCE_LINK_PASS);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002366 wrlp(mp, PORT_SERIAL_CONTROL, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002367}
2368
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002369static int mv643xx_eth_stop(struct net_device *dev)
2370{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002371 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002372 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002373
Gabriel Paubertfe65e702009-01-19 17:18:09 -08002374 wrlp(mp, INT_MASK_EXT, 0x00000000);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002375 wrlp(mp, INT_MASK, 0x00000000);
2376 rdlp(mp, INT_MASK);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002377
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002378 napi_disable(&mp->napi);
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002379
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002380 del_timer_sync(&mp->rx_oom);
2381
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002382 netif_carrier_off(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002383
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002384 free_irq(dev->irq, dev);
2385
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002386 port_reset(mp);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02002387 mv643xx_eth_get_stats(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002388 mib_counters_update(mp);
Sebastian Siewior57e8f262009-02-16 11:28:15 +00002389 del_timer_sync(&mp->mib_counters_timer);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002390
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002391 for (i = 0; i < mp->rxq_count; i++)
2392 rxq_deinit(mp->rxq + i);
2393 for (i = 0; i < mp->txq_count; i++)
2394 txq_deinit(mp->txq + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002395
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002396 return 0;
2397}
2398
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002399static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002400{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002401 struct mv643xx_eth_private *mp = netdev_priv(dev);
Phil Sutter260055b2013-03-06 07:49:02 +00002402 int ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002403
Phil Sutter260055b2013-03-06 07:49:02 +00002404 if (mp->phy == NULL)
2405 return -ENOTSUPP;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002406
Phil Sutter260055b2013-03-06 07:49:02 +00002407 ret = phy_mii_ioctl(mp->phy, ifr, cmd);
2408 if (!ret)
2409 mv643xx_adjust_pscr(mp);
2410 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002411}
2412
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002413static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2414{
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002415 struct mv643xx_eth_private *mp = netdev_priv(dev);
2416
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002417 if (new_mtu < 64 || new_mtu > 9500)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002418 return -EINVAL;
2419
2420 dev->mtu = new_mtu;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002421 mv643xx_eth_recalc_skb_size(mp);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002422 tx_set_rate(mp, 1000000000, 16777216);
2423
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002424 if (!netif_running(dev))
2425 return 0;
2426
2427 /*
2428 * Stop and then re-open the interface. This will allocate RX
2429 * skbs of the new MTU.
2430 * There is a possible danger that the open will not succeed,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002431 * due to memory being full.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002432 */
2433 mv643xx_eth_stop(dev);
2434 if (mv643xx_eth_open(dev)) {
Joe Perches7542db82011-03-02 17:50:35 +00002435 netdev_err(dev,
2436 "fatal error on re-opening device after MTU change\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002437 }
2438
2439 return 0;
2440}
2441
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002442static void tx_timeout_task(struct work_struct *ugly)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002443{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002444 struct mv643xx_eth_private *mp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002445
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002446 mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2447 if (netif_running(mp->dev)) {
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002448 netif_tx_stop_all_queues(mp->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002449 port_reset(mp);
2450 port_start(mp);
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002451 netif_tx_wake_all_queues(mp->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002452 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002453}
2454
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002455static void mv643xx_eth_tx_timeout(struct net_device *dev)
2456{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002457 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002458
Joe Perches7542db82011-03-02 17:50:35 +00002459 netdev_info(dev, "tx timeout\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002460
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002461 schedule_work(&mp->tx_timeout_task);
2462}
2463
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002464#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002465static void mv643xx_eth_netpoll(struct net_device *dev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002466{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002467 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002468
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002469 wrlp(mp, INT_MASK, 0x00000000);
2470 rdlp(mp, INT_MASK);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002471
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002472 mv643xx_eth_irq(dev->irq, dev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002473
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002474 wrlp(mp, INT_MASK, mp->int_mask);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002475}
2476#endif
2477
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002478
2479/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02002480static void
2481mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
Andrew Lunn63a93322011-12-07 21:48:07 +01002482 const struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002483{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002484 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002485 u32 win_enable;
2486 u32 win_protect;
2487 int i;
2488
2489 for (i = 0; i < 6; i++) {
2490 writel(0, base + WINDOW_BASE(i));
2491 writel(0, base + WINDOW_SIZE(i));
2492 if (i < 4)
2493 writel(0, base + WINDOW_REMAP_HIGH(i));
2494 }
2495
2496 win_enable = 0x3f;
2497 win_protect = 0;
2498
2499 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01002500 const struct mbus_dram_window *cs = dram->cs + i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002501
2502 writel((cs->base & 0xffff0000) |
2503 (cs->mbus_attr << 8) |
2504 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2505 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2506
2507 win_enable &= ~(1 << i);
2508 win_protect |= 3 << (2 * i);
2509 }
2510
2511 writel(win_enable, base + WINDOW_BAR_ENABLE);
2512 msp->win_protect = win_protect;
2513}
2514
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002515static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2516{
2517 /*
2518 * Check whether we have a 14-bit coal limit field in bits
2519 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2520 * SDMA config register.
2521 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002522 writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2523 if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002524 msp->extended_rx_coal_limit = 1;
2525 else
2526 msp->extended_rx_coal_limit = 0;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002527
2528 /*
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002529 * Check whether the MAC supports TX rate control, and if
2530 * yes, whether its associated registers are in the old or
2531 * the new place.
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002532 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002533 writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2534 if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002535 msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2536 } else {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002537 writel(7, msp->base + 0x0400 + TX_BW_RATE);
2538 if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002539 msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2540 else
2541 msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2542 }
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002543}
2544
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002545static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2546{
Lennert Buytenhek10a99482008-11-20 03:57:16 -08002547 static int mv643xx_eth_version_printed;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002548 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002549 struct mv643xx_eth_shared_private *msp;
Andrew Lunn63a93322011-12-07 21:48:07 +01002550 const struct mbus_dram_target_info *dram;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002551 struct resource *res;
2552 int ret;
2553
Lennert Buytenheke5371492008-06-01 02:18:13 +02002554 if (!mv643xx_eth_version_printed++)
Joe Perches7542db82011-03-02 17:50:35 +00002555 pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2556 mv643xx_eth_driver_version);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002557
2558 ret = -EINVAL;
2559 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2560 if (res == NULL)
2561 goto out;
2562
2563 ret = -ENOMEM;
Julia Lawallbeae22e2010-05-13 10:00:22 +00002564 msp = kzalloc(sizeof(*msp), GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002565 if (msp == NULL)
2566 goto out;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002567
Joe Perches28f65c112011-06-09 09:13:32 -07002568 msp->base = ioremap(res->start, resource_size(res));
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002569 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002570 goto out_free;
2571
Lennert Buytenheked944932008-08-26 13:34:19 +02002572 /*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002573 * (Re-)program MBUS remapping windows if we are asked to.
2574 */
Andrew Lunn63a93322011-12-07 21:48:07 +01002575 dram = mv_mbus_dram_info();
2576 if (dram)
2577 mv643xx_eth_conf_mbus_windows(msp, dram);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002578
Dan Carpenter50a749c2010-07-23 01:05:05 +00002579 msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2580 pd->tx_csum_limit : 9 * 1024;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002581 infer_hw_params(msp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002582
2583 platform_set_drvdata(pdev, msp);
2584
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002585 return 0;
2586
2587out_free:
2588 kfree(msp);
2589out:
2590 return ret;
2591}
2592
2593static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2594{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002595 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002596
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002597 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002598 kfree(msp);
2599
2600 return 0;
2601}
2602
2603static struct platform_driver mv643xx_eth_shared_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002604 .probe = mv643xx_eth_shared_probe,
2605 .remove = mv643xx_eth_shared_remove,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002606 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002607 .name = MV643XX_ETH_SHARED_NAME,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002608 .owner = THIS_MODULE,
2609 },
2610};
2611
Lennert Buytenheke5371492008-06-01 02:18:13 +02002612static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002613{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002614 int addr_shift = 5 * mp->port_num;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002615 u32 data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002616
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002617 data = rdl(mp, PHY_ADDR);
2618 data &= ~(0x1f << addr_shift);
2619 data |= (phy_addr & 0x1f) << addr_shift;
2620 wrl(mp, PHY_ADDR, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002621}
2622
Lennert Buytenheke5371492008-06-01 02:18:13 +02002623static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002624{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002625 unsigned int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002626
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002627 data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002628
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002629 return (data >> (5 * mp->port_num)) & 0x1f;
2630}
2631
2632static void set_params(struct mv643xx_eth_private *mp,
2633 struct mv643xx_eth_platform_data *pd)
2634{
2635 struct net_device *dev = mp->dev;
2636
2637 if (is_valid_ether_addr(pd->mac_addr))
2638 memcpy(dev->dev_addr, pd->mac_addr, 6);
2639 else
2640 uc_addr_get(mp, dev->dev_addr);
2641
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002642 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002643 if (pd->rx_queue_size)
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002644 mp->rx_ring_size = pd->rx_queue_size;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002645 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2646 mp->rx_desc_sram_size = pd->rx_sram_size;
2647
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002648 mp->rxq_count = pd->rx_queue_count ? : 1;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002649
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002650 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002651 if (pd->tx_queue_size)
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002652 mp->tx_ring_size = pd->tx_queue_size;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002653 mp->tx_desc_sram_addr = pd->tx_sram_addr;
2654 mp->tx_desc_sram_size = pd->tx_sram_size;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002655
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002656 mp->txq_count = pd->tx_queue_count ? : 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002657}
2658
Florian Fainellic3a07132013-03-22 03:39:28 +00002659static void mv643xx_eth_adjust_link(struct net_device *dev)
2660{
2661 struct mv643xx_eth_private *mp = netdev_priv(dev);
2662
2663 mv643xx_adjust_pscr(mp);
2664}
2665
Lennert Buytenheked944932008-08-26 13:34:19 +02002666static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2667 int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002668{
Lennert Buytenheked944932008-08-26 13:34:19 +02002669 struct phy_device *phydev;
2670 int start;
2671 int num;
2672 int i;
Florian Fainellic3a07132013-03-22 03:39:28 +00002673 char phy_id[MII_BUS_ID_SIZE + 3];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002674
Lennert Buytenheked944932008-08-26 13:34:19 +02002675 if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2676 start = phy_addr_get(mp) & 0x1f;
2677 num = 32;
2678 } else {
2679 start = phy_addr & 0x1f;
2680 num = 1;
2681 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002682
Florian Fainellic3a07132013-03-22 03:39:28 +00002683 /* Attempt to connect to the PHY using orion-mdio */
Simon Baatz976c90b2013-03-24 10:34:00 +00002684 phydev = ERR_PTR(-ENODEV);
Lennert Buytenheked944932008-08-26 13:34:19 +02002685 for (i = 0; i < num; i++) {
2686 int addr = (start + i) & 0x1f;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002687
Florian Fainellic3a07132013-03-22 03:39:28 +00002688 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
2689 "orion-mdio-mii", addr);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002690
Florian Fainellic3a07132013-03-22 03:39:28 +00002691 phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
2692 PHY_INTERFACE_MODE_GMII);
2693 if (!IS_ERR(phydev)) {
2694 phy_addr_set(mp, addr);
2695 break;
Lennert Buytenheked944932008-08-26 13:34:19 +02002696 }
2697 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002698
Lennert Buytenheked944932008-08-26 13:34:19 +02002699 return phydev;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002700}
2701
Lennert Buytenheked944932008-08-26 13:34:19 +02002702static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
James Chapmand0412d92006-01-27 01:15:30 -07002703{
Lennert Buytenheked944932008-08-26 13:34:19 +02002704 struct phy_device *phy = mp->phy;
James Chapmand0412d92006-01-27 01:15:30 -07002705
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002706 phy_reset(mp);
2707
Lennert Buytenheked944932008-08-26 13:34:19 +02002708 if (speed == 0) {
2709 phy->autoneg = AUTONEG_ENABLE;
2710 phy->speed = 0;
2711 phy->duplex = 0;
2712 phy->advertising = phy->supported | ADVERTISED_Autoneg;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002713 } else {
Lennert Buytenheked944932008-08-26 13:34:19 +02002714 phy->autoneg = AUTONEG_DISABLE;
2715 phy->advertising = 0;
2716 phy->speed = speed;
2717 phy->duplex = duplex;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002718 }
Lennert Buytenheked944932008-08-26 13:34:19 +02002719 phy_start_aneg(phy);
James Chapmand0412d92006-01-27 01:15:30 -07002720}
2721
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002722static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2723{
2724 u32 pscr;
2725
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002726 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002727 if (pscr & SERIAL_PORT_ENABLE) {
2728 pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002729 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002730 }
2731
2732 pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
Lennert Buytenheked944932008-08-26 13:34:19 +02002733 if (mp->phy == NULL) {
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002734 pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2735 if (speed == SPEED_1000)
2736 pscr |= SET_GMII_SPEED_TO_1000;
2737 else if (speed == SPEED_100)
2738 pscr |= SET_MII_SPEED_TO_100;
2739
2740 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2741
2742 pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2743 if (duplex == DUPLEX_FULL)
2744 pscr |= SET_FULL_DUPLEX_MODE;
2745 }
2746
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002747 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002748}
2749
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002750static const struct net_device_ops mv643xx_eth_netdev_ops = {
2751 .ndo_open = mv643xx_eth_open,
2752 .ndo_stop = mv643xx_eth_stop,
2753 .ndo_start_xmit = mv643xx_eth_xmit,
2754 .ndo_set_rx_mode = mv643xx_eth_set_rx_mode,
2755 .ndo_set_mac_address = mv643xx_eth_set_mac_address,
kirjanov@gmail.com1d4bd942010-02-16 20:40:11 +00002756 .ndo_validate_addr = eth_validate_addr,
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002757 .ndo_do_ioctl = mv643xx_eth_ioctl,
2758 .ndo_change_mtu = mv643xx_eth_change_mtu,
Michał Mirosławaad59c42011-04-17 00:15:46 +00002759 .ndo_set_features = mv643xx_eth_set_features,
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002760 .ndo_tx_timeout = mv643xx_eth_tx_timeout,
2761 .ndo_get_stats = mv643xx_eth_get_stats,
2762#ifdef CONFIG_NET_POLL_CONTROLLER
2763 .ndo_poll_controller = mv643xx_eth_netpoll,
2764#endif
2765};
2766
Russell King3ae5eae2005-11-09 22:32:44 +00002767static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 struct mv643xx_eth_platform_data *pd;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002770 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 struct resource *res;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002773 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002775 pd = pdev->dev.platform_data;
2776 if (pd == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002777 dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002778 return -ENODEV;
2779 }
2780
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002781 if (pd->shared == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002782 dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002783 return -ENODEV;
2784 }
2785
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002786 dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 if (!dev)
2788 return -ENOMEM;
2789
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002791 platform_set_drvdata(pdev, mp);
2792
2793 mp->shared = platform_get_drvdata(pd->shared);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002794 mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002795 mp->port_num = pd->port_number;
2796
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002797 mp->dev = dev;
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002798
Andrew Lunn452503e2011-12-24 01:24:24 +01002799 /*
Andrew Lunn9a43a022012-06-06 06:40:43 +00002800 * Start with a default rate, and if there is a clock, allow
2801 * it to override the default.
Andrew Lunn452503e2011-12-24 01:24:24 +01002802 */
Andrew Lunn9a43a022012-06-06 06:40:43 +00002803 mp->t_clk = 133000000;
2804#if defined(CONFIG_HAVE_CLK)
Andrew Lunn452503e2011-12-24 01:24:24 +01002805 mp->clk = clk_get(&pdev->dev, (pdev->id ? "1" : "0"));
2806 if (!IS_ERR(mp->clk)) {
2807 clk_prepare_enable(mp->clk);
2808 mp->t_clk = clk_get_rate(mp->clk);
Andrew Lunn452503e2011-12-24 01:24:24 +01002809 }
Andrew Lunn9a43a022012-06-06 06:40:43 +00002810#endif
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002811 set_params(mp, pd);
Ben Hutchings206d6b32010-09-27 08:30:05 +00002812 netif_set_real_num_tx_queues(dev, mp->txq_count);
2813 netif_set_real_num_rx_queues(dev, mp->rxq_count);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002814
Simon Baatz976c90b2013-03-24 10:34:00 +00002815 if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
Lennert Buytenheked944932008-08-26 13:34:19 +02002816 mp->phy = phy_scan(mp, pd->phy_addr);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002817
Simon Baatz976c90b2013-03-24 10:34:00 +00002818 if (IS_ERR(mp->phy)) {
2819 err = PTR_ERR(mp->phy);
2820 if (err == -ENODEV)
2821 err = -EPROBE_DEFER;
2822 goto out;
2823 }
Lennert Buytenheked944932008-08-26 13:34:19 +02002824 phy_init(mp, pd->speed, pd->duplex);
Simon Baatz976c90b2013-03-24 10:34:00 +00002825 }
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00002826
2827 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Lennert Buytenheked944932008-08-26 13:34:19 +02002828
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002829 init_pscr(mp, pd->speed, pd->duplex);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002830
Lennert Buytenhek4ff34952008-09-19 05:04:57 +02002831
2832 mib_counters_clear(mp);
2833
2834 init_timer(&mp->mib_counters_timer);
2835 mp->mib_counters_timer.data = (unsigned long)mp;
2836 mp->mib_counters_timer.function = mib_counters_timer_wrapper;
2837 mp->mib_counters_timer.expires = jiffies + 30 * HZ;
2838 add_timer(&mp->mib_counters_timer);
2839
2840 spin_lock_init(&mp->mib_counters_lock);
2841
2842 INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
2843
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002844 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
2845
2846 init_timer(&mp->rx_oom);
2847 mp->rx_oom.data = (unsigned long)mp;
2848 mp->rx_oom.function = oom_timer_wrapper;
2849
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2852 BUG_ON(!res);
2853 dev->irq = res->start;
2854
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002855 dev->netdev_ops = &mv643xx_eth_netdev_ops;
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 dev->base_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Michał Mirosławaad59c42011-04-17 00:15:46 +00002860 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
2861 NETIF_F_RXCSUM | NETIF_F_LRO;
2862 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Lennert Buytenheke32b6612008-07-24 06:22:59 +02002863 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
James Chapmanc28a4f82006-01-27 01:13:15 -07002864
Jiri Pirko01789342011-08-16 06:29:00 +00002865 dev->priv_flags |= IFF_UNICAST_FLT;
2866
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002867 SET_NETDEV_DEV(dev, &pdev->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002868
2869 if (mp->shared->win_protect)
2870 wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
2871
Lennert Buytenheka5fe3612009-02-24 15:41:47 +00002872 netif_carrier_off(dev);
2873
Lennert Buytenhekb5e86db2009-04-08 15:42:46 -07002874 wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
2875
Lennert Buytenhek4fb0a542009-02-24 15:42:05 +00002876 set_rx_coal(mp, 250);
Lennert Buytenheka5fe3612009-02-24 15:41:47 +00002877 set_tx_coal(mp, 0);
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 err = register_netdev(dev);
2880 if (err)
2881 goto out;
2882
Joe Perches7542db82011-03-02 17:50:35 +00002883 netdev_notice(dev, "port %d with MAC address %pM\n",
2884 mp->port_num, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002886 if (mp->tx_desc_sram_size > 0)
Joe Perches7542db82011-03-02 17:50:35 +00002887 netdev_notice(dev, "configured with sram\n");
Nicolas DETb1529872005-10-28 17:46:30 -07002888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 return 0;
2890
2891out:
Simon Baatzbaffab22012-07-19 00:04:09 +02002892#if defined(CONFIG_HAVE_CLK)
2893 if (!IS_ERR(mp->clk)) {
2894 clk_disable_unprepare(mp->clk);
2895 clk_put(mp->clk);
2896 }
2897#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 free_netdev(dev);
2899
2900 return err;
2901}
2902
Russell King3ae5eae2005-11-09 22:32:44 +00002903static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002905 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002907 unregister_netdev(mp->dev);
Lennert Buytenheked944932008-08-26 13:34:19 +02002908 if (mp->phy != NULL)
2909 phy_detach(mp->phy);
Tejun Heo23f333a2010-12-12 16:45:14 +01002910 cancel_work_sync(&mp->tx_timeout_task);
Andrew Lunn452503e2011-12-24 01:24:24 +01002911
Andrew Lunn9a43a022012-06-06 06:40:43 +00002912#if defined(CONFIG_HAVE_CLK)
Andrew Lunn452503e2011-12-24 01:24:24 +01002913 if (!IS_ERR(mp->clk)) {
2914 clk_disable_unprepare(mp->clk);
2915 clk_put(mp->clk);
2916 }
Andrew Lunn9a43a022012-06-06 06:40:43 +00002917#endif
2918
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002919 free_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
Russell King3ae5eae2005-11-09 22:32:44 +00002921 platform_set_drvdata(pdev, NULL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 return 0;
2924}
2925
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002926static void mv643xx_eth_shutdown(struct platform_device *pdev)
2927{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002928 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002929
2930 /* Mask all interrupts on ethernet port */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002931 wrlp(mp, INT_MASK, 0);
2932 rdlp(mp, INT_MASK);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002933
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002934 if (netif_running(mp->dev))
2935 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002936}
2937
Russell King3ae5eae2005-11-09 22:32:44 +00002938static struct platform_driver mv643xx_eth_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002939 .probe = mv643xx_eth_probe,
2940 .remove = mv643xx_eth_remove,
2941 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002942 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002943 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002944 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002945 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946};
2947
Lennert Buytenheke5371492008-06-01 02:18:13 +02002948static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949{
2950 int rc;
2951
Russell King3ae5eae2005-11-09 22:32:44 +00002952 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002954 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002956 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return rc;
2960}
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002961module_init(mv643xx_eth_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Lennert Buytenheke5371492008-06-01 02:18:13 +02002963static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Russell King3ae5eae2005-11-09 22:32:44 +00002965 platform_driver_unregister(&mv643xx_eth_driver);
2966 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
Lennert Buytenheke5371492008-06-01 02:18:13 +02002968module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969
Lennert Buytenhek45675bc2008-06-02 02:02:30 +02002970MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
2971 "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002973MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07002974MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002975MODULE_ALIAS("platform:" MV643XX_ETH_NAME);