blob: ea7a578836c310dfa893d963f5fef3bf607375d0 [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
144/* These macros describe Ethernet Port serial control reg (PSCR) bits */
145#define SERIAL_PORT_DISABLE (0 << 0)
146#define SERIAL_PORT_ENABLE (1 << 0)
147#define DO_NOT_FORCE_LINK_PASS (0 << 1)
148#define FORCE_LINK_PASS (1 << 1)
149#define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
150#define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
151#define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
152#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
153#define ADV_NO_FLOW_CTRL (0 << 4)
154#define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
155#define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
156#define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
157#define FORCE_BP_MODE_NO_JAM (0 << 7)
158#define FORCE_BP_MODE_JAM_TX (1 << 7)
159#define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
160#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
161#define FORCE_LINK_FAIL (0 << 10)
162#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
163#define RETRANSMIT_16_ATTEMPTS (0 << 11)
164#define RETRANSMIT_FOREVER (1 << 11)
165#define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
166#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
167#define DTE_ADV_0 (0 << 14)
168#define DTE_ADV_1 (1 << 14)
169#define DISABLE_AUTO_NEG_BYPASS (0 << 15)
170#define ENABLE_AUTO_NEG_BYPASS (1 << 15)
171#define AUTO_NEG_NO_CHANGE (0 << 16)
172#define RESTART_AUTO_NEG (1 << 16)
173#define MAX_RX_PACKET_1518BYTE (0 << 17)
174#define MAX_RX_PACKET_1522BYTE (1 << 17)
175#define MAX_RX_PACKET_1552BYTE (2 << 17)
176#define MAX_RX_PACKET_9022BYTE (3 << 17)
177#define MAX_RX_PACKET_9192BYTE (4 << 17)
178#define MAX_RX_PACKET_9700BYTE (5 << 17)
179#define MAX_RX_PACKET_MASK (7 << 17)
180#define CLR_EXT_LOOPBACK (0 << 20)
181#define SET_EXT_LOOPBACK (1 << 20)
182#define SET_HALF_DUPLEX_MODE (0 << 21)
183#define SET_FULL_DUPLEX_MODE (1 << 21)
184#define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
185#define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
186#define SET_GMII_SPEED_TO_10_100 (0 << 23)
187#define SET_GMII_SPEED_TO_1000 (1 << 23)
188#define SET_MII_SPEED_TO_10 (0 << 24)
189#define SET_MII_SPEED_TO_100 (1 << 24)
190
191#define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
192 DO_NOT_FORCE_LINK_PASS | \
193 ENABLE_AUTO_NEG_FOR_DUPLX | \
194 DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
195 ADV_SYMMETRIC_FLOW_CTRL | \
196 FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
197 FORCE_BP_MODE_NO_JAM | \
198 (1 << 9) /* reserved */ | \
199 DO_NOT_FORCE_LINK_FAIL | \
200 RETRANSMIT_16_ATTEMPTS | \
201 ENABLE_AUTO_NEG_SPEED_GMII | \
202 DTE_ADV_0 | \
203 DISABLE_AUTO_NEG_BYPASS | \
204 AUTO_NEG_NO_CHANGE | \
205 MAX_RX_PACKET_9700BYTE | \
206 CLR_EXT_LOOPBACK | \
207 SET_FULL_DUPLEX_MODE | \
208 ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
209
210/* These macros describe Ethernet Serial Status reg (PSR) bits */
211#define PORT_STATUS_MODE_10_BIT (1 << 0)
212#define PORT_STATUS_LINK_UP (1 << 1)
213#define PORT_STATUS_FULL_DUPLEX (1 << 2)
214#define PORT_STATUS_FLOW_CONTROL (1 << 3)
215#define PORT_STATUS_GMII_1000 (1 << 4)
216#define PORT_STATUS_MII_100 (1 << 5)
217/* PSR bit 6 is undocumented */
218#define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
219#define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
220#define PORT_STATUS_PARTITION (1 << 9)
221#define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
222/* PSR bits 11-31 are reserved */
223
224#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
225#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
226
227#define DESC_SIZE 64
228
229#define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
230#define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
231
232#define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
233#define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
234#define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
235#define ETH_INT_CAUSE_EXT 0x00000002
236#define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
237
238#define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
239#define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
240#define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
241#define ETH_INT_CAUSE_PHY 0x00010000
242#define ETH_INT_CAUSE_STATE 0x00100000
243#define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
244 ETH_INT_CAUSE_STATE)
245
246#define ETH_INT_MASK_ALL 0x00000000
247#define ETH_INT_MASK_ALL_EXT 0x00000000
248
249#define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
250#define PHY_WAIT_MICRO_SECONDS 10
251
252/* Buffer offset from buffer pointer */
253#define RX_BUF_OFFSET 0x2
254
255/* Gigabit Ethernet Unit Global Registers */
256
257/* MIB Counters register definitions */
258#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
259#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
260#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
261#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
262#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
263#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
264#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
265#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
266#define ETH_MIB_FRAMES_64_OCTETS 0x20
267#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
268#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
269#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
270#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
271#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
272#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
273#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
274#define ETH_MIB_GOOD_FRAMES_SENT 0x40
275#define ETH_MIB_EXCESSIVE_COLLISION 0x44
276#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
277#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
278#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
279#define ETH_MIB_FC_SENT 0x54
280#define ETH_MIB_GOOD_FC_RECEIVED 0x58
281#define ETH_MIB_BAD_FC_RECEIVED 0x5c
282#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
283#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
284#define ETH_MIB_OVERSIZE_RECEIVED 0x68
285#define ETH_MIB_JABBER_RECEIVED 0x6c
286#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
287#define ETH_MIB_BAD_CRC_EVENT 0x74
288#define ETH_MIB_COLLISION 0x78
289#define ETH_MIB_LATE_COLLISION 0x7c
290
291/* Port serial status reg (PSR) */
292#define ETH_INTERFACE_PCM 0x00000001
293#define ETH_LINK_IS_UP 0x00000002
294#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
295#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
296#define ETH_GMII_SPEED_1000 0x00000010
297#define ETH_MII_SPEED_100 0x00000020
298#define ETH_TX_IN_PROGRESS 0x00000080
299#define ETH_BYPASS_ACTIVE 0x00000100
300#define ETH_PORT_AT_PARTITION_STATE 0x00000200
301#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
302
303/* SMI reg */
304#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
305#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
306#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
307#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
308
309/* Interrupt Cause Register Bit Definitions */
310
311/* SDMA command status fields macros */
312
313/* Tx & Rx descriptors status */
314#define ETH_ERROR_SUMMARY 0x00000001
315
316/* Tx & Rx descriptors command */
317#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
318
319/* Tx descriptors status */
320#define ETH_LC_ERROR 0
321#define ETH_UR_ERROR 0x00000002
322#define ETH_RL_ERROR 0x00000004
323#define ETH_LLC_SNAP_FORMAT 0x00000200
324
325/* Rx descriptors status */
326#define ETH_OVERRUN_ERROR 0x00000002
327#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
328#define ETH_RESOURCE_ERROR 0x00000006
329#define ETH_VLAN_TAGGED 0x00080000
330#define ETH_BPDU_FRAME 0x00100000
331#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
332#define ETH_OTHER_FRAME_TYPE 0x00400000
333#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
334#define ETH_FRAME_TYPE_IP_V_4 0x01000000
335#define ETH_FRAME_HEADER_OK 0x02000000
336#define ETH_RX_LAST_DESC 0x04000000
337#define ETH_RX_FIRST_DESC 0x08000000
338#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
339#define ETH_RX_ENABLE_INTERRUPT 0x20000000
340#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
341
342/* Rx descriptors byte count */
343#define ETH_FRAME_FRAGMENTED 0x00000004
344
345/* Tx descriptors command */
346#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
347#define ETH_FRAME_SET_TO_VLAN 0x00008000
348#define ETH_UDP_FRAME 0x00010000
349#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
350#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
351#define ETH_ZERO_PADDING 0x00080000
352#define ETH_TX_LAST_DESC 0x00100000
353#define ETH_TX_FIRST_DESC 0x00200000
354#define ETH_GEN_CRC 0x00400000
355#define ETH_TX_ENABLE_INTERRUPT 0x00800000
356#define ETH_AUTO_MODE 0x40000000
357
358#define ETH_TX_IHL_SHIFT 11
359
360/* typedefs */
361
362typedef enum _eth_func_ret_status {
363 ETH_OK, /* Returned as expected. */
364 ETH_ERROR, /* Fundamental error. */
365 ETH_RETRY, /* Could not process request. Try later.*/
366 ETH_END_OF_JOB, /* Ring has nothing to process. */
367 ETH_QUEUE_FULL, /* Ring resource error. */
368 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
369} ETH_FUNC_RET_STATUS;
370
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200371/* These are for big-endian machines. Little endian needs different
372 * definitions.
373 */
374#if defined(__BIG_ENDIAN)
375struct eth_rx_desc {
376 u16 byte_cnt; /* Descriptor buffer byte count */
377 u16 buf_size; /* Buffer size */
378 u32 cmd_sts; /* Descriptor command status */
379 u32 next_desc_ptr; /* Next descriptor pointer */
380 u32 buf_ptr; /* Descriptor buffer pointer */
381};
382
383struct eth_tx_desc {
384 u16 byte_cnt; /* buffer byte count */
385 u16 l4i_chk; /* CPU provided TCP checksum */
386 u32 cmd_sts; /* Command/status field */
387 u32 next_desc_ptr; /* Pointer to next descriptor */
388 u32 buf_ptr; /* pointer to buffer for this descriptor*/
389};
390#elif defined(__LITTLE_ENDIAN)
391struct eth_rx_desc {
392 u32 cmd_sts; /* Descriptor command status */
393 u16 buf_size; /* Buffer size */
394 u16 byte_cnt; /* Descriptor buffer byte count */
395 u32 buf_ptr; /* Descriptor buffer pointer */
396 u32 next_desc_ptr; /* Next descriptor pointer */
397};
398
399struct eth_tx_desc {
400 u32 cmd_sts; /* Command/status field */
401 u16 l4i_chk; /* CPU provided TCP checksum */
402 u16 byte_cnt; /* buffer byte count */
403 u32 buf_ptr; /* pointer to buffer for this descriptor*/
404 u32 next_desc_ptr; /* Pointer to next descriptor */
405};
406#else
407#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
408#endif
409
410/* Unified struct for Rx and Tx operations. The user is not required to */
411/* be familier with neither Tx nor Rx descriptors. */
412struct pkt_info {
413 unsigned short byte_cnt; /* Descriptor buffer byte count */
414 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
415 unsigned int cmd_sts; /* Descriptor command status */
416 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
417 struct sk_buff *return_info; /* User resource return information */
418};
419
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200420
421/* global *******************************************************************/
422struct mv643xx_shared_private {
423 void __iomem *eth_base;
424
425 /* used to protect SMI_REG, which is shared across ports */
426 spinlock_t phy_lock;
427
428 u32 win_protect;
429
430 unsigned int t_clk;
431};
432
433
434/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200435struct mv643xx_mib_counters {
436 u64 good_octets_received;
437 u32 bad_octets_received;
438 u32 internal_mac_transmit_err;
439 u32 good_frames_received;
440 u32 bad_frames_received;
441 u32 broadcast_frames_received;
442 u32 multicast_frames_received;
443 u32 frames_64_octets;
444 u32 frames_65_to_127_octets;
445 u32 frames_128_to_255_octets;
446 u32 frames_256_to_511_octets;
447 u32 frames_512_to_1023_octets;
448 u32 frames_1024_to_max_octets;
449 u64 good_octets_sent;
450 u32 good_frames_sent;
451 u32 excessive_collision;
452 u32 multicast_frames_sent;
453 u32 broadcast_frames_sent;
454 u32 unrec_mac_control_received;
455 u32 fc_sent;
456 u32 good_fc_received;
457 u32 bad_fc_received;
458 u32 undersize_received;
459 u32 fragments_received;
460 u32 oversize_received;
461 u32 jabber_received;
462 u32 mac_receive_error;
463 u32 bad_crc_event;
464 u32 collision;
465 u32 late_collision;
466};
467
468struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200469 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200470 int port_num; /* User Ethernet port number */
471
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200472 struct mv643xx_shared_private *shared_smi;
473
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200474 u32 rx_sram_addr; /* Base address of rx sram area */
475 u32 rx_sram_size; /* Size of rx sram area */
476 u32 tx_sram_addr; /* Base address of tx sram area */
477 u32 tx_sram_size; /* Size of tx sram area */
478
479 int rx_resource_err; /* Rx ring resource error flag */
480
481 /* Tx/Rx rings managment indexes fields. For driver use */
482
483 /* Next available and first returning Rx resource */
484 int rx_curr_desc_q, rx_used_desc_q;
485
486 /* Next available and first returning Tx resource */
487 int tx_curr_desc_q, tx_used_desc_q;
488
489#ifdef MV643XX_TX_FAST_REFILL
490 u32 tx_clean_threshold;
491#endif
492
493 struct eth_rx_desc *p_rx_desc_area;
494 dma_addr_t rx_desc_dma;
495 int rx_desc_area_size;
496 struct sk_buff **rx_skb;
497
498 struct eth_tx_desc *p_tx_desc_area;
499 dma_addr_t tx_desc_dma;
500 int tx_desc_area_size;
501 struct sk_buff **tx_skb;
502
503 struct work_struct tx_timeout_task;
504
505 struct net_device *dev;
506 struct napi_struct napi;
507 struct net_device_stats stats;
508 struct mv643xx_mib_counters mib_counters;
509 spinlock_t lock;
510 /* Size of Tx Ring per queue */
511 int tx_ring_size;
512 /* Number of tx descriptors in use */
513 int tx_desc_count;
514 /* Size of Rx Ring per queue */
515 int rx_ring_size;
516 /* Number of rx descriptors in use */
517 int rx_desc_count;
518
519 /*
520 * Used in case RX Ring is empty, which can be caused when
521 * system does not have resources (skb's)
522 */
523 struct timer_list timeout;
524
525 u32 rx_int_coal;
526 u32 tx_int_coal;
527 struct mii_if_info mii;
528};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200530
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200531/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700532static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200534 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700537static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200539 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200542
543/* rxq/txq helper functions *************************************************/
544static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
545 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200547 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200548}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200550static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
551{
552 unsigned int port_num = mp->port_num;
553 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700554
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200555 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200556 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200557 if (queues) {
558 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200559 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200560
561 /* Wait for all Rx activity to terminate. */
562 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200563 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200564 udelay(PHY_WAIT_MICRO_SECONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200567 return queues;
568}
569
570static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
571 unsigned int queues)
572{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200573 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200574}
575
576static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
577{
578 unsigned int port_num = mp->port_num;
579 u32 queues;
580
581 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200582 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200583 if (queues) {
584 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200585 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200586
587 /* Wait for all Tx activity to terminate. */
588 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200589 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200590 udelay(PHY_WAIT_MICRO_SECONDS);
591
592 /* Wait for Tx FIFO to empty */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200593 while (rdl(mp, PORT_STATUS(port_num)) & ETH_PORT_TX_FIFO_EMPTY)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200594 udelay(PHY_WAIT_MICRO_SECONDS);
595 }
596
597 return queues;
598}
599
600
601/* rx ***********************************************************************/
602static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
603
604/*
605 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
606 *
607 * DESCRIPTION:
608 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
609 * next 'used' descriptor and attached the returned buffer to it.
610 * In case the Rx ring was in "resource error" condition, where there are
611 * no available Rx resources, the function resets the resource error flag.
612 *
613 * INPUT:
614 * struct mv643xx_private *mp Ethernet Port Control srtuct.
615 * struct pkt_info *p_pkt_info Information on returned buffer.
616 *
617 * OUTPUT:
618 * New available Rx resource in Rx descriptor ring.
619 *
620 * RETURN:
621 * ETH_ERROR in case the routine can not access Rx desc ring.
622 * ETH_OK otherwise.
623 */
624static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
625 struct pkt_info *p_pkt_info)
626{
627 int used_rx_desc; /* Where to return Rx resource */
628 volatile struct eth_rx_desc *p_used_rx_desc;
629 unsigned long flags;
630
631 spin_lock_irqsave(&mp->lock, flags);
632
633 /* Get 'used' Rx descriptor */
634 used_rx_desc = mp->rx_used_desc_q;
635 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
636
637 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
638 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
639 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
640
641 /* Flush the write pipe */
642
643 /* Return the descriptor to DMA ownership */
644 wmb();
645 p_used_rx_desc->cmd_sts =
646 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
647 wmb();
648
649 /* Move the used descriptor pointer to the next descriptor */
650 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
651
652 /* Any Rx return cancels the Rx resource error status */
653 mp->rx_resource_err = 0;
654
655 spin_unlock_irqrestore(&mp->lock, flags);
656
657 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700661 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 *
663 * Fills / refills RX queue on a certain gigabit ethernet port
664 *
665 * Input : pointer to ethernet interface network device structure
666 * Output : N/A
667 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700668static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct mv643xx_private *mp = netdev_priv(dev);
671 struct pkt_info pkt_info;
672 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700673 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700675 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000676 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (!skb)
678 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700679 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000680 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700681 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000682 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700684 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
685 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
686 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 pkt_info.return_info = skb;
688 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
689 printk(KERN_ERR
690 "%s: Error allocating RX Ring\n", dev->name);
691 break;
692 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700693 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 /*
696 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700697 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700699 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700701 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704}
705
706/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700707 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 *
709 * Timer routine to wake up RX queue filling task. This function is
710 * used only in case the RX queue is empty, and all alloc_skb has
711 * failed (due to out of memory event).
712 *
713 * Input : pointer to ethernet interface network device structure
714 * Output : N/A
715 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700716static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700718 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
721/*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200722 * eth_port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200724 * DESCRIPTION:
725 * This routine returns the received data to the caller. There is no
726 * data copying during routine operation. All information is returned
727 * using pointer to packet information struct passed from the caller.
728 * If the routine exhausts Rx ring resources then the resource error flag
729 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200731 * INPUT:
732 * struct mv643xx_private *mp Ethernet Port Control srtuct.
733 * struct pkt_info *p_pkt_info User packet buffer.
734 *
735 * OUTPUT:
736 * Rx ring current and used indexes are updated.
737 *
738 * RETURN:
739 * ETH_ERROR in case the routine can not access Rx desc ring.
740 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
741 * ETH_END_OF_JOB if there is no received data.
742 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200744static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
745 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200747 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
748 volatile struct eth_rx_desc *p_rx_desc;
749 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700750 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200752 /* Do not process Rx ring in case of Rx ring resource error */
753 if (mp->rx_resource_err)
754 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700755
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200756 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700757
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200758 /* Get the Rx Desc ring 'curr and 'used' indexes */
759 rx_curr_desc = mp->rx_curr_desc_q;
760 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200762 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700763
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200764 /* The following parameters are used to save readings from memory */
765 command_status = p_rx_desc->cmd_sts;
766 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700767
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200768 /* Nothing to receive... */
769 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700770 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200771 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200774 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
775 p_pkt_info->cmd_sts = command_status;
776 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
777 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
778 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200780 /*
781 * Clean the return info field to indicate that the
782 * packet has been moved to the upper layers
783 */
784 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700785
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200786 /* Update current index in data structure */
787 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
788 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700789
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200790 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
791 if (rx_next_curr_desc == rx_used_desc)
792 mp->rx_resource_err = 1;
793
794 spin_unlock_irqrestore(&mp->lock, flags);
795
796 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700797}
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/*
800 * mv643xx_eth_receive
801 *
802 * This function is forward packets that are received from the port's
803 * queues toward kernel core or FastRoute them to another interface.
804 *
805 * Input : dev - a pointer to the required interface
806 * max - maximum number to receive (0 means unlimted)
807 *
808 * Output : number of served packets
809 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700813 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 unsigned int received_packets = 0;
815 struct sk_buff *skb;
816 struct pkt_info pkt_info;
817
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700818 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400819 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700820 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700821 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700823
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700824 /*
825 * Update statistics.
826 * Note byte count includes 4 byte CRC count
827 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 stats->rx_packets++;
829 stats->rx_bytes += pkt_info.byte_cnt;
830 skb = pkt_info.return_info;
831 /*
832 * In case received a packet without first / last bits on OR
833 * the error summary bit is on, the packets needs to be dropeed.
834 */
835 if (((pkt_info.cmd_sts
836 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
837 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
838 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
839 stats->rx_dropped++;
840 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
841 ETH_RX_LAST_DESC)) !=
842 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
843 if (net_ratelimit())
844 printk(KERN_ERR
845 "%s: Received packet spread "
846 "on multiple descriptors\n",
847 dev->name);
848 }
849 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
850 stats->rx_errors++;
851
852 dev_kfree_skb_irq(skb);
853 } else {
854 /*
855 * The -4 is for the CRC in the trailer of the
856 * received packet
857 */
858 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
861 skb->ip_summed = CHECKSUM_UNNECESSARY;
862 skb->csum = htons(
863 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
864 }
865 skb->protocol = eth_type_trans(skb, dev);
866#ifdef MV643XX_NAPI
867 netif_receive_skb(skb);
868#else
869 netif_rx(skb);
870#endif
871 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700872 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700874 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 return received_packets;
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880/*
881 * mv643xx_poll
882 *
883 * This function is used in case of NAPI
884 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700885static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700887 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
888 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700890 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892#ifdef MV643XX_TX_FAST_REFILL
893 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700894 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897#endif
898
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700899 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200900 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700901 != (u32) mp->rx_used_desc_q)
902 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700904 if (work_done < budget) {
905 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200906 wrl(mp, INT_CAUSE(port_num), 0);
907 wrl(mp, INT_CAUSE_EXT(port_num), 0);
908 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700911 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913#endif
914
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200915
916/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700917/**
918 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
919 *
920 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700921 * This helper function detects that case.
922 */
923
924static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
925{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700926 unsigned int frag;
927 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700928
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700929 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
930 fragp = &skb_shinfo(skb)->frags[frag];
931 if (fragp->size <= 8 && fragp->page_offset & 0x7)
932 return 1;
933 }
934 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700935}
936
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700937/**
938 * eth_alloc_tx_desc_index - return the index of the next available tx desc
939 */
940static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
941{
942 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700943
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700944 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700945
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700946 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700947 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
948
949 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
950
951 return tx_desc_curr;
952}
953
954/**
955 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700957 * Ensure the data for each fragment to be transmitted is mapped properly,
958 * then fill in descriptors in the tx hw queue.
959 */
960static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
961 struct sk_buff *skb)
962{
963 int frag;
964 int tx_index;
965 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700966
967 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
968 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
969
970 tx_index = eth_alloc_tx_desc_index(mp);
971 desc = &mp->p_tx_desc_area[tx_index];
972
973 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
974 /* Last Frag enables interrupt and frees the skb */
975 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
976 desc->cmd_sts |= ETH_ZERO_PADDING |
977 ETH_TX_LAST_DESC |
978 ETH_TX_ENABLE_INTERRUPT;
979 mp->tx_skb[tx_index] = skb;
980 } else
Al Viro05980772006-05-30 23:59:09 -0400981 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700982
983 desc = &mp->p_tx_desc_area[tx_index];
984 desc->l4i_chk = 0;
985 desc->byte_cnt = this_frag->size;
986 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
987 this_frag->page_offset,
988 this_frag->size,
989 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700990 }
991}
992
Byron Bradley324ff2c2008-02-04 23:47:15 -0800993static inline __be16 sum16_as_be(__sum16 sum)
994{
995 return (__force __be16)sum;
996}
997
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700998/**
999 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001001 * Ensure the data for an skb to be transmitted is mapped properly,
1002 * then fill in descriptors in the tx hw queue and start the hardware.
1003 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001004static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
1005 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001006{
1007 int tx_index;
1008 struct eth_tx_desc *desc;
1009 u32 cmd_sts;
1010 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001011 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001012
1013 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
1014
1015 tx_index = eth_alloc_tx_desc_index(mp);
1016 desc = &mp->p_tx_desc_area[tx_index];
1017
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001018 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001019 eth_tx_fill_frag_descs(mp, skb);
1020
1021 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -04001022 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001023 } else {
1024 cmd_sts |= ETH_ZERO_PADDING |
1025 ETH_TX_LAST_DESC |
1026 ETH_TX_ENABLE_INTERRUPT;
1027 length = skb->len;
1028 mp->tx_skb[tx_index] = skb;
1029 }
1030
1031 desc->byte_cnt = length;
1032 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001033
Patrick McHardy84fa7932006-08-29 16:44:56 -07001034 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -08001035 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001036
1037 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
1038 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001039 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001040
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001041 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001042 case IPPROTO_UDP:
1043 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -08001044 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001045 break;
1046 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -08001047 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001048 break;
1049 default:
1050 BUG();
1051 }
1052 } else {
1053 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1054 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1055 desc->l4i_chk = 0;
1056 }
1057
1058 /* ensure all other descriptors are written before first cmd_sts */
1059 wmb();
1060 desc->cmd_sts = cmd_sts;
1061
1062 /* ensure all descriptors are written before poking hardware */
1063 wmb();
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001064 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001065
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001066 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001067}
1068
1069/**
1070 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 */
1073static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1074{
1075 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001076 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001079 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -07001080
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001081 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1082 stats->tx_dropped++;
1083 printk(KERN_DEBUG "%s: failed to linearize tiny "
1084 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001085 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -07001086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 spin_lock_irqsave(&mp->lock, flags);
1089
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001090 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1091 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1092 netif_stop_queue(dev);
1093 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001094 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001095 }
1096
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001097 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -07001098 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 stats->tx_packets++;
1100 dev->trans_start = jiffies;
1101
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001102 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1103 netif_stop_queue(dev);
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 spin_unlock_irqrestore(&mp->lock, flags);
1106
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001107 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108}
1109
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001110
1111/* mii management interface *************************************************/
1112static int ethernet_phy_get(struct mv643xx_private *mp);
1113
1114/*
1115 * eth_port_read_smi_reg - Read PHY registers
1116 *
1117 * DESCRIPTION:
1118 * This routine utilize the SMI interface to interact with the PHY in
1119 * order to perform PHY register read.
1120 *
1121 * INPUT:
1122 * struct mv643xx_private *mp Ethernet Port.
1123 * unsigned int phy_reg PHY register address offset.
1124 * unsigned int *value Register value buffer.
1125 *
1126 * OUTPUT:
1127 * Write the value of a specified PHY register into given buffer.
1128 *
1129 * RETURN:
1130 * false if the PHY is busy or read data is not in valid state.
1131 * true otherwise.
1132 *
1133 */
1134static void eth_port_read_smi_reg(struct mv643xx_private *mp,
1135 unsigned int phy_reg, unsigned int *value)
1136{
1137 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1138 int phy_addr = ethernet_phy_get(mp);
1139 unsigned long flags;
1140 int i;
1141
1142 /* the SMI register is a shared resource */
1143 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1144
1145 /* wait for the SMI register to become available */
1146 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1147 if (i == PHY_WAIT_ITERATIONS) {
1148 printk("%s: PHY busy timeout\n", mp->dev->name);
1149 goto out;
1150 }
1151 udelay(PHY_WAIT_MICRO_SECONDS);
1152 }
1153
1154 writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
1155 smi_reg);
1156
1157 /* now wait for the data to be valid */
1158 for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
1159 if (i == PHY_WAIT_ITERATIONS) {
1160 printk("%s: PHY read timeout\n", mp->dev->name);
1161 goto out;
1162 }
1163 udelay(PHY_WAIT_MICRO_SECONDS);
1164 }
1165
1166 *value = readl(smi_reg) & 0xffff;
1167out:
1168 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1169}
1170
1171/*
1172 * eth_port_write_smi_reg - Write to PHY registers
1173 *
1174 * DESCRIPTION:
1175 * This routine utilize the SMI interface to interact with the PHY in
1176 * order to perform writes to PHY registers.
1177 *
1178 * INPUT:
1179 * struct mv643xx_private *mp Ethernet Port.
1180 * unsigned int phy_reg PHY register address offset.
1181 * unsigned int value Register value.
1182 *
1183 * OUTPUT:
1184 * Write the given value to the specified PHY register.
1185 *
1186 * RETURN:
1187 * false if the PHY is busy.
1188 * true otherwise.
1189 *
1190 */
1191static void eth_port_write_smi_reg(struct mv643xx_private *mp,
1192 unsigned int phy_reg, unsigned int value)
1193{
1194 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1195 int phy_addr = ethernet_phy_get(mp);
1196 unsigned long flags;
1197 int i;
1198
1199 /* the SMI register is a shared resource */
1200 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1201
1202 /* wait for the SMI register to become available */
1203 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1204 if (i == PHY_WAIT_ITERATIONS) {
1205 printk("%s: PHY busy timeout\n", mp->dev->name);
1206 goto out;
1207 }
1208 udelay(PHY_WAIT_MICRO_SECONDS);
1209 }
1210
1211 writel((phy_addr << 16) | (phy_reg << 21) |
1212 ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
1213out:
1214 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1215}
1216
1217
1218/* mib counters *************************************************************/
1219/*
1220 * eth_clear_mib_counters - Clear all MIB counters
1221 *
1222 * DESCRIPTION:
1223 * This function clears all MIB counters of a specific ethernet port.
1224 * A read from the MIB counter will reset the counter.
1225 *
1226 * INPUT:
1227 * struct mv643xx_private *mp Ethernet Port.
1228 *
1229 * OUTPUT:
1230 * After reading all MIB counters, the counters resets.
1231 *
1232 * RETURN:
1233 * MIB counter value.
1234 *
1235 */
1236static void eth_clear_mib_counters(struct mv643xx_private *mp)
1237{
1238 unsigned int port_num = mp->port_num;
1239 int i;
1240
1241 /* Perform dummy reads from MIB counters */
1242 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
1243 i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001244 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001245}
1246
1247static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1248{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001249 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001250}
1251
1252static void eth_update_mib_counters(struct mv643xx_private *mp)
1253{
1254 struct mv643xx_mib_counters *p = &mp->mib_counters;
1255 int offset;
1256
1257 p->good_octets_received +=
1258 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
1259 p->good_octets_received +=
1260 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
1261
1262 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
1263 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
1264 offset += 4)
1265 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1266
1267 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
1268 p->good_octets_sent +=
1269 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
1270
1271 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
1272 offset <= ETH_MIB_LATE_COLLISION;
1273 offset += 4)
1274 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1275}
1276
1277
1278/* ethtool ******************************************************************/
1279struct mv643xx_stats {
1280 char stat_string[ETH_GSTRING_LEN];
1281 int sizeof_stat;
1282 int stat_offset;
1283};
1284
1285#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1286 offsetof(struct mv643xx_private, m)
1287
1288static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1289 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1290 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1291 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1292 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1293 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1294 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1295 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1296 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1297 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1298 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1299 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1300 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1301 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1302 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1303 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1304 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1305 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1306 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1307 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1308 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1309 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1310 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1311 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1312 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1313 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1314 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1315 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1316 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1317 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1318 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1319 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1320 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1321 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1322 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1323 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1324 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1325 { "collision", MV643XX_STAT(mib_counters.collision) },
1326 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1327};
1328
1329#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1330
1331static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1332{
1333 struct mv643xx_private *mp = netdev_priv(dev);
1334 int err;
1335
1336 spin_lock_irq(&mp->lock);
1337 err = mii_ethtool_gset(&mp->mii, cmd);
1338 spin_unlock_irq(&mp->lock);
1339
1340 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1341 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1342 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1343
1344 return err;
1345}
1346
1347static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1348{
1349 struct mv643xx_private *mp = netdev_priv(dev);
1350 int err;
1351
1352 spin_lock_irq(&mp->lock);
1353 err = mii_ethtool_sset(&mp->mii, cmd);
1354 spin_unlock_irq(&mp->lock);
1355
1356 return err;
1357}
1358
1359static void mv643xx_get_drvinfo(struct net_device *netdev,
1360 struct ethtool_drvinfo *drvinfo)
1361{
1362 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1363 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1364 strncpy(drvinfo->fw_version, "N/A", 32);
1365 strncpy(drvinfo->bus_info, "mv643xx", 32);
1366 drvinfo->n_stats = MV643XX_STATS_LEN;
1367}
1368
1369static int mv643xx_eth_nway_restart(struct net_device *dev)
1370{
1371 struct mv643xx_private *mp = netdev_priv(dev);
1372
1373 return mii_nway_restart(&mp->mii);
1374}
1375
1376static u32 mv643xx_eth_get_link(struct net_device *dev)
1377{
1378 struct mv643xx_private *mp = netdev_priv(dev);
1379
1380 return mii_link_ok(&mp->mii);
1381}
1382
1383static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1384 uint8_t *data)
1385{
1386 int i;
1387
1388 switch(stringset) {
1389 case ETH_SS_STATS:
1390 for (i=0; i < MV643XX_STATS_LEN; i++) {
1391 memcpy(data + i * ETH_GSTRING_LEN,
1392 mv643xx_gstrings_stats[i].stat_string,
1393 ETH_GSTRING_LEN);
1394 }
1395 break;
1396 }
1397}
1398
1399static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1400 struct ethtool_stats *stats, uint64_t *data)
1401{
1402 struct mv643xx_private *mp = netdev->priv;
1403 int i;
1404
1405 eth_update_mib_counters(mp);
1406
1407 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1408 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1409 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1410 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1411 }
1412}
1413
1414static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1415{
1416 switch (sset) {
1417 case ETH_SS_STATS:
1418 return MV643XX_STATS_LEN;
1419 default:
1420 return -EOPNOTSUPP;
1421 }
1422}
1423
1424static const struct ethtool_ops mv643xx_ethtool_ops = {
1425 .get_settings = mv643xx_get_settings,
1426 .set_settings = mv643xx_set_settings,
1427 .get_drvinfo = mv643xx_get_drvinfo,
1428 .get_link = mv643xx_eth_get_link,
1429 .set_sg = ethtool_op_set_sg,
1430 .get_sset_count = mv643xx_get_sset_count,
1431 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1432 .get_strings = mv643xx_get_strings,
1433 .nway_reset = mv643xx_eth_nway_restart,
1434};
1435
1436
1437/* address handling *********************************************************/
1438/*
1439 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1440 */
1441static void eth_port_uc_addr_get(struct mv643xx_private *mp,
1442 unsigned char *p_addr)
1443{
1444 unsigned int port_num = mp->port_num;
1445 unsigned int mac_h;
1446 unsigned int mac_l;
1447
1448 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1449 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1450
1451 p_addr[0] = (mac_h >> 24) & 0xff;
1452 p_addr[1] = (mac_h >> 16) & 0xff;
1453 p_addr[2] = (mac_h >> 8) & 0xff;
1454 p_addr[3] = mac_h & 0xff;
1455 p_addr[4] = (mac_l >> 8) & 0xff;
1456 p_addr[5] = mac_l & 0xff;
1457}
1458
1459/*
1460 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1461 *
1462 * DESCRIPTION:
1463 * Go through all the DA filter tables (Unicast, Special Multicast &
1464 * Other Multicast) and set each entry to 0.
1465 *
1466 * INPUT:
1467 * struct mv643xx_private *mp Ethernet Port.
1468 *
1469 * OUTPUT:
1470 * Multicast and Unicast packets are rejected.
1471 *
1472 * RETURN:
1473 * None.
1474 */
1475static void eth_port_init_mac_tables(struct mv643xx_private *mp)
1476{
1477 unsigned int port_num = mp->port_num;
1478 int table_index;
1479
1480 /* Clear DA filter unicast table (Ex_dFUT) */
1481 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001482 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001483
1484 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1485 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001486 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001487 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001488 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001489 }
1490}
1491
1492/*
1493 * The entries in each table are indexed by a hash of a packet's MAC
1494 * address. One bit in each entry determines whether the packet is
1495 * accepted. There are 4 entries (each 8 bits wide) in each register
1496 * of the table. The bits in each entry are defined as follows:
1497 * 0 Accept=1, Drop=0
1498 * 3-1 Queue (ETH_Q0=0)
1499 * 7-4 Reserved = 0;
1500 */
1501static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
1502 int table, unsigned char entry)
1503{
1504 unsigned int table_reg;
1505 unsigned int tbl_offset;
1506 unsigned int reg_offset;
1507
1508 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1509 reg_offset = entry % 4; /* Entry offset within the register */
1510
1511 /* Set "accepts frame bit" at specified table entry */
1512 table_reg = rdl(mp, table + tbl_offset);
1513 table_reg |= 0x01 << (8 * reg_offset);
1514 wrl(mp, table + tbl_offset, table_reg);
1515}
1516
1517/*
1518 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1519 */
1520static void eth_port_uc_addr_set(struct mv643xx_private *mp,
1521 unsigned char *p_addr)
1522{
1523 unsigned int port_num = mp->port_num;
1524 unsigned int mac_h;
1525 unsigned int mac_l;
1526 int table;
1527
1528 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1529 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1530 (p_addr[3] << 0);
1531
1532 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1533 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1534
1535 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001536 table = UNICAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001537 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
1538}
1539
1540/*
1541 * mv643xx_eth_update_mac_address
1542 *
1543 * Update the MAC address of the port in the address table
1544 *
1545 * Input : pointer to ethernet interface network device structure
1546 * Output : N/A
1547 */
1548static void mv643xx_eth_update_mac_address(struct net_device *dev)
1549{
1550 struct mv643xx_private *mp = netdev_priv(dev);
1551
1552 eth_port_init_mac_tables(mp);
1553 eth_port_uc_addr_set(mp, dev->dev_addr);
1554}
1555
1556/*
1557 * mv643xx_eth_set_mac_address
1558 *
1559 * Change the interface's mac address.
1560 * No special hardware thing should be done because interface is always
1561 * put in promiscuous mode.
1562 *
1563 * Input : pointer to ethernet interface network device structure and
1564 * a pointer to the designated entry to be added to the cache.
1565 * Output : zero upon success, negative upon failure
1566 */
1567static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1568{
1569 int i;
1570
1571 for (i = 0; i < 6; i++)
1572 /* +2 is for the offset of the HW addr type */
1573 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1574 mv643xx_eth_update_mac_address(dev);
1575 return 0;
1576}
1577
1578/*
1579 * eth_port_mc_addr - Multicast address settings.
1580 *
1581 * The MV device supports multicast using two tables:
1582 * 1) Special Multicast Table for MAC addresses of the form
1583 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1584 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1585 * Table entries in the DA-Filter table.
1586 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1587 * is used as an index to the Other Multicast Table entries in the
1588 * DA-Filter table. This function calculates the CRC-8bit value.
1589 * In either case, eth_port_set_filter_table_entry() is then called
1590 * to set to set the actual table entry.
1591 */
1592static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
1593{
1594 unsigned int port_num = mp->port_num;
1595 unsigned int mac_h;
1596 unsigned int mac_l;
1597 unsigned char crc_result = 0;
1598 int table;
1599 int mac_array[48];
1600 int crc[8];
1601 int i;
1602
1603 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1604 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001605 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001606 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
1607 return;
1608 }
1609
1610 /* Calculate CRC-8 out of the given address */
1611 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1612 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1613 (p_addr[4] << 8) | (p_addr[5] << 0);
1614
1615 for (i = 0; i < 32; i++)
1616 mac_array[i] = (mac_l >> i) & 0x1;
1617 for (i = 32; i < 48; i++)
1618 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1619
1620 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1621 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1622 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1623 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1624 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1625
1626 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1627 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1628 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1629 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1630 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1631 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1632 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1633
1634 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1635 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1636 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1637 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1638 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1639 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1640
1641 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1642 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1643 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1644 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1645 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1646 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1647
1648 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1649 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1650 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1651 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1652 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1653 mac_array[3] ^ mac_array[2];
1654
1655 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1656 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1657 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1658 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1659 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1660 mac_array[4] ^ mac_array[3];
1661
1662 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1663 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1664 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1665 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1666 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1667 mac_array[4];
1668
1669 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1670 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1671 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1672 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1673 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1674
1675 for (i = 0; i < 8; i++)
1676 crc_result = crc_result | (crc[i] << i);
1677
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001678 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001679 eth_port_set_filter_table_entry(mp, table, crc_result);
1680}
1681
1682/*
1683 * Set the entire multicast list based on dev->mc_list.
1684 */
1685static void eth_port_set_multicast_list(struct net_device *dev)
1686{
1687
1688 struct dev_mc_list *mc_list;
1689 int i;
1690 int table_index;
1691 struct mv643xx_private *mp = netdev_priv(dev);
1692 unsigned int eth_port_num = mp->port_num;
1693
1694 /* If the device is in promiscuous mode or in all multicast mode,
1695 * we will fully populate both multicast tables with accept.
1696 * This is guaranteed to yield a match on all multicast addresses...
1697 */
1698 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1699 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1700 /* Set all entries in DA filter special multicast
1701 * table (Ex_dFSMT)
1702 * Set for ETH_Q0 for now
1703 * Bits
1704 * 0 Accept=1, Drop=0
1705 * 3-1 Queue ETH_Q0=0
1706 * 7-4 Reserved = 0;
1707 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001708 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001709
1710 /* Set all entries in DA filter other multicast
1711 * table (Ex_dFOMT)
1712 * Set for ETH_Q0 for now
1713 * Bits
1714 * 0 Accept=1, Drop=0
1715 * 3-1 Queue ETH_Q0=0
1716 * 7-4 Reserved = 0;
1717 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001718 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001719 }
1720 return;
1721 }
1722
1723 /* We will clear out multicast tables every time we get the list.
1724 * Then add the entire new list...
1725 */
1726 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1727 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001728 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001729
1730 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001731 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001732 }
1733
1734 /* Get pointer to net_device multicast list and add each one... */
1735 for (i = 0, mc_list = dev->mc_list;
1736 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1737 i++, mc_list = mc_list->next)
1738 if (mc_list->dmi_addrlen == 6)
1739 eth_port_mc_addr(mp, mc_list->dmi_addr);
1740}
1741
1742/*
1743 * mv643xx_eth_set_rx_mode
1744 *
1745 * Change from promiscuos to regular rx mode
1746 *
1747 * Input : pointer to ethernet interface network device structure
1748 * Output : N/A
1749 */
1750static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1751{
1752 struct mv643xx_private *mp = netdev_priv(dev);
1753 u32 config_reg;
1754
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001755 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001756 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001757 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001758 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001759 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001760 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001761
1762 eth_port_set_multicast_list(dev);
1763}
1764
1765
1766/* rx/tx queue initialisation ***********************************************/
1767/*
1768 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1769 *
1770 * DESCRIPTION:
1771 * This function prepares a Rx chained list of descriptors and packet
1772 * buffers in a form of a ring. The routine must be called after port
1773 * initialization routine and before port start routine.
1774 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1775 * devices in the system (i.e. DRAM). This function uses the ethernet
1776 * struct 'virtual to physical' routine (set by the user) to set the ring
1777 * with physical addresses.
1778 *
1779 * INPUT:
1780 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1781 *
1782 * OUTPUT:
1783 * The routine updates the Ethernet port control struct with information
1784 * regarding the Rx descriptors and buffers.
1785 *
1786 * RETURN:
1787 * None.
1788 */
1789static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1790{
1791 volatile struct eth_rx_desc *p_rx_desc;
1792 int rx_desc_num = mp->rx_ring_size;
1793 int i;
1794
1795 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1796 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1797 for (i = 0; i < rx_desc_num; i++) {
1798 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1799 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1800 }
1801
1802 /* Save Rx desc pointer to driver struct. */
1803 mp->rx_curr_desc_q = 0;
1804 mp->rx_used_desc_q = 0;
1805
1806 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1807}
1808
1809static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1810{
1811 struct mv643xx_private *mp = netdev_priv(dev);
1812 int curr;
1813
1814 /* Stop RX Queues */
1815 mv643xx_eth_port_disable_rx(mp);
1816
1817 /* Free preallocated skb's on RX rings */
1818 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1819 if (mp->rx_skb[curr]) {
1820 dev_kfree_skb(mp->rx_skb[curr]);
1821 mp->rx_desc_count--;
1822 }
1823 }
1824
1825 if (mp->rx_desc_count)
1826 printk(KERN_ERR
1827 "%s: Error in freeing Rx Ring. %d skb's still"
1828 " stuck in RX Ring - ignoring them\n", dev->name,
1829 mp->rx_desc_count);
1830 /* Free RX ring */
1831 if (mp->rx_sram_size)
1832 iounmap(mp->p_rx_desc_area);
1833 else
1834 dma_free_coherent(NULL, mp->rx_desc_area_size,
1835 mp->p_rx_desc_area, mp->rx_desc_dma);
1836}
1837
1838/*
1839 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1840 *
1841 * DESCRIPTION:
1842 * This function prepares a Tx chained list of descriptors and packet
1843 * buffers in a form of a ring. The routine must be called after port
1844 * initialization routine and before port start routine.
1845 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1846 * devices in the system (i.e. DRAM). This function uses the ethernet
1847 * struct 'virtual to physical' routine (set by the user) to set the ring
1848 * with physical addresses.
1849 *
1850 * INPUT:
1851 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1852 *
1853 * OUTPUT:
1854 * The routine updates the Ethernet port control struct with information
1855 * regarding the Tx descriptors and buffers.
1856 *
1857 * RETURN:
1858 * None.
1859 */
1860static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1861{
1862 int tx_desc_num = mp->tx_ring_size;
1863 struct eth_tx_desc *p_tx_desc;
1864 int i;
1865
1866 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1867 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1868 for (i = 0; i < tx_desc_num; i++) {
1869 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1870 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1871 }
1872
1873 mp->tx_curr_desc_q = 0;
1874 mp->tx_used_desc_q = 0;
1875
1876 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1877}
1878
1879/**
1880 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1881 *
1882 * If force is non-zero, frees uncompleted descriptors as well
1883 */
1884static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1885{
1886 struct mv643xx_private *mp = netdev_priv(dev);
1887 struct eth_tx_desc *desc;
1888 u32 cmd_sts;
1889 struct sk_buff *skb;
1890 unsigned long flags;
1891 int tx_index;
1892 dma_addr_t addr;
1893 int count;
1894 int released = 0;
1895
1896 while (mp->tx_desc_count > 0) {
1897 spin_lock_irqsave(&mp->lock, flags);
1898
1899 /* tx_desc_count might have changed before acquiring the lock */
1900 if (mp->tx_desc_count <= 0) {
1901 spin_unlock_irqrestore(&mp->lock, flags);
1902 return released;
1903 }
1904
1905 tx_index = mp->tx_used_desc_q;
1906 desc = &mp->p_tx_desc_area[tx_index];
1907 cmd_sts = desc->cmd_sts;
1908
1909 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
1910 spin_unlock_irqrestore(&mp->lock, flags);
1911 return released;
1912 }
1913
1914 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1915 mp->tx_desc_count--;
1916
1917 addr = desc->buf_ptr;
1918 count = desc->byte_cnt;
1919 skb = mp->tx_skb[tx_index];
1920 if (skb)
1921 mp->tx_skb[tx_index] = NULL;
1922
1923 if (cmd_sts & ETH_ERROR_SUMMARY) {
1924 printk("%s: Error in TX\n", dev->name);
1925 dev->stats.tx_errors++;
1926 }
1927
1928 spin_unlock_irqrestore(&mp->lock, flags);
1929
1930 if (cmd_sts & ETH_TX_FIRST_DESC)
1931 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1932 else
1933 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1934
1935 if (skb)
1936 dev_kfree_skb_irq(skb);
1937
1938 released = 1;
1939 }
1940
1941 return released;
1942}
1943
1944static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1945{
1946 struct mv643xx_private *mp = netdev_priv(dev);
1947
1948 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1949 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1950 netif_wake_queue(dev);
1951}
1952
1953static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1954{
1955 mv643xx_eth_free_tx_descs(dev, 1);
1956}
1957
1958static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1959{
1960 struct mv643xx_private *mp = netdev_priv(dev);
1961
1962 /* Stop Tx Queues */
1963 mv643xx_eth_port_disable_tx(mp);
1964
1965 /* Free outstanding skb's on TX ring */
1966 mv643xx_eth_free_all_tx_descs(dev);
1967
1968 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1969
1970 /* Free TX ring */
1971 if (mp->tx_sram_size)
1972 iounmap(mp->p_tx_desc_area);
1973 else
1974 dma_free_coherent(NULL, mp->tx_desc_area_size,
1975 mp->p_tx_desc_area, mp->tx_desc_dma);
1976}
1977
1978
1979/* netdev ops and related ***************************************************/
1980static void eth_port_reset(struct mv643xx_private *mp);
1981
1982/* Set the mv643xx port configuration register for the speed/duplex mode. */
1983static void mv643xx_eth_update_pscr(struct net_device *dev,
1984 struct ethtool_cmd *ecmd)
1985{
1986 struct mv643xx_private *mp = netdev_priv(dev);
1987 int port_num = mp->port_num;
1988 u32 o_pscr, n_pscr;
1989 unsigned int queues;
1990
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001991 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001992 n_pscr = o_pscr;
1993
1994 /* clear speed, duplex and rx buffer size fields */
1995 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1996 SET_GMII_SPEED_TO_1000 |
1997 SET_FULL_DUPLEX_MODE |
1998 MAX_RX_PACKET_MASK);
1999
2000 if (ecmd->duplex == DUPLEX_FULL)
2001 n_pscr |= SET_FULL_DUPLEX_MODE;
2002
2003 if (ecmd->speed == SPEED_1000)
2004 n_pscr |= SET_GMII_SPEED_TO_1000 |
2005 MAX_RX_PACKET_9700BYTE;
2006 else {
2007 if (ecmd->speed == SPEED_100)
2008 n_pscr |= SET_MII_SPEED_TO_100;
2009 n_pscr |= MAX_RX_PACKET_1522BYTE;
2010 }
2011
2012 if (n_pscr != o_pscr) {
2013 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002014 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002015 else {
2016 queues = mv643xx_eth_port_disable_tx(mp);
2017
2018 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002019 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
2020 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
2021 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002022 if (queues)
2023 mv643xx_eth_port_enable_tx(mp, queues);
2024 }
2025 }
2026}
2027
2028/*
2029 * mv643xx_eth_int_handler
2030 *
2031 * Main interrupt handler for the gigbit ethernet ports
2032 *
2033 * Input : irq - irq number (not used)
2034 * dev_id - a pointer to the required interface's data structure
2035 * regs - not used
2036 * Output : N/A
2037 */
2038
2039static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
2040{
2041 struct net_device *dev = (struct net_device *)dev_id;
2042 struct mv643xx_private *mp = netdev_priv(dev);
2043 u32 eth_int_cause, eth_int_cause_ext = 0;
2044 unsigned int port_num = mp->port_num;
2045
2046 /* Read interrupt cause registers */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002047 eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & ETH_INT_UNMASK_ALL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002048 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002049 eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
2050 & ETH_INT_UNMASK_ALL_EXT;
2051 wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002052 }
2053
2054 /* PHY status changed */
2055 if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
2056 struct ethtool_cmd cmd;
2057
2058 if (mii_link_ok(&mp->mii)) {
2059 mii_ethtool_gset(&mp->mii, &cmd);
2060 mv643xx_eth_update_pscr(dev, &cmd);
2061 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
2062 if (!netif_carrier_ok(dev)) {
2063 netif_carrier_on(dev);
2064 if (mp->tx_ring_size - mp->tx_desc_count >=
2065 MAX_DESCS_PER_SKB)
2066 netif_wake_queue(dev);
2067 }
2068 } else if (netif_carrier_ok(dev)) {
2069 netif_stop_queue(dev);
2070 netif_carrier_off(dev);
2071 }
2072 }
2073
2074#ifdef MV643XX_NAPI
2075 if (eth_int_cause & ETH_INT_CAUSE_RX) {
2076 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002077 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002078
2079 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002080 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002081
2082 netif_rx_schedule(dev, &mp->napi);
2083 }
2084#else
2085 if (eth_int_cause & ETH_INT_CAUSE_RX)
2086 mv643xx_eth_receive_queue(dev, INT_MAX);
2087#endif
2088 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
2089 mv643xx_eth_free_completed_tx_descs(dev);
2090
2091 /*
2092 * If no real interrupt occured, exit.
2093 * This can happen when using gigE interrupt coalescing mechanism.
2094 */
2095 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
2096 return IRQ_NONE;
2097
2098 return IRQ_HANDLED;
2099}
2100
2101/*
2102 * ethernet_phy_reset - Reset Ethernet port PHY.
2103 *
2104 * DESCRIPTION:
2105 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2106 *
2107 * INPUT:
2108 * struct mv643xx_private *mp Ethernet Port.
2109 *
2110 * OUTPUT:
2111 * The PHY is reset.
2112 *
2113 * RETURN:
2114 * None.
2115 *
2116 */
2117static void ethernet_phy_reset(struct mv643xx_private *mp)
2118{
2119 unsigned int phy_reg_data;
2120
2121 /* Reset the PHY */
2122 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2123 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2124 eth_port_write_smi_reg(mp, 0, phy_reg_data);
2125
2126 /* wait for PHY to come out of reset */
2127 do {
2128 udelay(1);
2129 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2130 } while (phy_reg_data & 0x8000);
2131}
2132
2133/*
2134 * eth_port_start - Start the Ethernet port activity.
2135 *
2136 * DESCRIPTION:
2137 * This routine prepares the Ethernet port for Rx and Tx activity:
2138 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2139 * has been initialized a descriptor's ring (using
2140 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2141 * 2. Initialize and enable the Ethernet configuration port by writing to
2142 * the port's configuration and command registers.
2143 * 3. Initialize and enable the SDMA by writing to the SDMA's
2144 * configuration and command registers. After completing these steps,
2145 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2146 *
2147 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2148 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2149 * and ether_init_rx_desc_ring for Rx queues).
2150 *
2151 * INPUT:
2152 * dev - a pointer to the required interface
2153 *
2154 * OUTPUT:
2155 * Ethernet port is ready to receive and transmit.
2156 *
2157 * RETURN:
2158 * None.
2159 */
2160static void eth_port_start(struct net_device *dev)
2161{
2162 struct mv643xx_private *mp = netdev_priv(dev);
2163 unsigned int port_num = mp->port_num;
2164 int tx_curr_desc, rx_curr_desc;
2165 u32 pscr;
2166 struct ethtool_cmd ethtool_cmd;
2167
2168 /* Assignment of Tx CTRP of given queue */
2169 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002170 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002171 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2172
2173 /* Assignment of Rx CRDP of given queue */
2174 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002175 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002176 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2177
2178 /* Add the assigned Ethernet address to the port's address table */
2179 eth_port_uc_addr_set(mp, dev->dev_addr);
2180
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002181 /*
2182 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2183 * frames to RX queue #0.
2184 */
2185 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002186
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002187 /*
2188 * Treat BPDUs as normal multicasts, and disable partition mode.
2189 */
2190 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002191
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002192 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002193
2194 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002195 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002196
2197 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2198 DISABLE_AUTO_NEG_SPEED_GMII |
2199 DISABLE_AUTO_NEG_FOR_DUPLX |
2200 DO_NOT_FORCE_LINK_FAIL |
2201 SERIAL_PORT_CONTROL_RESERVED;
2202
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002203 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002204
2205 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002206 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002207
2208 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002209 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002210
2211 /* Enable port Rx. */
2212 mv643xx_eth_port_enable_rx(mp, ETH_RX_QUEUES_ENABLED);
2213
2214 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002215 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002216
2217 /* save phy settings across reset */
2218 mv643xx_get_settings(dev, &ethtool_cmd);
2219 ethernet_phy_reset(mp);
2220 mv643xx_set_settings(dev, &ethtool_cmd);
2221}
2222
2223#ifdef MV643XX_COAL
2224
2225/*
2226 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
2227 *
2228 * DESCRIPTION:
2229 * This routine sets the RX coalescing interrupt mechanism parameter.
2230 * This parameter is a timeout counter, that counts in 64 t_clk
2231 * chunks ; that when timeout event occurs a maskable interrupt
2232 * occurs.
2233 * The parameter is calculated using the tClk of the MV-643xx chip
2234 * , and the required delay of the interrupt in usec.
2235 *
2236 * INPUT:
2237 * struct mv643xx_private *mp Ethernet port
2238 * unsigned int delay Delay in usec
2239 *
2240 * OUTPUT:
2241 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2242 *
2243 * RETURN:
2244 * The interrupt coalescing value set in the gigE port.
2245 *
2246 */
2247static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
2248 unsigned int delay)
2249{
2250 unsigned int port_num = mp->port_num;
2251 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2252
2253 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002254 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002255 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002256 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002257 & 0xffc000ff));
2258
2259 return coal;
2260}
2261#endif
2262
2263/*
2264 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
2265 *
2266 * DESCRIPTION:
2267 * This routine sets the TX coalescing interrupt mechanism parameter.
2268 * This parameter is a timeout counter, that counts in 64 t_clk
2269 * chunks ; that when timeout event occurs a maskable interrupt
2270 * occurs.
2271 * The parameter is calculated using the t_cLK frequency of the
2272 * MV-643xx chip and the required delay in the interrupt in uSec
2273 *
2274 * INPUT:
2275 * struct mv643xx_private *mp Ethernet port
2276 * unsigned int delay Delay in uSeconds
2277 *
2278 * OUTPUT:
2279 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2280 *
2281 * RETURN:
2282 * The interrupt coalescing value set in the gigE port.
2283 *
2284 */
2285static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
2286 unsigned int delay)
2287{
2288 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2289
2290 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002291 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002292
2293 return coal;
2294}
2295
2296/*
2297 * eth_port_init - Initialize the Ethernet port driver
2298 *
2299 * DESCRIPTION:
2300 * This function prepares the ethernet port to start its activity:
2301 * 1) Completes the ethernet port driver struct initialization toward port
2302 * start routine.
2303 * 2) Resets the device to a quiescent state in case of warm reboot.
2304 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2305 * 4) Clean MAC tables. The reset status of those tables is unknown.
2306 * 5) Set PHY address.
2307 * Note: Call this routine prior to eth_port_start routine and after
2308 * setting user values in the user fields of Ethernet port control
2309 * struct.
2310 *
2311 * INPUT:
2312 * struct mv643xx_private *mp Ethernet port control struct
2313 *
2314 * OUTPUT:
2315 * See description.
2316 *
2317 * RETURN:
2318 * None.
2319 */
2320static void eth_port_init(struct mv643xx_private *mp)
2321{
2322 mp->rx_resource_err = 0;
2323
2324 eth_port_reset(mp);
2325
2326 eth_port_init_mac_tables(mp);
2327}
2328
2329/*
2330 * mv643xx_eth_open
2331 *
2332 * This function is called when openning the network device. The function
2333 * should initialize all the hardware, initialize cyclic Rx/Tx
2334 * descriptors chain and buffers and allocate an IRQ to the network
2335 * device.
2336 *
2337 * Input : a pointer to the network device structure
2338 *
2339 * Output : zero of success , nonzero if fails.
2340 */
2341
2342static int mv643xx_eth_open(struct net_device *dev)
2343{
2344 struct mv643xx_private *mp = netdev_priv(dev);
2345 unsigned int port_num = mp->port_num;
2346 unsigned int size;
2347 int err;
2348
2349 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002350 wrl(mp, INT_CAUSE(port_num), 0);
2351 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002352 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002353 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002354
2355 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2356 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2357 if (err) {
2358 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2359 return -EAGAIN;
2360 }
2361
2362 eth_port_init(mp);
2363
2364 memset(&mp->timeout, 0, sizeof(struct timer_list));
2365 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2366 mp->timeout.data = (unsigned long)dev;
2367
2368 /* Allocate RX and TX skb rings */
2369 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2370 GFP_KERNEL);
2371 if (!mp->rx_skb) {
2372 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2373 err = -ENOMEM;
2374 goto out_free_irq;
2375 }
2376 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2377 GFP_KERNEL);
2378 if (!mp->tx_skb) {
2379 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2380 err = -ENOMEM;
2381 goto out_free_rx_skb;
2382 }
2383
2384 /* Allocate TX ring */
2385 mp->tx_desc_count = 0;
2386 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
2387 mp->tx_desc_area_size = size;
2388
2389 if (mp->tx_sram_size) {
2390 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2391 mp->tx_sram_size);
2392 mp->tx_desc_dma = mp->tx_sram_addr;
2393 } else
2394 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2395 &mp->tx_desc_dma,
2396 GFP_KERNEL);
2397
2398 if (!mp->p_tx_desc_area) {
2399 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2400 dev->name, size);
2401 err = -ENOMEM;
2402 goto out_free_tx_skb;
2403 }
2404 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2405 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2406
2407 ether_init_tx_desc_ring(mp);
2408
2409 /* Allocate RX ring */
2410 mp->rx_desc_count = 0;
2411 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
2412 mp->rx_desc_area_size = size;
2413
2414 if (mp->rx_sram_size) {
2415 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2416 mp->rx_sram_size);
2417 mp->rx_desc_dma = mp->rx_sram_addr;
2418 } else
2419 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2420 &mp->rx_desc_dma,
2421 GFP_KERNEL);
2422
2423 if (!mp->p_rx_desc_area) {
2424 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2425 dev->name, size);
2426 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2427 dev->name);
2428 if (mp->rx_sram_size)
2429 iounmap(mp->p_tx_desc_area);
2430 else
2431 dma_free_coherent(NULL, mp->tx_desc_area_size,
2432 mp->p_tx_desc_area, mp->tx_desc_dma);
2433 err = -ENOMEM;
2434 goto out_free_tx_skb;
2435 }
2436 memset((void *)mp->p_rx_desc_area, 0, size);
2437
2438 ether_init_rx_desc_ring(mp);
2439
2440 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2441
2442#ifdef MV643XX_NAPI
2443 napi_enable(&mp->napi);
2444#endif
2445
2446 eth_port_start(dev);
2447
2448 /* Interrupt Coalescing */
2449
2450#ifdef MV643XX_COAL
2451 mp->rx_int_coal =
2452 eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
2453#endif
2454
2455 mp->tx_int_coal =
2456 eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
2457
2458 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002459 wrl(mp, INT_MASK_EXT(port_num), ETH_INT_UNMASK_ALL_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002460
2461 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002462 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002463
2464 return 0;
2465
2466out_free_tx_skb:
2467 kfree(mp->tx_skb);
2468out_free_rx_skb:
2469 kfree(mp->rx_skb);
2470out_free_irq:
2471 free_irq(dev->irq, dev);
2472
2473 return err;
2474}
2475
2476/*
2477 * eth_port_reset - Reset Ethernet port
2478 *
2479 * DESCRIPTION:
2480 * This routine resets the chip by aborting any SDMA engine activity and
2481 * clearing the MIB counters. The Receiver and the Transmit unit are in
2482 * idle state after this command is performed and the port is disabled.
2483 *
2484 * INPUT:
2485 * struct mv643xx_private *mp Ethernet Port.
2486 *
2487 * OUTPUT:
2488 * Channel activity is halted.
2489 *
2490 * RETURN:
2491 * None.
2492 *
2493 */
2494static void eth_port_reset(struct mv643xx_private *mp)
2495{
2496 unsigned int port_num = mp->port_num;
2497 unsigned int reg_data;
2498
2499 mv643xx_eth_port_disable_tx(mp);
2500 mv643xx_eth_port_disable_rx(mp);
2501
2502 /* Clear all MIB counters */
2503 eth_clear_mib_counters(mp);
2504
2505 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002506 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002507 reg_data &= ~(SERIAL_PORT_ENABLE |
2508 DO_NOT_FORCE_LINK_FAIL |
2509 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002510 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002511}
2512
2513/*
2514 * mv643xx_eth_stop
2515 *
2516 * This function is used when closing the network device.
2517 * It updates the hardware,
2518 * release all memory that holds buffers and descriptors and release the IRQ.
2519 * Input : a pointer to the device structure
2520 * Output : zero if success , nonzero if fails
2521 */
2522
2523static int mv643xx_eth_stop(struct net_device *dev)
2524{
2525 struct mv643xx_private *mp = netdev_priv(dev);
2526 unsigned int port_num = mp->port_num;
2527
2528 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002529 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002530 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002531 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002532
2533#ifdef MV643XX_NAPI
2534 napi_disable(&mp->napi);
2535#endif
2536 netif_carrier_off(dev);
2537 netif_stop_queue(dev);
2538
2539 eth_port_reset(mp);
2540
2541 mv643xx_eth_free_tx_rings(dev);
2542 mv643xx_eth_free_rx_rings(dev);
2543
2544 free_irq(dev->irq, dev);
2545
2546 return 0;
2547}
2548
2549static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2550{
2551 struct mv643xx_private *mp = netdev_priv(dev);
2552
2553 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2554}
2555
2556/*
2557 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2558 *
2559 * Input : pointer to ethernet interface network device structure
2560 * new mtu size
2561 * Output : 0 upon success, -EINVAL upon failure
2562 */
2563static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2564{
2565 if ((new_mtu > 9500) || (new_mtu < 64))
2566 return -EINVAL;
2567
2568 dev->mtu = new_mtu;
2569 if (!netif_running(dev))
2570 return 0;
2571
2572 /*
2573 * Stop and then re-open the interface. This will allocate RX
2574 * skbs of the new MTU.
2575 * There is a possible danger that the open will not succeed,
2576 * due to memory being full, which might fail the open function.
2577 */
2578 mv643xx_eth_stop(dev);
2579 if (mv643xx_eth_open(dev)) {
2580 printk(KERN_ERR "%s: Fatal error on opening device\n",
2581 dev->name);
2582 }
2583
2584 return 0;
2585}
2586
2587/*
2588 * mv643xx_eth_tx_timeout_task
2589 *
2590 * Actual routine to reset the adapter when a timeout on Tx has occurred
2591 */
2592static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2593{
2594 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2595 tx_timeout_task);
2596 struct net_device *dev = mp->dev;
2597
2598 if (!netif_running(dev))
2599 return;
2600
2601 netif_stop_queue(dev);
2602
2603 eth_port_reset(mp);
2604 eth_port_start(dev);
2605
2606 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2607 netif_wake_queue(dev);
2608}
2609
2610/*
2611 * mv643xx_eth_tx_timeout
2612 *
2613 * Called upon a timeout on transmitting a packet
2614 *
2615 * Input : pointer to ethernet interface network device structure.
2616 * Output : N/A
2617 */
2618static void mv643xx_eth_tx_timeout(struct net_device *dev)
2619{
2620 struct mv643xx_private *mp = netdev_priv(dev);
2621
2622 printk(KERN_INFO "%s: TX timeout ", dev->name);
2623
2624 /* Do the reset outside of interrupt context */
2625 schedule_work(&mp->tx_timeout_task);
2626}
2627
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002628#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002629static void mv643xx_netpoll(struct net_device *netdev)
2630{
2631 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002632 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002633
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002634 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002635 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002636 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002637
Al Viro9da3b1a2006-10-08 15:00:44 +01002638 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002639
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002640 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002641}
2642#endif
2643
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002644/*
2645 * Wrappers for MII support library.
2646 */
2647static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2648{
2649 struct mv643xx_private *mp = netdev_priv(dev);
2650 int val;
2651
2652 eth_port_read_smi_reg(mp, location, &val);
2653 return val;
2654}
2655
2656static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2657{
2658 struct mv643xx_private *mp = netdev_priv(dev);
2659 eth_port_write_smi_reg(mp, location, val);
2660}
2661
2662
2663/* platform glue ************************************************************/
2664static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2665 struct mbus_dram_target_info *dram)
2666{
2667 void __iomem *base = msp->eth_base;
2668 u32 win_enable;
2669 u32 win_protect;
2670 int i;
2671
2672 for (i = 0; i < 6; i++) {
2673 writel(0, base + WINDOW_BASE(i));
2674 writel(0, base + WINDOW_SIZE(i));
2675 if (i < 4)
2676 writel(0, base + WINDOW_REMAP_HIGH(i));
2677 }
2678
2679 win_enable = 0x3f;
2680 win_protect = 0;
2681
2682 for (i = 0; i < dram->num_cs; i++) {
2683 struct mbus_dram_window *cs = dram->cs + i;
2684
2685 writel((cs->base & 0xffff0000) |
2686 (cs->mbus_attr << 8) |
2687 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2688 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2689
2690 win_enable &= ~(1 << i);
2691 win_protect |= 3 << (2 * i);
2692 }
2693
2694 writel(win_enable, base + WINDOW_BAR_ENABLE);
2695 msp->win_protect = win_protect;
2696}
2697
2698static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2699{
2700 static int mv643xx_version_printed = 0;
2701 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2702 struct mv643xx_shared_private *msp;
2703 struct resource *res;
2704 int ret;
2705
2706 if (!mv643xx_version_printed++)
2707 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2708
2709 ret = -EINVAL;
2710 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2711 if (res == NULL)
2712 goto out;
2713
2714 ret = -ENOMEM;
2715 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2716 if (msp == NULL)
2717 goto out;
2718 memset(msp, 0, sizeof(*msp));
2719
2720 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2721 if (msp->eth_base == NULL)
2722 goto out_free;
2723
2724 spin_lock_init(&msp->phy_lock);
2725 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2726
2727 platform_set_drvdata(pdev, msp);
2728
2729 /*
2730 * (Re-)program MBUS remapping windows if we are asked to.
2731 */
2732 if (pd != NULL && pd->dram != NULL)
2733 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2734
2735 return 0;
2736
2737out_free:
2738 kfree(msp);
2739out:
2740 return ret;
2741}
2742
2743static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2744{
2745 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2746
2747 iounmap(msp->eth_base);
2748 kfree(msp);
2749
2750 return 0;
2751}
2752
2753static struct platform_driver mv643xx_eth_shared_driver = {
2754 .probe = mv643xx_eth_shared_probe,
2755 .remove = mv643xx_eth_shared_remove,
2756 .driver = {
2757 .name = MV643XX_ETH_SHARED_NAME,
2758 .owner = THIS_MODULE,
2759 },
2760};
2761
2762/*
2763 * ethernet_phy_set - Set the ethernet port PHY address.
2764 *
2765 * DESCRIPTION:
2766 * This routine sets the given ethernet port PHY address.
2767 *
2768 * INPUT:
2769 * struct mv643xx_private *mp Ethernet Port.
2770 * int phy_addr PHY address.
2771 *
2772 * OUTPUT:
2773 * None.
2774 *
2775 * RETURN:
2776 * None.
2777 *
2778 */
2779static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
2780{
2781 u32 reg_data;
2782 int addr_shift = 5 * mp->port_num;
2783
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002784 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002785 reg_data &= ~(0x1f << addr_shift);
2786 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002787 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002788}
2789
2790/*
2791 * ethernet_phy_get - Get the ethernet port PHY address.
2792 *
2793 * DESCRIPTION:
2794 * This routine returns the given ethernet port PHY address.
2795 *
2796 * INPUT:
2797 * struct mv643xx_private *mp Ethernet Port.
2798 *
2799 * OUTPUT:
2800 * None.
2801 *
2802 * RETURN:
2803 * PHY address.
2804 *
2805 */
2806static int ethernet_phy_get(struct mv643xx_private *mp)
2807{
2808 unsigned int reg_data;
2809
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002810 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002811
2812 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2813}
2814
2815/*
2816 * ethernet_phy_detect - Detect whether a phy is present
2817 *
2818 * DESCRIPTION:
2819 * This function tests whether there is a PHY present on
2820 * the specified port.
2821 *
2822 * INPUT:
2823 * struct mv643xx_private *mp Ethernet Port.
2824 *
2825 * OUTPUT:
2826 * None
2827 *
2828 * RETURN:
2829 * 0 on success
2830 * -ENODEV on failure
2831 *
2832 */
2833static int ethernet_phy_detect(struct mv643xx_private *mp)
2834{
2835 unsigned int phy_reg_data0;
2836 int auto_neg;
2837
2838 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2839 auto_neg = phy_reg_data0 & 0x1000;
2840 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2841 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2842
2843 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2844 if ((phy_reg_data0 & 0x1000) == auto_neg)
2845 return -ENODEV; /* change didn't take */
2846
2847 phy_reg_data0 ^= 0x1000;
2848 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2849 return 0;
2850}
2851
James Chapmand0412d92006-01-27 01:15:30 -07002852static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2853 int speed, int duplex,
2854 struct ethtool_cmd *cmd)
2855{
2856 struct mv643xx_private *mp = netdev_priv(dev);
2857
2858 memset(cmd, 0, sizeof(*cmd));
2859
2860 cmd->port = PORT_MII;
2861 cmd->transceiver = XCVR_INTERNAL;
2862 cmd->phy_address = phy_address;
2863
2864 if (speed == 0) {
2865 cmd->autoneg = AUTONEG_ENABLE;
2866 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2867 cmd->speed = SPEED_100;
2868 cmd->advertising = ADVERTISED_10baseT_Half |
2869 ADVERTISED_10baseT_Full |
2870 ADVERTISED_100baseT_Half |
2871 ADVERTISED_100baseT_Full;
2872 if (mp->mii.supports_gmii)
2873 cmd->advertising |= ADVERTISED_1000baseT_Full;
2874 } else {
2875 cmd->autoneg = AUTONEG_DISABLE;
2876 cmd->speed = speed;
2877 cmd->duplex = duplex;
2878 }
2879}
2880
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881/*/
2882 * mv643xx_eth_probe
2883 *
2884 * First function called after registering the network device.
2885 * It's purpose is to initialize the device as an ethernet device,
2886 * fill the ethernet device structure with pointers * to functions,
2887 * and set the MAC address of the interface
2888 *
2889 * Input : struct device *
2890 * Output : -ENOMEM if failed , 0 if success
2891 */
Russell King3ae5eae2005-11-09 22:32:44 +00002892static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002895 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 struct mv643xx_private *mp;
2897 struct net_device *dev;
2898 u8 *p;
2899 struct resource *res;
2900 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002901 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002902 int duplex = DUPLEX_HALF;
2903 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002904 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002906 pd = pdev->dev.platform_data;
2907 if (pd == NULL) {
2908 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2909 return -ENODEV;
2910 }
2911
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002912 if (pd->shared == NULL) {
2913 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2914 return -ENODEV;
2915 }
2916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2918 if (!dev)
2919 return -ENOMEM;
2920
Russell King3ae5eae2005-11-09 22:32:44 +00002921 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
2923 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002924 mp->dev = dev;
2925#ifdef MV643XX_NAPI
2926 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2930 BUG_ON(!res);
2931 dev->irq = res->start;
2932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 dev->open = mv643xx_eth_open;
2934 dev->stop = mv643xx_eth_stop;
2935 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 dev->set_mac_address = mv643xx_eth_set_mac_address;
2937 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2938
2939 /* No need to Tx Timeout */
2940 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002942#ifdef CONFIG_NET_POLL_CONTROLLER
2943 dev->poll_controller = mv643xx_netpoll;
2944#endif
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 dev->base_addr = 0;
2948 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002949 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
2951
2952#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2953#ifdef MAX_SKB_FRAGS
2954 /*
2955 * Zero copy can only work if we use Discovery II memory. Else, we will
2956 * have to map the buffers to ISA memory which is only 16 MB
2957 */
Wolfram Joost63890572006-01-16 16:57:41 -07002958 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959#endif
2960#endif
2961
2962 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002963 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 spin_lock_init(&mp->lock);
2966
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002967 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002968 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002969
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002970 if (mp->shared->win_protect)
2971 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2972
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002973 mp->shared_smi = mp->shared;
2974 if (pd->shared_smi != NULL)
2975 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002978 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002979 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
2980 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002982 if (is_valid_ether_addr(pd->mac_addr))
2983 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002985 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002986 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002988 if (pd->rx_queue_size)
2989 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002991 if (pd->tx_queue_size)
2992 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002994 if (pd->tx_sram_size) {
2995 mp->tx_sram_size = pd->tx_sram_size;
2996 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
2998
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002999 if (pd->rx_sram_size) {
3000 mp->rx_sram_size = pd->rx_sram_size;
3001 mp->rx_sram_addr = pd->rx_sram_addr;
3002 }
3003
3004 duplex = pd->duplex;
3005 speed = pd->speed;
3006
James Chapmanc28a4f82006-01-27 01:13:15 -07003007 /* Hook up MII support for ethtool */
3008 mp->mii.dev = dev;
3009 mp->mii.mdio_read = mv643xx_mdio_read;
3010 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003011 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07003012 mp->mii.phy_id_mask = 0x3f;
3013 mp->mii.reg_num_mask = 0x1f;
3014
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003015 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07003017 pr_debug("%s: No PHY detected at addr %d\n",
3018 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07003019 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003022 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07003023 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07003024 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
3025 mv643xx_eth_update_pscr(dev, &cmd);
3026 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07003027
Olaf Heringb0b8dab2006-04-27 18:23:49 -07003028 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 err = register_netdev(dev);
3030 if (err)
3031 goto out;
3032
3033 p = dev->dev_addr;
3034 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07003035 "%s: port %d with MAC address %s\n",
3036 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 if (dev->features & NETIF_F_SG)
3039 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
3040
3041 if (dev->features & NETIF_F_IP_CSUM)
3042 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
3043 dev->name);
3044
3045#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
3046 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
3047#endif
3048
3049#ifdef MV643XX_COAL
3050 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
3051 dev->name);
3052#endif
3053
3054#ifdef MV643XX_NAPI
3055 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
3056#endif
3057
Nicolas DETb1529872005-10-28 17:46:30 -07003058 if (mp->tx_sram_size > 0)
3059 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return 0;
3062
3063out:
3064 free_netdev(dev);
3065
3066 return err;
3067}
3068
Russell King3ae5eae2005-11-09 22:32:44 +00003069static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070{
Russell King3ae5eae2005-11-09 22:32:44 +00003071 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
3073 unregister_netdev(dev);
3074 flush_scheduled_work();
3075
3076 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00003077 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 return 0;
3079}
3080
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003081static void mv643xx_eth_shutdown(struct platform_device *pdev)
3082{
3083 struct net_device *dev = platform_get_drvdata(pdev);
3084 struct mv643xx_private *mp = netdev_priv(dev);
3085 unsigned int port_num = mp->port_num;
3086
3087 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02003088 wrl(mp, INT_MASK(port_num), 0);
3089 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003090
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003091 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003092}
3093
Russell King3ae5eae2005-11-09 22:32:44 +00003094static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .probe = mv643xx_eth_probe,
3096 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003097 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00003098 .driver = {
3099 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07003100 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003101 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102};
3103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104/*
3105 * mv643xx_init_module
3106 *
3107 * Registers the network drivers into the Linux kernel
3108 *
3109 * Input : N/A
3110 *
3111 * Output : N/A
3112 */
3113static int __init mv643xx_init_module(void)
3114{
3115 int rc;
3116
Russell King3ae5eae2005-11-09 22:32:44 +00003117 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003119 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003121 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123 return rc;
3124}
3125
3126/*
3127 * mv643xx_cleanup_module
3128 *
3129 * Registers the network drivers into the Linux kernel
3130 *
3131 * Input : N/A
3132 *
3133 * Output : N/A
3134 */
3135static void __exit mv643xx_cleanup_module(void)
3136{
Russell King3ae5eae2005-11-09 22:32:44 +00003137 platform_driver_unregister(&mv643xx_eth_driver);
3138 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139}
3140
3141module_init(mv643xx_init_module);
3142module_exit(mv643xx_cleanup_module);
3143
3144MODULE_LICENSE("GPL");
3145MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
3146 " and Dale Farnsworth");
3147MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07003148MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3149MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);