[PATCH] mv643xx: Fix promiscuous mode handling
[powerpc.git] / drivers / net / mv643xx_eth.c
1 /*
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.,
9  *      written by Manish Lachwani (lachwani@pmc-sierra.com)
10  *
11  * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org>
12  *
13  * Copyright (C) 2004-2005 MontaVista Software, Inc.
14  *                         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>
35 #include <linux/tcp.h>
36 #include <linux/udp.h>
37 #include <linux/etherdevice.h>
38
39 #include <linux/bitops.h>
40 #include <linux/delay.h>
41 #include <linux/ethtool.h>
42 #include <asm/io.h>
43 #include <asm/types.h>
44 #include <asm/pgtable.h>
45 #include <asm/system.h>
46 #include <asm/delay.h>
47 #include "mv643xx_eth.h"
48
49 /*
50  * The first part is the high level driver of the gigE ethernet ports.
51  */
52
53 /* Constants */
54 #define VLAN_HLEN               4
55 #define FCS_LEN                 4
56 #define WRAP                    NET_IP_ALIGN + ETH_HLEN + VLAN_HLEN + FCS_LEN
57 #define RX_SKB_SIZE             ((dev->mtu + WRAP + 7) & ~0x7)
58
59 #define INT_CAUSE_UNMASK_ALL            0x0007ffff
60 #define INT_CAUSE_UNMASK_ALL_EXT        0x0011ffff
61 #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK
62 #define INT_CAUSE_MASK_ALL              0x00000000
63 #define INT_CAUSE_CHECK_BITS            INT_CAUSE_UNMASK_ALL
64 #define INT_CAUSE_CHECK_BITS_EXT        INT_CAUSE_UNMASK_ALL_EXT
65 #endif
66
67 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
68 #define MAX_DESCS_PER_SKB       (MAX_SKB_FRAGS + 1)
69 #else
70 #define MAX_DESCS_PER_SKB       1
71 #endif
72
73 #define PHY_WAIT_ITERATIONS     1000    /* 1000 iterations * 10uS = 10mS max */
74 #define PHY_WAIT_MICRO_SECONDS  10
75
76 /* Static function declarations */
77 static int eth_port_link_is_up(unsigned int eth_port_num);
78 static void eth_port_uc_addr_get(struct net_device *dev,
79                                                 unsigned char *MacAddr);
80 static int mv643xx_eth_real_open(struct net_device *);
81 static int mv643xx_eth_real_stop(struct net_device *);
82 static int mv643xx_eth_change_mtu(struct net_device *, int);
83 static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *);
84 static void eth_port_init_mac_tables(unsigned int eth_port_num);
85 #ifdef MV643XX_NAPI
86 static int mv643xx_poll(struct net_device *dev, int *budget);
87 #endif
88 static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
89 static int ethernet_phy_detect(unsigned int eth_port_num);
90 static struct ethtool_ops mv643xx_ethtool_ops;
91
92 static char mv643xx_driver_name[] = "mv643xx_eth";
93 static char mv643xx_driver_version[] = "1.0";
94
95 static void __iomem *mv643xx_eth_shared_base;
96
97 /* used to protect MV643XX_ETH_SMI_REG, which is shared across ports */
98 static spinlock_t mv643xx_eth_phy_lock = SPIN_LOCK_UNLOCKED;
99
100 static inline u32 mv_read(int offset)
101 {
102         void __iomem *reg_base;
103
104         reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS;
105
106         return readl(reg_base + offset);
107 }
108
109 static inline void mv_write(int offset, u32 data)
110 {
111         void __iomem *reg_base;
112
113         reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS;
114         writel(data, reg_base + offset);
115 }
116
117 /*
118  * Changes MTU (maximum transfer unit) of the gigabit ethenret port
119  *
120  * Input :      pointer to ethernet interface network device structure
121  *              new mtu size
122  * Output :     0 upon success, -EINVAL upon failure
123  */
124 static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu)
125 {
126         struct mv643xx_private *mp = netdev_priv(dev);
127         unsigned long flags;
128
129         spin_lock_irqsave(&mp->lock, flags);
130
131         if ((new_mtu > 9500) || (new_mtu < 64)) {
132                 spin_unlock_irqrestore(&mp->lock, flags);
133                 return -EINVAL;
134         }
135
136         dev->mtu = new_mtu;
137         /*
138          * Stop then re-open the interface. This will allocate RX skb's with
139          * the new MTU.
140          * There is a possible danger that the open will not successed, due
141          * to memory is full, which might fail the open function.
142          */
143         if (netif_running(dev)) {
144                 if (mv643xx_eth_real_stop(dev))
145                         printk(KERN_ERR
146                                 "%s: Fatal error on stopping device\n",
147                                 dev->name);
148                 if (mv643xx_eth_real_open(dev))
149                         printk(KERN_ERR
150                                 "%s: Fatal error on opening device\n",
151                                 dev->name);
152         }
153
154         spin_unlock_irqrestore(&mp->lock, flags);
155         return 0;
156 }
157
158 /*
159  * mv643xx_eth_rx_task
160  *
161  * Fills / refills RX queue on a certain gigabit ethernet port
162  *
163  * Input :      pointer to ethernet interface network device structure
164  * Output :     N/A
165  */
166 static void mv643xx_eth_rx_task(void *data)
167 {
168         struct net_device *dev = (struct net_device *)data;
169         struct mv643xx_private *mp = netdev_priv(dev);
170         struct pkt_info pkt_info;
171         struct sk_buff *skb;
172
173         if (test_and_set_bit(0, &mp->rx_task_busy))
174                 panic("%s: Error in test_set_bit / clear_bit", dev->name);
175
176         while (mp->rx_ring_skbs < (mp->rx_ring_size - 5)) {
177                 skb = dev_alloc_skb(RX_SKB_SIZE);
178                 if (!skb)
179                         break;
180                 mp->rx_ring_skbs++;
181                 pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT;
182                 pkt_info.byte_cnt = RX_SKB_SIZE;
183                 pkt_info.buf_ptr = dma_map_single(NULL, skb->data, RX_SKB_SIZE,
184                                                         DMA_FROM_DEVICE);
185                 pkt_info.return_info = skb;
186                 if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) {
187                         printk(KERN_ERR
188                                 "%s: Error allocating RX Ring\n", dev->name);
189                         break;
190                 }
191                 skb_reserve(skb, 2);
192         }
193         clear_bit(0, &mp->rx_task_busy);
194         /*
195          * If RX ring is empty of SKB, set a timer to try allocating
196          * again in a later time .
197          */
198         if ((mp->rx_ring_skbs == 0) && (mp->rx_timer_flag == 0)) {
199                 printk(KERN_INFO "%s: Rx ring is empty\n", dev->name);
200                 /* After 100mSec */
201                 mp->timeout.expires = jiffies + (HZ / 10);
202                 add_timer(&mp->timeout);
203                 mp->rx_timer_flag = 1;
204         }
205 #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK
206         else {
207                 /* Return interrupts */
208                 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(mp->port_num),
209                                                         INT_CAUSE_UNMASK_ALL);
210         }
211 #endif
212 }
213
214 /*
215  * mv643xx_eth_rx_task_timer_wrapper
216  *
217  * Timer routine to wake up RX queue filling task. This function is
218  * used only in case the RX queue is empty, and all alloc_skb has
219  * failed (due to out of memory event).
220  *
221  * Input :      pointer to ethernet interface network device structure
222  * Output :     N/A
223  */
224 static void mv643xx_eth_rx_task_timer_wrapper(unsigned long data)
225 {
226         struct net_device *dev = (struct net_device *)data;
227         struct mv643xx_private *mp = netdev_priv(dev);
228
229         mp->rx_timer_flag = 0;
230         mv643xx_eth_rx_task((void *)data);
231 }
232
233 /*
234  * mv643xx_eth_update_mac_address
235  *
236  * Update the MAC address of the port in the address table
237  *
238  * Input :      pointer to ethernet interface network device structure
239  * Output :     N/A
240  */
241 static void mv643xx_eth_update_mac_address(struct net_device *dev)
242 {
243         struct mv643xx_private *mp = netdev_priv(dev);
244         unsigned int port_num = mp->port_num;
245
246         eth_port_init_mac_tables(port_num);
247         memcpy(mp->port_mac_addr, dev->dev_addr, 6);
248         eth_port_uc_addr_set(port_num, mp->port_mac_addr);
249 }
250
251 /*
252  * mv643xx_eth_set_rx_mode
253  *
254  * Change from promiscuos to regular rx mode
255  *
256  * Input :      pointer to ethernet interface network device structure
257  * Output :     N/A
258  */
259 static void mv643xx_eth_set_rx_mode(struct net_device *dev)
260 {
261         struct mv643xx_private *mp = netdev_priv(dev);
262
263         if (dev->flags & IFF_PROMISC)
264                 mp->port_config |= (u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE;
265         else
266                 mp->port_config &= ~(u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE;
267
268         mv_write(MV643XX_ETH_PORT_CONFIG_REG(mp->port_num), mp->port_config);
269 }
270
271 /*
272  * mv643xx_eth_set_mac_address
273  *
274  * Change the interface's mac address.
275  * No special hardware thing should be done because interface is always
276  * put in promiscuous mode.
277  *
278  * Input :      pointer to ethernet interface network device structure and
279  *              a pointer to the designated entry to be added to the cache.
280  * Output :     zero upon success, negative upon failure
281  */
282 static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr)
283 {
284         int i;
285
286         for (i = 0; i < 6; i++)
287                 /* +2 is for the offset of the HW addr type */
288                 dev->dev_addr[i] = ((unsigned char *)addr)[i + 2];
289         mv643xx_eth_update_mac_address(dev);
290         return 0;
291 }
292
293 /*
294  * mv643xx_eth_tx_timeout
295  *
296  * Called upon a timeout on transmitting a packet
297  *
298  * Input :      pointer to ethernet interface network device structure.
299  * Output :     N/A
300  */
301 static void mv643xx_eth_tx_timeout(struct net_device *dev)
302 {
303         struct mv643xx_private *mp = netdev_priv(dev);
304
305         printk(KERN_INFO "%s: TX timeout  ", dev->name);
306
307         /* Do the reset outside of interrupt context */
308         schedule_work(&mp->tx_timeout_task);
309 }
310
311 /*
312  * mv643xx_eth_tx_timeout_task
313  *
314  * Actual routine to reset the adapter when a timeout on Tx has occurred
315  */
316 static void mv643xx_eth_tx_timeout_task(struct net_device *dev)
317 {
318         struct mv643xx_private *mp = netdev_priv(dev);
319
320         netif_device_detach(dev);
321         eth_port_reset(mp->port_num);
322         eth_port_start(mp);
323         netif_device_attach(dev);
324 }
325
326 /*
327  * mv643xx_eth_free_tx_queue
328  *
329  * Input :      dev - a pointer to the required interface
330  *
331  * Output :     0 if was able to release skb , nonzero otherwise
332  */
333 static int mv643xx_eth_free_tx_queue(struct net_device *dev,
334                                         unsigned int eth_int_cause_ext)
335 {
336         struct mv643xx_private *mp = netdev_priv(dev);
337         struct net_device_stats *stats = &mp->stats;
338         struct pkt_info pkt_info;
339         int released = 1;
340
341         if (!(eth_int_cause_ext & (BIT0 | BIT8)))
342                 return released;
343
344         spin_lock(&mp->lock);
345
346         /* Check only queue 0 */
347         while (eth_tx_return_desc(mp, &pkt_info) == ETH_OK) {
348                 if (pkt_info.cmd_sts & BIT0) {
349                         printk("%s: Error in TX\n", dev->name);
350                         stats->tx_errors++;
351                 }
352
353                 /*
354                  * If return_info is different than 0, release the skb.
355                  * The case where return_info is not 0 is only in case
356                  * when transmitted a scatter/gather packet, where only
357                  * last skb releases the whole chain.
358                  */
359                 if (pkt_info.return_info) {
360                         if (skb_shinfo(pkt_info.return_info)->nr_frags)
361                                 dma_unmap_page(NULL, pkt_info.buf_ptr,
362                                                 pkt_info.byte_cnt,
363                                                 DMA_TO_DEVICE);
364                         else
365                                 dma_unmap_single(NULL, pkt_info.buf_ptr,
366                                                 pkt_info.byte_cnt,
367                                                 DMA_TO_DEVICE);
368
369                         dev_kfree_skb_irq(pkt_info.return_info);
370                         released = 0;
371                 } else
372                         dma_unmap_page(NULL, pkt_info.buf_ptr,
373                                         pkt_info.byte_cnt, DMA_TO_DEVICE);
374         }
375
376         spin_unlock(&mp->lock);
377
378         return released;
379 }
380
381 /*
382  * mv643xx_eth_receive
383  *
384  * This function is forward packets that are received from the port's
385  * queues toward kernel core or FastRoute them to another interface.
386  *
387  * Input :      dev - a pointer to the required interface
388  *              max - maximum number to receive (0 means unlimted)
389  *
390  * Output :     number of served packets
391  */
392 #ifdef MV643XX_NAPI
393 static int mv643xx_eth_receive_queue(struct net_device *dev, int budget)
394 #else
395 static int mv643xx_eth_receive_queue(struct net_device *dev)
396 #endif
397 {
398         struct mv643xx_private *mp = netdev_priv(dev);
399         struct net_device_stats *stats = &mp->stats;
400         unsigned int received_packets = 0;
401         struct sk_buff *skb;
402         struct pkt_info pkt_info;
403
404 #ifdef MV643XX_NAPI
405         while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) {
406 #else
407         while (eth_port_receive(mp, &pkt_info) == ETH_OK) {
408 #endif
409                 mp->rx_ring_skbs--;
410                 received_packets++;
411
412                 /* Update statistics. Note byte count includes 4 byte CRC count */
413                 stats->rx_packets++;
414                 stats->rx_bytes += pkt_info.byte_cnt;
415                 skb = pkt_info.return_info;
416                 /*
417                  * In case received a packet without first / last bits on OR
418                  * the error summary bit is on, the packets needs to be dropeed.
419                  */
420                 if (((pkt_info.cmd_sts
421                                 & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) !=
422                                         (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC))
423                                 || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) {
424                         stats->rx_dropped++;
425                         if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC |
426                                                         ETH_RX_LAST_DESC)) !=
427                                 (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) {
428                                 if (net_ratelimit())
429                                         printk(KERN_ERR
430                                                 "%s: Received packet spread "
431                                                 "on multiple descriptors\n",
432                                                 dev->name);
433                         }
434                         if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)
435                                 stats->rx_errors++;
436
437                         dev_kfree_skb_irq(skb);
438                 } else {
439                         /*
440                          * The -4 is for the CRC in the trailer of the
441                          * received packet
442                          */
443                         skb_put(skb, pkt_info.byte_cnt - 4);
444                         skb->dev = dev;
445
446                         if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) {
447                                 skb->ip_summed = CHECKSUM_UNNECESSARY;
448                                 skb->csum = htons(
449                                         (pkt_info.cmd_sts & 0x0007fff8) >> 3);
450                         }
451                         skb->protocol = eth_type_trans(skb, dev);
452 #ifdef MV643XX_NAPI
453                         netif_receive_skb(skb);
454 #else
455                         netif_rx(skb);
456 #endif
457                 }
458         }
459
460         return received_packets;
461 }
462
463 /*
464  * mv643xx_eth_int_handler
465  *
466  * Main interrupt handler for the gigbit ethernet ports
467  *
468  * Input :      irq     - irq number (not used)
469  *              dev_id  - a pointer to the required interface's data structure
470  *              regs    - not used
471  * Output :     N/A
472  */
473
474 static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id,
475                                                         struct pt_regs *regs)
476 {
477         struct net_device *dev = (struct net_device *)dev_id;
478         struct mv643xx_private *mp = netdev_priv(dev);
479         u32 eth_int_cause, eth_int_cause_ext = 0;
480         unsigned int port_num = mp->port_num;
481
482         /* Read interrupt cause registers */
483         eth_int_cause = mv_read(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num)) &
484                                                 INT_CAUSE_UNMASK_ALL;
485
486         if (eth_int_cause & BIT1)
487                 eth_int_cause_ext = mv_read(
488                         MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num)) &
489                                                 INT_CAUSE_UNMASK_ALL_EXT;
490
491 #ifdef MV643XX_NAPI
492         if (!(eth_int_cause & 0x0007fffd)) {
493                 /* Dont ack the Rx interrupt */
494 #endif
495                 /*
496                  * Clear specific ethernet port intrerrupt registers by
497                  * acknowleding relevant bits.
498                  */
499                 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num),
500                                                         ~eth_int_cause);
501                 if (eth_int_cause_ext != 0x0)
502                         mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG
503                                         (port_num), ~eth_int_cause_ext);
504
505                 /* UDP change : We may need this */
506                 if ((eth_int_cause_ext & 0x0000ffff) &&
507                     (mv643xx_eth_free_tx_queue(dev, eth_int_cause_ext) == 0) &&
508                     (mp->tx_ring_size > mp->tx_ring_skbs + MAX_DESCS_PER_SKB))
509                         netif_wake_queue(dev);
510 #ifdef MV643XX_NAPI
511         } else {
512                 if (netif_rx_schedule_prep(dev)) {
513                         /* Mask all the interrupts */
514                         mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), 0);
515                         mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG
516                                                                 (port_num), 0);
517                         __netif_rx_schedule(dev);
518                 }
519 #else
520                 if (eth_int_cause & (BIT2 | BIT11))
521                         mv643xx_eth_receive_queue(dev, 0);
522
523                 /*
524                  * After forwarded received packets to upper layer, add a task
525                  * in an interrupts enabled context that refills the RX ring
526                  * with skb's.
527                  */
528 #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK
529                 /* Unmask all interrupts on ethernet port */
530                 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
531                                                         INT_CAUSE_MASK_ALL);
532                 queue_task(&mp->rx_task, &tq_immediate);
533                 mark_bh(IMMEDIATE_BH);
534 #else
535                 mp->rx_task.func(dev);
536 #endif
537 #endif
538         }
539         /* PHY status changed */
540         if (eth_int_cause_ext & (BIT16 | BIT20)) {
541                 if (eth_port_link_is_up(port_num)) {
542                         netif_carrier_on(dev);
543                         netif_wake_queue(dev);
544                         /* Start TX queue */
545                         mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG
546                                                                 (port_num), 1);
547                 } else {
548                         netif_carrier_off(dev);
549                         netif_stop_queue(dev);
550                 }
551         }
552
553         /*
554          * If no real interrupt occured, exit.
555          * This can happen when using gigE interrupt coalescing mechanism.
556          */
557         if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0))
558                 return IRQ_NONE;
559
560         return IRQ_HANDLED;
561 }
562
563 #ifdef MV643XX_COAL
564
565 /*
566  * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path
567  *
568  * DESCRIPTION:
569  *      This routine sets the RX coalescing interrupt mechanism parameter.
570  *      This parameter is a timeout counter, that counts in 64 t_clk
571  *      chunks ; that when timeout event occurs a maskable interrupt
572  *      occurs.
573  *      The parameter is calculated using the tClk of the MV-643xx chip
574  *      , and the required delay of the interrupt in usec.
575  *
576  * INPUT:
577  *      unsigned int eth_port_num       Ethernet port number
578  *      unsigned int t_clk              t_clk of the MV-643xx chip in HZ units
579  *      unsigned int delay              Delay in usec
580  *
581  * OUTPUT:
582  *      Interrupt coalescing mechanism value is set in MV-643xx chip.
583  *
584  * RETURN:
585  *      The interrupt coalescing value set in the gigE port.
586  *
587  */
588 static unsigned int eth_port_set_rx_coal(unsigned int eth_port_num,
589                                         unsigned int t_clk, unsigned int delay)
590 {
591         unsigned int coal = ((t_clk / 1000000) * delay) / 64;
592
593         /* Set RX Coalescing mechanism */
594         mv_write(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num),
595                 ((coal & 0x3fff) << 8) |
596                 (mv_read(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num))
597                         & 0xffc000ff));
598
599         return coal;
600 }
601 #endif
602
603 /*
604  * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path
605  *
606  * DESCRIPTION:
607  *      This routine sets the TX coalescing interrupt mechanism parameter.
608  *      This parameter is a timeout counter, that counts in 64 t_clk
609  *      chunks ; that when timeout event occurs a maskable interrupt
610  *      occurs.
611  *      The parameter is calculated using the t_cLK frequency of the
612  *      MV-643xx chip and the required delay in the interrupt in uSec
613  *
614  * INPUT:
615  *      unsigned int eth_port_num       Ethernet port number
616  *      unsigned int t_clk              t_clk of the MV-643xx chip in HZ units
617  *      unsigned int delay              Delay in uSeconds
618  *
619  * OUTPUT:
620  *      Interrupt coalescing mechanism value is set in MV-643xx chip.
621  *
622  * RETURN:
623  *      The interrupt coalescing value set in the gigE port.
624  *
625  */
626 static unsigned int eth_port_set_tx_coal(unsigned int eth_port_num,
627                                         unsigned int t_clk, unsigned int delay)
628 {
629         unsigned int coal;
630         coal = ((t_clk / 1000000) * delay) / 64;
631         /* Set TX Coalescing mechanism */
632         mv_write(MV643XX_ETH_TX_FIFO_URGENT_THRESHOLD_REG(eth_port_num),
633                                                                 coal << 4);
634         return coal;
635 }
636
637 /*
638  * mv643xx_eth_open
639  *
640  * This function is called when openning the network device. The function
641  * should initialize all the hardware, initialize cyclic Rx/Tx
642  * descriptors chain and buffers and allocate an IRQ to the network
643  * device.
644  *
645  * Input :      a pointer to the network device structure
646  *
647  * Output :     zero of success , nonzero if fails.
648  */
649
650 static int mv643xx_eth_open(struct net_device *dev)
651 {
652         struct mv643xx_private *mp = netdev_priv(dev);
653         unsigned int port_num = mp->port_num;
654         int err;
655
656         spin_lock_irq(&mp->lock);
657
658         err = request_irq(dev->irq, mv643xx_eth_int_handler,
659                         SA_SHIRQ | SA_SAMPLE_RANDOM, dev->name, dev);
660
661         if (err) {
662                 printk(KERN_ERR "Can not assign IRQ number to MV643XX_eth%d\n",
663                                                                 port_num);
664                 err = -EAGAIN;
665                 goto out;
666         }
667
668         if (mv643xx_eth_real_open(dev)) {
669                 printk("%s: Error opening interface\n", dev->name);
670                 err = -EBUSY;
671                 goto out_free;
672         }
673
674         spin_unlock_irq(&mp->lock);
675
676         return 0;
677
678 out_free:
679         free_irq(dev->irq, dev);
680
681 out:
682         spin_unlock_irq(&mp->lock);
683
684         return err;
685 }
686
687 /*
688  * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory.
689  *
690  * DESCRIPTION:
691  *      This function prepares a Rx chained list of descriptors and packet
692  *      buffers in a form of a ring. The routine must be called after port
693  *      initialization routine and before port start routine.
694  *      The Ethernet SDMA engine uses CPU bus addresses to access the various
695  *      devices in the system (i.e. DRAM). This function uses the ethernet
696  *      struct 'virtual to physical' routine (set by the user) to set the ring
697  *      with physical addresses.
698  *
699  * INPUT:
700  *      struct mv643xx_private *mp      Ethernet Port Control srtuct.
701  *
702  * OUTPUT:
703  *      The routine updates the Ethernet port control struct with information
704  *      regarding the Rx descriptors and buffers.
705  *
706  * RETURN:
707  *      None.
708  */
709 static void ether_init_rx_desc_ring(struct mv643xx_private *mp)
710 {
711         volatile struct eth_rx_desc *p_rx_desc;
712         int rx_desc_num = mp->rx_ring_size;
713         int i;
714
715         /* initialize the next_desc_ptr links in the Rx descriptors ring */
716         p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area;
717         for (i = 0; i < rx_desc_num; i++) {
718                 p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma +
719                         ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc);
720         }
721
722         /* Save Rx desc pointer to driver struct. */
723         mp->rx_curr_desc_q = 0;
724         mp->rx_used_desc_q = 0;
725
726         mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc);
727
728         /* Add the queue to the list of RX queues of this port */
729         mp->port_rx_queue_command |= 1;
730 }
731
732 /*
733  * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory.
734  *
735  * DESCRIPTION:
736  *      This function prepares a Tx chained list of descriptors and packet
737  *      buffers in a form of a ring. The routine must be called after port
738  *      initialization routine and before port start routine.
739  *      The Ethernet SDMA engine uses CPU bus addresses to access the various
740  *      devices in the system (i.e. DRAM). This function uses the ethernet
741  *      struct 'virtual to physical' routine (set by the user) to set the ring
742  *      with physical addresses.
743  *
744  * INPUT:
745  *      struct mv643xx_private *mp      Ethernet Port Control srtuct.
746  *
747  * OUTPUT:
748  *      The routine updates the Ethernet port control struct with information
749  *      regarding the Tx descriptors and buffers.
750  *
751  * RETURN:
752  *      None.
753  */
754 static void ether_init_tx_desc_ring(struct mv643xx_private *mp)
755 {
756         int tx_desc_num = mp->tx_ring_size;
757         struct eth_tx_desc *p_tx_desc;
758         int i;
759
760         /* Initialize the next_desc_ptr links in the Tx descriptors ring */
761         p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area;
762         for (i = 0; i < tx_desc_num; i++) {
763                 p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma +
764                         ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc);
765         }
766
767         mp->tx_curr_desc_q = 0;
768         mp->tx_used_desc_q = 0;
769 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
770         mp->tx_first_desc_q = 0;
771 #endif
772
773         mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc);
774
775         /* Add the queue to the list of Tx queues of this port */
776         mp->port_tx_queue_command |= 1;
777 }
778
779 /* Helper function for mv643xx_eth_open */
780 static int mv643xx_eth_real_open(struct net_device *dev)
781 {
782         struct mv643xx_private *mp = netdev_priv(dev);
783         unsigned int port_num = mp->port_num;
784         unsigned int size;
785
786         /* Stop RX Queues */
787         mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), 0x0000ff00);
788
789         /* Clear the ethernet port interrupts */
790         mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
791         mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
792
793         /* Unmask RX buffer and TX end interrupt */
794         mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
795                                                 INT_CAUSE_UNMASK_ALL);
796
797         /* Unmask phy and link status changes interrupts */
798         mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num),
799                                                 INT_CAUSE_UNMASK_ALL_EXT);
800
801         /* Set the MAC Address */
802         memcpy(mp->port_mac_addr, dev->dev_addr, 6);
803
804         eth_port_init(mp);
805
806         INIT_WORK(&mp->rx_task, (void (*)(void *))mv643xx_eth_rx_task, dev);
807
808         memset(&mp->timeout, 0, sizeof(struct timer_list));
809         mp->timeout.function = mv643xx_eth_rx_task_timer_wrapper;
810         mp->timeout.data = (unsigned long)dev;
811
812         mp->rx_task_busy = 0;
813         mp->rx_timer_flag = 0;
814
815         /* Allocate RX and TX skb rings */
816         mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size,
817                                                                 GFP_KERNEL);
818         if (!mp->rx_skb) {
819                 printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name);
820                 return -ENOMEM;
821         }
822         mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size,
823                                                                 GFP_KERNEL);
824         if (!mp->tx_skb) {
825                 printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name);
826                 kfree(mp->rx_skb);
827                 return -ENOMEM;
828         }
829
830         /* Allocate TX ring */
831         mp->tx_ring_skbs = 0;
832         size = mp->tx_ring_size * sizeof(struct eth_tx_desc);
833         mp->tx_desc_area_size = size;
834
835         if (mp->tx_sram_size) {
836                 mp->p_tx_desc_area = ioremap(mp->tx_sram_addr,
837                                                         mp->tx_sram_size);
838                 mp->tx_desc_dma = mp->tx_sram_addr;
839         } else
840                 mp->p_tx_desc_area = dma_alloc_coherent(NULL, size,
841                                                         &mp->tx_desc_dma,
842                                                         GFP_KERNEL);
843
844         if (!mp->p_tx_desc_area) {
845                 printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n",
846                                                         dev->name, size);
847                 kfree(mp->rx_skb);
848                 kfree(mp->tx_skb);
849                 return -ENOMEM;
850         }
851         BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */
852         memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size);
853
854         ether_init_tx_desc_ring(mp);
855
856         /* Allocate RX ring */
857         mp->rx_ring_skbs = 0;
858         size = mp->rx_ring_size * sizeof(struct eth_rx_desc);
859         mp->rx_desc_area_size = size;
860
861         if (mp->rx_sram_size) {
862                 mp->p_rx_desc_area = ioremap(mp->rx_sram_addr,
863                                                         mp->rx_sram_size);
864                 mp->rx_desc_dma = mp->rx_sram_addr;
865         } else
866                 mp->p_rx_desc_area = dma_alloc_coherent(NULL, size,
867                                                         &mp->rx_desc_dma,
868                                                         GFP_KERNEL);
869
870         if (!mp->p_rx_desc_area) {
871                 printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n",
872                                                         dev->name, size);
873                 printk(KERN_ERR "%s: Freeing previously allocated TX queues...",
874                                                         dev->name);
875                 if (mp->rx_sram_size)
876                         iounmap(mp->p_rx_desc_area);
877                 else
878                         dma_free_coherent(NULL, mp->tx_desc_area_size,
879                                         mp->p_tx_desc_area, mp->tx_desc_dma);
880                 kfree(mp->rx_skb);
881                 kfree(mp->tx_skb);
882                 return -ENOMEM;
883         }
884         memset((void *)mp->p_rx_desc_area, 0, size);
885
886         ether_init_rx_desc_ring(mp);
887
888         mv643xx_eth_rx_task(dev);       /* Fill RX ring with skb's */
889
890         eth_port_start(mp);
891
892         /* Interrupt Coalescing */
893
894 #ifdef MV643XX_COAL
895         mp->rx_int_coal =
896                 eth_port_set_rx_coal(port_num, 133000000, MV643XX_RX_COAL);
897 #endif
898
899         mp->tx_int_coal =
900                 eth_port_set_tx_coal(port_num, 133000000, MV643XX_TX_COAL);
901
902         netif_start_queue(dev);
903
904         return 0;
905 }
906
907 static void mv643xx_eth_free_tx_rings(struct net_device *dev)
908 {
909         struct mv643xx_private *mp = netdev_priv(dev);
910         unsigned int port_num = mp->port_num;
911         unsigned int curr;
912
913         /* Stop Tx Queues */
914         mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num), 0x0000ff00);
915
916         /* Free outstanding skb's on TX rings */
917         for (curr = 0; mp->tx_ring_skbs && curr < mp->tx_ring_size; curr++) {
918                 if (mp->tx_skb[curr]) {
919                         dev_kfree_skb(mp->tx_skb[curr]);
920                         mp->tx_ring_skbs--;
921                 }
922         }
923         if (mp->tx_ring_skbs)
924                 printk("%s: Error on Tx descriptor free - could not free %d"
925                                 " descriptors\n", dev->name, mp->tx_ring_skbs);
926
927         /* Free TX ring */
928         if (mp->tx_sram_size)
929                 iounmap(mp->p_tx_desc_area);
930         else
931                 dma_free_coherent(NULL, mp->tx_desc_area_size,
932                                 mp->p_tx_desc_area, mp->tx_desc_dma);
933 }
934
935 static void mv643xx_eth_free_rx_rings(struct net_device *dev)
936 {
937         struct mv643xx_private *mp = netdev_priv(dev);
938         unsigned int port_num = mp->port_num;
939         int curr;
940
941         /* Stop RX Queues */
942         mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), 0x0000ff00);
943
944         /* Free preallocated skb's on RX rings */
945         for (curr = 0; mp->rx_ring_skbs && curr < mp->rx_ring_size; curr++) {
946                 if (mp->rx_skb[curr]) {
947                         dev_kfree_skb(mp->rx_skb[curr]);
948                         mp->rx_ring_skbs--;
949                 }
950         }
951
952         if (mp->rx_ring_skbs)
953                 printk(KERN_ERR
954                         "%s: Error in freeing Rx Ring. %d skb's still"
955                         " stuck in RX Ring - ignoring them\n", dev->name,
956                         mp->rx_ring_skbs);
957         /* Free RX ring */
958         if (mp->rx_sram_size)
959                 iounmap(mp->p_rx_desc_area);
960         else
961                 dma_free_coherent(NULL, mp->rx_desc_area_size,
962                                 mp->p_rx_desc_area, mp->rx_desc_dma);
963 }
964
965 /*
966  * mv643xx_eth_stop
967  *
968  * This function is used when closing the network device.
969  * It updates the hardware,
970  * release all memory that holds buffers and descriptors and release the IRQ.
971  * Input :      a pointer to the device structure
972  * Output :     zero if success , nonzero if fails
973  */
974
975 /* Helper function for mv643xx_eth_stop */
976
977 static int mv643xx_eth_real_stop(struct net_device *dev)
978 {
979         struct mv643xx_private *mp = netdev_priv(dev);
980         unsigned int port_num = mp->port_num;
981
982         netif_carrier_off(dev);
983         netif_stop_queue(dev);
984
985         mv643xx_eth_free_tx_rings(dev);
986         mv643xx_eth_free_rx_rings(dev);
987
988         eth_port_reset(mp->port_num);
989
990         /* Disable ethernet port interrupts */
991         mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
992         mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
993
994         /* Mask RX buffer and TX end interrupt */
995         mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), 0);
996
997         /* Mask phy and link status changes interrupts */
998         mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), 0);
999
1000         return 0;
1001 }
1002
1003 static int mv643xx_eth_stop(struct net_device *dev)
1004 {
1005         struct mv643xx_private *mp = netdev_priv(dev);
1006
1007         spin_lock_irq(&mp->lock);
1008
1009         mv643xx_eth_real_stop(dev);
1010
1011         free_irq(dev->irq, dev);
1012         spin_unlock_irq(&mp->lock);
1013
1014         return 0;
1015 }
1016
1017 #ifdef MV643XX_NAPI
1018 static void mv643xx_tx(struct net_device *dev)
1019 {
1020         struct mv643xx_private *mp = netdev_priv(dev);
1021         struct pkt_info pkt_info;
1022
1023         while (eth_tx_return_desc(mp, &pkt_info) == ETH_OK) {
1024                 if (pkt_info.return_info) {
1025                         if (skb_shinfo(pkt_info.return_info)->nr_frags)
1026                                 dma_unmap_page(NULL, pkt_info.buf_ptr,
1027                                                 pkt_info.byte_cnt,
1028                                                 DMA_TO_DEVICE);
1029                         else
1030                                 dma_unmap_single(NULL, pkt_info.buf_ptr,
1031                                                 pkt_info.byte_cnt,
1032                                                 DMA_TO_DEVICE);
1033
1034                         dev_kfree_skb_irq(pkt_info.return_info);
1035                 } else
1036                         dma_unmap_page(NULL, pkt_info.buf_ptr,
1037                                         pkt_info.byte_cnt, DMA_TO_DEVICE);
1038         }
1039
1040         if (netif_queue_stopped(dev) &&
1041                         mp->tx_ring_size > mp->tx_ring_skbs + MAX_DESCS_PER_SKB)
1042                 netif_wake_queue(dev);
1043 }
1044
1045 /*
1046  * mv643xx_poll
1047  *
1048  * This function is used in case of NAPI
1049  */
1050 static int mv643xx_poll(struct net_device *dev, int *budget)
1051 {
1052         struct mv643xx_private *mp = netdev_priv(dev);
1053         int done = 1, orig_budget, work_done;
1054         unsigned int port_num = mp->port_num;
1055         unsigned long flags;
1056
1057 #ifdef MV643XX_TX_FAST_REFILL
1058         if (++mp->tx_clean_threshold > 5) {
1059                 spin_lock_irqsave(&mp->lock, flags);
1060                 mv643xx_tx(dev);
1061                 mp->tx_clean_threshold = 0;
1062                 spin_unlock_irqrestore(&mp->lock, flags);
1063         }
1064 #endif
1065
1066         if ((mv_read(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num)))
1067                                                 != (u32) mp->rx_used_desc_q) {
1068                 orig_budget = *budget;
1069                 if (orig_budget > dev->quota)
1070                         orig_budget = dev->quota;
1071                 work_done = mv643xx_eth_receive_queue(dev, orig_budget);
1072                 mp->rx_task.func(dev);
1073                 *budget -= work_done;
1074                 dev->quota -= work_done;
1075                 if (work_done >= orig_budget)
1076                         done = 0;
1077         }
1078
1079         if (done) {
1080                 spin_lock_irqsave(&mp->lock, flags);
1081                 __netif_rx_complete(dev);
1082                 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0);
1083                 mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0);
1084                 mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num),
1085                                                 INT_CAUSE_UNMASK_ALL);
1086                 mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num),
1087                                                 INT_CAUSE_UNMASK_ALL_EXT);
1088                 spin_unlock_irqrestore(&mp->lock, flags);
1089         }
1090
1091         return done ? 0 : 1;
1092 }
1093 #endif
1094
1095 /*
1096  * mv643xx_eth_start_xmit
1097  *
1098  * This function is queues a packet in the Tx descriptor for
1099  * required port.
1100  *
1101  * Input :      skb - a pointer to socket buffer
1102  *              dev - a pointer to the required port
1103  *
1104  * Output :     zero upon success
1105  */
1106 static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev)
1107 {
1108         struct mv643xx_private *mp = netdev_priv(dev);
1109         struct net_device_stats *stats = &mp->stats;
1110         ETH_FUNC_RET_STATUS status;
1111         unsigned long flags;
1112         struct pkt_info pkt_info;
1113
1114         if (netif_queue_stopped(dev)) {
1115                 printk(KERN_ERR
1116                         "%s: Tried sending packet when interface is stopped\n",
1117                         dev->name);
1118                 return 1;
1119         }
1120
1121         /* This is a hard error, log it. */
1122         if ((mp->tx_ring_size - mp->tx_ring_skbs) <=
1123                                         (skb_shinfo(skb)->nr_frags + 1)) {
1124                 netif_stop_queue(dev);
1125                 printk(KERN_ERR
1126                         "%s: Bug in mv643xx_eth - Trying to transmit when"
1127                         " queue full !\n", dev->name);
1128                 return 1;
1129         }
1130
1131         /* Paranoid check - this shouldn't happen */
1132         if (skb == NULL) {
1133                 stats->tx_dropped++;
1134                 printk(KERN_ERR "mv64320_eth paranoid check failed\n");
1135                 return 1;
1136         }
1137
1138         spin_lock_irqsave(&mp->lock, flags);
1139
1140         /* Update packet info data structure -- DMA owned, first last */
1141 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1142         if (!skb_shinfo(skb)->nr_frags) {
1143 linear:
1144                 if (skb->ip_summed != CHECKSUM_HW) {
1145                         /* Errata BTS #50, IHL must be 5 if no HW checksum */
1146                         pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT |
1147                                            ETH_TX_FIRST_DESC |
1148                                            ETH_TX_LAST_DESC |
1149                                            5 << ETH_TX_IHL_SHIFT;
1150                         pkt_info.l4i_chk = 0;
1151                 } else {
1152
1153                         pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT |
1154                                            ETH_TX_FIRST_DESC |
1155                                            ETH_TX_LAST_DESC |
1156                                            ETH_GEN_TCP_UDP_CHECKSUM |
1157                                            ETH_GEN_IP_V_4_CHECKSUM |
1158                                            skb->nh.iph->ihl << ETH_TX_IHL_SHIFT;
1159                         /* CPU already calculated pseudo header checksum. */
1160                         if (skb->nh.iph->protocol == IPPROTO_UDP) {
1161                                 pkt_info.cmd_sts |= ETH_UDP_FRAME;
1162                                 pkt_info.l4i_chk = skb->h.uh->check;
1163                         } else if (skb->nh.iph->protocol == IPPROTO_TCP)
1164                                 pkt_info.l4i_chk = skb->h.th->check;
1165                         else {
1166                                 printk(KERN_ERR
1167                                         "%s: chksum proto != TCP or UDP\n",
1168                                         dev->name);
1169                                 spin_unlock_irqrestore(&mp->lock, flags);
1170                                 return 1;
1171                         }
1172                 }
1173                 pkt_info.byte_cnt = skb->len;
1174                 pkt_info.buf_ptr = dma_map_single(NULL, skb->data, skb->len,
1175                                                         DMA_TO_DEVICE);
1176                 pkt_info.return_info = skb;
1177                 status = eth_port_send(mp, &pkt_info);
1178                 if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL))
1179                         printk(KERN_ERR "%s: Error on transmitting packet\n",
1180                                                                 dev->name);
1181                 stats->tx_bytes += pkt_info.byte_cnt;
1182         } else {
1183                 unsigned int frag;
1184
1185                 /* Since hardware can't handle unaligned fragments smaller
1186                  * than 9 bytes, if we find any, we linearize the skb
1187                  * and start again.  When I've seen it, it's always been
1188                  * the first frag (probably near the end of the page),
1189                  * but we check all frags to be safe.
1190                  */
1191                 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1192                         skb_frag_t *fragp;
1193
1194                         fragp = &skb_shinfo(skb)->frags[frag];
1195                         if (fragp->size <= 8 && fragp->page_offset & 0x7) {
1196                                 skb_linearize(skb, GFP_ATOMIC);
1197                                 printk(KERN_DEBUG "%s: unaligned tiny fragment"
1198                                                 "%d of %d, fixed\n",
1199                                                 dev->name, frag,
1200                                                 skb_shinfo(skb)->nr_frags);
1201                                 goto linear;
1202                         }
1203                 }
1204
1205                 /* first frag which is skb header */
1206                 pkt_info.byte_cnt = skb_headlen(skb);
1207                 pkt_info.buf_ptr = dma_map_single(NULL, skb->data,
1208                                                         skb_headlen(skb),
1209                                                         DMA_TO_DEVICE);
1210                 pkt_info.l4i_chk = 0;
1211                 pkt_info.return_info = 0;
1212
1213                 if (skb->ip_summed != CHECKSUM_HW)
1214                         /* Errata BTS #50, IHL must be 5 if no HW checksum */
1215                         pkt_info.cmd_sts = ETH_TX_FIRST_DESC |
1216                                            5 << ETH_TX_IHL_SHIFT;
1217                 else {
1218                         pkt_info.cmd_sts = ETH_TX_FIRST_DESC |
1219                                            ETH_GEN_TCP_UDP_CHECKSUM |
1220                                            ETH_GEN_IP_V_4_CHECKSUM |
1221                                            skb->nh.iph->ihl << ETH_TX_IHL_SHIFT;
1222                         /* CPU already calculated pseudo header checksum. */
1223                         if (skb->nh.iph->protocol == IPPROTO_UDP) {
1224                                 pkt_info.cmd_sts |= ETH_UDP_FRAME;
1225                                 pkt_info.l4i_chk = skb->h.uh->check;
1226                         } else if (skb->nh.iph->protocol == IPPROTO_TCP)
1227                                 pkt_info.l4i_chk = skb->h.th->check;
1228                         else {
1229                                 printk(KERN_ERR
1230                                         "%s: chksum proto != TCP or UDP\n",
1231                                         dev->name);
1232                                 spin_unlock_irqrestore(&mp->lock, flags);
1233                                 return 1;
1234                         }
1235                 }
1236
1237                 status = eth_port_send(mp, &pkt_info);
1238                 if (status != ETH_OK) {
1239                         if ((status == ETH_ERROR))
1240                                 printk(KERN_ERR
1241                                         "%s: Error on transmitting packet\n",
1242                                         dev->name);
1243                         if (status == ETH_QUEUE_FULL)
1244                                 printk("Error on Queue Full \n");
1245                         if (status == ETH_QUEUE_LAST_RESOURCE)
1246                                 printk("Tx resource error \n");
1247                 }
1248                 stats->tx_bytes += pkt_info.byte_cnt;
1249
1250                 /* Check for the remaining frags */
1251                 for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) {
1252                         skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag];
1253                         pkt_info.l4i_chk = 0x0000;
1254                         pkt_info.cmd_sts = 0x00000000;
1255
1256                         /* Last Frag enables interrupt and frees the skb */
1257                         if (frag == (skb_shinfo(skb)->nr_frags - 1)) {
1258                                 pkt_info.cmd_sts |= ETH_TX_ENABLE_INTERRUPT |
1259                                                         ETH_TX_LAST_DESC;
1260                                 pkt_info.return_info = skb;
1261                         } else {
1262                                 pkt_info.return_info = 0;
1263                         }
1264                         pkt_info.l4i_chk = 0;
1265                         pkt_info.byte_cnt = this_frag->size;
1266
1267                         pkt_info.buf_ptr = dma_map_page(NULL, this_frag->page,
1268                                                         this_frag->page_offset,
1269                                                         this_frag->size,
1270                                                         DMA_TO_DEVICE);
1271
1272                         status = eth_port_send(mp, &pkt_info);
1273
1274                         if (status != ETH_OK) {
1275                                 if ((status == ETH_ERROR))
1276                                         printk(KERN_ERR "%s: Error on "
1277                                                         "transmitting packet\n",
1278                                                         dev->name);
1279
1280                                 if (status == ETH_QUEUE_LAST_RESOURCE)
1281                                         printk("Tx resource error \n");
1282
1283                                 if (status == ETH_QUEUE_FULL)
1284                                         printk("Queue is full \n");
1285                         }
1286                         stats->tx_bytes += pkt_info.byte_cnt;
1287                 }
1288         }
1289 #else
1290         pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT | ETH_TX_FIRST_DESC |
1291                                                         ETH_TX_LAST_DESC;
1292         pkt_info.l4i_chk = 0;
1293         pkt_info.byte_cnt = skb->len;
1294         pkt_info.buf_ptr = dma_map_single(NULL, skb->data, skb->len,
1295                                                                 DMA_TO_DEVICE);
1296         pkt_info.return_info = skb;
1297         status = eth_port_send(mp, &pkt_info);
1298         if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL))
1299                 printk(KERN_ERR "%s: Error on transmitting packet\n",
1300                                                                 dev->name);
1301         stats->tx_bytes += pkt_info.byte_cnt;
1302 #endif
1303
1304         /* Check if TX queue can handle another skb. If not, then
1305          * signal higher layers to stop requesting TX
1306          */
1307         if (mp->tx_ring_size <= (mp->tx_ring_skbs + MAX_DESCS_PER_SKB))
1308                 /*
1309                  * Stop getting skb's from upper layers.
1310                  * Getting skb's from upper layers will be enabled again after
1311                  * packets are released.
1312                  */
1313                 netif_stop_queue(dev);
1314
1315         /* Update statistics and start of transmittion time */
1316         stats->tx_packets++;
1317         dev->trans_start = jiffies;
1318
1319         spin_unlock_irqrestore(&mp->lock, flags);
1320
1321         return 0;               /* success */
1322 }
1323
1324 /*
1325  * mv643xx_eth_get_stats
1326  *
1327  * Returns a pointer to the interface statistics.
1328  *
1329  * Input :      dev - a pointer to the required interface
1330  *
1331  * Output :     a pointer to the interface's statistics
1332  */
1333
1334 static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev)
1335 {
1336         struct mv643xx_private *mp = netdev_priv(dev);
1337
1338         return &mp->stats;
1339 }
1340
1341 /*/
1342  * mv643xx_eth_probe
1343  *
1344  * First function called after registering the network device.
1345  * It's purpose is to initialize the device as an ethernet device,
1346  * fill the ethernet device structure with pointers * to functions,
1347  * and set the MAC address of the interface
1348  *
1349  * Input :      struct device *
1350  * Output :     -ENOMEM if failed , 0 if success
1351  */
1352 static int mv643xx_eth_probe(struct device *ddev)
1353 {
1354         struct platform_device *pdev = to_platform_device(ddev);
1355         struct mv643xx_eth_platform_data *pd;
1356         int port_num = pdev->id;
1357         struct mv643xx_private *mp;
1358         struct net_device *dev;
1359         u8 *p;
1360         struct resource *res;
1361         int err;
1362
1363         dev = alloc_etherdev(sizeof(struct mv643xx_private));
1364         if (!dev)
1365                 return -ENOMEM;
1366
1367         dev_set_drvdata(ddev, dev);
1368
1369         mp = netdev_priv(dev);
1370
1371         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1372         BUG_ON(!res);
1373         dev->irq = res->start;
1374
1375         mp->port_num = port_num;
1376
1377         dev->open = mv643xx_eth_open;
1378         dev->stop = mv643xx_eth_stop;
1379         dev->hard_start_xmit = mv643xx_eth_start_xmit;
1380         dev->get_stats = mv643xx_eth_get_stats;
1381         dev->set_mac_address = mv643xx_eth_set_mac_address;
1382         dev->set_multicast_list = mv643xx_eth_set_rx_mode;
1383
1384         /* No need to Tx Timeout */
1385         dev->tx_timeout = mv643xx_eth_tx_timeout;
1386 #ifdef MV643XX_NAPI
1387         dev->poll = mv643xx_poll;
1388         dev->weight = 64;
1389 #endif
1390
1391         dev->watchdog_timeo = 2 * HZ;
1392         dev->tx_queue_len = mp->tx_ring_size;
1393         dev->base_addr = 0;
1394         dev->change_mtu = mv643xx_eth_change_mtu;
1395         SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops);
1396
1397 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1398 #ifdef MAX_SKB_FRAGS
1399         /*
1400          * Zero copy can only work if we use Discovery II memory. Else, we will
1401          * have to map the buffers to ISA memory which is only 16 MB
1402          */
1403         dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HW_CSUM;
1404 #endif
1405 #endif
1406
1407         /* Configure the timeout task */
1408         INIT_WORK(&mp->tx_timeout_task,
1409                         (void (*)(void *))mv643xx_eth_tx_timeout_task, dev);
1410
1411         spin_lock_init(&mp->lock);
1412
1413         /* set default config values */
1414         eth_port_uc_addr_get(dev, dev->dev_addr);
1415         mp->port_config = MV643XX_ETH_PORT_CONFIG_DEFAULT_VALUE;
1416         mp->port_config_extend = MV643XX_ETH_PORT_CONFIG_EXTEND_DEFAULT_VALUE;
1417         mp->port_sdma_config = MV643XX_ETH_PORT_SDMA_CONFIG_DEFAULT_VALUE;
1418         mp->port_serial_control = MV643XX_ETH_PORT_SERIAL_CONTROL_DEFAULT_VALUE;
1419         mp->rx_ring_size = MV643XX_ETH_PORT_DEFAULT_RECEIVE_QUEUE_SIZE;
1420         mp->tx_ring_size = MV643XX_ETH_PORT_DEFAULT_TRANSMIT_QUEUE_SIZE;
1421
1422         pd = pdev->dev.platform_data;
1423         if (pd) {
1424                 if (pd->mac_addr != NULL)
1425                         memcpy(dev->dev_addr, pd->mac_addr, 6);
1426
1427                 if (pd->phy_addr || pd->force_phy_addr)
1428                         ethernet_phy_set(port_num, pd->phy_addr);
1429
1430                 if (pd->port_config || pd->force_port_config)
1431                         mp->port_config = pd->port_config;
1432
1433                 if (pd->port_config_extend || pd->force_port_config_extend)
1434                         mp->port_config_extend = pd->port_config_extend;
1435
1436                 if (pd->port_sdma_config || pd->force_port_sdma_config)
1437                         mp->port_sdma_config = pd->port_sdma_config;
1438
1439                 if (pd->port_serial_control || pd->force_port_serial_control)
1440                         mp->port_serial_control = pd->port_serial_control;
1441
1442                 if (pd->rx_queue_size)
1443                         mp->rx_ring_size = pd->rx_queue_size;
1444
1445                 if (pd->tx_queue_size)
1446                         mp->tx_ring_size = pd->tx_queue_size;
1447
1448                 if (pd->tx_sram_size) {
1449                         mp->tx_sram_size = pd->tx_sram_size;
1450                         mp->tx_sram_addr = pd->tx_sram_addr;
1451                 }
1452
1453                 if (pd->rx_sram_size) {
1454                         mp->rx_sram_size = pd->rx_sram_size;
1455                         mp->rx_sram_addr = pd->rx_sram_addr;
1456                 }
1457         }
1458
1459         err = ethernet_phy_detect(port_num);
1460         if (err) {
1461                 pr_debug("MV643xx ethernet port %d: "
1462                                         "No PHY detected at addr %d\n",
1463                                         port_num, ethernet_phy_get(port_num));
1464                 return err;
1465         }
1466
1467         err = register_netdev(dev);
1468         if (err)
1469                 goto out;
1470
1471         p = dev->dev_addr;
1472         printk(KERN_NOTICE
1473                 "%s: port %d with MAC address %02x:%02x:%02x:%02x:%02x:%02x\n",
1474                 dev->name, port_num, p[0], p[1], p[2], p[3], p[4], p[5]);
1475
1476         if (dev->features & NETIF_F_SG)
1477                 printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name);
1478
1479         if (dev->features & NETIF_F_IP_CSUM)
1480                 printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n",
1481                                                                 dev->name);
1482
1483 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
1484         printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name);
1485 #endif
1486
1487 #ifdef MV643XX_COAL
1488         printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n",
1489                                                                 dev->name);
1490 #endif
1491
1492 #ifdef MV643XX_NAPI
1493         printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name);
1494 #endif
1495
1496         return 0;
1497
1498 out:
1499         free_netdev(dev);
1500
1501         return err;
1502 }
1503
1504 static int mv643xx_eth_remove(struct device *ddev)
1505 {
1506         struct net_device *dev = dev_get_drvdata(ddev);
1507
1508         unregister_netdev(dev);
1509         flush_scheduled_work();
1510
1511         free_netdev(dev);
1512         dev_set_drvdata(ddev, NULL);
1513         return 0;
1514 }
1515
1516 static int mv643xx_eth_shared_probe(struct device *ddev)
1517 {
1518         struct platform_device *pdev = to_platform_device(ddev);
1519         struct resource *res;
1520
1521         printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n");
1522
1523         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1524         if (res == NULL)
1525                 return -ENODEV;
1526
1527         mv643xx_eth_shared_base = ioremap(res->start,
1528                                                 MV643XX_ETH_SHARED_REGS_SIZE);
1529         if (mv643xx_eth_shared_base == NULL)
1530                 return -ENOMEM;
1531
1532         return 0;
1533
1534 }
1535
1536 static int mv643xx_eth_shared_remove(struct device *ddev)
1537 {
1538         iounmap(mv643xx_eth_shared_base);
1539         mv643xx_eth_shared_base = NULL;
1540
1541         return 0;
1542 }
1543
1544 static struct device_driver mv643xx_eth_driver = {
1545         .name = MV643XX_ETH_NAME,
1546         .bus = &platform_bus_type,
1547         .probe = mv643xx_eth_probe,
1548         .remove = mv643xx_eth_remove,
1549 };
1550
1551 static struct device_driver mv643xx_eth_shared_driver = {
1552         .name = MV643XX_ETH_SHARED_NAME,
1553         .bus = &platform_bus_type,
1554         .probe = mv643xx_eth_shared_probe,
1555         .remove = mv643xx_eth_shared_remove,
1556 };
1557
1558 /*
1559  * mv643xx_init_module
1560  *
1561  * Registers the network drivers into the Linux kernel
1562  *
1563  * Input :      N/A
1564  *
1565  * Output :     N/A
1566  */
1567 static int __init mv643xx_init_module(void)
1568 {
1569         int rc;
1570
1571         rc = driver_register(&mv643xx_eth_shared_driver);
1572         if (!rc) {
1573                 rc = driver_register(&mv643xx_eth_driver);
1574                 if (rc)
1575                         driver_unregister(&mv643xx_eth_shared_driver);
1576         }
1577         return rc;
1578 }
1579
1580 /*
1581  * mv643xx_cleanup_module
1582  *
1583  * Registers the network drivers into the Linux kernel
1584  *
1585  * Input :      N/A
1586  *
1587  * Output :     N/A
1588  */
1589 static void __exit mv643xx_cleanup_module(void)
1590 {
1591         driver_unregister(&mv643xx_eth_driver);
1592         driver_unregister(&mv643xx_eth_shared_driver);
1593 }
1594
1595 module_init(mv643xx_init_module);
1596 module_exit(mv643xx_cleanup_module);
1597
1598 MODULE_LICENSE("GPL");
1599 MODULE_AUTHOR(  "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani"
1600                 " and Dale Farnsworth");
1601 MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX");
1602
1603 /*
1604  * The second part is the low level driver of the gigE ethernet ports.
1605  */
1606
1607 /*
1608  * Marvell's Gigabit Ethernet controller low level driver
1609  *
1610  * DESCRIPTION:
1611  *      This file introduce low level API to Marvell's Gigabit Ethernet
1612  *              controller. This Gigabit Ethernet Controller driver API controls
1613  *              1) Operations (i.e. port init, start, reset etc').
1614  *              2) Data flow (i.e. port send, receive etc').
1615  *              Each Gigabit Ethernet port is controlled via
1616  *              struct mv643xx_private.
1617  *              This struct includes user configuration information as well as
1618  *              driver internal data needed for its operations.
1619  *
1620  *              Supported Features:
1621  *              - This low level driver is OS independent. Allocating memory for
1622  *                the descriptor rings and buffers are not within the scope of
1623  *                this driver.
1624  *              - The user is free from Rx/Tx queue managing.
1625  *              - This low level driver introduce functionality API that enable
1626  *                the to operate Marvell's Gigabit Ethernet Controller in a
1627  *                convenient way.
1628  *              - Simple Gigabit Ethernet port operation API.
1629  *              - Simple Gigabit Ethernet port data flow API.
1630  *              - Data flow and operation API support per queue functionality.
1631  *              - Support cached descriptors for better performance.
1632  *              - Enable access to all four DRAM banks and internal SRAM memory
1633  *                spaces.
1634  *              - PHY access and control API.
1635  *              - Port control register configuration API.
1636  *              - Full control over Unicast and Multicast MAC configurations.
1637  *
1638  *              Operation flow:
1639  *
1640  *              Initialization phase
1641  *              This phase complete the initialization of the the
1642  *              mv643xx_private struct.
1643  *              User information regarding port configuration has to be set
1644  *              prior to calling the port initialization routine.
1645  *
1646  *              In this phase any port Tx/Rx activity is halted, MIB counters
1647  *              are cleared, PHY address is set according to user parameter and
1648  *              access to DRAM and internal SRAM memory spaces.
1649  *
1650  *              Driver ring initialization
1651  *              Allocating memory for the descriptor rings and buffers is not
1652  *              within the scope of this driver. Thus, the user is required to
1653  *              allocate memory for the descriptors ring and buffers. Those
1654  *              memory parameters are used by the Rx and Tx ring initialization
1655  *              routines in order to curve the descriptor linked list in a form
1656  *              of a ring.
1657  *              Note: Pay special attention to alignment issues when using
1658  *              cached descriptors/buffers. In this phase the driver store
1659  *              information in the mv643xx_private struct regarding each queue
1660  *              ring.
1661  *
1662  *              Driver start
1663  *              This phase prepares the Ethernet port for Rx and Tx activity.
1664  *              It uses the information stored in the mv643xx_private struct to
1665  *              initialize the various port registers.
1666  *
1667  *              Data flow:
1668  *              All packet references to/from the driver are done using
1669  *              struct pkt_info.
1670  *              This struct is a unified struct used with Rx and Tx operations.
1671  *              This way the user is not required to be familiar with neither
1672  *              Tx nor Rx descriptors structures.
1673  *              The driver's descriptors rings are management by indexes.
1674  *              Those indexes controls the ring resources and used to indicate
1675  *              a SW resource error:
1676  *              'current'
1677  *              This index points to the current available resource for use. For
1678  *              example in Rx process this index will point to the descriptor
1679  *              that will be passed to the user upon calling the receive
1680  *              routine.  In Tx process, this index will point to the descriptor
1681  *              that will be assigned with the user packet info and transmitted.
1682  *              'used'
1683  *              This index points to the descriptor that need to restore its
1684  *              resources. For example in Rx process, using the Rx buffer return
1685  *              API will attach the buffer returned in packet info to the
1686  *              descriptor pointed by 'used'. In Tx process, using the Tx
1687  *              descriptor return will merely return the user packet info with
1688  *              the command status of the transmitted buffer pointed by the
1689  *              'used' index. Nevertheless, it is essential to use this routine
1690  *              to update the 'used' index.
1691  *              'first'
1692  *              This index supports Tx Scatter-Gather. It points to the first
1693  *              descriptor of a packet assembled of multiple buffers. For
1694  *              example when in middle of Such packet we have a Tx resource
1695  *              error the 'curr' index get the value of 'first' to indicate
1696  *              that the ring returned to its state before trying to transmit
1697  *              this packet.
1698  *
1699  *              Receive operation:
1700  *              The eth_port_receive API set the packet information struct,
1701  *              passed by the caller, with received information from the
1702  *              'current' SDMA descriptor.
1703  *              It is the user responsibility to return this resource back
1704  *              to the Rx descriptor ring to enable the reuse of this source.
1705  *              Return Rx resource is done using the eth_rx_return_buff API.
1706  *
1707  *              Transmit operation:
1708  *              The eth_port_send API supports Scatter-Gather which enables to
1709  *              send a packet spanned over multiple buffers. This means that
1710  *              for each packet info structure given by the user and put into
1711  *              the Tx descriptors ring, will be transmitted only if the 'LAST'
1712  *              bit will be set in the packet info command status field. This
1713  *              API also consider restriction regarding buffer alignments and
1714  *              sizes.
1715  *              The user must return a Tx resource after ensuring the buffer
1716  *              has been transmitted to enable the Tx ring indexes to update.
1717  *
1718  *              BOARD LAYOUT
1719  *              This device is on-board.  No jumper diagram is necessary.
1720  *
1721  *              EXTERNAL INTERFACE
1722  *
1723  *      Prior to calling the initialization routine eth_port_init() the user
1724  *      must set the following fields under mv643xx_private struct:
1725  *      port_num                User Ethernet port number.
1726  *      port_mac_addr[6]        User defined port MAC address.
1727  *      port_config             User port configuration value.
1728  *      port_config_extend      User port config extend value.
1729  *      port_sdma_config        User port SDMA config value.
1730  *      port_serial_control     User port serial control value.
1731  *
1732  *              This driver data flow is done using the struct pkt_info which
1733  *              is a unified struct for Rx and Tx operations:
1734  *
1735  *              byte_cnt        Tx/Rx descriptor buffer byte count.
1736  *              l4i_chk         CPU provided TCP Checksum. For Tx operation
1737  *                              only.
1738  *              cmd_sts         Tx/Rx descriptor command status.
1739  *              buf_ptr         Tx/Rx descriptor buffer pointer.
1740  *              return_info     Tx/Rx user resource return information.
1741  */
1742
1743 /* defines */
1744 /* SDMA command macros */
1745 #define ETH_ENABLE_TX_QUEUE(eth_port) \
1746         mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port), 1)
1747
1748 /* locals */
1749
1750 /* PHY routines */
1751 static int ethernet_phy_get(unsigned int eth_port_num);
1752 static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr);
1753
1754 /* Ethernet Port routines */
1755 static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble,
1756                                                                 int option);
1757
1758 /*
1759  * eth_port_init - Initialize the Ethernet port driver
1760  *
1761  * DESCRIPTION:
1762  *      This function prepares the ethernet port to start its activity:
1763  *      1) Completes the ethernet port driver struct initialization toward port
1764  *              start routine.
1765  *      2) Resets the device to a quiescent state in case of warm reboot.
1766  *      3) Enable SDMA access to all four DRAM banks as well as internal SRAM.
1767  *      4) Clean MAC tables. The reset status of those tables is unknown.
1768  *      5) Set PHY address.
1769  *      Note: Call this routine prior to eth_port_start routine and after
1770  *      setting user values in the user fields of Ethernet port control
1771  *      struct.
1772  *
1773  * INPUT:
1774  *      struct mv643xx_private *mp      Ethernet port control struct
1775  *
1776  * OUTPUT:
1777  *      See description.
1778  *
1779  * RETURN:
1780  *      None.
1781  */
1782 static void eth_port_init(struct mv643xx_private *mp)
1783 {
1784         mp->port_rx_queue_command = 0;
1785         mp->port_tx_queue_command = 0;
1786
1787         mp->rx_resource_err = 0;
1788         mp->tx_resource_err = 0;
1789
1790         eth_port_reset(mp->port_num);
1791
1792         eth_port_init_mac_tables(mp->port_num);
1793
1794         ethernet_phy_reset(mp->port_num);
1795 }
1796
1797 /*
1798  * eth_port_start - Start the Ethernet port activity.
1799  *
1800  * DESCRIPTION:
1801  *      This routine prepares the Ethernet port for Rx and Tx activity:
1802  *       1. Initialize Tx and Rx Current Descriptor Pointer for each queue that
1803  *          has been initialized a descriptor's ring (using
1804  *          ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx)
1805  *       2. Initialize and enable the Ethernet configuration port by writing to
1806  *          the port's configuration and command registers.
1807  *       3. Initialize and enable the SDMA by writing to the SDMA's
1808  *          configuration and command registers.  After completing these steps,
1809  *          the ethernet port SDMA can starts to perform Rx and Tx activities.
1810  *
1811  *      Note: Each Rx and Tx queue descriptor's list must be initialized prior
1812  *      to calling this function (use ether_init_tx_desc_ring for Tx queues
1813  *      and ether_init_rx_desc_ring for Rx queues).
1814  *
1815  * INPUT:
1816  *      struct mv643xx_private *mp      Ethernet port control struct
1817  *
1818  * OUTPUT:
1819  *      Ethernet port is ready to receive and transmit.
1820  *
1821  * RETURN:
1822  *      None.
1823  */
1824 static void eth_port_start(struct mv643xx_private *mp)
1825 {
1826         unsigned int port_num = mp->port_num;
1827         int tx_curr_desc, rx_curr_desc;
1828
1829         /* Assignment of Tx CTRP of given queue */
1830         tx_curr_desc = mp->tx_curr_desc_q;
1831         mv_write(MV643XX_ETH_TX_CURRENT_QUEUE_DESC_PTR_0(port_num),
1832                 (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc));
1833
1834         /* Assignment of Rx CRDP of given queue */
1835         rx_curr_desc = mp->rx_curr_desc_q;
1836         mv_write(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num),
1837                 (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc));
1838
1839         /* Add the assigned Ethernet address to the port's address table */
1840         eth_port_uc_addr_set(port_num, mp->port_mac_addr);
1841
1842         /* Assign port configuration and command. */
1843         mv_write(MV643XX_ETH_PORT_CONFIG_REG(port_num), mp->port_config);
1844
1845         mv_write(MV643XX_ETH_PORT_CONFIG_EXTEND_REG(port_num),
1846                                                 mp->port_config_extend);
1847
1848
1849         /* Increase the Rx side buffer size if supporting GigE */
1850         if (mp->port_serial_control & MV643XX_ETH_SET_GMII_SPEED_TO_1000)
1851                 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
1852                         (mp->port_serial_control & 0xfff1ffff) | (0x5 << 17));
1853         else
1854                 mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
1855                                                 mp->port_serial_control);
1856
1857         mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num),
1858                 mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num)) |
1859                                                 MV643XX_ETH_SERIAL_PORT_ENABLE);
1860
1861         /* Assign port SDMA configuration */
1862         mv_write(MV643XX_ETH_SDMA_CONFIG_REG(port_num),
1863                                                         mp->port_sdma_config);
1864
1865         /* Enable port Rx. */
1866         mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
1867                                                 mp->port_rx_queue_command);
1868
1869         /* Disable port bandwidth limits by clearing MTU register */
1870         mv_write(MV643XX_ETH_MAXIMUM_TRANSMIT_UNIT(port_num), 0);
1871 }
1872
1873 /*
1874  * eth_port_uc_addr_set - This function Set the port Unicast address.
1875  *
1876  * DESCRIPTION:
1877  *              This function Set the port Ethernet MAC address.
1878  *
1879  * INPUT:
1880  *      unsigned int    eth_port_num    Port number.
1881  *      char *          p_addr          Address to be set
1882  *
1883  * OUTPUT:
1884  *      Set MAC address low and high registers. also calls eth_port_uc_addr()
1885  *      To set the unicast table with the proper information.
1886  *
1887  * RETURN:
1888  *      N/A.
1889  *
1890  */
1891 static void eth_port_uc_addr_set(unsigned int eth_port_num,
1892                                                         unsigned char *p_addr)
1893 {
1894         unsigned int mac_h;
1895         unsigned int mac_l;
1896
1897         mac_l = (p_addr[4] << 8) | (p_addr[5]);
1898         mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) |
1899                                                         (p_addr[3] << 0);
1900
1901         mv_write(MV643XX_ETH_MAC_ADDR_LOW(eth_port_num), mac_l);
1902         mv_write(MV643XX_ETH_MAC_ADDR_HIGH(eth_port_num), mac_h);
1903
1904         /* Accept frames of this address */
1905         eth_port_uc_addr(eth_port_num, p_addr[5], ACCEPT_MAC_ADDR);
1906
1907         return;
1908 }
1909
1910 /*
1911  * eth_port_uc_addr_get - This function retrieves the port Unicast address
1912  * (MAC address) from the ethernet hw registers.
1913  *
1914  * DESCRIPTION:
1915  *              This function retrieves the port Ethernet MAC address.
1916  *
1917  * INPUT:
1918  *      unsigned int    eth_port_num    Port number.
1919  *      char            *MacAddr        pointer where the MAC address is stored
1920  *
1921  * OUTPUT:
1922  *      Copy the MAC address to the location pointed to by MacAddr
1923  *
1924  * RETURN:
1925  *      N/A.
1926  *
1927  */
1928 static void eth_port_uc_addr_get(struct net_device *dev, unsigned char *p_addr)
1929 {
1930         struct mv643xx_private *mp = netdev_priv(dev);
1931         unsigned int mac_h;
1932         unsigned int mac_l;
1933
1934         mac_h = mv_read(MV643XX_ETH_MAC_ADDR_HIGH(mp->port_num));
1935         mac_l = mv_read(MV643XX_ETH_MAC_ADDR_LOW(mp->port_num));
1936
1937         p_addr[0] = (mac_h >> 24) & 0xff;
1938         p_addr[1] = (mac_h >> 16) & 0xff;
1939         p_addr[2] = (mac_h >> 8) & 0xff;
1940         p_addr[3] = mac_h & 0xff;
1941         p_addr[4] = (mac_l >> 8) & 0xff;
1942         p_addr[5] = mac_l & 0xff;
1943 }
1944
1945 /*
1946  * eth_port_uc_addr - This function Set the port unicast address table
1947  *
1948  * DESCRIPTION:
1949  *      This function locates the proper entry in the Unicast table for the
1950  *      specified MAC nibble and sets its properties according to function
1951  *      parameters.
1952  *
1953  * INPUT:
1954  *      unsigned int    eth_port_num    Port number.
1955  *      unsigned char   uc_nibble       Unicast MAC Address last nibble.
1956  *      int             option          0 = Add, 1 = remove address.
1957  *
1958  * OUTPUT:
1959  *      This function add/removes MAC addresses from the port unicast address
1960  *      table.
1961  *
1962  * RETURN:
1963  *      true is output succeeded.
1964  *      false if option parameter is invalid.
1965  *
1966  */
1967 static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble,
1968                                                                 int option)
1969 {
1970         unsigned int unicast_reg;
1971         unsigned int tbl_offset;
1972         unsigned int reg_offset;
1973
1974         /* Locate the Unicast table entry */
1975         uc_nibble = (0xf & uc_nibble);
1976         tbl_offset = (uc_nibble / 4) * 4;       /* Register offset from unicast table base */
1977         reg_offset = uc_nibble % 4;     /* Entry offset within the above register */
1978
1979         switch (option) {
1980         case REJECT_MAC_ADDR:
1981                 /* Clear accepts frame bit at given unicast DA table entry */
1982                 unicast_reg = mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
1983                                                 (eth_port_num) + tbl_offset));
1984
1985                 unicast_reg &= (0x0E << (8 * reg_offset));
1986
1987                 mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
1988                                 (eth_port_num) + tbl_offset), unicast_reg);
1989                 break;
1990
1991         case ACCEPT_MAC_ADDR:
1992                 /* Set accepts frame bit at unicast DA filter table entry */
1993                 unicast_reg =
1994                         mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
1995                                                 (eth_port_num) + tbl_offset));
1996
1997                 unicast_reg |= (0x01 << (8 * reg_offset));
1998
1999                 mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
2000                                 (eth_port_num) + tbl_offset), unicast_reg);
2001
2002                 break;
2003
2004         default:
2005                 return 0;
2006         }
2007
2008         return 1;
2009 }
2010
2011 /*
2012  * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables
2013  *
2014  * DESCRIPTION:
2015  *      Go through all the DA filter tables (Unicast, Special Multicast &
2016  *      Other Multicast) and set each entry to 0.
2017  *
2018  * INPUT:
2019  *      unsigned int    eth_port_num    Ethernet Port number.
2020  *
2021  * OUTPUT:
2022  *      Multicast and Unicast packets are rejected.
2023  *
2024  * RETURN:
2025  *      None.
2026  */
2027 static void eth_port_init_mac_tables(unsigned int eth_port_num)
2028 {
2029         int table_index;
2030
2031         /* Clear DA filter unicast table (Ex_dFUT) */
2032         for (table_index = 0; table_index <= 0xC; table_index += 4)
2033                 mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE
2034                                         (eth_port_num) + table_index), 0);
2035
2036         for (table_index = 0; table_index <= 0xFC; table_index += 4) {
2037                 /* Clear DA filter special multicast table (Ex_dFSMT) */
2038                 mv_write((MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE
2039                                         (eth_port_num) + table_index), 0);
2040                 /* Clear DA filter other multicast table (Ex_dFOMT) */
2041                 mv_write((MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE
2042                                         (eth_port_num) + table_index), 0);
2043         }
2044 }
2045
2046 /*
2047  * eth_clear_mib_counters - Clear all MIB counters
2048  *
2049  * DESCRIPTION:
2050  *      This function clears all MIB counters of a specific ethernet port.
2051  *      A read from the MIB counter will reset the counter.
2052  *
2053  * INPUT:
2054  *      unsigned int    eth_port_num    Ethernet Port number.
2055  *
2056  * OUTPUT:
2057  *      After reading all MIB counters, the counters resets.
2058  *
2059  * RETURN:
2060  *      MIB counter value.
2061  *
2062  */
2063 static void eth_clear_mib_counters(unsigned int eth_port_num)
2064 {
2065         int i;
2066
2067         /* Perform dummy reads from MIB counters */
2068         for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION;
2069                                                                         i += 4)
2070                 mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(eth_port_num) + i);
2071 }
2072
2073 static inline u32 read_mib(struct mv643xx_private *mp, int offset)
2074 {
2075         return mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(mp->port_num) + offset);
2076 }
2077
2078 static void eth_update_mib_counters(struct mv643xx_private *mp)
2079 {
2080         struct mv643xx_mib_counters *p = &mp->mib_counters;
2081         int offset;
2082
2083         p->good_octets_received +=
2084                 read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW);
2085         p->good_octets_received +=
2086                 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32;
2087
2088         for (offset = ETH_MIB_BAD_OCTETS_RECEIVED;
2089                         offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS;
2090                         offset += 4)
2091                 *(u32 *)((char *)p + offset) = read_mib(mp, offset);
2092
2093         p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW);
2094         p->good_octets_sent +=
2095                 (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32;
2096
2097         for (offset = ETH_MIB_GOOD_FRAMES_SENT;
2098                         offset <= ETH_MIB_LATE_COLLISION;
2099                         offset += 4)
2100                 *(u32 *)((char *)p + offset) = read_mib(mp, offset);
2101 }
2102
2103 /*
2104  * ethernet_phy_detect - Detect whether a phy is present
2105  *
2106  * DESCRIPTION:
2107  *      This function tests whether there is a PHY present on
2108  *      the specified port.
2109  *
2110  * INPUT:
2111  *      unsigned int    eth_port_num    Ethernet Port number.
2112  *
2113  * OUTPUT:
2114  *      None
2115  *
2116  * RETURN:
2117  *      0 on success
2118  *      -ENODEV on failure
2119  *
2120  */
2121 static int ethernet_phy_detect(unsigned int port_num)
2122 {
2123         unsigned int phy_reg_data0;
2124         int auto_neg;
2125
2126         eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
2127         auto_neg = phy_reg_data0 & 0x1000;
2128         phy_reg_data0 ^= 0x1000;        /* invert auto_neg */
2129         eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
2130
2131         eth_port_read_smi_reg(port_num, 0, &phy_reg_data0);
2132         if ((phy_reg_data0 & 0x1000) == auto_neg)
2133                 return -ENODEV;                         /* change didn't take */
2134
2135         phy_reg_data0 ^= 0x1000;
2136         eth_port_write_smi_reg(port_num, 0, phy_reg_data0);
2137         return 0;
2138 }
2139
2140 /*
2141  * ethernet_phy_get - Get the ethernet port PHY address.
2142  *
2143  * DESCRIPTION:
2144  *      This routine returns the given ethernet port PHY address.
2145  *
2146  * INPUT:
2147  *      unsigned int    eth_port_num    Ethernet Port number.
2148  *
2149  * OUTPUT:
2150  *      None.
2151  *
2152  * RETURN:
2153  *      PHY address.
2154  *
2155  */
2156 static int ethernet_phy_get(unsigned int eth_port_num)
2157 {
2158         unsigned int reg_data;
2159
2160         reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
2161
2162         return ((reg_data >> (5 * eth_port_num)) & 0x1f);
2163 }
2164
2165 /*
2166  * ethernet_phy_set - Set the ethernet port PHY address.
2167  *
2168  * DESCRIPTION:
2169  *      This routine sets the given ethernet port PHY address.
2170  *
2171  * INPUT:
2172  *      unsigned int    eth_port_num    Ethernet Port number.
2173  *      int             phy_addr        PHY address.
2174  *
2175  * OUTPUT:
2176  *      None.
2177  *
2178  * RETURN:
2179  *      None.
2180  *
2181  */
2182 static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr)
2183 {
2184         u32 reg_data;
2185         int addr_shift = 5 * eth_port_num;
2186
2187         reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG);
2188         reg_data &= ~(0x1f << addr_shift);
2189         reg_data |= (phy_addr & 0x1f) << addr_shift;
2190         mv_write(MV643XX_ETH_PHY_ADDR_REG, reg_data);
2191 }
2192
2193 /*
2194  * ethernet_phy_reset - Reset Ethernet port PHY.
2195  *
2196  * DESCRIPTION:
2197  *      This routine utilizes the SMI interface to reset the ethernet port PHY.
2198  *
2199  * INPUT:
2200  *      unsigned int    eth_port_num    Ethernet Port number.
2201  *
2202  * OUTPUT:
2203  *      The PHY is reset.
2204  *
2205  * RETURN:
2206  *      None.
2207  *
2208  */
2209 static void ethernet_phy_reset(unsigned int eth_port_num)
2210 {
2211         unsigned int phy_reg_data;
2212
2213         /* Reset the PHY */
2214         eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data);
2215         phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */
2216         eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data);
2217 }
2218
2219 /*
2220  * eth_port_reset - Reset Ethernet port
2221  *
2222  * DESCRIPTION:
2223  *      This routine resets the chip by aborting any SDMA engine activity and
2224  *      clearing the MIB counters. The Receiver and the Transmit unit are in
2225  *      idle state after this command is performed and the port is disabled.
2226  *
2227  * INPUT:
2228  *      unsigned int    eth_port_num    Ethernet Port number.
2229  *
2230  * OUTPUT:
2231  *      Channel activity is halted.
2232  *
2233  * RETURN:
2234  *      None.
2235  *
2236  */
2237 static void eth_port_reset(unsigned int port_num)
2238 {
2239         unsigned int reg_data;
2240
2241         /* Stop Tx port activity. Check port Tx activity. */
2242         reg_data = mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num));
2243
2244         if (reg_data & 0xFF) {
2245                 /* Issue stop command for active channels only */
2246                 mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num),
2247                                                         (reg_data << 8));
2248
2249                 /* Wait for all Tx activity to terminate. */
2250                 /* Check port cause register that all Tx queues are stopped */
2251                 while (mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num))
2252                                                                         & 0xFF)
2253                         udelay(10);
2254         }
2255
2256         /* Stop Rx port activity. Check port Rx activity. */
2257         reg_data = mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num));
2258
2259         if (reg_data & 0xFF) {
2260                 /* Issue stop command for active channels only */
2261                 mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num),
2262                                                         (reg_data << 8));
2263
2264                 /* Wait for all Rx activity to terminate. */
2265                 /* Check port cause register that all Rx queues are stopped */
2266                 while (mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num))
2267                                                                         & 0xFF)
2268                         udelay(10);
2269         }
2270
2271         /* Clear all MIB counters */
2272         eth_clear_mib_counters(port_num);
2273
2274         /* Reset the Enable bit in the Configuration Register */
2275         reg_data = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
2276         reg_data &= ~MV643XX_ETH_SERIAL_PORT_ENABLE;
2277         mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), reg_data);
2278 }
2279
2280
2281 static int eth_port_autoneg_supported(unsigned int eth_port_num)
2282 {
2283         unsigned int phy_reg_data0;
2284
2285         eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data0);
2286
2287         return phy_reg_data0 & 0x1000;
2288 }
2289
2290 static int eth_port_link_is_up(unsigned int eth_port_num)
2291 {
2292         unsigned int phy_reg_data1;
2293
2294         eth_port_read_smi_reg(eth_port_num, 1, &phy_reg_data1);
2295
2296         if (eth_port_autoneg_supported(eth_port_num)) {
2297                 if (phy_reg_data1 & 0x20)       /* auto-neg complete */
2298                         return 1;
2299         } else if (phy_reg_data1 & 0x4)         /* link up */
2300                 return 1;
2301
2302         return 0;
2303 }
2304
2305 /*
2306  * eth_port_read_smi_reg - Read PHY registers
2307  *
2308  * DESCRIPTION:
2309  *      This routine utilize the SMI interface to interact with the PHY in
2310  *      order to perform PHY register read.
2311  *
2312  * INPUT:
2313  *      unsigned int    port_num        Ethernet Port number.
2314  *      unsigned int    phy_reg         PHY register address offset.
2315  *      unsigned int    *value          Register value buffer.
2316  *
2317  * OUTPUT:
2318  *      Write the value of a specified PHY register into given buffer.
2319  *
2320  * RETURN:
2321  *      false if the PHY is busy or read data is not in valid state.
2322  *      true otherwise.
2323  *
2324  */
2325 static void eth_port_read_smi_reg(unsigned int port_num,
2326                                 unsigned int phy_reg, unsigned int *value)
2327 {
2328         int phy_addr = ethernet_phy_get(port_num);
2329         unsigned long flags;
2330         int i;
2331
2332         /* the SMI register is a shared resource */
2333         spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
2334
2335         /* wait for the SMI register to become available */
2336         for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
2337                 if (i == PHY_WAIT_ITERATIONS) {
2338                         printk("mv643xx PHY busy timeout, port %d\n", port_num);
2339                         goto out;
2340                 }
2341                 udelay(PHY_WAIT_MICRO_SECONDS);
2342         }
2343
2344         mv_write(MV643XX_ETH_SMI_REG,
2345                 (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ);
2346
2347         /* now wait for the data to be valid */
2348         for (i = 0; !(mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_READ_VALID); i++) {
2349                 if (i == PHY_WAIT_ITERATIONS) {
2350                         printk("mv643xx PHY read timeout, port %d\n", port_num);
2351                         goto out;
2352                 }
2353                 udelay(PHY_WAIT_MICRO_SECONDS);
2354         }
2355
2356         *value = mv_read(MV643XX_ETH_SMI_REG) & 0xffff;
2357 out:
2358         spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
2359 }
2360
2361 /*
2362  * eth_port_write_smi_reg - Write to PHY registers
2363  *
2364  * DESCRIPTION:
2365  *      This routine utilize the SMI interface to interact with the PHY in
2366  *      order to perform writes to PHY registers.
2367  *
2368  * INPUT:
2369  *      unsigned int    eth_port_num    Ethernet Port number.
2370  *      unsigned int    phy_reg         PHY register address offset.
2371  *      unsigned int    value           Register value.
2372  *
2373  * OUTPUT:
2374  *      Write the given value to the specified PHY register.
2375  *
2376  * RETURN:
2377  *      false if the PHY is busy.
2378  *      true otherwise.
2379  *
2380  */
2381 static void eth_port_write_smi_reg(unsigned int eth_port_num,
2382                                    unsigned int phy_reg, unsigned int value)
2383 {
2384         int phy_addr;
2385         int i;
2386         unsigned long flags;
2387
2388         phy_addr = ethernet_phy_get(eth_port_num);
2389
2390         /* the SMI register is a shared resource */
2391         spin_lock_irqsave(&mv643xx_eth_phy_lock, flags);
2392
2393         /* wait for the SMI register to become available */
2394         for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) {
2395                 if (i == PHY_WAIT_ITERATIONS) {
2396                         printk("mv643xx PHY busy timeout, port %d\n",
2397                                                                 eth_port_num);
2398                         goto out;
2399                 }
2400                 udelay(PHY_WAIT_MICRO_SECONDS);
2401         }
2402
2403         mv_write(MV643XX_ETH_SMI_REG, (phy_addr << 16) | (phy_reg << 21) |
2404                                 ETH_SMI_OPCODE_WRITE | (value & 0xffff));
2405 out:
2406         spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags);
2407 }
2408
2409 /*
2410  * eth_port_send - Send an Ethernet packet
2411  *
2412  * DESCRIPTION:
2413  *      This routine send a given packet described by p_pktinfo parameter. It
2414  *      supports transmitting of a packet spaned over multiple buffers. The
2415  *      routine updates 'curr' and 'first' indexes according to the packet
2416  *      segment passed to the routine. In case the packet segment is first,
2417  *      the 'first' index is update. In any case, the 'curr' index is updated.
2418  *      If the routine get into Tx resource error it assigns 'curr' index as
2419  *      'first'. This way the function can abort Tx process of multiple
2420  *      descriptors per packet.
2421  *
2422  * INPUT:
2423  *      struct mv643xx_private  *mp             Ethernet Port Control srtuct.
2424  *      struct pkt_info         *p_pkt_info     User packet buffer.
2425  *
2426  * OUTPUT:
2427  *      Tx ring 'curr' and 'first' indexes are updated.
2428  *
2429  * RETURN:
2430  *      ETH_QUEUE_FULL in case of Tx resource error.
2431  *      ETH_ERROR in case the routine can not access Tx desc ring.
2432  *      ETH_QUEUE_LAST_RESOURCE if the routine uses the last Tx resource.
2433  *      ETH_OK otherwise.
2434  *
2435  */
2436 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2437 /*
2438  * Modified to include the first descriptor pointer in case of SG
2439  */
2440 static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp,
2441                                          struct pkt_info *p_pkt_info)
2442 {
2443         int tx_desc_curr, tx_desc_used, tx_first_desc, tx_next_desc;
2444         struct eth_tx_desc *current_descriptor;
2445         struct eth_tx_desc *first_descriptor;
2446         u32 command;
2447
2448         /* Do not process Tx ring in case of Tx ring resource error */
2449         if (mp->tx_resource_err)
2450                 return ETH_QUEUE_FULL;
2451
2452         /*
2453          * The hardware requires that each buffer that is <= 8 bytes
2454          * in length must be aligned on an 8 byte boundary.
2455          */
2456         if (p_pkt_info->byte_cnt <= 8 && p_pkt_info->buf_ptr & 0x7) {
2457                 printk(KERN_ERR
2458                         "mv643xx_eth port %d: packet size <= 8 problem\n",
2459                         mp->port_num);
2460                 return ETH_ERROR;
2461         }
2462
2463         mp->tx_ring_skbs++;
2464         BUG_ON(mp->tx_ring_skbs > mp->tx_ring_size);
2465
2466         /* Get the Tx Desc ring indexes */
2467         tx_desc_curr = mp->tx_curr_desc_q;
2468         tx_desc_used = mp->tx_used_desc_q;
2469
2470         current_descriptor = &mp->p_tx_desc_area[tx_desc_curr];
2471
2472         tx_next_desc = (tx_desc_curr + 1) % mp->tx_ring_size;
2473
2474         current_descriptor->buf_ptr = p_pkt_info->buf_ptr;
2475         current_descriptor->byte_cnt = p_pkt_info->byte_cnt;
2476         current_descriptor->l4i_chk = p_pkt_info->l4i_chk;
2477         mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info;
2478
2479         command = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC |
2480                                                         ETH_BUFFER_OWNED_BY_DMA;
2481         if (command & ETH_TX_FIRST_DESC) {
2482                 tx_first_desc = tx_desc_curr;
2483                 mp->tx_first_desc_q = tx_first_desc;
2484                 first_descriptor = current_descriptor;
2485                 mp->tx_first_command = command;
2486         } else {
2487                 tx_first_desc = mp->tx_first_desc_q;
2488                 first_descriptor = &mp->p_tx_desc_area[tx_first_desc];
2489                 BUG_ON(first_descriptor == NULL);
2490                 current_descriptor->cmd_sts = command;
2491         }
2492
2493         if (command & ETH_TX_LAST_DESC) {
2494                 wmb();
2495                 first_descriptor->cmd_sts = mp->tx_first_command;
2496
2497                 wmb();
2498                 ETH_ENABLE_TX_QUEUE(mp->port_num);
2499
2500                 /*
2501                  * Finish Tx packet. Update first desc in case of Tx resource
2502                  * error */
2503                 tx_first_desc = tx_next_desc;
2504                 mp->tx_first_desc_q = tx_first_desc;
2505         }
2506
2507         /* Check for ring index overlap in the Tx desc ring */
2508         if (tx_next_desc == tx_desc_used) {
2509                 mp->tx_resource_err = 1;
2510                 mp->tx_curr_desc_q = tx_first_desc;
2511
2512                 return ETH_QUEUE_LAST_RESOURCE;
2513         }
2514
2515         mp->tx_curr_desc_q = tx_next_desc;
2516
2517         return ETH_OK;
2518 }
2519 #else
2520 static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp,
2521                                          struct pkt_info *p_pkt_info)
2522 {
2523         int tx_desc_curr;
2524         int tx_desc_used;
2525         struct eth_tx_desc *current_descriptor;
2526         unsigned int command_status;
2527
2528         /* Do not process Tx ring in case of Tx ring resource error */
2529         if (mp->tx_resource_err)
2530                 return ETH_QUEUE_FULL;
2531
2532         mp->tx_ring_skbs++;
2533         BUG_ON(mp->tx_ring_skbs > mp->tx_ring_size);
2534
2535         /* Get the Tx Desc ring indexes */
2536         tx_desc_curr = mp->tx_curr_desc_q;
2537         tx_desc_used = mp->tx_used_desc_q;
2538         current_descriptor = &mp->p_tx_desc_area[tx_desc_curr];
2539
2540         command_status = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC;
2541         current_descriptor->buf_ptr = p_pkt_info->buf_ptr;
2542         current_descriptor->byte_cnt = p_pkt_info->byte_cnt;
2543         mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info;
2544
2545         /* Set last desc with DMA ownership and interrupt enable. */
2546         wmb();
2547         current_descriptor->cmd_sts = command_status |
2548                         ETH_BUFFER_OWNED_BY_DMA | ETH_TX_ENABLE_INTERRUPT;
2549
2550         wmb();
2551         ETH_ENABLE_TX_QUEUE(mp->port_num);
2552
2553         /* Finish Tx packet. Update first desc in case of Tx resource error */
2554         tx_desc_curr = (tx_desc_curr + 1) % mp->tx_ring_size;
2555
2556         /* Update the current descriptor */
2557         mp->tx_curr_desc_q = tx_desc_curr;
2558
2559         /* Check for ring index overlap in the Tx desc ring */
2560         if (tx_desc_curr == tx_desc_used) {
2561                 mp->tx_resource_err = 1;
2562                 return ETH_QUEUE_LAST_RESOURCE;
2563         }
2564
2565         return ETH_OK;
2566 }
2567 #endif
2568
2569 /*
2570  * eth_tx_return_desc - Free all used Tx descriptors
2571  *
2572  * DESCRIPTION:
2573  *      This routine returns the transmitted packet information to the caller.
2574  *      It uses the 'first' index to support Tx desc return in case a transmit
2575  *      of a packet spanned over multiple buffer still in process.
2576  *      In case the Tx queue was in "resource error" condition, where there are
2577  *      no available Tx resources, the function resets the resource error flag.
2578  *
2579  * INPUT:
2580  *      struct mv643xx_private  *mp             Ethernet Port Control srtuct.
2581  *      struct pkt_info         *p_pkt_info     User packet buffer.
2582  *
2583  * OUTPUT:
2584  *      Tx ring 'first' and 'used' indexes are updated.
2585  *
2586  * RETURN:
2587  *      ETH_ERROR in case the routine can not access Tx desc ring.
2588  *      ETH_RETRY in case there is transmission in process.
2589  *      ETH_END_OF_JOB if the routine has nothing to release.
2590  *      ETH_OK otherwise.
2591  *
2592  */
2593 static ETH_FUNC_RET_STATUS eth_tx_return_desc(struct mv643xx_private *mp,
2594                                                 struct pkt_info *p_pkt_info)
2595 {
2596         int tx_desc_used;
2597 #ifdef MV643XX_CHECKSUM_OFFLOAD_TX
2598         int tx_busy_desc = mp->tx_first_desc_q;
2599 #else
2600         int tx_busy_desc = mp->tx_curr_desc_q;
2601 #endif
2602         struct eth_tx_desc *p_tx_desc_used;
2603         unsigned int command_status;
2604
2605         /* Get the Tx Desc ring indexes */
2606         tx_desc_used = mp->tx_used_desc_q;
2607
2608         p_tx_desc_used = &mp->p_tx_desc_area[tx_desc_used];
2609
2610         /* Sanity check */
2611         if (p_tx_desc_used == NULL)
2612                 return ETH_ERROR;
2613
2614         /* Stop release. About to overlap the current available Tx descriptor */
2615         if (tx_desc_used == tx_busy_desc && !mp->tx_resource_err)
2616                 return ETH_END_OF_JOB;
2617
2618         command_status = p_tx_desc_used->cmd_sts;
2619
2620         /* Still transmitting... */
2621         if (command_status & (ETH_BUFFER_OWNED_BY_DMA))
2622                 return ETH_RETRY;
2623
2624         /* Pass the packet information to the caller */
2625         p_pkt_info->cmd_sts = command_status;
2626         p_pkt_info->return_info = mp->tx_skb[tx_desc_used];
2627         mp->tx_skb[tx_desc_used] = NULL;
2628
2629         /* Update the next descriptor to release. */
2630         mp->tx_used_desc_q = (tx_desc_used + 1) % mp->tx_ring_size;
2631
2632         /* Any Tx return cancels the Tx resource error status */
2633         mp->tx_resource_err = 0;
2634
2635         BUG_ON(mp->tx_ring_skbs == 0);
2636         mp->tx_ring_skbs--;
2637
2638         return ETH_OK;
2639 }
2640
2641 /*
2642  * eth_port_receive - Get received information from Rx ring.
2643  *
2644  * DESCRIPTION:
2645  *      This routine returns the received data to the caller. There is no
2646  *      data copying during routine operation. All information is returned
2647  *      using pointer to packet information struct passed from the caller.
2648  *      If the routine exhausts Rx ring resources then the resource error flag
2649  *      is set.
2650  *
2651  * INPUT:
2652  *      struct mv643xx_private  *mp             Ethernet Port Control srtuct.
2653  *      struct pkt_info         *p_pkt_info     User packet buffer.
2654  *
2655  * OUTPUT:
2656  *      Rx ring current and used indexes are updated.
2657  *
2658  * RETURN:
2659  *      ETH_ERROR in case the routine can not access Rx desc ring.
2660  *      ETH_QUEUE_FULL if Rx ring resources are exhausted.
2661  *      ETH_END_OF_JOB if there is no received data.
2662  *      ETH_OK otherwise.
2663  */
2664 static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp,
2665                                                 struct pkt_info *p_pkt_info)
2666 {
2667         int rx_next_curr_desc, rx_curr_desc, rx_used_desc;
2668         volatile struct eth_rx_desc *p_rx_desc;
2669         unsigned int command_status;
2670
2671         /* Do not process Rx ring in case of Rx ring resource error */
2672         if (mp->rx_resource_err)
2673                 return ETH_QUEUE_FULL;
2674
2675         /* Get the Rx Desc ring 'curr and 'used' indexes */
2676         rx_curr_desc = mp->rx_curr_desc_q;
2677         rx_used_desc = mp->rx_used_desc_q;
2678
2679         p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc];
2680
2681         /* The following parameters are used to save readings from memory */
2682         command_status = p_rx_desc->cmd_sts;
2683         rmb();
2684
2685         /* Nothing to receive... */
2686         if (command_status & (ETH_BUFFER_OWNED_BY_DMA))
2687                 return ETH_END_OF_JOB;
2688
2689         p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET;
2690         p_pkt_info->cmd_sts = command_status;
2691         p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET;
2692         p_pkt_info->return_info = mp->rx_skb[rx_curr_desc];
2693         p_pkt_info->l4i_chk = p_rx_desc->buf_size;
2694
2695         /* Clean the return info field to indicate that the packet has been */
2696         /* moved to the upper layers                                        */
2697         mp->rx_skb[rx_curr_desc] = NULL;
2698
2699         /* Update current index in data structure */
2700         rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size;
2701         mp->rx_curr_desc_q = rx_next_curr_desc;
2702
2703         /* Rx descriptors exhausted. Set the Rx ring resource error flag */
2704         if (rx_next_curr_desc == rx_used_desc)
2705                 mp->rx_resource_err = 1;
2706
2707         return ETH_OK;
2708 }
2709
2710 /*
2711  * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring.
2712  *
2713  * DESCRIPTION:
2714  *      This routine returns a Rx buffer back to the Rx ring. It retrieves the
2715  *      next 'used' descriptor and attached the returned buffer to it.
2716  *      In case the Rx ring was in "resource error" condition, where there are
2717  *      no available Rx resources, the function resets the resource error flag.
2718  *
2719  * INPUT:
2720  *      struct mv643xx_private  *mp             Ethernet Port Control srtuct.
2721  *      struct pkt_info         *p_pkt_info     Information on returned buffer.
2722  *
2723  * OUTPUT:
2724  *      New available Rx resource in Rx descriptor ring.
2725  *
2726  * RETURN:
2727  *      ETH_ERROR in case the routine can not access Rx desc ring.
2728  *      ETH_OK otherwise.
2729  */
2730 static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp,
2731                                                 struct pkt_info *p_pkt_info)
2732 {
2733         int used_rx_desc;       /* Where to return Rx resource */
2734         volatile struct eth_rx_desc *p_used_rx_desc;
2735
2736         /* Get 'used' Rx descriptor */
2737         used_rx_desc = mp->rx_used_desc_q;
2738         p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc];
2739
2740         p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr;
2741         p_used_rx_desc->buf_size = p_pkt_info->byte_cnt;
2742         mp->rx_skb[used_rx_desc] = p_pkt_info->return_info;
2743
2744         /* Flush the write pipe */
2745
2746         /* Return the descriptor to DMA ownership */
2747         wmb();
2748         p_used_rx_desc->cmd_sts =
2749                         ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT;
2750         wmb();
2751
2752         /* Move the used descriptor pointer to the next descriptor */
2753         mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size;
2754
2755         /* Any Rx return cancels the Rx resource error status */
2756         mp->rx_resource_err = 0;
2757
2758         return ETH_OK;
2759 }
2760
2761 /************* Begin ethtool support *************************/
2762
2763 struct mv643xx_stats {
2764         char stat_string[ETH_GSTRING_LEN];
2765         int sizeof_stat;
2766         int stat_offset;
2767 };
2768
2769 #define MV643XX_STAT(m) sizeof(((struct mv643xx_private *)0)->m), \
2770                       offsetof(struct mv643xx_private, m)
2771
2772 static const struct mv643xx_stats mv643xx_gstrings_stats[] = {
2773         { "rx_packets", MV643XX_STAT(stats.rx_packets) },
2774         { "tx_packets", MV643XX_STAT(stats.tx_packets) },
2775         { "rx_bytes", MV643XX_STAT(stats.rx_bytes) },
2776         { "tx_bytes", MV643XX_STAT(stats.tx_bytes) },
2777         { "rx_errors", MV643XX_STAT(stats.rx_errors) },
2778         { "tx_errors", MV643XX_STAT(stats.tx_errors) },
2779         { "rx_dropped", MV643XX_STAT(stats.rx_dropped) },
2780         { "tx_dropped", MV643XX_STAT(stats.tx_dropped) },
2781         { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) },
2782         { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) },
2783         { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) },
2784         { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) },
2785         { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) },
2786         { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) },
2787         { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) },
2788         { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) },
2789         { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) },
2790         { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) },
2791         { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) },
2792         { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) },
2793         { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) },
2794         { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) },
2795         { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) },
2796         { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) },
2797         { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) },
2798         { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) },
2799         { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) },
2800         { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) },
2801         { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) },
2802         { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) },
2803         { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) },
2804         { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) },
2805         { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) },
2806         { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) },
2807         { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) },
2808         { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) },
2809         { "collision", MV643XX_STAT(mib_counters.collision) },
2810         { "late_collision", MV643XX_STAT(mib_counters.late_collision) },
2811 };
2812
2813 #define MV643XX_STATS_LEN       \
2814         sizeof(mv643xx_gstrings_stats) / sizeof(struct mv643xx_stats)
2815
2816 static int
2817 mv643xx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd)
2818 {
2819         struct mv643xx_private *mp = netdev->priv;
2820         int port_num = mp->port_num;
2821         int autoneg = eth_port_autoneg_supported(port_num);
2822         int mode_10_bit;
2823         int auto_duplex;
2824         int half_duplex = 0;
2825         int full_duplex = 0;
2826         int auto_speed;
2827         int speed_10 = 0;
2828         int speed_100 = 0;
2829         int speed_1000 = 0;
2830
2831         u32 pcs = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num));
2832         u32 psr = mv_read(MV643XX_ETH_PORT_STATUS_REG(port_num));
2833
2834         mode_10_bit = psr & MV643XX_ETH_PORT_STATUS_MODE_10_BIT;
2835
2836         if (mode_10_bit) {
2837                 ecmd->supported = SUPPORTED_10baseT_Half;
2838         } else {
2839                 ecmd->supported = (SUPPORTED_10baseT_Half               |
2840                                    SUPPORTED_10baseT_Full               |
2841                                    SUPPORTED_100baseT_Half              |
2842                                    SUPPORTED_100baseT_Full              |
2843                                    SUPPORTED_1000baseT_Full             |
2844                                    (autoneg ? SUPPORTED_Autoneg : 0)    |
2845                                    SUPPORTED_TP);
2846
2847                 auto_duplex = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_FOR_DUPLX);
2848                 auto_speed = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_SPEED_GMII);
2849
2850                 ecmd->advertising = ADVERTISED_TP;
2851
2852                 if (autoneg) {
2853                         ecmd->advertising |= ADVERTISED_Autoneg;
2854
2855                         if (auto_duplex) {
2856                                 half_duplex = 1;
2857                                 full_duplex = 1;
2858                         } else {
2859                                 if (pcs & MV643XX_ETH_SET_FULL_DUPLEX_MODE)
2860                                         full_duplex = 1;
2861                                 else
2862                                         half_duplex = 1;
2863                         }
2864
2865                         if (auto_speed) {
2866                                 speed_10 = 1;
2867                                 speed_100 = 1;
2868                                 speed_1000 = 1;
2869                         } else {
2870                                 if (pcs & MV643XX_ETH_SET_GMII_SPEED_TO_1000)
2871                                         speed_1000 = 1;
2872                                 else if (pcs & MV643XX_ETH_SET_MII_SPEED_TO_100)
2873                                         speed_100 = 1;
2874                                 else
2875                                         speed_10 = 1;
2876                         }
2877
2878                         if (speed_10 & half_duplex)
2879                                 ecmd->advertising |= ADVERTISED_10baseT_Half;
2880                         if (speed_10 & full_duplex)
2881                                 ecmd->advertising |= ADVERTISED_10baseT_Full;
2882                         if (speed_100 & half_duplex)
2883                                 ecmd->advertising |= ADVERTISED_100baseT_Half;
2884                         if (speed_100 & full_duplex)
2885                                 ecmd->advertising |= ADVERTISED_100baseT_Full;
2886                         if (speed_1000)
2887                                 ecmd->advertising |= ADVERTISED_1000baseT_Full;
2888                 }
2889         }
2890
2891         ecmd->port = PORT_TP;
2892         ecmd->phy_address = ethernet_phy_get(port_num);
2893
2894         ecmd->transceiver = XCVR_EXTERNAL;
2895
2896         if (netif_carrier_ok(netdev)) {
2897                 if (mode_10_bit)
2898                         ecmd->speed = SPEED_10;
2899                 else {
2900                         if (psr & MV643XX_ETH_PORT_STATUS_GMII_1000)
2901                                 ecmd->speed = SPEED_1000;
2902                         else if (psr & MV643XX_ETH_PORT_STATUS_MII_100)
2903                                 ecmd->speed = SPEED_100;
2904                         else
2905                                 ecmd->speed = SPEED_10;
2906                 }
2907
2908                 if (psr & MV643XX_ETH_PORT_STATUS_FULL_DUPLEX)
2909                         ecmd->duplex = DUPLEX_FULL;
2910                 else
2911                         ecmd->duplex = DUPLEX_HALF;
2912         } else {
2913                 ecmd->speed = -1;
2914                 ecmd->duplex = -1;
2915         }
2916
2917         ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE;
2918         return 0;
2919 }
2920
2921 static void
2922 mv643xx_get_drvinfo(struct net_device *netdev,
2923                        struct ethtool_drvinfo *drvinfo)
2924 {
2925         strncpy(drvinfo->driver,  mv643xx_driver_name, 32);
2926         strncpy(drvinfo->version, mv643xx_driver_version, 32);
2927         strncpy(drvinfo->fw_version, "N/A", 32);
2928         strncpy(drvinfo->bus_info, "mv643xx", 32);
2929         drvinfo->n_stats = MV643XX_STATS_LEN;
2930 }
2931
2932 static int 
2933 mv643xx_get_stats_count(struct net_device *netdev)
2934 {
2935         return MV643XX_STATS_LEN;
2936 }
2937
2938 static void 
2939 mv643xx_get_ethtool_stats(struct net_device *netdev, 
2940                 struct ethtool_stats *stats, uint64_t *data)
2941 {
2942         struct mv643xx_private *mp = netdev->priv;
2943         int i;
2944
2945         eth_update_mib_counters(mp);
2946
2947         for(i = 0; i < MV643XX_STATS_LEN; i++) {
2948                 char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset;     
2949                 data[i] = (mv643xx_gstrings_stats[i].sizeof_stat == 
2950                         sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p;
2951         }
2952 }
2953
2954 static void 
2955 mv643xx_get_strings(struct net_device *netdev, uint32_t stringset, uint8_t *data)
2956 {
2957         int i;
2958
2959         switch(stringset) {
2960         case ETH_SS_STATS:
2961                 for (i=0; i < MV643XX_STATS_LEN; i++) {
2962                         memcpy(data + i * ETH_GSTRING_LEN, 
2963                         mv643xx_gstrings_stats[i].stat_string,
2964                         ETH_GSTRING_LEN);
2965                 }
2966                 break;
2967         }
2968 }
2969
2970 static struct ethtool_ops mv643xx_ethtool_ops = {
2971         .get_settings           = mv643xx_get_settings,
2972         .get_drvinfo            = mv643xx_get_drvinfo,
2973         .get_link               = ethtool_op_get_link,
2974         .get_sg                 = ethtool_op_get_sg,
2975         .set_sg                 = ethtool_op_set_sg,
2976         .get_strings            = mv643xx_get_strings,
2977         .get_stats_count        = mv643xx_get_stats_count,
2978         .get_ethtool_stats      = mv643xx_get_ethtool_stats,
2979 };
2980
2981 /************* End ethtool support *************************/