blob: d1ecf4bf7da75c5acce2a070105e413265be2590 [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
72#define SMI_REG 0x0004
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +020073#define SMI_BUSY 0x10000000
74#define SMI_READ_VALID 0x08000000
75#define SMI_OPCODE_READ 0x04000000
76#define SMI_OPCODE_WRITE 0x00000000
77#define ERR_INT_CAUSE 0x0080
78#define ERR_INT_SMI_DONE 0x00000010
79#define ERR_INT_MASK 0x0084
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020080#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
81#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
82#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
83#define WINDOW_BAR_ENABLE 0x0290
84#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020085
86/*
Lennert Buytenhek37a60842008-11-20 03:57:36 -080087 * Main per-port registers. These live at offset 0x0400 for
88 * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020089 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -080090#define PORT_CONFIG 0x0000
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020091#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek37a60842008-11-20 03:57:36 -080092#define PORT_CONFIG_EXT 0x0004
93#define MAC_ADDR_LOW 0x0014
94#define MAC_ADDR_HIGH 0x0018
95#define SDMA_CONFIG 0x001c
Lennert Buytenhekbecfad92009-05-06 03:01:17 +000096#define TX_BURST_SIZE_16_64BIT 0x01000000
97#define TX_BURST_SIZE_4_64BIT 0x00800000
98#define BLM_TX_NO_SWAP 0x00000020
99#define BLM_RX_NO_SWAP 0x00000010
100#define RX_BURST_SIZE_16_64BIT 0x00000008
101#define RX_BURST_SIZE_4_64BIT 0x00000004
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800102#define PORT_SERIAL_CONTROL 0x003c
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000103#define SET_MII_SPEED_TO_100 0x01000000
104#define SET_GMII_SPEED_TO_1000 0x00800000
105#define SET_FULL_DUPLEX_MODE 0x00200000
106#define MAX_RX_PACKET_9700BYTE 0x000a0000
107#define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000
108#define DO_NOT_FORCE_LINK_FAIL 0x00000400
109#define SERIAL_PORT_CONTROL_RESERVED 0x00000200
110#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008
111#define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004
112#define FORCE_LINK_PASS 0x00000002
113#define SERIAL_PORT_ENABLE 0x00000001
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800114#define PORT_STATUS 0x0044
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200115#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhekae9ae062008-07-15 02:15:24 +0200116#define TX_IN_PROGRESS 0x00000080
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +0200117#define PORT_SPEED_MASK 0x00000030
118#define PORT_SPEED_1000 0x00000010
119#define PORT_SPEED_100 0x00000020
120#define PORT_SPEED_10 0x00000000
121#define FLOW_CONTROL_ENABLED 0x00000008
122#define FULL_DUPLEX 0x00000004
Lennert Buytenhek81600eea92008-07-14 14:29:40 +0200123#define LINK_UP 0x00000002
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800124#define TXQ_COMMAND 0x0048
125#define TXQ_FIX_PRIO_CONF 0x004c
126#define TX_BW_RATE 0x0050
127#define TX_BW_MTU 0x0058
128#define TX_BW_BURST 0x005c
129#define INT_CAUSE 0x0060
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200130#define INT_TX_END 0x07f80000
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000131#define INT_TX_END_0 0x00080000
Lennert Buytenhekbefefe22008-08-28 06:10:04 +0200132#define INT_RX 0x000003fc
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000133#define INT_RX_0 0x00000004
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200134#define INT_EXT 0x00000002
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800135#define INT_CAUSE_EXT 0x0064
Lennert Buytenhekbefefe22008-08-28 06:10:04 +0200136#define INT_EXT_LINK_PHY 0x00110000
137#define INT_EXT_TX 0x000000ff
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800138#define INT_MASK 0x0068
139#define INT_MASK_EXT 0x006c
140#define TX_FIFO_URGENT_THRESHOLD 0x0074
Paulius Zaleckas302476c2012-01-23 01:16:35 +0000141#define RX_DISCARD_FRAME_CNT 0x0084
142#define RX_OVERRUN_FRAME_CNT 0x0088
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800143#define TXQ_FIX_PRIO_CONF_MOVED 0x00dc
144#define TX_BW_RATE_MOVED 0x00e0
145#define TX_BW_MTU_MOVED 0x00e8
146#define TX_BW_BURST_MOVED 0x00ec
147#define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4))
148#define RXQ_COMMAND 0x0280
149#define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2))
150#define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4))
151#define TXQ_BW_CONF(q) (0x0304 + ((q) << 4))
152#define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4))
153
154/*
155 * Misc per-port registers.
156 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200157#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
158#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
159#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
160#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200161
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200162
163/*
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000164 * SDMA configuration register default value.
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200165 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200166#if defined(__BIG_ENDIAN)
167#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenheke0c6ef92009-01-19 17:16:00 -0800168 (RX_BURST_SIZE_4_64BIT | \
169 TX_BURST_SIZE_4_64BIT)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200170#elif defined(__LITTLE_ENDIAN)
171#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenheke0c6ef92009-01-19 17:16:00 -0800172 (RX_BURST_SIZE_4_64BIT | \
173 BLM_RX_NO_SWAP | \
174 BLM_TX_NO_SWAP | \
175 TX_BURST_SIZE_4_64BIT)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200176#else
177#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
178#endif
179
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200180
181/*
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000182 * Misc definitions.
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200183 */
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000184#define DEFAULT_RX_QUEUE_SIZE 128
185#define DEFAULT_TX_QUEUE_SIZE 256
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +0000186#define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200187
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200188
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200189/*
190 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200191 */
192#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200193struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200194 u16 byte_cnt; /* Descriptor buffer byte count */
195 u16 buf_size; /* Buffer size */
196 u32 cmd_sts; /* Descriptor command status */
197 u32 next_desc_ptr; /* Next descriptor pointer */
198 u32 buf_ptr; /* Descriptor buffer pointer */
199};
200
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200201struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200202 u16 byte_cnt; /* buffer byte count */
203 u16 l4i_chk; /* CPU provided TCP checksum */
204 u32 cmd_sts; /* Command/status field */
205 u32 next_desc_ptr; /* Pointer to next descriptor */
206 u32 buf_ptr; /* pointer to buffer for this descriptor*/
207};
208#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200209struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200210 u32 cmd_sts; /* Descriptor command status */
211 u16 buf_size; /* Buffer size */
212 u16 byte_cnt; /* Descriptor buffer byte count */
213 u32 buf_ptr; /* Descriptor buffer pointer */
214 u32 next_desc_ptr; /* Next descriptor pointer */
215};
216
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200217struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200218 u32 cmd_sts; /* Command/status field */
219 u16 l4i_chk; /* CPU provided TCP checksum */
220 u16 byte_cnt; /* buffer byte count */
221 u32 buf_ptr; /* pointer to buffer for this descriptor*/
222 u32 next_desc_ptr; /* Pointer to next descriptor */
223};
224#else
225#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
226#endif
227
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200228/* RX & TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200229#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200230
231/* RX & TX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200232#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200233
234/* RX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200235#define LAYER_4_CHECKSUM_OK 0x40000000
236#define RX_ENABLE_INTERRUPT 0x20000000
237#define RX_FIRST_DESC 0x08000000
238#define RX_LAST_DESC 0x04000000
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000239#define RX_IP_HDR_OK 0x02000000
240#define RX_PKT_IS_IPV4 0x01000000
241#define RX_PKT_IS_ETHERNETV2 0x00800000
242#define RX_PKT_LAYER4_TYPE_MASK 0x00600000
243#define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000
244#define RX_PKT_IS_VLAN_TAGGED 0x00080000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200245
246/* TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200247#define TX_ENABLE_INTERRUPT 0x00800000
248#define GEN_CRC 0x00400000
249#define TX_FIRST_DESC 0x00200000
250#define TX_LAST_DESC 0x00100000
251#define ZERO_PADDING 0x00080000
252#define GEN_IP_V4_CHECKSUM 0x00040000
253#define GEN_TCP_UDP_CHECKSUM 0x00020000
254#define UDP_FRAME 0x00010000
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200255#define MAC_HDR_EXTRA_4_BYTES 0x00008000
256#define MAC_HDR_EXTRA_8_BYTES 0x00000200
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200257
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200258#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200259
260
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200261/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200262struct mv643xx_eth_shared_private {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200263 /*
264 * Ethernet controller base address.
265 */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200266 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200267
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200268 /*
Lennert Buytenhekfc0eb9f2008-08-26 12:56:56 +0200269 * Points at the right SMI instance to use.
270 */
271 struct mv643xx_eth_shared_private *smi;
272
273 /*
Lennert Buytenheked944932008-08-26 13:34:19 +0200274 * Provides access to local SMI interface.
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200275 */
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -0700276 struct mii_bus *smi_bus;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200277
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200278 /*
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200279 * If we have access to the error interrupt pin (which is
280 * somewhat misnamed as it not only reflects internal errors
281 * but also reflects SMI completion), use that to wait for
282 * SMI access completion instead of polling the SMI busy bit.
283 */
284 int err_interrupt;
285 wait_queue_head_t smi_busy_wait;
286
287 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200288 * Per-port MBUS window access register value.
289 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200290 u32 win_protect;
291
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200292 /*
293 * Hardware-specific parameters.
294 */
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +0200295 int extended_rx_coal_limit;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200296 int tx_bw_control;
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000297 int tx_csum_limit;
Andrew Lunn452503e2011-12-24 01:24:24 +0100298
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200299};
300
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200301#define TX_BW_CONTROL_ABSENT 0
302#define TX_BW_CONTROL_OLD_LAYOUT 1
303#define TX_BW_CONTROL_NEW_LAYOUT 2
304
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000305static int mv643xx_eth_open(struct net_device *dev);
306static int mv643xx_eth_stop(struct net_device *dev);
307
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200308
309/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200310struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200311 u64 good_octets_received;
312 u32 bad_octets_received;
313 u32 internal_mac_transmit_err;
314 u32 good_frames_received;
315 u32 bad_frames_received;
316 u32 broadcast_frames_received;
317 u32 multicast_frames_received;
318 u32 frames_64_octets;
319 u32 frames_65_to_127_octets;
320 u32 frames_128_to_255_octets;
321 u32 frames_256_to_511_octets;
322 u32 frames_512_to_1023_octets;
323 u32 frames_1024_to_max_octets;
324 u64 good_octets_sent;
325 u32 good_frames_sent;
326 u32 excessive_collision;
327 u32 multicast_frames_sent;
328 u32 broadcast_frames_sent;
329 u32 unrec_mac_control_received;
330 u32 fc_sent;
331 u32 good_fc_received;
332 u32 bad_fc_received;
333 u32 undersize_received;
334 u32 fragments_received;
335 u32 oversize_received;
336 u32 jabber_received;
337 u32 mac_receive_error;
338 u32 bad_crc_event;
339 u32 collision;
340 u32 late_collision;
Paulius Zaleckas302476c2012-01-23 01:16:35 +0000341 /* Non MIB hardware counters */
342 u32 rx_discard;
343 u32 rx_overrun;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200344};
345
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000346struct lro_counters {
347 u32 lro_aggregated;
348 u32 lro_flushed;
349 u32 lro_no_desc;
350};
351
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200352struct rx_queue {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200353 int index;
354
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200355 int rx_ring_size;
356
357 int rx_desc_count;
358 int rx_curr_desc;
359 int rx_used_desc;
360
361 struct rx_desc *rx_desc_area;
362 dma_addr_t rx_desc_dma;
363 int rx_desc_area_size;
364 struct sk_buff **rx_skb;
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000365
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000366 struct net_lro_mgr lro_mgr;
367 struct net_lro_desc lro_arr[8];
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200368};
369
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200370struct tx_queue {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200371 int index;
372
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200373 int tx_ring_size;
374
375 int tx_desc_count;
376 int tx_curr_desc;
377 int tx_used_desc;
378
379 struct tx_desc *tx_desc_area;
380 dma_addr_t tx_desc_dma;
381 int tx_desc_area_size;
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200382
383 struct sk_buff_head tx_skb;
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200384
385 unsigned long tx_packets;
386 unsigned long tx_bytes;
387 unsigned long tx_dropped;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200388};
389
Lennert Buytenheke5371492008-06-01 02:18:13 +0200390struct mv643xx_eth_private {
391 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800392 void __iomem *base;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200393 int port_num;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200394
395 struct net_device *dev;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200396
Lennert Buytenheked944932008-08-26 13:34:19 +0200397 struct phy_device *phy;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200398
Lennert Buytenhek4ff34952008-09-19 05:04:57 +0200399 struct timer_list mib_counters_timer;
400 spinlock_t mib_counters_lock;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200401 struct mib_counters mib_counters;
Lennert Buytenhek4ff34952008-09-19 05:04:57 +0200402
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000403 struct lro_counters lro_counters;
404
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200405 struct work_struct tx_timeout_task;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200406
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200407 struct napi_struct napi;
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000408 u32 int_mask;
Lennert Buytenhek1319eba2009-04-29 11:57:34 +0000409 u8 oom;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200410 u8 work_link;
411 u8 work_tx;
412 u8 work_tx_end;
413 u8 work_rx;
414 u8 work_rx_refill;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200415
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700416 int skb_size;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700417
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200418 /*
419 * RX state.
420 */
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000421 int rx_ring_size;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200422 unsigned long rx_desc_sram_addr;
423 int rx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200424 int rxq_count;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200425 struct timer_list rx_oom;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200426 struct rx_queue rxq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200427
428 /*
429 * TX state.
430 */
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000431 int tx_ring_size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200432 unsigned long tx_desc_sram_addr;
433 int tx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200434 int txq_count;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200435 struct tx_queue txq[8];
Andrew Lunn452503e2011-12-24 01:24:24 +0100436
437 /*
438 * Hardware-specific parameters.
439 */
Andrew Lunn9a43a022012-06-06 06:40:43 +0000440#if defined(CONFIG_HAVE_CLK)
Andrew Lunn452503e2011-12-24 01:24:24 +0100441 struct clk *clk;
Andrew Lunn9a43a022012-06-06 06:40:43 +0000442#endif
Andrew Lunn452503e2011-12-24 01:24:24 +0100443 unsigned int t_clk;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200444};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200446
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200447/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200448static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200450 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800453static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
454{
455 return readl(mp->base + offset);
456}
457
Lennert Buytenheke5371492008-06-01 02:18:13 +0200458static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200460 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800463static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
464{
465 writel(data, mp->base + offset);
466}
467
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200468
469/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200470static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200472 return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200473}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200475static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
476{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200477 return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200478}
479
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200480static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200481{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200482 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800483 wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200484}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700485
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200486static void rxq_disable(struct rx_queue *rxq)
487{
488 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200489 u8 mask = 1 << rxq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200490
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800491 wrlp(mp, RXQ_COMMAND, mask << 8);
492 while (rdlp(mp, RXQ_COMMAND) & mask)
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200493 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200494}
495
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200496static void txq_reset_hw_ptr(struct tx_queue *txq)
497{
498 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200499 u32 addr;
500
501 addr = (u32)txq->tx_desc_dma;
502 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800503 wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200504}
505
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200506static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200507{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200508 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800509 wrlp(mp, TXQ_COMMAND, 1 << txq->index);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200510}
511
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200512static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200513{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200514 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200515 u8 mask = 1 << txq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200516
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800517 wrlp(mp, TXQ_COMMAND, mask << 8);
518 while (rdlp(mp, TXQ_COMMAND) & mask)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200519 udelay(10);
520}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200521
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200522static void txq_maybe_wake(struct tx_queue *txq)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200523{
524 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200525 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200526
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200527 if (netif_tx_queue_stopped(nq)) {
528 __netif_tx_lock(nq, smp_processor_id());
529 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
530 netif_tx_wake_queue(nq);
531 __netif_tx_unlock(nq);
532 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200533}
534
535
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200536/* rx napi ******************************************************************/
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000537static int
538mv643xx_get_skb_header(struct sk_buff *skb, void **iphdr, void **tcph,
539 u64 *hdr_flags, void *priv)
540{
541 unsigned long cmd_sts = (unsigned long)priv;
542
543 /*
544 * Make sure that this packet is Ethernet II, is not VLAN
545 * tagged, is IPv4, has a valid IP header, and is TCP.
546 */
547 if ((cmd_sts & (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
548 RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_MASK |
549 RX_PKT_IS_VLAN_TAGGED)) !=
550 (RX_IP_HDR_OK | RX_PKT_IS_IPV4 |
551 RX_PKT_IS_ETHERNETV2 | RX_PKT_LAYER4_TYPE_TCP_IPV4))
552 return -1;
553
554 skb_reset_network_header(skb);
555 skb_set_transport_header(skb, ip_hdrlen(skb));
556 *iphdr = ip_hdr(skb);
557 *tcph = tcp_hdr(skb);
558 *hdr_flags = LRO_IPV4 | LRO_TCP;
559
560 return 0;
561}
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000562
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200563static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200565 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
566 struct net_device_stats *stats = &mp->dev->stats;
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000567 int lro_flush_needed;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200568 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000570 lro_flush_needed = 0;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200571 rx = 0;
Lennert Buytenhek9e1f3772008-08-24 02:33:47 +0200572 while (rx < budget && rxq->rx_desc_count) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200573 struct rx_desc *rx_desc;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200574 unsigned int cmd_sts;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200575 struct sk_buff *skb;
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200576 u16 byte_cnt;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200577
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200578 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200579
580 cmd_sts = rx_desc->cmd_sts;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200581 if (cmd_sts & BUFFER_OWNED_BY_DMA)
Lennert Buytenhek96587662008-06-01 10:31:56 +0200582 break;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200583 rmb();
584
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200585 skb = rxq->rx_skb[rxq->rx_curr_desc];
586 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200587
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200588 rxq->rx_curr_desc++;
589 if (rxq->rx_curr_desc == rxq->rx_ring_size)
590 rxq->rx_curr_desc = 0;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200591
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700592 dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200593 rx_desc->buf_size, DMA_FROM_DEVICE);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200594 rxq->rx_desc_count--;
595 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700596
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200597 mp->work_rx_refill |= 1 << rxq->index;
598
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200599 byte_cnt = rx_desc->byte_cnt;
600
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700601 /*
602 * Update statistics.
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200603 *
604 * Note that the descriptor byte count includes 2 dummy
605 * bytes automatically inserted by the hardware at the
606 * start of the packet (which we don't count), and a 4
607 * byte CRC at the end of the packet (which we do count).
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700608 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 stats->rx_packets++;
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200610 stats->rx_bytes += byte_cnt - 2;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200613 * In case we received a packet without first / last bits
614 * on, or the error summary bit is set, the packet needs
615 * to be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 */
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800617 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
618 != (RX_FIRST_DESC | RX_LAST_DESC))
619 goto err;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200620
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800621 /*
622 * The -4 is for the CRC in the trailer of the
623 * received packet
624 */
625 skb_put(skb, byte_cnt - 2 - 4);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200626
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800627 if (cmd_sts & LAYER_4_CHECKSUM_OK)
628 skb->ip_summed = CHECKSUM_UNNECESSARY;
629 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000630
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000631 if (skb->dev->features & NETIF_F_LRO &&
632 skb->ip_summed == CHECKSUM_UNNECESSARY) {
633 lro_receive_skb(&rxq->lro_mgr, skb, (void *)cmd_sts);
634 lro_flush_needed = 1;
635 } else
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000636 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800638 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800640err:
641 stats->rx_dropped++;
642
643 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
644 (RX_FIRST_DESC | RX_LAST_DESC)) {
645 if (net_ratelimit())
Joe Perches7542db82011-03-02 17:50:35 +0000646 netdev_err(mp->dev,
647 "received packet spanning multiple descriptors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800649
650 if (cmd_sts & ERROR_SUMMARY)
651 stats->rx_errors++;
652
653 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200655
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000656 if (lro_flush_needed)
657 lro_flush_all(&rxq->lro_mgr);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000658
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200659 if (rx < budget)
660 mp->work_rx &= ~(1 << rxq->index);
661
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200662 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200665static int rxq_refill(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200667 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200668 int refilled;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200669
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200670 refilled = 0;
671 while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
672 struct sk_buff *skb;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200673 int rx;
Lennert Buytenhek53771522008-11-20 03:59:04 -0800674 struct rx_desc *rx_desc;
Saeed Bishara530e5572010-01-05 09:15:32 +0000675 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Eric Dumazetacb600d2012-10-05 06:23:55 +0000677 skb = netdev_alloc_skb(mp->dev, mp->skb_size);
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700678
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200679 if (skb == NULL) {
Lennert Buytenhek1319eba2009-04-29 11:57:34 +0000680 mp->oom = 1;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200681 goto oom;
682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +0000684 if (SKB_DMA_REALIGN)
685 skb_reserve(skb, SKB_DMA_REALIGN);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200686
687 refilled++;
688 rxq->rx_desc_count++;
689
690 rx = rxq->rx_used_desc++;
691 if (rxq->rx_used_desc == rxq->rx_ring_size)
692 rxq->rx_used_desc = 0;
693
Lennert Buytenhek53771522008-11-20 03:59:04 -0800694 rx_desc = rxq->rx_desc_area + rx;
695
Saeed Bishara530e5572010-01-05 09:15:32 +0000696 size = skb->end - skb->data;
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700697 rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
Saeed Bishara530e5572010-01-05 09:15:32 +0000698 skb->data, size,
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700699 DMA_FROM_DEVICE);
Saeed Bishara530e5572010-01-05 09:15:32 +0000700 rx_desc->buf_size = size;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200701 rxq->rx_skb[rx] = skb;
702 wmb();
Lennert Buytenhek53771522008-11-20 03:59:04 -0800703 rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200704 wmb();
705
706 /*
707 * The hardware automatically prepends 2 bytes of
708 * dummy data to each received packet, so that the
709 * IP header ends up 16-byte aligned.
710 */
711 skb_reserve(skb, 2);
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200712 }
713
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200714 if (refilled < budget)
715 mp->work_rx_refill &= ~(1 << rxq->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200717oom:
718 return refilled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200721
722/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700723static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
724{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200725 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700726
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700727 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000728 const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
729
730 if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700731 return 1;
732 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200733
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700734 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700735}
736
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200737static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700738{
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700739 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200740 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700741 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700742
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200743 for (frag = 0; frag < nr_frags; frag++) {
744 skb_frag_t *this_frag;
745 int tx_index;
746 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700747
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200748 this_frag = &skb_shinfo(skb)->frags[frag];
Lennert Buytenhek66823b92008-11-20 03:58:09 -0800749 tx_index = txq->tx_curr_desc++;
750 if (txq->tx_curr_desc == txq->tx_ring_size)
751 txq->tx_curr_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200752 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700753
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200754 /*
755 * The last fragment will generate an interrupt
756 * which will free the skb on TX completion.
757 */
758 if (frag == nr_frags - 1) {
759 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
760 ZERO_PADDING | TX_LAST_DESC |
761 TX_ENABLE_INTERRUPT;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200762 } else {
763 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200764 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700765
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700766 desc->l4i_chk = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000767 desc->byte_cnt = skb_frag_size(this_frag);
Ian Campbellf1063582011-08-31 00:46:57 +0000768 desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
769 this_frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000770 skb_frag_size(this_frag),
Ian Campbellf1063582011-08-31 00:46:57 +0000771 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700772 }
773}
774
Byron Bradley324ff2c2008-02-04 23:47:15 -0800775static inline __be16 sum16_as_be(__sum16 sum)
776{
777 return (__force __be16)sum;
778}
779
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200780static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700781{
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200782 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200783 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700784 int tx_index;
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200785 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700786 u32 cmd_sts;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200787 u16 l4i_chk;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700788 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700789
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200790 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200791 l4i_chk = 0;
792
793 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000794 int hdr_len;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200795 int tag_bytes;
796
797 BUG_ON(skb->protocol != htons(ETH_P_IP) &&
798 skb->protocol != htons(ETH_P_8021Q));
799
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000800 hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
801 tag_bytes = hdr_len - ETH_HLEN;
802 if (skb->len - hdr_len > mp->shared->tx_csum_limit ||
803 unlikely(tag_bytes & ~12)) {
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200804 if (skb_checksum_help(skb) == 0)
805 goto no_csum;
806 kfree_skb(skb);
807 return 1;
808 }
809
810 if (tag_bytes & 4)
811 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
812 if (tag_bytes & 8)
813 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
814
815 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
816 GEN_IP_V4_CHECKSUM |
817 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
818
819 switch (ip_hdr(skb)->protocol) {
820 case IPPROTO_UDP:
821 cmd_sts |= UDP_FRAME;
822 l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
823 break;
824 case IPPROTO_TCP:
825 l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
826 break;
827 default:
828 BUG();
829 }
830 } else {
831no_csum:
832 /* Errata BTS #50, IHL must be 5 if no HW checksum */
833 cmd_sts |= 5 << TX_IHL_SHIFT;
834 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700835
Lennert Buytenhek66823b92008-11-20 03:58:09 -0800836 tx_index = txq->tx_curr_desc++;
837 if (txq->tx_curr_desc == txq->tx_ring_size)
838 txq->tx_curr_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200839 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700840
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700841 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200842 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700843 length = skb_headlen(skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700844 } else {
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200845 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700846 length = skb->len;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700847 }
848
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200849 desc->l4i_chk = l4i_chk;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700850 desc->byte_cnt = length;
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700851 desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
852 length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700853
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200854 __skb_queue_tail(&txq->tx_skb, skb);
855
Richard Cochran3b182d72011-06-21 16:01:11 -0700856 skb_tx_timestamp(skb);
857
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700858 /* ensure all other descriptors are written before first cmd_sts */
859 wmb();
860 desc->cmd_sts = cmd_sts;
861
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200862 /* clear TX_END status */
863 mp->work_tx_end &= ~(1 << txq->index);
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200864
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700865 /* ensure all descriptors are written before poking hardware */
866 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200867 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700868
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200869 txq->tx_desc_count += nr_frags + 1;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200870
871 return 0;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700872}
873
Denis Kirjanov0ccfe642009-11-29 17:04:31 -0800874static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200876 struct mv643xx_eth_private *mp = netdev_priv(dev);
Richard Cochran73151ce2011-06-21 16:00:24 -0700877 int length, queue;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200878 struct tx_queue *txq;
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200879 struct netdev_queue *nq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200881 queue = skb_get_queue_mapping(skb);
882 txq = mp->txq + queue;
883 nq = netdev_get_tx_queue(dev, queue);
884
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700885 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200886 txq->tx_dropped++;
Joe Perches7542db82011-03-02 17:50:35 +0000887 netdev_printk(KERN_DEBUG, dev,
888 "failed to linearize skb with tiny unaligned fragment\n");
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700889 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700890 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Lennert Buytenhek17cd0a52008-08-24 05:33:55 +0200892 if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200893 if (net_ratelimit())
Joe Perches7542db82011-03-02 17:50:35 +0000894 netdev_err(dev, "tx queue full?!\n");
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200895 kfree_skb(skb);
896 return NETDEV_TX_OK;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700897 }
898
Richard Cochran73151ce2011-06-21 16:00:24 -0700899 length = skb->len;
900
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200901 if (!txq_submit_skb(txq, skb)) {
902 int entries_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Richard Cochran73151ce2011-06-21 16:00:24 -0700904 txq->tx_bytes += length;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200905 txq->tx_packets++;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200906
907 entries_left = txq->tx_ring_size - txq->tx_desc_count;
908 if (entries_left < MAX_SKB_FRAGS + 1)
909 netif_tx_stop_queue(nq);
910 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700911
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700912 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200915
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200916/* tx napi ******************************************************************/
917static void txq_kick(struct tx_queue *txq)
918{
919 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200920 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200921 u32 hw_desc_ptr;
922 u32 expected_ptr;
923
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200924 __netif_tx_lock(nq, smp_processor_id());
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200925
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800926 if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200927 goto out;
928
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800929 hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200930 expected_ptr = (u32)txq->tx_desc_dma +
931 txq->tx_curr_desc * sizeof(struct tx_desc);
932
933 if (hw_desc_ptr != expected_ptr)
934 txq_enable(txq);
935
936out:
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200937 __netif_tx_unlock(nq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200938
939 mp->work_tx_end &= ~(1 << txq->index);
940}
941
942static int txq_reclaim(struct tx_queue *txq, int budget, int force)
943{
944 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200945 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200946 int reclaimed;
947
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200948 __netif_tx_lock(nq, smp_processor_id());
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200949
950 reclaimed = 0;
951 while (reclaimed < budget && txq->tx_desc_count > 0) {
952 int tx_index;
953 struct tx_desc *desc;
954 u32 cmd_sts;
955 struct sk_buff *skb;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200956
957 tx_index = txq->tx_used_desc;
958 desc = &txq->tx_desc_area[tx_index];
959 cmd_sts = desc->cmd_sts;
960
961 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
962 if (!force)
963 break;
964 desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
965 }
966
967 txq->tx_used_desc = tx_index + 1;
968 if (txq->tx_used_desc == txq->tx_ring_size)
969 txq->tx_used_desc = 0;
970
971 reclaimed++;
972 txq->tx_desc_count--;
973
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200974 skb = NULL;
975 if (cmd_sts & TX_LAST_DESC)
976 skb = __skb_dequeue(&txq->tx_skb);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200977
978 if (cmd_sts & ERROR_SUMMARY) {
Joe Perches7542db82011-03-02 17:50:35 +0000979 netdev_info(mp->dev, "tx error\n");
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200980 mp->dev->stats.tx_errors++;
981 }
982
Lennert Buytenheka4189502008-09-13 04:16:15 +0200983 if (cmd_sts & TX_FIRST_DESC) {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700984 dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
Lennert Buytenheka4189502008-09-13 04:16:15 +0200985 desc->byte_cnt, DMA_TO_DEVICE);
986 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700987 dma_unmap_page(mp->dev->dev.parent, desc->buf_ptr,
Lennert Buytenheka4189502008-09-13 04:16:15 +0200988 desc->byte_cnt, DMA_TO_DEVICE);
989 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200990
Eric Dumazetacb600d2012-10-05 06:23:55 +0000991 dev_kfree_skb(skb);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200992 }
993
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200994 __netif_tx_unlock(nq);
995
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200996 if (reclaimed < budget)
997 mp->work_tx &= ~(1 << txq->index);
998
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200999 return reclaimed;
1000}
1001
1002
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001003/* tx rate control **********************************************************/
1004/*
1005 * Set total maximum TX rate (shared by all TX queues for this port)
1006 * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
1007 */
1008static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
1009{
1010 int token_rate;
1011 int mtu;
1012 int bucket_size;
1013
Andrew Lunn452503e2011-12-24 01:24:24 +01001014 token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001015 if (token_rate > 1023)
1016 token_rate = 1023;
1017
1018 mtu = (mp->dev->mtu + 255) >> 8;
1019 if (mtu > 63)
1020 mtu = 63;
1021
1022 bucket_size = (burst + 255) >> 8;
1023 if (bucket_size > 65535)
1024 bucket_size = 65535;
1025
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001026 switch (mp->shared->tx_bw_control) {
1027 case TX_BW_CONTROL_OLD_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001028 wrlp(mp, TX_BW_RATE, token_rate);
1029 wrlp(mp, TX_BW_MTU, mtu);
1030 wrlp(mp, TX_BW_BURST, bucket_size);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001031 break;
1032 case TX_BW_CONTROL_NEW_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001033 wrlp(mp, TX_BW_RATE_MOVED, token_rate);
1034 wrlp(mp, TX_BW_MTU_MOVED, mtu);
1035 wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001036 break;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02001037 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001038}
1039
1040static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
1041{
1042 struct mv643xx_eth_private *mp = txq_to_mp(txq);
1043 int token_rate;
1044 int bucket_size;
1045
Andrew Lunn452503e2011-12-24 01:24:24 +01001046 token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001047 if (token_rate > 1023)
1048 token_rate = 1023;
1049
1050 bucket_size = (burst + 255) >> 8;
1051 if (bucket_size > 65535)
1052 bucket_size = 65535;
1053
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001054 wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
1055 wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001056}
1057
1058static void txq_set_fixed_prio_mode(struct tx_queue *txq)
1059{
1060 struct mv643xx_eth_private *mp = txq_to_mp(txq);
1061 int off;
1062 u32 val;
1063
1064 /*
1065 * Turn on fixed priority mode.
1066 */
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001067 off = 0;
1068 switch (mp->shared->tx_bw_control) {
1069 case TX_BW_CONTROL_OLD_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001070 off = TXQ_FIX_PRIO_CONF;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001071 break;
1072 case TX_BW_CONTROL_NEW_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001073 off = TXQ_FIX_PRIO_CONF_MOVED;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001074 break;
1075 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001076
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001077 if (off) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001078 val = rdlp(mp, off);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001079 val |= 1 << txq->index;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001080 wrlp(mp, off, val);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001081 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001082}
1083
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001084
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001085/* mii management interface *************************************************/
Phil Sutter260055b2013-03-06 07:49:02 +00001086static void mv643xx_adjust_pscr(struct mv643xx_eth_private *mp)
1087{
1088 u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1089 u32 autoneg_disable = FORCE_LINK_PASS |
1090 DISABLE_AUTO_NEG_SPEED_GMII |
1091 DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1092 DISABLE_AUTO_NEG_FOR_DUPLEX;
1093
1094 if (mp->phy->autoneg == AUTONEG_ENABLE) {
1095 /* enable auto negotiation */
1096 pscr &= ~autoneg_disable;
1097 goto out_write;
1098 }
1099
1100 pscr |= autoneg_disable;
1101
1102 if (mp->phy->speed == SPEED_1000) {
1103 /* force gigabit, half duplex not supported */
1104 pscr |= SET_GMII_SPEED_TO_1000;
1105 pscr |= SET_FULL_DUPLEX_MODE;
1106 goto out_write;
1107 }
1108
1109 pscr &= ~SET_GMII_SPEED_TO_1000;
1110
1111 if (mp->phy->speed == SPEED_100)
1112 pscr |= SET_MII_SPEED_TO_100;
1113 else
1114 pscr &= ~SET_MII_SPEED_TO_100;
1115
1116 if (mp->phy->duplex == DUPLEX_FULL)
1117 pscr |= SET_FULL_DUPLEX_MODE;
1118 else
1119 pscr &= ~SET_FULL_DUPLEX_MODE;
1120
1121out_write:
1122 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1123}
1124
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001125static irqreturn_t mv643xx_eth_err_irq(int irq, void *dev_id)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001126{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001127 struct mv643xx_eth_shared_private *msp = dev_id;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001128
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001129 if (readl(msp->base + ERR_INT_CAUSE) & ERR_INT_SMI_DONE) {
1130 writel(~ERR_INT_SMI_DONE, msp->base + ERR_INT_CAUSE);
1131 wake_up(&msp->smi_busy_wait);
1132 return IRQ_HANDLED;
1133 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001134
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001135 return IRQ_NONE;
1136}
1137
1138static int smi_is_done(struct mv643xx_eth_shared_private *msp)
1139{
1140 return !(readl(msp->base + SMI_REG) & SMI_BUSY);
1141}
1142
1143static int smi_wait_ready(struct mv643xx_eth_shared_private *msp)
1144{
1145 if (msp->err_interrupt == NO_IRQ) {
1146 int i;
1147
1148 for (i = 0; !smi_is_done(msp); i++) {
1149 if (i == 10)
1150 return -ETIMEDOUT;
1151 msleep(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001152 }
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001153
1154 return 0;
1155 }
1156
Lennert Buytenhekee044482008-11-01 06:32:20 +01001157 if (!smi_is_done(msp)) {
1158 wait_event_timeout(msp->smi_busy_wait, smi_is_done(msp),
1159 msecs_to_jiffies(100));
1160 if (!smi_is_done(msp))
1161 return -ETIMEDOUT;
1162 }
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001163
1164 return 0;
1165}
1166
Lennert Buytenheked944932008-08-26 13:34:19 +02001167static int smi_bus_read(struct mii_bus *bus, int addr, int reg)
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001168{
Lennert Buytenheked944932008-08-26 13:34:19 +02001169 struct mv643xx_eth_shared_private *msp = bus->priv;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001170 void __iomem *smi_reg = msp->base + SMI_REG;
1171 int ret;
1172
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001173 if (smi_wait_ready(msp)) {
Joe Perches7542db82011-03-02 17:50:35 +00001174 pr_warn("SMI bus busy timeout\n");
Lennert Buytenheked944932008-08-26 13:34:19 +02001175 return -ETIMEDOUT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001176 }
1177
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001178 writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001179
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001180 if (smi_wait_ready(msp)) {
Joe Perches7542db82011-03-02 17:50:35 +00001181 pr_warn("SMI bus busy timeout\n");
Lennert Buytenheked944932008-08-26 13:34:19 +02001182 return -ETIMEDOUT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001183 }
1184
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001185 ret = readl(smi_reg);
1186 if (!(ret & SMI_READ_VALID)) {
Joe Perches7542db82011-03-02 17:50:35 +00001187 pr_warn("SMI bus read not valid\n");
Lennert Buytenheked944932008-08-26 13:34:19 +02001188 return -ENODEV;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001189 }
1190
Lennert Buytenheked944932008-08-26 13:34:19 +02001191 return ret & 0xffff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001192}
1193
Lennert Buytenheked944932008-08-26 13:34:19 +02001194static int smi_bus_write(struct mii_bus *bus, int addr, int reg, u16 val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001195{
Lennert Buytenheked944932008-08-26 13:34:19 +02001196 struct mv643xx_eth_shared_private *msp = bus->priv;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001197 void __iomem *smi_reg = msp->base + SMI_REG;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001198
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001199 if (smi_wait_ready(msp)) {
Joe Perches7542db82011-03-02 17:50:35 +00001200 pr_warn("SMI bus busy timeout\n");
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001201 return -ETIMEDOUT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001202 }
1203
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001204 writel(SMI_OPCODE_WRITE | (reg << 21) |
Lennert Buytenheked944932008-08-26 13:34:19 +02001205 (addr << 16) | (val & 0xffff), smi_reg);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001206
Lennert Buytenheked944932008-08-26 13:34:19 +02001207 if (smi_wait_ready(msp)) {
Joe Perches7542db82011-03-02 17:50:35 +00001208 pr_warn("SMI bus busy timeout\n");
Lennert Buytenheked944932008-08-26 13:34:19 +02001209 return -ETIMEDOUT;
1210 }
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001211
1212 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001213}
1214
1215
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02001216/* statistics ***************************************************************/
1217static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1218{
1219 struct mv643xx_eth_private *mp = netdev_priv(dev);
1220 struct net_device_stats *stats = &dev->stats;
1221 unsigned long tx_packets = 0;
1222 unsigned long tx_bytes = 0;
1223 unsigned long tx_dropped = 0;
1224 int i;
1225
1226 for (i = 0; i < mp->txq_count; i++) {
1227 struct tx_queue *txq = mp->txq + i;
1228
1229 tx_packets += txq->tx_packets;
1230 tx_bytes += txq->tx_bytes;
1231 tx_dropped += txq->tx_dropped;
1232 }
1233
1234 stats->tx_packets = tx_packets;
1235 stats->tx_bytes = tx_bytes;
1236 stats->tx_dropped = tx_dropped;
1237
1238 return stats;
1239}
1240
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001241static void mv643xx_eth_grab_lro_stats(struct mv643xx_eth_private *mp)
1242{
1243 u32 lro_aggregated = 0;
1244 u32 lro_flushed = 0;
1245 u32 lro_no_desc = 0;
1246 int i;
1247
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001248 for (i = 0; i < mp->rxq_count; i++) {
1249 struct rx_queue *rxq = mp->rxq + i;
1250
1251 lro_aggregated += rxq->lro_mgr.stats.aggregated;
1252 lro_flushed += rxq->lro_mgr.stats.flushed;
1253 lro_no_desc += rxq->lro_mgr.stats.no_desc;
1254 }
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001255
1256 mp->lro_counters.lro_aggregated = lro_aggregated;
1257 mp->lro_counters.lro_flushed = lro_flushed;
1258 mp->lro_counters.lro_no_desc = lro_no_desc;
1259}
1260
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001261static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001262{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001263 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001264}
1265
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001266static void mib_counters_clear(struct mv643xx_eth_private *mp)
1267{
1268 int i;
1269
1270 for (i = 0; i < 0x80; i += 4)
1271 mib_read(mp, i);
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001272
1273 /* Clear non MIB hw counters also */
1274 rdlp(mp, RX_DISCARD_FRAME_CNT);
1275 rdlp(mp, RX_OVERRUN_FRAME_CNT);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001276}
1277
1278static void mib_counters_update(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001279{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001280 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001281
Sebastian Siewior57e8f262009-02-16 11:28:15 +00001282 spin_lock_bh(&mp->mib_counters_lock);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001283 p->good_octets_received += mib_read(mp, 0x00);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001284 p->bad_octets_received += mib_read(mp, 0x08);
1285 p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1286 p->good_frames_received += mib_read(mp, 0x10);
1287 p->bad_frames_received += mib_read(mp, 0x14);
1288 p->broadcast_frames_received += mib_read(mp, 0x18);
1289 p->multicast_frames_received += mib_read(mp, 0x1c);
1290 p->frames_64_octets += mib_read(mp, 0x20);
1291 p->frames_65_to_127_octets += mib_read(mp, 0x24);
1292 p->frames_128_to_255_octets += mib_read(mp, 0x28);
1293 p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1294 p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1295 p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1296 p->good_octets_sent += mib_read(mp, 0x38);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001297 p->good_frames_sent += mib_read(mp, 0x40);
1298 p->excessive_collision += mib_read(mp, 0x44);
1299 p->multicast_frames_sent += mib_read(mp, 0x48);
1300 p->broadcast_frames_sent += mib_read(mp, 0x4c);
1301 p->unrec_mac_control_received += mib_read(mp, 0x50);
1302 p->fc_sent += mib_read(mp, 0x54);
1303 p->good_fc_received += mib_read(mp, 0x58);
1304 p->bad_fc_received += mib_read(mp, 0x5c);
1305 p->undersize_received += mib_read(mp, 0x60);
1306 p->fragments_received += mib_read(mp, 0x64);
1307 p->oversize_received += mib_read(mp, 0x68);
1308 p->jabber_received += mib_read(mp, 0x6c);
1309 p->mac_receive_error += mib_read(mp, 0x70);
1310 p->bad_crc_event += mib_read(mp, 0x74);
1311 p->collision += mib_read(mp, 0x78);
1312 p->late_collision += mib_read(mp, 0x7c);
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001313 /* Non MIB hardware counters */
1314 p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1315 p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
Sebastian Siewior57e8f262009-02-16 11:28:15 +00001316 spin_unlock_bh(&mp->mib_counters_lock);
Lennert Buytenhek4ff34952008-09-19 05:04:57 +02001317
1318 mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1319}
1320
1321static void mib_counters_timer_wrapper(unsigned long _mp)
1322{
1323 struct mv643xx_eth_private *mp = (void *)_mp;
1324
1325 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326}
1327
1328
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001329/* interrupt coalescing *****************************************************/
1330/*
1331 * Hardware coalescing parameters are set in units of 64 t_clk
1332 * cycles. I.e.:
1333 *
1334 * coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1335 *
1336 * register_value = coal_delay_in_usec * t_clk_rate / 64000000
1337 *
1338 * In the ->set*() methods, we round the computed register value
1339 * to the nearest integer.
1340 */
1341static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1342{
1343 u32 val = rdlp(mp, SDMA_CONFIG);
1344 u64 temp;
1345
1346 if (mp->shared->extended_rx_coal_limit)
1347 temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1348 else
1349 temp = (val & 0x003fff00) >> 8;
1350
1351 temp *= 64000000;
Andrew Lunn452503e2011-12-24 01:24:24 +01001352 do_div(temp, mp->t_clk);
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001353
1354 return (unsigned int)temp;
1355}
1356
1357static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1358{
1359 u64 temp;
1360 u32 val;
1361
Andrew Lunn452503e2011-12-24 01:24:24 +01001362 temp = (u64)usec * mp->t_clk;
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001363 temp += 31999999;
1364 do_div(temp, 64000000);
1365
1366 val = rdlp(mp, SDMA_CONFIG);
1367 if (mp->shared->extended_rx_coal_limit) {
1368 if (temp > 0xffff)
1369 temp = 0xffff;
1370 val &= ~0x023fff80;
1371 val |= (temp & 0x8000) << 10;
1372 val |= (temp & 0x7fff) << 7;
1373 } else {
1374 if (temp > 0x3fff)
1375 temp = 0x3fff;
1376 val &= ~0x003fff00;
1377 val |= (temp & 0x3fff) << 8;
1378 }
1379 wrlp(mp, SDMA_CONFIG, val);
1380}
1381
1382static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1383{
1384 u64 temp;
1385
1386 temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1387 temp *= 64000000;
Andrew Lunn452503e2011-12-24 01:24:24 +01001388 do_div(temp, mp->t_clk);
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001389
1390 return (unsigned int)temp;
1391}
1392
1393static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1394{
1395 u64 temp;
1396
Andrew Lunn452503e2011-12-24 01:24:24 +01001397 temp = (u64)usec * mp->t_clk;
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001398 temp += 31999999;
1399 do_div(temp, 64000000);
1400
1401 if (temp > 0x3fff)
1402 temp = 0x3fff;
1403
1404 wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1405}
1406
1407
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001408/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001409struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001410 char stat_string[ETH_GSTRING_LEN];
1411 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +02001412 int netdev_off;
1413 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001414};
1415
Lennert Buytenhek16820052008-06-01 11:40:29 +02001416#define SSTAT(m) \
1417 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
1418 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001419
Lennert Buytenhek16820052008-06-01 11:40:29 +02001420#define MIBSTAT(m) \
1421 { #m, FIELD_SIZEOF(struct mib_counters, m), \
1422 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1423
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001424#define LROSTAT(m) \
1425 { #m, FIELD_SIZEOF(struct lro_counters, m), \
1426 -1, offsetof(struct mv643xx_eth_private, lro_counters.m) }
1427
Lennert Buytenhek16820052008-06-01 11:40:29 +02001428static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1429 SSTAT(rx_packets),
1430 SSTAT(tx_packets),
1431 SSTAT(rx_bytes),
1432 SSTAT(tx_bytes),
1433 SSTAT(rx_errors),
1434 SSTAT(tx_errors),
1435 SSTAT(rx_dropped),
1436 SSTAT(tx_dropped),
1437 MIBSTAT(good_octets_received),
1438 MIBSTAT(bad_octets_received),
1439 MIBSTAT(internal_mac_transmit_err),
1440 MIBSTAT(good_frames_received),
1441 MIBSTAT(bad_frames_received),
1442 MIBSTAT(broadcast_frames_received),
1443 MIBSTAT(multicast_frames_received),
1444 MIBSTAT(frames_64_octets),
1445 MIBSTAT(frames_65_to_127_octets),
1446 MIBSTAT(frames_128_to_255_octets),
1447 MIBSTAT(frames_256_to_511_octets),
1448 MIBSTAT(frames_512_to_1023_octets),
1449 MIBSTAT(frames_1024_to_max_octets),
1450 MIBSTAT(good_octets_sent),
1451 MIBSTAT(good_frames_sent),
1452 MIBSTAT(excessive_collision),
1453 MIBSTAT(multicast_frames_sent),
1454 MIBSTAT(broadcast_frames_sent),
1455 MIBSTAT(unrec_mac_control_received),
1456 MIBSTAT(fc_sent),
1457 MIBSTAT(good_fc_received),
1458 MIBSTAT(bad_fc_received),
1459 MIBSTAT(undersize_received),
1460 MIBSTAT(fragments_received),
1461 MIBSTAT(oversize_received),
1462 MIBSTAT(jabber_received),
1463 MIBSTAT(mac_receive_error),
1464 MIBSTAT(bad_crc_event),
1465 MIBSTAT(collision),
1466 MIBSTAT(late_collision),
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001467 MIBSTAT(rx_discard),
1468 MIBSTAT(rx_overrun),
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001469 LROSTAT(lro_aggregated),
1470 LROSTAT(lro_flushed),
1471 LROSTAT(lro_no_desc),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001472};
1473
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001474static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001475mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
1476 struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001477{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001478 int err;
1479
Lennert Buytenheked944932008-08-26 13:34:19 +02001480 err = phy_read_status(mp->phy);
1481 if (err == 0)
1482 err = phy_ethtool_gset(mp->phy, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001483
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001484 /*
1485 * The MAC does not support 1000baseT_Half.
1486 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001487 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1488 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1489
1490 return err;
1491}
1492
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001493static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001494mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001495 struct ethtool_cmd *cmd)
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001496{
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001497 u32 port_status;
1498
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001499 port_status = rdlp(mp, PORT_STATUS);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001500
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001501 cmd->supported = SUPPORTED_MII;
1502 cmd->advertising = ADVERTISED_MII;
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001503 switch (port_status & PORT_SPEED_MASK) {
1504 case PORT_SPEED_10:
David Decotigny70739492011-04-27 18:32:40 +00001505 ethtool_cmd_speed_set(cmd, SPEED_10);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001506 break;
1507 case PORT_SPEED_100:
David Decotigny70739492011-04-27 18:32:40 +00001508 ethtool_cmd_speed_set(cmd, SPEED_100);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001509 break;
1510 case PORT_SPEED_1000:
David Decotigny70739492011-04-27 18:32:40 +00001511 ethtool_cmd_speed_set(cmd, SPEED_1000);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001512 break;
1513 default:
1514 cmd->speed = -1;
1515 break;
1516 }
1517 cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001518 cmd->port = PORT_MII;
1519 cmd->phy_address = 0;
1520 cmd->transceiver = XCVR_INTERNAL;
1521 cmd->autoneg = AUTONEG_DISABLE;
1522 cmd->maxtxpkt = 1;
1523 cmd->maxrxpkt = 1;
1524
1525 return 0;
1526}
1527
Michael Stapelberg3871c382013-03-11 13:56:45 +00001528static void
1529mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1530{
1531 struct mv643xx_eth_private *mp = netdev_priv(dev);
1532 wol->supported = 0;
1533 wol->wolopts = 0;
1534 if (mp->phy)
1535 phy_ethtool_get_wol(mp->phy, wol);
1536}
1537
1538static int
1539mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1540{
1541 struct mv643xx_eth_private *mp = netdev_priv(dev);
1542 int err;
1543
1544 if (mp->phy == NULL)
1545 return -EOPNOTSUPP;
1546
1547 err = phy_ethtool_set_wol(mp->phy, wol);
1548 /* Given that mv643xx_eth works without the marvell-specific PHY driver,
1549 * this debugging hint is useful to have.
1550 */
1551 if (err == -EOPNOTSUPP)
1552 netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
1553 return err;
1554}
1555
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001556static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001557mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1558{
1559 struct mv643xx_eth_private *mp = netdev_priv(dev);
1560
1561 if (mp->phy != NULL)
1562 return mv643xx_eth_get_settings_phy(mp, cmd);
1563 else
1564 return mv643xx_eth_get_settings_phyless(mp, cmd);
1565}
1566
1567static int
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001568mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001569{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001570 struct mv643xx_eth_private *mp = netdev_priv(dev);
Phil Sutter260055b2013-03-06 07:49:02 +00001571 int ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001572
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001573 if (mp->phy == NULL)
1574 return -EINVAL;
1575
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001576 /*
1577 * The MAC does not support 1000baseT_Half.
1578 */
1579 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1580
Phil Sutter260055b2013-03-06 07:49:02 +00001581 ret = phy_ethtool_sset(mp->phy, cmd);
1582 if (!ret)
1583 mv643xx_adjust_pscr(mp);
1584 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001585}
1586
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001587static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1588 struct ethtool_drvinfo *drvinfo)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001589{
Axel Lin6f39da22011-11-24 00:41:55 -05001590 strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
1591 sizeof(drvinfo->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001592 strlcpy(drvinfo->version, mv643xx_eth_driver_version,
Axel Lin6f39da22011-11-24 00:41:55 -05001593 sizeof(drvinfo->version));
1594 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1595 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek16820052008-06-01 11:40:29 +02001596 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001597}
1598
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001599static int mv643xx_eth_nway_reset(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001600{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001601 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001602
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001603 if (mp->phy == NULL)
1604 return -EINVAL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001605
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001606 return genphy_restart_aneg(mp->phy);
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001607}
1608
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001609static int
1610mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1611{
1612 struct mv643xx_eth_private *mp = netdev_priv(dev);
1613
1614 ec->rx_coalesce_usecs = get_rx_coal(mp);
1615 ec->tx_coalesce_usecs = get_tx_coal(mp);
1616
1617 return 0;
1618}
1619
1620static int
1621mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1622{
1623 struct mv643xx_eth_private *mp = netdev_priv(dev);
1624
1625 set_rx_coal(mp, ec->rx_coalesce_usecs);
1626 set_tx_coal(mp, ec->tx_coalesce_usecs);
1627
1628 return 0;
1629}
1630
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001631static void
1632mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1633{
1634 struct mv643xx_eth_private *mp = netdev_priv(dev);
1635
1636 er->rx_max_pending = 4096;
1637 er->tx_max_pending = 4096;
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001638
1639 er->rx_pending = mp->rx_ring_size;
1640 er->tx_pending = mp->tx_ring_size;
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001641}
1642
1643static int
1644mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1645{
1646 struct mv643xx_eth_private *mp = netdev_priv(dev);
1647
1648 if (er->rx_mini_pending || er->rx_jumbo_pending)
1649 return -EINVAL;
1650
1651 mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
1652 mp->tx_ring_size = er->tx_pending < 4096 ? er->tx_pending : 4096;
1653
1654 if (netif_running(dev)) {
1655 mv643xx_eth_stop(dev);
1656 if (mv643xx_eth_open(dev)) {
Joe Perches7542db82011-03-02 17:50:35 +00001657 netdev_err(dev,
1658 "fatal error on re-opening device after ring param change\n");
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001659 return -ENOMEM;
1660 }
1661 }
1662
1663 return 0;
1664}
1665
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001666
1667static int
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001668mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001669{
1670 struct mv643xx_eth_private *mp = netdev_priv(dev);
Michał Mirosław3ad9b352011-11-16 14:05:33 +00001671 bool rx_csum = features & NETIF_F_RXCSUM;
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001672
1673 wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1674
1675 return 0;
1676}
1677
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001678static void mv643xx_eth_get_strings(struct net_device *dev,
1679 uint32_t stringset, uint8_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001680{
1681 int i;
1682
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001683 if (stringset == ETH_SS_STATS) {
1684 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001685 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001686 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001687 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001688 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001689 }
1690}
1691
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001692static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1693 struct ethtool_stats *stats,
1694 uint64_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001695{
Lennert Buytenhekb9873842008-08-24 05:59:16 +02001696 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001697 int i;
1698
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02001699 mv643xx_eth_get_stats(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001700 mib_counters_update(mp);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001701 mv643xx_eth_grab_lro_stats(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001702
Lennert Buytenhek16820052008-06-01 11:40:29 +02001703 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1704 const struct mv643xx_eth_stats *stat;
1705 void *p;
1706
1707 stat = mv643xx_eth_stats + i;
1708
1709 if (stat->netdev_off >= 0)
1710 p = ((void *)mp->dev) + stat->netdev_off;
1711 else
1712 p = ((void *)mp) + stat->mp_off;
1713
1714 data[i] = (stat->sizeof_stat == 8) ?
1715 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001716 }
1717}
1718
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001719static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001720{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001721 if (sset == ETH_SS_STATS)
Lennert Buytenhek16820052008-06-01 11:40:29 +02001722 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001723
1724 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001725}
1726
Lennert Buytenheke5371492008-06-01 02:18:13 +02001727static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001728 .get_settings = mv643xx_eth_get_settings,
1729 .set_settings = mv643xx_eth_set_settings,
1730 .get_drvinfo = mv643xx_eth_get_drvinfo,
1731 .nway_reset = mv643xx_eth_nway_reset,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +00001732 .get_link = ethtool_op_get_link,
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001733 .get_coalesce = mv643xx_eth_get_coalesce,
1734 .set_coalesce = mv643xx_eth_set_coalesce,
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001735 .get_ringparam = mv643xx_eth_get_ringparam,
1736 .set_ringparam = mv643xx_eth_set_ringparam,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001737 .get_strings = mv643xx_eth_get_strings,
1738 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001739 .get_sset_count = mv643xx_eth_get_sset_count,
Richard Cochranebad0a82012-04-03 22:59:32 +00001740 .get_ts_info = ethtool_op_get_ts_info,
Michael Stapelberg3871c382013-03-11 13:56:45 +00001741 .get_wol = mv643xx_eth_get_wol,
1742 .set_wol = mv643xx_eth_set_wol,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001743};
1744
1745
1746/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001747static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001748{
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001749 unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1750 unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001751
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001752 addr[0] = (mac_h >> 24) & 0xff;
1753 addr[1] = (mac_h >> 16) & 0xff;
1754 addr[2] = (mac_h >> 8) & 0xff;
1755 addr[3] = mac_h & 0xff;
1756 addr[4] = (mac_l >> 8) & 0xff;
1757 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001758}
1759
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001760static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001761{
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001762 wrlp(mp, MAC_ADDR_HIGH,
1763 (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1764 wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001765}
1766
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001767static u32 uc_addr_filter_mask(struct net_device *dev)
1768{
Jiri Pirkoccffad252009-05-22 23:22:17 +00001769 struct netdev_hw_addr *ha;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001770 u32 nibbles;
1771
1772 if (dev->flags & IFF_PROMISC)
1773 return 0;
1774
1775 nibbles = 1 << (dev->dev_addr[5] & 0x0f);
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001776 netdev_for_each_uc_addr(ha, dev) {
Jiri Pirkoccffad252009-05-22 23:22:17 +00001777 if (memcmp(dev->dev_addr, ha->addr, 5))
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001778 return 0;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001779 if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001780 return 0;
1781
Jiri Pirkoccffad252009-05-22 23:22:17 +00001782 nibbles |= 1 << (ha->addr[5] & 0x0f);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001783 }
1784
1785 return nibbles;
1786}
1787
1788static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001789{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001790 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001791 u32 port_config;
1792 u32 nibbles;
1793 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001794
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001795 uc_addr_set(mp, dev->dev_addr);
1796
Prabhanjan Sarnaik6877f542009-06-18 11:35:02 +00001797 port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1798
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001799 nibbles = uc_addr_filter_mask(dev);
1800 if (!nibbles) {
1801 port_config |= UNICAST_PROMISCUOUS_MODE;
Prabhanjan Sarnaik6877f542009-06-18 11:35:02 +00001802 nibbles = 0xffff;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001803 }
1804
1805 for (i = 0; i < 16; i += 4) {
1806 int off = UNICAST_TABLE(mp->port_num) + i;
1807 u32 v;
1808
1809 v = 0;
1810 if (nibbles & 1)
1811 v |= 0x00000001;
1812 if (nibbles & 2)
1813 v |= 0x00000100;
1814 if (nibbles & 4)
1815 v |= 0x00010000;
1816 if (nibbles & 8)
1817 v |= 0x01000000;
1818 nibbles >>= 4;
1819
1820 wrl(mp, off, v);
1821 }
1822
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001823 wrlp(mp, PORT_CONFIG, port_config);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001824}
1825
Lennert Buytenhek69876562008-06-01 11:43:32 +02001826static int addr_crc(unsigned char *addr)
1827{
1828 int crc = 0;
1829 int i;
1830
1831 for (i = 0; i < 6; i++) {
1832 int j;
1833
1834 crc = (crc ^ addr[i]) << 8;
1835 for (j = 7; j >= 0; j--) {
1836 if (crc & (0x100 << j))
1837 crc ^= 0x107 << j;
1838 }
1839 }
1840
1841 return crc;
1842}
1843
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001844static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001845{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001846 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001847 u32 *mc_spec;
1848 u32 *mc_other;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001849 struct netdev_hw_addr *ha;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001850 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001851
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001852 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001853 int port_num;
1854 u32 accept;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001855
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001856oom:
1857 port_num = mp->port_num;
1858 accept = 0x01010101;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001859 for (i = 0; i < 0x100; i += 4) {
1860 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1861 wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001862 }
1863 return;
1864 }
1865
Sebastian Siewior82a5bd62009-02-14 23:26:18 +00001866 mc_spec = kmalloc(0x200, GFP_ATOMIC);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001867 if (mc_spec == NULL)
1868 goto oom;
1869 mc_other = mc_spec + (0x100 >> 2);
1870
1871 memset(mc_spec, 0, 0x100);
1872 memset(mc_other, 0, 0x100);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001873
Jiri Pirko22bedad32010-04-01 21:22:57 +00001874 netdev_for_each_mc_addr(ha, dev) {
1875 u8 *a = ha->addr;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001876 u32 *table;
1877 int entry;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001878
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001879 if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001880 table = mc_spec;
1881 entry = a[5];
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001882 } else {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001883 table = mc_other;
1884 entry = addr_crc(a);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001885 }
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001886
Lennert Buytenhek2b448332009-01-19 17:17:18 -08001887 table[entry >> 2] |= 1 << (8 * (entry & 3));
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001888 }
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001889
1890 for (i = 0; i < 0x100; i += 4) {
1891 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
1892 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
1893 }
1894
1895 kfree(mc_spec);
1896}
1897
1898static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1899{
1900 mv643xx_eth_program_unicast_filter(dev);
1901 mv643xx_eth_program_multicast_filter(dev);
1902}
1903
1904static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1905{
1906 struct sockaddr *sa = addr;
1907
Denis Kirjanova29ec082009-12-08 20:36:00 -08001908 if (!is_valid_ether_addr(sa->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00001909 return -EADDRNOTAVAIL;
Denis Kirjanova29ec082009-12-08 20:36:00 -08001910
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001911 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1912
1913 netif_addr_lock_bh(dev);
1914 mv643xx_eth_program_unicast_filter(dev);
1915 netif_addr_unlock_bh(dev);
1916
1917 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001918}
1919
1920
1921/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001922static int rxq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001923{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001924 struct rx_queue *rxq = mp->rxq + index;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001925 struct rx_desc *rx_desc;
1926 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001927 int i;
1928
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001929 rxq->index = index;
1930
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001931 rxq->rx_ring_size = mp->rx_ring_size;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001932
1933 rxq->rx_desc_count = 0;
1934 rxq->rx_curr_desc = 0;
1935 rxq->rx_used_desc = 0;
1936
1937 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1938
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001939 if (index == 0 && size <= mp->rx_desc_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001940 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1941 mp->rx_desc_sram_size);
1942 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1943 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001944 rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1945 size, &rxq->rx_desc_dma,
1946 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001947 }
1948
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001949 if (rxq->rx_desc_area == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00001950 netdev_err(mp->dev,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001951 "can't allocate rx ring (%d bytes)\n", size);
1952 goto out;
1953 }
1954 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001955
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001956 rxq->rx_desc_area_size = size;
Joe Perchesb2adaca2013-02-03 17:43:58 +00001957 rxq->rx_skb = kmalloc_array(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1958 GFP_KERNEL);
1959 if (rxq->rx_skb == NULL)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001960 goto out_free;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001961
Joe Perches64699332012-06-04 12:44:16 +00001962 rx_desc = rxq->rx_desc_area;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001963 for (i = 0; i < rxq->rx_ring_size; i++) {
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001964 int nexti;
1965
1966 nexti = i + 1;
1967 if (nexti == rxq->rx_ring_size)
1968 nexti = 0;
1969
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001970 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1971 nexti * sizeof(struct rx_desc);
1972 }
1973
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001974 rxq->lro_mgr.dev = mp->dev;
1975 memset(&rxq->lro_mgr.stats, 0, sizeof(rxq->lro_mgr.stats));
1976 rxq->lro_mgr.features = LRO_F_NAPI;
1977 rxq->lro_mgr.ip_summed = CHECKSUM_UNNECESSARY;
1978 rxq->lro_mgr.ip_summed_aggr = CHECKSUM_UNNECESSARY;
1979 rxq->lro_mgr.max_desc = ARRAY_SIZE(rxq->lro_arr);
1980 rxq->lro_mgr.max_aggr = 32;
1981 rxq->lro_mgr.frag_align_pad = 0;
1982 rxq->lro_mgr.lro_arr = rxq->lro_arr;
1983 rxq->lro_mgr.get_skb_header = mv643xx_get_skb_header;
1984
1985 memset(&rxq->lro_arr, 0, sizeof(rxq->lro_arr));
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +00001986
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001987 return 0;
1988
1989
1990out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001991 if (index == 0 && size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001992 iounmap(rxq->rx_desc_area);
1993 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001994 dma_free_coherent(mp->dev->dev.parent, size,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001995 rxq->rx_desc_area,
1996 rxq->rx_desc_dma);
1997
1998out:
1999 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002000}
2001
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002002static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002003{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002004 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
2005 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002006
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002007 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002008
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002009 for (i = 0; i < rxq->rx_ring_size; i++) {
2010 if (rxq->rx_skb[i]) {
2011 dev_kfree_skb(rxq->rx_skb[i]);
2012 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002013 }
2014 }
2015
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002016 if (rxq->rx_desc_count) {
Joe Perches7542db82011-03-02 17:50:35 +00002017 netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002018 rxq->rx_desc_count);
2019 }
2020
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002021 if (rxq->index == 0 &&
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002022 rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002023 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002024 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07002025 dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002026 rxq->rx_desc_area, rxq->rx_desc_dma);
2027
2028 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002029}
2030
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002031static int txq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002032{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002033 struct tx_queue *txq = mp->txq + index;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002034 struct tx_desc *tx_desc;
2035 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002036 int i;
2037
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002038 txq->index = index;
2039
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002040 txq->tx_ring_size = mp->tx_ring_size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002041
2042 txq->tx_desc_count = 0;
2043 txq->tx_curr_desc = 0;
2044 txq->tx_used_desc = 0;
2045
2046 size = txq->tx_ring_size * sizeof(struct tx_desc);
2047
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002048 if (index == 0 && size <= mp->tx_desc_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002049 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
2050 mp->tx_desc_sram_size);
2051 txq->tx_desc_dma = mp->tx_desc_sram_addr;
2052 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07002053 txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
2054 size, &txq->tx_desc_dma,
2055 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002056 }
2057
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002058 if (txq->tx_desc_area == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002059 netdev_err(mp->dev,
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002060 "can't allocate tx ring (%d bytes)\n", size);
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +02002061 return -ENOMEM;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002062 }
2063 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002064
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002065 txq->tx_desc_area_size = size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002066
Joe Perches64699332012-06-04 12:44:16 +00002067 tx_desc = txq->tx_desc_area;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002068 for (i = 0; i < txq->tx_ring_size; i++) {
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02002069 struct tx_desc *txd = tx_desc + i;
Lennert Buytenhek9da78742008-08-23 23:45:28 +02002070 int nexti;
2071
2072 nexti = i + 1;
2073 if (nexti == txq->tx_ring_size)
2074 nexti = 0;
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02002075
2076 txd->cmd_sts = 0;
2077 txd->next_desc_ptr = txq->tx_desc_dma +
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002078 nexti * sizeof(struct tx_desc);
2079 }
2080
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +02002081 skb_queue_head_init(&txq->tx_skb);
2082
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002083 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002084}
2085
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002086static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002087{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002088 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002089
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002090 txq_disable(txq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002091 txq_reclaim(txq, txq->tx_ring_size, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002092
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002093 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002094
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002095 if (txq->index == 0 &&
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002096 txq->tx_desc_area_size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002097 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002098 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07002099 dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002100 txq->tx_desc_area, txq->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002101}
2102
2103
2104/* netdev ops and related ***************************************************/
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002105static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
2106{
2107 u32 int_cause;
2108 u32 int_cause_ext;
2109
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002110 int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002111 if (int_cause == 0)
2112 return 0;
2113
2114 int_cause_ext = 0;
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002115 if (int_cause & INT_EXT) {
2116 int_cause &= ~INT_EXT;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002117 int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002118 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002119
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002120 if (int_cause) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002121 wrlp(mp, INT_CAUSE, ~int_cause);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002122 mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002123 ~(rdlp(mp, TXQ_COMMAND) & 0xff);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002124 mp->work_rx |= (int_cause & INT_RX) >> 2;
2125 }
2126
2127 int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
2128 if (int_cause_ext) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002129 wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002130 if (int_cause_ext & INT_EXT_LINK_PHY)
2131 mp->work_link = 1;
2132 mp->work_tx |= int_cause_ext & INT_EXT_TX;
2133 }
2134
2135 return 1;
2136}
2137
2138static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
2139{
2140 struct net_device *dev = (struct net_device *)dev_id;
2141 struct mv643xx_eth_private *mp = netdev_priv(dev);
2142
2143 if (unlikely(!mv643xx_eth_collect_events(mp)))
2144 return IRQ_NONE;
2145
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002146 wrlp(mp, INT_MASK, 0);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002147 napi_schedule(&mp->napi);
2148
2149 return IRQ_HANDLED;
2150}
2151
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002152static void handle_link_event(struct mv643xx_eth_private *mp)
2153{
2154 struct net_device *dev = mp->dev;
2155 u32 port_status;
2156 int speed;
2157 int duplex;
2158 int fc;
2159
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002160 port_status = rdlp(mp, PORT_STATUS);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002161 if (!(port_status & LINK_UP)) {
2162 if (netif_carrier_ok(dev)) {
2163 int i;
2164
Joe Perches7542db82011-03-02 17:50:35 +00002165 netdev_info(dev, "link down\n");
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002166
2167 netif_carrier_off(dev);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002168
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002169 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002170 struct tx_queue *txq = mp->txq + i;
2171
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002172 txq_reclaim(txq, txq->tx_ring_size, 1);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002173 txq_reset_hw_ptr(txq);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002174 }
2175 }
2176 return;
2177 }
2178
2179 switch (port_status & PORT_SPEED_MASK) {
2180 case PORT_SPEED_10:
2181 speed = 10;
2182 break;
2183 case PORT_SPEED_100:
2184 speed = 100;
2185 break;
2186 case PORT_SPEED_1000:
2187 speed = 1000;
2188 break;
2189 default:
2190 speed = -1;
2191 break;
2192 }
2193 duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
2194 fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
2195
Joe Perches7542db82011-03-02 17:50:35 +00002196 netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
2197 speed, duplex ? "full" : "half", fc ? "en" : "dis");
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002198
Lennert Buytenhek4fdeca32008-08-28 05:39:13 +02002199 if (!netif_carrier_ok(dev))
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002200 netif_carrier_on(dev);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002201}
2202
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002203static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002204{
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002205 struct mv643xx_eth_private *mp;
2206 int work_done;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002207
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002208 mp = container_of(napi, struct mv643xx_eth_private, napi);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002209
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002210 if (unlikely(mp->oom)) {
2211 mp->oom = 0;
2212 del_timer(&mp->rx_oom);
2213 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002214
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002215 work_done = 0;
2216 while (work_done < budget) {
2217 u8 queue_mask;
2218 int queue;
2219 int work_tbd;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002220
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002221 if (mp->work_link) {
2222 mp->work_link = 0;
2223 handle_link_event(mp);
Lennert Buytenhek26ef1f12009-05-06 03:01:10 +00002224 work_done++;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002225 continue;
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02002226 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002227
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002228 queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2229 if (likely(!mp->oom))
2230 queue_mask |= mp->work_rx_refill;
2231
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002232 if (!queue_mask) {
2233 if (mv643xx_eth_collect_events(mp))
2234 continue;
2235 break;
2236 }
2237
2238 queue = fls(queue_mask) - 1;
2239 queue_mask = 1 << queue;
2240
2241 work_tbd = budget - work_done;
2242 if (work_tbd > 16)
2243 work_tbd = 16;
2244
2245 if (mp->work_tx_end & queue_mask) {
2246 txq_kick(mp->txq + queue);
2247 } else if (mp->work_tx & queue_mask) {
2248 work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2249 txq_maybe_wake(mp->txq + queue);
2250 } else if (mp->work_rx & queue_mask) {
2251 work_done += rxq_process(mp->rxq + queue, work_tbd);
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002252 } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002253 work_done += rxq_refill(mp->rxq + queue, work_tbd);
2254 } else {
2255 BUG();
2256 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002257 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002258
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002259 if (work_done < budget) {
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002260 if (mp->oom)
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002261 mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
2262 napi_complete(napi);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002263 wrlp(mp, INT_MASK, mp->int_mask);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002264 }
2265
2266 return work_done;
2267}
2268
2269static inline void oom_timer_wrapper(unsigned long data)
2270{
2271 struct mv643xx_eth_private *mp = (void *)data;
2272
2273 napi_schedule(&mp->napi);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002274}
2275
Lennert Buytenheke5371492008-06-01 02:18:13 +02002276static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002277{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002278 int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002279
Lennert Buytenheked944932008-08-26 13:34:19 +02002280 data = phy_read(mp->phy, MII_BMCR);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002281 if (data < 0)
2282 return;
2283
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002284 data |= BMCR_RESET;
Lennert Buytenheked944932008-08-26 13:34:19 +02002285 if (phy_write(mp->phy, MII_BMCR, data) < 0)
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002286 return;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002287
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002288 do {
Lennert Buytenheked944932008-08-26 13:34:19 +02002289 data = phy_read(mp->phy, MII_BMCR);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002290 } while (data >= 0 && data & BMCR_RESET);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002291}
2292
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002293static void port_start(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002294{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002295 u32 pscr;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002296 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002297
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002298 /*
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002299 * Perform PHY reset, if there is a PHY.
2300 */
Lennert Buytenheked944932008-08-26 13:34:19 +02002301 if (mp->phy != NULL) {
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002302 struct ethtool_cmd cmd;
2303
2304 mv643xx_eth_get_settings(mp->dev, &cmd);
2305 phy_reset(mp);
2306 mv643xx_eth_set_settings(mp->dev, &cmd);
2307 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002308
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002309 /*
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002310 * Configure basic link parameters.
2311 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002312 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002313
2314 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002315 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002316
2317 pscr |= DO_NOT_FORCE_LINK_FAIL;
Lennert Buytenheked944932008-08-26 13:34:19 +02002318 if (mp->phy == NULL)
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002319 pscr |= FORCE_LINK_PASS;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002320 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002321
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002322 /*
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002323 * Configure TX path and queues.
2324 */
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002325 tx_set_rate(mp, 1000000000, 16777216);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002326 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002327 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002328
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02002329 txq_reset_hw_ptr(txq);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002330 txq_set_rate(txq, 1000000000, 16777216);
2331 txq_set_fixed_prio_mode(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002332 }
2333
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002334 /*
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002335 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
Lennert Buytenhek170e7102008-09-19 02:58:50 +02002336 * frames to RX queue #0, and include the pseudo-header when
2337 * calculating receive checksums.
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002338 */
David S. Millere138f962011-04-21 15:19:02 -07002339 mv643xx_eth_set_features(mp->dev, mp->dev->features);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002340
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002341 /*
2342 * Treat BPDUs as normal multicasts, and disable partition mode.
2343 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002344 wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002345
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002346 /*
Lennert Buytenhek5a893922009-03-13 15:48:02 -07002347 * Add configured unicast addresses to address filter table.
2348 */
2349 mv643xx_eth_program_unicast_filter(mp->dev);
2350
2351 /*
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002352 * Enable the receive queues.
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002353 */
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002354 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002355 struct rx_queue *rxq = mp->rxq + i;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002356 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002357
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002358 addr = (u32)rxq->rx_desc_dma;
2359 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002360 wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002361
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002362 rxq_enable(rxq);
2363 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002364}
2365
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002366static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2367{
2368 int skb_size;
2369
2370 /*
2371 * Reserve 2+14 bytes for an ethernet header (the hardware
2372 * automatically prepends 2 bytes of dummy data to each
2373 * received packet), 16 bytes for up to four VLAN tags, and
2374 * 4 bytes for the trailing FCS -- 36 bytes total.
2375 */
2376 skb_size = mp->dev->mtu + 36;
2377
2378 /*
2379 * Make sure that the skb size is a multiple of 8 bytes, as
2380 * the lower three bits of the receive descriptor's buffer
2381 * size field are ignored by the hardware.
2382 */
2383 mp->skb_size = (skb_size + 7) & ~7;
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +00002384
2385 /*
2386 * If NET_SKB_PAD is smaller than a cache line,
2387 * netdev_alloc_skb() will cause skb->data to be misaligned
2388 * to a cache line boundary. If this is the case, include
2389 * some extra space to allow re-aligning the data area.
2390 */
2391 mp->skb_size += SKB_DMA_REALIGN;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002392}
2393
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002394static int mv643xx_eth_open(struct net_device *dev)
2395{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002396 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002397 int err;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002398 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002399
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002400 wrlp(mp, INT_CAUSE, 0);
2401 wrlp(mp, INT_CAUSE_EXT, 0);
2402 rdlp(mp, INT_CAUSE_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002403
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002404 err = request_irq(dev->irq, mv643xx_eth_irq,
Lennert Buytenhek2a1867a2008-08-23 23:43:38 +02002405 IRQF_SHARED, dev->name, dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002406 if (err) {
Joe Perches7542db82011-03-02 17:50:35 +00002407 netdev_err(dev, "can't assign irq\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002408 return -EAGAIN;
2409 }
2410
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002411 mv643xx_eth_recalc_skb_size(mp);
2412
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002413 napi_enable(&mp->napi);
2414
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002415 mp->int_mask = INT_EXT;
2416
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002417 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002418 err = rxq_init(mp, i);
2419 if (err) {
2420 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002421 rxq_deinit(mp->rxq + i);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002422 goto out;
2423 }
2424
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002425 rxq_refill(mp->rxq + i, INT_MAX);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002426 mp->int_mask |= INT_RX_0 << i;
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002427 }
2428
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002429 if (mp->oom) {
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002430 mp->rx_oom.expires = jiffies + (HZ / 10);
2431 add_timer(&mp->rx_oom);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002432 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002433
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002434 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002435 err = txq_init(mp, i);
2436 if (err) {
2437 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002438 txq_deinit(mp->txq + i);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002439 goto out_free;
2440 }
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002441 mp->int_mask |= INT_TX_END_0 << i;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002442 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002443
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002444 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002445
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002446 wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002447 wrlp(mp, INT_MASK, mp->int_mask);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002448
2449 return 0;
2450
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002451
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002452out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002453 for (i = 0; i < mp->rxq_count; i++)
2454 rxq_deinit(mp->rxq + i);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002455out:
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002456 free_irq(dev->irq, dev);
2457
2458 return err;
2459}
2460
Lennert Buytenheke5371492008-06-01 02:18:13 +02002461static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002462{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002463 unsigned int data;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002464 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002465
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002466 for (i = 0; i < mp->rxq_count; i++)
2467 rxq_disable(mp->rxq + i);
2468 for (i = 0; i < mp->txq_count; i++)
2469 txq_disable(mp->txq + i);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002470
2471 while (1) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002472 u32 ps = rdlp(mp, PORT_STATUS);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002473
2474 if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2475 break;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002476 udelay(10);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002477 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002478
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002479 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002480 data = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002481 data &= ~(SERIAL_PORT_ENABLE |
2482 DO_NOT_FORCE_LINK_FAIL |
2483 FORCE_LINK_PASS);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002484 wrlp(mp, PORT_SERIAL_CONTROL, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002485}
2486
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002487static int mv643xx_eth_stop(struct net_device *dev)
2488{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002489 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002490 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002491
Gabriel Paubertfe65e702009-01-19 17:18:09 -08002492 wrlp(mp, INT_MASK_EXT, 0x00000000);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002493 wrlp(mp, INT_MASK, 0x00000000);
2494 rdlp(mp, INT_MASK);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002495
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002496 napi_disable(&mp->napi);
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002497
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002498 del_timer_sync(&mp->rx_oom);
2499
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002500 netif_carrier_off(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002501
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002502 free_irq(dev->irq, dev);
2503
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002504 port_reset(mp);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02002505 mv643xx_eth_get_stats(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002506 mib_counters_update(mp);
Sebastian Siewior57e8f262009-02-16 11:28:15 +00002507 del_timer_sync(&mp->mib_counters_timer);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002508
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002509 for (i = 0; i < mp->rxq_count; i++)
2510 rxq_deinit(mp->rxq + i);
2511 for (i = 0; i < mp->txq_count; i++)
2512 txq_deinit(mp->txq + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002513
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002514 return 0;
2515}
2516
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002517static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002518{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002519 struct mv643xx_eth_private *mp = netdev_priv(dev);
Phil Sutter260055b2013-03-06 07:49:02 +00002520 int ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002521
Phil Sutter260055b2013-03-06 07:49:02 +00002522 if (mp->phy == NULL)
2523 return -ENOTSUPP;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002524
Phil Sutter260055b2013-03-06 07:49:02 +00002525 ret = phy_mii_ioctl(mp->phy, ifr, cmd);
2526 if (!ret)
2527 mv643xx_adjust_pscr(mp);
2528 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002529}
2530
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002531static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2532{
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002533 struct mv643xx_eth_private *mp = netdev_priv(dev);
2534
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002535 if (new_mtu < 64 || new_mtu > 9500)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002536 return -EINVAL;
2537
2538 dev->mtu = new_mtu;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002539 mv643xx_eth_recalc_skb_size(mp);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002540 tx_set_rate(mp, 1000000000, 16777216);
2541
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002542 if (!netif_running(dev))
2543 return 0;
2544
2545 /*
2546 * Stop and then re-open the interface. This will allocate RX
2547 * skbs of the new MTU.
2548 * There is a possible danger that the open will not succeed,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002549 * due to memory being full.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002550 */
2551 mv643xx_eth_stop(dev);
2552 if (mv643xx_eth_open(dev)) {
Joe Perches7542db82011-03-02 17:50:35 +00002553 netdev_err(dev,
2554 "fatal error on re-opening device after MTU change\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002555 }
2556
2557 return 0;
2558}
2559
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002560static void tx_timeout_task(struct work_struct *ugly)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002561{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002562 struct mv643xx_eth_private *mp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002563
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002564 mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2565 if (netif_running(mp->dev)) {
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002566 netif_tx_stop_all_queues(mp->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002567 port_reset(mp);
2568 port_start(mp);
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002569 netif_tx_wake_all_queues(mp->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002570 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002571}
2572
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002573static void mv643xx_eth_tx_timeout(struct net_device *dev)
2574{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002575 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002576
Joe Perches7542db82011-03-02 17:50:35 +00002577 netdev_info(dev, "tx timeout\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002578
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002579 schedule_work(&mp->tx_timeout_task);
2580}
2581
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002582#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002583static void mv643xx_eth_netpoll(struct net_device *dev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002584{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002585 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002586
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002587 wrlp(mp, INT_MASK, 0x00000000);
2588 rdlp(mp, INT_MASK);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002589
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002590 mv643xx_eth_irq(dev->irq, dev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002591
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002592 wrlp(mp, INT_MASK, mp->int_mask);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002593}
2594#endif
2595
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002596
2597/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02002598static void
2599mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
Andrew Lunn63a93322011-12-07 21:48:07 +01002600 const struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002601{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002602 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002603 u32 win_enable;
2604 u32 win_protect;
2605 int i;
2606
2607 for (i = 0; i < 6; i++) {
2608 writel(0, base + WINDOW_BASE(i));
2609 writel(0, base + WINDOW_SIZE(i));
2610 if (i < 4)
2611 writel(0, base + WINDOW_REMAP_HIGH(i));
2612 }
2613
2614 win_enable = 0x3f;
2615 win_protect = 0;
2616
2617 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01002618 const struct mbus_dram_window *cs = dram->cs + i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002619
2620 writel((cs->base & 0xffff0000) |
2621 (cs->mbus_attr << 8) |
2622 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2623 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2624
2625 win_enable &= ~(1 << i);
2626 win_protect |= 3 << (2 * i);
2627 }
2628
2629 writel(win_enable, base + WINDOW_BAR_ENABLE);
2630 msp->win_protect = win_protect;
2631}
2632
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002633static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2634{
2635 /*
2636 * Check whether we have a 14-bit coal limit field in bits
2637 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2638 * SDMA config register.
2639 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002640 writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2641 if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002642 msp->extended_rx_coal_limit = 1;
2643 else
2644 msp->extended_rx_coal_limit = 0;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002645
2646 /*
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002647 * Check whether the MAC supports TX rate control, and if
2648 * yes, whether its associated registers are in the old or
2649 * the new place.
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002650 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002651 writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2652 if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002653 msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2654 } else {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002655 writel(7, msp->base + 0x0400 + TX_BW_RATE);
2656 if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002657 msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2658 else
2659 msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2660 }
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002661}
2662
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002663static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2664{
Lennert Buytenhek10a99482008-11-20 03:57:16 -08002665 static int mv643xx_eth_version_printed;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002666 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002667 struct mv643xx_eth_shared_private *msp;
Andrew Lunn63a93322011-12-07 21:48:07 +01002668 const struct mbus_dram_target_info *dram;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002669 struct resource *res;
2670 int ret;
2671
Lennert Buytenheke5371492008-06-01 02:18:13 +02002672 if (!mv643xx_eth_version_printed++)
Joe Perches7542db82011-03-02 17:50:35 +00002673 pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2674 mv643xx_eth_driver_version);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002675
2676 ret = -EINVAL;
2677 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2678 if (res == NULL)
2679 goto out;
2680
2681 ret = -ENOMEM;
Julia Lawallbeae22e2010-05-13 10:00:22 +00002682 msp = kzalloc(sizeof(*msp), GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002683 if (msp == NULL)
2684 goto out;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002685
Joe Perches28f65c112011-06-09 09:13:32 -07002686 msp->base = ioremap(res->start, resource_size(res));
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002687 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002688 goto out_free;
2689
Lennert Buytenheked944932008-08-26 13:34:19 +02002690 /*
2691 * Set up and register SMI bus.
2692 */
2693 if (pd == NULL || pd->shared_smi == NULL) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002694 msp->smi_bus = mdiobus_alloc();
2695 if (msp->smi_bus == NULL)
Lennert Buytenheked944932008-08-26 13:34:19 +02002696 goto out_unmap;
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002697
2698 msp->smi_bus->priv = msp;
2699 msp->smi_bus->name = "mv643xx_eth smi";
2700 msp->smi_bus->read = smi_bus_read;
2701 msp->smi_bus->write = smi_bus_write,
Florian Fainelli12322432012-01-09 23:59:15 +00002702 snprintf(msp->smi_bus->id, MII_BUS_ID_SIZE, "%s-%d",
2703 pdev->name, pdev->id);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002704 msp->smi_bus->parent = &pdev->dev;
2705 msp->smi_bus->phy_mask = 0xffffffff;
2706 if (mdiobus_register(msp->smi_bus) < 0)
2707 goto out_free_mii_bus;
Lennert Buytenheked944932008-08-26 13:34:19 +02002708 msp->smi = msp;
2709 } else {
Lennert Buytenhekfc0eb9f2008-08-26 12:56:56 +02002710 msp->smi = platform_get_drvdata(pd->shared_smi);
Lennert Buytenheked944932008-08-26 13:34:19 +02002711 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002712
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002713 msp->err_interrupt = NO_IRQ;
2714 init_waitqueue_head(&msp->smi_busy_wait);
2715
2716 /*
2717 * Check whether the error interrupt is hooked up.
2718 */
2719 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2720 if (res != NULL) {
2721 int err;
2722
2723 err = request_irq(res->start, mv643xx_eth_err_irq,
2724 IRQF_SHARED, "mv643xx_eth", msp);
2725 if (!err) {
2726 writel(ERR_INT_SMI_DONE, msp->base + ERR_INT_MASK);
2727 msp->err_interrupt = res->start;
2728 }
2729 }
2730
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002731 /*
2732 * (Re-)program MBUS remapping windows if we are asked to.
2733 */
Andrew Lunn63a93322011-12-07 21:48:07 +01002734 dram = mv_mbus_dram_info();
2735 if (dram)
2736 mv643xx_eth_conf_mbus_windows(msp, dram);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002737
Dan Carpenter50a749c2010-07-23 01:05:05 +00002738 msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2739 pd->tx_csum_limit : 9 * 1024;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002740 infer_hw_params(msp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002741
2742 platform_set_drvdata(pdev, msp);
2743
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002744 return 0;
2745
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002746out_free_mii_bus:
2747 mdiobus_free(msp->smi_bus);
Lennert Buytenheked944932008-08-26 13:34:19 +02002748out_unmap:
2749 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002750out_free:
2751 kfree(msp);
2752out:
2753 return ret;
2754}
2755
2756static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2757{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002758 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenheked944932008-08-26 13:34:19 +02002759 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002760
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002761 if (pd == NULL || pd->shared_smi == NULL) {
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002762 mdiobus_unregister(msp->smi_bus);
Lennert Buytenhekbcb33362008-11-18 04:28:35 +00002763 mdiobus_free(msp->smi_bus);
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002764 }
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002765 if (msp->err_interrupt != NO_IRQ)
2766 free_irq(msp->err_interrupt, msp);
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002767 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002768 kfree(msp);
2769
2770 return 0;
2771}
2772
2773static struct platform_driver mv643xx_eth_shared_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002774 .probe = mv643xx_eth_shared_probe,
2775 .remove = mv643xx_eth_shared_remove,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002776 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002777 .name = MV643XX_ETH_SHARED_NAME,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002778 .owner = THIS_MODULE,
2779 },
2780};
2781
Lennert Buytenheke5371492008-06-01 02:18:13 +02002782static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002783{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002784 int addr_shift = 5 * mp->port_num;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002785 u32 data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002786
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002787 data = rdl(mp, PHY_ADDR);
2788 data &= ~(0x1f << addr_shift);
2789 data |= (phy_addr & 0x1f) << addr_shift;
2790 wrl(mp, PHY_ADDR, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002791}
2792
Lennert Buytenheke5371492008-06-01 02:18:13 +02002793static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002794{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002795 unsigned int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002796
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002797 data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002798
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002799 return (data >> (5 * mp->port_num)) & 0x1f;
2800}
2801
2802static void set_params(struct mv643xx_eth_private *mp,
2803 struct mv643xx_eth_platform_data *pd)
2804{
2805 struct net_device *dev = mp->dev;
2806
2807 if (is_valid_ether_addr(pd->mac_addr))
2808 memcpy(dev->dev_addr, pd->mac_addr, 6);
2809 else
2810 uc_addr_get(mp, dev->dev_addr);
2811
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002812 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002813 if (pd->rx_queue_size)
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002814 mp->rx_ring_size = pd->rx_queue_size;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002815 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2816 mp->rx_desc_sram_size = pd->rx_sram_size;
2817
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002818 mp->rxq_count = pd->rx_queue_count ? : 1;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002819
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002820 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002821 if (pd->tx_queue_size)
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002822 mp->tx_ring_size = pd->tx_queue_size;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002823 mp->tx_desc_sram_addr = pd->tx_sram_addr;
2824 mp->tx_desc_sram_size = pd->tx_sram_size;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002825
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002826 mp->txq_count = pd->tx_queue_count ? : 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002827}
2828
Lennert Buytenheked944932008-08-26 13:34:19 +02002829static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2830 int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002831{
Lennert Buytenhek298cf9b2008-10-08 16:29:57 -07002832 struct mii_bus *bus = mp->shared->smi->smi_bus;
Lennert Buytenheked944932008-08-26 13:34:19 +02002833 struct phy_device *phydev;
2834 int start;
2835 int num;
2836 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002837
Lennert Buytenheked944932008-08-26 13:34:19 +02002838 if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2839 start = phy_addr_get(mp) & 0x1f;
2840 num = 32;
2841 } else {
2842 start = phy_addr & 0x1f;
2843 num = 1;
2844 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002845
Lennert Buytenheked944932008-08-26 13:34:19 +02002846 phydev = NULL;
2847 for (i = 0; i < num; i++) {
2848 int addr = (start + i) & 0x1f;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002849
Lennert Buytenheked944932008-08-26 13:34:19 +02002850 if (bus->phy_map[addr] == NULL)
2851 mdiobus_scan(bus, addr);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002852
Lennert Buytenheked944932008-08-26 13:34:19 +02002853 if (phydev == NULL) {
2854 phydev = bus->phy_map[addr];
2855 if (phydev != NULL)
2856 phy_addr_set(mp, addr);
2857 }
2858 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002859
Lennert Buytenheked944932008-08-26 13:34:19 +02002860 return phydev;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002861}
2862
Lennert Buytenheked944932008-08-26 13:34:19 +02002863static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
James Chapmand0412d92006-01-27 01:15:30 -07002864{
Lennert Buytenheked944932008-08-26 13:34:19 +02002865 struct phy_device *phy = mp->phy;
James Chapmand0412d92006-01-27 01:15:30 -07002866
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002867 phy_reset(mp);
2868
Florian Fainellif9a8f832013-01-14 00:52:52 +00002869 phy_attach(mp->dev, dev_name(&phy->dev), PHY_INTERFACE_MODE_GMII);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002870
Lennert Buytenheked944932008-08-26 13:34:19 +02002871 if (speed == 0) {
2872 phy->autoneg = AUTONEG_ENABLE;
2873 phy->speed = 0;
2874 phy->duplex = 0;
2875 phy->advertising = phy->supported | ADVERTISED_Autoneg;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002876 } else {
Lennert Buytenheked944932008-08-26 13:34:19 +02002877 phy->autoneg = AUTONEG_DISABLE;
2878 phy->advertising = 0;
2879 phy->speed = speed;
2880 phy->duplex = duplex;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002881 }
Lennert Buytenheked944932008-08-26 13:34:19 +02002882 phy_start_aneg(phy);
James Chapmand0412d92006-01-27 01:15:30 -07002883}
2884
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002885static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2886{
2887 u32 pscr;
2888
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002889 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002890 if (pscr & SERIAL_PORT_ENABLE) {
2891 pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002892 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002893 }
2894
2895 pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
Lennert Buytenheked944932008-08-26 13:34:19 +02002896 if (mp->phy == NULL) {
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002897 pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2898 if (speed == SPEED_1000)
2899 pscr |= SET_GMII_SPEED_TO_1000;
2900 else if (speed == SPEED_100)
2901 pscr |= SET_MII_SPEED_TO_100;
2902
2903 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2904
2905 pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2906 if (duplex == DUPLEX_FULL)
2907 pscr |= SET_FULL_DUPLEX_MODE;
2908 }
2909
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002910 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002911}
2912
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002913static const struct net_device_ops mv643xx_eth_netdev_ops = {
2914 .ndo_open = mv643xx_eth_open,
2915 .ndo_stop = mv643xx_eth_stop,
2916 .ndo_start_xmit = mv643xx_eth_xmit,
2917 .ndo_set_rx_mode = mv643xx_eth_set_rx_mode,
2918 .ndo_set_mac_address = mv643xx_eth_set_mac_address,
kirjanov@gmail.com1d4bd942010-02-16 20:40:11 +00002919 .ndo_validate_addr = eth_validate_addr,
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002920 .ndo_do_ioctl = mv643xx_eth_ioctl,
2921 .ndo_change_mtu = mv643xx_eth_change_mtu,
Michał Mirosławaad59c42011-04-17 00:15:46 +00002922 .ndo_set_features = mv643xx_eth_set_features,
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002923 .ndo_tx_timeout = mv643xx_eth_tx_timeout,
2924 .ndo_get_stats = mv643xx_eth_get_stats,
2925#ifdef CONFIG_NET_POLL_CONTROLLER
2926 .ndo_poll_controller = mv643xx_eth_netpoll,
2927#endif
2928};
2929
Russell King3ae5eae2005-11-09 22:32:44 +00002930static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 struct mv643xx_eth_platform_data *pd;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002933 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 struct resource *res;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002936 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002938 pd = pdev->dev.platform_data;
2939 if (pd == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002940 dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002941 return -ENODEV;
2942 }
2943
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002944 if (pd->shared == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002945 dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002946 return -ENODEV;
2947 }
2948
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002949 dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 if (!dev)
2951 return -ENOMEM;
2952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002954 platform_set_drvdata(pdev, mp);
2955
2956 mp->shared = platform_get_drvdata(pd->shared);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002957 mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002958 mp->port_num = pd->port_number;
2959
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002960 mp->dev = dev;
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002961
Andrew Lunn452503e2011-12-24 01:24:24 +01002962 /*
Andrew Lunn9a43a022012-06-06 06:40:43 +00002963 * Start with a default rate, and if there is a clock, allow
2964 * it to override the default.
Andrew Lunn452503e2011-12-24 01:24:24 +01002965 */
Andrew Lunn9a43a022012-06-06 06:40:43 +00002966 mp->t_clk = 133000000;
2967#if defined(CONFIG_HAVE_CLK)
Andrew Lunn452503e2011-12-24 01:24:24 +01002968 mp->clk = clk_get(&pdev->dev, (pdev->id ? "1" : "0"));
2969 if (!IS_ERR(mp->clk)) {
2970 clk_prepare_enable(mp->clk);
2971 mp->t_clk = clk_get_rate(mp->clk);
Andrew Lunn452503e2011-12-24 01:24:24 +01002972 }
Andrew Lunn9a43a022012-06-06 06:40:43 +00002973#endif
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002974 set_params(mp, pd);
Ben Hutchings206d6b32010-09-27 08:30:05 +00002975 netif_set_real_num_tx_queues(dev, mp->txq_count);
2976 netif_set_real_num_rx_queues(dev, mp->rxq_count);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002977
Lennert Buytenheked944932008-08-26 13:34:19 +02002978 if (pd->phy_addr != MV643XX_ETH_PHY_NONE)
2979 mp->phy = phy_scan(mp, pd->phy_addr);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002980
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00002981 if (mp->phy != NULL)
Lennert Buytenheked944932008-08-26 13:34:19 +02002982 phy_init(mp, pd->speed, pd->duplex);
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00002983
2984 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Lennert Buytenheked944932008-08-26 13:34:19 +02002985
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002986 init_pscr(mp, pd->speed, pd->duplex);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002987
Lennert Buytenhek4ff34952008-09-19 05:04:57 +02002988
2989 mib_counters_clear(mp);
2990
2991 init_timer(&mp->mib_counters_timer);
2992 mp->mib_counters_timer.data = (unsigned long)mp;
2993 mp->mib_counters_timer.function = mib_counters_timer_wrapper;
2994 mp->mib_counters_timer.expires = jiffies + 30 * HZ;
2995 add_timer(&mp->mib_counters_timer);
2996
2997 spin_lock_init(&mp->mib_counters_lock);
2998
2999 INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
3000
Lennert Buytenhek2257e052008-08-24 04:33:36 +02003001 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
3002
3003 init_timer(&mp->rx_oom);
3004 mp->rx_oom.data = (unsigned long)mp;
3005 mp->rx_oom.function = oom_timer_wrapper;
3006
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
3009 BUG_ON(!res);
3010 dev->irq = res->start;
3011
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00003012 dev->netdev_ops = &mv643xx_eth_netdev_ops;
3013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 dev->base_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
Michał Mirosławaad59c42011-04-17 00:15:46 +00003017 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM |
3018 NETIF_F_RXCSUM | NETIF_F_LRO;
3019 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Lennert Buytenheke32b6612008-07-24 06:22:59 +02003020 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
James Chapmanc28a4f82006-01-27 01:13:15 -07003021
Jiri Pirko01789342011-08-16 06:29:00 +00003022 dev->priv_flags |= IFF_UNICAST_FLT;
3023
Olaf Heringb0b8dab2006-04-27 18:23:49 -07003024 SET_NETDEV_DEV(dev, &pdev->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003025
3026 if (mp->shared->win_protect)
3027 wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
3028
Lennert Buytenheka5fe3612009-02-24 15:41:47 +00003029 netif_carrier_off(dev);
3030
Lennert Buytenhekb5e86db2009-04-08 15:42:46 -07003031 wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
3032
Lennert Buytenhek4fb0a542009-02-24 15:42:05 +00003033 set_rx_coal(mp, 250);
Lennert Buytenheka5fe3612009-02-24 15:41:47 +00003034 set_tx_coal(mp, 0);
3035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 err = register_netdev(dev);
3037 if (err)
3038 goto out;
3039
Joe Perches7542db82011-03-02 17:50:35 +00003040 netdev_notice(dev, "port %d with MAC address %pM\n",
3041 mp->port_num, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Lennert Buytenhek13d64282008-06-01 20:51:22 +02003043 if (mp->tx_desc_sram_size > 0)
Joe Perches7542db82011-03-02 17:50:35 +00003044 netdev_notice(dev, "configured with sram\n");
Nicolas DETb1529872005-10-28 17:46:30 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 return 0;
3047
3048out:
Simon Baatzbaffab22012-07-19 00:04:09 +02003049#if defined(CONFIG_HAVE_CLK)
3050 if (!IS_ERR(mp->clk)) {
3051 clk_disable_unprepare(mp->clk);
3052 clk_put(mp->clk);
3053 }
3054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 free_netdev(dev);
3056
3057 return err;
3058}
3059
Russell King3ae5eae2005-11-09 22:32:44 +00003060static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003062 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003064 unregister_netdev(mp->dev);
Lennert Buytenheked944932008-08-26 13:34:19 +02003065 if (mp->phy != NULL)
3066 phy_detach(mp->phy);
Tejun Heo23f333a2010-12-12 16:45:14 +01003067 cancel_work_sync(&mp->tx_timeout_task);
Andrew Lunn452503e2011-12-24 01:24:24 +01003068
Andrew Lunn9a43a022012-06-06 06:40:43 +00003069#if defined(CONFIG_HAVE_CLK)
Andrew Lunn452503e2011-12-24 01:24:24 +01003070 if (!IS_ERR(mp->clk)) {
3071 clk_disable_unprepare(mp->clk);
3072 clk_put(mp->clk);
3073 }
Andrew Lunn9a43a022012-06-06 06:40:43 +00003074#endif
3075
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003076 free_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Russell King3ae5eae2005-11-09 22:32:44 +00003078 platform_set_drvdata(pdev, NULL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 return 0;
3081}
3082
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003083static void mv643xx_eth_shutdown(struct platform_device *pdev)
3084{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003085 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003086
3087 /* Mask all interrupts on ethernet port */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08003088 wrlp(mp, INT_MASK, 0);
3089 rdlp(mp, INT_MASK);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003090
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003091 if (netif_running(mp->dev))
3092 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003093}
3094
Russell King3ae5eae2005-11-09 22:32:44 +00003095static struct platform_driver mv643xx_eth_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003096 .probe = mv643xx_eth_probe,
3097 .remove = mv643xx_eth_remove,
3098 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00003099 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003100 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07003101 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003102 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103};
3104
Lennert Buytenheke5371492008-06-01 02:18:13 +02003105static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106{
3107 int rc;
3108
Russell King3ae5eae2005-11-09 22:32:44 +00003109 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003111 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003113 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return rc;
3117}
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003118module_init(mv643xx_eth_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119
Lennert Buytenheke5371492008-06-01 02:18:13 +02003120static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Russell King3ae5eae2005-11-09 22:32:44 +00003122 platform_driver_unregister(&mv643xx_eth_driver);
3123 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
Lennert Buytenheke5371492008-06-01 02:18:13 +02003125module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Lennert Buytenhek45675bc2008-06-02 02:02:30 +02003127MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
3128 "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003130MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07003131MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02003132MODULE_ALIAS("platform:" MV643XX_ETH_NAME);