blob: b41860dc5b5e141109e0eec4d1f984f8befb2217 [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 Hering3bb8a182006-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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenheke5371492008-06-01 02:18:13 +020057static char mv643xx_eth_driver_name[] = "mv643xx_eth";
Lennert Buytenhekc4560312008-08-24 07:33:05 +020058static char mv643xx_eth_driver_version[] = "1.3";
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020059
Lennert Buytenheke5371492008-06-01 02:18:13 +020060#define MV643XX_ETH_CHECKSUM_OFFLOAD_TX
61#define MV643XX_ETH_NAPI
62#define MV643XX_ETH_TX_FAST_REFILL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020063
Lennert Buytenheke5371492008-06-01 02:18:13 +020064#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020065#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
66#else
67#define MAX_DESCS_PER_SKB 1
68#endif
69
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020070/*
71 * Registers shared between all ports.
72 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020073#define PHY_ADDR 0x0000
74#define SMI_REG 0x0004
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +020075#define SMI_BUSY 0x10000000
76#define SMI_READ_VALID 0x08000000
77#define SMI_OPCODE_READ 0x04000000
78#define SMI_OPCODE_WRITE 0x00000000
79#define ERR_INT_CAUSE 0x0080
80#define ERR_INT_SMI_DONE 0x00000010
81#define ERR_INT_MASK 0x0084
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020082#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
83#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
84#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
85#define WINDOW_BAR_ENABLE 0x0290
86#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020087
88/*
89 * Per-port registers.
90 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020091#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020092#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020093#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
94#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
95#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
96#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
97#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
98#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +020099#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhekae9ae062008-07-15 02:15:24 +0200100#define TX_IN_PROGRESS 0x00000080
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +0200101#define PORT_SPEED_MASK 0x00000030
102#define PORT_SPEED_1000 0x00000010
103#define PORT_SPEED_100 0x00000020
104#define PORT_SPEED_10 0x00000000
105#define FLOW_CONTROL_ENABLED 0x00000008
106#define FULL_DUPLEX 0x00000004
Lennert Buytenhek81600eea92008-07-14 14:29:40 +0200107#define LINK_UP 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200108#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200109#define TXQ_FIX_PRIO_CONF(p) (0x044c + ((p) << 10))
110#define TX_BW_RATE(p) (0x0450 + ((p) << 10))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200111#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200112#define TX_BW_BURST(p) (0x045c + ((p) << 10))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200113#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200114#define INT_TX_END_0 0x00080000
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200115#define INT_TX_END 0x07f80000
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200116#define INT_RX 0x0007fbfc
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200117#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200118#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200119#define INT_EXT_LINK 0x00100000
120#define INT_EXT_PHY 0x00010000
121#define INT_EXT_TX_ERROR_0 0x00000100
122#define INT_EXT_TX_0 0x00000001
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200123#define INT_EXT_TX 0x0000ffff
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200124#define INT_MASK(p) (0x0468 + ((p) << 10))
125#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
126#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200127#define TXQ_FIX_PRIO_CONF_MOVED(p) (0x04dc + ((p) << 10))
128#define TX_BW_RATE_MOVED(p) (0x04e0 + ((p) << 10))
129#define TX_BW_MTU_MOVED(p) (0x04e8 + ((p) << 10))
130#define TX_BW_BURST_MOVED(p) (0x04ec + ((p) << 10))
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200131#define RXQ_CURRENT_DESC_PTR(p, q) (0x060c + ((p) << 10) + ((q) << 4))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200132#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200133#define TXQ_CURRENT_DESC_PTR(p, q) (0x06c0 + ((p) << 10) + ((q) << 2))
134#define TXQ_BW_TOKENS(p, q) (0x0700 + ((p) << 10) + ((q) << 4))
135#define TXQ_BW_CONF(p, q) (0x0704 + ((p) << 10) + ((q) << 4))
136#define TXQ_BW_WRR_CONF(p, q) (0x0708 + ((p) << 10) + ((q) << 4))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200137#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
138#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
139#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
140#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200142
143/*
144 * SDMA configuration register.
145 */
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200146#define RX_BURST_SIZE_16_64BIT (4 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200148#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200149#define TX_BURST_SIZE_16_64BIT (4 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200150
151#if defined(__BIG_ENDIAN)
152#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200153 RX_BURST_SIZE_16_64BIT | \
154 TX_BURST_SIZE_16_64BIT
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200155#elif defined(__LITTLE_ENDIAN)
156#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200157 RX_BURST_SIZE_16_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200158 BLM_RX_NO_SWAP | \
159 BLM_TX_NO_SWAP | \
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200160 TX_BURST_SIZE_16_64BIT
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200161#else
162#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
163#endif
164
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200165
166/*
167 * Port serial control register.
168 */
169#define SET_MII_SPEED_TO_100 (1 << 24)
170#define SET_GMII_SPEED_TO_1000 (1 << 23)
171#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172#define MAX_RX_PACKET_9700BYTE (5 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200173#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
174#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
175#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
176#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
177#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
178#define FORCE_LINK_PASS (1 << 1)
179#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200180
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200181#define DEFAULT_RX_QUEUE_SIZE 400
182#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200183
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200184
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200185/*
186 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200187 */
188#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200189struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200190 u16 byte_cnt; /* Descriptor buffer byte count */
191 u16 buf_size; /* Buffer size */
192 u32 cmd_sts; /* Descriptor command status */
193 u32 next_desc_ptr; /* Next descriptor pointer */
194 u32 buf_ptr; /* Descriptor buffer pointer */
195};
196
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200197struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200198 u16 byte_cnt; /* buffer byte count */
199 u16 l4i_chk; /* CPU provided TCP checksum */
200 u32 cmd_sts; /* Command/status field */
201 u32 next_desc_ptr; /* Pointer to next descriptor */
202 u32 buf_ptr; /* pointer to buffer for this descriptor*/
203};
204#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200205struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200206 u32 cmd_sts; /* Descriptor command status */
207 u16 buf_size; /* Buffer size */
208 u16 byte_cnt; /* Descriptor buffer byte count */
209 u32 buf_ptr; /* Descriptor buffer pointer */
210 u32 next_desc_ptr; /* Next descriptor pointer */
211};
212
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200213struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200214 u32 cmd_sts; /* Command/status field */
215 u16 l4i_chk; /* CPU provided TCP checksum */
216 u16 byte_cnt; /* buffer byte count */
217 u32 buf_ptr; /* pointer to buffer for this descriptor*/
218 u32 next_desc_ptr; /* Pointer to next descriptor */
219};
220#else
221#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
222#endif
223
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200224/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200225#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200226
227/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200228#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200229
230/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200231#define LAYER_4_CHECKSUM_OK 0x40000000
232#define RX_ENABLE_INTERRUPT 0x20000000
233#define RX_FIRST_DESC 0x08000000
234#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200235
236/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200237#define TX_ENABLE_INTERRUPT 0x00800000
238#define GEN_CRC 0x00400000
239#define TX_FIRST_DESC 0x00200000
240#define TX_LAST_DESC 0x00100000
241#define ZERO_PADDING 0x00080000
242#define GEN_IP_V4_CHECKSUM 0x00040000
243#define GEN_TCP_UDP_CHECKSUM 0x00020000
244#define UDP_FRAME 0x00010000
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200245#define MAC_HDR_EXTRA_4_BYTES 0x00008000
246#define MAC_HDR_EXTRA_8_BYTES 0x00000200
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200247
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200248#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200249
250
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200251/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200252struct mv643xx_eth_shared_private {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200253 /*
254 * Ethernet controller base address.
255 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200256 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200257
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200258 /*
259 * Protects access to SMI_REG, which is shared between ports.
260 */
Lennert Buytenhek2b3ba0e2008-08-24 05:41:09 +0200261 struct mutex phy_lock;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200262
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200263 /*
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200264 * If we have access to the error interrupt pin (which is
265 * somewhat misnamed as it not only reflects internal errors
266 * but also reflects SMI completion), use that to wait for
267 * SMI access completion instead of polling the SMI busy bit.
268 */
269 int err_interrupt;
270 wait_queue_head_t smi_busy_wait;
271
272 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200273 * Per-port MBUS window access register value.
274 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200275 u32 win_protect;
276
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200277 /*
278 * Hardware-specific parameters.
279 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200280 unsigned int t_clk;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +0200281 int extended_rx_coal_limit;
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200282 int tx_bw_control_moved;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200283};
284
285
286/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200287struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200288 u64 good_octets_received;
289 u32 bad_octets_received;
290 u32 internal_mac_transmit_err;
291 u32 good_frames_received;
292 u32 bad_frames_received;
293 u32 broadcast_frames_received;
294 u32 multicast_frames_received;
295 u32 frames_64_octets;
296 u32 frames_65_to_127_octets;
297 u32 frames_128_to_255_octets;
298 u32 frames_256_to_511_octets;
299 u32 frames_512_to_1023_octets;
300 u32 frames_1024_to_max_octets;
301 u64 good_octets_sent;
302 u32 good_frames_sent;
303 u32 excessive_collision;
304 u32 multicast_frames_sent;
305 u32 broadcast_frames_sent;
306 u32 unrec_mac_control_received;
307 u32 fc_sent;
308 u32 good_fc_received;
309 u32 bad_fc_received;
310 u32 undersize_received;
311 u32 fragments_received;
312 u32 oversize_received;
313 u32 jabber_received;
314 u32 mac_receive_error;
315 u32 bad_crc_event;
316 u32 collision;
317 u32 late_collision;
318};
319
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200320struct rx_queue {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200321 int index;
322
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200323 int rx_ring_size;
324
325 int rx_desc_count;
326 int rx_curr_desc;
327 int rx_used_desc;
328
329 struct rx_desc *rx_desc_area;
330 dma_addr_t rx_desc_dma;
331 int rx_desc_area_size;
332 struct sk_buff **rx_skb;
333
334 struct timer_list rx_oom;
335};
336
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200337struct tx_queue {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200338 int index;
339
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200340 int tx_ring_size;
341
342 int tx_desc_count;
343 int tx_curr_desc;
344 int tx_used_desc;
345
346 struct tx_desc *tx_desc_area;
347 dma_addr_t tx_desc_dma;
348 int tx_desc_area_size;
349 struct sk_buff **tx_skb;
350};
351
Lennert Buytenheke5371492008-06-01 02:18:13 +0200352struct mv643xx_eth_private {
353 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200354 int port_num;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200355
356 struct net_device *dev;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200357
358 struct mv643xx_eth_shared_private *shared_smi;
359 int phy_addr;
360
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200361 spinlock_t lock;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200362
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200363 struct mib_counters mib_counters;
364 struct work_struct tx_timeout_task;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200365 struct mii_if_info mii;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200366
367 /*
368 * RX state.
369 */
370 int default_rx_ring_size;
371 unsigned long rx_desc_sram_addr;
372 int rx_desc_sram_size;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200373 u8 rxq_mask;
374 int rxq_primary;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200375 struct napi_struct napi;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200376 struct rx_queue rxq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200377
378 /*
379 * TX state.
380 */
381 int default_tx_ring_size;
382 unsigned long tx_desc_sram_addr;
383 int tx_desc_sram_size;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200384 u8 txq_mask;
385 int txq_primary;
386 struct tx_queue txq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200387#ifdef MV643XX_ETH_TX_FAST_REFILL
388 int tx_clean_threshold;
389#endif
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200390};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200392
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200393/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200394static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200396 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
Lennert Buytenheke5371492008-06-01 02:18:13 +0200399static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200401 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200404
405/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200406static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200408 return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200409}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200411static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
412{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200413 return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200414}
415
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200416static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200417{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200418 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200419 wrl(mp, RXQ_COMMAND(mp->port_num), 1 << rxq->index);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200420}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700421
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200422static void rxq_disable(struct rx_queue *rxq)
423{
424 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200425 u8 mask = 1 << rxq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200426
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200427 wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
428 while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
429 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200430}
431
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200432static void txq_reset_hw_ptr(struct tx_queue *txq)
433{
434 struct mv643xx_eth_private *mp = txq_to_mp(txq);
435 int off = TXQ_CURRENT_DESC_PTR(mp->port_num, txq->index);
436 u32 addr;
437
438 addr = (u32)txq->tx_desc_dma;
439 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
440 wrl(mp, off, addr);
441}
442
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200443static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200444{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200445 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200446 wrl(mp, TXQ_COMMAND(mp->port_num), 1 << txq->index);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200447}
448
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200449static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200450{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200451 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200452 u8 mask = 1 << txq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200453
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200454 wrl(mp, TXQ_COMMAND(mp->port_num), mask << 8);
455 while (rdl(mp, TXQ_COMMAND(mp->port_num)) & mask)
456 udelay(10);
457}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200458
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200459static void __txq_maybe_wake(struct tx_queue *txq)
460{
461 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200462
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200463 /*
464 * netif_{stop,wake}_queue() flow control only applies to
465 * the primary queue.
466 */
467 BUG_ON(txq->index != mp->txq_primary);
468
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200469 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_DESCS_PER_SKB)
470 netif_wake_queue(mp->dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200471}
472
473
474/* rx ***********************************************************************/
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200475static void txq_reclaim(struct tx_queue *txq, int force);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200476
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200477static void rxq_refill(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200478{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200479 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200480 unsigned long flags;
481
482 spin_lock_irqsave(&mp->lock, flags);
483
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200484 while (rxq->rx_desc_count < rxq->rx_ring_size) {
485 int skb_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200486 struct sk_buff *skb;
487 int unaligned;
488 int rx;
489
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200490 /*
491 * Reserve 2+14 bytes for an ethernet header (the
492 * hardware automatically prepends 2 bytes of dummy
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200493 * data to each received packet), 16 bytes for up to
494 * four VLAN tags, and 4 bytes for the trailing FCS
495 * -- 36 bytes total.
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200496 */
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200497 skb_size = mp->dev->mtu + 36;
498
499 /*
500 * Make sure that the skb size is a multiple of 8
501 * bytes, as the lower three bits of the receive
502 * descriptor's buffer size field are ignored by
503 * the hardware.
504 */
505 skb_size = (skb_size + 7) & ~7;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200506
507 skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200508 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200510
Ralf Baechle908b6372007-02-26 19:52:06 +0000511 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700512 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000513 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200514
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200515 rxq->rx_desc_count++;
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200516
517 rx = rxq->rx_used_desc++;
518 if (rxq->rx_used_desc == rxq->rx_ring_size)
519 rxq->rx_used_desc = 0;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200520
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200521 rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
522 skb_size, DMA_FROM_DEVICE);
523 rxq->rx_desc_area[rx].buf_size = skb_size;
524 rxq->rx_skb[rx] = skb;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200525 wmb();
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200526 rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200527 RX_ENABLE_INTERRUPT;
528 wmb();
529
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200530 /*
531 * The hardware automatically prepends 2 bytes of
532 * dummy data to each received packet, so that the
533 * IP header ends up 16-byte aligned.
534 */
535 skb_reserve(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200537
Lennert Buytenhek92c70f22008-08-24 01:59:05 +0200538 if (rxq->rx_desc_count != rxq->rx_ring_size)
539 mod_timer(&rxq->rx_oom, jiffies + (HZ / 10));
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200540
541 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200544static inline void rxq_refill_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200546 rxq_refill((struct rx_queue *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200549static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200551 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
552 struct net_device_stats *stats = &mp->dev->stats;
553 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200555 rx = 0;
Lennert Buytenhek9e1f3772008-08-24 02:33:47 +0200556 while (rx < budget && rxq->rx_desc_count) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200557 struct rx_desc *rx_desc;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200558 unsigned int cmd_sts;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200559 struct sk_buff *skb;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200560 unsigned long flags;
561
562 spin_lock_irqsave(&mp->lock, flags);
563
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200564 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200565
566 cmd_sts = rx_desc->cmd_sts;
567 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
568 spin_unlock_irqrestore(&mp->lock, flags);
569 break;
570 }
571 rmb();
572
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200573 skb = rxq->rx_skb[rxq->rx_curr_desc];
574 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200575
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200576 rxq->rx_curr_desc++;
577 if (rxq->rx_curr_desc == rxq->rx_ring_size)
578 rxq->rx_curr_desc = 0;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200579
580 spin_unlock_irqrestore(&mp->lock, flags);
581
Lennert Buytenhek3a499482008-08-24 07:19:48 +0200582 dma_unmap_single(NULL, rx_desc->buf_ptr,
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200583 rx_desc->buf_size, DMA_FROM_DEVICE);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200584 rxq->rx_desc_count--;
585 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700586
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700587 /*
588 * Update statistics.
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200589 *
590 * Note that the descriptor byte count includes 2 dummy
591 * bytes automatically inserted by the hardware at the
592 * start of the packet (which we don't count), and a 4
593 * byte CRC at the end of the packet (which we do count).
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 stats->rx_packets++;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200596 stats->rx_bytes += rx_desc->byte_cnt - 2;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200599 * In case we received a packet without first / last bits
600 * on, or the error summary bit is set, the packet needs
601 * to be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200603 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200604 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200605 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 stats->rx_dropped++;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200607
Lennert Buytenhek96587662008-06-01 10:31:56 +0200608 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200609 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 if (net_ratelimit())
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200611 dev_printk(KERN_ERR, &mp->dev->dev,
612 "received packet spanning "
613 "multiple descriptors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200615
Lennert Buytenhek96587662008-06-01 10:31:56 +0200616 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 stats->rx_errors++;
618
619 dev_kfree_skb_irq(skb);
620 } else {
621 /*
622 * The -4 is for the CRC in the trailer of the
623 * received packet
624 */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200625 skb_put(skb, rx_desc->byte_cnt - 2 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Lennert Buytenhek96587662008-06-01 10:31:56 +0200627 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 skb->ip_summed = CHECKSUM_UNNECESSARY;
629 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200630 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200632 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200633#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 netif_receive_skb(skb);
635#else
636 netif_rx(skb);
637#endif
638 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200639
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200640 mp->dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200642
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200643 rxq_refill(rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200645 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Lennert Buytenheke5371492008-06-01 02:18:13 +0200648#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200649static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200651 struct mv643xx_eth_private *mp;
652 int rx;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200653 int i;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200654
655 mp = container_of(napi, struct mv643xx_eth_private, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Lennert Buytenheke5371492008-06-01 02:18:13 +0200657#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (++mp->tx_clean_threshold > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 mp->tx_clean_threshold = 0;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200660 for (i = 0; i < 8; i++)
661 if (mp->txq_mask & (1 << i))
662 txq_reclaim(mp->txq + i, 0);
Lennert Buytenhek4dfc1c82008-07-15 13:34:51 +0200663
664 if (netif_carrier_ok(mp->dev)) {
Lennert Buytenhek8e0b1bf2008-08-24 02:30:42 +0200665 spin_lock_irq(&mp->lock);
Lennert Buytenhek4dfc1c82008-07-15 13:34:51 +0200666 __txq_maybe_wake(mp->txq + mp->txq_primary);
Lennert Buytenhek8e0b1bf2008-08-24 02:30:42 +0200667 spin_unlock_irq(&mp->lock);
Lennert Buytenhek4dfc1c82008-07-15 13:34:51 +0200668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
670#endif
671
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200672 rx = 0;
673 for (i = 7; rx < budget && i >= 0; i--)
674 if (mp->rxq_mask & (1 << i))
675 rx += rxq_process(mp->rxq + i, budget - rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200677 if (rx < budget) {
678 netif_rx_complete(mp->dev, napi);
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200679 wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200682 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684#endif
685
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200686
687/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700688static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
689{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200690 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700691
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700692 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200693 skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
694 if (fragp->size <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700695 return 1;
696 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200697
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700698 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700699}
700
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200701static int txq_alloc_desc_index(struct tx_queue *txq)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700702{
703 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700704
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200705 BUG_ON(txq->tx_desc_count >= txq->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700706
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200707 tx_desc_curr = txq->tx_curr_desc++;
708 if (txq->tx_curr_desc == txq->tx_ring_size)
709 txq->tx_curr_desc = 0;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700710
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200711 BUG_ON(txq->tx_curr_desc == txq->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700712
713 return tx_desc_curr;
714}
715
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200716static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700717{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200718 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700719 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700720
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200721 for (frag = 0; frag < nr_frags; frag++) {
722 skb_frag_t *this_frag;
723 int tx_index;
724 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700725
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200726 this_frag = &skb_shinfo(skb)->frags[frag];
727 tx_index = txq_alloc_desc_index(txq);
728 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700729
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200730 /*
731 * The last fragment will generate an interrupt
732 * which will free the skb on TX completion.
733 */
734 if (frag == nr_frags - 1) {
735 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
736 ZERO_PADDING | TX_LAST_DESC |
737 TX_ENABLE_INTERRUPT;
738 txq->tx_skb[tx_index] = skb;
739 } else {
740 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
741 txq->tx_skb[tx_index] = NULL;
742 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700743
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700744 desc->l4i_chk = 0;
745 desc->byte_cnt = this_frag->size;
746 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
747 this_frag->page_offset,
748 this_frag->size,
749 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700750 }
751}
752
Byron Bradley324ff2c2008-02-04 23:47:15 -0800753static inline __be16 sum16_as_be(__sum16 sum)
754{
755 return (__force __be16)sum;
756}
757
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200758static void txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700759{
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200760 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200761 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700762 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200763 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700764 u32 cmd_sts;
765 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700766
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200767 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700768
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200769 tx_index = txq_alloc_desc_index(txq);
770 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700771
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700772 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200773 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700774
775 length = skb_headlen(skb);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200776 txq->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700777 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200778 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700779 length = skb->len;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200780 txq->tx_skb[tx_index] = skb;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700781 }
782
783 desc->byte_cnt = length;
784 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700785
Patrick McHardy84fa7932006-08-29 16:44:56 -0700786 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200787 int mac_hdr_len;
788
789 BUG_ON(skb->protocol != htons(ETH_P_IP) &&
790 skb->protocol != htons(ETH_P_8021Q));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700791
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200792 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
793 GEN_IP_V4_CHECKSUM |
794 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700795
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200796 mac_hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
797 switch (mac_hdr_len - ETH_HLEN) {
798 case 0:
799 break;
800 case 4:
801 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
802 break;
803 case 8:
804 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
805 break;
806 case 12:
807 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
808 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
809 break;
810 default:
811 if (net_ratelimit())
812 dev_printk(KERN_ERR, &txq_to_mp(txq)->dev->dev,
813 "mac header length is %d?!\n", mac_hdr_len);
814 break;
815 }
816
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700817 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700818 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200819 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800820 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700821 break;
822 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800823 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700824 break;
825 default:
826 BUG();
827 }
828 } else {
829 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200830 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700831 desc->l4i_chk = 0;
832 }
833
834 /* ensure all other descriptors are written before first cmd_sts */
835 wmb();
836 desc->cmd_sts = cmd_sts;
837
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200838 /* clear TX_END interrupt status */
839 wrl(mp, INT_CAUSE(mp->port_num), ~(INT_TX_END_0 << txq->index));
840 rdl(mp, INT_CAUSE(mp->port_num));
841
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700842 /* ensure all descriptors are written before poking hardware */
843 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200844 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700845
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200846 txq->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700847}
848
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200849static int mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200851 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700852 struct net_device_stats *stats = &dev->stats;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200853 struct tx_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700856 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
857 stats->tx_dropped++;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200858 dev_printk(KERN_DEBUG, &dev->dev,
859 "failed to linearize skb with tiny "
860 "unaligned fragment\n");
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700861 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 spin_lock_irqsave(&mp->lock, flags);
865
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200866 txq = mp->txq + mp->txq_primary;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200867
868 if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB) {
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700869 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200870 if (txq->index == mp->txq_primary && net_ratelimit())
871 dev_printk(KERN_ERR, &dev->dev,
872 "primary tx queue full?!\n");
873 kfree_skb(skb);
874 return NETDEV_TX_OK;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700875 }
876
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200877 txq_submit_skb(txq, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700878 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 stats->tx_packets++;
880 dev->trans_start = jiffies;
881
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200882 if (txq->index == mp->txq_primary) {
883 int entries_left;
884
885 entries_left = txq->tx_ring_size - txq->tx_desc_count;
886 if (entries_left < MAX_DESCS_PER_SKB)
887 netif_stop_queue(dev);
888 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 spin_unlock_irqrestore(&mp->lock, flags);
891
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700892 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200895
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200896/* tx rate control **********************************************************/
897/*
898 * Set total maximum TX rate (shared by all TX queues for this port)
899 * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
900 */
901static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
902{
903 int token_rate;
904 int mtu;
905 int bucket_size;
906
907 token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
908 if (token_rate > 1023)
909 token_rate = 1023;
910
911 mtu = (mp->dev->mtu + 255) >> 8;
912 if (mtu > 63)
913 mtu = 63;
914
915 bucket_size = (burst + 255) >> 8;
916 if (bucket_size > 65535)
917 bucket_size = 65535;
918
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200919 if (mp->shared->tx_bw_control_moved) {
920 wrl(mp, TX_BW_RATE_MOVED(mp->port_num), token_rate);
921 wrl(mp, TX_BW_MTU_MOVED(mp->port_num), mtu);
922 wrl(mp, TX_BW_BURST_MOVED(mp->port_num), bucket_size);
923 } else {
924 wrl(mp, TX_BW_RATE(mp->port_num), token_rate);
925 wrl(mp, TX_BW_MTU(mp->port_num), mtu);
926 wrl(mp, TX_BW_BURST(mp->port_num), bucket_size);
927 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200928}
929
930static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
931{
932 struct mv643xx_eth_private *mp = txq_to_mp(txq);
933 int token_rate;
934 int bucket_size;
935
936 token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
937 if (token_rate > 1023)
938 token_rate = 1023;
939
940 bucket_size = (burst + 255) >> 8;
941 if (bucket_size > 65535)
942 bucket_size = 65535;
943
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200944 wrl(mp, TXQ_BW_TOKENS(mp->port_num, txq->index), token_rate << 14);
945 wrl(mp, TXQ_BW_CONF(mp->port_num, txq->index),
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200946 (bucket_size << 10) | token_rate);
947}
948
949static void txq_set_fixed_prio_mode(struct tx_queue *txq)
950{
951 struct mv643xx_eth_private *mp = txq_to_mp(txq);
952 int off;
953 u32 val;
954
955 /*
956 * Turn on fixed priority mode.
957 */
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200958 if (mp->shared->tx_bw_control_moved)
959 off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
960 else
961 off = TXQ_FIX_PRIO_CONF(mp->port_num);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200962
963 val = rdl(mp, off);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200964 val |= 1 << txq->index;
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200965 wrl(mp, off, val);
966}
967
968static void txq_set_wrr(struct tx_queue *txq, int weight)
969{
970 struct mv643xx_eth_private *mp = txq_to_mp(txq);
971 int off;
972 u32 val;
973
974 /*
975 * Turn off fixed priority mode.
976 */
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200977 if (mp->shared->tx_bw_control_moved)
978 off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
979 else
980 off = TXQ_FIX_PRIO_CONF(mp->port_num);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200981
982 val = rdl(mp, off);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200983 val &= ~(1 << txq->index);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200984 wrl(mp, off, val);
985
986 /*
987 * Configure WRR weight for this queue.
988 */
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200989 off = TXQ_BW_WRR_CONF(mp->port_num, txq->index);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200990
991 val = rdl(mp, off);
992 val = (val & ~0xff) | (weight & 0xff);
993 wrl(mp, off, val);
994}
995
996
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200997/* mii management interface *************************************************/
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200998static irqreturn_t mv643xx_eth_err_irq(int irq, void *dev_id)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200999{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001000 struct mv643xx_eth_shared_private *msp = dev_id;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001001
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001002 if (readl(msp->base + ERR_INT_CAUSE) & ERR_INT_SMI_DONE) {
1003 writel(~ERR_INT_SMI_DONE, msp->base + ERR_INT_CAUSE);
1004 wake_up(&msp->smi_busy_wait);
1005 return IRQ_HANDLED;
1006 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001007
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001008 return IRQ_NONE;
1009}
1010
1011static int smi_is_done(struct mv643xx_eth_shared_private *msp)
1012{
1013 return !(readl(msp->base + SMI_REG) & SMI_BUSY);
1014}
1015
1016static int smi_wait_ready(struct mv643xx_eth_shared_private *msp)
1017{
1018 if (msp->err_interrupt == NO_IRQ) {
1019 int i;
1020
1021 for (i = 0; !smi_is_done(msp); i++) {
1022 if (i == 10)
1023 return -ETIMEDOUT;
1024 msleep(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001025 }
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001026
1027 return 0;
1028 }
1029
1030 if (!wait_event_timeout(msp->smi_busy_wait, smi_is_done(msp),
1031 msecs_to_jiffies(100)))
1032 return -ETIMEDOUT;
1033
1034 return 0;
1035}
1036
1037static int smi_reg_read(struct mv643xx_eth_private *mp,
1038 unsigned int addr, unsigned int reg)
1039{
1040 struct mv643xx_eth_shared_private *msp = mp->shared_smi;
1041 void __iomem *smi_reg = msp->base + SMI_REG;
1042 int ret;
1043
1044 mutex_lock(&msp->phy_lock);
1045
1046 if (smi_wait_ready(msp)) {
1047 printk("%s: SMI bus busy timeout\n", mp->dev->name);
1048 ret = -ETIMEDOUT;
1049 goto out;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001050 }
1051
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001052 writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001053
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001054 if (smi_wait_ready(msp)) {
1055 printk("%s: SMI bus busy timeout\n", mp->dev->name);
1056 ret = -ETIMEDOUT;
1057 goto out;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001058 }
1059
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001060 ret = readl(smi_reg);
1061 if (!(ret & SMI_READ_VALID)) {
1062 printk("%s: SMI bus read not valid\n", mp->dev->name);
1063 ret = -ENODEV;
1064 goto out;
1065 }
1066
1067 ret &= 0xffff;
1068
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001069out:
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001070 mutex_unlock(&msp->phy_lock);
1071
1072 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001073}
1074
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001075static int smi_reg_write(struct mv643xx_eth_private *mp, unsigned int addr,
1076 unsigned int reg, unsigned int value)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001077{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001078 struct mv643xx_eth_shared_private *msp = mp->shared_smi;
1079 void __iomem *smi_reg = msp->base + SMI_REG;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001080
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001081 mutex_lock(&msp->phy_lock);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001082
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001083 if (smi_wait_ready(msp)) {
1084 printk("%s: SMI bus busy timeout\n", mp->dev->name);
1085 mutex_unlock(&msp->phy_lock);
1086 return -ETIMEDOUT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001087 }
1088
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001089 writel(SMI_OPCODE_WRITE | (reg << 21) |
1090 (addr << 16) | (value & 0xffff), smi_reg);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001091
1092 mutex_unlock(&msp->phy_lock);
1093
1094 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001095}
1096
1097
1098/* mib counters *************************************************************/
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001099static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001100{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001101 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001102}
1103
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001104static void mib_counters_clear(struct mv643xx_eth_private *mp)
1105{
1106 int i;
1107
1108 for (i = 0; i < 0x80; i += 4)
1109 mib_read(mp, i);
1110}
1111
1112static void mib_counters_update(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001113{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001114 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001115
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001116 p->good_octets_received += mib_read(mp, 0x00);
1117 p->good_octets_received += (u64)mib_read(mp, 0x04) << 32;
1118 p->bad_octets_received += mib_read(mp, 0x08);
1119 p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1120 p->good_frames_received += mib_read(mp, 0x10);
1121 p->bad_frames_received += mib_read(mp, 0x14);
1122 p->broadcast_frames_received += mib_read(mp, 0x18);
1123 p->multicast_frames_received += mib_read(mp, 0x1c);
1124 p->frames_64_octets += mib_read(mp, 0x20);
1125 p->frames_65_to_127_octets += mib_read(mp, 0x24);
1126 p->frames_128_to_255_octets += mib_read(mp, 0x28);
1127 p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1128 p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1129 p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1130 p->good_octets_sent += mib_read(mp, 0x38);
1131 p->good_octets_sent += (u64)mib_read(mp, 0x3c) << 32;
1132 p->good_frames_sent += mib_read(mp, 0x40);
1133 p->excessive_collision += mib_read(mp, 0x44);
1134 p->multicast_frames_sent += mib_read(mp, 0x48);
1135 p->broadcast_frames_sent += mib_read(mp, 0x4c);
1136 p->unrec_mac_control_received += mib_read(mp, 0x50);
1137 p->fc_sent += mib_read(mp, 0x54);
1138 p->good_fc_received += mib_read(mp, 0x58);
1139 p->bad_fc_received += mib_read(mp, 0x5c);
1140 p->undersize_received += mib_read(mp, 0x60);
1141 p->fragments_received += mib_read(mp, 0x64);
1142 p->oversize_received += mib_read(mp, 0x68);
1143 p->jabber_received += mib_read(mp, 0x6c);
1144 p->mac_receive_error += mib_read(mp, 0x70);
1145 p->bad_crc_event += mib_read(mp, 0x74);
1146 p->collision += mib_read(mp, 0x78);
1147 p->late_collision += mib_read(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001148}
1149
1150
1151/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001152struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001153 char stat_string[ETH_GSTRING_LEN];
1154 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +02001155 int netdev_off;
1156 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001157};
1158
Lennert Buytenhek16820052008-06-01 11:40:29 +02001159#define SSTAT(m) \
1160 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
1161 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001162
Lennert Buytenhek16820052008-06-01 11:40:29 +02001163#define MIBSTAT(m) \
1164 { #m, FIELD_SIZEOF(struct mib_counters, m), \
1165 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1166
1167static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1168 SSTAT(rx_packets),
1169 SSTAT(tx_packets),
1170 SSTAT(rx_bytes),
1171 SSTAT(tx_bytes),
1172 SSTAT(rx_errors),
1173 SSTAT(tx_errors),
1174 SSTAT(rx_dropped),
1175 SSTAT(tx_dropped),
1176 MIBSTAT(good_octets_received),
1177 MIBSTAT(bad_octets_received),
1178 MIBSTAT(internal_mac_transmit_err),
1179 MIBSTAT(good_frames_received),
1180 MIBSTAT(bad_frames_received),
1181 MIBSTAT(broadcast_frames_received),
1182 MIBSTAT(multicast_frames_received),
1183 MIBSTAT(frames_64_octets),
1184 MIBSTAT(frames_65_to_127_octets),
1185 MIBSTAT(frames_128_to_255_octets),
1186 MIBSTAT(frames_256_to_511_octets),
1187 MIBSTAT(frames_512_to_1023_octets),
1188 MIBSTAT(frames_1024_to_max_octets),
1189 MIBSTAT(good_octets_sent),
1190 MIBSTAT(good_frames_sent),
1191 MIBSTAT(excessive_collision),
1192 MIBSTAT(multicast_frames_sent),
1193 MIBSTAT(broadcast_frames_sent),
1194 MIBSTAT(unrec_mac_control_received),
1195 MIBSTAT(fc_sent),
1196 MIBSTAT(good_fc_received),
1197 MIBSTAT(bad_fc_received),
1198 MIBSTAT(undersize_received),
1199 MIBSTAT(fragments_received),
1200 MIBSTAT(oversize_received),
1201 MIBSTAT(jabber_received),
1202 MIBSTAT(mac_receive_error),
1203 MIBSTAT(bad_crc_event),
1204 MIBSTAT(collision),
1205 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001206};
1207
Lennert Buytenheke5371492008-06-01 02:18:13 +02001208static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001209{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001210 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001211 int err;
1212
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001213 err = mii_ethtool_gset(&mp->mii, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001214
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001215 /*
1216 * The MAC does not support 1000baseT_Half.
1217 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001218 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1219 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1220
1221 return err;
1222}
1223
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001224static int mv643xx_eth_get_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
1225{
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001226 struct mv643xx_eth_private *mp = netdev_priv(dev);
1227 u32 port_status;
1228
1229 port_status = rdl(mp, PORT_STATUS(mp->port_num));
1230
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001231 cmd->supported = SUPPORTED_MII;
1232 cmd->advertising = ADVERTISED_MII;
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001233 switch (port_status & PORT_SPEED_MASK) {
1234 case PORT_SPEED_10:
1235 cmd->speed = SPEED_10;
1236 break;
1237 case PORT_SPEED_100:
1238 cmd->speed = SPEED_100;
1239 break;
1240 case PORT_SPEED_1000:
1241 cmd->speed = SPEED_1000;
1242 break;
1243 default:
1244 cmd->speed = -1;
1245 break;
1246 }
1247 cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001248 cmd->port = PORT_MII;
1249 cmd->phy_address = 0;
1250 cmd->transceiver = XCVR_INTERNAL;
1251 cmd->autoneg = AUTONEG_DISABLE;
1252 cmd->maxtxpkt = 1;
1253 cmd->maxrxpkt = 1;
1254
1255 return 0;
1256}
1257
Lennert Buytenheke5371492008-06-01 02:18:13 +02001258static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001259{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001260 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001261
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001262 /*
1263 * The MAC does not support 1000baseT_Half.
1264 */
1265 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1266
Lennert Buytenhek2b3ba0e2008-08-24 05:41:09 +02001267 return mii_ethtool_sset(&mp->mii, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001268}
1269
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001270static int mv643xx_eth_set_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
1271{
1272 return -EINVAL;
1273}
1274
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001275static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1276 struct ethtool_drvinfo *drvinfo)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001277{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001278 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
1279 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001280 strncpy(drvinfo->fw_version, "N/A", 32);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001281 strncpy(drvinfo->bus_info, "platform", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +02001282 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001283}
1284
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001285static int mv643xx_eth_nway_reset(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001286{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001287 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001288
1289 return mii_nway_restart(&mp->mii);
1290}
1291
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001292static int mv643xx_eth_nway_reset_phyless(struct net_device *dev)
1293{
1294 return -EINVAL;
1295}
1296
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001297static u32 mv643xx_eth_get_link(struct net_device *dev)
1298{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001299 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001300
1301 return mii_link_ok(&mp->mii);
1302}
1303
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001304static u32 mv643xx_eth_get_link_phyless(struct net_device *dev)
1305{
1306 return 1;
1307}
1308
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001309static void mv643xx_eth_get_strings(struct net_device *dev,
1310 uint32_t stringset, uint8_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001311{
1312 int i;
1313
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001314 if (stringset == ETH_SS_STATS) {
1315 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001316 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001317 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001318 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001319 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001320 }
1321}
1322
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001323static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1324 struct ethtool_stats *stats,
1325 uint64_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326{
Lennert Buytenhekb9873842008-08-24 05:59:16 +02001327 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001328 int i;
1329
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001330 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001331
Lennert Buytenhek16820052008-06-01 11:40:29 +02001332 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1333 const struct mv643xx_eth_stats *stat;
1334 void *p;
1335
1336 stat = mv643xx_eth_stats + i;
1337
1338 if (stat->netdev_off >= 0)
1339 p = ((void *)mp->dev) + stat->netdev_off;
1340 else
1341 p = ((void *)mp) + stat->mp_off;
1342
1343 data[i] = (stat->sizeof_stat == 8) ?
1344 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001345 }
1346}
1347
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001348static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001349{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001350 if (sset == ETH_SS_STATS)
Lennert Buytenhek16820052008-06-01 11:40:29 +02001351 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001352
1353 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001354}
1355
Lennert Buytenheke5371492008-06-01 02:18:13 +02001356static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001357 .get_settings = mv643xx_eth_get_settings,
1358 .set_settings = mv643xx_eth_set_settings,
1359 .get_drvinfo = mv643xx_eth_get_drvinfo,
1360 .nway_reset = mv643xx_eth_nway_reset,
1361 .get_link = mv643xx_eth_get_link,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001362 .set_sg = ethtool_op_set_sg,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001363 .get_strings = mv643xx_eth_get_strings,
1364 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001365 .get_sset_count = mv643xx_eth_get_sset_count,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001366};
1367
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001368static const struct ethtool_ops mv643xx_eth_ethtool_ops_phyless = {
1369 .get_settings = mv643xx_eth_get_settings_phyless,
1370 .set_settings = mv643xx_eth_set_settings_phyless,
1371 .get_drvinfo = mv643xx_eth_get_drvinfo,
1372 .nway_reset = mv643xx_eth_nway_reset_phyless,
1373 .get_link = mv643xx_eth_get_link_phyless,
1374 .set_sg = ethtool_op_set_sg,
1375 .get_strings = mv643xx_eth_get_strings,
1376 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1377 .get_sset_count = mv643xx_eth_get_sset_count,
1378};
1379
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001380
1381/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001382static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001383{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001384 unsigned int mac_h;
1385 unsigned int mac_l;
1386
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001387 mac_h = rdl(mp, MAC_ADDR_HIGH(mp->port_num));
1388 mac_l = rdl(mp, MAC_ADDR_LOW(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001389
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001390 addr[0] = (mac_h >> 24) & 0xff;
1391 addr[1] = (mac_h >> 16) & 0xff;
1392 addr[2] = (mac_h >> 8) & 0xff;
1393 addr[3] = mac_h & 0xff;
1394 addr[4] = (mac_l >> 8) & 0xff;
1395 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001396}
1397
Lennert Buytenheke5371492008-06-01 02:18:13 +02001398static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001399{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001400 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001401
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001402 for (i = 0; i < 0x100; i += 4) {
1403 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
1404 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001405 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001406
1407 for (i = 0; i < 0x10; i += 4)
1408 wrl(mp, UNICAST_TABLE(mp->port_num) + i, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001409}
1410
Lennert Buytenheke5371492008-06-01 02:18:13 +02001411static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001412 int table, unsigned char entry)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001413{
1414 unsigned int table_reg;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001415
1416 /* Set "accepts frame bit" at specified table entry */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001417 table_reg = rdl(mp, table + (entry & 0xfc));
1418 table_reg |= 0x01 << (8 * (entry & 3));
1419 wrl(mp, table + (entry & 0xfc), table_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001420}
1421
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001422static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001423{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001424 unsigned int mac_h;
1425 unsigned int mac_l;
1426 int table;
1427
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001428 mac_l = (addr[4] << 8) | addr[5];
1429 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001430
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001431 wrl(mp, MAC_ADDR_LOW(mp->port_num), mac_l);
1432 wrl(mp, MAC_ADDR_HIGH(mp->port_num), mac_h);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001433
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001434 table = UNICAST_TABLE(mp->port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001435 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001436}
1437
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001438static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1439{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001440 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001441
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001442 /* +2 is for the offset of the HW addr type */
1443 memcpy(dev->dev_addr, addr + 2, 6);
1444
1445 init_mac_tables(mp);
1446 uc_addr_set(mp, dev->dev_addr);
1447
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001448 return 0;
1449}
1450
Lennert Buytenhek69876562008-06-01 11:43:32 +02001451static int addr_crc(unsigned char *addr)
1452{
1453 int crc = 0;
1454 int i;
1455
1456 for (i = 0; i < 6; i++) {
1457 int j;
1458
1459 crc = (crc ^ addr[i]) << 8;
1460 for (j = 7; j >= 0; j--) {
1461 if (crc & (0x100 << j))
1462 crc ^= 0x107 << j;
1463 }
1464 }
1465
1466 return crc;
1467}
1468
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001469static void mv643xx_eth_set_rx_mode(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001470{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001471 struct mv643xx_eth_private *mp = netdev_priv(dev);
1472 u32 port_config;
1473 struct dev_addr_list *addr;
1474 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001475
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001476 port_config = rdl(mp, PORT_CONFIG(mp->port_num));
1477 if (dev->flags & IFF_PROMISC)
1478 port_config |= UNICAST_PROMISCUOUS_MODE;
1479 else
1480 port_config &= ~UNICAST_PROMISCUOUS_MODE;
1481 wrl(mp, PORT_CONFIG(mp->port_num), port_config);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001482
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001483 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1484 int port_num = mp->port_num;
1485 u32 accept = 0x01010101;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001486
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001487 for (i = 0; i < 0x100; i += 4) {
1488 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1489 wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001490 }
1491 return;
1492 }
1493
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001494 for (i = 0; i < 0x100; i += 4) {
1495 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
1496 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001497 }
1498
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001499 for (addr = dev->mc_list; addr != NULL; addr = addr->next) {
1500 u8 *a = addr->da_addr;
1501 int table;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001502
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001503 if (addr->da_addrlen != 6)
1504 continue;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001505
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001506 if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1507 table = SPECIAL_MCAST_TABLE(mp->port_num);
1508 set_filter_table_entry(mp, table, a[5]);
1509 } else {
1510 int crc = addr_crc(a);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001511
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001512 table = OTHER_MCAST_TABLE(mp->port_num);
1513 set_filter_table_entry(mp, table, crc);
1514 }
1515 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001516}
1517
1518
1519/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001520static int rxq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001521{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001522 struct rx_queue *rxq = mp->rxq + index;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001523 struct rx_desc *rx_desc;
1524 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001525 int i;
1526
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001527 rxq->index = index;
1528
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001529 rxq->rx_ring_size = mp->default_rx_ring_size;
1530
1531 rxq->rx_desc_count = 0;
1532 rxq->rx_curr_desc = 0;
1533 rxq->rx_used_desc = 0;
1534
1535 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1536
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001537 if (index == mp->rxq_primary && size <= mp->rx_desc_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001538 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1539 mp->rx_desc_sram_size);
1540 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1541 } else {
1542 rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
1543 &rxq->rx_desc_dma,
1544 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001545 }
1546
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001547 if (rxq->rx_desc_area == NULL) {
1548 dev_printk(KERN_ERR, &mp->dev->dev,
1549 "can't allocate rx ring (%d bytes)\n", size);
1550 goto out;
1551 }
1552 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001553
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001554 rxq->rx_desc_area_size = size;
1555 rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
1556 GFP_KERNEL);
1557 if (rxq->rx_skb == NULL) {
1558 dev_printk(KERN_ERR, &mp->dev->dev,
1559 "can't allocate rx skb ring\n");
1560 goto out_free;
1561 }
1562
1563 rx_desc = (struct rx_desc *)rxq->rx_desc_area;
1564 for (i = 0; i < rxq->rx_ring_size; i++) {
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001565 int nexti;
1566
1567 nexti = i + 1;
1568 if (nexti == rxq->rx_ring_size)
1569 nexti = 0;
1570
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001571 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1572 nexti * sizeof(struct rx_desc);
1573 }
1574
1575 init_timer(&rxq->rx_oom);
1576 rxq->rx_oom.data = (unsigned long)rxq;
1577 rxq->rx_oom.function = rxq_refill_timer_wrapper;
1578
1579 return 0;
1580
1581
1582out_free:
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001583 if (index == mp->rxq_primary && size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001584 iounmap(rxq->rx_desc_area);
1585 else
1586 dma_free_coherent(NULL, size,
1587 rxq->rx_desc_area,
1588 rxq->rx_desc_dma);
1589
1590out:
1591 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001592}
1593
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001594static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001595{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001596 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1597 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001598
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001599 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001600
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001601 del_timer_sync(&rxq->rx_oom);
1602
1603 for (i = 0; i < rxq->rx_ring_size; i++) {
1604 if (rxq->rx_skb[i]) {
1605 dev_kfree_skb(rxq->rx_skb[i]);
1606 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001607 }
1608 }
1609
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001610 if (rxq->rx_desc_count) {
1611 dev_printk(KERN_ERR, &mp->dev->dev,
1612 "error freeing rx ring -- %d skbs stuck\n",
1613 rxq->rx_desc_count);
1614 }
1615
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001616 if (rxq->index == mp->rxq_primary &&
1617 rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001618 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001619 else
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001620 dma_free_coherent(NULL, rxq->rx_desc_area_size,
1621 rxq->rx_desc_area, rxq->rx_desc_dma);
1622
1623 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001624}
1625
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001626static int txq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001627{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001628 struct tx_queue *txq = mp->txq + index;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001629 struct tx_desc *tx_desc;
1630 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001631 int i;
1632
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001633 txq->index = index;
1634
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001635 txq->tx_ring_size = mp->default_tx_ring_size;
1636
1637 txq->tx_desc_count = 0;
1638 txq->tx_curr_desc = 0;
1639 txq->tx_used_desc = 0;
1640
1641 size = txq->tx_ring_size * sizeof(struct tx_desc);
1642
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001643 if (index == mp->txq_primary && size <= mp->tx_desc_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001644 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1645 mp->tx_desc_sram_size);
1646 txq->tx_desc_dma = mp->tx_desc_sram_addr;
1647 } else {
1648 txq->tx_desc_area = dma_alloc_coherent(NULL, size,
1649 &txq->tx_desc_dma,
1650 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001651 }
1652
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001653 if (txq->tx_desc_area == NULL) {
1654 dev_printk(KERN_ERR, &mp->dev->dev,
1655 "can't allocate tx ring (%d bytes)\n", size);
1656 goto out;
1657 }
1658 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001659
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001660 txq->tx_desc_area_size = size;
1661 txq->tx_skb = kmalloc(txq->tx_ring_size * sizeof(*txq->tx_skb),
1662 GFP_KERNEL);
1663 if (txq->tx_skb == NULL) {
1664 dev_printk(KERN_ERR, &mp->dev->dev,
1665 "can't allocate tx skb ring\n");
1666 goto out_free;
1667 }
1668
1669 tx_desc = (struct tx_desc *)txq->tx_desc_area;
1670 for (i = 0; i < txq->tx_ring_size; i++) {
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001671 struct tx_desc *txd = tx_desc + i;
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001672 int nexti;
1673
1674 nexti = i + 1;
1675 if (nexti == txq->tx_ring_size)
1676 nexti = 0;
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001677
1678 txd->cmd_sts = 0;
1679 txd->next_desc_ptr = txq->tx_desc_dma +
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001680 nexti * sizeof(struct tx_desc);
1681 }
1682
1683 return 0;
1684
1685
1686out_free:
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001687 if (index == mp->txq_primary && size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001688 iounmap(txq->tx_desc_area);
1689 else
1690 dma_free_coherent(NULL, size,
1691 txq->tx_desc_area,
1692 txq->tx_desc_dma);
1693
1694out:
1695 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001696}
1697
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001698static void txq_reclaim(struct tx_queue *txq, int force)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001699{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001700 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001701 unsigned long flags;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001702
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001703 spin_lock_irqsave(&mp->lock, flags);
1704 while (txq->tx_desc_count > 0) {
1705 int tx_index;
1706 struct tx_desc *desc;
1707 u32 cmd_sts;
1708 struct sk_buff *skb;
1709 dma_addr_t addr;
1710 int count;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001711
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001712 tx_index = txq->tx_used_desc;
1713 desc = &txq->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001714 cmd_sts = desc->cmd_sts;
1715
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001716 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
1717 if (!force)
1718 break;
1719 desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
1720 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001721
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001722 txq->tx_used_desc = tx_index + 1;
1723 if (txq->tx_used_desc == txq->tx_ring_size)
1724 txq->tx_used_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001725 txq->tx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001726
1727 addr = desc->buf_ptr;
1728 count = desc->byte_cnt;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001729 skb = txq->tx_skb[tx_index];
1730 txq->tx_skb[tx_index] = NULL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001731
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001732 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001733 dev_printk(KERN_INFO, &mp->dev->dev, "tx error\n");
1734 mp->dev->stats.tx_errors++;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001735 }
1736
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001737 /*
1738 * Drop mp->lock while we free the skb.
1739 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001740 spin_unlock_irqrestore(&mp->lock, flags);
1741
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001742 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001743 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1744 else
1745 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1746
1747 if (skb)
1748 dev_kfree_skb_irq(skb);
1749
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001750 spin_lock_irqsave(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001751 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001752 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001753}
1754
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001755static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001756{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001757 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001758
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001759 txq_disable(txq);
1760 txq_reclaim(txq, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001761
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001762 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001763
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001764 if (txq->index == mp->txq_primary &&
1765 txq->tx_desc_area_size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001766 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001767 else
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001768 dma_free_coherent(NULL, txq->tx_desc_area_size,
1769 txq->tx_desc_area, txq->tx_desc_dma);
1770
1771 kfree(txq->tx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001772}
1773
1774
1775/* netdev ops and related ***************************************************/
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001776static void handle_link_event(struct mv643xx_eth_private *mp)
1777{
1778 struct net_device *dev = mp->dev;
1779 u32 port_status;
1780 int speed;
1781 int duplex;
1782 int fc;
1783
1784 port_status = rdl(mp, PORT_STATUS(mp->port_num));
1785 if (!(port_status & LINK_UP)) {
1786 if (netif_carrier_ok(dev)) {
1787 int i;
1788
1789 printk(KERN_INFO "%s: link down\n", dev->name);
1790
1791 netif_carrier_off(dev);
1792 netif_stop_queue(dev);
1793
1794 for (i = 0; i < 8; i++) {
1795 struct tx_queue *txq = mp->txq + i;
1796
1797 if (mp->txq_mask & (1 << i)) {
1798 txq_reclaim(txq, 1);
1799 txq_reset_hw_ptr(txq);
1800 }
1801 }
1802 }
1803 return;
1804 }
1805
1806 switch (port_status & PORT_SPEED_MASK) {
1807 case PORT_SPEED_10:
1808 speed = 10;
1809 break;
1810 case PORT_SPEED_100:
1811 speed = 100;
1812 break;
1813 case PORT_SPEED_1000:
1814 speed = 1000;
1815 break;
1816 default:
1817 speed = -1;
1818 break;
1819 }
1820 duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
1821 fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
1822
1823 printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
1824 "flow control %sabled\n", dev->name,
1825 speed, duplex ? "full" : "half",
1826 fc ? "en" : "dis");
1827
1828 if (!netif_carrier_ok(dev)) {
1829 netif_carrier_on(dev);
1830 netif_wake_queue(dev);
1831 }
1832}
1833
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001834static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001835{
1836 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001837 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001838 u32 int_cause;
1839 u32 int_cause_ext;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001840
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001841 int_cause = rdl(mp, INT_CAUSE(mp->port_num)) &
1842 (INT_TX_END | INT_RX | INT_EXT);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001843 if (int_cause == 0)
1844 return IRQ_NONE;
1845
1846 int_cause_ext = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001847 if (int_cause & INT_EXT) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001848 int_cause_ext = rdl(mp, INT_CAUSE_EXT(mp->port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001849 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001850 wrl(mp, INT_CAUSE_EXT(mp->port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001851 }
1852
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001853 if (int_cause_ext & (INT_EXT_PHY | INT_EXT_LINK))
1854 handle_link_event(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001855
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001856 /*
1857 * RxBuffer or RxError set for any of the 8 queues?
1858 */
Lennert Buytenheke5371492008-06-01 02:18:13 +02001859#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001860 if (int_cause & INT_RX) {
Lennert Buytenhek819ddca2008-08-24 02:45:32 +02001861 wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_RX));
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001862 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001863 rdl(mp, INT_MASK(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001864
1865 netif_rx_schedule(dev, &mp->napi);
1866 }
1867#else
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001868 if (int_cause & INT_RX) {
1869 int i;
1870
1871 for (i = 7; i >= 0; i--)
1872 if (mp->rxq_mask & (1 << i))
1873 rxq_process(mp->rxq + i, INT_MAX);
1874 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001875#endif
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001876
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001877 /*
1878 * TxBuffer or TxError set for any of the 8 queues?
1879 */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001880 if (int_cause_ext & INT_EXT_TX) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001881 int i;
1882
1883 for (i = 0; i < 8; i++)
1884 if (mp->txq_mask & (1 << i))
1885 txq_reclaim(mp->txq + i, 0);
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001886
1887 /*
1888 * Enough space again in the primary TX queue for a
1889 * full packet?
1890 */
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001891 if (netif_carrier_ok(dev)) {
1892 spin_lock(&mp->lock);
1893 __txq_maybe_wake(mp->txq + mp->txq_primary);
1894 spin_unlock(&mp->lock);
1895 }
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001896 }
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001897
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001898 /*
1899 * Any TxEnd interrupts?
1900 */
1901 if (int_cause & INT_TX_END) {
1902 int i;
1903
1904 wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_TX_END));
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001905
1906 spin_lock(&mp->lock);
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001907 for (i = 0; i < 8; i++) {
1908 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001909 u32 hw_desc_ptr;
1910 u32 expected_ptr;
1911
1912 if ((int_cause & (INT_TX_END_0 << i)) == 0)
1913 continue;
1914
1915 hw_desc_ptr =
1916 rdl(mp, TXQ_CURRENT_DESC_PTR(mp->port_num, i));
1917 expected_ptr = (u32)txq->tx_desc_dma +
1918 txq->tx_curr_desc * sizeof(struct tx_desc);
1919
1920 if (hw_desc_ptr != expected_ptr)
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001921 txq_enable(txq);
1922 }
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001923 spin_unlock(&mp->lock);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001924 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001925
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001926 return IRQ_HANDLED;
1927}
1928
Lennert Buytenheke5371492008-06-01 02:18:13 +02001929static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001930{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001931 int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001932
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001933 data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
1934 if (data < 0)
1935 return;
1936
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02001937 data |= BMCR_RESET;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001938 if (smi_reg_write(mp, mp->phy_addr, MII_BMCR, data) < 0)
1939 return;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001940
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001941 do {
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001942 data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
1943 } while (data >= 0 && data & BMCR_RESET);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001944}
1945
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001946static void port_start(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001947{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001948 u32 pscr;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001949 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001950
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001951 /*
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001952 * Perform PHY reset, if there is a PHY.
1953 */
1954 if (mp->phy_addr != -1) {
1955 struct ethtool_cmd cmd;
1956
1957 mv643xx_eth_get_settings(mp->dev, &cmd);
1958 phy_reset(mp);
1959 mv643xx_eth_set_settings(mp->dev, &cmd);
1960 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001961
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001962 /*
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001963 * Configure basic link parameters.
1964 */
1965 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
1966
1967 pscr |= SERIAL_PORT_ENABLE;
1968 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1969
1970 pscr |= DO_NOT_FORCE_LINK_FAIL;
1971 if (mp->phy_addr == -1)
1972 pscr |= FORCE_LINK_PASS;
1973 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1974
1975 wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
1976
1977 /*
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001978 * Configure TX path and queues.
1979 */
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001980 tx_set_rate(mp, 1000000000, 16777216);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001981 for (i = 0; i < 8; i++) {
1982 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001983
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001984 if ((mp->txq_mask & (1 << i)) == 0)
1985 continue;
1986
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001987 txq_reset_hw_ptr(txq);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001988 txq_set_rate(txq, 1000000000, 16777216);
1989 txq_set_fixed_prio_mode(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001990 }
1991
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001992 /*
1993 * Add configured unicast address to address filter table.
1994 */
1995 uc_addr_set(mp, mp->dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001996
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001997 /*
1998 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1999 * frames to RX queue #0.
2000 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002001 wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002002
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002003 /*
2004 * Treat BPDUs as normal multicasts, and disable partition mode.
2005 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002006 wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002007
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002008 /*
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002009 * Enable the receive queues.
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002010 */
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002011 for (i = 0; i < 8; i++) {
2012 struct rx_queue *rxq = mp->rxq + i;
2013 int off = RXQ_CURRENT_DESC_PTR(mp->port_num, i);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002014 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002015
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002016 if ((mp->rxq_mask & (1 << i)) == 0)
2017 continue;
2018
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002019 addr = (u32)rxq->rx_desc_dma;
2020 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
2021 wrl(mp, off, addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002022
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002023 rxq_enable(rxq);
2024 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002025}
2026
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02002027static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002028{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002029 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002030 u32 val;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002031
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002032 val = rdl(mp, SDMA_CONFIG(mp->port_num));
2033 if (mp->shared->extended_rx_coal_limit) {
2034 if (coal > 0xffff)
2035 coal = 0xffff;
2036 val &= ~0x023fff80;
2037 val |= (coal & 0x8000) << 10;
2038 val |= (coal & 0x7fff) << 7;
2039 } else {
2040 if (coal > 0x3fff)
2041 coal = 0x3fff;
2042 val &= ~0x003fff00;
2043 val |= (coal & 0x3fff) << 8;
2044 }
2045 wrl(mp, SDMA_CONFIG(mp->port_num), val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002046}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002047
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02002048static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002049{
2050 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2051
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002052 if (coal > 0x3fff)
2053 coal = 0x3fff;
2054 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), (coal & 0x3fff) << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002055}
2056
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002057static int mv643xx_eth_open(struct net_device *dev)
2058{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002059 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002060 int err;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002061 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002062
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002063 wrl(mp, INT_CAUSE(mp->port_num), 0);
2064 wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
2065 rdl(mp, INT_CAUSE_EXT(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002066
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002067 err = request_irq(dev->irq, mv643xx_eth_irq,
Lennert Buytenhek2a1867a2008-08-23 23:43:38 +02002068 IRQF_SHARED, dev->name, dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002069 if (err) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002070 dev_printk(KERN_ERR, &dev->dev, "can't assign irq\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002071 return -EAGAIN;
2072 }
2073
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002074 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002075
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002076 for (i = 0; i < 8; i++) {
2077 if ((mp->rxq_mask & (1 << i)) == 0)
2078 continue;
2079
2080 err = rxq_init(mp, i);
2081 if (err) {
2082 while (--i >= 0)
2083 if (mp->rxq_mask & (1 << i))
2084 rxq_deinit(mp->rxq + i);
2085 goto out;
2086 }
2087
2088 rxq_refill(mp->rxq + i);
2089 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002090
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002091 for (i = 0; i < 8; i++) {
2092 if ((mp->txq_mask & (1 << i)) == 0)
2093 continue;
2094
2095 err = txq_init(mp, i);
2096 if (err) {
2097 while (--i >= 0)
2098 if (mp->txq_mask & (1 << i))
2099 txq_deinit(mp->txq + i);
2100 goto out_free;
2101 }
2102 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002103
Lennert Buytenheke5371492008-06-01 02:18:13 +02002104#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002105 napi_enable(&mp->napi);
2106#endif
2107
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002108 netif_carrier_off(dev);
2109 netif_stop_queue(dev);
2110
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002111 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002112
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02002113 set_rx_coal(mp, 0);
2114 set_tx_coal(mp, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002115
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002116 wrl(mp, INT_MASK_EXT(mp->port_num),
2117 INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002118
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02002119 wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002120
2121 return 0;
2122
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002123
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002124out_free:
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002125 for (i = 0; i < 8; i++)
2126 if (mp->rxq_mask & (1 << i))
2127 rxq_deinit(mp->rxq + i);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002128out:
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002129 free_irq(dev->irq, dev);
2130
2131 return err;
2132}
2133
Lennert Buytenheke5371492008-06-01 02:18:13 +02002134static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002135{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002136 unsigned int data;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002137 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002138
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002139 for (i = 0; i < 8; i++) {
2140 if (mp->rxq_mask & (1 << i))
2141 rxq_disable(mp->rxq + i);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002142 if (mp->txq_mask & (1 << i))
2143 txq_disable(mp->txq + i);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002144 }
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002145
2146 while (1) {
2147 u32 ps = rdl(mp, PORT_STATUS(mp->port_num));
2148
2149 if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2150 break;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002151 udelay(10);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002152 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002153
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002154 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002155 data = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
2156 data &= ~(SERIAL_PORT_ENABLE |
2157 DO_NOT_FORCE_LINK_FAIL |
2158 FORCE_LINK_PASS);
2159 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002160}
2161
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002162static int mv643xx_eth_stop(struct net_device *dev)
2163{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002164 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002165 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002166
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002167 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
2168 rdl(mp, INT_MASK(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002169
Lennert Buytenheke5371492008-06-01 02:18:13 +02002170#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002171 napi_disable(&mp->napi);
2172#endif
2173 netif_carrier_off(dev);
2174 netif_stop_queue(dev);
2175
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002176 free_irq(dev->irq, dev);
2177
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002178 port_reset(mp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002179 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002180
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002181 for (i = 0; i < 8; i++) {
2182 if (mp->rxq_mask & (1 << i))
2183 rxq_deinit(mp->rxq + i);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002184 if (mp->txq_mask & (1 << i))
2185 txq_deinit(mp->txq + i);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002186 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002187
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002188 return 0;
2189}
2190
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002191static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002192{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002193 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002194
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002195 if (mp->phy_addr != -1)
2196 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2197
2198 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002199}
2200
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002201static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2202{
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002203 struct mv643xx_eth_private *mp = netdev_priv(dev);
2204
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002205 if (new_mtu < 64 || new_mtu > 9500)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002206 return -EINVAL;
2207
2208 dev->mtu = new_mtu;
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002209 tx_set_rate(mp, 1000000000, 16777216);
2210
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002211 if (!netif_running(dev))
2212 return 0;
2213
2214 /*
2215 * Stop and then re-open the interface. This will allocate RX
2216 * skbs of the new MTU.
2217 * There is a possible danger that the open will not succeed,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002218 * due to memory being full.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002219 */
2220 mv643xx_eth_stop(dev);
2221 if (mv643xx_eth_open(dev)) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002222 dev_printk(KERN_ERR, &dev->dev,
2223 "fatal error on re-opening device after "
2224 "MTU change\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002225 }
2226
2227 return 0;
2228}
2229
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002230static void tx_timeout_task(struct work_struct *ugly)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002231{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002232 struct mv643xx_eth_private *mp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002233
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002234 mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2235 if (netif_running(mp->dev)) {
2236 netif_stop_queue(mp->dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002237
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002238 port_reset(mp);
2239 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002240
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002241 __txq_maybe_wake(mp->txq + mp->txq_primary);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002242 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002243}
2244
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002245static void mv643xx_eth_tx_timeout(struct net_device *dev)
2246{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002247 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002248
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002249 dev_printk(KERN_INFO, &dev->dev, "tx timeout\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002250
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002251 schedule_work(&mp->tx_timeout_task);
2252}
2253
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002254#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002255static void mv643xx_eth_netpoll(struct net_device *dev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002256{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002257 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002258
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002259 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
2260 rdl(mp, INT_MASK(mp->port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002261
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002262 mv643xx_eth_irq(dev->irq, dev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002263
Lennert Buytenhekf2ca60f2008-07-07 00:03:00 +02002264 wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002265}
2266#endif
2267
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002268static int mv643xx_eth_mdio_read(struct net_device *dev, int addr, int reg)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002269{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002270 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002271 return smi_reg_read(mp, addr, reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002272}
2273
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002274static void mv643xx_eth_mdio_write(struct net_device *dev, int addr, int reg, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002275{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002276 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002277 smi_reg_write(mp, addr, reg, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002278}
2279
2280
2281/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02002282static void
2283mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
2284 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002285{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002286 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002287 u32 win_enable;
2288 u32 win_protect;
2289 int i;
2290
2291 for (i = 0; i < 6; i++) {
2292 writel(0, base + WINDOW_BASE(i));
2293 writel(0, base + WINDOW_SIZE(i));
2294 if (i < 4)
2295 writel(0, base + WINDOW_REMAP_HIGH(i));
2296 }
2297
2298 win_enable = 0x3f;
2299 win_protect = 0;
2300
2301 for (i = 0; i < dram->num_cs; i++) {
2302 struct mbus_dram_window *cs = dram->cs + i;
2303
2304 writel((cs->base & 0xffff0000) |
2305 (cs->mbus_attr << 8) |
2306 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2307 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2308
2309 win_enable &= ~(1 << i);
2310 win_protect |= 3 << (2 * i);
2311 }
2312
2313 writel(win_enable, base + WINDOW_BAR_ENABLE);
2314 msp->win_protect = win_protect;
2315}
2316
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002317static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2318{
2319 /*
2320 * Check whether we have a 14-bit coal limit field in bits
2321 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2322 * SDMA config register.
2323 */
2324 writel(0x02000000, msp->base + SDMA_CONFIG(0));
2325 if (readl(msp->base + SDMA_CONFIG(0)) & 0x02000000)
2326 msp->extended_rx_coal_limit = 1;
2327 else
2328 msp->extended_rx_coal_limit = 0;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002329
2330 /*
2331 * Check whether the TX rate control registers are in the
2332 * old or the new place.
2333 */
2334 writel(1, msp->base + TX_BW_MTU_MOVED(0));
2335 if (readl(msp->base + TX_BW_MTU_MOVED(0)) & 1)
2336 msp->tx_bw_control_moved = 1;
2337 else
2338 msp->tx_bw_control_moved = 0;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002339}
2340
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002341static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2342{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002343 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002344 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002345 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002346 struct resource *res;
2347 int ret;
2348
Lennert Buytenheke5371492008-06-01 02:18:13 +02002349 if (!mv643xx_eth_version_printed++)
Lennert Buytenhek7dde1542008-07-15 12:20:30 +02002350 printk(KERN_NOTICE "MV-643xx 10/100/1000 ethernet "
2351 "driver version %s\n", mv643xx_eth_driver_version);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002352
2353 ret = -EINVAL;
2354 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2355 if (res == NULL)
2356 goto out;
2357
2358 ret = -ENOMEM;
2359 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2360 if (msp == NULL)
2361 goto out;
2362 memset(msp, 0, sizeof(*msp));
2363
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002364 msp->base = ioremap(res->start, res->end - res->start + 1);
2365 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002366 goto out_free;
2367
Lennert Buytenhek2b3ba0e2008-08-24 05:41:09 +02002368 mutex_init(&msp->phy_lock);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002369
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002370 msp->err_interrupt = NO_IRQ;
2371 init_waitqueue_head(&msp->smi_busy_wait);
2372
2373 /*
2374 * Check whether the error interrupt is hooked up.
2375 */
2376 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2377 if (res != NULL) {
2378 int err;
2379
2380 err = request_irq(res->start, mv643xx_eth_err_irq,
2381 IRQF_SHARED, "mv643xx_eth", msp);
2382 if (!err) {
2383 writel(ERR_INT_SMI_DONE, msp->base + ERR_INT_MASK);
2384 msp->err_interrupt = res->start;
2385 }
2386 }
2387
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002388 /*
2389 * (Re-)program MBUS remapping windows if we are asked to.
2390 */
2391 if (pd != NULL && pd->dram != NULL)
2392 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2393
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002394 /*
2395 * Detect hardware parameters.
2396 */
2397 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002398 infer_hw_params(msp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002399
2400 platform_set_drvdata(pdev, msp);
2401
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002402 return 0;
2403
2404out_free:
2405 kfree(msp);
2406out:
2407 return ret;
2408}
2409
2410static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2411{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002412 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002413
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002414 if (msp->err_interrupt != NO_IRQ)
2415 free_irq(msp->err_interrupt, msp);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002416 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002417 kfree(msp);
2418
2419 return 0;
2420}
2421
2422static struct platform_driver mv643xx_eth_shared_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002423 .probe = mv643xx_eth_shared_probe,
2424 .remove = mv643xx_eth_shared_remove,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002425 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002426 .name = MV643XX_ETH_SHARED_NAME,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002427 .owner = THIS_MODULE,
2428 },
2429};
2430
Lennert Buytenheke5371492008-06-01 02:18:13 +02002431static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002432{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002433 int addr_shift = 5 * mp->port_num;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002434 u32 data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002435
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002436 data = rdl(mp, PHY_ADDR);
2437 data &= ~(0x1f << addr_shift);
2438 data |= (phy_addr & 0x1f) << addr_shift;
2439 wrl(mp, PHY_ADDR, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002440}
2441
Lennert Buytenheke5371492008-06-01 02:18:13 +02002442static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002443{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002444 unsigned int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002445
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002446 data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002447
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002448 return (data >> (5 * mp->port_num)) & 0x1f;
2449}
2450
2451static void set_params(struct mv643xx_eth_private *mp,
2452 struct mv643xx_eth_platform_data *pd)
2453{
2454 struct net_device *dev = mp->dev;
2455
2456 if (is_valid_ether_addr(pd->mac_addr))
2457 memcpy(dev->dev_addr, pd->mac_addr, 6);
2458 else
2459 uc_addr_get(mp, dev->dev_addr);
2460
2461 if (pd->phy_addr == -1) {
2462 mp->shared_smi = NULL;
2463 mp->phy_addr = -1;
2464 } else {
2465 mp->shared_smi = mp->shared;
2466 if (pd->shared_smi != NULL)
2467 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2468
2469 if (pd->force_phy_addr || pd->phy_addr) {
2470 mp->phy_addr = pd->phy_addr & 0x3f;
2471 phy_addr_set(mp, mp->phy_addr);
2472 } else {
2473 mp->phy_addr = phy_addr_get(mp);
2474 }
2475 }
2476
2477 mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2478 if (pd->rx_queue_size)
2479 mp->default_rx_ring_size = pd->rx_queue_size;
2480 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2481 mp->rx_desc_sram_size = pd->rx_sram_size;
2482
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002483 if (pd->rx_queue_mask)
2484 mp->rxq_mask = pd->rx_queue_mask;
2485 else
2486 mp->rxq_mask = 0x01;
2487 mp->rxq_primary = fls(mp->rxq_mask) - 1;
2488
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002489 mp->default_tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2490 if (pd->tx_queue_size)
2491 mp->default_tx_ring_size = pd->tx_queue_size;
2492 mp->tx_desc_sram_addr = pd->tx_sram_addr;
2493 mp->tx_desc_sram_size = pd->tx_sram_size;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002494
2495 if (pd->tx_queue_mask)
2496 mp->txq_mask = pd->tx_queue_mask;
2497 else
2498 mp->txq_mask = 0x01;
2499 mp->txq_primary = fls(mp->txq_mask) - 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002500}
2501
Lennert Buytenheke5371492008-06-01 02:18:13 +02002502static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002503{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002504 int data;
2505 int data2;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002506
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002507 data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
2508 if (data < 0)
2509 return -ENODEV;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002510
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002511 if (smi_reg_write(mp, mp->phy_addr, MII_BMCR, data ^ BMCR_ANENABLE) < 0)
2512 return -ENODEV;
2513
2514 data2 = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
2515 if (data2 < 0)
2516 return -ENODEV;
2517
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002518 if (((data ^ data2) & BMCR_ANENABLE) == 0)
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002519 return -ENODEV;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002520
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002521 smi_reg_write(mp, mp->phy_addr, MII_BMCR, data);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002522
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002523 return 0;
2524}
2525
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002526static int phy_init(struct mv643xx_eth_private *mp,
2527 struct mv643xx_eth_platform_data *pd)
James Chapmand0412d92006-01-27 01:15:30 -07002528{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002529 struct ethtool_cmd cmd;
2530 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002531
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002532 err = phy_detect(mp);
2533 if (err) {
2534 dev_printk(KERN_INFO, &mp->dev->dev,
2535 "no PHY detected at addr %d\n", mp->phy_addr);
2536 return err;
James Chapmand0412d92006-01-27 01:15:30 -07002537 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002538 phy_reset(mp);
2539
2540 mp->mii.phy_id = mp->phy_addr;
2541 mp->mii.phy_id_mask = 0x3f;
2542 mp->mii.reg_num_mask = 0x1f;
2543 mp->mii.dev = mp->dev;
2544 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2545 mp->mii.mdio_write = mv643xx_eth_mdio_write;
2546
2547 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
2548
2549 memset(&cmd, 0, sizeof(cmd));
2550
2551 cmd.port = PORT_MII;
2552 cmd.transceiver = XCVR_INTERNAL;
2553 cmd.phy_address = mp->phy_addr;
2554 if (pd->speed == 0) {
2555 cmd.autoneg = AUTONEG_ENABLE;
2556 cmd.speed = SPEED_100;
2557 cmd.advertising = ADVERTISED_10baseT_Half |
2558 ADVERTISED_10baseT_Full |
2559 ADVERTISED_100baseT_Half |
2560 ADVERTISED_100baseT_Full;
2561 if (mp->mii.supports_gmii)
2562 cmd.advertising |= ADVERTISED_1000baseT_Full;
2563 } else {
2564 cmd.autoneg = AUTONEG_DISABLE;
2565 cmd.speed = pd->speed;
2566 cmd.duplex = pd->duplex;
2567 }
2568
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002569 mv643xx_eth_set_settings(mp->dev, &cmd);
2570
2571 return 0;
James Chapmand0412d92006-01-27 01:15:30 -07002572}
2573
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002574static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2575{
2576 u32 pscr;
2577
2578 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
2579 if (pscr & SERIAL_PORT_ENABLE) {
2580 pscr &= ~SERIAL_PORT_ENABLE;
2581 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
2582 }
2583
2584 pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
2585 if (mp->phy_addr == -1) {
2586 pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2587 if (speed == SPEED_1000)
2588 pscr |= SET_GMII_SPEED_TO_1000;
2589 else if (speed == SPEED_100)
2590 pscr |= SET_MII_SPEED_TO_100;
2591
2592 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2593
2594 pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2595 if (duplex == DUPLEX_FULL)
2596 pscr |= SET_FULL_DUPLEX_MODE;
2597 }
2598
2599 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
2600}
2601
Russell King3ae5eae2005-11-09 22:32:44 +00002602static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct mv643xx_eth_platform_data *pd;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002605 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 struct resource *res;
Al Viroc5d64712007-10-13 08:30:26 +01002608 DECLARE_MAC_BUF(mac);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002609 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002611 pd = pdev->dev.platform_data;
2612 if (pd == NULL) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002613 dev_printk(KERN_ERR, &pdev->dev,
2614 "no mv643xx_eth_platform_data\n");
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002615 return -ENODEV;
2616 }
2617
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002618 if (pd->shared == NULL) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002619 dev_printk(KERN_ERR, &pdev->dev,
2620 "no mv643xx_eth_platform_data->shared\n");
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002621 return -ENODEV;
2622 }
2623
Lennert Buytenheke5371492008-06-01 02:18:13 +02002624 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (!dev)
2626 return -ENOMEM;
2627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002629 platform_set_drvdata(pdev, mp);
2630
2631 mp->shared = platform_get_drvdata(pd->shared);
2632 mp->port_num = pd->port_number;
2633
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002634 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002635#ifdef MV643XX_ETH_NAPI
2636 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002637#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002639 set_params(mp, pd);
2640
2641 spin_lock_init(&mp->lock);
2642
2643 mib_counters_clear(mp);
2644 INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
2645
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002646 if (mp->phy_addr != -1) {
2647 err = phy_init(mp, pd);
2648 if (err)
2649 goto out;
2650
2651 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
2652 } else {
2653 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops_phyless);
2654 }
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002655 init_pscr(mp, pd->speed, pd->duplex);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002656
2657
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2659 BUG_ON(!res);
2660 dev->irq = res->start;
2661
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002662 dev->hard_start_xmit = mv643xx_eth_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 dev->open = mv643xx_eth_open;
2664 dev->stop = mv643xx_eth_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002666 dev->set_mac_address = mv643xx_eth_set_mac_address;
2667 dev->do_ioctl = mv643xx_eth_ioctl;
2668 dev->change_mtu = mv643xx_eth_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 dev->tx_timeout = mv643xx_eth_tx_timeout;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002670#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002671 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002672#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 dev->base_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675
Lennert Buytenheke5371492008-06-01 02:18:13 +02002676#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 /*
2678 * Zero copy can only work if we use Discovery II memory. Else, we will
2679 * have to map the buffers to ISA memory which is only 16 MB
2680 */
Wolfram Joost63890572006-01-16 16:57:41 -07002681 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Lennert Buytenheke32b6612008-07-24 06:22:59 +02002682 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683#endif
James Chapmanc28a4f82006-01-27 01:13:15 -07002684
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002685 SET_NETDEV_DEV(dev, &pdev->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002686
2687 if (mp->shared->win_protect)
2688 wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
2689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 err = register_netdev(dev);
2691 if (err)
2692 goto out;
2693
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002694 dev_printk(KERN_NOTICE, &dev->dev, "port %d with MAC address %s\n",
2695 mp->port_num, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 if (dev->features & NETIF_F_SG)
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002698 dev_printk(KERN_NOTICE, &dev->dev, "scatter/gather enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 if (dev->features & NETIF_F_IP_CSUM)
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002701 dev_printk(KERN_NOTICE, &dev->dev, "tx checksum offload\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
Lennert Buytenheke5371492008-06-01 02:18:13 +02002703#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002704 dev_printk(KERN_NOTICE, &dev->dev, "napi enabled\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705#endif
2706
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002707 if (mp->tx_desc_sram_size > 0)
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002708 dev_printk(KERN_NOTICE, &dev->dev, "configured with sram\n");
Nicolas DETb1529872005-10-28 17:46:30 -07002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 return 0;
2711
2712out:
2713 free_netdev(dev);
2714
2715 return err;
2716}
2717
Russell King3ae5eae2005-11-09 22:32:44 +00002718static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002720 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002722 unregister_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 flush_scheduled_work();
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002724 free_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
Russell King3ae5eae2005-11-09 22:32:44 +00002726 platform_set_drvdata(pdev, NULL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 return 0;
2729}
2730
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002731static void mv643xx_eth_shutdown(struct platform_device *pdev)
2732{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002733 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002734
2735 /* Mask all interrupts on ethernet port */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002736 wrl(mp, INT_MASK(mp->port_num), 0);
2737 rdl(mp, INT_MASK(mp->port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002738
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002739 if (netif_running(mp->dev))
2740 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002741}
2742
Russell King3ae5eae2005-11-09 22:32:44 +00002743static struct platform_driver mv643xx_eth_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002744 .probe = mv643xx_eth_probe,
2745 .remove = mv643xx_eth_remove,
2746 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002747 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002748 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002749 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002750 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751};
2752
Lennert Buytenheke5371492008-06-01 02:18:13 +02002753static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 int rc;
2756
Russell King3ae5eae2005-11-09 22:32:44 +00002757 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002759 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002761 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return rc;
2765}
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002766module_init(mv643xx_eth_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767
Lennert Buytenheke5371492008-06-01 02:18:13 +02002768static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769{
Russell King3ae5eae2005-11-09 22:32:44 +00002770 platform_driver_unregister(&mv643xx_eth_driver);
2771 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772}
Lennert Buytenheke5371492008-06-01 02:18:13 +02002773module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Lennert Buytenhek45675bc2008-06-02 02:02:30 +02002775MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
2776 "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002778MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07002779MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002780MODULE_ALIAS("platform:" MV643XX_ETH_NAME);