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