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