blob: e61cfd4c052e3ed912fab24d42dde4015e5717e0 [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))
109#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
110#define INT_MASK(p) (0x0468 + ((p) << 10))
111#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
112#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
113#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
114#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
115#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
116#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
117#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
118#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
119#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200120
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200121
122/*
123 * SDMA configuration register.
124 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200125#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200126#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200127#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200129
130#if defined(__BIG_ENDIAN)
131#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
132 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133 TX_BURST_SIZE_4_64BIT
134#elif defined(__LITTLE_ENDIAN)
135#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
136 RX_BURST_SIZE_4_64BIT | \
137 BLM_RX_NO_SWAP | \
138 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200139 TX_BURST_SIZE_4_64BIT
140#else
141#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
142#endif
143
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200144
145/*
146 * Port serial control register.
147 */
148#define SET_MII_SPEED_TO_100 (1 << 24)
149#define SET_GMII_SPEED_TO_1000 (1 << 23)
150#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200151#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200152#define MAX_RX_PACKET_9700BYTE (5 << 17)
153#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200154#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
155#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
156#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
157#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
158#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
159#define FORCE_LINK_PASS (1 << 1)
160#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200161
162/* These macros describe Ethernet Serial Status reg (PSR) bits */
163#define PORT_STATUS_MODE_10_BIT (1 << 0)
164#define PORT_STATUS_LINK_UP (1 << 1)
165#define PORT_STATUS_FULL_DUPLEX (1 << 2)
166#define PORT_STATUS_FLOW_CONTROL (1 << 3)
167#define PORT_STATUS_GMII_1000 (1 << 4)
168#define PORT_STATUS_MII_100 (1 << 5)
169/* PSR bit 6 is undocumented */
170#define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
171#define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
172#define PORT_STATUS_PARTITION (1 << 9)
173#define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
174/* PSR bits 11-31 are reserved */
175
176#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
177#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
178
179#define DESC_SIZE 64
180
181#define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
182#define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
183
184#define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
185#define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
186#define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
187#define ETH_INT_CAUSE_EXT 0x00000002
188#define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
189
190#define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
191#define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
192#define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
193#define ETH_INT_CAUSE_PHY 0x00010000
194#define ETH_INT_CAUSE_STATE 0x00100000
195#define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
196 ETH_INT_CAUSE_STATE)
197
198#define ETH_INT_MASK_ALL 0x00000000
199#define ETH_INT_MASK_ALL_EXT 0x00000000
200
201#define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
202#define PHY_WAIT_MICRO_SECONDS 10
203
204/* Buffer offset from buffer pointer */
205#define RX_BUF_OFFSET 0x2
206
207/* Gigabit Ethernet Unit Global Registers */
208
209/* MIB Counters register definitions */
210#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
211#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
212#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
213#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
214#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
215#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
216#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
217#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
218#define ETH_MIB_FRAMES_64_OCTETS 0x20
219#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
220#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
221#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
222#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
223#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
224#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
225#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
226#define ETH_MIB_GOOD_FRAMES_SENT 0x40
227#define ETH_MIB_EXCESSIVE_COLLISION 0x44
228#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
229#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
230#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
231#define ETH_MIB_FC_SENT 0x54
232#define ETH_MIB_GOOD_FC_RECEIVED 0x58
233#define ETH_MIB_BAD_FC_RECEIVED 0x5c
234#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
235#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
236#define ETH_MIB_OVERSIZE_RECEIVED 0x68
237#define ETH_MIB_JABBER_RECEIVED 0x6c
238#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
239#define ETH_MIB_BAD_CRC_EVENT 0x74
240#define ETH_MIB_COLLISION 0x78
241#define ETH_MIB_LATE_COLLISION 0x7c
242
243/* Port serial status reg (PSR) */
244#define ETH_INTERFACE_PCM 0x00000001
245#define ETH_LINK_IS_UP 0x00000002
246#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
247#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
248#define ETH_GMII_SPEED_1000 0x00000010
249#define ETH_MII_SPEED_100 0x00000020
250#define ETH_TX_IN_PROGRESS 0x00000080
251#define ETH_BYPASS_ACTIVE 0x00000100
252#define ETH_PORT_AT_PARTITION_STATE 0x00000200
253#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
254
255/* SMI reg */
256#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
257#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
258#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
259#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
260
261/* Interrupt Cause Register Bit Definitions */
262
263/* SDMA command status fields macros */
264
265/* Tx & Rx descriptors status */
266#define ETH_ERROR_SUMMARY 0x00000001
267
268/* Tx & Rx descriptors command */
269#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
270
271/* Tx descriptors status */
272#define ETH_LC_ERROR 0
273#define ETH_UR_ERROR 0x00000002
274#define ETH_RL_ERROR 0x00000004
275#define ETH_LLC_SNAP_FORMAT 0x00000200
276
277/* Rx descriptors status */
278#define ETH_OVERRUN_ERROR 0x00000002
279#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
280#define ETH_RESOURCE_ERROR 0x00000006
281#define ETH_VLAN_TAGGED 0x00080000
282#define ETH_BPDU_FRAME 0x00100000
283#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
284#define ETH_OTHER_FRAME_TYPE 0x00400000
285#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
286#define ETH_FRAME_TYPE_IP_V_4 0x01000000
287#define ETH_FRAME_HEADER_OK 0x02000000
288#define ETH_RX_LAST_DESC 0x04000000
289#define ETH_RX_FIRST_DESC 0x08000000
290#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
291#define ETH_RX_ENABLE_INTERRUPT 0x20000000
292#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
293
294/* Rx descriptors byte count */
295#define ETH_FRAME_FRAGMENTED 0x00000004
296
297/* Tx descriptors command */
298#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
299#define ETH_FRAME_SET_TO_VLAN 0x00008000
300#define ETH_UDP_FRAME 0x00010000
301#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
302#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
303#define ETH_ZERO_PADDING 0x00080000
304#define ETH_TX_LAST_DESC 0x00100000
305#define ETH_TX_FIRST_DESC 0x00200000
306#define ETH_GEN_CRC 0x00400000
307#define ETH_TX_ENABLE_INTERRUPT 0x00800000
308#define ETH_AUTO_MODE 0x40000000
309
310#define ETH_TX_IHL_SHIFT 11
311
312/* typedefs */
313
314typedef enum _eth_func_ret_status {
315 ETH_OK, /* Returned as expected. */
316 ETH_ERROR, /* Fundamental error. */
317 ETH_RETRY, /* Could not process request. Try later.*/
318 ETH_END_OF_JOB, /* Ring has nothing to process. */
319 ETH_QUEUE_FULL, /* Ring resource error. */
320 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
321} ETH_FUNC_RET_STATUS;
322
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200323/* These are for big-endian machines. Little endian needs different
324 * definitions.
325 */
326#if defined(__BIG_ENDIAN)
327struct eth_rx_desc {
328 u16 byte_cnt; /* Descriptor buffer byte count */
329 u16 buf_size; /* Buffer size */
330 u32 cmd_sts; /* Descriptor command status */
331 u32 next_desc_ptr; /* Next descriptor pointer */
332 u32 buf_ptr; /* Descriptor buffer pointer */
333};
334
335struct eth_tx_desc {
336 u16 byte_cnt; /* buffer byte count */
337 u16 l4i_chk; /* CPU provided TCP checksum */
338 u32 cmd_sts; /* Command/status field */
339 u32 next_desc_ptr; /* Pointer to next descriptor */
340 u32 buf_ptr; /* pointer to buffer for this descriptor*/
341};
342#elif defined(__LITTLE_ENDIAN)
343struct eth_rx_desc {
344 u32 cmd_sts; /* Descriptor command status */
345 u16 buf_size; /* Buffer size */
346 u16 byte_cnt; /* Descriptor buffer byte count */
347 u32 buf_ptr; /* Descriptor buffer pointer */
348 u32 next_desc_ptr; /* Next descriptor pointer */
349};
350
351struct eth_tx_desc {
352 u32 cmd_sts; /* Command/status field */
353 u16 l4i_chk; /* CPU provided TCP checksum */
354 u16 byte_cnt; /* buffer byte count */
355 u32 buf_ptr; /* pointer to buffer for this descriptor*/
356 u32 next_desc_ptr; /* Pointer to next descriptor */
357};
358#else
359#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
360#endif
361
362/* Unified struct for Rx and Tx operations. The user is not required to */
363/* be familier with neither Tx nor Rx descriptors. */
364struct pkt_info {
365 unsigned short byte_cnt; /* Descriptor buffer byte count */
366 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
367 unsigned int cmd_sts; /* Descriptor command status */
368 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
369 struct sk_buff *return_info; /* User resource return information */
370};
371
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200372
373/* global *******************************************************************/
374struct mv643xx_shared_private {
375 void __iomem *eth_base;
376
377 /* used to protect SMI_REG, which is shared across ports */
378 spinlock_t phy_lock;
379
380 u32 win_protect;
381
382 unsigned int t_clk;
383};
384
385
386/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200387struct mv643xx_mib_counters {
388 u64 good_octets_received;
389 u32 bad_octets_received;
390 u32 internal_mac_transmit_err;
391 u32 good_frames_received;
392 u32 bad_frames_received;
393 u32 broadcast_frames_received;
394 u32 multicast_frames_received;
395 u32 frames_64_octets;
396 u32 frames_65_to_127_octets;
397 u32 frames_128_to_255_octets;
398 u32 frames_256_to_511_octets;
399 u32 frames_512_to_1023_octets;
400 u32 frames_1024_to_max_octets;
401 u64 good_octets_sent;
402 u32 good_frames_sent;
403 u32 excessive_collision;
404 u32 multicast_frames_sent;
405 u32 broadcast_frames_sent;
406 u32 unrec_mac_control_received;
407 u32 fc_sent;
408 u32 good_fc_received;
409 u32 bad_fc_received;
410 u32 undersize_received;
411 u32 fragments_received;
412 u32 oversize_received;
413 u32 jabber_received;
414 u32 mac_receive_error;
415 u32 bad_crc_event;
416 u32 collision;
417 u32 late_collision;
418};
419
420struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200421 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200422 int port_num; /* User Ethernet port number */
423
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200424 struct mv643xx_shared_private *shared_smi;
425
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200426 u32 rx_sram_addr; /* Base address of rx sram area */
427 u32 rx_sram_size; /* Size of rx sram area */
428 u32 tx_sram_addr; /* Base address of tx sram area */
429 u32 tx_sram_size; /* Size of tx sram area */
430
431 int rx_resource_err; /* Rx ring resource error flag */
432
433 /* Tx/Rx rings managment indexes fields. For driver use */
434
435 /* Next available and first returning Rx resource */
436 int rx_curr_desc_q, rx_used_desc_q;
437
438 /* Next available and first returning Tx resource */
439 int tx_curr_desc_q, tx_used_desc_q;
440
441#ifdef MV643XX_TX_FAST_REFILL
442 u32 tx_clean_threshold;
443#endif
444
445 struct eth_rx_desc *p_rx_desc_area;
446 dma_addr_t rx_desc_dma;
447 int rx_desc_area_size;
448 struct sk_buff **rx_skb;
449
450 struct eth_tx_desc *p_tx_desc_area;
451 dma_addr_t tx_desc_dma;
452 int tx_desc_area_size;
453 struct sk_buff **tx_skb;
454
455 struct work_struct tx_timeout_task;
456
457 struct net_device *dev;
458 struct napi_struct napi;
459 struct net_device_stats stats;
460 struct mv643xx_mib_counters mib_counters;
461 spinlock_t lock;
462 /* Size of Tx Ring per queue */
463 int tx_ring_size;
464 /* Number of tx descriptors in use */
465 int tx_desc_count;
466 /* Size of Rx Ring per queue */
467 int rx_ring_size;
468 /* Number of rx descriptors in use */
469 int rx_desc_count;
470
471 /*
472 * Used in case RX Ring is empty, which can be caused when
473 * system does not have resources (skb's)
474 */
475 struct timer_list timeout;
476
477 u32 rx_int_coal;
478 u32 tx_int_coal;
479 struct mii_if_info mii;
480};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200482
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200483/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700484static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200486 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700489static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200491 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200494
495/* rxq/txq helper functions *************************************************/
496static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
497 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200499 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200502static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
503{
504 unsigned int port_num = mp->port_num;
505 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700506
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200507 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200508 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200509 if (queues) {
510 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200511 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200512
513 /* Wait for all Rx activity to terminate. */
514 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200515 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200516 udelay(PHY_WAIT_MICRO_SECONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200519 return queues;
520}
521
522static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
523 unsigned int queues)
524{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200525 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200526}
527
528static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
529{
530 unsigned int port_num = mp->port_num;
531 u32 queues;
532
533 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200534 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200535 if (queues) {
536 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200537 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200538
539 /* Wait for all Tx activity to terminate. */
540 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200541 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200542 udelay(PHY_WAIT_MICRO_SECONDS);
543
544 /* Wait for Tx FIFO to empty */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200545 while (rdl(mp, PORT_STATUS(port_num)) & ETH_PORT_TX_FIFO_EMPTY)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200546 udelay(PHY_WAIT_MICRO_SECONDS);
547 }
548
549 return queues;
550}
551
552
553/* rx ***********************************************************************/
554static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
555
556/*
557 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
558 *
559 * DESCRIPTION:
560 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
561 * next 'used' descriptor and attached the returned buffer to it.
562 * In case the Rx ring was in "resource error" condition, where there are
563 * no available Rx resources, the function resets the resource error flag.
564 *
565 * INPUT:
566 * struct mv643xx_private *mp Ethernet Port Control srtuct.
567 * struct pkt_info *p_pkt_info Information on returned buffer.
568 *
569 * OUTPUT:
570 * New available Rx resource in Rx descriptor ring.
571 *
572 * RETURN:
573 * ETH_ERROR in case the routine can not access Rx desc ring.
574 * ETH_OK otherwise.
575 */
576static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
577 struct pkt_info *p_pkt_info)
578{
579 int used_rx_desc; /* Where to return Rx resource */
580 volatile struct eth_rx_desc *p_used_rx_desc;
581 unsigned long flags;
582
583 spin_lock_irqsave(&mp->lock, flags);
584
585 /* Get 'used' Rx descriptor */
586 used_rx_desc = mp->rx_used_desc_q;
587 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
588
589 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
590 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
591 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
592
593 /* Flush the write pipe */
594
595 /* Return the descriptor to DMA ownership */
596 wmb();
597 p_used_rx_desc->cmd_sts =
598 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
599 wmb();
600
601 /* Move the used descriptor pointer to the next descriptor */
602 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
603
604 /* Any Rx return cancels the Rx resource error status */
605 mp->rx_resource_err = 0;
606
607 spin_unlock_irqrestore(&mp->lock, flags);
608
609 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
612/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700613 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 *
615 * Fills / refills RX queue on a certain gigabit ethernet port
616 *
617 * Input : pointer to ethernet interface network device structure
618 * Output : N/A
619 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700620static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct mv643xx_private *mp = netdev_priv(dev);
623 struct pkt_info pkt_info;
624 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700625 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700627 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000628 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (!skb)
630 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700631 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000632 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700633 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000634 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700636 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
637 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
638 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 pkt_info.return_info = skb;
640 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
641 printk(KERN_ERR
642 "%s: Error allocating RX Ring\n", dev->name);
643 break;
644 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700645 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 /*
648 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700649 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700651 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700653 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
658/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700659 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
661 * Timer routine to wake up RX queue filling task. This function is
662 * used only in case the RX queue is empty, and all alloc_skb has
663 * failed (due to out of memory event).
664 *
665 * Input : pointer to ethernet interface network device structure
666 * Output : N/A
667 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700668static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700670 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200674 * eth_port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200676 * DESCRIPTION:
677 * This routine returns the received data to the caller. There is no
678 * data copying during routine operation. All information is returned
679 * using pointer to packet information struct passed from the caller.
680 * If the routine exhausts Rx ring resources then the resource error flag
681 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200683 * INPUT:
684 * struct mv643xx_private *mp Ethernet Port Control srtuct.
685 * struct pkt_info *p_pkt_info User packet buffer.
686 *
687 * OUTPUT:
688 * Rx ring current and used indexes are updated.
689 *
690 * RETURN:
691 * ETH_ERROR in case the routine can not access Rx desc ring.
692 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
693 * ETH_END_OF_JOB if there is no received data.
694 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200696static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
697 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200699 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
700 volatile struct eth_rx_desc *p_rx_desc;
701 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700702 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200704 /* Do not process Rx ring in case of Rx ring resource error */
705 if (mp->rx_resource_err)
706 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700707
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200708 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700709
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200710 /* Get the Rx Desc ring 'curr and 'used' indexes */
711 rx_curr_desc = mp->rx_curr_desc_q;
712 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200714 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700715
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200716 /* The following parameters are used to save readings from memory */
717 command_status = p_rx_desc->cmd_sts;
718 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700719
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200720 /* Nothing to receive... */
721 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700722 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200723 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
725
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200726 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
727 p_pkt_info->cmd_sts = command_status;
728 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
729 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
730 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200732 /*
733 * Clean the return info field to indicate that the
734 * packet has been moved to the upper layers
735 */
736 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700737
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200738 /* Update current index in data structure */
739 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
740 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700741
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200742 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
743 if (rx_next_curr_desc == rx_used_desc)
744 mp->rx_resource_err = 1;
745
746 spin_unlock_irqrestore(&mp->lock, flags);
747
748 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700749}
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751/*
752 * mv643xx_eth_receive
753 *
754 * This function is forward packets that are received from the port's
755 * queues toward kernel core or FastRoute them to another interface.
756 *
757 * Input : dev - a pointer to the required interface
758 * max - maximum number to receive (0 means unlimted)
759 *
760 * Output : number of served packets
761 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700765 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 unsigned int received_packets = 0;
767 struct sk_buff *skb;
768 struct pkt_info pkt_info;
769
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700770 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400771 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700772 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700773 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700775
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700776 /*
777 * Update statistics.
778 * Note byte count includes 4 byte CRC count
779 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 stats->rx_packets++;
781 stats->rx_bytes += pkt_info.byte_cnt;
782 skb = pkt_info.return_info;
783 /*
784 * In case received a packet without first / last bits on OR
785 * the error summary bit is on, the packets needs to be dropeed.
786 */
787 if (((pkt_info.cmd_sts
788 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
789 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
790 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
791 stats->rx_dropped++;
792 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
793 ETH_RX_LAST_DESC)) !=
794 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
795 if (net_ratelimit())
796 printk(KERN_ERR
797 "%s: Received packet spread "
798 "on multiple descriptors\n",
799 dev->name);
800 }
801 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
802 stats->rx_errors++;
803
804 dev_kfree_skb_irq(skb);
805 } else {
806 /*
807 * The -4 is for the CRC in the trailer of the
808 * received packet
809 */
810 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
813 skb->ip_summed = CHECKSUM_UNNECESSARY;
814 skb->csum = htons(
815 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
816 }
817 skb->protocol = eth_type_trans(skb, dev);
818#ifdef MV643XX_NAPI
819 netif_receive_skb(skb);
820#else
821 netif_rx(skb);
822#endif
823 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700824 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700826 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 return received_packets;
829}
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832/*
833 * mv643xx_poll
834 *
835 * This function is used in case of NAPI
836 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700837static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700839 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
840 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700842 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844#ifdef MV643XX_TX_FAST_REFILL
845 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700846 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849#endif
850
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700851 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200852 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700853 != (u32) mp->rx_used_desc_q)
854 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700856 if (work_done < budget) {
857 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200858 wrl(mp, INT_CAUSE(port_num), 0);
859 wrl(mp, INT_CAUSE_EXT(port_num), 0);
860 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700863 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865#endif
866
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200867
868/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700869/**
870 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
871 *
872 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700873 * This helper function detects that case.
874 */
875
876static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
877{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700878 unsigned int frag;
879 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700880
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700881 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
882 fragp = &skb_shinfo(skb)->frags[frag];
883 if (fragp->size <= 8 && fragp->page_offset & 0x7)
884 return 1;
885 }
886 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700887}
888
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700889/**
890 * eth_alloc_tx_desc_index - return the index of the next available tx desc
891 */
892static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
893{
894 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700895
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700896 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700897
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700898 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700899 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
900
901 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
902
903 return tx_desc_curr;
904}
905
906/**
907 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700909 * Ensure the data for each fragment to be transmitted is mapped properly,
910 * then fill in descriptors in the tx hw queue.
911 */
912static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
913 struct sk_buff *skb)
914{
915 int frag;
916 int tx_index;
917 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700918
919 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
920 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
921
922 tx_index = eth_alloc_tx_desc_index(mp);
923 desc = &mp->p_tx_desc_area[tx_index];
924
925 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
926 /* Last Frag enables interrupt and frees the skb */
927 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
928 desc->cmd_sts |= ETH_ZERO_PADDING |
929 ETH_TX_LAST_DESC |
930 ETH_TX_ENABLE_INTERRUPT;
931 mp->tx_skb[tx_index] = skb;
932 } else
Al Viro05980772006-05-30 23:59:09 -0400933 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700934
935 desc = &mp->p_tx_desc_area[tx_index];
936 desc->l4i_chk = 0;
937 desc->byte_cnt = this_frag->size;
938 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
939 this_frag->page_offset,
940 this_frag->size,
941 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700942 }
943}
944
Byron Bradley324ff2c2008-02-04 23:47:15 -0800945static inline __be16 sum16_as_be(__sum16 sum)
946{
947 return (__force __be16)sum;
948}
949
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700950/**
951 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700953 * Ensure the data for an skb to be transmitted is mapped properly,
954 * then fill in descriptors in the tx hw queue and start the hardware.
955 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700956static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
957 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700958{
959 int tx_index;
960 struct eth_tx_desc *desc;
961 u32 cmd_sts;
962 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700963 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700964
965 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
966
967 tx_index = eth_alloc_tx_desc_index(mp);
968 desc = &mp->p_tx_desc_area[tx_index];
969
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700970 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700971 eth_tx_fill_frag_descs(mp, skb);
972
973 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400974 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700975 } else {
976 cmd_sts |= ETH_ZERO_PADDING |
977 ETH_TX_LAST_DESC |
978 ETH_TX_ENABLE_INTERRUPT;
979 length = skb->len;
980 mp->tx_skb[tx_index] = skb;
981 }
982
983 desc->byte_cnt = length;
984 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700985
Patrick McHardy84fa7932006-08-29 16:44:56 -0700986 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800987 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700988
989 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
990 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700991 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700992
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700993 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700994 case IPPROTO_UDP:
995 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800996 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700997 break;
998 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800999 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001000 break;
1001 default:
1002 BUG();
1003 }
1004 } else {
1005 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1006 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1007 desc->l4i_chk = 0;
1008 }
1009
1010 /* ensure all other descriptors are written before first cmd_sts */
1011 wmb();
1012 desc->cmd_sts = cmd_sts;
1013
1014 /* ensure all descriptors are written before poking hardware */
1015 wmb();
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001016 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001017
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001018 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001019}
1020
1021/**
1022 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 */
1025static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1026{
1027 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001028 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001031 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -07001032
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001033 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1034 stats->tx_dropped++;
1035 printk(KERN_DEBUG "%s: failed to linearize tiny "
1036 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001037 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -07001038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 spin_lock_irqsave(&mp->lock, flags);
1041
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001042 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1043 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1044 netif_stop_queue(dev);
1045 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001046 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001047 }
1048
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001049 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -07001050 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 stats->tx_packets++;
1052 dev->trans_start = jiffies;
1053
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001054 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1055 netif_stop_queue(dev);
1056
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 spin_unlock_irqrestore(&mp->lock, flags);
1058
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001059 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001062
1063/* mii management interface *************************************************/
1064static int ethernet_phy_get(struct mv643xx_private *mp);
1065
1066/*
1067 * eth_port_read_smi_reg - Read PHY registers
1068 *
1069 * DESCRIPTION:
1070 * This routine utilize the SMI interface to interact with the PHY in
1071 * order to perform PHY register read.
1072 *
1073 * INPUT:
1074 * struct mv643xx_private *mp Ethernet Port.
1075 * unsigned int phy_reg PHY register address offset.
1076 * unsigned int *value Register value buffer.
1077 *
1078 * OUTPUT:
1079 * Write the value of a specified PHY register into given buffer.
1080 *
1081 * RETURN:
1082 * false if the PHY is busy or read data is not in valid state.
1083 * true otherwise.
1084 *
1085 */
1086static void eth_port_read_smi_reg(struct mv643xx_private *mp,
1087 unsigned int phy_reg, unsigned int *value)
1088{
1089 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1090 int phy_addr = ethernet_phy_get(mp);
1091 unsigned long flags;
1092 int i;
1093
1094 /* the SMI register is a shared resource */
1095 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1096
1097 /* wait for the SMI register to become available */
1098 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1099 if (i == PHY_WAIT_ITERATIONS) {
1100 printk("%s: PHY busy timeout\n", mp->dev->name);
1101 goto out;
1102 }
1103 udelay(PHY_WAIT_MICRO_SECONDS);
1104 }
1105
1106 writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
1107 smi_reg);
1108
1109 /* now wait for the data to be valid */
1110 for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
1111 if (i == PHY_WAIT_ITERATIONS) {
1112 printk("%s: PHY read timeout\n", mp->dev->name);
1113 goto out;
1114 }
1115 udelay(PHY_WAIT_MICRO_SECONDS);
1116 }
1117
1118 *value = readl(smi_reg) & 0xffff;
1119out:
1120 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1121}
1122
1123/*
1124 * eth_port_write_smi_reg - Write to PHY registers
1125 *
1126 * DESCRIPTION:
1127 * This routine utilize the SMI interface to interact with the PHY in
1128 * order to perform writes to PHY registers.
1129 *
1130 * INPUT:
1131 * struct mv643xx_private *mp Ethernet Port.
1132 * unsigned int phy_reg PHY register address offset.
1133 * unsigned int value Register value.
1134 *
1135 * OUTPUT:
1136 * Write the given value to the specified PHY register.
1137 *
1138 * RETURN:
1139 * false if the PHY is busy.
1140 * true otherwise.
1141 *
1142 */
1143static void eth_port_write_smi_reg(struct mv643xx_private *mp,
1144 unsigned int phy_reg, unsigned int value)
1145{
1146 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1147 int phy_addr = ethernet_phy_get(mp);
1148 unsigned long flags;
1149 int i;
1150
1151 /* the SMI register is a shared resource */
1152 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1153
1154 /* wait for the SMI register to become available */
1155 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1156 if (i == PHY_WAIT_ITERATIONS) {
1157 printk("%s: PHY busy timeout\n", mp->dev->name);
1158 goto out;
1159 }
1160 udelay(PHY_WAIT_MICRO_SECONDS);
1161 }
1162
1163 writel((phy_addr << 16) | (phy_reg << 21) |
1164 ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
1165out:
1166 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1167}
1168
1169
1170/* mib counters *************************************************************/
1171/*
1172 * eth_clear_mib_counters - Clear all MIB counters
1173 *
1174 * DESCRIPTION:
1175 * This function clears all MIB counters of a specific ethernet port.
1176 * A read from the MIB counter will reset the counter.
1177 *
1178 * INPUT:
1179 * struct mv643xx_private *mp Ethernet Port.
1180 *
1181 * OUTPUT:
1182 * After reading all MIB counters, the counters resets.
1183 *
1184 * RETURN:
1185 * MIB counter value.
1186 *
1187 */
1188static void eth_clear_mib_counters(struct mv643xx_private *mp)
1189{
1190 unsigned int port_num = mp->port_num;
1191 int i;
1192
1193 /* Perform dummy reads from MIB counters */
1194 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
1195 i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001196 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001197}
1198
1199static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1200{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001201 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001202}
1203
1204static void eth_update_mib_counters(struct mv643xx_private *mp)
1205{
1206 struct mv643xx_mib_counters *p = &mp->mib_counters;
1207 int offset;
1208
1209 p->good_octets_received +=
1210 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
1211 p->good_octets_received +=
1212 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
1213
1214 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
1215 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
1216 offset += 4)
1217 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1218
1219 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
1220 p->good_octets_sent +=
1221 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
1222
1223 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
1224 offset <= ETH_MIB_LATE_COLLISION;
1225 offset += 4)
1226 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1227}
1228
1229
1230/* ethtool ******************************************************************/
1231struct mv643xx_stats {
1232 char stat_string[ETH_GSTRING_LEN];
1233 int sizeof_stat;
1234 int stat_offset;
1235};
1236
1237#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1238 offsetof(struct mv643xx_private, m)
1239
1240static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1241 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1242 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1243 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1244 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1245 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1246 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1247 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1248 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1249 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1250 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1251 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1252 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1253 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1254 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1255 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1256 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1257 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1258 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1259 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1260 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1261 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1262 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1263 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1264 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1265 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1266 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1267 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1268 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1269 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1270 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1271 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1272 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1273 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1274 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1275 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1276 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1277 { "collision", MV643XX_STAT(mib_counters.collision) },
1278 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1279};
1280
1281#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1282
1283static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1284{
1285 struct mv643xx_private *mp = netdev_priv(dev);
1286 int err;
1287
1288 spin_lock_irq(&mp->lock);
1289 err = mii_ethtool_gset(&mp->mii, cmd);
1290 spin_unlock_irq(&mp->lock);
1291
1292 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1293 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1294 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1295
1296 return err;
1297}
1298
1299static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1300{
1301 struct mv643xx_private *mp = netdev_priv(dev);
1302 int err;
1303
1304 spin_lock_irq(&mp->lock);
1305 err = mii_ethtool_sset(&mp->mii, cmd);
1306 spin_unlock_irq(&mp->lock);
1307
1308 return err;
1309}
1310
1311static void mv643xx_get_drvinfo(struct net_device *netdev,
1312 struct ethtool_drvinfo *drvinfo)
1313{
1314 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1315 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1316 strncpy(drvinfo->fw_version, "N/A", 32);
1317 strncpy(drvinfo->bus_info, "mv643xx", 32);
1318 drvinfo->n_stats = MV643XX_STATS_LEN;
1319}
1320
1321static int mv643xx_eth_nway_restart(struct net_device *dev)
1322{
1323 struct mv643xx_private *mp = netdev_priv(dev);
1324
1325 return mii_nway_restart(&mp->mii);
1326}
1327
1328static u32 mv643xx_eth_get_link(struct net_device *dev)
1329{
1330 struct mv643xx_private *mp = netdev_priv(dev);
1331
1332 return mii_link_ok(&mp->mii);
1333}
1334
1335static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1336 uint8_t *data)
1337{
1338 int i;
1339
1340 switch(stringset) {
1341 case ETH_SS_STATS:
1342 for (i=0; i < MV643XX_STATS_LEN; i++) {
1343 memcpy(data + i * ETH_GSTRING_LEN,
1344 mv643xx_gstrings_stats[i].stat_string,
1345 ETH_GSTRING_LEN);
1346 }
1347 break;
1348 }
1349}
1350
1351static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1352 struct ethtool_stats *stats, uint64_t *data)
1353{
1354 struct mv643xx_private *mp = netdev->priv;
1355 int i;
1356
1357 eth_update_mib_counters(mp);
1358
1359 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1360 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1361 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1362 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1363 }
1364}
1365
1366static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1367{
1368 switch (sset) {
1369 case ETH_SS_STATS:
1370 return MV643XX_STATS_LEN;
1371 default:
1372 return -EOPNOTSUPP;
1373 }
1374}
1375
1376static const struct ethtool_ops mv643xx_ethtool_ops = {
1377 .get_settings = mv643xx_get_settings,
1378 .set_settings = mv643xx_set_settings,
1379 .get_drvinfo = mv643xx_get_drvinfo,
1380 .get_link = mv643xx_eth_get_link,
1381 .set_sg = ethtool_op_set_sg,
1382 .get_sset_count = mv643xx_get_sset_count,
1383 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1384 .get_strings = mv643xx_get_strings,
1385 .nway_reset = mv643xx_eth_nway_restart,
1386};
1387
1388
1389/* address handling *********************************************************/
1390/*
1391 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1392 */
1393static void eth_port_uc_addr_get(struct mv643xx_private *mp,
1394 unsigned char *p_addr)
1395{
1396 unsigned int port_num = mp->port_num;
1397 unsigned int mac_h;
1398 unsigned int mac_l;
1399
1400 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1401 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1402
1403 p_addr[0] = (mac_h >> 24) & 0xff;
1404 p_addr[1] = (mac_h >> 16) & 0xff;
1405 p_addr[2] = (mac_h >> 8) & 0xff;
1406 p_addr[3] = mac_h & 0xff;
1407 p_addr[4] = (mac_l >> 8) & 0xff;
1408 p_addr[5] = mac_l & 0xff;
1409}
1410
1411/*
1412 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1413 *
1414 * DESCRIPTION:
1415 * Go through all the DA filter tables (Unicast, Special Multicast &
1416 * Other Multicast) and set each entry to 0.
1417 *
1418 * INPUT:
1419 * struct mv643xx_private *mp Ethernet Port.
1420 *
1421 * OUTPUT:
1422 * Multicast and Unicast packets are rejected.
1423 *
1424 * RETURN:
1425 * None.
1426 */
1427static void eth_port_init_mac_tables(struct mv643xx_private *mp)
1428{
1429 unsigned int port_num = mp->port_num;
1430 int table_index;
1431
1432 /* Clear DA filter unicast table (Ex_dFUT) */
1433 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001434 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001435
1436 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1437 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001438 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001439 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001440 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001441 }
1442}
1443
1444/*
1445 * The entries in each table are indexed by a hash of a packet's MAC
1446 * address. One bit in each entry determines whether the packet is
1447 * accepted. There are 4 entries (each 8 bits wide) in each register
1448 * of the table. The bits in each entry are defined as follows:
1449 * 0 Accept=1, Drop=0
1450 * 3-1 Queue (ETH_Q0=0)
1451 * 7-4 Reserved = 0;
1452 */
1453static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
1454 int table, unsigned char entry)
1455{
1456 unsigned int table_reg;
1457 unsigned int tbl_offset;
1458 unsigned int reg_offset;
1459
1460 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1461 reg_offset = entry % 4; /* Entry offset within the register */
1462
1463 /* Set "accepts frame bit" at specified table entry */
1464 table_reg = rdl(mp, table + tbl_offset);
1465 table_reg |= 0x01 << (8 * reg_offset);
1466 wrl(mp, table + tbl_offset, table_reg);
1467}
1468
1469/*
1470 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1471 */
1472static void eth_port_uc_addr_set(struct mv643xx_private *mp,
1473 unsigned char *p_addr)
1474{
1475 unsigned int port_num = mp->port_num;
1476 unsigned int mac_h;
1477 unsigned int mac_l;
1478 int table;
1479
1480 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1481 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1482 (p_addr[3] << 0);
1483
1484 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1485 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1486
1487 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001488 table = UNICAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001489 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
1490}
1491
1492/*
1493 * mv643xx_eth_update_mac_address
1494 *
1495 * Update the MAC address of the port in the address table
1496 *
1497 * Input : pointer to ethernet interface network device structure
1498 * Output : N/A
1499 */
1500static void mv643xx_eth_update_mac_address(struct net_device *dev)
1501{
1502 struct mv643xx_private *mp = netdev_priv(dev);
1503
1504 eth_port_init_mac_tables(mp);
1505 eth_port_uc_addr_set(mp, dev->dev_addr);
1506}
1507
1508/*
1509 * mv643xx_eth_set_mac_address
1510 *
1511 * Change the interface's mac address.
1512 * No special hardware thing should be done because interface is always
1513 * put in promiscuous mode.
1514 *
1515 * Input : pointer to ethernet interface network device structure and
1516 * a pointer to the designated entry to be added to the cache.
1517 * Output : zero upon success, negative upon failure
1518 */
1519static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1520{
1521 int i;
1522
1523 for (i = 0; i < 6; i++)
1524 /* +2 is for the offset of the HW addr type */
1525 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1526 mv643xx_eth_update_mac_address(dev);
1527 return 0;
1528}
1529
1530/*
1531 * eth_port_mc_addr - Multicast address settings.
1532 *
1533 * The MV device supports multicast using two tables:
1534 * 1) Special Multicast Table for MAC addresses of the form
1535 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1536 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1537 * Table entries in the DA-Filter table.
1538 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1539 * is used as an index to the Other Multicast Table entries in the
1540 * DA-Filter table. This function calculates the CRC-8bit value.
1541 * In either case, eth_port_set_filter_table_entry() is then called
1542 * to set to set the actual table entry.
1543 */
1544static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
1545{
1546 unsigned int port_num = mp->port_num;
1547 unsigned int mac_h;
1548 unsigned int mac_l;
1549 unsigned char crc_result = 0;
1550 int table;
1551 int mac_array[48];
1552 int crc[8];
1553 int i;
1554
1555 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1556 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001557 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001558 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
1559 return;
1560 }
1561
1562 /* Calculate CRC-8 out of the given address */
1563 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1564 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1565 (p_addr[4] << 8) | (p_addr[5] << 0);
1566
1567 for (i = 0; i < 32; i++)
1568 mac_array[i] = (mac_l >> i) & 0x1;
1569 for (i = 32; i < 48; i++)
1570 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1571
1572 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1573 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1574 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1575 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1576 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1577
1578 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1579 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1580 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1581 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1582 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1583 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1584 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1585
1586 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1587 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1588 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1589 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1590 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1591 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1592
1593 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1594 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1595 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1596 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1597 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1598 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1599
1600 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1601 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1602 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1603 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1604 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1605 mac_array[3] ^ mac_array[2];
1606
1607 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1608 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1609 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1610 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1611 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1612 mac_array[4] ^ mac_array[3];
1613
1614 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1615 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1616 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1617 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1618 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1619 mac_array[4];
1620
1621 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1622 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1623 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1624 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1625 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1626
1627 for (i = 0; i < 8; i++)
1628 crc_result = crc_result | (crc[i] << i);
1629
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001630 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001631 eth_port_set_filter_table_entry(mp, table, crc_result);
1632}
1633
1634/*
1635 * Set the entire multicast list based on dev->mc_list.
1636 */
1637static void eth_port_set_multicast_list(struct net_device *dev)
1638{
1639
1640 struct dev_mc_list *mc_list;
1641 int i;
1642 int table_index;
1643 struct mv643xx_private *mp = netdev_priv(dev);
1644 unsigned int eth_port_num = mp->port_num;
1645
1646 /* If the device is in promiscuous mode or in all multicast mode,
1647 * we will fully populate both multicast tables with accept.
1648 * This is guaranteed to yield a match on all multicast addresses...
1649 */
1650 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1651 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1652 /* Set all entries in DA filter special multicast
1653 * table (Ex_dFSMT)
1654 * Set for ETH_Q0 for now
1655 * Bits
1656 * 0 Accept=1, Drop=0
1657 * 3-1 Queue ETH_Q0=0
1658 * 7-4 Reserved = 0;
1659 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001660 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001661
1662 /* Set all entries in DA filter other multicast
1663 * table (Ex_dFOMT)
1664 * Set for ETH_Q0 for now
1665 * Bits
1666 * 0 Accept=1, Drop=0
1667 * 3-1 Queue ETH_Q0=0
1668 * 7-4 Reserved = 0;
1669 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001670 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001671 }
1672 return;
1673 }
1674
1675 /* We will clear out multicast tables every time we get the list.
1676 * Then add the entire new list...
1677 */
1678 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1679 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001680 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001681
1682 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001683 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001684 }
1685
1686 /* Get pointer to net_device multicast list and add each one... */
1687 for (i = 0, mc_list = dev->mc_list;
1688 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1689 i++, mc_list = mc_list->next)
1690 if (mc_list->dmi_addrlen == 6)
1691 eth_port_mc_addr(mp, mc_list->dmi_addr);
1692}
1693
1694/*
1695 * mv643xx_eth_set_rx_mode
1696 *
1697 * Change from promiscuos to regular rx mode
1698 *
1699 * Input : pointer to ethernet interface network device structure
1700 * Output : N/A
1701 */
1702static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1703{
1704 struct mv643xx_private *mp = netdev_priv(dev);
1705 u32 config_reg;
1706
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001707 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001708 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001709 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001710 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001711 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001712 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001713
1714 eth_port_set_multicast_list(dev);
1715}
1716
1717
1718/* rx/tx queue initialisation ***********************************************/
1719/*
1720 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1721 *
1722 * DESCRIPTION:
1723 * This function prepares a Rx chained list of descriptors and packet
1724 * buffers in a form of a ring. The routine must be called after port
1725 * initialization routine and before port start routine.
1726 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1727 * devices in the system (i.e. DRAM). This function uses the ethernet
1728 * struct 'virtual to physical' routine (set by the user) to set the ring
1729 * with physical addresses.
1730 *
1731 * INPUT:
1732 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1733 *
1734 * OUTPUT:
1735 * The routine updates the Ethernet port control struct with information
1736 * regarding the Rx descriptors and buffers.
1737 *
1738 * RETURN:
1739 * None.
1740 */
1741static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1742{
1743 volatile struct eth_rx_desc *p_rx_desc;
1744 int rx_desc_num = mp->rx_ring_size;
1745 int i;
1746
1747 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1748 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1749 for (i = 0; i < rx_desc_num; i++) {
1750 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1751 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1752 }
1753
1754 /* Save Rx desc pointer to driver struct. */
1755 mp->rx_curr_desc_q = 0;
1756 mp->rx_used_desc_q = 0;
1757
1758 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1759}
1760
1761static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1762{
1763 struct mv643xx_private *mp = netdev_priv(dev);
1764 int curr;
1765
1766 /* Stop RX Queues */
1767 mv643xx_eth_port_disable_rx(mp);
1768
1769 /* Free preallocated skb's on RX rings */
1770 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1771 if (mp->rx_skb[curr]) {
1772 dev_kfree_skb(mp->rx_skb[curr]);
1773 mp->rx_desc_count--;
1774 }
1775 }
1776
1777 if (mp->rx_desc_count)
1778 printk(KERN_ERR
1779 "%s: Error in freeing Rx Ring. %d skb's still"
1780 " stuck in RX Ring - ignoring them\n", dev->name,
1781 mp->rx_desc_count);
1782 /* Free RX ring */
1783 if (mp->rx_sram_size)
1784 iounmap(mp->p_rx_desc_area);
1785 else
1786 dma_free_coherent(NULL, mp->rx_desc_area_size,
1787 mp->p_rx_desc_area, mp->rx_desc_dma);
1788}
1789
1790/*
1791 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1792 *
1793 * DESCRIPTION:
1794 * This function prepares a Tx chained list of descriptors and packet
1795 * buffers in a form of a ring. The routine must be called after port
1796 * initialization routine and before port start routine.
1797 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1798 * devices in the system (i.e. DRAM). This function uses the ethernet
1799 * struct 'virtual to physical' routine (set by the user) to set the ring
1800 * with physical addresses.
1801 *
1802 * INPUT:
1803 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1804 *
1805 * OUTPUT:
1806 * The routine updates the Ethernet port control struct with information
1807 * regarding the Tx descriptors and buffers.
1808 *
1809 * RETURN:
1810 * None.
1811 */
1812static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1813{
1814 int tx_desc_num = mp->tx_ring_size;
1815 struct eth_tx_desc *p_tx_desc;
1816 int i;
1817
1818 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1819 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1820 for (i = 0; i < tx_desc_num; i++) {
1821 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1822 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1823 }
1824
1825 mp->tx_curr_desc_q = 0;
1826 mp->tx_used_desc_q = 0;
1827
1828 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1829}
1830
1831/**
1832 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1833 *
1834 * If force is non-zero, frees uncompleted descriptors as well
1835 */
1836static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1837{
1838 struct mv643xx_private *mp = netdev_priv(dev);
1839 struct eth_tx_desc *desc;
1840 u32 cmd_sts;
1841 struct sk_buff *skb;
1842 unsigned long flags;
1843 int tx_index;
1844 dma_addr_t addr;
1845 int count;
1846 int released = 0;
1847
1848 while (mp->tx_desc_count > 0) {
1849 spin_lock_irqsave(&mp->lock, flags);
1850
1851 /* tx_desc_count might have changed before acquiring the lock */
1852 if (mp->tx_desc_count <= 0) {
1853 spin_unlock_irqrestore(&mp->lock, flags);
1854 return released;
1855 }
1856
1857 tx_index = mp->tx_used_desc_q;
1858 desc = &mp->p_tx_desc_area[tx_index];
1859 cmd_sts = desc->cmd_sts;
1860
1861 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
1862 spin_unlock_irqrestore(&mp->lock, flags);
1863 return released;
1864 }
1865
1866 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1867 mp->tx_desc_count--;
1868
1869 addr = desc->buf_ptr;
1870 count = desc->byte_cnt;
1871 skb = mp->tx_skb[tx_index];
1872 if (skb)
1873 mp->tx_skb[tx_index] = NULL;
1874
1875 if (cmd_sts & ETH_ERROR_SUMMARY) {
1876 printk("%s: Error in TX\n", dev->name);
1877 dev->stats.tx_errors++;
1878 }
1879
1880 spin_unlock_irqrestore(&mp->lock, flags);
1881
1882 if (cmd_sts & ETH_TX_FIRST_DESC)
1883 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1884 else
1885 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1886
1887 if (skb)
1888 dev_kfree_skb_irq(skb);
1889
1890 released = 1;
1891 }
1892
1893 return released;
1894}
1895
1896static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1897{
1898 struct mv643xx_private *mp = netdev_priv(dev);
1899
1900 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1901 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1902 netif_wake_queue(dev);
1903}
1904
1905static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1906{
1907 mv643xx_eth_free_tx_descs(dev, 1);
1908}
1909
1910static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1911{
1912 struct mv643xx_private *mp = netdev_priv(dev);
1913
1914 /* Stop Tx Queues */
1915 mv643xx_eth_port_disable_tx(mp);
1916
1917 /* Free outstanding skb's on TX ring */
1918 mv643xx_eth_free_all_tx_descs(dev);
1919
1920 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1921
1922 /* Free TX ring */
1923 if (mp->tx_sram_size)
1924 iounmap(mp->p_tx_desc_area);
1925 else
1926 dma_free_coherent(NULL, mp->tx_desc_area_size,
1927 mp->p_tx_desc_area, mp->tx_desc_dma);
1928}
1929
1930
1931/* netdev ops and related ***************************************************/
1932static void eth_port_reset(struct mv643xx_private *mp);
1933
1934/* Set the mv643xx port configuration register for the speed/duplex mode. */
1935static void mv643xx_eth_update_pscr(struct net_device *dev,
1936 struct ethtool_cmd *ecmd)
1937{
1938 struct mv643xx_private *mp = netdev_priv(dev);
1939 int port_num = mp->port_num;
1940 u32 o_pscr, n_pscr;
1941 unsigned int queues;
1942
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001943 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001944 n_pscr = o_pscr;
1945
1946 /* clear speed, duplex and rx buffer size fields */
1947 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1948 SET_GMII_SPEED_TO_1000 |
1949 SET_FULL_DUPLEX_MODE |
1950 MAX_RX_PACKET_MASK);
1951
1952 if (ecmd->duplex == DUPLEX_FULL)
1953 n_pscr |= SET_FULL_DUPLEX_MODE;
1954
1955 if (ecmd->speed == SPEED_1000)
1956 n_pscr |= SET_GMII_SPEED_TO_1000 |
1957 MAX_RX_PACKET_9700BYTE;
1958 else {
1959 if (ecmd->speed == SPEED_100)
1960 n_pscr |= SET_MII_SPEED_TO_100;
1961 n_pscr |= MAX_RX_PACKET_1522BYTE;
1962 }
1963
1964 if (n_pscr != o_pscr) {
1965 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001966 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001967 else {
1968 queues = mv643xx_eth_port_disable_tx(mp);
1969
1970 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001971 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1972 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1973 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001974 if (queues)
1975 mv643xx_eth_port_enable_tx(mp, queues);
1976 }
1977 }
1978}
1979
1980/*
1981 * mv643xx_eth_int_handler
1982 *
1983 * Main interrupt handler for the gigbit ethernet ports
1984 *
1985 * Input : irq - irq number (not used)
1986 * dev_id - a pointer to the required interface's data structure
1987 * regs - not used
1988 * Output : N/A
1989 */
1990
1991static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1992{
1993 struct net_device *dev = (struct net_device *)dev_id;
1994 struct mv643xx_private *mp = netdev_priv(dev);
1995 u32 eth_int_cause, eth_int_cause_ext = 0;
1996 unsigned int port_num = mp->port_num;
1997
1998 /* Read interrupt cause registers */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001999 eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & ETH_INT_UNMASK_ALL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002000 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002001 eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
2002 & ETH_INT_UNMASK_ALL_EXT;
2003 wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002004 }
2005
2006 /* PHY status changed */
2007 if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
2008 struct ethtool_cmd cmd;
2009
2010 if (mii_link_ok(&mp->mii)) {
2011 mii_ethtool_gset(&mp->mii, &cmd);
2012 mv643xx_eth_update_pscr(dev, &cmd);
2013 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
2014 if (!netif_carrier_ok(dev)) {
2015 netif_carrier_on(dev);
2016 if (mp->tx_ring_size - mp->tx_desc_count >=
2017 MAX_DESCS_PER_SKB)
2018 netif_wake_queue(dev);
2019 }
2020 } else if (netif_carrier_ok(dev)) {
2021 netif_stop_queue(dev);
2022 netif_carrier_off(dev);
2023 }
2024 }
2025
2026#ifdef MV643XX_NAPI
2027 if (eth_int_cause & ETH_INT_CAUSE_RX) {
2028 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002029 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002030
2031 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002032 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002033
2034 netif_rx_schedule(dev, &mp->napi);
2035 }
2036#else
2037 if (eth_int_cause & ETH_INT_CAUSE_RX)
2038 mv643xx_eth_receive_queue(dev, INT_MAX);
2039#endif
2040 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
2041 mv643xx_eth_free_completed_tx_descs(dev);
2042
2043 /*
2044 * If no real interrupt occured, exit.
2045 * This can happen when using gigE interrupt coalescing mechanism.
2046 */
2047 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
2048 return IRQ_NONE;
2049
2050 return IRQ_HANDLED;
2051}
2052
2053/*
2054 * ethernet_phy_reset - Reset Ethernet port PHY.
2055 *
2056 * DESCRIPTION:
2057 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2058 *
2059 * INPUT:
2060 * struct mv643xx_private *mp Ethernet Port.
2061 *
2062 * OUTPUT:
2063 * The PHY is reset.
2064 *
2065 * RETURN:
2066 * None.
2067 *
2068 */
2069static void ethernet_phy_reset(struct mv643xx_private *mp)
2070{
2071 unsigned int phy_reg_data;
2072
2073 /* Reset the PHY */
2074 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2075 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2076 eth_port_write_smi_reg(mp, 0, phy_reg_data);
2077
2078 /* wait for PHY to come out of reset */
2079 do {
2080 udelay(1);
2081 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2082 } while (phy_reg_data & 0x8000);
2083}
2084
2085/*
2086 * eth_port_start - Start the Ethernet port activity.
2087 *
2088 * DESCRIPTION:
2089 * This routine prepares the Ethernet port for Rx and Tx activity:
2090 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2091 * has been initialized a descriptor's ring (using
2092 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2093 * 2. Initialize and enable the Ethernet configuration port by writing to
2094 * the port's configuration and command registers.
2095 * 3. Initialize and enable the SDMA by writing to the SDMA's
2096 * configuration and command registers. After completing these steps,
2097 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2098 *
2099 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2100 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2101 * and ether_init_rx_desc_ring for Rx queues).
2102 *
2103 * INPUT:
2104 * dev - a pointer to the required interface
2105 *
2106 * OUTPUT:
2107 * Ethernet port is ready to receive and transmit.
2108 *
2109 * RETURN:
2110 * None.
2111 */
2112static void eth_port_start(struct net_device *dev)
2113{
2114 struct mv643xx_private *mp = netdev_priv(dev);
2115 unsigned int port_num = mp->port_num;
2116 int tx_curr_desc, rx_curr_desc;
2117 u32 pscr;
2118 struct ethtool_cmd ethtool_cmd;
2119
2120 /* Assignment of Tx CTRP of given queue */
2121 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002122 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002123 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2124
2125 /* Assignment of Rx CRDP of given queue */
2126 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002127 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002128 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2129
2130 /* Add the assigned Ethernet address to the port's address table */
2131 eth_port_uc_addr_set(mp, dev->dev_addr);
2132
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002133 /*
2134 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2135 * frames to RX queue #0.
2136 */
2137 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002138
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002139 /*
2140 * Treat BPDUs as normal multicasts, and disable partition mode.
2141 */
2142 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002143
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002144 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002145
2146 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002147 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002148
2149 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2150 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02002151 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002152 DO_NOT_FORCE_LINK_FAIL |
2153 SERIAL_PORT_CONTROL_RESERVED;
2154
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002155 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002156
2157 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002158 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002159
2160 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002161 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002162
2163 /* Enable port Rx. */
2164 mv643xx_eth_port_enable_rx(mp, ETH_RX_QUEUES_ENABLED);
2165
2166 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002167 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002168
2169 /* save phy settings across reset */
2170 mv643xx_get_settings(dev, &ethtool_cmd);
2171 ethernet_phy_reset(mp);
2172 mv643xx_set_settings(dev, &ethtool_cmd);
2173}
2174
2175#ifdef MV643XX_COAL
2176
2177/*
2178 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
2179 *
2180 * DESCRIPTION:
2181 * This routine sets the RX coalescing interrupt mechanism parameter.
2182 * This parameter is a timeout counter, that counts in 64 t_clk
2183 * chunks ; that when timeout event occurs a maskable interrupt
2184 * occurs.
2185 * The parameter is calculated using the tClk of the MV-643xx chip
2186 * , and the required delay of the interrupt in usec.
2187 *
2188 * INPUT:
2189 * struct mv643xx_private *mp Ethernet port
2190 * unsigned int delay Delay in usec
2191 *
2192 * OUTPUT:
2193 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2194 *
2195 * RETURN:
2196 * The interrupt coalescing value set in the gigE port.
2197 *
2198 */
2199static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
2200 unsigned int delay)
2201{
2202 unsigned int port_num = mp->port_num;
2203 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2204
2205 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002206 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002207 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002208 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002209 & 0xffc000ff));
2210
2211 return coal;
2212}
2213#endif
2214
2215/*
2216 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
2217 *
2218 * DESCRIPTION:
2219 * This routine sets the TX coalescing interrupt mechanism parameter.
2220 * This parameter is a timeout counter, that counts in 64 t_clk
2221 * chunks ; that when timeout event occurs a maskable interrupt
2222 * occurs.
2223 * The parameter is calculated using the t_cLK frequency of the
2224 * MV-643xx chip and the required delay in the interrupt in uSec
2225 *
2226 * INPUT:
2227 * struct mv643xx_private *mp Ethernet port
2228 * unsigned int delay Delay in uSeconds
2229 *
2230 * OUTPUT:
2231 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2232 *
2233 * RETURN:
2234 * The interrupt coalescing value set in the gigE port.
2235 *
2236 */
2237static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
2238 unsigned int delay)
2239{
2240 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2241
2242 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002243 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002244
2245 return coal;
2246}
2247
2248/*
2249 * eth_port_init - Initialize the Ethernet port driver
2250 *
2251 * DESCRIPTION:
2252 * This function prepares the ethernet port to start its activity:
2253 * 1) Completes the ethernet port driver struct initialization toward port
2254 * start routine.
2255 * 2) Resets the device to a quiescent state in case of warm reboot.
2256 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2257 * 4) Clean MAC tables. The reset status of those tables is unknown.
2258 * 5) Set PHY address.
2259 * Note: Call this routine prior to eth_port_start routine and after
2260 * setting user values in the user fields of Ethernet port control
2261 * struct.
2262 *
2263 * INPUT:
2264 * struct mv643xx_private *mp Ethernet port control struct
2265 *
2266 * OUTPUT:
2267 * See description.
2268 *
2269 * RETURN:
2270 * None.
2271 */
2272static void eth_port_init(struct mv643xx_private *mp)
2273{
2274 mp->rx_resource_err = 0;
2275
2276 eth_port_reset(mp);
2277
2278 eth_port_init_mac_tables(mp);
2279}
2280
2281/*
2282 * mv643xx_eth_open
2283 *
2284 * This function is called when openning the network device. The function
2285 * should initialize all the hardware, initialize cyclic Rx/Tx
2286 * descriptors chain and buffers and allocate an IRQ to the network
2287 * device.
2288 *
2289 * Input : a pointer to the network device structure
2290 *
2291 * Output : zero of success , nonzero if fails.
2292 */
2293
2294static int mv643xx_eth_open(struct net_device *dev)
2295{
2296 struct mv643xx_private *mp = netdev_priv(dev);
2297 unsigned int port_num = mp->port_num;
2298 unsigned int size;
2299 int err;
2300
2301 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002302 wrl(mp, INT_CAUSE(port_num), 0);
2303 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002304 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002305 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002306
2307 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2308 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2309 if (err) {
2310 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2311 return -EAGAIN;
2312 }
2313
2314 eth_port_init(mp);
2315
2316 memset(&mp->timeout, 0, sizeof(struct timer_list));
2317 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2318 mp->timeout.data = (unsigned long)dev;
2319
2320 /* Allocate RX and TX skb rings */
2321 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2322 GFP_KERNEL);
2323 if (!mp->rx_skb) {
2324 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2325 err = -ENOMEM;
2326 goto out_free_irq;
2327 }
2328 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2329 GFP_KERNEL);
2330 if (!mp->tx_skb) {
2331 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2332 err = -ENOMEM;
2333 goto out_free_rx_skb;
2334 }
2335
2336 /* Allocate TX ring */
2337 mp->tx_desc_count = 0;
2338 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
2339 mp->tx_desc_area_size = size;
2340
2341 if (mp->tx_sram_size) {
2342 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2343 mp->tx_sram_size);
2344 mp->tx_desc_dma = mp->tx_sram_addr;
2345 } else
2346 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2347 &mp->tx_desc_dma,
2348 GFP_KERNEL);
2349
2350 if (!mp->p_tx_desc_area) {
2351 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2352 dev->name, size);
2353 err = -ENOMEM;
2354 goto out_free_tx_skb;
2355 }
2356 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2357 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2358
2359 ether_init_tx_desc_ring(mp);
2360
2361 /* Allocate RX ring */
2362 mp->rx_desc_count = 0;
2363 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
2364 mp->rx_desc_area_size = size;
2365
2366 if (mp->rx_sram_size) {
2367 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2368 mp->rx_sram_size);
2369 mp->rx_desc_dma = mp->rx_sram_addr;
2370 } else
2371 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2372 &mp->rx_desc_dma,
2373 GFP_KERNEL);
2374
2375 if (!mp->p_rx_desc_area) {
2376 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2377 dev->name, size);
2378 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2379 dev->name);
2380 if (mp->rx_sram_size)
2381 iounmap(mp->p_tx_desc_area);
2382 else
2383 dma_free_coherent(NULL, mp->tx_desc_area_size,
2384 mp->p_tx_desc_area, mp->tx_desc_dma);
2385 err = -ENOMEM;
2386 goto out_free_tx_skb;
2387 }
2388 memset((void *)mp->p_rx_desc_area, 0, size);
2389
2390 ether_init_rx_desc_ring(mp);
2391
2392 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2393
2394#ifdef MV643XX_NAPI
2395 napi_enable(&mp->napi);
2396#endif
2397
2398 eth_port_start(dev);
2399
2400 /* Interrupt Coalescing */
2401
2402#ifdef MV643XX_COAL
2403 mp->rx_int_coal =
2404 eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
2405#endif
2406
2407 mp->tx_int_coal =
2408 eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
2409
2410 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002411 wrl(mp, INT_MASK_EXT(port_num), ETH_INT_UNMASK_ALL_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002412
2413 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002414 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002415
2416 return 0;
2417
2418out_free_tx_skb:
2419 kfree(mp->tx_skb);
2420out_free_rx_skb:
2421 kfree(mp->rx_skb);
2422out_free_irq:
2423 free_irq(dev->irq, dev);
2424
2425 return err;
2426}
2427
2428/*
2429 * eth_port_reset - Reset Ethernet port
2430 *
2431 * DESCRIPTION:
2432 * This routine resets the chip by aborting any SDMA engine activity and
2433 * clearing the MIB counters. The Receiver and the Transmit unit are in
2434 * idle state after this command is performed and the port is disabled.
2435 *
2436 * INPUT:
2437 * struct mv643xx_private *mp Ethernet Port.
2438 *
2439 * OUTPUT:
2440 * Channel activity is halted.
2441 *
2442 * RETURN:
2443 * None.
2444 *
2445 */
2446static void eth_port_reset(struct mv643xx_private *mp)
2447{
2448 unsigned int port_num = mp->port_num;
2449 unsigned int reg_data;
2450
2451 mv643xx_eth_port_disable_tx(mp);
2452 mv643xx_eth_port_disable_rx(mp);
2453
2454 /* Clear all MIB counters */
2455 eth_clear_mib_counters(mp);
2456
2457 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002458 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002459 reg_data &= ~(SERIAL_PORT_ENABLE |
2460 DO_NOT_FORCE_LINK_FAIL |
2461 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002462 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002463}
2464
2465/*
2466 * mv643xx_eth_stop
2467 *
2468 * This function is used when closing the network device.
2469 * It updates the hardware,
2470 * release all memory that holds buffers and descriptors and release the IRQ.
2471 * Input : a pointer to the device structure
2472 * Output : zero if success , nonzero if fails
2473 */
2474
2475static int mv643xx_eth_stop(struct net_device *dev)
2476{
2477 struct mv643xx_private *mp = netdev_priv(dev);
2478 unsigned int port_num = mp->port_num;
2479
2480 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002481 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002482 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002483 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002484
2485#ifdef MV643XX_NAPI
2486 napi_disable(&mp->napi);
2487#endif
2488 netif_carrier_off(dev);
2489 netif_stop_queue(dev);
2490
2491 eth_port_reset(mp);
2492
2493 mv643xx_eth_free_tx_rings(dev);
2494 mv643xx_eth_free_rx_rings(dev);
2495
2496 free_irq(dev->irq, dev);
2497
2498 return 0;
2499}
2500
2501static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2502{
2503 struct mv643xx_private *mp = netdev_priv(dev);
2504
2505 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2506}
2507
2508/*
2509 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2510 *
2511 * Input : pointer to ethernet interface network device structure
2512 * new mtu size
2513 * Output : 0 upon success, -EINVAL upon failure
2514 */
2515static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2516{
2517 if ((new_mtu > 9500) || (new_mtu < 64))
2518 return -EINVAL;
2519
2520 dev->mtu = new_mtu;
2521 if (!netif_running(dev))
2522 return 0;
2523
2524 /*
2525 * Stop and then re-open the interface. This will allocate RX
2526 * skbs of the new MTU.
2527 * There is a possible danger that the open will not succeed,
2528 * due to memory being full, which might fail the open function.
2529 */
2530 mv643xx_eth_stop(dev);
2531 if (mv643xx_eth_open(dev)) {
2532 printk(KERN_ERR "%s: Fatal error on opening device\n",
2533 dev->name);
2534 }
2535
2536 return 0;
2537}
2538
2539/*
2540 * mv643xx_eth_tx_timeout_task
2541 *
2542 * Actual routine to reset the adapter when a timeout on Tx has occurred
2543 */
2544static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2545{
2546 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2547 tx_timeout_task);
2548 struct net_device *dev = mp->dev;
2549
2550 if (!netif_running(dev))
2551 return;
2552
2553 netif_stop_queue(dev);
2554
2555 eth_port_reset(mp);
2556 eth_port_start(dev);
2557
2558 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2559 netif_wake_queue(dev);
2560}
2561
2562/*
2563 * mv643xx_eth_tx_timeout
2564 *
2565 * Called upon a timeout on transmitting a packet
2566 *
2567 * Input : pointer to ethernet interface network device structure.
2568 * Output : N/A
2569 */
2570static void mv643xx_eth_tx_timeout(struct net_device *dev)
2571{
2572 struct mv643xx_private *mp = netdev_priv(dev);
2573
2574 printk(KERN_INFO "%s: TX timeout ", dev->name);
2575
2576 /* Do the reset outside of interrupt context */
2577 schedule_work(&mp->tx_timeout_task);
2578}
2579
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002580#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002581static void mv643xx_netpoll(struct net_device *netdev)
2582{
2583 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002584 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002585
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002586 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002587 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002588 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002589
Al Viro9da3b1a2006-10-08 15:00:44 +01002590 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002591
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002592 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002593}
2594#endif
2595
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002596/*
2597 * Wrappers for MII support library.
2598 */
2599static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2600{
2601 struct mv643xx_private *mp = netdev_priv(dev);
2602 int val;
2603
2604 eth_port_read_smi_reg(mp, location, &val);
2605 return val;
2606}
2607
2608static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2609{
2610 struct mv643xx_private *mp = netdev_priv(dev);
2611 eth_port_write_smi_reg(mp, location, val);
2612}
2613
2614
2615/* platform glue ************************************************************/
2616static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2617 struct mbus_dram_target_info *dram)
2618{
2619 void __iomem *base = msp->eth_base;
2620 u32 win_enable;
2621 u32 win_protect;
2622 int i;
2623
2624 for (i = 0; i < 6; i++) {
2625 writel(0, base + WINDOW_BASE(i));
2626 writel(0, base + WINDOW_SIZE(i));
2627 if (i < 4)
2628 writel(0, base + WINDOW_REMAP_HIGH(i));
2629 }
2630
2631 win_enable = 0x3f;
2632 win_protect = 0;
2633
2634 for (i = 0; i < dram->num_cs; i++) {
2635 struct mbus_dram_window *cs = dram->cs + i;
2636
2637 writel((cs->base & 0xffff0000) |
2638 (cs->mbus_attr << 8) |
2639 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2640 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2641
2642 win_enable &= ~(1 << i);
2643 win_protect |= 3 << (2 * i);
2644 }
2645
2646 writel(win_enable, base + WINDOW_BAR_ENABLE);
2647 msp->win_protect = win_protect;
2648}
2649
2650static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2651{
2652 static int mv643xx_version_printed = 0;
2653 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2654 struct mv643xx_shared_private *msp;
2655 struct resource *res;
2656 int ret;
2657
2658 if (!mv643xx_version_printed++)
2659 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2660
2661 ret = -EINVAL;
2662 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2663 if (res == NULL)
2664 goto out;
2665
2666 ret = -ENOMEM;
2667 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2668 if (msp == NULL)
2669 goto out;
2670 memset(msp, 0, sizeof(*msp));
2671
2672 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2673 if (msp->eth_base == NULL)
2674 goto out_free;
2675
2676 spin_lock_init(&msp->phy_lock);
2677 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2678
2679 platform_set_drvdata(pdev, msp);
2680
2681 /*
2682 * (Re-)program MBUS remapping windows if we are asked to.
2683 */
2684 if (pd != NULL && pd->dram != NULL)
2685 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2686
2687 return 0;
2688
2689out_free:
2690 kfree(msp);
2691out:
2692 return ret;
2693}
2694
2695static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2696{
2697 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2698
2699 iounmap(msp->eth_base);
2700 kfree(msp);
2701
2702 return 0;
2703}
2704
2705static struct platform_driver mv643xx_eth_shared_driver = {
2706 .probe = mv643xx_eth_shared_probe,
2707 .remove = mv643xx_eth_shared_remove,
2708 .driver = {
2709 .name = MV643XX_ETH_SHARED_NAME,
2710 .owner = THIS_MODULE,
2711 },
2712};
2713
2714/*
2715 * ethernet_phy_set - Set the ethernet port PHY address.
2716 *
2717 * DESCRIPTION:
2718 * This routine sets the given ethernet port PHY address.
2719 *
2720 * INPUT:
2721 * struct mv643xx_private *mp Ethernet Port.
2722 * int phy_addr PHY address.
2723 *
2724 * OUTPUT:
2725 * None.
2726 *
2727 * RETURN:
2728 * None.
2729 *
2730 */
2731static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
2732{
2733 u32 reg_data;
2734 int addr_shift = 5 * mp->port_num;
2735
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002736 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002737 reg_data &= ~(0x1f << addr_shift);
2738 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002739 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002740}
2741
2742/*
2743 * ethernet_phy_get - Get the ethernet port PHY address.
2744 *
2745 * DESCRIPTION:
2746 * This routine returns the given ethernet port PHY address.
2747 *
2748 * INPUT:
2749 * struct mv643xx_private *mp Ethernet Port.
2750 *
2751 * OUTPUT:
2752 * None.
2753 *
2754 * RETURN:
2755 * PHY address.
2756 *
2757 */
2758static int ethernet_phy_get(struct mv643xx_private *mp)
2759{
2760 unsigned int reg_data;
2761
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002762 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002763
2764 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2765}
2766
2767/*
2768 * ethernet_phy_detect - Detect whether a phy is present
2769 *
2770 * DESCRIPTION:
2771 * This function tests whether there is a PHY present on
2772 * the specified port.
2773 *
2774 * INPUT:
2775 * struct mv643xx_private *mp Ethernet Port.
2776 *
2777 * OUTPUT:
2778 * None
2779 *
2780 * RETURN:
2781 * 0 on success
2782 * -ENODEV on failure
2783 *
2784 */
2785static int ethernet_phy_detect(struct mv643xx_private *mp)
2786{
2787 unsigned int phy_reg_data0;
2788 int auto_neg;
2789
2790 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2791 auto_neg = phy_reg_data0 & 0x1000;
2792 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2793 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2794
2795 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2796 if ((phy_reg_data0 & 0x1000) == auto_neg)
2797 return -ENODEV; /* change didn't take */
2798
2799 phy_reg_data0 ^= 0x1000;
2800 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2801 return 0;
2802}
2803
James Chapmand0412d92006-01-27 01:15:30 -07002804static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2805 int speed, int duplex,
2806 struct ethtool_cmd *cmd)
2807{
2808 struct mv643xx_private *mp = netdev_priv(dev);
2809
2810 memset(cmd, 0, sizeof(*cmd));
2811
2812 cmd->port = PORT_MII;
2813 cmd->transceiver = XCVR_INTERNAL;
2814 cmd->phy_address = phy_address;
2815
2816 if (speed == 0) {
2817 cmd->autoneg = AUTONEG_ENABLE;
2818 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2819 cmd->speed = SPEED_100;
2820 cmd->advertising = ADVERTISED_10baseT_Half |
2821 ADVERTISED_10baseT_Full |
2822 ADVERTISED_100baseT_Half |
2823 ADVERTISED_100baseT_Full;
2824 if (mp->mii.supports_gmii)
2825 cmd->advertising |= ADVERTISED_1000baseT_Full;
2826 } else {
2827 cmd->autoneg = AUTONEG_DISABLE;
2828 cmd->speed = speed;
2829 cmd->duplex = duplex;
2830 }
2831}
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833/*/
2834 * mv643xx_eth_probe
2835 *
2836 * First function called after registering the network device.
2837 * It's purpose is to initialize the device as an ethernet device,
2838 * fill the ethernet device structure with pointers * to functions,
2839 * and set the MAC address of the interface
2840 *
2841 * Input : struct device *
2842 * Output : -ENOMEM if failed , 0 if success
2843 */
Russell King3ae5eae2005-11-09 22:32:44 +00002844static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002847 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct mv643xx_private *mp;
2849 struct net_device *dev;
2850 u8 *p;
2851 struct resource *res;
2852 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002853 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002854 int duplex = DUPLEX_HALF;
2855 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002856 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002858 pd = pdev->dev.platform_data;
2859 if (pd == NULL) {
2860 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2861 return -ENODEV;
2862 }
2863
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002864 if (pd->shared == NULL) {
2865 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2866 return -ENODEV;
2867 }
2868
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2870 if (!dev)
2871 return -ENOMEM;
2872
Russell King3ae5eae2005-11-09 22:32:44 +00002873 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002876 mp->dev = dev;
2877#ifdef MV643XX_NAPI
2878 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2879#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2882 BUG_ON(!res);
2883 dev->irq = res->start;
2884
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 dev->open = mv643xx_eth_open;
2886 dev->stop = mv643xx_eth_stop;
2887 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 dev->set_mac_address = mv643xx_eth_set_mac_address;
2889 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2890
2891 /* No need to Tx Timeout */
2892 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002894#ifdef CONFIG_NET_POLL_CONTROLLER
2895 dev->poll_controller = mv643xx_netpoll;
2896#endif
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 dev->base_addr = 0;
2900 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002901 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
2903
2904#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2905#ifdef MAX_SKB_FRAGS
2906 /*
2907 * Zero copy can only work if we use Discovery II memory. Else, we will
2908 * have to map the buffers to ISA memory which is only 16 MB
2909 */
Wolfram Joost63890572006-01-16 16:57:41 -07002910 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911#endif
2912#endif
2913
2914 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002915 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 spin_lock_init(&mp->lock);
2918
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002919 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002920 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002921
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002922 if (mp->shared->win_protect)
2923 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2924
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002925 mp->shared_smi = mp->shared;
2926 if (pd->shared_smi != NULL)
2927 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2928
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002930 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002931 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
2932 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002934 if (is_valid_ether_addr(pd->mac_addr))
2935 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002937 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002938 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002940 if (pd->rx_queue_size)
2941 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002943 if (pd->tx_queue_size)
2944 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002946 if (pd->tx_sram_size) {
2947 mp->tx_sram_size = pd->tx_sram_size;
2948 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002951 if (pd->rx_sram_size) {
2952 mp->rx_sram_size = pd->rx_sram_size;
2953 mp->rx_sram_addr = pd->rx_sram_addr;
2954 }
2955
2956 duplex = pd->duplex;
2957 speed = pd->speed;
2958
James Chapmanc28a4f82006-01-27 01:13:15 -07002959 /* Hook up MII support for ethtool */
2960 mp->mii.dev = dev;
2961 mp->mii.mdio_read = mv643xx_mdio_read;
2962 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002963 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002964 mp->mii.phy_id_mask = 0x3f;
2965 mp->mii.reg_num_mask = 0x1f;
2966
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002967 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002969 pr_debug("%s: No PHY detected at addr %d\n",
2970 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002971 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 }
2973
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002974 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002975 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002976 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2977 mv643xx_eth_update_pscr(dev, &cmd);
2978 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002979
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002980 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 err = register_netdev(dev);
2982 if (err)
2983 goto out;
2984
2985 p = dev->dev_addr;
2986 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002987 "%s: port %d with MAC address %s\n",
2988 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
2990 if (dev->features & NETIF_F_SG)
2991 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2992
2993 if (dev->features & NETIF_F_IP_CSUM)
2994 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2995 dev->name);
2996
2997#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2998 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2999#endif
3000
3001#ifdef MV643XX_COAL
3002 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
3003 dev->name);
3004#endif
3005
3006#ifdef MV643XX_NAPI
3007 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
3008#endif
3009
Nicolas DETb1529872005-10-28 17:46:30 -07003010 if (mp->tx_sram_size > 0)
3011 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
3012
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 return 0;
3014
3015out:
3016 free_netdev(dev);
3017
3018 return err;
3019}
3020
Russell King3ae5eae2005-11-09 22:32:44 +00003021static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
Russell King3ae5eae2005-11-09 22:32:44 +00003023 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
3025 unregister_netdev(dev);
3026 flush_scheduled_work();
3027
3028 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00003029 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 return 0;
3031}
3032
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003033static void mv643xx_eth_shutdown(struct platform_device *pdev)
3034{
3035 struct net_device *dev = platform_get_drvdata(pdev);
3036 struct mv643xx_private *mp = netdev_priv(dev);
3037 unsigned int port_num = mp->port_num;
3038
3039 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02003040 wrl(mp, INT_MASK(port_num), 0);
3041 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003042
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003043 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003044}
3045
Russell King3ae5eae2005-11-09 22:32:44 +00003046static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 .probe = mv643xx_eth_probe,
3048 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003049 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00003050 .driver = {
3051 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07003052 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003053 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054};
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056/*
3057 * mv643xx_init_module
3058 *
3059 * Registers the network drivers into the Linux kernel
3060 *
3061 * Input : N/A
3062 *
3063 * Output : N/A
3064 */
3065static int __init mv643xx_init_module(void)
3066{
3067 int rc;
3068
Russell King3ae5eae2005-11-09 22:32:44 +00003069 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003071 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003073 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075 return rc;
3076}
3077
3078/*
3079 * mv643xx_cleanup_module
3080 *
3081 * Registers the network drivers into the Linux kernel
3082 *
3083 * Input : N/A
3084 *
3085 * Output : N/A
3086 */
3087static void __exit mv643xx_cleanup_module(void)
3088{
Russell King3ae5eae2005-11-09 22:32:44 +00003089 platform_driver_unregister(&mv643xx_eth_driver);
3090 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
3092
3093module_init(mv643xx_init_module);
3094module_exit(mv643xx_cleanup_module);
3095
3096MODULE_LICENSE("GPL");
3097MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
3098 " and Dale Farnsworth");
3099MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07003100MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3101MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);