blob: ddd3e1148662dc356d1baa3037e0f56686c1cd3c [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;
328 struct net_device_stats stats;
Lennert Buytenheke5371492008-06-01 02:18:13 +0200329 struct mib_counters mib_counters;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200330 spinlock_t lock;
331 /* Size of Tx Ring per queue */
332 int tx_ring_size;
333 /* Number of tx descriptors in use */
334 int tx_desc_count;
335 /* Size of Rx Ring per queue */
336 int rx_ring_size;
337 /* Number of rx descriptors in use */
338 int rx_desc_count;
339
340 /*
341 * Used in case RX Ring is empty, which can be caused when
342 * system does not have resources (skb's)
343 */
344 struct timer_list timeout;
345
346 u32 rx_int_coal;
347 u32 tx_int_coal;
348 struct mii_if_info mii;
349};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200351
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200352/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200353static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200355 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Lennert Buytenheke5371492008-06-01 02:18:13 +0200358static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200360 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200363
364/* rxq/txq helper functions *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200365static void mv643xx_eth_port_enable_rx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200366 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200368 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200369}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Lennert Buytenheke5371492008-06-01 02:18:13 +0200371static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200372{
373 unsigned int port_num = mp->port_num;
374 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700375
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200376 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200377 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200378 if (queues) {
379 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200380 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200381
382 /* Wait for all Rx activity to terminate. */
383 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200384 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200385 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 }
387
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200388 return queues;
389}
390
Lennert Buytenheke5371492008-06-01 02:18:13 +0200391static void mv643xx_eth_port_enable_tx(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200392 unsigned int queues)
393{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200394 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200395}
396
Lennert Buytenheke5371492008-06-01 02:18:13 +0200397static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200398{
399 unsigned int port_num = mp->port_num;
400 u32 queues;
401
402 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200403 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200404 if (queues) {
405 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200406 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200407
408 /* Wait for all Tx activity to terminate. */
409 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200410 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200411 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200412
413 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200414 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200415 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200416 }
417
418 return queues;
419}
420
421
422/* rx ***********************************************************************/
423static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
424
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200425static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200426{
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200427 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200428 unsigned long flags;
429
430 spin_lock_irqsave(&mp->lock, flags);
431
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700432 while (mp->rx_desc_count < mp->rx_ring_size) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200433 struct sk_buff *skb;
434 int unaligned;
435 int rx;
436
Ralf Baechle908b6372007-02-26 19:52:06 +0000437 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200438 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 break;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200440
Ralf Baechle908b6372007-02-26 19:52:06 +0000441 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700442 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000443 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200444
445 mp->rx_desc_count++;
446 rx = mp->rx_used_desc;
447 mp->rx_used_desc = (rx + 1) % mp->rx_ring_size;
448
449 mp->rx_desc_area[rx].buf_ptr = dma_map_single(NULL,
450 skb->data,
451 ETH_RX_SKB_SIZE,
452 DMA_FROM_DEVICE);
453 mp->rx_desc_area[rx].buf_size = ETH_RX_SKB_SIZE;
454 mp->rx_skb[rx] = skb;
455 wmb();
456 mp->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
457 RX_ENABLE_INTERRUPT;
458 wmb();
459
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700460 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200462
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700463 if (mp->rx_desc_count == 0) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200464 mp->timeout.expires = jiffies + (HZ / 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200467
468 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700471static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700473 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200478 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700479 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 unsigned int received_packets = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Lennert Buytenhek96587662008-06-01 10:31:56 +0200482 while (budget-- > 0) {
483 struct sk_buff *skb;
484 volatile struct rx_desc *rx_desc;
485 unsigned int cmd_sts;
486 unsigned long flags;
487
488 spin_lock_irqsave(&mp->lock, flags);
489
490 rx_desc = &mp->rx_desc_area[mp->rx_curr_desc];
491
492 cmd_sts = rx_desc->cmd_sts;
493 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
494 spin_unlock_irqrestore(&mp->lock, flags);
495 break;
496 }
497 rmb();
498
499 skb = mp->rx_skb[mp->rx_curr_desc];
500 mp->rx_skb[mp->rx_curr_desc] = NULL;
501
502 mp->rx_curr_desc = (mp->rx_curr_desc + 1) % mp->rx_ring_size;
503
504 spin_unlock_irqrestore(&mp->lock, flags);
505
506 dma_unmap_single(NULL, rx_desc->buf_ptr + ETH_HW_IP_ALIGN,
507 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700508 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700510
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700511 /*
512 * Update statistics.
513 * Note byte count includes 4 byte CRC count
514 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 stats->rx_packets++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200516 stats->rx_bytes += rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /*
519 * In case received a packet without first / last bits on OR
520 * the error summary bit is on, the packets needs to be dropeed.
521 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200522 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200523 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200524 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 stats->rx_dropped++;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200526 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200527 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (net_ratelimit())
529 printk(KERN_ERR
530 "%s: Received packet spread "
531 "on multiple descriptors\n",
532 dev->name);
533 }
Lennert Buytenhek96587662008-06-01 10:31:56 +0200534 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 stats->rx_errors++;
536
537 dev_kfree_skb_irq(skb);
538 } else {
539 /*
540 * The -4 is for the CRC in the trailer of the
541 * received packet
542 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200543 skb_put(skb, rx_desc->byte_cnt - ETH_HW_IP_ALIGN - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Lennert Buytenhek96587662008-06-01 10:31:56 +0200545 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 skb->ip_summed = CHECKSUM_UNNECESSARY;
547 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200548 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550 skb->protocol = eth_type_trans(skb, dev);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200551#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 netif_receive_skb(skb);
553#else
554 netif_rx(skb);
555#endif
556 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700557 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700559 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 return received_packets;
562}
563
Lennert Buytenheke5371492008-06-01 02:18:13 +0200564#ifdef MV643XX_ETH_NAPI
Lennert Buytenheke5371492008-06-01 02:18:13 +0200565static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200567 struct mv643xx_eth_private *mp = container_of(napi, struct mv643xx_eth_private, napi);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700568 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700570 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Lennert Buytenheke5371492008-06-01 02:18:13 +0200572#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700574 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577#endif
578
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700579 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200580 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200581 != (u32) mp->rx_used_desc)
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700582 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700584 if (work_done < budget) {
585 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200586 wrl(mp, INT_CAUSE(port_num), 0);
587 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200588 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
590
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700591 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592}
593#endif
594
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200595
596/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700597static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
598{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700599 unsigned int frag;
600 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700601
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700602 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
603 fragp = &skb_shinfo(skb)->frags[frag];
604 if (fragp->size <= 8 && fragp->page_offset & 0x7)
605 return 1;
606 }
607 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700608}
609
Lennert Buytenheke5371492008-06-01 02:18:13 +0200610static int alloc_tx_desc_index(struct mv643xx_eth_private *mp)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700611{
612 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700613
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700614 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700615
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200616 tx_desc_curr = mp->tx_curr_desc;
617 mp->tx_curr_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700618
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200619 BUG_ON(mp->tx_curr_desc == mp->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700620
621 return tx_desc_curr;
622}
623
Lennert Buytenheke5371492008-06-01 02:18:13 +0200624static void tx_fill_frag_descs(struct mv643xx_eth_private *mp,
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700625 struct sk_buff *skb)
626{
627 int frag;
628 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200629 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700630
631 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
632 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
633
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200634 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200635 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700636
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200637 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700638 /* Last Frag enables interrupt and frees the skb */
639 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200640 desc->cmd_sts |= ZERO_PADDING |
641 TX_LAST_DESC |
642 TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700643 mp->tx_skb[tx_index] = skb;
644 } else
Al Viro05980772006-05-30 23:59:09 -0400645 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700646
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200647 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700648 desc->l4i_chk = 0;
649 desc->byte_cnt = this_frag->size;
650 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
651 this_frag->page_offset,
652 this_frag->size,
653 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700654 }
655}
656
Byron Bradley324ff2c2008-02-04 23:47:15 -0800657static inline __be16 sum16_as_be(__sum16 sum)
658{
659 return (__force __be16)sum;
660}
661
Lennert Buytenheke5371492008-06-01 02:18:13 +0200662static void tx_submit_descs_for_skb(struct mv643xx_eth_private *mp,
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700663 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700664{
665 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200666 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700667 u32 cmd_sts;
668 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700669 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700670
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200671 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700672
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200673 tx_index = alloc_tx_desc_index(mp);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +0200674 desc = &mp->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700675
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700676 if (nr_frags) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200677 tx_fill_frag_descs(mp, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700678
679 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400680 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700681 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200682 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700683 length = skb->len;
684 mp->tx_skb[tx_index] = skb;
685 }
686
687 desc->byte_cnt = length;
688 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700689
Patrick McHardy84fa7932006-08-29 16:44:56 -0700690 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800691 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700692
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200693 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
694 GEN_IP_V4_CHECKSUM |
695 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700696
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700697 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700698 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200699 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800700 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700701 break;
702 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800703 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700704 break;
705 default:
706 BUG();
707 }
708 } else {
709 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200710 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700711 desc->l4i_chk = 0;
712 }
713
714 /* ensure all other descriptors are written before first cmd_sts */
715 wmb();
716 desc->cmd_sts = cmd_sts;
717
718 /* ensure all descriptors are written before poking hardware */
719 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200720 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700721
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700722 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700723}
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
726{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200727 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700728 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700731 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700732
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700733 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
734 stats->tx_dropped++;
735 printk(KERN_DEBUG "%s: failed to linearize tiny "
736 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700737 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_lock_irqsave(&mp->lock, flags);
741
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700742 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
743 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
744 netif_stop_queue(dev);
745 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700746 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700747 }
748
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200749 tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700750 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 stats->tx_packets++;
752 dev->trans_start = jiffies;
753
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700754 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
755 netif_stop_queue(dev);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 spin_unlock_irqrestore(&mp->lock, flags);
758
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700759 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200762
763/* mii management interface *************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200764static int phy_addr_get(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200765
Lennert Buytenheke5371492008-06-01 02:18:13 +0200766static void read_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200767 unsigned int phy_reg, unsigned int *value)
768{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200769 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
770 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200771 unsigned long flags;
772 int i;
773
774 /* the SMI register is a shared resource */
775 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
776
777 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200778 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200779 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200780 printk("%s: PHY busy timeout\n", mp->dev->name);
781 goto out;
782 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200783 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200784 }
785
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200786 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200787
788 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200789 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200790 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200791 printk("%s: PHY read timeout\n", mp->dev->name);
792 goto out;
793 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200794 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200795 }
796
797 *value = readl(smi_reg) & 0xffff;
798out:
799 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
800}
801
Lennert Buytenheke5371492008-06-01 02:18:13 +0200802static void write_smi_reg(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200803 unsigned int phy_reg, unsigned int value)
804{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200805 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
806 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200807 unsigned long flags;
808 int i;
809
810 /* the SMI register is a shared resource */
811 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
812
813 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200814 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200815 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200816 printk("%s: PHY busy timeout\n", mp->dev->name);
817 goto out;
818 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200819 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200820 }
821
822 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200823 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200824out:
825 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
826}
827
828
829/* mib counters *************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200830static void clear_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200831{
832 unsigned int port_num = mp->port_num;
833 int i;
834
835 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200836 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200837 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200838}
839
Lennert Buytenheke5371492008-06-01 02:18:13 +0200840static inline u32 read_mib(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200841{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200842 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200843}
844
Lennert Buytenheke5371492008-06-01 02:18:13 +0200845static void update_mib_counters(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200846{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200847 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200848
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +0200849 p->good_octets_received += read_mib(mp, 0x00);
850 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
851 p->bad_octets_received += read_mib(mp, 0x08);
852 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
853 p->good_frames_received += read_mib(mp, 0x10);
854 p->bad_frames_received += read_mib(mp, 0x14);
855 p->broadcast_frames_received += read_mib(mp, 0x18);
856 p->multicast_frames_received += read_mib(mp, 0x1c);
857 p->frames_64_octets += read_mib(mp, 0x20);
858 p->frames_65_to_127_octets += read_mib(mp, 0x24);
859 p->frames_128_to_255_octets += read_mib(mp, 0x28);
860 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
861 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
862 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
863 p->good_octets_sent += read_mib(mp, 0x38);
864 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
865 p->good_frames_sent += read_mib(mp, 0x40);
866 p->excessive_collision += read_mib(mp, 0x44);
867 p->multicast_frames_sent += read_mib(mp, 0x48);
868 p->broadcast_frames_sent += read_mib(mp, 0x4c);
869 p->unrec_mac_control_received += read_mib(mp, 0x50);
870 p->fc_sent += read_mib(mp, 0x54);
871 p->good_fc_received += read_mib(mp, 0x58);
872 p->bad_fc_received += read_mib(mp, 0x5c);
873 p->undersize_received += read_mib(mp, 0x60);
874 p->fragments_received += read_mib(mp, 0x64);
875 p->oversize_received += read_mib(mp, 0x68);
876 p->jabber_received += read_mib(mp, 0x6c);
877 p->mac_receive_error += read_mib(mp, 0x70);
878 p->bad_crc_event += read_mib(mp, 0x74);
879 p->collision += read_mib(mp, 0x78);
880 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200881}
882
883
884/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200885struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200886 char stat_string[ETH_GSTRING_LEN];
887 int sizeof_stat;
888 int stat_offset;
889};
890
Lennert Buytenheke5371492008-06-01 02:18:13 +0200891#define MV643XX_ETH_STAT(m) FIELD_SIZEOF(struct mv643xx_eth_private, m), \
892 offsetof(struct mv643xx_eth_private, m)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200893
Lennert Buytenheke5371492008-06-01 02:18:13 +0200894static const struct mv643xx_eth_stats mv643xx_eth_gstrings_stats[] = {
895 { "rx_packets", MV643XX_ETH_STAT(stats.rx_packets) },
896 { "tx_packets", MV643XX_ETH_STAT(stats.tx_packets) },
897 { "rx_bytes", MV643XX_ETH_STAT(stats.rx_bytes) },
898 { "tx_bytes", MV643XX_ETH_STAT(stats.tx_bytes) },
899 { "rx_errors", MV643XX_ETH_STAT(stats.rx_errors) },
900 { "tx_errors", MV643XX_ETH_STAT(stats.tx_errors) },
901 { "rx_dropped", MV643XX_ETH_STAT(stats.rx_dropped) },
902 { "tx_dropped", MV643XX_ETH_STAT(stats.tx_dropped) },
903 { "good_octets_received", MV643XX_ETH_STAT(mib_counters.good_octets_received) },
904 { "bad_octets_received", MV643XX_ETH_STAT(mib_counters.bad_octets_received) },
905 { "internal_mac_transmit_err", MV643XX_ETH_STAT(mib_counters.internal_mac_transmit_err) },
906 { "good_frames_received", MV643XX_ETH_STAT(mib_counters.good_frames_received) },
907 { "bad_frames_received", MV643XX_ETH_STAT(mib_counters.bad_frames_received) },
908 { "broadcast_frames_received", MV643XX_ETH_STAT(mib_counters.broadcast_frames_received) },
909 { "multicast_frames_received", MV643XX_ETH_STAT(mib_counters.multicast_frames_received) },
910 { "frames_64_octets", MV643XX_ETH_STAT(mib_counters.frames_64_octets) },
911 { "frames_65_to_127_octets", MV643XX_ETH_STAT(mib_counters.frames_65_to_127_octets) },
912 { "frames_128_to_255_octets", MV643XX_ETH_STAT(mib_counters.frames_128_to_255_octets) },
913 { "frames_256_to_511_octets", MV643XX_ETH_STAT(mib_counters.frames_256_to_511_octets) },
914 { "frames_512_to_1023_octets", MV643XX_ETH_STAT(mib_counters.frames_512_to_1023_octets) },
915 { "frames_1024_to_max_octets", MV643XX_ETH_STAT(mib_counters.frames_1024_to_max_octets) },
916 { "good_octets_sent", MV643XX_ETH_STAT(mib_counters.good_octets_sent) },
917 { "good_frames_sent", MV643XX_ETH_STAT(mib_counters.good_frames_sent) },
918 { "excessive_collision", MV643XX_ETH_STAT(mib_counters.excessive_collision) },
919 { "multicast_frames_sent", MV643XX_ETH_STAT(mib_counters.multicast_frames_sent) },
920 { "broadcast_frames_sent", MV643XX_ETH_STAT(mib_counters.broadcast_frames_sent) },
921 { "unrec_mac_control_received", MV643XX_ETH_STAT(mib_counters.unrec_mac_control_received) },
922 { "fc_sent", MV643XX_ETH_STAT(mib_counters.fc_sent) },
923 { "good_fc_received", MV643XX_ETH_STAT(mib_counters.good_fc_received) },
924 { "bad_fc_received", MV643XX_ETH_STAT(mib_counters.bad_fc_received) },
925 { "undersize_received", MV643XX_ETH_STAT(mib_counters.undersize_received) },
926 { "fragments_received", MV643XX_ETH_STAT(mib_counters.fragments_received) },
927 { "oversize_received", MV643XX_ETH_STAT(mib_counters.oversize_received) },
928 { "jabber_received", MV643XX_ETH_STAT(mib_counters.jabber_received) },
929 { "mac_receive_error", MV643XX_ETH_STAT(mib_counters.mac_receive_error) },
930 { "bad_crc_event", MV643XX_ETH_STAT(mib_counters.bad_crc_event) },
931 { "collision", MV643XX_ETH_STAT(mib_counters.collision) },
932 { "late_collision", MV643XX_ETH_STAT(mib_counters.late_collision) },
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200933};
934
Lennert Buytenheke5371492008-06-01 02:18:13 +0200935#define MV643XX_ETH_STATS_LEN ARRAY_SIZE(mv643xx_eth_gstrings_stats)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200936
Lennert Buytenheke5371492008-06-01 02:18:13 +0200937static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200938{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200939 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200940 int err;
941
942 spin_lock_irq(&mp->lock);
943 err = mii_ethtool_gset(&mp->mii, cmd);
944 spin_unlock_irq(&mp->lock);
945
946 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
947 cmd->supported &= ~SUPPORTED_1000baseT_Half;
948 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
949
950 return err;
951}
952
Lennert Buytenheke5371492008-06-01 02:18:13 +0200953static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200954{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200955 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200956 int err;
957
958 spin_lock_irq(&mp->lock);
959 err = mii_ethtool_sset(&mp->mii, cmd);
960 spin_unlock_irq(&mp->lock);
961
962 return err;
963}
964
Lennert Buytenheke5371492008-06-01 02:18:13 +0200965static void mv643xx_eth_get_drvinfo(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200966 struct ethtool_drvinfo *drvinfo)
967{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200968 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
969 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200970 strncpy(drvinfo->fw_version, "N/A", 32);
971 strncpy(drvinfo->bus_info, "mv643xx", 32);
Lennert Buytenheke5371492008-06-01 02:18:13 +0200972 drvinfo->n_stats = MV643XX_ETH_STATS_LEN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200973}
974
975static int mv643xx_eth_nway_restart(struct net_device *dev)
976{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200977 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200978
979 return mii_nway_restart(&mp->mii);
980}
981
982static u32 mv643xx_eth_get_link(struct net_device *dev)
983{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200984 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200985
986 return mii_link_ok(&mp->mii);
987}
988
Lennert Buytenheke5371492008-06-01 02:18:13 +0200989static void mv643xx_eth_get_strings(struct net_device *netdev, uint32_t stringset,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200990 uint8_t *data)
991{
992 int i;
993
994 switch(stringset) {
995 case ETH_SS_STATS:
Lennert Buytenheke5371492008-06-01 02:18:13 +0200996 for (i=0; i < MV643XX_ETH_STATS_LEN; i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200997 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenheke5371492008-06-01 02:18:13 +0200998 mv643xx_eth_gstrings_stats[i].stat_string,
999 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001000 }
1001 break;
1002 }
1003}
1004
Lennert Buytenheke5371492008-06-01 02:18:13 +02001005static void mv643xx_eth_get_ethtool_stats(struct net_device *netdev,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001006 struct ethtool_stats *stats, uint64_t *data)
1007{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001008 struct mv643xx_eth_private *mp = netdev->priv;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001009 int i;
1010
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001011 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001012
Lennert Buytenheke5371492008-06-01 02:18:13 +02001013 for (i = 0; i < MV643XX_ETH_STATS_LEN; i++) {
1014 char *p = (char *)mp+mv643xx_eth_gstrings_stats[i].stat_offset;
1015 data[i] = (mv643xx_eth_gstrings_stats[i].sizeof_stat ==
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001016 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1017 }
1018}
1019
Lennert Buytenheke5371492008-06-01 02:18:13 +02001020static int mv643xx_eth_get_sset_count(struct net_device *netdev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001021{
1022 switch (sset) {
1023 case ETH_SS_STATS:
Lennert Buytenheke5371492008-06-01 02:18:13 +02001024 return MV643XX_ETH_STATS_LEN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001025 default:
1026 return -EOPNOTSUPP;
1027 }
1028}
1029
Lennert Buytenheke5371492008-06-01 02:18:13 +02001030static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
1031 .get_settings = mv643xx_eth_get_settings,
1032 .set_settings = mv643xx_eth_set_settings,
1033 .get_drvinfo = mv643xx_eth_get_drvinfo,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001034 .get_link = mv643xx_eth_get_link,
1035 .set_sg = ethtool_op_set_sg,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001036 .get_sset_count = mv643xx_eth_get_sset_count,
1037 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1038 .get_strings = mv643xx_eth_get_strings,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001039 .nway_reset = mv643xx_eth_nway_restart,
1040};
1041
1042
1043/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001044static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001045{
1046 unsigned int port_num = mp->port_num;
1047 unsigned int mac_h;
1048 unsigned int mac_l;
1049
1050 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1051 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1052
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001053 addr[0] = (mac_h >> 24) & 0xff;
1054 addr[1] = (mac_h >> 16) & 0xff;
1055 addr[2] = (mac_h >> 8) & 0xff;
1056 addr[3] = mac_h & 0xff;
1057 addr[4] = (mac_l >> 8) & 0xff;
1058 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001059}
1060
Lennert Buytenheke5371492008-06-01 02:18:13 +02001061static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001062{
1063 unsigned int port_num = mp->port_num;
1064 int table_index;
1065
1066 /* Clear DA filter unicast table (Ex_dFUT) */
1067 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001068 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001069
1070 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1071 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001072 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001073 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001074 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001075 }
1076}
1077
Lennert Buytenheke5371492008-06-01 02:18:13 +02001078static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001079 int table, unsigned char entry)
1080{
1081 unsigned int table_reg;
1082 unsigned int tbl_offset;
1083 unsigned int reg_offset;
1084
1085 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1086 reg_offset = entry % 4; /* Entry offset within the register */
1087
1088 /* Set "accepts frame bit" at specified table entry */
1089 table_reg = rdl(mp, table + tbl_offset);
1090 table_reg |= 0x01 << (8 * reg_offset);
1091 wrl(mp, table + tbl_offset, table_reg);
1092}
1093
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001094static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001095{
1096 unsigned int port_num = mp->port_num;
1097 unsigned int mac_h;
1098 unsigned int mac_l;
1099 int table;
1100
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001101 mac_l = (addr[4] << 8) | (addr[5]);
1102 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) |
1103 (addr[3] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001104
1105 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1106 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1107
1108 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001109 table = UNICAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001110 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001111}
1112
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001113static void mv643xx_eth_update_mac_address(struct net_device *dev)
1114{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001115 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001116
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001117 init_mac_tables(mp);
1118 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001119}
1120
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001121static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1122{
1123 int i;
1124
1125 for (i = 0; i < 6; i++)
1126 /* +2 is for the offset of the HW addr type */
1127 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1128 mv643xx_eth_update_mac_address(dev);
1129 return 0;
1130}
1131
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001132static void mc_addr(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001133{
1134 unsigned int port_num = mp->port_num;
1135 unsigned int mac_h;
1136 unsigned int mac_l;
1137 unsigned char crc_result = 0;
1138 int table;
1139 int mac_array[48];
1140 int crc[8];
1141 int i;
1142
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001143 if ((addr[0] == 0x01) && (addr[1] == 0x00) &&
1144 (addr[2] == 0x5E) && (addr[3] == 0x00) && (addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001145 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001146 set_filter_table_entry(mp, table, addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001147 return;
1148 }
1149
1150 /* Calculate CRC-8 out of the given address */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001151 mac_h = (addr[0] << 8) | (addr[1]);
1152 mac_l = (addr[2] << 24) | (addr[3] << 16) |
1153 (addr[4] << 8) | (addr[5] << 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001154
1155 for (i = 0; i < 32; i++)
1156 mac_array[i] = (mac_l >> i) & 0x1;
1157 for (i = 32; i < 48; i++)
1158 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1159
1160 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1161 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1162 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1163 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1164 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1165
1166 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1167 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1168 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1169 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1170 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1171 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1172 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1173
1174 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1175 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1176 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1177 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1178 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1179 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1180
1181 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1182 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1183 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1184 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1185 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1186 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1187
1188 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1189 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1190 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1191 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1192 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1193 mac_array[3] ^ mac_array[2];
1194
1195 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1196 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1197 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1198 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1199 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1200 mac_array[4] ^ mac_array[3];
1201
1202 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1203 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1204 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1205 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1206 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1207 mac_array[4];
1208
1209 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1210 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1211 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1212 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1213 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1214
1215 for (i = 0; i < 8; i++)
1216 crc_result = crc_result | (crc[i] << i);
1217
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001218 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001219 set_filter_table_entry(mp, table, crc_result);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001220}
1221
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001222static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001223{
1224
1225 struct dev_mc_list *mc_list;
1226 int i;
1227 int table_index;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001228 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001229 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001230
1231 /* If the device is in promiscuous mode or in all multicast mode,
1232 * we will fully populate both multicast tables with accept.
1233 * This is guaranteed to yield a match on all multicast addresses...
1234 */
1235 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1236 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1237 /* Set all entries in DA filter special multicast
1238 * table (Ex_dFSMT)
1239 * Set for ETH_Q0 for now
1240 * Bits
1241 * 0 Accept=1, Drop=0
1242 * 3-1 Queue ETH_Q0=0
1243 * 7-4 Reserved = 0;
1244 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001245 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001246
1247 /* Set all entries in DA filter other multicast
1248 * table (Ex_dFOMT)
1249 * Set for ETH_Q0 for now
1250 * Bits
1251 * 0 Accept=1, Drop=0
1252 * 3-1 Queue ETH_Q0=0
1253 * 7-4 Reserved = 0;
1254 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001255 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001256 }
1257 return;
1258 }
1259
1260 /* We will clear out multicast tables every time we get the list.
1261 * Then add the entire new list...
1262 */
1263 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1264 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001265 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001266
1267 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001268 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001269 }
1270
1271 /* Get pointer to net_device multicast list and add each one... */
1272 for (i = 0, mc_list = dev->mc_list;
1273 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1274 i++, mc_list = mc_list->next)
1275 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001276 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001277}
1278
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001279static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1280{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001281 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001282 u32 config_reg;
1283
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001284 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001285 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001286 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001287 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001288 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001289 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001290
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001291 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001292}
1293
1294
1295/* rx/tx queue initialisation ***********************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001296static void ether_init_rx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001297{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001298 volatile struct rx_desc *p_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001299 int rx_desc_num = mp->rx_ring_size;
1300 int i;
1301
1302 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001303 p_rx_desc = (struct rx_desc *)mp->rx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001304 for (i = 0; i < rx_desc_num; i++) {
1305 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001306 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001307 }
1308
1309 /* Save Rx desc pointer to driver struct. */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001310 mp->rx_curr_desc = 0;
1311 mp->rx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001312
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001313 mp->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001314}
1315
1316static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1317{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001318 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001319 int curr;
1320
1321 /* Stop RX Queues */
1322 mv643xx_eth_port_disable_rx(mp);
1323
1324 /* Free preallocated skb's on RX rings */
1325 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1326 if (mp->rx_skb[curr]) {
1327 dev_kfree_skb(mp->rx_skb[curr]);
1328 mp->rx_desc_count--;
1329 }
1330 }
1331
1332 if (mp->rx_desc_count)
1333 printk(KERN_ERR
1334 "%s: Error in freeing Rx Ring. %d skb's still"
1335 " stuck in RX Ring - ignoring them\n", dev->name,
1336 mp->rx_desc_count);
1337 /* Free RX ring */
1338 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001339 iounmap(mp->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001340 else
1341 dma_free_coherent(NULL, mp->rx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001342 mp->rx_desc_area, mp->rx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001343}
1344
Lennert Buytenheke5371492008-06-01 02:18:13 +02001345static void ether_init_tx_desc_ring(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001346{
1347 int tx_desc_num = mp->tx_ring_size;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001348 struct tx_desc *p_tx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001349 int i;
1350
1351 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001352 p_tx_desc = (struct tx_desc *)mp->tx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001353 for (i = 0; i < tx_desc_num; i++) {
1354 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001355 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001356 }
1357
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001358 mp->tx_curr_desc = 0;
1359 mp->tx_used_desc = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001360
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001361 mp->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001362}
1363
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001364static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1365{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001366 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001367 struct tx_desc *desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001368 u32 cmd_sts;
1369 struct sk_buff *skb;
1370 unsigned long flags;
1371 int tx_index;
1372 dma_addr_t addr;
1373 int count;
1374 int released = 0;
1375
1376 while (mp->tx_desc_count > 0) {
1377 spin_lock_irqsave(&mp->lock, flags);
1378
1379 /* tx_desc_count might have changed before acquiring the lock */
1380 if (mp->tx_desc_count <= 0) {
1381 spin_unlock_irqrestore(&mp->lock, flags);
1382 return released;
1383 }
1384
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001385 tx_index = mp->tx_used_desc;
1386 desc = &mp->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001387 cmd_sts = desc->cmd_sts;
1388
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001389 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001390 spin_unlock_irqrestore(&mp->lock, flags);
1391 return released;
1392 }
1393
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001394 mp->tx_used_desc = (tx_index + 1) % mp->tx_ring_size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001395 mp->tx_desc_count--;
1396
1397 addr = desc->buf_ptr;
1398 count = desc->byte_cnt;
1399 skb = mp->tx_skb[tx_index];
1400 if (skb)
1401 mp->tx_skb[tx_index] = NULL;
1402
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001403 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001404 printk("%s: Error in TX\n", dev->name);
1405 dev->stats.tx_errors++;
1406 }
1407
1408 spin_unlock_irqrestore(&mp->lock, flags);
1409
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001410 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001411 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1412 else
1413 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1414
1415 if (skb)
1416 dev_kfree_skb_irq(skb);
1417
1418 released = 1;
1419 }
1420
1421 return released;
1422}
1423
1424static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1425{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001426 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001427
1428 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1429 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1430 netif_wake_queue(dev);
1431}
1432
1433static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1434{
1435 mv643xx_eth_free_tx_descs(dev, 1);
1436}
1437
1438static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1439{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001440 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001441
1442 /* Stop Tx Queues */
1443 mv643xx_eth_port_disable_tx(mp);
1444
1445 /* Free outstanding skb's on TX ring */
1446 mv643xx_eth_free_all_tx_descs(dev);
1447
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001448 BUG_ON(mp->tx_used_desc != mp->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001449
1450 /* Free TX ring */
1451 if (mp->tx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001452 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001453 else
1454 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001455 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001456}
1457
1458
1459/* netdev ops and related ***************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001460static void port_reset(struct mv643xx_eth_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001461
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001462static void mv643xx_eth_update_pscr(struct net_device *dev,
1463 struct ethtool_cmd *ecmd)
1464{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001465 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001466 int port_num = mp->port_num;
1467 u32 o_pscr, n_pscr;
1468 unsigned int queues;
1469
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001470 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001471 n_pscr = o_pscr;
1472
1473 /* clear speed, duplex and rx buffer size fields */
1474 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1475 SET_GMII_SPEED_TO_1000 |
1476 SET_FULL_DUPLEX_MODE |
1477 MAX_RX_PACKET_MASK);
1478
1479 if (ecmd->duplex == DUPLEX_FULL)
1480 n_pscr |= SET_FULL_DUPLEX_MODE;
1481
1482 if (ecmd->speed == SPEED_1000)
1483 n_pscr |= SET_GMII_SPEED_TO_1000 |
1484 MAX_RX_PACKET_9700BYTE;
1485 else {
1486 if (ecmd->speed == SPEED_100)
1487 n_pscr |= SET_MII_SPEED_TO_100;
1488 n_pscr |= MAX_RX_PACKET_1522BYTE;
1489 }
1490
1491 if (n_pscr != o_pscr) {
1492 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001493 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001494 else {
1495 queues = mv643xx_eth_port_disable_tx(mp);
1496
1497 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001498 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1499 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1500 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001501 if (queues)
1502 mv643xx_eth_port_enable_tx(mp, queues);
1503 }
1504 }
1505}
1506
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001507static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1508{
1509 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001510 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001511 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001512 unsigned int port_num = mp->port_num;
1513
1514 /* Read interrupt cause registers */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001515 int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1516 if (int_cause & INT_EXT) {
1517 int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001518 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001519 wrl(mp, INT_CAUSE_EXT(port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001520 }
1521
1522 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001523 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001524 struct ethtool_cmd cmd;
1525
1526 if (mii_link_ok(&mp->mii)) {
1527 mii_ethtool_gset(&mp->mii, &cmd);
1528 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001529 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001530 if (!netif_carrier_ok(dev)) {
1531 netif_carrier_on(dev);
1532 if (mp->tx_ring_size - mp->tx_desc_count >=
1533 MAX_DESCS_PER_SKB)
1534 netif_wake_queue(dev);
1535 }
1536 } else if (netif_carrier_ok(dev)) {
1537 netif_stop_queue(dev);
1538 netif_carrier_off(dev);
1539 }
1540 }
1541
Lennert Buytenheke5371492008-06-01 02:18:13 +02001542#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001543 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001544 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001545 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001546
1547 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001548 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001549
1550 netif_rx_schedule(dev, &mp->napi);
1551 }
1552#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001553 if (int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001554 mv643xx_eth_receive_queue(dev, INT_MAX);
1555#endif
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001556 if (int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001557 mv643xx_eth_free_completed_tx_descs(dev);
1558
1559 /*
1560 * If no real interrupt occured, exit.
1561 * This can happen when using gigE interrupt coalescing mechanism.
1562 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001563 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001564 return IRQ_NONE;
1565
1566 return IRQ_HANDLED;
1567}
1568
Lennert Buytenheke5371492008-06-01 02:18:13 +02001569static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001570{
1571 unsigned int phy_reg_data;
1572
1573 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001574 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001575 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001576 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001577
1578 /* wait for PHY to come out of reset */
1579 do {
1580 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001581 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001582 } while (phy_reg_data & 0x8000);
1583}
1584
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001585static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001586{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001587 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001588 unsigned int port_num = mp->port_num;
1589 int tx_curr_desc, rx_curr_desc;
1590 u32 pscr;
1591 struct ethtool_cmd ethtool_cmd;
1592
1593 /* Assignment of Tx CTRP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001594 tx_curr_desc = mp->tx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001595 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001596 (u32)((struct tx_desc *)mp->tx_desc_dma + tx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001597
1598 /* Assignment of Rx CRDP of given queue */
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001599 rx_curr_desc = mp->rx_curr_desc;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001600 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001601 (u32)((struct rx_desc *)mp->rx_desc_dma + rx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001602
1603 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001604 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001605
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001606 /*
1607 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1608 * frames to RX queue #0.
1609 */
1610 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001611
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001612 /*
1613 * Treat BPDUs as normal multicasts, and disable partition mode.
1614 */
1615 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001616
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001617 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001618
1619 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001620 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001621
1622 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1623 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02001624 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001625 DO_NOT_FORCE_LINK_FAIL |
1626 SERIAL_PORT_CONTROL_RESERVED;
1627
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001628 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001629
1630 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001631 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001632
1633 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001634 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001635
1636 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001637 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001638
1639 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001640 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001641
1642 /* save phy settings across reset */
Lennert Buytenheke5371492008-06-01 02:18:13 +02001643 mv643xx_eth_get_settings(dev, &ethtool_cmd);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001644 phy_reset(mp);
Lennert Buytenheke5371492008-06-01 02:18:13 +02001645 mv643xx_eth_set_settings(dev, &ethtool_cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001646}
1647
Lennert Buytenheke5371492008-06-01 02:18:13 +02001648#ifdef MV643XX_ETH_COAL
Lennert Buytenheke5371492008-06-01 02:18:13 +02001649static unsigned int set_rx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001650 unsigned int delay)
1651{
1652 unsigned int port_num = mp->port_num;
1653 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1654
1655 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001656 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001657 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001658 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001659 & 0xffc000ff));
1660
1661 return coal;
1662}
1663#endif
1664
Lennert Buytenheke5371492008-06-01 02:18:13 +02001665static unsigned int set_tx_coal(struct mv643xx_eth_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001666 unsigned int delay)
1667{
1668 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
1669
1670 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001671 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001672
1673 return coal;
1674}
1675
Lennert Buytenheke5371492008-06-01 02:18:13 +02001676static void port_init(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001677{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001678 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001679
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001680 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001681}
1682
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001683static int mv643xx_eth_open(struct net_device *dev)
1684{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001685 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001686 unsigned int port_num = mp->port_num;
1687 unsigned int size;
1688 int err;
1689
1690 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001691 wrl(mp, INT_CAUSE(port_num), 0);
1692 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001693 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001694 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001695
1696 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1697 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
1698 if (err) {
1699 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
1700 return -EAGAIN;
1701 }
1702
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001703 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001704
1705 memset(&mp->timeout, 0, sizeof(struct timer_list));
1706 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
1707 mp->timeout.data = (unsigned long)dev;
1708
1709 /* Allocate RX and TX skb rings */
1710 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
1711 GFP_KERNEL);
1712 if (!mp->rx_skb) {
1713 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
1714 err = -ENOMEM;
1715 goto out_free_irq;
1716 }
1717 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1718 GFP_KERNEL);
1719 if (!mp->tx_skb) {
1720 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
1721 err = -ENOMEM;
1722 goto out_free_rx_skb;
1723 }
1724
1725 /* Allocate TX ring */
1726 mp->tx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001727 size = mp->tx_ring_size * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001728 mp->tx_desc_area_size = size;
1729
1730 if (mp->tx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001731 mp->tx_desc_area = ioremap(mp->tx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001732 mp->tx_sram_size);
1733 mp->tx_desc_dma = mp->tx_sram_addr;
1734 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001735 mp->tx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001736 &mp->tx_desc_dma,
1737 GFP_KERNEL);
1738
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001739 if (!mp->tx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001740 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1741 dev->name, size);
1742 err = -ENOMEM;
1743 goto out_free_tx_skb;
1744 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001745 BUG_ON((u32) mp->tx_desc_area & 0xf); /* check 16-byte alignment */
1746 memset((void *)mp->tx_desc_area, 0, mp->tx_desc_area_size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001747
1748 ether_init_tx_desc_ring(mp);
1749
1750 /* Allocate RX ring */
1751 mp->rx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001752 size = mp->rx_ring_size * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001753 mp->rx_desc_area_size = size;
1754
1755 if (mp->rx_sram_size) {
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001756 mp->rx_desc_area = ioremap(mp->rx_sram_addr,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001757 mp->rx_sram_size);
1758 mp->rx_desc_dma = mp->rx_sram_addr;
1759 } else
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001760 mp->rx_desc_area = dma_alloc_coherent(NULL, size,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001761 &mp->rx_desc_dma,
1762 GFP_KERNEL);
1763
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001764 if (!mp->rx_desc_area) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001765 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
1766 dev->name, size);
1767 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
1768 dev->name);
1769 if (mp->rx_sram_size)
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001770 iounmap(mp->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001771 else
1772 dma_free_coherent(NULL, mp->tx_desc_area_size,
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001773 mp->tx_desc_area, mp->tx_desc_dma);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001774 err = -ENOMEM;
1775 goto out_free_tx_skb;
1776 }
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001777 memset((void *)mp->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001778
1779 ether_init_rx_desc_ring(mp);
1780
1781 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
1782
Lennert Buytenheke5371492008-06-01 02:18:13 +02001783#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001784 napi_enable(&mp->napi);
1785#endif
1786
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001787 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001788
1789 /* Interrupt Coalescing */
1790
Lennert Buytenheke5371492008-06-01 02:18:13 +02001791#ifdef MV643XX_ETH_COAL
1792 mp->rx_int_coal = set_rx_coal(mp, MV643XX_ETH_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001793#endif
1794
Lennert Buytenheke5371492008-06-01 02:18:13 +02001795 mp->tx_int_coal = set_tx_coal(mp, MV643XX_ETH_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001796
1797 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001798 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001799
1800 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001801 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001802
1803 return 0;
1804
1805out_free_tx_skb:
1806 kfree(mp->tx_skb);
1807out_free_rx_skb:
1808 kfree(mp->rx_skb);
1809out_free_irq:
1810 free_irq(dev->irq, dev);
1811
1812 return err;
1813}
1814
Lennert Buytenheke5371492008-06-01 02:18:13 +02001815static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001816{
1817 unsigned int port_num = mp->port_num;
1818 unsigned int reg_data;
1819
1820 mv643xx_eth_port_disable_tx(mp);
1821 mv643xx_eth_port_disable_rx(mp);
1822
1823 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001824 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001825
1826 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001827 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001828 reg_data &= ~(SERIAL_PORT_ENABLE |
1829 DO_NOT_FORCE_LINK_FAIL |
1830 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001831 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001832}
1833
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001834static int mv643xx_eth_stop(struct net_device *dev)
1835{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001836 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001837 unsigned int port_num = mp->port_num;
1838
1839 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001840 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001841 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001842 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001843
Lennert Buytenheke5371492008-06-01 02:18:13 +02001844#ifdef MV643XX_ETH_NAPI
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001845 napi_disable(&mp->napi);
1846#endif
1847 netif_carrier_off(dev);
1848 netif_stop_queue(dev);
1849
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001850 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001851
1852 mv643xx_eth_free_tx_rings(dev);
1853 mv643xx_eth_free_rx_rings(dev);
1854
1855 free_irq(dev->irq, dev);
1856
1857 return 0;
1858}
1859
1860static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1861{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001862 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001863
1864 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
1865}
1866
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001867static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
1868{
1869 if ((new_mtu > 9500) || (new_mtu < 64))
1870 return -EINVAL;
1871
1872 dev->mtu = new_mtu;
1873 if (!netif_running(dev))
1874 return 0;
1875
1876 /*
1877 * Stop and then re-open the interface. This will allocate RX
1878 * skbs of the new MTU.
1879 * There is a possible danger that the open will not succeed,
1880 * due to memory being full, which might fail the open function.
1881 */
1882 mv643xx_eth_stop(dev);
1883 if (mv643xx_eth_open(dev)) {
1884 printk(KERN_ERR "%s: Fatal error on opening device\n",
1885 dev->name);
1886 }
1887
1888 return 0;
1889}
1890
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001891static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1892{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001893 struct mv643xx_eth_private *mp = container_of(ugly, struct mv643xx_eth_private,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001894 tx_timeout_task);
1895 struct net_device *dev = mp->dev;
1896
1897 if (!netif_running(dev))
1898 return;
1899
1900 netif_stop_queue(dev);
1901
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001902 port_reset(mp);
1903 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001904
1905 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1906 netif_wake_queue(dev);
1907}
1908
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001909static void mv643xx_eth_tx_timeout(struct net_device *dev)
1910{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001911 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001912
1913 printk(KERN_INFO "%s: TX timeout ", dev->name);
1914
1915 /* Do the reset outside of interrupt context */
1916 schedule_work(&mp->tx_timeout_task);
1917}
1918
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001919#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02001920static void mv643xx_eth_netpoll(struct net_device *netdev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001921{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001922 struct mv643xx_eth_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001923 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001924
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001925 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001926 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001927 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001928
Al Viro9da3b1a2006-10-08 15:00:44 +01001929 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001930
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001931 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001932}
1933#endif
1934
Lennert Buytenheke5371492008-06-01 02:18:13 +02001935static int mv643xx_eth_mdio_read(struct net_device *dev, int phy_id, int location)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001936{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001937 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001938 int val;
1939
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001940 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001941 return val;
1942}
1943
Lennert Buytenheke5371492008-06-01 02:18:13 +02001944static void mv643xx_eth_mdio_write(struct net_device *dev, int phy_id, int location, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001945{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001946 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001947 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001948}
1949
1950
1951/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001952static void
1953mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
1954 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001955{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001956 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001957 u32 win_enable;
1958 u32 win_protect;
1959 int i;
1960
1961 for (i = 0; i < 6; i++) {
1962 writel(0, base + WINDOW_BASE(i));
1963 writel(0, base + WINDOW_SIZE(i));
1964 if (i < 4)
1965 writel(0, base + WINDOW_REMAP_HIGH(i));
1966 }
1967
1968 win_enable = 0x3f;
1969 win_protect = 0;
1970
1971 for (i = 0; i < dram->num_cs; i++) {
1972 struct mbus_dram_window *cs = dram->cs + i;
1973
1974 writel((cs->base & 0xffff0000) |
1975 (cs->mbus_attr << 8) |
1976 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
1977 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
1978
1979 win_enable &= ~(1 << i);
1980 win_protect |= 3 << (2 * i);
1981 }
1982
1983 writel(win_enable, base + WINDOW_BAR_ENABLE);
1984 msp->win_protect = win_protect;
1985}
1986
1987static int mv643xx_eth_shared_probe(struct platform_device *pdev)
1988{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001989 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001990 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001991 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001992 struct resource *res;
1993 int ret;
1994
Lennert Buytenheke5371492008-06-01 02:18:13 +02001995 if (!mv643xx_eth_version_printed++)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001996 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1997
1998 ret = -EINVAL;
1999 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2000 if (res == NULL)
2001 goto out;
2002
2003 ret = -ENOMEM;
2004 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2005 if (msp == NULL)
2006 goto out;
2007 memset(msp, 0, sizeof(*msp));
2008
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002009 msp->base = ioremap(res->start, res->end - res->start + 1);
2010 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002011 goto out_free;
2012
2013 spin_lock_init(&msp->phy_lock);
2014 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2015
2016 platform_set_drvdata(pdev, msp);
2017
2018 /*
2019 * (Re-)program MBUS remapping windows if we are asked to.
2020 */
2021 if (pd != NULL && pd->dram != NULL)
2022 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2023
2024 return 0;
2025
2026out_free:
2027 kfree(msp);
2028out:
2029 return ret;
2030}
2031
2032static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2033{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002034 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002035
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002036 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002037 kfree(msp);
2038
2039 return 0;
2040}
2041
2042static struct platform_driver mv643xx_eth_shared_driver = {
2043 .probe = mv643xx_eth_shared_probe,
2044 .remove = mv643xx_eth_shared_remove,
2045 .driver = {
2046 .name = MV643XX_ETH_SHARED_NAME,
2047 .owner = THIS_MODULE,
2048 },
2049};
2050
Lennert Buytenheke5371492008-06-01 02:18:13 +02002051static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002052{
2053 u32 reg_data;
2054 int addr_shift = 5 * mp->port_num;
2055
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002056 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002057 reg_data &= ~(0x1f << addr_shift);
2058 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002059 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002060}
2061
Lennert Buytenheke5371492008-06-01 02:18:13 +02002062static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002063{
2064 unsigned int reg_data;
2065
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002066 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002067
2068 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2069}
2070
Lennert Buytenheke5371492008-06-01 02:18:13 +02002071static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002072{
2073 unsigned int phy_reg_data0;
2074 int auto_neg;
2075
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002076 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002077 auto_neg = phy_reg_data0 & 0x1000;
2078 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002079 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002080
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002081 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002082 if ((phy_reg_data0 & 0x1000) == auto_neg)
2083 return -ENODEV; /* change didn't take */
2084
2085 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002086 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002087 return 0;
2088}
2089
James Chapmand0412d92006-01-27 01:15:30 -07002090static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2091 int speed, int duplex,
2092 struct ethtool_cmd *cmd)
2093{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002094 struct mv643xx_eth_private *mp = netdev_priv(dev);
James Chapmand0412d92006-01-27 01:15:30 -07002095
2096 memset(cmd, 0, sizeof(*cmd));
2097
2098 cmd->port = PORT_MII;
2099 cmd->transceiver = XCVR_INTERNAL;
2100 cmd->phy_address = phy_address;
2101
2102 if (speed == 0) {
2103 cmd->autoneg = AUTONEG_ENABLE;
2104 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2105 cmd->speed = SPEED_100;
2106 cmd->advertising = ADVERTISED_10baseT_Half |
2107 ADVERTISED_10baseT_Full |
2108 ADVERTISED_100baseT_Half |
2109 ADVERTISED_100baseT_Full;
2110 if (mp->mii.supports_gmii)
2111 cmd->advertising |= ADVERTISED_1000baseT_Full;
2112 } else {
2113 cmd->autoneg = AUTONEG_DISABLE;
2114 cmd->speed = speed;
2115 cmd->duplex = duplex;
2116 }
2117}
2118
Russell King3ae5eae2005-11-09 22:32:44 +00002119static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002122 int port_num;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002123 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 struct net_device *dev;
2125 u8 *p;
2126 struct resource *res;
2127 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002128 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002129 int duplex = DUPLEX_HALF;
2130 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002131 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002133 pd = pdev->dev.platform_data;
2134 if (pd == NULL) {
2135 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2136 return -ENODEV;
2137 }
2138
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002139 if (pd->shared == NULL) {
2140 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2141 return -ENODEV;
2142 }
2143
Lennert Buytenheke5371492008-06-01 02:18:13 +02002144 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 if (!dev)
2146 return -ENOMEM;
2147
Russell King3ae5eae2005-11-09 22:32:44 +00002148 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002151 mp->dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002152#ifdef MV643XX_ETH_NAPI
2153 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 64);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2157 BUG_ON(!res);
2158 dev->irq = res->start;
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 dev->open = mv643xx_eth_open;
2161 dev->stop = mv643xx_eth_stop;
2162 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 dev->set_mac_address = mv643xx_eth_set_mac_address;
2164 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2165
2166 /* No need to Tx Timeout */
2167 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002169#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002170 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002171#endif
2172
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 dev->base_addr = 0;
2175 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002176 dev->do_ioctl = mv643xx_eth_do_ioctl;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002177 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Lennert Buytenheke5371492008-06-01 02:18:13 +02002179#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180#ifdef MAX_SKB_FRAGS
2181 /*
2182 * Zero copy can only work if we use Discovery II memory. Else, we will
2183 * have to map the buffers to ISA memory which is only 16 MB
2184 */
Wolfram Joost63890572006-01-16 16:57:41 -07002185 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#endif
2187#endif
2188
2189 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002190 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
2192 spin_lock_init(&mp->lock);
2193
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002194 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002195 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002196
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002197 if (mp->shared->win_protect)
2198 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2199
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002200 mp->shared_smi = mp->shared;
2201 if (pd->shared_smi != NULL)
2202 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002205 uc_addr_get(mp, dev->dev_addr);
2206 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2207 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002209 if (is_valid_ether_addr(pd->mac_addr))
2210 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002212 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002213 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002215 if (pd->rx_queue_size)
2216 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002218 if (pd->tx_queue_size)
2219 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002221 if (pd->tx_sram_size) {
2222 mp->tx_sram_size = pd->tx_sram_size;
2223 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002226 if (pd->rx_sram_size) {
2227 mp->rx_sram_size = pd->rx_sram_size;
2228 mp->rx_sram_addr = pd->rx_sram_addr;
2229 }
2230
2231 duplex = pd->duplex;
2232 speed = pd->speed;
2233
James Chapmanc28a4f82006-01-27 01:13:15 -07002234 /* Hook up MII support for ethtool */
2235 mp->mii.dev = dev;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002236 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2237 mp->mii.mdio_write = mv643xx_eth_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002238 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002239 mp->mii.phy_id_mask = 0x3f;
2240 mp->mii.reg_num_mask = 0x1f;
2241
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002242 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002244 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002245 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002246 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002249 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002250 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002251 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2252 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002253 mv643xx_eth_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002254
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002255 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 err = register_netdev(dev);
2257 if (err)
2258 goto out;
2259
2260 p = dev->dev_addr;
2261 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002262 "%s: port %d with MAC address %s\n",
2263 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 if (dev->features & NETIF_F_SG)
2266 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2267
2268 if (dev->features & NETIF_F_IP_CSUM)
2269 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2270 dev->name);
2271
Lennert Buytenheke5371492008-06-01 02:18:13 +02002272#ifdef MV643XX_ETH_CHECKSUM_OFFLOAD_TX
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2274#endif
2275
Lennert Buytenheke5371492008-06-01 02:18:13 +02002276#ifdef MV643XX_ETH_COAL
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2278 dev->name);
2279#endif
2280
Lennert Buytenheke5371492008-06-01 02:18:13 +02002281#ifdef MV643XX_ETH_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2283#endif
2284
Nicolas DETb1529872005-10-28 17:46:30 -07002285 if (mp->tx_sram_size > 0)
2286 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2287
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return 0;
2289
2290out:
2291 free_netdev(dev);
2292
2293 return err;
2294}
2295
Russell King3ae5eae2005-11-09 22:32:44 +00002296static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297{
Russell King3ae5eae2005-11-09 22:32:44 +00002298 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 unregister_netdev(dev);
2301 flush_scheduled_work();
2302
2303 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002304 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return 0;
2306}
2307
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002308static void mv643xx_eth_shutdown(struct platform_device *pdev)
2309{
2310 struct net_device *dev = platform_get_drvdata(pdev);
Lennert Buytenheke5371492008-06-01 02:18:13 +02002311 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002312 unsigned int port_num = mp->port_num;
2313
2314 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002315 wrl(mp, INT_MASK(port_num), 0);
2316 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002317
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002318 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002319}
2320
Russell King3ae5eae2005-11-09 22:32:44 +00002321static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 .probe = mv643xx_eth_probe,
2323 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002324 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002325 .driver = {
2326 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002327 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002328 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329};
2330
Lennert Buytenheke5371492008-06-01 02:18:13 +02002331static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 int rc;
2334
Russell King3ae5eae2005-11-09 22:32:44 +00002335 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002337 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002339 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341 return rc;
2342}
2343
Lennert Buytenheke5371492008-06-01 02:18:13 +02002344static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Russell King3ae5eae2005-11-09 22:32:44 +00002346 platform_driver_unregister(&mv643xx_eth_driver);
2347 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348}
2349
Lennert Buytenheke5371492008-06-01 02:18:13 +02002350module_init(mv643xx_eth_init_module);
2351module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353MODULE_LICENSE("GPL");
2354MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2355 " and Dale Farnsworth");
2356MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002357MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2358MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);