blob: 3d9c412e9ea1837add7520454bf737286f468edb [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 Buytenhekfbd6a752007-10-19 16:03:46 +0200121/* These macros describe Ethernet Port Sdma configuration reg (SDCR) bits */
122#define RIFB (1 << 0)
123#define RX_BURST_SIZE_1_64BIT (0 << 1)
124#define RX_BURST_SIZE_2_64BIT (1 << 1)
125#define RX_BURST_SIZE_4_64BIT (2 << 1)
126#define RX_BURST_SIZE_8_64BIT (3 << 1)
127#define RX_BURST_SIZE_16_64BIT (4 << 1)
128#define BLM_RX_NO_SWAP (1 << 4)
129#define BLM_RX_BYTE_SWAP (0 << 4)
130#define BLM_TX_NO_SWAP (1 << 5)
131#define BLM_TX_BYTE_SWAP (0 << 5)
132#define DESCRIPTORS_BYTE_SWAP (1 << 6)
133#define DESCRIPTORS_NO_SWAP (0 << 6)
134#define IPG_INT_RX(value) (((value) & 0x3fff) << 8)
135#define TX_BURST_SIZE_1_64BIT (0 << 22)
136#define TX_BURST_SIZE_2_64BIT (1 << 22)
137#define TX_BURST_SIZE_4_64BIT (2 << 22)
138#define TX_BURST_SIZE_8_64BIT (3 << 22)
139#define TX_BURST_SIZE_16_64BIT (4 << 22)
140
141#if defined(__BIG_ENDIAN)
142#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
143 RX_BURST_SIZE_4_64BIT | \
144 IPG_INT_RX(0) | \
145 TX_BURST_SIZE_4_64BIT
146#elif defined(__LITTLE_ENDIAN)
147#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
148 RX_BURST_SIZE_4_64BIT | \
149 BLM_RX_NO_SWAP | \
150 BLM_TX_NO_SWAP | \
151 IPG_INT_RX(0) | \
152 TX_BURST_SIZE_4_64BIT
153#else
154#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
155#endif
156
157/* These macros describe Ethernet Port serial control reg (PSCR) bits */
158#define SERIAL_PORT_DISABLE (0 << 0)
159#define SERIAL_PORT_ENABLE (1 << 0)
160#define DO_NOT_FORCE_LINK_PASS (0 << 1)
161#define FORCE_LINK_PASS (1 << 1)
162#define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
163#define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
164#define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
165#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
166#define ADV_NO_FLOW_CTRL (0 << 4)
167#define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
168#define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
169#define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
170#define FORCE_BP_MODE_NO_JAM (0 << 7)
171#define FORCE_BP_MODE_JAM_TX (1 << 7)
172#define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
173#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
174#define FORCE_LINK_FAIL (0 << 10)
175#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
176#define RETRANSMIT_16_ATTEMPTS (0 << 11)
177#define RETRANSMIT_FOREVER (1 << 11)
178#define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
179#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
180#define DTE_ADV_0 (0 << 14)
181#define DTE_ADV_1 (1 << 14)
182#define DISABLE_AUTO_NEG_BYPASS (0 << 15)
183#define ENABLE_AUTO_NEG_BYPASS (1 << 15)
184#define AUTO_NEG_NO_CHANGE (0 << 16)
185#define RESTART_AUTO_NEG (1 << 16)
186#define MAX_RX_PACKET_1518BYTE (0 << 17)
187#define MAX_RX_PACKET_1522BYTE (1 << 17)
188#define MAX_RX_PACKET_1552BYTE (2 << 17)
189#define MAX_RX_PACKET_9022BYTE (3 << 17)
190#define MAX_RX_PACKET_9192BYTE (4 << 17)
191#define MAX_RX_PACKET_9700BYTE (5 << 17)
192#define MAX_RX_PACKET_MASK (7 << 17)
193#define CLR_EXT_LOOPBACK (0 << 20)
194#define SET_EXT_LOOPBACK (1 << 20)
195#define SET_HALF_DUPLEX_MODE (0 << 21)
196#define SET_FULL_DUPLEX_MODE (1 << 21)
197#define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
198#define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
199#define SET_GMII_SPEED_TO_10_100 (0 << 23)
200#define SET_GMII_SPEED_TO_1000 (1 << 23)
201#define SET_MII_SPEED_TO_10 (0 << 24)
202#define SET_MII_SPEED_TO_100 (1 << 24)
203
204#define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
205 DO_NOT_FORCE_LINK_PASS | \
206 ENABLE_AUTO_NEG_FOR_DUPLX | \
207 DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
208 ADV_SYMMETRIC_FLOW_CTRL | \
209 FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
210 FORCE_BP_MODE_NO_JAM | \
211 (1 << 9) /* reserved */ | \
212 DO_NOT_FORCE_LINK_FAIL | \
213 RETRANSMIT_16_ATTEMPTS | \
214 ENABLE_AUTO_NEG_SPEED_GMII | \
215 DTE_ADV_0 | \
216 DISABLE_AUTO_NEG_BYPASS | \
217 AUTO_NEG_NO_CHANGE | \
218 MAX_RX_PACKET_9700BYTE | \
219 CLR_EXT_LOOPBACK | \
220 SET_FULL_DUPLEX_MODE | \
221 ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
222
223/* These macros describe Ethernet Serial Status reg (PSR) bits */
224#define PORT_STATUS_MODE_10_BIT (1 << 0)
225#define PORT_STATUS_LINK_UP (1 << 1)
226#define PORT_STATUS_FULL_DUPLEX (1 << 2)
227#define PORT_STATUS_FLOW_CONTROL (1 << 3)
228#define PORT_STATUS_GMII_1000 (1 << 4)
229#define PORT_STATUS_MII_100 (1 << 5)
230/* PSR bit 6 is undocumented */
231#define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
232#define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
233#define PORT_STATUS_PARTITION (1 << 9)
234#define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
235/* PSR bits 11-31 are reserved */
236
237#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
238#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
239
240#define DESC_SIZE 64
241
242#define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
243#define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
244
245#define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
246#define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
247#define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
248#define ETH_INT_CAUSE_EXT 0x00000002
249#define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
250
251#define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
252#define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
253#define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
254#define ETH_INT_CAUSE_PHY 0x00010000
255#define ETH_INT_CAUSE_STATE 0x00100000
256#define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
257 ETH_INT_CAUSE_STATE)
258
259#define ETH_INT_MASK_ALL 0x00000000
260#define ETH_INT_MASK_ALL_EXT 0x00000000
261
262#define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
263#define PHY_WAIT_MICRO_SECONDS 10
264
265/* Buffer offset from buffer pointer */
266#define RX_BUF_OFFSET 0x2
267
268/* Gigabit Ethernet Unit Global Registers */
269
270/* MIB Counters register definitions */
271#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
272#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
273#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
274#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
275#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
276#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
277#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
278#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
279#define ETH_MIB_FRAMES_64_OCTETS 0x20
280#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
281#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
282#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
283#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
284#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
285#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
286#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
287#define ETH_MIB_GOOD_FRAMES_SENT 0x40
288#define ETH_MIB_EXCESSIVE_COLLISION 0x44
289#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
290#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
291#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
292#define ETH_MIB_FC_SENT 0x54
293#define ETH_MIB_GOOD_FC_RECEIVED 0x58
294#define ETH_MIB_BAD_FC_RECEIVED 0x5c
295#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
296#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
297#define ETH_MIB_OVERSIZE_RECEIVED 0x68
298#define ETH_MIB_JABBER_RECEIVED 0x6c
299#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
300#define ETH_MIB_BAD_CRC_EVENT 0x74
301#define ETH_MIB_COLLISION 0x78
302#define ETH_MIB_LATE_COLLISION 0x7c
303
304/* Port serial status reg (PSR) */
305#define ETH_INTERFACE_PCM 0x00000001
306#define ETH_LINK_IS_UP 0x00000002
307#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
308#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
309#define ETH_GMII_SPEED_1000 0x00000010
310#define ETH_MII_SPEED_100 0x00000020
311#define ETH_TX_IN_PROGRESS 0x00000080
312#define ETH_BYPASS_ACTIVE 0x00000100
313#define ETH_PORT_AT_PARTITION_STATE 0x00000200
314#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
315
316/* SMI reg */
317#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
318#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
319#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
320#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
321
322/* Interrupt Cause Register Bit Definitions */
323
324/* SDMA command status fields macros */
325
326/* Tx & Rx descriptors status */
327#define ETH_ERROR_SUMMARY 0x00000001
328
329/* Tx & Rx descriptors command */
330#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
331
332/* Tx descriptors status */
333#define ETH_LC_ERROR 0
334#define ETH_UR_ERROR 0x00000002
335#define ETH_RL_ERROR 0x00000004
336#define ETH_LLC_SNAP_FORMAT 0x00000200
337
338/* Rx descriptors status */
339#define ETH_OVERRUN_ERROR 0x00000002
340#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
341#define ETH_RESOURCE_ERROR 0x00000006
342#define ETH_VLAN_TAGGED 0x00080000
343#define ETH_BPDU_FRAME 0x00100000
344#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
345#define ETH_OTHER_FRAME_TYPE 0x00400000
346#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
347#define ETH_FRAME_TYPE_IP_V_4 0x01000000
348#define ETH_FRAME_HEADER_OK 0x02000000
349#define ETH_RX_LAST_DESC 0x04000000
350#define ETH_RX_FIRST_DESC 0x08000000
351#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
352#define ETH_RX_ENABLE_INTERRUPT 0x20000000
353#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
354
355/* Rx descriptors byte count */
356#define ETH_FRAME_FRAGMENTED 0x00000004
357
358/* Tx descriptors command */
359#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
360#define ETH_FRAME_SET_TO_VLAN 0x00008000
361#define ETH_UDP_FRAME 0x00010000
362#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
363#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
364#define ETH_ZERO_PADDING 0x00080000
365#define ETH_TX_LAST_DESC 0x00100000
366#define ETH_TX_FIRST_DESC 0x00200000
367#define ETH_GEN_CRC 0x00400000
368#define ETH_TX_ENABLE_INTERRUPT 0x00800000
369#define ETH_AUTO_MODE 0x40000000
370
371#define ETH_TX_IHL_SHIFT 11
372
373/* typedefs */
374
375typedef enum _eth_func_ret_status {
376 ETH_OK, /* Returned as expected. */
377 ETH_ERROR, /* Fundamental error. */
378 ETH_RETRY, /* Could not process request. Try later.*/
379 ETH_END_OF_JOB, /* Ring has nothing to process. */
380 ETH_QUEUE_FULL, /* Ring resource error. */
381 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
382} ETH_FUNC_RET_STATUS;
383
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200384/* These are for big-endian machines. Little endian needs different
385 * definitions.
386 */
387#if defined(__BIG_ENDIAN)
388struct eth_rx_desc {
389 u16 byte_cnt; /* Descriptor buffer byte count */
390 u16 buf_size; /* Buffer size */
391 u32 cmd_sts; /* Descriptor command status */
392 u32 next_desc_ptr; /* Next descriptor pointer */
393 u32 buf_ptr; /* Descriptor buffer pointer */
394};
395
396struct eth_tx_desc {
397 u16 byte_cnt; /* buffer byte count */
398 u16 l4i_chk; /* CPU provided TCP checksum */
399 u32 cmd_sts; /* Command/status field */
400 u32 next_desc_ptr; /* Pointer to next descriptor */
401 u32 buf_ptr; /* pointer to buffer for this descriptor*/
402};
403#elif defined(__LITTLE_ENDIAN)
404struct eth_rx_desc {
405 u32 cmd_sts; /* Descriptor command status */
406 u16 buf_size; /* Buffer size */
407 u16 byte_cnt; /* Descriptor buffer byte count */
408 u32 buf_ptr; /* Descriptor buffer pointer */
409 u32 next_desc_ptr; /* Next descriptor pointer */
410};
411
412struct eth_tx_desc {
413 u32 cmd_sts; /* Command/status field */
414 u16 l4i_chk; /* CPU provided TCP checksum */
415 u16 byte_cnt; /* buffer byte count */
416 u32 buf_ptr; /* pointer to buffer for this descriptor*/
417 u32 next_desc_ptr; /* Pointer to next descriptor */
418};
419#else
420#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
421#endif
422
423/* Unified struct for Rx and Tx operations. The user is not required to */
424/* be familier with neither Tx nor Rx descriptors. */
425struct pkt_info {
426 unsigned short byte_cnt; /* Descriptor buffer byte count */
427 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
428 unsigned int cmd_sts; /* Descriptor command status */
429 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
430 struct sk_buff *return_info; /* User resource return information */
431};
432
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200433
434/* global *******************************************************************/
435struct mv643xx_shared_private {
436 void __iomem *eth_base;
437
438 /* used to protect SMI_REG, which is shared across ports */
439 spinlock_t phy_lock;
440
441 u32 win_protect;
442
443 unsigned int t_clk;
444};
445
446
447/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200448struct mv643xx_mib_counters {
449 u64 good_octets_received;
450 u32 bad_octets_received;
451 u32 internal_mac_transmit_err;
452 u32 good_frames_received;
453 u32 bad_frames_received;
454 u32 broadcast_frames_received;
455 u32 multicast_frames_received;
456 u32 frames_64_octets;
457 u32 frames_65_to_127_octets;
458 u32 frames_128_to_255_octets;
459 u32 frames_256_to_511_octets;
460 u32 frames_512_to_1023_octets;
461 u32 frames_1024_to_max_octets;
462 u64 good_octets_sent;
463 u32 good_frames_sent;
464 u32 excessive_collision;
465 u32 multicast_frames_sent;
466 u32 broadcast_frames_sent;
467 u32 unrec_mac_control_received;
468 u32 fc_sent;
469 u32 good_fc_received;
470 u32 bad_fc_received;
471 u32 undersize_received;
472 u32 fragments_received;
473 u32 oversize_received;
474 u32 jabber_received;
475 u32 mac_receive_error;
476 u32 bad_crc_event;
477 u32 collision;
478 u32 late_collision;
479};
480
481struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200482 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200483 int port_num; /* User Ethernet port number */
484
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200485 struct mv643xx_shared_private *shared_smi;
486
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200487 u32 rx_sram_addr; /* Base address of rx sram area */
488 u32 rx_sram_size; /* Size of rx sram area */
489 u32 tx_sram_addr; /* Base address of tx sram area */
490 u32 tx_sram_size; /* Size of tx sram area */
491
492 int rx_resource_err; /* Rx ring resource error flag */
493
494 /* Tx/Rx rings managment indexes fields. For driver use */
495
496 /* Next available and first returning Rx resource */
497 int rx_curr_desc_q, rx_used_desc_q;
498
499 /* Next available and first returning Tx resource */
500 int tx_curr_desc_q, tx_used_desc_q;
501
502#ifdef MV643XX_TX_FAST_REFILL
503 u32 tx_clean_threshold;
504#endif
505
506 struct eth_rx_desc *p_rx_desc_area;
507 dma_addr_t rx_desc_dma;
508 int rx_desc_area_size;
509 struct sk_buff **rx_skb;
510
511 struct eth_tx_desc *p_tx_desc_area;
512 dma_addr_t tx_desc_dma;
513 int tx_desc_area_size;
514 struct sk_buff **tx_skb;
515
516 struct work_struct tx_timeout_task;
517
518 struct net_device *dev;
519 struct napi_struct napi;
520 struct net_device_stats stats;
521 struct mv643xx_mib_counters mib_counters;
522 spinlock_t lock;
523 /* Size of Tx Ring per queue */
524 int tx_ring_size;
525 /* Number of tx descriptors in use */
526 int tx_desc_count;
527 /* Size of Rx Ring per queue */
528 int rx_ring_size;
529 /* Number of rx descriptors in use */
530 int rx_desc_count;
531
532 /*
533 * Used in case RX Ring is empty, which can be caused when
534 * system does not have resources (skb's)
535 */
536 struct timer_list timeout;
537
538 u32 rx_int_coal;
539 u32 tx_int_coal;
540 struct mii_if_info mii;
541};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200543
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200544/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700545static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200547 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700550static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200552 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200555
556/* rxq/txq helper functions *************************************************/
557static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
558 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200560 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200561}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200563static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
564{
565 unsigned int port_num = mp->port_num;
566 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700567
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200568 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200569 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200570 if (queues) {
571 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200572 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200573
574 /* Wait for all Rx activity to terminate. */
575 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200576 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200577 udelay(PHY_WAIT_MICRO_SECONDS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200580 return queues;
581}
582
583static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
584 unsigned int queues)
585{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200586 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200587}
588
589static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
590{
591 unsigned int port_num = mp->port_num;
592 u32 queues;
593
594 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200595 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200596 if (queues) {
597 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200598 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200599
600 /* Wait for all Tx activity to terminate. */
601 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200602 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200603 udelay(PHY_WAIT_MICRO_SECONDS);
604
605 /* Wait for Tx FIFO to empty */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200606 while (rdl(mp, PORT_STATUS(port_num)) & ETH_PORT_TX_FIFO_EMPTY)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200607 udelay(PHY_WAIT_MICRO_SECONDS);
608 }
609
610 return queues;
611}
612
613
614/* rx ***********************************************************************/
615static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
616
617/*
618 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
619 *
620 * DESCRIPTION:
621 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
622 * next 'used' descriptor and attached the returned buffer to it.
623 * In case the Rx ring was in "resource error" condition, where there are
624 * no available Rx resources, the function resets the resource error flag.
625 *
626 * INPUT:
627 * struct mv643xx_private *mp Ethernet Port Control srtuct.
628 * struct pkt_info *p_pkt_info Information on returned buffer.
629 *
630 * OUTPUT:
631 * New available Rx resource in Rx descriptor ring.
632 *
633 * RETURN:
634 * ETH_ERROR in case the routine can not access Rx desc ring.
635 * ETH_OK otherwise.
636 */
637static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
638 struct pkt_info *p_pkt_info)
639{
640 int used_rx_desc; /* Where to return Rx resource */
641 volatile struct eth_rx_desc *p_used_rx_desc;
642 unsigned long flags;
643
644 spin_lock_irqsave(&mp->lock, flags);
645
646 /* Get 'used' Rx descriptor */
647 used_rx_desc = mp->rx_used_desc_q;
648 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
649
650 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
651 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
652 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
653
654 /* Flush the write pipe */
655
656 /* Return the descriptor to DMA ownership */
657 wmb();
658 p_used_rx_desc->cmd_sts =
659 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
660 wmb();
661
662 /* Move the used descriptor pointer to the next descriptor */
663 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
664
665 /* Any Rx return cancels the Rx resource error status */
666 mp->rx_resource_err = 0;
667
668 spin_unlock_irqrestore(&mp->lock, flags);
669
670 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700674 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 *
676 * Fills / refills RX queue on a certain gigabit ethernet port
677 *
678 * Input : pointer to ethernet interface network device structure
679 * Output : N/A
680 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700681static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 struct mv643xx_private *mp = netdev_priv(dev);
684 struct pkt_info pkt_info;
685 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700686 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700688 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000689 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 if (!skb)
691 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700692 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000693 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700694 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000695 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700697 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
698 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
699 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 pkt_info.return_info = skb;
701 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
702 printk(KERN_ERR
703 "%s: Error allocating RX Ring\n", dev->name);
704 break;
705 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700706 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /*
709 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700710 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700712 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700714 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700720 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 *
722 * Timer routine to wake up RX queue filling task. This function is
723 * used only in case the RX queue is empty, and all alloc_skb has
724 * failed (due to out of memory event).
725 *
726 * Input : pointer to ethernet interface network device structure
727 * Output : N/A
728 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700729static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700731 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
734/*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200735 * eth_port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200737 * DESCRIPTION:
738 * This routine returns the received data to the caller. There is no
739 * data copying during routine operation. All information is returned
740 * using pointer to packet information struct passed from the caller.
741 * If the routine exhausts Rx ring resources then the resource error flag
742 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200744 * INPUT:
745 * struct mv643xx_private *mp Ethernet Port Control srtuct.
746 * struct pkt_info *p_pkt_info User packet buffer.
747 *
748 * OUTPUT:
749 * Rx ring current and used indexes are updated.
750 *
751 * RETURN:
752 * ETH_ERROR in case the routine can not access Rx desc ring.
753 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
754 * ETH_END_OF_JOB if there is no received data.
755 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200757static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
758 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200760 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
761 volatile struct eth_rx_desc *p_rx_desc;
762 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700763 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200765 /* Do not process Rx ring in case of Rx ring resource error */
766 if (mp->rx_resource_err)
767 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700768
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200769 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700770
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200771 /* Get the Rx Desc ring 'curr and 'used' indexes */
772 rx_curr_desc = mp->rx_curr_desc_q;
773 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200775 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700776
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200777 /* The following parameters are used to save readings from memory */
778 command_status = p_rx_desc->cmd_sts;
779 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700780
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200781 /* Nothing to receive... */
782 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700783 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200784 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
786
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200787 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
788 p_pkt_info->cmd_sts = command_status;
789 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
790 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
791 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200793 /*
794 * Clean the return info field to indicate that the
795 * packet has been moved to the upper layers
796 */
797 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700798
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200799 /* Update current index in data structure */
800 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
801 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700802
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200803 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
804 if (rx_next_curr_desc == rx_used_desc)
805 mp->rx_resource_err = 1;
806
807 spin_unlock_irqrestore(&mp->lock, flags);
808
809 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/*
813 * mv643xx_eth_receive
814 *
815 * This function is forward packets that are received from the port's
816 * queues toward kernel core or FastRoute them to another interface.
817 *
818 * Input : dev - a pointer to the required interface
819 * max - maximum number to receive (0 means unlimted)
820 *
821 * Output : number of served packets
822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
825 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700826 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 unsigned int received_packets = 0;
828 struct sk_buff *skb;
829 struct pkt_info pkt_info;
830
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700831 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400832 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700833 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700834 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700836
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700837 /*
838 * Update statistics.
839 * Note byte count includes 4 byte CRC count
840 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 stats->rx_packets++;
842 stats->rx_bytes += pkt_info.byte_cnt;
843 skb = pkt_info.return_info;
844 /*
845 * In case received a packet without first / last bits on OR
846 * the error summary bit is on, the packets needs to be dropeed.
847 */
848 if (((pkt_info.cmd_sts
849 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
850 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
851 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
852 stats->rx_dropped++;
853 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
854 ETH_RX_LAST_DESC)) !=
855 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
856 if (net_ratelimit())
857 printk(KERN_ERR
858 "%s: Received packet spread "
859 "on multiple descriptors\n",
860 dev->name);
861 }
862 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
863 stats->rx_errors++;
864
865 dev_kfree_skb_irq(skb);
866 } else {
867 /*
868 * The -4 is for the CRC in the trailer of the
869 * received packet
870 */
871 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
874 skb->ip_summed = CHECKSUM_UNNECESSARY;
875 skb->csum = htons(
876 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
877 }
878 skb->protocol = eth_type_trans(skb, dev);
879#ifdef MV643XX_NAPI
880 netif_receive_skb(skb);
881#else
882 netif_rx(skb);
883#endif
884 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700885 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700887 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 return received_packets;
890}
891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893/*
894 * mv643xx_poll
895 *
896 * This function is used in case of NAPI
897 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700898static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700900 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
901 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700903 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905#ifdef MV643XX_TX_FAST_REFILL
906 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700907 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
910#endif
911
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700912 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200913 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700914 != (u32) mp->rx_used_desc_q)
915 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700917 if (work_done < budget) {
918 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200919 wrl(mp, INT_CAUSE(port_num), 0);
920 wrl(mp, INT_CAUSE_EXT(port_num), 0);
921 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700924 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926#endif
927
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200928
929/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700930/**
931 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
932 *
933 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700934 * This helper function detects that case.
935 */
936
937static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
938{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700939 unsigned int frag;
940 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700941
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700942 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
943 fragp = &skb_shinfo(skb)->frags[frag];
944 if (fragp->size <= 8 && fragp->page_offset & 0x7)
945 return 1;
946 }
947 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700948}
949
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700950/**
951 * eth_alloc_tx_desc_index - return the index of the next available tx desc
952 */
953static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
954{
955 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700956
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700957 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700958
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700959 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700960 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
961
962 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
963
964 return tx_desc_curr;
965}
966
967/**
968 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700970 * Ensure the data for each fragment to be transmitted is mapped properly,
971 * then fill in descriptors in the tx hw queue.
972 */
973static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
974 struct sk_buff *skb)
975{
976 int frag;
977 int tx_index;
978 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700979
980 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
981 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
982
983 tx_index = eth_alloc_tx_desc_index(mp);
984 desc = &mp->p_tx_desc_area[tx_index];
985
986 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
987 /* Last Frag enables interrupt and frees the skb */
988 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
989 desc->cmd_sts |= ETH_ZERO_PADDING |
990 ETH_TX_LAST_DESC |
991 ETH_TX_ENABLE_INTERRUPT;
992 mp->tx_skb[tx_index] = skb;
993 } else
Al Viro05980772006-05-30 23:59:09 -0400994 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700995
996 desc = &mp->p_tx_desc_area[tx_index];
997 desc->l4i_chk = 0;
998 desc->byte_cnt = this_frag->size;
999 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
1000 this_frag->page_offset,
1001 this_frag->size,
1002 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001003 }
1004}
1005
Byron Bradley324ff2c2008-02-04 23:47:15 -08001006static inline __be16 sum16_as_be(__sum16 sum)
1007{
1008 return (__force __be16)sum;
1009}
1010
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001011/**
1012 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001014 * Ensure the data for an skb to be transmitted is mapped properly,
1015 * then fill in descriptors in the tx hw queue and start the hardware.
1016 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001017static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
1018 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001019{
1020 int tx_index;
1021 struct eth_tx_desc *desc;
1022 u32 cmd_sts;
1023 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001024 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001025
1026 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
1027
1028 tx_index = eth_alloc_tx_desc_index(mp);
1029 desc = &mp->p_tx_desc_area[tx_index];
1030
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001031 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001032 eth_tx_fill_frag_descs(mp, skb);
1033
1034 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -04001035 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001036 } else {
1037 cmd_sts |= ETH_ZERO_PADDING |
1038 ETH_TX_LAST_DESC |
1039 ETH_TX_ENABLE_INTERRUPT;
1040 length = skb->len;
1041 mp->tx_skb[tx_index] = skb;
1042 }
1043
1044 desc->byte_cnt = length;
1045 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001046
Patrick McHardy84fa7932006-08-29 16:44:56 -07001047 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -08001048 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001049
1050 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
1051 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001052 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001053
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001054 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001055 case IPPROTO_UDP:
1056 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -08001057 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001058 break;
1059 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -08001060 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001061 break;
1062 default:
1063 BUG();
1064 }
1065 } else {
1066 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1067 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1068 desc->l4i_chk = 0;
1069 }
1070
1071 /* ensure all other descriptors are written before first cmd_sts */
1072 wmb();
1073 desc->cmd_sts = cmd_sts;
1074
1075 /* ensure all descriptors are written before poking hardware */
1076 wmb();
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001077 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001078
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001079 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001080}
1081
1082/**
1083 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
1086static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1087{
1088 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001089 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001092 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -07001093
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001094 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1095 stats->tx_dropped++;
1096 printk(KERN_DEBUG "%s: failed to linearize tiny "
1097 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001098 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -07001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 spin_lock_irqsave(&mp->lock, flags);
1102
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001103 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1104 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1105 netif_stop_queue(dev);
1106 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001107 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001108 }
1109
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001110 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -07001111 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 stats->tx_packets++;
1113 dev->trans_start = jiffies;
1114
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001115 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1116 netif_stop_queue(dev);
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 spin_unlock_irqrestore(&mp->lock, flags);
1119
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001120 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001123
1124/* mii management interface *************************************************/
1125static int ethernet_phy_get(struct mv643xx_private *mp);
1126
1127/*
1128 * eth_port_read_smi_reg - Read PHY registers
1129 *
1130 * DESCRIPTION:
1131 * This routine utilize the SMI interface to interact with the PHY in
1132 * order to perform PHY register read.
1133 *
1134 * INPUT:
1135 * struct mv643xx_private *mp Ethernet Port.
1136 * unsigned int phy_reg PHY register address offset.
1137 * unsigned int *value Register value buffer.
1138 *
1139 * OUTPUT:
1140 * Write the value of a specified PHY register into given buffer.
1141 *
1142 * RETURN:
1143 * false if the PHY is busy or read data is not in valid state.
1144 * true otherwise.
1145 *
1146 */
1147static void eth_port_read_smi_reg(struct mv643xx_private *mp,
1148 unsigned int phy_reg, unsigned int *value)
1149{
1150 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1151 int phy_addr = ethernet_phy_get(mp);
1152 unsigned long flags;
1153 int i;
1154
1155 /* the SMI register is a shared resource */
1156 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1157
1158 /* wait for the SMI register to become available */
1159 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1160 if (i == PHY_WAIT_ITERATIONS) {
1161 printk("%s: PHY busy timeout\n", mp->dev->name);
1162 goto out;
1163 }
1164 udelay(PHY_WAIT_MICRO_SECONDS);
1165 }
1166
1167 writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
1168 smi_reg);
1169
1170 /* now wait for the data to be valid */
1171 for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
1172 if (i == PHY_WAIT_ITERATIONS) {
1173 printk("%s: PHY read timeout\n", mp->dev->name);
1174 goto out;
1175 }
1176 udelay(PHY_WAIT_MICRO_SECONDS);
1177 }
1178
1179 *value = readl(smi_reg) & 0xffff;
1180out:
1181 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1182}
1183
1184/*
1185 * eth_port_write_smi_reg - Write to PHY registers
1186 *
1187 * DESCRIPTION:
1188 * This routine utilize the SMI interface to interact with the PHY in
1189 * order to perform writes to PHY registers.
1190 *
1191 * INPUT:
1192 * struct mv643xx_private *mp Ethernet Port.
1193 * unsigned int phy_reg PHY register address offset.
1194 * unsigned int value Register value.
1195 *
1196 * OUTPUT:
1197 * Write the given value to the specified PHY register.
1198 *
1199 * RETURN:
1200 * false if the PHY is busy.
1201 * true otherwise.
1202 *
1203 */
1204static void eth_port_write_smi_reg(struct mv643xx_private *mp,
1205 unsigned int phy_reg, unsigned int value)
1206{
1207 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1208 int phy_addr = ethernet_phy_get(mp);
1209 unsigned long flags;
1210 int i;
1211
1212 /* the SMI register is a shared resource */
1213 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1214
1215 /* wait for the SMI register to become available */
1216 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
1217 if (i == PHY_WAIT_ITERATIONS) {
1218 printk("%s: PHY busy timeout\n", mp->dev->name);
1219 goto out;
1220 }
1221 udelay(PHY_WAIT_MICRO_SECONDS);
1222 }
1223
1224 writel((phy_addr << 16) | (phy_reg << 21) |
1225 ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
1226out:
1227 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1228}
1229
1230
1231/* mib counters *************************************************************/
1232/*
1233 * eth_clear_mib_counters - Clear all MIB counters
1234 *
1235 * DESCRIPTION:
1236 * This function clears all MIB counters of a specific ethernet port.
1237 * A read from the MIB counter will reset the counter.
1238 *
1239 * INPUT:
1240 * struct mv643xx_private *mp Ethernet Port.
1241 *
1242 * OUTPUT:
1243 * After reading all MIB counters, the counters resets.
1244 *
1245 * RETURN:
1246 * MIB counter value.
1247 *
1248 */
1249static void eth_clear_mib_counters(struct mv643xx_private *mp)
1250{
1251 unsigned int port_num = mp->port_num;
1252 int i;
1253
1254 /* Perform dummy reads from MIB counters */
1255 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
1256 i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001257 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001258}
1259
1260static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1261{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001262 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001263}
1264
1265static void eth_update_mib_counters(struct mv643xx_private *mp)
1266{
1267 struct mv643xx_mib_counters *p = &mp->mib_counters;
1268 int offset;
1269
1270 p->good_octets_received +=
1271 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
1272 p->good_octets_received +=
1273 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
1274
1275 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
1276 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
1277 offset += 4)
1278 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1279
1280 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
1281 p->good_octets_sent +=
1282 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
1283
1284 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
1285 offset <= ETH_MIB_LATE_COLLISION;
1286 offset += 4)
1287 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1288}
1289
1290
1291/* ethtool ******************************************************************/
1292struct mv643xx_stats {
1293 char stat_string[ETH_GSTRING_LEN];
1294 int sizeof_stat;
1295 int stat_offset;
1296};
1297
1298#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1299 offsetof(struct mv643xx_private, m)
1300
1301static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1302 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1303 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1304 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1305 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1306 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1307 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1308 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1309 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1310 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1311 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1312 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1313 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1314 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1315 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1316 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1317 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1318 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1319 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1320 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1321 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1322 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1323 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1324 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1325 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1326 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1327 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1328 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1329 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1330 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1331 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1332 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1333 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1334 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1335 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1336 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1337 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1338 { "collision", MV643XX_STAT(mib_counters.collision) },
1339 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1340};
1341
1342#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1343
1344static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1345{
1346 struct mv643xx_private *mp = netdev_priv(dev);
1347 int err;
1348
1349 spin_lock_irq(&mp->lock);
1350 err = mii_ethtool_gset(&mp->mii, cmd);
1351 spin_unlock_irq(&mp->lock);
1352
1353 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1354 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1355 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1356
1357 return err;
1358}
1359
1360static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1361{
1362 struct mv643xx_private *mp = netdev_priv(dev);
1363 int err;
1364
1365 spin_lock_irq(&mp->lock);
1366 err = mii_ethtool_sset(&mp->mii, cmd);
1367 spin_unlock_irq(&mp->lock);
1368
1369 return err;
1370}
1371
1372static void mv643xx_get_drvinfo(struct net_device *netdev,
1373 struct ethtool_drvinfo *drvinfo)
1374{
1375 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1376 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1377 strncpy(drvinfo->fw_version, "N/A", 32);
1378 strncpy(drvinfo->bus_info, "mv643xx", 32);
1379 drvinfo->n_stats = MV643XX_STATS_LEN;
1380}
1381
1382static int mv643xx_eth_nway_restart(struct net_device *dev)
1383{
1384 struct mv643xx_private *mp = netdev_priv(dev);
1385
1386 return mii_nway_restart(&mp->mii);
1387}
1388
1389static u32 mv643xx_eth_get_link(struct net_device *dev)
1390{
1391 struct mv643xx_private *mp = netdev_priv(dev);
1392
1393 return mii_link_ok(&mp->mii);
1394}
1395
1396static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1397 uint8_t *data)
1398{
1399 int i;
1400
1401 switch(stringset) {
1402 case ETH_SS_STATS:
1403 for (i=0; i < MV643XX_STATS_LEN; i++) {
1404 memcpy(data + i * ETH_GSTRING_LEN,
1405 mv643xx_gstrings_stats[i].stat_string,
1406 ETH_GSTRING_LEN);
1407 }
1408 break;
1409 }
1410}
1411
1412static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1413 struct ethtool_stats *stats, uint64_t *data)
1414{
1415 struct mv643xx_private *mp = netdev->priv;
1416 int i;
1417
1418 eth_update_mib_counters(mp);
1419
1420 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1421 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1422 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1423 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1424 }
1425}
1426
1427static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1428{
1429 switch (sset) {
1430 case ETH_SS_STATS:
1431 return MV643XX_STATS_LEN;
1432 default:
1433 return -EOPNOTSUPP;
1434 }
1435}
1436
1437static const struct ethtool_ops mv643xx_ethtool_ops = {
1438 .get_settings = mv643xx_get_settings,
1439 .set_settings = mv643xx_set_settings,
1440 .get_drvinfo = mv643xx_get_drvinfo,
1441 .get_link = mv643xx_eth_get_link,
1442 .set_sg = ethtool_op_set_sg,
1443 .get_sset_count = mv643xx_get_sset_count,
1444 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1445 .get_strings = mv643xx_get_strings,
1446 .nway_reset = mv643xx_eth_nway_restart,
1447};
1448
1449
1450/* address handling *********************************************************/
1451/*
1452 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1453 */
1454static void eth_port_uc_addr_get(struct mv643xx_private *mp,
1455 unsigned char *p_addr)
1456{
1457 unsigned int port_num = mp->port_num;
1458 unsigned int mac_h;
1459 unsigned int mac_l;
1460
1461 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1462 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1463
1464 p_addr[0] = (mac_h >> 24) & 0xff;
1465 p_addr[1] = (mac_h >> 16) & 0xff;
1466 p_addr[2] = (mac_h >> 8) & 0xff;
1467 p_addr[3] = mac_h & 0xff;
1468 p_addr[4] = (mac_l >> 8) & 0xff;
1469 p_addr[5] = mac_l & 0xff;
1470}
1471
1472/*
1473 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1474 *
1475 * DESCRIPTION:
1476 * Go through all the DA filter tables (Unicast, Special Multicast &
1477 * Other Multicast) and set each entry to 0.
1478 *
1479 * INPUT:
1480 * struct mv643xx_private *mp Ethernet Port.
1481 *
1482 * OUTPUT:
1483 * Multicast and Unicast packets are rejected.
1484 *
1485 * RETURN:
1486 * None.
1487 */
1488static void eth_port_init_mac_tables(struct mv643xx_private *mp)
1489{
1490 unsigned int port_num = mp->port_num;
1491 int table_index;
1492
1493 /* Clear DA filter unicast table (Ex_dFUT) */
1494 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001495 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001496
1497 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1498 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001499 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001500 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001501 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001502 }
1503}
1504
1505/*
1506 * The entries in each table are indexed by a hash of a packet's MAC
1507 * address. One bit in each entry determines whether the packet is
1508 * accepted. There are 4 entries (each 8 bits wide) in each register
1509 * of the table. The bits in each entry are defined as follows:
1510 * 0 Accept=1, Drop=0
1511 * 3-1 Queue (ETH_Q0=0)
1512 * 7-4 Reserved = 0;
1513 */
1514static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
1515 int table, unsigned char entry)
1516{
1517 unsigned int table_reg;
1518 unsigned int tbl_offset;
1519 unsigned int reg_offset;
1520
1521 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1522 reg_offset = entry % 4; /* Entry offset within the register */
1523
1524 /* Set "accepts frame bit" at specified table entry */
1525 table_reg = rdl(mp, table + tbl_offset);
1526 table_reg |= 0x01 << (8 * reg_offset);
1527 wrl(mp, table + tbl_offset, table_reg);
1528}
1529
1530/*
1531 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1532 */
1533static void eth_port_uc_addr_set(struct mv643xx_private *mp,
1534 unsigned char *p_addr)
1535{
1536 unsigned int port_num = mp->port_num;
1537 unsigned int mac_h;
1538 unsigned int mac_l;
1539 int table;
1540
1541 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1542 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1543 (p_addr[3] << 0);
1544
1545 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1546 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1547
1548 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001549 table = UNICAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001550 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
1551}
1552
1553/*
1554 * mv643xx_eth_update_mac_address
1555 *
1556 * Update the MAC address of the port in the address table
1557 *
1558 * Input : pointer to ethernet interface network device structure
1559 * Output : N/A
1560 */
1561static void mv643xx_eth_update_mac_address(struct net_device *dev)
1562{
1563 struct mv643xx_private *mp = netdev_priv(dev);
1564
1565 eth_port_init_mac_tables(mp);
1566 eth_port_uc_addr_set(mp, dev->dev_addr);
1567}
1568
1569/*
1570 * mv643xx_eth_set_mac_address
1571 *
1572 * Change the interface's mac address.
1573 * No special hardware thing should be done because interface is always
1574 * put in promiscuous mode.
1575 *
1576 * Input : pointer to ethernet interface network device structure and
1577 * a pointer to the designated entry to be added to the cache.
1578 * Output : zero upon success, negative upon failure
1579 */
1580static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1581{
1582 int i;
1583
1584 for (i = 0; i < 6; i++)
1585 /* +2 is for the offset of the HW addr type */
1586 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1587 mv643xx_eth_update_mac_address(dev);
1588 return 0;
1589}
1590
1591/*
1592 * eth_port_mc_addr - Multicast address settings.
1593 *
1594 * The MV device supports multicast using two tables:
1595 * 1) Special Multicast Table for MAC addresses of the form
1596 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1597 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1598 * Table entries in the DA-Filter table.
1599 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1600 * is used as an index to the Other Multicast Table entries in the
1601 * DA-Filter table. This function calculates the CRC-8bit value.
1602 * In either case, eth_port_set_filter_table_entry() is then called
1603 * to set to set the actual table entry.
1604 */
1605static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
1606{
1607 unsigned int port_num = mp->port_num;
1608 unsigned int mac_h;
1609 unsigned int mac_l;
1610 unsigned char crc_result = 0;
1611 int table;
1612 int mac_array[48];
1613 int crc[8];
1614 int i;
1615
1616 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1617 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001618 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001619 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
1620 return;
1621 }
1622
1623 /* Calculate CRC-8 out of the given address */
1624 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1625 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1626 (p_addr[4] << 8) | (p_addr[5] << 0);
1627
1628 for (i = 0; i < 32; i++)
1629 mac_array[i] = (mac_l >> i) & 0x1;
1630 for (i = 32; i < 48; i++)
1631 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1632
1633 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1634 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1635 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1636 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1637 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1638
1639 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1640 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1641 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1642 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1643 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1644 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1645 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1646
1647 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1648 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1649 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1650 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1651 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1652 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1653
1654 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1655 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1656 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1657 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1658 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1659 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1660
1661 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1662 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1663 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1664 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1665 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1666 mac_array[3] ^ mac_array[2];
1667
1668 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1669 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1670 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1671 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1672 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1673 mac_array[4] ^ mac_array[3];
1674
1675 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1676 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1677 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1678 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1679 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1680 mac_array[4];
1681
1682 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1683 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1684 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1685 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1686 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1687
1688 for (i = 0; i < 8; i++)
1689 crc_result = crc_result | (crc[i] << i);
1690
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001691 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001692 eth_port_set_filter_table_entry(mp, table, crc_result);
1693}
1694
1695/*
1696 * Set the entire multicast list based on dev->mc_list.
1697 */
1698static void eth_port_set_multicast_list(struct net_device *dev)
1699{
1700
1701 struct dev_mc_list *mc_list;
1702 int i;
1703 int table_index;
1704 struct mv643xx_private *mp = netdev_priv(dev);
1705 unsigned int eth_port_num = mp->port_num;
1706
1707 /* If the device is in promiscuous mode or in all multicast mode,
1708 * we will fully populate both multicast tables with accept.
1709 * This is guaranteed to yield a match on all multicast addresses...
1710 */
1711 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1712 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1713 /* Set all entries in DA filter special multicast
1714 * table (Ex_dFSMT)
1715 * Set for ETH_Q0 for now
1716 * Bits
1717 * 0 Accept=1, Drop=0
1718 * 3-1 Queue ETH_Q0=0
1719 * 7-4 Reserved = 0;
1720 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001721 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001722
1723 /* Set all entries in DA filter other multicast
1724 * table (Ex_dFOMT)
1725 * Set for ETH_Q0 for now
1726 * Bits
1727 * 0 Accept=1, Drop=0
1728 * 3-1 Queue ETH_Q0=0
1729 * 7-4 Reserved = 0;
1730 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001731 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001732 }
1733 return;
1734 }
1735
1736 /* We will clear out multicast tables every time we get the list.
1737 * Then add the entire new list...
1738 */
1739 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1740 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001741 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001742
1743 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001744 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001745 }
1746
1747 /* Get pointer to net_device multicast list and add each one... */
1748 for (i = 0, mc_list = dev->mc_list;
1749 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1750 i++, mc_list = mc_list->next)
1751 if (mc_list->dmi_addrlen == 6)
1752 eth_port_mc_addr(mp, mc_list->dmi_addr);
1753}
1754
1755/*
1756 * mv643xx_eth_set_rx_mode
1757 *
1758 * Change from promiscuos to regular rx mode
1759 *
1760 * Input : pointer to ethernet interface network device structure
1761 * Output : N/A
1762 */
1763static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1764{
1765 struct mv643xx_private *mp = netdev_priv(dev);
1766 u32 config_reg;
1767
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001768 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001769 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001770 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001771 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001772 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001773 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001774
1775 eth_port_set_multicast_list(dev);
1776}
1777
1778
1779/* rx/tx queue initialisation ***********************************************/
1780/*
1781 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1782 *
1783 * DESCRIPTION:
1784 * This function prepares a Rx chained list of descriptors and packet
1785 * buffers in a form of a ring. The routine must be called after port
1786 * initialization routine and before port start routine.
1787 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1788 * devices in the system (i.e. DRAM). This function uses the ethernet
1789 * struct 'virtual to physical' routine (set by the user) to set the ring
1790 * with physical addresses.
1791 *
1792 * INPUT:
1793 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1794 *
1795 * OUTPUT:
1796 * The routine updates the Ethernet port control struct with information
1797 * regarding the Rx descriptors and buffers.
1798 *
1799 * RETURN:
1800 * None.
1801 */
1802static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1803{
1804 volatile struct eth_rx_desc *p_rx_desc;
1805 int rx_desc_num = mp->rx_ring_size;
1806 int i;
1807
1808 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1809 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1810 for (i = 0; i < rx_desc_num; i++) {
1811 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1812 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1813 }
1814
1815 /* Save Rx desc pointer to driver struct. */
1816 mp->rx_curr_desc_q = 0;
1817 mp->rx_used_desc_q = 0;
1818
1819 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1820}
1821
1822static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1823{
1824 struct mv643xx_private *mp = netdev_priv(dev);
1825 int curr;
1826
1827 /* Stop RX Queues */
1828 mv643xx_eth_port_disable_rx(mp);
1829
1830 /* Free preallocated skb's on RX rings */
1831 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1832 if (mp->rx_skb[curr]) {
1833 dev_kfree_skb(mp->rx_skb[curr]);
1834 mp->rx_desc_count--;
1835 }
1836 }
1837
1838 if (mp->rx_desc_count)
1839 printk(KERN_ERR
1840 "%s: Error in freeing Rx Ring. %d skb's still"
1841 " stuck in RX Ring - ignoring them\n", dev->name,
1842 mp->rx_desc_count);
1843 /* Free RX ring */
1844 if (mp->rx_sram_size)
1845 iounmap(mp->p_rx_desc_area);
1846 else
1847 dma_free_coherent(NULL, mp->rx_desc_area_size,
1848 mp->p_rx_desc_area, mp->rx_desc_dma);
1849}
1850
1851/*
1852 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1853 *
1854 * DESCRIPTION:
1855 * This function prepares a Tx chained list of descriptors and packet
1856 * buffers in a form of a ring. The routine must be called after port
1857 * initialization routine and before port start routine.
1858 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1859 * devices in the system (i.e. DRAM). This function uses the ethernet
1860 * struct 'virtual to physical' routine (set by the user) to set the ring
1861 * with physical addresses.
1862 *
1863 * INPUT:
1864 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1865 *
1866 * OUTPUT:
1867 * The routine updates the Ethernet port control struct with information
1868 * regarding the Tx descriptors and buffers.
1869 *
1870 * RETURN:
1871 * None.
1872 */
1873static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1874{
1875 int tx_desc_num = mp->tx_ring_size;
1876 struct eth_tx_desc *p_tx_desc;
1877 int i;
1878
1879 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1880 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1881 for (i = 0; i < tx_desc_num; i++) {
1882 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1883 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1884 }
1885
1886 mp->tx_curr_desc_q = 0;
1887 mp->tx_used_desc_q = 0;
1888
1889 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1890}
1891
1892/**
1893 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1894 *
1895 * If force is non-zero, frees uncompleted descriptors as well
1896 */
1897static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1898{
1899 struct mv643xx_private *mp = netdev_priv(dev);
1900 struct eth_tx_desc *desc;
1901 u32 cmd_sts;
1902 struct sk_buff *skb;
1903 unsigned long flags;
1904 int tx_index;
1905 dma_addr_t addr;
1906 int count;
1907 int released = 0;
1908
1909 while (mp->tx_desc_count > 0) {
1910 spin_lock_irqsave(&mp->lock, flags);
1911
1912 /* tx_desc_count might have changed before acquiring the lock */
1913 if (mp->tx_desc_count <= 0) {
1914 spin_unlock_irqrestore(&mp->lock, flags);
1915 return released;
1916 }
1917
1918 tx_index = mp->tx_used_desc_q;
1919 desc = &mp->p_tx_desc_area[tx_index];
1920 cmd_sts = desc->cmd_sts;
1921
1922 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
1923 spin_unlock_irqrestore(&mp->lock, flags);
1924 return released;
1925 }
1926
1927 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1928 mp->tx_desc_count--;
1929
1930 addr = desc->buf_ptr;
1931 count = desc->byte_cnt;
1932 skb = mp->tx_skb[tx_index];
1933 if (skb)
1934 mp->tx_skb[tx_index] = NULL;
1935
1936 if (cmd_sts & ETH_ERROR_SUMMARY) {
1937 printk("%s: Error in TX\n", dev->name);
1938 dev->stats.tx_errors++;
1939 }
1940
1941 spin_unlock_irqrestore(&mp->lock, flags);
1942
1943 if (cmd_sts & ETH_TX_FIRST_DESC)
1944 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1945 else
1946 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1947
1948 if (skb)
1949 dev_kfree_skb_irq(skb);
1950
1951 released = 1;
1952 }
1953
1954 return released;
1955}
1956
1957static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1958{
1959 struct mv643xx_private *mp = netdev_priv(dev);
1960
1961 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1962 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1963 netif_wake_queue(dev);
1964}
1965
1966static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1967{
1968 mv643xx_eth_free_tx_descs(dev, 1);
1969}
1970
1971static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1972{
1973 struct mv643xx_private *mp = netdev_priv(dev);
1974
1975 /* Stop Tx Queues */
1976 mv643xx_eth_port_disable_tx(mp);
1977
1978 /* Free outstanding skb's on TX ring */
1979 mv643xx_eth_free_all_tx_descs(dev);
1980
1981 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1982
1983 /* Free TX ring */
1984 if (mp->tx_sram_size)
1985 iounmap(mp->p_tx_desc_area);
1986 else
1987 dma_free_coherent(NULL, mp->tx_desc_area_size,
1988 mp->p_tx_desc_area, mp->tx_desc_dma);
1989}
1990
1991
1992/* netdev ops and related ***************************************************/
1993static void eth_port_reset(struct mv643xx_private *mp);
1994
1995/* Set the mv643xx port configuration register for the speed/duplex mode. */
1996static void mv643xx_eth_update_pscr(struct net_device *dev,
1997 struct ethtool_cmd *ecmd)
1998{
1999 struct mv643xx_private *mp = netdev_priv(dev);
2000 int port_num = mp->port_num;
2001 u32 o_pscr, n_pscr;
2002 unsigned int queues;
2003
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002004 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002005 n_pscr = o_pscr;
2006
2007 /* clear speed, duplex and rx buffer size fields */
2008 n_pscr &= ~(SET_MII_SPEED_TO_100 |
2009 SET_GMII_SPEED_TO_1000 |
2010 SET_FULL_DUPLEX_MODE |
2011 MAX_RX_PACKET_MASK);
2012
2013 if (ecmd->duplex == DUPLEX_FULL)
2014 n_pscr |= SET_FULL_DUPLEX_MODE;
2015
2016 if (ecmd->speed == SPEED_1000)
2017 n_pscr |= SET_GMII_SPEED_TO_1000 |
2018 MAX_RX_PACKET_9700BYTE;
2019 else {
2020 if (ecmd->speed == SPEED_100)
2021 n_pscr |= SET_MII_SPEED_TO_100;
2022 n_pscr |= MAX_RX_PACKET_1522BYTE;
2023 }
2024
2025 if (n_pscr != o_pscr) {
2026 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002027 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002028 else {
2029 queues = mv643xx_eth_port_disable_tx(mp);
2030
2031 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002032 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
2033 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
2034 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002035 if (queues)
2036 mv643xx_eth_port_enable_tx(mp, queues);
2037 }
2038 }
2039}
2040
2041/*
2042 * mv643xx_eth_int_handler
2043 *
2044 * Main interrupt handler for the gigbit ethernet ports
2045 *
2046 * Input : irq - irq number (not used)
2047 * dev_id - a pointer to the required interface's data structure
2048 * regs - not used
2049 * Output : N/A
2050 */
2051
2052static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
2053{
2054 struct net_device *dev = (struct net_device *)dev_id;
2055 struct mv643xx_private *mp = netdev_priv(dev);
2056 u32 eth_int_cause, eth_int_cause_ext = 0;
2057 unsigned int port_num = mp->port_num;
2058
2059 /* Read interrupt cause registers */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002060 eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & ETH_INT_UNMASK_ALL;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002061 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002062 eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
2063 & ETH_INT_UNMASK_ALL_EXT;
2064 wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002065 }
2066
2067 /* PHY status changed */
2068 if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
2069 struct ethtool_cmd cmd;
2070
2071 if (mii_link_ok(&mp->mii)) {
2072 mii_ethtool_gset(&mp->mii, &cmd);
2073 mv643xx_eth_update_pscr(dev, &cmd);
2074 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
2075 if (!netif_carrier_ok(dev)) {
2076 netif_carrier_on(dev);
2077 if (mp->tx_ring_size - mp->tx_desc_count >=
2078 MAX_DESCS_PER_SKB)
2079 netif_wake_queue(dev);
2080 }
2081 } else if (netif_carrier_ok(dev)) {
2082 netif_stop_queue(dev);
2083 netif_carrier_off(dev);
2084 }
2085 }
2086
2087#ifdef MV643XX_NAPI
2088 if (eth_int_cause & ETH_INT_CAUSE_RX) {
2089 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002090 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002091
2092 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002093 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002094
2095 netif_rx_schedule(dev, &mp->napi);
2096 }
2097#else
2098 if (eth_int_cause & ETH_INT_CAUSE_RX)
2099 mv643xx_eth_receive_queue(dev, INT_MAX);
2100#endif
2101 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
2102 mv643xx_eth_free_completed_tx_descs(dev);
2103
2104 /*
2105 * If no real interrupt occured, exit.
2106 * This can happen when using gigE interrupt coalescing mechanism.
2107 */
2108 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
2109 return IRQ_NONE;
2110
2111 return IRQ_HANDLED;
2112}
2113
2114/*
2115 * ethernet_phy_reset - Reset Ethernet port PHY.
2116 *
2117 * DESCRIPTION:
2118 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2119 *
2120 * INPUT:
2121 * struct mv643xx_private *mp Ethernet Port.
2122 *
2123 * OUTPUT:
2124 * The PHY is reset.
2125 *
2126 * RETURN:
2127 * None.
2128 *
2129 */
2130static void ethernet_phy_reset(struct mv643xx_private *mp)
2131{
2132 unsigned int phy_reg_data;
2133
2134 /* Reset the PHY */
2135 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2136 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2137 eth_port_write_smi_reg(mp, 0, phy_reg_data);
2138
2139 /* wait for PHY to come out of reset */
2140 do {
2141 udelay(1);
2142 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2143 } while (phy_reg_data & 0x8000);
2144}
2145
2146/*
2147 * eth_port_start - Start the Ethernet port activity.
2148 *
2149 * DESCRIPTION:
2150 * This routine prepares the Ethernet port for Rx and Tx activity:
2151 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2152 * has been initialized a descriptor's ring (using
2153 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2154 * 2. Initialize and enable the Ethernet configuration port by writing to
2155 * the port's configuration and command registers.
2156 * 3. Initialize and enable the SDMA by writing to the SDMA's
2157 * configuration and command registers. After completing these steps,
2158 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2159 *
2160 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2161 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2162 * and ether_init_rx_desc_ring for Rx queues).
2163 *
2164 * INPUT:
2165 * dev - a pointer to the required interface
2166 *
2167 * OUTPUT:
2168 * Ethernet port is ready to receive and transmit.
2169 *
2170 * RETURN:
2171 * None.
2172 */
2173static void eth_port_start(struct net_device *dev)
2174{
2175 struct mv643xx_private *mp = netdev_priv(dev);
2176 unsigned int port_num = mp->port_num;
2177 int tx_curr_desc, rx_curr_desc;
2178 u32 pscr;
2179 struct ethtool_cmd ethtool_cmd;
2180
2181 /* Assignment of Tx CTRP of given queue */
2182 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002183 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002184 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2185
2186 /* Assignment of Rx CRDP of given queue */
2187 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002188 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002189 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2190
2191 /* Add the assigned Ethernet address to the port's address table */
2192 eth_port_uc_addr_set(mp, dev->dev_addr);
2193
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002194 /*
2195 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2196 * frames to RX queue #0.
2197 */
2198 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002199
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002200 /*
2201 * Treat BPDUs as normal multicasts, and disable partition mode.
2202 */
2203 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002204
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002205 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002206
2207 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002208 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002209
2210 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2211 DISABLE_AUTO_NEG_SPEED_GMII |
2212 DISABLE_AUTO_NEG_FOR_DUPLX |
2213 DO_NOT_FORCE_LINK_FAIL |
2214 SERIAL_PORT_CONTROL_RESERVED;
2215
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002216 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002217
2218 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002219 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002220
2221 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002222 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002223
2224 /* Enable port Rx. */
2225 mv643xx_eth_port_enable_rx(mp, ETH_RX_QUEUES_ENABLED);
2226
2227 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002228 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002229
2230 /* save phy settings across reset */
2231 mv643xx_get_settings(dev, &ethtool_cmd);
2232 ethernet_phy_reset(mp);
2233 mv643xx_set_settings(dev, &ethtool_cmd);
2234}
2235
2236#ifdef MV643XX_COAL
2237
2238/*
2239 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
2240 *
2241 * DESCRIPTION:
2242 * This routine sets the RX coalescing interrupt mechanism parameter.
2243 * This parameter is a timeout counter, that counts in 64 t_clk
2244 * chunks ; that when timeout event occurs a maskable interrupt
2245 * occurs.
2246 * The parameter is calculated using the tClk of the MV-643xx chip
2247 * , and the required delay of the interrupt in usec.
2248 *
2249 * INPUT:
2250 * struct mv643xx_private *mp Ethernet port
2251 * unsigned int delay Delay in usec
2252 *
2253 * OUTPUT:
2254 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2255 *
2256 * RETURN:
2257 * The interrupt coalescing value set in the gigE port.
2258 *
2259 */
2260static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
2261 unsigned int delay)
2262{
2263 unsigned int port_num = mp->port_num;
2264 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2265
2266 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002267 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002268 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002269 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002270 & 0xffc000ff));
2271
2272 return coal;
2273}
2274#endif
2275
2276/*
2277 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
2278 *
2279 * DESCRIPTION:
2280 * This routine sets the TX coalescing interrupt mechanism parameter.
2281 * This parameter is a timeout counter, that counts in 64 t_clk
2282 * chunks ; that when timeout event occurs a maskable interrupt
2283 * occurs.
2284 * The parameter is calculated using the t_cLK frequency of the
2285 * MV-643xx chip and the required delay in the interrupt in uSec
2286 *
2287 * INPUT:
2288 * struct mv643xx_private *mp Ethernet port
2289 * unsigned int delay Delay in uSeconds
2290 *
2291 * OUTPUT:
2292 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2293 *
2294 * RETURN:
2295 * The interrupt coalescing value set in the gigE port.
2296 *
2297 */
2298static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
2299 unsigned int delay)
2300{
2301 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2302
2303 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002304 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002305
2306 return coal;
2307}
2308
2309/*
2310 * eth_port_init - Initialize the Ethernet port driver
2311 *
2312 * DESCRIPTION:
2313 * This function prepares the ethernet port to start its activity:
2314 * 1) Completes the ethernet port driver struct initialization toward port
2315 * start routine.
2316 * 2) Resets the device to a quiescent state in case of warm reboot.
2317 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2318 * 4) Clean MAC tables. The reset status of those tables is unknown.
2319 * 5) Set PHY address.
2320 * Note: Call this routine prior to eth_port_start routine and after
2321 * setting user values in the user fields of Ethernet port control
2322 * struct.
2323 *
2324 * INPUT:
2325 * struct mv643xx_private *mp Ethernet port control struct
2326 *
2327 * OUTPUT:
2328 * See description.
2329 *
2330 * RETURN:
2331 * None.
2332 */
2333static void eth_port_init(struct mv643xx_private *mp)
2334{
2335 mp->rx_resource_err = 0;
2336
2337 eth_port_reset(mp);
2338
2339 eth_port_init_mac_tables(mp);
2340}
2341
2342/*
2343 * mv643xx_eth_open
2344 *
2345 * This function is called when openning the network device. The function
2346 * should initialize all the hardware, initialize cyclic Rx/Tx
2347 * descriptors chain and buffers and allocate an IRQ to the network
2348 * device.
2349 *
2350 * Input : a pointer to the network device structure
2351 *
2352 * Output : zero of success , nonzero if fails.
2353 */
2354
2355static int mv643xx_eth_open(struct net_device *dev)
2356{
2357 struct mv643xx_private *mp = netdev_priv(dev);
2358 unsigned int port_num = mp->port_num;
2359 unsigned int size;
2360 int err;
2361
2362 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002363 wrl(mp, INT_CAUSE(port_num), 0);
2364 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002365 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002366 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002367
2368 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2369 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2370 if (err) {
2371 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2372 return -EAGAIN;
2373 }
2374
2375 eth_port_init(mp);
2376
2377 memset(&mp->timeout, 0, sizeof(struct timer_list));
2378 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2379 mp->timeout.data = (unsigned long)dev;
2380
2381 /* Allocate RX and TX skb rings */
2382 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2383 GFP_KERNEL);
2384 if (!mp->rx_skb) {
2385 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2386 err = -ENOMEM;
2387 goto out_free_irq;
2388 }
2389 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2390 GFP_KERNEL);
2391 if (!mp->tx_skb) {
2392 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2393 err = -ENOMEM;
2394 goto out_free_rx_skb;
2395 }
2396
2397 /* Allocate TX ring */
2398 mp->tx_desc_count = 0;
2399 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
2400 mp->tx_desc_area_size = size;
2401
2402 if (mp->tx_sram_size) {
2403 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2404 mp->tx_sram_size);
2405 mp->tx_desc_dma = mp->tx_sram_addr;
2406 } else
2407 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2408 &mp->tx_desc_dma,
2409 GFP_KERNEL);
2410
2411 if (!mp->p_tx_desc_area) {
2412 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2413 dev->name, size);
2414 err = -ENOMEM;
2415 goto out_free_tx_skb;
2416 }
2417 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2418 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2419
2420 ether_init_tx_desc_ring(mp);
2421
2422 /* Allocate RX ring */
2423 mp->rx_desc_count = 0;
2424 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
2425 mp->rx_desc_area_size = size;
2426
2427 if (mp->rx_sram_size) {
2428 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2429 mp->rx_sram_size);
2430 mp->rx_desc_dma = mp->rx_sram_addr;
2431 } else
2432 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2433 &mp->rx_desc_dma,
2434 GFP_KERNEL);
2435
2436 if (!mp->p_rx_desc_area) {
2437 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2438 dev->name, size);
2439 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2440 dev->name);
2441 if (mp->rx_sram_size)
2442 iounmap(mp->p_tx_desc_area);
2443 else
2444 dma_free_coherent(NULL, mp->tx_desc_area_size,
2445 mp->p_tx_desc_area, mp->tx_desc_dma);
2446 err = -ENOMEM;
2447 goto out_free_tx_skb;
2448 }
2449 memset((void *)mp->p_rx_desc_area, 0, size);
2450
2451 ether_init_rx_desc_ring(mp);
2452
2453 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2454
2455#ifdef MV643XX_NAPI
2456 napi_enable(&mp->napi);
2457#endif
2458
2459 eth_port_start(dev);
2460
2461 /* Interrupt Coalescing */
2462
2463#ifdef MV643XX_COAL
2464 mp->rx_int_coal =
2465 eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
2466#endif
2467
2468 mp->tx_int_coal =
2469 eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
2470
2471 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002472 wrl(mp, INT_MASK_EXT(port_num), ETH_INT_UNMASK_ALL_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002473
2474 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002475 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002476
2477 return 0;
2478
2479out_free_tx_skb:
2480 kfree(mp->tx_skb);
2481out_free_rx_skb:
2482 kfree(mp->rx_skb);
2483out_free_irq:
2484 free_irq(dev->irq, dev);
2485
2486 return err;
2487}
2488
2489/*
2490 * eth_port_reset - Reset Ethernet port
2491 *
2492 * DESCRIPTION:
2493 * This routine resets the chip by aborting any SDMA engine activity and
2494 * clearing the MIB counters. The Receiver and the Transmit unit are in
2495 * idle state after this command is performed and the port is disabled.
2496 *
2497 * INPUT:
2498 * struct mv643xx_private *mp Ethernet Port.
2499 *
2500 * OUTPUT:
2501 * Channel activity is halted.
2502 *
2503 * RETURN:
2504 * None.
2505 *
2506 */
2507static void eth_port_reset(struct mv643xx_private *mp)
2508{
2509 unsigned int port_num = mp->port_num;
2510 unsigned int reg_data;
2511
2512 mv643xx_eth_port_disable_tx(mp);
2513 mv643xx_eth_port_disable_rx(mp);
2514
2515 /* Clear all MIB counters */
2516 eth_clear_mib_counters(mp);
2517
2518 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002519 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002520 reg_data &= ~(SERIAL_PORT_ENABLE |
2521 DO_NOT_FORCE_LINK_FAIL |
2522 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002523 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002524}
2525
2526/*
2527 * mv643xx_eth_stop
2528 *
2529 * This function is used when closing the network device.
2530 * It updates the hardware,
2531 * release all memory that holds buffers and descriptors and release the IRQ.
2532 * Input : a pointer to the device structure
2533 * Output : zero if success , nonzero if fails
2534 */
2535
2536static int mv643xx_eth_stop(struct net_device *dev)
2537{
2538 struct mv643xx_private *mp = netdev_priv(dev);
2539 unsigned int port_num = mp->port_num;
2540
2541 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002542 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002543 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002544 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002545
2546#ifdef MV643XX_NAPI
2547 napi_disable(&mp->napi);
2548#endif
2549 netif_carrier_off(dev);
2550 netif_stop_queue(dev);
2551
2552 eth_port_reset(mp);
2553
2554 mv643xx_eth_free_tx_rings(dev);
2555 mv643xx_eth_free_rx_rings(dev);
2556
2557 free_irq(dev->irq, dev);
2558
2559 return 0;
2560}
2561
2562static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2563{
2564 struct mv643xx_private *mp = netdev_priv(dev);
2565
2566 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2567}
2568
2569/*
2570 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2571 *
2572 * Input : pointer to ethernet interface network device structure
2573 * new mtu size
2574 * Output : 0 upon success, -EINVAL upon failure
2575 */
2576static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2577{
2578 if ((new_mtu > 9500) || (new_mtu < 64))
2579 return -EINVAL;
2580
2581 dev->mtu = new_mtu;
2582 if (!netif_running(dev))
2583 return 0;
2584
2585 /*
2586 * Stop and then re-open the interface. This will allocate RX
2587 * skbs of the new MTU.
2588 * There is a possible danger that the open will not succeed,
2589 * due to memory being full, which might fail the open function.
2590 */
2591 mv643xx_eth_stop(dev);
2592 if (mv643xx_eth_open(dev)) {
2593 printk(KERN_ERR "%s: Fatal error on opening device\n",
2594 dev->name);
2595 }
2596
2597 return 0;
2598}
2599
2600/*
2601 * mv643xx_eth_tx_timeout_task
2602 *
2603 * Actual routine to reset the adapter when a timeout on Tx has occurred
2604 */
2605static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2606{
2607 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2608 tx_timeout_task);
2609 struct net_device *dev = mp->dev;
2610
2611 if (!netif_running(dev))
2612 return;
2613
2614 netif_stop_queue(dev);
2615
2616 eth_port_reset(mp);
2617 eth_port_start(dev);
2618
2619 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2620 netif_wake_queue(dev);
2621}
2622
2623/*
2624 * mv643xx_eth_tx_timeout
2625 *
2626 * Called upon a timeout on transmitting a packet
2627 *
2628 * Input : pointer to ethernet interface network device structure.
2629 * Output : N/A
2630 */
2631static void mv643xx_eth_tx_timeout(struct net_device *dev)
2632{
2633 struct mv643xx_private *mp = netdev_priv(dev);
2634
2635 printk(KERN_INFO "%s: TX timeout ", dev->name);
2636
2637 /* Do the reset outside of interrupt context */
2638 schedule_work(&mp->tx_timeout_task);
2639}
2640
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002641#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002642static void mv643xx_netpoll(struct net_device *netdev)
2643{
2644 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002645 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002646
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002647 wrl(mp, INT_MASK(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002648 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002649 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002650
Al Viro9da3b1a2006-10-08 15:00:44 +01002651 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002652
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002653 wrl(mp, INT_MASK(port_num), ETH_INT_UNMASK_ALL);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002654}
2655#endif
2656
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002657/*
2658 * Wrappers for MII support library.
2659 */
2660static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2661{
2662 struct mv643xx_private *mp = netdev_priv(dev);
2663 int val;
2664
2665 eth_port_read_smi_reg(mp, location, &val);
2666 return val;
2667}
2668
2669static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2670{
2671 struct mv643xx_private *mp = netdev_priv(dev);
2672 eth_port_write_smi_reg(mp, location, val);
2673}
2674
2675
2676/* platform glue ************************************************************/
2677static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2678 struct mbus_dram_target_info *dram)
2679{
2680 void __iomem *base = msp->eth_base;
2681 u32 win_enable;
2682 u32 win_protect;
2683 int i;
2684
2685 for (i = 0; i < 6; i++) {
2686 writel(0, base + WINDOW_BASE(i));
2687 writel(0, base + WINDOW_SIZE(i));
2688 if (i < 4)
2689 writel(0, base + WINDOW_REMAP_HIGH(i));
2690 }
2691
2692 win_enable = 0x3f;
2693 win_protect = 0;
2694
2695 for (i = 0; i < dram->num_cs; i++) {
2696 struct mbus_dram_window *cs = dram->cs + i;
2697
2698 writel((cs->base & 0xffff0000) |
2699 (cs->mbus_attr << 8) |
2700 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2701 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2702
2703 win_enable &= ~(1 << i);
2704 win_protect |= 3 << (2 * i);
2705 }
2706
2707 writel(win_enable, base + WINDOW_BAR_ENABLE);
2708 msp->win_protect = win_protect;
2709}
2710
2711static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2712{
2713 static int mv643xx_version_printed = 0;
2714 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2715 struct mv643xx_shared_private *msp;
2716 struct resource *res;
2717 int ret;
2718
2719 if (!mv643xx_version_printed++)
2720 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2721
2722 ret = -EINVAL;
2723 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2724 if (res == NULL)
2725 goto out;
2726
2727 ret = -ENOMEM;
2728 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2729 if (msp == NULL)
2730 goto out;
2731 memset(msp, 0, sizeof(*msp));
2732
2733 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2734 if (msp->eth_base == NULL)
2735 goto out_free;
2736
2737 spin_lock_init(&msp->phy_lock);
2738 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2739
2740 platform_set_drvdata(pdev, msp);
2741
2742 /*
2743 * (Re-)program MBUS remapping windows if we are asked to.
2744 */
2745 if (pd != NULL && pd->dram != NULL)
2746 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2747
2748 return 0;
2749
2750out_free:
2751 kfree(msp);
2752out:
2753 return ret;
2754}
2755
2756static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2757{
2758 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2759
2760 iounmap(msp->eth_base);
2761 kfree(msp);
2762
2763 return 0;
2764}
2765
2766static struct platform_driver mv643xx_eth_shared_driver = {
2767 .probe = mv643xx_eth_shared_probe,
2768 .remove = mv643xx_eth_shared_remove,
2769 .driver = {
2770 .name = MV643XX_ETH_SHARED_NAME,
2771 .owner = THIS_MODULE,
2772 },
2773};
2774
2775/*
2776 * ethernet_phy_set - Set the ethernet port PHY address.
2777 *
2778 * DESCRIPTION:
2779 * This routine sets the given ethernet port PHY address.
2780 *
2781 * INPUT:
2782 * struct mv643xx_private *mp Ethernet Port.
2783 * int phy_addr PHY address.
2784 *
2785 * OUTPUT:
2786 * None.
2787 *
2788 * RETURN:
2789 * None.
2790 *
2791 */
2792static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
2793{
2794 u32 reg_data;
2795 int addr_shift = 5 * mp->port_num;
2796
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002797 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002798 reg_data &= ~(0x1f << addr_shift);
2799 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002800 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002801}
2802
2803/*
2804 * ethernet_phy_get - Get the ethernet port PHY address.
2805 *
2806 * DESCRIPTION:
2807 * This routine returns the given ethernet port PHY address.
2808 *
2809 * INPUT:
2810 * struct mv643xx_private *mp Ethernet Port.
2811 *
2812 * OUTPUT:
2813 * None.
2814 *
2815 * RETURN:
2816 * PHY address.
2817 *
2818 */
2819static int ethernet_phy_get(struct mv643xx_private *mp)
2820{
2821 unsigned int reg_data;
2822
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002823 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002824
2825 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2826}
2827
2828/*
2829 * ethernet_phy_detect - Detect whether a phy is present
2830 *
2831 * DESCRIPTION:
2832 * This function tests whether there is a PHY present on
2833 * the specified port.
2834 *
2835 * INPUT:
2836 * struct mv643xx_private *mp Ethernet Port.
2837 *
2838 * OUTPUT:
2839 * None
2840 *
2841 * RETURN:
2842 * 0 on success
2843 * -ENODEV on failure
2844 *
2845 */
2846static int ethernet_phy_detect(struct mv643xx_private *mp)
2847{
2848 unsigned int phy_reg_data0;
2849 int auto_neg;
2850
2851 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2852 auto_neg = phy_reg_data0 & 0x1000;
2853 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2854 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2855
2856 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2857 if ((phy_reg_data0 & 0x1000) == auto_neg)
2858 return -ENODEV; /* change didn't take */
2859
2860 phy_reg_data0 ^= 0x1000;
2861 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2862 return 0;
2863}
2864
James Chapmand0412d92006-01-27 01:15:30 -07002865static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2866 int speed, int duplex,
2867 struct ethtool_cmd *cmd)
2868{
2869 struct mv643xx_private *mp = netdev_priv(dev);
2870
2871 memset(cmd, 0, sizeof(*cmd));
2872
2873 cmd->port = PORT_MII;
2874 cmd->transceiver = XCVR_INTERNAL;
2875 cmd->phy_address = phy_address;
2876
2877 if (speed == 0) {
2878 cmd->autoneg = AUTONEG_ENABLE;
2879 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2880 cmd->speed = SPEED_100;
2881 cmd->advertising = ADVERTISED_10baseT_Half |
2882 ADVERTISED_10baseT_Full |
2883 ADVERTISED_100baseT_Half |
2884 ADVERTISED_100baseT_Full;
2885 if (mp->mii.supports_gmii)
2886 cmd->advertising |= ADVERTISED_1000baseT_Full;
2887 } else {
2888 cmd->autoneg = AUTONEG_DISABLE;
2889 cmd->speed = speed;
2890 cmd->duplex = duplex;
2891 }
2892}
2893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894/*/
2895 * mv643xx_eth_probe
2896 *
2897 * First function called after registering the network device.
2898 * It's purpose is to initialize the device as an ethernet device,
2899 * fill the ethernet device structure with pointers * to functions,
2900 * and set the MAC address of the interface
2901 *
2902 * Input : struct device *
2903 * Output : -ENOMEM if failed , 0 if success
2904 */
Russell King3ae5eae2005-11-09 22:32:44 +00002905static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002908 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 struct mv643xx_private *mp;
2910 struct net_device *dev;
2911 u8 *p;
2912 struct resource *res;
2913 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002914 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002915 int duplex = DUPLEX_HALF;
2916 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002917 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002919 pd = pdev->dev.platform_data;
2920 if (pd == NULL) {
2921 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2922 return -ENODEV;
2923 }
2924
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002925 if (pd->shared == NULL) {
2926 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2927 return -ENODEV;
2928 }
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2931 if (!dev)
2932 return -ENOMEM;
2933
Russell King3ae5eae2005-11-09 22:32:44 +00002934 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002937 mp->dev = dev;
2938#ifdef MV643XX_NAPI
2939 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2940#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2943 BUG_ON(!res);
2944 dev->irq = res->start;
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 dev->open = mv643xx_eth_open;
2947 dev->stop = mv643xx_eth_stop;
2948 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 dev->set_mac_address = mv643xx_eth_set_mac_address;
2950 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2951
2952 /* No need to Tx Timeout */
2953 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002955#ifdef CONFIG_NET_POLL_CONTROLLER
2956 dev->poll_controller = mv643xx_netpoll;
2957#endif
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 dev->base_addr = 0;
2961 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002962 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
2964
2965#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2966#ifdef MAX_SKB_FRAGS
2967 /*
2968 * Zero copy can only work if we use Discovery II memory. Else, we will
2969 * have to map the buffers to ISA memory which is only 16 MB
2970 */
Wolfram Joost63890572006-01-16 16:57:41 -07002971 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972#endif
2973#endif
2974
2975 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002976 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 spin_lock_init(&mp->lock);
2979
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002980 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002981 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002982
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002983 if (mp->shared->win_protect)
2984 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2985
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002986 mp->shared_smi = mp->shared;
2987 if (pd->shared_smi != NULL)
2988 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002991 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002992 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
2993 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002995 if (is_valid_ether_addr(pd->mac_addr))
2996 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002998 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002999 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003001 if (pd->rx_queue_size)
3002 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003004 if (pd->tx_queue_size)
3005 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003007 if (pd->tx_sram_size) {
3008 mp->tx_sram_size = pd->tx_sram_size;
3009 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 }
3011
Dale Farnsworth84dd6192007-03-03 06:40:28 -07003012 if (pd->rx_sram_size) {
3013 mp->rx_sram_size = pd->rx_sram_size;
3014 mp->rx_sram_addr = pd->rx_sram_addr;
3015 }
3016
3017 duplex = pd->duplex;
3018 speed = pd->speed;
3019
James Chapmanc28a4f82006-01-27 01:13:15 -07003020 /* Hook up MII support for ethtool */
3021 mp->mii.dev = dev;
3022 mp->mii.mdio_read = mv643xx_mdio_read;
3023 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003024 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07003025 mp->mii.phy_id_mask = 0x3f;
3026 mp->mii.reg_num_mask = 0x1f;
3027
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003028 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07003030 pr_debug("%s: No PHY detected at addr %d\n",
3031 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07003032 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003035 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07003036 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07003037 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
3038 mv643xx_eth_update_pscr(dev, &cmd);
3039 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07003040
Olaf Heringb0b8dab2006-04-27 18:23:49 -07003041 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 err = register_netdev(dev);
3043 if (err)
3044 goto out;
3045
3046 p = dev->dev_addr;
3047 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07003048 "%s: port %d with MAC address %s\n",
3049 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 if (dev->features & NETIF_F_SG)
3052 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
3053
3054 if (dev->features & NETIF_F_IP_CSUM)
3055 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
3056 dev->name);
3057
3058#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
3059 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
3060#endif
3061
3062#ifdef MV643XX_COAL
3063 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
3064 dev->name);
3065#endif
3066
3067#ifdef MV643XX_NAPI
3068 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
3069#endif
3070
Nicolas DETb1529872005-10-28 17:46:30 -07003071 if (mp->tx_sram_size > 0)
3072 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
3073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 return 0;
3075
3076out:
3077 free_netdev(dev);
3078
3079 return err;
3080}
3081
Russell King3ae5eae2005-11-09 22:32:44 +00003082static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083{
Russell King3ae5eae2005-11-09 22:32:44 +00003084 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085
3086 unregister_netdev(dev);
3087 flush_scheduled_work();
3088
3089 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00003090 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 return 0;
3092}
3093
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003094static void mv643xx_eth_shutdown(struct platform_device *pdev)
3095{
3096 struct net_device *dev = platform_get_drvdata(pdev);
3097 struct mv643xx_private *mp = netdev_priv(dev);
3098 unsigned int port_num = mp->port_num;
3099
3100 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02003101 wrl(mp, INT_MASK(port_num), 0);
3102 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003103
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003104 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003105}
3106
Russell King3ae5eae2005-11-09 22:32:44 +00003107static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 .probe = mv643xx_eth_probe,
3109 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07003110 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00003111 .driver = {
3112 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07003113 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00003114 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115};
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117/*
3118 * mv643xx_init_module
3119 *
3120 * Registers the network drivers into the Linux kernel
3121 *
3122 * Input : N/A
3123 *
3124 * Output : N/A
3125 */
3126static int __init mv643xx_init_module(void)
3127{
3128 int rc;
3129
Russell King3ae5eae2005-11-09 22:32:44 +00003130 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003132 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003134 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 }
3136 return rc;
3137}
3138
3139/*
3140 * mv643xx_cleanup_module
3141 *
3142 * Registers the network drivers into the Linux kernel
3143 *
3144 * Input : N/A
3145 *
3146 * Output : N/A
3147 */
3148static void __exit mv643xx_cleanup_module(void)
3149{
Russell King3ae5eae2005-11-09 22:32:44 +00003150 platform_driver_unregister(&mv643xx_eth_driver);
3151 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
3154module_init(mv643xx_init_module);
3155module_exit(mv643xx_cleanup_module);
3156
3157MODULE_LICENSE("GPL");
3158MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
3159 " and Dale Farnsworth");
3160MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07003161MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3162MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);