mv643xx_eth: various cleanups
[linux-2.6-block.git] / drivers / net / mv643xx_eth.c
CommitLineData
1da177e4 1/*
9c1bbdfe 2 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
1da177e4
LT
3 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
6 * Copyright (C) 2002 rabeeh@galileo.co.il
7 *
8 * Copyright (C) 2003 PMC-Sierra, Inc.,
3bb8a18a 9 * written by Manish Lachwani
1da177e4
LT
10 *
11 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
12 *
c8aaea25 13 * Copyright (C) 2004-2006 MontaVista Software, Inc.
1da177e4
LT
14 * Dale Farnsworth <dale@farnsworth.org>
15 *
16 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
17 * <sjhill@realitydiluted.com>
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 */
33#include <linux/init.h>
34#include <linux/dma-mapping.h>
b6298c22
AV
35#include <linux/in.h>
36#include <linux/ip.h>
1da177e4
LT
37#include <linux/tcp.h>
38#include <linux/udp.h>
39#include <linux/etherdevice.h>
40
41#include <linux/bitops.h>
42#include <linux/delay.h>
43#include <linux/ethtool.h>
d052d1be
RK
44#include <linux/platform_device.h>
45
fbd6a754
LB
46#include <linux/module.h>
47#include <linux/kernel.h>
48#include <linux/spinlock.h>
49#include <linux/workqueue.h>
50#include <linux/mii.h>
51
52#include <linux/mv643xx_eth.h>
53
1da177e4
LT
54#include <asm/io.h>
55#include <asm/types.h>
56#include <asm/pgtable.h>
57#include <asm/system.h>
58#include <asm/delay.h>
fbd6a754
LB
59#include <asm/dma-mapping.h>
60
fbd6a754
LB
61#define MV643XX_CHECKSUM_OFFLOAD_TX
62#define MV643XX_NAPI
63#define MV643XX_TX_FAST_REFILL
64#undef MV643XX_COAL
65
fbd6a754
LB
66#define MV643XX_TX_COAL 100
67#ifdef MV643XX_COAL
68#define MV643XX_RX_COAL 100
69#endif
70
71#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
72#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
73#else
74#define MAX_DESCS_PER_SKB 1
75#endif
76
77#define ETH_VLAN_HLEN 4
78#define ETH_FCS_LEN 4
79#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
80#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
81 ETH_VLAN_HLEN + ETH_FCS_LEN)
82#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
83 dma_get_cache_alignment())
84
85/*
86 * Registers shared between all ports.
87 */
88#define PHY_ADDR_REG 0x0000
89#define SMI_REG 0x0004
90
91/*
92 * Per-port registers.
93 */
94#define PORT_CONFIG_REG(p) (0x0400 + ((p) << 10))
95#define PORT_CONFIG_EXTEND_REG(p) (0x0404 + ((p) << 10))
96#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
97#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
98#define SDMA_CONFIG_REG(p) (0x041c + ((p) << 10))
99#define PORT_SERIAL_CONTROL_REG(p) (0x043c + ((p) << 10))
100#define PORT_STATUS_REG(p) (0x0444 + ((p) << 10))
101#define TRANSMIT_QUEUE_COMMAND_REG(p) (0x0448 + ((p) << 10))
102#define MAXIMUM_TRANSMIT_UNIT(p) (0x0458 + ((p) << 10))
103#define INTERRUPT_CAUSE_REG(p) (0x0460 + ((p) << 10))
104#define INTERRUPT_CAUSE_EXTEND_REG(p) (0x0464 + ((p) << 10))
105#define INTERRUPT_MASK_REG(p) (0x0468 + ((p) << 10))
106#define INTERRUPT_EXTEND_MASK_REG(p) (0x046c + ((p) << 10))
107#define TX_FIFO_URGENT_THRESHOLD_REG(p) (0x0474 + ((p) << 10))
108#define RX_CURRENT_QUEUE_DESC_PTR_0(p) (0x060c + ((p) << 10))
109#define RECEIVE_QUEUE_COMMAND_REG(p) (0x0680 + ((p) << 10))
110#define TX_CURRENT_QUEUE_DESC_PTR_0(p) (0x06c0 + ((p) << 10))
111#define MIB_COUNTERS_BASE(p) (0x1000 + ((p) << 7))
112#define DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(p) (0x1400 + ((p) << 10))
113#define DA_FILTER_OTHER_MULTICAST_TABLE_BASE(p) (0x1500 + ((p) << 10))
114#define DA_FILTER_UNICAST_TABLE_BASE(p) (0x1600 + ((p) << 10))
115
116/* These macros describe Ethernet Port configuration reg (Px_cR) bits */
117#define UNICAST_NORMAL_MODE (0 << 0)
118#define UNICAST_PROMISCUOUS_MODE (1 << 0)
119#define DEFAULT_RX_QUEUE(queue) ((queue) << 1)
120#define DEFAULT_RX_ARP_QUEUE(queue) ((queue) << 4)
121#define RECEIVE_BC_IF_NOT_IP_OR_ARP (0 << 7)
122#define REJECT_BC_IF_NOT_IP_OR_ARP (1 << 7)
123#define RECEIVE_BC_IF_IP (0 << 8)
124#define REJECT_BC_IF_IP (1 << 8)
125#define RECEIVE_BC_IF_ARP (0 << 9)
126#define REJECT_BC_IF_ARP (1 << 9)
127#define TX_AM_NO_UPDATE_ERROR_SUMMARY (1 << 12)
128#define CAPTURE_TCP_FRAMES_DIS (0 << 14)
129#define CAPTURE_TCP_FRAMES_EN (1 << 14)
130#define CAPTURE_UDP_FRAMES_DIS (0 << 15)
131#define CAPTURE_UDP_FRAMES_EN (1 << 15)
132#define DEFAULT_RX_TCP_QUEUE(queue) ((queue) << 16)
133#define DEFAULT_RX_UDP_QUEUE(queue) ((queue) << 19)
134#define DEFAULT_RX_BPDU_QUEUE(queue) ((queue) << 22)
135
136#define PORT_CONFIG_DEFAULT_VALUE \
137 UNICAST_NORMAL_MODE | \
138 DEFAULT_RX_QUEUE(0) | \
139 DEFAULT_RX_ARP_QUEUE(0) | \
140 RECEIVE_BC_IF_NOT_IP_OR_ARP | \
141 RECEIVE_BC_IF_IP | \
142 RECEIVE_BC_IF_ARP | \
143 CAPTURE_TCP_FRAMES_DIS | \
144 CAPTURE_UDP_FRAMES_DIS | \
145 DEFAULT_RX_TCP_QUEUE(0) | \
146 DEFAULT_RX_UDP_QUEUE(0) | \
147 DEFAULT_RX_BPDU_QUEUE(0)
148
149/* These macros describe Ethernet Port configuration extend reg (Px_cXR) bits*/
150#define CLASSIFY_EN (1 << 0)
151#define SPAN_BPDU_PACKETS_AS_NORMAL (0 << 1)
152#define SPAN_BPDU_PACKETS_TO_RX_QUEUE_7 (1 << 1)
153#define PARTITION_DISABLE (0 << 2)
154#define PARTITION_ENABLE (1 << 2)
155
156#define PORT_CONFIG_EXTEND_DEFAULT_VALUE \
157 SPAN_BPDU_PACKETS_AS_NORMAL | \
158 PARTITION_DISABLE
159
160/* These macros describe Ethernet Port Sdma configuration reg (SDCR) bits */
161#define RIFB (1 << 0)
162#define RX_BURST_SIZE_1_64BIT (0 << 1)
163#define RX_BURST_SIZE_2_64BIT (1 << 1)
164#define RX_BURST_SIZE_4_64BIT (2 << 1)
165#define RX_BURST_SIZE_8_64BIT (3 << 1)
166#define RX_BURST_SIZE_16_64BIT (4 << 1)
167#define BLM_RX_NO_SWAP (1 << 4)
168#define BLM_RX_BYTE_SWAP (0 << 4)
169#define BLM_TX_NO_SWAP (1 << 5)
170#define BLM_TX_BYTE_SWAP (0 << 5)
171#define DESCRIPTORS_BYTE_SWAP (1 << 6)
172#define DESCRIPTORS_NO_SWAP (0 << 6)
173#define IPG_INT_RX(value) (((value) & 0x3fff) << 8)
174#define TX_BURST_SIZE_1_64BIT (0 << 22)
175#define TX_BURST_SIZE_2_64BIT (1 << 22)
176#define TX_BURST_SIZE_4_64BIT (2 << 22)
177#define TX_BURST_SIZE_8_64BIT (3 << 22)
178#define TX_BURST_SIZE_16_64BIT (4 << 22)
179
180#if defined(__BIG_ENDIAN)
181#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
182 RX_BURST_SIZE_4_64BIT | \
183 IPG_INT_RX(0) | \
184 TX_BURST_SIZE_4_64BIT
185#elif defined(__LITTLE_ENDIAN)
186#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
187 RX_BURST_SIZE_4_64BIT | \
188 BLM_RX_NO_SWAP | \
189 BLM_TX_NO_SWAP | \
190 IPG_INT_RX(0) | \
191 TX_BURST_SIZE_4_64BIT
192#else
193#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
194#endif
195
196/* These macros describe Ethernet Port serial control reg (PSCR) bits */
197#define SERIAL_PORT_DISABLE (0 << 0)
198#define SERIAL_PORT_ENABLE (1 << 0)
199#define DO_NOT_FORCE_LINK_PASS (0 << 1)
200#define FORCE_LINK_PASS (1 << 1)
201#define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
202#define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
203#define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
204#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
205#define ADV_NO_FLOW_CTRL (0 << 4)
206#define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
207#define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
208#define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
209#define FORCE_BP_MODE_NO_JAM (0 << 7)
210#define FORCE_BP_MODE_JAM_TX (1 << 7)
211#define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
212#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
213#define FORCE_LINK_FAIL (0 << 10)
214#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
215#define RETRANSMIT_16_ATTEMPTS (0 << 11)
216#define RETRANSMIT_FOREVER (1 << 11)
217#define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
218#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
219#define DTE_ADV_0 (0 << 14)
220#define DTE_ADV_1 (1 << 14)
221#define DISABLE_AUTO_NEG_BYPASS (0 << 15)
222#define ENABLE_AUTO_NEG_BYPASS (1 << 15)
223#define AUTO_NEG_NO_CHANGE (0 << 16)
224#define RESTART_AUTO_NEG (1 << 16)
225#define MAX_RX_PACKET_1518BYTE (0 << 17)
226#define MAX_RX_PACKET_1522BYTE (1 << 17)
227#define MAX_RX_PACKET_1552BYTE (2 << 17)
228#define MAX_RX_PACKET_9022BYTE (3 << 17)
229#define MAX_RX_PACKET_9192BYTE (4 << 17)
230#define MAX_RX_PACKET_9700BYTE (5 << 17)
231#define MAX_RX_PACKET_MASK (7 << 17)
232#define CLR_EXT_LOOPBACK (0 << 20)
233#define SET_EXT_LOOPBACK (1 << 20)
234#define SET_HALF_DUPLEX_MODE (0 << 21)
235#define SET_FULL_DUPLEX_MODE (1 << 21)
236#define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
237#define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
238#define SET_GMII_SPEED_TO_10_100 (0 << 23)
239#define SET_GMII_SPEED_TO_1000 (1 << 23)
240#define SET_MII_SPEED_TO_10 (0 << 24)
241#define SET_MII_SPEED_TO_100 (1 << 24)
242
243#define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
244 DO_NOT_FORCE_LINK_PASS | \
245 ENABLE_AUTO_NEG_FOR_DUPLX | \
246 DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
247 ADV_SYMMETRIC_FLOW_CTRL | \
248 FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
249 FORCE_BP_MODE_NO_JAM | \
250 (1 << 9) /* reserved */ | \
251 DO_NOT_FORCE_LINK_FAIL | \
252 RETRANSMIT_16_ATTEMPTS | \
253 ENABLE_AUTO_NEG_SPEED_GMII | \
254 DTE_ADV_0 | \
255 DISABLE_AUTO_NEG_BYPASS | \
256 AUTO_NEG_NO_CHANGE | \
257 MAX_RX_PACKET_9700BYTE | \
258 CLR_EXT_LOOPBACK | \
259 SET_FULL_DUPLEX_MODE | \
260 ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
261
262/* These macros describe Ethernet Serial Status reg (PSR) bits */
263#define PORT_STATUS_MODE_10_BIT (1 << 0)
264#define PORT_STATUS_LINK_UP (1 << 1)
265#define PORT_STATUS_FULL_DUPLEX (1 << 2)
266#define PORT_STATUS_FLOW_CONTROL (1 << 3)
267#define PORT_STATUS_GMII_1000 (1 << 4)
268#define PORT_STATUS_MII_100 (1 << 5)
269/* PSR bit 6 is undocumented */
270#define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
271#define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
272#define PORT_STATUS_PARTITION (1 << 9)
273#define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
274/* PSR bits 11-31 are reserved */
275
276#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
277#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
278
279#define DESC_SIZE 64
280
281#define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
282#define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
283
284#define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
285#define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
286#define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
287#define ETH_INT_CAUSE_EXT 0x00000002
288#define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
289
290#define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
291#define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
292#define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
293#define ETH_INT_CAUSE_PHY 0x00010000
294#define ETH_INT_CAUSE_STATE 0x00100000
295#define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
296 ETH_INT_CAUSE_STATE)
297
298#define ETH_INT_MASK_ALL 0x00000000
299#define ETH_INT_MASK_ALL_EXT 0x00000000
300
301#define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
302#define PHY_WAIT_MICRO_SECONDS 10
303
304/* Buffer offset from buffer pointer */
305#define RX_BUF_OFFSET 0x2
306
307/* Gigabit Ethernet Unit Global Registers */
308
309/* MIB Counters register definitions */
310#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
311#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
312#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
313#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
314#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
315#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
316#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
317#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
318#define ETH_MIB_FRAMES_64_OCTETS 0x20
319#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
320#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
321#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
322#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
323#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
324#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
325#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
326#define ETH_MIB_GOOD_FRAMES_SENT 0x40
327#define ETH_MIB_EXCESSIVE_COLLISION 0x44
328#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
329#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
330#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
331#define ETH_MIB_FC_SENT 0x54
332#define ETH_MIB_GOOD_FC_RECEIVED 0x58
333#define ETH_MIB_BAD_FC_RECEIVED 0x5c
334#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
335#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
336#define ETH_MIB_OVERSIZE_RECEIVED 0x68
337#define ETH_MIB_JABBER_RECEIVED 0x6c
338#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
339#define ETH_MIB_BAD_CRC_EVENT 0x74
340#define ETH_MIB_COLLISION 0x78
341#define ETH_MIB_LATE_COLLISION 0x7c
342
343/* Port serial status reg (PSR) */
344#define ETH_INTERFACE_PCM 0x00000001
345#define ETH_LINK_IS_UP 0x00000002
346#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
347#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
348#define ETH_GMII_SPEED_1000 0x00000010
349#define ETH_MII_SPEED_100 0x00000020
350#define ETH_TX_IN_PROGRESS 0x00000080
351#define ETH_BYPASS_ACTIVE 0x00000100
352#define ETH_PORT_AT_PARTITION_STATE 0x00000200
353#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
354
355/* SMI reg */
356#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
357#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
358#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
359#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
360
361/* Interrupt Cause Register Bit Definitions */
362
363/* SDMA command status fields macros */
364
365/* Tx & Rx descriptors status */
366#define ETH_ERROR_SUMMARY 0x00000001
367
368/* Tx & Rx descriptors command */
369#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
370
371/* Tx descriptors status */
372#define ETH_LC_ERROR 0
373#define ETH_UR_ERROR 0x00000002
374#define ETH_RL_ERROR 0x00000004
375#define ETH_LLC_SNAP_FORMAT 0x00000200
376
377/* Rx descriptors status */
378#define ETH_OVERRUN_ERROR 0x00000002
379#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
380#define ETH_RESOURCE_ERROR 0x00000006
381#define ETH_VLAN_TAGGED 0x00080000
382#define ETH_BPDU_FRAME 0x00100000
383#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
384#define ETH_OTHER_FRAME_TYPE 0x00400000
385#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
386#define ETH_FRAME_TYPE_IP_V_4 0x01000000
387#define ETH_FRAME_HEADER_OK 0x02000000
388#define ETH_RX_LAST_DESC 0x04000000
389#define ETH_RX_FIRST_DESC 0x08000000
390#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
391#define ETH_RX_ENABLE_INTERRUPT 0x20000000
392#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
393
394/* Rx descriptors byte count */
395#define ETH_FRAME_FRAGMENTED 0x00000004
396
397/* Tx descriptors command */
398#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
399#define ETH_FRAME_SET_TO_VLAN 0x00008000
400#define ETH_UDP_FRAME 0x00010000
401#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
402#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
403#define ETH_ZERO_PADDING 0x00080000
404#define ETH_TX_LAST_DESC 0x00100000
405#define ETH_TX_FIRST_DESC 0x00200000
406#define ETH_GEN_CRC 0x00400000
407#define ETH_TX_ENABLE_INTERRUPT 0x00800000
408#define ETH_AUTO_MODE 0x40000000
409
410#define ETH_TX_IHL_SHIFT 11
411
412/* typedefs */
413
414typedef enum _eth_func_ret_status {
415 ETH_OK, /* Returned as expected. */
416 ETH_ERROR, /* Fundamental error. */
417 ETH_RETRY, /* Could not process request. Try later.*/
418 ETH_END_OF_JOB, /* Ring has nothing to process. */
419 ETH_QUEUE_FULL, /* Ring resource error. */
420 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
421} ETH_FUNC_RET_STATUS;
422
fbd6a754
LB
423/* These are for big-endian machines. Little endian needs different
424 * definitions.
425 */
426#if defined(__BIG_ENDIAN)
427struct eth_rx_desc {
428 u16 byte_cnt; /* Descriptor buffer byte count */
429 u16 buf_size; /* Buffer size */
430 u32 cmd_sts; /* Descriptor command status */
431 u32 next_desc_ptr; /* Next descriptor pointer */
432 u32 buf_ptr; /* Descriptor buffer pointer */
433};
434
435struct eth_tx_desc {
436 u16 byte_cnt; /* buffer byte count */
437 u16 l4i_chk; /* CPU provided TCP checksum */
438 u32 cmd_sts; /* Command/status field */
439 u32 next_desc_ptr; /* Pointer to next descriptor */
440 u32 buf_ptr; /* pointer to buffer for this descriptor*/
441};
442#elif defined(__LITTLE_ENDIAN)
443struct eth_rx_desc {
444 u32 cmd_sts; /* Descriptor command status */
445 u16 buf_size; /* Buffer size */
446 u16 byte_cnt; /* Descriptor buffer byte count */
447 u32 buf_ptr; /* Descriptor buffer pointer */
448 u32 next_desc_ptr; /* Next descriptor pointer */
449};
450
451struct eth_tx_desc {
452 u32 cmd_sts; /* Command/status field */
453 u16 l4i_chk; /* CPU provided TCP checksum */
454 u16 byte_cnt; /* buffer byte count */
455 u32 buf_ptr; /* pointer to buffer for this descriptor*/
456 u32 next_desc_ptr; /* Pointer to next descriptor */
457};
458#else
459#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
460#endif
461
462/* Unified struct for Rx and Tx operations. The user is not required to */
463/* be familier with neither Tx nor Rx descriptors. */
464struct pkt_info {
465 unsigned short byte_cnt; /* Descriptor buffer byte count */
466 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
467 unsigned int cmd_sts; /* Descriptor command status */
468 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
469 struct sk_buff *return_info; /* User resource return information */
470};
471
472/* Ethernet port specific information */
473struct mv643xx_mib_counters {
474 u64 good_octets_received;
475 u32 bad_octets_received;
476 u32 internal_mac_transmit_err;
477 u32 good_frames_received;
478 u32 bad_frames_received;
479 u32 broadcast_frames_received;
480 u32 multicast_frames_received;
481 u32 frames_64_octets;
482 u32 frames_65_to_127_octets;
483 u32 frames_128_to_255_octets;
484 u32 frames_256_to_511_octets;
485 u32 frames_512_to_1023_octets;
486 u32 frames_1024_to_max_octets;
487 u64 good_octets_sent;
488 u32 good_frames_sent;
489 u32 excessive_collision;
490 u32 multicast_frames_sent;
491 u32 broadcast_frames_sent;
492 u32 unrec_mac_control_received;
493 u32 fc_sent;
494 u32 good_fc_received;
495 u32 bad_fc_received;
496 u32 undersize_received;
497 u32 fragments_received;
498 u32 oversize_received;
499 u32 jabber_received;
500 u32 mac_receive_error;
501 u32 bad_crc_event;
502 u32 collision;
503 u32 late_collision;
504};
505
506struct mv643xx_private {
507 int port_num; /* User Ethernet port number */
508
509 u32 rx_sram_addr; /* Base address of rx sram area */
510 u32 rx_sram_size; /* Size of rx sram area */
511 u32 tx_sram_addr; /* Base address of tx sram area */
512 u32 tx_sram_size; /* Size of tx sram area */
513
514 int rx_resource_err; /* Rx ring resource error flag */
515
516 /* Tx/Rx rings managment indexes fields. For driver use */
517
518 /* Next available and first returning Rx resource */
519 int rx_curr_desc_q, rx_used_desc_q;
520
521 /* Next available and first returning Tx resource */
522 int tx_curr_desc_q, tx_used_desc_q;
523
524#ifdef MV643XX_TX_FAST_REFILL
525 u32 tx_clean_threshold;
526#endif
527
528 struct eth_rx_desc *p_rx_desc_area;
529 dma_addr_t rx_desc_dma;
530 int rx_desc_area_size;
531 struct sk_buff **rx_skb;
532
533 struct eth_tx_desc *p_tx_desc_area;
534 dma_addr_t tx_desc_dma;
535 int tx_desc_area_size;
536 struct sk_buff **tx_skb;
537
538 struct work_struct tx_timeout_task;
539
540 struct net_device *dev;
541 struct napi_struct napi;
542 struct net_device_stats stats;
543 struct mv643xx_mib_counters mib_counters;
544 spinlock_t lock;
545 /* Size of Tx Ring per queue */
546 int tx_ring_size;
547 /* Number of tx descriptors in use */
548 int tx_desc_count;
549 /* Size of Rx Ring per queue */
550 int rx_ring_size;
551 /* Number of rx descriptors in use */
552 int rx_desc_count;
553
554 /*
555 * Used in case RX Ring is empty, which can be caused when
556 * system does not have resources (skb's)
557 */
558 struct timer_list timeout;
559
560 u32 rx_int_coal;
561 u32 tx_int_coal;
562 struct mii_if_info mii;
563};
1da177e4 564
1da177e4 565/* Static function declarations */
fbd6a754
LB
566static void eth_port_init(struct mv643xx_private *mp);
567static void eth_port_reset(unsigned int eth_port_num);
568static void eth_port_start(struct net_device *dev);
569
570static void ethernet_phy_reset(unsigned int eth_port_num);
571
572static void eth_port_write_smi_reg(unsigned int eth_port_num,
573 unsigned int phy_reg, unsigned int value);
574
575static void eth_port_read_smi_reg(unsigned int eth_port_num,
576 unsigned int phy_reg, unsigned int *value);
577
578static void eth_clear_mib_counters(unsigned int eth_port_num);
579
580static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
581 struct pkt_info *p_pkt_info);
582static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
583 struct pkt_info *p_pkt_info);
584
144213d7
GP
585static void eth_port_uc_addr_get(unsigned int port_num, unsigned char *p_addr);
586static void eth_port_uc_addr_set(unsigned int port_num, unsigned char *p_addr);
16e03018 587static void eth_port_set_multicast_list(struct net_device *);
9f8dd319 588static void mv643xx_eth_port_enable_tx(unsigned int port_num,
12a87c64 589 unsigned int queues);
9f8dd319 590static void mv643xx_eth_port_enable_rx(unsigned int port_num,
12a87c64 591 unsigned int queues);
9f8dd319
DF
592static unsigned int mv643xx_eth_port_disable_tx(unsigned int port_num);
593static unsigned int mv643xx_eth_port_disable_rx(unsigned int port_num);
ab4384a6
DF
594static int mv643xx_eth_open(struct net_device *);
595static int mv643xx_eth_stop(struct net_device *);
1da177e4
LT
596static void eth_port_init_mac_tables(unsigned int eth_port_num);
597#ifdef MV643XX_NAPI
bea3348e 598static int mv643xx_poll(struct napi_struct *napi, int budget);
1da177e4 599#endif
c28a4f89 600static int ethernet_phy_get(unsigned int eth_port_num);
1da177e4
LT
601static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
602static int ethernet_phy_detect(unsigned int eth_port_num);
c28a4f89
JC
603static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location);
604static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val);
d0412d96 605static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
7282d491 606static const struct ethtool_ops mv643xx_ethtool_ops;
1da177e4
LT
607
608static char mv643xx_driver_name[] = "mv643xx_eth";
609static char mv643xx_driver_version[] = "1.0";
610
f9fbbc18 611static void __iomem *mv643xx_eth_base;
1da177e4 612
e4d00fa9 613/* used to protect SMI_REG, which is shared across ports */
a9f6a0dd 614static DEFINE_SPINLOCK(mv643xx_eth_phy_lock);
1da177e4
LT
615
616static inline u32 mv_read(int offset)
617{
f9fbbc18 618 return readl(mv643xx_eth_base + offset);
1da177e4
LT
619}
620
621static inline void mv_write(int offset, u32 data)
622{
f9fbbc18 623 writel(data, mv643xx_eth_base + offset);
1da177e4
LT
624}
625
626/*
627 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
628 *
629 * Input : pointer to ethernet interface network device structure
630 * new mtu size
631 * Output : 0 upon success, -EINVAL upon failure
632 */
633static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
634{
8f518703 635 if ((new_mtu > 9500) || (new_mtu < 64))
1da177e4 636 return -EINVAL;
1da177e4
LT
637
638 dev->mtu = new_mtu;
c0d0f2ca
LB
639 if (!netif_running(dev))
640 return 0;
641
1da177e4 642 /*
c0d0f2ca
LB
643 * Stop and then re-open the interface. This will allocate RX
644 * skbs of the new MTU.
645 * There is a possible danger that the open will not succeed,
646 * due to memory being full, which might fail the open function.
1da177e4 647 */
c0d0f2ca
LB
648 mv643xx_eth_stop(dev);
649 if (mv643xx_eth_open(dev)) {
650 printk(KERN_ERR "%s: Fatal error on opening device\n",
651 dev->name);
1da177e4
LT
652 }
653
1da177e4
LT
654 return 0;
655}
656
657/*
f78fb474 658 * mv643xx_eth_rx_refill_descs
1da177e4
LT
659 *
660 * Fills / refills RX queue on a certain gigabit ethernet port
661 *
662 * Input : pointer to ethernet interface network device structure
663 * Output : N/A
664 */
f78fb474 665static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
1da177e4 666{
1da177e4
LT
667 struct mv643xx_private *mp = netdev_priv(dev);
668 struct pkt_info pkt_info;
669 struct sk_buff *skb;
b44cd572 670 int unaligned;
1da177e4 671
f78fb474 672 while (mp->rx_desc_count < mp->rx_ring_size) {
908b637f 673 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
1da177e4
LT
674 if (!skb)
675 break;
f98e36f1 676 mp->rx_desc_count++;
908b637f 677 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
b44cd572 678 if (unaligned)
908b637f 679 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
1da177e4 680 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
7303fde8
DF
681 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
682 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
683 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
1da177e4
LT
684 pkt_info.return_info = skb;
685 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
686 printk(KERN_ERR
687 "%s: Error allocating RX Ring\n", dev->name);
688 break;
689 }
7303fde8 690 skb_reserve(skb, ETH_HW_IP_ALIGN);
1da177e4 691 }
1da177e4
LT
692 /*
693 * If RX ring is empty of SKB, set a timer to try allocating
f78fb474 694 * again at a later time.
1da177e4 695 */
f78fb474 696 if (mp->rx_desc_count == 0) {
1da177e4 697 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
f78fb474 698 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
1da177e4 699 add_timer(&mp->timeout);
1da177e4 700 }
1da177e4
LT
701}
702
703/*
f78fb474 704 * mv643xx_eth_rx_refill_descs_timer_wrapper
1da177e4
LT
705 *
706 * Timer routine to wake up RX queue filling task. This function is
707 * used only in case the RX queue is empty, and all alloc_skb has
708 * failed (due to out of memory event).
709 *
710 * Input : pointer to ethernet interface network device structure
711 * Output : N/A
712 */
f78fb474 713static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
1da177e4 714{
f78fb474 715 mv643xx_eth_rx_refill_descs((struct net_device *)data);
1da177e4
LT
716}
717
718/*
719 * mv643xx_eth_update_mac_address
720 *
721 * Update the MAC address of the port in the address table
722 *
723 * Input : pointer to ethernet interface network device structure
724 * Output : N/A
725 */
726static void mv643xx_eth_update_mac_address(struct net_device *dev)
727{
728 struct mv643xx_private *mp = netdev_priv(dev);
729 unsigned int port_num = mp->port_num;
730
731 eth_port_init_mac_tables(port_num);
ed9b5d45 732 eth_port_uc_addr_set(port_num, dev->dev_addr);
1da177e4
LT
733}
734
735/*
736 * mv643xx_eth_set_rx_mode
737 *
738 * Change from promiscuos to regular rx mode
739 *
740 * Input : pointer to ethernet interface network device structure
741 * Output : N/A
742 */
743static void mv643xx_eth_set_rx_mode(struct net_device *dev)
744{
745 struct mv643xx_private *mp = netdev_priv(dev);
01999873 746 u32 config_reg;
1da177e4 747
e4d00fa9 748 config_reg = mv_read(PORT_CONFIG_REG(mp->port_num));
1da177e4 749 if (dev->flags & IFF_PROMISC)
e4d00fa9 750 config_reg |= (u32) UNICAST_PROMISCUOUS_MODE;
1da177e4 751 else
e4d00fa9
LB
752 config_reg &= ~(u32) UNICAST_PROMISCUOUS_MODE;
753 mv_write(PORT_CONFIG_REG(mp->port_num), config_reg);
16e03018
DF
754
755 eth_port_set_multicast_list(dev);
1da177e4
LT
756}
757
758/*
759 * mv643xx_eth_set_mac_address
760 *
761 * Change the interface's mac address.
762 * No special hardware thing should be done because interface is always
763 * put in promiscuous mode.
764 *
765 * Input : pointer to ethernet interface network device structure and
766 * a pointer to the designated entry to be added to the cache.
767 * Output : zero upon success, negative upon failure
768 */
769static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
770{
771 int i;
772
773 for (i = 0; i < 6; i++)
774 /* +2 is for the offset of the HW addr type */
775 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
776 mv643xx_eth_update_mac_address(dev);
777 return 0;
778}
779
780/*
781 * mv643xx_eth_tx_timeout
782 *
783 * Called upon a timeout on transmitting a packet
784 *
785 * Input : pointer to ethernet interface network device structure.
786 * Output : N/A
787 */
788static void mv643xx_eth_tx_timeout(struct net_device *dev)
789{
790 struct mv643xx_private *mp = netdev_priv(dev);
791
792 printk(KERN_INFO "%s: TX timeout ", dev->name);
793
794 /* Do the reset outside of interrupt context */
795 schedule_work(&mp->tx_timeout_task);
796}
797
798/*
799 * mv643xx_eth_tx_timeout_task
800 *
801 * Actual routine to reset the adapter when a timeout on Tx has occurred
802 */
91c7c568 803static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
1da177e4 804{
91c7c568
AV
805 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
806 tx_timeout_task);
c0d0f2ca 807 struct net_device *dev = mp->dev;
1da177e4 808
94843566
DF
809 if (!netif_running(dev))
810 return;
811
812 netif_stop_queue(dev);
813
1da177e4 814 eth_port_reset(mp->port_num);
ed9b5d45 815 eth_port_start(dev);
94843566
DF
816
817 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
818 netif_wake_queue(dev);
1da177e4
LT
819}
820
ff561eef
DF
821/**
822 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1da177e4 823 *
ff561eef 824 * If force is non-zero, frees uncompleted descriptors as well
1da177e4 825 */
c0d0f2ca 826static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1da177e4
LT
827{
828 struct mv643xx_private *mp = netdev_priv(dev);
ff561eef
DF
829 struct eth_tx_desc *desc;
830 u32 cmd_sts;
831 struct sk_buff *skb;
832 unsigned long flags;
833 int tx_index;
834 dma_addr_t addr;
835 int count;
836 int released = 0;
1da177e4 837
ff561eef
DF
838 while (mp->tx_desc_count > 0) {
839 spin_lock_irqsave(&mp->lock, flags);
d344bff9
DF
840
841 /* tx_desc_count might have changed before acquiring the lock */
842 if (mp->tx_desc_count <= 0) {
843 spin_unlock_irqrestore(&mp->lock, flags);
844 return released;
845 }
846
ff561eef
DF
847 tx_index = mp->tx_used_desc_q;
848 desc = &mp->p_tx_desc_area[tx_index];
849 cmd_sts = desc->cmd_sts;
850
851 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
852 spin_unlock_irqrestore(&mp->lock, flags);
853 return released;
854 }
855
856 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
857 mp->tx_desc_count--;
858
859 addr = desc->buf_ptr;
860 count = desc->byte_cnt;
861 skb = mp->tx_skb[tx_index];
862 if (skb)
863 mp->tx_skb[tx_index] = NULL;
864
7303fde8 865 if (cmd_sts & ETH_ERROR_SUMMARY) {
1da177e4 866 printk("%s: Error in TX\n", dev->name);
09f75cd7 867 dev->stats.tx_errors++;
1da177e4
LT
868 }
869
d344bff9
DF
870 spin_unlock_irqrestore(&mp->lock, flags);
871
ff561eef
DF
872 if (cmd_sts & ETH_TX_FIRST_DESC)
873 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
cb415d30 874 else
ff561eef 875 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1da177e4 876
ff561eef
DF
877 if (skb)
878 dev_kfree_skb_irq(skb);
879
880 released = 1;
1da177e4
LT
881 }
882
1da177e4
LT
883 return released;
884}
885
ff561eef
DF
886static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
887{
888 struct mv643xx_private *mp = netdev_priv(dev);
889
890 if (mv643xx_eth_free_tx_descs(dev, 0) &&
891 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
892 netif_wake_queue(dev);
893}
894
895static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
896{
897 mv643xx_eth_free_tx_descs(dev, 1);
898}
899
1da177e4
LT
900/*
901 * mv643xx_eth_receive
902 *
903 * This function is forward packets that are received from the port's
904 * queues toward kernel core or FastRoute them to another interface.
905 *
906 * Input : dev - a pointer to the required interface
907 * max - maximum number to receive (0 means unlimted)
908 *
909 * Output : number of served packets
910 */
1da177e4 911static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
1da177e4
LT
912{
913 struct mv643xx_private *mp = netdev_priv(dev);
09f75cd7 914 struct net_device_stats *stats = &dev->stats;
1da177e4
LT
915 unsigned int received_packets = 0;
916 struct sk_buff *skb;
917 struct pkt_info pkt_info;
918
b1dd9ca1 919 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
54caf44d 920 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
71d28725 921 DMA_FROM_DEVICE);
f98e36f1 922 mp->rx_desc_count--;
1da177e4 923 received_packets++;
b1dd9ca1 924
468d09f8
DF
925 /*
926 * Update statistics.
927 * Note byte count includes 4 byte CRC count
928 */
1da177e4
LT
929 stats->rx_packets++;
930 stats->rx_bytes += pkt_info.byte_cnt;
931 skb = pkt_info.return_info;
932 /*
933 * In case received a packet without first / last bits on OR
934 * the error summary bit is on, the packets needs to be dropeed.
935 */
936 if (((pkt_info.cmd_sts
937 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
938 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
939 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
940 stats->rx_dropped++;
941 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
942 ETH_RX_LAST_DESC)) !=
943 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
944 if (net_ratelimit())
945 printk(KERN_ERR
946 "%s: Received packet spread "
947 "on multiple descriptors\n",
948 dev->name);
949 }
950 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
951 stats->rx_errors++;
952
953 dev_kfree_skb_irq(skb);
954 } else {
955 /*
956 * The -4 is for the CRC in the trailer of the
957 * received packet
958 */
959 skb_put(skb, pkt_info.byte_cnt - 4);
1da177e4
LT
960
961 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
962 skb->ip_summed = CHECKSUM_UNNECESSARY;
963 skb->csum = htons(
964 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
965 }
966 skb->protocol = eth_type_trans(skb, dev);
967#ifdef MV643XX_NAPI
968 netif_receive_skb(skb);
969#else
970 netif_rx(skb);
971#endif
972 }
12ad74f8 973 dev->last_rx = jiffies;
1da177e4 974 }
f78fb474 975 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
1da177e4
LT
976
977 return received_packets;
978}
979
d0412d96
JC
980/* Set the mv643xx port configuration register for the speed/duplex mode. */
981static void mv643xx_eth_update_pscr(struct net_device *dev,
982 struct ethtool_cmd *ecmd)
983{
984 struct mv643xx_private *mp = netdev_priv(dev);
985 int port_num = mp->port_num;
986 u32 o_pscr, n_pscr;
12a87c64 987 unsigned int queues;
d0412d96 988
e4d00fa9 989 o_pscr = mv_read(PORT_SERIAL_CONTROL_REG(port_num));
d0412d96
JC
990 n_pscr = o_pscr;
991
992 /* clear speed, duplex and rx buffer size fields */
e4d00fa9
LB
993 n_pscr &= ~(SET_MII_SPEED_TO_100 |
994 SET_GMII_SPEED_TO_1000 |
995 SET_FULL_DUPLEX_MODE |
996 MAX_RX_PACKET_MASK);
d0412d96
JC
997
998 if (ecmd->duplex == DUPLEX_FULL)
e4d00fa9 999 n_pscr |= SET_FULL_DUPLEX_MODE;
d0412d96
JC
1000
1001 if (ecmd->speed == SPEED_1000)
e4d00fa9
LB
1002 n_pscr |= SET_GMII_SPEED_TO_1000 |
1003 MAX_RX_PACKET_9700BYTE;
d0412d96
JC
1004 else {
1005 if (ecmd->speed == SPEED_100)
e4d00fa9
LB
1006 n_pscr |= SET_MII_SPEED_TO_100;
1007 n_pscr |= MAX_RX_PACKET_1522BYTE;
d0412d96
JC
1008 }
1009
1010 if (n_pscr != o_pscr) {
e4d00fa9
LB
1011 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
1012 mv_write(PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
d0412d96 1013 else {
12a87c64 1014 queues = mv643xx_eth_port_disable_tx(port_num);
d0412d96 1015
e4d00fa9
LB
1016 o_pscr &= ~SERIAL_PORT_ENABLE;
1017 mv_write(PORT_SERIAL_CONTROL_REG(port_num), o_pscr);
1018 mv_write(PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
1019 mv_write(PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
12a87c64
DF
1020 if (queues)
1021 mv643xx_eth_port_enable_tx(port_num, queues);
d0412d96
JC
1022 }
1023 }
1024}
1025
1da177e4
LT
1026/*
1027 * mv643xx_eth_int_handler
1028 *
1029 * Main interrupt handler for the gigbit ethernet ports
1030 *
1031 * Input : irq - irq number (not used)
1032 * dev_id - a pointer to the required interface's data structure
1033 * regs - not used
1034 * Output : N/A
1035 */
1036
7d12e780 1037static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1da177e4
LT
1038{
1039 struct net_device *dev = (struct net_device *)dev_id;
1040 struct mv643xx_private *mp = netdev_priv(dev);
1041 u32 eth_int_cause, eth_int_cause_ext = 0;
1042 unsigned int port_num = mp->port_num;
1043
1044 /* Read interrupt cause registers */
e4d00fa9 1045 eth_int_cause = mv_read(INTERRUPT_CAUSE_REG(port_num)) &
7303fde8 1046 ETH_INT_UNMASK_ALL;
468d09f8 1047 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
1da177e4 1048 eth_int_cause_ext = mv_read(
e4d00fa9 1049 INTERRUPT_CAUSE_EXTEND_REG(port_num)) &
7303fde8 1050 ETH_INT_UNMASK_ALL_EXT;
e4d00fa9 1051 mv_write(INTERRUPT_CAUSE_EXTEND_REG(port_num),
468d09f8 1052 ~eth_int_cause_ext);
1da177e4 1053 }
7303fde8 1054
1da177e4 1055 /* PHY status changed */
2bcff60f 1056 if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
d0412d96
JC
1057 struct ethtool_cmd cmd;
1058
c28a4f89 1059 if (mii_link_ok(&mp->mii)) {
d0412d96
JC
1060 mii_ethtool_gset(&mp->mii, &cmd);
1061 mv643xx_eth_update_pscr(dev, &cmd);
ff561eef
DF
1062 mv643xx_eth_port_enable_tx(port_num,
1063 ETH_TX_QUEUES_ENABLED);
c28a4f89
JC
1064 if (!netif_carrier_ok(dev)) {
1065 netif_carrier_on(dev);
ff561eef
DF
1066 if (mp->tx_ring_size - mp->tx_desc_count >=
1067 MAX_DESCS_PER_SKB)
d0412d96 1068 netif_wake_queue(dev);
c28a4f89
JC
1069 }
1070 } else if (netif_carrier_ok(dev)) {
1da177e4 1071 netif_stop_queue(dev);
c28a4f89 1072 netif_carrier_off(dev);
1da177e4
LT
1073 }
1074 }
1075
468d09f8
DF
1076#ifdef MV643XX_NAPI
1077 if (eth_int_cause & ETH_INT_CAUSE_RX) {
1078 /* schedule the NAPI poll routine to maintain port */
e4d00fa9
LB
1079 mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
1080
468d09f8 1081 /* wait for previous write to complete */
e4d00fa9 1082 mv_read(INTERRUPT_MASK_REG(port_num));
468d09f8 1083
bea3348e 1084 netif_rx_schedule(dev, &mp->napi);
468d09f8
DF
1085 }
1086#else
1087 if (eth_int_cause & ETH_INT_CAUSE_RX)
1088 mv643xx_eth_receive_queue(dev, INT_MAX);
5c537408 1089#endif
468d09f8
DF
1090 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
1091 mv643xx_eth_free_completed_tx_descs(dev);
468d09f8 1092
1da177e4
LT
1093 /*
1094 * If no real interrupt occured, exit.
1095 * This can happen when using gigE interrupt coalescing mechanism.
1096 */
1097 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
1098 return IRQ_NONE;
1099
1100 return IRQ_HANDLED;
1101}
1102
1103#ifdef MV643XX_COAL
1104
1105/*
1106 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
1107 *
1108 * DESCRIPTION:
1109 * This routine sets the RX coalescing interrupt mechanism parameter.
1110 * This parameter is a timeout counter, that counts in 64 t_clk
1111 * chunks ; that when timeout event occurs a maskable interrupt
1112 * occurs.
1113 * The parameter is calculated using the tClk of the MV-643xx chip
1114 * , and the required delay of the interrupt in usec.
1115 *
1116 * INPUT:
1117 * unsigned int eth_port_num Ethernet port number
1118 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
1119 * unsigned int delay Delay in usec
1120 *
1121 * OUTPUT:
1122 * Interrupt coalescing mechanism value is set in MV-643xx chip.
1123 *
1124 * RETURN:
1125 * The interrupt coalescing value set in the gigE port.
1126 *
1127 */
1128static unsigned int eth_port_set_rx_coal(unsigned int eth_port_num,
1129 unsigned int t_clk, unsigned int delay)
1130{
1131 unsigned int coal = ((t_clk / 1000000) * delay) / 64;
1132
1133 /* Set RX Coalescing mechanism */
e4d00fa9 1134 mv_write(SDMA_CONFIG_REG(eth_port_num),
1da177e4 1135 ((coal & 0x3fff) << 8) |
e4d00fa9 1136 (mv_read(SDMA_CONFIG_REG(eth_port_num))
1da177e4
LT
1137 & 0xffc000ff));
1138
1139 return coal;
1140}
1141#endif
1142
1143/*
1144 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
1145 *
1146 * DESCRIPTION:
1147 * This routine sets the TX coalescing interrupt mechanism parameter.
1148 * This parameter is a timeout counter, that counts in 64 t_clk
1149 * chunks ; that when timeout event occurs a maskable interrupt
1150 * occurs.
1151 * The parameter is calculated using the t_cLK frequency of the
1152 * MV-643xx chip and the required delay in the interrupt in uSec
1153 *
1154 * INPUT:
1155 * unsigned int eth_port_num Ethernet port number
1156 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
1157 * unsigned int delay Delay in uSeconds
1158 *
1159 * OUTPUT:
1160 * Interrupt coalescing mechanism value is set in MV-643xx chip.
1161 *
1162 * RETURN:
1163 * The interrupt coalescing value set in the gigE port.
1164 *
1165 */
1166static unsigned int eth_port_set_tx_coal(unsigned int eth_port_num,
1167 unsigned int t_clk, unsigned int delay)
1168{
1169 unsigned int coal;
1170 coal = ((t_clk / 1000000) * delay) / 64;
1171 /* Set TX Coalescing mechanism */
e4d00fa9 1172 mv_write(TX_FIFO_URGENT_THRESHOLD_REG(eth_port_num), coal << 4);
1da177e4
LT
1173 return coal;
1174}
1175
1da177e4
LT
1176/*
1177 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1178 *
1179 * DESCRIPTION:
1180 * This function prepares a Rx chained list of descriptors and packet
1181 * buffers in a form of a ring. The routine must be called after port
1182 * initialization routine and before port start routine.
1183 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1184 * devices in the system (i.e. DRAM). This function uses the ethernet
1185 * struct 'virtual to physical' routine (set by the user) to set the ring
1186 * with physical addresses.
1187 *
1188 * INPUT:
1189 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1190 *
1191 * OUTPUT:
1192 * The routine updates the Ethernet port control struct with information
1193 * regarding the Rx descriptors and buffers.
1194 *
1195 * RETURN:
1196 * None.
1197 */
1198static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1199{
1200 volatile struct eth_rx_desc *p_rx_desc;
1201 int rx_desc_num = mp->rx_ring_size;
1202 int i;
1203
1204 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1205 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1206 for (i = 0; i < rx_desc_num; i++) {
1207 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1208 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1209 }
1210
1211 /* Save Rx desc pointer to driver struct. */
1212 mp->rx_curr_desc_q = 0;
1213 mp->rx_used_desc_q = 0;
1214
1215 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1da177e4
LT
1216}
1217
1218/*
1219 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1220 *
1221 * DESCRIPTION:
1222 * This function prepares a Tx chained list of descriptors and packet
1223 * buffers in a form of a ring. The routine must be called after port
1224 * initialization routine and before port start routine.
1225 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1226 * devices in the system (i.e. DRAM). This function uses the ethernet
1227 * struct 'virtual to physical' routine (set by the user) to set the ring
1228 * with physical addresses.
1229 *
1230 * INPUT:
1231 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1232 *
1233 * OUTPUT:
1234 * The routine updates the Ethernet port control struct with information
1235 * regarding the Tx descriptors and buffers.
1236 *
1237 * RETURN:
1238 * None.
1239 */
1240static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1241{
1242 int tx_desc_num = mp->tx_ring_size;
1243 struct eth_tx_desc *p_tx_desc;
1244 int i;
1245
1246 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1247 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1248 for (i = 0; i < tx_desc_num; i++) {
1249 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1250 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1251 }
1252
1253 mp->tx_curr_desc_q = 0;
1254 mp->tx_used_desc_q = 0;
1da177e4
LT
1255
1256 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1da177e4
LT
1257}
1258
d0412d96
JC
1259static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1260{
1261 struct mv643xx_private *mp = netdev_priv(dev);
1262 int err;
1263
1264 spin_lock_irq(&mp->lock);
1265 err = mii_ethtool_sset(&mp->mii, cmd);
1266 spin_unlock_irq(&mp->lock);
1267
1268 return err;
1269}
1270
1271static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1272{
1273 struct mv643xx_private *mp = netdev_priv(dev);
1274 int err;
1275
1276 spin_lock_irq(&mp->lock);
1277 err = mii_ethtool_gset(&mp->mii, cmd);
1278 spin_unlock_irq(&mp->lock);
1279
1280 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1281 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1282 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1283
1284 return err;
1285}
1286
ab4384a6
DF
1287/*
1288 * mv643xx_eth_open
1289 *
1290 * This function is called when openning the network device. The function
1291 * should initialize all the hardware, initialize cyclic Rx/Tx
1292 * descriptors chain and buffers and allocate an IRQ to the network
1293 * device.
1294 *
1295 * Input : a pointer to the network device structure
1296 *
1297 * Output : zero of success , nonzero if fails.
1298 */
1299
1300static int mv643xx_eth_open(struct net_device *dev)
1da177e4
LT
1301{
1302 struct mv643xx_private *mp = netdev_priv(dev);
1303 unsigned int port_num = mp->port_num;
1304 unsigned int size;
ab4384a6
DF
1305 int err;
1306
85cf572c 1307 /* Clear any pending ethernet port interrupts */
e4d00fa9
LB
1308 mv_write(INTERRUPT_CAUSE_REG(port_num), 0);
1309 mv_write(INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
85cf572c 1310 /* wait for previous write to complete */
e4d00fa9 1311 mv_read (INTERRUPT_CAUSE_EXTEND_REG(port_num));
85cf572c 1312
ab4384a6 1313 err = request_irq(dev->irq, mv643xx_eth_int_handler,
1fb9df5d 1314 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
ab4384a6
DF
1315 if (err) {
1316 printk(KERN_ERR "Can not assign IRQ number to MV643XX_eth%d\n",
1317 port_num);
1318 return -EAGAIN;
1319 }
1da177e4 1320
1da177e4
LT
1321 eth_port_init(mp);
1322
1da177e4 1323 memset(&mp->timeout, 0, sizeof(struct timer_list));
f78fb474 1324 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
1da177e4
LT
1325 mp->timeout.data = (unsigned long)dev;
1326
1da177e4
LT
1327 /* Allocate RX and TX skb rings */
1328 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
1329 GFP_KERNEL);
1330 if (!mp->rx_skb) {
1331 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
ab4384a6
DF
1332 err = -ENOMEM;
1333 goto out_free_irq;
1da177e4
LT
1334 }
1335 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1336 GFP_KERNEL);
1337 if (!mp->tx_skb) {
1338 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
ab4384a6
DF
1339 err = -ENOMEM;
1340 goto out_free_rx_skb;
1da177e4
LT
1341 }
1342
1343 /* Allocate TX ring */
f98e36f1 1344 mp->tx_desc_count = 0;
1da177e4
LT
1345 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
1346 mp->tx_desc_area_size = size;
1347
1348 if (mp->tx_sram_size) {
1349 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
1350 mp->tx_sram_size);
1351 mp->tx_desc_dma = mp->tx_sram_addr;
1352 } else
1353 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
1354 &mp->tx_desc_dma,
1355 GFP_KERNEL);
1356
1357 if (!mp->p_tx_desc_area) {
1358 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1359 dev->name, size);
ab4384a6
DF
1360 err = -ENOMEM;
1361 goto out_free_tx_skb;
1da177e4
LT
1362 }
1363 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
1364 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
1365
1366 ether_init_tx_desc_ring(mp);
1367
1368 /* Allocate RX ring */
f98e36f1 1369 mp->rx_desc_count = 0;
1da177e4
LT
1370 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
1371 mp->rx_desc_area_size = size;
1372
1373 if (mp->rx_sram_size) {
1374 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
1375 mp->rx_sram_size);
1376 mp->rx_desc_dma = mp->rx_sram_addr;
1377 } else
1378 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
1379 &mp->rx_desc_dma,
1380 GFP_KERNEL);
1381
1382 if (!mp->p_rx_desc_area) {
1383 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
1384 dev->name, size);
1385 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
1386 dev->name);
1387 if (mp->rx_sram_size)
dd09b1de 1388 iounmap(mp->p_tx_desc_area);
1da177e4
LT
1389 else
1390 dma_free_coherent(NULL, mp->tx_desc_area_size,
1391 mp->p_tx_desc_area, mp->tx_desc_dma);
ab4384a6
DF
1392 err = -ENOMEM;
1393 goto out_free_tx_skb;
1da177e4
LT
1394 }
1395 memset((void *)mp->p_rx_desc_area, 0, size);
1396
1397 ether_init_rx_desc_ring(mp);
1398
f78fb474 1399 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
1da177e4 1400
bea3348e
SH
1401#ifdef MV643XX_NAPI
1402 napi_enable(&mp->napi);
1403#endif
1404
ed9b5d45 1405 eth_port_start(dev);
1da177e4
LT
1406
1407 /* Interrupt Coalescing */
1408
1409#ifdef MV643XX_COAL
1410 mp->rx_int_coal =
1411 eth_port_set_rx_coal(port_num, 133000000, MV643XX_RX_COAL);
1412#endif
1413
1414 mp->tx_int_coal =
1415 eth_port_set_tx_coal(port_num, 133000000, MV643XX_TX_COAL);
1416
8f518703 1417 /* Unmask phy and link status changes interrupts */
e4d00fa9 1418 mv_write(INTERRUPT_EXTEND_MASK_REG(port_num), ETH_INT_UNMASK_ALL_EXT);
1da177e4 1419
8f518703 1420 /* Unmask RX buffer and TX end interrupt */
e4d00fa9 1421 mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
d0412d96 1422
1da177e4 1423 return 0;
ab4384a6
DF
1424
1425out_free_tx_skb:
1426 kfree(mp->tx_skb);
1427out_free_rx_skb:
1428 kfree(mp->rx_skb);
1429out_free_irq:
1430 free_irq(dev->irq, dev);
1431
1432 return err;
1da177e4
LT
1433}
1434
1435static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1436{
1437 struct mv643xx_private *mp = netdev_priv(dev);
1da177e4
LT
1438
1439 /* Stop Tx Queues */
ff561eef 1440 mv643xx_eth_port_disable_tx(mp->port_num);
1da177e4 1441
ff561eef
DF
1442 /* Free outstanding skb's on TX ring */
1443 mv643xx_eth_free_all_tx_descs(dev);
1444
1445 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1da177e4
LT
1446
1447 /* Free TX ring */
1448 if (mp->tx_sram_size)
1449 iounmap(mp->p_tx_desc_area);
1450 else
1451 dma_free_coherent(NULL, mp->tx_desc_area_size,
1452 mp->p_tx_desc_area, mp->tx_desc_dma);
1453}
1454
1455static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1456{
1457 struct mv643xx_private *mp = netdev_priv(dev);
1458 unsigned int port_num = mp->port_num;
1459 int curr;
1460
1461 /* Stop RX Queues */
9f8dd319 1462 mv643xx_eth_port_disable_rx(port_num);
1da177e4
LT
1463
1464 /* Free preallocated skb's on RX rings */
f98e36f1 1465 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1da177e4
LT
1466 if (mp->rx_skb[curr]) {
1467 dev_kfree_skb(mp->rx_skb[curr]);
f98e36f1 1468 mp->rx_desc_count--;
1da177e4
LT
1469 }
1470 }
1471
f98e36f1 1472 if (mp->rx_desc_count)
1da177e4
LT
1473 printk(KERN_ERR
1474 "%s: Error in freeing Rx Ring. %d skb's still"
1475 " stuck in RX Ring - ignoring them\n", dev->name,
f98e36f1 1476 mp->rx_desc_count);
1da177e4
LT
1477 /* Free RX ring */
1478 if (mp->rx_sram_size)
1479 iounmap(mp->p_rx_desc_area);
1480 else
1481 dma_free_coherent(NULL, mp->rx_desc_area_size,
1482 mp->p_rx_desc_area, mp->rx_desc_dma);
1483}
1484
1485/*
1486 * mv643xx_eth_stop
1487 *
1488 * This function is used when closing the network device.
1489 * It updates the hardware,
1490 * release all memory that holds buffers and descriptors and release the IRQ.
1491 * Input : a pointer to the device structure
1492 * Output : zero if success , nonzero if fails
1493 */
1494
ab4384a6 1495static int mv643xx_eth_stop(struct net_device *dev)
1da177e4
LT
1496{
1497 struct mv643xx_private *mp = netdev_priv(dev);
1498 unsigned int port_num = mp->port_num;
1499
c2e5b352 1500 /* Mask all interrupts on ethernet port */
e4d00fa9 1501 mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
c2e5b352 1502 /* wait for previous write to complete */
e4d00fa9 1503 mv_read(INTERRUPT_MASK_REG(port_num));
8f518703
DF
1504
1505#ifdef MV643XX_NAPI
bea3348e 1506 napi_disable(&mp->napi);
8f518703 1507#endif
1da177e4
LT
1508 netif_carrier_off(dev);
1509 netif_stop_queue(dev);
1510
1da177e4
LT
1511 eth_port_reset(mp->port_num);
1512
8f518703
DF
1513 mv643xx_eth_free_tx_rings(dev);
1514 mv643xx_eth_free_rx_rings(dev);
1da177e4 1515
1da177e4 1516 free_irq(dev->irq, dev);
1da177e4
LT
1517
1518 return 0;
1519}
1520
1521#ifdef MV643XX_NAPI
1da177e4
LT
1522/*
1523 * mv643xx_poll
1524 *
1525 * This function is used in case of NAPI
1526 */
bea3348e 1527static int mv643xx_poll(struct napi_struct *napi, int budget)
1da177e4 1528{
bea3348e
SH
1529 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
1530 struct net_device *dev = mp->dev;
1da177e4 1531 unsigned int port_num = mp->port_num;
bea3348e 1532 int work_done;
1da177e4
LT
1533
1534#ifdef MV643XX_TX_FAST_REFILL
1535 if (++mp->tx_clean_threshold > 5) {
ff561eef 1536 mv643xx_eth_free_completed_tx_descs(dev);
1da177e4 1537 mp->tx_clean_threshold = 0;
1da177e4
LT
1538 }
1539#endif
1540
bea3348e 1541 work_done = 0;
e4d00fa9 1542 if ((mv_read(RX_CURRENT_QUEUE_DESC_PTR_0(port_num)))
bea3348e
SH
1543 != (u32) mp->rx_used_desc_q)
1544 work_done = mv643xx_eth_receive_queue(dev, budget);
1da177e4 1545
bea3348e
SH
1546 if (work_done < budget) {
1547 netif_rx_complete(dev, napi);
e4d00fa9
LB
1548 mv_write(INTERRUPT_CAUSE_REG(port_num), 0);
1549 mv_write(INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
1550 mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
1da177e4
LT
1551 }
1552
bea3348e 1553 return work_done;
1da177e4
LT
1554}
1555#endif
1556
c8aaea25
DF
1557/**
1558 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
1559 *
1560 * Hardware can't handle unaligned fragments smaller than 9 bytes.
f7ea3337
PJ
1561 * This helper function detects that case.
1562 */
1563
1564static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
1565{
b4de9051
DF
1566 unsigned int frag;
1567 skb_frag_t *fragp;
f7ea3337 1568
b4de9051
DF
1569 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1570 fragp = &skb_shinfo(skb)->frags[frag];
1571 if (fragp->size <= 8 && fragp->page_offset & 0x7)
1572 return 1;
1573 }
1574 return 0;
f7ea3337
PJ
1575}
1576
c8aaea25
DF
1577/**
1578 * eth_alloc_tx_desc_index - return the index of the next available tx desc
1579 */
1580static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
1581{
1582 int tx_desc_curr;
1583
c8aaea25 1584 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
c8aaea25 1585
ff561eef 1586 tx_desc_curr = mp->tx_curr_desc_q;
c8aaea25
DF
1587 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
1588
1589 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
1590
1591 return tx_desc_curr;
1592}
1593
1594/**
1595 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
1da177e4 1596 *
c8aaea25
DF
1597 * Ensure the data for each fragment to be transmitted is mapped properly,
1598 * then fill in descriptors in the tx hw queue.
1da177e4 1599 */
c8aaea25
DF
1600static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
1601 struct sk_buff *skb)
1da177e4 1602{
c8aaea25
DF
1603 int frag;
1604 int tx_index;
1605 struct eth_tx_desc *desc;
1da177e4 1606
c8aaea25
DF
1607 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1608 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1609
1610 tx_index = eth_alloc_tx_desc_index(mp);
1611 desc = &mp->p_tx_desc_area[tx_index];
1612
1613 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
1614 /* Last Frag enables interrupt and frees the skb */
1615 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
1616 desc->cmd_sts |= ETH_ZERO_PADDING |
1617 ETH_TX_LAST_DESC |
1618 ETH_TX_ENABLE_INTERRUPT;
1619 mp->tx_skb[tx_index] = skb;
1620 } else
05980775 1621 mp->tx_skb[tx_index] = NULL;
c8aaea25
DF
1622
1623 desc = &mp->p_tx_desc_area[tx_index];
1624 desc->l4i_chk = 0;
1625 desc->byte_cnt = this_frag->size;
1626 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
1627 this_frag->page_offset,
1628 this_frag->size,
1629 DMA_TO_DEVICE);
1da177e4 1630 }
c8aaea25 1631}
1da177e4 1632
324ff2c1
BB
1633static inline __be16 sum16_as_be(__sum16 sum)
1634{
1635 return (__force __be16)sum;
1636}
1637
c8aaea25
DF
1638/**
1639 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
1640 *
1641 * Ensure the data for an skb to be transmitted is mapped properly,
1642 * then fill in descriptors in the tx hw queue and start the hardware.
1643 */
ff561eef
DF
1644static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
1645 struct sk_buff *skb)
c8aaea25
DF
1646{
1647 int tx_index;
1648 struct eth_tx_desc *desc;
1649 u32 cmd_sts;
1650 int length;
ff561eef 1651 int nr_frags = skb_shinfo(skb)->nr_frags;
1da177e4 1652
c8aaea25 1653 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
1da177e4 1654
c8aaea25
DF
1655 tx_index = eth_alloc_tx_desc_index(mp);
1656 desc = &mp->p_tx_desc_area[tx_index];
1657
ff561eef 1658 if (nr_frags) {
c8aaea25
DF
1659 eth_tx_fill_frag_descs(mp, skb);
1660
1661 length = skb_headlen(skb);
05980775 1662 mp->tx_skb[tx_index] = NULL;
c8aaea25
DF
1663 } else {
1664 cmd_sts |= ETH_ZERO_PADDING |
1665 ETH_TX_LAST_DESC |
1666 ETH_TX_ENABLE_INTERRUPT;
1667 length = skb->len;
1668 mp->tx_skb[tx_index] = skb;
f7ea3337
PJ
1669 }
1670
c8aaea25
DF
1671 desc->byte_cnt = length;
1672 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
1da177e4 1673
84fa7933 1674 if (skb->ip_summed == CHECKSUM_PARTIAL) {
324ff2c1 1675 BUG_ON(skb->protocol != htons(ETH_P_IP));
c8aaea25
DF
1676
1677 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
1678 ETH_GEN_IP_V_4_CHECKSUM |
eddc9ec5 1679 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
c8aaea25 1680
eddc9ec5 1681 switch (ip_hdr(skb)->protocol) {
c8aaea25
DF
1682 case IPPROTO_UDP:
1683 cmd_sts |= ETH_UDP_FRAME;
324ff2c1 1684 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
c8aaea25
DF
1685 break;
1686 case IPPROTO_TCP:
324ff2c1 1687 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
c8aaea25
DF
1688 break;
1689 default:
1690 BUG();
1da177e4 1691 }
1da177e4 1692 } else {
c8aaea25
DF
1693 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1694 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1695 desc->l4i_chk = 0;
1696 }
1da177e4 1697
c8aaea25
DF
1698 /* ensure all other descriptors are written before first cmd_sts */
1699 wmb();
1700 desc->cmd_sts = cmd_sts;
1da177e4 1701
c8aaea25
DF
1702 /* ensure all descriptors are written before poking hardware */
1703 wmb();
ff561eef 1704 mv643xx_eth_port_enable_tx(mp->port_num, ETH_TX_QUEUES_ENABLED);
1da177e4 1705
ff561eef 1706 mp->tx_desc_count += nr_frags + 1;
c8aaea25 1707}
1da177e4 1708
c8aaea25
DF
1709/**
1710 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
1711 *
1712 */
1713static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1714{
1715 struct mv643xx_private *mp = netdev_priv(dev);
09f75cd7 1716 struct net_device_stats *stats = &dev->stats;
c8aaea25 1717 unsigned long flags;
1da177e4 1718
c8aaea25 1719 BUG_ON(netif_queue_stopped(dev));
94843566 1720
4d64e718
LB
1721 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1722 stats->tx_dropped++;
1723 printk(KERN_DEBUG "%s: failed to linearize tiny "
1724 "unaligned fragment\n", dev->name);
c0d0f2ca 1725 return NETDEV_TX_BUSY;
4d64e718
LB
1726 }
1727
1728 spin_lock_irqsave(&mp->lock, flags);
1729
94843566
DF
1730 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1731 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1732 netif_stop_queue(dev);
4d64e718 1733 spin_unlock_irqrestore(&mp->lock, flags);
c0d0f2ca 1734 return NETDEV_TX_BUSY;
94843566 1735 }
1da177e4 1736
ff561eef 1737 eth_tx_submit_descs_for_skb(mp, skb);
e7e381f6 1738 stats->tx_bytes += skb->len;
1da177e4
LT
1739 stats->tx_packets++;
1740 dev->trans_start = jiffies;
1741
c8aaea25
DF
1742 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1743 netif_stop_queue(dev);
1744
1da177e4
LT
1745 spin_unlock_irqrestore(&mp->lock, flags);
1746
c0d0f2ca 1747 return NETDEV_TX_OK;
1da177e4
LT
1748}
1749
63c9e549 1750#ifdef CONFIG_NET_POLL_CONTROLLER
63c9e549
DF
1751static void mv643xx_netpoll(struct net_device *netdev)
1752{
1753 struct mv643xx_private *mp = netdev_priv(netdev);
c2e5b352
DF
1754 int port_num = mp->port_num;
1755
e4d00fa9 1756 mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
c2e5b352 1757 /* wait for previous write to complete */
e4d00fa9 1758 mv_read(INTERRUPT_MASK_REG(port_num));
63c9e549 1759
9da3b1ad 1760 mv643xx_eth_int_handler(netdev->irq, netdev);
c2e5b352 1761
e4d00fa9 1762 mv_write(INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
63c9e549
DF
1763}
1764#endif
1765
d0412d96
JC
1766static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
1767 int speed, int duplex,
1768 struct ethtool_cmd *cmd)
1769{
1770 struct mv643xx_private *mp = netdev_priv(dev);
1771
1772 memset(cmd, 0, sizeof(*cmd));
1773
1774 cmd->port = PORT_MII;
1775 cmd->transceiver = XCVR_INTERNAL;
1776 cmd->phy_address = phy_address;
1777
1778 if (speed == 0) {
1779 cmd->autoneg = AUTONEG_ENABLE;
1780 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
1781 cmd->speed = SPEED_100;
1782 cmd->advertising = ADVERTISED_10baseT_Half |
1783 ADVERTISED_10baseT_Full |
1784 ADVERTISED_100baseT_Half |
1785 ADVERTISED_100baseT_Full;
1786 if (mp->mii.supports_gmii)
1787 cmd->advertising |= ADVERTISED_1000baseT_Full;
1788 } else {
1789 cmd->autoneg = AUTONEG_DISABLE;
1790 cmd->speed = speed;
1791 cmd->duplex = duplex;
1792 }
1793}
1794
1da177e4
LT
1795/*/
1796 * mv643xx_eth_probe
1797 *
1798 * First function called after registering the network device.
1799 * It's purpose is to initialize the device as an ethernet device,
1800 * fill the ethernet device structure with pointers * to functions,
1801 * and set the MAC address of the interface
1802 *
1803 * Input : struct device *
1804 * Output : -ENOMEM if failed , 0 if success
1805 */
3ae5eaec 1806static int mv643xx_eth_probe(struct platform_device *pdev)
1da177e4 1807{
1da177e4 1808 struct mv643xx_eth_platform_data *pd;
84dd619e 1809 int port_num;
1da177e4
LT
1810 struct mv643xx_private *mp;
1811 struct net_device *dev;
1812 u8 *p;
1813 struct resource *res;
1814 int err;
d0412d96 1815 struct ethtool_cmd cmd;
01999873
DF
1816 int duplex = DUPLEX_HALF;
1817 int speed = 0; /* default to auto-negotiation */
c5d6471f 1818 DECLARE_MAC_BUF(mac);
1da177e4 1819
84dd619e
DF
1820 pd = pdev->dev.platform_data;
1821 if (pd == NULL) {
1822 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
1823 return -ENODEV;
1824 }
1825
1da177e4
LT
1826 dev = alloc_etherdev(sizeof(struct mv643xx_private));
1827 if (!dev)
1828 return -ENOMEM;
1829
3ae5eaec 1830 platform_set_drvdata(pdev, dev);
1da177e4
LT
1831
1832 mp = netdev_priv(dev);
bea3348e
SH
1833 mp->dev = dev;
1834#ifdef MV643XX_NAPI
1835 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
1836#endif
1da177e4
LT
1837
1838 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1839 BUG_ON(!res);
1840 dev->irq = res->start;
1841
1da177e4
LT
1842 dev->open = mv643xx_eth_open;
1843 dev->stop = mv643xx_eth_stop;
1844 dev->hard_start_xmit = mv643xx_eth_start_xmit;
1da177e4
LT
1845 dev->set_mac_address = mv643xx_eth_set_mac_address;
1846 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
1847
1848 /* No need to Tx Timeout */
1849 dev->tx_timeout = mv643xx_eth_tx_timeout;
1da177e4 1850
63c9e549
DF
1851#ifdef CONFIG_NET_POLL_CONTROLLER
1852 dev->poll_controller = mv643xx_netpoll;
1853#endif
1854
1da177e4 1855 dev->watchdog_timeo = 2 * HZ;
1da177e4
LT
1856 dev->base_addr = 0;
1857 dev->change_mtu = mv643xx_eth_change_mtu;
d0412d96 1858 dev->do_ioctl = mv643xx_eth_do_ioctl;
1da177e4
LT
1859 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
1860
1861#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1862#ifdef MAX_SKB_FRAGS
1863 /*
1864 * Zero copy can only work if we use Discovery II memory. Else, we will
1865 * have to map the buffers to ISA memory which is only 16 MB
1866 */
63890576 1867 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
1da177e4
LT
1868#endif
1869#endif
1870
1871 /* Configure the timeout task */
91c7c568 1872 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
1da177e4
LT
1873
1874 spin_lock_init(&mp->lock);
1875
fadac406 1876 port_num = mp->port_num = pd->port_number;
84dd619e 1877
1da177e4 1878 /* set default config values */
144213d7 1879 eth_port_uc_addr_get(port_num, dev->dev_addr);
e4d00fa9
LB
1880 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
1881 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
1da177e4 1882
84dd619e
DF
1883 if (is_valid_ether_addr(pd->mac_addr))
1884 memcpy(dev->dev_addr, pd->mac_addr, 6);
1da177e4 1885
84dd619e
DF
1886 if (pd->phy_addr || pd->force_phy_addr)
1887 ethernet_phy_set(port_num, pd->phy_addr);
1da177e4 1888
84dd619e
DF
1889 if (pd->rx_queue_size)
1890 mp->rx_ring_size = pd->rx_queue_size;
1da177e4 1891
84dd619e
DF
1892 if (pd->tx_queue_size)
1893 mp->tx_ring_size = pd->tx_queue_size;
1da177e4 1894
84dd619e
DF
1895 if (pd->tx_sram_size) {
1896 mp->tx_sram_size = pd->tx_sram_size;
1897 mp->tx_sram_addr = pd->tx_sram_addr;
1898 }
01999873 1899
84dd619e
DF
1900 if (pd->rx_sram_size) {
1901 mp->rx_sram_size = pd->rx_sram_size;
1902 mp->rx_sram_addr = pd->rx_sram_addr;
1da177e4
LT
1903 }
1904
84dd619e
DF
1905 duplex = pd->duplex;
1906 speed = pd->speed;
1907
c28a4f89
JC
1908 /* Hook up MII support for ethtool */
1909 mp->mii.dev = dev;
1910 mp->mii.mdio_read = mv643xx_mdio_read;
1911 mp->mii.mdio_write = mv643xx_mdio_write;
1912 mp->mii.phy_id = ethernet_phy_get(port_num);
1913 mp->mii.phy_id_mask = 0x3f;
1914 mp->mii.reg_num_mask = 0x1f;
1915
1da177e4
LT
1916 err = ethernet_phy_detect(port_num);
1917 if (err) {
1918 pr_debug("MV643xx ethernet port %d: "
1919 "No PHY detected at addr %d\n",
1920 port_num, ethernet_phy_get(port_num));
d0412d96 1921 goto out;
1da177e4
LT
1922 }
1923
01999873 1924 ethernet_phy_reset(port_num);
c28a4f89 1925 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
d0412d96
JC
1926 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
1927 mv643xx_eth_update_pscr(dev, &cmd);
1928 mv643xx_set_settings(dev, &cmd);
c28a4f89 1929
b0b8dab2 1930 SET_NETDEV_DEV(dev, &pdev->dev);
1da177e4
LT
1931 err = register_netdev(dev);
1932 if (err)
1933 goto out;
1934
1935 p = dev->dev_addr;
1936 printk(KERN_NOTICE
0795af57
JP
1937 "%s: port %d with MAC address %s\n",
1938 dev->name, port_num, print_mac(mac, p));
1da177e4
LT
1939
1940 if (dev->features & NETIF_F_SG)
1941 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
1942
1943 if (dev->features & NETIF_F_IP_CSUM)
1944 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
1945 dev->name);
1946
1947#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1948 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
1949#endif
1950
1951#ifdef MV643XX_COAL
1952 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
1953 dev->name);
1954#endif
1955
1956#ifdef MV643XX_NAPI
1957 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
1958#endif
1959
b1529871
ND
1960 if (mp->tx_sram_size > 0)
1961 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
1962
1da177e4
LT
1963 return 0;
1964
1965out:
1966 free_netdev(dev);
1967
1968 return err;
1969}
1970
3ae5eaec 1971static int mv643xx_eth_remove(struct platform_device *pdev)
1da177e4 1972{
3ae5eaec 1973 struct net_device *dev = platform_get_drvdata(pdev);
1da177e4
LT
1974
1975 unregister_netdev(dev);
1976 flush_scheduled_work();
1977
1978 free_netdev(dev);
3ae5eaec 1979 platform_set_drvdata(pdev, NULL);
1da177e4
LT
1980 return 0;
1981}
1982
3ae5eaec 1983static int mv643xx_eth_shared_probe(struct platform_device *pdev)
1da177e4 1984{
1da177e4
LT
1985 struct resource *res;
1986
1987 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1988
1989 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1990 if (res == NULL)
1991 return -ENODEV;
1992
f9fbbc18
LB
1993 mv643xx_eth_base = ioremap(res->start, res->end - res->start + 1);
1994 if (mv643xx_eth_base == NULL)
1da177e4
LT
1995 return -ENOMEM;
1996
1997 return 0;
1998
1999}
2000
3ae5eaec 2001static int mv643xx_eth_shared_remove(struct platform_device *pdev)
1da177e4 2002{
f9fbbc18
LB
2003 iounmap(mv643xx_eth_base);
2004 mv643xx_eth_base = NULL;
1da177e4
LT
2005
2006 return 0;
2007}
2008
d57ab6fd
DF
2009static void mv643xx_eth_shutdown(struct platform_device *pdev)
2010{
2011 struct net_device *dev = platform_get_drvdata(pdev);
2012 struct mv643xx_private *mp = netdev_priv(dev);
2013 unsigned int port_num = mp->port_num;
2014
2015 /* Mask all interrupts on ethernet port */
e4d00fa9
LB
2016 mv_write(INTERRUPT_MASK_REG(port_num), 0);
2017 mv_read (INTERRUPT_MASK_REG(port_num));
d57ab6fd
DF
2018
2019 eth_port_reset(port_num);
2020}
2021
3ae5eaec 2022static struct platform_driver mv643xx_eth_driver = {
1da177e4
LT
2023 .probe = mv643xx_eth_probe,
2024 .remove = mv643xx_eth_remove,
d57ab6fd 2025 .shutdown = mv643xx_eth_shutdown,
3ae5eaec
RK
2026 .driver = {
2027 .name = MV643XX_ETH_NAME,
2028 },
1da177e4
LT
2029};
2030
3ae5eaec 2031static struct platform_driver mv643xx_eth_shared_driver = {
1da177e4
LT
2032 .probe = mv643xx_eth_shared_probe,
2033 .remove = mv643xx_eth_shared_remove,
3ae5eaec
RK
2034 .driver = {
2035 .name = MV643XX_ETH_SHARED_NAME,
2036 },
1da177e4
LT
2037};
2038
2039/*
2040 * mv643xx_init_module
2041 *
2042 * Registers the network drivers into the Linux kernel
2043 *
2044 * Input : N/A
2045 *
2046 * Output : N/A
2047 */
2048static int __init mv643xx_init_module(void)
2049{
2050 int rc;
2051
3ae5eaec 2052 rc = platform_driver_register(&mv643xx_eth_shared_driver);
1da177e4 2053 if (!rc) {
3ae5eaec 2054 rc = platform_driver_register(&mv643xx_eth_driver);
1da177e4 2055 if (rc)
3ae5eaec 2056 platform_driver_unregister(&mv643xx_eth_shared_driver);
1da177e4
LT
2057 }
2058 return rc;
2059}
2060
2061/*
2062 * mv643xx_cleanup_module
2063 *
2064 * Registers the network drivers into the Linux kernel
2065 *
2066 * Input : N/A
2067 *
2068 * Output : N/A
2069 */
2070static void __exit mv643xx_cleanup_module(void)
2071{
3ae5eaec
RK
2072 platform_driver_unregister(&mv643xx_eth_driver);
2073 platform_driver_unregister(&mv643xx_eth_shared_driver);
1da177e4
LT
2074}
2075
2076module_init(mv643xx_init_module);
2077module_exit(mv643xx_cleanup_module);
2078
2079MODULE_LICENSE("GPL");
2080MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2081 " and Dale Farnsworth");
2082MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
27c8bc2c 2083MODULE_ALIAS("platform:mv643xx_eth");
1da177e4
LT
2084
2085/*
2086 * The second part is the low level driver of the gigE ethernet ports.
2087 */
2088
2089/*
2090 * Marvell's Gigabit Ethernet controller low level driver
2091 *
2092 * DESCRIPTION:
2093 * This file introduce low level API to Marvell's Gigabit Ethernet
2094 * controller. This Gigabit Ethernet Controller driver API controls
2095 * 1) Operations (i.e. port init, start, reset etc').
2096 * 2) Data flow (i.e. port send, receive etc').
2097 * Each Gigabit Ethernet port is controlled via
2098 * struct mv643xx_private.
2099 * This struct includes user configuration information as well as
2100 * driver internal data needed for its operations.
2101 *
2102 * Supported Features:
2103 * - This low level driver is OS independent. Allocating memory for
2104 * the descriptor rings and buffers are not within the scope of
2105 * this driver.
2106 * - The user is free from Rx/Tx queue managing.
2107 * - This low level driver introduce functionality API that enable
2108 * the to operate Marvell's Gigabit Ethernet Controller in a
2109 * convenient way.
2110 * - Simple Gigabit Ethernet port operation API.
2111 * - Simple Gigabit Ethernet port data flow API.
2112 * - Data flow and operation API support per queue functionality.
2113 * - Support cached descriptors for better performance.
2114 * - Enable access to all four DRAM banks and internal SRAM memory
2115 * spaces.
2116 * - PHY access and control API.
2117 * - Port control register configuration API.
2118 * - Full control over Unicast and Multicast MAC configurations.
2119 *
2120 * Operation flow:
2121 *
2122 * Initialization phase
2123 * This phase complete the initialization of the the
2124 * mv643xx_private struct.
2125 * User information regarding port configuration has to be set
2126 * prior to calling the port initialization routine.
2127 *
2128 * In this phase any port Tx/Rx activity is halted, MIB counters
2129 * are cleared, PHY address is set according to user parameter and
2130 * access to DRAM and internal SRAM memory spaces.
2131 *
2132 * Driver ring initialization
2133 * Allocating memory for the descriptor rings and buffers is not
2134 * within the scope of this driver. Thus, the user is required to
2135 * allocate memory for the descriptors ring and buffers. Those
2136 * memory parameters are used by the Rx and Tx ring initialization
2137 * routines in order to curve the descriptor linked list in a form
2138 * of a ring.
2139 * Note: Pay special attention to alignment issues when using
2140 * cached descriptors/buffers. In this phase the driver store
2141 * information in the mv643xx_private struct regarding each queue
2142 * ring.
2143 *
2144 * Driver start
2145 * This phase prepares the Ethernet port for Rx and Tx activity.
2146 * It uses the information stored in the mv643xx_private struct to
2147 * initialize the various port registers.
2148 *
2149 * Data flow:
2150 * All packet references to/from the driver are done using
2151 * struct pkt_info.
2152 * This struct is a unified struct used with Rx and Tx operations.
2153 * This way the user is not required to be familiar with neither
2154 * Tx nor Rx descriptors structures.
2155 * The driver's descriptors rings are management by indexes.
2156 * Those indexes controls the ring resources and used to indicate
2157 * a SW resource error:
2158 * 'current'
2159 * This index points to the current available resource for use. For
2160 * example in Rx process this index will point to the descriptor
2161 * that will be passed to the user upon calling the receive
2162 * routine. In Tx process, this index will point to the descriptor
2163 * that will be assigned with the user packet info and transmitted.
2164 * 'used'
2165 * This index points to the descriptor that need to restore its
2166 * resources. For example in Rx process, using the Rx buffer return
2167 * API will attach the buffer returned in packet info to the
2168 * descriptor pointed by 'used'. In Tx process, using the Tx
2169 * descriptor return will merely return the user packet info with
2170 * the command status of the transmitted buffer pointed by the
2171 * 'used' index. Nevertheless, it is essential to use this routine
2172 * to update the 'used' index.
2173 * 'first'
2174 * This index supports Tx Scatter-Gather. It points to the first
2175 * descriptor of a packet assembled of multiple buffers. For
2176 * example when in middle of Such packet we have a Tx resource
2177 * error the 'curr' index get the value of 'first' to indicate
2178 * that the ring returned to its state before trying to transmit
2179 * this packet.
2180 *
2181 * Receive operation:
2182 * The eth_port_receive API set the packet information struct,
2183 * passed by the caller, with received information from the
2184 * 'current' SDMA descriptor.
2185 * It is the user responsibility to return this resource back
2186 * to the Rx descriptor ring to enable the reuse of this source.
2187 * Return Rx resource is done using the eth_rx_return_buff API.
2188 *
1da177e4
LT
2189 * Prior to calling the initialization routine eth_port_init() the user
2190 * must set the following fields under mv643xx_private struct:
2191 * port_num User Ethernet port number.
1da177e4
LT
2192 * port_config User port configuration value.
2193 * port_config_extend User port config extend value.
2194 * port_sdma_config User port SDMA config value.
2195 * port_serial_control User port serial control value.
2196 *
2197 * This driver data flow is done using the struct pkt_info which
2198 * is a unified struct for Rx and Tx operations:
2199 *
2200 * byte_cnt Tx/Rx descriptor buffer byte count.
2201 * l4i_chk CPU provided TCP Checksum. For Tx operation
2202 * only.
2203 * cmd_sts Tx/Rx descriptor command status.
2204 * buf_ptr Tx/Rx descriptor buffer pointer.
2205 * return_info Tx/Rx user resource return information.
2206 */
2207
1da177e4
LT
2208/* PHY routines */
2209static int ethernet_phy_get(unsigned int eth_port_num);
2210static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
2211
2212/* Ethernet Port routines */
cf4086c7 2213static void eth_port_set_filter_table_entry(int table, unsigned char entry);
1da177e4
LT
2214
2215/*
2216 * eth_port_init - Initialize the Ethernet port driver
2217 *
2218 * DESCRIPTION:
2219 * This function prepares the ethernet port to start its activity:
2220 * 1) Completes the ethernet port driver struct initialization toward port
2221 * start routine.
2222 * 2) Resets the device to a quiescent state in case of warm reboot.
2223 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2224 * 4) Clean MAC tables. The reset status of those tables is unknown.
2225 * 5) Set PHY address.
2226 * Note: Call this routine prior to eth_port_start routine and after
2227 * setting user values in the user fields of Ethernet port control
2228 * struct.
2229 *
2230 * INPUT:
2231 * struct mv643xx_private *mp Ethernet port control struct
2232 *
2233 * OUTPUT:
2234 * See description.
2235 *
2236 * RETURN:
2237 * None.
2238 */
2239static void eth_port_init(struct mv643xx_private *mp)
2240{
1da177e4 2241 mp->rx_resource_err = 0;
1da177e4
LT
2242
2243 eth_port_reset(mp->port_num);
2244
2245 eth_port_init_mac_tables(mp->port_num);
1da177e4
LT
2246}
2247
2248/*
2249 * eth_port_start - Start the Ethernet port activity.
2250 *
2251 * DESCRIPTION:
2252 * This routine prepares the Ethernet port for Rx and Tx activity:
2253 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2254 * has been initialized a descriptor's ring (using
2255 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2256 * 2. Initialize and enable the Ethernet configuration port by writing to
2257 * the port's configuration and command registers.
2258 * 3. Initialize and enable the SDMA by writing to the SDMA's
2259 * configuration and command registers. After completing these steps,
2260 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2261 *
2262 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2263 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2264 * and ether_init_rx_desc_ring for Rx queues).
2265 *
2266 * INPUT:
ed9b5d45 2267 * dev - a pointer to the required interface
1da177e4
LT
2268 *
2269 * OUTPUT:
2270 * Ethernet port is ready to receive and transmit.
2271 *
2272 * RETURN:
2273 * None.
2274 */
ed9b5d45 2275static void eth_port_start(struct net_device *dev)
1da177e4 2276{
ed9b5d45 2277 struct mv643xx_private *mp = netdev_priv(dev);
1da177e4
LT
2278 unsigned int port_num = mp->port_num;
2279 int tx_curr_desc, rx_curr_desc;
d0412d96
JC
2280 u32 pscr;
2281 struct ethtool_cmd ethtool_cmd;
1da177e4
LT
2282
2283 /* Assignment of Tx CTRP of given queue */
2284 tx_curr_desc = mp->tx_curr_desc_q;
e4d00fa9 2285 mv_write(TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
1da177e4
LT
2286 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2287
2288 /* Assignment of Rx CRDP of given queue */
2289 rx_curr_desc = mp->rx_curr_desc_q;
e4d00fa9 2290 mv_write(RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
1da177e4
LT
2291 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2292
2293 /* Add the assigned Ethernet address to the port's address table */
ed9b5d45 2294 eth_port_uc_addr_set(port_num, dev->dev_addr);
1da177e4
LT
2295
2296 /* Assign port configuration and command. */
e4d00fa9
LB
2297 mv_write(PORT_CONFIG_REG(port_num),
2298 PORT_CONFIG_DEFAULT_VALUE);
01999873 2299
e4d00fa9
LB
2300 mv_write(PORT_CONFIG_EXTEND_REG(port_num),
2301 PORT_CONFIG_EXTEND_DEFAULT_VALUE);
1da177e4 2302
e4d00fa9 2303 pscr = mv_read(PORT_SERIAL_CONTROL_REG(port_num));
01999873 2304
e4d00fa9
LB
2305 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
2306 mv_write(PORT_SERIAL_CONTROL_REG(port_num), pscr);
1da177e4 2307
e4d00fa9
LB
2308 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2309 DISABLE_AUTO_NEG_SPEED_GMII |
2310 DISABLE_AUTO_NEG_FOR_DUPLX |
2311 DO_NOT_FORCE_LINK_FAIL |
2312 SERIAL_PORT_CONTROL_RESERVED;
1da177e4 2313
e4d00fa9 2314 mv_write(PORT_SERIAL_CONTROL_REG(port_num), pscr);
1da177e4 2315
e4d00fa9
LB
2316 pscr |= SERIAL_PORT_ENABLE;
2317 mv_write(PORT_SERIAL_CONTROL_REG(port_num), pscr);
1da177e4
LT
2318
2319 /* Assign port SDMA configuration */
e4d00fa9
LB
2320 mv_write(SDMA_CONFIG_REG(port_num),
2321 PORT_SDMA_CONFIG_DEFAULT_VALUE);
1da177e4
LT
2322
2323 /* Enable port Rx. */
ff561eef 2324 mv643xx_eth_port_enable_rx(port_num, ETH_RX_QUEUES_ENABLED);
8f543718
DF
2325
2326 /* Disable port bandwidth limits by clearing MTU register */
e4d00fa9 2327 mv_write(MAXIMUM_TRANSMIT_UNIT(port_num), 0);
d0412d96
JC
2328
2329 /* save phy settings across reset */
2330 mv643xx_get_settings(dev, &ethtool_cmd);
2331 ethernet_phy_reset(mp->port_num);
2332 mv643xx_set_settings(dev, &ethtool_cmd);
1da177e4
LT
2333}
2334
2335/*
144213d7 2336 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1da177e4 2337 */
144213d7 2338static void eth_port_uc_addr_set(unsigned int port_num, unsigned char *p_addr)
1da177e4
LT
2339{
2340 unsigned int mac_h;
2341 unsigned int mac_l;
cf4086c7 2342 int table;
1da177e4
LT
2343
2344 mac_l = (p_addr[4] << 8) | (p_addr[5]);
2345 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
2346 (p_addr[3] << 0);
2347
e4d00fa9
LB
2348 mv_write(MAC_ADDR_LOW(port_num), mac_l);
2349 mv_write(MAC_ADDR_HIGH(port_num), mac_h);
1da177e4 2350
144213d7 2351 /* Accept frames with this address */
e4d00fa9 2352 table = DA_FILTER_UNICAST_TABLE_BASE(port_num);
cf4086c7 2353 eth_port_set_filter_table_entry(table, p_addr[5] & 0x0f);
1da177e4
LT
2354}
2355
2356/*
144213d7 2357 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1da177e4 2358 */
144213d7 2359static void eth_port_uc_addr_get(unsigned int port_num, unsigned char *p_addr)
1da177e4 2360{
1da177e4
LT
2361 unsigned int mac_h;
2362 unsigned int mac_l;
2363
e4d00fa9
LB
2364 mac_h = mv_read(MAC_ADDR_HIGH(port_num));
2365 mac_l = mv_read(MAC_ADDR_LOW(port_num));
1da177e4
LT
2366
2367 p_addr[0] = (mac_h >> 24) & 0xff;
2368 p_addr[1] = (mac_h >> 16) & 0xff;
2369 p_addr[2] = (mac_h >> 8) & 0xff;
2370 p_addr[3] = mac_h & 0xff;
2371 p_addr[4] = (mac_l >> 8) & 0xff;
2372 p_addr[5] = mac_l & 0xff;
2373}
2374
16e03018
DF
2375/*
2376 * The entries in each table are indexed by a hash of a packet's MAC
2377 * address. One bit in each entry determines whether the packet is
2378 * accepted. There are 4 entries (each 8 bits wide) in each register
2379 * of the table. The bits in each entry are defined as follows:
2380 * 0 Accept=1, Drop=0
2381 * 3-1 Queue (ETH_Q0=0)
2382 * 7-4 Reserved = 0;
2383 */
2384static void eth_port_set_filter_table_entry(int table, unsigned char entry)
2385{
2386 unsigned int table_reg;
2387 unsigned int tbl_offset;
2388 unsigned int reg_offset;
2389
2390 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
2391 reg_offset = entry % 4; /* Entry offset within the register */
2392
2393 /* Set "accepts frame bit" at specified table entry */
2394 table_reg = mv_read(table + tbl_offset);
2395 table_reg |= 0x01 << (8 * reg_offset);
2396 mv_write(table + tbl_offset, table_reg);
2397}
2398
2399/*
2400 * eth_port_mc_addr - Multicast address settings.
2401 *
2402 * The MV device supports multicast using two tables:
2403 * 1) Special Multicast Table for MAC addresses of the form
2404 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
2405 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2406 * Table entries in the DA-Filter table.
2407 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
2408 * is used as an index to the Other Multicast Table entries in the
2409 * DA-Filter table. This function calculates the CRC-8bit value.
2410 * In either case, eth_port_set_filter_table_entry() is then called
2411 * to set to set the actual table entry.
2412 */
2413static void eth_port_mc_addr(unsigned int eth_port_num, unsigned char *p_addr)
2414{
2415 unsigned int mac_h;
2416 unsigned int mac_l;
2417 unsigned char crc_result = 0;
2418 int table;
2419 int mac_array[48];
2420 int crc[8];
2421 int i;
2422
2423 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
2424 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
e4d00fa9 2425 table = DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
16e03018
DF
2426 (eth_port_num);
2427 eth_port_set_filter_table_entry(table, p_addr[5]);
2428 return;
2429 }
2430
2431 /* Calculate CRC-8 out of the given address */
2432 mac_h = (p_addr[0] << 8) | (p_addr[1]);
2433 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
2434 (p_addr[4] << 8) | (p_addr[5] << 0);
2435
2436 for (i = 0; i < 32; i++)
2437 mac_array[i] = (mac_l >> i) & 0x1;
2438 for (i = 32; i < 48; i++)
2439 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
2440
2441 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
2442 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
2443 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
2444 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
2445 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
2446
2447 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
2448 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
2449 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
2450 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
2451 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
2452 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
2453 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
2454
2455 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
2456 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
2457 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
2458 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
2459 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
2460 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
2461
2462 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
2463 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
2464 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
2465 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
2466 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
2467 mac_array[3] ^ mac_array[2] ^ mac_array[1];
2468
2469 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
2470 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
2471 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
2472 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
2473 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
2474 mac_array[3] ^ mac_array[2];
2475
2476 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
2477 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
2478 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
2479 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
2480 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
2481 mac_array[4] ^ mac_array[3];
2482
2483 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
2484 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
2485 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
2486 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
2487 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
2488 mac_array[4];
2489
2490 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
2491 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
2492 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
2493 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
2494 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
2495
2496 for (i = 0; i < 8; i++)
2497 crc_result = crc_result | (crc[i] << i);
2498
e4d00fa9 2499 table = DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num);
16e03018
DF
2500 eth_port_set_filter_table_entry(table, crc_result);
2501}
2502
2503/*
2504 * Set the entire multicast list based on dev->mc_list.
2505 */
2506static void eth_port_set_multicast_list(struct net_device *dev)
2507{
2508
2509 struct dev_mc_list *mc_list;
2510 int i;
2511 int table_index;
2512 struct mv643xx_private *mp = netdev_priv(dev);
2513 unsigned int eth_port_num = mp->port_num;
2514
2515 /* If the device is in promiscuous mode or in all multicast mode,
2516 * we will fully populate both multicast tables with accept.
2517 * This is guaranteed to yield a match on all multicast addresses...
2518 */
2519 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
2520 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
b4de9051
DF
2521 /* Set all entries in DA filter special multicast
2522 * table (Ex_dFSMT)
2523 * Set for ETH_Q0 for now
2524 * Bits
2525 * 0 Accept=1, Drop=0
2526 * 3-1 Queue ETH_Q0=0
2527 * 7-4 Reserved = 0;
2528 */
e4d00fa9 2529 mv_write(DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
b4de9051
DF
2530
2531 /* Set all entries in DA filter other multicast
2532 * table (Ex_dFOMT)
2533 * Set for ETH_Q0 for now
2534 * Bits
2535 * 0 Accept=1, Drop=0
2536 * 3-1 Queue ETH_Q0=0
2537 * 7-4 Reserved = 0;
2538 */
e4d00fa9 2539 mv_write(DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
b4de9051 2540 }
16e03018
DF
2541 return;
2542 }
2543
2544 /* We will clear out multicast tables every time we get the list.
2545 * Then add the entire new list...
2546 */
2547 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2548 /* Clear DA filter special multicast table (Ex_dFSMT) */
e4d00fa9 2549 mv_write(DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
16e03018
DF
2550 (eth_port_num) + table_index, 0);
2551
2552 /* Clear DA filter other multicast table (Ex_dFOMT) */
e4d00fa9 2553 mv_write(DA_FILTER_OTHER_MULTICAST_TABLE_BASE
16e03018
DF
2554 (eth_port_num) + table_index, 0);
2555 }
2556
2557 /* Get pointer to net_device multicast list and add each one... */
2558 for (i = 0, mc_list = dev->mc_list;
2559 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
2560 i++, mc_list = mc_list->next)
2561 if (mc_list->dmi_addrlen == 6)
2562 eth_port_mc_addr(eth_port_num, mc_list->dmi_addr);
2563}
2564
1da177e4
LT
2565/*
2566 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
2567 *
2568 * DESCRIPTION:
2569 * Go through all the DA filter tables (Unicast, Special Multicast &
2570 * Other Multicast) and set each entry to 0.
2571 *
2572 * INPUT:
2573 * unsigned int eth_port_num Ethernet Port number.
2574 *
2575 * OUTPUT:
2576 * Multicast and Unicast packets are rejected.
2577 *
2578 * RETURN:
2579 * None.
2580 */
2581static void eth_port_init_mac_tables(unsigned int eth_port_num)
2582{
2583 int table_index;
2584
2585 /* Clear DA filter unicast table (Ex_dFUT) */
2586 for (table_index = 0; table_index <= 0xC; table_index += 4)
e4d00fa9 2587 mv_write(DA_FILTER_UNICAST_TABLE_BASE
cf4086c7 2588 (eth_port_num) + table_index, 0);
1da177e4
LT
2589
2590 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2591 /* Clear DA filter special multicast table (Ex_dFSMT) */
e4d00fa9 2592 mv_write(DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
16e03018 2593 (eth_port_num) + table_index, 0);
1da177e4 2594 /* Clear DA filter other multicast table (Ex_dFOMT) */
e4d00fa9 2595 mv_write(DA_FILTER_OTHER_MULTICAST_TABLE_BASE
16e03018 2596 (eth_port_num) + table_index, 0);
1da177e4
LT
2597 }
2598}
2599
2600/*
2601 * eth_clear_mib_counters - Clear all MIB counters
2602 *
2603 * DESCRIPTION:
2604 * This function clears all MIB counters of a specific ethernet port.
2605 * A read from the MIB counter will reset the counter.
2606 *
2607 * INPUT:
2608 * unsigned int eth_port_num Ethernet Port number.
2609 *
2610 * OUTPUT:
2611 * After reading all MIB counters, the counters resets.
2612 *
2613 * RETURN:
2614 * MIB counter value.
2615 *
2616 */
2617static void eth_clear_mib_counters(unsigned int eth_port_num)
2618{
2619 int i;
2620
2621 /* Perform dummy reads from MIB counters */
2622 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
2623 i += 4)
e4d00fa9 2624 mv_read(MIB_COUNTERS_BASE(eth_port_num) + i);
1da177e4
LT
2625}
2626
2627static inline u32 read_mib(struct mv643xx_private *mp, int offset)
2628{
e4d00fa9 2629 return mv_read(MIB_COUNTERS_BASE(mp->port_num) + offset);
1da177e4
LT
2630}
2631
2632static void eth_update_mib_counters(struct mv643xx_private *mp)
2633{
2634 struct mv643xx_mib_counters *p = &mp->mib_counters;
2635 int offset;
2636
2637 p->good_octets_received +=
2638 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
2639 p->good_octets_received +=
2640 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
2641
2642 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
2643 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
2644 offset += 4)
70fbf327 2645 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1da177e4
LT
2646
2647 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
2648 p->good_octets_sent +=
2649 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
2650
2651 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
2652 offset <= ETH_MIB_LATE_COLLISION;
2653 offset += 4)
70fbf327 2654 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
1da177e4
LT
2655}
2656
2657/*
2658 * ethernet_phy_detect - Detect whether a phy is present
2659 *
2660 * DESCRIPTION:
2661 * This function tests whether there is a PHY present on
2662 * the specified port.
2663 *
2664 * INPUT:
2665 * unsigned int eth_port_num Ethernet Port number.
2666 *
2667 * OUTPUT:
2668 * None
2669 *
2670 * RETURN:
2671 * 0 on success
2672 * -ENODEV on failure
2673 *
2674 */
2675static int ethernet_phy_detect(unsigned int port_num)
2676{
2677 unsigned int phy_reg_data0;
2678 int auto_neg;
2679
2680 eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
2681 auto_neg = phy_reg_data0 & 0x1000;
2682 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2683 eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
2684
2685 eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
2686 if ((phy_reg_data0 & 0x1000) == auto_neg)
2687 return -ENODEV; /* change didn't take */
2688
2689 phy_reg_data0 ^= 0x1000;
2690 eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
2691 return 0;
2692}
2693
2694/*
2695 * ethernet_phy_get - Get the ethernet port PHY address.
2696 *
2697 * DESCRIPTION:
2698 * This routine returns the given ethernet port PHY address.
2699 *
2700 * INPUT:
2701 * unsigned int eth_port_num Ethernet Port number.
2702 *
2703 * OUTPUT:
2704 * None.
2705 *
2706 * RETURN:
2707 * PHY address.
2708 *
2709 */
2710static int ethernet_phy_get(unsigned int eth_port_num)
2711{
2712 unsigned int reg_data;
2713
e4d00fa9 2714 reg_data = mv_read(PHY_ADDR_REG);
1da177e4
LT
2715
2716 return ((reg_data >> (5 * eth_port_num)) & 0x1f);
2717}
2718
2719/*
2720 * ethernet_phy_set - Set the ethernet port PHY address.
2721 *
2722 * DESCRIPTION:
2723 * This routine sets the given ethernet port PHY address.
2724 *
2725 * INPUT:
2726 * unsigned int eth_port_num Ethernet Port number.
2727 * int phy_addr PHY address.
2728 *
2729 * OUTPUT:
2730 * None.
2731 *
2732 * RETURN:
2733 * None.
2734 *
2735 */
2736static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr)
2737{
2738 u32 reg_data;
2739 int addr_shift = 5 * eth_port_num;
2740
e4d00fa9 2741 reg_data = mv_read(PHY_ADDR_REG);
1da177e4
LT
2742 reg_data &= ~(0x1f << addr_shift);
2743 reg_data |= (phy_addr & 0x1f) << addr_shift;
e4d00fa9 2744 mv_write(PHY_ADDR_REG, reg_data);
1da177e4
LT
2745}
2746
2747/*
2748 * ethernet_phy_reset - Reset Ethernet port PHY.
2749 *
2750 * DESCRIPTION:
2751 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2752 *
2753 * INPUT:
2754 * unsigned int eth_port_num Ethernet Port number.
2755 *
2756 * OUTPUT:
2757 * The PHY is reset.
2758 *
2759 * RETURN:
2760 * None.
2761 *
2762 */
2763static void ethernet_phy_reset(unsigned int eth_port_num)
2764{
2765 unsigned int phy_reg_data;
2766
2767 /* Reset the PHY */
2768 eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
2769 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2770 eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data);
d0412d96
JC
2771
2772 /* wait for PHY to come out of reset */
2773 do {
2774 udelay(1);
2775 eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
2776 } while (phy_reg_data & 0x8000);
1da177e4
LT
2777}
2778
9f8dd319 2779static void mv643xx_eth_port_enable_tx(unsigned int port_num,
12a87c64 2780 unsigned int queues)
9f8dd319 2781{
e4d00fa9 2782 mv_write(TRANSMIT_QUEUE_COMMAND_REG(port_num), queues);
9f8dd319
DF
2783}
2784
2785static void mv643xx_eth_port_enable_rx(unsigned int port_num,
12a87c64 2786 unsigned int queues)
9f8dd319 2787{
e4d00fa9 2788 mv_write(RECEIVE_QUEUE_COMMAND_REG(port_num), queues);
9f8dd319
DF
2789}
2790
2791static unsigned int mv643xx_eth_port_disable_tx(unsigned int port_num)
2792{
12a87c64 2793 u32 queues;
9f8dd319
DF
2794
2795 /* Stop Tx port activity. Check port Tx activity. */
e4d00fa9 2796 queues = mv_read(TRANSMIT_QUEUE_COMMAND_REG(port_num)) & 0xFF;
12a87c64
DF
2797 if (queues) {
2798 /* Issue stop command for active queues only */
e4d00fa9 2799 mv_write(TRANSMIT_QUEUE_COMMAND_REG(port_num), (queues << 8));
9f8dd319
DF
2800
2801 /* Wait for all Tx activity to terminate. */
2802 /* Check port cause register that all Tx queues are stopped */
e4d00fa9 2803 while (mv_read(TRANSMIT_QUEUE_COMMAND_REG(port_num)) & 0xFF)
9f8dd319
DF
2804 udelay(PHY_WAIT_MICRO_SECONDS);
2805
2806 /* Wait for Tx FIFO to empty */
e4d00fa9 2807 while (mv_read(PORT_STATUS_REG(port_num)) &
9f8dd319
DF
2808 ETH_PORT_TX_FIFO_EMPTY)
2809 udelay(PHY_WAIT_MICRO_SECONDS);
2810 }
2811
12a87c64 2812 return queues;
9f8dd319
DF
2813}
2814
2815static unsigned int mv643xx_eth_port_disable_rx(unsigned int port_num)
2816{
12a87c64 2817 u32 queues;
9f8dd319
DF
2818
2819 /* Stop Rx port activity. Check port Rx activity. */
e4d00fa9 2820 queues = mv_read(RECEIVE_QUEUE_COMMAND_REG(port_num)) & 0xFF;
12a87c64
DF
2821 if (queues) {
2822 /* Issue stop command for active queues only */
e4d00fa9 2823 mv_write(RECEIVE_QUEUE_COMMAND_REG(port_num), (queues << 8));
9f8dd319
DF
2824
2825 /* Wait for all Rx activity to terminate. */
2826 /* Check port cause register that all Rx queues are stopped */
e4d00fa9 2827 while (mv_read(RECEIVE_QUEUE_COMMAND_REG(port_num)) & 0xFF)
9f8dd319
DF
2828 udelay(PHY_WAIT_MICRO_SECONDS);
2829 }
2830
12a87c64 2831 return queues;
9f8dd319
DF
2832}
2833
1da177e4
LT
2834/*
2835 * eth_port_reset - Reset Ethernet port
2836 *
2837 * DESCRIPTION:
2838 * This routine resets the chip by aborting any SDMA engine activity and
2839 * clearing the MIB counters. The Receiver and the Transmit unit are in
2840 * idle state after this command is performed and the port is disabled.
2841 *
2842 * INPUT:
2843 * unsigned int eth_port_num Ethernet Port number.
2844 *
2845 * OUTPUT:
2846 * Channel activity is halted.
2847 *
2848 * RETURN:
2849 * None.
2850 *
2851 */
2852static void eth_port_reset(unsigned int port_num)
2853{
2854 unsigned int reg_data;
2855
9f8dd319
DF
2856 mv643xx_eth_port_disable_tx(port_num);
2857 mv643xx_eth_port_disable_rx(port_num);
1da177e4
LT
2858
2859 /* Clear all MIB counters */
2860 eth_clear_mib_counters(port_num);
2861
2862 /* Reset the Enable bit in the Configuration Register */
e4d00fa9
LB
2863 reg_data = mv_read(PORT_SERIAL_CONTROL_REG(port_num));
2864 reg_data &= ~(SERIAL_PORT_ENABLE |
2865 DO_NOT_FORCE_LINK_FAIL |
2866 FORCE_LINK_PASS);
2867 mv_write(PORT_SERIAL_CONTROL_REG(port_num), reg_data);
1da177e4
LT
2868}
2869
1da177e4 2870
1da177e4
LT
2871/*
2872 * eth_port_read_smi_reg - Read PHY registers
2873 *
2874 * DESCRIPTION:
2875 * This routine utilize the SMI interface to interact with the PHY in
2876 * order to perform PHY register read.
2877 *
2878 * INPUT:
2879 * unsigned int port_num Ethernet Port number.
2880 * unsigned int phy_reg PHY register address offset.
2881 * unsigned int *value Register value buffer.
2882 *
2883 * OUTPUT:
2884 * Write the value of a specified PHY register into given buffer.
2885 *
2886 * RETURN:
2887 * false if the PHY is busy or read data is not in valid state.
2888 * true otherwise.
2889 *
2890 */
2891static void eth_port_read_smi_reg(unsigned int port_num,
2892 unsigned int phy_reg, unsigned int *value)
2893{
2894 int phy_addr = ethernet_phy_get(port_num);
2895 unsigned long flags;
2896 int i;
2897
2898 /* the SMI register is a shared resource */
2899 spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
2900
2901 /* wait for the SMI register to become available */
e4d00fa9 2902 for (i = 0; mv_read(SMI_REG) & ETH_SMI_BUSY; i++) {
1da177e4
LT
2903 if (i == PHY_WAIT_ITERATIONS) {
2904 printk("mv643xx PHY busy timeout, port %d\n", port_num);
2905 goto out;
2906 }
2907 udelay(PHY_WAIT_MICRO_SECONDS);
2908 }
2909
e4d00fa9 2910 mv_write(SMI_REG,
1da177e4
LT
2911 (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
2912
2913 /* now wait for the data to be valid */
e4d00fa9 2914 for (i = 0; !(mv_read(SMI_REG) & ETH_SMI_READ_VALID); i++) {
1da177e4
LT
2915 if (i == PHY_WAIT_ITERATIONS) {
2916 printk("mv643xx PHY read timeout, port %d\n", port_num);
2917 goto out;
2918 }
2919 udelay(PHY_WAIT_MICRO_SECONDS);
2920 }
2921
e4d00fa9 2922 *value = mv_read(SMI_REG) & 0xffff;
1da177e4
LT
2923out:
2924 spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
2925}
2926
2927/*
2928 * eth_port_write_smi_reg - Write to PHY registers
2929 *
2930 * DESCRIPTION:
2931 * This routine utilize the SMI interface to interact with the PHY in
2932 * order to perform writes to PHY registers.
2933 *
2934 * INPUT:
2935 * unsigned int eth_port_num Ethernet Port number.
2936 * unsigned int phy_reg PHY register address offset.
2937 * unsigned int value Register value.
2938 *
2939 * OUTPUT:
2940 * Write the given value to the specified PHY register.
2941 *
2942 * RETURN:
2943 * false if the PHY is busy.
2944 * true otherwise.
2945 *
2946 */
2947static void eth_port_write_smi_reg(unsigned int eth_port_num,
2948 unsigned int phy_reg, unsigned int value)
2949{
2950 int phy_addr;
2951 int i;
2952 unsigned long flags;
2953
2954 phy_addr = ethernet_phy_get(eth_port_num);
2955
2956 /* the SMI register is a shared resource */
2957 spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
2958
2959 /* wait for the SMI register to become available */
e4d00fa9 2960 for (i = 0; mv_read(SMI_REG) & ETH_SMI_BUSY; i++) {
1da177e4
LT
2961 if (i == PHY_WAIT_ITERATIONS) {
2962 printk("mv643xx PHY busy timeout, port %d\n",
2963 eth_port_num);
2964 goto out;
2965 }
2966 udelay(PHY_WAIT_MICRO_SECONDS);
2967 }
2968
e4d00fa9 2969 mv_write(SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
1da177e4
LT
2970 ETH_SMI_OPCODE_WRITE | (value & 0xffff));
2971out:
2972 spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
2973}
2974
c28a4f89
JC
2975/*
2976 * Wrappers for MII support library.
2977 */
2978static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2979{
2980 int val;
2981 struct mv643xx_private *mp = netdev_priv(dev);
2982
2983 eth_port_read_smi_reg(mp->port_num, location, &val);
2984 return val;
2985}
2986
2987static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2988{
2989 struct mv643xx_private *mp = netdev_priv(dev);
2990 eth_port_write_smi_reg(mp->port_num, location, val);
2991}
2992
1da177e4
LT
2993/*
2994 * eth_port_receive - Get received information from Rx ring.
2995 *
2996 * DESCRIPTION:
2997 * This routine returns the received data to the caller. There is no
2998 * data copying during routine operation. All information is returned
2999 * using pointer to packet information struct passed from the caller.
3000 * If the routine exhausts Rx ring resources then the resource error flag
3001 * is set.
3002 *
3003 * INPUT:
3004 * struct mv643xx_private *mp Ethernet Port Control srtuct.
3005 * struct pkt_info *p_pkt_info User packet buffer.
3006 *
3007 * OUTPUT:
3008 * Rx ring current and used indexes are updated.
3009 *
3010 * RETURN:
3011 * ETH_ERROR in case the routine can not access Rx desc ring.
3012 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
3013 * ETH_END_OF_JOB if there is no received data.
3014 * ETH_OK otherwise.
3015 */
3016static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
3017 struct pkt_info *p_pkt_info)
3018{
3019 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
3020 volatile struct eth_rx_desc *p_rx_desc;
3021 unsigned int command_status;
8f518703 3022 unsigned long flags;
1da177e4
LT
3023
3024 /* Do not process Rx ring in case of Rx ring resource error */
3025 if (mp->rx_resource_err)
3026 return ETH_QUEUE_FULL;
3027
8f518703
DF
3028 spin_lock_irqsave(&mp->lock, flags);
3029
1da177e4
LT
3030 /* Get the Rx Desc ring 'curr and 'used' indexes */
3031 rx_curr_desc = mp->rx_curr_desc_q;
3032 rx_used_desc = mp->rx_used_desc_q;
3033
3034 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
3035
3036 /* The following parameters are used to save readings from memory */
3037 command_status = p_rx_desc->cmd_sts;
3038 rmb();
3039
3040 /* Nothing to receive... */
8f518703
DF
3041 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
3042 spin_unlock_irqrestore(&mp->lock, flags);
1da177e4 3043 return ETH_END_OF_JOB;
8f518703 3044 }
1da177e4
LT
3045
3046 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
3047 p_pkt_info->cmd_sts = command_status;
3048 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
3049 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
3050 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
3051
b4de9051
DF
3052 /*
3053 * Clean the return info field to indicate that the
3054 * packet has been moved to the upper layers
3055 */
1da177e4
LT
3056 mp->rx_skb[rx_curr_desc] = NULL;
3057
3058 /* Update current index in data structure */
3059 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
3060 mp->rx_curr_desc_q = rx_next_curr_desc;
3061
3062 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
3063 if (rx_next_curr_desc == rx_used_desc)
3064 mp->rx_resource_err = 1;
3065
8f518703
DF
3066 spin_unlock_irqrestore(&mp->lock, flags);
3067
1da177e4
LT
3068 return ETH_OK;
3069}
3070
3071/*
3072 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
3073 *
3074 * DESCRIPTION:
3075 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
3076 * next 'used' descriptor and attached the returned buffer to it.
3077 * In case the Rx ring was in "resource error" condition, where there are
3078 * no available Rx resources, the function resets the resource error flag.
3079 *
3080 * INPUT:
3081 * struct mv643xx_private *mp Ethernet Port Control srtuct.
3082 * struct pkt_info *p_pkt_info Information on returned buffer.
3083 *
3084 * OUTPUT:
3085 * New available Rx resource in Rx descriptor ring.
3086 *
3087 * RETURN:
3088 * ETH_ERROR in case the routine can not access Rx desc ring.
3089 * ETH_OK otherwise.
3090 */
3091static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
3092 struct pkt_info *p_pkt_info)
3093{
3094 int used_rx_desc; /* Where to return Rx resource */
3095 volatile struct eth_rx_desc *p_used_rx_desc;
8f518703
DF
3096 unsigned long flags;
3097
3098 spin_lock_irqsave(&mp->lock, flags);
1da177e4
LT
3099
3100 /* Get 'used' Rx descriptor */
3101 used_rx_desc = mp->rx_used_desc_q;
3102 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
3103
3104 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
3105 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
3106 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
3107
3108 /* Flush the write pipe */
3109
3110 /* Return the descriptor to DMA ownership */
3111 wmb();
3112 p_used_rx_desc->cmd_sts =
3113 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
3114 wmb();
3115
3116 /* Move the used descriptor pointer to the next descriptor */
3117 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
3118
3119 /* Any Rx return cancels the Rx resource error status */
3120 mp->rx_resource_err = 0;
3121
8f518703
DF
3122 spin_unlock_irqrestore(&mp->lock, flags);
3123
1da177e4
LT
3124 return ETH_OK;
3125}
3126
3127/************* Begin ethtool support *************************/
3128
3129struct mv643xx_stats {
3130 char stat_string[ETH_GSTRING_LEN];
3131 int sizeof_stat;
3132 int stat_offset;
3133};
3134
7a6d84c1 3135#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
b4de9051 3136 offsetof(struct mv643xx_private, m)
1da177e4
LT
3137
3138static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
3139 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
3140 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
3141 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
3142 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
3143 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
3144 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
3145 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
3146 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
3147 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
3148 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
3149 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
3150 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
3151 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
3152 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
3153 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
3154 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
3155 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
3156 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
3157 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
3158 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
3159 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
3160 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
3161 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
3162 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
3163 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
3164 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
3165 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
3166 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
3167 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
3168 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
3169 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
3170 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
3171 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
3172 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
3173 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
3174 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
3175 { "collision", MV643XX_STAT(mib_counters.collision) },
3176 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
3177};
3178
ff8ac609 3179#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1da177e4 3180
b4de9051
DF
3181static void mv643xx_get_drvinfo(struct net_device *netdev,
3182 struct ethtool_drvinfo *drvinfo)
1da177e4
LT
3183{
3184 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
3185 strncpy(drvinfo->version, mv643xx_driver_version, 32);
3186 strncpy(drvinfo->fw_version, "N/A", 32);
3187 strncpy(drvinfo->bus_info, "mv643xx", 32);
3188 drvinfo->n_stats = MV643XX_STATS_LEN;
3189}
3190
b9f2c044 3191static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1da177e4 3192{
b9f2c044
JG
3193 switch (sset) {
3194 case ETH_SS_STATS:
3195 return MV643XX_STATS_LEN;
3196 default:
3197 return -EOPNOTSUPP;
3198 }
1da177e4
LT
3199}
3200
b4de9051
DF
3201static void mv643xx_get_ethtool_stats(struct net_device *netdev,
3202 struct ethtool_stats *stats, uint64_t *data)
1da177e4
LT
3203{
3204 struct mv643xx_private *mp = netdev->priv;
3205 int i;
3206
3207 eth_update_mib_counters(mp);
3208
b4de9051 3209 for (i = 0; i < MV643XX_STATS_LEN; i++) {
6aa20a22 3210 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
b4de9051 3211 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1da177e4
LT
3212 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
3213 }
3214}
3215
b4de9051
DF
3216static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
3217 uint8_t *data)
1da177e4
LT
3218{
3219 int i;
3220
3221 switch(stringset) {
3222 case ETH_SS_STATS:
3223 for (i=0; i < MV643XX_STATS_LEN; i++) {
b4de9051
DF
3224 memcpy(data + i * ETH_GSTRING_LEN,
3225 mv643xx_gstrings_stats[i].stat_string,
3226 ETH_GSTRING_LEN);
1da177e4
LT
3227 }
3228 break;
3229 }
3230}
3231
d0412d96
JC
3232static u32 mv643xx_eth_get_link(struct net_device *dev)
3233{
3234 struct mv643xx_private *mp = netdev_priv(dev);
3235
3236 return mii_link_ok(&mp->mii);
3237}
3238
3239static int mv643xx_eth_nway_restart(struct net_device *dev)
3240{
3241 struct mv643xx_private *mp = netdev_priv(dev);
3242
3243 return mii_nway_restart(&mp->mii);
3244}
3245
3246static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3247{
3248 struct mv643xx_private *mp = netdev_priv(dev);
3249
3250 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
3251}
3252
7282d491 3253static const struct ethtool_ops mv643xx_ethtool_ops = {
1da177e4 3254 .get_settings = mv643xx_get_settings,
d0412d96 3255 .set_settings = mv643xx_set_settings,
1da177e4 3256 .get_drvinfo = mv643xx_get_drvinfo,
d0412d96 3257 .get_link = mv643xx_eth_get_link,
1da177e4 3258 .set_sg = ethtool_op_set_sg,
194b2d0e 3259 .get_sset_count = mv643xx_get_sset_count,
1da177e4 3260 .get_ethtool_stats = mv643xx_get_ethtool_stats,
d0412d96 3261 .get_strings = mv643xx_get_strings,
d0412d96 3262 .nway_reset = mv643xx_eth_nway_restart,
1da177e4
LT
3263};
3264
3265/************* End ethtool support *************************/