blob: ba1348f3b25bbf7ab09e3e9e83598c760bc02f5e [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 Buytenhekc9df406f2008-06-01 00:48:39 +020057static char mv643xx_driver_name[] = "mv643xx_eth";
58static char mv643xx_driver_version[] = "1.0";
59
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020060#define MV643XX_CHECKSUM_OFFLOAD_TX
61#define MV643XX_NAPI
62#define MV643XX_TX_FAST_REFILL
63#undef MV643XX_COAL
64
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020065#define MV643XX_TX_COAL 100
66#ifdef MV643XX_COAL
67#define MV643XX_RX_COAL 100
68#endif
69
70#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
71#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
72#else
73#define MAX_DESCS_PER_SKB 1
74#endif
75
76#define ETH_VLAN_HLEN 4
77#define ETH_FCS_LEN 4
78#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
79#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
80 ETH_VLAN_HLEN + ETH_FCS_LEN)
81#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
82 dma_get_cache_alignment())
83
84/*
85 * Registers shared between all ports.
86 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020087#define PHY_ADDR 0x0000
88#define SMI_REG 0x0004
89#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
90#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
91#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
92#define WINDOW_BAR_ENABLE 0x0290
93#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020094
95/*
96 * Per-port registers.
97 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020098#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020099#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200100#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
101#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
102#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
103#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
104#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
105#define PORT_STATUS(p) (0x0444 + ((p) << 10))
106#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
107#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
108#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200109#define INT_RX 0x00000804
110#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200111#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200112#define INT_EXT_LINK 0x00100000
113#define INT_EXT_PHY 0x00010000
114#define INT_EXT_TX_ERROR_0 0x00000100
115#define INT_EXT_TX_0 0x00000001
116#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200117#define INT_MASK(p) (0x0468 + ((p) << 10))
118#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
119#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
120#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
121#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
122#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
123#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
124#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
125#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
126#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200127
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200128
129/*
130 * SDMA configuration register.
131 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200132#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200134#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200136
137#if defined(__BIG_ENDIAN)
138#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
139 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200140 TX_BURST_SIZE_4_64BIT
141#elif defined(__LITTLE_ENDIAN)
142#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
143 RX_BURST_SIZE_4_64BIT | \
144 BLM_RX_NO_SWAP | \
145 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200146 TX_BURST_SIZE_4_64BIT
147#else
148#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
149#endif
150
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200151
152/*
153 * Port serial control register.
154 */
155#define SET_MII_SPEED_TO_100 (1 << 24)
156#define SET_GMII_SPEED_TO_1000 (1 << 23)
157#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200158#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200159#define MAX_RX_PACKET_9700BYTE (5 << 17)
160#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200161#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
162#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
163#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
164#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
165#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
166#define FORCE_LINK_PASS (1 << 1)
167#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200168
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200169#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
170#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
171
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172/* Buffer offset from buffer pointer */
173#define RX_BUF_OFFSET 0x2
174
175/* Gigabit Ethernet Unit Global Registers */
176
177/* MIB Counters register definitions */
178#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
179#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
180#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
181#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
182#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
183#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
184#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
185#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
186#define ETH_MIB_FRAMES_64_OCTETS 0x20
187#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
188#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
189#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
190#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
191#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
192#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
193#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
194#define ETH_MIB_GOOD_FRAMES_SENT 0x40
195#define ETH_MIB_EXCESSIVE_COLLISION 0x44
196#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
197#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
198#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
199#define ETH_MIB_FC_SENT 0x54
200#define ETH_MIB_GOOD_FC_RECEIVED 0x58
201#define ETH_MIB_BAD_FC_RECEIVED 0x5c
202#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
203#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
204#define ETH_MIB_OVERSIZE_RECEIVED 0x68
205#define ETH_MIB_JABBER_RECEIVED 0x6c
206#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
207#define ETH_MIB_BAD_CRC_EVENT 0x74
208#define ETH_MIB_COLLISION 0x78
209#define ETH_MIB_LATE_COLLISION 0x7c
210
211/* Port serial status reg (PSR) */
212#define ETH_INTERFACE_PCM 0x00000001
213#define ETH_LINK_IS_UP 0x00000002
214#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
215#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
216#define ETH_GMII_SPEED_1000 0x00000010
217#define ETH_MII_SPEED_100 0x00000020
218#define ETH_TX_IN_PROGRESS 0x00000080
219#define ETH_BYPASS_ACTIVE 0x00000100
220#define ETH_PORT_AT_PARTITION_STATE 0x00000200
221#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
222
223/* SMI reg */
224#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
225#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
226#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
227#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
228
229/* Interrupt Cause Register Bit Definitions */
230
231/* SDMA command status fields macros */
232
233/* Tx & Rx descriptors status */
234#define ETH_ERROR_SUMMARY 0x00000001
235
236/* Tx & Rx descriptors command */
237#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
238
239/* Tx descriptors status */
240#define ETH_LC_ERROR 0
241#define ETH_UR_ERROR 0x00000002
242#define ETH_RL_ERROR 0x00000004
243#define ETH_LLC_SNAP_FORMAT 0x00000200
244
245/* Rx descriptors status */
246#define ETH_OVERRUN_ERROR 0x00000002
247#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
248#define ETH_RESOURCE_ERROR 0x00000006
249#define ETH_VLAN_TAGGED 0x00080000
250#define ETH_BPDU_FRAME 0x00100000
251#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
252#define ETH_OTHER_FRAME_TYPE 0x00400000
253#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
254#define ETH_FRAME_TYPE_IP_V_4 0x01000000
255#define ETH_FRAME_HEADER_OK 0x02000000
256#define ETH_RX_LAST_DESC 0x04000000
257#define ETH_RX_FIRST_DESC 0x08000000
258#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
259#define ETH_RX_ENABLE_INTERRUPT 0x20000000
260#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
261
262/* Rx descriptors byte count */
263#define ETH_FRAME_FRAGMENTED 0x00000004
264
265/* Tx descriptors command */
266#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
267#define ETH_FRAME_SET_TO_VLAN 0x00008000
268#define ETH_UDP_FRAME 0x00010000
269#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
270#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
271#define ETH_ZERO_PADDING 0x00080000
272#define ETH_TX_LAST_DESC 0x00100000
273#define ETH_TX_FIRST_DESC 0x00200000
274#define ETH_GEN_CRC 0x00400000
275#define ETH_TX_ENABLE_INTERRUPT 0x00800000
276#define ETH_AUTO_MODE 0x40000000
277
278#define ETH_TX_IHL_SHIFT 11
279
280/* typedefs */
281
282typedef enum _eth_func_ret_status {
283 ETH_OK, /* Returned as expected. */
284 ETH_ERROR, /* Fundamental error. */
285 ETH_RETRY, /* Could not process request. Try later.*/
286 ETH_END_OF_JOB, /* Ring has nothing to process. */
287 ETH_QUEUE_FULL, /* Ring resource error. */
288 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
289} ETH_FUNC_RET_STATUS;
290
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200291/* These are for big-endian machines. Little endian needs different
292 * definitions.
293 */
294#if defined(__BIG_ENDIAN)
295struct eth_rx_desc {
296 u16 byte_cnt; /* Descriptor buffer byte count */
297 u16 buf_size; /* Buffer size */
298 u32 cmd_sts; /* Descriptor command status */
299 u32 next_desc_ptr; /* Next descriptor pointer */
300 u32 buf_ptr; /* Descriptor buffer pointer */
301};
302
303struct eth_tx_desc {
304 u16 byte_cnt; /* buffer byte count */
305 u16 l4i_chk; /* CPU provided TCP checksum */
306 u32 cmd_sts; /* Command/status field */
307 u32 next_desc_ptr; /* Pointer to next descriptor */
308 u32 buf_ptr; /* pointer to buffer for this descriptor*/
309};
310#elif defined(__LITTLE_ENDIAN)
311struct eth_rx_desc {
312 u32 cmd_sts; /* Descriptor command status */
313 u16 buf_size; /* Buffer size */
314 u16 byte_cnt; /* Descriptor buffer byte count */
315 u32 buf_ptr; /* Descriptor buffer pointer */
316 u32 next_desc_ptr; /* Next descriptor pointer */
317};
318
319struct eth_tx_desc {
320 u32 cmd_sts; /* Command/status field */
321 u16 l4i_chk; /* CPU provided TCP checksum */
322 u16 byte_cnt; /* buffer byte count */
323 u32 buf_ptr; /* pointer to buffer for this descriptor*/
324 u32 next_desc_ptr; /* Pointer to next descriptor */
325};
326#else
327#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
328#endif
329
330/* Unified struct for Rx and Tx operations. The user is not required to */
331/* be familier with neither Tx nor Rx descriptors. */
332struct pkt_info {
333 unsigned short byte_cnt; /* Descriptor buffer byte count */
334 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
335 unsigned int cmd_sts; /* Descriptor command status */
336 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
337 struct sk_buff *return_info; /* User resource return information */
338};
339
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200340
341/* global *******************************************************************/
342struct mv643xx_shared_private {
343 void __iomem *eth_base;
344
345 /* used to protect SMI_REG, which is shared across ports */
346 spinlock_t phy_lock;
347
348 u32 win_protect;
349
350 unsigned int t_clk;
351};
352
353
354/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200355struct mv643xx_mib_counters {
356 u64 good_octets_received;
357 u32 bad_octets_received;
358 u32 internal_mac_transmit_err;
359 u32 good_frames_received;
360 u32 bad_frames_received;
361 u32 broadcast_frames_received;
362 u32 multicast_frames_received;
363 u32 frames_64_octets;
364 u32 frames_65_to_127_octets;
365 u32 frames_128_to_255_octets;
366 u32 frames_256_to_511_octets;
367 u32 frames_512_to_1023_octets;
368 u32 frames_1024_to_max_octets;
369 u64 good_octets_sent;
370 u32 good_frames_sent;
371 u32 excessive_collision;
372 u32 multicast_frames_sent;
373 u32 broadcast_frames_sent;
374 u32 unrec_mac_control_received;
375 u32 fc_sent;
376 u32 good_fc_received;
377 u32 bad_fc_received;
378 u32 undersize_received;
379 u32 fragments_received;
380 u32 oversize_received;
381 u32 jabber_received;
382 u32 mac_receive_error;
383 u32 bad_crc_event;
384 u32 collision;
385 u32 late_collision;
386};
387
388struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200389 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200390 int port_num; /* User Ethernet port number */
391
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200392 struct mv643xx_shared_private *shared_smi;
393
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200394 u32 rx_sram_addr; /* Base address of rx sram area */
395 u32 rx_sram_size; /* Size of rx sram area */
396 u32 tx_sram_addr; /* Base address of tx sram area */
397 u32 tx_sram_size; /* Size of tx sram area */
398
399 int rx_resource_err; /* Rx ring resource error flag */
400
401 /* Tx/Rx rings managment indexes fields. For driver use */
402
403 /* Next available and first returning Rx resource */
404 int rx_curr_desc_q, rx_used_desc_q;
405
406 /* Next available and first returning Tx resource */
407 int tx_curr_desc_q, tx_used_desc_q;
408
409#ifdef MV643XX_TX_FAST_REFILL
410 u32 tx_clean_threshold;
411#endif
412
413 struct eth_rx_desc *p_rx_desc_area;
414 dma_addr_t rx_desc_dma;
415 int rx_desc_area_size;
416 struct sk_buff **rx_skb;
417
418 struct eth_tx_desc *p_tx_desc_area;
419 dma_addr_t tx_desc_dma;
420 int tx_desc_area_size;
421 struct sk_buff **tx_skb;
422
423 struct work_struct tx_timeout_task;
424
425 struct net_device *dev;
426 struct napi_struct napi;
427 struct net_device_stats stats;
428 struct mv643xx_mib_counters mib_counters;
429 spinlock_t lock;
430 /* Size of Tx Ring per queue */
431 int tx_ring_size;
432 /* Number of tx descriptors in use */
433 int tx_desc_count;
434 /* Size of Rx Ring per queue */
435 int rx_ring_size;
436 /* Number of rx descriptors in use */
437 int rx_desc_count;
438
439 /*
440 * Used in case RX Ring is empty, which can be caused when
441 * system does not have resources (skb's)
442 */
443 struct timer_list timeout;
444
445 u32 rx_int_coal;
446 u32 tx_int_coal;
447 struct mii_if_info mii;
448};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200450
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200451/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700452static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200454 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700457static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200459 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200462
463/* rxq/txq helper functions *************************************************/
464static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
465 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200467 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200468}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200470static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
471{
472 unsigned int port_num = mp->port_num;
473 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700474
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200475 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200476 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200477 if (queues) {
478 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200479 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200480
481 /* Wait for all Rx activity to terminate. */
482 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200483 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200484 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200487 return queues;
488}
489
490static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
491 unsigned int queues)
492{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200493 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200494}
495
496static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
497{
498 unsigned int port_num = mp->port_num;
499 u32 queues;
500
501 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200502 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200503 if (queues) {
504 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200505 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200506
507 /* Wait for all Tx activity to terminate. */
508 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200509 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200510 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200511
512 /* Wait for Tx FIFO to empty */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200513 while (rdl(mp, PORT_STATUS(port_num)) & ETH_PORT_TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200514 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200515 }
516
517 return queues;
518}
519
520
521/* rx ***********************************************************************/
522static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
523
524/*
525 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
526 *
527 * DESCRIPTION:
528 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
529 * next 'used' descriptor and attached the returned buffer to it.
530 * In case the Rx ring was in "resource error" condition, where there are
531 * no available Rx resources, the function resets the resource error flag.
532 *
533 * INPUT:
534 * struct mv643xx_private *mp Ethernet Port Control srtuct.
535 * struct pkt_info *p_pkt_info Information on returned buffer.
536 *
537 * OUTPUT:
538 * New available Rx resource in Rx descriptor ring.
539 *
540 * RETURN:
541 * ETH_ERROR in case the routine can not access Rx desc ring.
542 * ETH_OK otherwise.
543 */
544static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
545 struct pkt_info *p_pkt_info)
546{
547 int used_rx_desc; /* Where to return Rx resource */
548 volatile struct eth_rx_desc *p_used_rx_desc;
549 unsigned long flags;
550
551 spin_lock_irqsave(&mp->lock, flags);
552
553 /* Get 'used' Rx descriptor */
554 used_rx_desc = mp->rx_used_desc_q;
555 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
556
557 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
558 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
559 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
560
561 /* Flush the write pipe */
562
563 /* Return the descriptor to DMA ownership */
564 wmb();
565 p_used_rx_desc->cmd_sts =
566 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
567 wmb();
568
569 /* Move the used descriptor pointer to the next descriptor */
570 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
571
572 /* Any Rx return cancels the Rx resource error status */
573 mp->rx_resource_err = 0;
574
575 spin_unlock_irqrestore(&mp->lock, flags);
576
577 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
580/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700581 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
583 * Fills / refills RX queue on a certain gigabit ethernet port
584 *
585 * Input : pointer to ethernet interface network device structure
586 * Output : N/A
587 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700588static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 struct mv643xx_private *mp = netdev_priv(dev);
591 struct pkt_info pkt_info;
592 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700593 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700595 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000596 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!skb)
598 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700599 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000600 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700601 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000602 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700604 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
605 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
606 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 pkt_info.return_info = skb;
608 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
609 printk(KERN_ERR
610 "%s: Error allocating RX Ring\n", dev->name);
611 break;
612 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700613 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 /*
616 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700617 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700619 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700621 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700627 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 *
629 * Timer routine to wake up RX queue filling task. This function is
630 * used only in case the RX queue is empty, and all alloc_skb has
631 * failed (due to out of memory event).
632 *
633 * Input : pointer to ethernet interface network device structure
634 * Output : N/A
635 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700636static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700638 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200642 * eth_port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200644 * DESCRIPTION:
645 * This routine returns the received data to the caller. There is no
646 * data copying during routine operation. All information is returned
647 * using pointer to packet information struct passed from the caller.
648 * If the routine exhausts Rx ring resources then the resource error flag
649 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200651 * INPUT:
652 * struct mv643xx_private *mp Ethernet Port Control srtuct.
653 * struct pkt_info *p_pkt_info User packet buffer.
654 *
655 * OUTPUT:
656 * Rx ring current and used indexes are updated.
657 *
658 * RETURN:
659 * ETH_ERROR in case the routine can not access Rx desc ring.
660 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
661 * ETH_END_OF_JOB if there is no received data.
662 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200664static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
665 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200667 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
668 volatile struct eth_rx_desc *p_rx_desc;
669 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700670 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200672 /* Do not process Rx ring in case of Rx ring resource error */
673 if (mp->rx_resource_err)
674 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700675
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200676 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700677
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200678 /* Get the Rx Desc ring 'curr and 'used' indexes */
679 rx_curr_desc = mp->rx_curr_desc_q;
680 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200682 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700683
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200684 /* The following parameters are used to save readings from memory */
685 command_status = p_rx_desc->cmd_sts;
686 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700687
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200688 /* Nothing to receive... */
689 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700690 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200691 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
693
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200694 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
695 p_pkt_info->cmd_sts = command_status;
696 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
697 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
698 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200700 /*
701 * Clean the return info field to indicate that the
702 * packet has been moved to the upper layers
703 */
704 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700705
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200706 /* Update current index in data structure */
707 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
708 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700709
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200710 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
711 if (rx_next_curr_desc == rx_used_desc)
712 mp->rx_resource_err = 1;
713
714 spin_unlock_irqrestore(&mp->lock, flags);
715
716 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * mv643xx_eth_receive
721 *
722 * This function is forward packets that are received from the port's
723 * queues toward kernel core or FastRoute them to another interface.
724 *
725 * Input : dev - a pointer to the required interface
726 * max - maximum number to receive (0 means unlimted)
727 *
728 * Output : number of served packets
729 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700733 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 unsigned int received_packets = 0;
735 struct sk_buff *skb;
736 struct pkt_info pkt_info;
737
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700738 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400739 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700740 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700741 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700743
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700744 /*
745 * Update statistics.
746 * Note byte count includes 4 byte CRC count
747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 stats->rx_packets++;
749 stats->rx_bytes += pkt_info.byte_cnt;
750 skb = pkt_info.return_info;
751 /*
752 * In case received a packet without first / last bits on OR
753 * the error summary bit is on, the packets needs to be dropeed.
754 */
755 if (((pkt_info.cmd_sts
756 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
757 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
758 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
759 stats->rx_dropped++;
760 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
761 ETH_RX_LAST_DESC)) !=
762 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
763 if (net_ratelimit())
764 printk(KERN_ERR
765 "%s: Received packet spread "
766 "on multiple descriptors\n",
767 dev->name);
768 }
769 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
770 stats->rx_errors++;
771
772 dev_kfree_skb_irq(skb);
773 } else {
774 /*
775 * The -4 is for the CRC in the trailer of the
776 * received packet
777 */
778 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
781 skb->ip_summed = CHECKSUM_UNNECESSARY;
782 skb->csum = htons(
783 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
784 }
785 skb->protocol = eth_type_trans(skb, dev);
786#ifdef MV643XX_NAPI
787 netif_receive_skb(skb);
788#else
789 netif_rx(skb);
790#endif
791 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700792 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700794 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 return received_packets;
797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800/*
801 * mv643xx_poll
802 *
803 * This function is used in case of NAPI
804 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700805static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700807 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
808 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700810 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812#ifdef MV643XX_TX_FAST_REFILL
813 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700814 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817#endif
818
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700819 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200820 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700821 != (u32) mp->rx_used_desc_q)
822 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700824 if (work_done < budget) {
825 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200826 wrl(mp, INT_CAUSE(port_num), 0);
827 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200828 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700831 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832}
833#endif
834
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200835
836/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700837/**
838 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
839 *
840 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700841 * This helper function detects that case.
842 */
843
844static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
845{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700846 unsigned int frag;
847 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700848
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700849 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
850 fragp = &skb_shinfo(skb)->frags[frag];
851 if (fragp->size <= 8 && fragp->page_offset & 0x7)
852 return 1;
853 }
854 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700855}
856
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700857/**
858 * eth_alloc_tx_desc_index - return the index of the next available tx desc
859 */
860static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
861{
862 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700863
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700864 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700865
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700866 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700867 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
868
869 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
870
871 return tx_desc_curr;
872}
873
874/**
875 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700877 * Ensure the data for each fragment to be transmitted is mapped properly,
878 * then fill in descriptors in the tx hw queue.
879 */
880static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
881 struct sk_buff *skb)
882{
883 int frag;
884 int tx_index;
885 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700886
887 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
888 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
889
890 tx_index = eth_alloc_tx_desc_index(mp);
891 desc = &mp->p_tx_desc_area[tx_index];
892
893 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
894 /* Last Frag enables interrupt and frees the skb */
895 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
896 desc->cmd_sts |= ETH_ZERO_PADDING |
897 ETH_TX_LAST_DESC |
898 ETH_TX_ENABLE_INTERRUPT;
899 mp->tx_skb[tx_index] = skb;
900 } else
Al Viro05980772006-05-30 23:59:09 -0400901 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700902
903 desc = &mp->p_tx_desc_area[tx_index];
904 desc->l4i_chk = 0;
905 desc->byte_cnt = this_frag->size;
906 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
907 this_frag->page_offset,
908 this_frag->size,
909 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700910 }
911}
912
Byron Bradley324ff2c2008-02-04 23:47:15 -0800913static inline __be16 sum16_as_be(__sum16 sum)
914{
915 return (__force __be16)sum;
916}
917
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700918/**
919 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700921 * Ensure the data for an skb to be transmitted is mapped properly,
922 * then fill in descriptors in the tx hw queue and start the hardware.
923 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700924static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
925 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700926{
927 int tx_index;
928 struct eth_tx_desc *desc;
929 u32 cmd_sts;
930 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700931 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700932
933 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
934
935 tx_index = eth_alloc_tx_desc_index(mp);
936 desc = &mp->p_tx_desc_area[tx_index];
937
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700938 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700939 eth_tx_fill_frag_descs(mp, skb);
940
941 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400942 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700943 } else {
944 cmd_sts |= ETH_ZERO_PADDING |
945 ETH_TX_LAST_DESC |
946 ETH_TX_ENABLE_INTERRUPT;
947 length = skb->len;
948 mp->tx_skb[tx_index] = skb;
949 }
950
951 desc->byte_cnt = length;
952 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700953
Patrick McHardy84fa7932006-08-29 16:44:56 -0700954 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800955 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700956
957 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
958 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700959 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700960
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700961 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700962 case IPPROTO_UDP:
963 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800964 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700965 break;
966 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800967 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700968 break;
969 default:
970 BUG();
971 }
972 } else {
973 /* Errata BTS #50, IHL must be 5 if no HW checksum */
974 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
975 desc->l4i_chk = 0;
976 }
977
978 /* ensure all other descriptors are written before first cmd_sts */
979 wmb();
980 desc->cmd_sts = cmd_sts;
981
982 /* ensure all descriptors are written before poking hardware */
983 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200984 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700985
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700986 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700987}
988
989/**
990 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 */
993static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
994{
995 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700996 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700999 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -07001000
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001001 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1002 stats->tx_dropped++;
1003 printk(KERN_DEBUG "%s: failed to linearize tiny "
1004 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001005 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -07001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_lock_irqsave(&mp->lock, flags);
1009
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001010 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1011 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1012 netif_stop_queue(dev);
1013 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001014 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001015 }
1016
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001017 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -07001018 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 stats->tx_packets++;
1020 dev->trans_start = jiffies;
1021
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001022 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1023 netif_stop_queue(dev);
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 spin_unlock_irqrestore(&mp->lock, flags);
1026
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001027 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028}
1029
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001030
1031/* mii management interface *************************************************/
1032static int ethernet_phy_get(struct mv643xx_private *mp);
1033
1034/*
1035 * eth_port_read_smi_reg - Read PHY registers
1036 *
1037 * DESCRIPTION:
1038 * This routine utilize the SMI interface to interact with the PHY in
1039 * order to perform PHY register read.
1040 *
1041 * INPUT:
1042 * struct mv643xx_private *mp Ethernet Port.
1043 * unsigned int phy_reg PHY register address offset.
1044 * unsigned int *value Register value buffer.
1045 *
1046 * OUTPUT:
1047 * Write the value of a specified PHY register into given buffer.
1048 *
1049 * RETURN:
1050 * false if the PHY is busy or read data is not in valid state.
1051 * true otherwise.
1052 *
1053 */
1054static void eth_port_read_smi_reg(struct mv643xx_private *mp,
1055 unsigned int phy_reg, unsigned int *value)
1056{
1057 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1058 int phy_addr = ethernet_phy_get(mp);
1059 unsigned long flags;
1060 int i;
1061
1062 /* the SMI register is a shared resource */
1063 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1064
1065 /* wait for the SMI register to become available */
1066 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001067 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001068 printk("%s: PHY busy timeout\n", mp->dev->name);
1069 goto out;
1070 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001071 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001072 }
1073
1074 writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
1075 smi_reg);
1076
1077 /* now wait for the data to be valid */
1078 for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001079 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001080 printk("%s: PHY read timeout\n", mp->dev->name);
1081 goto out;
1082 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001083 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001084 }
1085
1086 *value = readl(smi_reg) & 0xffff;
1087out:
1088 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1089}
1090
1091/*
1092 * eth_port_write_smi_reg - Write to PHY registers
1093 *
1094 * DESCRIPTION:
1095 * This routine utilize the SMI interface to interact with the PHY in
1096 * order to perform writes to PHY registers.
1097 *
1098 * INPUT:
1099 * struct mv643xx_private *mp Ethernet Port.
1100 * unsigned int phy_reg PHY register address offset.
1101 * unsigned int value Register value.
1102 *
1103 * OUTPUT:
1104 * Write the given value to the specified PHY register.
1105 *
1106 * RETURN:
1107 * false if the PHY is busy.
1108 * true otherwise.
1109 *
1110 */
1111static void eth_port_write_smi_reg(struct mv643xx_private *mp,
1112 unsigned int phy_reg, unsigned int value)
1113{
1114 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1115 int phy_addr = ethernet_phy_get(mp);
1116 unsigned long flags;
1117 int i;
1118
1119 /* the SMI register is a shared resource */
1120 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1121
1122 /* wait for the SMI register to become available */
1123 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001124 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001125 printk("%s: PHY busy timeout\n", mp->dev->name);
1126 goto out;
1127 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001128 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001129 }
1130
1131 writel((phy_addr << 16) | (phy_reg << 21) |
1132 ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
1133out:
1134 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1135}
1136
1137
1138/* mib counters *************************************************************/
1139/*
1140 * eth_clear_mib_counters - Clear all MIB counters
1141 *
1142 * DESCRIPTION:
1143 * This function clears all MIB counters of a specific ethernet port.
1144 * A read from the MIB counter will reset the counter.
1145 *
1146 * INPUT:
1147 * struct mv643xx_private *mp Ethernet Port.
1148 *
1149 * OUTPUT:
1150 * After reading all MIB counters, the counters resets.
1151 *
1152 * RETURN:
1153 * MIB counter value.
1154 *
1155 */
1156static void eth_clear_mib_counters(struct mv643xx_private *mp)
1157{
1158 unsigned int port_num = mp->port_num;
1159 int i;
1160
1161 /* Perform dummy reads from MIB counters */
1162 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
1163 i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001164 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001165}
1166
1167static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1168{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001169 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001170}
1171
1172static void eth_update_mib_counters(struct mv643xx_private *mp)
1173{
1174 struct mv643xx_mib_counters *p = &mp->mib_counters;
1175 int offset;
1176
1177 p->good_octets_received +=
1178 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
1179 p->good_octets_received +=
1180 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
1181
1182 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
1183 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
1184 offset += 4)
1185 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1186
1187 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
1188 p->good_octets_sent +=
1189 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
1190
1191 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
1192 offset <= ETH_MIB_LATE_COLLISION;
1193 offset += 4)
1194 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1195}
1196
1197
1198/* ethtool ******************************************************************/
1199struct mv643xx_stats {
1200 char stat_string[ETH_GSTRING_LEN];
1201 int sizeof_stat;
1202 int stat_offset;
1203};
1204
1205#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1206 offsetof(struct mv643xx_private, m)
1207
1208static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1209 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1210 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1211 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1212 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1213 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1214 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1215 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1216 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1217 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1218 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1219 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1220 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1221 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1222 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1223 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1224 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1225 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1226 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1227 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1228 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1229 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1230 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1231 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1232 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1233 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1234 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1235 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1236 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1237 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1238 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1239 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1240 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1241 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1242 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1243 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1244 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1245 { "collision", MV643XX_STAT(mib_counters.collision) },
1246 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1247};
1248
1249#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1250
1251static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1252{
1253 struct mv643xx_private *mp = netdev_priv(dev);
1254 int err;
1255
1256 spin_lock_irq(&mp->lock);
1257 err = mii_ethtool_gset(&mp->mii, cmd);
1258 spin_unlock_irq(&mp->lock);
1259
1260 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1261 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1262 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1263
1264 return err;
1265}
1266
1267static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1268{
1269 struct mv643xx_private *mp = netdev_priv(dev);
1270 int err;
1271
1272 spin_lock_irq(&mp->lock);
1273 err = mii_ethtool_sset(&mp->mii, cmd);
1274 spin_unlock_irq(&mp->lock);
1275
1276 return err;
1277}
1278
1279static void mv643xx_get_drvinfo(struct net_device *netdev,
1280 struct ethtool_drvinfo *drvinfo)
1281{
1282 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1283 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1284 strncpy(drvinfo->fw_version, "N/A", 32);
1285 strncpy(drvinfo->bus_info, "mv643xx", 32);
1286 drvinfo->n_stats = MV643XX_STATS_LEN;
1287}
1288
1289static int mv643xx_eth_nway_restart(struct net_device *dev)
1290{
1291 struct mv643xx_private *mp = netdev_priv(dev);
1292
1293 return mii_nway_restart(&mp->mii);
1294}
1295
1296static u32 mv643xx_eth_get_link(struct net_device *dev)
1297{
1298 struct mv643xx_private *mp = netdev_priv(dev);
1299
1300 return mii_link_ok(&mp->mii);
1301}
1302
1303static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1304 uint8_t *data)
1305{
1306 int i;
1307
1308 switch(stringset) {
1309 case ETH_SS_STATS:
1310 for (i=0; i < MV643XX_STATS_LEN; i++) {
1311 memcpy(data + i * ETH_GSTRING_LEN,
1312 mv643xx_gstrings_stats[i].stat_string,
1313 ETH_GSTRING_LEN);
1314 }
1315 break;
1316 }
1317}
1318
1319static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1320 struct ethtool_stats *stats, uint64_t *data)
1321{
1322 struct mv643xx_private *mp = netdev->priv;
1323 int i;
1324
1325 eth_update_mib_counters(mp);
1326
1327 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1328 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1329 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1330 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1331 }
1332}
1333
1334static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1335{
1336 switch (sset) {
1337 case ETH_SS_STATS:
1338 return MV643XX_STATS_LEN;
1339 default:
1340 return -EOPNOTSUPP;
1341 }
1342}
1343
1344static const struct ethtool_ops mv643xx_ethtool_ops = {
1345 .get_settings = mv643xx_get_settings,
1346 .set_settings = mv643xx_set_settings,
1347 .get_drvinfo = mv643xx_get_drvinfo,
1348 .get_link = mv643xx_eth_get_link,
1349 .set_sg = ethtool_op_set_sg,
1350 .get_sset_count = mv643xx_get_sset_count,
1351 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1352 .get_strings = mv643xx_get_strings,
1353 .nway_reset = mv643xx_eth_nway_restart,
1354};
1355
1356
1357/* address handling *********************************************************/
1358/*
1359 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1360 */
1361static void eth_port_uc_addr_get(struct mv643xx_private *mp,
1362 unsigned char *p_addr)
1363{
1364 unsigned int port_num = mp->port_num;
1365 unsigned int mac_h;
1366 unsigned int mac_l;
1367
1368 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1369 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1370
1371 p_addr[0] = (mac_h >> 24) & 0xff;
1372 p_addr[1] = (mac_h >> 16) & 0xff;
1373 p_addr[2] = (mac_h >> 8) & 0xff;
1374 p_addr[3] = mac_h & 0xff;
1375 p_addr[4] = (mac_l >> 8) & 0xff;
1376 p_addr[5] = mac_l & 0xff;
1377}
1378
1379/*
1380 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1381 *
1382 * DESCRIPTION:
1383 * Go through all the DA filter tables (Unicast, Special Multicast &
1384 * Other Multicast) and set each entry to 0.
1385 *
1386 * INPUT:
1387 * struct mv643xx_private *mp Ethernet Port.
1388 *
1389 * OUTPUT:
1390 * Multicast and Unicast packets are rejected.
1391 *
1392 * RETURN:
1393 * None.
1394 */
1395static void eth_port_init_mac_tables(struct mv643xx_private *mp)
1396{
1397 unsigned int port_num = mp->port_num;
1398 int table_index;
1399
1400 /* Clear DA filter unicast table (Ex_dFUT) */
1401 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001402 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001403
1404 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1405 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001406 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001407 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001408 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001409 }
1410}
1411
1412/*
1413 * The entries in each table are indexed by a hash of a packet's MAC
1414 * address. One bit in each entry determines whether the packet is
1415 * accepted. There are 4 entries (each 8 bits wide) in each register
1416 * of the table. The bits in each entry are defined as follows:
1417 * 0 Accept=1, Drop=0
1418 * 3-1 Queue (ETH_Q0=0)
1419 * 7-4 Reserved = 0;
1420 */
1421static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
1422 int table, unsigned char entry)
1423{
1424 unsigned int table_reg;
1425 unsigned int tbl_offset;
1426 unsigned int reg_offset;
1427
1428 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1429 reg_offset = entry % 4; /* Entry offset within the register */
1430
1431 /* Set "accepts frame bit" at specified table entry */
1432 table_reg = rdl(mp, table + tbl_offset);
1433 table_reg |= 0x01 << (8 * reg_offset);
1434 wrl(mp, table + tbl_offset, table_reg);
1435}
1436
1437/*
1438 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1439 */
1440static void eth_port_uc_addr_set(struct mv643xx_private *mp,
1441 unsigned char *p_addr)
1442{
1443 unsigned int port_num = mp->port_num;
1444 unsigned int mac_h;
1445 unsigned int mac_l;
1446 int table;
1447
1448 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1449 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1450 (p_addr[3] << 0);
1451
1452 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1453 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1454
1455 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001456 table = UNICAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001457 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
1458}
1459
1460/*
1461 * mv643xx_eth_update_mac_address
1462 *
1463 * Update the MAC address of the port in the address table
1464 *
1465 * Input : pointer to ethernet interface network device structure
1466 * Output : N/A
1467 */
1468static void mv643xx_eth_update_mac_address(struct net_device *dev)
1469{
1470 struct mv643xx_private *mp = netdev_priv(dev);
1471
1472 eth_port_init_mac_tables(mp);
1473 eth_port_uc_addr_set(mp, dev->dev_addr);
1474}
1475
1476/*
1477 * mv643xx_eth_set_mac_address
1478 *
1479 * Change the interface's mac address.
1480 * No special hardware thing should be done because interface is always
1481 * put in promiscuous mode.
1482 *
1483 * Input : pointer to ethernet interface network device structure and
1484 * a pointer to the designated entry to be added to the cache.
1485 * Output : zero upon success, negative upon failure
1486 */
1487static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1488{
1489 int i;
1490
1491 for (i = 0; i < 6; i++)
1492 /* +2 is for the offset of the HW addr type */
1493 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1494 mv643xx_eth_update_mac_address(dev);
1495 return 0;
1496}
1497
1498/*
1499 * eth_port_mc_addr - Multicast address settings.
1500 *
1501 * The MV device supports multicast using two tables:
1502 * 1) Special Multicast Table for MAC addresses of the form
1503 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1504 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1505 * Table entries in the DA-Filter table.
1506 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1507 * is used as an index to the Other Multicast Table entries in the
1508 * DA-Filter table. This function calculates the CRC-8bit value.
1509 * In either case, eth_port_set_filter_table_entry() is then called
1510 * to set to set the actual table entry.
1511 */
1512static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
1513{
1514 unsigned int port_num = mp->port_num;
1515 unsigned int mac_h;
1516 unsigned int mac_l;
1517 unsigned char crc_result = 0;
1518 int table;
1519 int mac_array[48];
1520 int crc[8];
1521 int i;
1522
1523 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1524 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001525 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001526 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
1527 return;
1528 }
1529
1530 /* Calculate CRC-8 out of the given address */
1531 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1532 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1533 (p_addr[4] << 8) | (p_addr[5] << 0);
1534
1535 for (i = 0; i < 32; i++)
1536 mac_array[i] = (mac_l >> i) & 0x1;
1537 for (i = 32; i < 48; i++)
1538 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1539
1540 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1541 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1542 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1543 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1544 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1545
1546 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1547 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1548 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1549 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1550 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1551 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1552 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1553
1554 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1555 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1556 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1557 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1558 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1559 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1560
1561 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1562 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1563 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1564 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1565 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1566 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1567
1568 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1569 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1570 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1571 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1572 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1573 mac_array[3] ^ mac_array[2];
1574
1575 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1576 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1577 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1578 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1579 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1580 mac_array[4] ^ mac_array[3];
1581
1582 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1583 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1584 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1585 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1586 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1587 mac_array[4];
1588
1589 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1590 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1591 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1592 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1593 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1594
1595 for (i = 0; i < 8; i++)
1596 crc_result = crc_result | (crc[i] << i);
1597
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001598 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001599 eth_port_set_filter_table_entry(mp, table, crc_result);
1600}
1601
1602/*
1603 * Set the entire multicast list based on dev->mc_list.
1604 */
1605static void eth_port_set_multicast_list(struct net_device *dev)
1606{
1607
1608 struct dev_mc_list *mc_list;
1609 int i;
1610 int table_index;
1611 struct mv643xx_private *mp = netdev_priv(dev);
1612 unsigned int eth_port_num = mp->port_num;
1613
1614 /* If the device is in promiscuous mode or in all multicast mode,
1615 * we will fully populate both multicast tables with accept.
1616 * This is guaranteed to yield a match on all multicast addresses...
1617 */
1618 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1619 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1620 /* Set all entries in DA filter special multicast
1621 * table (Ex_dFSMT)
1622 * Set for ETH_Q0 for now
1623 * Bits
1624 * 0 Accept=1, Drop=0
1625 * 3-1 Queue ETH_Q0=0
1626 * 7-4 Reserved = 0;
1627 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001628 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001629
1630 /* Set all entries in DA filter other multicast
1631 * table (Ex_dFOMT)
1632 * Set for ETH_Q0 for now
1633 * Bits
1634 * 0 Accept=1, Drop=0
1635 * 3-1 Queue ETH_Q0=0
1636 * 7-4 Reserved = 0;
1637 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001638 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001639 }
1640 return;
1641 }
1642
1643 /* We will clear out multicast tables every time we get the list.
1644 * Then add the entire new list...
1645 */
1646 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1647 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001648 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001649
1650 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001651 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001652 }
1653
1654 /* Get pointer to net_device multicast list and add each one... */
1655 for (i = 0, mc_list = dev->mc_list;
1656 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1657 i++, mc_list = mc_list->next)
1658 if (mc_list->dmi_addrlen == 6)
1659 eth_port_mc_addr(mp, mc_list->dmi_addr);
1660}
1661
1662/*
1663 * mv643xx_eth_set_rx_mode
1664 *
1665 * Change from promiscuos to regular rx mode
1666 *
1667 * Input : pointer to ethernet interface network device structure
1668 * Output : N/A
1669 */
1670static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1671{
1672 struct mv643xx_private *mp = netdev_priv(dev);
1673 u32 config_reg;
1674
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001675 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001676 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001677 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001678 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001679 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001680 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001681
1682 eth_port_set_multicast_list(dev);
1683}
1684
1685
1686/* rx/tx queue initialisation ***********************************************/
1687/*
1688 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1689 *
1690 * DESCRIPTION:
1691 * This function prepares a Rx chained list of descriptors and packet
1692 * buffers in a form of a ring. The routine must be called after port
1693 * initialization routine and before port start routine.
1694 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1695 * devices in the system (i.e. DRAM). This function uses the ethernet
1696 * struct 'virtual to physical' routine (set by the user) to set the ring
1697 * with physical addresses.
1698 *
1699 * INPUT:
1700 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1701 *
1702 * OUTPUT:
1703 * The routine updates the Ethernet port control struct with information
1704 * regarding the Rx descriptors and buffers.
1705 *
1706 * RETURN:
1707 * None.
1708 */
1709static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1710{
1711 volatile struct eth_rx_desc *p_rx_desc;
1712 int rx_desc_num = mp->rx_ring_size;
1713 int i;
1714
1715 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1716 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1717 for (i = 0; i < rx_desc_num; i++) {
1718 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1719 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1720 }
1721
1722 /* Save Rx desc pointer to driver struct. */
1723 mp->rx_curr_desc_q = 0;
1724 mp->rx_used_desc_q = 0;
1725
1726 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1727}
1728
1729static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1730{
1731 struct mv643xx_private *mp = netdev_priv(dev);
1732 int curr;
1733
1734 /* Stop RX Queues */
1735 mv643xx_eth_port_disable_rx(mp);
1736
1737 /* Free preallocated skb's on RX rings */
1738 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1739 if (mp->rx_skb[curr]) {
1740 dev_kfree_skb(mp->rx_skb[curr]);
1741 mp->rx_desc_count--;
1742 }
1743 }
1744
1745 if (mp->rx_desc_count)
1746 printk(KERN_ERR
1747 "%s: Error in freeing Rx Ring. %d skb's still"
1748 " stuck in RX Ring - ignoring them\n", dev->name,
1749 mp->rx_desc_count);
1750 /* Free RX ring */
1751 if (mp->rx_sram_size)
1752 iounmap(mp->p_rx_desc_area);
1753 else
1754 dma_free_coherent(NULL, mp->rx_desc_area_size,
1755 mp->p_rx_desc_area, mp->rx_desc_dma);
1756}
1757
1758/*
1759 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1760 *
1761 * DESCRIPTION:
1762 * This function prepares a Tx chained list of descriptors and packet
1763 * buffers in a form of a ring. The routine must be called after port
1764 * initialization routine and before port start routine.
1765 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1766 * devices in the system (i.e. DRAM). This function uses the ethernet
1767 * struct 'virtual to physical' routine (set by the user) to set the ring
1768 * with physical addresses.
1769 *
1770 * INPUT:
1771 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1772 *
1773 * OUTPUT:
1774 * The routine updates the Ethernet port control struct with information
1775 * regarding the Tx descriptors and buffers.
1776 *
1777 * RETURN:
1778 * None.
1779 */
1780static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1781{
1782 int tx_desc_num = mp->tx_ring_size;
1783 struct eth_tx_desc *p_tx_desc;
1784 int i;
1785
1786 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1787 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1788 for (i = 0; i < tx_desc_num; i++) {
1789 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1790 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1791 }
1792
1793 mp->tx_curr_desc_q = 0;
1794 mp->tx_used_desc_q = 0;
1795
1796 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1797}
1798
1799/**
1800 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1801 *
1802 * If force is non-zero, frees uncompleted descriptors as well
1803 */
1804static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1805{
1806 struct mv643xx_private *mp = netdev_priv(dev);
1807 struct eth_tx_desc *desc;
1808 u32 cmd_sts;
1809 struct sk_buff *skb;
1810 unsigned long flags;
1811 int tx_index;
1812 dma_addr_t addr;
1813 int count;
1814 int released = 0;
1815
1816 while (mp->tx_desc_count > 0) {
1817 spin_lock_irqsave(&mp->lock, flags);
1818
1819 /* tx_desc_count might have changed before acquiring the lock */
1820 if (mp->tx_desc_count <= 0) {
1821 spin_unlock_irqrestore(&mp->lock, flags);
1822 return released;
1823 }
1824
1825 tx_index = mp->tx_used_desc_q;
1826 desc = &mp->p_tx_desc_area[tx_index];
1827 cmd_sts = desc->cmd_sts;
1828
1829 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
1830 spin_unlock_irqrestore(&mp->lock, flags);
1831 return released;
1832 }
1833
1834 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1835 mp->tx_desc_count--;
1836
1837 addr = desc->buf_ptr;
1838 count = desc->byte_cnt;
1839 skb = mp->tx_skb[tx_index];
1840 if (skb)
1841 mp->tx_skb[tx_index] = NULL;
1842
1843 if (cmd_sts & ETH_ERROR_SUMMARY) {
1844 printk("%s: Error in TX\n", dev->name);
1845 dev->stats.tx_errors++;
1846 }
1847
1848 spin_unlock_irqrestore(&mp->lock, flags);
1849
1850 if (cmd_sts & ETH_TX_FIRST_DESC)
1851 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1852 else
1853 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1854
1855 if (skb)
1856 dev_kfree_skb_irq(skb);
1857
1858 released = 1;
1859 }
1860
1861 return released;
1862}
1863
1864static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1865{
1866 struct mv643xx_private *mp = netdev_priv(dev);
1867
1868 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1869 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1870 netif_wake_queue(dev);
1871}
1872
1873static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1874{
1875 mv643xx_eth_free_tx_descs(dev, 1);
1876}
1877
1878static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1879{
1880 struct mv643xx_private *mp = netdev_priv(dev);
1881
1882 /* Stop Tx Queues */
1883 mv643xx_eth_port_disable_tx(mp);
1884
1885 /* Free outstanding skb's on TX ring */
1886 mv643xx_eth_free_all_tx_descs(dev);
1887
1888 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1889
1890 /* Free TX ring */
1891 if (mp->tx_sram_size)
1892 iounmap(mp->p_tx_desc_area);
1893 else
1894 dma_free_coherent(NULL, mp->tx_desc_area_size,
1895 mp->p_tx_desc_area, mp->tx_desc_dma);
1896}
1897
1898
1899/* netdev ops and related ***************************************************/
1900static void eth_port_reset(struct mv643xx_private *mp);
1901
1902/* Set the mv643xx port configuration register for the speed/duplex mode. */
1903static void mv643xx_eth_update_pscr(struct net_device *dev,
1904 struct ethtool_cmd *ecmd)
1905{
1906 struct mv643xx_private *mp = netdev_priv(dev);
1907 int port_num = mp->port_num;
1908 u32 o_pscr, n_pscr;
1909 unsigned int queues;
1910
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001911 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001912 n_pscr = o_pscr;
1913
1914 /* clear speed, duplex and rx buffer size fields */
1915 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1916 SET_GMII_SPEED_TO_1000 |
1917 SET_FULL_DUPLEX_MODE |
1918 MAX_RX_PACKET_MASK);
1919
1920 if (ecmd->duplex == DUPLEX_FULL)
1921 n_pscr |= SET_FULL_DUPLEX_MODE;
1922
1923 if (ecmd->speed == SPEED_1000)
1924 n_pscr |= SET_GMII_SPEED_TO_1000 |
1925 MAX_RX_PACKET_9700BYTE;
1926 else {
1927 if (ecmd->speed == SPEED_100)
1928 n_pscr |= SET_MII_SPEED_TO_100;
1929 n_pscr |= MAX_RX_PACKET_1522BYTE;
1930 }
1931
1932 if (n_pscr != o_pscr) {
1933 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001934 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001935 else {
1936 queues = mv643xx_eth_port_disable_tx(mp);
1937
1938 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001939 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1940 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1941 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001942 if (queues)
1943 mv643xx_eth_port_enable_tx(mp, queues);
1944 }
1945 }
1946}
1947
1948/*
1949 * mv643xx_eth_int_handler
1950 *
1951 * Main interrupt handler for the gigbit ethernet ports
1952 *
1953 * Input : irq - irq number (not used)
1954 * dev_id - a pointer to the required interface's data structure
1955 * regs - not used
1956 * Output : N/A
1957 */
1958
1959static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1960{
1961 struct net_device *dev = (struct net_device *)dev_id;
1962 struct mv643xx_private *mp = netdev_priv(dev);
1963 u32 eth_int_cause, eth_int_cause_ext = 0;
1964 unsigned int port_num = mp->port_num;
1965
1966 /* Read interrupt cause registers */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001967 eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1968 if (eth_int_cause & INT_EXT) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001969 eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001970 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001971 wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001972 }
1973
1974 /* PHY status changed */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001975 if (eth_int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001976 struct ethtool_cmd cmd;
1977
1978 if (mii_link_ok(&mp->mii)) {
1979 mii_ethtool_gset(&mp->mii, &cmd);
1980 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001981 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001982 if (!netif_carrier_ok(dev)) {
1983 netif_carrier_on(dev);
1984 if (mp->tx_ring_size - mp->tx_desc_count >=
1985 MAX_DESCS_PER_SKB)
1986 netif_wake_queue(dev);
1987 }
1988 } else if (netif_carrier_ok(dev)) {
1989 netif_stop_queue(dev);
1990 netif_carrier_off(dev);
1991 }
1992 }
1993
1994#ifdef MV643XX_NAPI
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001995 if (eth_int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001996 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001997 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001998
1999 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002000 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002001
2002 netif_rx_schedule(dev, &mp->napi);
2003 }
2004#else
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002005 if (eth_int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002006 mv643xx_eth_receive_queue(dev, INT_MAX);
2007#endif
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002008 if (eth_int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002009 mv643xx_eth_free_completed_tx_descs(dev);
2010
2011 /*
2012 * If no real interrupt occured, exit.
2013 * This can happen when using gigE interrupt coalescing mechanism.
2014 */
2015 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
2016 return IRQ_NONE;
2017
2018 return IRQ_HANDLED;
2019}
2020
2021/*
2022 * ethernet_phy_reset - Reset Ethernet port PHY.
2023 *
2024 * DESCRIPTION:
2025 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2026 *
2027 * INPUT:
2028 * struct mv643xx_private *mp Ethernet Port.
2029 *
2030 * OUTPUT:
2031 * The PHY is reset.
2032 *
2033 * RETURN:
2034 * None.
2035 *
2036 */
2037static void ethernet_phy_reset(struct mv643xx_private *mp)
2038{
2039 unsigned int phy_reg_data;
2040
2041 /* Reset the PHY */
2042 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2043 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2044 eth_port_write_smi_reg(mp, 0, phy_reg_data);
2045
2046 /* wait for PHY to come out of reset */
2047 do {
2048 udelay(1);
2049 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2050 } while (phy_reg_data & 0x8000);
2051}
2052
2053/*
2054 * eth_port_start - Start the Ethernet port activity.
2055 *
2056 * DESCRIPTION:
2057 * This routine prepares the Ethernet port for Rx and Tx activity:
2058 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2059 * has been initialized a descriptor's ring (using
2060 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2061 * 2. Initialize and enable the Ethernet configuration port by writing to
2062 * the port's configuration and command registers.
2063 * 3. Initialize and enable the SDMA by writing to the SDMA's
2064 * configuration and command registers. After completing these steps,
2065 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2066 *
2067 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2068 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2069 * and ether_init_rx_desc_ring for Rx queues).
2070 *
2071 * INPUT:
2072 * dev - a pointer to the required interface
2073 *
2074 * OUTPUT:
2075 * Ethernet port is ready to receive and transmit.
2076 *
2077 * RETURN:
2078 * None.
2079 */
2080static void eth_port_start(struct net_device *dev)
2081{
2082 struct mv643xx_private *mp = netdev_priv(dev);
2083 unsigned int port_num = mp->port_num;
2084 int tx_curr_desc, rx_curr_desc;
2085 u32 pscr;
2086 struct ethtool_cmd ethtool_cmd;
2087
2088 /* Assignment of Tx CTRP of given queue */
2089 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002090 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002091 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2092
2093 /* Assignment of Rx CRDP of given queue */
2094 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002095 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002096 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2097
2098 /* Add the assigned Ethernet address to the port's address table */
2099 eth_port_uc_addr_set(mp, dev->dev_addr);
2100
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002101 /*
2102 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2103 * frames to RX queue #0.
2104 */
2105 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002106
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002107 /*
2108 * Treat BPDUs as normal multicasts, and disable partition mode.
2109 */
2110 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002111
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002112 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002113
2114 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002115 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002116
2117 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2118 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02002119 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002120 DO_NOT_FORCE_LINK_FAIL |
2121 SERIAL_PORT_CONTROL_RESERVED;
2122
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002123 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002124
2125 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002126 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002127
2128 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002129 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002130
2131 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002132 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002133
2134 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002135 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002136
2137 /* save phy settings across reset */
2138 mv643xx_get_settings(dev, &ethtool_cmd);
2139 ethernet_phy_reset(mp);
2140 mv643xx_set_settings(dev, &ethtool_cmd);
2141}
2142
2143#ifdef MV643XX_COAL
2144
2145/*
2146 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
2147 *
2148 * DESCRIPTION:
2149 * This routine sets the RX coalescing interrupt mechanism parameter.
2150 * This parameter is a timeout counter, that counts in 64 t_clk
2151 * chunks ; that when timeout event occurs a maskable interrupt
2152 * occurs.
2153 * The parameter is calculated using the tClk of the MV-643xx chip
2154 * , and the required delay of the interrupt in usec.
2155 *
2156 * INPUT:
2157 * struct mv643xx_private *mp Ethernet port
2158 * unsigned int delay Delay in usec
2159 *
2160 * OUTPUT:
2161 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2162 *
2163 * RETURN:
2164 * The interrupt coalescing value set in the gigE port.
2165 *
2166 */
2167static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
2168 unsigned int delay)
2169{
2170 unsigned int port_num = mp->port_num;
2171 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2172
2173 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002174 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002175 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002176 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002177 & 0xffc000ff));
2178
2179 return coal;
2180}
2181#endif
2182
2183/*
2184 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
2185 *
2186 * DESCRIPTION:
2187 * This routine sets the TX coalescing interrupt mechanism parameter.
2188 * This parameter is a timeout counter, that counts in 64 t_clk
2189 * chunks ; that when timeout event occurs a maskable interrupt
2190 * occurs.
2191 * The parameter is calculated using the t_cLK frequency of the
2192 * MV-643xx chip and the required delay in the interrupt in uSec
2193 *
2194 * INPUT:
2195 * struct mv643xx_private *mp Ethernet port
2196 * unsigned int delay Delay in uSeconds
2197 *
2198 * OUTPUT:
2199 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2200 *
2201 * RETURN:
2202 * The interrupt coalescing value set in the gigE port.
2203 *
2204 */
2205static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
2206 unsigned int delay)
2207{
2208 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2209
2210 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002211 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002212
2213 return coal;
2214}
2215
2216/*
2217 * eth_port_init - Initialize the Ethernet port driver
2218 *
2219 * DESCRIPTION:
2220 * This function prepares the ethernet port to start its activity:
2221 * 1) Completes the ethernet port driver struct initialization toward port
2222 * start routine.
2223 * 2) Resets the device to a quiescent state in case of warm reboot.
2224 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2225 * 4) Clean MAC tables. The reset status of those tables is unknown.
2226 * 5) Set PHY address.
2227 * Note: Call this routine prior to eth_port_start routine and after
2228 * setting user values in the user fields of Ethernet port control
2229 * struct.
2230 *
2231 * INPUT:
2232 * struct mv643xx_private *mp Ethernet port control struct
2233 *
2234 * OUTPUT:
2235 * See description.
2236 *
2237 * RETURN:
2238 * None.
2239 */
2240static void eth_port_init(struct mv643xx_private *mp)
2241{
2242 mp->rx_resource_err = 0;
2243
2244 eth_port_reset(mp);
2245
2246 eth_port_init_mac_tables(mp);
2247}
2248
2249/*
2250 * mv643xx_eth_open
2251 *
2252 * This function is called when openning the network device. The function
2253 * should initialize all the hardware, initialize cyclic Rx/Tx
2254 * descriptors chain and buffers and allocate an IRQ to the network
2255 * device.
2256 *
2257 * Input : a pointer to the network device structure
2258 *
2259 * Output : zero of success , nonzero if fails.
2260 */
2261
2262static int mv643xx_eth_open(struct net_device *dev)
2263{
2264 struct mv643xx_private *mp = netdev_priv(dev);
2265 unsigned int port_num = mp->port_num;
2266 unsigned int size;
2267 int err;
2268
2269 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002270 wrl(mp, INT_CAUSE(port_num), 0);
2271 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002272 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002273 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002274
2275 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2276 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2277 if (err) {
2278 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2279 return -EAGAIN;
2280 }
2281
2282 eth_port_init(mp);
2283
2284 memset(&mp->timeout, 0, sizeof(struct timer_list));
2285 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2286 mp->timeout.data = (unsigned long)dev;
2287
2288 /* Allocate RX and TX skb rings */
2289 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2290 GFP_KERNEL);
2291 if (!mp->rx_skb) {
2292 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2293 err = -ENOMEM;
2294 goto out_free_irq;
2295 }
2296 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2297 GFP_KERNEL);
2298 if (!mp->tx_skb) {
2299 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2300 err = -ENOMEM;
2301 goto out_free_rx_skb;
2302 }
2303
2304 /* Allocate TX ring */
2305 mp->tx_desc_count = 0;
2306 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
2307 mp->tx_desc_area_size = size;
2308
2309 if (mp->tx_sram_size) {
2310 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2311 mp->tx_sram_size);
2312 mp->tx_desc_dma = mp->tx_sram_addr;
2313 } else
2314 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2315 &mp->tx_desc_dma,
2316 GFP_KERNEL);
2317
2318 if (!mp->p_tx_desc_area) {
2319 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2320 dev->name, size);
2321 err = -ENOMEM;
2322 goto out_free_tx_skb;
2323 }
2324 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2325 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2326
2327 ether_init_tx_desc_ring(mp);
2328
2329 /* Allocate RX ring */
2330 mp->rx_desc_count = 0;
2331 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
2332 mp->rx_desc_area_size = size;
2333
2334 if (mp->rx_sram_size) {
2335 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2336 mp->rx_sram_size);
2337 mp->rx_desc_dma = mp->rx_sram_addr;
2338 } else
2339 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2340 &mp->rx_desc_dma,
2341 GFP_KERNEL);
2342
2343 if (!mp->p_rx_desc_area) {
2344 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2345 dev->name, size);
2346 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2347 dev->name);
2348 if (mp->rx_sram_size)
2349 iounmap(mp->p_tx_desc_area);
2350 else
2351 dma_free_coherent(NULL, mp->tx_desc_area_size,
2352 mp->p_tx_desc_area, mp->tx_desc_dma);
2353 err = -ENOMEM;
2354 goto out_free_tx_skb;
2355 }
2356 memset((void *)mp->p_rx_desc_area, 0, size);
2357
2358 ether_init_rx_desc_ring(mp);
2359
2360 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2361
2362#ifdef MV643XX_NAPI
2363 napi_enable(&mp->napi);
2364#endif
2365
2366 eth_port_start(dev);
2367
2368 /* Interrupt Coalescing */
2369
2370#ifdef MV643XX_COAL
2371 mp->rx_int_coal =
2372 eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
2373#endif
2374
2375 mp->tx_int_coal =
2376 eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
2377
2378 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002379 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002380
2381 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002382 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002383
2384 return 0;
2385
2386out_free_tx_skb:
2387 kfree(mp->tx_skb);
2388out_free_rx_skb:
2389 kfree(mp->rx_skb);
2390out_free_irq:
2391 free_irq(dev->irq, dev);
2392
2393 return err;
2394}
2395
2396/*
2397 * eth_port_reset - Reset Ethernet port
2398 *
2399 * DESCRIPTION:
2400 * This routine resets the chip by aborting any SDMA engine activity and
2401 * clearing the MIB counters. The Receiver and the Transmit unit are in
2402 * idle state after this command is performed and the port is disabled.
2403 *
2404 * INPUT:
2405 * struct mv643xx_private *mp Ethernet Port.
2406 *
2407 * OUTPUT:
2408 * Channel activity is halted.
2409 *
2410 * RETURN:
2411 * None.
2412 *
2413 */
2414static void eth_port_reset(struct mv643xx_private *mp)
2415{
2416 unsigned int port_num = mp->port_num;
2417 unsigned int reg_data;
2418
2419 mv643xx_eth_port_disable_tx(mp);
2420 mv643xx_eth_port_disable_rx(mp);
2421
2422 /* Clear all MIB counters */
2423 eth_clear_mib_counters(mp);
2424
2425 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002426 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002427 reg_data &= ~(SERIAL_PORT_ENABLE |
2428 DO_NOT_FORCE_LINK_FAIL |
2429 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002430 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002431}
2432
2433/*
2434 * mv643xx_eth_stop
2435 *
2436 * This function is used when closing the network device.
2437 * It updates the hardware,
2438 * release all memory that holds buffers and descriptors and release the IRQ.
2439 * Input : a pointer to the device structure
2440 * Output : zero if success , nonzero if fails
2441 */
2442
2443static int mv643xx_eth_stop(struct net_device *dev)
2444{
2445 struct mv643xx_private *mp = netdev_priv(dev);
2446 unsigned int port_num = mp->port_num;
2447
2448 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002449 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002450 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002451 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002452
2453#ifdef MV643XX_NAPI
2454 napi_disable(&mp->napi);
2455#endif
2456 netif_carrier_off(dev);
2457 netif_stop_queue(dev);
2458
2459 eth_port_reset(mp);
2460
2461 mv643xx_eth_free_tx_rings(dev);
2462 mv643xx_eth_free_rx_rings(dev);
2463
2464 free_irq(dev->irq, dev);
2465
2466 return 0;
2467}
2468
2469static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2470{
2471 struct mv643xx_private *mp = netdev_priv(dev);
2472
2473 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2474}
2475
2476/*
2477 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2478 *
2479 * Input : pointer to ethernet interface network device structure
2480 * new mtu size
2481 * Output : 0 upon success, -EINVAL upon failure
2482 */
2483static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2484{
2485 if ((new_mtu > 9500) || (new_mtu < 64))
2486 return -EINVAL;
2487
2488 dev->mtu = new_mtu;
2489 if (!netif_running(dev))
2490 return 0;
2491
2492 /*
2493 * Stop and then re-open the interface. This will allocate RX
2494 * skbs of the new MTU.
2495 * There is a possible danger that the open will not succeed,
2496 * due to memory being full, which might fail the open function.
2497 */
2498 mv643xx_eth_stop(dev);
2499 if (mv643xx_eth_open(dev)) {
2500 printk(KERN_ERR "%s: Fatal error on opening device\n",
2501 dev->name);
2502 }
2503
2504 return 0;
2505}
2506
2507/*
2508 * mv643xx_eth_tx_timeout_task
2509 *
2510 * Actual routine to reset the adapter when a timeout on Tx has occurred
2511 */
2512static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2513{
2514 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2515 tx_timeout_task);
2516 struct net_device *dev = mp->dev;
2517
2518 if (!netif_running(dev))
2519 return;
2520
2521 netif_stop_queue(dev);
2522
2523 eth_port_reset(mp);
2524 eth_port_start(dev);
2525
2526 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2527 netif_wake_queue(dev);
2528}
2529
2530/*
2531 * mv643xx_eth_tx_timeout
2532 *
2533 * Called upon a timeout on transmitting a packet
2534 *
2535 * Input : pointer to ethernet interface network device structure.
2536 * Output : N/A
2537 */
2538static void mv643xx_eth_tx_timeout(struct net_device *dev)
2539{
2540 struct mv643xx_private *mp = netdev_priv(dev);
2541
2542 printk(KERN_INFO "%s: TX timeout ", dev->name);
2543
2544 /* Do the reset outside of interrupt context */
2545 schedule_work(&mp->tx_timeout_task);
2546}
2547
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002548#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002549static void mv643xx_netpoll(struct net_device *netdev)
2550{
2551 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002552 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002553
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002554 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002555 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002556 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002557
Al Viro9da3b1a2006-10-08 15:00:44 +01002558 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002559
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002560 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002561}
2562#endif
2563
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002564/*
2565 * Wrappers for MII support library.
2566 */
2567static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2568{
2569 struct mv643xx_private *mp = netdev_priv(dev);
2570 int val;
2571
2572 eth_port_read_smi_reg(mp, location, &val);
2573 return val;
2574}
2575
2576static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2577{
2578 struct mv643xx_private *mp = netdev_priv(dev);
2579 eth_port_write_smi_reg(mp, location, val);
2580}
2581
2582
2583/* platform glue ************************************************************/
2584static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2585 struct mbus_dram_target_info *dram)
2586{
2587 void __iomem *base = msp->eth_base;
2588 u32 win_enable;
2589 u32 win_protect;
2590 int i;
2591
2592 for (i = 0; i < 6; i++) {
2593 writel(0, base + WINDOW_BASE(i));
2594 writel(0, base + WINDOW_SIZE(i));
2595 if (i < 4)
2596 writel(0, base + WINDOW_REMAP_HIGH(i));
2597 }
2598
2599 win_enable = 0x3f;
2600 win_protect = 0;
2601
2602 for (i = 0; i < dram->num_cs; i++) {
2603 struct mbus_dram_window *cs = dram->cs + i;
2604
2605 writel((cs->base & 0xffff0000) |
2606 (cs->mbus_attr << 8) |
2607 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2608 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2609
2610 win_enable &= ~(1 << i);
2611 win_protect |= 3 << (2 * i);
2612 }
2613
2614 writel(win_enable, base + WINDOW_BAR_ENABLE);
2615 msp->win_protect = win_protect;
2616}
2617
2618static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2619{
2620 static int mv643xx_version_printed = 0;
2621 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2622 struct mv643xx_shared_private *msp;
2623 struct resource *res;
2624 int ret;
2625
2626 if (!mv643xx_version_printed++)
2627 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2628
2629 ret = -EINVAL;
2630 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2631 if (res == NULL)
2632 goto out;
2633
2634 ret = -ENOMEM;
2635 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2636 if (msp == NULL)
2637 goto out;
2638 memset(msp, 0, sizeof(*msp));
2639
2640 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2641 if (msp->eth_base == NULL)
2642 goto out_free;
2643
2644 spin_lock_init(&msp->phy_lock);
2645 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2646
2647 platform_set_drvdata(pdev, msp);
2648
2649 /*
2650 * (Re-)program MBUS remapping windows if we are asked to.
2651 */
2652 if (pd != NULL && pd->dram != NULL)
2653 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2654
2655 return 0;
2656
2657out_free:
2658 kfree(msp);
2659out:
2660 return ret;
2661}
2662
2663static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2664{
2665 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2666
2667 iounmap(msp->eth_base);
2668 kfree(msp);
2669
2670 return 0;
2671}
2672
2673static struct platform_driver mv643xx_eth_shared_driver = {
2674 .probe = mv643xx_eth_shared_probe,
2675 .remove = mv643xx_eth_shared_remove,
2676 .driver = {
2677 .name = MV643XX_ETH_SHARED_NAME,
2678 .owner = THIS_MODULE,
2679 },
2680};
2681
2682/*
2683 * ethernet_phy_set - Set the ethernet port PHY address.
2684 *
2685 * DESCRIPTION:
2686 * This routine sets the given ethernet port PHY address.
2687 *
2688 * INPUT:
2689 * struct mv643xx_private *mp Ethernet Port.
2690 * int phy_addr PHY address.
2691 *
2692 * OUTPUT:
2693 * None.
2694 *
2695 * RETURN:
2696 * None.
2697 *
2698 */
2699static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
2700{
2701 u32 reg_data;
2702 int addr_shift = 5 * mp->port_num;
2703
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002704 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002705 reg_data &= ~(0x1f << addr_shift);
2706 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002707 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002708}
2709
2710/*
2711 * ethernet_phy_get - Get the ethernet port PHY address.
2712 *
2713 * DESCRIPTION:
2714 * This routine returns the given ethernet port PHY address.
2715 *
2716 * INPUT:
2717 * struct mv643xx_private *mp Ethernet Port.
2718 *
2719 * OUTPUT:
2720 * None.
2721 *
2722 * RETURN:
2723 * PHY address.
2724 *
2725 */
2726static int ethernet_phy_get(struct mv643xx_private *mp)
2727{
2728 unsigned int reg_data;
2729
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002730 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002731
2732 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2733}
2734
2735/*
2736 * ethernet_phy_detect - Detect whether a phy is present
2737 *
2738 * DESCRIPTION:
2739 * This function tests whether there is a PHY present on
2740 * the specified port.
2741 *
2742 * INPUT:
2743 * struct mv643xx_private *mp Ethernet Port.
2744 *
2745 * OUTPUT:
2746 * None
2747 *
2748 * RETURN:
2749 * 0 on success
2750 * -ENODEV on failure
2751 *
2752 */
2753static int ethernet_phy_detect(struct mv643xx_private *mp)
2754{
2755 unsigned int phy_reg_data0;
2756 int auto_neg;
2757
2758 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2759 auto_neg = phy_reg_data0 & 0x1000;
2760 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2761 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2762
2763 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2764 if ((phy_reg_data0 & 0x1000) == auto_neg)
2765 return -ENODEV; /* change didn't take */
2766
2767 phy_reg_data0 ^= 0x1000;
2768 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2769 return 0;
2770}
2771
James Chapmand0412d92006-01-27 01:15:30 -07002772static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2773 int speed, int duplex,
2774 struct ethtool_cmd *cmd)
2775{
2776 struct mv643xx_private *mp = netdev_priv(dev);
2777
2778 memset(cmd, 0, sizeof(*cmd));
2779
2780 cmd->port = PORT_MII;
2781 cmd->transceiver = XCVR_INTERNAL;
2782 cmd->phy_address = phy_address;
2783
2784 if (speed == 0) {
2785 cmd->autoneg = AUTONEG_ENABLE;
2786 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2787 cmd->speed = SPEED_100;
2788 cmd->advertising = ADVERTISED_10baseT_Half |
2789 ADVERTISED_10baseT_Full |
2790 ADVERTISED_100baseT_Half |
2791 ADVERTISED_100baseT_Full;
2792 if (mp->mii.supports_gmii)
2793 cmd->advertising |= ADVERTISED_1000baseT_Full;
2794 } else {
2795 cmd->autoneg = AUTONEG_DISABLE;
2796 cmd->speed = speed;
2797 cmd->duplex = duplex;
2798 }
2799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801/*/
2802 * mv643xx_eth_probe
2803 *
2804 * First function called after registering the network device.
2805 * It's purpose is to initialize the device as an ethernet device,
2806 * fill the ethernet device structure with pointers * to functions,
2807 * and set the MAC address of the interface
2808 *
2809 * Input : struct device *
2810 * Output : -ENOMEM if failed , 0 if success
2811 */
Russell King3ae5eae2005-11-09 22:32:44 +00002812static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002815 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 struct mv643xx_private *mp;
2817 struct net_device *dev;
2818 u8 *p;
2819 struct resource *res;
2820 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002821 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002822 int duplex = DUPLEX_HALF;
2823 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002824 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002826 pd = pdev->dev.platform_data;
2827 if (pd == NULL) {
2828 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2829 return -ENODEV;
2830 }
2831
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002832 if (pd->shared == NULL) {
2833 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2834 return -ENODEV;
2835 }
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2838 if (!dev)
2839 return -ENOMEM;
2840
Russell King3ae5eae2005-11-09 22:32:44 +00002841 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842
2843 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002844 mp->dev = dev;
2845#ifdef MV643XX_NAPI
2846 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2847#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2850 BUG_ON(!res);
2851 dev->irq = res->start;
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 dev->open = mv643xx_eth_open;
2854 dev->stop = mv643xx_eth_stop;
2855 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 dev->set_mac_address = mv643xx_eth_set_mac_address;
2857 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2858
2859 /* No need to Tx Timeout */
2860 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002862#ifdef CONFIG_NET_POLL_CONTROLLER
2863 dev->poll_controller = mv643xx_netpoll;
2864#endif
2865
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 dev->base_addr = 0;
2868 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002869 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
2871
2872#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2873#ifdef MAX_SKB_FRAGS
2874 /*
2875 * Zero copy can only work if we use Discovery II memory. Else, we will
2876 * have to map the buffers to ISA memory which is only 16 MB
2877 */
Wolfram Joost63890572006-01-16 16:57:41 -07002878 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879#endif
2880#endif
2881
2882 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002883 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 spin_lock_init(&mp->lock);
2886
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002887 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002888 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002889
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002890 if (mp->shared->win_protect)
2891 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2892
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002893 mp->shared_smi = mp->shared;
2894 if (pd->shared_smi != NULL)
2895 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002898 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002899 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
2900 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002902 if (is_valid_ether_addr(pd->mac_addr))
2903 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002905 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002906 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002908 if (pd->rx_queue_size)
2909 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002911 if (pd->tx_queue_size)
2912 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002914 if (pd->tx_sram_size) {
2915 mp->tx_sram_size = pd->tx_sram_size;
2916 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 }
2918
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002919 if (pd->rx_sram_size) {
2920 mp->rx_sram_size = pd->rx_sram_size;
2921 mp->rx_sram_addr = pd->rx_sram_addr;
2922 }
2923
2924 duplex = pd->duplex;
2925 speed = pd->speed;
2926
James Chapmanc28a4f82006-01-27 01:13:15 -07002927 /* Hook up MII support for ethtool */
2928 mp->mii.dev = dev;
2929 mp->mii.mdio_read = mv643xx_mdio_read;
2930 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002931 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002932 mp->mii.phy_id_mask = 0x3f;
2933 mp->mii.reg_num_mask = 0x1f;
2934
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002935 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002937 pr_debug("%s: No PHY detected at addr %d\n",
2938 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002939 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 }
2941
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002942 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002943 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002944 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2945 mv643xx_eth_update_pscr(dev, &cmd);
2946 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002947
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002948 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 err = register_netdev(dev);
2950 if (err)
2951 goto out;
2952
2953 p = dev->dev_addr;
2954 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002955 "%s: port %d with MAC address %s\n",
2956 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 if (dev->features & NETIF_F_SG)
2959 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2960
2961 if (dev->features & NETIF_F_IP_CSUM)
2962 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2963 dev->name);
2964
2965#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2966 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2967#endif
2968
2969#ifdef MV643XX_COAL
2970 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2971 dev->name);
2972#endif
2973
2974#ifdef MV643XX_NAPI
2975 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2976#endif
2977
Nicolas DETb1529872005-10-28 17:46:30 -07002978 if (mp->tx_sram_size > 0)
2979 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return 0;
2982
2983out:
2984 free_netdev(dev);
2985
2986 return err;
2987}
2988
Russell King3ae5eae2005-11-09 22:32:44 +00002989static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990{
Russell King3ae5eae2005-11-09 22:32:44 +00002991 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 unregister_netdev(dev);
2994 flush_scheduled_work();
2995
2996 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002997 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return 0;
2999}
3000
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003001static void mv643xx_eth_shutdown(struct platform_device *pdev)
3002{
3003 struct net_device *dev = platform_get_drvdata(pdev);
3004 struct mv643xx_private *mp = netdev_priv(dev);
3005 unsigned int port_num = mp->port_num;
3006
3007 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02003008 wrl(mp, INT_MASK(port_num), 0);
3009 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003010
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003011 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003012}
3013
Russell King3ae5eae2005-11-09 22:32:44 +00003014static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 .probe = mv643xx_eth_probe,
3016 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003017 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00003018 .driver = {
3019 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07003020 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003021 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022};
3023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024/*
3025 * mv643xx_init_module
3026 *
3027 * Registers the network drivers into the Linux kernel
3028 *
3029 * Input : N/A
3030 *
3031 * Output : N/A
3032 */
3033static int __init mv643xx_init_module(void)
3034{
3035 int rc;
3036
Russell King3ae5eae2005-11-09 22:32:44 +00003037 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003039 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003041 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 }
3043 return rc;
3044}
3045
3046/*
3047 * mv643xx_cleanup_module
3048 *
3049 * Registers the network drivers into the Linux kernel
3050 *
3051 * Input : N/A
3052 *
3053 * Output : N/A
3054 */
3055static void __exit mv643xx_cleanup_module(void)
3056{
Russell King3ae5eae2005-11-09 22:32:44 +00003057 platform_driver_unregister(&mv643xx_eth_driver);
3058 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059}
3060
3061module_init(mv643xx_init_module);
3062module_exit(mv643xx_cleanup_module);
3063
3064MODULE_LICENSE("GPL");
3065MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
3066 " and Dale Farnsworth");
3067MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07003068MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3069MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);