blob: ff6460124307d54fbd541fdea0ebe6d313584cb3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a18a2006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
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";
58static char mv643xx_eth_driver_version[] = "1.0";
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 Buytenhek8a578112008-06-01 18:09:35 +020070#define ETH_HW_IP_ALIGN 2
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020071
72/*
73 * Registers shared between all ports.
74 */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +020075#define PHY_ADDR 0x0000
76#define SMI_REG 0x0004
77#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
78#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
79#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
80#define WINDOW_BAR_ENABLE 0x0290
81#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020082
83/*
84 * Per-port registers.
85 */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +020086#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020087#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +020088#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
89#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
90#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
91#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
92#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
93#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +020094#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +020095#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
96#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
97#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +020098#define INT_RX 0x00000804
99#define INT_EXT 0x00000002
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +0200100#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200101#define INT_EXT_LINK 0x00100000
102#define INT_EXT_PHY 0x00010000
103#define INT_EXT_TX_ERROR_0 0x00000100
104#define INT_EXT_TX_0 0x00000001
105#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +0200106#define INT_MASK(p) (0x0468 + ((p) << 10))
107#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
108#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
109#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
110#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
111#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
112#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
113#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
114#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
115#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200116
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200117
118/*
119 * SDMA configuration register.
120 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200121#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200122#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200123#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200124#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200125
126#if defined(__BIG_ENDIAN)
127#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
128 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200129 TX_BURST_SIZE_4_64BIT
130#elif defined(__LITTLE_ENDIAN)
131#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
132 RX_BURST_SIZE_4_64BIT | \
133 BLM_RX_NO_SWAP | \
134 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135 TX_BURST_SIZE_4_64BIT
136#else
137#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
138#endif
139
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200140
141/*
142 * Port serial control register.
143 */
144#define SET_MII_SPEED_TO_100 (1 << 24)
145#define SET_GMII_SPEED_TO_1000 (1 << 23)
146#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200148#define MAX_RX_PACKET_9700BYTE (5 << 17)
149#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200150#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
151#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
152#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
153#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
154#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
155#define FORCE_LINK_PASS (1 << 1)
156#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200157
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200158#define DEFAULT_RX_QUEUE_SIZE 400
159#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200160
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200161/* SMI reg */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200162#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
163#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
164#define SMI_OPCODE_WRITE 0 /* Completion of Read */
165#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200166
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200167
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200168/*
169 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200170 */
171#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200172struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173 u16 byte_cnt; /* Descriptor buffer byte count */
174 u16 buf_size; /* Buffer size */
175 u32 cmd_sts; /* Descriptor command status */
176 u32 next_desc_ptr; /* Next descriptor pointer */
177 u32 buf_ptr; /* Descriptor buffer pointer */
178};
179
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200180struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200181 u16 byte_cnt; /* buffer byte count */
182 u16 l4i_chk; /* CPU provided TCP checksum */
183 u32 cmd_sts; /* Command/status field */
184 u32 next_desc_ptr; /* Pointer to next descriptor */
185 u32 buf_ptr; /* pointer to buffer for this descriptor*/
186};
187#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200188struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200189 u32 cmd_sts; /* Descriptor command status */
190 u16 buf_size; /* Buffer size */
191 u16 byte_cnt; /* Descriptor buffer byte count */
192 u32 buf_ptr; /* Descriptor buffer pointer */
193 u32 next_desc_ptr; /* Next descriptor pointer */
194};
195
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200196struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200197 u32 cmd_sts; /* Command/status field */
198 u16 l4i_chk; /* CPU provided TCP checksum */
199 u16 byte_cnt; /* buffer byte count */
200 u32 buf_ptr; /* pointer to buffer for this descriptor*/
201 u32 next_desc_ptr; /* Pointer to next descriptor */
202};
203#else
204#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
205#endif
206
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200207/* RX & TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200208#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200209
210/* RX & TX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200211#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200212
213/* RX descriptor status */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200214#define LAYER_4_CHECKSUM_OK 0x40000000
215#define RX_ENABLE_INTERRUPT 0x20000000
216#define RX_FIRST_DESC 0x08000000
217#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200218
219/* TX descriptor command */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200220#define TX_ENABLE_INTERRUPT 0x00800000
221#define GEN_CRC 0x00400000
222#define TX_FIRST_DESC 0x00200000
223#define TX_LAST_DESC 0x00100000
224#define ZERO_PADDING 0x00080000
225#define GEN_IP_V4_CHECKSUM 0x00040000
226#define GEN_TCP_UDP_CHECKSUM 0x00020000
227#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200228
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200229#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200230
231
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200232/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200233struct mv643xx_eth_shared_private {
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200234 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200235
236 /* used to protect SMI_REG, which is shared across ports */
237 spinlock_t phy_lock;
238
239 u32 win_protect;
240
241 unsigned int t_clk;
242};
243
244
245/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200246struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200247 u64 good_octets_received;
248 u32 bad_octets_received;
249 u32 internal_mac_transmit_err;
250 u32 good_frames_received;
251 u32 bad_frames_received;
252 u32 broadcast_frames_received;
253 u32 multicast_frames_received;
254 u32 frames_64_octets;
255 u32 frames_65_to_127_octets;
256 u32 frames_128_to_255_octets;
257 u32 frames_256_to_511_octets;
258 u32 frames_512_to_1023_octets;
259 u32 frames_1024_to_max_octets;
260 u64 good_octets_sent;
261 u32 good_frames_sent;
262 u32 excessive_collision;
263 u32 multicast_frames_sent;
264 u32 broadcast_frames_sent;
265 u32 unrec_mac_control_received;
266 u32 fc_sent;
267 u32 good_fc_received;
268 u32 bad_fc_received;
269 u32 undersize_received;
270 u32 fragments_received;
271 u32 oversize_received;
272 u32 jabber_received;
273 u32 mac_receive_error;
274 u32 bad_crc_event;
275 u32 collision;
276 u32 late_collision;
277};
278
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200279struct rx_queue {
280 int rx_ring_size;
281
282 int rx_desc_count;
283 int rx_curr_desc;
284 int rx_used_desc;
285
286 struct rx_desc *rx_desc_area;
287 dma_addr_t rx_desc_dma;
288 int rx_desc_area_size;
289 struct sk_buff **rx_skb;
290
291 struct timer_list rx_oom;
292};
293
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200294struct tx_queue {
295 int tx_ring_size;
296
297 int tx_desc_count;
298 int tx_curr_desc;
299 int tx_used_desc;
300
301 struct tx_desc *tx_desc_area;
302 dma_addr_t tx_desc_dma;
303 int tx_desc_area_size;
304 struct sk_buff **tx_skb;
305};
306
Lennert Buytenheke5371492008-06-01 02:18:13 +0200307struct mv643xx_eth_private {
308 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200309 int port_num; /* User Ethernet port number */
310
Lennert Buytenheke5371492008-06-01 02:18:13 +0200311 struct mv643xx_eth_shared_private *shared_smi;
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200312
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200313 struct work_struct tx_timeout_task;
314
315 struct net_device *dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200316 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200317 spinlock_t lock;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200318
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200319 struct mii_if_info mii;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200320
321 /*
322 * RX state.
323 */
324 int default_rx_ring_size;
325 unsigned long rx_desc_sram_addr;
326 int rx_desc_sram_size;
327 struct napi_struct napi;
328 struct rx_queue rxq[1];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200329
330 /*
331 * TX state.
332 */
333 int default_tx_ring_size;
334 unsigned long tx_desc_sram_addr;
335 int tx_desc_sram_size;
336 struct tx_queue txq[1];
337#ifdef MV643XX_ETH_TX_FAST_REFILL
338 int tx_clean_threshold;
339#endif
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200340};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200342
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200343/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200344static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200346 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Lennert Buytenheke5371492008-06-01 02:18:13 +0200349static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200351 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200354
355/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200356static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200358 return container_of(rxq, struct mv643xx_eth_private, rxq[0]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200361static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
362{
363 return container_of(txq, struct mv643xx_eth_private, txq[0]);
364}
365
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200366static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200367{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200368 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
369 wrl(mp, RXQ_COMMAND(mp->port_num), 1);
370}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700371
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200372static void rxq_disable(struct rx_queue *rxq)
373{
374 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
375 u8 mask = 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200376
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200377 wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
378 while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
379 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200380}
381
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200382static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200383{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200384 struct mv643xx_eth_private *mp = txq_to_mp(txq);
385 wrl(mp, TXQ_COMMAND(mp->port_num), 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200386}
387
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200388static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200389{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200390 struct mv643xx_eth_private *mp = txq_to_mp(txq);
391 u8 mask = 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200392
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200393 wrl(mp, TXQ_COMMAND(mp->port_num), mask << 8);
394 while (rdl(mp, TXQ_COMMAND(mp->port_num)) & mask)
395 udelay(10);
396}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200397
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200398static void __txq_maybe_wake(struct tx_queue *txq)
399{
400 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200401
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200402 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_DESCS_PER_SKB)
403 netif_wake_queue(mp->dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200404}
405
406
407/* rx ***********************************************************************/
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200408static void txq_reclaim(struct tx_queue *txq, int force);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200409
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200410static void rxq_refill(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200411{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200412 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200413 unsigned long flags;
414
415 spin_lock_irqsave(&mp->lock, flags);
416
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200417 while (rxq->rx_desc_count < rxq->rx_ring_size) {
418 int skb_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200419 struct sk_buff *skb;
420 int unaligned;
421 int rx;
422
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200423 /*
424 * Reserve 2+14 bytes for an ethernet header (the
425 * hardware automatically prepends 2 bytes of dummy
426 * data to each received packet), 4 bytes for a VLAN
427 * header, and 4 bytes for the trailing FCS -- 24
428 * bytes total.
429 */
430 skb_size = mp->dev->mtu + 24;
431
432 skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200433 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200435
Ralf Baechle908b6372007-02-26 19:52:06 +0000436 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700437 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000438 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200439
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200440 rxq->rx_desc_count++;
441 rx = rxq->rx_used_desc;
442 rxq->rx_used_desc = (rx + 1) % rxq->rx_ring_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200443
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200444 rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
445 skb_size, DMA_FROM_DEVICE);
446 rxq->rx_desc_area[rx].buf_size = skb_size;
447 rxq->rx_skb[rx] = skb;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200448 wmb();
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200449 rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200450 RX_ENABLE_INTERRUPT;
451 wmb();
452
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700453 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200455
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200456 if (rxq->rx_desc_count == 0) {
457 rxq->rx_oom.expires = jiffies + (HZ / 10);
458 add_timer(&rxq->rx_oom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200460
461 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200464static inline void rxq_refill_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200466 rxq_refill((struct rx_queue *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200469static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200471 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
472 struct net_device_stats *stats = &mp->dev->stats;
473 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200475 rx = 0;
476 while (rx < budget) {
Lennert Buytenhek96587662008-06-01 10:31:56 +0200477 struct sk_buff *skb;
478 volatile struct rx_desc *rx_desc;
479 unsigned int cmd_sts;
480 unsigned long flags;
481
482 spin_lock_irqsave(&mp->lock, flags);
483
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200484 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200485
486 cmd_sts = rx_desc->cmd_sts;
487 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
488 spin_unlock_irqrestore(&mp->lock, flags);
489 break;
490 }
491 rmb();
492
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200493 skb = rxq->rx_skb[rxq->rx_curr_desc];
494 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200495
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200496 rxq->rx_curr_desc = (rxq->rx_curr_desc + 1) % rxq->rx_ring_size;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200497
498 spin_unlock_irqrestore(&mp->lock, flags);
499
500 dma_unmap_single(NULL, rx_desc->buf_ptr + ETH_HW_IP_ALIGN,
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200501 mp->dev->mtu + 24, DMA_FROM_DEVICE);
502 rxq->rx_desc_count--;
503 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700504
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700505 /*
506 * Update statistics.
507 * Note byte count includes 4 byte CRC count
508 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 stats->rx_packets++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200510 stats->rx_bytes += rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 /*
513 * In case received a packet without first / last bits on OR
514 * the error summary bit is on, the packets needs to be dropeed.
515 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200516 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200517 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200518 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 stats->rx_dropped++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200520 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200521 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 if (net_ratelimit())
523 printk(KERN_ERR
524 "%s: Received packet spread "
525 "on multiple descriptors\n",
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200526 mp->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Lennert Buytenhek96587662008-06-01 10:31:56 +0200528 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 stats->rx_errors++;
530
531 dev_kfree_skb_irq(skb);
532 } else {
533 /*
534 * The -4 is for the CRC in the trailer of the
535 * received packet
536 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200537 skb_put(skb, rx_desc->byte_cnt - ETH_HW_IP_ALIGN - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Lennert Buytenhek96587662008-06-01 10:31:56 +0200539 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 skb->ip_summed = CHECKSUM_UNNECESSARY;
541 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200542 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200544 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200545#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 netif_receive_skb(skb);
547#else
548 netif_rx(skb);
549#endif
550 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200551 mp->dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200553 rxq_refill(rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200555 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Lennert Buytenheke5371492008-06-01 02:18:13 +0200558#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200559static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200561 struct mv643xx_eth_private *mp;
562 int rx;
563
564 mp = container_of(napi, struct mv643xx_eth_private, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Lennert Buytenheke5371492008-06-01 02:18:13 +0200566#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (++mp->tx_clean_threshold > 5) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200568 txq_reclaim(mp->txq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571#endif
572
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200573 rx = rxq_process(mp->rxq, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200575 if (rx < budget) {
576 netif_rx_complete(mp->dev, napi);
577 wrl(mp, INT_CAUSE(mp->port_num), 0);
578 wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
579 wrl(mp, INT_MASK(mp->port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200582 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584#endif
585
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200586
587/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700588static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
589{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200590 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700591
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700592 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200593 skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
594 if (fragp->size <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700595 return 1;
596 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200597
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700598 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700599}
600
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200601static int txq_alloc_desc_index(struct tx_queue *txq)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700602{
603 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700604
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200605 BUG_ON(txq->tx_desc_count >= txq->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700606
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200607 tx_desc_curr = txq->tx_curr_desc;
608 txq->tx_curr_desc = (tx_desc_curr + 1) % txq->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700609
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200610 BUG_ON(txq->tx_curr_desc == txq->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700611
612 return tx_desc_curr;
613}
614
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200615static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700616{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200617 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700618 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700619
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200620 for (frag = 0; frag < nr_frags; frag++) {
621 skb_frag_t *this_frag;
622 int tx_index;
623 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700624
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200625 this_frag = &skb_shinfo(skb)->frags[frag];
626 tx_index = txq_alloc_desc_index(txq);
627 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700628
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200629 /*
630 * The last fragment will generate an interrupt
631 * which will free the skb on TX completion.
632 */
633 if (frag == nr_frags - 1) {
634 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
635 ZERO_PADDING | TX_LAST_DESC |
636 TX_ENABLE_INTERRUPT;
637 txq->tx_skb[tx_index] = skb;
638 } else {
639 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
640 txq->tx_skb[tx_index] = NULL;
641 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700642
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700643 desc->l4i_chk = 0;
644 desc->byte_cnt = this_frag->size;
645 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
646 this_frag->page_offset,
647 this_frag->size,
648 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700649 }
650}
651
Byron Bradley324ff2c2008-02-04 23:47:15 -0800652static inline __be16 sum16_as_be(__sum16 sum)
653{
654 return (__force __be16)sum;
655}
656
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200657static void txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700658{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200659 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700660 int tx_index;
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200661 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700662 u32 cmd_sts;
663 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700664
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200665 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700666
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200667 tx_index = txq_alloc_desc_index(txq);
668 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700669
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700670 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200671 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700672
673 length = skb_headlen(skb);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200674 txq->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700675 } else {
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200676 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700677 length = skb->len;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200678 txq->tx_skb[tx_index] = skb;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700679 }
680
681 desc->byte_cnt = length;
682 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700683
Patrick McHardy84fa7932006-08-29 16:44:56 -0700684 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800685 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700686
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200687 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
688 GEN_IP_V4_CHECKSUM |
689 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700690
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700691 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700692 case IPPROTO_UDP:
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200693 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800694 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700695 break;
696 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800697 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700698 break;
699 default:
700 BUG();
701 }
702 } else {
703 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200704 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700705 desc->l4i_chk = 0;
706 }
707
708 /* ensure all other descriptors are written before first cmd_sts */
709 wmb();
710 desc->cmd_sts = cmd_sts;
711
712 /* ensure all descriptors are written before poking hardware */
713 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200714 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700715
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200716 txq->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
720{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200721 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700722 struct net_device_stats *stats = &dev->stats;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200723 struct tx_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700726 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700727
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700728 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
729 stats->tx_dropped++;
730 printk(KERN_DEBUG "%s: failed to linearize tiny "
731 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700732 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 spin_lock_irqsave(&mp->lock, flags);
736
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200737 txq = mp->txq;
738
739 if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB) {
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700740 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
741 netif_stop_queue(dev);
742 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700743 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700744 }
745
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200746 txq_submit_skb(txq, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700747 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 stats->tx_packets++;
749 dev->trans_start = jiffies;
750
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200751 if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700752 netif_stop_queue(dev);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 spin_unlock_irqrestore(&mp->lock, flags);
755
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700756 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200759
760/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200761static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200762
Lennert Buytenheke5371492008-06-01 02:18:13 +0200763static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200764 unsigned int phy_reg, unsigned int *value)
765{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200766 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
767 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200768 unsigned long flags;
769 int i;
770
771 /* the SMI register is a shared resource */
772 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
773
774 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200775 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200776 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200777 printk("%s: PHY busy timeout\n", mp->dev->name);
778 goto out;
779 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200780 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200781 }
782
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200783 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200784
785 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200786 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200787 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200788 printk("%s: PHY read timeout\n", mp->dev->name);
789 goto out;
790 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200791 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200792 }
793
794 *value = readl(smi_reg) & 0xffff;
795out:
796 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
797}
798
Lennert Buytenheke5371492008-06-01 02:18:13 +0200799static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200800 unsigned int phy_reg, unsigned int value)
801{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200802 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
803 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200804 unsigned long flags;
805 int i;
806
807 /* the SMI register is a shared resource */
808 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
809
810 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200811 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200812 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200813 printk("%s: PHY busy timeout\n", mp->dev->name);
814 goto out;
815 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200816 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200817 }
818
819 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +0200820 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200821out:
822 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
823}
824
825
826/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200827static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200828{
829 unsigned int port_num = mp->port_num;
830 int i;
831
832 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200833 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +0200834 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200835}
836
Lennert Buytenheke5371492008-06-01 02:18:13 +0200837static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200838{
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +0200839 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200840}
841
Lennert Buytenheke5371492008-06-01 02:18:13 +0200842static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200843{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200844 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200845
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200846 p->good_octets_received += read_mib(mp, 0x00);
847 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
848 p->bad_octets_received += read_mib(mp, 0x08);
849 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
850 p->good_frames_received += read_mib(mp, 0x10);
851 p->bad_frames_received += read_mib(mp, 0x14);
852 p->broadcast_frames_received += read_mib(mp, 0x18);
853 p->multicast_frames_received += read_mib(mp, 0x1c);
854 p->frames_64_octets += read_mib(mp, 0x20);
855 p->frames_65_to_127_octets += read_mib(mp, 0x24);
856 p->frames_128_to_255_octets += read_mib(mp, 0x28);
857 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
858 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
859 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
860 p->good_octets_sent += read_mib(mp, 0x38);
861 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
862 p->good_frames_sent += read_mib(mp, 0x40);
863 p->excessive_collision += read_mib(mp, 0x44);
864 p->multicast_frames_sent += read_mib(mp, 0x48);
865 p->broadcast_frames_sent += read_mib(mp, 0x4c);
866 p->unrec_mac_control_received += read_mib(mp, 0x50);
867 p->fc_sent += read_mib(mp, 0x54);
868 p->good_fc_received += read_mib(mp, 0x58);
869 p->bad_fc_received += read_mib(mp, 0x5c);
870 p->undersize_received += read_mib(mp, 0x60);
871 p->fragments_received += read_mib(mp, 0x64);
872 p->oversize_received += read_mib(mp, 0x68);
873 p->jabber_received += read_mib(mp, 0x6c);
874 p->mac_receive_error += read_mib(mp, 0x70);
875 p->bad_crc_event += read_mib(mp, 0x74);
876 p->collision += read_mib(mp, 0x78);
877 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200878}
879
880
881/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200882struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200883 char stat_string[ETH_GSTRING_LEN];
884 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +0200885 int netdev_off;
886 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200887};
888
Lennert Buytenhek16820052008-06-01 11:40:29 +0200889#define SSTAT(m) \
890 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
891 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200892
Lennert Buytenhek16820052008-06-01 11:40:29 +0200893#define MIBSTAT(m) \
894 { #m, FIELD_SIZEOF(struct mib_counters, m), \
895 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
896
897static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
898 SSTAT(rx_packets),
899 SSTAT(tx_packets),
900 SSTAT(rx_bytes),
901 SSTAT(tx_bytes),
902 SSTAT(rx_errors),
903 SSTAT(tx_errors),
904 SSTAT(rx_dropped),
905 SSTAT(tx_dropped),
906 MIBSTAT(good_octets_received),
907 MIBSTAT(bad_octets_received),
908 MIBSTAT(internal_mac_transmit_err),
909 MIBSTAT(good_frames_received),
910 MIBSTAT(bad_frames_received),
911 MIBSTAT(broadcast_frames_received),
912 MIBSTAT(multicast_frames_received),
913 MIBSTAT(frames_64_octets),
914 MIBSTAT(frames_65_to_127_octets),
915 MIBSTAT(frames_128_to_255_octets),
916 MIBSTAT(frames_256_to_511_octets),
917 MIBSTAT(frames_512_to_1023_octets),
918 MIBSTAT(frames_1024_to_max_octets),
919 MIBSTAT(good_octets_sent),
920 MIBSTAT(good_frames_sent),
921 MIBSTAT(excessive_collision),
922 MIBSTAT(multicast_frames_sent),
923 MIBSTAT(broadcast_frames_sent),
924 MIBSTAT(unrec_mac_control_received),
925 MIBSTAT(fc_sent),
926 MIBSTAT(good_fc_received),
927 MIBSTAT(bad_fc_received),
928 MIBSTAT(undersize_received),
929 MIBSTAT(fragments_received),
930 MIBSTAT(oversize_received),
931 MIBSTAT(jabber_received),
932 MIBSTAT(mac_receive_error),
933 MIBSTAT(bad_crc_event),
934 MIBSTAT(collision),
935 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200936};
937
Lennert Buytenheke5371492008-06-01 02:18:13 +0200938static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200939{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200940 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200941 int err;
942
943 spin_lock_irq(&mp->lock);
944 err = mii_ethtool_gset(&mp->mii, cmd);
945 spin_unlock_irq(&mp->lock);
946
947 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
948 cmd->supported &= ~SUPPORTED_1000baseT_Half;
949 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
950
951 return err;
952}
953
Lennert Buytenheke5371492008-06-01 02:18:13 +0200954static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200955{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200956 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200957 int err;
958
959 spin_lock_irq(&mp->lock);
960 err = mii_ethtool_sset(&mp->mii, cmd);
961 spin_unlock_irq(&mp->lock);
962
963 return err;
964}
965
Lennert Buytenheke5371492008-06-01 02:18:13 +0200966static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200967 struct ethtool_drvinfo *drvinfo)
968{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200969 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
970 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200971 strncpy(drvinfo->fw_version, "N/A", 32);
972 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +0200973 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200974}
975
976static int mv643xx_eth_nway_restart(struct net_device *dev)
977{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200978 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200979
980 return mii_nway_restart(&mp->mii);
981}
982
983static u32 mv643xx_eth_get_link(struct net_device *dev)
984{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200985 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200986
987 return mii_link_ok(&mp->mii);
988}
989
Lennert Buytenheke5371492008-06-01 02:18:13 +0200990static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200991 uint8_t *data)
992{
993 int i;
994
995 switch(stringset) {
996 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +0200997 for (i=0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200998 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +0200999 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001000 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001001 }
1002 break;
1003 }
1004}
1005
Lennert Buytenheke5371492008-06-01 02:18:13 +02001006static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001007 struct ethtool_stats *stats, uint64_t *data)
1008{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001009 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001010 int i;
1011
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001012 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001013
Lennert Buytenhek16820052008-06-01 11:40:29 +02001014 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1015 const struct mv643xx_eth_stats *stat;
1016 void *p;
1017
1018 stat = mv643xx_eth_stats + i;
1019
1020 if (stat->netdev_off >= 0)
1021 p = ((void *)mp->dev) + stat->netdev_off;
1022 else
1023 p = ((void *)mp) + stat->mp_off;
1024
1025 data[i] = (stat->sizeof_stat == 8) ?
1026 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001027 }
1028}
1029
Lennert Buytenheke5371492008-06-01 02:18:13 +02001030static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001031{
1032 switch (sset) {
1033 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +02001034 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001035 default:
1036 return -EOPNOTSUPP;
1037 }
1038}
1039
Lennert Buytenheke5371492008-06-01 02:18:13 +02001040static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1041 .get_settings = mv643xx_eth_get_settings,
1042 .set_settings = mv643xx_eth_set_settings,
1043 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001044 .get_link = mv643xx_eth_get_link,
1045 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001046 .get_sset_count = mv643xx_eth_get_sset_count,
1047 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1048 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001049 .nway_reset = mv643xx_eth_nway_restart,
1050};
1051
1052
1053/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001054static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001055{
1056 unsigned int port_num = mp->port_num;
1057 unsigned int mac_h;
1058 unsigned int mac_l;
1059
1060 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1061 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1062
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001063 addr[0] = (mac_h >> 24) & 0xff;
1064 addr[1] = (mac_h >> 16) & 0xff;
1065 addr[2] = (mac_h >> 8) & 0xff;
1066 addr[3] = mac_h & 0xff;
1067 addr[4] = (mac_l >> 8) & 0xff;
1068 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001069}
1070
Lennert Buytenheke5371492008-06-01 02:18:13 +02001071static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001072{
1073 unsigned int port_num = mp->port_num;
1074 int table_index;
1075
1076 /* Clear DA filter unicast table (Ex_dFUT) */
1077 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001078 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001079
1080 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1081 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001082 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001083 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001084 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001085 }
1086}
1087
Lennert Buytenheke5371492008-06-01 02:18:13 +02001088static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001089 int table, unsigned char entry)
1090{
1091 unsigned int table_reg;
1092 unsigned int tbl_offset;
1093 unsigned int reg_offset;
1094
1095 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1096 reg_offset = entry % 4; /* Entry offset within the register */
1097
1098 /* Set "accepts frame bit" at specified table entry */
1099 table_reg = rdl(mp, table + tbl_offset);
1100 table_reg |= 0x01 << (8 * reg_offset);
1101 wrl(mp, table + tbl_offset, table_reg);
1102}
1103
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001104static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001105{
1106 unsigned int port_num = mp->port_num;
1107 unsigned int mac_h;
1108 unsigned int mac_l;
1109 int table;
1110
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001111 mac_l = (addr[4] << 8) | (addr[5]);
1112 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1113 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001114
1115 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1116 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1117
1118 /* Accept frames with this address */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001119 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001120 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001121}
1122
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001123static void mv643xx_eth_update_mac_address(struct net_device *dev)
1124{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001125 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001126
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001127 init_mac_tables(mp);
1128 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001129}
1130
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1132{
1133 int i;
1134
1135 for (i = 0; i < 6; i++)
1136 /* +2 is for the offset of the HW addr type */
1137 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1138 mv643xx_eth_update_mac_address(dev);
1139 return 0;
1140}
1141
Lennert Buytenhek69876562008-06-01 11:43:32 +02001142static int addr_crc(unsigned char *addr)
1143{
1144 int crc = 0;
1145 int i;
1146
1147 for (i = 0; i < 6; i++) {
1148 int j;
1149
1150 crc = (crc ^ addr[i]) << 8;
1151 for (j = 7; j >= 0; j--) {
1152 if (crc & (0x100 << j))
1153 crc ^= 0x107 << j;
1154 }
1155 }
1156
1157 return crc;
1158}
1159
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001160static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001161{
1162 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001163 int table;
Lennert Buytenhek69876562008-06-01 11:43:32 +02001164 int crc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001165
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001166 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1167 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001168 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001169 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001170 return;
1171 }
1172
Lennert Buytenhek69876562008-06-01 11:43:32 +02001173 crc = addr_crc(addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001174
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001175 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhek69876562008-06-01 11:43:32 +02001176 set_filter_table_entry(mp, table, crc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001177}
1178
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001179static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001180{
1181
1182 struct dev_mc_list *mc_list;
1183 int i;
1184 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001185 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001186 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001187
1188 /* If the device is in promiscuous mode or in all multicast mode,
1189 * we will fully populate both multicast tables with accept.
1190 * This is guaranteed to yield a match on all multicast addresses...
1191 */
1192 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1193 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1194 /* Set all entries in DA filter special multicast
1195 * table (Ex_dFSMT)
1196 * Set for ETH_Q0 for now
1197 * Bits
1198 * 0 Accept=1, Drop=0
1199 * 3-1 Queue ETH_Q0=0
1200 * 7-4 Reserved = 0;
1201 */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001202 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001203
1204 /* Set all entries in DA filter other multicast
1205 * table (Ex_dFOMT)
1206 * Set for ETH_Q0 for now
1207 * Bits
1208 * 0 Accept=1, Drop=0
1209 * 3-1 Queue ETH_Q0=0
1210 * 7-4 Reserved = 0;
1211 */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001212 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001213 }
1214 return;
1215 }
1216
1217 /* We will clear out multicast tables every time we get the list.
1218 * Then add the entire new list...
1219 */
1220 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1221 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001222 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001223
1224 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001225 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001226 }
1227
1228 /* Get pointer to net_device multicast list and add each one... */
1229 for (i = 0, mc_list = dev->mc_list;
1230 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1231 i++, mc_list = mc_list->next)
1232 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001233 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001234}
1235
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001236static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1237{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001238 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001239 u32 config_reg;
1240
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001241 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001242 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001243 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001244 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001245 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001246 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001247
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001248 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001249}
1250
1251
1252/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001253static int rxq_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001254{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001255 struct rx_queue *rxq = mp->rxq;
1256 struct rx_desc *rx_desc;
1257 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001258 int i;
1259
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001260 rxq->rx_ring_size = mp->default_rx_ring_size;
1261
1262 rxq->rx_desc_count = 0;
1263 rxq->rx_curr_desc = 0;
1264 rxq->rx_used_desc = 0;
1265
1266 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1267
1268 if (size <= mp->rx_desc_sram_size) {
1269 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1270 mp->rx_desc_sram_size);
1271 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1272 } else {
1273 rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
1274 &rxq->rx_desc_dma,
1275 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001276 }
1277
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001278 if (rxq->rx_desc_area == NULL) {
1279 dev_printk(KERN_ERR, &mp->dev->dev,
1280 "can't allocate rx ring (%d bytes)\n", size);
1281 goto out;
1282 }
1283 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001284
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001285 rxq->rx_desc_area_size = size;
1286 rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
1287 GFP_KERNEL);
1288 if (rxq->rx_skb == NULL) {
1289 dev_printk(KERN_ERR, &mp->dev->dev,
1290 "can't allocate rx skb ring\n");
1291 goto out_free;
1292 }
1293
1294 rx_desc = (struct rx_desc *)rxq->rx_desc_area;
1295 for (i = 0; i < rxq->rx_ring_size; i++) {
1296 int nexti = (i + 1) % rxq->rx_ring_size;
1297 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1298 nexti * sizeof(struct rx_desc);
1299 }
1300
1301 init_timer(&rxq->rx_oom);
1302 rxq->rx_oom.data = (unsigned long)rxq;
1303 rxq->rx_oom.function = rxq_refill_timer_wrapper;
1304
1305 return 0;
1306
1307
1308out_free:
1309 if (size <= mp->rx_desc_sram_size)
1310 iounmap(rxq->rx_desc_area);
1311 else
1312 dma_free_coherent(NULL, size,
1313 rxq->rx_desc_area,
1314 rxq->rx_desc_dma);
1315
1316out:
1317 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001318}
1319
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001320static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001321{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001322 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1323 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001324
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001325 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001327 del_timer_sync(&rxq->rx_oom);
1328
1329 for (i = 0; i < rxq->rx_ring_size; i++) {
1330 if (rxq->rx_skb[i]) {
1331 dev_kfree_skb(rxq->rx_skb[i]);
1332 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001333 }
1334 }
1335
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001336 if (rxq->rx_desc_count) {
1337 dev_printk(KERN_ERR, &mp->dev->dev,
1338 "error freeing rx ring -- %d skbs stuck\n",
1339 rxq->rx_desc_count);
1340 }
1341
1342 if (rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
1343 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001344 else
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001345 dma_free_coherent(NULL, rxq->rx_desc_area_size,
1346 rxq->rx_desc_area, rxq->rx_desc_dma);
1347
1348 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001349}
1350
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001351static int txq_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001352{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001353 struct tx_queue *txq = mp->txq;
1354 struct tx_desc *tx_desc;
1355 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001356 int i;
1357
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001358 txq->tx_ring_size = mp->default_tx_ring_size;
1359
1360 txq->tx_desc_count = 0;
1361 txq->tx_curr_desc = 0;
1362 txq->tx_used_desc = 0;
1363
1364 size = txq->tx_ring_size * sizeof(struct tx_desc);
1365
1366 if (size <= mp->tx_desc_sram_size) {
1367 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1368 mp->tx_desc_sram_size);
1369 txq->tx_desc_dma = mp->tx_desc_sram_addr;
1370 } else {
1371 txq->tx_desc_area = dma_alloc_coherent(NULL, size,
1372 &txq->tx_desc_dma,
1373 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001374 }
1375
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001376 if (txq->tx_desc_area == NULL) {
1377 dev_printk(KERN_ERR, &mp->dev->dev,
1378 "can't allocate tx ring (%d bytes)\n", size);
1379 goto out;
1380 }
1381 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001382
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001383 txq->tx_desc_area_size = size;
1384 txq->tx_skb = kmalloc(txq->tx_ring_size * sizeof(*txq->tx_skb),
1385 GFP_KERNEL);
1386 if (txq->tx_skb == NULL) {
1387 dev_printk(KERN_ERR, &mp->dev->dev,
1388 "can't allocate tx skb ring\n");
1389 goto out_free;
1390 }
1391
1392 tx_desc = (struct tx_desc *)txq->tx_desc_area;
1393 for (i = 0; i < txq->tx_ring_size; i++) {
1394 int nexti = (i + 1) % txq->tx_ring_size;
1395 tx_desc[i].next_desc_ptr = txq->tx_desc_dma +
1396 nexti * sizeof(struct tx_desc);
1397 }
1398
1399 return 0;
1400
1401
1402out_free:
1403 if (size <= mp->tx_desc_sram_size)
1404 iounmap(txq->tx_desc_area);
1405 else
1406 dma_free_coherent(NULL, size,
1407 txq->tx_desc_area,
1408 txq->tx_desc_dma);
1409
1410out:
1411 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001412}
1413
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001414static void txq_reclaim(struct tx_queue *txq, int force)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001415{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001416 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001417 unsigned long flags;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001418
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001419 spin_lock_irqsave(&mp->lock, flags);
1420 while (txq->tx_desc_count > 0) {
1421 int tx_index;
1422 struct tx_desc *desc;
1423 u32 cmd_sts;
1424 struct sk_buff *skb;
1425 dma_addr_t addr;
1426 int count;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001427
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001428 tx_index = txq->tx_used_desc;
1429 desc = &txq->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001430 cmd_sts = desc->cmd_sts;
1431
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001432 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA))
1433 break;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001434
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001435 txq->tx_used_desc = (tx_index + 1) % txq->tx_ring_size;
1436 txq->tx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001437
1438 addr = desc->buf_ptr;
1439 count = desc->byte_cnt;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001440 skb = txq->tx_skb[tx_index];
1441 txq->tx_skb[tx_index] = NULL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001442
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001443 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001444 dev_printk(KERN_INFO, &mp->dev->dev, "tx error\n");
1445 mp->dev->stats.tx_errors++;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001446 }
1447
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001448 /*
1449 * Drop mp->lock while we free the skb.
1450 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001451 spin_unlock_irqrestore(&mp->lock, flags);
1452
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001453 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001454 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1455 else
1456 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1457
1458 if (skb)
1459 dev_kfree_skb_irq(skb);
1460
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001461 spin_lock_irqsave(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001462 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001463 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001464}
1465
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001466static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001467{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001468 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001469
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001470 txq_disable(txq);
1471 txq_reclaim(txq, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001472
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001473 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001474
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001475 if (txq->tx_desc_area_size <= mp->tx_desc_sram_size)
1476 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001477 else
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001478 dma_free_coherent(NULL, txq->tx_desc_area_size,
1479 txq->tx_desc_area, txq->tx_desc_dma);
1480
1481 kfree(txq->tx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001482}
1483
1484
1485/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001486static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001487
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001488static void mv643xx_eth_update_pscr(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001489 struct ethtool_cmd *ecmd)
1490{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001491 u32 pscr_o;
1492 u32 pscr_n;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001493
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001494 pscr_o = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001495
1496 /* clear speed, duplex and rx buffer size fields */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001497 pscr_n = pscr_o & ~(SET_MII_SPEED_TO_100 |
1498 SET_GMII_SPEED_TO_1000 |
1499 SET_FULL_DUPLEX_MODE |
1500 MAX_RX_PACKET_MASK);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001501
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001502 if (ecmd->speed == SPEED_1000) {
1503 pscr_n |= SET_GMII_SPEED_TO_1000 | MAX_RX_PACKET_9700BYTE;
1504 } else {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001505 if (ecmd->speed == SPEED_100)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001506 pscr_n |= SET_MII_SPEED_TO_100;
1507 pscr_n |= MAX_RX_PACKET_1522BYTE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001508 }
1509
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001510 if (ecmd->duplex == DUPLEX_FULL)
1511 pscr_n |= SET_FULL_DUPLEX_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001512
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001513 if (pscr_n != pscr_o) {
1514 if ((pscr_o & SERIAL_PORT_ENABLE) == 0)
1515 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
1516 else {
1517 txq_disable(mp->txq);
1518 pscr_o &= ~SERIAL_PORT_ENABLE;
1519 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_o);
1520 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
1521 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
1522 txq_enable(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001523 }
1524 }
1525}
1526
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001527static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1528{
1529 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001530 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001531 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001532
1533 /* Read interrupt cause registers */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001534 int_cause = rdl(mp, INT_CAUSE(mp->port_num)) & (INT_RX | INT_EXT);
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001535 if (int_cause & INT_EXT) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001536 int_cause_ext = rdl(mp, INT_CAUSE_EXT(mp->port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001537 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001538 wrl(mp, INT_CAUSE_EXT(mp->port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001539 }
1540
1541 /* PHY status changed */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001542 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001543 if (mii_link_ok(&mp->mii)) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001544 struct ethtool_cmd cmd;
1545
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001546 mii_ethtool_gset(&mp->mii, &cmd);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001547 mv643xx_eth_update_pscr(mp, &cmd);
1548 txq_enable(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001549 if (!netif_carrier_ok(dev)) {
1550 netif_carrier_on(dev);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001551 __txq_maybe_wake(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001552 }
1553 } else if (netif_carrier_ok(dev)) {
1554 netif_stop_queue(dev);
1555 netif_carrier_off(dev);
1556 }
1557 }
1558
Lennert Buytenheke5371492008-06-01 02:18:13 +02001559#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001560 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001561 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001562 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001563
1564 /* wait for previous write to complete */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001565 rdl(mp, INT_MASK(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001566
1567 netif_rx_schedule(dev, &mp->napi);
1568 }
1569#else
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001570 if (int_cause & INT_RX)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001571 rxq_process(mp->rxq, INT_MAX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001572#endif
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001573 if (int_cause_ext & INT_EXT_TX) {
1574 txq_reclaim(mp->txq, 0);
1575 __txq_maybe_wake(mp->txq);
1576 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001577
1578 /*
1579 * If no real interrupt occured, exit.
1580 * This can happen when using gigE interrupt coalescing mechanism.
1581 */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001582 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001583 return IRQ_NONE;
1584
1585 return IRQ_HANDLED;
1586}
1587
Lennert Buytenheke5371492008-06-01 02:18:13 +02001588static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001589{
1590 unsigned int phy_reg_data;
1591
1592 /* Reset the PHY */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001593 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001594 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001595 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001596
1597 /* wait for PHY to come out of reset */
1598 do {
1599 udelay(1);
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001600 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001601 } while (phy_reg_data & 0x8000);
1602}
1603
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001604static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001605{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001606 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001607 u32 pscr;
1608 struct ethtool_cmd ethtool_cmd;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001609 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001610
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001611 /*
1612 * Configure basic link parameters.
1613 */
1614 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
1615 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
1616 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1617 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1618 DISABLE_AUTO_NEG_SPEED_GMII |
1619 DISABLE_AUTO_NEG_FOR_DUPLEX |
1620 DO_NOT_FORCE_LINK_FAIL |
1621 SERIAL_PORT_CONTROL_RESERVED;
1622 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1623 pscr |= SERIAL_PORT_ENABLE;
1624 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001625
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001626 wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
1627
1628 mv643xx_eth_get_settings(dev, &ethtool_cmd);
1629 phy_reset(mp);
1630 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001631
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001632 /*
1633 * Configure TX path and queues.
1634 */
1635 wrl(mp, TX_BW_MTU(mp->port_num), 0);
1636 for (i = 0; i < 1; i++) {
1637 struct tx_queue *txq = mp->txq;
1638 int off = TXQ_CURRENT_DESC_PTR(mp->port_num);
1639 u32 addr;
1640
1641 addr = (u32)txq->tx_desc_dma;
1642 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
1643 wrl(mp, off, addr);
1644 }
1645
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001646 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001647 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001648
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001649 /*
1650 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1651 * frames to RX queue #0.
1652 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001653 wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001654
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001655 /*
1656 * Treat BPDUs as normal multicasts, and disable partition mode.
1657 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001658 wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001659
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001660 /*
1661 * Enable the receive queue.
1662 */
1663 for (i = 0; i < 1; i++) {
1664 struct rx_queue *rxq = mp->rxq;
1665 int off = RXQ_CURRENT_DESC_PTR(mp->port_num);
1666 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001667
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001668 addr = (u32)rxq->rx_desc_dma;
1669 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
1670 wrl(mp, off, addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001671
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001672 rxq_enable(rxq);
1673 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001674}
1675
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02001676static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001677{
1678 unsigned int port_num = mp->port_num;
1679 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1680
1681 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001682 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001683 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001684 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001685 & 0xffc000ff));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001686}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001687
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02001688static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001689{
1690 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1691
1692 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001693 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001694}
1695
Lennert Buytenheke5371492008-06-01 02:18:13 +02001696static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001697{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001698 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001699
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001700 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001701}
1702
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001703static int mv643xx_eth_open(struct net_device *dev)
1704{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001705 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001706 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001707 int err;
1708
1709 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001710 wrl(mp, INT_CAUSE(port_num), 0);
1711 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001712 /* wait for previous write to complete */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001713 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001714
1715 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1716 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1717 if (err) {
1718 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1719 return -EAGAIN;
1720 }
1721
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001722 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001723
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001724 err = rxq_init(mp);
1725 if (err)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001726 goto out_free_irq;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001727 rxq_refill(mp->rxq);
1728
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001729 err = txq_init(mp);
1730 if (err)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001731 goto out_free_rx_skb;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001732
Lennert Buytenheke5371492008-06-01 02:18:13 +02001733#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001734 napi_enable(&mp->napi);
1735#endif
1736
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001737 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001738
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02001739 set_rx_coal(mp, 0);
1740 set_tx_coal(mp, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001741
1742 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001743 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001744
1745 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001746 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001747
1748 return 0;
1749
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001750
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001751out_free_rx_skb:
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001752 rxq_deinit(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001753out_free_irq:
1754 free_irq(dev->irq, dev);
1755
1756 return err;
1757}
1758
Lennert Buytenheke5371492008-06-01 02:18:13 +02001759static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001760{
1761 unsigned int port_num = mp->port_num;
1762 unsigned int reg_data;
1763
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001764 txq_disable(mp->txq);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001765 rxq_disable(mp->rxq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001766 while (!(rdl(mp, PORT_STATUS(mp->port_num)) & TX_FIFO_EMPTY))
1767 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001768
1769 /* Clear all MIB counters */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001770 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001771
1772 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001773 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001774 reg_data &= ~(SERIAL_PORT_ENABLE |
1775 DO_NOT_FORCE_LINK_FAIL |
1776 FORCE_LINK_PASS);
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001777 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001778}
1779
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001780static int mv643xx_eth_stop(struct net_device *dev)
1781{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001782 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001783 unsigned int port_num = mp->port_num;
1784
1785 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001786 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001787 /* wait for previous write to complete */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001788 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001789
Lennert Buytenheke5371492008-06-01 02:18:13 +02001790#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001791 napi_disable(&mp->napi);
1792#endif
1793 netif_carrier_off(dev);
1794 netif_stop_queue(dev);
1795
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001796 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001797
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001798 txq_deinit(mp->txq);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001799 rxq_deinit(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001800
1801 free_irq(dev->irq, dev);
1802
1803 return 0;
1804}
1805
1806static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1807{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001808 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001809
1810 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1811}
1812
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001813static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1814{
1815 if ((new_mtu > 9500) || (new_mtu < 64))
1816 return -EINVAL;
1817
1818 dev->mtu = new_mtu;
1819 if (!netif_running(dev))
1820 return 0;
1821
1822 /*
1823 * Stop and then re-open the interface. This will allocate RX
1824 * skbs of the new MTU.
1825 * There is a possible danger that the open will not succeed,
1826 * due to memory being full, which might fail the open function.
1827 */
1828 mv643xx_eth_stop(dev);
1829 if (mv643xx_eth_open(dev)) {
1830 printk(KERN_ERR "%s: Fatal error on opening device\n",
1831 dev->name);
1832 }
1833
1834 return 0;
1835}
1836
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001837static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1838{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001839 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001840 tx_timeout_task);
1841 struct net_device *dev = mp->dev;
1842
1843 if (!netif_running(dev))
1844 return;
1845
1846 netif_stop_queue(dev);
1847
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001848 port_reset(mp);
1849 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001850
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001851 __txq_maybe_wake(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001852}
1853
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001854static void mv643xx_eth_tx_timeout(struct net_device *dev)
1855{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001856 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001857
1858 printk(KERN_INFO "%s: TX timeout ", dev->name);
1859
1860 /* Do the reset outside of interrupt context */
1861 schedule_work(&mp->tx_timeout_task);
1862}
1863
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001864#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001865static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001866{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001867 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001868 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001869
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001870 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001871 /* wait for previous write to complete */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02001872 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001873
Al Viro9da3b1a2006-10-08 15:00:44 +01001874 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001875
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001876 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001877}
1878#endif
1879
Lennert Buytenheke5371492008-06-01 02:18:13 +02001880static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001881{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001882 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001883 int val;
1884
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001885 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001886 return val;
1887}
1888
Lennert Buytenheke5371492008-06-01 02:18:13 +02001889static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001890{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001891 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001892 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001893}
1894
1895
1896/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001897static void
1898mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
1899 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001900{
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001901 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001902 u32 win_enable;
1903 u32 win_protect;
1904 int i;
1905
1906 for (i = 0; i < 6; i++) {
1907 writel(0, base + WINDOW_BASE(i));
1908 writel(0, base + WINDOW_SIZE(i));
1909 if (i < 4)
1910 writel(0, base + WINDOW_REMAP_HIGH(i));
1911 }
1912
1913 win_enable = 0x3f;
1914 win_protect = 0;
1915
1916 for (i = 0; i < dram->num_cs; i++) {
1917 struct mbus_dram_window *cs = dram->cs + i;
1918
1919 writel((cs->base & 0xffff0000) |
1920 (cs->mbus_attr << 8) |
1921 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1922 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1923
1924 win_enable &= ~(1 << i);
1925 win_protect |= 3 << (2 * i);
1926 }
1927
1928 writel(win_enable, base + WINDOW_BAR_ENABLE);
1929 msp->win_protect = win_protect;
1930}
1931
1932static int mv643xx_eth_shared_probe(struct platform_device *pdev)
1933{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001934 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001935 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001936 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001937 struct resource *res;
1938 int ret;
1939
Lennert Buytenheke5371492008-06-01 02:18:13 +02001940 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001941 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1942
1943 ret = -EINVAL;
1944 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1945 if (res == NULL)
1946 goto out;
1947
1948 ret = -ENOMEM;
1949 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
1950 if (msp == NULL)
1951 goto out;
1952 memset(msp, 0, sizeof(*msp));
1953
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001954 msp->base = ioremap(res->start, res->end - res->start + 1);
1955 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001956 goto out_free;
1957
1958 spin_lock_init(&msp->phy_lock);
1959 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
1960
1961 platform_set_drvdata(pdev, msp);
1962
1963 /*
1964 * (Re-)program MBUS remapping windows if we are asked to.
1965 */
1966 if (pd != NULL && pd->dram != NULL)
1967 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
1968
1969 return 0;
1970
1971out_free:
1972 kfree(msp);
1973out:
1974 return ret;
1975}
1976
1977static int mv643xx_eth_shared_remove(struct platform_device *pdev)
1978{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001979 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001980
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02001981 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001982 kfree(msp);
1983
1984 return 0;
1985}
1986
1987static struct platform_driver mv643xx_eth_shared_driver = {
1988 .probe = mv643xx_eth_shared_probe,
1989 .remove = mv643xx_eth_shared_remove,
1990 .driver = {
1991 .name = MV643XX_ETH_SHARED_NAME,
1992 .owner = THIS_MODULE,
1993 },
1994};
1995
Lennert Buytenheke5371492008-06-01 02:18:13 +02001996static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001997{
1998 u32 reg_data;
1999 int addr_shift = 5 * mp->port_num;
2000
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02002001 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002002 reg_data &= ~(0x1f << addr_shift);
2003 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02002004 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002005}
2006
Lennert Buytenheke5371492008-06-01 02:18:13 +02002007static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002008{
2009 unsigned int reg_data;
2010
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02002011 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002012
2013 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2014}
2015
Lennert Buytenheke5371492008-06-01 02:18:13 +02002016static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002017{
2018 unsigned int phy_reg_data0;
2019 int auto_neg;
2020
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002021 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002022 auto_neg = phy_reg_data0 & 0x1000;
2023 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002024 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002025
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002026 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002027 if ((phy_reg_data0 & 0x1000) == auto_neg)
2028 return -ENODEV; /* change didn't take */
2029
2030 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002031 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002032 return 0;
2033}
2034
James Chapmand0412d92006-01-27 01:15:30 -07002035static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2036 int speed, int duplex,
2037 struct ethtool_cmd *cmd)
2038{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002039 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002040
2041 memset(cmd, 0, sizeof(*cmd));
2042
2043 cmd->port = PORT_MII;
2044 cmd->transceiver = XCVR_INTERNAL;
2045 cmd->phy_address = phy_address;
2046
2047 if (speed == 0) {
2048 cmd->autoneg = AUTONEG_ENABLE;
2049 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2050 cmd->speed = SPEED_100;
2051 cmd->advertising = ADVERTISED_10baseT_Half |
2052 ADVERTISED_10baseT_Full |
2053 ADVERTISED_100baseT_Half |
2054 ADVERTISED_100baseT_Full;
2055 if (mp->mii.supports_gmii)
2056 cmd->advertising |= ADVERTISED_1000baseT_Full;
2057 } else {
2058 cmd->autoneg = AUTONEG_DISABLE;
2059 cmd->speed = speed;
2060 cmd->duplex = duplex;
2061 }
2062}
2063
Russell King3ae5eae2005-11-09 22:32:44 +00002064static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002067 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002068 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 struct net_device *dev;
2070 u8 *p;
2071 struct resource *res;
2072 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002073 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002074 int duplex = DUPLEX_HALF;
2075 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002076 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002078 pd = pdev->dev.platform_data;
2079 if (pd == NULL) {
2080 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2081 return -ENODEV;
2082 }
2083
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002084 if (pd->shared == NULL) {
2085 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2086 return -ENODEV;
2087 }
2088
Lennert Buytenheke5371492008-06-01 02:18:13 +02002089 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (!dev)
2091 return -ENOMEM;
2092
Russell King3ae5eae2005-11-09 22:32:44 +00002093 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002096 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002097#ifdef MV643XX_ETH_NAPI
2098 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002099#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
2101 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2102 BUG_ON(!res);
2103 dev->irq = res->start;
2104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 dev->open = mv643xx_eth_open;
2106 dev->stop = mv643xx_eth_stop;
2107 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 dev->set_mac_address = mv643xx_eth_set_mac_address;
2109 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2110
2111 /* No need to Tx Timeout */
2112 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002114#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002115 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002116#endif
2117
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 dev->base_addr = 0;
2120 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002121 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002122 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Lennert Buytenheke5371492008-06-01 02:18:13 +02002124#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125#ifdef MAX_SKB_FRAGS
2126 /*
2127 * Zero copy can only work if we use Discovery II memory. Else, we will
2128 * have to map the buffers to ISA memory which is only 16 MB
2129 */
Wolfram Joost63890572006-01-16 16:57:41 -07002130 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131#endif
2132#endif
2133
2134 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002135 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
2137 spin_lock_init(&mp->lock);
2138
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002139 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002140 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002141
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002142 if (mp->shared->win_protect)
2143 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2144
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002145 mp->shared_smi = mp->shared;
2146 if (pd->shared_smi != NULL)
2147 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2148
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 /* set default config values */
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002150 uc_addr_get(mp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002152 if (is_valid_ether_addr(pd->mac_addr))
2153 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002155 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002156 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002158 mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002159 if (pd->rx_queue_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002160 mp->default_rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002162 mp->default_tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002163 if (pd->tx_queue_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002164 mp->default_tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002166 if (pd->tx_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002167 mp->tx_desc_sram_size = pd->tx_sram_size;
2168 mp->tx_desc_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002171 if (pd->rx_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002172 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2173 mp->rx_desc_sram_size = pd->rx_sram_size;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002174 }
2175
2176 duplex = pd->duplex;
2177 speed = pd->speed;
2178
James Chapmanc28a4f82006-01-27 01:13:15 -07002179 /* Hook up MII support for ethtool */
2180 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002181 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2182 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002183 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002184 mp->mii.phy_id_mask = 0x3f;
2185 mp->mii.reg_num_mask = 0x1f;
2186
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002187 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002189 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002190 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002191 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002194 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002195 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002196 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002197 mv643xx_eth_update_pscr(mp, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002198 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002199
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002200 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 err = register_netdev(dev);
2202 if (err)
2203 goto out;
2204
2205 p = dev->dev_addr;
2206 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002207 "%s: port %d with MAC address %s\n",
2208 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 if (dev->features & NETIF_F_SG)
2211 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2212
2213 if (dev->features & NETIF_F_IP_CSUM)
2214 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2215 dev->name);
2216
Lennert Buytenheke5371492008-06-01 02:18:13 +02002217#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2219#endif
2220
Lennert Buytenheke5371492008-06-01 02:18:13 +02002221#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2223 dev->name);
2224#endif
2225
Lennert Buytenheke5371492008-06-01 02:18:13 +02002226#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2228#endif
2229
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002230 if (mp->tx_desc_sram_size > 0)
Nicolas DETb1529872005-10-28 17:46:30 -07002231 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2232
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 return 0;
2234
2235out:
2236 free_netdev(dev);
2237
2238 return err;
2239}
2240
Russell King3ae5eae2005-11-09 22:32:44 +00002241static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242{
Russell King3ae5eae2005-11-09 22:32:44 +00002243 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 unregister_netdev(dev);
2246 flush_scheduled_work();
2247
2248 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002249 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 return 0;
2251}
2252
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002253static void mv643xx_eth_shutdown(struct platform_device *pdev)
2254{
2255 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002256 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002257 unsigned int port_num = mp->port_num;
2258
2259 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb4667c2008-06-01 01:03:23 +02002260 wrl(mp, INT_MASK(port_num), 0);
2261 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002262
Lennert Buytenhekcc9754b32008-06-01 02:10:27 +02002263 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002264}
2265
Russell King3ae5eae2005-11-09 22:32:44 +00002266static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 .probe = mv643xx_eth_probe,
2268 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002269 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002270 .driver = {
2271 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002272 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002273 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274};
2275
Lennert Buytenheke5371492008-06-01 02:18:13 +02002276static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277{
2278 int rc;
2279
Russell King3ae5eae2005-11-09 22:32:44 +00002280 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002282 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002284 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
2286 return rc;
2287}
2288
Lennert Buytenheke5371492008-06-01 02:18:13 +02002289static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290{
Russell King3ae5eae2005-11-09 22:32:44 +00002291 platform_driver_unregister(&mv643xx_eth_driver);
2292 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293}
2294
Lennert Buytenheke5371492008-06-01 02:18:13 +02002295module_init(mv643xx_eth_init_module);
2296module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297
2298MODULE_LICENSE("GPL");
2299MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2300 " and Dale Farnsworth");
2301MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002302MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2303MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);