blob: c41541d8710fd817be17ca727899071b56fb7d04 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenheke5371492008-06-01 02:18:13 +020057static char mv643xx_eth_driver_name[] = "mv643xx_eth";
Lennert Buytenhekc4560312008-08-24 07:33:05 +020058static char mv643xx_eth_driver_version[] = "1.3";
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020059
Lennert Buytenheke5371492008-06-01 02:18:13 +020060#define MV643XX_ETH_TX_FAST_REFILL
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020061
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020062/*
63 * Registers shared between all ports.
64 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020065#define PHY_ADDR 0x0000
66#define SMI_REG 0x0004
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +020067#define SMI_BUSY 0x10000000
68#define SMI_READ_VALID 0x08000000
69#define SMI_OPCODE_READ 0x04000000
70#define SMI_OPCODE_WRITE 0x00000000
71#define ERR_INT_CAUSE 0x0080
72#define ERR_INT_SMI_DONE 0x00000010
73#define ERR_INT_MASK 0x0084
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020074#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
75#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
76#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
77#define WINDOW_BAR_ENABLE 0x0290
78#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020079
80/*
81 * Per-port registers.
82 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020083#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020084#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020085#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
86#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
87#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
88#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
89#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
90#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +020091#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhekae9ae062008-07-15 02:15:24 +020092#define TX_IN_PROGRESS 0x00000080
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +020093#define PORT_SPEED_MASK 0x00000030
94#define PORT_SPEED_1000 0x00000010
95#define PORT_SPEED_100 0x00000020
96#define PORT_SPEED_10 0x00000000
97#define FLOW_CONTROL_ENABLED 0x00000008
98#define FULL_DUPLEX 0x00000004
Lennert Buytenhek81600eea92008-07-14 14:29:40 +020099#define LINK_UP 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200100#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200101#define TXQ_FIX_PRIO_CONF(p) (0x044c + ((p) << 10))
102#define TX_BW_RATE(p) (0x0450 + ((p) << 10))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200103#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200104#define TX_BW_BURST(p) (0x045c + ((p) << 10))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200105#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200106#define INT_TX_END_0 0x00080000
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200107#define INT_TX_END 0x07f80000
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200108#define INT_RX 0x0007fbfc
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200109#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200110#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200111#define INT_EXT_LINK 0x00100000
112#define INT_EXT_PHY 0x00010000
113#define INT_EXT_TX_ERROR_0 0x00000100
114#define INT_EXT_TX_0 0x00000001
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200115#define INT_EXT_TX 0x0000ffff
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200116#define INT_MASK(p) (0x0468 + ((p) << 10))
117#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
118#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200119#define TXQ_FIX_PRIO_CONF_MOVED(p) (0x04dc + ((p) << 10))
120#define TX_BW_RATE_MOVED(p) (0x04e0 + ((p) << 10))
121#define TX_BW_MTU_MOVED(p) (0x04e8 + ((p) << 10))
122#define TX_BW_BURST_MOVED(p) (0x04ec + ((p) << 10))
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200123#define RXQ_CURRENT_DESC_PTR(p, q) (0x060c + ((p) << 10) + ((q) << 4))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200124#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200125#define TXQ_CURRENT_DESC_PTR(p, q) (0x06c0 + ((p) << 10) + ((q) << 2))
126#define TXQ_BW_TOKENS(p, q) (0x0700 + ((p) << 10) + ((q) << 4))
127#define TXQ_BW_CONF(p, q) (0x0704 + ((p) << 10) + ((q) << 4))
128#define TXQ_BW_WRR_CONF(p, q) (0x0708 + ((p) << 10) + ((q) << 4))
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200129#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
130#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
131#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
132#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200134
135/*
136 * SDMA configuration register.
137 */
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200138#define RX_BURST_SIZE_16_64BIT (4 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200139#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200140#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200141#define TX_BURST_SIZE_16_64BIT (4 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200142
143#if defined(__BIG_ENDIAN)
144#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200145 RX_BURST_SIZE_16_64BIT | \
146 TX_BURST_SIZE_16_64BIT
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147#elif defined(__LITTLE_ENDIAN)
148#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200149 RX_BURST_SIZE_16_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200150 BLM_RX_NO_SWAP | \
151 BLM_TX_NO_SWAP | \
Lennert Buytenhekcd4ccf72008-07-10 14:40:51 +0200152 TX_BURST_SIZE_16_64BIT
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200153#else
154#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
155#endif
156
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200157
158/*
159 * Port serial control register.
160 */
161#define SET_MII_SPEED_TO_100 (1 << 24)
162#define SET_GMII_SPEED_TO_1000 (1 << 23)
163#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200164#define MAX_RX_PACKET_9700BYTE (5 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200165#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
166#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
167#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
168#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
169#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
170#define FORCE_LINK_PASS (1 << 1)
171#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200173#define DEFAULT_RX_QUEUE_SIZE 400
174#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200175
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200176
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200177/*
178 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179 */
180#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200181struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200182 u16 byte_cnt; /* Descriptor buffer byte count */
183 u16 buf_size; /* Buffer size */
184 u32 cmd_sts; /* Descriptor command status */
185 u32 next_desc_ptr; /* Next descriptor pointer */
186 u32 buf_ptr; /* Descriptor buffer pointer */
187};
188
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200189struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200190 u16 byte_cnt; /* buffer byte count */
191 u16 l4i_chk; /* CPU provided TCP checksum */
192 u32 cmd_sts; /* Command/status field */
193 u32 next_desc_ptr; /* Pointer to next descriptor */
194 u32 buf_ptr; /* pointer to buffer for this descriptor*/
195};
196#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200197struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200198 u32 cmd_sts; /* Descriptor command status */
199 u16 buf_size; /* Buffer size */
200 u16 byte_cnt; /* Descriptor buffer byte count */
201 u32 buf_ptr; /* Descriptor buffer pointer */
202 u32 next_desc_ptr; /* Next descriptor pointer */
203};
204
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200205struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200206 u32 cmd_sts; /* Command/status field */
207 u16 l4i_chk; /* CPU provided TCP checksum */
208 u16 byte_cnt; /* buffer byte count */
209 u32 buf_ptr; /* pointer to buffer for this descriptor*/
210 u32 next_desc_ptr; /* Pointer to next descriptor */
211};
212#else
213#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
214#endif
215
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200216/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200217#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200218
219/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200220#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200221
222/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200223#define LAYER_4_CHECKSUM_OK 0x40000000
224#define RX_ENABLE_INTERRUPT 0x20000000
225#define RX_FIRST_DESC 0x08000000
226#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200227
228/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200229#define TX_ENABLE_INTERRUPT 0x00800000
230#define GEN_CRC 0x00400000
231#define TX_FIRST_DESC 0x00200000
232#define TX_LAST_DESC 0x00100000
233#define ZERO_PADDING 0x00080000
234#define GEN_IP_V4_CHECKSUM 0x00040000
235#define GEN_TCP_UDP_CHECKSUM 0x00020000
236#define UDP_FRAME 0x00010000
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200237#define MAC_HDR_EXTRA_4_BYTES 0x00008000
238#define MAC_HDR_EXTRA_8_BYTES 0x00000200
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200239
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200240#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200241
242
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200243/* global *******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200244struct mv643xx_eth_shared_private {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200245 /*
246 * Ethernet controller base address.
247 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200248 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200249
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200250 /*
251 * Protects access to SMI_REG, which is shared between ports.
252 */
Lennert Buytenhek2b3ba0e2008-08-24 05:41:09 +0200253 struct mutex phy_lock;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200254
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200255 /*
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200256 * If we have access to the error interrupt pin (which is
257 * somewhat misnamed as it not only reflects internal errors
258 * but also reflects SMI completion), use that to wait for
259 * SMI access completion instead of polling the SMI busy bit.
260 */
261 int err_interrupt;
262 wait_queue_head_t smi_busy_wait;
263
264 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200265 * Per-port MBUS window access register value.
266 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200267 u32 win_protect;
268
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200269 /*
270 * Hardware-specific parameters.
271 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200272 unsigned int t_clk;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +0200273 int extended_rx_coal_limit;
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200274 int tx_bw_control_moved;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200275};
276
277
278/* per-port *****************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200279struct mib_counters {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200280 u64 good_octets_received;
281 u32 bad_octets_received;
282 u32 internal_mac_transmit_err;
283 u32 good_frames_received;
284 u32 bad_frames_received;
285 u32 broadcast_frames_received;
286 u32 multicast_frames_received;
287 u32 frames_64_octets;
288 u32 frames_65_to_127_octets;
289 u32 frames_128_to_255_octets;
290 u32 frames_256_to_511_octets;
291 u32 frames_512_to_1023_octets;
292 u32 frames_1024_to_max_octets;
293 u64 good_octets_sent;
294 u32 good_frames_sent;
295 u32 excessive_collision;
296 u32 multicast_frames_sent;
297 u32 broadcast_frames_sent;
298 u32 unrec_mac_control_received;
299 u32 fc_sent;
300 u32 good_fc_received;
301 u32 bad_fc_received;
302 u32 undersize_received;
303 u32 fragments_received;
304 u32 oversize_received;
305 u32 jabber_received;
306 u32 mac_receive_error;
307 u32 bad_crc_event;
308 u32 collision;
309 u32 late_collision;
310};
311
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200312struct rx_queue {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200313 int index;
314
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200315 int rx_ring_size;
316
317 int rx_desc_count;
318 int rx_curr_desc;
319 int rx_used_desc;
320
321 struct rx_desc *rx_desc_area;
322 dma_addr_t rx_desc_dma;
323 int rx_desc_area_size;
324 struct sk_buff **rx_skb;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200325};
326
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200327struct tx_queue {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200328 int index;
329
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200330 int tx_ring_size;
331
332 int tx_desc_count;
333 int tx_curr_desc;
334 int tx_used_desc;
335
336 struct tx_desc *tx_desc_area;
337 dma_addr_t tx_desc_dma;
338 int tx_desc_area_size;
339 struct sk_buff **tx_skb;
340};
341
Lennert Buytenheke5371492008-06-01 02:18:13 +0200342struct mv643xx_eth_private {
343 struct mv643xx_eth_shared_private *shared;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200344 int port_num;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200345
346 struct net_device *dev;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200347
348 struct mv643xx_eth_shared_private *shared_smi;
349 int phy_addr;
350
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200351 spinlock_t lock;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200352
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200353 struct mib_counters mib_counters;
354 struct work_struct tx_timeout_task;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200355 struct mii_if_info mii;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200356
357 /*
358 * RX state.
359 */
360 int default_rx_ring_size;
361 unsigned long rx_desc_sram_addr;
362 int rx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200363 int rxq_count;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200364 struct napi_struct napi;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200365 struct timer_list rx_oom;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200366 struct rx_queue rxq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200367
368 /*
369 * TX state.
370 */
371 int default_tx_ring_size;
372 unsigned long tx_desc_sram_addr;
373 int tx_desc_sram_size;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200374 int txq_count;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200375 struct tx_queue txq[8];
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200376#ifdef MV643XX_ETH_TX_FAST_REFILL
377 int tx_clean_threshold;
378#endif
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200379};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200381
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200382/* port register accessors **************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +0200383static inline u32 rdl(struct mv643xx_eth_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200385 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Lennert Buytenheke5371492008-06-01 02:18:13 +0200388static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200390 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200393
394/* rxq/txq helper functions *************************************************/
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200395static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200397 return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200398}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200400static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq)
401{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200402 return container_of(txq, struct mv643xx_eth_private, txq[txq->index]);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200403}
404
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200405static void rxq_enable(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200406{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200407 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200408 wrl(mp, RXQ_COMMAND(mp->port_num), 1 << rxq->index);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200409}
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700410
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200411static void rxq_disable(struct rx_queue *rxq)
412{
413 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200414 u8 mask = 1 << rxq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200415
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200416 wrl(mp, RXQ_COMMAND(mp->port_num), mask << 8);
417 while (rdl(mp, RXQ_COMMAND(mp->port_num)) & mask)
418 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200419}
420
Lennert Buytenhek6b368f62008-07-11 19:38:34 +0200421static void txq_reset_hw_ptr(struct tx_queue *txq)
422{
423 struct mv643xx_eth_private *mp = txq_to_mp(txq);
424 int off = TXQ_CURRENT_DESC_PTR(mp->port_num, txq->index);
425 u32 addr;
426
427 addr = (u32)txq->tx_desc_dma;
428 addr += txq->tx_curr_desc * sizeof(struct tx_desc);
429 wrl(mp, off, addr);
430}
431
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200432static void txq_enable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200433{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200434 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200435 wrl(mp, TXQ_COMMAND(mp->port_num), 1 << txq->index);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200436}
437
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200438static void txq_disable(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200439{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200440 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200441 u8 mask = 1 << txq->index;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200442
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200443 wrl(mp, TXQ_COMMAND(mp->port_num), mask << 8);
444 while (rdl(mp, TXQ_COMMAND(mp->port_num)) & mask)
445 udelay(10);
446}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200447
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200448static void __txq_maybe_wake(struct tx_queue *txq)
449{
450 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200451
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200452 /*
453 * netif_{stop,wake}_queue() flow control only applies to
454 * the primary queue.
455 */
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200456 BUG_ON(txq->index != 0);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200457
Lennert Buytenhek17cd0a52008-08-24 05:33:55 +0200458 if (txq->tx_ring_size - txq->tx_desc_count >= MAX_SKB_FRAGS + 1)
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200459 netif_wake_queue(mp->dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200460}
461
462
463/* rx ***********************************************************************/
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200464static void txq_reclaim(struct tx_queue *txq, int force);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200465
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200466static int rxq_refill(struct rx_queue *rxq, int budget, int *oom)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200467{
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200468 int skb_size;
469 int refilled;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200470
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200471 /*
472 * Reserve 2+14 bytes for an ethernet header (the hardware
473 * automatically prepends 2 bytes of dummy data to each
474 * received packet), 16 bytes for up to four VLAN tags, and
475 * 4 bytes for the trailing FCS -- 36 bytes total.
476 */
477 skb_size = rxq_to_mp(rxq)->dev->mtu + 36;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200478
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200479 /*
480 * Make sure that the skb size is a multiple of 8 bytes, as
481 * the lower three bits of the receive descriptor's buffer
482 * size field are ignored by the hardware.
483 */
484 skb_size = (skb_size + 7) & ~7;
485
486 refilled = 0;
487 while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) {
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200488 struct sk_buff *skb;
489 int unaligned;
490 int rx;
491
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200492 skb = dev_alloc_skb(skb_size + dma_get_cache_alignment() - 1);
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200493 if (skb == NULL) {
494 *oom = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 break;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200496 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200497
Ralf Baechle908b6372007-02-26 19:52:06 +0000498 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700499 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000500 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200501
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200502 refilled++;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200503 rxq->rx_desc_count++;
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200504
505 rx = rxq->rx_used_desc++;
506 if (rxq->rx_used_desc == rxq->rx_ring_size)
507 rxq->rx_used_desc = 0;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200508
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200509 rxq->rx_desc_area[rx].buf_ptr = dma_map_single(NULL, skb->data,
510 skb_size, DMA_FROM_DEVICE);
511 rxq->rx_desc_area[rx].buf_size = skb_size;
512 rxq->rx_skb[rx] = skb;
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200513 wmb();
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200514 rxq->rx_desc_area[rx].cmd_sts = BUFFER_OWNED_BY_DMA |
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200515 RX_ENABLE_INTERRUPT;
516 wmb();
517
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200518 /*
519 * The hardware automatically prepends 2 bytes of
520 * dummy data to each received packet, so that the
521 * IP header ends up 16-byte aligned.
522 */
523 skb_reserve(skb, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Lennert Buytenhekde34f222008-06-01 10:07:49 +0200525
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200526 return refilled;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200529static int rxq_process(struct rx_queue *rxq, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200531 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
532 struct net_device_stats *stats = &mp->dev->stats;
533 int rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200535 rx = 0;
Lennert Buytenhek9e1f3772008-08-24 02:33:47 +0200536 while (rx < budget && rxq->rx_desc_count) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200537 struct rx_desc *rx_desc;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200538 unsigned int cmd_sts;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200539 struct sk_buff *skb;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200540
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200541 rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc];
Lennert Buytenhek96587662008-06-01 10:31:56 +0200542
543 cmd_sts = rx_desc->cmd_sts;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200544 if (cmd_sts & BUFFER_OWNED_BY_DMA)
Lennert Buytenhek96587662008-06-01 10:31:56 +0200545 break;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200546 rmb();
547
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200548 skb = rxq->rx_skb[rxq->rx_curr_desc];
549 rxq->rx_skb[rxq->rx_curr_desc] = NULL;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200550
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200551 rxq->rx_curr_desc++;
552 if (rxq->rx_curr_desc == rxq->rx_ring_size)
553 rxq->rx_curr_desc = 0;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200554
Lennert Buytenhek3a499482008-08-24 07:19:48 +0200555 dma_unmap_single(NULL, rx_desc->buf_ptr,
Lennert Buytenhekabe78712008-08-24 03:00:20 +0200556 rx_desc->buf_size, DMA_FROM_DEVICE);
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200557 rxq->rx_desc_count--;
558 rx++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700559
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700560 /*
561 * Update statistics.
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200562 *
563 * Note that the descriptor byte count includes 2 dummy
564 * bytes automatically inserted by the hardware at the
565 * start of the packet (which we don't count), and a 4
566 * byte CRC at the end of the packet (which we do count).
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700567 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 stats->rx_packets++;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200569 stats->rx_bytes += rx_desc->byte_cnt - 2;
Lennert Buytenhek96587662008-06-01 10:31:56 +0200570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /*
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200572 * In case we received a packet without first / last bits
573 * on, or the error summary bit is set, the packet needs
574 * to be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 */
Lennert Buytenhek96587662008-06-01 10:31:56 +0200576 if (((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200577 (RX_FIRST_DESC | RX_LAST_DESC))
Lennert Buytenhek96587662008-06-01 10:31:56 +0200578 || (cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 stats->rx_dropped++;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200580
Lennert Buytenhek96587662008-06-01 10:31:56 +0200581 if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200582 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (net_ratelimit())
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200584 dev_printk(KERN_ERR, &mp->dev->dev,
585 "received packet spanning "
586 "multiple descriptors\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200588
Lennert Buytenhek96587662008-06-01 10:31:56 +0200589 if (cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 stats->rx_errors++;
591
Lennert Buytenhek78fff832008-08-24 01:03:57 +0200592 dev_kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 } else {
594 /*
595 * The -4 is for the CRC in the trailer of the
596 * received packet
597 */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200598 skb_put(skb, rx_desc->byte_cnt - 2 - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Lennert Buytenhek96587662008-06-01 10:31:56 +0200600 if (cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 skb->ip_summed = CHECKSUM_UNNECESSARY;
602 skb->csum = htons(
Lennert Buytenhek96587662008-06-01 10:31:56 +0200603 (cmd_sts & 0x0007fff8) >> 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200605 skb->protocol = eth_type_trans(skb, mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 netif_receive_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200608
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200609 mp->dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200611
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200612 return rx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Lennert Buytenheke5371492008-06-01 02:18:13 +0200615static int mv643xx_eth_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200617 struct mv643xx_eth_private *mp;
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200618 int work_done;
619 int oom;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +0200620 int i;
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200621
622 mp = container_of(napi, struct mv643xx_eth_private, napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Lennert Buytenheke5371492008-06-01 02:18:13 +0200624#ifdef MV643XX_ETH_TX_FAST_REFILL
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (++mp->tx_clean_threshold > 5) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 mp->tx_clean_threshold = 0;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200627 for (i = 0; i < mp->txq_count; i++)
628 txq_reclaim(mp->txq + i, 0);
Lennert Buytenhek4dfc1c82008-07-15 13:34:51 +0200629
630 if (netif_carrier_ok(mp->dev)) {
Lennert Buytenhek8e0b1bf2008-08-24 02:30:42 +0200631 spin_lock_irq(&mp->lock);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200632 __txq_maybe_wake(mp->txq);
Lennert Buytenhek8e0b1bf2008-08-24 02:30:42 +0200633 spin_unlock_irq(&mp->lock);
Lennert Buytenhek4dfc1c82008-07-15 13:34:51 +0200634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
636#endif
637
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200638 work_done = 0;
639 oom = 0;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200640 for (i = mp->rxq_count - 1; work_done < budget && i >= 0; i--) {
641 struct rx_queue *rxq = mp->rxq + i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200643 work_done += rxq_process(rxq, budget - work_done);
644 work_done += rxq_refill(rxq, budget - work_done, &oom);
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200645 }
646
647 if (work_done < budget) {
648 if (oom)
649 mod_timer(&mp->rx_oom, jiffies + (HZ / 10));
Lennert Buytenhek8a578112008-06-01 18:09:35 +0200650 netif_rx_complete(mp->dev, napi);
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +0200651 wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Lennert Buytenhek2257e052008-08-24 04:33:36 +0200654 return work_done;
655}
656
657static inline void oom_timer_wrapper(unsigned long data)
658{
659 struct mv643xx_eth_private *mp = (void *)data;
660
661 napi_schedule(&mp->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200664
665/* tx ***********************************************************************/
Paul Janzenf7ea3332006-01-16 16:52:13 -0700666static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
667{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200668 int frag;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700669
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700670 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200671 skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
672 if (fragp->size <= 8 && fragp->page_offset & 7)
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700673 return 1;
674 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200675
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700676 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700677}
678
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200679static int txq_alloc_desc_index(struct tx_queue *txq)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700680{
681 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700682
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200683 BUG_ON(txq->tx_desc_count >= txq->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700684
Lennert Buytenhek9da78742008-08-23 23:45:28 +0200685 tx_desc_curr = txq->tx_curr_desc++;
686 if (txq->tx_curr_desc == txq->tx_ring_size)
687 txq->tx_curr_desc = 0;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700688
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200689 BUG_ON(txq->tx_curr_desc == txq->tx_used_desc);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700690
691 return tx_desc_curr;
692}
693
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200694static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700695{
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200696 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700697 int frag;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700698
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200699 for (frag = 0; frag < nr_frags; frag++) {
700 skb_frag_t *this_frag;
701 int tx_index;
702 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700703
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200704 this_frag = &skb_shinfo(skb)->frags[frag];
705 tx_index = txq_alloc_desc_index(txq);
706 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700707
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200708 /*
709 * The last fragment will generate an interrupt
710 * which will free the skb on TX completion.
711 */
712 if (frag == nr_frags - 1) {
713 desc->cmd_sts = BUFFER_OWNED_BY_DMA |
714 ZERO_PADDING | TX_LAST_DESC |
715 TX_ENABLE_INTERRUPT;
716 txq->tx_skb[tx_index] = skb;
717 } else {
718 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
719 txq->tx_skb[tx_index] = NULL;
720 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700721
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700722 desc->l4i_chk = 0;
723 desc->byte_cnt = this_frag->size;
724 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
725 this_frag->page_offset,
726 this_frag->size,
727 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700728 }
729}
730
Byron Bradley324ff2c2008-02-04 23:47:15 -0800731static inline __be16 sum16_as_be(__sum16 sum)
732{
733 return (__force __be16)sum;
734}
735
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200736static void txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700737{
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200738 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200739 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700740 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200741 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700742 u32 cmd_sts;
743 int length;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700744
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200745 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700746
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200747 tx_index = txq_alloc_desc_index(txq);
748 desc = &txq->tx_desc_area[tx_index];
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700749
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700750 if (nr_frags) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200751 txq_submit_frag_skb(txq, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700752
753 length = skb_headlen(skb);
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200754 txq->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700755 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200756 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700757 length = skb->len;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200758 txq->tx_skb[tx_index] = skb;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700759 }
760
761 desc->byte_cnt = length;
762 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700763
Patrick McHardy84fa7932006-08-29 16:44:56 -0700764 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200765 int mac_hdr_len;
766
767 BUG_ON(skb->protocol != htons(ETH_P_IP) &&
768 skb->protocol != htons(ETH_P_8021Q));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700769
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200770 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
771 GEN_IP_V4_CHECKSUM |
772 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700773
Lennert Buytenheke32b6612008-07-24 06:22:59 +0200774 mac_hdr_len = (void *)ip_hdr(skb) - (void *)skb->data;
775 switch (mac_hdr_len - ETH_HLEN) {
776 case 0:
777 break;
778 case 4:
779 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
780 break;
781 case 8:
782 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
783 break;
784 case 12:
785 cmd_sts |= MAC_HDR_EXTRA_4_BYTES;
786 cmd_sts |= MAC_HDR_EXTRA_8_BYTES;
787 break;
788 default:
789 if (net_ratelimit())
790 dev_printk(KERN_ERR, &txq_to_mp(txq)->dev->dev,
791 "mac header length is %d?!\n", mac_hdr_len);
792 break;
793 }
794
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700795 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700796 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200797 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800798 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700799 break;
800 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800801 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700802 break;
803 default:
804 BUG();
805 }
806 } else {
807 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200808 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700809 desc->l4i_chk = 0;
810 }
811
812 /* ensure all other descriptors are written before first cmd_sts */
813 wmb();
814 desc->cmd_sts = cmd_sts;
815
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +0200816 /* clear TX_END interrupt status */
817 wrl(mp, INT_CAUSE(mp->port_num), ~(INT_TX_END_0 << txq->index));
818 rdl(mp, INT_CAUSE(mp->port_num));
819
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700820 /* ensure all descriptors are written before poking hardware */
821 wmb();
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200822 txq_enable(txq);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700823
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200824 txq->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700825}
826
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200827static int mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828{
Lennert Buytenheke5371492008-06-01 02:18:13 +0200829 struct mv643xx_eth_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700830 struct net_device_stats *stats = &dev->stats;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200831 struct tx_queue *txq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700834 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
835 stats->tx_dropped++;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +0200836 dev_printk(KERN_DEBUG, &dev->dev,
837 "failed to linearize skb with tiny "
838 "unaligned fragment\n");
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700839 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 spin_lock_irqsave(&mp->lock, flags);
843
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200844 txq = mp->txq;
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200845
Lennert Buytenhek17cd0a52008-08-24 05:33:55 +0200846 if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) {
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700847 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200848 if (txq->index == 0 && net_ratelimit())
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200849 dev_printk(KERN_ERR, &dev->dev,
850 "primary tx queue full?!\n");
851 kfree_skb(skb);
852 return NETDEV_TX_OK;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700853 }
854
Lennert Buytenhek13d64282008-06-01 20:51:22 +0200855 txq_submit_skb(txq, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700856 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 stats->tx_packets++;
858 dev->trans_start = jiffies;
859
Lennert Buytenhekf7981c12008-08-26 10:23:22 +0200860 if (txq->index == 0) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200861 int entries_left;
862
863 entries_left = txq->tx_ring_size - txq->tx_desc_count;
Lennert Buytenhek17cd0a52008-08-24 05:33:55 +0200864 if (entries_left < MAX_SKB_FRAGS + 1)
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200865 netif_stop_queue(dev);
866 }
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 spin_unlock_irqrestore(&mp->lock, flags);
869
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700870 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871}
872
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200873
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200874/* tx rate control **********************************************************/
875/*
876 * Set total maximum TX rate (shared by all TX queues for this port)
877 * to 'rate' bits per second, with a maximum burst of 'burst' bytes.
878 */
879static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst)
880{
881 int token_rate;
882 int mtu;
883 int bucket_size;
884
885 token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
886 if (token_rate > 1023)
887 token_rate = 1023;
888
889 mtu = (mp->dev->mtu + 255) >> 8;
890 if (mtu > 63)
891 mtu = 63;
892
893 bucket_size = (burst + 255) >> 8;
894 if (bucket_size > 65535)
895 bucket_size = 65535;
896
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200897 if (mp->shared->tx_bw_control_moved) {
898 wrl(mp, TX_BW_RATE_MOVED(mp->port_num), token_rate);
899 wrl(mp, TX_BW_MTU_MOVED(mp->port_num), mtu);
900 wrl(mp, TX_BW_BURST_MOVED(mp->port_num), bucket_size);
901 } else {
902 wrl(mp, TX_BW_RATE(mp->port_num), token_rate);
903 wrl(mp, TX_BW_MTU(mp->port_num), mtu);
904 wrl(mp, TX_BW_BURST(mp->port_num), bucket_size);
905 }
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200906}
907
908static void txq_set_rate(struct tx_queue *txq, int rate, int burst)
909{
910 struct mv643xx_eth_private *mp = txq_to_mp(txq);
911 int token_rate;
912 int bucket_size;
913
914 token_rate = ((rate / 1000) * 64) / (mp->shared->t_clk / 1000);
915 if (token_rate > 1023)
916 token_rate = 1023;
917
918 bucket_size = (burst + 255) >> 8;
919 if (bucket_size > 65535)
920 bucket_size = 65535;
921
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200922 wrl(mp, TXQ_BW_TOKENS(mp->port_num, txq->index), token_rate << 14);
923 wrl(mp, TXQ_BW_CONF(mp->port_num, txq->index),
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200924 (bucket_size << 10) | token_rate);
925}
926
927static void txq_set_fixed_prio_mode(struct tx_queue *txq)
928{
929 struct mv643xx_eth_private *mp = txq_to_mp(txq);
930 int off;
931 u32 val;
932
933 /*
934 * Turn on fixed priority mode.
935 */
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200936 if (mp->shared->tx_bw_control_moved)
937 off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
938 else
939 off = TXQ_FIX_PRIO_CONF(mp->port_num);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200940
941 val = rdl(mp, off);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200942 val |= 1 << txq->index;
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200943 wrl(mp, off, val);
944}
945
946static void txq_set_wrr(struct tx_queue *txq, int weight)
947{
948 struct mv643xx_eth_private *mp = txq_to_mp(txq);
949 int off;
950 u32 val;
951
952 /*
953 * Turn off fixed priority mode.
954 */
Lennert Buytenhek1e881592008-06-02 01:57:36 +0200955 if (mp->shared->tx_bw_control_moved)
956 off = TXQ_FIX_PRIO_CONF_MOVED(mp->port_num);
957 else
958 off = TXQ_FIX_PRIO_CONF(mp->port_num);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200959
960 val = rdl(mp, off);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200961 val &= ~(1 << txq->index);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200962 wrl(mp, off, val);
963
964 /*
965 * Configure WRR weight for this queue.
966 */
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +0200967 off = TXQ_BW_WRR_CONF(mp->port_num, txq->index);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +0200968
969 val = rdl(mp, off);
970 val = (val & ~0xff) | (weight & 0xff);
971 wrl(mp, off, val);
972}
973
974
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200975/* mii management interface *************************************************/
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200976static irqreturn_t mv643xx_eth_err_irq(int irq, void *dev_id)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200977{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200978 struct mv643xx_eth_shared_private *msp = dev_id;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200979
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200980 if (readl(msp->base + ERR_INT_CAUSE) & ERR_INT_SMI_DONE) {
981 writel(~ERR_INT_SMI_DONE, msp->base + ERR_INT_CAUSE);
982 wake_up(&msp->smi_busy_wait);
983 return IRQ_HANDLED;
984 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200985
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +0200986 return IRQ_NONE;
987}
988
989static int smi_is_done(struct mv643xx_eth_shared_private *msp)
990{
991 return !(readl(msp->base + SMI_REG) & SMI_BUSY);
992}
993
994static int smi_wait_ready(struct mv643xx_eth_shared_private *msp)
995{
996 if (msp->err_interrupt == NO_IRQ) {
997 int i;
998
999 for (i = 0; !smi_is_done(msp); i++) {
1000 if (i == 10)
1001 return -ETIMEDOUT;
1002 msleep(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001003 }
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001004
1005 return 0;
1006 }
1007
1008 if (!wait_event_timeout(msp->smi_busy_wait, smi_is_done(msp),
1009 msecs_to_jiffies(100)))
1010 return -ETIMEDOUT;
1011
1012 return 0;
1013}
1014
1015static int smi_reg_read(struct mv643xx_eth_private *mp,
1016 unsigned int addr, unsigned int reg)
1017{
1018 struct mv643xx_eth_shared_private *msp = mp->shared_smi;
1019 void __iomem *smi_reg = msp->base + SMI_REG;
1020 int ret;
1021
1022 mutex_lock(&msp->phy_lock);
1023
1024 if (smi_wait_ready(msp)) {
1025 printk("%s: SMI bus busy timeout\n", mp->dev->name);
1026 ret = -ETIMEDOUT;
1027 goto out;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001028 }
1029
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001030 writel(SMI_OPCODE_READ | (reg << 21) | (addr << 16), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001031
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001032 if (smi_wait_ready(msp)) {
1033 printk("%s: SMI bus busy timeout\n", mp->dev->name);
1034 ret = -ETIMEDOUT;
1035 goto out;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001036 }
1037
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001038 ret = readl(smi_reg);
1039 if (!(ret & SMI_READ_VALID)) {
1040 printk("%s: SMI bus read not valid\n", mp->dev->name);
1041 ret = -ENODEV;
1042 goto out;
1043 }
1044
1045 ret &= 0xffff;
1046
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001047out:
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001048 mutex_unlock(&msp->phy_lock);
1049
1050 return ret;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001051}
1052
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001053static int smi_reg_write(struct mv643xx_eth_private *mp, unsigned int addr,
1054 unsigned int reg, unsigned int value)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001055{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001056 struct mv643xx_eth_shared_private *msp = mp->shared_smi;
1057 void __iomem *smi_reg = msp->base + SMI_REG;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001058
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001059 mutex_lock(&msp->phy_lock);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001060
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001061 if (smi_wait_ready(msp)) {
1062 printk("%s: SMI bus busy timeout\n", mp->dev->name);
1063 mutex_unlock(&msp->phy_lock);
1064 return -ETIMEDOUT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001065 }
1066
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001067 writel(SMI_OPCODE_WRITE | (reg << 21) |
1068 (addr << 16) | (value & 0xffff), smi_reg);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001069
1070 mutex_unlock(&msp->phy_lock);
1071
1072 return 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001073}
1074
1075
1076/* mib counters *************************************************************/
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001077static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001078{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001079 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001080}
1081
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001082static void mib_counters_clear(struct mv643xx_eth_private *mp)
1083{
1084 int i;
1085
1086 for (i = 0; i < 0x80; i += 4)
1087 mib_read(mp, i);
1088}
1089
1090static void mib_counters_update(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001091{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001092 struct mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001093
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001094 p->good_octets_received += mib_read(mp, 0x00);
1095 p->good_octets_received += (u64)mib_read(mp, 0x04) << 32;
1096 p->bad_octets_received += mib_read(mp, 0x08);
1097 p->internal_mac_transmit_err += mib_read(mp, 0x0c);
1098 p->good_frames_received += mib_read(mp, 0x10);
1099 p->bad_frames_received += mib_read(mp, 0x14);
1100 p->broadcast_frames_received += mib_read(mp, 0x18);
1101 p->multicast_frames_received += mib_read(mp, 0x1c);
1102 p->frames_64_octets += mib_read(mp, 0x20);
1103 p->frames_65_to_127_octets += mib_read(mp, 0x24);
1104 p->frames_128_to_255_octets += mib_read(mp, 0x28);
1105 p->frames_256_to_511_octets += mib_read(mp, 0x2c);
1106 p->frames_512_to_1023_octets += mib_read(mp, 0x30);
1107 p->frames_1024_to_max_octets += mib_read(mp, 0x34);
1108 p->good_octets_sent += mib_read(mp, 0x38);
1109 p->good_octets_sent += (u64)mib_read(mp, 0x3c) << 32;
1110 p->good_frames_sent += mib_read(mp, 0x40);
1111 p->excessive_collision += mib_read(mp, 0x44);
1112 p->multicast_frames_sent += mib_read(mp, 0x48);
1113 p->broadcast_frames_sent += mib_read(mp, 0x4c);
1114 p->unrec_mac_control_received += mib_read(mp, 0x50);
1115 p->fc_sent += mib_read(mp, 0x54);
1116 p->good_fc_received += mib_read(mp, 0x58);
1117 p->bad_fc_received += mib_read(mp, 0x5c);
1118 p->undersize_received += mib_read(mp, 0x60);
1119 p->fragments_received += mib_read(mp, 0x64);
1120 p->oversize_received += mib_read(mp, 0x68);
1121 p->jabber_received += mib_read(mp, 0x6c);
1122 p->mac_receive_error += mib_read(mp, 0x70);
1123 p->bad_crc_event += mib_read(mp, 0x74);
1124 p->collision += mib_read(mp, 0x78);
1125 p->late_collision += mib_read(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001126}
1127
1128
1129/* ethtool ******************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02001130struct mv643xx_eth_stats {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001131 char stat_string[ETH_GSTRING_LEN];
1132 int sizeof_stat;
Lennert Buytenhek16820052008-06-01 11:40:29 +02001133 int netdev_off;
1134 int mp_off;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001135};
1136
Lennert Buytenhek16820052008-06-01 11:40:29 +02001137#define SSTAT(m) \
1138 { #m, FIELD_SIZEOF(struct net_device_stats, m), \
1139 offsetof(struct net_device, stats.m), -1 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001140
Lennert Buytenhek16820052008-06-01 11:40:29 +02001141#define MIBSTAT(m) \
1142 { #m, FIELD_SIZEOF(struct mib_counters, m), \
1143 -1, offsetof(struct mv643xx_eth_private, mib_counters.m) }
1144
1145static const struct mv643xx_eth_stats mv643xx_eth_stats[] = {
1146 SSTAT(rx_packets),
1147 SSTAT(tx_packets),
1148 SSTAT(rx_bytes),
1149 SSTAT(tx_bytes),
1150 SSTAT(rx_errors),
1151 SSTAT(tx_errors),
1152 SSTAT(rx_dropped),
1153 SSTAT(tx_dropped),
1154 MIBSTAT(good_octets_received),
1155 MIBSTAT(bad_octets_received),
1156 MIBSTAT(internal_mac_transmit_err),
1157 MIBSTAT(good_frames_received),
1158 MIBSTAT(bad_frames_received),
1159 MIBSTAT(broadcast_frames_received),
1160 MIBSTAT(multicast_frames_received),
1161 MIBSTAT(frames_64_octets),
1162 MIBSTAT(frames_65_to_127_octets),
1163 MIBSTAT(frames_128_to_255_octets),
1164 MIBSTAT(frames_256_to_511_octets),
1165 MIBSTAT(frames_512_to_1023_octets),
1166 MIBSTAT(frames_1024_to_max_octets),
1167 MIBSTAT(good_octets_sent),
1168 MIBSTAT(good_frames_sent),
1169 MIBSTAT(excessive_collision),
1170 MIBSTAT(multicast_frames_sent),
1171 MIBSTAT(broadcast_frames_sent),
1172 MIBSTAT(unrec_mac_control_received),
1173 MIBSTAT(fc_sent),
1174 MIBSTAT(good_fc_received),
1175 MIBSTAT(bad_fc_received),
1176 MIBSTAT(undersize_received),
1177 MIBSTAT(fragments_received),
1178 MIBSTAT(oversize_received),
1179 MIBSTAT(jabber_received),
1180 MIBSTAT(mac_receive_error),
1181 MIBSTAT(bad_crc_event),
1182 MIBSTAT(collision),
1183 MIBSTAT(late_collision),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001184};
1185
Lennert Buytenheke5371492008-06-01 02:18:13 +02001186static int mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001187{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001188 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001189 int err;
1190
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001191 err = mii_ethtool_gset(&mp->mii, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001192
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001193 /*
1194 * The MAC does not support 1000baseT_Half.
1195 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001196 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1197 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1198
1199 return err;
1200}
1201
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001202static int mv643xx_eth_get_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
1203{
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001204 struct mv643xx_eth_private *mp = netdev_priv(dev);
1205 u32 port_status;
1206
1207 port_status = rdl(mp, PORT_STATUS(mp->port_num));
1208
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001209 cmd->supported = SUPPORTED_MII;
1210 cmd->advertising = ADVERTISED_MII;
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001211 switch (port_status & PORT_SPEED_MASK) {
1212 case PORT_SPEED_10:
1213 cmd->speed = SPEED_10;
1214 break;
1215 case PORT_SPEED_100:
1216 cmd->speed = SPEED_100;
1217 break;
1218 case PORT_SPEED_1000:
1219 cmd->speed = SPEED_1000;
1220 break;
1221 default:
1222 cmd->speed = -1;
1223 break;
1224 }
1225 cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001226 cmd->port = PORT_MII;
1227 cmd->phy_address = 0;
1228 cmd->transceiver = XCVR_INTERNAL;
1229 cmd->autoneg = AUTONEG_DISABLE;
1230 cmd->maxtxpkt = 1;
1231 cmd->maxrxpkt = 1;
1232
1233 return 0;
1234}
1235
Lennert Buytenheke5371492008-06-01 02:18:13 +02001236static int mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001237{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001238 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001239
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001240 /*
1241 * The MAC does not support 1000baseT_Half.
1242 */
1243 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1244
Lennert Buytenhek2b3ba0e2008-08-24 05:41:09 +02001245 return mii_ethtool_sset(&mp->mii, cmd);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001246}
1247
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001248static int mv643xx_eth_set_settings_phyless(struct net_device *dev, struct ethtool_cmd *cmd)
1249{
1250 return -EINVAL;
1251}
1252
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001253static void mv643xx_eth_get_drvinfo(struct net_device *dev,
1254 struct ethtool_drvinfo *drvinfo)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001255{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001256 strncpy(drvinfo->driver, mv643xx_eth_driver_name, 32);
1257 strncpy(drvinfo->version, mv643xx_eth_driver_version, 32);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001258 strncpy(drvinfo->fw_version, "N/A", 32);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001259 strncpy(drvinfo->bus_info, "platform", 32);
Lennert Buytenhek16820052008-06-01 11:40:29 +02001260 drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001261}
1262
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001263static int mv643xx_eth_nway_reset(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001264{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001265 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001266
1267 return mii_nway_restart(&mp->mii);
1268}
1269
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001270static int mv643xx_eth_nway_reset_phyless(struct net_device *dev)
1271{
1272 return -EINVAL;
1273}
1274
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001275static u32 mv643xx_eth_get_link(struct net_device *dev)
1276{
Lennert Buytenheke5371492008-06-01 02:18:13 +02001277 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001278
1279 return mii_link_ok(&mp->mii);
1280}
1281
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001282static u32 mv643xx_eth_get_link_phyless(struct net_device *dev)
1283{
1284 return 1;
1285}
1286
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001287static void mv643xx_eth_get_strings(struct net_device *dev,
1288 uint32_t stringset, uint8_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001289{
1290 int i;
1291
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001292 if (stringset == ETH_SS_STATS) {
1293 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001294 memcpy(data + i * ETH_GSTRING_LEN,
Lennert Buytenhek16820052008-06-01 11:40:29 +02001295 mv643xx_eth_stats[i].stat_string,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001296 ETH_GSTRING_LEN);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001297 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001298 }
1299}
1300
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001301static void mv643xx_eth_get_ethtool_stats(struct net_device *dev,
1302 struct ethtool_stats *stats,
1303 uint64_t *data)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001304{
Lennert Buytenhekb9873842008-08-24 05:59:16 +02001305 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001306 int i;
1307
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001308 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001309
Lennert Buytenhek16820052008-06-01 11:40:29 +02001310 for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) {
1311 const struct mv643xx_eth_stats *stat;
1312 void *p;
1313
1314 stat = mv643xx_eth_stats + i;
1315
1316 if (stat->netdev_off >= 0)
1317 p = ((void *)mp->dev) + stat->netdev_off;
1318 else
1319 p = ((void *)mp) + stat->mp_off;
1320
1321 data[i] = (stat->sizeof_stat == 8) ?
1322 *(uint64_t *)p : *(uint32_t *)p;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001323 }
1324}
1325
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001326static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001327{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001328 if (sset == ETH_SS_STATS)
Lennert Buytenhek16820052008-06-01 11:40:29 +02001329 return ARRAY_SIZE(mv643xx_eth_stats);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001330
1331 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001332}
1333
Lennert Buytenheke5371492008-06-01 02:18:13 +02001334static const struct ethtool_ops mv643xx_eth_ethtool_ops = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001335 .get_settings = mv643xx_eth_get_settings,
1336 .set_settings = mv643xx_eth_set_settings,
1337 .get_drvinfo = mv643xx_eth_get_drvinfo,
1338 .nway_reset = mv643xx_eth_nway_reset,
1339 .get_link = mv643xx_eth_get_link,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001340 .set_sg = ethtool_op_set_sg,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001341 .get_strings = mv643xx_eth_get_strings,
1342 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
Lennert Buytenheke5371492008-06-01 02:18:13 +02001343 .get_sset_count = mv643xx_eth_get_sset_count,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001344};
1345
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001346static const struct ethtool_ops mv643xx_eth_ethtool_ops_phyless = {
1347 .get_settings = mv643xx_eth_get_settings_phyless,
1348 .set_settings = mv643xx_eth_set_settings_phyless,
1349 .get_drvinfo = mv643xx_eth_get_drvinfo,
1350 .nway_reset = mv643xx_eth_nway_reset_phyless,
1351 .get_link = mv643xx_eth_get_link_phyless,
1352 .set_sg = ethtool_op_set_sg,
1353 .get_strings = mv643xx_eth_get_strings,
1354 .get_ethtool_stats = mv643xx_eth_get_ethtool_stats,
1355 .get_sset_count = mv643xx_eth_get_sset_count,
1356};
1357
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001358
1359/* address handling *********************************************************/
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001360static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001361{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001362 unsigned int mac_h;
1363 unsigned int mac_l;
1364
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001365 mac_h = rdl(mp, MAC_ADDR_HIGH(mp->port_num));
1366 mac_l = rdl(mp, MAC_ADDR_LOW(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001367
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001368 addr[0] = (mac_h >> 24) & 0xff;
1369 addr[1] = (mac_h >> 16) & 0xff;
1370 addr[2] = (mac_h >> 8) & 0xff;
1371 addr[3] = mac_h & 0xff;
1372 addr[4] = (mac_l >> 8) & 0xff;
1373 addr[5] = mac_l & 0xff;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001374}
1375
Lennert Buytenheke5371492008-06-01 02:18:13 +02001376static void init_mac_tables(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001377{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001378 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001379
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001380 for (i = 0; i < 0x100; i += 4) {
1381 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
1382 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001383 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001384
1385 for (i = 0; i < 0x10; i += 4)
1386 wrl(mp, UNICAST_TABLE(mp->port_num) + i, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001387}
1388
Lennert Buytenheke5371492008-06-01 02:18:13 +02001389static void set_filter_table_entry(struct mv643xx_eth_private *mp,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001390 int table, unsigned char entry)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001391{
1392 unsigned int table_reg;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001393
1394 /* Set "accepts frame bit" at specified table entry */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001395 table_reg = rdl(mp, table + (entry & 0xfc));
1396 table_reg |= 0x01 << (8 * (entry & 3));
1397 wrl(mp, table + (entry & 0xfc), table_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001398}
1399
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001400static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001401{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001402 unsigned int mac_h;
1403 unsigned int mac_l;
1404 int table;
1405
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001406 mac_l = (addr[4] << 8) | addr[5];
1407 mac_h = (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001408
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001409 wrl(mp, MAC_ADDR_LOW(mp->port_num), mac_l);
1410 wrl(mp, MAC_ADDR_HIGH(mp->port_num), mac_h);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001411
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001412 table = UNICAST_TABLE(mp->port_num);
Lennert Buytenhek5daffe92008-06-01 02:52:41 +02001413 set_filter_table_entry(mp, table, addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001414}
1415
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001416static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1417{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001418 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001419
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001420 /* +2 is for the offset of the HW addr type */
1421 memcpy(dev->dev_addr, addr + 2, 6);
1422
1423 init_mac_tables(mp);
1424 uc_addr_set(mp, dev->dev_addr);
1425
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001426 return 0;
1427}
1428
Lennert Buytenhek69876562008-06-01 11:43:32 +02001429static int addr_crc(unsigned char *addr)
1430{
1431 int crc = 0;
1432 int i;
1433
1434 for (i = 0; i < 6; i++) {
1435 int j;
1436
1437 crc = (crc ^ addr[i]) << 8;
1438 for (j = 7; j >= 0; j--) {
1439 if (crc & (0x100 << j))
1440 crc ^= 0x107 << j;
1441 }
1442 }
1443
1444 return crc;
1445}
1446
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001447static void mv643xx_eth_set_rx_mode(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001448{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001449 struct mv643xx_eth_private *mp = netdev_priv(dev);
1450 u32 port_config;
1451 struct dev_addr_list *addr;
1452 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001453
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001454 port_config = rdl(mp, PORT_CONFIG(mp->port_num));
1455 if (dev->flags & IFF_PROMISC)
1456 port_config |= UNICAST_PROMISCUOUS_MODE;
1457 else
1458 port_config &= ~UNICAST_PROMISCUOUS_MODE;
1459 wrl(mp, PORT_CONFIG(mp->port_num), port_config);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001460
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001461 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
1462 int port_num = mp->port_num;
1463 u32 accept = 0x01010101;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001464
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001465 for (i = 0; i < 0x100; i += 4) {
1466 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept);
1467 wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001468 }
1469 return;
1470 }
1471
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001472 for (i = 0; i < 0x100; i += 4) {
1473 wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, 0);
1474 wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001475 }
1476
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001477 for (addr = dev->mc_list; addr != NULL; addr = addr->next) {
1478 u8 *a = addr->da_addr;
1479 int table;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001480
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001481 if (addr->da_addrlen != 6)
1482 continue;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001483
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001484 if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) {
1485 table = SPECIAL_MCAST_TABLE(mp->port_num);
1486 set_filter_table_entry(mp, table, a[5]);
1487 } else {
1488 int crc = addr_crc(a);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001489
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001490 table = OTHER_MCAST_TABLE(mp->port_num);
1491 set_filter_table_entry(mp, table, crc);
1492 }
1493 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001494}
1495
1496
1497/* rx/tx queue initialisation ***********************************************/
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001498static int rxq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001499{
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001500 struct rx_queue *rxq = mp->rxq + index;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001501 struct rx_desc *rx_desc;
1502 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001503 int i;
1504
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001505 rxq->index = index;
1506
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001507 rxq->rx_ring_size = mp->default_rx_ring_size;
1508
1509 rxq->rx_desc_count = 0;
1510 rxq->rx_curr_desc = 0;
1511 rxq->rx_used_desc = 0;
1512
1513 size = rxq->rx_ring_size * sizeof(struct rx_desc);
1514
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001515 if (index == 0 && size <= mp->rx_desc_sram_size) {
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001516 rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr,
1517 mp->rx_desc_sram_size);
1518 rxq->rx_desc_dma = mp->rx_desc_sram_addr;
1519 } else {
1520 rxq->rx_desc_area = dma_alloc_coherent(NULL, size,
1521 &rxq->rx_desc_dma,
1522 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001523 }
1524
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001525 if (rxq->rx_desc_area == NULL) {
1526 dev_printk(KERN_ERR, &mp->dev->dev,
1527 "can't allocate rx ring (%d bytes)\n", size);
1528 goto out;
1529 }
1530 memset(rxq->rx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001531
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001532 rxq->rx_desc_area_size = size;
1533 rxq->rx_skb = kmalloc(rxq->rx_ring_size * sizeof(*rxq->rx_skb),
1534 GFP_KERNEL);
1535 if (rxq->rx_skb == NULL) {
1536 dev_printk(KERN_ERR, &mp->dev->dev,
1537 "can't allocate rx skb ring\n");
1538 goto out_free;
1539 }
1540
1541 rx_desc = (struct rx_desc *)rxq->rx_desc_area;
1542 for (i = 0; i < rxq->rx_ring_size; i++) {
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001543 int nexti;
1544
1545 nexti = i + 1;
1546 if (nexti == rxq->rx_ring_size)
1547 nexti = 0;
1548
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001549 rx_desc[i].next_desc_ptr = rxq->rx_desc_dma +
1550 nexti * sizeof(struct rx_desc);
1551 }
1552
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001553 return 0;
1554
1555
1556out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001557 if (index == 0 && size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001558 iounmap(rxq->rx_desc_area);
1559 else
1560 dma_free_coherent(NULL, size,
1561 rxq->rx_desc_area,
1562 rxq->rx_desc_dma);
1563
1564out:
1565 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001566}
1567
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001568static void rxq_deinit(struct rx_queue *rxq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001569{
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001570 struct mv643xx_eth_private *mp = rxq_to_mp(rxq);
1571 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001572
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001573 rxq_disable(rxq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001574
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001575 for (i = 0; i < rxq->rx_ring_size; i++) {
1576 if (rxq->rx_skb[i]) {
1577 dev_kfree_skb(rxq->rx_skb[i]);
1578 rxq->rx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001579 }
1580 }
1581
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001582 if (rxq->rx_desc_count) {
1583 dev_printk(KERN_ERR, &mp->dev->dev,
1584 "error freeing rx ring -- %d skbs stuck\n",
1585 rxq->rx_desc_count);
1586 }
1587
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001588 if (rxq->index == 0 &&
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001589 rxq->rx_desc_area_size <= mp->rx_desc_sram_size)
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001590 iounmap(rxq->rx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001591 else
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001592 dma_free_coherent(NULL, rxq->rx_desc_area_size,
1593 rxq->rx_desc_area, rxq->rx_desc_dma);
1594
1595 kfree(rxq->rx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001596}
1597
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001598static int txq_init(struct mv643xx_eth_private *mp, int index)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001599{
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001600 struct tx_queue *txq = mp->txq + index;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001601 struct tx_desc *tx_desc;
1602 int size;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001603 int i;
1604
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001605 txq->index = index;
1606
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001607 txq->tx_ring_size = mp->default_tx_ring_size;
1608
1609 txq->tx_desc_count = 0;
1610 txq->tx_curr_desc = 0;
1611 txq->tx_used_desc = 0;
1612
1613 size = txq->tx_ring_size * sizeof(struct tx_desc);
1614
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001615 if (index == 0 && size <= mp->tx_desc_sram_size) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001616 txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr,
1617 mp->tx_desc_sram_size);
1618 txq->tx_desc_dma = mp->tx_desc_sram_addr;
1619 } else {
1620 txq->tx_desc_area = dma_alloc_coherent(NULL, size,
1621 &txq->tx_desc_dma,
1622 GFP_KERNEL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001623 }
1624
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001625 if (txq->tx_desc_area == NULL) {
1626 dev_printk(KERN_ERR, &mp->dev->dev,
1627 "can't allocate tx ring (%d bytes)\n", size);
1628 goto out;
1629 }
1630 memset(txq->tx_desc_area, 0, size);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001631
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001632 txq->tx_desc_area_size = size;
1633 txq->tx_skb = kmalloc(txq->tx_ring_size * sizeof(*txq->tx_skb),
1634 GFP_KERNEL);
1635 if (txq->tx_skb == NULL) {
1636 dev_printk(KERN_ERR, &mp->dev->dev,
1637 "can't allocate tx skb ring\n");
1638 goto out_free;
1639 }
1640
1641 tx_desc = (struct tx_desc *)txq->tx_desc_area;
1642 for (i = 0; i < txq->tx_ring_size; i++) {
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001643 struct tx_desc *txd = tx_desc + i;
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001644 int nexti;
1645
1646 nexti = i + 1;
1647 if (nexti == txq->tx_ring_size)
1648 nexti = 0;
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001649
1650 txd->cmd_sts = 0;
1651 txd->next_desc_ptr = txq->tx_desc_dma +
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001652 nexti * sizeof(struct tx_desc);
1653 }
1654
1655 return 0;
1656
1657
1658out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001659 if (index == 0 && size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001660 iounmap(txq->tx_desc_area);
1661 else
1662 dma_free_coherent(NULL, size,
1663 txq->tx_desc_area,
1664 txq->tx_desc_dma);
1665
1666out:
1667 return -ENOMEM;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001668}
1669
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001670static void txq_reclaim(struct tx_queue *txq, int force)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001671{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001672 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001673 unsigned long flags;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001674
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001675 spin_lock_irqsave(&mp->lock, flags);
1676 while (txq->tx_desc_count > 0) {
1677 int tx_index;
1678 struct tx_desc *desc;
1679 u32 cmd_sts;
1680 struct sk_buff *skb;
1681 dma_addr_t addr;
1682 int count;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001683
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001684 tx_index = txq->tx_used_desc;
1685 desc = &txq->tx_desc_area[tx_index];
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001686 cmd_sts = desc->cmd_sts;
1687
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001688 if (cmd_sts & BUFFER_OWNED_BY_DMA) {
1689 if (!force)
1690 break;
1691 desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA;
1692 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001693
Lennert Buytenhek9da78742008-08-23 23:45:28 +02001694 txq->tx_used_desc = tx_index + 1;
1695 if (txq->tx_used_desc == txq->tx_ring_size)
1696 txq->tx_used_desc = 0;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001697 txq->tx_desc_count--;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001698
1699 addr = desc->buf_ptr;
1700 count = desc->byte_cnt;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001701 skb = txq->tx_skb[tx_index];
1702 txq->tx_skb[tx_index] = NULL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001703
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001704 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001705 dev_printk(KERN_INFO, &mp->dev->dev, "tx error\n");
1706 mp->dev->stats.tx_errors++;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001707 }
1708
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001709 /*
1710 * Drop mp->lock while we free the skb.
1711 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001712 spin_unlock_irqrestore(&mp->lock, flags);
1713
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001714 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001715 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1716 else
1717 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1718
1719 if (skb)
1720 dev_kfree_skb_irq(skb);
1721
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001722 spin_lock_irqsave(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001723 }
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001724 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001725}
1726
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001727static void txq_deinit(struct tx_queue *txq)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001728{
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001729 struct mv643xx_eth_private *mp = txq_to_mp(txq);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001730
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001731 txq_disable(txq);
1732 txq_reclaim(txq, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001733
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001734 BUG_ON(txq->tx_used_desc != txq->tx_curr_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001735
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001736 if (txq->index == 0 &&
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001737 txq->tx_desc_area_size <= mp->tx_desc_sram_size)
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001738 iounmap(txq->tx_desc_area);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001739 else
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001740 dma_free_coherent(NULL, txq->tx_desc_area_size,
1741 txq->tx_desc_area, txq->tx_desc_dma);
1742
1743 kfree(txq->tx_skb);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001744}
1745
1746
1747/* netdev ops and related ***************************************************/
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001748static void handle_link_event(struct mv643xx_eth_private *mp)
1749{
1750 struct net_device *dev = mp->dev;
1751 u32 port_status;
1752 int speed;
1753 int duplex;
1754 int fc;
1755
1756 port_status = rdl(mp, PORT_STATUS(mp->port_num));
1757 if (!(port_status & LINK_UP)) {
1758 if (netif_carrier_ok(dev)) {
1759 int i;
1760
1761 printk(KERN_INFO "%s: link down\n", dev->name);
1762
1763 netif_carrier_off(dev);
1764 netif_stop_queue(dev);
1765
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001766 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001767 struct tx_queue *txq = mp->txq + i;
1768
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001769 txq_reclaim(txq, 1);
1770 txq_reset_hw_ptr(txq);
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001771 }
1772 }
1773 return;
1774 }
1775
1776 switch (port_status & PORT_SPEED_MASK) {
1777 case PORT_SPEED_10:
1778 speed = 10;
1779 break;
1780 case PORT_SPEED_100:
1781 speed = 100;
1782 break;
1783 case PORT_SPEED_1000:
1784 speed = 1000;
1785 break;
1786 default:
1787 speed = -1;
1788 break;
1789 }
1790 duplex = (port_status & FULL_DUPLEX) ? 1 : 0;
1791 fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0;
1792
1793 printk(KERN_INFO "%s: link up, %d Mb/s, %s duplex, "
1794 "flow control %sabled\n", dev->name,
1795 speed, duplex ? "full" : "half",
1796 fc ? "en" : "dis");
1797
1798 if (!netif_carrier_ok(dev)) {
1799 netif_carrier_on(dev);
1800 netif_wake_queue(dev);
1801 }
1802}
1803
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001804static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001805{
1806 struct net_device *dev = (struct net_device *)dev_id;
Lennert Buytenheke5371492008-06-01 02:18:13 +02001807 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001808 u32 int_cause;
1809 u32 int_cause_ext;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001810
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001811 int_cause = rdl(mp, INT_CAUSE(mp->port_num)) &
1812 (INT_TX_END | INT_RX | INT_EXT);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001813 if (int_cause == 0)
1814 return IRQ_NONE;
1815
1816 int_cause_ext = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001817 if (int_cause & INT_EXT) {
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001818 int_cause_ext = rdl(mp, INT_CAUSE_EXT(mp->port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001819 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001820 wrl(mp, INT_CAUSE_EXT(mp->port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001821 }
1822
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02001823 if (int_cause_ext & (INT_EXT_PHY | INT_EXT_LINK))
1824 handle_link_event(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001825
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001826 /*
1827 * RxBuffer or RxError set for any of the 8 queues?
1828 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001829 if (int_cause & INT_RX) {
Lennert Buytenhek819ddca2008-08-24 02:45:32 +02001830 wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_RX));
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001831 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001832 rdl(mp, INT_MASK(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001833
Lennert Buytenhek2257e052008-08-24 04:33:36 +02001834 napi_schedule(&mp->napi);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001835 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001836
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001837 /*
1838 * TxBuffer or TxError set for any of the 8 queues?
1839 */
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001840 if (int_cause_ext & INT_EXT_TX) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001841 int i;
1842
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001843 for (i = 0; i < mp->txq_count; i++)
1844 txq_reclaim(mp->txq + i, 0);
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001845
1846 /*
1847 * Enough space again in the primary TX queue for a
1848 * full packet?
1849 */
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001850 if (netif_carrier_ok(dev)) {
1851 spin_lock(&mp->lock);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001852 __txq_maybe_wake(mp->txq);
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001853 spin_unlock(&mp->lock);
1854 }
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001855 }
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001856
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001857 /*
1858 * Any TxEnd interrupts?
1859 */
1860 if (int_cause & INT_TX_END) {
1861 int i;
1862
1863 wrl(mp, INT_CAUSE(mp->port_num), ~(int_cause & INT_TX_END));
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001864
1865 spin_lock(&mp->lock);
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001866 for (i = 0; i < 8; i++) {
1867 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001868 u32 hw_desc_ptr;
1869 u32 expected_ptr;
1870
1871 if ((int_cause & (INT_TX_END_0 << i)) == 0)
1872 continue;
1873
1874 hw_desc_ptr =
1875 rdl(mp, TXQ_CURRENT_DESC_PTR(mp->port_num, i));
1876 expected_ptr = (u32)txq->tx_desc_dma +
1877 txq->tx_curr_desc * sizeof(struct tx_desc);
1878
1879 if (hw_desc_ptr != expected_ptr)
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02001880 txq_enable(txq);
1881 }
Lennert Buytenhek8fa89bf2008-07-14 22:56:55 +02001882 spin_unlock(&mp->lock);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001883 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001884
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001885 return IRQ_HANDLED;
1886}
1887
Lennert Buytenheke5371492008-06-01 02:18:13 +02001888static void phy_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001889{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001890 int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001891
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001892 data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
1893 if (data < 0)
1894 return;
1895
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02001896 data |= BMCR_RESET;
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001897 if (smi_reg_write(mp, mp->phy_addr, MII_BMCR, data) < 0)
1898 return;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001899
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001900 do {
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02001901 data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
1902 } while (data >= 0 && data & BMCR_RESET);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001903}
1904
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001905static void port_start(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001906{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001907 u32 pscr;
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001908 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001909
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001910 /*
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02001911 * Perform PHY reset, if there is a PHY.
1912 */
1913 if (mp->phy_addr != -1) {
1914 struct ethtool_cmd cmd;
1915
1916 mv643xx_eth_get_settings(mp->dev, &cmd);
1917 phy_reset(mp);
1918 mv643xx_eth_set_settings(mp->dev, &cmd);
1919 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001920
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001921 /*
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02001922 * Configure basic link parameters.
1923 */
1924 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
1925
1926 pscr |= SERIAL_PORT_ENABLE;
1927 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1928
1929 pscr |= DO_NOT_FORCE_LINK_FAIL;
1930 if (mp->phy_addr == -1)
1931 pscr |= FORCE_LINK_PASS;
1932 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
1933
1934 wrl(mp, SDMA_CONFIG(mp->port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
1935
1936 /*
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001937 * Configure TX path and queues.
1938 */
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001939 tx_set_rate(mp, 1000000000, 16777216);
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001940 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02001941 struct tx_queue *txq = mp->txq + i;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001942
Lennert Buytenhek6b368f62008-07-11 19:38:34 +02001943 txq_reset_hw_ptr(txq);
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02001944 txq_set_rate(txq, 1000000000, 16777216);
1945 txq_set_fixed_prio_mode(txq);
Lennert Buytenhek13d64282008-06-01 20:51:22 +02001946 }
1947
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02001948 /*
1949 * Add configured unicast address to address filter table.
1950 */
1951 uc_addr_set(mp, mp->dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001952
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001953 /*
1954 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
1955 * frames to RX queue #0.
1956 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001957 wrl(mp, PORT_CONFIG(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001958
Lennert Buytenhek376489a2008-06-01 01:17:44 +02001959 /*
1960 * Treat BPDUs as normal multicasts, and disable partition mode.
1961 */
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001962 wrl(mp, PORT_CONFIG_EXT(mp->port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001963
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001964 /*
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001965 * Enable the receive queues.
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001966 */
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02001967 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02001968 struct rx_queue *rxq = mp->rxq + i;
1969 int off = RXQ_CURRENT_DESC_PTR(mp->port_num, i);
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001970 u32 addr;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001971
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001972 addr = (u32)rxq->rx_desc_dma;
1973 addr += rxq->rx_curr_desc * sizeof(struct rx_desc);
1974 wrl(mp, off, addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001975
Lennert Buytenhek8a578112008-06-01 18:09:35 +02001976 rxq_enable(rxq);
1977 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001978}
1979
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02001980static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001981{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001982 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02001983 u32 val;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001984
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02001985 val = rdl(mp, SDMA_CONFIG(mp->port_num));
1986 if (mp->shared->extended_rx_coal_limit) {
1987 if (coal > 0xffff)
1988 coal = 0xffff;
1989 val &= ~0x023fff80;
1990 val |= (coal & 0x8000) << 10;
1991 val |= (coal & 0x7fff) << 7;
1992 } else {
1993 if (coal > 0x3fff)
1994 coal = 0x3fff;
1995 val &= ~0x003fff00;
1996 val |= (coal & 0x3fff) << 8;
1997 }
1998 wrl(mp, SDMA_CONFIG(mp->port_num), val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001999}
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002000
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02002001static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int delay)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002002{
2003 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2004
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002005 if (coal > 0x3fff)
2006 coal = 0x3fff;
2007 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), (coal & 0x3fff) << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002008}
2009
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002010static int mv643xx_eth_open(struct net_device *dev)
2011{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002012 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002013 int err;
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002014 int oom;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002015 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002016
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002017 wrl(mp, INT_CAUSE(mp->port_num), 0);
2018 wrl(mp, INT_CAUSE_EXT(mp->port_num), 0);
2019 rdl(mp, INT_CAUSE_EXT(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002020
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002021 err = request_irq(dev->irq, mv643xx_eth_irq,
Lennert Buytenhek2a1867a2008-08-23 23:43:38 +02002022 IRQF_SHARED, dev->name, dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002023 if (err) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002024 dev_printk(KERN_ERR, &dev->dev, "can't assign irq\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002025 return -EAGAIN;
2026 }
2027
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002028 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002029
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002030 napi_enable(&mp->napi);
2031
2032 oom = 0;
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002033 for (i = 0; i < mp->rxq_count; i++) {
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002034 err = rxq_init(mp, i);
2035 if (err) {
2036 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002037 rxq_deinit(mp->rxq + i);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002038 goto out;
2039 }
2040
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002041 rxq_refill(mp->rxq + i, INT_MAX, &oom);
2042 }
2043
2044 if (oom) {
2045 mp->rx_oom.expires = jiffies + (HZ / 10);
2046 add_timer(&mp->rx_oom);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002047 }
Lennert Buytenhek8a578112008-06-01 18:09:35 +02002048
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002049 for (i = 0; i < mp->txq_count; i++) {
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002050 err = txq_init(mp, i);
2051 if (err) {
2052 while (--i >= 0)
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002053 txq_deinit(mp->txq + i);
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002054 goto out_free;
2055 }
2056 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002057
Lennert Buytenhek2f7eb472008-07-24 06:22:59 +02002058 netif_carrier_off(dev);
2059 netif_stop_queue(dev);
2060
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002061 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002062
Lennert Buytenhekffd86bb2008-06-01 21:59:27 +02002063 set_rx_coal(mp, 0);
2064 set_tx_coal(mp, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002065
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002066 wrl(mp, INT_MASK_EXT(mp->port_num),
2067 INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002068
Lennert Buytenhek226bb6b2008-06-02 01:47:21 +02002069 wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002070
2071 return 0;
2072
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002073
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002074out_free:
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002075 for (i = 0; i < mp->rxq_count; i++)
2076 rxq_deinit(mp->rxq + i);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002077out:
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002078 free_irq(dev->irq, dev);
2079
2080 return err;
2081}
2082
Lennert Buytenheke5371492008-06-01 02:18:13 +02002083static void port_reset(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002084{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002085 unsigned int data;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002086 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002087
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002088 for (i = 0; i < mp->rxq_count; i++)
2089 rxq_disable(mp->rxq + i);
2090 for (i = 0; i < mp->txq_count; i++)
2091 txq_disable(mp->txq + i);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002092
2093 while (1) {
2094 u32 ps = rdl(mp, PORT_STATUS(mp->port_num));
2095
2096 if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY)
2097 break;
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002098 udelay(10);
Lennert Buytenhekae9ae062008-07-15 02:15:24 +02002099 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002100
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002101 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002102 data = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
2103 data &= ~(SERIAL_PORT_ENABLE |
2104 DO_NOT_FORCE_LINK_FAIL |
2105 FORCE_LINK_PASS);
2106 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002107}
2108
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002109static int mv643xx_eth_stop(struct net_device *dev)
2110{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002111 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002112 int i;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002113
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002114 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
2115 rdl(mp, INT_MASK(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002116
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002117 napi_disable(&mp->napi);
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002118
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002119 del_timer_sync(&mp->rx_oom);
2120
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002121 netif_carrier_off(dev);
2122 netif_stop_queue(dev);
2123
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002124 free_irq(dev->irq, dev);
2125
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002126 port_reset(mp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002127 mib_counters_update(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002128
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002129 for (i = 0; i < mp->rxq_count; i++)
2130 rxq_deinit(mp->rxq + i);
2131 for (i = 0; i < mp->txq_count; i++)
2132 txq_deinit(mp->txq + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002133
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002134 return 0;
2135}
2136
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002137static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002138{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002139 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002140
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002141 if (mp->phy_addr != -1)
2142 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2143
2144 return -EOPNOTSUPP;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002145}
2146
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002147static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2148{
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002149 struct mv643xx_eth_private *mp = netdev_priv(dev);
2150
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002151 if (new_mtu < 64 || new_mtu > 9500)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002152 return -EINVAL;
2153
2154 dev->mtu = new_mtu;
Lennert Buytenhek89df5fd2008-06-02 00:51:05 +02002155 tx_set_rate(mp, 1000000000, 16777216);
2156
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002157 if (!netif_running(dev))
2158 return 0;
2159
2160 /*
2161 * Stop and then re-open the interface. This will allocate RX
2162 * skbs of the new MTU.
2163 * There is a possible danger that the open will not succeed,
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002164 * due to memory being full.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002165 */
2166 mv643xx_eth_stop(dev);
2167 if (mv643xx_eth_open(dev)) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002168 dev_printk(KERN_ERR, &dev->dev,
2169 "fatal error on re-opening device after "
2170 "MTU change\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002171 }
2172
2173 return 0;
2174}
2175
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002176static void tx_timeout_task(struct work_struct *ugly)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002177{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002178 struct mv643xx_eth_private *mp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002179
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002180 mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task);
2181 if (netif_running(mp->dev)) {
2182 netif_stop_queue(mp->dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002183
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002184 port_reset(mp);
2185 port_start(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002186
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002187 __txq_maybe_wake(mp->txq);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002188 }
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002189}
2190
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002191static void mv643xx_eth_tx_timeout(struct net_device *dev)
2192{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002193 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002194
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002195 dev_printk(KERN_INFO, &dev->dev, "tx timeout\n");
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002196
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002197 schedule_work(&mp->tx_timeout_task);
2198}
2199
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002200#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002201static void mv643xx_eth_netpoll(struct net_device *dev)
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002202{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002203 struct mv643xx_eth_private *mp = netdev_priv(dev);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002204
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002205 wrl(mp, INT_MASK(mp->port_num), 0x00000000);
2206 rdl(mp, INT_MASK(mp->port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002207
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002208 mv643xx_eth_irq(dev->irq, dev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002209
Lennert Buytenhekf2ca60f2008-07-07 00:03:00 +02002210 wrl(mp, INT_MASK(mp->port_num), INT_TX_END | INT_RX | INT_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002211}
2212#endif
2213
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002214static int mv643xx_eth_mdio_read(struct net_device *dev, int addr, int reg)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002215{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002216 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002217 return smi_reg_read(mp, addr, reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002218}
2219
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002220static void mv643xx_eth_mdio_write(struct net_device *dev, int addr, int reg, int val)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002221{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002222 struct mv643xx_eth_private *mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002223 smi_reg_write(mp, addr, reg, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002224}
2225
2226
2227/* platform glue ************************************************************/
Lennert Buytenheke5371492008-06-01 02:18:13 +02002228static void
2229mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp,
2230 struct mbus_dram_target_info *dram)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002231{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002232 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002233 u32 win_enable;
2234 u32 win_protect;
2235 int i;
2236
2237 for (i = 0; i < 6; i++) {
2238 writel(0, base + WINDOW_BASE(i));
2239 writel(0, base + WINDOW_SIZE(i));
2240 if (i < 4)
2241 writel(0, base + WINDOW_REMAP_HIGH(i));
2242 }
2243
2244 win_enable = 0x3f;
2245 win_protect = 0;
2246
2247 for (i = 0; i < dram->num_cs; i++) {
2248 struct mbus_dram_window *cs = dram->cs + i;
2249
2250 writel((cs->base & 0xffff0000) |
2251 (cs->mbus_attr << 8) |
2252 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2253 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2254
2255 win_enable &= ~(1 << i);
2256 win_protect |= 3 << (2 * i);
2257 }
2258
2259 writel(win_enable, base + WINDOW_BAR_ENABLE);
2260 msp->win_protect = win_protect;
2261}
2262
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002263static void infer_hw_params(struct mv643xx_eth_shared_private *msp)
2264{
2265 /*
2266 * Check whether we have a 14-bit coal limit field in bits
2267 * [21:8], or a 16-bit coal limit in bits [25,21:7] of the
2268 * SDMA config register.
2269 */
2270 writel(0x02000000, msp->base + SDMA_CONFIG(0));
2271 if (readl(msp->base + SDMA_CONFIG(0)) & 0x02000000)
2272 msp->extended_rx_coal_limit = 1;
2273 else
2274 msp->extended_rx_coal_limit = 0;
Lennert Buytenhek1e881592008-06-02 01:57:36 +02002275
2276 /*
2277 * Check whether the TX rate control registers are in the
2278 * old or the new place.
2279 */
2280 writel(1, msp->base + TX_BW_MTU_MOVED(0));
2281 if (readl(msp->base + TX_BW_MTU_MOVED(0)) & 1)
2282 msp->tx_bw_control_moved = 1;
2283 else
2284 msp->tx_bw_control_moved = 0;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002285}
2286
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002287static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2288{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002289 static int mv643xx_eth_version_printed = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002290 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002291 struct mv643xx_eth_shared_private *msp;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002292 struct resource *res;
2293 int ret;
2294
Lennert Buytenheke5371492008-06-01 02:18:13 +02002295 if (!mv643xx_eth_version_printed++)
Lennert Buytenhek7dde1542008-07-15 12:20:30 +02002296 printk(KERN_NOTICE "MV-643xx 10/100/1000 ethernet "
2297 "driver version %s\n", mv643xx_eth_driver_version);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002298
2299 ret = -EINVAL;
2300 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2301 if (res == NULL)
2302 goto out;
2303
2304 ret = -ENOMEM;
2305 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2306 if (msp == NULL)
2307 goto out;
2308 memset(msp, 0, sizeof(*msp));
2309
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002310 msp->base = ioremap(res->start, res->end - res->start + 1);
2311 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002312 goto out_free;
2313
Lennert Buytenhek2b3ba0e2008-08-24 05:41:09 +02002314 mutex_init(&msp->phy_lock);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002315
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002316 msp->err_interrupt = NO_IRQ;
2317 init_waitqueue_head(&msp->smi_busy_wait);
2318
2319 /*
2320 * Check whether the error interrupt is hooked up.
2321 */
2322 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2323 if (res != NULL) {
2324 int err;
2325
2326 err = request_irq(res->start, mv643xx_eth_err_irq,
2327 IRQF_SHARED, "mv643xx_eth", msp);
2328 if (!err) {
2329 writel(ERR_INT_SMI_DONE, msp->base + ERR_INT_MASK);
2330 msp->err_interrupt = res->start;
2331 }
2332 }
2333
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002334 /*
2335 * (Re-)program MBUS remapping windows if we are asked to.
2336 */
2337 if (pd != NULL && pd->dram != NULL)
2338 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2339
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002340 /*
2341 * Detect hardware parameters.
2342 */
2343 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
Lennert Buytenhek773fc3e2008-06-02 01:54:16 +02002344 infer_hw_params(msp);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002345
2346 platform_set_drvdata(pdev, msp);
2347
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002348 return 0;
2349
2350out_free:
2351 kfree(msp);
2352out:
2353 return ret;
2354}
2355
2356static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2357{
Lennert Buytenheke5371492008-06-01 02:18:13 +02002358 struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002359
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002360 if (msp->err_interrupt != NO_IRQ)
2361 free_irq(msp->err_interrupt, msp);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002362 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002363 kfree(msp);
2364
2365 return 0;
2366}
2367
2368static struct platform_driver mv643xx_eth_shared_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002369 .probe = mv643xx_eth_shared_probe,
2370 .remove = mv643xx_eth_shared_remove,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002371 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002372 .name = MV643XX_ETH_SHARED_NAME,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002373 .owner = THIS_MODULE,
2374 },
2375};
2376
Lennert Buytenheke5371492008-06-01 02:18:13 +02002377static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002378{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002379 int addr_shift = 5 * mp->port_num;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002380 u32 data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002381
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002382 data = rdl(mp, PHY_ADDR);
2383 data &= ~(0x1f << addr_shift);
2384 data |= (phy_addr & 0x1f) << addr_shift;
2385 wrl(mp, PHY_ADDR, data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002386}
2387
Lennert Buytenheke5371492008-06-01 02:18:13 +02002388static int phy_addr_get(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002389{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002390 unsigned int data;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002391
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002392 data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002393
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002394 return (data >> (5 * mp->port_num)) & 0x1f;
2395}
2396
2397static void set_params(struct mv643xx_eth_private *mp,
2398 struct mv643xx_eth_platform_data *pd)
2399{
2400 struct net_device *dev = mp->dev;
2401
2402 if (is_valid_ether_addr(pd->mac_addr))
2403 memcpy(dev->dev_addr, pd->mac_addr, 6);
2404 else
2405 uc_addr_get(mp, dev->dev_addr);
2406
2407 if (pd->phy_addr == -1) {
2408 mp->shared_smi = NULL;
2409 mp->phy_addr = -1;
2410 } else {
2411 mp->shared_smi = mp->shared;
2412 if (pd->shared_smi != NULL)
2413 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2414
2415 if (pd->force_phy_addr || pd->phy_addr) {
2416 mp->phy_addr = pd->phy_addr & 0x3f;
2417 phy_addr_set(mp, mp->phy_addr);
2418 } else {
2419 mp->phy_addr = phy_addr_get(mp);
2420 }
2421 }
2422
2423 mp->default_rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2424 if (pd->rx_queue_size)
2425 mp->default_rx_ring_size = pd->rx_queue_size;
2426 mp->rx_desc_sram_addr = pd->rx_sram_addr;
2427 mp->rx_desc_sram_size = pd->rx_sram_size;
2428
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002429 mp->rxq_count = pd->rx_queue_count ? : 1;
Lennert Buytenhek64da80a2008-06-02 01:01:26 +02002430
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002431 mp->default_tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
2432 if (pd->tx_queue_size)
2433 mp->default_tx_ring_size = pd->tx_queue_size;
2434 mp->tx_desc_sram_addr = pd->tx_sram_addr;
2435 mp->tx_desc_sram_size = pd->tx_sram_size;
Lennert Buytenhek3d6b35b2008-06-02 01:28:22 +02002436
Lennert Buytenhekf7981c12008-08-26 10:23:22 +02002437 mp->txq_count = pd->tx_queue_count ? : 1;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002438}
2439
Lennert Buytenheke5371492008-06-01 02:18:13 +02002440static int phy_detect(struct mv643xx_eth_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002441{
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002442 int data;
2443 int data2;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002444
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002445 data = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
2446 if (data < 0)
2447 return -ENODEV;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002448
Lennert Buytenhek45c5d3b2008-08-26 04:42:59 +02002449 if (smi_reg_write(mp, mp->phy_addr, MII_BMCR, data ^ BMCR_ANENABLE) < 0)
2450 return -ENODEV;
2451
2452 data2 = smi_reg_read(mp, mp->phy_addr, MII_BMCR);
2453 if (data2 < 0)
2454 return -ENODEV;
2455
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002456 if (((data ^ data2) & BMCR_ANENABLE) == 0)
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002457 return -ENODEV;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002458
Lennert Buytenhek7f106c12008-07-15 02:28:47 +02002459 smi_reg_write(mp, mp->phy_addr, MII_BMCR, data);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002460
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002461 return 0;
2462}
2463
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002464static int phy_init(struct mv643xx_eth_private *mp,
2465 struct mv643xx_eth_platform_data *pd)
James Chapmand0412d92006-01-27 01:15:30 -07002466{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002467 struct ethtool_cmd cmd;
2468 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002469
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002470 err = phy_detect(mp);
2471 if (err) {
2472 dev_printk(KERN_INFO, &mp->dev->dev,
2473 "no PHY detected at addr %d\n", mp->phy_addr);
2474 return err;
James Chapmand0412d92006-01-27 01:15:30 -07002475 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002476 phy_reset(mp);
2477
2478 mp->mii.phy_id = mp->phy_addr;
2479 mp->mii.phy_id_mask = 0x3f;
2480 mp->mii.reg_num_mask = 0x1f;
2481 mp->mii.dev = mp->dev;
2482 mp->mii.mdio_read = mv643xx_eth_mdio_read;
2483 mp->mii.mdio_write = mv643xx_eth_mdio_write;
2484
2485 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
2486
2487 memset(&cmd, 0, sizeof(cmd));
2488
2489 cmd.port = PORT_MII;
2490 cmd.transceiver = XCVR_INTERNAL;
2491 cmd.phy_address = mp->phy_addr;
2492 if (pd->speed == 0) {
2493 cmd.autoneg = AUTONEG_ENABLE;
2494 cmd.speed = SPEED_100;
2495 cmd.advertising = ADVERTISED_10baseT_Half |
2496 ADVERTISED_10baseT_Full |
2497 ADVERTISED_100baseT_Half |
2498 ADVERTISED_100baseT_Full;
2499 if (mp->mii.supports_gmii)
2500 cmd.advertising |= ADVERTISED_1000baseT_Full;
2501 } else {
2502 cmd.autoneg = AUTONEG_DISABLE;
2503 cmd.speed = pd->speed;
2504 cmd.duplex = pd->duplex;
2505 }
2506
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002507 mv643xx_eth_set_settings(mp->dev, &cmd);
2508
2509 return 0;
James Chapmand0412d92006-01-27 01:15:30 -07002510}
2511
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002512static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex)
2513{
2514 u32 pscr;
2515
2516 pscr = rdl(mp, PORT_SERIAL_CONTROL(mp->port_num));
2517 if (pscr & SERIAL_PORT_ENABLE) {
2518 pscr &= ~SERIAL_PORT_ENABLE;
2519 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
2520 }
2521
2522 pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED;
2523 if (mp->phy_addr == -1) {
2524 pscr |= DISABLE_AUTO_NEG_SPEED_GMII;
2525 if (speed == SPEED_1000)
2526 pscr |= SET_GMII_SPEED_TO_1000;
2527 else if (speed == SPEED_100)
2528 pscr |= SET_MII_SPEED_TO_100;
2529
2530 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL;
2531
2532 pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX;
2533 if (duplex == DUPLEX_FULL)
2534 pscr |= SET_FULL_DUPLEX_MODE;
2535 }
2536
2537 wrl(mp, PORT_SERIAL_CONTROL(mp->port_num), pscr);
2538}
2539
Russell King3ae5eae2005-11-09 22:32:44 +00002540static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 struct mv643xx_eth_platform_data *pd;
Lennert Buytenheke5371492008-06-01 02:18:13 +02002543 struct mv643xx_eth_private *mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 struct resource *res;
Al Viroc5d64712007-10-13 08:30:26 +01002546 DECLARE_MAC_BUF(mac);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002547 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002549 pd = pdev->dev.platform_data;
2550 if (pd == NULL) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002551 dev_printk(KERN_ERR, &pdev->dev,
2552 "no mv643xx_eth_platform_data\n");
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002553 return -ENODEV;
2554 }
2555
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002556 if (pd->shared == NULL) {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002557 dev_printk(KERN_ERR, &pdev->dev,
2558 "no mv643xx_eth_platform_data->shared\n");
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002559 return -ENODEV;
2560 }
2561
Lennert Buytenheke5371492008-06-01 02:18:13 +02002562 dev = alloc_etherdev(sizeof(struct mv643xx_eth_private));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 if (!dev)
2564 return -ENOMEM;
2565
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 mp = netdev_priv(dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002567 platform_set_drvdata(pdev, mp);
2568
2569 mp->shared = platform_get_drvdata(pd->shared);
2570 mp->port_num = pd->port_number;
2571
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002572 mp->dev = dev;
Lennert Buytenhek78fff832008-08-24 01:03:57 +02002573
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002574 set_params(mp, pd);
2575
2576 spin_lock_init(&mp->lock);
2577
2578 mib_counters_clear(mp);
2579 INIT_WORK(&mp->tx_timeout_task, tx_timeout_task);
2580
Lennert Buytenhekbedfe322008-06-02 02:13:03 +02002581 if (mp->phy_addr != -1) {
2582 err = phy_init(mp, pd);
2583 if (err)
2584 goto out;
2585
2586 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops);
2587 } else {
2588 SET_ETHTOOL_OPS(dev, &mv643xx_eth_ethtool_ops_phyless);
2589 }
Lennert Buytenhek81600eea92008-07-14 14:29:40 +02002590 init_pscr(mp, pd->speed, pd->duplex);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002591
Lennert Buytenhek2257e052008-08-24 04:33:36 +02002592 netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, 128);
2593
2594 init_timer(&mp->rx_oom);
2595 mp->rx_oom.data = (unsigned long)mp;
2596 mp->rx_oom.function = oom_timer_wrapper;
2597
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2600 BUG_ON(!res);
2601 dev->irq = res->start;
2602
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002603 dev->hard_start_xmit = mv643xx_eth_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 dev->open = mv643xx_eth_open;
2605 dev->stop = mv643xx_eth_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002607 dev->set_mac_address = mv643xx_eth_set_mac_address;
2608 dev->do_ioctl = mv643xx_eth_ioctl;
2609 dev->change_mtu = mv643xx_eth_change_mtu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 dev->tx_timeout = mv643xx_eth_tx_timeout;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002611#ifdef CONFIG_NET_POLL_CONTROLLER
Lennert Buytenheke5371492008-06-01 02:18:13 +02002612 dev->poll_controller = mv643xx_eth_netpoll;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002613#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 dev->base_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
Wolfram Joost63890572006-01-16 16:57:41 -07002617 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Lennert Buytenheke32b6612008-07-24 06:22:59 +02002618 dev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM;
James Chapmanc28a4f82006-01-27 01:13:15 -07002619
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002620 SET_NETDEV_DEV(dev, &pdev->dev);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002621
2622 if (mp->shared->win_protect)
2623 wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect);
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 err = register_netdev(dev);
2626 if (err)
2627 goto out;
2628
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002629 dev_printk(KERN_NOTICE, &dev->dev, "port %d with MAC address %s\n",
2630 mp->port_num, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Lennert Buytenhek13d64282008-06-01 20:51:22 +02002632 if (mp->tx_desc_sram_size > 0)
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002633 dev_printk(KERN_NOTICE, &dev->dev, "configured with sram\n");
Nicolas DETb1529872005-10-28 17:46:30 -07002634
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return 0;
2636
2637out:
2638 free_netdev(dev);
2639
2640 return err;
2641}
2642
Russell King3ae5eae2005-11-09 22:32:44 +00002643static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002645 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002647 unregister_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 flush_scheduled_work();
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002649 free_netdev(mp->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Russell King3ae5eae2005-11-09 22:32:44 +00002651 platform_set_drvdata(pdev, NULL);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002652
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 return 0;
2654}
2655
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002656static void mv643xx_eth_shutdown(struct platform_device *pdev)
2657{
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002658 struct mv643xx_eth_private *mp = platform_get_drvdata(pdev);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002659
2660 /* Mask all interrupts on ethernet port */
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002661 wrl(mp, INT_MASK(mp->port_num), 0);
2662 rdl(mp, INT_MASK(mp->port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002663
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002664 if (netif_running(mp->dev))
2665 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002666}
2667
Russell King3ae5eae2005-11-09 22:32:44 +00002668static struct platform_driver mv643xx_eth_driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002669 .probe = mv643xx_eth_probe,
2670 .remove = mv643xx_eth_remove,
2671 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002672 .driver = {
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002673 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002674 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002675 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676};
2677
Lennert Buytenheke5371492008-06-01 02:18:13 +02002678static int __init mv643xx_eth_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 int rc;
2681
Russell King3ae5eae2005-11-09 22:32:44 +00002682 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002684 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002686 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 return rc;
2690}
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002691module_init(mv643xx_eth_init_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692
Lennert Buytenheke5371492008-06-01 02:18:13 +02002693static void __exit mv643xx_eth_cleanup_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694{
Russell King3ae5eae2005-11-09 22:32:44 +00002695 platform_driver_unregister(&mv643xx_eth_driver);
2696 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
Lennert Buytenheke5371492008-06-01 02:18:13 +02002698module_exit(mv643xx_eth_cleanup_module);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
Lennert Buytenhek45675bc2008-06-02 02:02:30 +02002700MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, "
2701 "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002703MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -07002704MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
Lennert Buytenhekfc32b0e2008-06-02 00:28:40 +02002705MODULE_ALIAS("platform:" MV643XX_ETH_NAME);