blob: 624f807755061c3a87db680f921f15d0ae18bcd8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Lennert Buytenhek9c1bbdf2007-10-19 04:11:03 +02002 * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
Lennert Buytenhek4547fa62008-03-18 11:40:14 -07006 * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il>
7 * Rabeeh Khoury <rabeeh@marvell.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a182006-01-05 22:45:45 -080010 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
13 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070014 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070015 * Dale Farnsworth <dale@farnsworth.org>
16 *
17 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
18 * <sjhill@realitydiluted.com>
19 *
Lennert Buytenhek4547fa62008-03-18 11:40:14 -070020 * Copyright (C) 2007-2008 Marvell Semiconductor
21 * Lennert Buytenhek <buytenh@marvell.com>
22 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * as published by the Free Software Foundation; either version 2
26 * of the License, or (at your option) any later version.
27 *
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
32 *
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 */
Lennert Buytenheka779d382008-06-01 00:54:05 +020037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/init.h>
39#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050040#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/tcp.h>
42#include <linux/udp.h>
43#include <linux/etherdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/delay.h>
45#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010046#include <linux/platform_device.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020047#include <linux/module.h>
48#include <linux/kernel.h>
49#include <linux/spinlock.h>
50#include <linux/workqueue.h>
51#include <linux/mii.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020052#include <linux/mv643xx_eth.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/io.h>
54#include <asm/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/system.h>
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020056
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +020057static char mv643xx_driver_name[] = "mv643xx_eth";
58static char mv643xx_driver_version[] = "1.0";
59
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020060#define MV643XX_CHECKSUM_OFFLOAD_TX
61#define MV643XX_NAPI
62#define MV643XX_TX_FAST_REFILL
63#undef MV643XX_COAL
64
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020065#define MV643XX_TX_COAL 100
66#ifdef MV643XX_COAL
67#define MV643XX_RX_COAL 100
68#endif
69
70#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
71#define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1)
72#else
73#define MAX_DESCS_PER_SKB 1
74#endif
75
76#define ETH_VLAN_HLEN 4
77#define ETH_FCS_LEN 4
78#define ETH_HW_IP_ALIGN 2 /* hw aligns IP header */
79#define ETH_WRAPPER_LEN (ETH_HW_IP_ALIGN + ETH_HLEN + \
80 ETH_VLAN_HLEN + ETH_FCS_LEN)
81#define ETH_RX_SKB_SIZE (dev->mtu + ETH_WRAPPER_LEN + \
82 dma_get_cache_alignment())
83
84/*
85 * Registers shared between all ports.
86 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020087#define PHY_ADDR 0x0000
88#define SMI_REG 0x0004
89#define WINDOW_BASE(w) (0x0200 + ((w) << 3))
90#define WINDOW_SIZE(w) (0x0204 + ((w) << 3))
91#define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2))
92#define WINDOW_BAR_ENABLE 0x0290
93#define WINDOW_PROTECT(w) (0x0294 + ((w) << 4))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +020094
95/*
96 * Per-port registers.
97 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +020098#define PORT_CONFIG(p) (0x0400 + ((p) << 10))
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +020099#define UNICAST_PROMISCUOUS_MODE 0x00000001
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200100#define PORT_CONFIG_EXT(p) (0x0404 + ((p) << 10))
101#define MAC_ADDR_LOW(p) (0x0414 + ((p) << 10))
102#define MAC_ADDR_HIGH(p) (0x0418 + ((p) << 10))
103#define SDMA_CONFIG(p) (0x041c + ((p) << 10))
104#define PORT_SERIAL_CONTROL(p) (0x043c + ((p) << 10))
105#define PORT_STATUS(p) (0x0444 + ((p) << 10))
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200106#define TX_FIFO_EMPTY 0x00000400
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200107#define TXQ_COMMAND(p) (0x0448 + ((p) << 10))
108#define TX_BW_MTU(p) (0x0458 + ((p) << 10))
109#define INT_CAUSE(p) (0x0460 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200110#define INT_RX 0x00000804
111#define INT_EXT 0x00000002
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200112#define INT_CAUSE_EXT(p) (0x0464 + ((p) << 10))
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200113#define INT_EXT_LINK 0x00100000
114#define INT_EXT_PHY 0x00010000
115#define INT_EXT_TX_ERROR_0 0x00000100
116#define INT_EXT_TX_0 0x00000001
117#define INT_EXT_TX 0x00000101
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200118#define INT_MASK(p) (0x0468 + ((p) << 10))
119#define INT_MASK_EXT(p) (0x046c + ((p) << 10))
120#define TX_FIFO_URGENT_THRESHOLD(p) (0x0474 + ((p) << 10))
121#define RXQ_CURRENT_DESC_PTR(p) (0x060c + ((p) << 10))
122#define RXQ_COMMAND(p) (0x0680 + ((p) << 10))
123#define TXQ_CURRENT_DESC_PTR(p) (0x06c0 + ((p) << 10))
124#define MIB_COUNTERS(p) (0x1000 + ((p) << 7))
125#define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10))
126#define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10))
127#define UNICAST_TABLE(p) (0x1600 + ((p) << 10))
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200128
Lennert Buytenhek2679a552008-06-01 01:18:58 +0200129
130/*
131 * SDMA configuration register.
132 */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200133#define RX_BURST_SIZE_4_64BIT (2 << 1)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200134#define BLM_RX_NO_SWAP (1 << 4)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200135#define BLM_TX_NO_SWAP (1 << 5)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200136#define TX_BURST_SIZE_4_64BIT (2 << 22)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200137
138#if defined(__BIG_ENDIAN)
139#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
140 RX_BURST_SIZE_4_64BIT | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200141 TX_BURST_SIZE_4_64BIT
142#elif defined(__LITTLE_ENDIAN)
143#define PORT_SDMA_CONFIG_DEFAULT_VALUE \
144 RX_BURST_SIZE_4_64BIT | \
145 BLM_RX_NO_SWAP | \
146 BLM_TX_NO_SWAP | \
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200147 TX_BURST_SIZE_4_64BIT
148#else
149#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
150#endif
151
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200152
153/*
154 * Port serial control register.
155 */
156#define SET_MII_SPEED_TO_100 (1 << 24)
157#define SET_GMII_SPEED_TO_1000 (1 << 23)
158#define SET_FULL_DUPLEX_MODE (1 << 21)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200159#define MAX_RX_PACKET_1522BYTE (1 << 17)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200160#define MAX_RX_PACKET_9700BYTE (5 << 17)
161#define MAX_RX_PACKET_MASK (7 << 17)
Lennert Buytenhek2beff772008-06-01 01:22:37 +0200162#define DISABLE_AUTO_NEG_SPEED_GMII (1 << 13)
163#define DO_NOT_FORCE_LINK_FAIL (1 << 10)
164#define SERIAL_PORT_CONTROL_RESERVED (1 << 9)
165#define DISABLE_AUTO_NEG_FOR_FLOW_CTRL (1 << 3)
166#define DISABLE_AUTO_NEG_FOR_DUPLEX (1 << 2)
167#define FORCE_LINK_PASS (1 << 1)
168#define SERIAL_PORT_ENABLE (1 << 0)
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200169
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200170#define DEFAULT_RX_QUEUE_SIZE 400
171#define DEFAULT_TX_QUEUE_SIZE 800
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173/* SMI reg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200174#define SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
175#define SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
176#define SMI_OPCODE_WRITE 0 /* Completion of Read */
177#define SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200178
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200179/* typedefs */
180
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200181typedef enum _func_ret_status {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200182 ETH_OK, /* Returned as expected. */
183 ETH_ERROR, /* Fundamental error. */
184 ETH_RETRY, /* Could not process request. Try later.*/
185 ETH_END_OF_JOB, /* Ring has nothing to process. */
186 ETH_QUEUE_FULL, /* Ring resource error. */
187 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200188} FUNC_RET_STATUS;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200189
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200190/*
191 * RX/TX descriptors.
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200192 */
193#if defined(__BIG_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200194struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200195 u16 byte_cnt; /* Descriptor buffer byte count */
196 u16 buf_size; /* Buffer size */
197 u32 cmd_sts; /* Descriptor command status */
198 u32 next_desc_ptr; /* Next descriptor pointer */
199 u32 buf_ptr; /* Descriptor buffer pointer */
200};
201
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200202struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200203 u16 byte_cnt; /* buffer byte count */
204 u16 l4i_chk; /* CPU provided TCP checksum */
205 u32 cmd_sts; /* Command/status field */
206 u32 next_desc_ptr; /* Pointer to next descriptor */
207 u32 buf_ptr; /* pointer to buffer for this descriptor*/
208};
209#elif defined(__LITTLE_ENDIAN)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200210struct rx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200211 u32 cmd_sts; /* Descriptor command status */
212 u16 buf_size; /* Buffer size */
213 u16 byte_cnt; /* Descriptor buffer byte count */
214 u32 buf_ptr; /* Descriptor buffer pointer */
215 u32 next_desc_ptr; /* Next descriptor pointer */
216};
217
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200218struct tx_desc {
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200219 u32 cmd_sts; /* Command/status field */
220 u16 l4i_chk; /* CPU provided TCP checksum */
221 u16 byte_cnt; /* buffer byte count */
222 u32 buf_ptr; /* pointer to buffer for this descriptor*/
223 u32 next_desc_ptr; /* Pointer to next descriptor */
224};
225#else
226#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
227#endif
228
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200229/* RX & TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200230#define BUFFER_OWNED_BY_DMA 0x80000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200231
232/* RX & TX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200233#define ERROR_SUMMARY 0x00000001
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200234
235/* RX descriptor status */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200236#define LAYER_4_CHECKSUM_OK 0x40000000
237#define RX_ENABLE_INTERRUPT 0x20000000
238#define RX_FIRST_DESC 0x08000000
239#define RX_LAST_DESC 0x04000000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200240
241/* TX descriptor command */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200242#define TX_ENABLE_INTERRUPT 0x00800000
243#define GEN_CRC 0x00400000
244#define TX_FIRST_DESC 0x00200000
245#define TX_LAST_DESC 0x00100000
246#define ZERO_PADDING 0x00080000
247#define GEN_IP_V4_CHECKSUM 0x00040000
248#define GEN_TCP_UDP_CHECKSUM 0x00020000
249#define UDP_FRAME 0x00010000
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200250
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200251#define TX_IHL_SHIFT 11
Lennert Buytenhek7ca72a32008-06-01 01:41:29 +0200252
253
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200254/* Unified struct for Rx and Tx operations. The user is not required to */
255/* be familier with neither Tx nor Rx descriptors. */
256struct pkt_info {
257 unsigned short byte_cnt; /* Descriptor buffer byte count */
258 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
259 unsigned int cmd_sts; /* Descriptor command status */
260 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
261 struct sk_buff *return_info; /* User resource return information */
262};
263
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200264
265/* global *******************************************************************/
266struct mv643xx_shared_private {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200267 void __iomem *base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200268
269 /* used to protect SMI_REG, which is shared across ports */
270 spinlock_t phy_lock;
271
272 u32 win_protect;
273
274 unsigned int t_clk;
275};
276
277
278/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200279struct mv643xx_mib_counters {
280 u64 good_octets_received;
281 u32 bad_octets_received;
282 u32 internal_mac_transmit_err;
283 u32 good_frames_received;
284 u32 bad_frames_received;
285 u32 broadcast_frames_received;
286 u32 multicast_frames_received;
287 u32 frames_64_octets;
288 u32 frames_65_to_127_octets;
289 u32 frames_128_to_255_octets;
290 u32 frames_256_to_511_octets;
291 u32 frames_512_to_1023_octets;
292 u32 frames_1024_to_max_octets;
293 u64 good_octets_sent;
294 u32 good_frames_sent;
295 u32 excessive_collision;
296 u32 multicast_frames_sent;
297 u32 broadcast_frames_sent;
298 u32 unrec_mac_control_received;
299 u32 fc_sent;
300 u32 good_fc_received;
301 u32 bad_fc_received;
302 u32 undersize_received;
303 u32 fragments_received;
304 u32 oversize_received;
305 u32 jabber_received;
306 u32 mac_receive_error;
307 u32 bad_crc_event;
308 u32 collision;
309 u32 late_collision;
310};
311
312struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200313 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200314 int port_num; /* User Ethernet port number */
315
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200316 struct mv643xx_shared_private *shared_smi;
317
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200318 u32 rx_sram_addr; /* Base address of rx sram area */
319 u32 rx_sram_size; /* Size of rx sram area */
320 u32 tx_sram_addr; /* Base address of tx sram area */
321 u32 tx_sram_size; /* Size of tx sram area */
322
323 int rx_resource_err; /* Rx ring resource error flag */
324
325 /* Tx/Rx rings managment indexes fields. For driver use */
326
327 /* Next available and first returning Rx resource */
328 int rx_curr_desc_q, rx_used_desc_q;
329
330 /* Next available and first returning Tx resource */
331 int tx_curr_desc_q, tx_used_desc_q;
332
333#ifdef MV643XX_TX_FAST_REFILL
334 u32 tx_clean_threshold;
335#endif
336
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200337 struct rx_desc *p_rx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200338 dma_addr_t rx_desc_dma;
339 int rx_desc_area_size;
340 struct sk_buff **rx_skb;
341
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200342 struct tx_desc *p_tx_desc_area;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200343 dma_addr_t tx_desc_dma;
344 int tx_desc_area_size;
345 struct sk_buff **tx_skb;
346
347 struct work_struct tx_timeout_task;
348
349 struct net_device *dev;
350 struct napi_struct napi;
351 struct net_device_stats stats;
352 struct mv643xx_mib_counters mib_counters;
353 spinlock_t lock;
354 /* Size of Tx Ring per queue */
355 int tx_ring_size;
356 /* Number of tx descriptors in use */
357 int tx_desc_count;
358 /* Size of Rx Ring per queue */
359 int rx_ring_size;
360 /* Number of rx descriptors in use */
361 int rx_desc_count;
362
363 /*
364 * Used in case RX Ring is empty, which can be caused when
365 * system does not have resources (skb's)
366 */
367 struct timer_list timeout;
368
369 u32 rx_int_coal;
370 u32 tx_int_coal;
371 struct mii_if_info mii;
372};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200374
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200375/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700376static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200378 return readl(mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700381static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200383 writel(data, mp->shared->base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200386
387/* rxq/txq helper functions *************************************************/
388static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
389 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200391 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200392}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200394static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
395{
396 unsigned int port_num = mp->port_num;
397 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700398
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200399 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200400 queues = rdl(mp, RXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200401 if (queues) {
402 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200403 wrl(mp, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200404
405 /* Wait for all Rx activity to terminate. */
406 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200407 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200408 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 }
410
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200411 return queues;
412}
413
414static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
415 unsigned int queues)
416{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200417 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200418}
419
420static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
421{
422 unsigned int port_num = mp->port_num;
423 u32 queues;
424
425 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200426 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200427 if (queues) {
428 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200429 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200430
431 /* Wait for all Tx activity to terminate. */
432 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200433 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200434 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200435
436 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200437 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200438 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200439 }
440
441 return queues;
442}
443
444
445/* rx ***********************************************************************/
446static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
447
448/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200449 * rx_return_buff - Returns a Rx buffer back to the Rx ring.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200450 *
451 * DESCRIPTION:
452 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
453 * next 'used' descriptor and attached the returned buffer to it.
454 * In case the Rx ring was in "resource error" condition, where there are
455 * no available Rx resources, the function resets the resource error flag.
456 *
457 * INPUT:
458 * struct mv643xx_private *mp Ethernet Port Control srtuct.
459 * struct pkt_info *p_pkt_info Information on returned buffer.
460 *
461 * OUTPUT:
462 * New available Rx resource in Rx descriptor ring.
463 *
464 * RETURN:
465 * ETH_ERROR in case the routine can not access Rx desc ring.
466 * ETH_OK otherwise.
467 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200468static FUNC_RET_STATUS rx_return_buff(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200469 struct pkt_info *p_pkt_info)
470{
471 int used_rx_desc; /* Where to return Rx resource */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200472 volatile struct rx_desc *p_used_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200473 unsigned long flags;
474
475 spin_lock_irqsave(&mp->lock, flags);
476
477 /* Get 'used' Rx descriptor */
478 used_rx_desc = mp->rx_used_desc_q;
479 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
480
481 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
482 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
483 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
484
485 /* Flush the write pipe */
486
487 /* Return the descriptor to DMA ownership */
488 wmb();
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200489 p_used_rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200490 wmb();
491
492 /* Move the used descriptor pointer to the next descriptor */
493 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
494
495 /* Any Rx return cancels the Rx resource error status */
496 mp->rx_resource_err = 0;
497
498 spin_unlock_irqrestore(&mp->lock, flags);
499
500 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700504 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *
506 * Fills / refills RX queue on a certain gigabit ethernet port
507 *
508 * Input : pointer to ethernet interface network device structure
509 * Output : N/A
510 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700511static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct mv643xx_private *mp = netdev_priv(dev);
514 struct pkt_info pkt_info;
515 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700516 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700518 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000519 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 if (!skb)
521 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700522 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000523 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700524 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000525 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200526 pkt_info.cmd_sts = RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700527 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
528 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
529 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 pkt_info.return_info = skb;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200531 if (rx_return_buff(mp, &pkt_info) != ETH_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 printk(KERN_ERR
533 "%s: Error allocating RX Ring\n", dev->name);
534 break;
535 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700536 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /*
539 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700540 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700542 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700544 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700550 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 *
552 * Timer routine to wake up RX queue filling task. This function is
553 * used only in case the RX queue is empty, and all alloc_skb has
554 * failed (due to out of memory event).
555 *
556 * Input : pointer to ethernet interface network device structure
557 * Output : N/A
558 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700559static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700561 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200565 * port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200567 * DESCRIPTION:
568 * This routine returns the received data to the caller. There is no
569 * data copying during routine operation. All information is returned
570 * using pointer to packet information struct passed from the caller.
571 * If the routine exhausts Rx ring resources then the resource error flag
572 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200574 * INPUT:
575 * struct mv643xx_private *mp Ethernet Port Control srtuct.
576 * struct pkt_info *p_pkt_info User packet buffer.
577 *
578 * OUTPUT:
579 * Rx ring current and used indexes are updated.
580 *
581 * RETURN:
582 * ETH_ERROR in case the routine can not access Rx desc ring.
583 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
584 * ETH_END_OF_JOB if there is no received data.
585 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200587static FUNC_RET_STATUS port_receive(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200588 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200590 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200591 volatile struct rx_desc *p_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200592 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700593 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200595 /* Do not process Rx ring in case of Rx ring resource error */
596 if (mp->rx_resource_err)
597 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700598
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200599 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700600
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200601 /* Get the Rx Desc ring 'curr and 'used' indexes */
602 rx_curr_desc = mp->rx_curr_desc_q;
603 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200605 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700606
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200607 /* The following parameters are used to save readings from memory */
608 command_status = p_rx_desc->cmd_sts;
609 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700610
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200611 /* Nothing to receive... */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200612 if (command_status & BUFFER_OWNED_BY_DMA) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700613 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200614 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
Lennert Buytenhek0a6cf74d2008-06-01 11:16:21 +0200617 p_pkt_info->byte_cnt = p_rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200618 p_pkt_info->cmd_sts = command_status;
Lennert Buytenhek0a6cf74d2008-06-01 11:16:21 +0200619 p_pkt_info->buf_ptr = p_rx_desc->buf_ptr + ETH_HW_IP_ALIGN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200620 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
621 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200623 /*
624 * Clean the return info field to indicate that the
625 * packet has been moved to the upper layers
626 */
627 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700628
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200629 /* Update current index in data structure */
630 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
631 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700632
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200633 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
634 if (rx_next_curr_desc == rx_used_desc)
635 mp->rx_resource_err = 1;
636
637 spin_unlock_irqrestore(&mp->lock, flags);
638
639 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642/*
643 * mv643xx_eth_receive
644 *
645 * This function is forward packets that are received from the port's
646 * queues toward kernel core or FastRoute them to another interface.
647 *
648 * Input : dev - a pointer to the required interface
649 * max - maximum number to receive (0 means unlimted)
650 *
651 * Output : number of served packets
652 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700656 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 unsigned int received_packets = 0;
658 struct sk_buff *skb;
659 struct pkt_info pkt_info;
660
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200661 while (budget-- > 0 && port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400662 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700663 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700664 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700666
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700667 /*
668 * Update statistics.
669 * Note byte count includes 4 byte CRC count
670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 stats->rx_packets++;
672 stats->rx_bytes += pkt_info.byte_cnt;
673 skb = pkt_info.return_info;
674 /*
675 * In case received a packet without first / last bits on OR
676 * the error summary bit is on, the packets needs to be dropeed.
677 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200678 if (((pkt_info.cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) !=
679 (RX_FIRST_DESC | RX_LAST_DESC))
680 || (pkt_info.cmd_sts & ERROR_SUMMARY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 stats->rx_dropped++;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200682 if ((pkt_info.cmd_sts & (RX_FIRST_DESC |
683 RX_LAST_DESC)) !=
684 (RX_FIRST_DESC | RX_LAST_DESC)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 if (net_ratelimit())
686 printk(KERN_ERR
687 "%s: Received packet spread "
688 "on multiple descriptors\n",
689 dev->name);
690 }
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200691 if (pkt_info.cmd_sts & ERROR_SUMMARY)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 stats->rx_errors++;
693
694 dev_kfree_skb_irq(skb);
695 } else {
696 /*
697 * The -4 is for the CRC in the trailer of the
698 * received packet
699 */
700 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200702 if (pkt_info.cmd_sts & LAYER_4_CHECKSUM_OK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 skb->ip_summed = CHECKSUM_UNNECESSARY;
704 skb->csum = htons(
705 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
706 }
707 skb->protocol = eth_type_trans(skb, dev);
708#ifdef MV643XX_NAPI
709 netif_receive_skb(skb);
710#else
711 netif_rx(skb);
712#endif
713 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700714 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700716 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 return received_packets;
719}
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/*
723 * mv643xx_poll
724 *
725 * This function is used in case of NAPI
726 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700727static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700729 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
730 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700732 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734#ifdef MV643XX_TX_FAST_REFILL
735 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700736 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739#endif
740
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700741 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200742 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700743 != (u32) mp->rx_used_desc_q)
744 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700746 if (work_done < budget) {
747 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200748 wrl(mp, INT_CAUSE(port_num), 0);
749 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200750 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700753 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755#endif
756
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200757
758/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700759/**
760 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
761 *
762 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700763 * This helper function detects that case.
764 */
765
766static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
767{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700768 unsigned int frag;
769 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700770
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700771 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
772 fragp = &skb_shinfo(skb)->frags[frag];
773 if (fragp->size <= 8 && fragp->page_offset & 0x7)
774 return 1;
775 }
776 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700777}
778
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700779/**
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200780 * alloc_tx_desc_index - return the index of the next available tx desc
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700781 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200782static int alloc_tx_desc_index(struct mv643xx_private *mp)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700783{
784 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700785
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700786 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700787
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700788 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700789 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
790
791 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
792
793 return tx_desc_curr;
794}
795
796/**
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200797 * tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700799 * Ensure the data for each fragment to be transmitted is mapped properly,
800 * then fill in descriptors in the tx hw queue.
801 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200802static void tx_fill_frag_descs(struct mv643xx_private *mp,
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700803 struct sk_buff *skb)
804{
805 int frag;
806 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200807 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700808
809 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
810 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
811
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200812 tx_index = alloc_tx_desc_index(mp);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700813 desc = &mp->p_tx_desc_area[tx_index];
814
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200815 desc->cmd_sts = BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700816 /* Last Frag enables interrupt and frees the skb */
817 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200818 desc->cmd_sts |= ZERO_PADDING |
819 TX_LAST_DESC |
820 TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700821 mp->tx_skb[tx_index] = skb;
822 } else
Al Viro05980772006-05-30 23:59:09 -0400823 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700824
825 desc = &mp->p_tx_desc_area[tx_index];
826 desc->l4i_chk = 0;
827 desc->byte_cnt = this_frag->size;
828 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
829 this_frag->page_offset,
830 this_frag->size,
831 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700832 }
833}
834
Byron Bradley324ff2c2008-02-04 23:47:15 -0800835static inline __be16 sum16_as_be(__sum16 sum)
836{
837 return (__force __be16)sum;
838}
839
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700840/**
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200841 * tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700843 * Ensure the data for an skb to be transmitted is mapped properly,
844 * then fill in descriptors in the tx hw queue and start the hardware.
845 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200846static void tx_submit_descs_for_skb(struct mv643xx_private *mp,
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700847 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700848{
849 int tx_index;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200850 struct tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700851 u32 cmd_sts;
852 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700853 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700854
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200855 cmd_sts = TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700856
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200857 tx_index = alloc_tx_desc_index(mp);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700858 desc = &mp->p_tx_desc_area[tx_index];
859
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700860 if (nr_frags) {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200861 tx_fill_frag_descs(mp, skb);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700862
863 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400864 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700865 } else {
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200866 cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700867 length = skb->len;
868 mp->tx_skb[tx_index] = skb;
869 }
870
871 desc->byte_cnt = length;
872 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700873
Patrick McHardy84fa7932006-08-29 16:44:56 -0700874 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800875 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700876
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200877 cmd_sts |= GEN_TCP_UDP_CHECKSUM |
878 GEN_IP_V4_CHECKSUM |
879 ip_hdr(skb)->ihl << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700880
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700881 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700882 case IPPROTO_UDP:
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200883 cmd_sts |= UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800884 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700885 break;
886 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800887 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700888 break;
889 default:
890 BUG();
891 }
892 } else {
893 /* Errata BTS #50, IHL must be 5 if no HW checksum */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200894 cmd_sts |= 5 << TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700895 desc->l4i_chk = 0;
896 }
897
898 /* ensure all other descriptors are written before first cmd_sts */
899 wmb();
900 desc->cmd_sts = cmd_sts;
901
902 /* ensure all descriptors are written before poking hardware */
903 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200904 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700905
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700906 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700907}
908
909/**
910 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
913static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
914{
915 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700916 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700919 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700920
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700921 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
922 stats->tx_dropped++;
923 printk(KERN_DEBUG "%s: failed to linearize tiny "
924 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700925 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700926 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 spin_lock_irqsave(&mp->lock, flags);
929
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700930 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
931 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
932 netif_stop_queue(dev);
933 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700934 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700935 }
936
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200937 tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700938 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 stats->tx_packets++;
940 dev->trans_start = jiffies;
941
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700942 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
943 netif_stop_queue(dev);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 spin_unlock_irqrestore(&mp->lock, flags);
946
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700947 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200950
951/* mii management interface *************************************************/
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200952static int phy_addr_get(struct mv643xx_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200953
954/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200955 * read_smi_reg - Read PHY registers
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200956 *
957 * DESCRIPTION:
958 * This routine utilize the SMI interface to interact with the PHY in
959 * order to perform PHY register read.
960 *
961 * INPUT:
962 * struct mv643xx_private *mp Ethernet Port.
963 * unsigned int phy_reg PHY register address offset.
964 * unsigned int *value Register value buffer.
965 *
966 * OUTPUT:
967 * Write the value of a specified PHY register into given buffer.
968 *
969 * RETURN:
970 * false if the PHY is busy or read data is not in valid state.
971 * true otherwise.
972 *
973 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200974static void read_smi_reg(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200975 unsigned int phy_reg, unsigned int *value)
976{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200977 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
978 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200979 unsigned long flags;
980 int i;
981
982 /* the SMI register is a shared resource */
983 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
984
985 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200986 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200987 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200988 printk("%s: PHY busy timeout\n", mp->dev->name);
989 goto out;
990 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200991 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200992 }
993
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200994 writel((phy_addr << 16) | (phy_reg << 21) | SMI_OPCODE_READ, smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200995
996 /* now wait for the data to be valid */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +0200997 for (i = 0; !(readl(smi_reg) & SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200998 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200999 printk("%s: PHY read timeout\n", mp->dev->name);
1000 goto out;
1001 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001002 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001003 }
1004
1005 *value = readl(smi_reg) & 0xffff;
1006out:
1007 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1008}
1009
1010/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001011 * write_smi_reg - Write to PHY registers
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001012 *
1013 * DESCRIPTION:
1014 * This routine utilize the SMI interface to interact with the PHY in
1015 * order to perform writes to PHY registers.
1016 *
1017 * INPUT:
1018 * struct mv643xx_private *mp Ethernet Port.
1019 * unsigned int phy_reg PHY register address offset.
1020 * unsigned int value Register value.
1021 *
1022 * OUTPUT:
1023 * Write the given value to the specified PHY register.
1024 *
1025 * RETURN:
1026 * false if the PHY is busy.
1027 * true otherwise.
1028 *
1029 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001030static void write_smi_reg(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001031 unsigned int phy_reg, unsigned int value)
1032{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001033 void __iomem *smi_reg = mp->shared_smi->base + SMI_REG;
1034 int phy_addr = phy_addr_get(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001035 unsigned long flags;
1036 int i;
1037
1038 /* the SMI register is a shared resource */
1039 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1040
1041 /* wait for the SMI register to become available */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001042 for (i = 0; readl(smi_reg) & SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001043 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001044 printk("%s: PHY busy timeout\n", mp->dev->name);
1045 goto out;
1046 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001047 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001048 }
1049
1050 writel((phy_addr << 16) | (phy_reg << 21) |
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001051 SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001052out:
1053 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1054}
1055
1056
1057/* mib counters *************************************************************/
1058/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001059 * clear_mib_counters - Clear all MIB counters
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001060 *
1061 * DESCRIPTION:
1062 * This function clears all MIB counters of a specific ethernet port.
1063 * A read from the MIB counter will reset the counter.
1064 *
1065 * INPUT:
1066 * struct mv643xx_private *mp Ethernet Port.
1067 *
1068 * OUTPUT:
1069 * After reading all MIB counters, the counters resets.
1070 *
1071 * RETURN:
1072 * MIB counter value.
1073 *
1074 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001075static void clear_mib_counters(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001076{
1077 unsigned int port_num = mp->port_num;
1078 int i;
1079
1080 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +02001081 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001082 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001083}
1084
1085static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1086{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001087 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001088}
1089
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001090static void update_mib_counters(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001091{
1092 struct mv643xx_mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001093
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +02001094 p->good_octets_received += read_mib(mp, 0x00);
1095 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
1096 p->bad_octets_received += read_mib(mp, 0x08);
1097 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
1098 p->good_frames_received += read_mib(mp, 0x10);
1099 p->bad_frames_received += read_mib(mp, 0x14);
1100 p->broadcast_frames_received += read_mib(mp, 0x18);
1101 p->multicast_frames_received += read_mib(mp, 0x1c);
1102 p->frames_64_octets += read_mib(mp, 0x20);
1103 p->frames_65_to_127_octets += read_mib(mp, 0x24);
1104 p->frames_128_to_255_octets += read_mib(mp, 0x28);
1105 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
1106 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
1107 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
1108 p->good_octets_sent += read_mib(mp, 0x38);
1109 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
1110 p->good_frames_sent += read_mib(mp, 0x40);
1111 p->excessive_collision += read_mib(mp, 0x44);
1112 p->multicast_frames_sent += read_mib(mp, 0x48);
1113 p->broadcast_frames_sent += read_mib(mp, 0x4c);
1114 p->unrec_mac_control_received += read_mib(mp, 0x50);
1115 p->fc_sent += read_mib(mp, 0x54);
1116 p->good_fc_received += read_mib(mp, 0x58);
1117 p->bad_fc_received += read_mib(mp, 0x5c);
1118 p->undersize_received += read_mib(mp, 0x60);
1119 p->fragments_received += read_mib(mp, 0x64);
1120 p->oversize_received += read_mib(mp, 0x68);
1121 p->jabber_received += read_mib(mp, 0x6c);
1122 p->mac_receive_error += read_mib(mp, 0x70);
1123 p->bad_crc_event += read_mib(mp, 0x74);
1124 p->collision += read_mib(mp, 0x78);
1125 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001126}
1127
1128
1129/* ethtool ******************************************************************/
1130struct mv643xx_stats {
1131 char stat_string[ETH_GSTRING_LEN];
1132 int sizeof_stat;
1133 int stat_offset;
1134};
1135
1136#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1137 offsetof(struct mv643xx_private, m)
1138
1139static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1140 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1141 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1142 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1143 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1144 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1145 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1146 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1147 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1148 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1149 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1150 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1151 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1152 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1153 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1154 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1155 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1156 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1157 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1158 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1159 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1160 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1161 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1162 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1163 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1164 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1165 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1166 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1167 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1168 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1169 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1170 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1171 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1172 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1173 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1174 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1175 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1176 { "collision", MV643XX_STAT(mib_counters.collision) },
1177 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1178};
1179
1180#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1181
1182static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1183{
1184 struct mv643xx_private *mp = netdev_priv(dev);
1185 int err;
1186
1187 spin_lock_irq(&mp->lock);
1188 err = mii_ethtool_gset(&mp->mii, cmd);
1189 spin_unlock_irq(&mp->lock);
1190
1191 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1192 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1193 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1194
1195 return err;
1196}
1197
1198static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1199{
1200 struct mv643xx_private *mp = netdev_priv(dev);
1201 int err;
1202
1203 spin_lock_irq(&mp->lock);
1204 err = mii_ethtool_sset(&mp->mii, cmd);
1205 spin_unlock_irq(&mp->lock);
1206
1207 return err;
1208}
1209
1210static void mv643xx_get_drvinfo(struct net_device *netdev,
1211 struct ethtool_drvinfo *drvinfo)
1212{
1213 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1214 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1215 strncpy(drvinfo->fw_version, "N/A", 32);
1216 strncpy(drvinfo->bus_info, "mv643xx", 32);
1217 drvinfo->n_stats = MV643XX_STATS_LEN;
1218}
1219
1220static int mv643xx_eth_nway_restart(struct net_device *dev)
1221{
1222 struct mv643xx_private *mp = netdev_priv(dev);
1223
1224 return mii_nway_restart(&mp->mii);
1225}
1226
1227static u32 mv643xx_eth_get_link(struct net_device *dev)
1228{
1229 struct mv643xx_private *mp = netdev_priv(dev);
1230
1231 return mii_link_ok(&mp->mii);
1232}
1233
1234static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1235 uint8_t *data)
1236{
1237 int i;
1238
1239 switch(stringset) {
1240 case ETH_SS_STATS:
1241 for (i=0; i < MV643XX_STATS_LEN; i++) {
1242 memcpy(data + i * ETH_GSTRING_LEN,
1243 mv643xx_gstrings_stats[i].stat_string,
1244 ETH_GSTRING_LEN);
1245 }
1246 break;
1247 }
1248}
1249
1250static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1251 struct ethtool_stats *stats, uint64_t *data)
1252{
1253 struct mv643xx_private *mp = netdev->priv;
1254 int i;
1255
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001256 update_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001257
1258 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1259 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1260 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1261 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1262 }
1263}
1264
1265static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1266{
1267 switch (sset) {
1268 case ETH_SS_STATS:
1269 return MV643XX_STATS_LEN;
1270 default:
1271 return -EOPNOTSUPP;
1272 }
1273}
1274
1275static const struct ethtool_ops mv643xx_ethtool_ops = {
1276 .get_settings = mv643xx_get_settings,
1277 .set_settings = mv643xx_set_settings,
1278 .get_drvinfo = mv643xx_get_drvinfo,
1279 .get_link = mv643xx_eth_get_link,
1280 .set_sg = ethtool_op_set_sg,
1281 .get_sset_count = mv643xx_get_sset_count,
1282 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1283 .get_strings = mv643xx_get_strings,
1284 .nway_reset = mv643xx_eth_nway_restart,
1285};
1286
1287
1288/* address handling *********************************************************/
1289/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001290 * uc_addr_get - Read the MAC address from the port's hw registers
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001291 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001292static void uc_addr_get(struct mv643xx_private *mp, unsigned char *p_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001293{
1294 unsigned int port_num = mp->port_num;
1295 unsigned int mac_h;
1296 unsigned int mac_l;
1297
1298 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1299 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1300
1301 p_addr[0] = (mac_h >> 24) & 0xff;
1302 p_addr[1] = (mac_h >> 16) & 0xff;
1303 p_addr[2] = (mac_h >> 8) & 0xff;
1304 p_addr[3] = mac_h & 0xff;
1305 p_addr[4] = (mac_l >> 8) & 0xff;
1306 p_addr[5] = mac_l & 0xff;
1307}
1308
1309/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001310 * init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001311 *
1312 * DESCRIPTION:
1313 * Go through all the DA filter tables (Unicast, Special Multicast &
1314 * Other Multicast) and set each entry to 0.
1315 *
1316 * INPUT:
1317 * struct mv643xx_private *mp Ethernet Port.
1318 *
1319 * OUTPUT:
1320 * Multicast and Unicast packets are rejected.
1321 *
1322 * RETURN:
1323 * None.
1324 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001325static void init_mac_tables(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001326{
1327 unsigned int port_num = mp->port_num;
1328 int table_index;
1329
1330 /* Clear DA filter unicast table (Ex_dFUT) */
1331 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001332 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001333
1334 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1335 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001336 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001337 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001338 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001339 }
1340}
1341
1342/*
1343 * The entries in each table are indexed by a hash of a packet's MAC
1344 * address. One bit in each entry determines whether the packet is
1345 * accepted. There are 4 entries (each 8 bits wide) in each register
1346 * of the table. The bits in each entry are defined as follows:
1347 * 0 Accept=1, Drop=0
1348 * 3-1 Queue (ETH_Q0=0)
1349 * 7-4 Reserved = 0;
1350 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001351static void set_filter_table_entry(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001352 int table, unsigned char entry)
1353{
1354 unsigned int table_reg;
1355 unsigned int tbl_offset;
1356 unsigned int reg_offset;
1357
1358 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1359 reg_offset = entry % 4; /* Entry offset within the register */
1360
1361 /* Set "accepts frame bit" at specified table entry */
1362 table_reg = rdl(mp, table + tbl_offset);
1363 table_reg |= 0x01 << (8 * reg_offset);
1364 wrl(mp, table + tbl_offset, table_reg);
1365}
1366
1367/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001368 * uc_addr_set - Write a MAC address into the port's hw registers
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001369 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001370static void uc_addr_set(struct mv643xx_private *mp, unsigned char *p_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001371{
1372 unsigned int port_num = mp->port_num;
1373 unsigned int mac_h;
1374 unsigned int mac_l;
1375 int table;
1376
1377 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1378 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1379 (p_addr[3] << 0);
1380
1381 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1382 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1383
1384 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001385 table = UNICAST_TABLE(port_num);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001386 set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001387}
1388
1389/*
1390 * mv643xx_eth_update_mac_address
1391 *
1392 * Update the MAC address of the port in the address table
1393 *
1394 * Input : pointer to ethernet interface network device structure
1395 * Output : N/A
1396 */
1397static void mv643xx_eth_update_mac_address(struct net_device *dev)
1398{
1399 struct mv643xx_private *mp = netdev_priv(dev);
1400
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001401 init_mac_tables(mp);
1402 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001403}
1404
1405/*
1406 * mv643xx_eth_set_mac_address
1407 *
1408 * Change the interface's mac address.
1409 * No special hardware thing should be done because interface is always
1410 * put in promiscuous mode.
1411 *
1412 * Input : pointer to ethernet interface network device structure and
1413 * a pointer to the designated entry to be added to the cache.
1414 * Output : zero upon success, negative upon failure
1415 */
1416static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1417{
1418 int i;
1419
1420 for (i = 0; i < 6; i++)
1421 /* +2 is for the offset of the HW addr type */
1422 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1423 mv643xx_eth_update_mac_address(dev);
1424 return 0;
1425}
1426
1427/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001428 * mc_addr - Multicast address settings.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001429 *
1430 * The MV device supports multicast using two tables:
1431 * 1) Special Multicast Table for MAC addresses of the form
1432 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1433 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1434 * Table entries in the DA-Filter table.
1435 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1436 * is used as an index to the Other Multicast Table entries in the
1437 * DA-Filter table. This function calculates the CRC-8bit value.
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001438 * In either case, set_filter_table_entry() is then called
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001439 * to set to set the actual table entry.
1440 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001441static void mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001442{
1443 unsigned int port_num = mp->port_num;
1444 unsigned int mac_h;
1445 unsigned int mac_l;
1446 unsigned char crc_result = 0;
1447 int table;
1448 int mac_array[48];
1449 int crc[8];
1450 int i;
1451
1452 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1453 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001454 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001455 set_filter_table_entry(mp, table, p_addr[5]);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001456 return;
1457 }
1458
1459 /* Calculate CRC-8 out of the given address */
1460 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1461 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1462 (p_addr[4] << 8) | (p_addr[5] << 0);
1463
1464 for (i = 0; i < 32; i++)
1465 mac_array[i] = (mac_l >> i) & 0x1;
1466 for (i = 32; i < 48; i++)
1467 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1468
1469 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1470 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1471 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1472 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1473 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1474
1475 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1476 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1477 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1478 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1479 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1480 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1481 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1482
1483 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1484 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1485 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1486 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1487 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1488 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1489
1490 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1491 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1492 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1493 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1494 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1495 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1496
1497 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1498 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1499 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1500 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1501 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1502 mac_array[3] ^ mac_array[2];
1503
1504 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1505 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1506 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1507 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1508 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1509 mac_array[4] ^ mac_array[3];
1510
1511 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1512 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1513 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1514 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1515 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1516 mac_array[4];
1517
1518 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1519 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1520 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1521 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1522 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1523
1524 for (i = 0; i < 8; i++)
1525 crc_result = crc_result | (crc[i] << i);
1526
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001527 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001528 set_filter_table_entry(mp, table, crc_result);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001529}
1530
1531/*
1532 * Set the entire multicast list based on dev->mc_list.
1533 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001534static void set_multicast_list(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001535{
1536
1537 struct dev_mc_list *mc_list;
1538 int i;
1539 int table_index;
1540 struct mv643xx_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001541 unsigned int port_num = mp->port_num;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001542
1543 /* If the device is in promiscuous mode or in all multicast mode,
1544 * we will fully populate both multicast tables with accept.
1545 * This is guaranteed to yield a match on all multicast addresses...
1546 */
1547 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1548 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1549 /* Set all entries in DA filter special multicast
1550 * table (Ex_dFSMT)
1551 * Set for ETH_Q0 for now
1552 * Bits
1553 * 0 Accept=1, Drop=0
1554 * 3-1 Queue ETH_Q0=0
1555 * 7-4 Reserved = 0;
1556 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001557 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001558
1559 /* Set all entries in DA filter other multicast
1560 * table (Ex_dFOMT)
1561 * Set for ETH_Q0 for now
1562 * Bits
1563 * 0 Accept=1, Drop=0
1564 * 3-1 Queue ETH_Q0=0
1565 * 7-4 Reserved = 0;
1566 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001567 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001568 }
1569 return;
1570 }
1571
1572 /* We will clear out multicast tables every time we get the list.
1573 * Then add the entire new list...
1574 */
1575 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1576 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001577 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001578
1579 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001580 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001581 }
1582
1583 /* Get pointer to net_device multicast list and add each one... */
1584 for (i = 0, mc_list = dev->mc_list;
1585 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1586 i++, mc_list = mc_list->next)
1587 if (mc_list->dmi_addrlen == 6)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001588 mc_addr(mp, mc_list->dmi_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001589}
1590
1591/*
1592 * mv643xx_eth_set_rx_mode
1593 *
1594 * Change from promiscuos to regular rx mode
1595 *
1596 * Input : pointer to ethernet interface network device structure
1597 * Output : N/A
1598 */
1599static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1600{
1601 struct mv643xx_private *mp = netdev_priv(dev);
1602 u32 config_reg;
1603
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001604 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001605 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001606 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001607 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001608 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001609 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001610
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001611 set_multicast_list(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001612}
1613
1614
1615/* rx/tx queue initialisation ***********************************************/
1616/*
1617 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1618 *
1619 * DESCRIPTION:
1620 * This function prepares a Rx chained list of descriptors and packet
1621 * buffers in a form of a ring. The routine must be called after port
1622 * initialization routine and before port start routine.
1623 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1624 * devices in the system (i.e. DRAM). This function uses the ethernet
1625 * struct 'virtual to physical' routine (set by the user) to set the ring
1626 * with physical addresses.
1627 *
1628 * INPUT:
1629 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1630 *
1631 * OUTPUT:
1632 * The routine updates the Ethernet port control struct with information
1633 * regarding the Rx descriptors and buffers.
1634 *
1635 * RETURN:
1636 * None.
1637 */
1638static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1639{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001640 volatile struct rx_desc *p_rx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001641 int rx_desc_num = mp->rx_ring_size;
1642 int i;
1643
1644 /* initialize the next_desc_ptr links in the Rx descriptors ring */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001645 p_rx_desc = (struct rx_desc *)mp->p_rx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001646 for (i = 0; i < rx_desc_num; i++) {
1647 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001648 ((i + 1) % rx_desc_num) * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001649 }
1650
1651 /* Save Rx desc pointer to driver struct. */
1652 mp->rx_curr_desc_q = 0;
1653 mp->rx_used_desc_q = 0;
1654
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001655 mp->rx_desc_area_size = rx_desc_num * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001656}
1657
1658static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1659{
1660 struct mv643xx_private *mp = netdev_priv(dev);
1661 int curr;
1662
1663 /* Stop RX Queues */
1664 mv643xx_eth_port_disable_rx(mp);
1665
1666 /* Free preallocated skb's on RX rings */
1667 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1668 if (mp->rx_skb[curr]) {
1669 dev_kfree_skb(mp->rx_skb[curr]);
1670 mp->rx_desc_count--;
1671 }
1672 }
1673
1674 if (mp->rx_desc_count)
1675 printk(KERN_ERR
1676 "%s: Error in freeing Rx Ring. %d skb's still"
1677 " stuck in RX Ring - ignoring them\n", dev->name,
1678 mp->rx_desc_count);
1679 /* Free RX ring */
1680 if (mp->rx_sram_size)
1681 iounmap(mp->p_rx_desc_area);
1682 else
1683 dma_free_coherent(NULL, mp->rx_desc_area_size,
1684 mp->p_rx_desc_area, mp->rx_desc_dma);
1685}
1686
1687/*
1688 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1689 *
1690 * DESCRIPTION:
1691 * This function prepares a Tx chained list of descriptors and packet
1692 * buffers in a form of a ring. The routine must be called after port
1693 * initialization routine and before port start routine.
1694 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1695 * devices in the system (i.e. DRAM). This function uses the ethernet
1696 * struct 'virtual to physical' routine (set by the user) to set the ring
1697 * with physical addresses.
1698 *
1699 * INPUT:
1700 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1701 *
1702 * OUTPUT:
1703 * The routine updates the Ethernet port control struct with information
1704 * regarding the Tx descriptors and buffers.
1705 *
1706 * RETURN:
1707 * None.
1708 */
1709static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1710{
1711 int tx_desc_num = mp->tx_ring_size;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001712 struct tx_desc *p_tx_desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001713 int i;
1714
1715 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001716 p_tx_desc = (struct tx_desc *)mp->p_tx_desc_area;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001717 for (i = 0; i < tx_desc_num; i++) {
1718 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001719 ((i + 1) % tx_desc_num) * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001720 }
1721
1722 mp->tx_curr_desc_q = 0;
1723 mp->tx_used_desc_q = 0;
1724
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001725 mp->tx_desc_area_size = tx_desc_num * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001726}
1727
1728/**
1729 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1730 *
1731 * If force is non-zero, frees uncompleted descriptors as well
1732 */
1733static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1734{
1735 struct mv643xx_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001736 struct tx_desc *desc;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001737 u32 cmd_sts;
1738 struct sk_buff *skb;
1739 unsigned long flags;
1740 int tx_index;
1741 dma_addr_t addr;
1742 int count;
1743 int released = 0;
1744
1745 while (mp->tx_desc_count > 0) {
1746 spin_lock_irqsave(&mp->lock, flags);
1747
1748 /* tx_desc_count might have changed before acquiring the lock */
1749 if (mp->tx_desc_count <= 0) {
1750 spin_unlock_irqrestore(&mp->lock, flags);
1751 return released;
1752 }
1753
1754 tx_index = mp->tx_used_desc_q;
1755 desc = &mp->p_tx_desc_area[tx_index];
1756 cmd_sts = desc->cmd_sts;
1757
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001758 if (!force && (cmd_sts & BUFFER_OWNED_BY_DMA)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001759 spin_unlock_irqrestore(&mp->lock, flags);
1760 return released;
1761 }
1762
1763 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1764 mp->tx_desc_count--;
1765
1766 addr = desc->buf_ptr;
1767 count = desc->byte_cnt;
1768 skb = mp->tx_skb[tx_index];
1769 if (skb)
1770 mp->tx_skb[tx_index] = NULL;
1771
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001772 if (cmd_sts & ERROR_SUMMARY) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001773 printk("%s: Error in TX\n", dev->name);
1774 dev->stats.tx_errors++;
1775 }
1776
1777 spin_unlock_irqrestore(&mp->lock, flags);
1778
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001779 if (cmd_sts & TX_FIRST_DESC)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001780 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1781 else
1782 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1783
1784 if (skb)
1785 dev_kfree_skb_irq(skb);
1786
1787 released = 1;
1788 }
1789
1790 return released;
1791}
1792
1793static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1794{
1795 struct mv643xx_private *mp = netdev_priv(dev);
1796
1797 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1798 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1799 netif_wake_queue(dev);
1800}
1801
1802static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1803{
1804 mv643xx_eth_free_tx_descs(dev, 1);
1805}
1806
1807static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1808{
1809 struct mv643xx_private *mp = netdev_priv(dev);
1810
1811 /* Stop Tx Queues */
1812 mv643xx_eth_port_disable_tx(mp);
1813
1814 /* Free outstanding skb's on TX ring */
1815 mv643xx_eth_free_all_tx_descs(dev);
1816
1817 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1818
1819 /* Free TX ring */
1820 if (mp->tx_sram_size)
1821 iounmap(mp->p_tx_desc_area);
1822 else
1823 dma_free_coherent(NULL, mp->tx_desc_area_size,
1824 mp->p_tx_desc_area, mp->tx_desc_dma);
1825}
1826
1827
1828/* netdev ops and related ***************************************************/
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001829static void port_reset(struct mv643xx_private *mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001830
1831/* Set the mv643xx port configuration register for the speed/duplex mode. */
1832static void mv643xx_eth_update_pscr(struct net_device *dev,
1833 struct ethtool_cmd *ecmd)
1834{
1835 struct mv643xx_private *mp = netdev_priv(dev);
1836 int port_num = mp->port_num;
1837 u32 o_pscr, n_pscr;
1838 unsigned int queues;
1839
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001840 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001841 n_pscr = o_pscr;
1842
1843 /* clear speed, duplex and rx buffer size fields */
1844 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1845 SET_GMII_SPEED_TO_1000 |
1846 SET_FULL_DUPLEX_MODE |
1847 MAX_RX_PACKET_MASK);
1848
1849 if (ecmd->duplex == DUPLEX_FULL)
1850 n_pscr |= SET_FULL_DUPLEX_MODE;
1851
1852 if (ecmd->speed == SPEED_1000)
1853 n_pscr |= SET_GMII_SPEED_TO_1000 |
1854 MAX_RX_PACKET_9700BYTE;
1855 else {
1856 if (ecmd->speed == SPEED_100)
1857 n_pscr |= SET_MII_SPEED_TO_100;
1858 n_pscr |= MAX_RX_PACKET_1522BYTE;
1859 }
1860
1861 if (n_pscr != o_pscr) {
1862 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001863 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001864 else {
1865 queues = mv643xx_eth_port_disable_tx(mp);
1866
1867 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001868 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1869 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1870 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001871 if (queues)
1872 mv643xx_eth_port_enable_tx(mp, queues);
1873 }
1874 }
1875}
1876
1877/*
1878 * mv643xx_eth_int_handler
1879 *
1880 * Main interrupt handler for the gigbit ethernet ports
1881 *
1882 * Input : irq - irq number (not used)
1883 * dev_id - a pointer to the required interface's data structure
1884 * regs - not used
1885 * Output : N/A
1886 */
1887
1888static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1889{
1890 struct net_device *dev = (struct net_device *)dev_id;
1891 struct mv643xx_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001892 u32 int_cause, int_cause_ext = 0;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001893 unsigned int port_num = mp->port_num;
1894
1895 /* Read interrupt cause registers */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001896 int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1897 if (int_cause & INT_EXT) {
1898 int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001899 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001900 wrl(mp, INT_CAUSE_EXT(port_num), ~int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001901 }
1902
1903 /* PHY status changed */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001904 if (int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001905 struct ethtool_cmd cmd;
1906
1907 if (mii_link_ok(&mp->mii)) {
1908 mii_ethtool_gset(&mp->mii, &cmd);
1909 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001910 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001911 if (!netif_carrier_ok(dev)) {
1912 netif_carrier_on(dev);
1913 if (mp->tx_ring_size - mp->tx_desc_count >=
1914 MAX_DESCS_PER_SKB)
1915 netif_wake_queue(dev);
1916 }
1917 } else if (netif_carrier_ok(dev)) {
1918 netif_stop_queue(dev);
1919 netif_carrier_off(dev);
1920 }
1921 }
1922
1923#ifdef MV643XX_NAPI
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001924 if (int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001925 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001926 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001927
1928 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001929 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001930
1931 netif_rx_schedule(dev, &mp->napi);
1932 }
1933#else
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001934 if (int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001935 mv643xx_eth_receive_queue(dev, INT_MAX);
1936#endif
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001937 if (int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001938 mv643xx_eth_free_completed_tx_descs(dev);
1939
1940 /*
1941 * If no real interrupt occured, exit.
1942 * This can happen when using gigE interrupt coalescing mechanism.
1943 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001944 if ((int_cause == 0x0) && (int_cause_ext == 0x0))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001945 return IRQ_NONE;
1946
1947 return IRQ_HANDLED;
1948}
1949
1950/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001951 * phy_reset - Reset Ethernet port PHY.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001952 *
1953 * DESCRIPTION:
1954 * This routine utilizes the SMI interface to reset the ethernet port PHY.
1955 *
1956 * INPUT:
1957 * struct mv643xx_private *mp Ethernet Port.
1958 *
1959 * OUTPUT:
1960 * The PHY is reset.
1961 *
1962 * RETURN:
1963 * None.
1964 *
1965 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001966static void phy_reset(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001967{
1968 unsigned int phy_reg_data;
1969
1970 /* Reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001971 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001972 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001973 write_smi_reg(mp, 0, phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001974
1975 /* wait for PHY to come out of reset */
1976 do {
1977 udelay(1);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001978 read_smi_reg(mp, 0, &phy_reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001979 } while (phy_reg_data & 0x8000);
1980}
1981
1982/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02001983 * port_start - Start the Ethernet port activity.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001984 *
1985 * DESCRIPTION:
1986 * This routine prepares the Ethernet port for Rx and Tx activity:
1987 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
1988 * has been initialized a descriptor's ring (using
1989 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
1990 * 2. Initialize and enable the Ethernet configuration port by writing to
1991 * the port's configuration and command registers.
1992 * 3. Initialize and enable the SDMA by writing to the SDMA's
1993 * configuration and command registers. After completing these steps,
1994 * the ethernet port SDMA can starts to perform Rx and Tx activities.
1995 *
1996 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
1997 * to calling this function (use ether_init_tx_desc_ring for Tx queues
1998 * and ether_init_rx_desc_ring for Rx queues).
1999 *
2000 * INPUT:
2001 * dev - a pointer to the required interface
2002 *
2003 * OUTPUT:
2004 * Ethernet port is ready to receive and transmit.
2005 *
2006 * RETURN:
2007 * None.
2008 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002009static void port_start(struct net_device *dev)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002010{
2011 struct mv643xx_private *mp = netdev_priv(dev);
2012 unsigned int port_num = mp->port_num;
2013 int tx_curr_desc, rx_curr_desc;
2014 u32 pscr;
2015 struct ethtool_cmd ethtool_cmd;
2016
2017 /* Assignment of Tx CTRP of given queue */
2018 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002019 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002020 (u32)((struct tx_desc *)mp->tx_desc_dma + tx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002021
2022 /* Assignment of Rx CRDP of given queue */
2023 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002024 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002025 (u32)((struct rx_desc *)mp->rx_desc_dma + rx_curr_desc));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002026
2027 /* Add the assigned Ethernet address to the port's address table */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002028 uc_addr_set(mp, dev->dev_addr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002029
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002030 /*
2031 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2032 * frames to RX queue #0.
2033 */
2034 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002035
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002036 /*
2037 * Treat BPDUs as normal multicasts, and disable partition mode.
2038 */
2039 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002040
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002041 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002042
2043 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002044 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002045
2046 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2047 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02002048 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002049 DO_NOT_FORCE_LINK_FAIL |
2050 SERIAL_PORT_CONTROL_RESERVED;
2051
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002052 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002053
2054 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002055 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002056
2057 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002058 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002059
2060 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002061 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002062
2063 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002064 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002065
2066 /* save phy settings across reset */
2067 mv643xx_get_settings(dev, &ethtool_cmd);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002068 phy_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002069 mv643xx_set_settings(dev, &ethtool_cmd);
2070}
2071
2072#ifdef MV643XX_COAL
2073
2074/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002075 * set_rx_coal - Sets coalescing interrupt mechanism on RX path
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002076 *
2077 * DESCRIPTION:
2078 * This routine sets the RX coalescing interrupt mechanism parameter.
2079 * This parameter is a timeout counter, that counts in 64 t_clk
2080 * chunks ; that when timeout event occurs a maskable interrupt
2081 * occurs.
2082 * The parameter is calculated using the tClk of the MV-643xx chip
2083 * , and the required delay of the interrupt in usec.
2084 *
2085 * INPUT:
2086 * struct mv643xx_private *mp Ethernet port
2087 * unsigned int delay Delay in usec
2088 *
2089 * OUTPUT:
2090 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2091 *
2092 * RETURN:
2093 * The interrupt coalescing value set in the gigE port.
2094 *
2095 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002096static unsigned int set_rx_coal(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002097 unsigned int delay)
2098{
2099 unsigned int port_num = mp->port_num;
2100 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2101
2102 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002103 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002104 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002105 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002106 & 0xffc000ff));
2107
2108 return coal;
2109}
2110#endif
2111
2112/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002113 * set_tx_coal - Sets coalescing interrupt mechanism on TX path
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002114 *
2115 * DESCRIPTION:
2116 * This routine sets the TX coalescing interrupt mechanism parameter.
2117 * This parameter is a timeout counter, that counts in 64 t_clk
2118 * chunks ; that when timeout event occurs a maskable interrupt
2119 * occurs.
2120 * The parameter is calculated using the t_cLK frequency of the
2121 * MV-643xx chip and the required delay in the interrupt in uSec
2122 *
2123 * INPUT:
2124 * struct mv643xx_private *mp Ethernet port
2125 * unsigned int delay Delay in uSeconds
2126 *
2127 * OUTPUT:
2128 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2129 *
2130 * RETURN:
2131 * The interrupt coalescing value set in the gigE port.
2132 *
2133 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002134static unsigned int set_tx_coal(struct mv643xx_private *mp,
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002135 unsigned int delay)
2136{
2137 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2138
2139 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002140 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002141
2142 return coal;
2143}
2144
2145/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002146 * port_init - Initialize the Ethernet port driver
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002147 *
2148 * DESCRIPTION:
2149 * This function prepares the ethernet port to start its activity:
2150 * 1) Completes the ethernet port driver struct initialization toward port
2151 * start routine.
2152 * 2) Resets the device to a quiescent state in case of warm reboot.
2153 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2154 * 4) Clean MAC tables. The reset status of those tables is unknown.
2155 * 5) Set PHY address.
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002156 * Note: Call this routine prior to port_start routine and after
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002157 * setting user values in the user fields of Ethernet port control
2158 * struct.
2159 *
2160 * INPUT:
2161 * struct mv643xx_private *mp Ethernet port control struct
2162 *
2163 * OUTPUT:
2164 * See description.
2165 *
2166 * RETURN:
2167 * None.
2168 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002169static void port_init(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002170{
2171 mp->rx_resource_err = 0;
2172
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002173 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002174
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002175 init_mac_tables(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002176}
2177
2178/*
2179 * mv643xx_eth_open
2180 *
2181 * This function is called when openning the network device. The function
2182 * should initialize all the hardware, initialize cyclic Rx/Tx
2183 * descriptors chain and buffers and allocate an IRQ to the network
2184 * device.
2185 *
2186 * Input : a pointer to the network device structure
2187 *
2188 * Output : zero of success , nonzero if fails.
2189 */
2190
2191static int mv643xx_eth_open(struct net_device *dev)
2192{
2193 struct mv643xx_private *mp = netdev_priv(dev);
2194 unsigned int port_num = mp->port_num;
2195 unsigned int size;
2196 int err;
2197
2198 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002199 wrl(mp, INT_CAUSE(port_num), 0);
2200 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002201 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002202 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002203
2204 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2205 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2206 if (err) {
2207 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2208 return -EAGAIN;
2209 }
2210
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002211 port_init(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002212
2213 memset(&mp->timeout, 0, sizeof(struct timer_list));
2214 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2215 mp->timeout.data = (unsigned long)dev;
2216
2217 /* Allocate RX and TX skb rings */
2218 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2219 GFP_KERNEL);
2220 if (!mp->rx_skb) {
2221 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2222 err = -ENOMEM;
2223 goto out_free_irq;
2224 }
2225 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2226 GFP_KERNEL);
2227 if (!mp->tx_skb) {
2228 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2229 err = -ENOMEM;
2230 goto out_free_rx_skb;
2231 }
2232
2233 /* Allocate TX ring */
2234 mp->tx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002235 size = mp->tx_ring_size * sizeof(struct tx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002236 mp->tx_desc_area_size = size;
2237
2238 if (mp->tx_sram_size) {
2239 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2240 mp->tx_sram_size);
2241 mp->tx_desc_dma = mp->tx_sram_addr;
2242 } else
2243 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2244 &mp->tx_desc_dma,
2245 GFP_KERNEL);
2246
2247 if (!mp->p_tx_desc_area) {
2248 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2249 dev->name, size);
2250 err = -ENOMEM;
2251 goto out_free_tx_skb;
2252 }
2253 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2254 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2255
2256 ether_init_tx_desc_ring(mp);
2257
2258 /* Allocate RX ring */
2259 mp->rx_desc_count = 0;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002260 size = mp->rx_ring_size * sizeof(struct rx_desc);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002261 mp->rx_desc_area_size = size;
2262
2263 if (mp->rx_sram_size) {
2264 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2265 mp->rx_sram_size);
2266 mp->rx_desc_dma = mp->rx_sram_addr;
2267 } else
2268 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2269 &mp->rx_desc_dma,
2270 GFP_KERNEL);
2271
2272 if (!mp->p_rx_desc_area) {
2273 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2274 dev->name, size);
2275 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2276 dev->name);
2277 if (mp->rx_sram_size)
2278 iounmap(mp->p_tx_desc_area);
2279 else
2280 dma_free_coherent(NULL, mp->tx_desc_area_size,
2281 mp->p_tx_desc_area, mp->tx_desc_dma);
2282 err = -ENOMEM;
2283 goto out_free_tx_skb;
2284 }
2285 memset((void *)mp->p_rx_desc_area, 0, size);
2286
2287 ether_init_rx_desc_ring(mp);
2288
2289 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2290
2291#ifdef MV643XX_NAPI
2292 napi_enable(&mp->napi);
2293#endif
2294
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002295 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002296
2297 /* Interrupt Coalescing */
2298
2299#ifdef MV643XX_COAL
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002300 mp->rx_int_coal = set_rx_coal(mp, MV643XX_RX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002301#endif
2302
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002303 mp->tx_int_coal = set_tx_coal(mp, MV643XX_TX_COAL);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002304
2305 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002306 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002307
2308 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002309 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002310
2311 return 0;
2312
2313out_free_tx_skb:
2314 kfree(mp->tx_skb);
2315out_free_rx_skb:
2316 kfree(mp->rx_skb);
2317out_free_irq:
2318 free_irq(dev->irq, dev);
2319
2320 return err;
2321}
2322
2323/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002324 * port_reset - Reset Ethernet port
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002325 *
2326 * DESCRIPTION:
2327 * This routine resets the chip by aborting any SDMA engine activity and
2328 * clearing the MIB counters. The Receiver and the Transmit unit are in
2329 * idle state after this command is performed and the port is disabled.
2330 *
2331 * INPUT:
2332 * struct mv643xx_private *mp Ethernet Port.
2333 *
2334 * OUTPUT:
2335 * Channel activity is halted.
2336 *
2337 * RETURN:
2338 * None.
2339 *
2340 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002341static void port_reset(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002342{
2343 unsigned int port_num = mp->port_num;
2344 unsigned int reg_data;
2345
2346 mv643xx_eth_port_disable_tx(mp);
2347 mv643xx_eth_port_disable_rx(mp);
2348
2349 /* Clear all MIB counters */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002350 clear_mib_counters(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002351
2352 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002353 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002354 reg_data &= ~(SERIAL_PORT_ENABLE |
2355 DO_NOT_FORCE_LINK_FAIL |
2356 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002357 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002358}
2359
2360/*
2361 * mv643xx_eth_stop
2362 *
2363 * This function is used when closing the network device.
2364 * It updates the hardware,
2365 * release all memory that holds buffers and descriptors and release the IRQ.
2366 * Input : a pointer to the device structure
2367 * Output : zero if success , nonzero if fails
2368 */
2369
2370static int mv643xx_eth_stop(struct net_device *dev)
2371{
2372 struct mv643xx_private *mp = netdev_priv(dev);
2373 unsigned int port_num = mp->port_num;
2374
2375 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002376 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002377 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002378 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002379
2380#ifdef MV643XX_NAPI
2381 napi_disable(&mp->napi);
2382#endif
2383 netif_carrier_off(dev);
2384 netif_stop_queue(dev);
2385
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002386 port_reset(mp);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002387
2388 mv643xx_eth_free_tx_rings(dev);
2389 mv643xx_eth_free_rx_rings(dev);
2390
2391 free_irq(dev->irq, dev);
2392
2393 return 0;
2394}
2395
2396static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2397{
2398 struct mv643xx_private *mp = netdev_priv(dev);
2399
2400 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2401}
2402
2403/*
2404 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2405 *
2406 * Input : pointer to ethernet interface network device structure
2407 * new mtu size
2408 * Output : 0 upon success, -EINVAL upon failure
2409 */
2410static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2411{
2412 if ((new_mtu > 9500) || (new_mtu < 64))
2413 return -EINVAL;
2414
2415 dev->mtu = new_mtu;
2416 if (!netif_running(dev))
2417 return 0;
2418
2419 /*
2420 * Stop and then re-open the interface. This will allocate RX
2421 * skbs of the new MTU.
2422 * There is a possible danger that the open will not succeed,
2423 * due to memory being full, which might fail the open function.
2424 */
2425 mv643xx_eth_stop(dev);
2426 if (mv643xx_eth_open(dev)) {
2427 printk(KERN_ERR "%s: Fatal error on opening device\n",
2428 dev->name);
2429 }
2430
2431 return 0;
2432}
2433
2434/*
2435 * mv643xx_eth_tx_timeout_task
2436 *
2437 * Actual routine to reset the adapter when a timeout on Tx has occurred
2438 */
2439static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2440{
2441 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2442 tx_timeout_task);
2443 struct net_device *dev = mp->dev;
2444
2445 if (!netif_running(dev))
2446 return;
2447
2448 netif_stop_queue(dev);
2449
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002450 port_reset(mp);
2451 port_start(dev);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002452
2453 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2454 netif_wake_queue(dev);
2455}
2456
2457/*
2458 * mv643xx_eth_tx_timeout
2459 *
2460 * Called upon a timeout on transmitting a packet
2461 *
2462 * Input : pointer to ethernet interface network device structure.
2463 * Output : N/A
2464 */
2465static void mv643xx_eth_tx_timeout(struct net_device *dev)
2466{
2467 struct mv643xx_private *mp = netdev_priv(dev);
2468
2469 printk(KERN_INFO "%s: TX timeout ", dev->name);
2470
2471 /* Do the reset outside of interrupt context */
2472 schedule_work(&mp->tx_timeout_task);
2473}
2474
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002475#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002476static void mv643xx_netpoll(struct net_device *netdev)
2477{
2478 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002479 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002480
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002481 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002482 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002483 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002484
Al Viro9da3b1a2006-10-08 15:00:44 +01002485 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002486
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002487 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002488}
2489#endif
2490
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002491/*
2492 * Wrappers for MII support library.
2493 */
2494static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2495{
2496 struct mv643xx_private *mp = netdev_priv(dev);
2497 int val;
2498
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002499 read_smi_reg(mp, location, &val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002500 return val;
2501}
2502
2503static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2504{
2505 struct mv643xx_private *mp = netdev_priv(dev);
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002506 write_smi_reg(mp, location, val);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002507}
2508
2509
2510/* platform glue ************************************************************/
2511static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2512 struct mbus_dram_target_info *dram)
2513{
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002514 void __iomem *base = msp->base;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002515 u32 win_enable;
2516 u32 win_protect;
2517 int i;
2518
2519 for (i = 0; i < 6; i++) {
2520 writel(0, base + WINDOW_BASE(i));
2521 writel(0, base + WINDOW_SIZE(i));
2522 if (i < 4)
2523 writel(0, base + WINDOW_REMAP_HIGH(i));
2524 }
2525
2526 win_enable = 0x3f;
2527 win_protect = 0;
2528
2529 for (i = 0; i < dram->num_cs; i++) {
2530 struct mbus_dram_window *cs = dram->cs + i;
2531
2532 writel((cs->base & 0xffff0000) |
2533 (cs->mbus_attr << 8) |
2534 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2535 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2536
2537 win_enable &= ~(1 << i);
2538 win_protect |= 3 << (2 * i);
2539 }
2540
2541 writel(win_enable, base + WINDOW_BAR_ENABLE);
2542 msp->win_protect = win_protect;
2543}
2544
2545static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2546{
2547 static int mv643xx_version_printed = 0;
2548 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2549 struct mv643xx_shared_private *msp;
2550 struct resource *res;
2551 int ret;
2552
2553 if (!mv643xx_version_printed++)
2554 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2555
2556 ret = -EINVAL;
2557 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2558 if (res == NULL)
2559 goto out;
2560
2561 ret = -ENOMEM;
2562 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2563 if (msp == NULL)
2564 goto out;
2565 memset(msp, 0, sizeof(*msp));
2566
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002567 msp->base = ioremap(res->start, res->end - res->start + 1);
2568 if (msp->base == NULL)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002569 goto out_free;
2570
2571 spin_lock_init(&msp->phy_lock);
2572 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2573
2574 platform_set_drvdata(pdev, msp);
2575
2576 /*
2577 * (Re-)program MBUS remapping windows if we are asked to.
2578 */
2579 if (pd != NULL && pd->dram != NULL)
2580 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2581
2582 return 0;
2583
2584out_free:
2585 kfree(msp);
2586out:
2587 return ret;
2588}
2589
2590static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2591{
2592 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2593
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002594 iounmap(msp->base);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002595 kfree(msp);
2596
2597 return 0;
2598}
2599
2600static struct platform_driver mv643xx_eth_shared_driver = {
2601 .probe = mv643xx_eth_shared_probe,
2602 .remove = mv643xx_eth_shared_remove,
2603 .driver = {
2604 .name = MV643XX_ETH_SHARED_NAME,
2605 .owner = THIS_MODULE,
2606 },
2607};
2608
2609/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002610 * phy_addr_set - Set the ethernet port PHY address.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002611 *
2612 * DESCRIPTION:
2613 * This routine sets the given ethernet port PHY address.
2614 *
2615 * INPUT:
2616 * struct mv643xx_private *mp Ethernet Port.
2617 * int phy_addr PHY address.
2618 *
2619 * OUTPUT:
2620 * None.
2621 *
2622 * RETURN:
2623 * None.
2624 *
2625 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002626static void phy_addr_set(struct mv643xx_private *mp, int phy_addr)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002627{
2628 u32 reg_data;
2629 int addr_shift = 5 * mp->port_num;
2630
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002631 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002632 reg_data &= ~(0x1f << addr_shift);
2633 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002634 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002635}
2636
2637/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002638 * phy_addr_get - Get the ethernet port PHY address.
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002639 *
2640 * DESCRIPTION:
2641 * This routine returns the given ethernet port PHY address.
2642 *
2643 * INPUT:
2644 * struct mv643xx_private *mp Ethernet Port.
2645 *
2646 * OUTPUT:
2647 * None.
2648 *
2649 * RETURN:
2650 * PHY address.
2651 *
2652 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002653static int phy_addr_get(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002654{
2655 unsigned int reg_data;
2656
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002657 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002658
2659 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2660}
2661
2662/*
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002663 * phy_detect - Detect whether a phy is present
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002664 *
2665 * DESCRIPTION:
2666 * This function tests whether there is a PHY present on
2667 * the specified port.
2668 *
2669 * INPUT:
2670 * struct mv643xx_private *mp Ethernet Port.
2671 *
2672 * OUTPUT:
2673 * None
2674 *
2675 * RETURN:
2676 * 0 on success
2677 * -ENODEV on failure
2678 *
2679 */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002680static int phy_detect(struct mv643xx_private *mp)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002681{
2682 unsigned int phy_reg_data0;
2683 int auto_neg;
2684
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002685 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002686 auto_neg = phy_reg_data0 & 0x1000;
2687 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002688 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002689
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002690 read_smi_reg(mp, 0, &phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002691 if ((phy_reg_data0 & 0x1000) == auto_neg)
2692 return -ENODEV; /* change didn't take */
2693
2694 phy_reg_data0 ^= 0x1000;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002695 write_smi_reg(mp, 0, phy_reg_data0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002696 return 0;
2697}
2698
James Chapmand0412d92006-01-27 01:15:30 -07002699static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2700 int speed, int duplex,
2701 struct ethtool_cmd *cmd)
2702{
2703 struct mv643xx_private *mp = netdev_priv(dev);
2704
2705 memset(cmd, 0, sizeof(*cmd));
2706
2707 cmd->port = PORT_MII;
2708 cmd->transceiver = XCVR_INTERNAL;
2709 cmd->phy_address = phy_address;
2710
2711 if (speed == 0) {
2712 cmd->autoneg = AUTONEG_ENABLE;
2713 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2714 cmd->speed = SPEED_100;
2715 cmd->advertising = ADVERTISED_10baseT_Half |
2716 ADVERTISED_10baseT_Full |
2717 ADVERTISED_100baseT_Half |
2718 ADVERTISED_100baseT_Full;
2719 if (mp->mii.supports_gmii)
2720 cmd->advertising |= ADVERTISED_1000baseT_Full;
2721 } else {
2722 cmd->autoneg = AUTONEG_DISABLE;
2723 cmd->speed = speed;
2724 cmd->duplex = duplex;
2725 }
2726}
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728/*/
2729 * mv643xx_eth_probe
2730 *
2731 * First function called after registering the network device.
2732 * It's purpose is to initialize the device as an ethernet device,
2733 * fill the ethernet device structure with pointers * to functions,
2734 * and set the MAC address of the interface
2735 *
2736 * Input : struct device *
2737 * Output : -ENOMEM if failed , 0 if success
2738 */
Russell King3ae5eae2005-11-09 22:32:44 +00002739static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002742 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 struct mv643xx_private *mp;
2744 struct net_device *dev;
2745 u8 *p;
2746 struct resource *res;
2747 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002748 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002749 int duplex = DUPLEX_HALF;
2750 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002751 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002753 pd = pdev->dev.platform_data;
2754 if (pd == NULL) {
2755 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2756 return -ENODEV;
2757 }
2758
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002759 if (pd->shared == NULL) {
2760 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2761 return -ENODEV;
2762 }
2763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2765 if (!dev)
2766 return -ENOMEM;
2767
Russell King3ae5eae2005-11-09 22:32:44 +00002768 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
2770 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002771 mp->dev = dev;
2772#ifdef MV643XX_NAPI
2773 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2774#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
2776 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2777 BUG_ON(!res);
2778 dev->irq = res->start;
2779
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 dev->open = mv643xx_eth_open;
2781 dev->stop = mv643xx_eth_stop;
2782 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 dev->set_mac_address = mv643xx_eth_set_mac_address;
2784 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2785
2786 /* No need to Tx Timeout */
2787 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002789#ifdef CONFIG_NET_POLL_CONTROLLER
2790 dev->poll_controller = mv643xx_netpoll;
2791#endif
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 dev->base_addr = 0;
2795 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002796 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
2798
2799#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2800#ifdef MAX_SKB_FRAGS
2801 /*
2802 * Zero copy can only work if we use Discovery II memory. Else, we will
2803 * have to map the buffers to ISA memory which is only 16 MB
2804 */
Wolfram Joost63890572006-01-16 16:57:41 -07002805 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806#endif
2807#endif
2808
2809 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002810 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
2812 spin_lock_init(&mp->lock);
2813
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002814 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002815 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002816
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002817 if (mp->shared->win_protect)
2818 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2819
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002820 mp->shared_smi = mp->shared;
2821 if (pd->shared_smi != NULL)
2822 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 /* set default config values */
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002825 uc_addr_get(mp, dev->dev_addr);
2826 mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE;
2827 mp->tx_ring_size = DEFAULT_TX_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002829 if (is_valid_ether_addr(pd->mac_addr))
2830 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002832 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002833 phy_addr_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002835 if (pd->rx_queue_size)
2836 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002838 if (pd->tx_queue_size)
2839 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002841 if (pd->tx_sram_size) {
2842 mp->tx_sram_size = pd->tx_sram_size;
2843 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 }
2845
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002846 if (pd->rx_sram_size) {
2847 mp->rx_sram_size = pd->rx_sram_size;
2848 mp->rx_sram_addr = pd->rx_sram_addr;
2849 }
2850
2851 duplex = pd->duplex;
2852 speed = pd->speed;
2853
James Chapmanc28a4f82006-01-27 01:13:15 -07002854 /* Hook up MII support for ethtool */
2855 mp->mii.dev = dev;
2856 mp->mii.mdio_read = mv643xx_mdio_read;
2857 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002858 mp->mii.phy_id = phy_addr_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002859 mp->mii.phy_id_mask = 0x3f;
2860 mp->mii.reg_num_mask = 0x1f;
2861
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002862 err = phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002864 pr_debug("%s: No PHY detected at addr %d\n",
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002865 dev->name, phy_addr_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002866 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002869 phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002870 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002871 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2872 mv643xx_eth_update_pscr(dev, &cmd);
2873 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002874
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002875 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 err = register_netdev(dev);
2877 if (err)
2878 goto out;
2879
2880 p = dev->dev_addr;
2881 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002882 "%s: port %d with MAC address %s\n",
2883 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 if (dev->features & NETIF_F_SG)
2886 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2887
2888 if (dev->features & NETIF_F_IP_CSUM)
2889 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2890 dev->name);
2891
2892#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2893 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2894#endif
2895
2896#ifdef MV643XX_COAL
2897 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2898 dev->name);
2899#endif
2900
2901#ifdef MV643XX_NAPI
2902 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2903#endif
2904
Nicolas DETb1529872005-10-28 17:46:30 -07002905 if (mp->tx_sram_size > 0)
2906 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 return 0;
2909
2910out:
2911 free_netdev(dev);
2912
2913 return err;
2914}
2915
Russell King3ae5eae2005-11-09 22:32:44 +00002916static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Russell King3ae5eae2005-11-09 22:32:44 +00002918 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 unregister_netdev(dev);
2921 flush_scheduled_work();
2922
2923 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002924 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
2926}
2927
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002928static void mv643xx_eth_shutdown(struct platform_device *pdev)
2929{
2930 struct net_device *dev = platform_get_drvdata(pdev);
2931 struct mv643xx_private *mp = netdev_priv(dev);
2932 unsigned int port_num = mp->port_num;
2933
2934 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002935 wrl(mp, INT_MASK(port_num), 0);
2936 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002937
Lennert Buytenhekcc9754b2008-06-01 02:10:27 +02002938 port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002939}
2940
Russell King3ae5eae2005-11-09 22:32:44 +00002941static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 .probe = mv643xx_eth_probe,
2943 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002944 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002945 .driver = {
2946 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002947 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002948 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949};
2950
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951/*
2952 * mv643xx_init_module
2953 *
2954 * Registers the network drivers into the Linux kernel
2955 *
2956 * Input : N/A
2957 *
2958 * Output : N/A
2959 */
2960static int __init mv643xx_init_module(void)
2961{
2962 int rc;
2963
Russell King3ae5eae2005-11-09 22:32:44 +00002964 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00002966 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00002968 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 }
2970 return rc;
2971}
2972
2973/*
2974 * mv643xx_cleanup_module
2975 *
2976 * Registers the network drivers into the Linux kernel
2977 *
2978 * Input : N/A
2979 *
2980 * Output : N/A
2981 */
2982static void __exit mv643xx_cleanup_module(void)
2983{
Russell King3ae5eae2005-11-09 22:32:44 +00002984 platform_driver_unregister(&mv643xx_eth_driver);
2985 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986}
2987
2988module_init(mv643xx_init_module);
2989module_exit(mv643xx_cleanup_module);
2990
2991MODULE_LICENSE("GPL");
2992MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
2993 " and Dale Farnsworth");
2994MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07002995MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
2996MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);