blob: 6324556ccb67253fab0338f2399e3299e8f3fa96 [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 Buytenhekfbd6a752007-10-19 16:03:46 +0200170#define PORT_DEFAULT_TRANSMIT_QUEUE_SIZE 800
171#define PORT_DEFAULT_RECEIVE_QUEUE_SIZE 400
172
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200173/* SMI reg */
174#define ETH_SMI_BUSY 0x10000000 /* 0 - Write, 1 - Read */
175#define ETH_SMI_READ_VALID 0x08000000 /* 0 - Write, 1 - Read */
176#define ETH_SMI_OPCODE_WRITE 0 /* Completion of Read */
177#define ETH_SMI_OPCODE_READ 0x04000000 /* Operation is in progress */
178
179/* Interrupt Cause Register Bit Definitions */
180
181/* SDMA command status fields macros */
182
183/* Tx & Rx descriptors status */
184#define ETH_ERROR_SUMMARY 0x00000001
185
186/* Tx & Rx descriptors command */
187#define ETH_BUFFER_OWNED_BY_DMA 0x80000000
188
189/* Tx descriptors status */
190#define ETH_LC_ERROR 0
191#define ETH_UR_ERROR 0x00000002
192#define ETH_RL_ERROR 0x00000004
193#define ETH_LLC_SNAP_FORMAT 0x00000200
194
195/* Rx descriptors status */
196#define ETH_OVERRUN_ERROR 0x00000002
197#define ETH_MAX_FRAME_LENGTH_ERROR 0x00000004
198#define ETH_RESOURCE_ERROR 0x00000006
199#define ETH_VLAN_TAGGED 0x00080000
200#define ETH_BPDU_FRAME 0x00100000
201#define ETH_UDP_FRAME_OVER_IP_V_4 0x00200000
202#define ETH_OTHER_FRAME_TYPE 0x00400000
203#define ETH_LAYER_2_IS_ETH_V_2 0x00800000
204#define ETH_FRAME_TYPE_IP_V_4 0x01000000
205#define ETH_FRAME_HEADER_OK 0x02000000
206#define ETH_RX_LAST_DESC 0x04000000
207#define ETH_RX_FIRST_DESC 0x08000000
208#define ETH_UNKNOWN_DESTINATION_ADDR 0x10000000
209#define ETH_RX_ENABLE_INTERRUPT 0x20000000
210#define ETH_LAYER_4_CHECKSUM_OK 0x40000000
211
212/* Rx descriptors byte count */
213#define ETH_FRAME_FRAGMENTED 0x00000004
214
215/* Tx descriptors command */
216#define ETH_LAYER_4_CHECKSUM_FIRST_DESC 0x00000400
217#define ETH_FRAME_SET_TO_VLAN 0x00008000
218#define ETH_UDP_FRAME 0x00010000
219#define ETH_GEN_TCP_UDP_CHECKSUM 0x00020000
220#define ETH_GEN_IP_V_4_CHECKSUM 0x00040000
221#define ETH_ZERO_PADDING 0x00080000
222#define ETH_TX_LAST_DESC 0x00100000
223#define ETH_TX_FIRST_DESC 0x00200000
224#define ETH_GEN_CRC 0x00400000
225#define ETH_TX_ENABLE_INTERRUPT 0x00800000
226#define ETH_AUTO_MODE 0x40000000
227
228#define ETH_TX_IHL_SHIFT 11
229
230/* typedefs */
231
232typedef enum _eth_func_ret_status {
233 ETH_OK, /* Returned as expected. */
234 ETH_ERROR, /* Fundamental error. */
235 ETH_RETRY, /* Could not process request. Try later.*/
236 ETH_END_OF_JOB, /* Ring has nothing to process. */
237 ETH_QUEUE_FULL, /* Ring resource error. */
238 ETH_QUEUE_LAST_RESOURCE /* Ring resources about to exhaust. */
239} ETH_FUNC_RET_STATUS;
240
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200241/* These are for big-endian machines. Little endian needs different
242 * definitions.
243 */
244#if defined(__BIG_ENDIAN)
245struct eth_rx_desc {
246 u16 byte_cnt; /* Descriptor buffer byte count */
247 u16 buf_size; /* Buffer size */
248 u32 cmd_sts; /* Descriptor command status */
249 u32 next_desc_ptr; /* Next descriptor pointer */
250 u32 buf_ptr; /* Descriptor buffer pointer */
251};
252
253struct eth_tx_desc {
254 u16 byte_cnt; /* buffer byte count */
255 u16 l4i_chk; /* CPU provided TCP checksum */
256 u32 cmd_sts; /* Command/status field */
257 u32 next_desc_ptr; /* Pointer to next descriptor */
258 u32 buf_ptr; /* pointer to buffer for this descriptor*/
259};
260#elif defined(__LITTLE_ENDIAN)
261struct eth_rx_desc {
262 u32 cmd_sts; /* Descriptor command status */
263 u16 buf_size; /* Buffer size */
264 u16 byte_cnt; /* Descriptor buffer byte count */
265 u32 buf_ptr; /* Descriptor buffer pointer */
266 u32 next_desc_ptr; /* Next descriptor pointer */
267};
268
269struct eth_tx_desc {
270 u32 cmd_sts; /* Command/status field */
271 u16 l4i_chk; /* CPU provided TCP checksum */
272 u16 byte_cnt; /* buffer byte count */
273 u32 buf_ptr; /* pointer to buffer for this descriptor*/
274 u32 next_desc_ptr; /* Pointer to next descriptor */
275};
276#else
277#error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined
278#endif
279
280/* Unified struct for Rx and Tx operations. The user is not required to */
281/* be familier with neither Tx nor Rx descriptors. */
282struct pkt_info {
283 unsigned short byte_cnt; /* Descriptor buffer byte count */
284 unsigned short l4i_chk; /* Tx CPU provided TCP Checksum */
285 unsigned int cmd_sts; /* Descriptor command status */
286 dma_addr_t buf_ptr; /* Descriptor buffer pointer */
287 struct sk_buff *return_info; /* User resource return information */
288};
289
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200290
291/* global *******************************************************************/
292struct mv643xx_shared_private {
293 void __iomem *eth_base;
294
295 /* used to protect SMI_REG, which is shared across ports */
296 spinlock_t phy_lock;
297
298 u32 win_protect;
299
300 unsigned int t_clk;
301};
302
303
304/* per-port *****************************************************************/
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200305struct mv643xx_mib_counters {
306 u64 good_octets_received;
307 u32 bad_octets_received;
308 u32 internal_mac_transmit_err;
309 u32 good_frames_received;
310 u32 bad_frames_received;
311 u32 broadcast_frames_received;
312 u32 multicast_frames_received;
313 u32 frames_64_octets;
314 u32 frames_65_to_127_octets;
315 u32 frames_128_to_255_octets;
316 u32 frames_256_to_511_octets;
317 u32 frames_512_to_1023_octets;
318 u32 frames_1024_to_max_octets;
319 u64 good_octets_sent;
320 u32 good_frames_sent;
321 u32 excessive_collision;
322 u32 multicast_frames_sent;
323 u32 broadcast_frames_sent;
324 u32 unrec_mac_control_received;
325 u32 fc_sent;
326 u32 good_fc_received;
327 u32 bad_fc_received;
328 u32 undersize_received;
329 u32 fragments_received;
330 u32 oversize_received;
331 u32 jabber_received;
332 u32 mac_receive_error;
333 u32 bad_crc_event;
334 u32 collision;
335 u32 late_collision;
336};
337
338struct mv643xx_private {
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200339 struct mv643xx_shared_private *shared;
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200340 int port_num; /* User Ethernet port number */
341
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +0200342 struct mv643xx_shared_private *shared_smi;
343
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200344 u32 rx_sram_addr; /* Base address of rx sram area */
345 u32 rx_sram_size; /* Size of rx sram area */
346 u32 tx_sram_addr; /* Base address of tx sram area */
347 u32 tx_sram_size; /* Size of tx sram area */
348
349 int rx_resource_err; /* Rx ring resource error flag */
350
351 /* Tx/Rx rings managment indexes fields. For driver use */
352
353 /* Next available and first returning Rx resource */
354 int rx_curr_desc_q, rx_used_desc_q;
355
356 /* Next available and first returning Tx resource */
357 int tx_curr_desc_q, tx_used_desc_q;
358
359#ifdef MV643XX_TX_FAST_REFILL
360 u32 tx_clean_threshold;
361#endif
362
363 struct eth_rx_desc *p_rx_desc_area;
364 dma_addr_t rx_desc_dma;
365 int rx_desc_area_size;
366 struct sk_buff **rx_skb;
367
368 struct eth_tx_desc *p_tx_desc_area;
369 dma_addr_t tx_desc_dma;
370 int tx_desc_area_size;
371 struct sk_buff **tx_skb;
372
373 struct work_struct tx_timeout_task;
374
375 struct net_device *dev;
376 struct napi_struct napi;
377 struct net_device_stats stats;
378 struct mv643xx_mib_counters mib_counters;
379 spinlock_t lock;
380 /* Size of Tx Ring per queue */
381 int tx_ring_size;
382 /* Number of tx descriptors in use */
383 int tx_desc_count;
384 /* Size of Rx Ring per queue */
385 int rx_ring_size;
386 /* Number of rx descriptors in use */
387 int rx_desc_count;
388
389 /*
390 * Used in case RX Ring is empty, which can be caused when
391 * system does not have resources (skb's)
392 */
393 struct timer_list timeout;
394
395 u32 rx_int_coal;
396 u32 tx_int_coal;
397 struct mii_if_info mii;
398};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Lennert Buytenhekfbd6a752007-10-19 16:03:46 +0200400
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200401/* port register accessors **************************************************/
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700402static inline u32 rdl(struct mv643xx_private *mp, int offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200404 return readl(mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Lennert Buytenhekec69d652008-03-18 11:38:05 -0700407static inline void wrl(struct mv643xx_private *mp, int offset, u32 data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +0200409 writel(data, mp->shared->eth_base + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200412
413/* rxq/txq helper functions *************************************************/
414static void mv643xx_eth_port_enable_rx(struct mv643xx_private *mp,
415 unsigned int queues)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200417 wrl(mp, RXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200420static unsigned int mv643xx_eth_port_disable_rx(struct mv643xx_private *mp)
421{
422 unsigned int port_num = mp->port_num;
423 u32 queues;
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700424
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200425 /* Stop Rx port activity. Check port Rx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200426 queues = rdl(mp, RXQ_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, RXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200430
431 /* Wait for all Rx activity to terminate. */
432 /* Check port cause register that all Rx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200433 while (rdl(mp, RXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200434 udelay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200437 return queues;
438}
439
440static void mv643xx_eth_port_enable_tx(struct mv643xx_private *mp,
441 unsigned int queues)
442{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200443 wrl(mp, TXQ_COMMAND(mp->port_num), queues);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200444}
445
446static unsigned int mv643xx_eth_port_disable_tx(struct mv643xx_private *mp)
447{
448 unsigned int port_num = mp->port_num;
449 u32 queues;
450
451 /* Stop Tx port activity. Check port Tx activity. */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200452 queues = rdl(mp, TXQ_COMMAND(port_num)) & 0xFF;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200453 if (queues) {
454 /* Issue stop command for active queues only */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200455 wrl(mp, TXQ_COMMAND(port_num), (queues << 8));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200456
457 /* Wait for all Tx activity to terminate. */
458 /* Check port cause register that all Tx queues are stopped */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200459 while (rdl(mp, TXQ_COMMAND(port_num)) & 0xFF)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200460 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200461
462 /* Wait for Tx FIFO to empty */
Lennert Buytenheka2a41682008-06-01 01:30:42 +0200463 while (rdl(mp, PORT_STATUS(port_num)) & TX_FIFO_EMPTY)
Lennert Buytenheke1bea502008-06-01 01:29:14 +0200464 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200465 }
466
467 return queues;
468}
469
470
471/* rx ***********************************************************************/
472static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev);
473
474/*
475 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
476 *
477 * DESCRIPTION:
478 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
479 * next 'used' descriptor and attached the returned buffer to it.
480 * In case the Rx ring was in "resource error" condition, where there are
481 * no available Rx resources, the function resets the resource error flag.
482 *
483 * INPUT:
484 * struct mv643xx_private *mp Ethernet Port Control srtuct.
485 * struct pkt_info *p_pkt_info Information on returned buffer.
486 *
487 * OUTPUT:
488 * New available Rx resource in Rx descriptor ring.
489 *
490 * RETURN:
491 * ETH_ERROR in case the routine can not access Rx desc ring.
492 * ETH_OK otherwise.
493 */
494static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
495 struct pkt_info *p_pkt_info)
496{
497 int used_rx_desc; /* Where to return Rx resource */
498 volatile struct eth_rx_desc *p_used_rx_desc;
499 unsigned long flags;
500
501 spin_lock_irqsave(&mp->lock, flags);
502
503 /* Get 'used' Rx descriptor */
504 used_rx_desc = mp->rx_used_desc_q;
505 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
506
507 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
508 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
509 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
510
511 /* Flush the write pipe */
512
513 /* Return the descriptor to DMA ownership */
514 wmb();
515 p_used_rx_desc->cmd_sts =
516 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
517 wmb();
518
519 /* Move the used descriptor pointer to the next descriptor */
520 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
521
522 /* Any Rx return cancels the Rx resource error status */
523 mp->rx_resource_err = 0;
524
525 spin_unlock_irqrestore(&mp->lock, flags);
526
527 return ETH_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700531 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *
533 * Fills / refills RX queue on a certain gigabit ethernet port
534 *
535 * Input : pointer to ethernet interface network device structure
536 * Output : N/A
537 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700538static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct mv643xx_private *mp = netdev_priv(dev);
541 struct pkt_info pkt_info;
542 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700543 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700545 while (mp->rx_desc_count < mp->rx_ring_size) {
Ralf Baechle908b6372007-02-26 19:52:06 +0000546 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + dma_get_cache_alignment());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!skb)
548 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700549 mp->rx_desc_count++;
Ralf Baechle908b6372007-02-26 19:52:06 +0000550 unaligned = (u32)skb->data & (dma_get_cache_alignment() - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700551 if (unaligned)
Ralf Baechle908b6372007-02-26 19:52:06 +0000552 skb_reserve(skb, dma_get_cache_alignment() - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700554 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
555 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
556 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 pkt_info.return_info = skb;
558 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
559 printk(KERN_ERR
560 "%s: Error allocating RX Ring\n", dev->name);
561 break;
562 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700563 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700567 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700569 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700571 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700577 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 *
579 * Timer routine to wake up RX queue filling task. This function is
580 * used only in case the RX queue is empty, and all alloc_skb has
581 * failed (due to out of memory event).
582 *
583 * Input : pointer to ethernet interface network device structure
584 * Output : N/A
585 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700586static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700588 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200592 * eth_port_receive - Get received information from Rx ring.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200594 * DESCRIPTION:
595 * This routine returns the received data to the caller. There is no
596 * data copying during routine operation. All information is returned
597 * using pointer to packet information struct passed from the caller.
598 * If the routine exhausts Rx ring resources then the resource error flag
599 * is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 *
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200601 * INPUT:
602 * struct mv643xx_private *mp Ethernet Port Control srtuct.
603 * struct pkt_info *p_pkt_info User packet buffer.
604 *
605 * OUTPUT:
606 * Rx ring current and used indexes are updated.
607 *
608 * RETURN:
609 * ETH_ERROR in case the routine can not access Rx desc ring.
610 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
611 * ETH_END_OF_JOB if there is no received data.
612 * ETH_OK otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200614static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
615 struct pkt_info *p_pkt_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200617 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
618 volatile struct eth_rx_desc *p_rx_desc;
619 unsigned int command_status;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700620 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200622 /* Do not process Rx ring in case of Rx ring resource error */
623 if (mp->rx_resource_err)
624 return ETH_QUEUE_FULL;
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700625
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200626 spin_lock_irqsave(&mp->lock, flags);
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700627
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200628 /* Get the Rx Desc ring 'curr and 'used' indexes */
629 rx_curr_desc = mp->rx_curr_desc_q;
630 rx_used_desc = mp->rx_used_desc_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200632 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700633
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200634 /* The following parameters are used to save readings from memory */
635 command_status = p_rx_desc->cmd_sts;
636 rmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700637
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200638 /* Nothing to receive... */
639 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
Dale Farnsworthd344bff2007-01-23 09:52:25 -0700640 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200641 return ETH_END_OF_JOB;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Lennert Buytenhek0a6cf74d2008-06-01 11:16:21 +0200644 p_pkt_info->byte_cnt = p_rx_desc->byte_cnt - ETH_HW_IP_ALIGN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200645 p_pkt_info->cmd_sts = command_status;
Lennert Buytenhek0a6cf74d2008-06-01 11:16:21 +0200646 p_pkt_info->buf_ptr = p_rx_desc->buf_ptr + ETH_HW_IP_ALIGN;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200647 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
648 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200650 /*
651 * Clean the return info field to indicate that the
652 * packet has been moved to the upper layers
653 */
654 mp->rx_skb[rx_curr_desc] = NULL;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700655
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200656 /* Update current index in data structure */
657 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
658 mp->rx_curr_desc_q = rx_next_curr_desc;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700659
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200660 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
661 if (rx_next_curr_desc == rx_used_desc)
662 mp->rx_resource_err = 1;
663
664 spin_unlock_irqrestore(&mp->lock, flags);
665
666 return ETH_OK;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
670 * mv643xx_eth_receive
671 *
672 * This function is forward packets that are received from the port's
673 * queues toward kernel core or FastRoute them to another interface.
674 *
675 * Input : dev - a pointer to the required interface
676 * max - maximum number to receive (0 means unlimted)
677 *
678 * Output : number of served packets
679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
682 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700683 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 unsigned int received_packets = 0;
685 struct sk_buff *skb;
686 struct pkt_info pkt_info;
687
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700688 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Jeff Garzik54caf442006-09-21 00:08:10 -0400689 dma_unmap_single(NULL, pkt_info.buf_ptr, ETH_RX_SKB_SIZE,
Dale Farnsworth71d28722006-09-13 09:21:08 -0700690 DMA_FROM_DEVICE);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700691 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700693
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700694 /*
695 * Update statistics.
696 * Note byte count includes 4 byte CRC count
697 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 stats->rx_packets++;
699 stats->rx_bytes += pkt_info.byte_cnt;
700 skb = pkt_info.return_info;
701 /*
702 * In case received a packet without first / last bits on OR
703 * the error summary bit is on, the packets needs to be dropeed.
704 */
705 if (((pkt_info.cmd_sts
706 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
707 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
708 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
709 stats->rx_dropped++;
710 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
711 ETH_RX_LAST_DESC)) !=
712 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
713 if (net_ratelimit())
714 printk(KERN_ERR
715 "%s: Received packet spread "
716 "on multiple descriptors\n",
717 dev->name);
718 }
719 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
720 stats->rx_errors++;
721
722 dev_kfree_skb_irq(skb);
723 } else {
724 /*
725 * The -4 is for the CRC in the trailer of the
726 * received packet
727 */
728 skb_put(skb, pkt_info.byte_cnt - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
731 skb->ip_summed = CHECKSUM_UNNECESSARY;
732 skb->csum = htons(
733 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
734 }
735 skb->protocol = eth_type_trans(skb, dev);
736#ifdef MV643XX_NAPI
737 netif_receive_skb(skb);
738#else
739 netif_rx(skb);
740#endif
741 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700742 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700744 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 return received_packets;
747}
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/*
751 * mv643xx_poll
752 *
753 * This function is used in case of NAPI
754 */
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700755static int mv643xx_poll(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700757 struct mv643xx_private *mp = container_of(napi, struct mv643xx_private, napi);
758 struct net_device *dev = mp->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 unsigned int port_num = mp->port_num;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700760 int work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762#ifdef MV643XX_TX_FAST_REFILL
763 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700764 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767#endif
768
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700769 work_done = 0;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200770 if ((rdl(mp, RXQ_CURRENT_DESC_PTR(port_num)))
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700771 != (u32) mp->rx_used_desc_q)
772 work_done = mv643xx_eth_receive_queue(dev, budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700774 if (work_done < budget) {
775 netif_rx_complete(dev, napi);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +0200776 wrl(mp, INT_CAUSE(port_num), 0);
777 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200778 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700781 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783#endif
784
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200785
786/* tx ***********************************************************************/
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700787/**
788 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
789 *
790 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -0700791 * This helper function detects that case.
792 */
793
794static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
795{
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700796 unsigned int frag;
797 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700798
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700799 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
800 fragp = &skb_shinfo(skb)->frags[frag];
801 if (fragp->size <= 8 && fragp->page_offset & 0x7)
802 return 1;
803 }
804 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700805}
806
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700807/**
808 * eth_alloc_tx_desc_index - return the index of the next available tx desc
809 */
810static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
811{
812 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -0700813
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700814 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700815
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700816 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700817 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
818
819 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
820
821 return tx_desc_curr;
822}
823
824/**
825 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700827 * Ensure the data for each fragment to be transmitted is mapped properly,
828 * then fill in descriptors in the tx hw queue.
829 */
830static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
831 struct sk_buff *skb)
832{
833 int frag;
834 int tx_index;
835 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700836
837 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
838 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
839
840 tx_index = eth_alloc_tx_desc_index(mp);
841 desc = &mp->p_tx_desc_area[tx_index];
842
843 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
844 /* Last Frag enables interrupt and frees the skb */
845 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
846 desc->cmd_sts |= ETH_ZERO_PADDING |
847 ETH_TX_LAST_DESC |
848 ETH_TX_ENABLE_INTERRUPT;
849 mp->tx_skb[tx_index] = skb;
850 } else
Al Viro05980772006-05-30 23:59:09 -0400851 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700852
853 desc = &mp->p_tx_desc_area[tx_index];
854 desc->l4i_chk = 0;
855 desc->byte_cnt = this_frag->size;
856 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
857 this_frag->page_offset,
858 this_frag->size,
859 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700860 }
861}
862
Byron Bradley324ff2c2008-02-04 23:47:15 -0800863static inline __be16 sum16_as_be(__sum16 sum)
864{
865 return (__force __be16)sum;
866}
867
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700868/**
869 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700871 * Ensure the data for an skb to be transmitted is mapped properly,
872 * then fill in descriptors in the tx hw queue and start the hardware.
873 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700874static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
875 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700876{
877 int tx_index;
878 struct eth_tx_desc *desc;
879 u32 cmd_sts;
880 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700881 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700882
883 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
884
885 tx_index = eth_alloc_tx_desc_index(mp);
886 desc = &mp->p_tx_desc_area[tx_index];
887
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700888 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700889 eth_tx_fill_frag_descs(mp, skb);
890
891 length = skb_headlen(skb);
Al Viro05980772006-05-30 23:59:09 -0400892 mp->tx_skb[tx_index] = NULL;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700893 } else {
894 cmd_sts |= ETH_ZERO_PADDING |
895 ETH_TX_LAST_DESC |
896 ETH_TX_ENABLE_INTERRUPT;
897 length = skb->len;
898 mp->tx_skb[tx_index] = skb;
899 }
900
901 desc->byte_cnt = length;
902 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700903
Patrick McHardy84fa7932006-08-29 16:44:56 -0700904 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Byron Bradley324ff2c2008-02-04 23:47:15 -0800905 BUG_ON(skb->protocol != htons(ETH_P_IP));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700906
907 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
908 ETH_GEN_IP_V_4_CHECKSUM |
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700909 ip_hdr(skb)->ihl << ETH_TX_IHL_SHIFT;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700910
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700911 switch (ip_hdr(skb)->protocol) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700912 case IPPROTO_UDP:
913 cmd_sts |= ETH_UDP_FRAME;
Byron Bradley324ff2c2008-02-04 23:47:15 -0800914 desc->l4i_chk = ntohs(sum16_as_be(udp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700915 break;
916 case IPPROTO_TCP:
Byron Bradley324ff2c2008-02-04 23:47:15 -0800917 desc->l4i_chk = ntohs(sum16_as_be(tcp_hdr(skb)->check));
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700918 break;
919 default:
920 BUG();
921 }
922 } else {
923 /* Errata BTS #50, IHL must be 5 if no HW checksum */
924 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
925 desc->l4i_chk = 0;
926 }
927
928 /* ensure all other descriptors are written before first cmd_sts */
929 wmb();
930 desc->cmd_sts = cmd_sts;
931
932 /* ensure all descriptors are written before poking hardware */
933 wmb();
Lennert Buytenhek073a3452008-06-01 02:00:31 +0200934 mv643xx_eth_port_enable_tx(mp, 1);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700935
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700936 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700937}
938
939/**
940 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
943static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
944{
945 struct mv643xx_private *mp = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700946 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700949 BUG_ON(netif_queue_stopped(dev));
Dale Farnsworth94843562006-04-11 18:24:26 -0700950
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700951 if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) {
952 stats->tx_dropped++;
953 printk(KERN_DEBUG "%s: failed to linearize tiny "
954 "unaligned fragment\n", dev->name);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700955 return NETDEV_TX_BUSY;
Dale Farnsworth94843562006-04-11 18:24:26 -0700956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 spin_lock_irqsave(&mp->lock, flags);
959
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700960 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
961 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
962 netif_stop_queue(dev);
963 spin_unlock_irqrestore(&mp->lock, flags);
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700964 return NETDEV_TX_BUSY;
Lennert Buytenhek4d64e712008-03-18 11:32:41 -0700965 }
966
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700967 eth_tx_submit_descs_for_skb(mp, skb);
Dale Farnsworthe7e381f2007-09-14 11:23:16 -0700968 stats->tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 stats->tx_packets++;
970 dev->trans_start = jiffies;
971
Dale Farnsworthc8aaea22006-03-03 10:02:05 -0700972 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
973 netif_stop_queue(dev);
974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 spin_unlock_irqrestore(&mp->lock, flags);
976
Lennert Buytenhekc0d0f2c2008-03-18 11:34:34 -0700977 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +0200980
981/* mii management interface *************************************************/
982static int ethernet_phy_get(struct mv643xx_private *mp);
983
984/*
985 * eth_port_read_smi_reg - Read PHY registers
986 *
987 * DESCRIPTION:
988 * This routine utilize the SMI interface to interact with the PHY in
989 * order to perform PHY register read.
990 *
991 * INPUT:
992 * struct mv643xx_private *mp Ethernet Port.
993 * unsigned int phy_reg PHY register address offset.
994 * unsigned int *value Register value buffer.
995 *
996 * OUTPUT:
997 * Write the value of a specified PHY register into given buffer.
998 *
999 * RETURN:
1000 * false if the PHY is busy or read data is not in valid state.
1001 * true otherwise.
1002 *
1003 */
1004static void eth_port_read_smi_reg(struct mv643xx_private *mp,
1005 unsigned int phy_reg, unsigned int *value)
1006{
1007 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1008 int phy_addr = ethernet_phy_get(mp);
1009 unsigned long flags;
1010 int i;
1011
1012 /* the SMI register is a shared resource */
1013 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1014
1015 /* wait for the SMI register to become available */
1016 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001017 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001018 printk("%s: PHY busy timeout\n", mp->dev->name);
1019 goto out;
1020 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001021 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001022 }
1023
1024 writel((phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ,
1025 smi_reg);
1026
1027 /* now wait for the data to be valid */
1028 for (i = 0; !(readl(smi_reg) & ETH_SMI_READ_VALID); i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001029 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001030 printk("%s: PHY read timeout\n", mp->dev->name);
1031 goto out;
1032 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001033 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001034 }
1035
1036 *value = readl(smi_reg) & 0xffff;
1037out:
1038 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1039}
1040
1041/*
1042 * eth_port_write_smi_reg - Write to PHY registers
1043 *
1044 * DESCRIPTION:
1045 * This routine utilize the SMI interface to interact with the PHY in
1046 * order to perform writes to PHY registers.
1047 *
1048 * INPUT:
1049 * struct mv643xx_private *mp Ethernet Port.
1050 * unsigned int phy_reg PHY register address offset.
1051 * unsigned int value Register value.
1052 *
1053 * OUTPUT:
1054 * Write the given value to the specified PHY register.
1055 *
1056 * RETURN:
1057 * false if the PHY is busy.
1058 * true otherwise.
1059 *
1060 */
1061static void eth_port_write_smi_reg(struct mv643xx_private *mp,
1062 unsigned int phy_reg, unsigned int value)
1063{
1064 void __iomem *smi_reg = mp->shared_smi->eth_base + SMI_REG;
1065 int phy_addr = ethernet_phy_get(mp);
1066 unsigned long flags;
1067 int i;
1068
1069 /* the SMI register is a shared resource */
1070 spin_lock_irqsave(&mp->shared_smi->phy_lock, flags);
1071
1072 /* wait for the SMI register to become available */
1073 for (i = 0; readl(smi_reg) & ETH_SMI_BUSY; i++) {
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001074 if (i == 1000) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001075 printk("%s: PHY busy timeout\n", mp->dev->name);
1076 goto out;
1077 }
Lennert Buytenheke1bea502008-06-01 01:29:14 +02001078 udelay(10);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001079 }
1080
1081 writel((phy_addr << 16) | (phy_reg << 21) |
1082 ETH_SMI_OPCODE_WRITE | (value & 0xffff), smi_reg);
1083out:
1084 spin_unlock_irqrestore(&mp->shared_smi->phy_lock, flags);
1085}
1086
1087
1088/* mib counters *************************************************************/
1089/*
1090 * eth_clear_mib_counters - Clear all MIB counters
1091 *
1092 * DESCRIPTION:
1093 * This function clears all MIB counters of a specific ethernet port.
1094 * A read from the MIB counter will reset the counter.
1095 *
1096 * INPUT:
1097 * struct mv643xx_private *mp Ethernet Port.
1098 *
1099 * OUTPUT:
1100 * After reading all MIB counters, the counters resets.
1101 *
1102 * RETURN:
1103 * MIB counter value.
1104 *
1105 */
1106static void eth_clear_mib_counters(struct mv643xx_private *mp)
1107{
1108 unsigned int port_num = mp->port_num;
1109 int i;
1110
1111 /* Perform dummy reads from MIB counters */
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +02001112 for (i = 0; i < 0x80; i += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001113 rdl(mp, MIB_COUNTERS(port_num) + i);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001114}
1115
1116static inline u32 read_mib(struct mv643xx_private *mp, int offset)
1117{
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001118 return rdl(mp, MIB_COUNTERS(mp->port_num) + offset);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001119}
1120
1121static void eth_update_mib_counters(struct mv643xx_private *mp)
1122{
1123 struct mv643xx_mib_counters *p = &mp->mib_counters;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001124
Lennert Buytenhek4b8e3652008-06-01 01:29:58 +02001125 p->good_octets_received += read_mib(mp, 0x00);
1126 p->good_octets_received += (u64)read_mib(mp, 0x04) << 32;
1127 p->bad_octets_received += read_mib(mp, 0x08);
1128 p->internal_mac_transmit_err += read_mib(mp, 0x0c);
1129 p->good_frames_received += read_mib(mp, 0x10);
1130 p->bad_frames_received += read_mib(mp, 0x14);
1131 p->broadcast_frames_received += read_mib(mp, 0x18);
1132 p->multicast_frames_received += read_mib(mp, 0x1c);
1133 p->frames_64_octets += read_mib(mp, 0x20);
1134 p->frames_65_to_127_octets += read_mib(mp, 0x24);
1135 p->frames_128_to_255_octets += read_mib(mp, 0x28);
1136 p->frames_256_to_511_octets += read_mib(mp, 0x2c);
1137 p->frames_512_to_1023_octets += read_mib(mp, 0x30);
1138 p->frames_1024_to_max_octets += read_mib(mp, 0x34);
1139 p->good_octets_sent += read_mib(mp, 0x38);
1140 p->good_octets_sent += (u64)read_mib(mp, 0x3c) << 32;
1141 p->good_frames_sent += read_mib(mp, 0x40);
1142 p->excessive_collision += read_mib(mp, 0x44);
1143 p->multicast_frames_sent += read_mib(mp, 0x48);
1144 p->broadcast_frames_sent += read_mib(mp, 0x4c);
1145 p->unrec_mac_control_received += read_mib(mp, 0x50);
1146 p->fc_sent += read_mib(mp, 0x54);
1147 p->good_fc_received += read_mib(mp, 0x58);
1148 p->bad_fc_received += read_mib(mp, 0x5c);
1149 p->undersize_received += read_mib(mp, 0x60);
1150 p->fragments_received += read_mib(mp, 0x64);
1151 p->oversize_received += read_mib(mp, 0x68);
1152 p->jabber_received += read_mib(mp, 0x6c);
1153 p->mac_receive_error += read_mib(mp, 0x70);
1154 p->bad_crc_event += read_mib(mp, 0x74);
1155 p->collision += read_mib(mp, 0x78);
1156 p->late_collision += read_mib(mp, 0x7c);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001157}
1158
1159
1160/* ethtool ******************************************************************/
1161struct mv643xx_stats {
1162 char stat_string[ETH_GSTRING_LEN];
1163 int sizeof_stat;
1164 int stat_offset;
1165};
1166
1167#define MV643XX_STAT(m) FIELD_SIZEOF(struct mv643xx_private, m), \
1168 offsetof(struct mv643xx_private, m)
1169
1170static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
1171 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
1172 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
1173 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
1174 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
1175 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
1176 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
1177 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
1178 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
1179 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
1180 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
1181 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
1182 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
1183 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
1184 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
1185 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
1186 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
1187 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
1188 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
1189 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
1190 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
1191 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
1192 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
1193 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
1194 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
1195 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
1196 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
1197 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
1198 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
1199 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
1200 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
1201 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
1202 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
1203 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
1204 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
1205 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
1206 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
1207 { "collision", MV643XX_STAT(mib_counters.collision) },
1208 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
1209};
1210
1211#define MV643XX_STATS_LEN ARRAY_SIZE(mv643xx_gstrings_stats)
1212
1213static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1214{
1215 struct mv643xx_private *mp = netdev_priv(dev);
1216 int err;
1217
1218 spin_lock_irq(&mp->lock);
1219 err = mii_ethtool_gset(&mp->mii, cmd);
1220 spin_unlock_irq(&mp->lock);
1221
1222 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
1223 cmd->supported &= ~SUPPORTED_1000baseT_Half;
1224 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
1225
1226 return err;
1227}
1228
1229static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1230{
1231 struct mv643xx_private *mp = netdev_priv(dev);
1232 int err;
1233
1234 spin_lock_irq(&mp->lock);
1235 err = mii_ethtool_sset(&mp->mii, cmd);
1236 spin_unlock_irq(&mp->lock);
1237
1238 return err;
1239}
1240
1241static void mv643xx_get_drvinfo(struct net_device *netdev,
1242 struct ethtool_drvinfo *drvinfo)
1243{
1244 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
1245 strncpy(drvinfo->version, mv643xx_driver_version, 32);
1246 strncpy(drvinfo->fw_version, "N/A", 32);
1247 strncpy(drvinfo->bus_info, "mv643xx", 32);
1248 drvinfo->n_stats = MV643XX_STATS_LEN;
1249}
1250
1251static int mv643xx_eth_nway_restart(struct net_device *dev)
1252{
1253 struct mv643xx_private *mp = netdev_priv(dev);
1254
1255 return mii_nway_restart(&mp->mii);
1256}
1257
1258static u32 mv643xx_eth_get_link(struct net_device *dev)
1259{
1260 struct mv643xx_private *mp = netdev_priv(dev);
1261
1262 return mii_link_ok(&mp->mii);
1263}
1264
1265static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
1266 uint8_t *data)
1267{
1268 int i;
1269
1270 switch(stringset) {
1271 case ETH_SS_STATS:
1272 for (i=0; i < MV643XX_STATS_LEN; i++) {
1273 memcpy(data + i * ETH_GSTRING_LEN,
1274 mv643xx_gstrings_stats[i].stat_string,
1275 ETH_GSTRING_LEN);
1276 }
1277 break;
1278 }
1279}
1280
1281static void mv643xx_get_ethtool_stats(struct net_device *netdev,
1282 struct ethtool_stats *stats, uint64_t *data)
1283{
1284 struct mv643xx_private *mp = netdev->priv;
1285 int i;
1286
1287 eth_update_mib_counters(mp);
1288
1289 for (i = 0; i < MV643XX_STATS_LEN; i++) {
1290 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
1291 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
1292 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
1293 }
1294}
1295
1296static int mv643xx_get_sset_count(struct net_device *netdev, int sset)
1297{
1298 switch (sset) {
1299 case ETH_SS_STATS:
1300 return MV643XX_STATS_LEN;
1301 default:
1302 return -EOPNOTSUPP;
1303 }
1304}
1305
1306static const struct ethtool_ops mv643xx_ethtool_ops = {
1307 .get_settings = mv643xx_get_settings,
1308 .set_settings = mv643xx_set_settings,
1309 .get_drvinfo = mv643xx_get_drvinfo,
1310 .get_link = mv643xx_eth_get_link,
1311 .set_sg = ethtool_op_set_sg,
1312 .get_sset_count = mv643xx_get_sset_count,
1313 .get_ethtool_stats = mv643xx_get_ethtool_stats,
1314 .get_strings = mv643xx_get_strings,
1315 .nway_reset = mv643xx_eth_nway_restart,
1316};
1317
1318
1319/* address handling *********************************************************/
1320/*
1321 * eth_port_uc_addr_get - Read the MAC address from the port's hw registers
1322 */
1323static void eth_port_uc_addr_get(struct mv643xx_private *mp,
1324 unsigned char *p_addr)
1325{
1326 unsigned int port_num = mp->port_num;
1327 unsigned int mac_h;
1328 unsigned int mac_l;
1329
1330 mac_h = rdl(mp, MAC_ADDR_HIGH(port_num));
1331 mac_l = rdl(mp, MAC_ADDR_LOW(port_num));
1332
1333 p_addr[0] = (mac_h >> 24) & 0xff;
1334 p_addr[1] = (mac_h >> 16) & 0xff;
1335 p_addr[2] = (mac_h >> 8) & 0xff;
1336 p_addr[3] = mac_h & 0xff;
1337 p_addr[4] = (mac_l >> 8) & 0xff;
1338 p_addr[5] = mac_l & 0xff;
1339}
1340
1341/*
1342 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
1343 *
1344 * DESCRIPTION:
1345 * Go through all the DA filter tables (Unicast, Special Multicast &
1346 * Other Multicast) and set each entry to 0.
1347 *
1348 * INPUT:
1349 * struct mv643xx_private *mp Ethernet Port.
1350 *
1351 * OUTPUT:
1352 * Multicast and Unicast packets are rejected.
1353 *
1354 * RETURN:
1355 * None.
1356 */
1357static void eth_port_init_mac_tables(struct mv643xx_private *mp)
1358{
1359 unsigned int port_num = mp->port_num;
1360 int table_index;
1361
1362 /* Clear DA filter unicast table (Ex_dFUT) */
1363 for (table_index = 0; table_index <= 0xC; table_index += 4)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001364 wrl(mp, UNICAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001365
1366 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1367 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001368 wrl(mp, SPECIAL_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001369 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001370 wrl(mp, OTHER_MCAST_TABLE(port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001371 }
1372}
1373
1374/*
1375 * The entries in each table are indexed by a hash of a packet's MAC
1376 * address. One bit in each entry determines whether the packet is
1377 * accepted. There are 4 entries (each 8 bits wide) in each register
1378 * of the table. The bits in each entry are defined as follows:
1379 * 0 Accept=1, Drop=0
1380 * 3-1 Queue (ETH_Q0=0)
1381 * 7-4 Reserved = 0;
1382 */
1383static void eth_port_set_filter_table_entry(struct mv643xx_private *mp,
1384 int table, unsigned char entry)
1385{
1386 unsigned int table_reg;
1387 unsigned int tbl_offset;
1388 unsigned int reg_offset;
1389
1390 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1391 reg_offset = entry % 4; /* Entry offset within the register */
1392
1393 /* Set "accepts frame bit" at specified table entry */
1394 table_reg = rdl(mp, table + tbl_offset);
1395 table_reg |= 0x01 << (8 * reg_offset);
1396 wrl(mp, table + tbl_offset, table_reg);
1397}
1398
1399/*
1400 * eth_port_uc_addr_set - Write a MAC address into the port's hw registers
1401 */
1402static void eth_port_uc_addr_set(struct mv643xx_private *mp,
1403 unsigned char *p_addr)
1404{
1405 unsigned int port_num = mp->port_num;
1406 unsigned int mac_h;
1407 unsigned int mac_l;
1408 int table;
1409
1410 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1411 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1412 (p_addr[3] << 0);
1413
1414 wrl(mp, MAC_ADDR_LOW(port_num), mac_l);
1415 wrl(mp, MAC_ADDR_HIGH(port_num), mac_h);
1416
1417 /* Accept frames with this address */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001418 table = UNICAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001419 eth_port_set_filter_table_entry(mp, table, p_addr[5] & 0x0f);
1420}
1421
1422/*
1423 * mv643xx_eth_update_mac_address
1424 *
1425 * Update the MAC address of the port in the address table
1426 *
1427 * Input : pointer to ethernet interface network device structure
1428 * Output : N/A
1429 */
1430static void mv643xx_eth_update_mac_address(struct net_device *dev)
1431{
1432 struct mv643xx_private *mp = netdev_priv(dev);
1433
1434 eth_port_init_mac_tables(mp);
1435 eth_port_uc_addr_set(mp, dev->dev_addr);
1436}
1437
1438/*
1439 * mv643xx_eth_set_mac_address
1440 *
1441 * Change the interface's mac address.
1442 * No special hardware thing should be done because interface is always
1443 * put in promiscuous mode.
1444 *
1445 * Input : pointer to ethernet interface network device structure and
1446 * a pointer to the designated entry to be added to the cache.
1447 * Output : zero upon success, negative upon failure
1448 */
1449static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
1450{
1451 int i;
1452
1453 for (i = 0; i < 6; i++)
1454 /* +2 is for the offset of the HW addr type */
1455 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
1456 mv643xx_eth_update_mac_address(dev);
1457 return 0;
1458}
1459
1460/*
1461 * eth_port_mc_addr - Multicast address settings.
1462 *
1463 * The MV device supports multicast using two tables:
1464 * 1) Special Multicast Table for MAC addresses of the form
1465 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1466 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1467 * Table entries in the DA-Filter table.
1468 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1469 * is used as an index to the Other Multicast Table entries in the
1470 * DA-Filter table. This function calculates the CRC-8bit value.
1471 * In either case, eth_port_set_filter_table_entry() is then called
1472 * to set to set the actual table entry.
1473 */
1474static void eth_port_mc_addr(struct mv643xx_private *mp, unsigned char *p_addr)
1475{
1476 unsigned int port_num = mp->port_num;
1477 unsigned int mac_h;
1478 unsigned int mac_l;
1479 unsigned char crc_result = 0;
1480 int table;
1481 int mac_array[48];
1482 int crc[8];
1483 int i;
1484
1485 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1486 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001487 table = SPECIAL_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001488 eth_port_set_filter_table_entry(mp, table, p_addr[5]);
1489 return;
1490 }
1491
1492 /* Calculate CRC-8 out of the given address */
1493 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1494 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1495 (p_addr[4] << 8) | (p_addr[5] << 0);
1496
1497 for (i = 0; i < 32; i++)
1498 mac_array[i] = (mac_l >> i) & 0x1;
1499 for (i = 32; i < 48; i++)
1500 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1501
1502 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1503 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1504 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1505 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1506 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1507
1508 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1509 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1510 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1511 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1512 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1513 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1514 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1515
1516 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1517 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1518 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1519 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1520 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1521 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1522
1523 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1524 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1525 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1526 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1527 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1528 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1529
1530 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1531 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1532 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1533 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1534 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1535 mac_array[3] ^ mac_array[2];
1536
1537 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1538 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1539 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1540 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1541 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1542 mac_array[4] ^ mac_array[3];
1543
1544 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1545 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1546 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1547 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1548 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
1549 mac_array[4];
1550
1551 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
1552 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
1553 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
1554 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
1555 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
1556
1557 for (i = 0; i < 8; i++)
1558 crc_result = crc_result | (crc[i] << i);
1559
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001560 table = OTHER_MCAST_TABLE(port_num);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001561 eth_port_set_filter_table_entry(mp, table, crc_result);
1562}
1563
1564/*
1565 * Set the entire multicast list based on dev->mc_list.
1566 */
1567static void eth_port_set_multicast_list(struct net_device *dev)
1568{
1569
1570 struct dev_mc_list *mc_list;
1571 int i;
1572 int table_index;
1573 struct mv643xx_private *mp = netdev_priv(dev);
1574 unsigned int eth_port_num = mp->port_num;
1575
1576 /* If the device is in promiscuous mode or in all multicast mode,
1577 * we will fully populate both multicast tables with accept.
1578 * This is guaranteed to yield a match on all multicast addresses...
1579 */
1580 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
1581 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1582 /* Set all entries in DA filter special multicast
1583 * table (Ex_dFSMT)
1584 * Set for ETH_Q0 for now
1585 * Bits
1586 * 0 Accept=1, Drop=0
1587 * 3-1 Queue ETH_Q0=0
1588 * 7-4 Reserved = 0;
1589 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001590 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001591
1592 /* Set all entries in DA filter other multicast
1593 * table (Ex_dFOMT)
1594 * Set for ETH_Q0 for now
1595 * Bits
1596 * 0 Accept=1, Drop=0
1597 * 3-1 Queue ETH_Q0=0
1598 * 7-4 Reserved = 0;
1599 */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001600 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0x01010101);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001601 }
1602 return;
1603 }
1604
1605 /* We will clear out multicast tables every time we get the list.
1606 * Then add the entire new list...
1607 */
1608 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
1609 /* Clear DA filter special multicast table (Ex_dFSMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001610 wrl(mp, SPECIAL_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001611
1612 /* Clear DA filter other multicast table (Ex_dFOMT) */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001613 wrl(mp, OTHER_MCAST_TABLE(eth_port_num) + table_index, 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001614 }
1615
1616 /* Get pointer to net_device multicast list and add each one... */
1617 for (i = 0, mc_list = dev->mc_list;
1618 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
1619 i++, mc_list = mc_list->next)
1620 if (mc_list->dmi_addrlen == 6)
1621 eth_port_mc_addr(mp, mc_list->dmi_addr);
1622}
1623
1624/*
1625 * mv643xx_eth_set_rx_mode
1626 *
1627 * Change from promiscuos to regular rx mode
1628 *
1629 * Input : pointer to ethernet interface network device structure
1630 * Output : N/A
1631 */
1632static void mv643xx_eth_set_rx_mode(struct net_device *dev)
1633{
1634 struct mv643xx_private *mp = netdev_priv(dev);
1635 u32 config_reg;
1636
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001637 config_reg = rdl(mp, PORT_CONFIG(mp->port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001638 if (dev->flags & IFF_PROMISC)
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001639 config_reg |= UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001640 else
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02001641 config_reg &= ~UNICAST_PROMISCUOUS_MODE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001642 wrl(mp, PORT_CONFIG(mp->port_num), config_reg);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001643
1644 eth_port_set_multicast_list(dev);
1645}
1646
1647
1648/* rx/tx queue initialisation ***********************************************/
1649/*
1650 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
1651 *
1652 * DESCRIPTION:
1653 * This function prepares a Rx chained list of descriptors and packet
1654 * buffers in a form of a ring. The routine must be called after port
1655 * initialization routine and before port start routine.
1656 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1657 * devices in the system (i.e. DRAM). This function uses the ethernet
1658 * struct 'virtual to physical' routine (set by the user) to set the ring
1659 * with physical addresses.
1660 *
1661 * INPUT:
1662 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1663 *
1664 * OUTPUT:
1665 * The routine updates the Ethernet port control struct with information
1666 * regarding the Rx descriptors and buffers.
1667 *
1668 * RETURN:
1669 * None.
1670 */
1671static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
1672{
1673 volatile struct eth_rx_desc *p_rx_desc;
1674 int rx_desc_num = mp->rx_ring_size;
1675 int i;
1676
1677 /* initialize the next_desc_ptr links in the Rx descriptors ring */
1678 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
1679 for (i = 0; i < rx_desc_num; i++) {
1680 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
1681 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
1682 }
1683
1684 /* Save Rx desc pointer to driver struct. */
1685 mp->rx_curr_desc_q = 0;
1686 mp->rx_used_desc_q = 0;
1687
1688 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
1689}
1690
1691static void mv643xx_eth_free_rx_rings(struct net_device *dev)
1692{
1693 struct mv643xx_private *mp = netdev_priv(dev);
1694 int curr;
1695
1696 /* Stop RX Queues */
1697 mv643xx_eth_port_disable_rx(mp);
1698
1699 /* Free preallocated skb's on RX rings */
1700 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
1701 if (mp->rx_skb[curr]) {
1702 dev_kfree_skb(mp->rx_skb[curr]);
1703 mp->rx_desc_count--;
1704 }
1705 }
1706
1707 if (mp->rx_desc_count)
1708 printk(KERN_ERR
1709 "%s: Error in freeing Rx Ring. %d skb's still"
1710 " stuck in RX Ring - ignoring them\n", dev->name,
1711 mp->rx_desc_count);
1712 /* Free RX ring */
1713 if (mp->rx_sram_size)
1714 iounmap(mp->p_rx_desc_area);
1715 else
1716 dma_free_coherent(NULL, mp->rx_desc_area_size,
1717 mp->p_rx_desc_area, mp->rx_desc_dma);
1718}
1719
1720/*
1721 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
1722 *
1723 * DESCRIPTION:
1724 * This function prepares a Tx chained list of descriptors and packet
1725 * buffers in a form of a ring. The routine must be called after port
1726 * initialization routine and before port start routine.
1727 * The Ethernet SDMA engine uses CPU bus addresses to access the various
1728 * devices in the system (i.e. DRAM). This function uses the ethernet
1729 * struct 'virtual to physical' routine (set by the user) to set the ring
1730 * with physical addresses.
1731 *
1732 * INPUT:
1733 * struct mv643xx_private *mp Ethernet Port Control srtuct.
1734 *
1735 * OUTPUT:
1736 * The routine updates the Ethernet port control struct with information
1737 * regarding the Tx descriptors and buffers.
1738 *
1739 * RETURN:
1740 * None.
1741 */
1742static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
1743{
1744 int tx_desc_num = mp->tx_ring_size;
1745 struct eth_tx_desc *p_tx_desc;
1746 int i;
1747
1748 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
1749 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
1750 for (i = 0; i < tx_desc_num; i++) {
1751 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
1752 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
1753 }
1754
1755 mp->tx_curr_desc_q = 0;
1756 mp->tx_used_desc_q = 0;
1757
1758 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
1759}
1760
1761/**
1762 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
1763 *
1764 * If force is non-zero, frees uncompleted descriptors as well
1765 */
1766static int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
1767{
1768 struct mv643xx_private *mp = netdev_priv(dev);
1769 struct eth_tx_desc *desc;
1770 u32 cmd_sts;
1771 struct sk_buff *skb;
1772 unsigned long flags;
1773 int tx_index;
1774 dma_addr_t addr;
1775 int count;
1776 int released = 0;
1777
1778 while (mp->tx_desc_count > 0) {
1779 spin_lock_irqsave(&mp->lock, flags);
1780
1781 /* tx_desc_count might have changed before acquiring the lock */
1782 if (mp->tx_desc_count <= 0) {
1783 spin_unlock_irqrestore(&mp->lock, flags);
1784 return released;
1785 }
1786
1787 tx_index = mp->tx_used_desc_q;
1788 desc = &mp->p_tx_desc_area[tx_index];
1789 cmd_sts = desc->cmd_sts;
1790
1791 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
1792 spin_unlock_irqrestore(&mp->lock, flags);
1793 return released;
1794 }
1795
1796 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
1797 mp->tx_desc_count--;
1798
1799 addr = desc->buf_ptr;
1800 count = desc->byte_cnt;
1801 skb = mp->tx_skb[tx_index];
1802 if (skb)
1803 mp->tx_skb[tx_index] = NULL;
1804
1805 if (cmd_sts & ETH_ERROR_SUMMARY) {
1806 printk("%s: Error in TX\n", dev->name);
1807 dev->stats.tx_errors++;
1808 }
1809
1810 spin_unlock_irqrestore(&mp->lock, flags);
1811
1812 if (cmd_sts & ETH_TX_FIRST_DESC)
1813 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
1814 else
1815 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
1816
1817 if (skb)
1818 dev_kfree_skb_irq(skb);
1819
1820 released = 1;
1821 }
1822
1823 return released;
1824}
1825
1826static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
1827{
1828 struct mv643xx_private *mp = netdev_priv(dev);
1829
1830 if (mv643xx_eth_free_tx_descs(dev, 0) &&
1831 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
1832 netif_wake_queue(dev);
1833}
1834
1835static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
1836{
1837 mv643xx_eth_free_tx_descs(dev, 1);
1838}
1839
1840static void mv643xx_eth_free_tx_rings(struct net_device *dev)
1841{
1842 struct mv643xx_private *mp = netdev_priv(dev);
1843
1844 /* Stop Tx Queues */
1845 mv643xx_eth_port_disable_tx(mp);
1846
1847 /* Free outstanding skb's on TX ring */
1848 mv643xx_eth_free_all_tx_descs(dev);
1849
1850 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
1851
1852 /* Free TX ring */
1853 if (mp->tx_sram_size)
1854 iounmap(mp->p_tx_desc_area);
1855 else
1856 dma_free_coherent(NULL, mp->tx_desc_area_size,
1857 mp->p_tx_desc_area, mp->tx_desc_dma);
1858}
1859
1860
1861/* netdev ops and related ***************************************************/
1862static void eth_port_reset(struct mv643xx_private *mp);
1863
1864/* Set the mv643xx port configuration register for the speed/duplex mode. */
1865static void mv643xx_eth_update_pscr(struct net_device *dev,
1866 struct ethtool_cmd *ecmd)
1867{
1868 struct mv643xx_private *mp = netdev_priv(dev);
1869 int port_num = mp->port_num;
1870 u32 o_pscr, n_pscr;
1871 unsigned int queues;
1872
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001873 o_pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001874 n_pscr = o_pscr;
1875
1876 /* clear speed, duplex and rx buffer size fields */
1877 n_pscr &= ~(SET_MII_SPEED_TO_100 |
1878 SET_GMII_SPEED_TO_1000 |
1879 SET_FULL_DUPLEX_MODE |
1880 MAX_RX_PACKET_MASK);
1881
1882 if (ecmd->duplex == DUPLEX_FULL)
1883 n_pscr |= SET_FULL_DUPLEX_MODE;
1884
1885 if (ecmd->speed == SPEED_1000)
1886 n_pscr |= SET_GMII_SPEED_TO_1000 |
1887 MAX_RX_PACKET_9700BYTE;
1888 else {
1889 if (ecmd->speed == SPEED_100)
1890 n_pscr |= SET_MII_SPEED_TO_100;
1891 n_pscr |= MAX_RX_PACKET_1522BYTE;
1892 }
1893
1894 if (n_pscr != o_pscr) {
1895 if ((o_pscr & SERIAL_PORT_ENABLE) == 0)
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001896 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001897 else {
1898 queues = mv643xx_eth_port_disable_tx(mp);
1899
1900 o_pscr &= ~SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001901 wrl(mp, PORT_SERIAL_CONTROL(port_num), o_pscr);
1902 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
1903 wrl(mp, PORT_SERIAL_CONTROL(port_num), n_pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001904 if (queues)
1905 mv643xx_eth_port_enable_tx(mp, queues);
1906 }
1907 }
1908}
1909
1910/*
1911 * mv643xx_eth_int_handler
1912 *
1913 * Main interrupt handler for the gigbit ethernet ports
1914 *
1915 * Input : irq - irq number (not used)
1916 * dev_id - a pointer to the required interface's data structure
1917 * regs - not used
1918 * Output : N/A
1919 */
1920
1921static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id)
1922{
1923 struct net_device *dev = (struct net_device *)dev_id;
1924 struct mv643xx_private *mp = netdev_priv(dev);
1925 u32 eth_int_cause, eth_int_cause_ext = 0;
1926 unsigned int port_num = mp->port_num;
1927
1928 /* Read interrupt cause registers */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001929 eth_int_cause = rdl(mp, INT_CAUSE(port_num)) & (INT_RX | INT_EXT);
1930 if (eth_int_cause & INT_EXT) {
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001931 eth_int_cause_ext = rdl(mp, INT_CAUSE_EXT(port_num))
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001932 & (INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001933 wrl(mp, INT_CAUSE_EXT(port_num), ~eth_int_cause_ext);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001934 }
1935
1936 /* PHY status changed */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001937 if (eth_int_cause_ext & (INT_EXT_LINK | INT_EXT_PHY)) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001938 struct ethtool_cmd cmd;
1939
1940 if (mii_link_ok(&mp->mii)) {
1941 mii_ethtool_gset(&mp->mii, &cmd);
1942 mv643xx_eth_update_pscr(dev, &cmd);
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001943 mv643xx_eth_port_enable_tx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001944 if (!netif_carrier_ok(dev)) {
1945 netif_carrier_on(dev);
1946 if (mp->tx_ring_size - mp->tx_desc_count >=
1947 MAX_DESCS_PER_SKB)
1948 netif_wake_queue(dev);
1949 }
1950 } else if (netif_carrier_ok(dev)) {
1951 netif_stop_queue(dev);
1952 netif_carrier_off(dev);
1953 }
1954 }
1955
1956#ifdef MV643XX_NAPI
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001957 if (eth_int_cause & INT_RX) {
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001958 /* schedule the NAPI poll routine to maintain port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001959 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001960
1961 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02001962 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001963
1964 netif_rx_schedule(dev, &mp->napi);
1965 }
1966#else
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001967 if (eth_int_cause & INT_RX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001968 mv643xx_eth_receive_queue(dev, INT_MAX);
1969#endif
Lennert Buytenhek073a3452008-06-01 02:00:31 +02001970 if (eth_int_cause_ext & INT_EXT_TX)
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02001971 mv643xx_eth_free_completed_tx_descs(dev);
1972
1973 /*
1974 * If no real interrupt occured, exit.
1975 * This can happen when using gigE interrupt coalescing mechanism.
1976 */
1977 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
1978 return IRQ_NONE;
1979
1980 return IRQ_HANDLED;
1981}
1982
1983/*
1984 * ethernet_phy_reset - Reset Ethernet port PHY.
1985 *
1986 * DESCRIPTION:
1987 * This routine utilizes the SMI interface to reset the ethernet port PHY.
1988 *
1989 * INPUT:
1990 * struct mv643xx_private *mp Ethernet Port.
1991 *
1992 * OUTPUT:
1993 * The PHY is reset.
1994 *
1995 * RETURN:
1996 * None.
1997 *
1998 */
1999static void ethernet_phy_reset(struct mv643xx_private *mp)
2000{
2001 unsigned int phy_reg_data;
2002
2003 /* Reset the PHY */
2004 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2005 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2006 eth_port_write_smi_reg(mp, 0, phy_reg_data);
2007
2008 /* wait for PHY to come out of reset */
2009 do {
2010 udelay(1);
2011 eth_port_read_smi_reg(mp, 0, &phy_reg_data);
2012 } while (phy_reg_data & 0x8000);
2013}
2014
2015/*
2016 * eth_port_start - Start the Ethernet port activity.
2017 *
2018 * DESCRIPTION:
2019 * This routine prepares the Ethernet port for Rx and Tx activity:
2020 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
2021 * has been initialized a descriptor's ring (using
2022 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
2023 * 2. Initialize and enable the Ethernet configuration port by writing to
2024 * the port's configuration and command registers.
2025 * 3. Initialize and enable the SDMA by writing to the SDMA's
2026 * configuration and command registers. After completing these steps,
2027 * the ethernet port SDMA can starts to perform Rx and Tx activities.
2028 *
2029 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
2030 * to calling this function (use ether_init_tx_desc_ring for Tx queues
2031 * and ether_init_rx_desc_ring for Rx queues).
2032 *
2033 * INPUT:
2034 * dev - a pointer to the required interface
2035 *
2036 * OUTPUT:
2037 * Ethernet port is ready to receive and transmit.
2038 *
2039 * RETURN:
2040 * None.
2041 */
2042static void eth_port_start(struct net_device *dev)
2043{
2044 struct mv643xx_private *mp = netdev_priv(dev);
2045 unsigned int port_num = mp->port_num;
2046 int tx_curr_desc, rx_curr_desc;
2047 u32 pscr;
2048 struct ethtool_cmd ethtool_cmd;
2049
2050 /* Assignment of Tx CTRP of given queue */
2051 tx_curr_desc = mp->tx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002052 wrl(mp, TXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002053 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
2054
2055 /* Assignment of Rx CRDP of given queue */
2056 rx_curr_desc = mp->rx_curr_desc_q;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002057 wrl(mp, RXQ_CURRENT_DESC_PTR(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002058 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
2059
2060 /* Add the assigned Ethernet address to the port's address table */
2061 eth_port_uc_addr_set(mp, dev->dev_addr);
2062
Lennert Buytenhekd9a073e2008-06-01 01:22:06 +02002063 /*
2064 * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast
2065 * frames to RX queue #0.
2066 */
2067 wrl(mp, PORT_CONFIG(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002068
Lennert Buytenhek376489a2008-06-01 01:17:44 +02002069 /*
2070 * Treat BPDUs as normal multicasts, and disable partition mode.
2071 */
2072 wrl(mp, PORT_CONFIG_EXT(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002073
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002074 pscr = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002075
2076 pscr &= ~(SERIAL_PORT_ENABLE | FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002077 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002078
2079 pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
2080 DISABLE_AUTO_NEG_SPEED_GMII |
Lennert Buytenhek2beff772008-06-01 01:22:37 +02002081 DISABLE_AUTO_NEG_FOR_DUPLEX |
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002082 DO_NOT_FORCE_LINK_FAIL |
2083 SERIAL_PORT_CONTROL_RESERVED;
2084
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002085 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002086
2087 pscr |= SERIAL_PORT_ENABLE;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002088 wrl(mp, PORT_SERIAL_CONTROL(port_num), pscr);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002089
2090 /* Assign port SDMA configuration */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002091 wrl(mp, SDMA_CONFIG(port_num), PORT_SDMA_CONFIG_DEFAULT_VALUE);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002092
2093 /* Enable port Rx. */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002094 mv643xx_eth_port_enable_rx(mp, 1);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002095
2096 /* Disable port bandwidth limits by clearing MTU register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002097 wrl(mp, TX_BW_MTU(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002098
2099 /* save phy settings across reset */
2100 mv643xx_get_settings(dev, &ethtool_cmd);
2101 ethernet_phy_reset(mp);
2102 mv643xx_set_settings(dev, &ethtool_cmd);
2103}
2104
2105#ifdef MV643XX_COAL
2106
2107/*
2108 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
2109 *
2110 * DESCRIPTION:
2111 * This routine sets the RX coalescing interrupt mechanism parameter.
2112 * This parameter is a timeout counter, that counts in 64 t_clk
2113 * chunks ; that when timeout event occurs a maskable interrupt
2114 * occurs.
2115 * The parameter is calculated using the tClk of the MV-643xx chip
2116 * , and the required delay of the interrupt in usec.
2117 *
2118 * INPUT:
2119 * struct mv643xx_private *mp Ethernet port
2120 * unsigned int delay Delay in usec
2121 *
2122 * OUTPUT:
2123 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2124 *
2125 * RETURN:
2126 * The interrupt coalescing value set in the gigE port.
2127 *
2128 */
2129static unsigned int eth_port_set_rx_coal(struct mv643xx_private *mp,
2130 unsigned int delay)
2131{
2132 unsigned int port_num = mp->port_num;
2133 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2134
2135 /* Set RX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002136 wrl(mp, SDMA_CONFIG(port_num),
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002137 ((coal & 0x3fff) << 8) |
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002138 (rdl(mp, SDMA_CONFIG(port_num))
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002139 & 0xffc000ff));
2140
2141 return coal;
2142}
2143#endif
2144
2145/*
2146 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
2147 *
2148 * DESCRIPTION:
2149 * This routine sets the TX coalescing interrupt mechanism parameter.
2150 * This parameter is a timeout counter, that counts in 64 t_clk
2151 * chunks ; that when timeout event occurs a maskable interrupt
2152 * occurs.
2153 * The parameter is calculated using the t_cLK frequency of the
2154 * MV-643xx chip and the required delay in the interrupt in uSec
2155 *
2156 * INPUT:
2157 * struct mv643xx_private *mp Ethernet port
2158 * unsigned int delay Delay in uSeconds
2159 *
2160 * OUTPUT:
2161 * Interrupt coalescing mechanism value is set in MV-643xx chip.
2162 *
2163 * RETURN:
2164 * The interrupt coalescing value set in the gigE port.
2165 *
2166 */
2167static unsigned int eth_port_set_tx_coal(struct mv643xx_private *mp,
2168 unsigned int delay)
2169{
2170 unsigned int coal = ((mp->shared->t_clk / 1000000) * delay) / 64;
2171
2172 /* Set TX Coalescing mechanism */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002173 wrl(mp, TX_FIFO_URGENT_THRESHOLD(mp->port_num), coal << 4);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002174
2175 return coal;
2176}
2177
2178/*
2179 * eth_port_init - Initialize the Ethernet port driver
2180 *
2181 * DESCRIPTION:
2182 * This function prepares the ethernet port to start its activity:
2183 * 1) Completes the ethernet port driver struct initialization toward port
2184 * start routine.
2185 * 2) Resets the device to a quiescent state in case of warm reboot.
2186 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
2187 * 4) Clean MAC tables. The reset status of those tables is unknown.
2188 * 5) Set PHY address.
2189 * Note: Call this routine prior to eth_port_start routine and after
2190 * setting user values in the user fields of Ethernet port control
2191 * struct.
2192 *
2193 * INPUT:
2194 * struct mv643xx_private *mp Ethernet port control struct
2195 *
2196 * OUTPUT:
2197 * See description.
2198 *
2199 * RETURN:
2200 * None.
2201 */
2202static void eth_port_init(struct mv643xx_private *mp)
2203{
2204 mp->rx_resource_err = 0;
2205
2206 eth_port_reset(mp);
2207
2208 eth_port_init_mac_tables(mp);
2209}
2210
2211/*
2212 * mv643xx_eth_open
2213 *
2214 * This function is called when openning the network device. The function
2215 * should initialize all the hardware, initialize cyclic Rx/Tx
2216 * descriptors chain and buffers and allocate an IRQ to the network
2217 * device.
2218 *
2219 * Input : a pointer to the network device structure
2220 *
2221 * Output : zero of success , nonzero if fails.
2222 */
2223
2224static int mv643xx_eth_open(struct net_device *dev)
2225{
2226 struct mv643xx_private *mp = netdev_priv(dev);
2227 unsigned int port_num = mp->port_num;
2228 unsigned int size;
2229 int err;
2230
2231 /* Clear any pending ethernet port interrupts */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002232 wrl(mp, INT_CAUSE(port_num), 0);
2233 wrl(mp, INT_CAUSE_EXT(port_num), 0);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002234 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002235 rdl(mp, INT_CAUSE_EXT(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002236
2237 err = request_irq(dev->irq, mv643xx_eth_int_handler,
2238 IRQF_SHARED | IRQF_SAMPLE_RANDOM, dev->name, dev);
2239 if (err) {
2240 printk(KERN_ERR "%s: Can not assign IRQ\n", dev->name);
2241 return -EAGAIN;
2242 }
2243
2244 eth_port_init(mp);
2245
2246 memset(&mp->timeout, 0, sizeof(struct timer_list));
2247 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
2248 mp->timeout.data = (unsigned long)dev;
2249
2250 /* Allocate RX and TX skb rings */
2251 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
2252 GFP_KERNEL);
2253 if (!mp->rx_skb) {
2254 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
2255 err = -ENOMEM;
2256 goto out_free_irq;
2257 }
2258 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
2259 GFP_KERNEL);
2260 if (!mp->tx_skb) {
2261 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
2262 err = -ENOMEM;
2263 goto out_free_rx_skb;
2264 }
2265
2266 /* Allocate TX ring */
2267 mp->tx_desc_count = 0;
2268 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
2269 mp->tx_desc_area_size = size;
2270
2271 if (mp->tx_sram_size) {
2272 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
2273 mp->tx_sram_size);
2274 mp->tx_desc_dma = mp->tx_sram_addr;
2275 } else
2276 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
2277 &mp->tx_desc_dma,
2278 GFP_KERNEL);
2279
2280 if (!mp->p_tx_desc_area) {
2281 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
2282 dev->name, size);
2283 err = -ENOMEM;
2284 goto out_free_tx_skb;
2285 }
2286 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
2287 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
2288
2289 ether_init_tx_desc_ring(mp);
2290
2291 /* Allocate RX ring */
2292 mp->rx_desc_count = 0;
2293 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
2294 mp->rx_desc_area_size = size;
2295
2296 if (mp->rx_sram_size) {
2297 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
2298 mp->rx_sram_size);
2299 mp->rx_desc_dma = mp->rx_sram_addr;
2300 } else
2301 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
2302 &mp->rx_desc_dma,
2303 GFP_KERNEL);
2304
2305 if (!mp->p_rx_desc_area) {
2306 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
2307 dev->name, size);
2308 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
2309 dev->name);
2310 if (mp->rx_sram_size)
2311 iounmap(mp->p_tx_desc_area);
2312 else
2313 dma_free_coherent(NULL, mp->tx_desc_area_size,
2314 mp->p_tx_desc_area, mp->tx_desc_dma);
2315 err = -ENOMEM;
2316 goto out_free_tx_skb;
2317 }
2318 memset((void *)mp->p_rx_desc_area, 0, size);
2319
2320 ether_init_rx_desc_ring(mp);
2321
2322 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
2323
2324#ifdef MV643XX_NAPI
2325 napi_enable(&mp->napi);
2326#endif
2327
2328 eth_port_start(dev);
2329
2330 /* Interrupt Coalescing */
2331
2332#ifdef MV643XX_COAL
2333 mp->rx_int_coal =
2334 eth_port_set_rx_coal(mp, MV643XX_RX_COAL);
2335#endif
2336
2337 mp->tx_int_coal =
2338 eth_port_set_tx_coal(mp, MV643XX_TX_COAL);
2339
2340 /* Unmask phy and link status changes interrupts */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002341 wrl(mp, INT_MASK_EXT(port_num), INT_EXT_LINK | INT_EXT_PHY | INT_EXT_TX);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002342
2343 /* Unmask RX buffer and TX end interrupt */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002344 wrl(mp, INT_MASK(port_num), INT_RX | INT_EXT);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002345
2346 return 0;
2347
2348out_free_tx_skb:
2349 kfree(mp->tx_skb);
2350out_free_rx_skb:
2351 kfree(mp->rx_skb);
2352out_free_irq:
2353 free_irq(dev->irq, dev);
2354
2355 return err;
2356}
2357
2358/*
2359 * eth_port_reset - Reset Ethernet port
2360 *
2361 * DESCRIPTION:
2362 * This routine resets the chip by aborting any SDMA engine activity and
2363 * clearing the MIB counters. The Receiver and the Transmit unit are in
2364 * idle state after this command is performed and the port is disabled.
2365 *
2366 * INPUT:
2367 * struct mv643xx_private *mp Ethernet Port.
2368 *
2369 * OUTPUT:
2370 * Channel activity is halted.
2371 *
2372 * RETURN:
2373 * None.
2374 *
2375 */
2376static void eth_port_reset(struct mv643xx_private *mp)
2377{
2378 unsigned int port_num = mp->port_num;
2379 unsigned int reg_data;
2380
2381 mv643xx_eth_port_disable_tx(mp);
2382 mv643xx_eth_port_disable_rx(mp);
2383
2384 /* Clear all MIB counters */
2385 eth_clear_mib_counters(mp);
2386
2387 /* Reset the Enable bit in the Configuration Register */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002388 reg_data = rdl(mp, PORT_SERIAL_CONTROL(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002389 reg_data &= ~(SERIAL_PORT_ENABLE |
2390 DO_NOT_FORCE_LINK_FAIL |
2391 FORCE_LINK_PASS);
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002392 wrl(mp, PORT_SERIAL_CONTROL(port_num), reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002393}
2394
2395/*
2396 * mv643xx_eth_stop
2397 *
2398 * This function is used when closing the network device.
2399 * It updates the hardware,
2400 * release all memory that holds buffers and descriptors and release the IRQ.
2401 * Input : a pointer to the device structure
2402 * Output : zero if success , nonzero if fails
2403 */
2404
2405static int mv643xx_eth_stop(struct net_device *dev)
2406{
2407 struct mv643xx_private *mp = netdev_priv(dev);
2408 unsigned int port_num = mp->port_num;
2409
2410 /* Mask all interrupts on ethernet port */
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002411 wrl(mp, INT_MASK(port_num), 0x00000000);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002412 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002413 rdl(mp, INT_MASK(port_num));
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002414
2415#ifdef MV643XX_NAPI
2416 napi_disable(&mp->napi);
2417#endif
2418 netif_carrier_off(dev);
2419 netif_stop_queue(dev);
2420
2421 eth_port_reset(mp);
2422
2423 mv643xx_eth_free_tx_rings(dev);
2424 mv643xx_eth_free_rx_rings(dev);
2425
2426 free_irq(dev->irq, dev);
2427
2428 return 0;
2429}
2430
2431static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2432{
2433 struct mv643xx_private *mp = netdev_priv(dev);
2434
2435 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2436}
2437
2438/*
2439 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
2440 *
2441 * Input : pointer to ethernet interface network device structure
2442 * new mtu size
2443 * Output : 0 upon success, -EINVAL upon failure
2444 */
2445static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
2446{
2447 if ((new_mtu > 9500) || (new_mtu < 64))
2448 return -EINVAL;
2449
2450 dev->mtu = new_mtu;
2451 if (!netif_running(dev))
2452 return 0;
2453
2454 /*
2455 * Stop and then re-open the interface. This will allocate RX
2456 * skbs of the new MTU.
2457 * There is a possible danger that the open will not succeed,
2458 * due to memory being full, which might fail the open function.
2459 */
2460 mv643xx_eth_stop(dev);
2461 if (mv643xx_eth_open(dev)) {
2462 printk(KERN_ERR "%s: Fatal error on opening device\n",
2463 dev->name);
2464 }
2465
2466 return 0;
2467}
2468
2469/*
2470 * mv643xx_eth_tx_timeout_task
2471 *
2472 * Actual routine to reset the adapter when a timeout on Tx has occurred
2473 */
2474static void mv643xx_eth_tx_timeout_task(struct work_struct *ugly)
2475{
2476 struct mv643xx_private *mp = container_of(ugly, struct mv643xx_private,
2477 tx_timeout_task);
2478 struct net_device *dev = mp->dev;
2479
2480 if (!netif_running(dev))
2481 return;
2482
2483 netif_stop_queue(dev);
2484
2485 eth_port_reset(mp);
2486 eth_port_start(dev);
2487
2488 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
2489 netif_wake_queue(dev);
2490}
2491
2492/*
2493 * mv643xx_eth_tx_timeout
2494 *
2495 * Called upon a timeout on transmitting a packet
2496 *
2497 * Input : pointer to ethernet interface network device structure.
2498 * Output : N/A
2499 */
2500static void mv643xx_eth_tx_timeout(struct net_device *dev)
2501{
2502 struct mv643xx_private *mp = netdev_priv(dev);
2503
2504 printk(KERN_INFO "%s: TX timeout ", dev->name);
2505
2506 /* Do the reset outside of interrupt context */
2507 schedule_work(&mp->tx_timeout_task);
2508}
2509
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002510#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002511static void mv643xx_netpoll(struct net_device *netdev)
2512{
2513 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002514 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002515
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002516 wrl(mp, INT_MASK(port_num), 0x00000000);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002517 /* wait for previous write to complete */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002518 rdl(mp, INT_MASK(port_num));
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002519
Al Viro9da3b1a2006-10-08 15:00:44 +01002520 mv643xx_eth_int_handler(netdev->irq, netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07002521
Lennert Buytenhek073a3452008-06-01 02:00:31 +02002522 wrl(mp, INT_MASK(port_num), INT_RX | INT_CAUSE_EXT);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002523}
2524#endif
2525
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002526/*
2527 * Wrappers for MII support library.
2528 */
2529static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2530{
2531 struct mv643xx_private *mp = netdev_priv(dev);
2532 int val;
2533
2534 eth_port_read_smi_reg(mp, location, &val);
2535 return val;
2536}
2537
2538static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2539{
2540 struct mv643xx_private *mp = netdev_priv(dev);
2541 eth_port_write_smi_reg(mp, location, val);
2542}
2543
2544
2545/* platform glue ************************************************************/
2546static void mv643xx_eth_conf_mbus_windows(struct mv643xx_shared_private *msp,
2547 struct mbus_dram_target_info *dram)
2548{
2549 void __iomem *base = msp->eth_base;
2550 u32 win_enable;
2551 u32 win_protect;
2552 int i;
2553
2554 for (i = 0; i < 6; i++) {
2555 writel(0, base + WINDOW_BASE(i));
2556 writel(0, base + WINDOW_SIZE(i));
2557 if (i < 4)
2558 writel(0, base + WINDOW_REMAP_HIGH(i));
2559 }
2560
2561 win_enable = 0x3f;
2562 win_protect = 0;
2563
2564 for (i = 0; i < dram->num_cs; i++) {
2565 struct mbus_dram_window *cs = dram->cs + i;
2566
2567 writel((cs->base & 0xffff0000) |
2568 (cs->mbus_attr << 8) |
2569 dram->mbus_dram_target_id, base + WINDOW_BASE(i));
2570 writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i));
2571
2572 win_enable &= ~(1 << i);
2573 win_protect |= 3 << (2 * i);
2574 }
2575
2576 writel(win_enable, base + WINDOW_BAR_ENABLE);
2577 msp->win_protect = win_protect;
2578}
2579
2580static int mv643xx_eth_shared_probe(struct platform_device *pdev)
2581{
2582 static int mv643xx_version_printed = 0;
2583 struct mv643xx_eth_shared_platform_data *pd = pdev->dev.platform_data;
2584 struct mv643xx_shared_private *msp;
2585 struct resource *res;
2586 int ret;
2587
2588 if (!mv643xx_version_printed++)
2589 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
2590
2591 ret = -EINVAL;
2592 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2593 if (res == NULL)
2594 goto out;
2595
2596 ret = -ENOMEM;
2597 msp = kmalloc(sizeof(*msp), GFP_KERNEL);
2598 if (msp == NULL)
2599 goto out;
2600 memset(msp, 0, sizeof(*msp));
2601
2602 msp->eth_base = ioremap(res->start, res->end - res->start + 1);
2603 if (msp->eth_base == NULL)
2604 goto out_free;
2605
2606 spin_lock_init(&msp->phy_lock);
2607 msp->t_clk = (pd != NULL && pd->t_clk != 0) ? pd->t_clk : 133000000;
2608
2609 platform_set_drvdata(pdev, msp);
2610
2611 /*
2612 * (Re-)program MBUS remapping windows if we are asked to.
2613 */
2614 if (pd != NULL && pd->dram != NULL)
2615 mv643xx_eth_conf_mbus_windows(msp, pd->dram);
2616
2617 return 0;
2618
2619out_free:
2620 kfree(msp);
2621out:
2622 return ret;
2623}
2624
2625static int mv643xx_eth_shared_remove(struct platform_device *pdev)
2626{
2627 struct mv643xx_shared_private *msp = platform_get_drvdata(pdev);
2628
2629 iounmap(msp->eth_base);
2630 kfree(msp);
2631
2632 return 0;
2633}
2634
2635static struct platform_driver mv643xx_eth_shared_driver = {
2636 .probe = mv643xx_eth_shared_probe,
2637 .remove = mv643xx_eth_shared_remove,
2638 .driver = {
2639 .name = MV643XX_ETH_SHARED_NAME,
2640 .owner = THIS_MODULE,
2641 },
2642};
2643
2644/*
2645 * ethernet_phy_set - Set the ethernet port PHY address.
2646 *
2647 * DESCRIPTION:
2648 * This routine sets the given ethernet port PHY address.
2649 *
2650 * INPUT:
2651 * struct mv643xx_private *mp Ethernet Port.
2652 * int phy_addr PHY address.
2653 *
2654 * OUTPUT:
2655 * None.
2656 *
2657 * RETURN:
2658 * None.
2659 *
2660 */
2661static void ethernet_phy_set(struct mv643xx_private *mp, int phy_addr)
2662{
2663 u32 reg_data;
2664 int addr_shift = 5 * mp->port_num;
2665
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002666 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002667 reg_data &= ~(0x1f << addr_shift);
2668 reg_data |= (phy_addr & 0x1f) << addr_shift;
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002669 wrl(mp, PHY_ADDR, reg_data);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002670}
2671
2672/*
2673 * ethernet_phy_get - Get the ethernet port PHY address.
2674 *
2675 * DESCRIPTION:
2676 * This routine returns the given ethernet port PHY address.
2677 *
2678 * INPUT:
2679 * struct mv643xx_private *mp Ethernet Port.
2680 *
2681 * OUTPUT:
2682 * None.
2683 *
2684 * RETURN:
2685 * PHY address.
2686 *
2687 */
2688static int ethernet_phy_get(struct mv643xx_private *mp)
2689{
2690 unsigned int reg_data;
2691
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002692 reg_data = rdl(mp, PHY_ADDR);
Lennert Buytenhekc9df406f2008-06-01 00:48:39 +02002693
2694 return ((reg_data >> (5 * mp->port_num)) & 0x1f);
2695}
2696
2697/*
2698 * ethernet_phy_detect - Detect whether a phy is present
2699 *
2700 * DESCRIPTION:
2701 * This function tests whether there is a PHY present on
2702 * the specified port.
2703 *
2704 * INPUT:
2705 * struct mv643xx_private *mp Ethernet Port.
2706 *
2707 * OUTPUT:
2708 * None
2709 *
2710 * RETURN:
2711 * 0 on success
2712 * -ENODEV on failure
2713 *
2714 */
2715static int ethernet_phy_detect(struct mv643xx_private *mp)
2716{
2717 unsigned int phy_reg_data0;
2718 int auto_neg;
2719
2720 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2721 auto_neg = phy_reg_data0 & 0x1000;
2722 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2723 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2724
2725 eth_port_read_smi_reg(mp, 0, &phy_reg_data0);
2726 if ((phy_reg_data0 & 0x1000) == auto_neg)
2727 return -ENODEV; /* change didn't take */
2728
2729 phy_reg_data0 ^= 0x1000;
2730 eth_port_write_smi_reg(mp, 0, phy_reg_data0);
2731 return 0;
2732}
2733
James Chapmand0412d92006-01-27 01:15:30 -07002734static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
2735 int speed, int duplex,
2736 struct ethtool_cmd *cmd)
2737{
2738 struct mv643xx_private *mp = netdev_priv(dev);
2739
2740 memset(cmd, 0, sizeof(*cmd));
2741
2742 cmd->port = PORT_MII;
2743 cmd->transceiver = XCVR_INTERNAL;
2744 cmd->phy_address = phy_address;
2745
2746 if (speed == 0) {
2747 cmd->autoneg = AUTONEG_ENABLE;
2748 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
2749 cmd->speed = SPEED_100;
2750 cmd->advertising = ADVERTISED_10baseT_Half |
2751 ADVERTISED_10baseT_Full |
2752 ADVERTISED_100baseT_Half |
2753 ADVERTISED_100baseT_Full;
2754 if (mp->mii.supports_gmii)
2755 cmd->advertising |= ADVERTISED_1000baseT_Full;
2756 } else {
2757 cmd->autoneg = AUTONEG_DISABLE;
2758 cmd->speed = speed;
2759 cmd->duplex = duplex;
2760 }
2761}
2762
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763/*/
2764 * mv643xx_eth_probe
2765 *
2766 * First function called after registering the network device.
2767 * It's purpose is to initialize the device as an ethernet device,
2768 * fill the ethernet device structure with pointers * to functions,
2769 * and set the MAC address of the interface
2770 *
2771 * Input : struct device *
2772 * Output : -ENOMEM if failed , 0 if success
2773 */
Russell King3ae5eae2005-11-09 22:32:44 +00002774static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 struct mv643xx_eth_platform_data *pd;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002777 int port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 struct mv643xx_private *mp;
2779 struct net_device *dev;
2780 u8 *p;
2781 struct resource *res;
2782 int err;
James Chapmand0412d92006-01-27 01:15:30 -07002783 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07002784 int duplex = DUPLEX_HALF;
2785 int speed = 0; /* default to auto-negotiation */
Al Viroc5d64712007-10-13 08:30:26 +01002786 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002788 pd = pdev->dev.platform_data;
2789 if (pd == NULL) {
2790 printk(KERN_ERR "No mv643xx_eth_platform_data\n");
2791 return -ENODEV;
2792 }
2793
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002794 if (pd->shared == NULL) {
2795 printk(KERN_ERR "No mv643xx_eth_platform_data->shared\n");
2796 return -ENODEV;
2797 }
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 dev = alloc_etherdev(sizeof(struct mv643xx_private));
2800 if (!dev)
2801 return -ENOMEM;
2802
Russell King3ae5eae2005-11-09 22:32:44 +00002803 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 mp = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002806 mp->dev = dev;
2807#ifdef MV643XX_NAPI
2808 netif_napi_add(dev, &mp->napi, mv643xx_poll, 64);
2809#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2812 BUG_ON(!res);
2813 dev->irq = res->start;
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 dev->open = mv643xx_eth_open;
2816 dev->stop = mv643xx_eth_stop;
2817 dev->hard_start_xmit = mv643xx_eth_start_xmit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 dev->set_mac_address = mv643xx_eth_set_mac_address;
2819 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
2820
2821 /* No need to Tx Timeout */
2822 dev->tx_timeout = mv643xx_eth_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Dale Farnsworth63c9e542005-09-02 13:49:10 -07002824#ifdef CONFIG_NET_POLL_CONTROLLER
2825 dev->poll_controller = mv643xx_netpoll;
2826#endif
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 dev->watchdog_timeo = 2 * HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 dev->base_addr = 0;
2830 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07002831 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
2833
2834#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2835#ifdef MAX_SKB_FRAGS
2836 /*
2837 * Zero copy can only work if we use Discovery II memory. Else, we will
2838 * have to map the buffers to ISA memory which is only 16 MB
2839 */
Wolfram Joost63890572006-01-16 16:57:41 -07002840 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841#endif
2842#endif
2843
2844 /* Configure the timeout task */
Al Viro91c7c562006-12-06 19:50:06 +00002845 INIT_WORK(&mp->tx_timeout_task, mv643xx_eth_tx_timeout_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 spin_lock_init(&mp->lock);
2848
Lennert Buytenhekfa3959f2008-04-24 01:27:02 +02002849 mp->shared = platform_get_drvdata(pd->shared);
Gabriel Paubertfadac402007-03-23 12:03:52 -07002850 port_num = mp->port_num = pd->port_number;
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002851
Lennert Buytenhekf2ce8252008-04-24 01:27:17 +02002852 if (mp->shared->win_protect)
2853 wrl(mp, WINDOW_PROTECT(port_num), mp->shared->win_protect);
2854
Lennert Buytenhekce4e2e42008-04-24 01:29:59 +02002855 mp->shared_smi = mp->shared;
2856 if (pd->shared_smi != NULL)
2857 mp->shared_smi = platform_get_drvdata(pd->shared_smi);
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 /* set default config values */
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002860 eth_port_uc_addr_get(mp, dev->dev_addr);
Lennert Buytenheke4d00fa2007-10-19 04:11:28 +02002861 mp->rx_ring_size = PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
2862 mp->tx_ring_size = PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002864 if (is_valid_ether_addr(pd->mac_addr))
2865 memcpy(dev->dev_addr, pd->mac_addr, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002867 if (pd->phy_addr || pd->force_phy_addr)
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002868 ethernet_phy_set(mp, pd->phy_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002870 if (pd->rx_queue_size)
2871 mp->rx_ring_size = pd->rx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002873 if (pd->tx_queue_size)
2874 mp->tx_ring_size = pd->tx_queue_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002876 if (pd->tx_sram_size) {
2877 mp->tx_sram_size = pd->tx_sram_size;
2878 mp->tx_sram_addr = pd->tx_sram_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 }
2880
Dale Farnsworth84dd6192007-03-03 06:40:28 -07002881 if (pd->rx_sram_size) {
2882 mp->rx_sram_size = pd->rx_sram_size;
2883 mp->rx_sram_addr = pd->rx_sram_addr;
2884 }
2885
2886 duplex = pd->duplex;
2887 speed = pd->speed;
2888
James Chapmanc28a4f82006-01-27 01:13:15 -07002889 /* Hook up MII support for ethtool */
2890 mp->mii.dev = dev;
2891 mp->mii.mdio_read = mv643xx_mdio_read;
2892 mp->mii.mdio_write = mv643xx_mdio_write;
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002893 mp->mii.phy_id = ethernet_phy_get(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002894 mp->mii.phy_id_mask = 0x3f;
2895 mp->mii.reg_num_mask = 0x1f;
2896
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002897 err = ethernet_phy_detect(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (err) {
Lennert Buytenhekc1b35a22008-03-18 11:37:19 -07002899 pr_debug("%s: No PHY detected at addr %d\n",
2900 dev->name, ethernet_phy_get(mp));
James Chapmand0412d92006-01-27 01:15:30 -07002901 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
2903
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002904 ethernet_phy_reset(mp);
James Chapmanc28a4f82006-01-27 01:13:15 -07002905 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07002906 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
2907 mv643xx_eth_update_pscr(dev, &cmd);
2908 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07002909
Olaf Heringb0b8dab2006-04-27 18:23:49 -07002910 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 err = register_netdev(dev);
2912 if (err)
2913 goto out;
2914
2915 p = dev->dev_addr;
2916 printk(KERN_NOTICE
Joe Perches0795af52007-10-03 17:59:30 -07002917 "%s: port %d with MAC address %s\n",
2918 dev->name, port_num, print_mac(mac, p));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920 if (dev->features & NETIF_F_SG)
2921 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
2922
2923 if (dev->features & NETIF_F_IP_CSUM)
2924 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
2925 dev->name);
2926
2927#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2928 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
2929#endif
2930
2931#ifdef MV643XX_COAL
2932 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
2933 dev->name);
2934#endif
2935
2936#ifdef MV643XX_NAPI
2937 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
2938#endif
2939
Nicolas DETb1529872005-10-28 17:46:30 -07002940 if (mp->tx_sram_size > 0)
2941 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 return 0;
2944
2945out:
2946 free_netdev(dev);
2947
2948 return err;
2949}
2950
Russell King3ae5eae2005-11-09 22:32:44 +00002951static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
Russell King3ae5eae2005-11-09 22:32:44 +00002953 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 unregister_netdev(dev);
2956 flush_scheduled_work();
2957
2958 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00002959 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 return 0;
2961}
2962
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002963static void mv643xx_eth_shutdown(struct platform_device *pdev)
2964{
2965 struct net_device *dev = platform_get_drvdata(pdev);
2966 struct mv643xx_private *mp = netdev_priv(dev);
2967 unsigned int port_num = mp->port_num;
2968
2969 /* Mask all interrupts on ethernet port */
Lennert Buytenhek3cb46672008-06-01 01:03:23 +02002970 wrl(mp, INT_MASK(port_num), 0);
2971 rdl(mp, INT_MASK(port_num));
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002972
Lennert Buytenhekafdb57a2008-03-18 11:36:08 -07002973 eth_port_reset(mp);
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002974}
2975
Russell King3ae5eae2005-11-09 22:32:44 +00002976static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 .probe = mv643xx_eth_probe,
2978 .remove = mv643xx_eth_remove,
Dale Farnsworthd57ab6f2007-03-20 16:38:04 -07002979 .shutdown = mv643xx_eth_shutdown,
Russell King3ae5eae2005-11-09 22:32:44 +00002980 .driver = {
2981 .name = MV643XX_ETH_NAME,
Kay Sievers72abb462008-04-18 13:50:44 -07002982 .owner = THIS_MODULE,
Russell King3ae5eae2005-11-09 22:32:44 +00002983 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984};
2985
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986/*
2987 * mv643xx_init_module
2988 *
2989 * Registers the network drivers into the Linux kernel
2990 *
2991 * Input : N/A
2992 *
2993 * Output : N/A
2994 */
2995static int __init mv643xx_init_module(void)
2996{
2997 int rc;
2998
Russell King3ae5eae2005-11-09 22:32:44 +00002999 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00003001 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00003003 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 }
3005 return rc;
3006}
3007
3008/*
3009 * mv643xx_cleanup_module
3010 *
3011 * Registers the network drivers into the Linux kernel
3012 *
3013 * Input : N/A
3014 *
3015 * Output : N/A
3016 */
3017static void __exit mv643xx_cleanup_module(void)
3018{
Russell King3ae5eae2005-11-09 22:32:44 +00003019 platform_driver_unregister(&mv643xx_eth_driver);
3020 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021}
3022
3023module_init(mv643xx_init_module);
3024module_exit(mv643xx_cleanup_module);
3025
3026MODULE_LICENSE("GPL");
3027MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
3028 " and Dale Farnsworth");
3029MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
Kay Sievers72abb462008-04-18 13:50:44 -07003030MODULE_ALIAS("platform:" MV643XX_ETH_NAME);
3031MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME);