blob: eebf0d288e36969ad8843ed67e26bd10e444878a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
37#include <linux/init.h>
38#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050039#include <linux/in.h>
40#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
44
45#include <linux/bitops.h>
46#include <linux/delay.h>
47#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010048#include <linux/platform_device.h>
49
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020050#include <linux/module.h>
51#include <linux/kernel.h>
52#include <linux/spinlock.h>
53#include <linux/workqueue.h>
54#include <linux/mii.h>
55
56#include <linux/mv643xx_eth.h>
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <asm/io.h>
59#include <asm/types.h>
60#include <asm/pgtable.h>
61#include <asm/system.h>
62#include <asm/delay.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020063#include <asm/dma-mapping.h>
64
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020065#define MV643XX_CHECKSUM_OFFLOAD_TX
66#define MV643XX_NAPI
67#define MV643XX_TX_FAST_REFILL
68#undef MV643XX_COAL
69
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020070#define MV643XX_TX_COAL 100
71#ifdef MV643XX_COAL
72#define MV643XX_RX_COAL 100
73#endif
74
75#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
76#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
77#else
78#define MAX_DESCS_PER_SKB 1
79#endif
80
81#define ETH_VLAN_HLEN 4
82#define ETH_FCS_LEN 4
83#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
84#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
85 ETH_VLAN_HLEN + ETH_FCS_LEN)
86#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
87 dma_get_cache_alignment())
88
89/*
90 * Registers shared between all ports.
91 */
92#define PHY_ADDR_REG 0x0000
93#define SMI_REG 0x0004
94
95/*
96 * Per-port registers.
97 */
98#define PORT_CONFIG_REG(p) (0x0400 + ((p) << 10))
99#define PORT_CONFIG_EXTEND_REG(p) (0x0404 + ((p) << 10))
100#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
101#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
102#define SDMA_CONFIG_REG(p) (0x041c + ((p) << 10))
103#define PORT_SERIAL_CONTROL_REG(p) (0x043c + ((p) << 10))
104#define PORT_STATUS_REG(p) (0x0444 + ((p) << 10))
105#define TRANSMIT_QUEUE_COMMAND_REG(p) (0x0448 + ((p) << 10))
106#define MAXIMUM_TRANSMIT_UNIT(p) (0x0458 + ((p) << 10))
107#define INTERRUPT_CAUSE_REG(p) (0x0460 + ((p) << 10))
108#define INTERRUPT_CAUSE_EXTEND_REG(p) (0x0464 + ((p) << 10))
109#define INTERRUPT_MASK_REG(p) (0x0468 + ((p) << 10))
110#define INTERRUPT_EXTEND_MASK_REG(p) (0x046c + ((p) << 10))
111#define TX_FIFO_URGENT_THRESHOLD_REG(p) (0x0474 + ((p) << 10))
112#define RX_CURRENT_QUEUE_DESC_PTR_0(p) (0x060c + ((p) << 10))
113#define RECEIVE_QUEUE_COMMAND_REG(p) (0x0680 + ((p) << 10))
114#define TX_CURRENT_QUEUE_DESC_PTR_0(p) (0x06c0 + ((p) << 10))
115#define MIB_COUNTERS_BASE(p) (0x1000 + ((p) << 7))
116#define DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(p) (0x1400 + ((p) << 10))
117#define DA_FILTER_OTHER_MULTICAST_TABLE_BASE(p) (0x1500 + ((p) << 10))
118#define DA_FILTER_UNICAST_TABLE_BASE(p) (0x1600 + ((p) << 10))
119
120/* These macros describe Ethernet Port configuration reg (Px_cR) bits */
121#define UNICAST_NORMAL_MODE (0 << 0)
122#define UNICAST_PROMISCUOUS_MODE (1 << 0)
123#define DEFAULT_RX_QUEUE(queue) ((queue) << 1)
124#define DEFAULT_RX_ARP_QUEUE(queue) ((queue) << 4)
125#define RECEIVE_BC_IF_NOT_IP_OR_ARP (0 << 7)
126#define REJECT_BC_IF_NOT_IP_OR_ARP (1 << 7)
127#define RECEIVE_BC_IF_IP (0 << 8)
128#define REJECT_BC_IF_IP (1 << 8)
129#define RECEIVE_BC_IF_ARP (0 << 9)
130#define REJECT_BC_IF_ARP (1 << 9)
131#define TX_AM_NO_UPDATE_ERROR_SUMMARY (1 << 12)
132#define CAPTURE_TCP_FRAMES_DIS (0 << 14)
133#define CAPTURE_TCP_FRAMES_EN (1 << 14)
134#define CAPTURE_UDP_FRAMES_DIS (0 << 15)
135#define CAPTURE_UDP_FRAMES_EN (1 << 15)
136#define DEFAULT_RX_TCP_QUEUE(queue) ((queue) << 16)
137#define DEFAULT_RX_UDP_QUEUE(queue) ((queue) << 19)
138#define DEFAULT_RX_BPDU_QUEUE(queue) ((queue) << 22)
139
140#define PORT_CONFIG_DEFAULT_VALUE \
141 UNICAST_NORMAL_MODE | \
142 DEFAULT_RX_QUEUE(0) | \
143 DEFAULT_RX_ARP_QUEUE(0) | \
144 RECEIVE_BC_IF_NOT_IP_OR_ARP | \
145 RECEIVE_BC_IF_IP | \
146 RECEIVE_BC_IF_ARP | \
147 CAPTURE_TCP_FRAMES_DIS | \
148 CAPTURE_UDP_FRAMES_DIS | \
149 DEFAULT_RX_TCP_QUEUE(0) | \
150 DEFAULT_RX_UDP_QUEUE(0) | \
151 DEFAULT_RX_BPDU_QUEUE(0)
152
153/* These macros describe Ethernet Port configuration extend reg (Px_cXR) bits*/
154#define CLASSIFY_EN (1 << 0)
155#define SPAN_BPDU_PACKETS_AS_NORMAL (0 << 1)
156#define SPAN_BPDU_PACKETS_TO_RX_QUEUE_7 (1 << 1)
157#define PARTITION_DISABLE (0 << 2)
158#define PARTITION_ENABLE (1 << 2)
159
160#define PORT_CONFIG_EXTEND_DEFAULT_VALUE \
161 SPAN_BPDU_PACKETS_AS_NORMAL | \
162 PARTITION_DISABLE
163
164/* These macros describe Ethernet Port Sdma configuration reg (SDCR) bits */
165#define RIFB (1 << 0)
166#define RX_BURST_SIZE_1_64BIT (0 << 1)
167#define RX_BURST_SIZE_2_64BIT (1 << 1)
168#define RX_BURST_SIZE_4_64BIT (2 << 1)
169#define RX_BURST_SIZE_8_64BIT (3 << 1)
170#define RX_BURST_SIZE_16_64BIT (4 << 1)
171#define BLM_RX_NO_SWAP (1 << 4)
172#define BLM_RX_BYTE_SWAP (0 << 4)
173#define BLM_TX_NO_SWAP (1 << 5)
174#define BLM_TX_BYTE_SWAP (0 << 5)
175#define DESCRIPTORS_BYTE_SWAP (1 << 6)
176#define DESCRIPTORS_NO_SWAP (0 << 6)
177#define IPG_INT_RX(value) (((value) & 0x3fff) << 8)
178#define TX_BURST_SIZE_1_64BIT (0 << 22)
179#define TX_BURST_SIZE_2_64BIT (1 << 22)
180#define TX_BURST_SIZE_4_64BIT (2 << 22)
181#define TX_BURST_SIZE_8_64BIT (3 << 22)
182#define TX_BURST_SIZE_16_64BIT (4 << 22)
183
184#if defined(__BIG_ENDIAN)
185#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
186 RX_BURST_SIZE_4_64BIT | \
187 IPG_INT_RX(0) | \
188 TX_BURST_SIZE_4_64BIT
189#elif defined(__LITTLE_ENDIAN)
190#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
191 RX_BURST_SIZE_4_64BIT | \
192 BLM_RX_NO_SWAP | \
193 BLM_TX_NO_SWAP | \
194 IPG_INT_RX(0) | \
195 TX_BURST_SIZE_4_64BIT
196#else
197#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
198#endif
199
200/* These macros describe Ethernet Port serial control reg (PSCR) bits */
201#define SERIAL_PORT_DISABLE (0 << 0)
202#define SERIAL_PORT_ENABLE (1 << 0)
203#define DO_NOT_FORCE_LINK_PASS (0 << 1)
204#define FORCE_LINK_PASS (1 << 1)
205#define ENABLE_AUTO_NEG_FOR_DUPLX (0 << 2)
206#define DISABLE_AUTO_NEG_FOR_DUPLX (1 << 2)
207#define ENABLE_AUTO_NEG_FOR_FLOW_CTRL (0 << 3)
208#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
209#define ADV_NO_FLOW_CTRL (0 << 4)
210#define ADV_SYMMETRIC_FLOW_CTRL (1 << 4)
211#define FORCE_FC_MODE_NO_PAUSE_DIS_TX (0 << 5)
212#define FORCE_FC_MODE_TX_PAUSE_DIS (1 << 5)
213#define FORCE_BP_MODE_NO_JAM (0 << 7)
214#define FORCE_BP_MODE_JAM_TX (1 << 7)
215#define FORCE_BP_MODE_JAM_TX_ON_RX_ERR (2 << 7)
216#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
217#define FORCE_LINK_FAIL (0 << 10)
218#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
219#define RETRANSMIT_16_ATTEMPTS (0 << 11)
220#define RETRANSMIT_FOREVER (1 << 11)
221#define ENABLE_AUTO_NEG_SPEED_GMII (0 << 13)
222#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
223#define DTE_ADV_0 (0 << 14)
224#define DTE_ADV_1 (1 << 14)
225#define DISABLE_AUTO_NEG_BYPASS (0 << 15)
226#define ENABLE_AUTO_NEG_BYPASS (1 << 15)
227#define AUTO_NEG_NO_CHANGE (0 << 16)
228#define RESTART_AUTO_NEG (1 << 16)
229#define MAX_RX_PACKET_1518BYTE (0 << 17)
230#define MAX_RX_PACKET_1522BYTE (1 << 17)
231#define MAX_RX_PACKET_1552BYTE (2 << 17)
232#define MAX_RX_PACKET_9022BYTE (3 << 17)
233#define MAX_RX_PACKET_9192BYTE (4 << 17)
234#define MAX_RX_PACKET_9700BYTE (5 << 17)
235#define MAX_RX_PACKET_MASK (7 << 17)
236#define CLR_EXT_LOOPBACK (0 << 20)
237#define SET_EXT_LOOPBACK (1 << 20)
238#define SET_HALF_DUPLEX_MODE (0 << 21)
239#define SET_FULL_DUPLEX_MODE (1 << 21)
240#define DISABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (0 << 22)
241#define ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX (1 << 22)
242#define SET_GMII_SPEED_TO_10_100 (0 << 23)
243#define SET_GMII_SPEED_TO_1000 (1 << 23)
244#define SET_MII_SPEED_TO_10 (0 << 24)
245#define SET_MII_SPEED_TO_100 (1 << 24)
246
247#define PORT_SERIAL_CONTROL_DEFAULT_VALUE \
248 DO_NOT_FORCE_LINK_PASS | \
249 ENABLE_AUTO_NEG_FOR_DUPLX | \
250 DISABLE_AUTO_NEG_FOR_FLOW_CTRL | \
251 ADV_SYMMETRIC_FLOW_CTRL | \
252 FORCE_FC_MODE_NO_PAUSE_DIS_TX | \
253 FORCE_BP_MODE_NO_JAM | \
254 (1 << 9) /* reserved */ | \
255 DO_NOT_FORCE_LINK_FAIL | \
256 RETRANSMIT_16_ATTEMPTS | \
257 ENABLE_AUTO_NEG_SPEED_GMII | \
258 DTE_ADV_0 | \
259 DISABLE_AUTO_NEG_BYPASS | \
260 AUTO_NEG_NO_CHANGE | \
261 MAX_RX_PACKET_9700BYTE | \
262 CLR_EXT_LOOPBACK | \
263 SET_FULL_DUPLEX_MODE | \
264 ENABLE_FLOW_CTRL_TX_RX_IN_FULL_DUPLEX
265
266/* These macros describe Ethernet Serial Status reg (PSR) bits */
267#define PORT_STATUS_MODE_10_BIT (1 << 0)
268#define PORT_STATUS_LINK_UP (1 << 1)
269#define PORT_STATUS_FULL_DUPLEX (1 << 2)
270#define PORT_STATUS_FLOW_CONTROL (1 << 3)
271#define PORT_STATUS_GMII_1000 (1 << 4)
272#define PORT_STATUS_MII_100 (1 << 5)
273/* PSR bit 6 is undocumented */
274#define PORT_STATUS_TX_IN_PROGRESS (1 << 7)
275#define PORT_STATUS_AUTONEG_BYPASSED (1 << 8)
276#define PORT_STATUS_PARTITION (1 << 9)
277#define PORT_STATUS_TX_FIFO_EMPTY (1 << 10)
278/* PSR bits 11-31 are reserved */
279
280#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
281#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
282
283#define DESC_SIZE 64
284
285#define ETH_RX_QUEUES_ENABLED (1 << 0) /* use only Q0 for receive */
286#define ETH_TX_QUEUES_ENABLED (1 << 0) /* use only Q0 for transmit */
287
288#define ETH_INT_CAUSE_RX_DONE (ETH_RX_QUEUES_ENABLED << 2)
289#define ETH_INT_CAUSE_RX_ERROR (ETH_RX_QUEUES_ENABLED << 9)
290#define ETH_INT_CAUSE_RX (ETH_INT_CAUSE_RX_DONE | ETH_INT_CAUSE_RX_ERROR)
291#define ETH_INT_CAUSE_EXT 0x00000002
292#define ETH_INT_UNMASK_ALL (ETH_INT_CAUSE_RX | ETH_INT_CAUSE_EXT)
293
294#define ETH_INT_CAUSE_TX_DONE (ETH_TX_QUEUES_ENABLED << 0)
295#define ETH_INT_CAUSE_TX_ERROR (ETH_TX_QUEUES_ENABLED << 8)
296#define ETH_INT_CAUSE_TX (ETH_INT_CAUSE_TX_DONE | ETH_INT_CAUSE_TX_ERROR)
297#define ETH_INT_CAUSE_PHY 0x00010000
298#define ETH_INT_CAUSE_STATE 0x00100000
299#define ETH_INT_UNMASK_ALL_EXT (ETH_INT_CAUSE_TX | ETH_INT_CAUSE_PHY | \
300 ETH_INT_CAUSE_STATE)
301
302#define ETH_INT_MASK_ALL 0x00000000
303#define ETH_INT_MASK_ALL_EXT 0x00000000
304
305#define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */
306#define PHY_WAIT_MICRO_SECONDS 10
307
308/* Buffer offset from buffer pointer */
309#define RX_BUF_OFFSET 0x2
310
311/* Gigabit Ethernet Unit Global Registers */
312
313/* MIB Counters register definitions */
314#define ETH_MIB_GOOD_OCTETS_RECEIVED_LOW 0x0
315#define ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH 0x4
316#define ETH_MIB_BAD_OCTETS_RECEIVED 0x8
317#define ETH_MIB_INTERNAL_MAC_TRANSMIT_ERR 0xc
318#define ETH_MIB_GOOD_FRAMES_RECEIVED 0x10
319#define ETH_MIB_BAD_FRAMES_RECEIVED 0x14
320#define ETH_MIB_BROADCAST_FRAMES_RECEIVED 0x18
321#define ETH_MIB_MULTICAST_FRAMES_RECEIVED 0x1c
322#define ETH_MIB_FRAMES_64_OCTETS 0x20
323#define ETH_MIB_FRAMES_65_TO_127_OCTETS 0x24
324#define ETH_MIB_FRAMES_128_TO_255_OCTETS 0x28
325#define ETH_MIB_FRAMES_256_TO_511_OCTETS 0x2c
326#define ETH_MIB_FRAMES_512_TO_1023_OCTETS 0x30
327#define ETH_MIB_FRAMES_1024_TO_MAX_OCTETS 0x34
328#define ETH_MIB_GOOD_OCTETS_SENT_LOW 0x38
329#define ETH_MIB_GOOD_OCTETS_SENT_HIGH 0x3c
330#define ETH_MIB_GOOD_FRAMES_SENT 0x40
331#define ETH_MIB_EXCESSIVE_COLLISION 0x44
332#define ETH_MIB_MULTICAST_FRAMES_SENT 0x48
333#define ETH_MIB_BROADCAST_FRAMES_SENT 0x4c
334#define ETH_MIB_UNREC_MAC_CONTROL_RECEIVED 0x50
335#define ETH_MIB_FC_SENT 0x54
336#define ETH_MIB_GOOD_FC_RECEIVED 0x58
337#define ETH_MIB_BAD_FC_RECEIVED 0x5c
338#define ETH_MIB_UNDERSIZE_RECEIVED 0x60
339#define ETH_MIB_FRAGMENTS_RECEIVED 0x64
340#define ETH_MIB_OVERSIZE_RECEIVED 0x68
341#define ETH_MIB_JABBER_RECEIVED 0x6c
342#define ETH_MIB_MAC_RECEIVE_ERROR 0x70
343#define ETH_MIB_BAD_CRC_EVENT 0x74
344#define ETH_MIB_COLLISION 0x78
345#define ETH_MIB_LATE_COLLISION 0x7c
346
347/* Port serial status reg (PSR) */
348#define ETH_INTERFACE_PCM 0x00000001
349#define ETH_LINK_IS_UP 0x00000002
350#define ETH_PORT_AT_FULL_DUPLEX 0x00000004
351#define ETH_RX_FLOW_CTRL_ENABLED 0x00000008
352#define ETH_GMII_SPEED_1000 0x00000010
353#define ETH_MII_SPEED_100 0x00000020
354#define ETH_TX_IN_PROGRESS 0x00000080
355#define ETH_BYPASS_ACTIVE 0x00000100
356#define ETH_PORT_AT_PARTITION_STATE 0x00000200
357#define ETH_PORT_TX_FIFO_EMPTY 0x00000400
358
359/* SMI reg */
360#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
361#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
362#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
363#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
364
365/* Interrupt Cause Register Bit Definitions */
366
367/* SDMA command status fields macros */
368
369/* Tx & Rx descriptors status */
370#define ETH_ERROR_SUMMARY 0x00000001
371
372/* Tx & Rx descriptors command */
373#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
374
375/* Tx descriptors status */
376#define ETH_LC_ERROR 0
377#define ETH_UR_ERROR 0x00000002
378#define ETH_RL_ERROR 0x00000004
379#define ETH_LLC_SNAP_FORMAT 0x00000200
380
381/* Rx descriptors status */
382#define ETH_OVERRUN_ERROR 0x00000002
383#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
384#define ETH_RESOURCE_ERROR 0x00000006
385#define ETH_VLAN_TAGGED 0x00080000
386#define ETH_BPDU_FRAME 0x00100000
387#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
388#define ETH_OTHER_FRAME_TYPE 0x00400000
389#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
390#define ETH_FRAME_TYPE_IP_V_4 0x01000000
391#define ETH_FRAME_HEADER_OK 0x02000000
392#define ETH_RX_LAST_DESC 0x04000000
393#define ETH_RX_FIRST_DESC 0x08000000
394#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
395#define ETH_RX_ENABLE_INTERRUPT 0x20000000
396#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
397
398/* Rx descriptors byte count */
399#define ETH_FRAME_FRAGMENTED 0x00000004
400
401/* Tx descriptors command */
402#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
403#define ETH_FRAME_SET_TO_VLAN 0x00008000
404#define ETH_UDP_FRAME 0x00010000
405#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
406#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
407#define ETH_ZERO_PADDING 0x00080000
408#define ETH_TX_LAST_DESC 0x00100000
409#define ETH_TX_FIRST_DESC 0x00200000
410#define ETH_GEN_CRC 0x00400000
411#define ETH_TX_ENABLE_INTERRUPT 0x00800000
412#define ETH_AUTO_MODE 0x40000000
413
414#define ETH_TX_IHL_SHIFT 11
415
416/* typedefs */
417
418typedef enum _eth_func_ret_status {
419 ETH_OK, /* Returned as expected. */
420 ETH_ERROR, /* Fundamental error. */
421 ETH_RETRY, /* Could not process request. Try later.*/
422 ETH_END_OF_JOB, /* Ring has nothing to process. */
423 ETH_QUEUE_FULL, /* Ring resource error. */
424 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
425} ETH_FUNC_RET_STATUS;
426
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200427/* These are for big-endian machines. Little endian needs different
428 * definitions.
429 */
430#if defined(__BIG_ENDIAN)
431struct eth_rx_desc {
432 u16 byte_cnt; /* Descriptor buffer byte count */
433 u16 buf_size; /* Buffer size */
434 u32 cmd_sts; /* Descriptor command status */
435 u32 next_desc_ptr; /* Next descriptor pointer */
436 u32 buf_ptr; /* Descriptor buffer pointer */
437};
438
439struct eth_tx_desc {
440 u16 byte_cnt; /* buffer byte count */
441 u16 l4i_chk; /* CPU provided TCP checksum */
442 u32 cmd_sts; /* Command/status field */
443 u32 next_desc_ptr; /* Pointer to next descriptor */
444 u32 buf_ptr; /* pointer to buffer for this descriptor*/
445};
446#elif defined(__LITTLE_ENDIAN)
447struct eth_rx_desc {
448 u32 cmd_sts; /* Descriptor command status */
449 u16 buf_size; /* Buffer size */
450 u16 byte_cnt; /* Descriptor buffer byte count */
451 u32 buf_ptr; /* Descriptor buffer pointer */
452 u32 next_desc_ptr; /* Next descriptor pointer */
453};
454
455struct eth_tx_desc {
456 u32 cmd_sts; /* Command/status field */
457 u16 l4i_chk; /* CPU provided TCP checksum */
458 u16 byte_cnt; /* buffer byte count */
459 u32 buf_ptr; /* pointer to buffer for this descriptor*/
460 u32 next_desc_ptr; /* Pointer to next descriptor */
461};
462#else
463#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
464#endif
465
466/* Unified struct for Rx and Tx operations. The user is not required to */
467/* be familier with neither Tx nor Rx descriptors. */
468struct pkt_info {
469 unsigned short byte_cnt; /* Descriptor buffer byte count */
470 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
471 unsigned int cmd_sts; /* Descriptor command status */
472 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
473 struct sk_buff *return_info; /* User resource return information */
474};
475
476/* Ethernet port specific information */
477struct mv643xx_mib_counters {
478 u64 good_octets_received;
479 u32 bad_octets_received;
480 u32 internal_mac_transmit_err;
481 u32 good_frames_received;
482 u32 bad_frames_received;
483 u32 broadcast_frames_received;
484 u32 multicast_frames_received;
485 u32 frames_64_octets;
486 u32 frames_65_to_127_octets;
487 u32 frames_128_to_255_octets;
488 u32 frames_256_to_511_octets;
489 u32 frames_512_to_1023_octets;
490 u32 frames_1024_to_max_octets;
491 u64 good_octets_sent;
492 u32 good_frames_sent;
493 u32 excessive_collision;
494 u32 multicast_frames_sent;
495 u32 broadcast_frames_sent;
496 u32 unrec_mac_control_received;
497 u32 fc_sent;
498 u32 good_fc_received;
499 u32 bad_fc_received;
500 u32 undersize_received;
501 u32 fragments_received;
502 u32 oversize_received;
503 u32 jabber_received;
504 u32 mac_receive_error;
505 u32 bad_crc_event;
506 u32 collision;
507 u32 late_collision;
508};
509
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200510struct mv643xx_shared_private {
511 void __iomem *eth_base;
512
513 /* used to protect SMI_REG, which is shared across ports */
514 spinlock_t phy_lock;
515};
516
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200517struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200518 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200519 int port_num; /* User Ethernet port number */
520
521 u32 rx_sram_addr; /* Base address of rx sram area */
522 u32 rx_sram_size; /* Size of rx sram area */
523 u32 tx_sram_addr; /* Base address of tx sram area */
524 u32 tx_sram_size; /* Size of tx sram area */
525
526 int rx_resource_err; /* Rx ring resource error flag */
527
528 /* Tx/Rx rings managment indexes fields. For driver use */
529
530 /* Next available and first returning Rx resource */
531 int rx_curr_desc_q, rx_used_desc_q;
532
533 /* Next available and first returning Tx resource */
534 int tx_curr_desc_q, tx_used_desc_q;
535
536#ifdef MV643XX_TX_FAST_REFILL
537 u32 tx_clean_threshold;
538#endif
539
540 struct eth_rx_desc *p_rx_desc_area;
541 dma_addr_t rx_desc_dma;
542 int rx_desc_area_size;
543 struct sk_buff **rx_skb;
544
545 struct eth_tx_desc *p_tx_desc_area;
546 dma_addr_t tx_desc_dma;
547 int tx_desc_area_size;
548 struct sk_buff **tx_skb;
549
550 struct work_struct tx_timeout_task;
551
552 struct net_device *dev;
553 struct napi_struct napi;
554 struct net_device_stats stats;
555 struct mv643xx_mib_counters mib_counters;
556 spinlock_t lock;
557 /* Size of Tx Ring per queue */
558 int tx_ring_size;
559 /* Number of tx descriptors in use */
560 int tx_desc_count;
561 /* Size of Rx Ring per queue */
562 int rx_ring_size;
563 /* Number of rx descriptors in use */
564 int rx_desc_count;
565
566 /*
567 * Used in case RX Ring is empty, which can be caused when
568 * system does not have resources (skb's)
569 */
570 struct timer_list timeout;
571
572 u32 rx_int_coal;
573 u32 tx_int_coal;
574 struct mii_if_info mii;
575};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577/* Static function declarations */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200578static void eth_port_init(struct mv643xx_private *mp);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700579static void eth_port_reset(struct mv643xx_private *mp);
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200580static void eth_port_start(struct net_device *dev);
581
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700582static void ethernet_phy_reset(struct mv643xx_private *mp);
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200583
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700584static void eth_port_write_smi_reg(struct mv643xx_private *mp,
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200585 unsigned int phy_reg, unsigned int value);
586
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700587static void eth_port_read_smi_reg(struct mv643xx_private *mp,
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200588 unsigned int phy_reg, unsigned int *value);
589
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700590static void eth_clear_mib_counters(struct mv643xx_private *mp);
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200591
592static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
593 struct pkt_info *p_pkt_info);
594static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
595 struct pkt_info *p_pkt_info);
596
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700597static void eth_port_uc_addr_get(struct mv643xx_private *mp,
598 unsigned char *p_addr);
599static void eth_port_uc_addr_set(struct mv643xx_private *mp,
600 unsigned char *p_addr);
Dale Farnsworth16e03012006-01-16 16:50:02 -0700601static void eth_port_set_multicast_list(struct net_device *);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700602static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
Dale Farnsworth12a87c62006-03-03 10:00:22 -0700603 unsigned int queues);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700604static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
Dale Farnsworth12a87c62006-03-03 10:00:22 -0700605 unsigned int queues);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700606static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp);
607static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp);
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700608static int mv643xx_eth_open(struct net_device *);
609static int mv643xx_eth_stop(struct net_device *);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700610static void eth_port_init_mac_tables(struct mv643xx_private *mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611#ifdef MV643XX_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700612static int mv643xx_poll(struct napi_struct *napi, int budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613#endif
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700614static int ethernet_phy_get(struct mv643xx_private *mp);
615static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr);
616static int ethernet_phy_detect(struct mv643xx_private *mp);
James Chapmanc28a4f82006-01-27 01:13:15 -0700617static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location);
618static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val);
James Chapmand0412d92006-01-27 01:15:30 -0700619static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Jeff Garzik7282d492006-09-13 14:30:00 -0400620static const struct ethtool_ops mv643xx_ethtool_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622static char mv643xx_driver_name[] = "mv643xx_eth";
623static char mv643xx_driver_version[] = "1.0";
624
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700625static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200627 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700630static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200632 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/*
636 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
637 *
638 * Input : pointer to ethernet interface network device structure
639 * new mtu size
640 * Output : 0 upon success, -EINVAL upon failure
641 */
642static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
643{
Dale Farnsworth8f518702006-01-16 16:56:30 -0700644 if ((new_mtu > 9500) || (new_mtu < 64))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 dev->mtu = new_mtu;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700648 if (!netif_running(dev))
649 return 0;
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /*
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700652 * Stop and then re-open the interface. This will allocate RX
653 * skbs of the new MTU.
654 * There is a possible danger that the open will not succeed,
655 * due to memory being full, which might fail the open function.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700657 mv643xx_eth_stop(dev);
658 if (mv643xx_eth_open(dev)) {
659 printk(KERN_ERR "%s: Fatal error on opening device\n",
660 dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 return 0;
664}
665
666/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700667 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 *
669 * Fills / refills RX queue on a certain gigabit ethernet port
670 *
671 * Input : pointer to ethernet interface network device structure
672 * Output : N/A
673 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700674static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 struct mv643xx_private *mp = netdev_priv(dev);
677 struct pkt_info pkt_info;
678 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700679 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700681 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000682 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (!skb)
684 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700685 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000686 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700687 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000688 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700690 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
691 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
692 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 pkt_info.return_info = skb;
694 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
695 printk(KERN_ERR
696 "%s: Error allocating RX Ring\n", dev->name);
697 break;
698 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700699 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 /*
702 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700703 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700705 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700707 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700713 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 *
715 * Timer routine to wake up RX queue filling task. This function is
716 * used only in case the RX queue is empty, and all alloc_skb has
717 * failed (due to out of memory event).
718 *
719 * Input : pointer to ethernet interface network device structure
720 * Output : N/A
721 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700722static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700724 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
727/*
728 * mv643xx_eth_update_mac_address
729 *
730 * Update the MAC address of the port in the address table
731 *
732 * Input : pointer to ethernet interface network device structure
733 * Output : N/A
734 */
735static void mv643xx_eth_update_mac_address(struct net_device *dev)
736{
737 struct mv643xx_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700739 eth_port_init_mac_tables(mp);
740 eth_port_uc_addr_set(mp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743/*
744 * mv643xx_eth_set_rx_mode
745 *
746 * Change from promiscuos to regular rx mode
747 *
748 * Input : pointer to ethernet interface network device structure
749 * Output : N/A
750 */
751static void mv643xx_eth_set_rx_mode(struct net_device *dev)
752{
753 struct mv643xx_private *mp = netdev_priv(dev);
Dale Farnsworth01999872006-01-27 01:18:01 -0700754 u32 config_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700756 config_reg = rdl(mp, PORT_CONFIG_REG(mp->port_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (dev->flags & IFF_PROMISC)
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +0200758 config_reg |= (u32) UNICAST_PROMISCUOUS_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 else
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +0200760 config_reg &= ~(u32) UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700761 wrl(mp, PORT_CONFIG_REG(mp->port_num), config_reg);
Dale Farnsworth16e03012006-01-16 16:50:02 -0700762
763 eth_port_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766/*
767 * mv643xx_eth_set_mac_address
768 *
769 * Change the interface's mac address.
770 * No special hardware thing should be done because interface is always
771 * put in promiscuous mode.
772 *
773 * Input : pointer to ethernet interface network device structure and
774 * a pointer to the designated entry to be added to the cache.
775 * Output : zero upon success, negative upon failure
776 */
777static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
778{
779 int i;
780
781 for (i = 0; i < 6; i++)
782 /* +2 is for the offset of the HW addr type */
783 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
784 mv643xx_eth_update_mac_address(dev);
785 return 0;
786}
787
788/*
789 * mv643xx_eth_tx_timeout
790 *
791 * Called upon a timeout on transmitting a packet
792 *
793 * Input : pointer to ethernet interface network device structure.
794 * Output : N/A
795 */
796static void mv643xx_eth_tx_timeout(struct net_device *dev)
797{
798 struct mv643xx_private *mp = netdev_priv(dev);
799
800 printk(KERN_INFO "%s: TX timeout ", dev->name);
801
802 /* Do the reset outside of interrupt context */
803 schedule_work(&mp->tx_timeout_task);
804}
805
806/*
807 * mv643xx_eth_tx_timeout_task
808 *
809 * Actual routine to reset the adapter when a timeout on Tx has occurred
810 */
Al Viro91c7c562006-12-06 19:50:06 +0000811static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Al Viro91c7c562006-12-06 19:50:06 +0000813 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
814 tx_timeout_task);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700815 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dale Farnsworth94843562006-04-11 18:24:26 -0700817 if (!netif_running(dev))
818 return;
819
820 netif_stop_queue(dev);
821
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -0700822 eth_port_reset(mp);
Dale Farnsworthed9b5d42006-01-27 01:06:38 -0700823 eth_port_start(dev);
Dale Farnsworth94843562006-04-11 18:24:26 -0700824
825 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
826 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700829/**
830 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 *
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700832 * If force is non-zero, frees uncompleted descriptors as well
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 */
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700834static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 struct mv643xx_private *mp = netdev_priv(dev);
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700837 struct eth_tx_desc *desc;
838 u32 cmd_sts;
839 struct sk_buff *skb;
840 unsigned long flags;
841 int tx_index;
842 dma_addr_t addr;
843 int count;
844 int released = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700846 while (mp->tx_desc_count > 0) {
847 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700848
849 /* tx_desc_count might have changed before acquiring the lock */
850 if (mp->tx_desc_count <= 0) {
851 spin_unlock_irqrestore(&mp->lock, flags);
852 return released;
853 }
854
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700855 tx_index = mp->tx_used_desc_q;
856 desc = &mp->p_tx_desc_area[tx_index];
857 cmd_sts = desc->cmd_sts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700859 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
860 spin_unlock_irqrestore(&mp->lock, flags);
861 return released;
862 }
863
864 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
865 mp->tx_desc_count--;
866
867 addr = desc->buf_ptr;
868 count = desc->byte_cnt;
869 skb = mp->tx_skb[tx_index];
870 if (skb)
871 mp->tx_skb[tx_index] = NULL;
872
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700873 if (cmd_sts & ETH_ERROR_SUMMARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 printk("%s: Error in TX\n", dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700875 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700878 spin_unlock_irqrestore(&mp->lock, flags);
879
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700880 if (cmd_sts & ETH_TX_FIRST_DESC)
881 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
Paolo Galtiericb415d32006-01-16 16:48:02 -0700882 else
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700883 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700885 if (skb)
886 dev_kfree_skb_irq(skb);
887
888 released = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return released;
892}
893
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700894static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
895{
896 struct mv643xx_private *mp = netdev_priv(dev);
897
898 if (mv643xx_eth_free_tx_descs(dev, 0) &&
899 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
900 netif_wake_queue(dev);
901}
902
903static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
904{
905 mv643xx_eth_free_tx_descs(dev, 1);
906}
907
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908/*
909 * mv643xx_eth_receive
910 *
911 * This function is forward packets that are received from the port's
912 * queues toward kernel core or FastRoute them to another interface.
913 *
914 * Input : dev - a pointer to the required interface
915 * max - maximum number to receive (0 means unlimted)
916 *
917 * Output : number of served packets
918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700922 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 unsigned int received_packets = 0;
924 struct sk_buff *skb;
925 struct pkt_info pkt_info;
926
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700927 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400928 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700929 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700930 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700932
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700933 /*
934 * Update statistics.
935 * Note byte count includes 4 byte CRC count
936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 stats->rx_packets++;
938 stats->rx_bytes += pkt_info.byte_cnt;
939 skb = pkt_info.return_info;
940 /*
941 * In case received a packet without first / last bits on OR
942 * the error summary bit is on, the packets needs to be dropeed.
943 */
944 if (((pkt_info.cmd_sts
945 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
946 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
947 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
948 stats->rx_dropped++;
949 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
950 ETH_RX_LAST_DESC)) !=
951 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
952 if (net_ratelimit())
953 printk(KERN_ERR
954 "%s: Received packet spread "
955 "on multiple descriptors\n",
956 dev->name);
957 }
958 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
959 stats->rx_errors++;
960
961 dev_kfree_skb_irq(skb);
962 } else {
963 /*
964 * The -4 is for the CRC in the trailer of the
965 * received packet
966 */
967 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
970 skb->ip_summed = CHECKSUM_UNNECESSARY;
971 skb->csum = htons(
972 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
973 }
974 skb->protocol = eth_type_trans(skb, dev);
975#ifdef MV643XX_NAPI
976 netif_receive_skb(skb);
977#else
978 netif_rx(skb);
979#endif
980 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700981 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700983 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985 return received_packets;
986}
987
James Chapmand0412d92006-01-27 01:15:30 -0700988/* Set the mv643xx port configuration register for the speed/duplex mode. */
989static void mv643xx_eth_update_pscr(struct net_device *dev,
990 struct ethtool_cmd *ecmd)
991{
992 struct mv643xx_private *mp = netdev_priv(dev);
993 int port_num = mp->port_num;
994 u32 o_pscr, n_pscr;
Dale Farnsworth12a87c62006-03-03 10:00:22 -0700995 unsigned int queues;
James Chapmand0412d92006-01-27 01:15:30 -0700996
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700997 o_pscr = rdl(mp, PORT_SERIAL_CONTROL_REG(port_num));
James Chapmand0412d92006-01-27 01:15:30 -0700998 n_pscr = o_pscr;
999
1000 /* clear speed, duplex and rx buffer size fields */
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001001 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1002 SET_GMII_SPEED_TO_1000 |
1003 SET_FULL_DUPLEX_MODE |
1004 MAX_RX_PACKET_MASK);
James Chapmand0412d92006-01-27 01:15:30 -07001005
1006 if (ecmd->duplex == DUPLEX_FULL)
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001007 n_pscr |= SET_FULL_DUPLEX_MODE;
James Chapmand0412d92006-01-27 01:15:30 -07001008
1009 if (ecmd->speed == SPEED_1000)
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001010 n_pscr |= SET_GMII_SPEED_TO_1000 |
1011 MAX_RX_PACKET_9700BYTE;
James Chapmand0412d92006-01-27 01:15:30 -07001012 else {
1013 if (ecmd->speed == SPEED_100)
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001014 n_pscr |= SET_MII_SPEED_TO_100;
1015 n_pscr |= MAX_RX_PACKET_1522BYTE;
James Chapmand0412d92006-01-27 01:15:30 -07001016 }
1017
1018 if (n_pscr != o_pscr) {
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001019 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001020 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
James Chapmand0412d92006-01-27 01:15:30 -07001021 else {
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001022 queues = mv643xx_eth_port_disable_tx(mp);
James Chapmand0412d92006-01-27 01:15:30 -07001023
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001024 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001025 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), o_pscr);
1026 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
1027 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), n_pscr);
Dale Farnsworth12a87c62006-03-03 10:00:22 -07001028 if (queues)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001029 mv643xx_eth_port_enable_tx(mp, queues);
James Chapmand0412d92006-01-27 01:15:30 -07001030 }
1031 }
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * mv643xx_eth_int_handler
1036 *
1037 * Main interrupt handler for the gigbit ethernet ports
1038 *
1039 * Input : irq - irq number (not used)
1040 * dev_id - a pointer to the required interface's data structure
1041 * regs - not used
1042 * Output : N/A
1043 */
1044
David Howells7d12e782006-10-05 14:55:46 +01001045static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046{
1047 struct net_device *dev = (struct net_device *)dev_id;
1048 struct mv643xx_private *mp = netdev_priv(dev);
1049 u32 eth_int_cause, eth_int_cause_ext = 0;
1050 unsigned int port_num = mp->port_num;
1051
1052 /* Read interrupt cause registers */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001053 eth_int_cause = rdl(mp, INTERRUPT_CAUSE_REG(port_num)) &
Dale Farnsworth7303fde2006-03-03 10:03:36 -07001054 ETH_INT_UNMASK_ALL;
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001055 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001056 eth_int_cause_ext = rdl(mp,
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001057 INTERRUPT_CAUSE_EXTEND_REG(port_num)) &
Dale Farnsworth7303fde2006-03-03 10:03:36 -07001058 ETH_INT_UNMASK_ALL_EXT;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001059 wrl(mp, INTERRUPT_CAUSE_EXTEND_REG(port_num),
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001060 ~eth_int_cause_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -07001062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 /* PHY status changed */
Dale Farnsworth2bcff602007-09-28 06:30:43 -07001064 if (eth_int_cause_ext & (ETH_INT_CAUSE_PHY | ETH_INT_CAUSE_STATE)) {
James Chapmand0412d92006-01-27 01:15:30 -07001065 struct ethtool_cmd cmd;
1066
James Chapmanc28a4f82006-01-27 01:13:15 -07001067 if (mii_link_ok(&mp->mii)) {
James Chapmand0412d92006-01-27 01:15:30 -07001068 mii_ethtool_gset(&mp->mii, &cmd);
1069 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001070 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
James Chapmanc28a4f82006-01-27 01:13:15 -07001071 if (!netif_carrier_ok(dev)) {
1072 netif_carrier_on(dev);
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001073 if (mp->tx_ring_size - mp->tx_desc_count >=
1074 MAX_DESCS_PER_SKB)
James Chapmand0412d92006-01-27 01:15:30 -07001075 netif_wake_queue(dev);
James Chapmanc28a4f82006-01-27 01:13:15 -07001076 }
1077 } else if (netif_carrier_ok(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 netif_stop_queue(dev);
James Chapmanc28a4f82006-01-27 01:13:15 -07001079 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081 }
1082
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001083#ifdef MV643XX_NAPI
1084 if (eth_int_cause & ETH_INT_CAUSE_RX) {
1085 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001086 wrl(mp, INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001087
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001088 /* wait for previous write to complete */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001089 rdl(mp, INTERRUPT_MASK_REG(port_num));
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001090
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001091 netif_rx_schedule(dev, &mp->napi);
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001092 }
1093#else
1094 if (eth_int_cause & ETH_INT_CAUSE_RX)
1095 mv643xx_eth_receive_queue(dev, INT_MAX);
Brent Cook5c537402006-04-11 18:23:15 -07001096#endif
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001097 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
1098 mv643xx_eth_free_completed_tx_descs(dev);
Dale Farnsworth468d09f2006-03-03 10:04:39 -07001099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 /*
1101 * If no real interrupt occured, exit.
1102 * This can happen when using gigE interrupt coalescing mechanism.
1103 */
1104 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
1105 return IRQ_NONE;
1106
1107 return IRQ_HANDLED;
1108}
1109
1110#ifdef MV643XX_COAL
1111
1112/*
1113 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
1114 *
1115 * DESCRIPTION:
1116 * This routine sets the RX coalescing interrupt mechanism parameter.
1117 * This parameter is a timeout counter, that counts in 64 t_clk
1118 * chunks ; that when timeout event occurs a maskable interrupt
1119 * occurs.
1120 * The parameter is calculated using the tClk of the MV-643xx chip
1121 * , and the required delay of the interrupt in usec.
1122 *
1123 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001124 * struct mv643xx_private *mp Ethernet port
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
1126 * unsigned int delay Delay in usec
1127 *
1128 * OUTPUT:
1129 * Interrupt coalescing mechanism value is set in MV-643xx chip.
1130 *
1131 * RETURN:
1132 * The interrupt coalescing value set in the gigE port.
1133 *
1134 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001135static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 unsigned int t_clk, unsigned int delay)
1137{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001138 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 unsigned int coal = ((t_clk / 1000000) * delay) / 64;
1140
1141 /* Set RX Coalescing mechanism */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001142 wrl(mp, SDMA_CONFIG_REG(port_num),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 ((coal & 0x3fff) << 8) |
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001144 (rdl(mp, SDMA_CONFIG_REG(port_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 & 0xffc000ff));
1146
1147 return coal;
1148}
1149#endif
1150
1151/*
1152 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
1153 *
1154 * DESCRIPTION:
1155 * This routine sets the TX coalescing interrupt mechanism parameter.
1156 * This parameter is a timeout counter, that counts in 64 t_clk
1157 * chunks ; that when timeout event occurs a maskable interrupt
1158 * occurs.
1159 * The parameter is calculated using the t_cLK frequency of the
1160 * MV-643xx chip and the required delay in the interrupt in uSec
1161 *
1162 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001163 * struct mv643xx_private *mp Ethernet port
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
1165 * unsigned int delay Delay in uSeconds
1166 *
1167 * OUTPUT:
1168 * Interrupt coalescing mechanism value is set in MV-643xx chip.
1169 *
1170 * RETURN:
1171 * The interrupt coalescing value set in the gigE port.
1172 *
1173 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001174static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 unsigned int t_clk, unsigned int delay)
1176{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001177 unsigned int coal = ((t_clk / 1000000) * delay) / 64;
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /* Set TX Coalescing mechanism */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001180 wrl(mp, TX_FIFO_URGENT_THRESHOLD_REG(mp->port_num), coal << 4);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return coal;
1183}
1184
1185/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1187 *
1188 * DESCRIPTION:
1189 * This function prepares a Rx chained list of descriptors and packet
1190 * buffers in a form of a ring. The routine must be called after port
1191 * initialization routine and before port start routine.
1192 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1193 * devices in the system (i.e. DRAM). This function uses the ethernet
1194 * struct 'virtual to physical' routine (set by the user) to set the ring
1195 * with physical addresses.
1196 *
1197 * INPUT:
1198 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1199 *
1200 * OUTPUT:
1201 * The routine updates the Ethernet port control struct with information
1202 * regarding the Rx descriptors and buffers.
1203 *
1204 * RETURN:
1205 * None.
1206 */
1207static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1208{
1209 volatile struct eth_rx_desc *p_rx_desc;
1210 int rx_desc_num = mp->rx_ring_size;
1211 int i;
1212
1213 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1214 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1215 for (i = 0; i < rx_desc_num; i++) {
1216 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1217 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1218 }
1219
1220 /* Save Rx desc pointer to driver struct. */
1221 mp->rx_curr_desc_q = 0;
1222 mp->rx_used_desc_q = 0;
1223
1224 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
1227/*
1228 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1229 *
1230 * DESCRIPTION:
1231 * This function prepares a Tx chained list of descriptors and packet
1232 * buffers in a form of a ring. The routine must be called after port
1233 * initialization routine and before port start routine.
1234 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1235 * devices in the system (i.e. DRAM). This function uses the ethernet
1236 * struct 'virtual to physical' routine (set by the user) to set the ring
1237 * with physical addresses.
1238 *
1239 * INPUT:
1240 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1241 *
1242 * OUTPUT:
1243 * The routine updates the Ethernet port control struct with information
1244 * regarding the Tx descriptors and buffers.
1245 *
1246 * RETURN:
1247 * None.
1248 */
1249static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1250{
1251 int tx_desc_num = mp->tx_ring_size;
1252 struct eth_tx_desc *p_tx_desc;
1253 int i;
1254
1255 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1256 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1257 for (i = 0; i < tx_desc_num; i++) {
1258 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1259 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1260 }
1261
1262 mp->tx_curr_desc_q = 0;
1263 mp->tx_used_desc_q = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
James Chapmand0412d92006-01-27 01:15:30 -07001268static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1269{
1270 struct mv643xx_private *mp = netdev_priv(dev);
1271 int err;
1272
1273 spin_lock_irq(&mp->lock);
1274 err = mii_ethtool_sset(&mp->mii, cmd);
1275 spin_unlock_irq(&mp->lock);
1276
1277 return err;
1278}
1279
1280static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1281{
1282 struct mv643xx_private *mp = netdev_priv(dev);
1283 int err;
1284
1285 spin_lock_irq(&mp->lock);
1286 err = mii_ethtool_gset(&mp->mii, cmd);
1287 spin_unlock_irq(&mp->lock);
1288
1289 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1290 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1291 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1292
1293 return err;
1294}
1295
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001296/*
1297 * mv643xx_eth_open
1298 *
1299 * This function is called when openning the network device. The function
1300 * should initialize all the hardware, initialize cyclic Rx/Tx
1301 * descriptors chain and buffers and allocate an IRQ to the network
1302 * device.
1303 *
1304 * Input : a pointer to the network device structure
1305 *
1306 * Output : zero of success , nonzero if fails.
1307 */
1308
1309static int mv643xx_eth_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
1311 struct mv643xx_private *mp = netdev_priv(dev);
1312 unsigned int port_num = mp->port_num;
1313 unsigned int size;
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001314 int err;
1315
Dale Farnsworth85cf5722007-03-07 22:21:23 -07001316 /* Clear any pending ethernet port interrupts */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001317 wrl(mp, INTERRUPT_CAUSE_REG(port_num), 0);
1318 wrl(mp, INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
Dale Farnsworth85cf5722007-03-07 22:21:23 -07001319 /* wait for previous write to complete */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001320 rdl(mp, INTERRUPT_CAUSE_EXTEND_REG(port_num));
Dale Farnsworth85cf5722007-03-07 22:21:23 -07001321
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001322 err = request_irq(dev->irq, mv643xx_eth_int_handler,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001323 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001324 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07001325 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001326 return -EAGAIN;
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 eth_port_init(mp);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 memset(&mp->timeout, 0, sizeof(struct timer_list));
Dale Farnsworthf78fb472006-03-03 10:05:26 -07001332 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 mp->timeout.data = (unsigned long)dev;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 /* Allocate RX and TX skb rings */
1336 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
1337 GFP_KERNEL);
1338 if (!mp->rx_skb) {
1339 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001340 err = -ENOMEM;
1341 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 }
1343 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
1344 GFP_KERNEL);
1345 if (!mp->tx_skb) {
1346 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001347 err = -ENOMEM;
1348 goto out_free_rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
1351 /* Allocate TX ring */
Dale Farnsworthf98e36f12006-01-27 01:09:18 -07001352 mp->tx_desc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
1354 mp->tx_desc_area_size = size;
1355
1356 if (mp->tx_sram_size) {
1357 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
1358 mp->tx_sram_size);
1359 mp->tx_desc_dma = mp->tx_sram_addr;
1360 } else
1361 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
1362 &mp->tx_desc_dma,
1363 GFP_KERNEL);
1364
1365 if (!mp->p_tx_desc_area) {
1366 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
1367 dev->name, size);
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001368 err = -ENOMEM;
1369 goto out_free_tx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
1372 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
1373
1374 ether_init_tx_desc_ring(mp);
1375
1376 /* Allocate RX ring */
Dale Farnsworthf98e36f12006-01-27 01:09:18 -07001377 mp->rx_desc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
1379 mp->rx_desc_area_size = size;
1380
1381 if (mp->rx_sram_size) {
1382 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
1383 mp->rx_sram_size);
1384 mp->rx_desc_dma = mp->rx_sram_addr;
1385 } else
1386 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
1387 &mp->rx_desc_dma,
1388 GFP_KERNEL);
1389
1390 if (!mp->p_rx_desc_area) {
1391 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
1392 dev->name, size);
1393 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
1394 dev->name);
1395 if (mp->rx_sram_size)
Dale Farnsworthdd09b1d2006-01-16 16:53:15 -07001396 iounmap(mp->p_tx_desc_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 else
1398 dma_free_coherent(NULL, mp->tx_desc_area_size,
1399 mp->p_tx_desc_area, mp->tx_desc_dma);
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001400 err = -ENOMEM;
1401 goto out_free_tx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403 memset((void *)mp->p_rx_desc_area, 0, size);
1404
1405 ether_init_rx_desc_ring(mp);
1406
Dale Farnsworthf78fb472006-03-03 10:05:26 -07001407 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001409#ifdef MV643XX_NAPI
1410 napi_enable(&mp->napi);
1411#endif
1412
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07001413 eth_port_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414
1415 /* Interrupt Coalescing */
1416
1417#ifdef MV643XX_COAL
1418 mp->rx_int_coal =
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001419 eth_port_set_rx_coal(mp, 133000000, MV643XX_RX_COAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420#endif
1421
1422 mp->tx_int_coal =
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001423 eth_port_set_tx_coal(mp, 133000000, MV643XX_TX_COAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Dale Farnsworth8f518702006-01-16 16:56:30 -07001425 /* Unmask phy and link status changes interrupts */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001426 wrl(mp, INTERRUPT_EXTEND_MASK_REG(port_num), ETH_INT_UNMASK_ALL_EXT);
Dale Farnsworth8f518702006-01-16 16:56:30 -07001427
1428 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001429 wrl(mp, INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
James Chapmand0412d92006-01-27 01:15:30 -07001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return 0;
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001432
1433out_free_tx_skb:
1434 kfree(mp->tx_skb);
1435out_free_rx_skb:
1436 kfree(mp->rx_skb);
1437out_free_irq:
1438 free_irq(dev->irq, dev);
1439
1440 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
1443static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1444{
1445 struct mv643xx_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
1447 /* Stop Tx Queues */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001448 mv643xx_eth_port_disable_tx(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001450 /* Free outstanding skb's on TX ring */
1451 mv643xx_eth_free_all_tx_descs(dev);
1452
1453 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 /* Free TX ring */
1456 if (mp->tx_sram_size)
1457 iounmap(mp->p_tx_desc_area);
1458 else
1459 dma_free_coherent(NULL, mp->tx_desc_area_size,
1460 mp->p_tx_desc_area, mp->tx_desc_dma);
1461}
1462
1463static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1464{
1465 struct mv643xx_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 int curr;
1467
1468 /* Stop RX Queues */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001469 mv643xx_eth_port_disable_rx(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
1471 /* Free preallocated skb's on RX rings */
Dale Farnsworthf98e36f12006-01-27 01:09:18 -07001472 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (mp->rx_skb[curr]) {
1474 dev_kfree_skb(mp->rx_skb[curr]);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -07001475 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 }
1477 }
1478
Dale Farnsworthf98e36f12006-01-27 01:09:18 -07001479 if (mp->rx_desc_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 printk(KERN_ERR
1481 "%s: Error in freeing Rx Ring. %d skb's still"
1482 " stuck in RX Ring - ignoring them\n", dev->name,
Dale Farnsworthf98e36f12006-01-27 01:09:18 -07001483 mp->rx_desc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 /* Free RX ring */
1485 if (mp->rx_sram_size)
1486 iounmap(mp->p_rx_desc_area);
1487 else
1488 dma_free_coherent(NULL, mp->rx_desc_area_size,
1489 mp->p_rx_desc_area, mp->rx_desc_dma);
1490}
1491
1492/*
1493 * mv643xx_eth_stop
1494 *
1495 * This function is used when closing the network device.
1496 * It updates the hardware,
1497 * release all memory that holds buffers and descriptors and release the IRQ.
1498 * Input : a pointer to the device structure
1499 * Output : zero if success , nonzero if fails
1500 */
1501
Dale Farnsworthab4384a2006-01-16 16:59:21 -07001502static int mv643xx_eth_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 struct mv643xx_private *mp = netdev_priv(dev);
1505 unsigned int port_num = mp->port_num;
1506
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001507 /* Mask all interrupts on ethernet port */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001508 wrl(mp, INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001509 /* wait for previous write to complete */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001510 rdl(mp, INTERRUPT_MASK_REG(port_num));
Dale Farnsworth8f518702006-01-16 16:56:30 -07001511
1512#ifdef MV643XX_NAPI
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001513 napi_disable(&mp->napi);
Dale Farnsworth8f518702006-01-16 16:56:30 -07001514#endif
1515 netif_carrier_off(dev);
1516 netif_stop_queue(dev);
1517
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001518 eth_port_reset(mp);
Dale Farnsworth8f518702006-01-16 16:56:30 -07001519
1520 mv643xx_eth_free_tx_rings(dev);
1521 mv643xx_eth_free_rx_rings(dev);
1522
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 return 0;
1526}
1527
1528#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529/*
1530 * mv643xx_poll
1531 *
1532 * This function is used in case of NAPI
1533 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001534static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001536 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
1537 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001539 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541#ifdef MV643XX_TX_FAST_REFILL
1542 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001543 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
1546#endif
1547
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001548 work_done = 0;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001549 if ((rdl(mp, RX_CURRENT_QUEUE_DESC_PTR_0(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001550 != (u32) mp->rx_used_desc_q)
1551 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001553 if (work_done < budget) {
1554 netif_rx_complete(dev, napi);
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001555 wrl(mp, INTERRUPT_CAUSE_REG(port_num), 0);
1556 wrl(mp, INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
1557 wrl(mp, INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
1559
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001560 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562#endif
1563
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001564/**
1565 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
1566 *
1567 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -07001568 * This helper function detects that case.
1569 */
1570
1571static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
1572{
Dale Farnsworthb4de9052006-01-27 01:04:43 -07001573 unsigned int frag;
1574 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -07001575
Dale Farnsworthb4de9052006-01-27 01:04:43 -07001576 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1577 fragp = &skb_shinfo(skb)->frags[frag];
1578 if (fragp->size <= 8 && fragp->page_offset & 0x7)
1579 return 1;
1580 }
1581 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -07001582}
1583
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001584/**
1585 * eth_alloc_tx_desc_index - return the index of the next available tx desc
1586 */
1587static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
1588{
1589 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -07001590
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001591 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001592
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001593 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001594 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
1595
1596 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
1597
1598 return tx_desc_curr;
1599}
1600
1601/**
1602 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001604 * Ensure the data for each fragment to be transmitted is mapped properly,
1605 * then fill in descriptors in the tx hw queue.
1606 */
1607static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
1608 struct sk_buff *skb)
1609{
1610 int frag;
1611 int tx_index;
1612 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001613
1614 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1615 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1616
1617 tx_index = eth_alloc_tx_desc_index(mp);
1618 desc = &mp->p_tx_desc_area[tx_index];
1619
1620 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
1621 /* Last Frag enables interrupt and frees the skb */
1622 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
1623 desc->cmd_sts |= ETH_ZERO_PADDING |
1624 ETH_TX_LAST_DESC |
1625 ETH_TX_ENABLE_INTERRUPT;
1626 mp->tx_skb[tx_index] = skb;
1627 } else
Al Viro05980772006-05-30 23:59:09 -04001628 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001629
1630 desc = &mp->p_tx_desc_area[tx_index];
1631 desc->l4i_chk = 0;
1632 desc->byte_cnt = this_frag->size;
1633 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
1634 this_frag->page_offset,
1635 this_frag->size,
1636 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001637 }
1638}
1639
Byron Bradley324ff2c2008-02-04 23:47:15 -08001640static inline __be16 sum16_as_be(__sum16 sum)
1641{
1642 return (__force __be16)sum;
1643}
1644
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001645/**
1646 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001648 * Ensure the data for an skb to be transmitted is mapped properly,
1649 * then fill in descriptors in the tx hw queue and start the hardware.
1650 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001651static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
1652 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001653{
1654 int tx_index;
1655 struct eth_tx_desc *desc;
1656 u32 cmd_sts;
1657 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001658 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001659
1660 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
1661
1662 tx_index = eth_alloc_tx_desc_index(mp);
1663 desc = &mp->p_tx_desc_area[tx_index];
1664
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001665 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001666 eth_tx_fill_frag_descs(mp, skb);
1667
1668 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -04001669 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001670 } else {
1671 cmd_sts |= ETH_ZERO_PADDING |
1672 ETH_TX_LAST_DESC |
1673 ETH_TX_ENABLE_INTERRUPT;
1674 length = skb->len;
1675 mp->tx_skb[tx_index] = skb;
1676 }
1677
1678 desc->byte_cnt = length;
1679 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001680
Patrick McHardy84fa7932006-08-29 16:44:56 -07001681 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -08001682 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001683
1684 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
1685 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001686 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001687
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001688 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001689 case IPPROTO_UDP:
1690 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -08001691 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001692 break;
1693 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -08001694 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001695 break;
1696 default:
1697 BUG();
1698 }
1699 } else {
1700 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1701 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1702 desc->l4i_chk = 0;
1703 }
1704
1705 /* ensure all other descriptors are written before first cmd_sts */
1706 wmb();
1707 desc->cmd_sts = cmd_sts;
1708
1709 /* ensure all descriptors are written before poking hardware */
1710 wmb();
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001711 mv643xx_eth_port_enable_tx(mp, ETH_TX_QUEUES_ENABLED);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001712
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001713 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001714}
1715
1716/**
1717 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
1720static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1721{
1722 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07001723 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001726 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -07001727
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001728 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
1729 stats->tx_dropped++;
1730 printk(KERN_DEBUG "%s: failed to linearize tiny "
1731 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001732 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -07001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 spin_lock_irqsave(&mp->lock, flags);
1736
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001737 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1738 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1739 netif_stop_queue(dev);
1740 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001741 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -07001742 }
1743
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001744 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -07001745 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 stats->tx_packets++;
1747 dev->trans_start = jiffies;
1748
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001749 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1750 netif_stop_queue(dev);
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 spin_unlock_irqrestore(&mp->lock, flags);
1753
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -07001754 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001757#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001758static void mv643xx_netpoll(struct net_device *netdev)
1759{
1760 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001761 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001762
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001763 wrl(mp, INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001764 /* wait for previous write to complete */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001765 rdl(mp, INTERRUPT_MASK_REG(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001766
Al Viro9da3b1a2006-10-08 15:00:44 +01001767 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001768
Lennert Buytenhekec69d652008-03-18 11:38:05 -07001769 wrl(mp, INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001770}
1771#endif
1772
James Chapmand0412d92006-01-27 01:15:30 -07001773static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
1774 int speed, int duplex,
1775 struct ethtool_cmd *cmd)
1776{
1777 struct mv643xx_private *mp = netdev_priv(dev);
1778
1779 memset(cmd, 0, sizeof(*cmd));
1780
1781 cmd->port = PORT_MII;
1782 cmd->transceiver = XCVR_INTERNAL;
1783 cmd->phy_address = phy_address;
1784
1785 if (speed == 0) {
1786 cmd->autoneg = AUTONEG_ENABLE;
1787 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
1788 cmd->speed = SPEED_100;
1789 cmd->advertising = ADVERTISED_10baseT_Half |
1790 ADVERTISED_10baseT_Full |
1791 ADVERTISED_100baseT_Half |
1792 ADVERTISED_100baseT_Full;
1793 if (mp->mii.supports_gmii)
1794 cmd->advertising |= ADVERTISED_1000baseT_Full;
1795 } else {
1796 cmd->autoneg = AUTONEG_DISABLE;
1797 cmd->speed = speed;
1798 cmd->duplex = duplex;
1799 }
1800}
1801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802/*/
1803 * mv643xx_eth_probe
1804 *
1805 * First function called after registering the network device.
1806 * It's purpose is to initialize the device as an ethernet device,
1807 * fill the ethernet device structure with pointers * to functions,
1808 * and set the MAC address of the interface
1809 *
1810 * Input : struct device *
1811 * Output : -ENOMEM if failed , 0 if success
1812 */
Russell King3ae5eae2005-11-09 22:32:44 +00001813static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001816 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 struct mv643xx_private *mp;
1818 struct net_device *dev;
1819 u8 *p;
1820 struct resource *res;
1821 int err;
James Chapmand0412d92006-01-27 01:15:30 -07001822 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07001823 int duplex = DUPLEX_HALF;
1824 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01001825 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001827 pd = pdev->dev.platform_data;
1828 if (pd == NULL) {
1829 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
1830 return -ENODEV;
1831 }
1832
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02001833 if (pd->shared == NULL) {
1834 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
1835 return -ENODEV;
1836 }
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 dev = alloc_etherdev(sizeof(struct mv643xx_private));
1839 if (!dev)
1840 return -ENOMEM;
1841
Russell King3ae5eae2005-11-09 22:32:44 +00001842 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001845 mp->dev = dev;
1846#ifdef MV643XX_NAPI
1847 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
1848#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1851 BUG_ON(!res);
1852 dev->irq = res->start;
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 dev->open = mv643xx_eth_open;
1855 dev->stop = mv643xx_eth_stop;
1856 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 dev->set_mac_address = mv643xx_eth_set_mac_address;
1858 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
1859
1860 /* No need to Tx Timeout */
1861 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001863#ifdef CONFIG_NET_POLL_CONTROLLER
1864 dev->poll_controller = mv643xx_netpoll;
1865#endif
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 dev->base_addr = 0;
1869 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07001870 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
1872
1873#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1874#ifdef MAX_SKB_FRAGS
1875 /*
1876 * Zero copy can only work if we use Discovery II memory. Else, we will
1877 * have to map the buffers to ISA memory which is only 16 MB
1878 */
Wolfram Joost63890572006-01-16 16:57:41 -07001879 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880#endif
1881#endif
1882
1883 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00001884 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 spin_lock_init(&mp->lock);
1887
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02001888 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07001889 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001890
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001892 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02001893 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
1894 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001896 if (is_valid_ether_addr(pd->mac_addr))
1897 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001899 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001900 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001902 if (pd->rx_queue_size)
1903 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001905 if (pd->tx_queue_size)
1906 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001908 if (pd->tx_sram_size) {
1909 mp->tx_sram_size = pd->tx_sram_size;
1910 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
Dale Farnsworth84dd6192007-03-03 06:40:28 -07001913 if (pd->rx_sram_size) {
1914 mp->rx_sram_size = pd->rx_sram_size;
1915 mp->rx_sram_addr = pd->rx_sram_addr;
1916 }
1917
1918 duplex = pd->duplex;
1919 speed = pd->speed;
1920
James Chapmanc28a4f82006-01-27 01:13:15 -07001921 /* Hook up MII support for ethtool */
1922 mp->mii.dev = dev;
1923 mp->mii.mdio_read = mv643xx_mdio_read;
1924 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001925 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07001926 mp->mii.phy_id_mask = 0x3f;
1927 mp->mii.reg_num_mask = 0x1f;
1928
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001929 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07001931 pr_debug("%s: No PHY detected at addr %d\n",
1932 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07001933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 }
1935
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07001936 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07001937 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07001938 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
1939 mv643xx_eth_update_pscr(dev, &cmd);
1940 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07001941
Olaf Heringb0b8dab2006-04-27 18:23:49 -07001942 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 err = register_netdev(dev);
1944 if (err)
1945 goto out;
1946
1947 p = dev->dev_addr;
1948 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07001949 "%s: port %d with MAC address %s\n",
1950 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 if (dev->features & NETIF_F_SG)
1953 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
1954
1955 if (dev->features & NETIF_F_IP_CSUM)
1956 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
1957 dev->name);
1958
1959#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1960 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
1961#endif
1962
1963#ifdef MV643XX_COAL
1964 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
1965 dev->name);
1966#endif
1967
1968#ifdef MV643XX_NAPI
1969 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
1970#endif
1971
Nicolas DETb1529872005-10-28 17:46:30 -07001972 if (mp->tx_sram_size > 0)
1973 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return 0;
1976
1977out:
1978 free_netdev(dev);
1979
1980 return err;
1981}
1982
Russell King3ae5eae2005-11-09 22:32:44 +00001983static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Russell King3ae5eae2005-11-09 22:32:44 +00001985 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 unregister_netdev(dev);
1988 flush_scheduled_work();
1989
1990 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00001991 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return 0;
1993}
1994
Russell King3ae5eae2005-11-09 22:32:44 +00001995static int mv643xx_eth_shared_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Lennert Buytenheke519abb2008-03-18 11:39:14 -07001997 static int mv643xx_version_printed = 0;
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02001998 struct mv643xx_shared_private *msp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 struct resource *res;
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002000 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Lennert Buytenheke519abb2008-03-18 11:39:14 -07002002 if (!mv643xx_version_printed++)
2003 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002005 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2007 if (res == NULL)
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002008 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002010 ret = -ENOMEM;
2011 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2012 if (msp == NULL)
2013 goto out;
2014 memset(msp, 0, sizeof(*msp));
2015
2016 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2017 if (msp->eth_base == NULL)
2018 goto out_free;
2019
2020 spin_lock_init(&msp->phy_lock);
2021
2022 platform_set_drvdata(pdev, msp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023
2024 return 0;
2025
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002026out_free:
2027 kfree(msp);
2028out:
2029 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030}
2031
Russell King3ae5eae2005-11-09 22:32:44 +00002032static int mv643xx_eth_shared_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002034 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2035
2036 iounmap(msp->eth_base);
2037 kfree(msp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 return 0;
2040}
2041
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002042static void mv643xx_eth_shutdown(struct platform_device *pdev)
2043{
2044 struct net_device *dev = platform_get_drvdata(pdev);
2045 struct mv643xx_private *mp = netdev_priv(dev);
2046 unsigned int port_num = mp->port_num;
2047
2048 /* Mask all interrupts on ethernet port */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002049 wrl(mp, INTERRUPT_MASK_REG(port_num), 0);
2050 rdl(mp, INTERRUPT_MASK_REG(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002051
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002052 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002053}
2054
Russell King3ae5eae2005-11-09 22:32:44 +00002055static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 .probe = mv643xx_eth_probe,
2057 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002058 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002059 .driver = {
2060 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002061 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002062 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063};
2064
Russell King3ae5eae2005-11-09 22:32:44 +00002065static struct platform_driver mv643xx_eth_shared_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 .probe = mv643xx_eth_shared_probe,
2067 .remove = mv643xx_eth_shared_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00002068 .driver = {
2069 .name = MV643XX_ETH_SHARED_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002070 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002071 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072};
2073
2074/*
2075 * mv643xx_init_module
2076 *
2077 * Registers the network drivers into the Linux kernel
2078 *
2079 * Input : N/A
2080 *
2081 * Output : N/A
2082 */
2083static int __init mv643xx_init_module(void)
2084{
2085 int rc;
2086
Russell King3ae5eae2005-11-09 22:32:44 +00002087 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002089 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002091 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 return rc;
2094}
2095
2096/*
2097 * mv643xx_cleanup_module
2098 *
2099 * Registers the network drivers into the Linux kernel
2100 *
2101 * Input : N/A
2102 *
2103 * Output : N/A
2104 */
2105static void __exit mv643xx_cleanup_module(void)
2106{
Russell King3ae5eae2005-11-09 22:32:44 +00002107 platform_driver_unregister(&mv643xx_eth_driver);
2108 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111module_init(mv643xx_init_module);
2112module_exit(mv643xx_cleanup_module);
2113
2114MODULE_LICENSE("GPL");
2115MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2116 " and Dale Farnsworth");
2117MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002118MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2119MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121/*
2122 * The second part is the low level driver of the gigE ethernet ports.
2123 */
2124
2125/*
2126 * Marvell's Gigabit Ethernet controller low level driver
2127 *
2128 * DESCRIPTION:
2129 * This file introduce low level API to Marvell's Gigabit Ethernet
2130 * controller. This Gigabit Ethernet Controller driver API controls
2131 * 1) Operations (i.e. port init, start, reset etc').
2132 * 2) Data flow (i.e. port send, receive etc').
2133 * Each Gigabit Ethernet port is controlled via
2134 * struct mv643xx_private.
2135 * This struct includes user configuration information as well as
2136 * driver internal data needed for its operations.
2137 *
2138 * Supported Features:
2139 * - This low level driver is OS independent. Allocating memory for
2140 * the descriptor rings and buffers are not within the scope of
2141 * this driver.
2142 * - The user is free from Rx/Tx queue managing.
2143 * - This low level driver introduce functionality API that enable
2144 * the to operate Marvell's Gigabit Ethernet Controller in a
2145 * convenient way.
2146 * - Simple Gigabit Ethernet port operation API.
2147 * - Simple Gigabit Ethernet port data flow API.
2148 * - Data flow and operation API support per queue functionality.
2149 * - Support cached descriptors for better performance.
2150 * - Enable access to all four DRAM banks and internal SRAM memory
2151 * spaces.
2152 * - PHY access and control API.
2153 * - Port control register configuration API.
2154 * - Full control over Unicast and Multicast MAC configurations.
2155 *
2156 * Operation flow:
2157 *
2158 * Initialization phase
2159 * This phase complete the initialization of the the
2160 * mv643xx_private struct.
2161 * User information regarding port configuration has to be set
2162 * prior to calling the port initialization routine.
2163 *
2164 * In this phase any port Tx/Rx activity is halted, MIB counters
2165 * are cleared, PHY address is set according to user parameter and
2166 * access to DRAM and internal SRAM memory spaces.
2167 *
2168 * Driver ring initialization
2169 * Allocating memory for the descriptor rings and buffers is not
2170 * within the scope of this driver. Thus, the user is required to
2171 * allocate memory for the descriptors ring and buffers. Those
2172 * memory parameters are used by the Rx and Tx ring initialization
2173 * routines in order to curve the descriptor linked list in a form
2174 * of a ring.
2175 * Note: Pay special attention to alignment issues when using
2176 * cached descriptors/buffers. In this phase the driver store
2177 * information in the mv643xx_private struct regarding each queue
2178 * ring.
2179 *
2180 * Driver start
2181 * This phase prepares the Ethernet port for Rx and Tx activity.
2182 * It uses the information stored in the mv643xx_private struct to
2183 * initialize the various port registers.
2184 *
2185 * Data flow:
2186 * All packet references to/from the driver are done using
2187 * struct pkt_info.
2188 * This struct is a unified struct used with Rx and Tx operations.
2189 * This way the user is not required to be familiar with neither
2190 * Tx nor Rx descriptors structures.
2191 * The driver's descriptors rings are management by indexes.
2192 * Those indexes controls the ring resources and used to indicate
2193 * a SW resource error:
2194 * 'current'
2195 * This index points to the current available resource for use. For
2196 * example in Rx process this index will point to the descriptor
2197 * that will be passed to the user upon calling the receive
2198 * routine. In Tx process, this index will point to the descriptor
2199 * that will be assigned with the user packet info and transmitted.
2200 * 'used'
2201 * This index points to the descriptor that need to restore its
2202 * resources. For example in Rx process, using the Rx buffer return
2203 * API will attach the buffer returned in packet info to the
2204 * descriptor pointed by 'used'. In Tx process, using the Tx
2205 * descriptor return will merely return the user packet info with
2206 * the command status of the transmitted buffer pointed by the
2207 * 'used' index. Nevertheless, it is essential to use this routine
2208 * to update the 'used' index.
2209 * 'first'
2210 * This index supports Tx Scatter-Gather. It points to the first
2211 * descriptor of a packet assembled of multiple buffers. For
2212 * example when in middle of Such packet we have a Tx resource
2213 * error the 'curr' index get the value of 'first' to indicate
2214 * that the ring returned to its state before trying to transmit
2215 * this packet.
2216 *
2217 * Receive operation:
2218 * The eth_port_receive API set the packet information struct,
2219 * passed by the caller, with received information from the
2220 * 'current' SDMA descriptor.
2221 * It is the user responsibility to return this resource back
2222 * to the Rx descriptor ring to enable the reuse of this source.
2223 * Return Rx resource is done using the eth_rx_return_buff API.
2224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * Prior to calling the initialization routine eth_port_init() the user
2226 * must set the following fields under mv643xx_private struct:
2227 * port_num User Ethernet port number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 * port_config User port configuration value.
2229 * port_config_extend User port config extend value.
2230 * port_sdma_config User port SDMA config value.
2231 * port_serial_control User port serial control value.
2232 *
2233 * This driver data flow is done using the struct pkt_info which
2234 * is a unified struct for Rx and Tx operations:
2235 *
2236 * byte_cnt Tx/Rx descriptor buffer byte count.
2237 * l4i_chk CPU provided TCP Checksum. For Tx operation
2238 * only.
2239 * cmd_sts Tx/Rx descriptor command status.
2240 * buf_ptr Tx/Rx descriptor buffer pointer.
2241 * return_info Tx/Rx user resource return information.
2242 */
2243
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244/* Ethernet Port routines */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002245static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
2246 int table, unsigned char entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247
2248/*
2249 * eth_port_init - Initialize the Ethernet port driver
2250 *
2251 * DESCRIPTION:
2252 * This function prepares the ethernet port to start its activity:
2253 * 1) Completes the ethernet port driver struct initialization toward port
2254 * start routine.
2255 * 2) Resets the device to a quiescent state in case of warm reboot.
2256 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2257 * 4) Clean MAC tables. The reset status of those tables is unknown.
2258 * 5) Set PHY address.
2259 * Note: Call this routine prior to eth_port_start routine and after
2260 * setting user values in the user fields of Ethernet port control
2261 * struct.
2262 *
2263 * INPUT:
2264 * struct mv643xx_private *mp Ethernet port control struct
2265 *
2266 * OUTPUT:
2267 * See description.
2268 *
2269 * RETURN:
2270 * None.
2271 */
2272static void eth_port_init(struct mv643xx_private *mp)
2273{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 mp->rx_resource_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002276 eth_port_reset(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002278 eth_port_init_mac_tables(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279}
2280
2281/*
2282 * eth_port_start - Start the Ethernet port activity.
2283 *
2284 * DESCRIPTION:
2285 * This routine prepares the Ethernet port for Rx and Tx activity:
2286 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2287 * has been initialized a descriptor's ring (using
2288 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2289 * 2. Initialize and enable the Ethernet configuration port by writing to
2290 * the port's configuration and command registers.
2291 * 3. Initialize and enable the SDMA by writing to the SDMA's
2292 * configuration and command registers. After completing these steps,
2293 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2294 *
2295 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2296 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2297 * and ether_init_rx_desc_ring for Rx queues).
2298 *
2299 * INPUT:
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07002300 * dev - a pointer to the required interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 *
2302 * OUTPUT:
2303 * Ethernet port is ready to receive and transmit.
2304 *
2305 * RETURN:
2306 * None.
2307 */
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07002308static void eth_port_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07002310 struct mv643xx_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 unsigned int port_num = mp->port_num;
2312 int tx_curr_desc, rx_curr_desc;
James Chapmand0412d92006-01-27 01:15:30 -07002313 u32 pscr;
2314 struct ethtool_cmd ethtool_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 /* Assignment of Tx CTRP of given queue */
2317 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002318 wrl(mp, TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2320
2321 /* Assignment of Rx CRDP of given queue */
2322 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002323 wrl(mp, RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2325
2326 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002327 eth_port_uc_addr_set(mp, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /* Assign port configuration and command. */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002330 wrl(mp, PORT_CONFIG_REG(port_num),
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002331 PORT_CONFIG_DEFAULT_VALUE);
Dale Farnsworth01999872006-01-27 01:18:01 -07002332
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002333 wrl(mp, PORT_CONFIG_EXTEND_REG(port_num),
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002334 PORT_CONFIG_EXTEND_DEFAULT_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002336 pscr = rdl(mp, PORT_SERIAL_CONTROL_REG(port_num));
Dale Farnsworth01999872006-01-27 01:18:01 -07002337
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002338 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002339 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), pscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002341 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2342 DISABLE_AUTO_NEG_SPEED_GMII |
2343 DISABLE_AUTO_NEG_FOR_DUPLX |
2344 DO_NOT_FORCE_LINK_FAIL |
2345 SERIAL_PORT_CONTROL_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002347 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), pscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002349 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002350 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), pscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
2352 /* Assign port SDMA configuration */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002353 wrl(mp, SDMA_CONFIG_REG(port_num),
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002354 PORT_SDMA_CONFIG_DEFAULT_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356 /* Enable port Rx. */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002357 mv643xx_eth_port_enable_rx(mp, ETH_RX_QUEUES_ENABLED);
Dale Farnsworth8f543712005-09-02 12:34:35 -07002358
2359 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002360 wrl(mp, MAXIMUM_TRANSMIT_UNIT(port_num), 0);
James Chapmand0412d92006-01-27 01:15:30 -07002361
2362 /* save phy settings across reset */
2363 mv643xx_get_settings(dev, &ethtool_cmd);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002364 ethernet_phy_reset(mp);
James Chapmand0412d92006-01-27 01:15:30 -07002365 mv643xx_set_settings(dev, &ethtool_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366}
2367
2368/*
Gabriel Paubert144213d2007-03-23 12:07:26 -07002369 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002371static void eth_port_uc_addr_set(struct mv643xx_private *mp,
2372 unsigned char *p_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002374 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 unsigned int mac_h;
2376 unsigned int mac_l;
Dale Farnsworthcf4086c2006-01-27 01:07:48 -07002377 int table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 mac_l = (p_addr[4] << 8) | (p_addr[5]);
2380 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
2381 (p_addr[3] << 0);
2382
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002383 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
2384 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Gabriel Paubert144213d2007-03-23 12:07:26 -07002386 /* Accept frames with this address */
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002387 table = DA_FILTER_UNICAST_TABLE_BASE(port_num);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002388 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389}
2390
2391/*
Gabriel Paubert144213d2007-03-23 12:07:26 -07002392 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002394static void eth_port_uc_addr_get(struct mv643xx_private *mp,
2395 unsigned char *p_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002397 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 unsigned int mac_h;
2399 unsigned int mac_l;
2400
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002401 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
2402 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 p_addr[0] = (mac_h >> 24) & 0xff;
2405 p_addr[1] = (mac_h >> 16) & 0xff;
2406 p_addr[2] = (mac_h >> 8) & 0xff;
2407 p_addr[3] = mac_h & 0xff;
2408 p_addr[4] = (mac_l >> 8) & 0xff;
2409 p_addr[5] = mac_l & 0xff;
2410}
2411
2412/*
Dale Farnsworth16e03012006-01-16 16:50:02 -07002413 * The entries in each table are indexed by a hash of a packet's MAC
2414 * address. One bit in each entry determines whether the packet is
2415 * accepted. There are 4 entries (each 8 bits wide) in each register
2416 * of the table. The bits in each entry are defined as follows:
2417 * 0 Accept=1, Drop=0
2418 * 3-1 Queue (ETH_Q0=0)
2419 * 7-4 Reserved = 0;
2420 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002421static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
2422 int table, unsigned char entry)
Dale Farnsworth16e03012006-01-16 16:50:02 -07002423{
2424 unsigned int table_reg;
2425 unsigned int tbl_offset;
2426 unsigned int reg_offset;
2427
2428 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
2429 reg_offset = entry % 4; /* Entry offset within the register */
2430
2431 /* Set "accepts frame bit" at specified table entry */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002432 table_reg = rdl(mp, table + tbl_offset);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002433 table_reg |= 0x01 << (8 * reg_offset);
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002434 wrl(mp, table + tbl_offset, table_reg);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002435}
2436
2437/*
2438 * eth_port_mc_addr - Multicast address settings.
2439 *
2440 * The MV device supports multicast using two tables:
2441 * 1) Special Multicast Table for MAC addresses of the form
2442 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
2443 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
2444 * Table entries in the DA-Filter table.
2445 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
2446 * is used as an index to the Other Multicast Table entries in the
2447 * DA-Filter table. This function calculates the CRC-8bit value.
2448 * In either case, eth_port_set_filter_table_entry() is then called
2449 * to set to set the actual table entry.
2450 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002451static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
Dale Farnsworth16e03012006-01-16 16:50:02 -07002452{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002453 unsigned int port_num = mp->port_num;
Dale Farnsworth16e03012006-01-16 16:50:02 -07002454 unsigned int mac_h;
2455 unsigned int mac_l;
2456 unsigned char crc_result = 0;
2457 int table;
2458 int mac_array[48];
2459 int crc[8];
2460 int i;
2461
2462 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
2463 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002464 table = DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(port_num);
2465 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002466 return;
2467 }
2468
2469 /* Calculate CRC-8 out of the given address */
2470 mac_h = (p_addr[0] << 8) | (p_addr[1]);
2471 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
2472 (p_addr[4] << 8) | (p_addr[5] << 0);
2473
2474 for (i = 0; i < 32; i++)
2475 mac_array[i] = (mac_l >> i) & 0x1;
2476 for (i = 32; i < 48; i++)
2477 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
2478
2479 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
2480 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
2481 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
2482 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
2483 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
2484
2485 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
2486 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
2487 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
2488 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
2489 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
2490 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
2491 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
2492
2493 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
2494 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
2495 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
2496 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
2497 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
2498 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
2499
2500 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
2501 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
2502 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
2503 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
2504 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
2505 mac_array[3] ^ mac_array[2] ^ mac_array[1];
2506
2507 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
2508 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
2509 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
2510 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
2511 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
2512 mac_array[3] ^ mac_array[2];
2513
2514 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
2515 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
2516 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
2517 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
2518 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
2519 mac_array[4] ^ mac_array[3];
2520
2521 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
2522 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
2523 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
2524 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
2525 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
2526 mac_array[4];
2527
2528 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
2529 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
2530 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
2531 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
2532 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
2533
2534 for (i = 0; i < 8; i++)
2535 crc_result = crc_result | (crc[i] << i);
2536
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002537 table = DA_FILTER_OTHER_MULTICAST_TABLE_BASE(port_num);
2538 eth_port_set_filter_table_entry(mp, table, crc_result);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002539}
2540
2541/*
2542 * Set the entire multicast list based on dev->mc_list.
2543 */
2544static void eth_port_set_multicast_list(struct net_device *dev)
2545{
2546
2547 struct dev_mc_list *mc_list;
2548 int i;
2549 int table_index;
2550 struct mv643xx_private *mp = netdev_priv(dev);
2551 unsigned int eth_port_num = mp->port_num;
2552
2553 /* If the device is in promiscuous mode or in all multicast mode,
2554 * we will fully populate both multicast tables with accept.
2555 * This is guaranteed to yield a match on all multicast addresses...
2556 */
2557 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
2558 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002559 /* Set all entries in DA filter special multicast
2560 * table (Ex_dFSMT)
2561 * Set for ETH_Q0 for now
2562 * Bits
2563 * 0 Accept=1, Drop=0
2564 * 3-1 Queue ETH_Q0=0
2565 * 7-4 Reserved = 0;
2566 */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002567 wrl(mp, DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002568
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002569 /* Set all entries in DA filter other multicast
2570 * table (Ex_dFOMT)
2571 * Set for ETH_Q0 for now
2572 * Bits
2573 * 0 Accept=1, Drop=0
2574 * 3-1 Queue ETH_Q0=0
2575 * 7-4 Reserved = 0;
2576 */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002577 wrl(mp, DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002578 }
Dale Farnsworth16e03012006-01-16 16:50:02 -07002579 return;
2580 }
2581
2582 /* We will clear out multicast tables every time we get the list.
2583 * Then add the entire new list...
2584 */
2585 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2586 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002587 wrl(mp, DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
Dale Farnsworth16e03012006-01-16 16:50:02 -07002588 (eth_port_num) + table_index, 0);
2589
2590 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002591 wrl(mp, DA_FILTER_OTHER_MULTICAST_TABLE_BASE
Dale Farnsworth16e03012006-01-16 16:50:02 -07002592 (eth_port_num) + table_index, 0);
2593 }
2594
2595 /* Get pointer to net_device multicast list and add each one... */
2596 for (i = 0, mc_list = dev->mc_list;
2597 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
2598 i++, mc_list = mc_list->next)
2599 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002600 eth_port_mc_addr(mp, mc_list->dmi_addr);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002601}
2602
2603/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
2605 *
2606 * DESCRIPTION:
2607 * Go through all the DA filter tables (Unicast, Special Multicast &
2608 * Other Multicast) and set each entry to 0.
2609 *
2610 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002611 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 *
2613 * OUTPUT:
2614 * Multicast and Unicast packets are rejected.
2615 *
2616 * RETURN:
2617 * None.
2618 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002619static void eth_port_init_mac_tables(struct mv643xx_private *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002621 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 int table_index;
2623
2624 /* Clear DA filter unicast table (Ex_dFUT) */
2625 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002626 wrl(mp, DA_FILTER_UNICAST_TABLE_BASE(port_num) +
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002627 table_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
2629 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2630 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002631 wrl(mp, DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(port_num) +
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002632 table_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002634 wrl(mp, DA_FILTER_OTHER_MULTICAST_TABLE_BASE(port_num) +
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002635 table_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637}
2638
2639/*
2640 * eth_clear_mib_counters - Clear all MIB counters
2641 *
2642 * DESCRIPTION:
2643 * This function clears all MIB counters of a specific ethernet port.
2644 * A read from the MIB counter will reset the counter.
2645 *
2646 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002647 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
2649 * OUTPUT:
2650 * After reading all MIB counters, the counters resets.
2651 *
2652 * RETURN:
2653 * MIB counter value.
2654 *
2655 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002656static void eth_clear_mib_counters(struct mv643xx_private *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002658 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 int i;
2660
2661 /* Perform dummy reads from MIB counters */
2662 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
2663 i += 4)
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002664 rdl(mp, MIB_COUNTERS_BASE(port_num) + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
2667static inline u32 read_mib(struct mv643xx_private *mp, int offset)
2668{
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002669 return rdl(mp, MIB_COUNTERS_BASE(mp->port_num) + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671
2672static void eth_update_mib_counters(struct mv643xx_private *mp)
2673{
2674 struct mv643xx_mib_counters *p = &mp->mib_counters;
2675 int offset;
2676
2677 p->good_octets_received +=
2678 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
2679 p->good_octets_received +=
2680 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
2681
2682 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
2683 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
2684 offset += 4)
Maxime Bizon70fbf322006-10-03 10:27:10 -07002685 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
2688 p->good_octets_sent +=
2689 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
2690
2691 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
2692 offset <= ETH_MIB_LATE_COLLISION;
2693 offset += 4)
Maxime Bizon70fbf322006-10-03 10:27:10 -07002694 *(u32 *)((char *)p + offset) += read_mib(mp, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
2696
2697/*
2698 * ethernet_phy_detect - Detect whether a phy is present
2699 *
2700 * DESCRIPTION:
2701 * This function tests whether there is a PHY present on
2702 * the specified port.
2703 *
2704 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002705 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 *
2707 * OUTPUT:
2708 * None
2709 *
2710 * RETURN:
2711 * 0 on success
2712 * -ENODEV on failure
2713 *
2714 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002715static int ethernet_phy_detect(struct mv643xx_private *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716{
2717 unsigned int phy_reg_data0;
2718 int auto_neg;
2719
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002720 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 auto_neg = phy_reg_data0 & 0x1000;
2722 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002723 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002725 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if ((phy_reg_data0 & 0x1000) == auto_neg)
2727 return -ENODEV; /* change didn't take */
2728
2729 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002730 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 return 0;
2732}
2733
2734/*
2735 * ethernet_phy_get - Get the ethernet port PHY address.
2736 *
2737 * DESCRIPTION:
2738 * This routine returns the given ethernet port PHY address.
2739 *
2740 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002741 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 *
2743 * OUTPUT:
2744 * None.
2745 *
2746 * RETURN:
2747 * PHY address.
2748 *
2749 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002750static int ethernet_phy_get(struct mv643xx_private *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751{
2752 unsigned int reg_data;
2753
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002754 reg_data = rdl(mp, PHY_ADDR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002756 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
2759/*
2760 * ethernet_phy_set - Set the ethernet port PHY address.
2761 *
2762 * DESCRIPTION:
2763 * This routine sets the given ethernet port PHY address.
2764 *
2765 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002766 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 * int phy_addr PHY address.
2768 *
2769 * OUTPUT:
2770 * None.
2771 *
2772 * RETURN:
2773 * None.
2774 *
2775 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002776static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 u32 reg_data;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002779 int addr_shift = 5 * mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002781 reg_data = rdl(mp, PHY_ADDR_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 reg_data &= ~(0x1f << addr_shift);
2783 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002784 wrl(mp, PHY_ADDR_REG, reg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
2786
2787/*
2788 * ethernet_phy_reset - Reset Ethernet port PHY.
2789 *
2790 * DESCRIPTION:
2791 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2792 *
2793 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002794 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 *
2796 * OUTPUT:
2797 * The PHY is reset.
2798 *
2799 * RETURN:
2800 * None.
2801 *
2802 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002803static void ethernet_phy_reset(struct mv643xx_private *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804{
2805 unsigned int phy_reg_data;
2806
2807 /* Reset the PHY */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002808 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002810 eth_port_write_smi_reg(mp, 0, phy_reg_data);
James Chapmand0412d92006-01-27 01:15:30 -07002811
2812 /* wait for PHY to come out of reset */
2813 do {
2814 udelay(1);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002815 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
James Chapmand0412d92006-01-27 01:15:30 -07002816 } while (phy_reg_data & 0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817}
2818
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002819static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002820 unsigned int queues)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002821{
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002822 wrl(mp, TRANSMIT_QUEUE_COMMAND_REG(mp->port_num), queues);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002823}
2824
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002825static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002826 unsigned int queues)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002827{
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002828 wrl(mp, RECEIVE_QUEUE_COMMAND_REG(mp->port_num), queues);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002829}
2830
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002831static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002832{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002833 unsigned int port_num = mp->port_num;
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002834 u32 queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002835
2836 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002837 queues = rdl(mp, TRANSMIT_QUEUE_COMMAND_REG(port_num)) & 0xFF;
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002838 if (queues) {
2839 /* Issue stop command for active queues only */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002840 wrl(mp, TRANSMIT_QUEUE_COMMAND_REG(port_num), (queues << 8));
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002841
2842 /* Wait for all Tx activity to terminate. */
2843 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002844 while (rdl(mp, TRANSMIT_QUEUE_COMMAND_REG(port_num)) & 0xFF)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002845 udelay(PHY_WAIT_MICRO_SECONDS);
2846
2847 /* Wait for Tx FIFO to empty */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002848 while (rdl(mp, PORT_STATUS_REG(port_num)) &
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002849 ETH_PORT_TX_FIFO_EMPTY)
2850 udelay(PHY_WAIT_MICRO_SECONDS);
2851 }
2852
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002853 return queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002854}
2855
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002856static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002857{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002858 unsigned int port_num = mp->port_num;
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002859 u32 queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002860
2861 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002862 queues = rdl(mp, RECEIVE_QUEUE_COMMAND_REG(port_num)) & 0xFF;
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002863 if (queues) {
2864 /* Issue stop command for active queues only */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002865 wrl(mp, RECEIVE_QUEUE_COMMAND_REG(port_num), (queues << 8));
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002866
2867 /* Wait for all Rx activity to terminate. */
2868 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002869 while (rdl(mp, RECEIVE_QUEUE_COMMAND_REG(port_num)) & 0xFF)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002870 udelay(PHY_WAIT_MICRO_SECONDS);
2871 }
2872
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002873 return queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002874}
2875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876/*
2877 * eth_port_reset - Reset Ethernet port
2878 *
2879 * DESCRIPTION:
2880 * This routine resets the chip by aborting any SDMA engine activity and
2881 * clearing the MIB counters. The Receiver and the Transmit unit are in
2882 * idle state after this command is performed and the port is disabled.
2883 *
2884 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002885 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 *
2887 * OUTPUT:
2888 * Channel activity is halted.
2889 *
2890 * RETURN:
2891 * None.
2892 *
2893 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002894static void eth_port_reset(struct mv643xx_private *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002896 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 unsigned int reg_data;
2898
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002899 mv643xx_eth_port_disable_tx(mp);
2900 mv643xx_eth_port_disable_rx(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
2902 /* Clear all MIB counters */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002903 eth_clear_mib_counters(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
2905 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002906 reg_data = rdl(mp, PORT_SERIAL_CONTROL_REG(port_num));
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002907 reg_data &= ~(SERIAL_PORT_ENABLE |
2908 DO_NOT_FORCE_LINK_FAIL |
2909 FORCE_LINK_PASS);
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002910 wrl(mp, PORT_SERIAL_CONTROL_REG(port_num), reg_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 * eth_port_read_smi_reg - Read PHY registers
2916 *
2917 * DESCRIPTION:
2918 * This routine utilize the SMI interface to interact with the PHY in
2919 * order to perform PHY register read.
2920 *
2921 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002922 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 * unsigned int phy_reg PHY register address offset.
2924 * unsigned int *value Register value buffer.
2925 *
2926 * OUTPUT:
2927 * Write the value of a specified PHY register into given buffer.
2928 *
2929 * RETURN:
2930 * false if the PHY is busy or read data is not in valid state.
2931 * true otherwise.
2932 *
2933 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002934static void eth_port_read_smi_reg(struct mv643xx_private *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 unsigned int phy_reg, unsigned int *value)
2936{
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002937 int phy_addr = ethernet_phy_get(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 unsigned long flags;
2939 int i;
2940
2941 /* the SMI register is a shared resource */
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002942 spin_lock_irqsave(&mp->shared->phy_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 /* wait for the SMI register to become available */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002945 for (i = 0; rdl(mp, SMI_REG) & ETH_SMI_BUSY; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 if (i == PHY_WAIT_ITERATIONS) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002947 printk("%s: PHY busy timeout\n", mp->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 goto out;
2949 }
2950 udelay(PHY_WAIT_MICRO_SECONDS);
2951 }
2952
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002953 wrl(mp, SMI_REG,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
2955
2956 /* now wait for the data to be valid */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002957 for (i = 0; !(rdl(mp, SMI_REG) & ETH_SMI_READ_VALID); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 if (i == PHY_WAIT_ITERATIONS) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002959 printk("%s: PHY read timeout\n", mp->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 goto out;
2961 }
2962 udelay(PHY_WAIT_MICRO_SECONDS);
2963 }
2964
Lennert Buytenhekec69d652008-03-18 11:38:05 -07002965 *value = rdl(mp, SMI_REG) & 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966out:
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002967 spin_unlock_irqrestore(&mp->shared->phy_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
2970/*
2971 * eth_port_write_smi_reg - Write to PHY registers
2972 *
2973 * DESCRIPTION:
2974 * This routine utilize the SMI interface to interact with the PHY in
2975 * order to perform writes to PHY registers.
2976 *
2977 * INPUT:
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002978 * struct mv643xx_private *mp Ethernet Port.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 * unsigned int phy_reg PHY register address offset.
2980 * unsigned int value Register value.
2981 *
2982 * OUTPUT:
2983 * Write the given value to the specified PHY register.
2984 *
2985 * RETURN:
2986 * false if the PHY is busy.
2987 * true otherwise.
2988 *
2989 */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002990static void eth_port_write_smi_reg(struct mv643xx_private *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 unsigned int phy_reg, unsigned int value)
2992{
2993 int phy_addr;
2994 int i;
2995 unsigned long flags;
2996
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002997 phy_addr = ethernet_phy_get(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
2999 /* the SMI register is a shared resource */
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02003000 spin_lock_irqsave(&mp->shared->phy_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 /* wait for the SMI register to become available */
Lennert Buytenhekec69d652008-03-18 11:38:05 -07003003 for (i = 0; rdl(mp, SMI_REG) & ETH_SMI_BUSY; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (i == PHY_WAIT_ITERATIONS) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07003005 printk("%s: PHY busy timeout\n", mp->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 goto out;
3007 }
3008 udelay(PHY_WAIT_MICRO_SECONDS);
3009 }
3010
Lennert Buytenhekec69d652008-03-18 11:38:05 -07003011 wrl(mp, SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 ETH_SMI_OPCODE_WRITE | (value & 0xffff));
3013out:
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02003014 spin_unlock_irqrestore(&mp->shared->phy_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017/*
James Chapmanc28a4f82006-01-27 01:13:15 -07003018 * Wrappers for MII support library.
3019 */
3020static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
3021{
James Chapmanc28a4f82006-01-27 01:13:15 -07003022 struct mv643xx_private *mp = netdev_priv(dev);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003023 int val;
James Chapmanc28a4f82006-01-27 01:13:15 -07003024
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003025 eth_port_read_smi_reg(mp, location, &val);
James Chapmanc28a4f82006-01-27 01:13:15 -07003026 return val;
3027}
3028
3029static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
3030{
3031 struct mv643xx_private *mp = netdev_priv(dev);
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07003032 eth_port_write_smi_reg(mp, location, val);
James Chapmanc28a4f82006-01-27 01:13:15 -07003033}
3034
3035/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 * eth_port_receive - Get received information from Rx ring.
3037 *
3038 * DESCRIPTION:
3039 * This routine returns the received data to the caller. There is no
3040 * data copying during routine operation. All information is returned
3041 * using pointer to packet information struct passed from the caller.
3042 * If the routine exhausts Rx ring resources then the resource error flag
3043 * is set.
3044 *
3045 * INPUT:
3046 * struct mv643xx_private *mp Ethernet Port Control srtuct.
3047 * struct pkt_info *p_pkt_info User packet buffer.
3048 *
3049 * OUTPUT:
3050 * Rx ring current and used indexes are updated.
3051 *
3052 * RETURN:
3053 * ETH_ERROR in case the routine can not access Rx desc ring.
3054 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
3055 * ETH_END_OF_JOB if there is no received data.
3056 * ETH_OK otherwise.
3057 */
3058static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
3059 struct pkt_info *p_pkt_info)
3060{
3061 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
3062 volatile struct eth_rx_desc *p_rx_desc;
3063 unsigned int command_status;
Dale Farnsworth8f518702006-01-16 16:56:30 -07003064 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065
3066 /* Do not process Rx ring in case of Rx ring resource error */
3067 if (mp->rx_resource_err)
3068 return ETH_QUEUE_FULL;
3069
Dale Farnsworth8f518702006-01-16 16:56:30 -07003070 spin_lock_irqsave(&mp->lock, flags);
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 /* Get the Rx Desc ring 'curr and 'used' indexes */
3073 rx_curr_desc = mp->rx_curr_desc_q;
3074 rx_used_desc = mp->rx_used_desc_q;
3075
3076 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
3077
3078 /* The following parameters are used to save readings from memory */
3079 command_status = p_rx_desc->cmd_sts;
3080 rmb();
3081
3082 /* Nothing to receive... */
Dale Farnsworth8f518702006-01-16 16:56:30 -07003083 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
3084 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return ETH_END_OF_JOB;
Dale Farnsworth8f518702006-01-16 16:56:30 -07003086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087
3088 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
3089 p_pkt_info->cmd_sts = command_status;
3090 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
3091 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
3092 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
3093
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003094 /*
3095 * Clean the return info field to indicate that the
3096 * packet has been moved to the upper layers
3097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 mp->rx_skb[rx_curr_desc] = NULL;
3099
3100 /* Update current index in data structure */
3101 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
3102 mp->rx_curr_desc_q = rx_next_curr_desc;
3103
3104 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
3105 if (rx_next_curr_desc == rx_used_desc)
3106 mp->rx_resource_err = 1;
3107
Dale Farnsworth8f518702006-01-16 16:56:30 -07003108 spin_unlock_irqrestore(&mp->lock, flags);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return ETH_OK;
3111}
3112
3113/*
3114 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
3115 *
3116 * DESCRIPTION:
3117 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
3118 * next 'used' descriptor and attached the returned buffer to it.
3119 * In case the Rx ring was in "resource error" condition, where there are
3120 * no available Rx resources, the function resets the resource error flag.
3121 *
3122 * INPUT:
3123 * struct mv643xx_private *mp Ethernet Port Control srtuct.
3124 * struct pkt_info *p_pkt_info Information on returned buffer.
3125 *
3126 * OUTPUT:
3127 * New available Rx resource in Rx descriptor ring.
3128 *
3129 * RETURN:
3130 * ETH_ERROR in case the routine can not access Rx desc ring.
3131 * ETH_OK otherwise.
3132 */
3133static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
3134 struct pkt_info *p_pkt_info)
3135{
3136 int used_rx_desc; /* Where to return Rx resource */
3137 volatile struct eth_rx_desc *p_used_rx_desc;
Dale Farnsworth8f518702006-01-16 16:56:30 -07003138 unsigned long flags;
3139
3140 spin_lock_irqsave(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142 /* Get 'used' Rx descriptor */
3143 used_rx_desc = mp->rx_used_desc_q;
3144 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
3145
3146 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
3147 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
3148 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
3149
3150 /* Flush the write pipe */
3151
3152 /* Return the descriptor to DMA ownership */
3153 wmb();
3154 p_used_rx_desc->cmd_sts =
3155 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
3156 wmb();
3157
3158 /* Move the used descriptor pointer to the next descriptor */
3159 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
3160
3161 /* Any Rx return cancels the Rx resource error status */
3162 mp->rx_resource_err = 0;
3163
Dale Farnsworth8f518702006-01-16 16:56:30 -07003164 spin_unlock_irqrestore(&mp->lock, flags);
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return ETH_OK;
3167}
3168
3169/************* Begin ethtool support *************************/
3170
3171struct mv643xx_stats {
3172 char stat_string[ETH_GSTRING_LEN];
3173 int sizeof_stat;
3174 int stat_offset;
3175};
3176
Julia Lawall7a6d84c2008-03-04 15:19:19 -08003177#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003178 offsetof(struct mv643xx_private, m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
3181 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
3182 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
3183 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
3184 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
3185 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
3186 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
3187 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
3188 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
3189 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
3190 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
3191 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
3192 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
3193 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
3194 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
3195 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
3196 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
3197 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
3198 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
3199 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
3200 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
3201 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
3202 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
3203 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
3204 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
3205 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
3206 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
3207 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
3208 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
3209 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
3210 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
3211 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
3212 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
3213 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
3214 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
3215 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
3216 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
3217 { "collision", MV643XX_STAT(mib_counters.collision) },
3218 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
3219};
3220
Denis Chengff8ac602007-09-02 18:30:18 +08003221#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003223static void mv643xx_get_drvinfo(struct net_device *netdev,
3224 struct ethtool_drvinfo *drvinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225{
3226 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
3227 strncpy(drvinfo->version, mv643xx_driver_version, 32);
3228 strncpy(drvinfo->fw_version, "N/A", 32);
3229 strncpy(drvinfo->bus_info, "mv643xx", 32);
3230 drvinfo->n_stats = MV643XX_STATS_LEN;
3231}
3232
Jeff Garzikb9f2c042007-10-03 18:07:32 -07003233static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234{
Jeff Garzikb9f2c042007-10-03 18:07:32 -07003235 switch (sset) {
3236 case ETH_SS_STATS:
3237 return MV643XX_STATS_LEN;
3238 default:
3239 return -EOPNOTSUPP;
3240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241}
3242
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003243static void mv643xx_get_ethtool_stats(struct net_device *netdev,
3244 struct ethtool_stats *stats, uint64_t *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245{
3246 struct mv643xx_private *mp = netdev->priv;
3247 int i;
3248
3249 eth_update_mib_counters(mp);
3250
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003251 for (i = 0; i < MV643XX_STATS_LEN; i++) {
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003252 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003253 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
3255 }
3256}
3257
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003258static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
3259 uint8_t *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260{
3261 int i;
3262
3263 switch(stringset) {
3264 case ETH_SS_STATS:
3265 for (i=0; i < MV643XX_STATS_LEN; i++) {
Dale Farnsworthb4de9052006-01-27 01:04:43 -07003266 memcpy(data + i * ETH_GSTRING_LEN,
3267 mv643xx_gstrings_stats[i].stat_string,
3268 ETH_GSTRING_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 }
3270 break;
3271 }
3272}
3273
James Chapmand0412d92006-01-27 01:15:30 -07003274static u32 mv643xx_eth_get_link(struct net_device *dev)
3275{
3276 struct mv643xx_private *mp = netdev_priv(dev);
3277
3278 return mii_link_ok(&mp->mii);
3279}
3280
3281static int mv643xx_eth_nway_restart(struct net_device *dev)
3282{
3283 struct mv643xx_private *mp = netdev_priv(dev);
3284
3285 return mii_nway_restart(&mp->mii);
3286}
3287
3288static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
3289{
3290 struct mv643xx_private *mp = netdev_priv(dev);
3291
3292 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
3293}
3294
Jeff Garzik7282d492006-09-13 14:30:00 -04003295static const struct ethtool_ops mv643xx_ethtool_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296 .get_settings = mv643xx_get_settings,
James Chapmand0412d92006-01-27 01:15:30 -07003297 .set_settings = mv643xx_set_settings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 .get_drvinfo = mv643xx_get_drvinfo,
James Chapmand0412d92006-01-27 01:15:30 -07003299 .get_link = mv643xx_eth_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 .set_sg = ethtool_op_set_sg,
Dale Farnsworth194b2d02007-10-20 12:16:27 -07003301 .get_sset_count = mv643xx_get_sset_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 .get_ethtool_stats = mv643xx_get_ethtool_stats,
James Chapmand0412d92006-01-27 01:15:30 -07003303 .get_strings = mv643xx_get_strings,
James Chapmand0412d92006-01-27 01:15:30 -07003304 .nway_reset = mv643xx_eth_nway_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305};
3306
3307/************* End ethtool support *************************/