2 * ar2313.c: Linux driver for the Atheros AR231z Ethernet device.
4 * Copyright (C) 2004 by Sameer Dekate <sdekate@arubanetworks.com>
5 * Copyright (C) 2006 Imre Kaloz <kaloz@openwrt.org>
6 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
8 * Thanks to Atheros for providing hardware and documentation
9 * enabling me to write this driver.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 * This code is taken from John Taylor's Sibyte driver and then
18 * modified for the AR2313.
21 #include <linux/autoconf.h>
22 #include <linux/module.h>
23 #include <linux/version.h>
24 #include <linux/types.h>
25 #include <linux/errno.h>
26 #include <linux/ioport.h>
27 #include <linux/pci.h>
28 #include <linux/netdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
34 #include <linux/highmem.h>
35 #include <linux/sockios.h>
36 #include <linux/pkt_sched.h>
37 #include <linux/compile.h>
38 #include <linux/mii.h>
39 #include <linux/ethtool.h>
40 #include <linux/ctype.h>
41 #include <linux/platform_device.h>
46 #include <asm/system.h>
49 #include <asm/byteorder.h>
50 #include <asm/uaccess.h>
51 #include <asm/bootinfo.h>
53 #include <ar531x_platform.h>
64 #define min(a,b) (((a)<(b))?(a):(b))
67 #ifndef SMP_CACHE_BYTES
68 #define SMP_CACHE_BYTES L1_CACHE_BYTES
71 #ifndef SET_MODULE_OWNER
72 #define SET_MODULE_OWNER(dev) {do{} while(0);}
73 #define AR2313_MOD_INC_USE_COUNT MOD_INC_USE_COUNT
74 #define AR2313_MOD_DEC_USE_COUNT MOD_DEC_USE_COUNT
76 #define AR2313_MOD_INC_USE_COUNT {do{} while(0);}
77 #define AR2313_MOD_DEC_USE_COUNT {do{} while(0);}
80 #define PHYSADDR(a) ((_ACAST32_ (a)) & 0x1fffffff)
82 static char ifname[5] = "bond";
84 module_param_string(ifname, ifname, 5, 0);
86 #define AR2313_MBOX_SET_BIT 0x8
88 #define BOARD_IDX_STATIC 0
89 #define BOARD_IDX_OVERFLOW -1
91 #include "ar2313_msg.h"
97 * New interrupt handler strategy:
99 * An old interrupt handler worked using the traditional method of
100 * replacing an skbuff with a new one when a packet arrives. However
101 * the rx rings do not need to contain a static number of buffer
102 * descriptors, thus it makes sense to move the memory allocation out
103 * of the main interrupt handler and do it in a bottom half handler
104 * and only allocate new buffers when the number of buffers in the
105 * ring is below a certain threshold. In order to avoid starving the
106 * NIC under heavy load it is however necessary to force allocation
107 * when hitting a minimum threshold. The strategy for alloction is as
110 * RX_LOW_BUF_THRES - allocate buffers in the bottom half
111 * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
112 * the buffers in the interrupt handler
113 * RX_RING_THRES - maximum number of buffers in the rx ring
115 * One advantagous side effect of this allocation approach is that the
116 * entire rx processing can be done without holding any spin lock
117 * since the rx rings and registers are totally independent of the tx
118 * ring and its registers. This of course includes the kmalloc's of
119 * new skb's. Thus start_xmit can run in parallel with rx processing
120 * and the memory allocation on SMP systems.
122 * Note that running the skb reallocation in a bottom half opens up
123 * another can of races which needs to be handled properly. In
124 * particular it can happen that the interrupt handler tries to run
125 * the reallocation while the bottom half is either running on another
126 * CPU or was interrupted on the same CPU. To get around this the
127 * driver uses bitops to prevent the reallocation routines from being
130 * TX handling can also be done without holding any spin lock, wheee
131 * this is fun! since tx_csm is only written to by the interrupt
136 * Threshold values for RX buffer allocation - the low water marks for
137 * when to start refilling the rings are set to 75% of the ring
138 * sizes. It seems to make sense to refill the rings entirely from the
139 * intrrupt handler once it gets below the panic threshold, that way
140 * we don't risk that the refilling is moved to another CPU when the
141 * one running the interrupt handler just got the slab code hot in its
144 #define RX_RING_SIZE AR2313_DESCR_ENTRIES
145 #define RX_PANIC_THRES (RX_RING_SIZE/4)
146 #define RX_LOW_THRES ((3*RX_RING_SIZE)/4)
150 #define AR2313_BUFSIZE (AR2313_MTU + ETH_HLEN + CRC_LEN + RX_OFFSET)
153 MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@openwrt.org>");
154 MODULE_DESCRIPTION("AR2313 Ethernet driver");
158 static char version[] __initdata =
159 "ar2313.c: v0.03 2006/07/12 sdekate@arubanetworks.com\n";
162 #define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
165 static short armiiread(short phy, short reg);
166 static void armiiwrite(short phy, short reg, short data);
168 static void ar2313_tx_timeout(struct net_device *dev);
170 static void ar2313_halt(struct net_device *dev);
171 static void rx_tasklet_func(unsigned long data);
172 static void ar2313_multicast_list(struct net_device *dev);
174 static int probed __initdata = 0;
175 static unsigned long ar_eth_base;
176 static unsigned long ar_dma_base;
177 static unsigned long ar_int_base;
178 static unsigned long ar_int_mac_mask;
179 static unsigned long ar_int_phy_mask;
182 #define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
186 int __init ar2313_probe(struct platform_device *pdev)
188 struct net_device *dev;
189 struct ar2313_private *sp;
190 struct ar531x_eth *cfg;
191 struct resource *res;
200 sprintf(name, "%s%%d", ifname) ;
201 dev = alloc_etherdev(sizeof(struct ar2313_private));
204 printk(KERN_ERR "ar2313: Unable to allocate net_device structure!\n");
208 SET_MODULE_OWNER(dev);
209 platform_set_drvdata(pdev, dev);
213 cfg = pdev->dev.platform_data;
215 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eth_membase");
220 ar_eth_base = res->start;
221 ar_dma_base = ar_eth_base + 0x1000;
222 ar_int_base = cfg->reset_base;
223 ar_int_mac_mask = cfg->reset_mac;
224 ar_int_phy_mask = cfg->reset_phy;
227 dev->irq = platform_get_irq_byname(pdev, "eth_irq");
229 spin_lock_init(&sp->lock);
231 /* initialize func pointers */
232 dev->open = &ar2313_open;
233 dev->stop = &ar2313_close;
234 dev->hard_start_xmit = &ar2313_start_xmit;
236 dev->get_stats = &ar2313_get_stats;
237 dev->set_multicast_list = &ar2313_multicast_list;
239 dev->tx_timeout = ar2313_tx_timeout;
240 dev->watchdog_timeo = AR2313_TX_TIMEOUT;
242 dev->do_ioctl = &ar2313_ioctl;
244 // SAMEER: do we need this?
245 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA;
247 tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long) dev);
248 tasklet_disable(&sp->rx_tasklet);
250 /* display version info if adapter is found */
252 /* set display flag to TRUE so that */
253 /* we only display this string ONCE */
261 request_region(PHYSADDR(ar_eth_base), ETHERNET_SIZE*ETHERNET_MACS,
265 sp->eth_regs = ioremap_nocache(PHYSADDR(ar_eth_base), sizeof(*sp->eth_regs));
267 printk("Can't remap eth registers\n");
271 sp->dma_regs = ioremap_nocache(PHYSADDR(ar_eth_base + 0x1000), sizeof(*sp->dma_regs));
272 dev->base_addr = (unsigned int) sp->dma_regs;
274 printk("Can't remap DMA registers\n");
278 sp->int_regs = ioremap_nocache(PHYSADDR(ar_int_base), 4);
280 printk("Can't remap INTERRUPT registers\n");
284 strncpy(sp->name, "Atheros AR2313", sizeof (sp->name) - 1);
285 sp->name [sizeof (sp->name) - 1] = '\0';
288 /* XXX: Will have to rewrite this part later */
290 unsigned char def_mac[6] = {0, 0xaa, 0xbb, 0xcc, 0xdd, 0xee};
292 configstart = (char *) cfg->board_config;
295 printk("no valid mac found, using defaults");
296 memcpy(dev->dev_addr, def_mac, 6);
298 memcpy(dev->dev_addr, ((u8 *)configstart)+102, 6);
302 sp->board_idx = BOARD_IDX_STATIC;
304 if (ar2313_init(dev)) {
306 * ar2313_init() calls ar2313_init_cleanup() on error.
312 if (register_netdev(dev)){
313 printk("%s: register_netdev failed\n", __func__);
317 printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
319 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
320 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5],
323 /* start link poll timer */
324 ar2313_setup_timer(dev);
330 static void ar2313_dump_regs(struct net_device *dev)
332 unsigned int *ptr, i;
333 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
335 ptr = (unsigned int *)sp->eth_regs;
336 for(i=0; i< (sizeof(ETHERNET_STRUCT)/ sizeof(unsigned int)); i++, ptr++) {
337 printk("ENET: %08x = %08x\n", (int)ptr, *ptr);
340 ptr = (unsigned int *)sp->dma_regs;
341 for(i=0; i< (sizeof(DMA)/ sizeof(unsigned int)); i++, ptr++) {
342 printk("DMA: %08x = %08x\n", (int)ptr, *ptr);
345 ptr = (unsigned int *)sp->int_regs;
346 for(i=0; i< (sizeof(INTERRUPT)/ sizeof(unsigned int)); i++, ptr++){
347 printk("INT: %08x = %08x\n", (int)ptr, *ptr);
350 for (i = 0; i < AR2313_DESCR_ENTRIES; i++) {
351 ar2313_descr_t *td = &sp->tx_ring[i];
352 printk("Tx desc %2d: %08x %08x %08x %08x\n", i,
353 td->status, td->devcs, td->addr, td->descr);
360 ar2313_tx_timeout(struct net_device *dev)
362 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
366 printk("Tx timeout\n");
368 spin_lock_irqsave(&sp->lock, flags);
370 spin_unlock_irqrestore(&sp->lock, flags);
376 printMcList(struct net_device *dev)
378 struct dev_mc_list *list = dev->mc_list;
381 printk("%d MC ADDR ", num);
382 for(i=0;i<list->dmi_addrlen;i++) {
383 printk(":%02x", list->dmi_addr[i]);
392 * Set or clear the multicast filter for this adaptor.
393 * THIS IS ABSOLUTE CRAP, disabled
396 ar2313_multicast_list(struct net_device *dev)
399 * Always listen to broadcasts and
400 * treat IFF bits independently
402 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
403 unsigned int recognise;
405 recognise = sp->eth_regs->mac_control;
407 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
408 recognise |= MAC_CONTROL_PR;
410 recognise &= ~MAC_CONTROL_PR;
413 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
416 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__, dev->mc_count);
418 recognise |= MAC_CONTROL_PM;/* all multicast */
419 } else if (dev->mc_count > 0) {
422 printk("%s: mc_count %d\n", __FUNCTION__, dev->mc_count);
424 recognise |= MAC_CONTROL_PM; /* for the time being */
427 printk("%s: setting %08x to %08x\n", __FUNCTION__, (int)sp->eth_regs, recognise);
430 sp->eth_regs->mac_control = recognise;
433 static void rx_tasklet_cleanup(struct net_device *dev)
435 struct ar2313_private *sp = dev->priv;
438 * Tasklet may be scheduled. Need to get it removed from the list
439 * since we're about to free the struct.
443 tasklet_enable(&sp->rx_tasklet);
444 tasklet_kill(&sp->rx_tasklet);
447 static int __exit ar2313_remove(struct platform_device *pdev)
449 struct net_device *dev = platform_get_drvdata(pdev);
450 rx_tasklet_cleanup(dev);
451 ar2313_init_cleanup(dev);
452 unregister_netdev(dev);
459 * Restart the AR2313 ethernet controller.
461 static int ar2313_restart(struct net_device *dev)
463 /* disable interrupts */
464 disable_irq(dev->irq);
472 /* enable interrupts */
473 enable_irq(dev->irq);
478 static struct platform_driver ar2313_driver = {
479 .driver.name = "ar531x-eth",
480 .probe = ar2313_probe,
481 .remove = ar2313_remove,
484 int __init ar2313_module_init(void)
486 return platform_driver_register(&ar2313_driver);
489 void __exit ar2313_module_cleanup(void)
491 platform_driver_unregister(&ar2313_driver);
494 module_init(ar2313_module_init);
495 module_exit(ar2313_module_cleanup);
498 static void ar2313_free_descriptors(struct net_device *dev)
500 struct ar2313_private *sp = dev->priv;
501 if (sp->rx_ring != NULL) {
502 kfree((void*)KSEG0ADDR(sp->rx_ring));
509 static int ar2313_allocate_descriptors(struct net_device *dev)
511 struct ar2313_private *sp = dev->priv;
514 ar2313_descr_t *space;
516 if(sp->rx_ring != NULL){
517 printk("%s: already done.\n", __FUNCTION__);
521 size = (sizeof(ar2313_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES));
522 space = kmalloc(size, GFP_KERNEL);
526 /* invalidate caches */
527 dma_cache_inv((unsigned int)space, size);
529 /* now convert pointer to KSEG1 */
530 space = (ar2313_descr_t *)KSEG1ADDR(space);
532 memset((void *)space, 0, size);
535 space += AR2313_DESCR_ENTRIES;
538 space += AR2313_DESCR_ENTRIES;
540 /* Initialize the transmit Descriptors */
541 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
542 ar2313_descr_t *td = &sp->tx_ring[j];
544 td->devcs = DMA_TX1_CHAINED;
546 td->descr = K1_TO_PHYS(&sp->tx_ring[(j+1) & (AR2313_DESCR_ENTRIES-1)]);
554 * Generic cleanup handling data allocated during init. Used when the
555 * module is unloaded or if an error occurs during initialization
557 static void ar2313_init_cleanup(struct net_device *dev)
559 struct ar2313_private *sp = dev->priv;
563 ar2313_free_descriptors(dev);
565 if (sp->eth_regs) iounmap((void*)sp->eth_regs);
566 if (sp->dma_regs) iounmap((void*)sp->dma_regs);
569 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
572 sp->rx_skb[j] = NULL;
581 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
584 sp->tx_skb[j] = NULL;
593 static int ar2313_setup_timer(struct net_device *dev)
595 struct ar2313_private *sp = dev->priv;
597 init_timer(&sp->link_timer);
599 sp->link_timer.function = ar2313_link_timer_fn;
600 sp->link_timer.data = (int) dev;
601 sp->link_timer.expires = jiffies + HZ;
603 add_timer(&sp->link_timer);
608 static void ar2313_link_timer_fn(unsigned long data)
610 struct net_device *dev = (struct net_device *) data;
611 struct ar2313_private *sp = dev->priv;
613 // see if the link status changed
614 // This was needed to make sure we set the PHY to the
615 // autonegotiated value of half or full duplex.
616 ar2313_check_link(dev);
618 // Loop faster when we don't have link.
619 // This was needed to speed up the AP bootstrap time.
621 mod_timer(&sp->link_timer, jiffies + HZ/2);
623 mod_timer(&sp->link_timer, jiffies + LINK_TIMER);
627 static void ar2313_check_link(struct net_device *dev)
629 struct ar2313_private *sp = dev->priv;
632 phyData = armiiread(sp->phy, MII_BMSR);
633 if (sp->phyData != phyData) {
634 if (phyData & BMSR_LSTATUS) {
635 /* link is present, ready link partner ability to deterine duplexity */
640 reg = armiiread(sp->phy, MII_BMCR);
641 if (reg & BMCR_ANENABLE) {
642 /* auto neg enabled */
643 reg = armiiread(sp->phy, MII_LPA);
644 duplex = (reg & (LPA_100FULL|LPA_10FULL))? 1:0;
646 /* no auto neg, just read duplex config */
647 duplex = (reg & BMCR_FULLDPLX)? 1:0;
650 printk(KERN_INFO "%s: Configuring MAC for %s duplex\n", dev->name,
651 (duplex)? "full":"half");
655 sp->eth_regs->mac_control = ((sp->eth_regs->mac_control | MAC_CONTROL_F) &
659 sp->eth_regs->mac_control = ((sp->eth_regs->mac_control | MAC_CONTROL_DRO) &
666 sp->phyData = phyData;
671 ar2313_reset_reg(struct net_device *dev)
673 struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
674 unsigned int ethsal, ethsah;
677 *sp->int_regs |= ar_int_mac_mask;
679 *sp->int_regs &= ~ar_int_mac_mask;
681 *sp->int_regs |= ar_int_phy_mask;
683 *sp->int_regs &= ~ar_int_phy_mask;
686 sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
688 sp->dma_regs->bus_mode = ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
690 /* enable interrupts */
691 sp->dma_regs->intr_ena = (DMA_STATUS_AIS |
696 sp->dma_regs->xmt_base = K1_TO_PHYS(sp->tx_ring);
697 sp->dma_regs->rcv_base = K1_TO_PHYS(sp->rx_ring);
698 sp->dma_regs->control = (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
700 sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
701 sp->eth_regs->vlan_tag = (0x8100);
703 /* Enable Ethernet Interface */
704 flags = (MAC_CONTROL_TE | /* transmit enable */
705 MAC_CONTROL_PM | /* pass mcast */
706 MAC_CONTROL_F | /* full duplex */
707 MAC_CONTROL_HBD); /* heart beat disabled */
709 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
710 flags |= MAC_CONTROL_PR;
712 sp->eth_regs->mac_control = flags;
714 /* Set all Ethernet station address registers to their initial values */
715 ethsah = ((((u_int)(dev->dev_addr[5]) << 8) & (u_int)0x0000FF00) |
716 (((u_int)(dev->dev_addr[4]) << 0) & (u_int)0x000000FF));
718 ethsal = ((((u_int)(dev->dev_addr[3]) << 24) & (u_int)0xFF000000) |
719 (((u_int)(dev->dev_addr[2]) << 16) & (u_int)0x00FF0000) |
720 (((u_int)(dev->dev_addr[1]) << 8) & (u_int)0x0000FF00) |
721 (((u_int)(dev->dev_addr[0]) << 0) & (u_int)0x000000FF) );
723 sp->eth_regs->mac_addr[0] = ethsah;
724 sp->eth_regs->mac_addr[1] = ethsal;
732 static int ar2313_init(struct net_device *dev)
734 struct ar2313_private *sp = dev->priv;
738 * Allocate descriptors
740 if (ar2313_allocate_descriptors(dev)) {
741 printk("%s: %s: ar2313_allocate_descriptors failed\n",
742 dev->name, __FUNCTION__);
748 * Get the memory for the skb rings.
750 if(sp->rx_skb == NULL) {
751 sp->rx_skb = kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES, GFP_KERNEL);
753 printk("%s: %s: rx_skb kmalloc failed\n",
754 dev->name, __FUNCTION__);
759 memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
761 if(sp->tx_skb == NULL) {
762 sp->tx_skb = kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES, GFP_KERNEL);
764 printk("%s: %s: tx_skb kmalloc failed\n",
765 dev->name, __FUNCTION__);
770 memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
773 * Set tx_csm before we start receiving interrupts, otherwise
774 * the interrupt handler might think it is supposed to process
775 * tx ints before we are up and running, which may cause a null
776 * pointer access in the int handler.
784 * Zero the stats before starting the interface
786 memset(&sp->stats, 0, sizeof(sp->stats));
789 * We load the ring here as there seem to be no way to tell the
790 * firmware to wipe the ring without re-initializing it.
792 ar2313_load_rx_ring(dev, RX_RING_SIZE);
797 ar2313_reset_reg(dev);
802 ecode = request_irq(dev->irq, &ar2313_interrupt, IRQF_SHARED | IRQF_DISABLED | IRQF_SAMPLE_RANDOM, dev->name, dev);
804 printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
805 dev->name, __FUNCTION__, dev->irq);
810 tasklet_enable(&sp->rx_tasklet);
815 ar2313_init_cleanup(dev);
822 * Loading rings is safe without holding the spin lock since this is
823 * done only before the device is enabled, thus no interrupts are
824 * generated and by the interrupt handler/tasklet handler.
826 static void ar2313_load_rx_ring(struct net_device *dev, int nr_bufs)
829 struct ar2313_private *sp = ((struct net_device *)dev)->priv;
834 for (i = 0; i < nr_bufs; i++) {
838 if (sp->rx_skb[idx]) {
840 printk(KERN_INFO "ar2313 rx refill full\n");
845 // partha: create additional room for the second GRE fragment
846 skb = alloc_skb(AR2313_BUFSIZE+128, GFP_ATOMIC);
848 printk("\n\n\n\n %s: No memory in system\n\n\n\n", __FUNCTION__);
851 // partha: create additional room in the front for tx pkt capture
852 skb_reserve(skb, 32);
855 * Make sure IP header starts on a fresh cache line.
858 skb_reserve(skb, RX_OFFSET);
859 sp->rx_skb[idx] = skb;
861 rd = (ar2313_descr_t *) &sp->rx_ring[idx];
863 /* initialize dma descriptor */
864 rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
866 rd->addr = virt_to_phys(skb->data);
867 rd->descr = virt_to_phys(&sp->rx_ring[(idx+1) & (AR2313_DESCR_ENTRIES-1)]);
868 rd->status = DMA_RX_OWN;
875 printk(KERN_INFO "Out of memory when allocating standard receive buffers\n");
884 #define AR2313_MAX_PKTS_PER_CALL 64
886 static int ar2313_rx_int(struct net_device *dev)
888 struct ar2313_private *sp = dev->priv;
889 struct sk_buff *skb, *skb_new;
890 ar2313_descr_t *rxdesc;
898 /* process at most the entire ring and then wait for another interrupt */
901 rxdesc = &sp->rx_ring[idx];
902 status = rxdesc->status;
903 if (status & DMA_RX_OWN) {
904 /* SiByte owns descriptor or descr not yet filled in */
909 if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
915 printk("index %d\n", idx);
916 printk("RX status %08x\n", rxdesc->status);
917 printk("RX devcs %08x\n", rxdesc->devcs );
918 printk("RX addr %08x\n", rxdesc->addr );
919 printk("RX descr %08x\n", rxdesc->descr );
922 if ((status & (DMA_RX_ERROR|DMA_RX_ERR_LENGTH)) &&
923 (!(status & DMA_RX_LONG))){
925 printk("%s: rx ERROR %08x\n", __FUNCTION__, status);
927 sp->stats.rx_errors++;
928 sp->stats.rx_dropped++;
930 /* add statistics counters */
931 if (status & DMA_RX_ERR_CRC) sp->stats.rx_crc_errors++;
932 if (status & DMA_RX_ERR_COL) sp->stats.rx_over_errors++;
933 if (status & DMA_RX_ERR_LENGTH)
934 sp->stats.rx_length_errors++;
935 if (status & DMA_RX_ERR_RUNT) sp->stats.rx_over_errors++;
936 if (status & DMA_RX_ERR_DESC) sp->stats.rx_over_errors++;
939 /* alloc new buffer. */
940 skb_new = dev_alloc_skb(AR2313_BUFSIZE + RX_OFFSET + 128);
941 if (skb_new != NULL) {
943 skb = sp->rx_skb[idx];
945 skb_put(skb, ((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
948 if ((dev->am_pkt_handler == NULL) ||
949 (dev->am_pkt_handler(skb, dev) == 0)) {
951 sp->stats.rx_bytes += skb->len;
952 skb->protocol = eth_type_trans(skb, dev);
953 /* pass the packet to upper layers */
956 if (dev->asap_netif_rx)
957 dev->asap_netif_rx(skb);
966 skb_reserve(skb_new, RX_OFFSET+32);
967 /* reset descriptor's curr_addr */
968 rxdesc->addr = virt_to_phys(skb_new->data);
970 sp->stats.rx_packets++;
971 sp->rx_skb[idx] = skb_new;
974 sp->stats.rx_dropped++;
978 rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
980 rxdesc->status = DMA_RX_OWN;
991 static void ar2313_tx_int(struct net_device *dev)
993 struct ar2313_private *sp = dev->priv;
996 ar2313_descr_t *txdesc;
997 unsigned int status=0;
1001 while (idx != sp->tx_prd) {
1003 txdesc = &sp->tx_ring[idx];
1006 printk("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
1007 dev->name, sp->tx_csm, idx, sp->tx_prd,
1008 txdesc->status, txdesc->devcs, txdesc->addr, txdesc->descr);
1011 if ((status = txdesc->status) & DMA_TX_OWN) {
1012 /* ar2313 dma still owns descr */
1015 /* done with this descriptor */
1016 dma_unmap_single(NULL, txdesc->addr, txdesc->devcs & DMA_TX1_BSIZE_MASK, DMA_TO_DEVICE);
1019 if (status & DMA_TX_ERROR){
1020 sp->stats.tx_errors++;
1021 sp->stats.tx_dropped++;
1022 if(status & DMA_TX_ERR_UNDER)
1023 sp->stats.tx_fifo_errors++;
1024 if(status & DMA_TX_ERR_HB)
1025 sp->stats.tx_heartbeat_errors++;
1026 if(status & (DMA_TX_ERR_LOSS |
1028 sp->stats.tx_carrier_errors++;
1029 if (status & (DMA_TX_ERR_LATE|
1033 sp->stats.tx_aborted_errors++;
1036 sp->stats.tx_packets++;
1039 skb = sp->tx_skb[idx];
1040 sp->tx_skb[idx] = NULL;
1041 idx = DSC_NEXT(idx);
1042 sp->stats.tx_bytes += skb->len;
1043 dev_kfree_skb_irq(skb);
1053 rx_tasklet_func(unsigned long data)
1055 struct net_device *dev = (struct net_device *) data;
1056 struct ar2313_private *sp = dev->priv;
1058 if (sp->unloading) {
1062 if (ar2313_rx_int(dev)) {
1063 tasklet_hi_schedule(&sp->rx_tasklet);
1066 unsigned long flags;
1067 spin_lock_irqsave(&sp->lock, flags);
1068 sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1069 spin_unlock_irqrestore(&sp->lock, flags);
1074 rx_schedule(struct net_device *dev)
1076 struct ar2313_private *sp = dev->priv;
1078 sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1080 tasklet_hi_schedule(&sp->rx_tasklet);
1083 static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1085 struct net_device *dev = (struct net_device *)dev_id;
1086 struct ar2313_private *sp = dev->priv;
1087 unsigned int status, enabled;
1089 /* clear interrupt */
1091 * Don't clear RI bit if currently disabled.
1093 status = sp->dma_regs->status;
1094 enabled = sp->dma_regs->intr_ena;
1095 sp->dma_regs->status = status & enabled;
1097 if (status & DMA_STATUS_NIS) {
1100 * Don't schedule rx processing if interrupt
1101 * is already disabled.
1103 if (status & enabled & DMA_STATUS_RI) {
1104 /* receive interrupt */
1107 if (status & DMA_STATUS_TI) {
1108 /* transmit interrupt */
1113 if (status & DMA_STATUS_AIS) {
1115 printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1116 status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1118 /* abnormal status */
1119 if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1120 ar2313_restart(dev);
1127 static int ar2313_open(struct net_device *dev)
1129 struct ar2313_private *sp;
1134 netif_start_queue(dev);
1136 sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1138 AR2313_MOD_INC_USE_COUNT;
1143 static void ar2313_halt(struct net_device *dev)
1145 struct ar2313_private *sp = dev->priv;
1148 tasklet_disable(&sp->rx_tasklet);
1151 sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1152 MAC_CONTROL_TE); /* disable Transmits */
1154 sp->dma_regs->control = 0;
1155 sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1157 /* place phy and MAC in reset */
1158 *sp->int_regs |= (ar_int_mac_mask | ar_int_phy_mask);
1160 /* free buffers on tx ring */
1161 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1162 struct sk_buff *skb;
1163 ar2313_descr_t *txdesc;
1165 txdesc = &sp->tx_ring[j];
1168 skb = sp->tx_skb[j];
1171 sp->tx_skb[j] = NULL;
1177 * close should do nothing. Here's why. It's called when
1178 * 'ifconfig bond0 down' is run. If it calls free_irq then
1179 * the irq is gone forever ! When bond0 is made 'up' again,
1180 * the ar2313_open () does not call request_irq (). Worse,
1181 * the call to ar2313_halt() generates a WDOG reset due to
1182 * the write to 'sp->int_regs' and the box reboots.
1183 * Commenting this out is good since it allows the
1184 * system to resume when bond0 is made up again.
1186 static int ar2313_close(struct net_device *dev)
1190 * Disable interrupts
1192 disable_irq(dev->irq);
1195 * Without (or before) releasing irq and stopping hardware, this
1196 * is an absolute non-sense, by the way. It will be reset instantly
1199 netif_stop_queue(dev);
1201 /* stop the MAC and DMA engines */
1204 /* release the interrupt */
1205 free_irq(dev->irq, dev);
1208 AR2313_MOD_DEC_USE_COUNT;
1212 static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1214 struct ar2313_private *sp = dev->priv;
1219 td = &sp->tx_ring[idx];
1221 if (td->status & DMA_TX_OWN) {
1223 printk("%s: No space left to Tx\n", __FUNCTION__);
1225 /* free skbuf and lie to the caller that we sent it out */
1226 sp->stats.tx_dropped++;
1229 /* restart transmitter in case locked */
1230 sp->dma_regs->xmt_poll = 0;
1234 /* Setup the transmit descriptor. */
1235 td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
1236 (DMA_TX1_LS|DMA_TX1_IC|DMA_TX1_CHAINED));
1237 td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
1238 td->status = DMA_TX_OWN;
1240 /* kick transmitter last */
1241 sp->dma_regs->xmt_poll = 0;
1244 printk("index %d\n", idx);
1245 printk("TX status %08x\n", td->status);
1246 printk("TX devcs %08x\n", td->devcs );
1247 printk("TX addr %08x\n", td->addr );
1248 printk("TX descr %08x\n", td->descr );
1251 sp->tx_skb[idx] = skb;
1252 idx = DSC_NEXT(idx);
1255 //dev->trans_start = jiffies;
1260 static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1262 struct ar2313_private *np = dev->priv;
1266 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1267 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1268 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
1270 ecmd->port = PORT_TP;
1271 /* only supports internal transceiver */
1272 ecmd->transceiver = XCVR_INTERNAL;
1273 /* not sure what this is for */
1274 ecmd->phy_address = 1;
1276 ecmd->advertising = ADVERTISED_MII;
1277 tmp = armiiread(np->phy, MII_ADVERTISE);
1278 if (tmp & ADVERTISE_10HALF)
1279 ecmd->advertising |= ADVERTISED_10baseT_Half;
1280 if (tmp & ADVERTISE_10FULL)
1281 ecmd->advertising |= ADVERTISED_10baseT_Full;
1282 if (tmp & ADVERTISE_100HALF)
1283 ecmd->advertising |= ADVERTISED_100baseT_Half;
1284 if (tmp & ADVERTISE_100FULL)
1285 ecmd->advertising |= ADVERTISED_100baseT_Full;
1287 tmp = armiiread(np->phy, MII_BMCR);
1288 if (tmp & BMCR_ANENABLE) {
1289 ecmd->advertising |= ADVERTISED_Autoneg;
1290 ecmd->autoneg = AUTONEG_ENABLE;
1292 ecmd->autoneg = AUTONEG_DISABLE;
1295 if (ecmd->autoneg == AUTONEG_ENABLE) {
1296 tmp = armiiread(np->phy, MII_LPA);
1297 if (tmp & (LPA_100FULL|LPA_10FULL)) {
1298 ecmd->duplex = DUPLEX_FULL;
1300 ecmd->duplex = DUPLEX_HALF;
1302 if (tmp & (LPA_100FULL|LPA_100HALF)) {
1303 ecmd->speed = SPEED_100;
1305 ecmd->speed = SPEED_10;
1308 if (tmp & BMCR_FULLDPLX) {
1309 ecmd->duplex = DUPLEX_FULL;
1311 ecmd->duplex = DUPLEX_HALF;
1313 if (tmp & BMCR_SPEED100) {
1314 ecmd->speed = SPEED_100;
1316 ecmd->speed = SPEED_10;
1320 /* ignore maxtxpkt, maxrxpkt for now */
1325 static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1327 struct ar2313_private *np = dev->priv;
1330 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
1332 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1334 if (ecmd->port != PORT_TP)
1336 if (ecmd->transceiver != XCVR_INTERNAL)
1338 if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1340 /* ignore phy_address, maxtxpkt, maxrxpkt for now */
1342 /* WHEW! now lets bang some bits */
1344 tmp = armiiread(np->phy, MII_BMCR);
1345 if (ecmd->autoneg == AUTONEG_ENABLE) {
1346 /* turn on autonegotiation */
1347 tmp |= BMCR_ANENABLE;
1348 printk("%s: Enabling auto-neg\n", dev->name);
1350 /* turn off auto negotiation, set speed and duplexity */
1351 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1352 if (ecmd->speed == SPEED_100)
1353 tmp |= BMCR_SPEED100;
1354 if (ecmd->duplex == DUPLEX_FULL)
1355 tmp |= BMCR_FULLDPLX;
1356 printk("%s: Hard coding %d/%s\n", dev->name,
1357 (ecmd->speed == SPEED_100)? 100:10,
1358 (ecmd->duplex == DUPLEX_FULL)? "full":"half");
1360 armiiwrite(np->phy, MII_BMCR, tmp);
1365 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1367 struct ar2313_private *np = dev->priv;
1370 if (get_user(cmd, (u32 *)useraddr))
1375 case ETHTOOL_GSET: {
1376 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1377 spin_lock_irq(&np->lock);
1378 netdev_get_ecmd(dev, &ecmd);
1379 spin_unlock_irq(&np->lock);
1380 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1385 case ETHTOOL_SSET: {
1386 struct ethtool_cmd ecmd;
1388 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1390 spin_lock_irq(&np->lock);
1391 r = netdev_set_ecmd(dev, &ecmd);
1392 spin_unlock_irq(&np->lock);
1395 /* restart autonegotiation */
1396 case ETHTOOL_NWAY_RST: {
1399 /* if autoneg is off, it's an error */
1400 tmp = armiiread(np->phy, MII_BMCR);
1401 if (tmp & BMCR_ANENABLE) {
1402 tmp |= (BMCR_ANRESTART);
1403 armiiwrite(np->phy, MII_BMCR, tmp);
1408 /* get link status */
1409 case ETHTOOL_GLINK: {
1410 struct ethtool_value edata = {ETHTOOL_GLINK};
1411 edata.data = (armiiread(np->phy, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
1412 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1421 static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1423 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&ifr->ifr_data;
1426 case SIOCDEVPRIVATE: {
1427 struct ar2313_cmd scmd;
1429 if (copy_from_user(&scmd, ifr->ifr_data, sizeof(scmd)))
1433 printk("%s: ioctl devprivate c=%d a=%x l=%d m=%d d=%x,%x\n",
1434 dev->name, scmd.cmd,
1435 scmd.address, scmd.length,
1436 scmd.mailbox, scmd.data[0], scmd.data[1]);
1440 case AR2313_READ_DATA:
1442 scmd.data[0] = *((u32*)scmd.address);
1443 } else if(scmd.length==2) {
1444 scmd.data[0] = *((u16*)scmd.address);
1445 } else if (scmd.length==1) {
1446 scmd.data[0] = *((u8*)scmd.address);
1450 if(copy_to_user(ifr->ifr_data, &scmd, sizeof(scmd)))
1454 case AR2313_WRITE_DATA:
1456 *((u32*)scmd.address) = scmd.data[0];
1457 } else if(scmd.length==2) {
1458 *((u16*)scmd.address) = scmd.data[0];
1459 } else if (scmd.length==1) {
1460 *((u8*)scmd.address) = scmd.data[0];
1466 case AR2313_GET_VERSION:
1467 // SAMEER: sprintf((char*) &scmd, "%s", ARUBA_VERSION);
1468 if(copy_to_user(ifr->ifr_data, &scmd, sizeof(scmd)))
1479 return netdev_ethtool_ioctl(dev, (void *) ifr->ifr_data);
1481 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1485 case SIOCGMIIREG: /* Read MII PHY register. */
1486 case SIOCDEVPRIVATE+1: /* for binary compat, remove in 2.5 */
1487 data->val_out = armiiread(data->phy_id & 0x1f,
1488 data->reg_num & 0x1f);
1490 case SIOCSMIIREG: /* Write MII PHY register. */
1491 case SIOCDEVPRIVATE+2: /* for binary compat, remove in 2.5 */
1492 if (!capable(CAP_NET_ADMIN))
1494 armiiwrite(data->phy_id & 0x1f,
1495 data->reg_num & 0x1f, data->val_in);
1499 if (copy_from_user(dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1504 if (copy_to_user(ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1515 static struct net_device_stats *ar2313_get_stats(struct net_device *dev)
1517 struct ar2313_private *sp = dev->priv;
1522 armiiread(short phy, short reg)
1524 volatile ETHERNET_STRUCT * ethernet;
1526 ethernet = (volatile ETHERNET_STRUCT *)(ar_eth_base); /* always MAC 0 */
1527 ethernet->mii_addr = ((reg << MII_ADDR_REG_SHIFT) |
1528 (phy << MII_ADDR_PHY_SHIFT));
1529 while (ethernet->mii_addr & MII_ADDR_BUSY);
1530 return (ethernet->mii_data >> MII_DATA_SHIFT);
1534 armiiwrite(short phy, short reg, short data)
1536 volatile ETHERNET_STRUCT * ethernet;
1538 ethernet = (volatile ETHERNET_STRUCT *)(ar_eth_base); /* always MAC 0 */
1539 while (ethernet->mii_addr & MII_ADDR_BUSY);
1540 ethernet->mii_data = data << MII_DATA_SHIFT;
1541 ethernet->mii_addr = ((reg << MII_ADDR_REG_SHIFT) |
1542 (phy << MII_ADDR_PHY_SHIFT) |