blob: 2ef71c48ef475af4ec6513ab434b1c59c0f3c989 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenheke5371492008-06-01 02:18:13 +020057static char mv643xx_eth_driver_name[] = "mv643xx_eth";
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
63#undef MV643XX_ETH_COAL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020064
Lennert Buytenheke5371492008-06-01 02:18:13 +020065#define MV643XX_ETH_TX_COAL 100
66#ifdef MV643XX_ETH_COAL
67#define MV643XX_ETH_RX_COAL 100
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020068#endif
69
Lennert Buytenheke5371492008-06-01 02:18:13 +020070#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020071#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
72#else
73#define MAX_DESCS_PER_SKB 1
74#endif
75
Lennert Buytenhek8a578112008-06-01 18:09:35 +020076#define ETH_HW_IP_ALIGN 2
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020077
78/*
79 * Registers shared between all ports.
80 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020081#define PHY_ADDR 0x0000
82#define SMI_REG 0x0004
83#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
84#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
85#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
86#define WINDOW_BAR_ENABLE 0x0290
87#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020088
89/*
90 * Per-port registers.
91 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020092#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020093#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020094#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
95#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
96#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
97#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
98#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
99#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200100#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200101#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
102#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
103#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200104#define INT_RX 0x00000804
105#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200106#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200107#define INT_EXT_LINK 0x00100000
108#define INT_EXT_PHY 0x00010000
109#define INT_EXT_TX_ERROR_0 0x00000100
110#define INT_EXT_TX_0 0x00000001
111#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200112#define INT_MASK(p) (0x0468 + ((p) << 10))
113#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
114#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
115#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
116#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
117#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
118#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
119#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
120#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
121#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200122
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200123
124/*
125 * SDMA configuration register.
126 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200127#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200129#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200130#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200131
132#if defined(__BIG_ENDIAN)
133#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
134 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135 TX_BURST_SIZE_4_64BIT
136#elif defined(__LITTLE_ENDIAN)
137#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
138 RX_BURST_SIZE_4_64BIT | \
139 BLM_RX_NO_SWAP | \
140 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141 TX_BURST_SIZE_4_64BIT
142#else
143#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
144#endif
145
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200146
147/*
148 * Port serial control register.
149 */
150#define SET_MII_SPEED_TO_100 (1 << 24)
151#define SET_GMII_SPEED_TO_1000 (1 << 23)
152#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200153#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200154#define MAX_RX_PACKET_9700BYTE (5 << 17)
155#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200156#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
157#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
158#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
159#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
160#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
161#define FORCE_LINK_PASS (1 << 1)
162#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200163
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200164#define DEFAULT_RX_QUEUE_SIZE 400
165#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200166
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200167/* SMI reg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200168#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
169#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
170#define SMI_OPCODE_WRITE 0 /* Completion of Read */
171#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200174/*
175 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200176 */
177#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200178struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179 u16 byte_cnt; /* Descriptor buffer byte count */
180 u16 buf_size; /* Buffer size */
181 u32 cmd_sts; /* Descriptor command status */
182 u32 next_desc_ptr; /* Next descriptor pointer */
183 u32 buf_ptr; /* Descriptor buffer pointer */
184};
185
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200186struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200187 u16 byte_cnt; /* buffer byte count */
188 u16 l4i_chk; /* CPU provided TCP checksum */
189 u32 cmd_sts; /* Command/status field */
190 u32 next_desc_ptr; /* Pointer to next descriptor */
191 u32 buf_ptr; /* pointer to buffer for this descriptor*/
192};
193#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200194struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200195 u32 cmd_sts; /* Descriptor command status */
196 u16 buf_size; /* Buffer size */
197 u16 byte_cnt; /* Descriptor buffer byte count */
198 u32 buf_ptr; /* Descriptor buffer pointer */
199 u32 next_desc_ptr; /* Next descriptor pointer */
200};
201
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200202struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200203 u32 cmd_sts; /* Command/status field */
204 u16 l4i_chk; /* CPU provided TCP checksum */
205 u16 byte_cnt; /* buffer byte count */
206 u32 buf_ptr; /* pointer to buffer for this descriptor*/
207 u32 next_desc_ptr; /* Pointer to next descriptor */
208};
209#else
210#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
211#endif
212
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200213/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200214#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200215
216/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200217#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200218
219/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200220#define LAYER_4_CHECKSUM_OK 0x40000000
221#define RX_ENABLE_INTERRUPT 0x20000000
222#define RX_FIRST_DESC 0x08000000
223#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200224
225/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200226#define TX_ENABLE_INTERRUPT 0x00800000
227#define GEN_CRC 0x00400000
228#define TX_FIRST_DESC 0x00200000
229#define TX_LAST_DESC 0x00100000
230#define ZERO_PADDING 0x00080000
231#define GEN_IP_V4_CHECKSUM 0x00040000
232#define GEN_TCP_UDP_CHECKSUM 0x00020000
233#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200234
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200235#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200236
237
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200238/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200239struct mv643xx_eth_shared_private {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200240 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200241
242 /* used to protect SMI_REG, which is shared across ports */
243 spinlock_t phy_lock;
244
245 u32 win_protect;
246
247 unsigned int t_clk;
248};
249
250
251/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200252struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200253 u64 good_octets_received;
254 u32 bad_octets_received;
255 u32 internal_mac_transmit_err;
256 u32 good_frames_received;
257 u32 bad_frames_received;
258 u32 broadcast_frames_received;
259 u32 multicast_frames_received;
260 u32 frames_64_octets;
261 u32 frames_65_to_127_octets;
262 u32 frames_128_to_255_octets;
263 u32 frames_256_to_511_octets;
264 u32 frames_512_to_1023_octets;
265 u32 frames_1024_to_max_octets;
266 u64 good_octets_sent;
267 u32 good_frames_sent;
268 u32 excessive_collision;
269 u32 multicast_frames_sent;
270 u32 broadcast_frames_sent;
271 u32 unrec_mac_control_received;
272 u32 fc_sent;
273 u32 good_fc_received;
274 u32 bad_fc_received;
275 u32 undersize_received;
276 u32 fragments_received;
277 u32 oversize_received;
278 u32 jabber_received;
279 u32 mac_receive_error;
280 u32 bad_crc_event;
281 u32 collision;
282 u32 late_collision;
283};
284
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200285struct rx_queue {
286 int rx_ring_size;
287
288 int rx_desc_count;
289 int rx_curr_desc;
290 int rx_used_desc;
291
292 struct rx_desc *rx_desc_area;
293 dma_addr_t rx_desc_dma;
294 int rx_desc_area_size;
295 struct sk_buff **rx_skb;
296
297 struct timer_list rx_oom;
298};
299
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200300struct tx_queue {
301 int tx_ring_size;
302
303 int tx_desc_count;
304 int tx_curr_desc;
305 int tx_used_desc;
306
307 struct tx_desc *tx_desc_area;
308 dma_addr_t tx_desc_dma;
309 int tx_desc_area_size;
310 struct sk_buff **tx_skb;
311};
312
Lennert Buytenheke5371492008-06-01 02:18:13 +0200313struct mv643xx_eth_private {
314 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200315 int port_num; /* User Ethernet port number */
316
Lennert Buytenheke5371492008-06-01 02:18:13 +0200317 struct mv643xx_eth_shared_private *shared_smi;
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200318
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200319 struct work_struct tx_timeout_task;
320
321 struct net_device *dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200322 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200323 spinlock_t lock;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200324
325 u32 rx_int_coal;
326 u32 tx_int_coal;
327 struct mii_if_info mii;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200328
329 /*
330 * RX state.
331 */
332 int default_rx_ring_size;
333 unsigned long rx_desc_sram_addr;
334 int rx_desc_sram_size;
335 struct napi_struct napi;
336 struct rx_queue rxq[1];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200337
338 /*
339 * TX state.
340 */
341 int default_tx_ring_size;
342 unsigned long tx_desc_sram_addr;
343 int tx_desc_sram_size;
344 struct tx_queue txq[1];
345#ifdef MV643XX_ETH_TX_FAST_REFILL
346 int tx_clean_threshold;
347#endif
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200348};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200350
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200351/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200352static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200354 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Lennert Buytenheke5371492008-06-01 02:18:13 +0200357static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200359 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200362
363/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200364static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200366 return container_of(rxq, struct mv643xx_eth_private, rxq[0]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200367}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200369static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
370{
371 return container_of(txq, struct mv643xx_eth_private, txq[0]);
372}
373
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200374static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200375{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200376 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
377 wrl(mp, RXQ_COMMAND(mp->port_num), 1);
378}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700379
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200380static void rxq_disable(struct rx_queue *rxq)
381{
382 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
383 u8 mask = 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200384
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200385 wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
386 while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
387 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200388}
389
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200390static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200391{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200392 struct mv643xx_eth_private *mp = txq_to_mp(txq);
393 wrl(mp, TXQ_COMMAND(mp->port_num), 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200394}
395
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200396static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200397{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200398 struct mv643xx_eth_private *mp = txq_to_mp(txq);
399 u8 mask = 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200400
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200401 wrl(mp, TXQ_COMMAND(mp->port_num), mask << 8);
402 while (rdl(mp, TXQ_COMMAND(mp->port_num)) & mask)
403 udelay(10);
404}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200405
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200406static void __txq_maybe_wake(struct tx_queue *txq)
407{
408 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200409
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200410 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_DESCS_PER_SKB)
411 netif_wake_queue(mp->dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200412}
413
414
415/* rx ***********************************************************************/
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200416static void txq_reclaim(struct tx_queue *txq, int force);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200417
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200418static void rxq_refill(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200419{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200420 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200421 unsigned long flags;
422
423 spin_lock_irqsave(&mp->lock, flags);
424
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200425 while (rxq->rx_desc_count < rxq->rx_ring_size) {
426 int skb_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200427 struct sk_buff *skb;
428 int unaligned;
429 int rx;
430
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200431 /*
432 * Reserve 2+14 bytes for an ethernet header (the
433 * hardware automatically prepends 2 bytes of dummy
434 * data to each received packet), 4 bytes for a VLAN
435 * header, and 4 bytes for the trailing FCS -- 24
436 * bytes total.
437 */
438 skb_size = mp->dev->mtu + 24;
439
440 skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200441 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200443
Ralf Baechle908b6372007-02-26 19:52:06 +0000444 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700445 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000446 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200447
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200448 rxq->rx_desc_count++;
449 rx = rxq->rx_used_desc;
450 rxq->rx_used_desc = (rx + 1) % rxq->rx_ring_size;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200451
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200452 rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
453 skb_size, DMA_FROM_DEVICE);
454 rxq->rx_desc_area[rx].buf_size = skb_size;
455 rxq->rx_skb[rx] = skb;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200456 wmb();
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200457 rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200458 RX_ENABLE_INTERRUPT;
459 wmb();
460
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700461 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200463
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200464 if (rxq->rx_desc_count == 0) {
465 rxq->rx_oom.expires = jiffies + (HZ / 10);
466 add_timer(&rxq->rx_oom);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200468
469 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470}
471
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200472static inline void rxq_refill_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200474 rxq_refill((struct rx_queue *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200477static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200479 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
480 struct net_device_stats *stats = &mp->dev->stats;
481 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200483 rx = 0;
484 while (rx < budget) {
Lennert Buytenhek96587662008-06-01 10:31:56 +0200485 struct sk_buff *skb;
486 volatile struct rx_desc *rx_desc;
487 unsigned int cmd_sts;
488 unsigned long flags;
489
490 spin_lock_irqsave(&mp->lock, flags);
491
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200492 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200493
494 cmd_sts = rx_desc->cmd_sts;
495 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
496 spin_unlock_irqrestore(&mp->lock, flags);
497 break;
498 }
499 rmb();
500
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200501 skb = rxq->rx_skb[rxq->rx_curr_desc];
502 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200503
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200504 rxq->rx_curr_desc = (rxq->rx_curr_desc + 1) % rxq->rx_ring_size;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200505
506 spin_unlock_irqrestore(&mp->lock, flags);
507
508 dma_unmap_single(NULL, rx_desc->buf_ptr + ETH_HW_IP_ALIGN,
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200509 mp->dev->mtu + 24, DMA_FROM_DEVICE);
510 rxq->rx_desc_count--;
511 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700512
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700513 /*
514 * Update statistics.
515 * Note byte count includes 4 byte CRC count
516 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 stats->rx_packets++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200518 stats->rx_bytes += rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 /*
521 * In case received a packet without first / last bits on OR
522 * the error summary bit is on, the packets needs to be dropeed.
523 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200524 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200525 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200526 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 stats->rx_dropped++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200528 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200529 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (net_ratelimit())
531 printk(KERN_ERR
532 "%s: Received packet spread "
533 "on multiple descriptors\n",
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200534 mp->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Lennert Buytenhek96587662008-06-01 10:31:56 +0200536 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 stats->rx_errors++;
538
539 dev_kfree_skb_irq(skb);
540 } else {
541 /*
542 * The -4 is for the CRC in the trailer of the
543 * received packet
544 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200545 skb_put(skb, rx_desc->byte_cnt - ETH_HW_IP_ALIGN - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Lennert Buytenhek96587662008-06-01 10:31:56 +0200547 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 skb->ip_summed = CHECKSUM_UNNECESSARY;
549 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200550 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200552 skb->protocol = eth_type_trans(skb, mp->dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200553#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 netif_receive_skb(skb);
555#else
556 netif_rx(skb);
557#endif
558 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200559 mp->dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200561 rxq_refill(rxq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200563 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Lennert Buytenheke5371492008-06-01 02:18:13 +0200566#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200567static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200569 struct mv643xx_eth_private *mp;
570 int rx;
571
572 mp = container_of(napi, struct mv643xx_eth_private, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Lennert Buytenheke5371492008-06-01 02:18:13 +0200574#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 if (++mp->tx_clean_threshold > 5) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200576 txq_reclaim(mp->txq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579#endif
580
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200581 rx = rxq_process(mp->rxq, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200583 if (rx < budget) {
584 netif_rx_complete(mp->dev, napi);
585 wrl(mp, INT_CAUSE(mp->port_num), 0);
586 wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
587 wrl(mp, INT_MASK(mp->port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200590 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591}
592#endif
593
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200594
595/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700596static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
597{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200598 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700599
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700600 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200601 skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
602 if (fragp->size <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700603 return 1;
604 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200605
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700606 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700607}
608
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200609static int txq_alloc_desc_index(struct tx_queue *txq)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700610{
611 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700612
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200613 BUG_ON(txq->tx_desc_count >= txq->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700614
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200615 tx_desc_curr = txq->tx_curr_desc;
616 txq->tx_curr_desc = (tx_desc_curr + 1) % txq->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700617
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200618 BUG_ON(txq->tx_curr_desc == txq->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700619
620 return tx_desc_curr;
621}
622
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200623static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700624{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200625 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700626 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700627
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200628 for (frag = 0; frag < nr_frags; frag++) {
629 skb_frag_t *this_frag;
630 int tx_index;
631 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700632
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200633 this_frag = &skb_shinfo(skb)->frags[frag];
634 tx_index = txq_alloc_desc_index(txq);
635 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700636
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200637 /*
638 * The last fragment will generate an interrupt
639 * which will free the skb on TX completion.
640 */
641 if (frag == nr_frags - 1) {
642 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
643 ZERO_PADDING | TX_LAST_DESC |
644 TX_ENABLE_INTERRUPT;
645 txq->tx_skb[tx_index] = skb;
646 } else {
647 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
648 txq->tx_skb[tx_index] = NULL;
649 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700650
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700651 desc->l4i_chk = 0;
652 desc->byte_cnt = this_frag->size;
653 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
654 this_frag->page_offset,
655 this_frag->size,
656 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700657 }
658}
659
Byron Bradley324ff2c2008-02-04 23:47:15 -0800660static inline __be16 sum16_as_be(__sum16 sum)
661{
662 return (__force __be16)sum;
663}
664
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200665static void txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700666{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200667 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700668 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200669 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700670 u32 cmd_sts;
671 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700672
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200673 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700674
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200675 tx_index = txq_alloc_desc_index(txq);
676 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700677
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700678 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200679 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700680
681 length = skb_headlen(skb);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200682 txq->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700683 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200684 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700685 length = skb->len;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200686 txq->tx_skb[tx_index] = skb;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700687 }
688
689 desc->byte_cnt = length;
690 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700691
Patrick McHardy84fa7932006-08-29 16:44:56 -0700692 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800693 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700694
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200695 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
696 GEN_IP_V4_CHECKSUM |
697 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700698
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700699 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700700 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200701 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800702 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700703 break;
704 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800705 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700706 break;
707 default:
708 BUG();
709 }
710 } else {
711 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200712 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700713 desc->l4i_chk = 0;
714 }
715
716 /* ensure all other descriptors are written before first cmd_sts */
717 wmb();
718 desc->cmd_sts = cmd_sts;
719
720 /* ensure all descriptors are written before poking hardware */
721 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200722 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700723
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200724 txq->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
728{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200729 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700730 struct net_device_stats *stats = &dev->stats;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200731 struct tx_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700734 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700735
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700736 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
737 stats->tx_dropped++;
738 printk(KERN_DEBUG "%s: failed to linearize tiny "
739 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700740 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 spin_lock_irqsave(&mp->lock, flags);
744
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200745 txq = mp->txq;
746
747 if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB) {
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700748 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
749 netif_stop_queue(dev);
750 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700751 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700752 }
753
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200754 txq_submit_skb(txq, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700755 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 stats->tx_packets++;
757 dev->trans_start = jiffies;
758
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200759 if (txq->tx_ring_size - txq->tx_desc_count < MAX_DESCS_PER_SKB)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700760 netif_stop_queue(dev);
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 spin_unlock_irqrestore(&mp->lock, flags);
763
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700764 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200767
768/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200769static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200770
Lennert Buytenheke5371492008-06-01 02:18:13 +0200771static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200772 unsigned int phy_reg, unsigned int *value)
773{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200774 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
775 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200776 unsigned long flags;
777 int i;
778
779 /* the SMI register is a shared resource */
780 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
781
782 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200783 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200784 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200785 printk("%s: PHY busy timeout\n", mp->dev->name);
786 goto out;
787 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200788 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200789 }
790
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200791 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200792
793 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200794 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200795 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200796 printk("%s: PHY read timeout\n", mp->dev->name);
797 goto out;
798 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200799 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200800 }
801
802 *value = readl(smi_reg) & 0xffff;
803out:
804 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
805}
806
Lennert Buytenheke5371492008-06-01 02:18:13 +0200807static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200808 unsigned int phy_reg, unsigned int value)
809{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200810 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
811 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200812 unsigned long flags;
813 int i;
814
815 /* the SMI register is a shared resource */
816 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
817
818 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200819 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200820 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200821 printk("%s: PHY busy timeout\n", mp->dev->name);
822 goto out;
823 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200824 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200825 }
826
827 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200828 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200829out:
830 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
831}
832
833
834/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200835static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200836{
837 unsigned int port_num = mp->port_num;
838 int i;
839
840 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200841 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200842 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200843}
844
Lennert Buytenheke5371492008-06-01 02:18:13 +0200845static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200846{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200847 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200848}
849
Lennert Buytenheke5371492008-06-01 02:18:13 +0200850static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200851{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200852 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200853
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200854 p->good_octets_received += read_mib(mp, 0x00);
855 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
856 p->bad_octets_received += read_mib(mp, 0x08);
857 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
858 p->good_frames_received += read_mib(mp, 0x10);
859 p->bad_frames_received += read_mib(mp, 0x14);
860 p->broadcast_frames_received += read_mib(mp, 0x18);
861 p->multicast_frames_received += read_mib(mp, 0x1c);
862 p->frames_64_octets += read_mib(mp, 0x20);
863 p->frames_65_to_127_octets += read_mib(mp, 0x24);
864 p->frames_128_to_255_octets += read_mib(mp, 0x28);
865 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
866 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
867 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
868 p->good_octets_sent += read_mib(mp, 0x38);
869 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
870 p->good_frames_sent += read_mib(mp, 0x40);
871 p->excessive_collision += read_mib(mp, 0x44);
872 p->multicast_frames_sent += read_mib(mp, 0x48);
873 p->broadcast_frames_sent += read_mib(mp, 0x4c);
874 p->unrec_mac_control_received += read_mib(mp, 0x50);
875 p->fc_sent += read_mib(mp, 0x54);
876 p->good_fc_received += read_mib(mp, 0x58);
877 p->bad_fc_received += read_mib(mp, 0x5c);
878 p->undersize_received += read_mib(mp, 0x60);
879 p->fragments_received += read_mib(mp, 0x64);
880 p->oversize_received += read_mib(mp, 0x68);
881 p->jabber_received += read_mib(mp, 0x6c);
882 p->mac_receive_error += read_mib(mp, 0x70);
883 p->bad_crc_event += read_mib(mp, 0x74);
884 p->collision += read_mib(mp, 0x78);
885 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200886}
887
888
889/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200890struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200891 char stat_string[ETH_GSTRING_LEN];
892 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +0200893 int netdev_off;
894 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200895};
896
Lennert Buytenhek16820052008-06-01 11:40:29 +0200897#define SSTAT(m) \
898 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
899 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200900
Lennert Buytenhek16820052008-06-01 11:40:29 +0200901#define MIBSTAT(m) \
902 { #m, FIELD_SIZEOF(struct mib_counters, m), \
903 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
904
905static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
906 SSTAT(rx_packets),
907 SSTAT(tx_packets),
908 SSTAT(rx_bytes),
909 SSTAT(tx_bytes),
910 SSTAT(rx_errors),
911 SSTAT(tx_errors),
912 SSTAT(rx_dropped),
913 SSTAT(tx_dropped),
914 MIBSTAT(good_octets_received),
915 MIBSTAT(bad_octets_received),
916 MIBSTAT(internal_mac_transmit_err),
917 MIBSTAT(good_frames_received),
918 MIBSTAT(bad_frames_received),
919 MIBSTAT(broadcast_frames_received),
920 MIBSTAT(multicast_frames_received),
921 MIBSTAT(frames_64_octets),
922 MIBSTAT(frames_65_to_127_octets),
923 MIBSTAT(frames_128_to_255_octets),
924 MIBSTAT(frames_256_to_511_octets),
925 MIBSTAT(frames_512_to_1023_octets),
926 MIBSTAT(frames_1024_to_max_octets),
927 MIBSTAT(good_octets_sent),
928 MIBSTAT(good_frames_sent),
929 MIBSTAT(excessive_collision),
930 MIBSTAT(multicast_frames_sent),
931 MIBSTAT(broadcast_frames_sent),
932 MIBSTAT(unrec_mac_control_received),
933 MIBSTAT(fc_sent),
934 MIBSTAT(good_fc_received),
935 MIBSTAT(bad_fc_received),
936 MIBSTAT(undersize_received),
937 MIBSTAT(fragments_received),
938 MIBSTAT(oversize_received),
939 MIBSTAT(jabber_received),
940 MIBSTAT(mac_receive_error),
941 MIBSTAT(bad_crc_event),
942 MIBSTAT(collision),
943 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200944};
945
Lennert Buytenheke5371492008-06-01 02:18:13 +0200946static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200947{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200948 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200949 int err;
950
951 spin_lock_irq(&mp->lock);
952 err = mii_ethtool_gset(&mp->mii, cmd);
953 spin_unlock_irq(&mp->lock);
954
955 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
956 cmd->supported &= ~SUPPORTED_1000baseT_Half;
957 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
958
959 return err;
960}
961
Lennert Buytenheke5371492008-06-01 02:18:13 +0200962static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200963{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200964 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200965 int err;
966
967 spin_lock_irq(&mp->lock);
968 err = mii_ethtool_sset(&mp->mii, cmd);
969 spin_unlock_irq(&mp->lock);
970
971 return err;
972}
973
Lennert Buytenheke5371492008-06-01 02:18:13 +0200974static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200975 struct ethtool_drvinfo *drvinfo)
976{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200977 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
978 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200979 strncpy(drvinfo->fw_version, "N/A", 32);
980 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +0200981 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200982}
983
984static int mv643xx_eth_nway_restart(struct net_device *dev)
985{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200986 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200987
988 return mii_nway_restart(&mp->mii);
989}
990
991static u32 mv643xx_eth_get_link(struct net_device *dev)
992{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200993 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200994
995 return mii_link_ok(&mp->mii);
996}
997
Lennert Buytenheke5371492008-06-01 02:18:13 +0200998static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200999 uint8_t *data)
1000{
1001 int i;
1002
1003 switch(stringset) {
1004 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +02001005 for (i=0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001006 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001007 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001008 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001009 }
1010 break;
1011 }
1012}
1013
Lennert Buytenheke5371492008-06-01 02:18:13 +02001014static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001015 struct ethtool_stats *stats, uint64_t *data)
1016{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001017 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001018 int i;
1019
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001020 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001021
Lennert Buytenhek16820052008-06-01 11:40:29 +02001022 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1023 const struct mv643xx_eth_stats *stat;
1024 void *p;
1025
1026 stat = mv643xx_eth_stats + i;
1027
1028 if (stat->netdev_off >= 0)
1029 p = ((void *)mp->dev) + stat->netdev_off;
1030 else
1031 p = ((void *)mp) + stat->mp_off;
1032
1033 data[i] = (stat->sizeof_stat == 8) ?
1034 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001035 }
1036}
1037
Lennert Buytenheke5371492008-06-01 02:18:13 +02001038static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001039{
1040 switch (sset) {
1041 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +02001042 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001043 default:
1044 return -EOPNOTSUPP;
1045 }
1046}
1047
Lennert Buytenheke5371492008-06-01 02:18:13 +02001048static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1049 .get_settings = mv643xx_eth_get_settings,
1050 .set_settings = mv643xx_eth_set_settings,
1051 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001052 .get_link = mv643xx_eth_get_link,
1053 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001054 .get_sset_count = mv643xx_eth_get_sset_count,
1055 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1056 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001057 .nway_reset = mv643xx_eth_nway_restart,
1058};
1059
1060
1061/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001062static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001063{
1064 unsigned int port_num = mp->port_num;
1065 unsigned int mac_h;
1066 unsigned int mac_l;
1067
1068 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1069 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1070
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001071 addr[0] = (mac_h >> 24) & 0xff;
1072 addr[1] = (mac_h >> 16) & 0xff;
1073 addr[2] = (mac_h >> 8) & 0xff;
1074 addr[3] = mac_h & 0xff;
1075 addr[4] = (mac_l >> 8) & 0xff;
1076 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001077}
1078
Lennert Buytenheke5371492008-06-01 02:18:13 +02001079static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001080{
1081 unsigned int port_num = mp->port_num;
1082 int table_index;
1083
1084 /* Clear DA filter unicast table (Ex_dFUT) */
1085 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001086 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001087
1088 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1089 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001090 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001091 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001092 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001093 }
1094}
1095
Lennert Buytenheke5371492008-06-01 02:18:13 +02001096static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001097 int table, unsigned char entry)
1098{
1099 unsigned int table_reg;
1100 unsigned int tbl_offset;
1101 unsigned int reg_offset;
1102
1103 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1104 reg_offset = entry % 4; /* Entry offset within the register */
1105
1106 /* Set "accepts frame bit" at specified table entry */
1107 table_reg = rdl(mp, table + tbl_offset);
1108 table_reg |= 0x01 << (8 * reg_offset);
1109 wrl(mp, table + tbl_offset, table_reg);
1110}
1111
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001112static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001113{
1114 unsigned int port_num = mp->port_num;
1115 unsigned int mac_h;
1116 unsigned int mac_l;
1117 int table;
1118
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001119 mac_l = (addr[4] << 8) | (addr[5]);
1120 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1121 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001122
1123 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1124 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1125
1126 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001127 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001128 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001129}
1130
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131static void mv643xx_eth_update_mac_address(struct net_device *dev)
1132{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001133 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001134
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001135 init_mac_tables(mp);
1136 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001137}
1138
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001139static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1140{
1141 int i;
1142
1143 for (i = 0; i < 6; i++)
1144 /* +2 is for the offset of the HW addr type */
1145 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1146 mv643xx_eth_update_mac_address(dev);
1147 return 0;
1148}
1149
Lennert Buytenhek69876562008-06-01 11:43:32 +02001150static int addr_crc(unsigned char *addr)
1151{
1152 int crc = 0;
1153 int i;
1154
1155 for (i = 0; i < 6; i++) {
1156 int j;
1157
1158 crc = (crc ^ addr[i]) << 8;
1159 for (j = 7; j >= 0; j--) {
1160 if (crc & (0x100 << j))
1161 crc ^= 0x107 << j;
1162 }
1163 }
1164
1165 return crc;
1166}
1167
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001168static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001169{
1170 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001171 int table;
Lennert Buytenhek69876562008-06-01 11:43:32 +02001172 int crc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001173
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001174 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1175 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001176 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001177 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001178 return;
1179 }
1180
Lennert Buytenhek69876562008-06-01 11:43:32 +02001181 crc = addr_crc(addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001182
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001183 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhek69876562008-06-01 11:43:32 +02001184 set_filter_table_entry(mp, table, crc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001185}
1186
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001187static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001188{
1189
1190 struct dev_mc_list *mc_list;
1191 int i;
1192 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001193 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001194 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001195
1196 /* If the device is in promiscuous mode or in all multicast mode,
1197 * we will fully populate both multicast tables with accept.
1198 * This is guaranteed to yield a match on all multicast addresses...
1199 */
1200 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1201 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1202 /* Set all entries in DA filter special multicast
1203 * table (Ex_dFSMT)
1204 * Set for ETH_Q0 for now
1205 * Bits
1206 * 0 Accept=1, Drop=0
1207 * 3-1 Queue ETH_Q0=0
1208 * 7-4 Reserved = 0;
1209 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001210 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001211
1212 /* Set all entries in DA filter other multicast
1213 * table (Ex_dFOMT)
1214 * Set for ETH_Q0 for now
1215 * Bits
1216 * 0 Accept=1, Drop=0
1217 * 3-1 Queue ETH_Q0=0
1218 * 7-4 Reserved = 0;
1219 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001220 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001221 }
1222 return;
1223 }
1224
1225 /* We will clear out multicast tables every time we get the list.
1226 * Then add the entire new list...
1227 */
1228 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1229 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001230 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001231
1232 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001233 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001234 }
1235
1236 /* Get pointer to net_device multicast list and add each one... */
1237 for (i = 0, mc_list = dev->mc_list;
1238 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1239 i++, mc_list = mc_list->next)
1240 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001241 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001242}
1243
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001244static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1245{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001246 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001247 u32 config_reg;
1248
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001249 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001250 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001251 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001252 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001253 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001254 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001255
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001256 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001257}
1258
1259
1260/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001261static int rxq_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001262{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001263 struct rx_queue *rxq = mp->rxq;
1264 struct rx_desc *rx_desc;
1265 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001266 int i;
1267
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001268 rxq->rx_ring_size = mp->default_rx_ring_size;
1269
1270 rxq->rx_desc_count = 0;
1271 rxq->rx_curr_desc = 0;
1272 rxq->rx_used_desc = 0;
1273
1274 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1275
1276 if (size <= mp->rx_desc_sram_size) {
1277 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1278 mp->rx_desc_sram_size);
1279 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1280 } else {
1281 rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
1282 &rxq->rx_desc_dma,
1283 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001284 }
1285
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001286 if (rxq->rx_desc_area == NULL) {
1287 dev_printk(KERN_ERR, &mp->dev->dev,
1288 "can't allocate rx ring (%d bytes)\n", size);
1289 goto out;
1290 }
1291 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001292
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001293 rxq->rx_desc_area_size = size;
1294 rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
1295 GFP_KERNEL);
1296 if (rxq->rx_skb == NULL) {
1297 dev_printk(KERN_ERR, &mp->dev->dev,
1298 "can't allocate rx skb ring\n");
1299 goto out_free;
1300 }
1301
1302 rx_desc = (struct rx_desc *)rxq->rx_desc_area;
1303 for (i = 0; i < rxq->rx_ring_size; i++) {
1304 int nexti = (i + 1) % rxq->rx_ring_size;
1305 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1306 nexti * sizeof(struct rx_desc);
1307 }
1308
1309 init_timer(&rxq->rx_oom);
1310 rxq->rx_oom.data = (unsigned long)rxq;
1311 rxq->rx_oom.function = rxq_refill_timer_wrapper;
1312
1313 return 0;
1314
1315
1316out_free:
1317 if (size <= mp->rx_desc_sram_size)
1318 iounmap(rxq->rx_desc_area);
1319 else
1320 dma_free_coherent(NULL, size,
1321 rxq->rx_desc_area,
1322 rxq->rx_desc_dma);
1323
1324out:
1325 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326}
1327
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001328static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001329{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001330 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1331 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001332
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001333 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001334
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001335 del_timer_sync(&rxq->rx_oom);
1336
1337 for (i = 0; i < rxq->rx_ring_size; i++) {
1338 if (rxq->rx_skb[i]) {
1339 dev_kfree_skb(rxq->rx_skb[i]);
1340 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001341 }
1342 }
1343
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001344 if (rxq->rx_desc_count) {
1345 dev_printk(KERN_ERR, &mp->dev->dev,
1346 "error freeing rx ring -- %d skbs stuck\n",
1347 rxq->rx_desc_count);
1348 }
1349
1350 if (rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
1351 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001352 else
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001353 dma_free_coherent(NULL, rxq->rx_desc_area_size,
1354 rxq->rx_desc_area, rxq->rx_desc_dma);
1355
1356 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001357}
1358
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001359static int txq_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001360{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001361 struct tx_queue *txq = mp->txq;
1362 struct tx_desc *tx_desc;
1363 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001364 int i;
1365
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001366 txq->tx_ring_size = mp->default_tx_ring_size;
1367
1368 txq->tx_desc_count = 0;
1369 txq->tx_curr_desc = 0;
1370 txq->tx_used_desc = 0;
1371
1372 size = txq->tx_ring_size * sizeof(struct tx_desc);
1373
1374 if (size <= mp->tx_desc_sram_size) {
1375 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1376 mp->tx_desc_sram_size);
1377 txq->tx_desc_dma = mp->tx_desc_sram_addr;
1378 } else {
1379 txq->tx_desc_area = dma_alloc_coherent(NULL, size,
1380 &txq->tx_desc_dma,
1381 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001382 }
1383
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001384 if (txq->tx_desc_area == NULL) {
1385 dev_printk(KERN_ERR, &mp->dev->dev,
1386 "can't allocate tx ring (%d bytes)\n", size);
1387 goto out;
1388 }
1389 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001390
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001391 txq->tx_desc_area_size = size;
1392 txq->tx_skb = kmalloc(txq->tx_ring_size * sizeof(*txq->tx_skb),
1393 GFP_KERNEL);
1394 if (txq->tx_skb == NULL) {
1395 dev_printk(KERN_ERR, &mp->dev->dev,
1396 "can't allocate tx skb ring\n");
1397 goto out_free;
1398 }
1399
1400 tx_desc = (struct tx_desc *)txq->tx_desc_area;
1401 for (i = 0; i < txq->tx_ring_size; i++) {
1402 int nexti = (i + 1) % txq->tx_ring_size;
1403 tx_desc[i].next_desc_ptr = txq->tx_desc_dma +
1404 nexti * sizeof(struct tx_desc);
1405 }
1406
1407 return 0;
1408
1409
1410out_free:
1411 if (size <= mp->tx_desc_sram_size)
1412 iounmap(txq->tx_desc_area);
1413 else
1414 dma_free_coherent(NULL, size,
1415 txq->tx_desc_area,
1416 txq->tx_desc_dma);
1417
1418out:
1419 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001420}
1421
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001422static void txq_reclaim(struct tx_queue *txq, int force)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001423{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001424 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001425 unsigned long flags;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001426
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001427 spin_lock_irqsave(&mp->lock, flags);
1428 while (txq->tx_desc_count > 0) {
1429 int tx_index;
1430 struct tx_desc *desc;
1431 u32 cmd_sts;
1432 struct sk_buff *skb;
1433 dma_addr_t addr;
1434 int count;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001435
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001436 tx_index = txq->tx_used_desc;
1437 desc = &txq->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001438 cmd_sts = desc->cmd_sts;
1439
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001440 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA))
1441 break;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001442
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001443 txq->tx_used_desc = (tx_index + 1) % txq->tx_ring_size;
1444 txq->tx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001445
1446 addr = desc->buf_ptr;
1447 count = desc->byte_cnt;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001448 skb = txq->tx_skb[tx_index];
1449 txq->tx_skb[tx_index] = NULL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001450
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001451 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001452 dev_printk(KERN_INFO, &mp->dev->dev, "tx error\n");
1453 mp->dev->stats.tx_errors++;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001454 }
1455
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001456 /*
1457 * Drop mp->lock while we free the skb.
1458 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001459 spin_unlock_irqrestore(&mp->lock, flags);
1460
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001461 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001462 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1463 else
1464 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1465
1466 if (skb)
1467 dev_kfree_skb_irq(skb);
1468
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001469 spin_lock_irqsave(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001470 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001471 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001472}
1473
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001474static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001475{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001476 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001477
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001478 txq_disable(txq);
1479 txq_reclaim(txq, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001480
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001481 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001482
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001483 if (txq->tx_desc_area_size <= mp->tx_desc_sram_size)
1484 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001485 else
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001486 dma_free_coherent(NULL, txq->tx_desc_area_size,
1487 txq->tx_desc_area, txq->tx_desc_dma);
1488
1489 kfree(txq->tx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001490}
1491
1492
1493/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001494static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001495
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001496static void mv643xx_eth_update_pscr(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001497 struct ethtool_cmd *ecmd)
1498{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001499 u32 pscr_o;
1500 u32 pscr_n;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001501
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001502 pscr_o = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001503
1504 /* clear speed, duplex and rx buffer size fields */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001505 pscr_n = pscr_o & ~(SET_MII_SPEED_TO_100 |
1506 SET_GMII_SPEED_TO_1000 |
1507 SET_FULL_DUPLEX_MODE |
1508 MAX_RX_PACKET_MASK);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001509
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001510 if (ecmd->speed == SPEED_1000) {
1511 pscr_n |= SET_GMII_SPEED_TO_1000 | MAX_RX_PACKET_9700BYTE;
1512 } else {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001513 if (ecmd->speed == SPEED_100)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001514 pscr_n |= SET_MII_SPEED_TO_100;
1515 pscr_n |= MAX_RX_PACKET_1522BYTE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001516 }
1517
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001518 if (ecmd->duplex == DUPLEX_FULL)
1519 pscr_n |= SET_FULL_DUPLEX_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001520
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001521 if (pscr_n != pscr_o) {
1522 if ((pscr_o & SERIAL_PORT_ENABLE) == 0)
1523 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
1524 else {
1525 txq_disable(mp->txq);
1526 pscr_o &= ~SERIAL_PORT_ENABLE;
1527 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_o);
1528 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
1529 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr_n);
1530 txq_enable(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001531 }
1532 }
1533}
1534
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001535static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1536{
1537 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001538 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001539 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001540
1541 /* Read interrupt cause registers */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001542 int_cause = rdl(mp, INT_CAUSE(mp->port_num)) & (INT_RX | INT_EXT);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001543 if (int_cause & INT_EXT) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001544 int_cause_ext = rdl(mp, INT_CAUSE_EXT(mp->port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001545 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001546 wrl(mp, INT_CAUSE_EXT(mp->port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001547 }
1548
1549 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001550 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001551 if (mii_link_ok(&mp->mii)) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001552 struct ethtool_cmd cmd;
1553
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001554 mii_ethtool_gset(&mp->mii, &cmd);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001555 mv643xx_eth_update_pscr(mp, &cmd);
1556 txq_enable(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001557 if (!netif_carrier_ok(dev)) {
1558 netif_carrier_on(dev);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001559 __txq_maybe_wake(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001560 }
1561 } else if (netif_carrier_ok(dev)) {
1562 netif_stop_queue(dev);
1563 netif_carrier_off(dev);
1564 }
1565 }
1566
Lennert Buytenheke5371492008-06-01 02:18:13 +02001567#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001568 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001569 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001570 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001571
1572 /* wait for previous write to complete */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001573 rdl(mp, INT_MASK(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001574
1575 netif_rx_schedule(dev, &mp->napi);
1576 }
1577#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001578 if (int_cause & INT_RX)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001579 rxq_process(mp->rxq, INT_MAX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001580#endif
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001581 if (int_cause_ext & INT_EXT_TX) {
1582 txq_reclaim(mp->txq, 0);
1583 __txq_maybe_wake(mp->txq);
1584 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001585
1586 /*
1587 * If no real interrupt occured, exit.
1588 * This can happen when using gigE interrupt coalescing mechanism.
1589 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001590 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001591 return IRQ_NONE;
1592
1593 return IRQ_HANDLED;
1594}
1595
Lennert Buytenheke5371492008-06-01 02:18:13 +02001596static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001597{
1598 unsigned int phy_reg_data;
1599
1600 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001601 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001602 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001603 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001604
1605 /* wait for PHY to come out of reset */
1606 do {
1607 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001608 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001609 } while (phy_reg_data & 0x8000);
1610}
1611
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001612static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001613{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001614 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001615 u32 pscr;
1616 struct ethtool_cmd ethtool_cmd;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001617 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001618
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001619 /*
1620 * Configure basic link parameters.
1621 */
1622 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
1623 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
1624 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1625 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1626 DISABLE_AUTO_NEG_SPEED_GMII |
1627 DISABLE_AUTO_NEG_FOR_DUPLEX |
1628 DO_NOT_FORCE_LINK_FAIL |
1629 SERIAL_PORT_CONTROL_RESERVED;
1630 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1631 pscr |= SERIAL_PORT_ENABLE;
1632 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001633
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001634 wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
1635
1636 mv643xx_eth_get_settings(dev, &ethtool_cmd);
1637 phy_reset(mp);
1638 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001639
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001640 /*
1641 * Configure TX path and queues.
1642 */
1643 wrl(mp, TX_BW_MTU(mp->port_num), 0);
1644 for (i = 0; i < 1; i++) {
1645 struct tx_queue *txq = mp->txq;
1646 int off = TXQ_CURRENT_DESC_PTR(mp->port_num);
1647 u32 addr;
1648
1649 addr = (u32)txq->tx_desc_dma;
1650 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
1651 wrl(mp, off, addr);
1652 }
1653
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001654 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001655 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001656
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001657 /*
1658 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1659 * frames to RX queue #0.
1660 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001661 wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001662
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001663 /*
1664 * Treat BPDUs as normal multicasts, and disable partition mode.
1665 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001666 wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001667
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001668 /*
1669 * Enable the receive queue.
1670 */
1671 for (i = 0; i < 1; i++) {
1672 struct rx_queue *rxq = mp->rxq;
1673 int off = RXQ_CURRENT_DESC_PTR(mp->port_num);
1674 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001675
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001676 addr = (u32)rxq->rx_desc_dma;
1677 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
1678 wrl(mp, off, addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001679
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001680 rxq_enable(rxq);
1681 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001682}
1683
Lennert Buytenheke5371492008-06-01 02:18:13 +02001684#ifdef MV643XX_ETH_COAL
Lennert Buytenheke5371492008-06-01 02:18:13 +02001685static unsigned int set_rx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001686 unsigned int delay)
1687{
1688 unsigned int port_num = mp->port_num;
1689 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1690
1691 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001692 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001693 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001694 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001695 & 0xffc000ff));
1696
1697 return coal;
1698}
1699#endif
1700
Lennert Buytenheke5371492008-06-01 02:18:13 +02001701static unsigned int set_tx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001702 unsigned int delay)
1703{
1704 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1705
1706 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001707 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001708
1709 return coal;
1710}
1711
Lennert Buytenheke5371492008-06-01 02:18:13 +02001712static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001713{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001714 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001715
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001716 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001717}
1718
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001719static int mv643xx_eth_open(struct net_device *dev)
1720{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001721 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001722 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001723 int err;
1724
1725 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001726 wrl(mp, INT_CAUSE(port_num), 0);
1727 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001728 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001729 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001730
1731 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1732 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1733 if (err) {
1734 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1735 return -EAGAIN;
1736 }
1737
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001738 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001739
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001740 err = rxq_init(mp);
1741 if (err)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001742 goto out_free_irq;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001743 rxq_refill(mp->rxq);
1744
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001745 err = txq_init(mp);
1746 if (err)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001747 goto out_free_rx_skb;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001748
Lennert Buytenheke5371492008-06-01 02:18:13 +02001749#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001750 napi_enable(&mp->napi);
1751#endif
1752
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001753 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001754
1755 /* Interrupt Coalescing */
1756
Lennert Buytenheke5371492008-06-01 02:18:13 +02001757#ifdef MV643XX_ETH_COAL
1758 mp->rx_int_coal = set_rx_coal(mp, MV643XX_ETH_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001759#endif
1760
Lennert Buytenheke5371492008-06-01 02:18:13 +02001761 mp->tx_int_coal = set_tx_coal(mp, MV643XX_ETH_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001762
1763 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001764 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001765
1766 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001767 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001768
1769 return 0;
1770
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001771
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001772out_free_rx_skb:
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001773 rxq_deinit(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001774out_free_irq:
1775 free_irq(dev->irq, dev);
1776
1777 return err;
1778}
1779
Lennert Buytenheke5371492008-06-01 02:18:13 +02001780static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001781{
1782 unsigned int port_num = mp->port_num;
1783 unsigned int reg_data;
1784
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001785 txq_disable(mp->txq);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001786 rxq_disable(mp->rxq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001787 while (!(rdl(mp, PORT_STATUS(mp->port_num)) & TX_FIFO_EMPTY))
1788 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001789
1790 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001791 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001792
1793 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001794 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001795 reg_data &= ~(SERIAL_PORT_ENABLE |
1796 DO_NOT_FORCE_LINK_FAIL |
1797 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001798 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001799}
1800
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001801static int mv643xx_eth_stop(struct net_device *dev)
1802{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001803 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001804 unsigned int port_num = mp->port_num;
1805
1806 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001807 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001808 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001809 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001810
Lennert Buytenheke5371492008-06-01 02:18:13 +02001811#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001812 napi_disable(&mp->napi);
1813#endif
1814 netif_carrier_off(dev);
1815 netif_stop_queue(dev);
1816
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001817 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001818
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001819 txq_deinit(mp->txq);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001820 rxq_deinit(mp->rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001821
1822 free_irq(dev->irq, dev);
1823
1824 return 0;
1825}
1826
1827static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1828{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001829 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001830
1831 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1832}
1833
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001834static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1835{
1836 if ((new_mtu > 9500) || (new_mtu < 64))
1837 return -EINVAL;
1838
1839 dev->mtu = new_mtu;
1840 if (!netif_running(dev))
1841 return 0;
1842
1843 /*
1844 * Stop and then re-open the interface. This will allocate RX
1845 * skbs of the new MTU.
1846 * There is a possible danger that the open will not succeed,
1847 * due to memory being full, which might fail the open function.
1848 */
1849 mv643xx_eth_stop(dev);
1850 if (mv643xx_eth_open(dev)) {
1851 printk(KERN_ERR "%s: Fatal error on opening device\n",
1852 dev->name);
1853 }
1854
1855 return 0;
1856}
1857
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001858static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1859{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001860 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001861 tx_timeout_task);
1862 struct net_device *dev = mp->dev;
1863
1864 if (!netif_running(dev))
1865 return;
1866
1867 netif_stop_queue(dev);
1868
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001869 port_reset(mp);
1870 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001871
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001872 __txq_maybe_wake(mp->txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001873}
1874
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001875static void mv643xx_eth_tx_timeout(struct net_device *dev)
1876{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001877 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001878
1879 printk(KERN_INFO "%s: TX timeout ", dev->name);
1880
1881 /* Do the reset outside of interrupt context */
1882 schedule_work(&mp->tx_timeout_task);
1883}
1884
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001885#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001886static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001887{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001888 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001889 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001890
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001891 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001892 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001893 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001894
Al Viro9da3b1a2006-10-08 15:00:44 +01001895 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001896
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001897 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001898}
1899#endif
1900
Lennert Buytenheke5371492008-06-01 02:18:13 +02001901static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001902{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001903 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001904 int val;
1905
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001906 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001907 return val;
1908}
1909
Lennert Buytenheke5371492008-06-01 02:18:13 +02001910static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001911{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001912 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001913 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001914}
1915
1916
1917/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001918static void
1919mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
1920 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001921{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001922 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001923 u32 win_enable;
1924 u32 win_protect;
1925 int i;
1926
1927 for (i = 0; i < 6; i++) {
1928 writel(0, base + WINDOW_BASE(i));
1929 writel(0, base + WINDOW_SIZE(i));
1930 if (i < 4)
1931 writel(0, base + WINDOW_REMAP_HIGH(i));
1932 }
1933
1934 win_enable = 0x3f;
1935 win_protect = 0;
1936
1937 for (i = 0; i < dram->num_cs; i++) {
1938 struct mbus_dram_window *cs = dram->cs + i;
1939
1940 writel((cs->base & 0xffff0000) |
1941 (cs->mbus_attr << 8) |
1942 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1943 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1944
1945 win_enable &= ~(1 << i);
1946 win_protect |= 3 << (2 * i);
1947 }
1948
1949 writel(win_enable, base + WINDOW_BAR_ENABLE);
1950 msp->win_protect = win_protect;
1951}
1952
1953static int mv643xx_eth_shared_probe(struct platform_device *pdev)
1954{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001955 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001956 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001957 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001958 struct resource *res;
1959 int ret;
1960
Lennert Buytenheke5371492008-06-01 02:18:13 +02001961 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001962 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1963
1964 ret = -EINVAL;
1965 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1966 if (res == NULL)
1967 goto out;
1968
1969 ret = -ENOMEM;
1970 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
1971 if (msp == NULL)
1972 goto out;
1973 memset(msp, 0, sizeof(*msp));
1974
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001975 msp->base = ioremap(res->start, res->end - res->start + 1);
1976 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001977 goto out_free;
1978
1979 spin_lock_init(&msp->phy_lock);
1980 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
1981
1982 platform_set_drvdata(pdev, msp);
1983
1984 /*
1985 * (Re-)program MBUS remapping windows if we are asked to.
1986 */
1987 if (pd != NULL && pd->dram != NULL)
1988 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
1989
1990 return 0;
1991
1992out_free:
1993 kfree(msp);
1994out:
1995 return ret;
1996}
1997
1998static int mv643xx_eth_shared_remove(struct platform_device *pdev)
1999{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002000 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002001
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002002 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002003 kfree(msp);
2004
2005 return 0;
2006}
2007
2008static struct platform_driver mv643xx_eth_shared_driver = {
2009 .probe = mv643xx_eth_shared_probe,
2010 .remove = mv643xx_eth_shared_remove,
2011 .driver = {
2012 .name = MV643XX_ETH_SHARED_NAME,
2013 .owner = THIS_MODULE,
2014 },
2015};
2016
Lennert Buytenheke5371492008-06-01 02:18:13 +02002017static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002018{
2019 u32 reg_data;
2020 int addr_shift = 5 * mp->port_num;
2021
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002022 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002023 reg_data &= ~(0x1f << addr_shift);
2024 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002025 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002026}
2027
Lennert Buytenheke5371492008-06-01 02:18:13 +02002028static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002029{
2030 unsigned int reg_data;
2031
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002032 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002033
2034 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2035}
2036
Lennert Buytenheke5371492008-06-01 02:18:13 +02002037static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002038{
2039 unsigned int phy_reg_data0;
2040 int auto_neg;
2041
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002042 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002043 auto_neg = phy_reg_data0 & 0x1000;
2044 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002045 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002046
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002047 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002048 if ((phy_reg_data0 & 0x1000) == auto_neg)
2049 return -ENODEV; /* change didn't take */
2050
2051 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002052 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002053 return 0;
2054}
2055
James Chapmand0412d92006-01-27 01:15:30 -07002056static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2057 int speed, int duplex,
2058 struct ethtool_cmd *cmd)
2059{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002060 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002061
2062 memset(cmd, 0, sizeof(*cmd));
2063
2064 cmd->port = PORT_MII;
2065 cmd->transceiver = XCVR_INTERNAL;
2066 cmd->phy_address = phy_address;
2067
2068 if (speed == 0) {
2069 cmd->autoneg = AUTONEG_ENABLE;
2070 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2071 cmd->speed = SPEED_100;
2072 cmd->advertising = ADVERTISED_10baseT_Half |
2073 ADVERTISED_10baseT_Full |
2074 ADVERTISED_100baseT_Half |
2075 ADVERTISED_100baseT_Full;
2076 if (mp->mii.supports_gmii)
2077 cmd->advertising |= ADVERTISED_1000baseT_Full;
2078 } else {
2079 cmd->autoneg = AUTONEG_DISABLE;
2080 cmd->speed = speed;
2081 cmd->duplex = duplex;
2082 }
2083}
2084
Russell King3ae5eae2005-11-09 22:32:44 +00002085static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002088 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002089 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 struct net_device *dev;
2091 u8 *p;
2092 struct resource *res;
2093 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002094 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002095 int duplex = DUPLEX_HALF;
2096 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002097 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002099 pd = pdev->dev.platform_data;
2100 if (pd == NULL) {
2101 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2102 return -ENODEV;
2103 }
2104
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002105 if (pd->shared == NULL) {
2106 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2107 return -ENODEV;
2108 }
2109
Lennert Buytenheke5371492008-06-01 02:18:13 +02002110 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 if (!dev)
2112 return -ENOMEM;
2113
Russell King3ae5eae2005-11-09 22:32:44 +00002114 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002117 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002118#ifdef MV643XX_ETH_NAPI
2119 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002120#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2123 BUG_ON(!res);
2124 dev->irq = res->start;
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 dev->open = mv643xx_eth_open;
2127 dev->stop = mv643xx_eth_stop;
2128 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 dev->set_mac_address = mv643xx_eth_set_mac_address;
2130 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2131
2132 /* No need to Tx Timeout */
2133 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002135#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002136 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002137#endif
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 dev->base_addr = 0;
2141 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002142 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002143 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Lennert Buytenheke5371492008-06-01 02:18:13 +02002145#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146#ifdef MAX_SKB_FRAGS
2147 /*
2148 * Zero copy can only work if we use Discovery II memory. Else, we will
2149 * have to map the buffers to ISA memory which is only 16 MB
2150 */
Wolfram Joost63890572006-01-16 16:57:41 -07002151 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152#endif
2153#endif
2154
2155 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002156 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 spin_lock_init(&mp->lock);
2159
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002160 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002161 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002162
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002163 if (mp->shared->win_protect)
2164 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2165
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002166 mp->shared_smi = mp->shared;
2167 if (pd->shared_smi != NULL)
2168 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002171 uc_addr_get(mp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002173 if (is_valid_ether_addr(pd->mac_addr))
2174 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002176 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002177 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002179 mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002180 if (pd->rx_queue_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002181 mp->default_rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002183 mp->default_tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002184 if (pd->tx_queue_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002185 mp->default_tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002187 if (pd->tx_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002188 mp->tx_desc_sram_size = pd->tx_sram_size;
2189 mp->tx_desc_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 }
2191
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002192 if (pd->rx_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002193 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2194 mp->rx_desc_sram_size = pd->rx_sram_size;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002195 }
2196
2197 duplex = pd->duplex;
2198 speed = pd->speed;
2199
James Chapmanc28a4f82006-01-27 01:13:15 -07002200 /* Hook up MII support for ethtool */
2201 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002202 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2203 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002204 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002205 mp->mii.phy_id_mask = 0x3f;
2206 mp->mii.reg_num_mask = 0x1f;
2207
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002208 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002210 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002211 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002212 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 }
2214
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002215 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002216 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002217 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002218 mv643xx_eth_update_pscr(mp, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002219 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002220
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002221 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 err = register_netdev(dev);
2223 if (err)
2224 goto out;
2225
2226 p = dev->dev_addr;
2227 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002228 "%s: port %d with MAC address %s\n",
2229 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
2231 if (dev->features & NETIF_F_SG)
2232 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2233
2234 if (dev->features & NETIF_F_IP_CSUM)
2235 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2236 dev->name);
2237
Lennert Buytenheke5371492008-06-01 02:18:13 +02002238#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2240#endif
2241
Lennert Buytenheke5371492008-06-01 02:18:13 +02002242#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2244 dev->name);
2245#endif
2246
Lennert Buytenheke5371492008-06-01 02:18:13 +02002247#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2249#endif
2250
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002251 if (mp->tx_desc_sram_size > 0)
Nicolas DETb1529872005-10-28 17:46:30 -07002252 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return 0;
2255
2256out:
2257 free_netdev(dev);
2258
2259 return err;
2260}
2261
Russell King3ae5eae2005-11-09 22:32:44 +00002262static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
Russell King3ae5eae2005-11-09 22:32:44 +00002264 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 unregister_netdev(dev);
2267 flush_scheduled_work();
2268
2269 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002270 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 return 0;
2272}
2273
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002274static void mv643xx_eth_shutdown(struct platform_device *pdev)
2275{
2276 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002277 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002278 unsigned int port_num = mp->port_num;
2279
2280 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002281 wrl(mp, INT_MASK(port_num), 0);
2282 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002283
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002284 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002285}
2286
Russell King3ae5eae2005-11-09 22:32:44 +00002287static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 .probe = mv643xx_eth_probe,
2289 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002290 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002291 .driver = {
2292 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002293 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002294 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295};
2296
Lennert Buytenheke5371492008-06-01 02:18:13 +02002297static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 int rc;
2300
Russell King3ae5eae2005-11-09 22:32:44 +00002301 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002303 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002305 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 }
2307 return rc;
2308}
2309
Lennert Buytenheke5371492008-06-01 02:18:13 +02002310static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Russell King3ae5eae2005-11-09 22:32:44 +00002312 platform_driver_unregister(&mv643xx_eth_driver);
2313 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314}
2315
Lennert Buytenheke5371492008-06-01 02:18:13 +02002316module_init(mv643xx_eth_init_module);
2317module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
2319MODULE_LICENSE("GPL");
2320MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2321 " and Dale Farnsworth");
2322MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002323MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2324MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);