blob: 411f4d809c477d99175ee0eaa10257938a08d407 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/net/mv643xx_eth.c - Driver for MV643XX ethernet ports
3 * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com>
4 *
5 * Based on the 64360 driver from:
6 * Copyright (C) 2002 rabeeh@galileo.co.il
7 *
8 * Copyright (C) 2003 PMC-Sierra, Inc.,
Olaf Hering3bb8a18a2006-01-05 22:45:45 -08009 * written by Manish Lachwani
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
11 * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
12 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -070013 * Copyright (C) 2004-2006 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * Dale Farnsworth <dale@farnsworth.org>
15 *
16 * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com>
17 * <sjhill@realitydiluted.com>
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation; either version 2
22 * of the License, or (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 */
33#include <linux/init.h>
34#include <linux/dma-mapping.h>
Al Virob6298c22006-01-18 19:35:54 -050035#include <linux/in.h>
36#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/tcp.h>
38#include <linux/udp.h>
39#include <linux/etherdevice.h>
40
41#include <linux/bitops.h>
42#include <linux/delay.h>
43#include <linux/ethtool.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010044#include <linux/platform_device.h>
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/io.h>
47#include <asm/types.h>
48#include <asm/pgtable.h>
49#include <asm/system.h>
50#include <asm/delay.h>
51#include "mv643xx_eth.h"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/* Static function declarations */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static void eth_port_uc_addr_get(struct net_device *dev,
55 unsigned char *MacAddr);
Dale Farnsworth16e03012006-01-16 16:50:02 -070056static void eth_port_set_multicast_list(struct net_device *);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -070057static void mv643xx_eth_port_enable_tx(unsigned int port_num,
Dale Farnsworth12a87c62006-03-03 10:00:22 -070058 unsigned int queues);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -070059static void mv643xx_eth_port_enable_rx(unsigned int port_num,
Dale Farnsworth12a87c62006-03-03 10:00:22 -070060 unsigned int queues);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -070061static unsigned int mv643xx_eth_port_disable_tx(unsigned int port_num);
62static unsigned int mv643xx_eth_port_disable_rx(unsigned int port_num);
Dale Farnsworthab4384a2006-01-16 16:59:21 -070063static int mv643xx_eth_open(struct net_device *);
64static int mv643xx_eth_stop(struct net_device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065static int mv643xx_eth_change_mtu(struct net_device *, int);
66static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *);
67static void eth_port_init_mac_tables(unsigned int eth_port_num);
68#ifdef MV643XX_NAPI
69static int mv643xx_poll(struct net_device *dev, int *budget);
70#endif
James Chapmanc28a4f82006-01-27 01:13:15 -070071static int ethernet_phy_get(unsigned int eth_port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
73static int ethernet_phy_detect(unsigned int eth_port_num);
James Chapmanc28a4f82006-01-27 01:13:15 -070074static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location);
75static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val);
James Chapmand0412d92006-01-27 01:15:30 -070076static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static struct ethtool_ops mv643xx_ethtool_ops;
78
79static char mv643xx_driver_name[] = "mv643xx_eth";
80static char mv643xx_driver_version[] = "1.0";
81
82static void __iomem *mv643xx_eth_shared_base;
83
84/* used to protect MV643XX_ETH_SMI_REG, which is shared across ports */
Ingo Molnara9f6a0d2005-09-09 13:10:41 -070085static DEFINE_SPINLOCK(mv643xx_eth_phy_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
87static inline u32 mv_read(int offset)
88{
Al Virodc074a82005-04-25 07:55:58 -070089 void __iomem *reg_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91 reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS;
92
93 return readl(reg_base + offset);
94}
95
96static inline void mv_write(int offset, u32 data)
97{
Al Virodc074a82005-04-25 07:55:58 -070098 void __iomem *reg_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS;
101 writel(data, reg_base + offset);
102}
103
104/*
105 * Changes MTU (maximum transfer unit) of the gigabit ethenret port
106 *
107 * Input : pointer to ethernet interface network device structure
108 * new mtu size
109 * Output : 0 upon success, -EINVAL upon failure
110 */
111static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
112{
Dale Farnsworth8f518702006-01-16 16:56:30 -0700113 if ((new_mtu > 9500) || (new_mtu < 64))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 dev->mtu = new_mtu;
117 /*
118 * Stop then re-open the interface. This will allocate RX skb's with
119 * the new MTU.
120 * There is a possible danger that the open will not successed, due
121 * to memory is full, which might fail the open function.
122 */
123 if (netif_running(dev)) {
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700124 mv643xx_eth_stop(dev);
125 if (mv643xx_eth_open(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 printk(KERN_ERR
127 "%s: Fatal error on opening device\n",
128 dev->name);
129 }
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 return 0;
132}
133
134/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700135 * mv643xx_eth_rx_refill_descs
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
137 * Fills / refills RX queue on a certain gigabit ethernet port
138 *
139 * Input : pointer to ethernet interface network device structure
140 * Output : N/A
141 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700142static void mv643xx_eth_rx_refill_descs(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 struct mv643xx_private *mp = netdev_priv(dev);
145 struct pkt_info pkt_info;
146 struct sk_buff *skb;
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700147 int unaligned;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700149 while (mp->rx_desc_count < mp->rx_ring_size) {
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700150 skb = dev_alloc_skb(ETH_RX_SKB_SIZE + ETH_DMA_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (!skb)
152 break;
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700153 mp->rx_desc_count++;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700154 unaligned = (u32)skb->data & (ETH_DMA_ALIGN - 1);
Dale Farnsworthb44cd572006-01-16 16:51:22 -0700155 if (unaligned)
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700156 skb_reserve(skb, ETH_DMA_ALIGN - unaligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700158 pkt_info.byte_cnt = ETH_RX_SKB_SIZE;
159 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
160 ETH_RX_SKB_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 pkt_info.return_info = skb;
162 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
163 printk(KERN_ERR
164 "%s: Error allocating RX Ring\n", dev->name);
165 break;
166 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700167 skb_reserve(skb, ETH_HW_IP_ALIGN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 /*
170 * If RX ring is empty of SKB, set a timer to try allocating
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700171 * again at a later time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700173 if (mp->rx_desc_count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700175 mp->timeout.expires = jiffies + (HZ / 10); /* 100 mSec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 add_timer(&mp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
180/*
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700181 * mv643xx_eth_rx_refill_descs_timer_wrapper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 *
183 * Timer routine to wake up RX queue filling task. This function is
184 * used only in case the RX queue is empty, and all alloc_skb has
185 * failed (due to out of memory event).
186 *
187 * Input : pointer to ethernet interface network device structure
188 * Output : N/A
189 */
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700190static inline void mv643xx_eth_rx_refill_descs_timer_wrapper(unsigned long data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700192 mv643xx_eth_rx_refill_descs((struct net_device *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
196 * mv643xx_eth_update_mac_address
197 *
198 * Update the MAC address of the port in the address table
199 *
200 * Input : pointer to ethernet interface network device structure
201 * Output : N/A
202 */
203static void mv643xx_eth_update_mac_address(struct net_device *dev)
204{
205 struct mv643xx_private *mp = netdev_priv(dev);
206 unsigned int port_num = mp->port_num;
207
208 eth_port_init_mac_tables(port_num);
Dale Farnsworthed9b5d42006-01-27 01:06:38 -0700209 eth_port_uc_addr_set(port_num, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
212/*
213 * mv643xx_eth_set_rx_mode
214 *
215 * Change from promiscuos to regular rx mode
216 *
217 * Input : pointer to ethernet interface network device structure
218 * Output : N/A
219 */
220static void mv643xx_eth_set_rx_mode(struct net_device *dev)
221{
222 struct mv643xx_private *mp = netdev_priv(dev);
Dale Farnsworth01999872006-01-27 01:18:01 -0700223 u32 config_reg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Dale Farnsworth01999872006-01-27 01:18:01 -0700225 config_reg = mv_read(MV643XX_ETH_PORT_CONFIG_REG(mp->port_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 if (dev->flags & IFF_PROMISC)
Dale Farnsworth01999872006-01-27 01:18:01 -0700227 config_reg |= (u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 else
Dale Farnsworth01999872006-01-27 01:18:01 -0700229 config_reg &= ~(u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE;
230 mv_write(MV643XX_ETH_PORT_CONFIG_REG(mp->port_num), config_reg);
Dale Farnsworth16e03012006-01-16 16:50:02 -0700231
232 eth_port_set_multicast_list(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*
236 * mv643xx_eth_set_mac_address
237 *
238 * Change the interface's mac address.
239 * No special hardware thing should be done because interface is always
240 * put in promiscuous mode.
241 *
242 * Input : pointer to ethernet interface network device structure and
243 * a pointer to the designated entry to be added to the cache.
244 * Output : zero upon success, negative upon failure
245 */
246static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
247{
248 int i;
249
250 for (i = 0; i < 6; i++)
251 /* +2 is for the offset of the HW addr type */
252 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
253 mv643xx_eth_update_mac_address(dev);
254 return 0;
255}
256
257/*
258 * mv643xx_eth_tx_timeout
259 *
260 * Called upon a timeout on transmitting a packet
261 *
262 * Input : pointer to ethernet interface network device structure.
263 * Output : N/A
264 */
265static void mv643xx_eth_tx_timeout(struct net_device *dev)
266{
267 struct mv643xx_private *mp = netdev_priv(dev);
268
269 printk(KERN_INFO "%s: TX timeout ", dev->name);
270
271 /* Do the reset outside of interrupt context */
272 schedule_work(&mp->tx_timeout_task);
273}
274
275/*
276 * mv643xx_eth_tx_timeout_task
277 *
278 * Actual routine to reset the adapter when a timeout on Tx has occurred
279 */
280static void mv643xx_eth_tx_timeout_task(struct net_device *dev)
281{
282 struct mv643xx_private *mp = netdev_priv(dev);
283
Dale Farnsworth94843562006-04-11 18:24:26 -0700284 if (!netif_running(dev))
285 return;
286
287 netif_stop_queue(dev);
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 eth_port_reset(mp->port_num);
Dale Farnsworthed9b5d42006-01-27 01:06:38 -0700290 eth_port_start(dev);
Dale Farnsworth94843562006-04-11 18:24:26 -0700291
292 if (mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
293 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700296/**
297 * mv643xx_eth_free_tx_descs - Free the tx desc data for completed descriptors
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 *
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700299 * If force is non-zero, frees uncompleted descriptors as well
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700301int mv643xx_eth_free_tx_descs(struct net_device *dev, int force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct mv643xx_private *mp = netdev_priv(dev);
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700304 struct eth_tx_desc *desc;
305 u32 cmd_sts;
306 struct sk_buff *skb;
307 unsigned long flags;
308 int tx_index;
309 dma_addr_t addr;
310 int count;
311 int released = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700313 while (mp->tx_desc_count > 0) {
314 spin_lock_irqsave(&mp->lock, flags);
315 tx_index = mp->tx_used_desc_q;
316 desc = &mp->p_tx_desc_area[tx_index];
317 cmd_sts = desc->cmd_sts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700319 if (!force && (cmd_sts & ETH_BUFFER_OWNED_BY_DMA)) {
320 spin_unlock_irqrestore(&mp->lock, flags);
321 return released;
322 }
323
324 mp->tx_used_desc_q = (tx_index + 1) % mp->tx_ring_size;
325 mp->tx_desc_count--;
326
327 addr = desc->buf_ptr;
328 count = desc->byte_cnt;
329 skb = mp->tx_skb[tx_index];
330 if (skb)
331 mp->tx_skb[tx_index] = NULL;
332
333 spin_unlock_irqrestore(&mp->lock, flags);
334
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700335 if (cmd_sts & ETH_ERROR_SUMMARY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 printk("%s: Error in TX\n", dev->name);
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700337 mp->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700340 if (cmd_sts & ETH_TX_FIRST_DESC)
341 dma_unmap_single(NULL, addr, count, DMA_TO_DEVICE);
Paolo Galtiericb415d32006-01-16 16:48:02 -0700342 else
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700343 dma_unmap_page(NULL, addr, count, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700345 if (skb)
346 dev_kfree_skb_irq(skb);
347
348 released = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return released;
352}
353
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700354static void mv643xx_eth_free_completed_tx_descs(struct net_device *dev)
355{
356 struct mv643xx_private *mp = netdev_priv(dev);
357
358 if (mv643xx_eth_free_tx_descs(dev, 0) &&
359 mp->tx_ring_size - mp->tx_desc_count >= MAX_DESCS_PER_SKB)
360 netif_wake_queue(dev);
361}
362
363static void mv643xx_eth_free_all_tx_descs(struct net_device *dev)
364{
365 mv643xx_eth_free_tx_descs(dev, 1);
366}
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368/*
369 * mv643xx_eth_receive
370 *
371 * This function is forward packets that are received from the port's
372 * queues toward kernel core or FastRoute them to another interface.
373 *
374 * Input : dev - a pointer to the required interface
375 * max - maximum number to receive (0 means unlimted)
376 *
377 * Output : number of served packets
378 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 struct mv643xx_private *mp = netdev_priv(dev);
382 struct net_device_stats *stats = &mp->stats;
383 unsigned int received_packets = 0;
384 struct sk_buff *skb;
385 struct pkt_info pkt_info;
386
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700387 while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700388 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 received_packets++;
Dale Farnsworthb1dd9ca2005-09-01 09:59:23 -0700390
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700391 /*
392 * Update statistics.
393 * Note byte count includes 4 byte CRC count
394 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 stats->rx_packets++;
396 stats->rx_bytes += pkt_info.byte_cnt;
397 skb = pkt_info.return_info;
398 /*
399 * In case received a packet without first / last bits on OR
400 * the error summary bit is on, the packets needs to be dropeed.
401 */
402 if (((pkt_info.cmd_sts
403 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
404 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
405 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
406 stats->rx_dropped++;
407 if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
408 ETH_RX_LAST_DESC)) !=
409 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
410 if (net_ratelimit())
411 printk(KERN_ERR
412 "%s: Received packet spread "
413 "on multiple descriptors\n",
414 dev->name);
415 }
416 if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
417 stats->rx_errors++;
418
419 dev_kfree_skb_irq(skb);
420 } else {
421 /*
422 * The -4 is for the CRC in the trailer of the
423 * received packet
424 */
425 skb_put(skb, pkt_info.byte_cnt - 4);
426 skb->dev = dev;
427
428 if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
429 skb->ip_summed = CHECKSUM_UNNECESSARY;
430 skb->csum = htons(
431 (pkt_info.cmd_sts & 0x0007fff8) >> 3);
432 }
433 skb->protocol = eth_type_trans(skb, dev);
434#ifdef MV643XX_NAPI
435 netif_receive_skb(skb);
436#else
437 netif_rx(skb);
438#endif
439 }
Paolo Galtieri12ad74f2006-01-27 01:03:38 -0700440 dev->last_rx = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700442 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 return received_packets;
445}
446
James Chapmand0412d92006-01-27 01:15:30 -0700447/* Set the mv643xx port configuration register for the speed/duplex mode. */
448static void mv643xx_eth_update_pscr(struct net_device *dev,
449 struct ethtool_cmd *ecmd)
450{
451 struct mv643xx_private *mp = netdev_priv(dev);
452 int port_num = mp->port_num;
453 u32 o_pscr, n_pscr;
Dale Farnsworth12a87c62006-03-03 10:00:22 -0700454 unsigned int queues;
James Chapmand0412d92006-01-27 01:15:30 -0700455
456 o_pscr = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
457 n_pscr = o_pscr;
458
459 /* clear speed, duplex and rx buffer size fields */
460 n_pscr &= ~(MV643XX_ETH_SET_MII_SPEED_TO_100 |
461 MV643XX_ETH_SET_GMII_SPEED_TO_1000 |
462 MV643XX_ETH_SET_FULL_DUPLEX_MODE |
463 MV643XX_ETH_MAX_RX_PACKET_MASK);
464
465 if (ecmd->duplex == DUPLEX_FULL)
466 n_pscr |= MV643XX_ETH_SET_FULL_DUPLEX_MODE;
467
468 if (ecmd->speed == SPEED_1000)
469 n_pscr |= MV643XX_ETH_SET_GMII_SPEED_TO_1000 |
470 MV643XX_ETH_MAX_RX_PACKET_9700BYTE;
471 else {
472 if (ecmd->speed == SPEED_100)
473 n_pscr |= MV643XX_ETH_SET_MII_SPEED_TO_100;
474 n_pscr |= MV643XX_ETH_MAX_RX_PACKET_1522BYTE;
475 }
476
477 if (n_pscr != o_pscr) {
478 if ((o_pscr & MV643XX_ETH_SERIAL_PORT_ENABLE) == 0)
479 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
480 n_pscr);
481 else {
Dale Farnsworth12a87c62006-03-03 10:00:22 -0700482 queues = mv643xx_eth_port_disable_tx(port_num);
James Chapmand0412d92006-01-27 01:15:30 -0700483
484 o_pscr &= ~MV643XX_ETH_SERIAL_PORT_ENABLE;
485 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
486 o_pscr);
487 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
488 n_pscr);
489 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
490 n_pscr);
Dale Farnsworth12a87c62006-03-03 10:00:22 -0700491 if (queues)
492 mv643xx_eth_port_enable_tx(port_num, queues);
James Chapmand0412d92006-01-27 01:15:30 -0700493 }
494 }
495}
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497/*
498 * mv643xx_eth_int_handler
499 *
500 * Main interrupt handler for the gigbit ethernet ports
501 *
502 * Input : irq - irq number (not used)
503 * dev_id - a pointer to the required interface's data structure
504 * regs - not used
505 * Output : N/A
506 */
507
508static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id,
Dale Farnsworthb4de9052006-01-27 01:04:43 -0700509 struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 struct net_device *dev = (struct net_device *)dev_id;
512 struct mv643xx_private *mp = netdev_priv(dev);
513 u32 eth_int_cause, eth_int_cause_ext = 0;
514 unsigned int port_num = mp->port_num;
515
516 /* Read interrupt cause registers */
517 eth_int_cause = mv_read(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num)) &
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700518 ETH_INT_UNMASK_ALL;
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700519 if (eth_int_cause & ETH_INT_CAUSE_EXT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 eth_int_cause_ext = mv_read(
521 MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num)) &
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700522 ETH_INT_UNMASK_ALL_EXT;
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700523 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num),
524 ~eth_int_cause_ext);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /* PHY status changed */
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700528 if (eth_int_cause_ext & ETH_INT_CAUSE_PHY) {
James Chapmand0412d92006-01-27 01:15:30 -0700529 struct ethtool_cmd cmd;
530
James Chapmanc28a4f82006-01-27 01:13:15 -0700531 if (mii_link_ok(&mp->mii)) {
James Chapmand0412d92006-01-27 01:15:30 -0700532 mii_ethtool_gset(&mp->mii, &cmd);
533 mv643xx_eth_update_pscr(dev, &cmd);
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700534 mv643xx_eth_port_enable_tx(port_num,
535 ETH_TX_QUEUES_ENABLED);
James Chapmanc28a4f82006-01-27 01:13:15 -0700536 if (!netif_carrier_ok(dev)) {
537 netif_carrier_on(dev);
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700538 if (mp->tx_ring_size - mp->tx_desc_count >=
539 MAX_DESCS_PER_SKB)
James Chapmand0412d92006-01-27 01:15:30 -0700540 netif_wake_queue(dev);
James Chapmanc28a4f82006-01-27 01:13:15 -0700541 }
542 } else if (netif_carrier_ok(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 netif_stop_queue(dev);
James Chapmanc28a4f82006-01-27 01:13:15 -0700544 netif_carrier_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
546 }
547
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700548#ifdef MV643XX_NAPI
549 if (eth_int_cause & ETH_INT_CAUSE_RX) {
550 /* schedule the NAPI poll routine to maintain port */
551 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
552 ETH_INT_MASK_ALL);
553 /* wait for previous write to complete */
554 mv_read(MV643XX_ETH_INTERRUPT_MASK_REG(port_num));
555
556 netif_rx_schedule(dev);
557 }
558#else
559 if (eth_int_cause & ETH_INT_CAUSE_RX)
560 mv643xx_eth_receive_queue(dev, INT_MAX);
Brent Cook5c537402006-04-11 18:23:15 -0700561#endif
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700562 if (eth_int_cause_ext & ETH_INT_CAUSE_TX)
563 mv643xx_eth_free_completed_tx_descs(dev);
Dale Farnsworth468d09f2006-03-03 10:04:39 -0700564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /*
566 * If no real interrupt occured, exit.
567 * This can happen when using gigE interrupt coalescing mechanism.
568 */
569 if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
570 return IRQ_NONE;
571
572 return IRQ_HANDLED;
573}
574
575#ifdef MV643XX_COAL
576
577/*
578 * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
579 *
580 * DESCRIPTION:
581 * This routine sets the RX coalescing interrupt mechanism parameter.
582 * This parameter is a timeout counter, that counts in 64 t_clk
583 * chunks ; that when timeout event occurs a maskable interrupt
584 * occurs.
585 * The parameter is calculated using the tClk of the MV-643xx chip
586 * , and the required delay of the interrupt in usec.
587 *
588 * INPUT:
589 * unsigned int eth_port_num Ethernet port number
590 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
591 * unsigned int delay Delay in usec
592 *
593 * OUTPUT:
594 * Interrupt coalescing mechanism value is set in MV-643xx chip.
595 *
596 * RETURN:
597 * The interrupt coalescing value set in the gigE port.
598 *
599 */
600static unsigned int eth_port_set_rx_coal(unsigned int eth_port_num,
601 unsigned int t_clk, unsigned int delay)
602{
603 unsigned int coal = ((t_clk / 1000000) * delay) / 64;
604
605 /* Set RX Coalescing mechanism */
606 mv_write(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num),
607 ((coal & 0x3fff) << 8) |
608 (mv_read(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num))
609 & 0xffc000ff));
610
611 return coal;
612}
613#endif
614
615/*
616 * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
617 *
618 * DESCRIPTION:
619 * This routine sets the TX coalescing interrupt mechanism parameter.
620 * This parameter is a timeout counter, that counts in 64 t_clk
621 * chunks ; that when timeout event occurs a maskable interrupt
622 * occurs.
623 * The parameter is calculated using the t_cLK frequency of the
624 * MV-643xx chip and the required delay in the interrupt in uSec
625 *
626 * INPUT:
627 * unsigned int eth_port_num Ethernet port number
628 * unsigned int t_clk t_clk of the MV-643xx chip in HZ units
629 * unsigned int delay Delay in uSeconds
630 *
631 * OUTPUT:
632 * Interrupt coalescing mechanism value is set in MV-643xx chip.
633 *
634 * RETURN:
635 * The interrupt coalescing value set in the gigE port.
636 *
637 */
638static unsigned int eth_port_set_tx_coal(unsigned int eth_port_num,
639 unsigned int t_clk, unsigned int delay)
640{
641 unsigned int coal;
642 coal = ((t_clk / 1000000) * delay) / 64;
643 /* Set TX Coalescing mechanism */
644 mv_write(MV643XX_ETH_TX_FIFO_URGENT_THRESHOLD_REG(eth_port_num),
645 coal << 4);
646 return coal;
647}
648
649/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
651 *
652 * DESCRIPTION:
653 * This function prepares a Rx chained list of descriptors and packet
654 * buffers in a form of a ring. The routine must be called after port
655 * initialization routine and before port start routine.
656 * The Ethernet SDMA engine uses CPU bus addresses to access the various
657 * devices in the system (i.e. DRAM). This function uses the ethernet
658 * struct 'virtual to physical' routine (set by the user) to set the ring
659 * with physical addresses.
660 *
661 * INPUT:
662 * struct mv643xx_private *mp Ethernet Port Control srtuct.
663 *
664 * OUTPUT:
665 * The routine updates the Ethernet port control struct with information
666 * regarding the Rx descriptors and buffers.
667 *
668 * RETURN:
669 * None.
670 */
671static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
672{
673 volatile struct eth_rx_desc *p_rx_desc;
674 int rx_desc_num = mp->rx_ring_size;
675 int i;
676
677 /* initialize the next_desc_ptr links in the Rx descriptors ring */
678 p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
679 for (i = 0; i < rx_desc_num; i++) {
680 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
681 ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
682 }
683
684 /* Save Rx desc pointer to driver struct. */
685 mp->rx_curr_desc_q = 0;
686 mp->rx_used_desc_q = 0;
687
688 mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
691/*
692 * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
693 *
694 * DESCRIPTION:
695 * This function prepares a Tx chained list of descriptors and packet
696 * buffers in a form of a ring. The routine must be called after port
697 * initialization routine and before port start routine.
698 * The Ethernet SDMA engine uses CPU bus addresses to access the various
699 * devices in the system (i.e. DRAM). This function uses the ethernet
700 * struct 'virtual to physical' routine (set by the user) to set the ring
701 * with physical addresses.
702 *
703 * INPUT:
704 * struct mv643xx_private *mp Ethernet Port Control srtuct.
705 *
706 * OUTPUT:
707 * The routine updates the Ethernet port control struct with information
708 * regarding the Tx descriptors and buffers.
709 *
710 * RETURN:
711 * None.
712 */
713static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
714{
715 int tx_desc_num = mp->tx_ring_size;
716 struct eth_tx_desc *p_tx_desc;
717 int i;
718
719 /* Initialize the next_desc_ptr links in the Tx descriptors ring */
720 p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
721 for (i = 0; i < tx_desc_num; i++) {
722 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
723 ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
724 }
725
726 mp->tx_curr_desc_q = 0;
727 mp->tx_used_desc_q = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
James Chapmand0412d92006-01-27 01:15:30 -0700732static int mv643xx_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
733{
734 struct mv643xx_private *mp = netdev_priv(dev);
735 int err;
736
737 spin_lock_irq(&mp->lock);
738 err = mii_ethtool_sset(&mp->mii, cmd);
739 spin_unlock_irq(&mp->lock);
740
741 return err;
742}
743
744static int mv643xx_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
745{
746 struct mv643xx_private *mp = netdev_priv(dev);
747 int err;
748
749 spin_lock_irq(&mp->lock);
750 err = mii_ethtool_gset(&mp->mii, cmd);
751 spin_unlock_irq(&mp->lock);
752
753 /* The PHY may support 1000baseT_Half, but the mv643xx does not */
754 cmd->supported &= ~SUPPORTED_1000baseT_Half;
755 cmd->advertising &= ~ADVERTISED_1000baseT_Half;
756
757 return err;
758}
759
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700760/*
761 * mv643xx_eth_open
762 *
763 * This function is called when openning the network device. The function
764 * should initialize all the hardware, initialize cyclic Rx/Tx
765 * descriptors chain and buffers and allocate an IRQ to the network
766 * device.
767 *
768 * Input : a pointer to the network device structure
769 *
770 * Output : zero of success , nonzero if fails.
771 */
772
773static int mv643xx_eth_open(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
775 struct mv643xx_private *mp = netdev_priv(dev);
776 unsigned int port_num = mp->port_num;
777 unsigned int size;
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700778 int err;
779
780 err = request_irq(dev->irq, mv643xx_eth_int_handler,
781 SA_SHIRQ | SA_SAMPLE_RANDOM, dev->name, dev);
782 if (err) {
783 printk(KERN_ERR "Can not assign IRQ number to MV643XX_eth%d\n",
784 port_num);
785 return -EAGAIN;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 eth_port_init(mp);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 memset(&mp->timeout, 0, sizeof(struct timer_list));
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700791 mp->timeout.function = mv643xx_eth_rx_refill_descs_timer_wrapper;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 mp->timeout.data = (unsigned long)dev;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /* Allocate RX and TX skb rings */
795 mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
796 GFP_KERNEL);
797 if (!mp->rx_skb) {
798 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700799 err = -ENOMEM;
800 goto out_free_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
803 GFP_KERNEL);
804 if (!mp->tx_skb) {
805 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700806 err = -ENOMEM;
807 goto out_free_rx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 /* Allocate TX ring */
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700811 mp->tx_desc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
813 mp->tx_desc_area_size = size;
814
815 if (mp->tx_sram_size) {
816 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
817 mp->tx_sram_size);
818 mp->tx_desc_dma = mp->tx_sram_addr;
819 } else
820 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
821 &mp->tx_desc_dma,
822 GFP_KERNEL);
823
824 if (!mp->p_tx_desc_area) {
825 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
826 dev->name, size);
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700827 err = -ENOMEM;
828 goto out_free_tx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830 BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
831 memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
832
833 ether_init_tx_desc_ring(mp);
834
835 /* Allocate RX ring */
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700836 mp->rx_desc_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
838 mp->rx_desc_area_size = size;
839
840 if (mp->rx_sram_size) {
841 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
842 mp->rx_sram_size);
843 mp->rx_desc_dma = mp->rx_sram_addr;
844 } else
845 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
846 &mp->rx_desc_dma,
847 GFP_KERNEL);
848
849 if (!mp->p_rx_desc_area) {
850 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
851 dev->name, size);
852 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
853 dev->name);
854 if (mp->rx_sram_size)
Dale Farnsworthdd09b1d2006-01-16 16:53:15 -0700855 iounmap(mp->p_tx_desc_area);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 else
857 dma_free_coherent(NULL, mp->tx_desc_area_size,
858 mp->p_tx_desc_area, mp->tx_desc_dma);
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700859 err = -ENOMEM;
860 goto out_free_tx_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 }
862 memset((void *)mp->p_rx_desc_area, 0, size);
863
864 ether_init_rx_desc_ring(mp);
865
Dale Farnsworthf78fb472006-03-03 10:05:26 -0700866 mv643xx_eth_rx_refill_descs(dev); /* Fill RX ring with skb's */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
James Chapmand0412d92006-01-27 01:15:30 -0700868 /* Clear any pending ethernet port interrupts */
869 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
870 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
871
Dale Farnsworthed9b5d42006-01-27 01:06:38 -0700872 eth_port_start(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* Interrupt Coalescing */
875
876#ifdef MV643XX_COAL
877 mp->rx_int_coal =
878 eth_port_set_rx_coal(port_num, 133000000, MV643XX_RX_COAL);
879#endif
880
881 mp->tx_int_coal =
882 eth_port_set_tx_coal(port_num, 133000000, MV643XX_TX_COAL);
883
Dale Farnsworth8f518702006-01-16 16:56:30 -0700884 /* Unmask phy and link status changes interrupts */
885 mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num),
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700886 ETH_INT_UNMASK_ALL_EXT);
Dale Farnsworth8f518702006-01-16 16:56:30 -0700887
888 /* Unmask RX buffer and TX end interrupt */
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700889 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
James Chapmand0412d92006-01-27 01:15:30 -0700890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700892
893out_free_tx_skb:
894 kfree(mp->tx_skb);
895out_free_rx_skb:
896 kfree(mp->rx_skb);
897out_free_irq:
898 free_irq(dev->irq, dev);
899
900 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}
902
903static void mv643xx_eth_free_tx_rings(struct net_device *dev)
904{
905 struct mv643xx_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* Stop Tx Queues */
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700908 mv643xx_eth_port_disable_tx(mp->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Dale Farnsworthff561ee2006-03-03 10:02:51 -0700910 /* Free outstanding skb's on TX ring */
911 mv643xx_eth_free_all_tx_descs(dev);
912
913 BUG_ON(mp->tx_used_desc_q != mp->tx_curr_desc_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Free TX ring */
916 if (mp->tx_sram_size)
917 iounmap(mp->p_tx_desc_area);
918 else
919 dma_free_coherent(NULL, mp->tx_desc_area_size,
920 mp->p_tx_desc_area, mp->tx_desc_dma);
921}
922
923static void mv643xx_eth_free_rx_rings(struct net_device *dev)
924{
925 struct mv643xx_private *mp = netdev_priv(dev);
926 unsigned int port_num = mp->port_num;
927 int curr;
928
929 /* Stop RX Queues */
Dale Farnsworth9f8dd312006-01-27 01:10:47 -0700930 mv643xx_eth_port_disable_rx(port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /* Free preallocated skb's on RX rings */
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700933 for (curr = 0; mp->rx_desc_count && curr < mp->rx_ring_size; curr++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (mp->rx_skb[curr]) {
935 dev_kfree_skb(mp->rx_skb[curr]);
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700936 mp->rx_desc_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938 }
939
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700940 if (mp->rx_desc_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 printk(KERN_ERR
942 "%s: Error in freeing Rx Ring. %d skb's still"
943 " stuck in RX Ring - ignoring them\n", dev->name,
Dale Farnsworthf98e36f12006-01-27 01:09:18 -0700944 mp->rx_desc_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 /* Free RX ring */
946 if (mp->rx_sram_size)
947 iounmap(mp->p_rx_desc_area);
948 else
949 dma_free_coherent(NULL, mp->rx_desc_area_size,
950 mp->p_rx_desc_area, mp->rx_desc_dma);
951}
952
953/*
954 * mv643xx_eth_stop
955 *
956 * This function is used when closing the network device.
957 * It updates the hardware,
958 * release all memory that holds buffers and descriptors and release the IRQ.
959 * Input : a pointer to the device structure
960 * Output : zero if success , nonzero if fails
961 */
962
Dale Farnsworthab4384a2006-01-16 16:59:21 -0700963static int mv643xx_eth_stop(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 struct mv643xx_private *mp = netdev_priv(dev);
966 unsigned int port_num = mp->port_num;
967
Dale Farnsworthc2e5b352006-01-16 17:00:24 -0700968 /* Mask all interrupts on ethernet port */
Dale Farnsworth7303fde2006-03-03 10:03:36 -0700969 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -0700970 /* wait for previous write to complete */
Dale Farnsworth8f518702006-01-16 16:56:30 -0700971 mv_read(MV643XX_ETH_INTERRUPT_MASK_REG(port_num));
972
973#ifdef MV643XX_NAPI
974 netif_poll_disable(dev);
975#endif
976 netif_carrier_off(dev);
977 netif_stop_queue(dev);
978
979 eth_port_reset(mp->port_num);
980
981 mv643xx_eth_free_tx_rings(dev);
982 mv643xx_eth_free_rx_rings(dev);
983
984#ifdef MV643XX_NAPI
985 netif_poll_enable(dev);
986#endif
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
990 return 0;
991}
992
993#ifdef MV643XX_NAPI
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994/*
995 * mv643xx_poll
996 *
997 * This function is used in case of NAPI
998 */
999static int mv643xx_poll(struct net_device *dev, int *budget)
1000{
1001 struct mv643xx_private *mp = netdev_priv(dev);
1002 int done = 1, orig_budget, work_done;
1003 unsigned int port_num = mp->port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005#ifdef MV643XX_TX_FAST_REFILL
1006 if (++mp->tx_clean_threshold > 5) {
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001007 mv643xx_eth_free_completed_tx_descs(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 mp->tx_clean_threshold = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010#endif
1011
1012 if ((mv_read(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num)))
1013 != (u32) mp->rx_used_desc_q) {
1014 orig_budget = *budget;
1015 if (orig_budget > dev->quota)
1016 orig_budget = dev->quota;
1017 work_done = mv643xx_eth_receive_queue(dev, orig_budget);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 *budget -= work_done;
1019 dev->quota -= work_done;
1020 if (work_done >= orig_budget)
1021 done = 0;
1022 }
1023
1024 if (done) {
Dale Farnsworth8f518702006-01-16 16:56:30 -07001025 netif_rx_complete(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
1027 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
1028 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
Dale Farnsworth7303fde2006-03-03 10:03:36 -07001029 ETH_INT_UNMASK_ALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032 return done ? 0 : 1;
1033}
1034#endif
1035
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001036/**
1037 * has_tiny_unaligned_frags - check if skb has any small, unaligned fragments
1038 *
1039 * Hardware can't handle unaligned fragments smaller than 9 bytes.
Paul Janzenf7ea3332006-01-16 16:52:13 -07001040 * This helper function detects that case.
1041 */
1042
1043static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb)
1044{
Dale Farnsworthb4de9052006-01-27 01:04:43 -07001045 unsigned int frag;
1046 skb_frag_t *fragp;
Paul Janzenf7ea3332006-01-16 16:52:13 -07001047
Dale Farnsworthb4de9052006-01-27 01:04:43 -07001048 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1049 fragp = &skb_shinfo(skb)->frags[frag];
1050 if (fragp->size <= 8 && fragp->page_offset & 0x7)
1051 return 1;
1052 }
1053 return 0;
Paul Janzenf7ea3332006-01-16 16:52:13 -07001054}
1055
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001056/**
1057 * eth_alloc_tx_desc_index - return the index of the next available tx desc
1058 */
1059static int eth_alloc_tx_desc_index(struct mv643xx_private *mp)
1060{
1061 int tx_desc_curr;
Paul Janzenf7ea3332006-01-16 16:52:13 -07001062
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001063 BUG_ON(mp->tx_desc_count >= mp->tx_ring_size);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001064
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001065 tx_desc_curr = mp->tx_curr_desc_q;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001066 mp->tx_curr_desc_q = (tx_desc_curr + 1) % mp->tx_ring_size;
1067
1068 BUG_ON(mp->tx_curr_desc_q == mp->tx_used_desc_q);
1069
1070 return tx_desc_curr;
1071}
1072
1073/**
1074 * eth_tx_fill_frag_descs - fill tx hw descriptors for an skb's fragments.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001076 * Ensure the data for each fragment to be transmitted is mapped properly,
1077 * then fill in descriptors in the tx hw queue.
1078 */
1079static void eth_tx_fill_frag_descs(struct mv643xx_private *mp,
1080 struct sk_buff *skb)
1081{
1082 int frag;
1083 int tx_index;
1084 struct eth_tx_desc *desc;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001085
1086 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1087 skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1088
1089 tx_index = eth_alloc_tx_desc_index(mp);
1090 desc = &mp->p_tx_desc_area[tx_index];
1091
1092 desc->cmd_sts = ETH_BUFFER_OWNED_BY_DMA;
1093 /* Last Frag enables interrupt and frees the skb */
1094 if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
1095 desc->cmd_sts |= ETH_ZERO_PADDING |
1096 ETH_TX_LAST_DESC |
1097 ETH_TX_ENABLE_INTERRUPT;
1098 mp->tx_skb[tx_index] = skb;
1099 } else
1100 mp->tx_skb[tx_index] = 0;
1101
1102 desc = &mp->p_tx_desc_area[tx_index];
1103 desc->l4i_chk = 0;
1104 desc->byte_cnt = this_frag->size;
1105 desc->buf_ptr = dma_map_page(NULL, this_frag->page,
1106 this_frag->page_offset,
1107 this_frag->size,
1108 DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001109 }
1110}
1111
1112/**
1113 * eth_tx_submit_descs_for_skb - submit data from an skb to the tx hw
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001115 * Ensure the data for an skb to be transmitted is mapped properly,
1116 * then fill in descriptors in the tx hw queue and start the hardware.
1117 */
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001118static void eth_tx_submit_descs_for_skb(struct mv643xx_private *mp,
1119 struct sk_buff *skb)
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001120{
1121 int tx_index;
1122 struct eth_tx_desc *desc;
1123 u32 cmd_sts;
1124 int length;
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001125 int nr_frags = skb_shinfo(skb)->nr_frags;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001126
1127 cmd_sts = ETH_TX_FIRST_DESC | ETH_GEN_CRC | ETH_BUFFER_OWNED_BY_DMA;
1128
1129 tx_index = eth_alloc_tx_desc_index(mp);
1130 desc = &mp->p_tx_desc_area[tx_index];
1131
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001132 if (nr_frags) {
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001133 eth_tx_fill_frag_descs(mp, skb);
1134
1135 length = skb_headlen(skb);
1136 mp->tx_skb[tx_index] = 0;
1137 } else {
1138 cmd_sts |= ETH_ZERO_PADDING |
1139 ETH_TX_LAST_DESC |
1140 ETH_TX_ENABLE_INTERRUPT;
1141 length = skb->len;
1142 mp->tx_skb[tx_index] = skb;
1143 }
1144
1145 desc->byte_cnt = length;
1146 desc->buf_ptr = dma_map_single(NULL, skb->data, length, DMA_TO_DEVICE);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001147
1148 if (skb->ip_summed == CHECKSUM_HW) {
1149 BUG_ON(skb->protocol != ETH_P_IP);
1150
1151 cmd_sts |= ETH_GEN_TCP_UDP_CHECKSUM |
1152 ETH_GEN_IP_V_4_CHECKSUM |
1153 skb->nh.iph->ihl << ETH_TX_IHL_SHIFT;
1154
1155 switch (skb->nh.iph->protocol) {
1156 case IPPROTO_UDP:
1157 cmd_sts |= ETH_UDP_FRAME;
1158 desc->l4i_chk = skb->h.uh->check;
1159 break;
1160 case IPPROTO_TCP:
1161 desc->l4i_chk = skb->h.th->check;
1162 break;
1163 default:
1164 BUG();
1165 }
1166 } else {
1167 /* Errata BTS #50, IHL must be 5 if no HW checksum */
1168 cmd_sts |= 5 << ETH_TX_IHL_SHIFT;
1169 desc->l4i_chk = 0;
1170 }
1171
1172 /* ensure all other descriptors are written before first cmd_sts */
1173 wmb();
1174 desc->cmd_sts = cmd_sts;
1175
1176 /* ensure all descriptors are written before poking hardware */
1177 wmb();
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001178 mv643xx_eth_port_enable_tx(mp->port_num, ETH_TX_QUEUES_ENABLED);
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001179
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001180 mp->tx_desc_count += nr_frags + 1;
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001181}
1182
1183/**
1184 * mv643xx_eth_start_xmit - queue an skb to the hardware for transmission
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 */
1187static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1188{
1189 struct mv643xx_private *mp = netdev_priv(dev);
1190 struct net_device_stats *stats = &mp->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001193 BUG_ON(netif_queue_stopped(dev));
1194 BUG_ON(skb == NULL);
Dale Farnsworth94843562006-04-11 18:24:26 -07001195
1196 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB) {
1197 printk(KERN_ERR "%s: transmit with queue full\n", dev->name);
1198 netif_stop_queue(dev);
1199 return 1;
1200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201
Paul Janzenf7ea3332006-01-16 16:52:13 -07001202 if (has_tiny_unaligned_frags(skb)) {
1203 if ((skb_linearize(skb, GFP_ATOMIC) != 0)) {
1204 stats->tx_dropped++;
1205 printk(KERN_DEBUG "%s: failed to linearize tiny "
1206 "unaligned fragment\n", dev->name);
1207 return 1;
1208 }
1209 }
1210
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 spin_lock_irqsave(&mp->lock, flags);
1212
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001213 eth_tx_submit_descs_for_skb(mp, skb);
1214 stats->tx_bytes = skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 stats->tx_packets++;
1216 dev->trans_start = jiffies;
1217
Dale Farnsworthc8aaea22006-03-03 10:02:05 -07001218 if (mp->tx_ring_size - mp->tx_desc_count < MAX_DESCS_PER_SKB)
1219 netif_stop_queue(dev);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock_irqrestore(&mp->lock, flags);
1222
1223 return 0; /* success */
1224}
1225
1226/*
1227 * mv643xx_eth_get_stats
1228 *
1229 * Returns a pointer to the interface statistics.
1230 *
1231 * Input : dev - a pointer to the required interface
1232 *
1233 * Output : a pointer to the interface's statistics
1234 */
1235
1236static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1237{
1238 struct mv643xx_private *mp = netdev_priv(dev);
1239
1240 return &mp->stats;
1241}
1242
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001243#ifdef CONFIG_NET_POLL_CONTROLLER
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001244static void mv643xx_netpoll(struct net_device *netdev)
1245{
1246 struct mv643xx_private *mp = netdev_priv(netdev);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001247 int port_num = mp->port_num;
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001248
Dale Farnsworth7303fde2006-03-03 10:03:36 -07001249 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), ETH_INT_MASK_ALL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001250 /* wait for previous write to complete */
1251 mv_read(MV643XX_ETH_INTERRUPT_MASK_REG(port_num));
1252
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001253 mv643xx_eth_int_handler(netdev->irq, netdev, NULL);
Dale Farnsworthc2e5b352006-01-16 17:00:24 -07001254
Dale Farnsworth7303fde2006-03-03 10:03:36 -07001255 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), ETH_INT_UNMASK_ALL);
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001256}
1257#endif
1258
James Chapmand0412d92006-01-27 01:15:30 -07001259static void mv643xx_init_ethtool_cmd(struct net_device *dev, int phy_address,
1260 int speed, int duplex,
1261 struct ethtool_cmd *cmd)
1262{
1263 struct mv643xx_private *mp = netdev_priv(dev);
1264
1265 memset(cmd, 0, sizeof(*cmd));
1266
1267 cmd->port = PORT_MII;
1268 cmd->transceiver = XCVR_INTERNAL;
1269 cmd->phy_address = phy_address;
1270
1271 if (speed == 0) {
1272 cmd->autoneg = AUTONEG_ENABLE;
1273 /* mii lib checks, but doesn't use speed on AUTONEG_ENABLE */
1274 cmd->speed = SPEED_100;
1275 cmd->advertising = ADVERTISED_10baseT_Half |
1276 ADVERTISED_10baseT_Full |
1277 ADVERTISED_100baseT_Half |
1278 ADVERTISED_100baseT_Full;
1279 if (mp->mii.supports_gmii)
1280 cmd->advertising |= ADVERTISED_1000baseT_Full;
1281 } else {
1282 cmd->autoneg = AUTONEG_DISABLE;
1283 cmd->speed = speed;
1284 cmd->duplex = duplex;
1285 }
1286}
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288/*/
1289 * mv643xx_eth_probe
1290 *
1291 * First function called after registering the network device.
1292 * It's purpose is to initialize the device as an ethernet device,
1293 * fill the ethernet device structure with pointers * to functions,
1294 * and set the MAC address of the interface
1295 *
1296 * Input : struct device *
1297 * Output : -ENOMEM if failed , 0 if success
1298 */
Russell King3ae5eae2005-11-09 22:32:44 +00001299static int mv643xx_eth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 struct mv643xx_eth_platform_data *pd;
1302 int port_num = pdev->id;
1303 struct mv643xx_private *mp;
1304 struct net_device *dev;
1305 u8 *p;
1306 struct resource *res;
1307 int err;
James Chapmand0412d92006-01-27 01:15:30 -07001308 struct ethtool_cmd cmd;
Dale Farnsworth01999872006-01-27 01:18:01 -07001309 int duplex = DUPLEX_HALF;
1310 int speed = 0; /* default to auto-negotiation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 dev = alloc_etherdev(sizeof(struct mv643xx_private));
1313 if (!dev)
1314 return -ENOMEM;
1315
Russell King3ae5eae2005-11-09 22:32:44 +00001316 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 mp = netdev_priv(dev);
1319
1320 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1321 BUG_ON(!res);
1322 dev->irq = res->start;
1323
1324 mp->port_num = port_num;
1325
1326 dev->open = mv643xx_eth_open;
1327 dev->stop = mv643xx_eth_stop;
1328 dev->hard_start_xmit = mv643xx_eth_start_xmit;
1329 dev->get_stats = mv643xx_eth_get_stats;
1330 dev->set_mac_address = mv643xx_eth_set_mac_address;
1331 dev->set_multicast_list = mv643xx_eth_set_rx_mode;
1332
1333 /* No need to Tx Timeout */
1334 dev->tx_timeout = mv643xx_eth_tx_timeout;
1335#ifdef MV643XX_NAPI
1336 dev->poll = mv643xx_poll;
1337 dev->weight = 64;
1338#endif
1339
Dale Farnsworth63c9e542005-09-02 13:49:10 -07001340#ifdef CONFIG_NET_POLL_CONTROLLER
1341 dev->poll_controller = mv643xx_netpoll;
1342#endif
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 dev->watchdog_timeo = 2 * HZ;
1345 dev->tx_queue_len = mp->tx_ring_size;
1346 dev->base_addr = 0;
1347 dev->change_mtu = mv643xx_eth_change_mtu;
James Chapmand0412d92006-01-27 01:15:30 -07001348 dev->do_ioctl = mv643xx_eth_do_ioctl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
1350
1351#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1352#ifdef MAX_SKB_FRAGS
1353 /*
1354 * Zero copy can only work if we use Discovery II memory. Else, we will
1355 * have to map the buffers to ISA memory which is only 16 MB
1356 */
Wolfram Joost63890572006-01-16 16:57:41 -07001357 dev->features = NETIF_F_SG | NETIF_F_IP_CSUM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358#endif
1359#endif
1360
1361 /* Configure the timeout task */
1362 INIT_WORK(&mp->tx_timeout_task,
1363 (void (*)(void *))mv643xx_eth_tx_timeout_task, dev);
1364
1365 spin_lock_init(&mp->lock);
1366
1367 /* set default config values */
1368 eth_port_uc_addr_get(dev, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 mp->rx_ring_size = MV643XX_ETH_PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
1370 mp->tx_ring_size = MV643XX_ETH_PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
1371
1372 pd = pdev->dev.platform_data;
1373 if (pd) {
Dale Farnsworth01999872006-01-27 01:18:01 -07001374 if (pd->mac_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 memcpy(dev->dev_addr, pd->mac_addr, 6);
1376
1377 if (pd->phy_addr || pd->force_phy_addr)
1378 ethernet_phy_set(port_num, pd->phy_addr);
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 if (pd->rx_queue_size)
1381 mp->rx_ring_size = pd->rx_queue_size;
1382
1383 if (pd->tx_queue_size)
1384 mp->tx_ring_size = pd->tx_queue_size;
1385
1386 if (pd->tx_sram_size) {
1387 mp->tx_sram_size = pd->tx_sram_size;
1388 mp->tx_sram_addr = pd->tx_sram_addr;
1389 }
1390
1391 if (pd->rx_sram_size) {
1392 mp->rx_sram_size = pd->rx_sram_size;
1393 mp->rx_sram_addr = pd->rx_sram_addr;
1394 }
Dale Farnsworth01999872006-01-27 01:18:01 -07001395
1396 duplex = pd->duplex;
1397 speed = pd->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399
James Chapmanc28a4f82006-01-27 01:13:15 -07001400 /* Hook up MII support for ethtool */
1401 mp->mii.dev = dev;
1402 mp->mii.mdio_read = mv643xx_mdio_read;
1403 mp->mii.mdio_write = mv643xx_mdio_write;
1404 mp->mii.phy_id = ethernet_phy_get(port_num);
1405 mp->mii.phy_id_mask = 0x3f;
1406 mp->mii.reg_num_mask = 0x1f;
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 err = ethernet_phy_detect(port_num);
1409 if (err) {
1410 pr_debug("MV643xx ethernet port %d: "
1411 "No PHY detected at addr %d\n",
1412 port_num, ethernet_phy_get(port_num));
James Chapmand0412d92006-01-27 01:15:30 -07001413 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 }
1415
Dale Farnsworth01999872006-01-27 01:18:01 -07001416 ethernet_phy_reset(port_num);
James Chapmanc28a4f82006-01-27 01:13:15 -07001417 mp->mii.supports_gmii = mii_check_gmii_support(&mp->mii);
James Chapmand0412d92006-01-27 01:15:30 -07001418 mv643xx_init_ethtool_cmd(dev, mp->mii.phy_id, speed, duplex, &cmd);
1419 mv643xx_eth_update_pscr(dev, &cmd);
1420 mv643xx_set_settings(dev, &cmd);
James Chapmanc28a4f82006-01-27 01:13:15 -07001421
Olaf Heringb0b8dab2006-04-27 18:23:49 -07001422 SET_MODULE_OWNER(dev);
1423 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 err = register_netdev(dev);
1425 if (err)
1426 goto out;
1427
1428 p = dev->dev_addr;
1429 printk(KERN_NOTICE
1430 "%s: port %d with MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
1431 dev->name, port_num, p[0], p[1], p[2], p[3], p[4], p[5]);
1432
1433 if (dev->features & NETIF_F_SG)
1434 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
1435
1436 if (dev->features & NETIF_F_IP_CSUM)
1437 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
1438 dev->name);
1439
1440#ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1441 printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
1442#endif
1443
1444#ifdef MV643XX_COAL
1445 printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
1446 dev->name);
1447#endif
1448
1449#ifdef MV643XX_NAPI
1450 printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
1451#endif
1452
Nicolas DETb1529872005-10-28 17:46:30 -07001453 if (mp->tx_sram_size > 0)
1454 printk(KERN_NOTICE "%s: Using SRAM\n", dev->name);
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 return 0;
1457
1458out:
1459 free_netdev(dev);
1460
1461 return err;
1462}
1463
Russell King3ae5eae2005-11-09 22:32:44 +00001464static int mv643xx_eth_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Russell King3ae5eae2005-11-09 22:32:44 +00001466 struct net_device *dev = platform_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
1468 unregister_netdev(dev);
1469 flush_scheduled_work();
1470
1471 free_netdev(dev);
Russell King3ae5eae2005-11-09 22:32:44 +00001472 platform_set_drvdata(pdev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 return 0;
1474}
1475
Russell King3ae5eae2005-11-09 22:32:44 +00001476static int mv643xx_eth_shared_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 struct resource *res;
1479
1480 printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1481
1482 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1483 if (res == NULL)
1484 return -ENODEV;
1485
1486 mv643xx_eth_shared_base = ioremap(res->start,
1487 MV643XX_ETH_SHARED_REGS_SIZE);
1488 if (mv643xx_eth_shared_base == NULL)
1489 return -ENOMEM;
1490
1491 return 0;
1492
1493}
1494
Russell King3ae5eae2005-11-09 22:32:44 +00001495static int mv643xx_eth_shared_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496{
1497 iounmap(mv643xx_eth_shared_base);
1498 mv643xx_eth_shared_base = NULL;
1499
1500 return 0;
1501}
1502
Russell King3ae5eae2005-11-09 22:32:44 +00001503static struct platform_driver mv643xx_eth_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 .probe = mv643xx_eth_probe,
1505 .remove = mv643xx_eth_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001506 .driver = {
1507 .name = MV643XX_ETH_NAME,
1508 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509};
1510
Russell King3ae5eae2005-11-09 22:32:44 +00001511static struct platform_driver mv643xx_eth_shared_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 .probe = mv643xx_eth_shared_probe,
1513 .remove = mv643xx_eth_shared_remove,
Russell King3ae5eae2005-11-09 22:32:44 +00001514 .driver = {
1515 .name = MV643XX_ETH_SHARED_NAME,
1516 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517};
1518
1519/*
1520 * mv643xx_init_module
1521 *
1522 * Registers the network drivers into the Linux kernel
1523 *
1524 * Input : N/A
1525 *
1526 * Output : N/A
1527 */
1528static int __init mv643xx_init_module(void)
1529{
1530 int rc;
1531
Russell King3ae5eae2005-11-09 22:32:44 +00001532 rc = platform_driver_register(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (!rc) {
Russell King3ae5eae2005-11-09 22:32:44 +00001534 rc = platform_driver_register(&mv643xx_eth_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (rc)
Russell King3ae5eae2005-11-09 22:32:44 +00001536 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538 return rc;
1539}
1540
1541/*
1542 * mv643xx_cleanup_module
1543 *
1544 * Registers the network drivers into the Linux kernel
1545 *
1546 * Input : N/A
1547 *
1548 * Output : N/A
1549 */
1550static void __exit mv643xx_cleanup_module(void)
1551{
Russell King3ae5eae2005-11-09 22:32:44 +00001552 platform_driver_unregister(&mv643xx_eth_driver);
1553 platform_driver_unregister(&mv643xx_eth_shared_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
1555
1556module_init(mv643xx_init_module);
1557module_exit(mv643xx_cleanup_module);
1558
1559MODULE_LICENSE("GPL");
1560MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
1561 " and Dale Farnsworth");
1562MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
1563
1564/*
1565 * The second part is the low level driver of the gigE ethernet ports.
1566 */
1567
1568/*
1569 * Marvell's Gigabit Ethernet controller low level driver
1570 *
1571 * DESCRIPTION:
1572 * This file introduce low level API to Marvell's Gigabit Ethernet
1573 * controller. This Gigabit Ethernet Controller driver API controls
1574 * 1) Operations (i.e. port init, start, reset etc').
1575 * 2) Data flow (i.e. port send, receive etc').
1576 * Each Gigabit Ethernet port is controlled via
1577 * struct mv643xx_private.
1578 * This struct includes user configuration information as well as
1579 * driver internal data needed for its operations.
1580 *
1581 * Supported Features:
1582 * - This low level driver is OS independent. Allocating memory for
1583 * the descriptor rings and buffers are not within the scope of
1584 * this driver.
1585 * - The user is free from Rx/Tx queue managing.
1586 * - This low level driver introduce functionality API that enable
1587 * the to operate Marvell's Gigabit Ethernet Controller in a
1588 * convenient way.
1589 * - Simple Gigabit Ethernet port operation API.
1590 * - Simple Gigabit Ethernet port data flow API.
1591 * - Data flow and operation API support per queue functionality.
1592 * - Support cached descriptors for better performance.
1593 * - Enable access to all four DRAM banks and internal SRAM memory
1594 * spaces.
1595 * - PHY access and control API.
1596 * - Port control register configuration API.
1597 * - Full control over Unicast and Multicast MAC configurations.
1598 *
1599 * Operation flow:
1600 *
1601 * Initialization phase
1602 * This phase complete the initialization of the the
1603 * mv643xx_private struct.
1604 * User information regarding port configuration has to be set
1605 * prior to calling the port initialization routine.
1606 *
1607 * In this phase any port Tx/Rx activity is halted, MIB counters
1608 * are cleared, PHY address is set according to user parameter and
1609 * access to DRAM and internal SRAM memory spaces.
1610 *
1611 * Driver ring initialization
1612 * Allocating memory for the descriptor rings and buffers is not
1613 * within the scope of this driver. Thus, the user is required to
1614 * allocate memory for the descriptors ring and buffers. Those
1615 * memory parameters are used by the Rx and Tx ring initialization
1616 * routines in order to curve the descriptor linked list in a form
1617 * of a ring.
1618 * Note: Pay special attention to alignment issues when using
1619 * cached descriptors/buffers. In this phase the driver store
1620 * information in the mv643xx_private struct regarding each queue
1621 * ring.
1622 *
1623 * Driver start
1624 * This phase prepares the Ethernet port for Rx and Tx activity.
1625 * It uses the information stored in the mv643xx_private struct to
1626 * initialize the various port registers.
1627 *
1628 * Data flow:
1629 * All packet references to/from the driver are done using
1630 * struct pkt_info.
1631 * This struct is a unified struct used with Rx and Tx operations.
1632 * This way the user is not required to be familiar with neither
1633 * Tx nor Rx descriptors structures.
1634 * The driver's descriptors rings are management by indexes.
1635 * Those indexes controls the ring resources and used to indicate
1636 * a SW resource error:
1637 * 'current'
1638 * This index points to the current available resource for use. For
1639 * example in Rx process this index will point to the descriptor
1640 * that will be passed to the user upon calling the receive
1641 * routine. In Tx process, this index will point to the descriptor
1642 * that will be assigned with the user packet info and transmitted.
1643 * 'used'
1644 * This index points to the descriptor that need to restore its
1645 * resources. For example in Rx process, using the Rx buffer return
1646 * API will attach the buffer returned in packet info to the
1647 * descriptor pointed by 'used'. In Tx process, using the Tx
1648 * descriptor return will merely return the user packet info with
1649 * the command status of the transmitted buffer pointed by the
1650 * 'used' index. Nevertheless, it is essential to use this routine
1651 * to update the 'used' index.
1652 * 'first'
1653 * This index supports Tx Scatter-Gather. It points to the first
1654 * descriptor of a packet assembled of multiple buffers. For
1655 * example when in middle of Such packet we have a Tx resource
1656 * error the 'curr' index get the value of 'first' to indicate
1657 * that the ring returned to its state before trying to transmit
1658 * this packet.
1659 *
1660 * Receive operation:
1661 * The eth_port_receive API set the packet information struct,
1662 * passed by the caller, with received information from the
1663 * 'current' SDMA descriptor.
1664 * It is the user responsibility to return this resource back
1665 * to the Rx descriptor ring to enable the reuse of this source.
1666 * Return Rx resource is done using the eth_rx_return_buff API.
1667 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * Prior to calling the initialization routine eth_port_init() the user
1669 * must set the following fields under mv643xx_private struct:
1670 * port_num User Ethernet port number.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * port_config User port configuration value.
1672 * port_config_extend User port config extend value.
1673 * port_sdma_config User port SDMA config value.
1674 * port_serial_control User port serial control value.
1675 *
1676 * This driver data flow is done using the struct pkt_info which
1677 * is a unified struct for Rx and Tx operations:
1678 *
1679 * byte_cnt Tx/Rx descriptor buffer byte count.
1680 * l4i_chk CPU provided TCP Checksum. For Tx operation
1681 * only.
1682 * cmd_sts Tx/Rx descriptor command status.
1683 * buf_ptr Tx/Rx descriptor buffer pointer.
1684 * return_info Tx/Rx user resource return information.
1685 */
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687/* PHY routines */
1688static int ethernet_phy_get(unsigned int eth_port_num);
1689static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
1690
1691/* Ethernet Port routines */
Dale Farnsworthcf4086c2006-01-27 01:07:48 -07001692static void eth_port_set_filter_table_entry(int table, unsigned char entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694/*
1695 * eth_port_init - Initialize the Ethernet port driver
1696 *
1697 * DESCRIPTION:
1698 * This function prepares the ethernet port to start its activity:
1699 * 1) Completes the ethernet port driver struct initialization toward port
1700 * start routine.
1701 * 2) Resets the device to a quiescent state in case of warm reboot.
1702 * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
1703 * 4) Clean MAC tables. The reset status of those tables is unknown.
1704 * 5) Set PHY address.
1705 * Note: Call this routine prior to eth_port_start routine and after
1706 * setting user values in the user fields of Ethernet port control
1707 * struct.
1708 *
1709 * INPUT:
1710 * struct mv643xx_private *mp Ethernet port control struct
1711 *
1712 * OUTPUT:
1713 * See description.
1714 *
1715 * RETURN:
1716 * None.
1717 */
1718static void eth_port_init(struct mv643xx_private *mp)
1719{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 mp->rx_resource_err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 eth_port_reset(mp->port_num);
1723
1724 eth_port_init_mac_tables(mp->port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725}
1726
1727/*
1728 * eth_port_start - Start the Ethernet port activity.
1729 *
1730 * DESCRIPTION:
1731 * This routine prepares the Ethernet port for Rx and Tx activity:
1732 * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
1733 * has been initialized a descriptor's ring (using
1734 * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
1735 * 2. Initialize and enable the Ethernet configuration port by writing to
1736 * the port's configuration and command registers.
1737 * 3. Initialize and enable the SDMA by writing to the SDMA's
1738 * configuration and command registers. After completing these steps,
1739 * the ethernet port SDMA can starts to perform Rx and Tx activities.
1740 *
1741 * Note: Each Rx and Tx queue descriptor's list must be initialized prior
1742 * to calling this function (use ether_init_tx_desc_ring for Tx queues
1743 * and ether_init_rx_desc_ring for Rx queues).
1744 *
1745 * INPUT:
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07001746 * dev - a pointer to the required interface
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 *
1748 * OUTPUT:
1749 * Ethernet port is ready to receive and transmit.
1750 *
1751 * RETURN:
1752 * None.
1753 */
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07001754static void eth_port_start(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07001756 struct mv643xx_private *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 unsigned int port_num = mp->port_num;
1758 int tx_curr_desc, rx_curr_desc;
James Chapmand0412d92006-01-27 01:15:30 -07001759 u32 pscr;
1760 struct ethtool_cmd ethtool_cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
1762 /* Assignment of Tx CTRP of given queue */
1763 tx_curr_desc = mp->tx_curr_desc_q;
1764 mv_write(MV643XX_ETH_TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
1765 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
1766
1767 /* Assignment of Rx CRDP of given queue */
1768 rx_curr_desc = mp->rx_curr_desc_q;
1769 mv_write(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
1770 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
1771
1772 /* Add the assigned Ethernet address to the port's address table */
Dale Farnsworthed9b5d42006-01-27 01:06:38 -07001773 eth_port_uc_addr_set(port_num, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
1775 /* Assign port configuration and command. */
Dale Farnsworth01999872006-01-27 01:18:01 -07001776 mv_write(MV643XX_ETH_PORT_CONFIG_REG(port_num),
1777 MV643XX_ETH_PORT_CONFIG_DEFAULT_VALUE);
1778
1779 mv_write(MV643XX_ETH_PORT_CONFIG_EXTEND_REG(port_num),
1780 MV643XX_ETH_PORT_CONFIG_EXTEND_DEFAULT_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
James Chapmand0412d92006-01-27 01:15:30 -07001782 pscr = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
Dale Farnsworth01999872006-01-27 01:18:01 -07001783
1784 pscr &= ~(MV643XX_ETH_SERIAL_PORT_ENABLE | MV643XX_ETH_FORCE_LINK_PASS);
James Chapmand0412d92006-01-27 01:15:30 -07001785 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), pscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
James Chapmand0412d92006-01-27 01:15:30 -07001787 pscr |= MV643XX_ETH_DISABLE_AUTO_NEG_FOR_FLOW_CTRL |
1788 MV643XX_ETH_DISABLE_AUTO_NEG_SPEED_GMII |
1789 MV643XX_ETH_DISABLE_AUTO_NEG_FOR_DUPLX |
1790 MV643XX_ETH_DO_NOT_FORCE_LINK_FAIL |
1791 MV643XX_ETH_SERIAL_PORT_CONTROL_RESERVED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
James Chapmand0412d92006-01-27 01:15:30 -07001793 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), pscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
James Chapmand0412d92006-01-27 01:15:30 -07001795 pscr |= MV643XX_ETH_SERIAL_PORT_ENABLE;
1796 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), pscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 /* Assign port SDMA configuration */
Dale Farnsworth01999872006-01-27 01:18:01 -07001799 mv_write(MV643XX_ETH_SDMA_CONFIG_REG(port_num),
1800 MV643XX_ETH_PORT_SDMA_CONFIG_DEFAULT_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
1802 /* Enable port Rx. */
Dale Farnsworthff561ee2006-03-03 10:02:51 -07001803 mv643xx_eth_port_enable_rx(port_num, ETH_RX_QUEUES_ENABLED);
Dale Farnsworth8f543712005-09-02 12:34:35 -07001804
1805 /* Disable port bandwidth limits by clearing MTU register */
1806 mv_write(MV643XX_ETH_MAXIMUM_TRANSMIT_UNIT(port_num), 0);
James Chapmand0412d92006-01-27 01:15:30 -07001807
1808 /* save phy settings across reset */
1809 mv643xx_get_settings(dev, &ethtool_cmd);
1810 ethernet_phy_reset(mp->port_num);
1811 mv643xx_set_settings(dev, &ethtool_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
1814/*
1815 * eth_port_uc_addr_set - This function Set the port Unicast address.
1816 *
1817 * DESCRIPTION:
1818 * This function Set the port Ethernet MAC address.
1819 *
1820 * INPUT:
1821 * unsigned int eth_port_num Port number.
1822 * char * p_addr Address to be set
1823 *
1824 * OUTPUT:
Dale Farnsworthcf4086c2006-01-27 01:07:48 -07001825 * Set MAC address low and high registers. also calls
1826 * eth_port_set_filter_table_entry() to set the unicast
1827 * table with the proper information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 *
1829 * RETURN:
1830 * N/A.
1831 *
1832 */
1833static void eth_port_uc_addr_set(unsigned int eth_port_num,
1834 unsigned char *p_addr)
1835{
1836 unsigned int mac_h;
1837 unsigned int mac_l;
Dale Farnsworthcf4086c2006-01-27 01:07:48 -07001838 int table;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 mac_l = (p_addr[4] << 8) | (p_addr[5]);
1841 mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1842 (p_addr[3] << 0);
1843
1844 mv_write(MV643XX_ETH_MAC_ADDR_LOW(eth_port_num), mac_l);
1845 mv_write(MV643XX_ETH_MAC_ADDR_HIGH(eth_port_num), mac_h);
1846
1847 /* Accept frames of this address */
Dale Farnsworthcf4086c2006-01-27 01:07:48 -07001848 table = MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE(eth_port_num);
1849 eth_port_set_filter_table_entry(table, p_addr[5] & 0x0f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850}
1851
1852/*
1853 * eth_port_uc_addr_get - This function retrieves the port Unicast address
1854 * (MAC address) from the ethernet hw registers.
1855 *
1856 * DESCRIPTION:
1857 * This function retrieves the port Ethernet MAC address.
1858 *
1859 * INPUT:
1860 * unsigned int eth_port_num Port number.
1861 * char *MacAddr pointer where the MAC address is stored
1862 *
1863 * OUTPUT:
1864 * Copy the MAC address to the location pointed to by MacAddr
1865 *
1866 * RETURN:
1867 * N/A.
1868 *
1869 */
1870static void eth_port_uc_addr_get(struct net_device *dev, unsigned char *p_addr)
1871{
1872 struct mv643xx_private *mp = netdev_priv(dev);
1873 unsigned int mac_h;
1874 unsigned int mac_l;
1875
1876 mac_h = mv_read(MV643XX_ETH_MAC_ADDR_HIGH(mp->port_num));
1877 mac_l = mv_read(MV643XX_ETH_MAC_ADDR_LOW(mp->port_num));
1878
1879 p_addr[0] = (mac_h >> 24) & 0xff;
1880 p_addr[1] = (mac_h >> 16) & 0xff;
1881 p_addr[2] = (mac_h >> 8) & 0xff;
1882 p_addr[3] = mac_h & 0xff;
1883 p_addr[4] = (mac_l >> 8) & 0xff;
1884 p_addr[5] = mac_l & 0xff;
1885}
1886
1887/*
Dale Farnsworth16e03012006-01-16 16:50:02 -07001888 * The entries in each table are indexed by a hash of a packet's MAC
1889 * address. One bit in each entry determines whether the packet is
1890 * accepted. There are 4 entries (each 8 bits wide) in each register
1891 * of the table. The bits in each entry are defined as follows:
1892 * 0 Accept=1, Drop=0
1893 * 3-1 Queue (ETH_Q0=0)
1894 * 7-4 Reserved = 0;
1895 */
1896static void eth_port_set_filter_table_entry(int table, unsigned char entry)
1897{
1898 unsigned int table_reg;
1899 unsigned int tbl_offset;
1900 unsigned int reg_offset;
1901
1902 tbl_offset = (entry / 4) * 4; /* Register offset of DA table entry */
1903 reg_offset = entry % 4; /* Entry offset within the register */
1904
1905 /* Set "accepts frame bit" at specified table entry */
1906 table_reg = mv_read(table + tbl_offset);
1907 table_reg |= 0x01 << (8 * reg_offset);
1908 mv_write(table + tbl_offset, table_reg);
1909}
1910
1911/*
1912 * eth_port_mc_addr - Multicast address settings.
1913 *
1914 * The MV device supports multicast using two tables:
1915 * 1) Special Multicast Table for MAC addresses of the form
1916 * 0x01-00-5E-00-00-XX (where XX is between 0x00 and 0x_FF).
1917 * The MAC DA[7:0] bits are used as a pointer to the Special Multicast
1918 * Table entries in the DA-Filter table.
1919 * 2) Other Multicast Table for multicast of another type. A CRC-8bit
1920 * is used as an index to the Other Multicast Table entries in the
1921 * DA-Filter table. This function calculates the CRC-8bit value.
1922 * In either case, eth_port_set_filter_table_entry() is then called
1923 * to set to set the actual table entry.
1924 */
1925static void eth_port_mc_addr(unsigned int eth_port_num, unsigned char *p_addr)
1926{
1927 unsigned int mac_h;
1928 unsigned int mac_l;
1929 unsigned char crc_result = 0;
1930 int table;
1931 int mac_array[48];
1932 int crc[8];
1933 int i;
1934
1935 if ((p_addr[0] == 0x01) && (p_addr[1] == 0x00) &&
1936 (p_addr[2] == 0x5E) && (p_addr[3] == 0x00) && (p_addr[4] == 0x00)) {
1937 table = MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
1938 (eth_port_num);
1939 eth_port_set_filter_table_entry(table, p_addr[5]);
1940 return;
1941 }
1942
1943 /* Calculate CRC-8 out of the given address */
1944 mac_h = (p_addr[0] << 8) | (p_addr[1]);
1945 mac_l = (p_addr[2] << 24) | (p_addr[3] << 16) |
1946 (p_addr[4] << 8) | (p_addr[5] << 0);
1947
1948 for (i = 0; i < 32; i++)
1949 mac_array[i] = (mac_l >> i) & 0x1;
1950 for (i = 32; i < 48; i++)
1951 mac_array[i] = (mac_h >> (i - 32)) & 0x1;
1952
1953 crc[0] = mac_array[45] ^ mac_array[43] ^ mac_array[40] ^ mac_array[39] ^
1954 mac_array[35] ^ mac_array[34] ^ mac_array[31] ^ mac_array[30] ^
1955 mac_array[28] ^ mac_array[23] ^ mac_array[21] ^ mac_array[19] ^
1956 mac_array[18] ^ mac_array[16] ^ mac_array[14] ^ mac_array[12] ^
1957 mac_array[8] ^ mac_array[7] ^ mac_array[6] ^ mac_array[0];
1958
1959 crc[1] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1960 mac_array[41] ^ mac_array[39] ^ mac_array[36] ^ mac_array[34] ^
1961 mac_array[32] ^ mac_array[30] ^ mac_array[29] ^ mac_array[28] ^
1962 mac_array[24] ^ mac_array[23] ^ mac_array[22] ^ mac_array[21] ^
1963 mac_array[20] ^ mac_array[18] ^ mac_array[17] ^ mac_array[16] ^
1964 mac_array[15] ^ mac_array[14] ^ mac_array[13] ^ mac_array[12] ^
1965 mac_array[9] ^ mac_array[6] ^ mac_array[1] ^ mac_array[0];
1966
1967 crc[2] = mac_array[47] ^ mac_array[46] ^ mac_array[44] ^ mac_array[43] ^
1968 mac_array[42] ^ mac_array[39] ^ mac_array[37] ^ mac_array[34] ^
1969 mac_array[33] ^ mac_array[29] ^ mac_array[28] ^ mac_array[25] ^
1970 mac_array[24] ^ mac_array[22] ^ mac_array[17] ^ mac_array[15] ^
1971 mac_array[13] ^ mac_array[12] ^ mac_array[10] ^ mac_array[8] ^
1972 mac_array[6] ^ mac_array[2] ^ mac_array[1] ^ mac_array[0];
1973
1974 crc[3] = mac_array[47] ^ mac_array[45] ^ mac_array[44] ^ mac_array[43] ^
1975 mac_array[40] ^ mac_array[38] ^ mac_array[35] ^ mac_array[34] ^
1976 mac_array[30] ^ mac_array[29] ^ mac_array[26] ^ mac_array[25] ^
1977 mac_array[23] ^ mac_array[18] ^ mac_array[16] ^ mac_array[14] ^
1978 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[7] ^
1979 mac_array[3] ^ mac_array[2] ^ mac_array[1];
1980
1981 crc[4] = mac_array[46] ^ mac_array[45] ^ mac_array[44] ^ mac_array[41] ^
1982 mac_array[39] ^ mac_array[36] ^ mac_array[35] ^ mac_array[31] ^
1983 mac_array[30] ^ mac_array[27] ^ mac_array[26] ^ mac_array[24] ^
1984 mac_array[19] ^ mac_array[17] ^ mac_array[15] ^ mac_array[14] ^
1985 mac_array[12] ^ mac_array[10] ^ mac_array[8] ^ mac_array[4] ^
1986 mac_array[3] ^ mac_array[2];
1987
1988 crc[5] = mac_array[47] ^ mac_array[46] ^ mac_array[45] ^ mac_array[42] ^
1989 mac_array[40] ^ mac_array[37] ^ mac_array[36] ^ mac_array[32] ^
1990 mac_array[31] ^ mac_array[28] ^ mac_array[27] ^ mac_array[25] ^
1991 mac_array[20] ^ mac_array[18] ^ mac_array[16] ^ mac_array[15] ^
1992 mac_array[13] ^ mac_array[11] ^ mac_array[9] ^ mac_array[5] ^
1993 mac_array[4] ^ mac_array[3];
1994
1995 crc[6] = mac_array[47] ^ mac_array[46] ^ mac_array[43] ^ mac_array[41] ^
1996 mac_array[38] ^ mac_array[37] ^ mac_array[33] ^ mac_array[32] ^
1997 mac_array[29] ^ mac_array[28] ^ mac_array[26] ^ mac_array[21] ^
1998 mac_array[19] ^ mac_array[17] ^ mac_array[16] ^ mac_array[14] ^
1999 mac_array[12] ^ mac_array[10] ^ mac_array[6] ^ mac_array[5] ^
2000 mac_array[4];
2001
2002 crc[7] = mac_array[47] ^ mac_array[44] ^ mac_array[42] ^ mac_array[39] ^
2003 mac_array[38] ^ mac_array[34] ^ mac_array[33] ^ mac_array[30] ^
2004 mac_array[29] ^ mac_array[27] ^ mac_array[22] ^ mac_array[20] ^
2005 mac_array[18] ^ mac_array[17] ^ mac_array[15] ^ mac_array[13] ^
2006 mac_array[11] ^ mac_array[7] ^ mac_array[6] ^ mac_array[5];
2007
2008 for (i = 0; i < 8; i++)
2009 crc_result = crc_result | (crc[i] << i);
2010
2011 table = MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num);
2012 eth_port_set_filter_table_entry(table, crc_result);
2013}
2014
2015/*
2016 * Set the entire multicast list based on dev->mc_list.
2017 */
2018static void eth_port_set_multicast_list(struct net_device *dev)
2019{
2020
2021 struct dev_mc_list *mc_list;
2022 int i;
2023 int table_index;
2024 struct mv643xx_private *mp = netdev_priv(dev);
2025 unsigned int eth_port_num = mp->port_num;
2026
2027 /* If the device is in promiscuous mode or in all multicast mode,
2028 * we will fully populate both multicast tables with accept.
2029 * This is guaranteed to yield a match on all multicast addresses...
2030 */
2031 if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI)) {
2032 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002033 /* Set all entries in DA filter special multicast
2034 * table (Ex_dFSMT)
2035 * Set for ETH_Q0 for now
2036 * Bits
2037 * 0 Accept=1, Drop=0
2038 * 3-1 Queue ETH_Q0=0
2039 * 7-4 Reserved = 0;
2040 */
2041 mv_write(MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
Dale Farnsworth16e03012006-01-16 16:50:02 -07002042
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002043 /* Set all entries in DA filter other multicast
2044 * table (Ex_dFOMT)
2045 * Set for ETH_Q0 for now
2046 * Bits
2047 * 0 Accept=1, Drop=0
2048 * 3-1 Queue ETH_Q0=0
2049 * 7-4 Reserved = 0;
2050 */
2051 mv_write(MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE(eth_port_num) + table_index, 0x01010101);
2052 }
Dale Farnsworth16e03012006-01-16 16:50:02 -07002053 return;
2054 }
2055
2056 /* We will clear out multicast tables every time we get the list.
2057 * Then add the entire new list...
2058 */
2059 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2060 /* Clear DA filter special multicast table (Ex_dFSMT) */
2061 mv_write(MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
2062 (eth_port_num) + table_index, 0);
2063
2064 /* Clear DA filter other multicast table (Ex_dFOMT) */
2065 mv_write(MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE
2066 (eth_port_num) + table_index, 0);
2067 }
2068
2069 /* Get pointer to net_device multicast list and add each one... */
2070 for (i = 0, mc_list = dev->mc_list;
2071 (i < 256) && (mc_list != NULL) && (i < dev->mc_count);
2072 i++, mc_list = mc_list->next)
2073 if (mc_list->dmi_addrlen == 6)
2074 eth_port_mc_addr(eth_port_num, mc_list->dmi_addr);
2075}
2076
2077/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
2079 *
2080 * DESCRIPTION:
2081 * Go through all the DA filter tables (Unicast, Special Multicast &
2082 * Other Multicast) and set each entry to 0.
2083 *
2084 * INPUT:
2085 * unsigned int eth_port_num Ethernet Port number.
2086 *
2087 * OUTPUT:
2088 * Multicast and Unicast packets are rejected.
2089 *
2090 * RETURN:
2091 * None.
2092 */
2093static void eth_port_init_mac_tables(unsigned int eth_port_num)
2094{
2095 int table_index;
2096
2097 /* Clear DA filter unicast table (Ex_dFUT) */
2098 for (table_index = 0; table_index <= 0xC; table_index += 4)
Dale Farnsworthcf4086c2006-01-27 01:07:48 -07002099 mv_write(MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
2100 (eth_port_num) + table_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2103 /* Clear DA filter special multicast table (Ex_dFSMT) */
Dale Farnsworth16e03012006-01-16 16:50:02 -07002104 mv_write(MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
2105 (eth_port_num) + table_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 /* Clear DA filter other multicast table (Ex_dFOMT) */
Dale Farnsworth16e03012006-01-16 16:50:02 -07002107 mv_write(MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE
2108 (eth_port_num) + table_index, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 }
2110}
2111
2112/*
2113 * eth_clear_mib_counters - Clear all MIB counters
2114 *
2115 * DESCRIPTION:
2116 * This function clears all MIB counters of a specific ethernet port.
2117 * A read from the MIB counter will reset the counter.
2118 *
2119 * INPUT:
2120 * unsigned int eth_port_num Ethernet Port number.
2121 *
2122 * OUTPUT:
2123 * After reading all MIB counters, the counters resets.
2124 *
2125 * RETURN:
2126 * MIB counter value.
2127 *
2128 */
2129static void eth_clear_mib_counters(unsigned int eth_port_num)
2130{
2131 int i;
2132
2133 /* Perform dummy reads from MIB counters */
2134 for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
2135 i += 4)
2136 mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(eth_port_num) + i);
2137}
2138
2139static inline u32 read_mib(struct mv643xx_private *mp, int offset)
2140{
2141 return mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(mp->port_num) + offset);
2142}
2143
2144static void eth_update_mib_counters(struct mv643xx_private *mp)
2145{
2146 struct mv643xx_mib_counters *p = &mp->mib_counters;
2147 int offset;
2148
2149 p->good_octets_received +=
2150 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
2151 p->good_octets_received +=
2152 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
2153
2154 for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
2155 offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
2156 offset += 4)
2157 *(u32 *)((char *)p + offset) = read_mib(mp, offset);
2158
2159 p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
2160 p->good_octets_sent +=
2161 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
2162
2163 for (offset = ETH_MIB_GOOD_FRAMES_SENT;
2164 offset <= ETH_MIB_LATE_COLLISION;
2165 offset += 4)
2166 *(u32 *)((char *)p + offset) = read_mib(mp, offset);
2167}
2168
2169/*
2170 * ethernet_phy_detect - Detect whether a phy is present
2171 *
2172 * DESCRIPTION:
2173 * This function tests whether there is a PHY present on
2174 * the specified port.
2175 *
2176 * INPUT:
2177 * unsigned int eth_port_num Ethernet Port number.
2178 *
2179 * OUTPUT:
2180 * None
2181 *
2182 * RETURN:
2183 * 0 on success
2184 * -ENODEV on failure
2185 *
2186 */
2187static int ethernet_phy_detect(unsigned int port_num)
2188{
2189 unsigned int phy_reg_data0;
2190 int auto_neg;
2191
2192 eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
2193 auto_neg = phy_reg_data0 & 0x1000;
2194 phy_reg_data0 ^= 0x1000; /* invert auto_neg */
2195 eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
2196
2197 eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
2198 if ((phy_reg_data0 & 0x1000) == auto_neg)
2199 return -ENODEV; /* change didn't take */
2200
2201 phy_reg_data0 ^= 0x1000;
2202 eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
2203 return 0;
2204}
2205
2206/*
2207 * ethernet_phy_get - Get the ethernet port PHY address.
2208 *
2209 * DESCRIPTION:
2210 * This routine returns the given ethernet port PHY address.
2211 *
2212 * INPUT:
2213 * unsigned int eth_port_num Ethernet Port number.
2214 *
2215 * OUTPUT:
2216 * None.
2217 *
2218 * RETURN:
2219 * PHY address.
2220 *
2221 */
2222static int ethernet_phy_get(unsigned int eth_port_num)
2223{
2224 unsigned int reg_data;
2225
2226 reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
2227
2228 return ((reg_data >> (5 * eth_port_num)) & 0x1f);
2229}
2230
2231/*
2232 * ethernet_phy_set - Set the ethernet port PHY address.
2233 *
2234 * DESCRIPTION:
2235 * This routine sets the given ethernet port PHY address.
2236 *
2237 * INPUT:
2238 * unsigned int eth_port_num Ethernet Port number.
2239 * int phy_addr PHY address.
2240 *
2241 * OUTPUT:
2242 * None.
2243 *
2244 * RETURN:
2245 * None.
2246 *
2247 */
2248static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr)
2249{
2250 u32 reg_data;
2251 int addr_shift = 5 * eth_port_num;
2252
2253 reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
2254 reg_data &= ~(0x1f << addr_shift);
2255 reg_data |= (phy_addr & 0x1f) << addr_shift;
2256 mv_write(MV643XX_ETH_PHY_ADDR_REG, reg_data);
2257}
2258
2259/*
2260 * ethernet_phy_reset - Reset Ethernet port PHY.
2261 *
2262 * DESCRIPTION:
2263 * This routine utilizes the SMI interface to reset the ethernet port PHY.
2264 *
2265 * INPUT:
2266 * unsigned int eth_port_num Ethernet Port number.
2267 *
2268 * OUTPUT:
2269 * The PHY is reset.
2270 *
2271 * RETURN:
2272 * None.
2273 *
2274 */
2275static void ethernet_phy_reset(unsigned int eth_port_num)
2276{
2277 unsigned int phy_reg_data;
2278
2279 /* Reset the PHY */
2280 eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
2281 phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2282 eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data);
James Chapmand0412d92006-01-27 01:15:30 -07002283
2284 /* wait for PHY to come out of reset */
2285 do {
2286 udelay(1);
2287 eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
2288 } while (phy_reg_data & 0x8000);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289}
2290
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002291static void mv643xx_eth_port_enable_tx(unsigned int port_num,
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002292 unsigned int queues)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002293{
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002294 mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num), queues);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002295}
2296
2297static void mv643xx_eth_port_enable_rx(unsigned int port_num,
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002298 unsigned int queues)
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002299{
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002300 mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), queues);
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002301}
2302
2303static unsigned int mv643xx_eth_port_disable_tx(unsigned int port_num)
2304{
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002305 u32 queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002306
2307 /* Stop Tx port activity. Check port Tx activity. */
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002308 queues = mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num))
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002309 & 0xFF;
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002310 if (queues) {
2311 /* Issue stop command for active queues only */
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002312 mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num),
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002313 (queues << 8));
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002314
2315 /* Wait for all Tx activity to terminate. */
2316 /* Check port cause register that all Tx queues are stopped */
2317 while (mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num))
2318 & 0xFF)
2319 udelay(PHY_WAIT_MICRO_SECONDS);
2320
2321 /* Wait for Tx FIFO to empty */
2322 while (mv_read(MV643XX_ETH_PORT_STATUS_REG(port_num)) &
2323 ETH_PORT_TX_FIFO_EMPTY)
2324 udelay(PHY_WAIT_MICRO_SECONDS);
2325 }
2326
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002327 return queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002328}
2329
2330static unsigned int mv643xx_eth_port_disable_rx(unsigned int port_num)
2331{
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002332 u32 queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002333
2334 /* Stop Rx port activity. Check port Rx activity. */
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002335 queues = mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num))
Dale Farnsworthe38fd1a2006-03-03 09:59:28 -07002336 & 0xFF;
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002337 if (queues) {
2338 /* Issue stop command for active queues only */
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002339 mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002340 (queues << 8));
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002341
2342 /* Wait for all Rx activity to terminate. */
2343 /* Check port cause register that all Rx queues are stopped */
2344 while (mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num))
2345 & 0xFF)
2346 udelay(PHY_WAIT_MICRO_SECONDS);
2347 }
2348
Dale Farnsworth12a87c62006-03-03 10:00:22 -07002349 return queues;
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002350}
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352/*
2353 * eth_port_reset - Reset Ethernet port
2354 *
2355 * DESCRIPTION:
2356 * This routine resets the chip by aborting any SDMA engine activity and
2357 * clearing the MIB counters. The Receiver and the Transmit unit are in
2358 * idle state after this command is performed and the port is disabled.
2359 *
2360 * INPUT:
2361 * unsigned int eth_port_num Ethernet Port number.
2362 *
2363 * OUTPUT:
2364 * Channel activity is halted.
2365 *
2366 * RETURN:
2367 * None.
2368 *
2369 */
2370static void eth_port_reset(unsigned int port_num)
2371{
2372 unsigned int reg_data;
2373
Dale Farnsworth9f8dd312006-01-27 01:10:47 -07002374 mv643xx_eth_port_disable_tx(port_num);
2375 mv643xx_eth_port_disable_rx(port_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
2377 /* Clear all MIB counters */
2378 eth_clear_mib_counters(port_num);
2379
2380 /* Reset the Enable bit in the Configuration Register */
2381 reg_data = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
James Chapmand0412d92006-01-27 01:15:30 -07002382 reg_data &= ~(MV643XX_ETH_SERIAL_PORT_ENABLE |
2383 MV643XX_ETH_DO_NOT_FORCE_LINK_FAIL |
2384 MV643XX_ETH_FORCE_LINK_PASS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), reg_data);
2386}
2387
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 * eth_port_read_smi_reg - Read PHY registers
2391 *
2392 * DESCRIPTION:
2393 * This routine utilize the SMI interface to interact with the PHY in
2394 * order to perform PHY register read.
2395 *
2396 * INPUT:
2397 * unsigned int port_num Ethernet Port number.
2398 * unsigned int phy_reg PHY register address offset.
2399 * unsigned int *value Register value buffer.
2400 *
2401 * OUTPUT:
2402 * Write the value of a specified PHY register into given buffer.
2403 *
2404 * RETURN:
2405 * false if the PHY is busy or read data is not in valid state.
2406 * true otherwise.
2407 *
2408 */
2409static void eth_port_read_smi_reg(unsigned int port_num,
2410 unsigned int phy_reg, unsigned int *value)
2411{
2412 int phy_addr = ethernet_phy_get(port_num);
2413 unsigned long flags;
2414 int i;
2415
2416 /* the SMI register is a shared resource */
2417 spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
2418
2419 /* wait for the SMI register to become available */
2420 for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
2421 if (i == PHY_WAIT_ITERATIONS) {
2422 printk("mv643xx PHY busy timeout, port %d\n", port_num);
2423 goto out;
2424 }
2425 udelay(PHY_WAIT_MICRO_SECONDS);
2426 }
2427
2428 mv_write(MV643XX_ETH_SMI_REG,
2429 (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
2430
2431 /* now wait for the data to be valid */
2432 for (i = 0; !(mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_READ_VALID); i++) {
2433 if (i == PHY_WAIT_ITERATIONS) {
2434 printk("mv643xx PHY read timeout, port %d\n", port_num);
2435 goto out;
2436 }
2437 udelay(PHY_WAIT_MICRO_SECONDS);
2438 }
2439
2440 *value = mv_read(MV643XX_ETH_SMI_REG) & 0xffff;
2441out:
2442 spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
2443}
2444
2445/*
2446 * eth_port_write_smi_reg - Write to PHY registers
2447 *
2448 * DESCRIPTION:
2449 * This routine utilize the SMI interface to interact with the PHY in
2450 * order to perform writes to PHY registers.
2451 *
2452 * INPUT:
2453 * unsigned int eth_port_num Ethernet Port number.
2454 * unsigned int phy_reg PHY register address offset.
2455 * unsigned int value Register value.
2456 *
2457 * OUTPUT:
2458 * Write the given value to the specified PHY register.
2459 *
2460 * RETURN:
2461 * false if the PHY is busy.
2462 * true otherwise.
2463 *
2464 */
2465static void eth_port_write_smi_reg(unsigned int eth_port_num,
2466 unsigned int phy_reg, unsigned int value)
2467{
2468 int phy_addr;
2469 int i;
2470 unsigned long flags;
2471
2472 phy_addr = ethernet_phy_get(eth_port_num);
2473
2474 /* the SMI register is a shared resource */
2475 spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
2476
2477 /* wait for the SMI register to become available */
2478 for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
2479 if (i == PHY_WAIT_ITERATIONS) {
2480 printk("mv643xx PHY busy timeout, port %d\n",
2481 eth_port_num);
2482 goto out;
2483 }
2484 udelay(PHY_WAIT_MICRO_SECONDS);
2485 }
2486
2487 mv_write(MV643XX_ETH_SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
2488 ETH_SMI_OPCODE_WRITE | (value & 0xffff));
2489out:
2490 spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
2491}
2492
2493/*
James Chapmanc28a4f82006-01-27 01:13:15 -07002494 * Wrappers for MII support library.
2495 */
2496static int mv643xx_mdio_read(struct net_device *dev, int phy_id, int location)
2497{
2498 int val;
2499 struct mv643xx_private *mp = netdev_priv(dev);
2500
2501 eth_port_read_smi_reg(mp->port_num, location, &val);
2502 return val;
2503}
2504
2505static void mv643xx_mdio_write(struct net_device *dev, int phy_id, int location, int val)
2506{
2507 struct mv643xx_private *mp = netdev_priv(dev);
2508 eth_port_write_smi_reg(mp->port_num, location, val);
2509}
2510
2511/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 * eth_port_receive - Get received information from Rx ring.
2513 *
2514 * DESCRIPTION:
2515 * This routine returns the received data to the caller. There is no
2516 * data copying during routine operation. All information is returned
2517 * using pointer to packet information struct passed from the caller.
2518 * If the routine exhausts Rx ring resources then the resource error flag
2519 * is set.
2520 *
2521 * INPUT:
2522 * struct mv643xx_private *mp Ethernet Port Control srtuct.
2523 * struct pkt_info *p_pkt_info User packet buffer.
2524 *
2525 * OUTPUT:
2526 * Rx ring current and used indexes are updated.
2527 *
2528 * RETURN:
2529 * ETH_ERROR in case the routine can not access Rx desc ring.
2530 * ETH_QUEUE_FULL if Rx ring resources are exhausted.
2531 * ETH_END_OF_JOB if there is no received data.
2532 * ETH_OK otherwise.
2533 */
2534static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
2535 struct pkt_info *p_pkt_info)
2536{
2537 int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
2538 volatile struct eth_rx_desc *p_rx_desc;
2539 unsigned int command_status;
Dale Farnsworth8f518702006-01-16 16:56:30 -07002540 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 /* Do not process Rx ring in case of Rx ring resource error */
2543 if (mp->rx_resource_err)
2544 return ETH_QUEUE_FULL;
2545
Dale Farnsworth8f518702006-01-16 16:56:30 -07002546 spin_lock_irqsave(&mp->lock, flags);
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 /* Get the Rx Desc ring 'curr and 'used' indexes */
2549 rx_curr_desc = mp->rx_curr_desc_q;
2550 rx_used_desc = mp->rx_used_desc_q;
2551
2552 p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
2553
2554 /* The following parameters are used to save readings from memory */
2555 command_status = p_rx_desc->cmd_sts;
2556 rmb();
2557
2558 /* Nothing to receive... */
Dale Farnsworth8f518702006-01-16 16:56:30 -07002559 if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) {
2560 spin_unlock_irqrestore(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 return ETH_END_OF_JOB;
Dale Farnsworth8f518702006-01-16 16:56:30 -07002562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
2565 p_pkt_info->cmd_sts = command_status;
2566 p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
2567 p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
2568 p_pkt_info->l4i_chk = p_rx_desc->buf_size;
2569
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002570 /*
2571 * Clean the return info field to indicate that the
2572 * packet has been moved to the upper layers
2573 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 mp->rx_skb[rx_curr_desc] = NULL;
2575
2576 /* Update current index in data structure */
2577 rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
2578 mp->rx_curr_desc_q = rx_next_curr_desc;
2579
2580 /* Rx descriptors exhausted. Set the Rx ring resource error flag */
2581 if (rx_next_curr_desc == rx_used_desc)
2582 mp->rx_resource_err = 1;
2583
Dale Farnsworth8f518702006-01-16 16:56:30 -07002584 spin_unlock_irqrestore(&mp->lock, flags);
2585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 return ETH_OK;
2587}
2588
2589/*
2590 * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
2591 *
2592 * DESCRIPTION:
2593 * This routine returns a Rx buffer back to the Rx ring. It retrieves the
2594 * next 'used' descriptor and attached the returned buffer to it.
2595 * In case the Rx ring was in "resource error" condition, where there are
2596 * no available Rx resources, the function resets the resource error flag.
2597 *
2598 * INPUT:
2599 * struct mv643xx_private *mp Ethernet Port Control srtuct.
2600 * struct pkt_info *p_pkt_info Information on returned buffer.
2601 *
2602 * OUTPUT:
2603 * New available Rx resource in Rx descriptor ring.
2604 *
2605 * RETURN:
2606 * ETH_ERROR in case the routine can not access Rx desc ring.
2607 * ETH_OK otherwise.
2608 */
2609static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
2610 struct pkt_info *p_pkt_info)
2611{
2612 int used_rx_desc; /* Where to return Rx resource */
2613 volatile struct eth_rx_desc *p_used_rx_desc;
Dale Farnsworth8f518702006-01-16 16:56:30 -07002614 unsigned long flags;
2615
2616 spin_lock_irqsave(&mp->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617
2618 /* Get 'used' Rx descriptor */
2619 used_rx_desc = mp->rx_used_desc_q;
2620 p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
2621
2622 p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
2623 p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
2624 mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
2625
2626 /* Flush the write pipe */
2627
2628 /* Return the descriptor to DMA ownership */
2629 wmb();
2630 p_used_rx_desc->cmd_sts =
2631 ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
2632 wmb();
2633
2634 /* Move the used descriptor pointer to the next descriptor */
2635 mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
2636
2637 /* Any Rx return cancels the Rx resource error status */
2638 mp->rx_resource_err = 0;
2639
Dale Farnsworth8f518702006-01-16 16:56:30 -07002640 spin_unlock_irqrestore(&mp->lock, flags);
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 return ETH_OK;
2643}
2644
2645/************* Begin ethtool support *************************/
2646
2647struct mv643xx_stats {
2648 char stat_string[ETH_GSTRING_LEN];
2649 int sizeof_stat;
2650 int stat_offset;
2651};
2652
2653#define MV643XX_STAT(m) sizeof(((struct mv643xx_private *)0)->m), \
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002654 offsetof(struct mv643xx_private, m)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
2656static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
2657 { "rx_packets", MV643XX_STAT(stats.rx_packets) },
2658 { "tx_packets", MV643XX_STAT(stats.tx_packets) },
2659 { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
2660 { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
2661 { "rx_errors", MV643XX_STAT(stats.rx_errors) },
2662 { "tx_errors", MV643XX_STAT(stats.tx_errors) },
2663 { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
2664 { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
2665 { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
2666 { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
2667 { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
2668 { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
2669 { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
2670 { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
2671 { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
2672 { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
2673 { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
2674 { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
2675 { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
2676 { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
2677 { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
2678 { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
2679 { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
2680 { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
2681 { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
2682 { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
2683 { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
2684 { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
2685 { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
2686 { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
2687 { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
2688 { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
2689 { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
2690 { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
2691 { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
2692 { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
2693 { "collision", MV643XX_STAT(mib_counters.collision) },
2694 { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
2695};
2696
2697#define MV643XX_STATS_LEN \
2698 sizeof(mv643xx_gstrings_stats) / sizeof(struct mv643xx_stats)
2699
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002700static void mv643xx_get_drvinfo(struct net_device *netdev,
2701 struct ethtool_drvinfo *drvinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702{
2703 strncpy(drvinfo->driver, mv643xx_driver_name, 32);
2704 strncpy(drvinfo->version, mv643xx_driver_version, 32);
2705 strncpy(drvinfo->fw_version, "N/A", 32);
2706 strncpy(drvinfo->bus_info, "mv643xx", 32);
2707 drvinfo->n_stats = MV643XX_STATS_LEN;
2708}
2709
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002710static int mv643xx_get_stats_count(struct net_device *netdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 return MV643XX_STATS_LEN;
2713}
2714
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002715static void mv643xx_get_ethtool_stats(struct net_device *netdev,
2716 struct ethtool_stats *stats, uint64_t *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 struct mv643xx_private *mp = netdev->priv;
2719 int i;
2720
2721 eth_update_mib_counters(mp);
2722
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002723 for (i = 0; i < MV643XX_STATS_LEN; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002725 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat ==
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
2727 }
2728}
2729
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002730static void mv643xx_get_strings(struct net_device *netdev, uint32_t stringset,
2731 uint8_t *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
2733 int i;
2734
2735 switch(stringset) {
2736 case ETH_SS_STATS:
2737 for (i=0; i < MV643XX_STATS_LEN; i++) {
Dale Farnsworthb4de9052006-01-27 01:04:43 -07002738 memcpy(data + i * ETH_GSTRING_LEN,
2739 mv643xx_gstrings_stats[i].stat_string,
2740 ETH_GSTRING_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 }
2742 break;
2743 }
2744}
2745
James Chapmand0412d92006-01-27 01:15:30 -07002746static u32 mv643xx_eth_get_link(struct net_device *dev)
2747{
2748 struct mv643xx_private *mp = netdev_priv(dev);
2749
2750 return mii_link_ok(&mp->mii);
2751}
2752
2753static int mv643xx_eth_nway_restart(struct net_device *dev)
2754{
2755 struct mv643xx_private *mp = netdev_priv(dev);
2756
2757 return mii_nway_restart(&mp->mii);
2758}
2759
2760static int mv643xx_eth_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2761{
2762 struct mv643xx_private *mp = netdev_priv(dev);
2763
2764 return generic_mii_ioctl(&mp->mii, if_mii(ifr), cmd, NULL);
2765}
2766
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767static struct ethtool_ops mv643xx_ethtool_ops = {
2768 .get_settings = mv643xx_get_settings,
James Chapmand0412d92006-01-27 01:15:30 -07002769 .set_settings = mv643xx_set_settings,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 .get_drvinfo = mv643xx_get_drvinfo,
James Chapmand0412d92006-01-27 01:15:30 -07002771 .get_link = mv643xx_eth_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 .get_sg = ethtool_op_get_sg,
2773 .set_sg = ethtool_op_set_sg,
2774 .get_strings = mv643xx_get_strings,
2775 .get_stats_count = mv643xx_get_stats_count,
2776 .get_ethtool_stats = mv643xx_get_ethtool_stats,
James Chapmand0412d92006-01-27 01:15:30 -07002777 .get_strings = mv643xx_get_strings,
2778 .get_stats_count = mv643xx_get_stats_count,
2779 .get_ethtool_stats = mv643xx_get_ethtool_stats,
2780 .nway_reset = mv643xx_eth_nway_restart,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781};
2782
2783/************* End ethtool support *************************/