blob: d0afeea181fb0fd35918941c6e19a0008ded1141 [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>
Sebastian Hesselbarth3619eb82013-04-11 23:20:00 +000059#include <linux/interrupt.h>
Lennert Buytenhek10a99482008-11-20 03:57:16 -080060#include <linux/types.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Andrew Lunn452503e2011-12-24 01:24:24 +010062#include <linux/clk.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020063
Lennert Buytenheke5371492008-06-01 02:18:13 +020064static char mv643xx_eth_driver_name[] = "mv643xx_eth";
Lennert Buytenhek042af532008-08-24 09:01:57 +020065static char mv643xx_eth_driver_version[] = "1.4";
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020066
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020067
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020068/*
69 * Registers shared between all ports.
70 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020071#define PHY_ADDR 0x0000
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020072#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
73#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
74#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
75#define WINDOW_BAR_ENABLE 0x0290
76#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020077
78/*
Lennert Buytenhek37a60842008-11-20 03:57:36 -080079 * Main per-port registers. These live at offset 0x0400 for
80 * port #0, 0x0800 for port #1, and 0x0c00 for port #2.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020081 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -080082#define PORT_CONFIG 0x0000
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020083#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek37a60842008-11-20 03:57:36 -080084#define PORT_CONFIG_EXT 0x0004
85#define MAC_ADDR_LOW 0x0014
86#define MAC_ADDR_HIGH 0x0018
87#define SDMA_CONFIG 0x001c
Lennert Buytenhekbecfad92009-05-06 03:01:17 +000088#define TX_BURST_SIZE_16_64BIT 0x01000000
89#define TX_BURST_SIZE_4_64BIT 0x00800000
90#define BLM_TX_NO_SWAP 0x00000020
91#define BLM_RX_NO_SWAP 0x00000010
92#define RX_BURST_SIZE_16_64BIT 0x00000008
93#define RX_BURST_SIZE_4_64BIT 0x00000004
Lennert Buytenhek37a60842008-11-20 03:57:36 -080094#define PORT_SERIAL_CONTROL 0x003c
Lennert Buytenhekbecfad92009-05-06 03:01:17 +000095#define SET_MII_SPEED_TO_100 0x01000000
96#define SET_GMII_SPEED_TO_1000 0x00800000
97#define SET_FULL_DUPLEX_MODE 0x00200000
98#define MAX_RX_PACKET_9700BYTE 0x000a0000
99#define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000
100#define DO_NOT_FORCE_LINK_FAIL 0x00000400
101#define SERIAL_PORT_CONTROL_RESERVED 0x00000200
102#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008
103#define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004
104#define FORCE_LINK_PASS 0x00000002
105#define SERIAL_PORT_ENABLE 0x00000001
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800106#define PORT_STATUS 0x0044
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200107#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhekae9ae062008-07-15 02:15:24 +0200108#define TX_IN_PROGRESS 0x00000080
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +0200109#define PORT_SPEED_MASK 0x00000030
110#define PORT_SPEED_1000 0x00000010
111#define PORT_SPEED_100 0x00000020
112#define PORT_SPEED_10 0x00000000
113#define FLOW_CONTROL_ENABLED 0x00000008
114#define FULL_DUPLEX 0x00000004
Lennert Buytenhek81600eea92008-07-14 14:29:40 +0200115#define LINK_UP 0x00000002
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800116#define TXQ_COMMAND 0x0048
117#define TXQ_FIX_PRIO_CONF 0x004c
118#define TX_BW_RATE 0x0050
119#define TX_BW_MTU 0x0058
120#define TX_BW_BURST 0x005c
121#define INT_CAUSE 0x0060
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200122#define INT_TX_END 0x07f80000
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000123#define INT_TX_END_0 0x00080000
Lennert Buytenhekbefefe22008-08-28 06:10:04 +0200124#define INT_RX 0x000003fc
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000125#define INT_RX_0 0x00000004
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200126#define INT_EXT 0x00000002
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800127#define INT_CAUSE_EXT 0x0064
Lennert Buytenhekbefefe22008-08-28 06:10:04 +0200128#define INT_EXT_LINK_PHY 0x00110000
129#define INT_EXT_TX 0x000000ff
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800130#define INT_MASK 0x0068
131#define INT_MASK_EXT 0x006c
132#define TX_FIFO_URGENT_THRESHOLD 0x0074
Paulius Zaleckas302476c2012-01-23 01:16:35 +0000133#define RX_DISCARD_FRAME_CNT 0x0084
134#define RX_OVERRUN_FRAME_CNT 0x0088
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800135#define TXQ_FIX_PRIO_CONF_MOVED 0x00dc
136#define TX_BW_RATE_MOVED 0x00e0
137#define TX_BW_MTU_MOVED 0x00e8
138#define TX_BW_BURST_MOVED 0x00ec
139#define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4))
140#define RXQ_COMMAND 0x0280
141#define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2))
142#define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4))
143#define TXQ_BW_CONF(q) (0x0304 + ((q) << 4))
144#define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4))
145
146/*
147 * Misc per-port registers.
148 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200149#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
150#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
151#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
152#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200153
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200154
155/*
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000156 * SDMA configuration register default value.
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200157 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200158#if defined(__BIG_ENDIAN)
159#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenheke0c6ef92009-01-19 17:16:00 -0800160 (RX_BURST_SIZE_4_64BIT | \
161 TX_BURST_SIZE_4_64BIT)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200162#elif defined(__LITTLE_ENDIAN)
163#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenheke0c6ef92009-01-19 17:16:00 -0800164 (RX_BURST_SIZE_4_64BIT | \
165 BLM_RX_NO_SWAP | \
166 BLM_TX_NO_SWAP | \
167 TX_BURST_SIZE_4_64BIT)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200168#else
169#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
170#endif
171
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200172
173/*
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000174 * Misc definitions.
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200175 */
Lennert Buytenhekbecfad92009-05-06 03:01:17 +0000176#define DEFAULT_RX_QUEUE_SIZE 128
177#define DEFAULT_TX_QUEUE_SIZE 256
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +0000178#define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200180
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200181/*
182 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200183 */
184#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200185struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200186 u16 byte_cnt; /* Descriptor buffer byte count */
187 u16 buf_size; /* Buffer size */
188 u32 cmd_sts; /* Descriptor command status */
189 u32 next_desc_ptr; /* Next descriptor pointer */
190 u32 buf_ptr; /* Descriptor buffer pointer */
191};
192
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200193struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200194 u16 byte_cnt; /* buffer byte count */
195 u16 l4i_chk; /* CPU provided TCP checksum */
196 u32 cmd_sts; /* Command/status field */
197 u32 next_desc_ptr; /* Pointer to next descriptor */
198 u32 buf_ptr; /* pointer to buffer for this descriptor*/
199};
200#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200201struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200202 u32 cmd_sts; /* Descriptor command status */
203 u16 buf_size; /* Buffer size */
204 u16 byte_cnt; /* Descriptor buffer byte count */
205 u32 buf_ptr; /* Descriptor buffer pointer */
206 u32 next_desc_ptr; /* Next descriptor pointer */
207};
208
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200209struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200210 u32 cmd_sts; /* Command/status field */
211 u16 l4i_chk; /* CPU provided TCP checksum */
212 u16 byte_cnt; /* buffer byte count */
213 u32 buf_ptr; /* pointer to buffer for this descriptor*/
214 u32 next_desc_ptr; /* Pointer to next descriptor */
215};
216#else
217#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
218#endif
219
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200220/* RX & TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200221#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200222
223/* RX & TX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200224#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200225
226/* RX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200227#define LAYER_4_CHECKSUM_OK 0x40000000
228#define RX_ENABLE_INTERRUPT 0x20000000
229#define RX_FIRST_DESC 0x08000000
230#define RX_LAST_DESC 0x04000000
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000231#define RX_IP_HDR_OK 0x02000000
232#define RX_PKT_IS_IPV4 0x01000000
233#define RX_PKT_IS_ETHERNETV2 0x00800000
234#define RX_PKT_LAYER4_TYPE_MASK 0x00600000
235#define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000
236#define RX_PKT_IS_VLAN_TAGGED 0x00080000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200237
238/* TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200239#define TX_ENABLE_INTERRUPT 0x00800000
240#define GEN_CRC 0x00400000
241#define TX_FIRST_DESC 0x00200000
242#define TX_LAST_DESC 0x00100000
243#define ZERO_PADDING 0x00080000
244#define GEN_IP_V4_CHECKSUM 0x00040000
245#define GEN_TCP_UDP_CHECKSUM 0x00020000
246#define UDP_FRAME 0x00010000
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200247#define MAC_HDR_EXTRA_4_BYTES 0x00008000
248#define MAC_HDR_EXTRA_8_BYTES 0x00000200
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200249
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200250#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200251
252
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200253/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200254struct mv643xx_eth_shared_private {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200255 /*
256 * Ethernet controller base address.
257 */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200258 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200259
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200260 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200261 * Per-port MBUS window access register value.
262 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200263 u32 win_protect;
264
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200265 /*
266 * Hardware-specific parameters.
267 */
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +0200268 int extended_rx_coal_limit;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200269 int tx_bw_control;
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000270 int tx_csum_limit;
Sebastian Hesselbarth20922482013-04-10 23:29:33 +0000271 struct clk *clk;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200272};
273
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200274#define TX_BW_CONTROL_ABSENT 0
275#define TX_BW_CONTROL_OLD_LAYOUT 1
276#define TX_BW_CONTROL_NEW_LAYOUT 2
277
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000278static int mv643xx_eth_open(struct net_device *dev);
279static int mv643xx_eth_stop(struct net_device *dev);
280
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200281
282/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200283struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200284 u64 good_octets_received;
285 u32 bad_octets_received;
286 u32 internal_mac_transmit_err;
287 u32 good_frames_received;
288 u32 bad_frames_received;
289 u32 broadcast_frames_received;
290 u32 multicast_frames_received;
291 u32 frames_64_octets;
292 u32 frames_65_to_127_octets;
293 u32 frames_128_to_255_octets;
294 u32 frames_256_to_511_octets;
295 u32 frames_512_to_1023_octets;
296 u32 frames_1024_to_max_octets;
297 u64 good_octets_sent;
298 u32 good_frames_sent;
299 u32 excessive_collision;
300 u32 multicast_frames_sent;
301 u32 broadcast_frames_sent;
302 u32 unrec_mac_control_received;
303 u32 fc_sent;
304 u32 good_fc_received;
305 u32 bad_fc_received;
306 u32 undersize_received;
307 u32 fragments_received;
308 u32 oversize_received;
309 u32 jabber_received;
310 u32 mac_receive_error;
311 u32 bad_crc_event;
312 u32 collision;
313 u32 late_collision;
Paulius Zaleckas302476c2012-01-23 01:16:35 +0000314 /* Non MIB hardware counters */
315 u32 rx_discard;
316 u32 rx_overrun;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200317};
318
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200319struct rx_queue {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200320 int index;
321
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200322 int rx_ring_size;
323
324 int rx_desc_count;
325 int rx_curr_desc;
326 int rx_used_desc;
327
328 struct rx_desc *rx_desc_area;
329 dma_addr_t rx_desc_dma;
330 int rx_desc_area_size;
331 struct sk_buff **rx_skb;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200332};
333
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200334struct tx_queue {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200335 int index;
336
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200337 int tx_ring_size;
338
339 int tx_desc_count;
340 int tx_curr_desc;
341 int tx_used_desc;
342
343 struct tx_desc *tx_desc_area;
344 dma_addr_t tx_desc_dma;
345 int tx_desc_area_size;
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200346
347 struct sk_buff_head tx_skb;
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200348
349 unsigned long tx_packets;
350 unsigned long tx_bytes;
351 unsigned long tx_dropped;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200352};
353
Lennert Buytenheke5371492008-06-01 02:18:13 +0200354struct mv643xx_eth_private {
355 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800356 void __iomem *base;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200357 int port_num;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200358
359 struct net_device *dev;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200360
Lennert Buytenheked944932008-08-26 13:34:19 +0200361 struct phy_device *phy;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200362
Lennert Buytenhek4ff34952008-09-19 05:04:57 +0200363 struct timer_list mib_counters_timer;
364 spinlock_t mib_counters_lock;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200365 struct mib_counters mib_counters;
Lennert Buytenhek4ff34952008-09-19 05:04:57 +0200366
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200367 struct work_struct tx_timeout_task;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200368
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200369 struct napi_struct napi;
Saeed Bisharae0ca8412009-05-06 03:02:01 +0000370 u32 int_mask;
Lennert Buytenhek1319eba2009-04-29 11:57:34 +0000371 u8 oom;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200372 u8 work_link;
373 u8 work_tx;
374 u8 work_tx_end;
375 u8 work_rx;
376 u8 work_rx_refill;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200377
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700378 int skb_size;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700379
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200380 /*
381 * RX state.
382 */
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000383 int rx_ring_size;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200384 unsigned long rx_desc_sram_addr;
385 int rx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200386 int rxq_count;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200387 struct timer_list rx_oom;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200388 struct rx_queue rxq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200389
390 /*
391 * TX state.
392 */
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +0000393 int tx_ring_size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200394 unsigned long tx_desc_sram_addr;
395 int tx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200396 int txq_count;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200397 struct tx_queue txq[8];
Andrew Lunn452503e2011-12-24 01:24:24 +0100398
399 /*
400 * Hardware-specific parameters.
401 */
402 struct clk *clk;
403 unsigned int t_clk;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200404};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200406
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200407/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200408static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200410 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800413static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset)
414{
415 return readl(mp->base + offset);
416}
417
Lennert Buytenheke5371492008-06-01 02:18:13 +0200418static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200420 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800423static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data)
424{
425 writel(data, mp->base + offset);
426}
427
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200428
429/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200430static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200432 return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200433}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200435static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
436{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200437 return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200438}
439
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200440static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200441{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200442 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800443 wrlp(mp, RXQ_COMMAND, 1 << rxq->index);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200444}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700445
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200446static void rxq_disable(struct rx_queue *rxq)
447{
448 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200449 u8 mask = 1 << rxq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200450
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800451 wrlp(mp, RXQ_COMMAND, mask << 8);
452 while (rdlp(mp, RXQ_COMMAND) & mask)
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200453 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200454}
455
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200456static void txq_reset_hw_ptr(struct tx_queue *txq)
457{
458 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200459 u32 addr;
460
461 addr = (u32)txq->tx_desc_dma;
462 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800463 wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200464}
465
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200466static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200467{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200468 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800469 wrlp(mp, TXQ_COMMAND, 1 << txq->index);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200470}
471
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200472static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200473{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200474 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200475 u8 mask = 1 << txq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200476
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800477 wrlp(mp, TXQ_COMMAND, mask << 8);
478 while (rdlp(mp, TXQ_COMMAND) & mask)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200479 udelay(10);
480}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200481
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200482static void txq_maybe_wake(struct tx_queue *txq)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200483{
484 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200485 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200486
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200487 if (netif_tx_queue_stopped(nq)) {
488 __netif_tx_lock(nq, smp_processor_id());
489 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
490 netif_tx_wake_queue(nq);
491 __netif_tx_unlock(nq);
492 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200493}
494
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200495static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200497 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
498 struct net_device_stats *stats = &mp->dev->stats;
499 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200501 rx = 0;
Lennert Buytenhek9e1f3772008-08-24 02:33:47 +0200502 while (rx < budget && rxq->rx_desc_count) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200503 struct rx_desc *rx_desc;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200504 unsigned int cmd_sts;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200505 struct sk_buff *skb;
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200506 u16 byte_cnt;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200507
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200508 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200509
510 cmd_sts = rx_desc->cmd_sts;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200511 if (cmd_sts & BUFFER_OWNED_BY_DMA)
Lennert Buytenhek96587662008-06-01 10:31:56 +0200512 break;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200513 rmb();
514
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200515 skb = rxq->rx_skb[rxq->rx_curr_desc];
516 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200517
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200518 rxq->rx_curr_desc++;
519 if (rxq->rx_curr_desc == rxq->rx_ring_size)
520 rxq->rx_curr_desc = 0;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200521
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700522 dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr,
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200523 rx_desc->buf_size, DMA_FROM_DEVICE);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200524 rxq->rx_desc_count--;
525 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700526
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200527 mp->work_rx_refill |= 1 << rxq->index;
528
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200529 byte_cnt = rx_desc->byte_cnt;
530
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700531 /*
532 * Update statistics.
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200533 *
534 * Note that the descriptor byte count includes 2 dummy
535 * bytes automatically inserted by the hardware at the
536 * start of the packet (which we don't count), and a 4
537 * byte CRC at the end of the packet (which we do count).
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 stats->rx_packets++;
Lennert Buytenhek6b8f90c2008-09-14 15:50:32 +0200540 stats->rx_bytes += byte_cnt - 2;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200543 * In case we received a packet without first / last bits
544 * on, or the error summary bit is set, the packet needs
545 * to be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 */
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800547 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY))
548 != (RX_FIRST_DESC | RX_LAST_DESC))
549 goto err;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200550
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800551 /*
552 * The -4 is for the CRC in the trailer of the
553 * received packet
554 */
555 skb_put(skb, byte_cnt - 2 - 4);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200556
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800557 if (cmd_sts & LAYER_4_CHECKSUM_OK)
558 skb->ip_summed = CHECKSUM_UNNECESSARY;
559 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenhekeaf5d592009-02-12 14:08:39 +0000560
Sebastian Hesselbarth3619eb82013-04-11 23:20:00 +0000561 napi_gro_receive(&mp->napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800563 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800565err:
566 stats->rx_dropped++;
567
568 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
569 (RX_FIRST_DESC | RX_LAST_DESC)) {
570 if (net_ratelimit())
Joe Perches7542db82011-03-02 17:50:35 +0000571 netdev_err(mp->dev,
572 "received packet spanning multiple descriptors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Lennert Buytenhekf61e5542008-11-20 03:58:46 -0800574
575 if (cmd_sts & ERROR_SUMMARY)
576 stats->rx_errors++;
577
578 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200580
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200581 if (rx < budget)
582 mp->work_rx &= ~(1 << rxq->index);
583
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200584 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585}
586
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200587static int rxq_refill(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200589 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200590 int refilled;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200591
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200592 refilled = 0;
593 while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
594 struct sk_buff *skb;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200595 int rx;
Lennert Buytenhek53771522008-11-20 03:59:04 -0800596 struct rx_desc *rx_desc;
Saeed Bishara530e5572010-01-05 09:15:32 +0000597 int size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Eric Dumazetacb600d2012-10-05 06:23:55 +0000599 skb = netdev_alloc_skb(mp->dev, mp->skb_size);
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -0700600
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200601 if (skb == NULL) {
Lennert Buytenhek1319eba2009-04-29 11:57:34 +0000602 mp->oom = 1;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200603 goto oom;
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +0000606 if (SKB_DMA_REALIGN)
607 skb_reserve(skb, SKB_DMA_REALIGN);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200608
609 refilled++;
610 rxq->rx_desc_count++;
611
612 rx = rxq->rx_used_desc++;
613 if (rxq->rx_used_desc == rxq->rx_ring_size)
614 rxq->rx_used_desc = 0;
615
Lennert Buytenhek53771522008-11-20 03:59:04 -0800616 rx_desc = rxq->rx_desc_area + rx;
617
Saeed Bishara530e5572010-01-05 09:15:32 +0000618 size = skb->end - skb->data;
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700619 rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent,
Saeed Bishara530e5572010-01-05 09:15:32 +0000620 skb->data, size,
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700621 DMA_FROM_DEVICE);
Saeed Bishara530e5572010-01-05 09:15:32 +0000622 rx_desc->buf_size = size;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200623 rxq->rx_skb[rx] = skb;
624 wmb();
Lennert Buytenhek53771522008-11-20 03:59:04 -0800625 rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200626 wmb();
627
628 /*
629 * The hardware automatically prepends 2 bytes of
630 * dummy data to each received packet, so that the
631 * IP header ends up 16-byte aligned.
632 */
633 skb_reserve(skb, 2);
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200634 }
635
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200636 if (refilled < budget)
637 mp->work_rx_refill &= ~(1 << rxq->index);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200639oom:
640 return refilled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200643
644/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700645static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
646{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200647 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700648
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700649 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Eric Dumazet9e903e02011-10-18 21:00:24 +0000650 const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
651
652 if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700653 return 1;
654 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200655
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700656 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700657}
658
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200659static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700660{
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700661 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200662 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700663 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700664
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200665 for (frag = 0; frag < nr_frags; frag++) {
666 skb_frag_t *this_frag;
667 int tx_index;
668 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700669
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200670 this_frag = &skb_shinfo(skb)->frags[frag];
Lennert Buytenhek66823b92008-11-20 03:58:09 -0800671 tx_index = txq->tx_curr_desc++;
672 if (txq->tx_curr_desc == txq->tx_ring_size)
673 txq->tx_curr_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200674 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700675
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200676 /*
677 * The last fragment will generate an interrupt
678 * which will free the skb on TX completion.
679 */
680 if (frag == nr_frags - 1) {
681 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
682 ZERO_PADDING | TX_LAST_DESC |
683 TX_ENABLE_INTERRUPT;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200684 } else {
685 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200686 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700687
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700688 desc->l4i_chk = 0;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000689 desc->byte_cnt = skb_frag_size(this_frag);
Ian Campbellf1063582011-08-31 00:46:57 +0000690 desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent,
691 this_frag, 0,
Eric Dumazet9e903e02011-10-18 21:00:24 +0000692 skb_frag_size(this_frag),
Ian Campbellf1063582011-08-31 00:46:57 +0000693 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700694 }
695}
696
Byron Bradley324ff2c2008-02-04 23:47:15 -0800697static inline __be16 sum16_as_be(__sum16 sum)
698{
699 return (__force __be16)sum;
700}
701
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200702static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700703{
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200704 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200705 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700706 int tx_index;
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200707 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700708 u32 cmd_sts;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200709 u16 l4i_chk;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700710 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700711
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200712 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200713 l4i_chk = 0;
714
715 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000716 int hdr_len;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200717 int tag_bytes;
718
719 BUG_ON(skb->protocol != htons(ETH_P_IP) &&
720 skb->protocol != htons(ETH_P_8021Q));
721
Saeed Bishara9b2c2ff2010-06-27 00:26:43 +0000722 hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
723 tag_bytes = hdr_len - ETH_HLEN;
724 if (skb->len - hdr_len > mp->shared->tx_csum_limit ||
725 unlikely(tag_bytes & ~12)) {
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200726 if (skb_checksum_help(skb) == 0)
727 goto no_csum;
728 kfree_skb(skb);
729 return 1;
730 }
731
732 if (tag_bytes & 4)
733 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
734 if (tag_bytes & 8)
735 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
736
737 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
738 GEN_IP_V4_CHECKSUM |
739 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
740
741 switch (ip_hdr(skb)->protocol) {
742 case IPPROTO_UDP:
743 cmd_sts |= UDP_FRAME;
744 l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
745 break;
746 case IPPROTO_TCP:
747 l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
748 break;
749 default:
750 BUG();
751 }
752 } else {
753no_csum:
754 /* Errata BTS #50, IHL must be 5 if no HW checksum */
755 cmd_sts |= 5 << TX_IHL_SHIFT;
756 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700757
Lennert Buytenhek66823b92008-11-20 03:58:09 -0800758 tx_index = txq->tx_curr_desc++;
759 if (txq->tx_curr_desc == txq->tx_ring_size)
760 txq->tx_curr_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200761 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700762
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700763 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200764 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700765 length = skb_headlen(skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700766 } else {
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200767 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700768 length = skb->len;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700769 }
770
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200771 desc->l4i_chk = l4i_chk;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700772 desc->byte_cnt = length;
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700773 desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data,
774 length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700775
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200776 __skb_queue_tail(&txq->tx_skb, skb);
777
Richard Cochran3b182d72011-06-21 16:01:11 -0700778 skb_tx_timestamp(skb);
779
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700780 /* ensure all other descriptors are written before first cmd_sts */
781 wmb();
782 desc->cmd_sts = cmd_sts;
783
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200784 /* clear TX_END status */
785 mp->work_tx_end &= ~(1 << txq->index);
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200786
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700787 /* ensure all descriptors are written before poking hardware */
788 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200789 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700790
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200791 txq->tx_desc_count += nr_frags + 1;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200792
793 return 0;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700794}
795
Denis Kirjanov0ccfe642009-11-29 17:04:31 -0800796static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200798 struct mv643xx_eth_private *mp = netdev_priv(dev);
Richard Cochran73151ce2011-06-21 16:00:24 -0700799 int length, queue;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200800 struct tx_queue *txq;
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200801 struct netdev_queue *nq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200803 queue = skb_get_queue_mapping(skb);
804 txq = mp->txq + queue;
805 nq = netdev_get_tx_queue(dev, queue);
806
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700807 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200808 txq->tx_dropped++;
Joe Perches7542db82011-03-02 17:50:35 +0000809 netdev_printk(KERN_DEBUG, dev,
810 "failed to linearize skb with tiny unaligned fragment\n");
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700811 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Lennert Buytenhek17cd0a52008-08-24 05:33:55 +0200814 if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +0200815 if (net_ratelimit())
Joe Perches7542db82011-03-02 17:50:35 +0000816 netdev_err(dev, "tx queue full?!\n");
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200817 kfree_skb(skb);
818 return NETDEV_TX_OK;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700819 }
820
Richard Cochran73151ce2011-06-21 16:00:24 -0700821 length = skb->len;
822
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200823 if (!txq_submit_skb(txq, skb)) {
824 int entries_left;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Richard Cochran73151ce2011-06-21 16:00:24 -0700826 txq->tx_bytes += length;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200827 txq->tx_packets++;
Lennert Buytenhek4df89bd2008-09-19 04:05:00 +0200828
829 entries_left = txq->tx_ring_size - txq->tx_desc_count;
830 if (entries_left < MAX_SKB_FRAGS + 1)
831 netif_tx_stop_queue(nq);
832 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700833
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700834 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200837
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200838/* tx napi ******************************************************************/
839static void txq_kick(struct tx_queue *txq)
840{
841 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200842 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200843 u32 hw_desc_ptr;
844 u32 expected_ptr;
845
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200846 __netif_tx_lock(nq, smp_processor_id());
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200847
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800848 if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index))
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200849 goto out;
850
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800851 hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index));
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200852 expected_ptr = (u32)txq->tx_desc_dma +
853 txq->tx_curr_desc * sizeof(struct tx_desc);
854
855 if (hw_desc_ptr != expected_ptr)
856 txq_enable(txq);
857
858out:
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200859 __netif_tx_unlock(nq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200860
861 mp->work_tx_end &= ~(1 << txq->index);
862}
863
864static int txq_reclaim(struct tx_queue *txq, int budget, int force)
865{
866 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200867 struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200868 int reclaimed;
869
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200870 __netif_tx_lock(nq, smp_processor_id());
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200871
872 reclaimed = 0;
873 while (reclaimed < budget && txq->tx_desc_count > 0) {
874 int tx_index;
875 struct tx_desc *desc;
876 u32 cmd_sts;
877 struct sk_buff *skb;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200878
879 tx_index = txq->tx_used_desc;
880 desc = &txq->tx_desc_area[tx_index];
881 cmd_sts = desc->cmd_sts;
882
883 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
884 if (!force)
885 break;
886 desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
887 }
888
889 txq->tx_used_desc = tx_index + 1;
890 if (txq->tx_used_desc == txq->tx_ring_size)
891 txq->tx_used_desc = 0;
892
893 reclaimed++;
894 txq->tx_desc_count--;
895
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +0200896 skb = NULL;
897 if (cmd_sts & TX_LAST_DESC)
898 skb = __skb_dequeue(&txq->tx_skb);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200899
900 if (cmd_sts & ERROR_SUMMARY) {
Joe Perches7542db82011-03-02 17:50:35 +0000901 netdev_info(mp->dev, "tx error\n");
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200902 mp->dev->stats.tx_errors++;
903 }
904
Lennert Buytenheka4189502008-09-13 04:16:15 +0200905 if (cmd_sts & TX_FIRST_DESC) {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700906 dma_unmap_single(mp->dev->dev.parent, desc->buf_ptr,
Lennert Buytenheka4189502008-09-13 04:16:15 +0200907 desc->byte_cnt, DMA_TO_DEVICE);
908 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -0700909 dma_unmap_page(mp->dev->dev.parent, desc->buf_ptr,
Lennert Buytenheka4189502008-09-13 04:16:15 +0200910 desc->byte_cnt, DMA_TO_DEVICE);
911 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200912
Eric Dumazetacb600d2012-10-05 06:23:55 +0000913 dev_kfree_skb(skb);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200914 }
915
Lennert Buytenhek8fd89212008-08-28 08:26:28 +0200916 __netif_tx_unlock(nq);
917
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200918 if (reclaimed < budget)
919 mp->work_tx &= ~(1 << txq->index);
920
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +0200921 return reclaimed;
922}
923
924
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200925/* tx rate control **********************************************************/
926/*
927 * Set total maximum TX rate (shared by all TX queues for this port)
928 * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
929 */
930static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
931{
932 int token_rate;
933 int mtu;
934 int bucket_size;
935
Andrew Lunn452503e2011-12-24 01:24:24 +0100936 token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200937 if (token_rate > 1023)
938 token_rate = 1023;
939
940 mtu = (mp->dev->mtu + 255) >> 8;
941 if (mtu > 63)
942 mtu = 63;
943
944 bucket_size = (burst + 255) >> 8;
945 if (bucket_size > 65535)
946 bucket_size = 65535;
947
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200948 switch (mp->shared->tx_bw_control) {
949 case TX_BW_CONTROL_OLD_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800950 wrlp(mp, TX_BW_RATE, token_rate);
951 wrlp(mp, TX_BW_MTU, mtu);
952 wrlp(mp, TX_BW_BURST, bucket_size);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200953 break;
954 case TX_BW_CONTROL_NEW_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800955 wrlp(mp, TX_BW_RATE_MOVED, token_rate);
956 wrlp(mp, TX_BW_MTU_MOVED, mtu);
957 wrlp(mp, TX_BW_BURST_MOVED, bucket_size);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200958 break;
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200959 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200960}
961
962static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
963{
964 struct mv643xx_eth_private *mp = txq_to_mp(txq);
965 int token_rate;
966 int bucket_size;
967
Andrew Lunn452503e2011-12-24 01:24:24 +0100968 token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200969 if (token_rate > 1023)
970 token_rate = 1023;
971
972 bucket_size = (burst + 255) >> 8;
973 if (bucket_size > 65535)
974 bucket_size = 65535;
975
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800976 wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14);
977 wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200978}
979
980static void txq_set_fixed_prio_mode(struct tx_queue *txq)
981{
982 struct mv643xx_eth_private *mp = txq_to_mp(txq);
983 int off;
984 u32 val;
985
986 /*
987 * Turn on fixed priority mode.
988 */
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200989 off = 0;
990 switch (mp->shared->tx_bw_control) {
991 case TX_BW_CONTROL_OLD_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800992 off = TXQ_FIX_PRIO_CONF;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200993 break;
994 case TX_BW_CONTROL_NEW_LAYOUT:
Lennert Buytenhek37a60842008-11-20 03:57:36 -0800995 off = TXQ_FIX_PRIO_CONF_MOVED;
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200996 break;
997 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200998
Lennert Buytenhek457b1d52008-09-04 12:22:34 +0200999 if (off) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001000 val = rdlp(mp, off);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001001 val |= 1 << txq->index;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001002 wrlp(mp, off, val);
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02001003 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001004}
1005
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001006
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001007/* mii management interface *************************************************/
Phil Sutter260055b2013-03-06 07:49:02 +00001008static void mv643xx_adjust_pscr(struct mv643xx_eth_private *mp)
1009{
1010 u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
1011 u32 autoneg_disable = FORCE_LINK_PASS |
1012 DISABLE_AUTO_NEG_SPEED_GMII |
1013 DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1014 DISABLE_AUTO_NEG_FOR_DUPLEX;
1015
1016 if (mp->phy->autoneg == AUTONEG_ENABLE) {
1017 /* enable auto negotiation */
1018 pscr &= ~autoneg_disable;
1019 goto out_write;
1020 }
1021
1022 pscr |= autoneg_disable;
1023
1024 if (mp->phy->speed == SPEED_1000) {
1025 /* force gigabit, half duplex not supported */
1026 pscr |= SET_GMII_SPEED_TO_1000;
1027 pscr |= SET_FULL_DUPLEX_MODE;
1028 goto out_write;
1029 }
1030
1031 pscr &= ~SET_GMII_SPEED_TO_1000;
1032
1033 if (mp->phy->speed == SPEED_100)
1034 pscr |= SET_MII_SPEED_TO_100;
1035 else
1036 pscr &= ~SET_MII_SPEED_TO_100;
1037
1038 if (mp->phy->duplex == DUPLEX_FULL)
1039 pscr |= SET_FULL_DUPLEX_MODE;
1040 else
1041 pscr &= ~SET_FULL_DUPLEX_MODE;
1042
1043out_write:
1044 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
1045}
1046
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02001047/* statistics ***************************************************************/
1048static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1049{
1050 struct mv643xx_eth_private *mp = netdev_priv(dev);
1051 struct net_device_stats *stats = &dev->stats;
1052 unsigned long tx_packets = 0;
1053 unsigned long tx_bytes = 0;
1054 unsigned long tx_dropped = 0;
1055 int i;
1056
1057 for (i = 0; i < mp->txq_count; i++) {
1058 struct tx_queue *txq = mp->txq + i;
1059
1060 tx_packets += txq->tx_packets;
1061 tx_bytes += txq->tx_bytes;
1062 tx_dropped += txq->tx_dropped;
1063 }
1064
1065 stats->tx_packets = tx_packets;
1066 stats->tx_bytes = tx_bytes;
1067 stats->tx_dropped = tx_dropped;
1068
1069 return stats;
1070}
1071
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001072static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001073{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001074 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001075}
1076
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001077static void mib_counters_clear(struct mv643xx_eth_private *mp)
1078{
1079 int i;
1080
1081 for (i = 0; i < 0x80; i += 4)
1082 mib_read(mp, i);
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001083
1084 /* Clear non MIB hw counters also */
1085 rdlp(mp, RX_DISCARD_FRAME_CNT);
1086 rdlp(mp, RX_OVERRUN_FRAME_CNT);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001087}
1088
1089static void mib_counters_update(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001090{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001091 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001092
Sebastian Siewior57e8f262009-02-16 11:28:15 +00001093 spin_lock_bh(&mp->mib_counters_lock);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001094 p->good_octets_received += mib_read(mp, 0x00);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001095 p->bad_octets_received += mib_read(mp, 0x08);
1096 p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1097 p->good_frames_received += mib_read(mp, 0x10);
1098 p->bad_frames_received += mib_read(mp, 0x14);
1099 p->broadcast_frames_received += mib_read(mp, 0x18);
1100 p->multicast_frames_received += mib_read(mp, 0x1c);
1101 p->frames_64_octets += mib_read(mp, 0x20);
1102 p->frames_65_to_127_octets += mib_read(mp, 0x24);
1103 p->frames_128_to_255_octets += mib_read(mp, 0x28);
1104 p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1105 p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1106 p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1107 p->good_octets_sent += mib_read(mp, 0x38);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001108 p->good_frames_sent += mib_read(mp, 0x40);
1109 p->excessive_collision += mib_read(mp, 0x44);
1110 p->multicast_frames_sent += mib_read(mp, 0x48);
1111 p->broadcast_frames_sent += mib_read(mp, 0x4c);
1112 p->unrec_mac_control_received += mib_read(mp, 0x50);
1113 p->fc_sent += mib_read(mp, 0x54);
1114 p->good_fc_received += mib_read(mp, 0x58);
1115 p->bad_fc_received += mib_read(mp, 0x5c);
1116 p->undersize_received += mib_read(mp, 0x60);
1117 p->fragments_received += mib_read(mp, 0x64);
1118 p->oversize_received += mib_read(mp, 0x68);
1119 p->jabber_received += mib_read(mp, 0x6c);
1120 p->mac_receive_error += mib_read(mp, 0x70);
1121 p->bad_crc_event += mib_read(mp, 0x74);
1122 p->collision += mib_read(mp, 0x78);
1123 p->late_collision += mib_read(mp, 0x7c);
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001124 /* Non MIB hardware counters */
1125 p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT);
1126 p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT);
Sebastian Siewior57e8f262009-02-16 11:28:15 +00001127 spin_unlock_bh(&mp->mib_counters_lock);
Lennert Buytenhek4ff34952008-09-19 05:04:57 +02001128
1129 mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ);
1130}
1131
1132static void mib_counters_timer_wrapper(unsigned long _mp)
1133{
1134 struct mv643xx_eth_private *mp = (void *)_mp;
1135
1136 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001137}
1138
1139
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001140/* interrupt coalescing *****************************************************/
1141/*
1142 * Hardware coalescing parameters are set in units of 64 t_clk
1143 * cycles. I.e.:
1144 *
1145 * coal_delay_in_usec = 64000000 * register_value / t_clk_rate
1146 *
1147 * register_value = coal_delay_in_usec * t_clk_rate / 64000000
1148 *
1149 * In the ->set*() methods, we round the computed register value
1150 * to the nearest integer.
1151 */
1152static unsigned int get_rx_coal(struct mv643xx_eth_private *mp)
1153{
1154 u32 val = rdlp(mp, SDMA_CONFIG);
1155 u64 temp;
1156
1157 if (mp->shared->extended_rx_coal_limit)
1158 temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7);
1159 else
1160 temp = (val & 0x003fff00) >> 8;
1161
1162 temp *= 64000000;
Andrew Lunn452503e2011-12-24 01:24:24 +01001163 do_div(temp, mp->t_clk);
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001164
1165 return (unsigned int)temp;
1166}
1167
1168static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1169{
1170 u64 temp;
1171 u32 val;
1172
Andrew Lunn452503e2011-12-24 01:24:24 +01001173 temp = (u64)usec * mp->t_clk;
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001174 temp += 31999999;
1175 do_div(temp, 64000000);
1176
1177 val = rdlp(mp, SDMA_CONFIG);
1178 if (mp->shared->extended_rx_coal_limit) {
1179 if (temp > 0xffff)
1180 temp = 0xffff;
1181 val &= ~0x023fff80;
1182 val |= (temp & 0x8000) << 10;
1183 val |= (temp & 0x7fff) << 7;
1184 } else {
1185 if (temp > 0x3fff)
1186 temp = 0x3fff;
1187 val &= ~0x003fff00;
1188 val |= (temp & 0x3fff) << 8;
1189 }
1190 wrlp(mp, SDMA_CONFIG, val);
1191}
1192
1193static unsigned int get_tx_coal(struct mv643xx_eth_private *mp)
1194{
1195 u64 temp;
1196
1197 temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4;
1198 temp *= 64000000;
Andrew Lunn452503e2011-12-24 01:24:24 +01001199 do_div(temp, mp->t_clk);
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001200
1201 return (unsigned int)temp;
1202}
1203
1204static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec)
1205{
1206 u64 temp;
1207
Andrew Lunn452503e2011-12-24 01:24:24 +01001208 temp = (u64)usec * mp->t_clk;
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001209 temp += 31999999;
1210 do_div(temp, 64000000);
1211
1212 if (temp > 0x3fff)
1213 temp = 0x3fff;
1214
1215 wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4);
1216}
1217
1218
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001219/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001220struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001221 char stat_string[ETH_GSTRING_LEN];
1222 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +02001223 int netdev_off;
1224 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001225};
1226
Lennert Buytenhek16820052008-06-01 11:40:29 +02001227#define SSTAT(m) \
1228 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
1229 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001230
Lennert Buytenhek16820052008-06-01 11:40:29 +02001231#define MIBSTAT(m) \
1232 { #m, FIELD_SIZEOF(struct mib_counters, m), \
1233 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1234
1235static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1236 SSTAT(rx_packets),
1237 SSTAT(tx_packets),
1238 SSTAT(rx_bytes),
1239 SSTAT(tx_bytes),
1240 SSTAT(rx_errors),
1241 SSTAT(tx_errors),
1242 SSTAT(rx_dropped),
1243 SSTAT(tx_dropped),
1244 MIBSTAT(good_octets_received),
1245 MIBSTAT(bad_octets_received),
1246 MIBSTAT(internal_mac_transmit_err),
1247 MIBSTAT(good_frames_received),
1248 MIBSTAT(bad_frames_received),
1249 MIBSTAT(broadcast_frames_received),
1250 MIBSTAT(multicast_frames_received),
1251 MIBSTAT(frames_64_octets),
1252 MIBSTAT(frames_65_to_127_octets),
1253 MIBSTAT(frames_128_to_255_octets),
1254 MIBSTAT(frames_256_to_511_octets),
1255 MIBSTAT(frames_512_to_1023_octets),
1256 MIBSTAT(frames_1024_to_max_octets),
1257 MIBSTAT(good_octets_sent),
1258 MIBSTAT(good_frames_sent),
1259 MIBSTAT(excessive_collision),
1260 MIBSTAT(multicast_frames_sent),
1261 MIBSTAT(broadcast_frames_sent),
1262 MIBSTAT(unrec_mac_control_received),
1263 MIBSTAT(fc_sent),
1264 MIBSTAT(good_fc_received),
1265 MIBSTAT(bad_fc_received),
1266 MIBSTAT(undersize_received),
1267 MIBSTAT(fragments_received),
1268 MIBSTAT(oversize_received),
1269 MIBSTAT(jabber_received),
1270 MIBSTAT(mac_receive_error),
1271 MIBSTAT(bad_crc_event),
1272 MIBSTAT(collision),
1273 MIBSTAT(late_collision),
Paulius Zaleckas302476c2012-01-23 01:16:35 +00001274 MIBSTAT(rx_discard),
1275 MIBSTAT(rx_overrun),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001276};
1277
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001278static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001279mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp,
1280 struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001281{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001282 int err;
1283
Lennert Buytenheked944932008-08-26 13:34:19 +02001284 err = phy_read_status(mp->phy);
1285 if (err == 0)
1286 err = phy_ethtool_gset(mp->phy, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001287
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001288 /*
1289 * The MAC does not support 1000baseT_Half.
1290 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001291 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1292 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1293
1294 return err;
1295}
1296
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001297static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001298mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp,
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001299 struct ethtool_cmd *cmd)
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001300{
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001301 u32 port_status;
1302
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001303 port_status = rdlp(mp, PORT_STATUS);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001304
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001305 cmd->supported = SUPPORTED_MII;
1306 cmd->advertising = ADVERTISED_MII;
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001307 switch (port_status & PORT_SPEED_MASK) {
1308 case PORT_SPEED_10:
David Decotigny70739492011-04-27 18:32:40 +00001309 ethtool_cmd_speed_set(cmd, SPEED_10);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001310 break;
1311 case PORT_SPEED_100:
David Decotigny70739492011-04-27 18:32:40 +00001312 ethtool_cmd_speed_set(cmd, SPEED_100);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001313 break;
1314 case PORT_SPEED_1000:
David Decotigny70739492011-04-27 18:32:40 +00001315 ethtool_cmd_speed_set(cmd, SPEED_1000);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001316 break;
1317 default:
1318 cmd->speed = -1;
1319 break;
1320 }
1321 cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001322 cmd->port = PORT_MII;
1323 cmd->phy_address = 0;
1324 cmd->transceiver = XCVR_INTERNAL;
1325 cmd->autoneg = AUTONEG_DISABLE;
1326 cmd->maxtxpkt = 1;
1327 cmd->maxrxpkt = 1;
1328
1329 return 0;
1330}
1331
Michael Stapelberg3871c382013-03-11 13:56:45 +00001332static void
1333mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1334{
1335 struct mv643xx_eth_private *mp = netdev_priv(dev);
1336 wol->supported = 0;
1337 wol->wolopts = 0;
1338 if (mp->phy)
1339 phy_ethtool_get_wol(mp->phy, wol);
1340}
1341
1342static int
1343mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
1344{
1345 struct mv643xx_eth_private *mp = netdev_priv(dev);
1346 int err;
1347
1348 if (mp->phy == NULL)
1349 return -EOPNOTSUPP;
1350
1351 err = phy_ethtool_set_wol(mp->phy, wol);
1352 /* Given that mv643xx_eth works without the marvell-specific PHY driver,
1353 * this debugging hint is useful to have.
1354 */
1355 if (err == -EOPNOTSUPP)
1356 netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n");
1357 return err;
1358}
1359
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001360static int
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001361mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1362{
1363 struct mv643xx_eth_private *mp = netdev_priv(dev);
1364
1365 if (mp->phy != NULL)
1366 return mv643xx_eth_get_settings_phy(mp, cmd);
1367 else
1368 return mv643xx_eth_get_settings_phyless(mp, cmd);
1369}
1370
1371static int
Lennert Buytenhek10a99482008-11-20 03:57:16 -08001372mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001373{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001374 struct mv643xx_eth_private *mp = netdev_priv(dev);
Phil Sutter260055b2013-03-06 07:49:02 +00001375 int ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001376
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001377 if (mp->phy == NULL)
1378 return -EINVAL;
1379
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001380 /*
1381 * The MAC does not support 1000baseT_Half.
1382 */
1383 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1384
Phil Sutter260055b2013-03-06 07:49:02 +00001385 ret = phy_ethtool_sset(mp->phy, cmd);
1386 if (!ret)
1387 mv643xx_adjust_pscr(mp);
1388 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001389}
1390
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001391static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1392 struct ethtool_drvinfo *drvinfo)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001393{
Axel Lin6f39da22011-11-24 00:41:55 -05001394 strlcpy(drvinfo->driver, mv643xx_eth_driver_name,
1395 sizeof(drvinfo->driver));
Rick Jones68aad782011-11-07 13:29:27 +00001396 strlcpy(drvinfo->version, mv643xx_eth_driver_version,
Axel Lin6f39da22011-11-24 00:41:55 -05001397 sizeof(drvinfo->version));
1398 strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version));
1399 strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info));
Lennert Buytenhek16820052008-06-01 11:40:29 +02001400 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001401}
1402
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001403static int mv643xx_eth_nway_reset(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001404{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001405 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001406
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001407 if (mp->phy == NULL)
1408 return -EINVAL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001409
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00001410 return genphy_restart_aneg(mp->phy);
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001411}
1412
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001413static int
1414mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1415{
1416 struct mv643xx_eth_private *mp = netdev_priv(dev);
1417
1418 ec->rx_coalesce_usecs = get_rx_coal(mp);
1419 ec->tx_coalesce_usecs = get_tx_coal(mp);
1420
1421 return 0;
1422}
1423
1424static int
1425mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec)
1426{
1427 struct mv643xx_eth_private *mp = netdev_priv(dev);
1428
1429 set_rx_coal(mp, ec->rx_coalesce_usecs);
1430 set_tx_coal(mp, ec->tx_coalesce_usecs);
1431
1432 return 0;
1433}
1434
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001435static void
1436mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1437{
1438 struct mv643xx_eth_private *mp = netdev_priv(dev);
1439
1440 er->rx_max_pending = 4096;
1441 er->tx_max_pending = 4096;
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001442
1443 er->rx_pending = mp->rx_ring_size;
1444 er->tx_pending = mp->tx_ring_size;
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001445}
1446
1447static int
1448mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er)
1449{
1450 struct mv643xx_eth_private *mp = netdev_priv(dev);
1451
1452 if (er->rx_mini_pending || er->rx_jumbo_pending)
1453 return -EINVAL;
1454
1455 mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096;
1456 mp->tx_ring_size = er->tx_pending < 4096 ? er->tx_pending : 4096;
1457
1458 if (netif_running(dev)) {
1459 mv643xx_eth_stop(dev);
1460 if (mv643xx_eth_open(dev)) {
Joe Perches7542db82011-03-02 17:50:35 +00001461 netdev_err(dev,
1462 "fatal error on re-opening device after ring param change\n");
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001463 return -ENOMEM;
1464 }
1465 }
1466
1467 return 0;
1468}
1469
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001470
1471static int
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001472mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features)
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001473{
1474 struct mv643xx_eth_private *mp = netdev_priv(dev);
Michał Mirosław3ad9b352011-11-16 14:05:33 +00001475 bool rx_csum = features & NETIF_F_RXCSUM;
Lennert Buytenhekd888b372009-02-12 14:07:56 +00001476
1477 wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000);
1478
1479 return 0;
1480}
1481
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001482static void mv643xx_eth_get_strings(struct net_device *dev,
1483 uint32_t stringset, uint8_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001484{
1485 int i;
1486
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001487 if (stringset == ETH_SS_STATS) {
1488 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001489 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001490 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001491 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001492 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001493 }
1494}
1495
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001496static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1497 struct ethtool_stats *stats,
1498 uint64_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001499{
Lennert Buytenhekb9873842008-08-24 05:59:16 +02001500 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001501 int i;
1502
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02001503 mv643xx_eth_get_stats(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001504 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001505
Lennert Buytenhek16820052008-06-01 11:40:29 +02001506 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1507 const struct mv643xx_eth_stats *stat;
1508 void *p;
1509
1510 stat = mv643xx_eth_stats + i;
1511
1512 if (stat->netdev_off >= 0)
1513 p = ((void *)mp->dev) + stat->netdev_off;
1514 else
1515 p = ((void *)mp) + stat->mp_off;
1516
1517 data[i] = (stat->sizeof_stat == 8) ?
1518 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001519 }
1520}
1521
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001522static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001523{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001524 if (sset == ETH_SS_STATS)
Lennert Buytenhek16820052008-06-01 11:40:29 +02001525 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001526
1527 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001528}
1529
Lennert Buytenheke5371492008-06-01 02:18:13 +02001530static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001531 .get_settings = mv643xx_eth_get_settings,
1532 .set_settings = mv643xx_eth_set_settings,
1533 .get_drvinfo = mv643xx_eth_get_drvinfo,
1534 .nway_reset = mv643xx_eth_nway_reset,
Ben Hutchingsed4ba4b2010-12-09 12:10:25 +00001535 .get_link = ethtool_op_get_link,
Lennert Buytenhek3e508032009-02-12 14:07:09 +00001536 .get_coalesce = mv643xx_eth_get_coalesce,
1537 .set_coalesce = mv643xx_eth_set_coalesce,
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001538 .get_ringparam = mv643xx_eth_get_ringparam,
1539 .set_ringparam = mv643xx_eth_set_ringparam,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001540 .get_strings = mv643xx_eth_get_strings,
1541 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001542 .get_sset_count = mv643xx_eth_get_sset_count,
Richard Cochranebad0a82012-04-03 22:59:32 +00001543 .get_ts_info = ethtool_op_get_ts_info,
Michael Stapelberg3871c382013-03-11 13:56:45 +00001544 .get_wol = mv643xx_eth_get_wol,
1545 .set_wol = mv643xx_eth_set_wol,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001546};
1547
1548
1549/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001550static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001551{
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001552 unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH);
1553 unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001554
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001555 addr[0] = (mac_h >> 24) & 0xff;
1556 addr[1] = (mac_h >> 16) & 0xff;
1557 addr[2] = (mac_h >> 8) & 0xff;
1558 addr[3] = mac_h & 0xff;
1559 addr[4] = (mac_l >> 8) & 0xff;
1560 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001561}
1562
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001563static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001564{
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001565 wrlp(mp, MAC_ADDR_HIGH,
1566 (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]);
1567 wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001568}
1569
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001570static u32 uc_addr_filter_mask(struct net_device *dev)
1571{
Jiri Pirkoccffad252009-05-22 23:22:17 +00001572 struct netdev_hw_addr *ha;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001573 u32 nibbles;
1574
1575 if (dev->flags & IFF_PROMISC)
1576 return 0;
1577
1578 nibbles = 1 << (dev->dev_addr[5] & 0x0f);
Jiri Pirko32e7bfc2010-01-25 13:36:10 -08001579 netdev_for_each_uc_addr(ha, dev) {
Jiri Pirkoccffad252009-05-22 23:22:17 +00001580 if (memcmp(dev->dev_addr, ha->addr, 5))
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001581 return 0;
Jiri Pirkoccffad252009-05-22 23:22:17 +00001582 if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0)
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001583 return 0;
1584
Jiri Pirkoccffad252009-05-22 23:22:17 +00001585 nibbles |= 1 << (ha->addr[5] & 0x0f);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001586 }
1587
1588 return nibbles;
1589}
1590
1591static void mv643xx_eth_program_unicast_filter(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001592{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001593 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001594 u32 port_config;
1595 u32 nibbles;
1596 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001597
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001598 uc_addr_set(mp, dev->dev_addr);
1599
Prabhanjan Sarnaik6877f542009-06-18 11:35:02 +00001600 port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE;
1601
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001602 nibbles = uc_addr_filter_mask(dev);
1603 if (!nibbles) {
1604 port_config |= UNICAST_PROMISCUOUS_MODE;
Prabhanjan Sarnaik6877f542009-06-18 11:35:02 +00001605 nibbles = 0xffff;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001606 }
1607
1608 for (i = 0; i < 16; i += 4) {
1609 int off = UNICAST_TABLE(mp->port_num) + i;
1610 u32 v;
1611
1612 v = 0;
1613 if (nibbles & 1)
1614 v |= 0x00000001;
1615 if (nibbles & 2)
1616 v |= 0x00000100;
1617 if (nibbles & 4)
1618 v |= 0x00010000;
1619 if (nibbles & 8)
1620 v |= 0x01000000;
1621 nibbles >>= 4;
1622
1623 wrl(mp, off, v);
1624 }
1625
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001626 wrlp(mp, PORT_CONFIG, port_config);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001627}
1628
Lennert Buytenhek69876562008-06-01 11:43:32 +02001629static int addr_crc(unsigned char *addr)
1630{
1631 int crc = 0;
1632 int i;
1633
1634 for (i = 0; i < 6; i++) {
1635 int j;
1636
1637 crc = (crc ^ addr[i]) << 8;
1638 for (j = 7; j >= 0; j--) {
1639 if (crc & (0x100 << j))
1640 crc ^= 0x107 << j;
1641 }
1642 }
1643
1644 return crc;
1645}
1646
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001647static void mv643xx_eth_program_multicast_filter(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001648{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001649 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001650 u32 *mc_spec;
1651 u32 *mc_other;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001652 struct netdev_hw_addr *ha;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001653 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001654
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001655 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001656 int port_num;
1657 u32 accept;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001658
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001659oom:
1660 port_num = mp->port_num;
1661 accept = 0x01010101;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001662 for (i = 0; i < 0x100; i += 4) {
1663 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1664 wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001665 }
1666 return;
1667 }
1668
Sebastian Siewior82a5bd62009-02-14 23:26:18 +00001669 mc_spec = kmalloc(0x200, GFP_ATOMIC);
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001670 if (mc_spec == NULL)
1671 goto oom;
1672 mc_other = mc_spec + (0x100 >> 2);
1673
1674 memset(mc_spec, 0, 0x100);
1675 memset(mc_other, 0, 0x100);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001676
Jiri Pirko22bedad32010-04-01 21:22:57 +00001677 netdev_for_each_mc_addr(ha, dev) {
1678 u8 *a = ha->addr;
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001679 u32 *table;
1680 int entry;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001681
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001682 if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001683 table = mc_spec;
1684 entry = a[5];
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001685 } else {
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001686 table = mc_other;
1687 entry = addr_crc(a);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001688 }
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001689
Lennert Buytenhek2b448332009-01-19 17:17:18 -08001690 table[entry >> 2] |= 1 << (8 * (entry & 3));
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001691 }
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001692
1693 for (i = 0; i < 0x100; i += 4) {
1694 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]);
1695 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]);
1696 }
1697
1698 kfree(mc_spec);
1699}
1700
1701static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1702{
1703 mv643xx_eth_program_unicast_filter(dev);
1704 mv643xx_eth_program_multicast_filter(dev);
1705}
1706
1707static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1708{
1709 struct sockaddr *sa = addr;
1710
Denis Kirjanova29ec082009-12-08 20:36:00 -08001711 if (!is_valid_ether_addr(sa->sa_data))
Danny Kukawka504f9b52012-02-21 02:07:49 +00001712 return -EADDRNOTAVAIL;
Denis Kirjanova29ec082009-12-08 20:36:00 -08001713
Lennert Buytenhek66e63ff2008-11-20 03:58:27 -08001714 memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN);
1715
1716 netif_addr_lock_bh(dev);
1717 mv643xx_eth_program_unicast_filter(dev);
1718 netif_addr_unlock_bh(dev);
1719
1720 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001721}
1722
1723
1724/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001725static int rxq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001726{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001727 struct rx_queue *rxq = mp->rxq + index;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001728 struct rx_desc *rx_desc;
1729 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001730 int i;
1731
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001732 rxq->index = index;
1733
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001734 rxq->rx_ring_size = mp->rx_ring_size;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001735
1736 rxq->rx_desc_count = 0;
1737 rxq->rx_curr_desc = 0;
1738 rxq->rx_used_desc = 0;
1739
1740 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1741
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001742 if (index == 0 && size <= mp->rx_desc_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001743 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1744 mp->rx_desc_sram_size);
1745 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1746 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001747 rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1748 size, &rxq->rx_desc_dma,
1749 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001750 }
1751
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001752 if (rxq->rx_desc_area == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00001753 netdev_err(mp->dev,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001754 "can't allocate rx ring (%d bytes)\n", size);
1755 goto out;
1756 }
1757 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001758
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001759 rxq->rx_desc_area_size = size;
Joe Perchesb2adaca2013-02-03 17:43:58 +00001760 rxq->rx_skb = kmalloc_array(rxq->rx_ring_size, sizeof(*rxq->rx_skb),
1761 GFP_KERNEL);
1762 if (rxq->rx_skb == NULL)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001763 goto out_free;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001764
Joe Perches64699332012-06-04 12:44:16 +00001765 rx_desc = rxq->rx_desc_area;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001766 for (i = 0; i < rxq->rx_ring_size; i++) {
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001767 int nexti;
1768
1769 nexti = i + 1;
1770 if (nexti == rxq->rx_ring_size)
1771 nexti = 0;
1772
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001773 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1774 nexti * sizeof(struct rx_desc);
1775 }
1776
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001777 return 0;
1778
1779
1780out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001781 if (index == 0 && size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001782 iounmap(rxq->rx_desc_area);
1783 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001784 dma_free_coherent(mp->dev->dev.parent, size,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001785 rxq->rx_desc_area,
1786 rxq->rx_desc_dma);
1787
1788out:
1789 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001790}
1791
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001792static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001793{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001794 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1795 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001796
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001797 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001798
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001799 for (i = 0; i < rxq->rx_ring_size; i++) {
1800 if (rxq->rx_skb[i]) {
1801 dev_kfree_skb(rxq->rx_skb[i]);
1802 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001803 }
1804 }
1805
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001806 if (rxq->rx_desc_count) {
Joe Perches7542db82011-03-02 17:50:35 +00001807 netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n",
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001808 rxq->rx_desc_count);
1809 }
1810
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001811 if (rxq->index == 0 &&
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001812 rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001813 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001814 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001815 dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size,
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001816 rxq->rx_desc_area, rxq->rx_desc_dma);
1817
1818 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001819}
1820
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001821static int txq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001822{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001823 struct tx_queue *txq = mp->txq + index;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001824 struct tx_desc *tx_desc;
1825 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001826 int i;
1827
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001828 txq->index = index;
1829
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00001830 txq->tx_ring_size = mp->tx_ring_size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001831
1832 txq->tx_desc_count = 0;
1833 txq->tx_curr_desc = 0;
1834 txq->tx_used_desc = 0;
1835
1836 size = txq->tx_ring_size * sizeof(struct tx_desc);
1837
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001838 if (index == 0 && size <= mp->tx_desc_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001839 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1840 mp->tx_desc_sram_size);
1841 txq->tx_desc_dma = mp->tx_desc_sram_addr;
1842 } else {
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001843 txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent,
1844 size, &txq->tx_desc_dma,
1845 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001846 }
1847
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001848 if (txq->tx_desc_area == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00001849 netdev_err(mp->dev,
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001850 "can't allocate tx ring (%d bytes)\n", size);
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +02001851 return -ENOMEM;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001852 }
1853 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001854
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001855 txq->tx_desc_area_size = size;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001856
Joe Perches64699332012-06-04 12:44:16 +00001857 tx_desc = txq->tx_desc_area;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001858 for (i = 0; i < txq->tx_ring_size; i++) {
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001859 struct tx_desc *txd = tx_desc + i;
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001860 int nexti;
1861
1862 nexti = i + 1;
1863 if (nexti == txq->tx_ring_size)
1864 nexti = 0;
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001865
1866 txd->cmd_sts = 0;
1867 txd->next_desc_ptr = txq->tx_desc_dma +
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001868 nexti * sizeof(struct tx_desc);
1869 }
1870
Lennert Buytenhek99ab08e2008-08-28 05:53:18 +02001871 skb_queue_head_init(&txq->tx_skb);
1872
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001873 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001874}
1875
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001876static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001877{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001878 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001879
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001880 txq_disable(txq);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001881 txq_reclaim(txq, txq->tx_ring_size, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001882
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001883 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001884
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001885 if (txq->index == 0 &&
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001886 txq->tx_desc_area_size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001887 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001888 else
Gabriel Pauberteb0519b2009-05-17 21:16:47 -07001889 dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size,
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001890 txq->tx_desc_area, txq->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001891}
1892
1893
1894/* netdev ops and related ***************************************************/
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001895static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp)
1896{
1897 u32 int_cause;
1898 u32 int_cause_ext;
1899
Saeed Bisharae0ca8412009-05-06 03:02:01 +00001900 int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001901 if (int_cause == 0)
1902 return 0;
1903
1904 int_cause_ext = 0;
Saeed Bisharae0ca8412009-05-06 03:02:01 +00001905 if (int_cause & INT_EXT) {
1906 int_cause &= ~INT_EXT;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001907 int_cause_ext = rdlp(mp, INT_CAUSE_EXT);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00001908 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001909
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001910 if (int_cause) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001911 wrlp(mp, INT_CAUSE, ~int_cause);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001912 mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) &
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001913 ~(rdlp(mp, TXQ_COMMAND) & 0xff);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001914 mp->work_rx |= (int_cause & INT_RX) >> 2;
1915 }
1916
1917 int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX;
1918 if (int_cause_ext) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001919 wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001920 if (int_cause_ext & INT_EXT_LINK_PHY)
1921 mp->work_link = 1;
1922 mp->work_tx |= int_cause_ext & INT_EXT_TX;
1923 }
1924
1925 return 1;
1926}
1927
1928static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
1929{
1930 struct net_device *dev = (struct net_device *)dev_id;
1931 struct mv643xx_eth_private *mp = netdev_priv(dev);
1932
1933 if (unlikely(!mv643xx_eth_collect_events(mp)))
1934 return IRQ_NONE;
1935
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001936 wrlp(mp, INT_MASK, 0);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001937 napi_schedule(&mp->napi);
1938
1939 return IRQ_HANDLED;
1940}
1941
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001942static void handle_link_event(struct mv643xx_eth_private *mp)
1943{
1944 struct net_device *dev = mp->dev;
1945 u32 port_status;
1946 int speed;
1947 int duplex;
1948 int fc;
1949
Lennert Buytenhek37a60842008-11-20 03:57:36 -08001950 port_status = rdlp(mp, PORT_STATUS);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001951 if (!(port_status & LINK_UP)) {
1952 if (netif_carrier_ok(dev)) {
1953 int i;
1954
Joe Perches7542db82011-03-02 17:50:35 +00001955 netdev_info(dev, "link down\n");
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001956
1957 netif_carrier_off(dev);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001958
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001959 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001960 struct tx_queue *txq = mp->txq + i;
1961
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001962 txq_reclaim(txq, txq->tx_ring_size, 1);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001963 txq_reset_hw_ptr(txq);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001964 }
1965 }
1966 return;
1967 }
1968
1969 switch (port_status & PORT_SPEED_MASK) {
1970 case PORT_SPEED_10:
1971 speed = 10;
1972 break;
1973 case PORT_SPEED_100:
1974 speed = 100;
1975 break;
1976 case PORT_SPEED_1000:
1977 speed = 1000;
1978 break;
1979 default:
1980 speed = -1;
1981 break;
1982 }
1983 duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
1984 fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
1985
Joe Perches7542db82011-03-02 17:50:35 +00001986 netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n",
1987 speed, duplex ? "full" : "half", fc ? "en" : "dis");
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001988
Lennert Buytenhek4fdeca32008-08-28 05:39:13 +02001989 if (!netif_carrier_ok(dev))
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001990 netif_carrier_on(dev);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001991}
1992
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001993static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001994{
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001995 struct mv643xx_eth_private *mp;
1996 int work_done;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001997
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02001998 mp = container_of(napi, struct mv643xx_eth_private, napi);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001999
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002000 if (unlikely(mp->oom)) {
2001 mp->oom = 0;
2002 del_timer(&mp->rx_oom);
2003 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002004
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002005 work_done = 0;
2006 while (work_done < budget) {
2007 u8 queue_mask;
2008 int queue;
2009 int work_tbd;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002010
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002011 if (mp->work_link) {
2012 mp->work_link = 0;
2013 handle_link_event(mp);
Lennert Buytenhek26ef1f12009-05-06 03:01:10 +00002014 work_done++;
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002015 continue;
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02002016 }
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002017
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002018 queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx;
2019 if (likely(!mp->oom))
2020 queue_mask |= mp->work_rx_refill;
2021
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002022 if (!queue_mask) {
2023 if (mv643xx_eth_collect_events(mp))
2024 continue;
2025 break;
2026 }
2027
2028 queue = fls(queue_mask) - 1;
2029 queue_mask = 1 << queue;
2030
2031 work_tbd = budget - work_done;
2032 if (work_tbd > 16)
2033 work_tbd = 16;
2034
2035 if (mp->work_tx_end & queue_mask) {
2036 txq_kick(mp->txq + queue);
2037 } else if (mp->work_tx & queue_mask) {
2038 work_done += txq_reclaim(mp->txq + queue, work_tbd, 0);
2039 txq_maybe_wake(mp->txq + queue);
2040 } else if (mp->work_rx & queue_mask) {
2041 work_done += rxq_process(mp->rxq + queue, work_tbd);
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002042 } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) {
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002043 work_done += rxq_refill(mp->rxq + queue, work_tbd);
2044 } else {
2045 BUG();
2046 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002047 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002048
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002049 if (work_done < budget) {
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002050 if (mp->oom)
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002051 mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
2052 napi_complete(napi);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002053 wrlp(mp, INT_MASK, mp->int_mask);
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002054 }
2055
2056 return work_done;
2057}
2058
2059static inline void oom_timer_wrapper(unsigned long data)
2060{
2061 struct mv643xx_eth_private *mp = (void *)data;
2062
2063 napi_schedule(&mp->napi);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002064}
2065
Lennert Buytenheke5371492008-06-01 02:18:13 +02002066static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002067{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002068 int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002069
Lennert Buytenheked944932008-08-26 13:34:19 +02002070 data = phy_read(mp->phy, MII_BMCR);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002071 if (data < 0)
2072 return;
2073
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002074 data |= BMCR_RESET;
Lennert Buytenheked944932008-08-26 13:34:19 +02002075 if (phy_write(mp->phy, MII_BMCR, data) < 0)
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002076 return;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002077
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002078 do {
Lennert Buytenheked944932008-08-26 13:34:19 +02002079 data = phy_read(mp->phy, MII_BMCR);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002080 } while (data >= 0 && data & BMCR_RESET);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002081}
2082
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002083static void port_start(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002084{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002085 u32 pscr;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002086 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002087
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002088 /*
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002089 * Perform PHY reset, if there is a PHY.
2090 */
Lennert Buytenheked944932008-08-26 13:34:19 +02002091 if (mp->phy != NULL) {
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002092 struct ethtool_cmd cmd;
2093
2094 mv643xx_eth_get_settings(mp->dev, &cmd);
2095 phy_reset(mp);
2096 mv643xx_eth_set_settings(mp->dev, &cmd);
2097 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002098
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002099 /*
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002100 * Configure basic link parameters.
2101 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002102 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002103
2104 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002105 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002106
2107 pscr |= DO_NOT_FORCE_LINK_FAIL;
Lennert Buytenheked944932008-08-26 13:34:19 +02002108 if (mp->phy == NULL)
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002109 pscr |= FORCE_LINK_PASS;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002110 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002111
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002112 /*
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002113 * Configure TX path and queues.
2114 */
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002115 tx_set_rate(mp, 1000000000, 16777216);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002116 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002117 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002118
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02002119 txq_reset_hw_ptr(txq);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002120 txq_set_rate(txq, 1000000000, 16777216);
2121 txq_set_fixed_prio_mode(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002122 }
2123
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002124 /*
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002125 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
Lennert Buytenhek170e7102008-09-19 02:58:50 +02002126 * frames to RX queue #0, and include the pseudo-header when
2127 * calculating receive checksums.
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002128 */
David S. Millere138f962011-04-21 15:19:02 -07002129 mv643xx_eth_set_features(mp->dev, mp->dev->features);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002130
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002131 /*
2132 * Treat BPDUs as normal multicasts, and disable partition mode.
2133 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002134 wrlp(mp, PORT_CONFIG_EXT, 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002135
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002136 /*
Lennert Buytenhek5a893922009-03-13 15:48:02 -07002137 * Add configured unicast addresses to address filter table.
2138 */
2139 mv643xx_eth_program_unicast_filter(mp->dev);
2140
2141 /*
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002142 * Enable the receive queues.
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002143 */
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002144 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002145 struct rx_queue *rxq = mp->rxq + i;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002146 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002147
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002148 addr = (u32)rxq->rx_desc_dma;
2149 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002150 wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002151
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002152 rxq_enable(rxq);
2153 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002154}
2155
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002156static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp)
2157{
2158 int skb_size;
2159
2160 /*
2161 * Reserve 2+14 bytes for an ethernet header (the hardware
2162 * automatically prepends 2 bytes of dummy data to each
2163 * received packet), 16 bytes for up to four VLAN tags, and
2164 * 4 bytes for the trailing FCS -- 36 bytes total.
2165 */
2166 skb_size = mp->dev->mtu + 36;
2167
2168 /*
2169 * Make sure that the skb size is a multiple of 8 bytes, as
2170 * the lower three bits of the receive descriptor's buffer
2171 * size field are ignored by the hardware.
2172 */
2173 mp->skb_size = (skb_size + 7) & ~7;
Lennert Buytenhek7fd96ce2009-05-06 03:01:22 +00002174
2175 /*
2176 * If NET_SKB_PAD is smaller than a cache line,
2177 * netdev_alloc_skb() will cause skb->data to be misaligned
2178 * to a cache line boundary. If this is the case, include
2179 * some extra space to allow re-aligning the data area.
2180 */
2181 mp->skb_size += SKB_DMA_REALIGN;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002182}
2183
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002184static int mv643xx_eth_open(struct net_device *dev)
2185{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002186 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002187 int err;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002188 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002189
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002190 wrlp(mp, INT_CAUSE, 0);
2191 wrlp(mp, INT_CAUSE_EXT, 0);
2192 rdlp(mp, INT_CAUSE_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002193
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002194 err = request_irq(dev->irq, mv643xx_eth_irq,
Lennert Buytenhek2a1867a2008-08-23 23:43:38 +02002195 IRQF_SHARED, dev->name, dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002196 if (err) {
Joe Perches7542db82011-03-02 17:50:35 +00002197 netdev_err(dev, "can't assign irq\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002198 return -EAGAIN;
2199 }
2200
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002201 mv643xx_eth_recalc_skb_size(mp);
2202
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002203 napi_enable(&mp->napi);
2204
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002205 mp->int_mask = INT_EXT;
2206
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002207 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002208 err = rxq_init(mp, i);
2209 if (err) {
2210 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002211 rxq_deinit(mp->rxq + i);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002212 goto out;
2213 }
2214
Lennert Buytenhek1fa38c52008-08-28 08:09:10 +02002215 rxq_refill(mp->rxq + i, INT_MAX);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002216 mp->int_mask |= INT_RX_0 << i;
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002217 }
2218
Lennert Buytenhek1319eba2009-04-29 11:57:34 +00002219 if (mp->oom) {
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002220 mp->rx_oom.expires = jiffies + (HZ / 10);
2221 add_timer(&mp->rx_oom);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002222 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002223
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002224 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002225 err = txq_init(mp, i);
2226 if (err) {
2227 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002228 txq_deinit(mp->txq + i);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002229 goto out_free;
2230 }
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002231 mp->int_mask |= INT_TX_END_0 << i;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002232 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002233
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002234 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002235
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002236 wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX);
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002237 wrlp(mp, INT_MASK, mp->int_mask);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002238
2239 return 0;
2240
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002241
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002242out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002243 for (i = 0; i < mp->rxq_count; i++)
2244 rxq_deinit(mp->rxq + i);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002245out:
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002246 free_irq(dev->irq, dev);
2247
2248 return err;
2249}
2250
Lennert Buytenheke5371492008-06-01 02:18:13 +02002251static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002252{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002253 unsigned int data;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002254 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002255
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002256 for (i = 0; i < mp->rxq_count; i++)
2257 rxq_disable(mp->rxq + i);
2258 for (i = 0; i < mp->txq_count; i++)
2259 txq_disable(mp->txq + i);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002260
2261 while (1) {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002262 u32 ps = rdlp(mp, PORT_STATUS);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002263
2264 if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2265 break;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002266 udelay(10);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002267 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002268
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002269 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002270 data = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002271 data &= ~(SERIAL_PORT_ENABLE |
2272 DO_NOT_FORCE_LINK_FAIL |
2273 FORCE_LINK_PASS);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002274 wrlp(mp, PORT_SERIAL_CONTROL, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002275}
2276
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002277static int mv643xx_eth_stop(struct net_device *dev)
2278{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002279 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002280 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002281
Gabriel Paubertfe65e702009-01-19 17:18:09 -08002282 wrlp(mp, INT_MASK_EXT, 0x00000000);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002283 wrlp(mp, INT_MASK, 0x00000000);
2284 rdlp(mp, INT_MASK);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002285
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002286 napi_disable(&mp->napi);
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002287
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002288 del_timer_sync(&mp->rx_oom);
2289
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002290 netif_carrier_off(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002291
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002292 free_irq(dev->irq, dev);
2293
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002294 port_reset(mp);
Lennert Buytenhek8fd89212008-08-28 08:26:28 +02002295 mv643xx_eth_get_stats(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002296 mib_counters_update(mp);
Sebastian Siewior57e8f262009-02-16 11:28:15 +00002297 del_timer_sync(&mp->mib_counters_timer);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002298
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002299 for (i = 0; i < mp->rxq_count; i++)
2300 rxq_deinit(mp->rxq + i);
2301 for (i = 0; i < mp->txq_count; i++)
2302 txq_deinit(mp->txq + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002303
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002304 return 0;
2305}
2306
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002307static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002308{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002309 struct mv643xx_eth_private *mp = netdev_priv(dev);
Phil Sutter260055b2013-03-06 07:49:02 +00002310 int ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002311
Phil Sutter260055b2013-03-06 07:49:02 +00002312 if (mp->phy == NULL)
2313 return -ENOTSUPP;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002314
Phil Sutter260055b2013-03-06 07:49:02 +00002315 ret = phy_mii_ioctl(mp->phy, ifr, cmd);
2316 if (!ret)
2317 mv643xx_adjust_pscr(mp);
2318 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002319}
2320
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002321static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2322{
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002323 struct mv643xx_eth_private *mp = netdev_priv(dev);
2324
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002325 if (new_mtu < 64 || new_mtu > 9500)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002326 return -EINVAL;
2327
2328 dev->mtu = new_mtu;
Lennert Buytenhek2bcb4b02008-10-01 02:33:57 -07002329 mv643xx_eth_recalc_skb_size(mp);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002330 tx_set_rate(mp, 1000000000, 16777216);
2331
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002332 if (!netif_running(dev))
2333 return 0;
2334
2335 /*
2336 * Stop and then re-open the interface. This will allocate RX
2337 * skbs of the new MTU.
2338 * There is a possible danger that the open will not succeed,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002339 * due to memory being full.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002340 */
2341 mv643xx_eth_stop(dev);
2342 if (mv643xx_eth_open(dev)) {
Joe Perches7542db82011-03-02 17:50:35 +00002343 netdev_err(dev,
2344 "fatal error on re-opening device after MTU change\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002345 }
2346
2347 return 0;
2348}
2349
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002350static void tx_timeout_task(struct work_struct *ugly)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002351{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002352 struct mv643xx_eth_private *mp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002353
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002354 mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2355 if (netif_running(mp->dev)) {
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002356 netif_tx_stop_all_queues(mp->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002357 port_reset(mp);
2358 port_start(mp);
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002359 netif_tx_wake_all_queues(mp->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002360 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002361}
2362
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002363static void mv643xx_eth_tx_timeout(struct net_device *dev)
2364{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002365 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002366
Joe Perches7542db82011-03-02 17:50:35 +00002367 netdev_info(dev, "tx timeout\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002368
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002369 schedule_work(&mp->tx_timeout_task);
2370}
2371
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002372#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002373static void mv643xx_eth_netpoll(struct net_device *dev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002374{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002375 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002376
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002377 wrlp(mp, INT_MASK, 0x00000000);
2378 rdlp(mp, INT_MASK);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002379
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002380 mv643xx_eth_irq(dev->irq, dev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002381
Saeed Bisharae0ca8412009-05-06 03:02:01 +00002382 wrlp(mp, INT_MASK, mp->int_mask);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002383}
2384#endif
2385
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002386
2387/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02002388static void
2389mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
Andrew Lunn63a93322011-12-07 21:48:07 +01002390 const struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002391{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002392 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002393 u32 win_enable;
2394 u32 win_protect;
2395 int i;
2396
2397 for (i = 0; i < 6; i++) {
2398 writel(0, base + WINDOW_BASE(i));
2399 writel(0, base + WINDOW_SIZE(i));
2400 if (i < 4)
2401 writel(0, base + WINDOW_REMAP_HIGH(i));
2402 }
2403
2404 win_enable = 0x3f;
2405 win_protect = 0;
2406
2407 for (i = 0; i < dram->num_cs; i++) {
Andrew Lunn63a93322011-12-07 21:48:07 +01002408 const struct mbus_dram_window *cs = dram->cs + i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002409
2410 writel((cs->base & 0xffff0000) |
2411 (cs->mbus_attr << 8) |
2412 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2413 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2414
2415 win_enable &= ~(1 << i);
2416 win_protect |= 3 << (2 * i);
2417 }
2418
2419 writel(win_enable, base + WINDOW_BAR_ENABLE);
2420 msp->win_protect = win_protect;
2421}
2422
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002423static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2424{
2425 /*
2426 * Check whether we have a 14-bit coal limit field in bits
2427 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2428 * SDMA config register.
2429 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002430 writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG);
2431 if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000)
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002432 msp->extended_rx_coal_limit = 1;
2433 else
2434 msp->extended_rx_coal_limit = 0;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002435
2436 /*
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002437 * Check whether the MAC supports TX rate control, and if
2438 * yes, whether its associated registers are in the old or
2439 * the new place.
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002440 */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002441 writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED);
2442 if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) {
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002443 msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT;
2444 } else {
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002445 writel(7, msp->base + 0x0400 + TX_BW_RATE);
2446 if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7)
Lennert Buytenhek457b1d52008-09-04 12:22:34 +02002447 msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT;
2448 else
2449 msp->tx_bw_control = TX_BW_CONTROL_ABSENT;
2450 }
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002451}
2452
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002453static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2454{
Lennert Buytenhek10a99482008-11-20 03:57:16 -08002455 static int mv643xx_eth_version_printed;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002456 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002457 struct mv643xx_eth_shared_private *msp;
Andrew Lunn63a93322011-12-07 21:48:07 +01002458 const struct mbus_dram_target_info *dram;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002459 struct resource *res;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002460
Lennert Buytenheke5371492008-06-01 02:18:13 +02002461 if (!mv643xx_eth_version_printed++)
Joe Perches7542db82011-03-02 17:50:35 +00002462 pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n",
2463 mv643xx_eth_driver_version);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002464
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002465 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2466 if (res == NULL)
Sebastian Hesselbarth727f9572013-04-10 23:29:34 +00002467 return -EINVAL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002468
Sebastian Hesselbarth727f9572013-04-10 23:29:34 +00002469 msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002470 if (msp == NULL)
Sebastian Hesselbarth727f9572013-04-10 23:29:34 +00002471 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002472
Joe Perches28f65c112011-06-09 09:13:32 -07002473 msp->base = ioremap(res->start, resource_size(res));
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002474 if (msp->base == NULL)
Sebastian Hesselbarth727f9572013-04-10 23:29:34 +00002475 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002476
Sebastian Hesselbarth20922482013-04-10 23:29:33 +00002477 msp->clk = devm_clk_get(&pdev->dev, NULL);
2478 if (!IS_ERR(msp->clk))
2479 clk_prepare_enable(msp->clk);
2480
Lennert Buytenheked944932008-08-26 13:34:19 +02002481 /*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002482 * (Re-)program MBUS remapping windows if we are asked to.
2483 */
Andrew Lunn63a93322011-12-07 21:48:07 +01002484 dram = mv_mbus_dram_info();
2485 if (dram)
2486 mv643xx_eth_conf_mbus_windows(msp, dram);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002487
Dan Carpenter50a749c2010-07-23 01:05:05 +00002488 msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ?
2489 pd->tx_csum_limit : 9 * 1024;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002490 infer_hw_params(msp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002491
2492 platform_set_drvdata(pdev, msp);
2493
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002494 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002495}
2496
2497static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2498{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002499 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002500
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002501 iounmap(msp->base);
Sebastian Hesselbarth20922482013-04-10 23:29:33 +00002502 if (!IS_ERR(msp->clk))
2503 clk_disable_unprepare(msp->clk);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002504
2505 return 0;
2506}
2507
2508static struct platform_driver mv643xx_eth_shared_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002509 .probe = mv643xx_eth_shared_probe,
2510 .remove = mv643xx_eth_shared_remove,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002511 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002512 .name = MV643XX_ETH_SHARED_NAME,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002513 .owner = THIS_MODULE,
2514 },
2515};
2516
Lennert Buytenheke5371492008-06-01 02:18:13 +02002517static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002518{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002519 int addr_shift = 5 * mp->port_num;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002520 u32 data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002521
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002522 data = rdl(mp, PHY_ADDR);
2523 data &= ~(0x1f << addr_shift);
2524 data |= (phy_addr & 0x1f) << addr_shift;
2525 wrl(mp, PHY_ADDR, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002526}
2527
Lennert Buytenheke5371492008-06-01 02:18:13 +02002528static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002529{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002530 unsigned int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002531
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002532 data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002533
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002534 return (data >> (5 * mp->port_num)) & 0x1f;
2535}
2536
2537static void set_params(struct mv643xx_eth_private *mp,
2538 struct mv643xx_eth_platform_data *pd)
2539{
2540 struct net_device *dev = mp->dev;
2541
2542 if (is_valid_ether_addr(pd->mac_addr))
2543 memcpy(dev->dev_addr, pd->mac_addr, 6);
2544 else
2545 uc_addr_get(mp, dev->dev_addr);
2546
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002547 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002548 if (pd->rx_queue_size)
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002549 mp->rx_ring_size = pd->rx_queue_size;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002550 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2551 mp->rx_desc_sram_size = pd->rx_sram_size;
2552
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002553 mp->rxq_count = pd->rx_queue_count ? : 1;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002554
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002555 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002556 if (pd->tx_queue_size)
Lennert Buytenheke7d2f4d2009-02-12 14:07:37 +00002557 mp->tx_ring_size = pd->tx_queue_size;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002558 mp->tx_desc_sram_addr = pd->tx_sram_addr;
2559 mp->tx_desc_sram_size = pd->tx_sram_size;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002560
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002561 mp->txq_count = pd->tx_queue_count ? : 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002562}
2563
Florian Fainellic3a07132013-03-22 03:39:28 +00002564static void mv643xx_eth_adjust_link(struct net_device *dev)
2565{
2566 struct mv643xx_eth_private *mp = netdev_priv(dev);
2567
2568 mv643xx_adjust_pscr(mp);
2569}
2570
Lennert Buytenheked944932008-08-26 13:34:19 +02002571static struct phy_device *phy_scan(struct mv643xx_eth_private *mp,
2572 int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002573{
Lennert Buytenheked944932008-08-26 13:34:19 +02002574 struct phy_device *phydev;
2575 int start;
2576 int num;
2577 int i;
Florian Fainellic3a07132013-03-22 03:39:28 +00002578 char phy_id[MII_BUS_ID_SIZE + 3];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002579
Lennert Buytenheked944932008-08-26 13:34:19 +02002580 if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) {
2581 start = phy_addr_get(mp) & 0x1f;
2582 num = 32;
2583 } else {
2584 start = phy_addr & 0x1f;
2585 num = 1;
2586 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002587
Florian Fainellic3a07132013-03-22 03:39:28 +00002588 /* Attempt to connect to the PHY using orion-mdio */
Simon Baatz976c90b2013-03-24 10:34:00 +00002589 phydev = ERR_PTR(-ENODEV);
Lennert Buytenheked944932008-08-26 13:34:19 +02002590 for (i = 0; i < num; i++) {
2591 int addr = (start + i) & 0x1f;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002592
Florian Fainellic3a07132013-03-22 03:39:28 +00002593 snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT,
2594 "orion-mdio-mii", addr);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002595
Florian Fainellic3a07132013-03-22 03:39:28 +00002596 phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link,
2597 PHY_INTERFACE_MODE_GMII);
2598 if (!IS_ERR(phydev)) {
2599 phy_addr_set(mp, addr);
2600 break;
Lennert Buytenheked944932008-08-26 13:34:19 +02002601 }
2602 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002603
Lennert Buytenheked944932008-08-26 13:34:19 +02002604 return phydev;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002605}
2606
Lennert Buytenheked944932008-08-26 13:34:19 +02002607static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex)
James Chapmand0412d92006-01-27 01:15:30 -07002608{
Lennert Buytenheked944932008-08-26 13:34:19 +02002609 struct phy_device *phy = mp->phy;
James Chapmand0412d92006-01-27 01:15:30 -07002610
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002611 phy_reset(mp);
2612
Lennert Buytenheked944932008-08-26 13:34:19 +02002613 if (speed == 0) {
2614 phy->autoneg = AUTONEG_ENABLE;
2615 phy->speed = 0;
2616 phy->duplex = 0;
2617 phy->advertising = phy->supported | ADVERTISED_Autoneg;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002618 } else {
Lennert Buytenheked944932008-08-26 13:34:19 +02002619 phy->autoneg = AUTONEG_DISABLE;
2620 phy->advertising = 0;
2621 phy->speed = speed;
2622 phy->duplex = duplex;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002623 }
Lennert Buytenheked944932008-08-26 13:34:19 +02002624 phy_start_aneg(phy);
James Chapmand0412d92006-01-27 01:15:30 -07002625}
2626
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002627static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2628{
2629 u32 pscr;
2630
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002631 pscr = rdlp(mp, PORT_SERIAL_CONTROL);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002632 if (pscr & SERIAL_PORT_ENABLE) {
2633 pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002634 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002635 }
2636
2637 pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
Lennert Buytenheked944932008-08-26 13:34:19 +02002638 if (mp->phy == NULL) {
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002639 pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2640 if (speed == SPEED_1000)
2641 pscr |= SET_GMII_SPEED_TO_1000;
2642 else if (speed == SPEED_100)
2643 pscr |= SET_MII_SPEED_TO_100;
2644
2645 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2646
2647 pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2648 if (duplex == DUPLEX_FULL)
2649 pscr |= SET_FULL_DUPLEX_MODE;
2650 }
2651
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002652 wrlp(mp, PORT_SERIAL_CONTROL, pscr);
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002653}
2654
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002655static const struct net_device_ops mv643xx_eth_netdev_ops = {
2656 .ndo_open = mv643xx_eth_open,
2657 .ndo_stop = mv643xx_eth_stop,
2658 .ndo_start_xmit = mv643xx_eth_xmit,
2659 .ndo_set_rx_mode = mv643xx_eth_set_rx_mode,
2660 .ndo_set_mac_address = mv643xx_eth_set_mac_address,
kirjanov@gmail.com1d4bd942010-02-16 20:40:11 +00002661 .ndo_validate_addr = eth_validate_addr,
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002662 .ndo_do_ioctl = mv643xx_eth_ioctl,
2663 .ndo_change_mtu = mv643xx_eth_change_mtu,
Michał Mirosławaad59c42011-04-17 00:15:46 +00002664 .ndo_set_features = mv643xx_eth_set_features,
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002665 .ndo_tx_timeout = mv643xx_eth_tx_timeout,
2666 .ndo_get_stats = mv643xx_eth_get_stats,
2667#ifdef CONFIG_NET_POLL_CONTROLLER
2668 .ndo_poll_controller = mv643xx_eth_netpoll,
2669#endif
2670};
2671
Russell King3ae5eae2005-11-09 22:32:44 +00002672static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct mv643xx_eth_platform_data *pd;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002675 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 struct resource *res;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002678 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002680 pd = pdev->dev.platform_data;
2681 if (pd == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002682 dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n");
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002683 return -ENODEV;
2684 }
2685
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002686 if (pd->shared == NULL) {
Joe Perches7542db82011-03-02 17:50:35 +00002687 dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n");
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002688 return -ENODEV;
2689 }
2690
Lennert Buytenheke5ef1de2008-08-28 06:26:23 +02002691 dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 if (!dev)
2693 return -ENOMEM;
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002696 platform_set_drvdata(pdev, mp);
2697
2698 mp->shared = platform_get_drvdata(pd->shared);
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002699 mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002700 mp->port_num = pd->port_number;
2701
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002702 mp->dev = dev;
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002703
Andrew Lunn452503e2011-12-24 01:24:24 +01002704 /*
Andrew Lunn9a43a022012-06-06 06:40:43 +00002705 * Start with a default rate, and if there is a clock, allow
2706 * it to override the default.
Andrew Lunn452503e2011-12-24 01:24:24 +01002707 */
Andrew Lunn9a43a022012-06-06 06:40:43 +00002708 mp->t_clk = 133000000;
Sebastian Hesselbarth20922482013-04-10 23:29:33 +00002709 mp->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn452503e2011-12-24 01:24:24 +01002710 if (!IS_ERR(mp->clk)) {
2711 clk_prepare_enable(mp->clk);
2712 mp->t_clk = clk_get_rate(mp->clk);
Andrew Lunn452503e2011-12-24 01:24:24 +01002713 }
Sebastian Hesselbarth20922482013-04-10 23:29:33 +00002714
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002715 set_params(mp, pd);
Ben Hutchings206d6b32010-09-27 08:30:05 +00002716 netif_set_real_num_tx_queues(dev, mp->txq_count);
2717 netif_set_real_num_rx_queues(dev, mp->rxq_count);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002718
Simon Baatz976c90b2013-03-24 10:34:00 +00002719 if (pd->phy_addr != MV643XX_ETH_PHY_NONE) {
Lennert Buytenheked944932008-08-26 13:34:19 +02002720 mp->phy = phy_scan(mp, pd->phy_addr);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002721
Simon Baatz976c90b2013-03-24 10:34:00 +00002722 if (IS_ERR(mp->phy)) {
2723 err = PTR_ERR(mp->phy);
2724 if (err == -ENODEV)
2725 err = -EPROBE_DEFER;
2726 goto out;
2727 }
Lennert Buytenheked944932008-08-26 13:34:19 +02002728 phy_init(mp, pd->speed, pd->duplex);
Simon Baatz976c90b2013-03-24 10:34:00 +00002729 }
Lennert Buytenhek6bdf5762009-02-12 14:06:46 +00002730
2731 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Lennert Buytenheked944932008-08-26 13:34:19 +02002732
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002733 init_pscr(mp, pd->speed, pd->duplex);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002734
Lennert Buytenhek4ff34952008-09-19 05:04:57 +02002735
2736 mib_counters_clear(mp);
2737
2738 init_timer(&mp->mib_counters_timer);
2739 mp->mib_counters_timer.data = (unsigned long)mp;
2740 mp->mib_counters_timer.function = mib_counters_timer_wrapper;
2741 mp->mib_counters_timer.expires = jiffies + 30 * HZ;
2742 add_timer(&mp->mib_counters_timer);
2743
2744 spin_lock_init(&mp->mib_counters_lock);
2745
2746 INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
2747
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002748 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
2749
2750 init_timer(&mp->rx_oom);
2751 mp->rx_oom.data = (unsigned long)mp;
2752 mp->rx_oom.function = oom_timer_wrapper;
2753
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2756 BUG_ON(!res);
2757 dev->irq = res->start;
2758
Lennert Buytenhekea8a8642009-02-24 15:41:40 +00002759 dev->netdev_ops = &mv643xx_eth_netdev_ops;
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 dev->base_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Sebastian Hesselbarth3619eb82013-04-11 23:20:00 +00002764 dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Michał Mirosławaad59c42011-04-17 00:15:46 +00002765 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Lennert Buytenheke32b6612008-07-24 06:22:59 +02002766 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
James Chapmanc28a4f82006-01-27 01:13:15 -07002767
Jiri Pirko01789342011-08-16 06:29:00 +00002768 dev->priv_flags |= IFF_UNICAST_FLT;
2769
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002770 SET_NETDEV_DEV(dev, &pdev->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002771
2772 if (mp->shared->win_protect)
2773 wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
2774
Lennert Buytenheka5fe3612009-02-24 15:41:47 +00002775 netif_carrier_off(dev);
2776
Lennert Buytenhekb5e86db2009-04-08 15:42:46 -07002777 wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE);
2778
Lennert Buytenhek4fb0a542009-02-24 15:42:05 +00002779 set_rx_coal(mp, 250);
Lennert Buytenheka5fe3612009-02-24 15:41:47 +00002780 set_tx_coal(mp, 0);
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 err = register_netdev(dev);
2783 if (err)
2784 goto out;
2785
Joe Perches7542db82011-03-02 17:50:35 +00002786 netdev_notice(dev, "port %d with MAC address %pM\n",
2787 mp->port_num, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002789 if (mp->tx_desc_sram_size > 0)
Joe Perches7542db82011-03-02 17:50:35 +00002790 netdev_notice(dev, "configured with sram\n");
Nicolas DETb1529872005-10-28 17:46:30 -07002791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 return 0;
2793
2794out:
Sebastian Hesselbarth20922482013-04-10 23:29:33 +00002795 if (!IS_ERR(mp->clk))
Simon Baatzbaffab22012-07-19 00:04:09 +02002796 clk_disable_unprepare(mp->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 free_netdev(dev);
2798
2799 return err;
2800}
2801
Russell King3ae5eae2005-11-09 22:32:44 +00002802static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002804 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002806 unregister_netdev(mp->dev);
Lennert Buytenheked944932008-08-26 13:34:19 +02002807 if (mp->phy != NULL)
2808 phy_detach(mp->phy);
Tejun Heo23f333a2010-12-12 16:45:14 +01002809 cancel_work_sync(&mp->tx_timeout_task);
Andrew Lunn452503e2011-12-24 01:24:24 +01002810
Sebastian Hesselbarth20922482013-04-10 23:29:33 +00002811 if (!IS_ERR(mp->clk))
Andrew Lunn452503e2011-12-24 01:24:24 +01002812 clk_disable_unprepare(mp->clk);
Andrew Lunn9a43a022012-06-06 06:40:43 +00002813
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002814 free_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Russell King3ae5eae2005-11-09 22:32:44 +00002816 platform_set_drvdata(pdev, NULL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002817
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return 0;
2819}
2820
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002821static void mv643xx_eth_shutdown(struct platform_device *pdev)
2822{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002823 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002824
2825 /* Mask all interrupts on ethernet port */
Lennert Buytenhek37a60842008-11-20 03:57:36 -08002826 wrlp(mp, INT_MASK, 0);
2827 rdlp(mp, INT_MASK);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002828
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002829 if (netif_running(mp->dev))
2830 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002831}
2832
Russell King3ae5eae2005-11-09 22:32:44 +00002833static struct platform_driver mv643xx_eth_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002834 .probe = mv643xx_eth_probe,
2835 .remove = mv643xx_eth_remove,
2836 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002837 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002838 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002839 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002840 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841};
2842
Lennert Buytenheke5371492008-06-01 02:18:13 +02002843static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 int rc;
2846
Russell King3ae5eae2005-11-09 22:32:44 +00002847 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002849 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002851 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002853
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return rc;
2855}
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002856module_init(mv643xx_eth_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Lennert Buytenheke5371492008-06-01 02:18:13 +02002858static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Russell King3ae5eae2005-11-09 22:32:44 +00002860 platform_driver_unregister(&mv643xx_eth_driver);
2861 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862}
Lennert Buytenheke5371492008-06-01 02:18:13 +02002863module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Lennert Buytenhek45675bc2008-06-02 02:02:30 +02002865MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
2866 "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002868MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07002869MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002870MODULE_ALIAS("platform:" MV643XX_ETH_NAME);