blob: cc93af00201369716babc2934c8770e31dff4871 [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
76#define ETH_VLAN_HLEN 4
77#define ETH_FCS_LEN 4
78#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
79#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
80 ETH_VLAN_HLEN + ETH_FCS_LEN)
81#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
82 dma_get_cache_alignment())
83
84/*
85 * Registers shared between all ports.
86 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020087#define PHY_ADDR 0x0000
88#define SMI_REG 0x0004
89#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
90#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
91#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
92#define WINDOW_BAR_ENABLE 0x0290
93#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020094
95/*
96 * Per-port registers.
97 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020098#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020099#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200100#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
101#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
102#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
103#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
104#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
105#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200106#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200107#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
108#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
109#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200110#define INT_RX 0x00000804
111#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200112#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200113#define INT_EXT_LINK 0x00100000
114#define INT_EXT_PHY 0x00010000
115#define INT_EXT_TX_ERROR_0 0x00000100
116#define INT_EXT_TX_0 0x00000001
117#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200118#define INT_MASK(p) (0x0468 + ((p) << 10))
119#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
120#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
121#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
122#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
123#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
124#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
125#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
126#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
127#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200129
130/*
131 * SDMA configuration register.
132 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200134#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200136#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200137
138#if defined(__BIG_ENDIAN)
139#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
140 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141 TX_BURST_SIZE_4_64BIT
142#elif defined(__LITTLE_ENDIAN)
143#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
144 RX_BURST_SIZE_4_64BIT | \
145 BLM_RX_NO_SWAP | \
146 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147 TX_BURST_SIZE_4_64BIT
148#else
149#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
150#endif
151
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200152
153/*
154 * Port serial control register.
155 */
156#define SET_MII_SPEED_TO_100 (1 << 24)
157#define SET_GMII_SPEED_TO_1000 (1 << 23)
158#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200159#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200160#define MAX_RX_PACKET_9700BYTE (5 << 17)
161#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200162#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
163#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
164#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
165#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
166#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
167#define FORCE_LINK_PASS (1 << 1)
168#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200169
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200170#define DEFAULT_RX_QUEUE_SIZE 400
171#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173/* SMI reg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200174#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
175#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
176#define SMI_OPCODE_WRITE 0 /* Completion of Read */
177#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200178
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200180/*
181 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200182 */
183#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200184struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200185 u16 byte_cnt; /* Descriptor buffer byte count */
186 u16 buf_size; /* Buffer size */
187 u32 cmd_sts; /* Descriptor command status */
188 u32 next_desc_ptr; /* Next descriptor pointer */
189 u32 buf_ptr; /* Descriptor buffer pointer */
190};
191
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200192struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200193 u16 byte_cnt; /* buffer byte count */
194 u16 l4i_chk; /* CPU provided TCP checksum */
195 u32 cmd_sts; /* Command/status field */
196 u32 next_desc_ptr; /* Pointer to next descriptor */
197 u32 buf_ptr; /* pointer to buffer for this descriptor*/
198};
199#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200200struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200201 u32 cmd_sts; /* Descriptor command status */
202 u16 buf_size; /* Buffer size */
203 u16 byte_cnt; /* Descriptor buffer byte count */
204 u32 buf_ptr; /* Descriptor buffer pointer */
205 u32 next_desc_ptr; /* Next descriptor pointer */
206};
207
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200208struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200209 u32 cmd_sts; /* Command/status field */
210 u16 l4i_chk; /* CPU provided TCP checksum */
211 u16 byte_cnt; /* buffer byte count */
212 u32 buf_ptr; /* pointer to buffer for this descriptor*/
213 u32 next_desc_ptr; /* Pointer to next descriptor */
214};
215#else
216#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
217#endif
218
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200219/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200220#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200221
222/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200223#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200224
225/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200226#define LAYER_4_CHECKSUM_OK 0x40000000
227#define RX_ENABLE_INTERRUPT 0x20000000
228#define RX_FIRST_DESC 0x08000000
229#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200230
231/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200232#define TX_ENABLE_INTERRUPT 0x00800000
233#define GEN_CRC 0x00400000
234#define TX_FIRST_DESC 0x00200000
235#define TX_LAST_DESC 0x00100000
236#define ZERO_PADDING 0x00080000
237#define GEN_IP_V4_CHECKSUM 0x00040000
238#define GEN_TCP_UDP_CHECKSUM 0x00020000
239#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200240
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200241#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200242
243
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200244/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200245struct mv643xx_eth_shared_private {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200246 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200247
248 /* used to protect SMI_REG, which is shared across ports */
249 spinlock_t phy_lock;
250
251 u32 win_protect;
252
253 unsigned int t_clk;
254};
255
256
257/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200258struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200259 u64 good_octets_received;
260 u32 bad_octets_received;
261 u32 internal_mac_transmit_err;
262 u32 good_frames_received;
263 u32 bad_frames_received;
264 u32 broadcast_frames_received;
265 u32 multicast_frames_received;
266 u32 frames_64_octets;
267 u32 frames_65_to_127_octets;
268 u32 frames_128_to_255_octets;
269 u32 frames_256_to_511_octets;
270 u32 frames_512_to_1023_octets;
271 u32 frames_1024_to_max_octets;
272 u64 good_octets_sent;
273 u32 good_frames_sent;
274 u32 excessive_collision;
275 u32 multicast_frames_sent;
276 u32 broadcast_frames_sent;
277 u32 unrec_mac_control_received;
278 u32 fc_sent;
279 u32 good_fc_received;
280 u32 bad_fc_received;
281 u32 undersize_received;
282 u32 fragments_received;
283 u32 oversize_received;
284 u32 jabber_received;
285 u32 mac_receive_error;
286 u32 bad_crc_event;
287 u32 collision;
288 u32 late_collision;
289};
290
Lennert Buytenheke5371492008-06-01 02:18:13 +0200291struct mv643xx_eth_private {
292 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200293 int port_num; /* User Ethernet port number */
294
Lennert Buytenheke5371492008-06-01 02:18:13 +0200295 struct mv643xx_eth_shared_private *shared_smi;
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200296
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200297 u32 rx_sram_addr; /* Base address of rx sram area */
298 u32 rx_sram_size; /* Size of rx sram area */
299 u32 tx_sram_addr; /* Base address of tx sram area */
300 u32 tx_sram_size; /* Size of tx sram area */
301
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200302 /* Tx/Rx rings managment indexes fields. For driver use */
303
304 /* Next available and first returning Rx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200305 int rx_curr_desc, rx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200306
307 /* Next available and first returning Tx resource */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200308 int tx_curr_desc, tx_used_desc;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200309
Lennert Buytenheke5371492008-06-01 02:18:13 +0200310#ifdef MV643XX_ETH_TX_FAST_REFILL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200311 u32 tx_clean_threshold;
312#endif
313
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200314 struct rx_desc *rx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200315 dma_addr_t rx_desc_dma;
316 int rx_desc_area_size;
317 struct sk_buff **rx_skb;
318
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200319 struct tx_desc *tx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200320 dma_addr_t tx_desc_dma;
321 int tx_desc_area_size;
322 struct sk_buff **tx_skb;
323
324 struct work_struct tx_timeout_task;
325
326 struct net_device *dev;
327 struct napi_struct napi;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200328 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200329 spinlock_t lock;
330 /* Size of Tx Ring per queue */
331 int tx_ring_size;
332 /* Number of tx descriptors in use */
333 int tx_desc_count;
334 /* Size of Rx Ring per queue */
335 int rx_ring_size;
336 /* Number of rx descriptors in use */
337 int rx_desc_count;
338
339 /*
340 * Used in case RX Ring is empty, which can be caused when
341 * system does not have resources (skb's)
342 */
343 struct timer_list timeout;
344
345 u32 rx_int_coal;
346 u32 tx_int_coal;
347 struct mii_if_info mii;
348};
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 Buytenheke5371492008-06-01 02:18:13 +0200364static void mv643xx_eth_port_enable_rx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200365 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200367 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200368}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Lennert Buytenheke5371492008-06-01 02:18:13 +0200370static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200371{
372 unsigned int port_num = mp->port_num;
373 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700374
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200375 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200376 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200377 if (queues) {
378 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200379 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200380
381 /* Wait for all Rx activity to terminate. */
382 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200383 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200384 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 }
386
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200387 return queues;
388}
389
Lennert Buytenheke5371492008-06-01 02:18:13 +0200390static void mv643xx_eth_port_enable_tx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200391 unsigned int queues)
392{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200393 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200394}
395
Lennert Buytenheke5371492008-06-01 02:18:13 +0200396static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200397{
398 unsigned int port_num = mp->port_num;
399 u32 queues;
400
401 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200402 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200403 if (queues) {
404 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200405 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200406
407 /* Wait for all Tx activity to terminate. */
408 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200409 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200410 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200411
412 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200413 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200414 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200415 }
416
417 return queues;
418}
419
420
421/* rx ***********************************************************************/
422static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
423
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200424static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200425{
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200426 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200427 unsigned long flags;
428
429 spin_lock_irqsave(&mp->lock, flags);
430
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700431 while (mp->rx_desc_count < mp->rx_ring_size) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200432 struct sk_buff *skb;
433 int unaligned;
434 int rx;
435
Ralf Baechle908b6372007-02-26 19:52:06 +0000436 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200437 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200439
Ralf Baechle908b6372007-02-26 19:52:06 +0000440 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700441 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000442 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200443
444 mp->rx_desc_count++;
445 rx = mp->rx_used_desc;
446 mp->rx_used_desc = (rx + 1) % mp->rx_ring_size;
447
448 mp->rx_desc_area[rx].buf_ptr = dma_map_single(NULL,
449 skb->data,
450 ETH_RX_SKB_SIZE,
451 DMA_FROM_DEVICE);
452 mp->rx_desc_area[rx].buf_size = ETH_RX_SKB_SIZE;
453 mp->rx_skb[rx] = skb;
454 wmb();
455 mp->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
456 RX_ENABLE_INTERRUPT;
457 wmb();
458
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700459 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200461
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700462 if (mp->rx_desc_count == 0) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200463 mp->timeout.expires = jiffies + (HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200466
467 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700470static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700472 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200477 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700478 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 unsigned int received_packets = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Lennert Buytenhek96587662008-06-01 10:31:56 +0200481 while (budget-- > 0) {
482 struct sk_buff *skb;
483 volatile struct rx_desc *rx_desc;
484 unsigned int cmd_sts;
485 unsigned long flags;
486
487 spin_lock_irqsave(&mp->lock, flags);
488
489 rx_desc = &mp->rx_desc_area[mp->rx_curr_desc];
490
491 cmd_sts = rx_desc->cmd_sts;
492 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
493 spin_unlock_irqrestore(&mp->lock, flags);
494 break;
495 }
496 rmb();
497
498 skb = mp->rx_skb[mp->rx_curr_desc];
499 mp->rx_skb[mp->rx_curr_desc] = NULL;
500
501 mp->rx_curr_desc = (mp->rx_curr_desc + 1) % mp->rx_ring_size;
502
503 spin_unlock_irqrestore(&mp->lock, flags);
504
505 dma_unmap_single(NULL, rx_desc->buf_ptr + ETH_HW_IP_ALIGN,
506 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700507 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700509
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700510 /*
511 * Update statistics.
512 * Note byte count includes 4 byte CRC count
513 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 stats->rx_packets++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200515 stats->rx_bytes += rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
516
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * In case received a packet without first / last bits on OR
519 * the error summary bit is on, the packets needs to be dropeed.
520 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200521 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200522 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200523 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 stats->rx_dropped++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200525 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200526 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (net_ratelimit())
528 printk(KERN_ERR
529 "%s: Received packet spread "
530 "on multiple descriptors\n",
531 dev->name);
532 }
Lennert Buytenhek96587662008-06-01 10:31:56 +0200533 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 stats->rx_errors++;
535
536 dev_kfree_skb_irq(skb);
537 } else {
538 /*
539 * The -4 is for the CRC in the trailer of the
540 * received packet
541 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200542 skb_put(skb, rx_desc->byte_cnt - ETH_HW_IP_ALIGN - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Lennert Buytenhek96587662008-06-01 10:31:56 +0200544 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 skb->ip_summed = CHECKSUM_UNNECESSARY;
546 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200547 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 skb->protocol = eth_type_trans(skb, dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200550#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 netif_receive_skb(skb);
552#else
553 netif_rx(skb);
554#endif
555 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700556 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700558 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 return received_packets;
561}
562
Lennert Buytenheke5371492008-06-01 02:18:13 +0200563#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200564static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200566 struct mv643xx_eth_private *mp = container_of(napi, struct mv643xx_eth_private, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700567 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700569 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Lennert Buytenheke5371492008-06-01 02:18:13 +0200571#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700573 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576#endif
577
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700578 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200579 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200580 != (u32) mp->rx_used_desc)
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700581 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700583 if (work_done < budget) {
584 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200585 wrl(mp, INT_CAUSE(port_num), 0);
586 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200587 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700590 return work_done;
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{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700598 unsigned int frag;
599 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700600
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700601 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
602 fragp = &skb_shinfo(skb)->frags[frag];
603 if (fragp->size <= 8 && fragp->page_offset & 0x7)
604 return 1;
605 }
606 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700607}
608
Lennert Buytenheke5371492008-06-01 02:18:13 +0200609static int alloc_tx_desc_index(struct mv643xx_eth_private *mp)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700610{
611 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700612
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700613 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700614
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200615 tx_desc_curr = mp->tx_curr_desc;
616 mp->tx_curr_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700617
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200618 BUG_ON(mp->tx_curr_desc == mp->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700619
620 return tx_desc_curr;
621}
622
Lennert Buytenheke5371492008-06-01 02:18:13 +0200623static void tx_fill_frag_descs(struct mv643xx_eth_private *mp,
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700624 struct sk_buff *skb)
625{
626 int frag;
627 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200628 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700629
630 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
631 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
632
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200633 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200634 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700635
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200636 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700637 /* Last Frag enables interrupt and frees the skb */
638 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200639 desc->cmd_sts |= ZERO_PADDING |
640 TX_LAST_DESC |
641 TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700642 mp->tx_skb[tx_index] = skb;
643 } else
Al Viro05980772006-05-30 23:59:09 -0400644 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700645
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200646 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700647 desc->l4i_chk = 0;
648 desc->byte_cnt = this_frag->size;
649 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
650 this_frag->page_offset,
651 this_frag->size,
652 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700653 }
654}
655
Byron Bradley324ff2c2008-02-04 23:47:15 -0800656static inline __be16 sum16_as_be(__sum16 sum)
657{
658 return (__force __be16)sum;
659}
660
Lennert Buytenheke5371492008-06-01 02:18:13 +0200661static void tx_submit_descs_for_skb(struct mv643xx_eth_private *mp,
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700662 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700663{
664 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200665 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700666 u32 cmd_sts;
667 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700668 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700669
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200670 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700671
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200672 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200673 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700674
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700675 if (nr_frags) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200676 tx_fill_frag_descs(mp, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700677
678 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400679 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700680 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200681 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700682 length = skb->len;
683 mp->tx_skb[tx_index] = skb;
684 }
685
686 desc->byte_cnt = length;
687 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700688
Patrick McHardy84fa7932006-08-29 16:44:56 -0700689 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800690 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700691
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200692 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
693 GEN_IP_V4_CHECKSUM |
694 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700695
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700696 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700697 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200698 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800699 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700700 break;
701 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800702 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700703 break;
704 default:
705 BUG();
706 }
707 } else {
708 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200709 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700710 desc->l4i_chk = 0;
711 }
712
713 /* ensure all other descriptors are written before first cmd_sts */
714 wmb();
715 desc->cmd_sts = cmd_sts;
716
717 /* ensure all descriptors are written before poking hardware */
718 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200719 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700720
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700721 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
725{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200726 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700727 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700730 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700731
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700732 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
733 stats->tx_dropped++;
734 printk(KERN_DEBUG "%s: failed to linearize tiny "
735 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700736 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 spin_lock_irqsave(&mp->lock, flags);
740
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700741 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
742 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
743 netif_stop_queue(dev);
744 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700745 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700746 }
747
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200748 tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700749 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 stats->tx_packets++;
751 dev->trans_start = jiffies;
752
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700753 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
754 netif_stop_queue(dev);
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 spin_unlock_irqrestore(&mp->lock, flags);
757
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700758 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200761
762/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200763static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200764
Lennert Buytenheke5371492008-06-01 02:18:13 +0200765static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200766 unsigned int phy_reg, unsigned int *value)
767{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200768 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
769 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200770 unsigned long flags;
771 int i;
772
773 /* the SMI register is a shared resource */
774 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
775
776 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200777 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200778 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200779 printk("%s: PHY busy timeout\n", mp->dev->name);
780 goto out;
781 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200782 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200783 }
784
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200785 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200786
787 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200788 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200789 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200790 printk("%s: PHY read timeout\n", mp->dev->name);
791 goto out;
792 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200793 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200794 }
795
796 *value = readl(smi_reg) & 0xffff;
797out:
798 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
799}
800
Lennert Buytenheke5371492008-06-01 02:18:13 +0200801static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200802 unsigned int phy_reg, unsigned int value)
803{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200804 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
805 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200806 unsigned long flags;
807 int i;
808
809 /* the SMI register is a shared resource */
810 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
811
812 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200813 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200814 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200815 printk("%s: PHY busy timeout\n", mp->dev->name);
816 goto out;
817 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200818 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200819 }
820
821 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200822 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200823out:
824 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
825}
826
827
828/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200829static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200830{
831 unsigned int port_num = mp->port_num;
832 int i;
833
834 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200835 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200836 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200837}
838
Lennert Buytenheke5371492008-06-01 02:18:13 +0200839static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200840{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200841 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200842}
843
Lennert Buytenheke5371492008-06-01 02:18:13 +0200844static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200845{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200846 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200847
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200848 p->good_octets_received += read_mib(mp, 0x00);
849 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
850 p->bad_octets_received += read_mib(mp, 0x08);
851 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
852 p->good_frames_received += read_mib(mp, 0x10);
853 p->bad_frames_received += read_mib(mp, 0x14);
854 p->broadcast_frames_received += read_mib(mp, 0x18);
855 p->multicast_frames_received += read_mib(mp, 0x1c);
856 p->frames_64_octets += read_mib(mp, 0x20);
857 p->frames_65_to_127_octets += read_mib(mp, 0x24);
858 p->frames_128_to_255_octets += read_mib(mp, 0x28);
859 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
860 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
861 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
862 p->good_octets_sent += read_mib(mp, 0x38);
863 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
864 p->good_frames_sent += read_mib(mp, 0x40);
865 p->excessive_collision += read_mib(mp, 0x44);
866 p->multicast_frames_sent += read_mib(mp, 0x48);
867 p->broadcast_frames_sent += read_mib(mp, 0x4c);
868 p->unrec_mac_control_received += read_mib(mp, 0x50);
869 p->fc_sent += read_mib(mp, 0x54);
870 p->good_fc_received += read_mib(mp, 0x58);
871 p->bad_fc_received += read_mib(mp, 0x5c);
872 p->undersize_received += read_mib(mp, 0x60);
873 p->fragments_received += read_mib(mp, 0x64);
874 p->oversize_received += read_mib(mp, 0x68);
875 p->jabber_received += read_mib(mp, 0x6c);
876 p->mac_receive_error += read_mib(mp, 0x70);
877 p->bad_crc_event += read_mib(mp, 0x74);
878 p->collision += read_mib(mp, 0x78);
879 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200880}
881
882
883/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200884struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200885 char stat_string[ETH_GSTRING_LEN];
886 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +0200887 int netdev_off;
888 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200889};
890
Lennert Buytenhek16820052008-06-01 11:40:29 +0200891#define SSTAT(m) \
892 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
893 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200894
Lennert Buytenhek16820052008-06-01 11:40:29 +0200895#define MIBSTAT(m) \
896 { #m, FIELD_SIZEOF(struct mib_counters, m), \
897 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
898
899static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
900 SSTAT(rx_packets),
901 SSTAT(tx_packets),
902 SSTAT(rx_bytes),
903 SSTAT(tx_bytes),
904 SSTAT(rx_errors),
905 SSTAT(tx_errors),
906 SSTAT(rx_dropped),
907 SSTAT(tx_dropped),
908 MIBSTAT(good_octets_received),
909 MIBSTAT(bad_octets_received),
910 MIBSTAT(internal_mac_transmit_err),
911 MIBSTAT(good_frames_received),
912 MIBSTAT(bad_frames_received),
913 MIBSTAT(broadcast_frames_received),
914 MIBSTAT(multicast_frames_received),
915 MIBSTAT(frames_64_octets),
916 MIBSTAT(frames_65_to_127_octets),
917 MIBSTAT(frames_128_to_255_octets),
918 MIBSTAT(frames_256_to_511_octets),
919 MIBSTAT(frames_512_to_1023_octets),
920 MIBSTAT(frames_1024_to_max_octets),
921 MIBSTAT(good_octets_sent),
922 MIBSTAT(good_frames_sent),
923 MIBSTAT(excessive_collision),
924 MIBSTAT(multicast_frames_sent),
925 MIBSTAT(broadcast_frames_sent),
926 MIBSTAT(unrec_mac_control_received),
927 MIBSTAT(fc_sent),
928 MIBSTAT(good_fc_received),
929 MIBSTAT(bad_fc_received),
930 MIBSTAT(undersize_received),
931 MIBSTAT(fragments_received),
932 MIBSTAT(oversize_received),
933 MIBSTAT(jabber_received),
934 MIBSTAT(mac_receive_error),
935 MIBSTAT(bad_crc_event),
936 MIBSTAT(collision),
937 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200938};
939
Lennert Buytenheke5371492008-06-01 02:18:13 +0200940static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200941{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200942 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200943 int err;
944
945 spin_lock_irq(&mp->lock);
946 err = mii_ethtool_gset(&mp->mii, cmd);
947 spin_unlock_irq(&mp->lock);
948
949 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
950 cmd->supported &= ~SUPPORTED_1000baseT_Half;
951 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
952
953 return err;
954}
955
Lennert Buytenheke5371492008-06-01 02:18:13 +0200956static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200957{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200958 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200959 int err;
960
961 spin_lock_irq(&mp->lock);
962 err = mii_ethtool_sset(&mp->mii, cmd);
963 spin_unlock_irq(&mp->lock);
964
965 return err;
966}
967
Lennert Buytenheke5371492008-06-01 02:18:13 +0200968static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200969 struct ethtool_drvinfo *drvinfo)
970{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200971 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
972 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200973 strncpy(drvinfo->fw_version, "N/A", 32);
974 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +0200975 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200976}
977
978static int mv643xx_eth_nway_restart(struct net_device *dev)
979{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200980 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200981
982 return mii_nway_restart(&mp->mii);
983}
984
985static u32 mv643xx_eth_get_link(struct net_device *dev)
986{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200987 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200988
989 return mii_link_ok(&mp->mii);
990}
991
Lennert Buytenheke5371492008-06-01 02:18:13 +0200992static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200993 uint8_t *data)
994{
995 int i;
996
997 switch(stringset) {
998 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +0200999 for (i=0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001000 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001001 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001002 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001003 }
1004 break;
1005 }
1006}
1007
Lennert Buytenheke5371492008-06-01 02:18:13 +02001008static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001009 struct ethtool_stats *stats, uint64_t *data)
1010{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001011 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001012 int i;
1013
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001014 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001015
Lennert Buytenhek16820052008-06-01 11:40:29 +02001016 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1017 const struct mv643xx_eth_stats *stat;
1018 void *p;
1019
1020 stat = mv643xx_eth_stats + i;
1021
1022 if (stat->netdev_off >= 0)
1023 p = ((void *)mp->dev) + stat->netdev_off;
1024 else
1025 p = ((void *)mp) + stat->mp_off;
1026
1027 data[i] = (stat->sizeof_stat == 8) ?
1028 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001029 }
1030}
1031
Lennert Buytenheke5371492008-06-01 02:18:13 +02001032static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001033{
1034 switch (sset) {
1035 case ETH_SS_STATS:
Lennert Buytenhek16820052008-06-01 11:40:29 +02001036 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001037 default:
1038 return -EOPNOTSUPP;
1039 }
1040}
1041
Lennert Buytenheke5371492008-06-01 02:18:13 +02001042static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1043 .get_settings = mv643xx_eth_get_settings,
1044 .set_settings = mv643xx_eth_set_settings,
1045 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001046 .get_link = mv643xx_eth_get_link,
1047 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001048 .get_sset_count = mv643xx_eth_get_sset_count,
1049 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1050 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001051 .nway_reset = mv643xx_eth_nway_restart,
1052};
1053
1054
1055/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001056static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001057{
1058 unsigned int port_num = mp->port_num;
1059 unsigned int mac_h;
1060 unsigned int mac_l;
1061
1062 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1063 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1064
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001065 addr[0] = (mac_h >> 24) & 0xff;
1066 addr[1] = (mac_h >> 16) & 0xff;
1067 addr[2] = (mac_h >> 8) & 0xff;
1068 addr[3] = mac_h & 0xff;
1069 addr[4] = (mac_l >> 8) & 0xff;
1070 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001071}
1072
Lennert Buytenheke5371492008-06-01 02:18:13 +02001073static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001074{
1075 unsigned int port_num = mp->port_num;
1076 int table_index;
1077
1078 /* Clear DA filter unicast table (Ex_dFUT) */
1079 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001080 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001081
1082 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1083 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001084 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001085 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001086 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001087 }
1088}
1089
Lennert Buytenheke5371492008-06-01 02:18:13 +02001090static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001091 int table, unsigned char entry)
1092{
1093 unsigned int table_reg;
1094 unsigned int tbl_offset;
1095 unsigned int reg_offset;
1096
1097 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1098 reg_offset = entry % 4; /* Entry offset within the register */
1099
1100 /* Set "accepts frame bit" at specified table entry */
1101 table_reg = rdl(mp, table + tbl_offset);
1102 table_reg |= 0x01 << (8 * reg_offset);
1103 wrl(mp, table + tbl_offset, table_reg);
1104}
1105
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001106static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001107{
1108 unsigned int port_num = mp->port_num;
1109 unsigned int mac_h;
1110 unsigned int mac_l;
1111 int table;
1112
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001113 mac_l = (addr[4] << 8) | (addr[5]);
1114 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1115 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001116
1117 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1118 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1119
1120 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001121 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001122 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001123}
1124
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001125static void mv643xx_eth_update_mac_address(struct net_device *dev)
1126{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001127 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001128
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001129 init_mac_tables(mp);
1130 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131}
1132
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001133static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1134{
1135 int i;
1136
1137 for (i = 0; i < 6; i++)
1138 /* +2 is for the offset of the HW addr type */
1139 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1140 mv643xx_eth_update_mac_address(dev);
1141 return 0;
1142}
1143
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001144static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001145{
1146 unsigned int port_num = mp->port_num;
1147 unsigned int mac_h;
1148 unsigned int mac_l;
1149 unsigned char crc_result = 0;
1150 int table;
1151 int mac_array[48];
1152 int crc[8];
1153 int i;
1154
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001155 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1156 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001157 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001158 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001159 return;
1160 }
1161
1162 /* Calculate CRC-8 out of the given address */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001163 mac_h = (addr[0] << 8) | (addr[1]);
1164 mac_l = (addr[2] << 24) | (addr[3] << 16) |
1165 (addr[4] << 8) | (addr[5] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001166
1167 for (i = 0; i < 32; i++)
1168 mac_array[i] = (mac_l >> i) & 0x1;
1169 for (i = 32; i < 48; i++)
1170 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1171
1172 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1173 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1174 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1175 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1176 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1177
1178 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1179 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1180 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1181 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1182 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1183 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1184 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1185
1186 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1187 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1188 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1189 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1190 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1191 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1192
1193 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1194 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1195 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1196 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1197 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1198 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1199
1200 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1201 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1202 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1203 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1204 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1205 mac_array[3] ^ mac_array[2];
1206
1207 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1208 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1209 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1210 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1211 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1212 mac_array[4] ^ mac_array[3];
1213
1214 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1215 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1216 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1217 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1218 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1219 mac_array[4];
1220
1221 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1222 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1223 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1224 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1225 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1226
1227 for (i = 0; i < 8; i++)
1228 crc_result = crc_result | (crc[i] << i);
1229
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001230 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001231 set_filter_table_entry(mp, table, crc_result);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001232}
1233
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001234static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001235{
1236
1237 struct dev_mc_list *mc_list;
1238 int i;
1239 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001240 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001241 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001242
1243 /* If the device is in promiscuous mode or in all multicast mode,
1244 * we will fully populate both multicast tables with accept.
1245 * This is guaranteed to yield a match on all multicast addresses...
1246 */
1247 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1248 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1249 /* Set all entries in DA filter special multicast
1250 * table (Ex_dFSMT)
1251 * Set for ETH_Q0 for now
1252 * Bits
1253 * 0 Accept=1, Drop=0
1254 * 3-1 Queue ETH_Q0=0
1255 * 7-4 Reserved = 0;
1256 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001257 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001258
1259 /* Set all entries in DA filter other multicast
1260 * table (Ex_dFOMT)
1261 * Set for ETH_Q0 for now
1262 * Bits
1263 * 0 Accept=1, Drop=0
1264 * 3-1 Queue ETH_Q0=0
1265 * 7-4 Reserved = 0;
1266 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001267 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001268 }
1269 return;
1270 }
1271
1272 /* We will clear out multicast tables every time we get the list.
1273 * Then add the entire new list...
1274 */
1275 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1276 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001277 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001278
1279 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001280 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001281 }
1282
1283 /* Get pointer to net_device multicast list and add each one... */
1284 for (i = 0, mc_list = dev->mc_list;
1285 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1286 i++, mc_list = mc_list->next)
1287 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001288 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001289}
1290
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001291static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1292{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001293 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001294 u32 config_reg;
1295
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001296 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001297 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001298 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001299 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001300 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001301 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001302
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001303 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001304}
1305
1306
1307/* rx/tx queue initialisation ***********************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001308static void ether_init_rx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001309{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001310 volatile struct rx_desc *p_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001311 int rx_desc_num = mp->rx_ring_size;
1312 int i;
1313
1314 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001315 p_rx_desc = (struct rx_desc *)mp->rx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001316 for (i = 0; i < rx_desc_num; i++) {
1317 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001318 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001319 }
1320
1321 /* Save Rx desc pointer to driver struct. */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001322 mp->rx_curr_desc = 0;
1323 mp->rx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001324
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001325 mp->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326}
1327
1328static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1329{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001330 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001331 int curr;
1332
1333 /* Stop RX Queues */
1334 mv643xx_eth_port_disable_rx(mp);
1335
1336 /* Free preallocated skb's on RX rings */
1337 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1338 if (mp->rx_skb[curr]) {
1339 dev_kfree_skb(mp->rx_skb[curr]);
1340 mp->rx_desc_count--;
1341 }
1342 }
1343
1344 if (mp->rx_desc_count)
1345 printk(KERN_ERR
1346 "%s: Error in freeing Rx Ring. %d skb's still"
1347 " stuck in RX Ring - ignoring them\n", dev->name,
1348 mp->rx_desc_count);
1349 /* Free RX ring */
1350 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001351 iounmap(mp->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001352 else
1353 dma_free_coherent(NULL, mp->rx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001354 mp->rx_desc_area, mp->rx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001355}
1356
Lennert Buytenheke5371492008-06-01 02:18:13 +02001357static void ether_init_tx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001358{
1359 int tx_desc_num = mp->tx_ring_size;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001360 struct tx_desc *p_tx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001361 int i;
1362
1363 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001364 p_tx_desc = (struct tx_desc *)mp->tx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001365 for (i = 0; i < tx_desc_num; i++) {
1366 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001367 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001368 }
1369
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001370 mp->tx_curr_desc = 0;
1371 mp->tx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001372
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001373 mp->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001374}
1375
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001376static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1377{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001378 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001379 struct tx_desc *desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001380 u32 cmd_sts;
1381 struct sk_buff *skb;
1382 unsigned long flags;
1383 int tx_index;
1384 dma_addr_t addr;
1385 int count;
1386 int released = 0;
1387
1388 while (mp->tx_desc_count > 0) {
1389 spin_lock_irqsave(&mp->lock, flags);
1390
1391 /* tx_desc_count might have changed before acquiring the lock */
1392 if (mp->tx_desc_count <= 0) {
1393 spin_unlock_irqrestore(&mp->lock, flags);
1394 return released;
1395 }
1396
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001397 tx_index = mp->tx_used_desc;
1398 desc = &mp->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001399 cmd_sts = desc->cmd_sts;
1400
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001401 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001402 spin_unlock_irqrestore(&mp->lock, flags);
1403 return released;
1404 }
1405
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001406 mp->tx_used_desc = (tx_index + 1) % mp->tx_ring_size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001407 mp->tx_desc_count--;
1408
1409 addr = desc->buf_ptr;
1410 count = desc->byte_cnt;
1411 skb = mp->tx_skb[tx_index];
1412 if (skb)
1413 mp->tx_skb[tx_index] = NULL;
1414
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001415 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001416 printk("%s: Error in TX\n", dev->name);
1417 dev->stats.tx_errors++;
1418 }
1419
1420 spin_unlock_irqrestore(&mp->lock, flags);
1421
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001422 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001423 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1424 else
1425 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1426
1427 if (skb)
1428 dev_kfree_skb_irq(skb);
1429
1430 released = 1;
1431 }
1432
1433 return released;
1434}
1435
1436static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1437{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001438 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001439
1440 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1441 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1442 netif_wake_queue(dev);
1443}
1444
1445static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1446{
1447 mv643xx_eth_free_tx_descs(dev, 1);
1448}
1449
1450static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1451{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001452 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001453
1454 /* Stop Tx Queues */
1455 mv643xx_eth_port_disable_tx(mp);
1456
1457 /* Free outstanding skb's on TX ring */
1458 mv643xx_eth_free_all_tx_descs(dev);
1459
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001460 BUG_ON(mp->tx_used_desc != mp->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001461
1462 /* Free TX ring */
1463 if (mp->tx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001464 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001465 else
1466 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001467 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001468}
1469
1470
1471/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001472static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001473
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001474static void mv643xx_eth_update_pscr(struct net_device *dev,
1475 struct ethtool_cmd *ecmd)
1476{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001477 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001478 int port_num = mp->port_num;
1479 u32 o_pscr, n_pscr;
1480 unsigned int queues;
1481
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001482 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001483 n_pscr = o_pscr;
1484
1485 /* clear speed, duplex and rx buffer size fields */
1486 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1487 SET_GMII_SPEED_TO_1000 |
1488 SET_FULL_DUPLEX_MODE |
1489 MAX_RX_PACKET_MASK);
1490
1491 if (ecmd->duplex == DUPLEX_FULL)
1492 n_pscr |= SET_FULL_DUPLEX_MODE;
1493
1494 if (ecmd->speed == SPEED_1000)
1495 n_pscr |= SET_GMII_SPEED_TO_1000 |
1496 MAX_RX_PACKET_9700BYTE;
1497 else {
1498 if (ecmd->speed == SPEED_100)
1499 n_pscr |= SET_MII_SPEED_TO_100;
1500 n_pscr |= MAX_RX_PACKET_1522BYTE;
1501 }
1502
1503 if (n_pscr != o_pscr) {
1504 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001505 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001506 else {
1507 queues = mv643xx_eth_port_disable_tx(mp);
1508
1509 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001510 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1511 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1512 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001513 if (queues)
1514 mv643xx_eth_port_enable_tx(mp, queues);
1515 }
1516 }
1517}
1518
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001519static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1520{
1521 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001522 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001523 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001524 unsigned int port_num = mp->port_num;
1525
1526 /* Read interrupt cause registers */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001527 int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1528 if (int_cause & INT_EXT) {
1529 int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001530 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001531 wrl(mp, INT_CAUSE_EXT(port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001532 }
1533
1534 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001535 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001536 struct ethtool_cmd cmd;
1537
1538 if (mii_link_ok(&mp->mii)) {
1539 mii_ethtool_gset(&mp->mii, &cmd);
1540 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001541 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001542 if (!netif_carrier_ok(dev)) {
1543 netif_carrier_on(dev);
1544 if (mp->tx_ring_size - mp->tx_desc_count >=
1545 MAX_DESCS_PER_SKB)
1546 netif_wake_queue(dev);
1547 }
1548 } else if (netif_carrier_ok(dev)) {
1549 netif_stop_queue(dev);
1550 netif_carrier_off(dev);
1551 }
1552 }
1553
Lennert Buytenheke5371492008-06-01 02:18:13 +02001554#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001555 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001556 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001557 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001558
1559 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001560 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001561
1562 netif_rx_schedule(dev, &mp->napi);
1563 }
1564#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001565 if (int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001566 mv643xx_eth_receive_queue(dev, INT_MAX);
1567#endif
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001568 if (int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001569 mv643xx_eth_free_completed_tx_descs(dev);
1570
1571 /*
1572 * If no real interrupt occured, exit.
1573 * This can happen when using gigE interrupt coalescing mechanism.
1574 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001575 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001576 return IRQ_NONE;
1577
1578 return IRQ_HANDLED;
1579}
1580
Lennert Buytenheke5371492008-06-01 02:18:13 +02001581static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001582{
1583 unsigned int phy_reg_data;
1584
1585 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001586 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001587 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001588 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001589
1590 /* wait for PHY to come out of reset */
1591 do {
1592 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001593 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001594 } while (phy_reg_data & 0x8000);
1595}
1596
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001597static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001598{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001599 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001600 unsigned int port_num = mp->port_num;
1601 int tx_curr_desc, rx_curr_desc;
1602 u32 pscr;
1603 struct ethtool_cmd ethtool_cmd;
1604
1605 /* Assignment of Tx CTRP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001606 tx_curr_desc = mp->tx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001607 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001608 (u32)((struct tx_desc *)mp->tx_desc_dma + tx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001609
1610 /* Assignment of Rx CRDP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001611 rx_curr_desc = mp->rx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001612 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001613 (u32)((struct rx_desc *)mp->rx_desc_dma + rx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001614
1615 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001616 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001617
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001618 /*
1619 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1620 * frames to RX queue #0.
1621 */
1622 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001623
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001624 /*
1625 * Treat BPDUs as normal multicasts, and disable partition mode.
1626 */
1627 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001628
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001629 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001630
1631 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001632 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001633
1634 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1635 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02001636 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001637 DO_NOT_FORCE_LINK_FAIL |
1638 SERIAL_PORT_CONTROL_RESERVED;
1639
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001640 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001641
1642 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001643 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001644
1645 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001646 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001647
1648 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001649 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001650
1651 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001652 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001653
1654 /* save phy settings across reset */
Lennert Buytenheke5371492008-06-01 02:18:13 +02001655 mv643xx_eth_get_settings(dev, &ethtool_cmd);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001656 phy_reset(mp);
Lennert Buytenheke5371492008-06-01 02:18:13 +02001657 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001658}
1659
Lennert Buytenheke5371492008-06-01 02:18:13 +02001660#ifdef MV643XX_ETH_COAL
Lennert Buytenheke5371492008-06-01 02:18:13 +02001661static unsigned int set_rx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001662 unsigned int delay)
1663{
1664 unsigned int port_num = mp->port_num;
1665 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1666
1667 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001668 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001669 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001670 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001671 & 0xffc000ff));
1672
1673 return coal;
1674}
1675#endif
1676
Lennert Buytenheke5371492008-06-01 02:18:13 +02001677static unsigned int set_tx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001678 unsigned int delay)
1679{
1680 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1681
1682 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001683 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001684
1685 return coal;
1686}
1687
Lennert Buytenheke5371492008-06-01 02:18:13 +02001688static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001689{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001690 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001691
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001692 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001693}
1694
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001695static int mv643xx_eth_open(struct net_device *dev)
1696{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001697 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001698 unsigned int port_num = mp->port_num;
1699 unsigned int size;
1700 int err;
1701
1702 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001703 wrl(mp, INT_CAUSE(port_num), 0);
1704 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001705 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001706 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001707
1708 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1709 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1710 if (err) {
1711 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1712 return -EAGAIN;
1713 }
1714
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001715 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001716
1717 memset(&mp->timeout, 0, sizeof(struct timer_list));
1718 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
1719 mp->timeout.data = (unsigned long)dev;
1720
1721 /* Allocate RX and TX skb rings */
1722 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
1723 GFP_KERNEL);
1724 if (!mp->rx_skb) {
1725 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
1726 err = -ENOMEM;
1727 goto out_free_irq;
1728 }
1729 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1730 GFP_KERNEL);
1731 if (!mp->tx_skb) {
1732 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
1733 err = -ENOMEM;
1734 goto out_free_rx_skb;
1735 }
1736
1737 /* Allocate TX ring */
1738 mp->tx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001739 size = mp->tx_ring_size * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001740 mp->tx_desc_area_size = size;
1741
1742 if (mp->tx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001743 mp->tx_desc_area = ioremap(mp->tx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001744 mp->tx_sram_size);
1745 mp->tx_desc_dma = mp->tx_sram_addr;
1746 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001747 mp->tx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001748 &mp->tx_desc_dma,
1749 GFP_KERNEL);
1750
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001751 if (!mp->tx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001752 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1753 dev->name, size);
1754 err = -ENOMEM;
1755 goto out_free_tx_skb;
1756 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001757 BUG_ON((u32) mp->tx_desc_area & 0xf); /* check 16-byte alignment */
1758 memset((void *)mp->tx_desc_area, 0, mp->tx_desc_area_size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001759
1760 ether_init_tx_desc_ring(mp);
1761
1762 /* Allocate RX ring */
1763 mp->rx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001764 size = mp->rx_ring_size * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001765 mp->rx_desc_area_size = size;
1766
1767 if (mp->rx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001768 mp->rx_desc_area = ioremap(mp->rx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001769 mp->rx_sram_size);
1770 mp->rx_desc_dma = mp->rx_sram_addr;
1771 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001772 mp->rx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001773 &mp->rx_desc_dma,
1774 GFP_KERNEL);
1775
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001776 if (!mp->rx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001777 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
1778 dev->name, size);
1779 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
1780 dev->name);
1781 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001782 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001783 else
1784 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001785 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001786 err = -ENOMEM;
1787 goto out_free_tx_skb;
1788 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001789 memset((void *)mp->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001790
1791 ether_init_rx_desc_ring(mp);
1792
1793 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
1794
Lennert Buytenheke5371492008-06-01 02:18:13 +02001795#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001796 napi_enable(&mp->napi);
1797#endif
1798
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001799 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001800
1801 /* Interrupt Coalescing */
1802
Lennert Buytenheke5371492008-06-01 02:18:13 +02001803#ifdef MV643XX_ETH_COAL
1804 mp->rx_int_coal = set_rx_coal(mp, MV643XX_ETH_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001805#endif
1806
Lennert Buytenheke5371492008-06-01 02:18:13 +02001807 mp->tx_int_coal = set_tx_coal(mp, MV643XX_ETH_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001808
1809 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001810 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001811
1812 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001813 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001814
1815 return 0;
1816
1817out_free_tx_skb:
1818 kfree(mp->tx_skb);
1819out_free_rx_skb:
1820 kfree(mp->rx_skb);
1821out_free_irq:
1822 free_irq(dev->irq, dev);
1823
1824 return err;
1825}
1826
Lennert Buytenheke5371492008-06-01 02:18:13 +02001827static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001828{
1829 unsigned int port_num = mp->port_num;
1830 unsigned int reg_data;
1831
1832 mv643xx_eth_port_disable_tx(mp);
1833 mv643xx_eth_port_disable_rx(mp);
1834
1835 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001836 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001837
1838 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001839 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001840 reg_data &= ~(SERIAL_PORT_ENABLE |
1841 DO_NOT_FORCE_LINK_FAIL |
1842 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001843 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001844}
1845
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001846static int mv643xx_eth_stop(struct net_device *dev)
1847{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001848 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001849 unsigned int port_num = mp->port_num;
1850
1851 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001852 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001853 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001854 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001855
Lennert Buytenheke5371492008-06-01 02:18:13 +02001856#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001857 napi_disable(&mp->napi);
1858#endif
1859 netif_carrier_off(dev);
1860 netif_stop_queue(dev);
1861
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001862 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001863
1864 mv643xx_eth_free_tx_rings(dev);
1865 mv643xx_eth_free_rx_rings(dev);
1866
1867 free_irq(dev->irq, dev);
1868
1869 return 0;
1870}
1871
1872static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1873{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001874 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001875
1876 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1877}
1878
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001879static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1880{
1881 if ((new_mtu > 9500) || (new_mtu < 64))
1882 return -EINVAL;
1883
1884 dev->mtu = new_mtu;
1885 if (!netif_running(dev))
1886 return 0;
1887
1888 /*
1889 * Stop and then re-open the interface. This will allocate RX
1890 * skbs of the new MTU.
1891 * There is a possible danger that the open will not succeed,
1892 * due to memory being full, which might fail the open function.
1893 */
1894 mv643xx_eth_stop(dev);
1895 if (mv643xx_eth_open(dev)) {
1896 printk(KERN_ERR "%s: Fatal error on opening device\n",
1897 dev->name);
1898 }
1899
1900 return 0;
1901}
1902
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001903static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1904{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001905 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001906 tx_timeout_task);
1907 struct net_device *dev = mp->dev;
1908
1909 if (!netif_running(dev))
1910 return;
1911
1912 netif_stop_queue(dev);
1913
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001914 port_reset(mp);
1915 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001916
1917 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1918 netif_wake_queue(dev);
1919}
1920
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001921static void mv643xx_eth_tx_timeout(struct net_device *dev)
1922{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001923 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001924
1925 printk(KERN_INFO "%s: TX timeout ", dev->name);
1926
1927 /* Do the reset outside of interrupt context */
1928 schedule_work(&mp->tx_timeout_task);
1929}
1930
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001931#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001932static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001933{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001934 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001935 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001936
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001937 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001938 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001939 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001940
Al Viro9da3b1a2006-10-08 15:00:44 +01001941 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001942
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001943 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001944}
1945#endif
1946
Lennert Buytenheke5371492008-06-01 02:18:13 +02001947static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001948{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001949 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001950 int val;
1951
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001952 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001953 return val;
1954}
1955
Lennert Buytenheke5371492008-06-01 02:18:13 +02001956static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001957{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001958 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001959 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001960}
1961
1962
1963/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001964static void
1965mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
1966 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001967{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001968 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001969 u32 win_enable;
1970 u32 win_protect;
1971 int i;
1972
1973 for (i = 0; i < 6; i++) {
1974 writel(0, base + WINDOW_BASE(i));
1975 writel(0, base + WINDOW_SIZE(i));
1976 if (i < 4)
1977 writel(0, base + WINDOW_REMAP_HIGH(i));
1978 }
1979
1980 win_enable = 0x3f;
1981 win_protect = 0;
1982
1983 for (i = 0; i < dram->num_cs; i++) {
1984 struct mbus_dram_window *cs = dram->cs + i;
1985
1986 writel((cs->base & 0xffff0000) |
1987 (cs->mbus_attr << 8) |
1988 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1989 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1990
1991 win_enable &= ~(1 << i);
1992 win_protect |= 3 << (2 * i);
1993 }
1994
1995 writel(win_enable, base + WINDOW_BAR_ENABLE);
1996 msp->win_protect = win_protect;
1997}
1998
1999static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2000{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002001 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002002 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002003 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002004 struct resource *res;
2005 int ret;
2006
Lennert Buytenheke5371492008-06-01 02:18:13 +02002007 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002008 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2009
2010 ret = -EINVAL;
2011 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2012 if (res == NULL)
2013 goto out;
2014
2015 ret = -ENOMEM;
2016 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2017 if (msp == NULL)
2018 goto out;
2019 memset(msp, 0, sizeof(*msp));
2020
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002021 msp->base = ioremap(res->start, res->end - res->start + 1);
2022 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002023 goto out_free;
2024
2025 spin_lock_init(&msp->phy_lock);
2026 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2027
2028 platform_set_drvdata(pdev, msp);
2029
2030 /*
2031 * (Re-)program MBUS remapping windows if we are asked to.
2032 */
2033 if (pd != NULL && pd->dram != NULL)
2034 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2035
2036 return 0;
2037
2038out_free:
2039 kfree(msp);
2040out:
2041 return ret;
2042}
2043
2044static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2045{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002046 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002047
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002048 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002049 kfree(msp);
2050
2051 return 0;
2052}
2053
2054static struct platform_driver mv643xx_eth_shared_driver = {
2055 .probe = mv643xx_eth_shared_probe,
2056 .remove = mv643xx_eth_shared_remove,
2057 .driver = {
2058 .name = MV643XX_ETH_SHARED_NAME,
2059 .owner = THIS_MODULE,
2060 },
2061};
2062
Lennert Buytenheke5371492008-06-01 02:18:13 +02002063static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002064{
2065 u32 reg_data;
2066 int addr_shift = 5 * mp->port_num;
2067
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002068 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002069 reg_data &= ~(0x1f << addr_shift);
2070 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002071 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002072}
2073
Lennert Buytenheke5371492008-06-01 02:18:13 +02002074static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002075{
2076 unsigned int reg_data;
2077
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002078 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002079
2080 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2081}
2082
Lennert Buytenheke5371492008-06-01 02:18:13 +02002083static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002084{
2085 unsigned int phy_reg_data0;
2086 int auto_neg;
2087
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002088 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002089 auto_neg = phy_reg_data0 & 0x1000;
2090 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002091 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002092
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002093 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002094 if ((phy_reg_data0 & 0x1000) == auto_neg)
2095 return -ENODEV; /* change didn't take */
2096
2097 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002098 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002099 return 0;
2100}
2101
James Chapmand0412d92006-01-27 01:15:30 -07002102static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2103 int speed, int duplex,
2104 struct ethtool_cmd *cmd)
2105{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002106 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002107
2108 memset(cmd, 0, sizeof(*cmd));
2109
2110 cmd->port = PORT_MII;
2111 cmd->transceiver = XCVR_INTERNAL;
2112 cmd->phy_address = phy_address;
2113
2114 if (speed == 0) {
2115 cmd->autoneg = AUTONEG_ENABLE;
2116 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2117 cmd->speed = SPEED_100;
2118 cmd->advertising = ADVERTISED_10baseT_Half |
2119 ADVERTISED_10baseT_Full |
2120 ADVERTISED_100baseT_Half |
2121 ADVERTISED_100baseT_Full;
2122 if (mp->mii.supports_gmii)
2123 cmd->advertising |= ADVERTISED_1000baseT_Full;
2124 } else {
2125 cmd->autoneg = AUTONEG_DISABLE;
2126 cmd->speed = speed;
2127 cmd->duplex = duplex;
2128 }
2129}
2130
Russell King3ae5eae2005-11-09 22:32:44 +00002131static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002134 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002135 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct net_device *dev;
2137 u8 *p;
2138 struct resource *res;
2139 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002140 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002141 int duplex = DUPLEX_HALF;
2142 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002143 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002145 pd = pdev->dev.platform_data;
2146 if (pd == NULL) {
2147 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2148 return -ENODEV;
2149 }
2150
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002151 if (pd->shared == NULL) {
2152 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2153 return -ENODEV;
2154 }
2155
Lennert Buytenheke5371492008-06-01 02:18:13 +02002156 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 if (!dev)
2158 return -ENOMEM;
2159
Russell King3ae5eae2005-11-09 22:32:44 +00002160 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002163 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002164#ifdef MV643XX_ETH_NAPI
2165 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002166#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2169 BUG_ON(!res);
2170 dev->irq = res->start;
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 dev->open = mv643xx_eth_open;
2173 dev->stop = mv643xx_eth_stop;
2174 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 dev->set_mac_address = mv643xx_eth_set_mac_address;
2176 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2177
2178 /* No need to Tx Timeout */
2179 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002181#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002182 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002183#endif
2184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 dev->base_addr = 0;
2187 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002188 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002189 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Lennert Buytenheke5371492008-06-01 02:18:13 +02002191#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192#ifdef MAX_SKB_FRAGS
2193 /*
2194 * Zero copy can only work if we use Discovery II memory. Else, we will
2195 * have to map the buffers to ISA memory which is only 16 MB
2196 */
Wolfram Joost63890572006-01-16 16:57:41 -07002197 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198#endif
2199#endif
2200
2201 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002202 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
2204 spin_lock_init(&mp->lock);
2205
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002206 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002207 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002208
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002209 if (mp->shared->win_protect)
2210 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2211
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002212 mp->shared_smi = mp->shared;
2213 if (pd->shared_smi != NULL)
2214 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2215
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002217 uc_addr_get(mp, dev->dev_addr);
2218 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2219 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002221 if (is_valid_ether_addr(pd->mac_addr))
2222 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002224 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002225 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002227 if (pd->rx_queue_size)
2228 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002230 if (pd->tx_queue_size)
2231 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002233 if (pd->tx_sram_size) {
2234 mp->tx_sram_size = pd->tx_sram_size;
2235 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
2237
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002238 if (pd->rx_sram_size) {
2239 mp->rx_sram_size = pd->rx_sram_size;
2240 mp->rx_sram_addr = pd->rx_sram_addr;
2241 }
2242
2243 duplex = pd->duplex;
2244 speed = pd->speed;
2245
James Chapmanc28a4f82006-01-27 01:13:15 -07002246 /* Hook up MII support for ethtool */
2247 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002248 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2249 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002250 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002251 mp->mii.phy_id_mask = 0x3f;
2252 mp->mii.reg_num_mask = 0x1f;
2253
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002254 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002256 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002257 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002258 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002261 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002262 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002263 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2264 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002265 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002266
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002267 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 err = register_netdev(dev);
2269 if (err)
2270 goto out;
2271
2272 p = dev->dev_addr;
2273 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002274 "%s: port %d with MAC address %s\n",
2275 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 if (dev->features & NETIF_F_SG)
2278 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2279
2280 if (dev->features & NETIF_F_IP_CSUM)
2281 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2282 dev->name);
2283
Lennert Buytenheke5371492008-06-01 02:18:13 +02002284#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2286#endif
2287
Lennert Buytenheke5371492008-06-01 02:18:13 +02002288#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2290 dev->name);
2291#endif
2292
Lennert Buytenheke5371492008-06-01 02:18:13 +02002293#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2295#endif
2296
Nicolas DETb1529872005-10-28 17:46:30 -07002297 if (mp->tx_sram_size > 0)
2298 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 return 0;
2301
2302out:
2303 free_netdev(dev);
2304
2305 return err;
2306}
2307
Russell King3ae5eae2005-11-09 22:32:44 +00002308static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Russell King3ae5eae2005-11-09 22:32:44 +00002310 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
2312 unregister_netdev(dev);
2313 flush_scheduled_work();
2314
2315 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002316 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return 0;
2318}
2319
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002320static void mv643xx_eth_shutdown(struct platform_device *pdev)
2321{
2322 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002323 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002324 unsigned int port_num = mp->port_num;
2325
2326 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002327 wrl(mp, INT_MASK(port_num), 0);
2328 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002329
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002330 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002331}
2332
Russell King3ae5eae2005-11-09 22:32:44 +00002333static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 .probe = mv643xx_eth_probe,
2335 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002336 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002337 .driver = {
2338 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002339 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002340 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341};
2342
Lennert Buytenheke5371492008-06-01 02:18:13 +02002343static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 int rc;
2346
Russell King3ae5eae2005-11-09 22:32:44 +00002347 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002349 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002351 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 }
2353 return rc;
2354}
2355
Lennert Buytenheke5371492008-06-01 02:18:13 +02002356static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357{
Russell King3ae5eae2005-11-09 22:32:44 +00002358 platform_driver_unregister(&mv643xx_eth_driver);
2359 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360}
2361
Lennert Buytenheke5371492008-06-01 02:18:13 +02002362module_init(mv643xx_eth_init_module);
2363module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365MODULE_LICENSE("GPL");
2366MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2367 " and Dale Farnsworth");
2368MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002369MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2370MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);