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);
299 /* use the other MAC slot if the first one is empty */
300 if (!memcmp(dev
->dev_addr
, "\xff\xff\xff\xff\xff\xff", 6))
301 memcpy(dev
->dev_addr
, ((u8
*)configstart
)+102 + 6, 6);
305 sp
->board_idx
= BOARD_IDX_STATIC
;
307 if (ar2313_init(dev
)) {
309 * ar2313_init() calls ar2313_init_cleanup() on error.
315 if (register_netdev(dev
)){
316 printk("%s: register_netdev failed\n", __func__
);
320 printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
322 dev
->dev_addr
[0], dev
->dev_addr
[1], dev
->dev_addr
[2],
323 dev
->dev_addr
[3], dev
->dev_addr
[4], dev
->dev_addr
[5],
326 /* start link poll timer */
327 ar2313_setup_timer(dev
);
333 static void ar2313_dump_regs(struct net_device
*dev
)
335 unsigned int *ptr
, i
;
336 struct ar2313_private
*sp
= (struct ar2313_private
*)dev
->priv
;
338 ptr
= (unsigned int *)sp
->eth_regs
;
339 for(i
=0; i
< (sizeof(ETHERNET_STRUCT
)/ sizeof(unsigned int)); i
++, ptr
++) {
340 printk("ENET: %08x = %08x\n", (int)ptr
, *ptr
);
343 ptr
= (unsigned int *)sp
->dma_regs
;
344 for(i
=0; i
< (sizeof(DMA
)/ sizeof(unsigned int)); i
++, ptr
++) {
345 printk("DMA: %08x = %08x\n", (int)ptr
, *ptr
);
348 ptr
= (unsigned int *)sp
->int_regs
;
349 for(i
=0; i
< (sizeof(INTERRUPT
)/ sizeof(unsigned int)); i
++, ptr
++){
350 printk("INT: %08x = %08x\n", (int)ptr
, *ptr
);
353 for (i
= 0; i
< AR2313_DESCR_ENTRIES
; i
++) {
354 ar2313_descr_t
*td
= &sp
->tx_ring
[i
];
355 printk("Tx desc %2d: %08x %08x %08x %08x\n", i
,
356 td
->status
, td
->devcs
, td
->addr
, td
->descr
);
363 ar2313_tx_timeout(struct net_device
*dev
)
365 struct ar2313_private
*sp
= (struct ar2313_private
*)dev
->priv
;
369 printk("Tx timeout\n");
371 spin_lock_irqsave(&sp
->lock
, flags
);
373 spin_unlock_irqrestore(&sp
->lock
, flags
);
379 printMcList(struct net_device
*dev
)
381 struct dev_mc_list
*list
= dev
->mc_list
;
384 printk("%d MC ADDR ", num
);
385 for(i
=0;i
<list
->dmi_addrlen
;i
++) {
386 printk(":%02x", list
->dmi_addr
[i
]);
395 * Set or clear the multicast filter for this adaptor.
396 * THIS IS ABSOLUTE CRAP, disabled
399 ar2313_multicast_list(struct net_device
*dev
)
402 * Always listen to broadcasts and
403 * treat IFF bits independently
405 struct ar2313_private
*sp
= (struct ar2313_private
*)dev
->priv
;
406 unsigned int recognise
;
408 recognise
= sp
->eth_regs
->mac_control
;
410 if (dev
->flags
& IFF_PROMISC
) { /* set promiscuous mode */
411 recognise
|= MAC_CONTROL_PR
;
413 recognise
&= ~MAC_CONTROL_PR
;
416 if ((dev
->flags
& IFF_ALLMULTI
) || (dev
->mc_count
> 15)) {
419 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__
, dev
->mc_count
);
421 recognise
|= MAC_CONTROL_PM
;/* all multicast */
422 } else if (dev
->mc_count
> 0) {
425 printk("%s: mc_count %d\n", __FUNCTION__
, dev
->mc_count
);
427 recognise
|= MAC_CONTROL_PM
; /* for the time being */
430 printk("%s: setting %08x to %08x\n", __FUNCTION__
, (int)sp
->eth_regs
, recognise
);
433 sp
->eth_regs
->mac_control
= recognise
;
436 static void rx_tasklet_cleanup(struct net_device
*dev
)
438 struct ar2313_private
*sp
= dev
->priv
;
441 * Tasklet may be scheduled. Need to get it removed from the list
442 * since we're about to free the struct.
446 tasklet_enable(&sp
->rx_tasklet
);
447 tasklet_kill(&sp
->rx_tasklet
);
450 static int __exit
ar2313_remove(struct platform_device
*pdev
)
452 struct net_device
*dev
= platform_get_drvdata(pdev
);
453 rx_tasklet_cleanup(dev
);
454 ar2313_init_cleanup(dev
);
455 unregister_netdev(dev
);
462 * Restart the AR2313 ethernet controller.
464 static int ar2313_restart(struct net_device
*dev
)
466 /* disable interrupts */
467 disable_irq(dev
->irq
);
475 /* enable interrupts */
476 enable_irq(dev
->irq
);
481 static struct platform_driver ar2313_driver
= {
482 .driver
.name
= "ar531x-eth",
483 .probe
= ar2313_probe
,
484 .remove
= ar2313_remove
,
487 int __init
ar2313_module_init(void)
489 return platform_driver_register(&ar2313_driver
);
492 void __exit
ar2313_module_cleanup(void)
494 platform_driver_unregister(&ar2313_driver
);
497 module_init(ar2313_module_init
);
498 module_exit(ar2313_module_cleanup
);
501 static void ar2313_free_descriptors(struct net_device
*dev
)
503 struct ar2313_private
*sp
= dev
->priv
;
504 if (sp
->rx_ring
!= NULL
) {
505 kfree((void*)KSEG0ADDR(sp
->rx_ring
));
512 static int ar2313_allocate_descriptors(struct net_device
*dev
)
514 struct ar2313_private
*sp
= dev
->priv
;
517 ar2313_descr_t
*space
;
519 if(sp
->rx_ring
!= NULL
){
520 printk("%s: already done.\n", __FUNCTION__
);
524 size
= (sizeof(ar2313_descr_t
) * (AR2313_DESCR_ENTRIES
* AR2313_QUEUES
));
525 space
= kmalloc(size
, GFP_KERNEL
);
529 /* invalidate caches */
530 dma_cache_inv((unsigned int)space
, size
);
532 /* now convert pointer to KSEG1 */
533 space
= (ar2313_descr_t
*)KSEG1ADDR(space
);
535 memset((void *)space
, 0, size
);
538 space
+= AR2313_DESCR_ENTRIES
;
541 space
+= AR2313_DESCR_ENTRIES
;
543 /* Initialize the transmit Descriptors */
544 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
545 ar2313_descr_t
*td
= &sp
->tx_ring
[j
];
547 td
->devcs
= DMA_TX1_CHAINED
;
549 td
->descr
= K1_TO_PHYS(&sp
->tx_ring
[(j
+1) & (AR2313_DESCR_ENTRIES
-1)]);
557 * Generic cleanup handling data allocated during init. Used when the
558 * module is unloaded or if an error occurs during initialization
560 static void ar2313_init_cleanup(struct net_device
*dev
)
562 struct ar2313_private
*sp
= dev
->priv
;
566 ar2313_free_descriptors(dev
);
568 if (sp
->eth_regs
) iounmap((void*)sp
->eth_regs
);
569 if (sp
->dma_regs
) iounmap((void*)sp
->dma_regs
);
572 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
575 sp
->rx_skb
[j
] = NULL
;
584 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
587 sp
->tx_skb
[j
] = NULL
;
596 static int ar2313_setup_timer(struct net_device
*dev
)
598 struct ar2313_private
*sp
= dev
->priv
;
600 init_timer(&sp
->link_timer
);
602 sp
->link_timer
.function
= ar2313_link_timer_fn
;
603 sp
->link_timer
.data
= (int) dev
;
604 sp
->link_timer
.expires
= jiffies
+ HZ
;
606 add_timer(&sp
->link_timer
);
611 static void ar2313_link_timer_fn(unsigned long data
)
613 struct net_device
*dev
= (struct net_device
*) data
;
614 struct ar2313_private
*sp
= dev
->priv
;
616 // see if the link status changed
617 // This was needed to make sure we set the PHY to the
618 // autonegotiated value of half or full duplex.
619 ar2313_check_link(dev
);
621 // Loop faster when we don't have link.
622 // This was needed to speed up the AP bootstrap time.
624 mod_timer(&sp
->link_timer
, jiffies
+ HZ
/2);
626 mod_timer(&sp
->link_timer
, jiffies
+ LINK_TIMER
);
630 static void ar2313_check_link(struct net_device
*dev
)
632 struct ar2313_private
*sp
= dev
->priv
;
635 phyData
= armiiread(sp
->phy
, MII_BMSR
);
636 if (sp
->phyData
!= phyData
) {
637 if (phyData
& BMSR_LSTATUS
) {
638 /* link is present, ready link partner ability to deterine duplexity */
643 reg
= armiiread(sp
->phy
, MII_BMCR
);
644 if (reg
& BMCR_ANENABLE
) {
645 /* auto neg enabled */
646 reg
= armiiread(sp
->phy
, MII_LPA
);
647 duplex
= (reg
& (LPA_100FULL
|LPA_10FULL
))? 1:0;
649 /* no auto neg, just read duplex config */
650 duplex
= (reg
& BMCR_FULLDPLX
)? 1:0;
653 printk(KERN_INFO
"%s: Configuring MAC for %s duplex\n", dev
->name
,
654 (duplex
)? "full":"half");
658 sp
->eth_regs
->mac_control
= ((sp
->eth_regs
->mac_control
| MAC_CONTROL_F
) &
662 sp
->eth_regs
->mac_control
= ((sp
->eth_regs
->mac_control
| MAC_CONTROL_DRO
) &
669 sp
->phyData
= phyData
;
674 ar2313_reset_reg(struct net_device
*dev
)
676 struct ar2313_private
*sp
= (struct ar2313_private
*)dev
->priv
;
677 unsigned int ethsal
, ethsah
;
680 *sp
->int_regs
|= ar_int_mac_mask
;
682 *sp
->int_regs
&= ~ar_int_mac_mask
;
684 *sp
->int_regs
|= ar_int_phy_mask
;
686 *sp
->int_regs
&= ~ar_int_phy_mask
;
689 sp
->dma_regs
->bus_mode
= (DMA_BUS_MODE_SWR
);
691 sp
->dma_regs
->bus_mode
= ((32 << DMA_BUS_MODE_PBL_SHIFT
) | DMA_BUS_MODE_BLE
);
693 /* enable interrupts */
694 sp
->dma_regs
->intr_ena
= (DMA_STATUS_AIS
|
699 sp
->dma_regs
->xmt_base
= K1_TO_PHYS(sp
->tx_ring
);
700 sp
->dma_regs
->rcv_base
= K1_TO_PHYS(sp
->rx_ring
);
701 sp
->dma_regs
->control
= (DMA_CONTROL_SR
| DMA_CONTROL_ST
| DMA_CONTROL_SF
);
703 sp
->eth_regs
->flow_control
= (FLOW_CONTROL_FCE
);
704 sp
->eth_regs
->vlan_tag
= (0x8100);
706 /* Enable Ethernet Interface */
707 flags
= (MAC_CONTROL_TE
| /* transmit enable */
708 MAC_CONTROL_PM
| /* pass mcast */
709 MAC_CONTROL_F
| /* full duplex */
710 MAC_CONTROL_HBD
); /* heart beat disabled */
712 if (dev
->flags
& IFF_PROMISC
) { /* set promiscuous mode */
713 flags
|= MAC_CONTROL_PR
;
715 sp
->eth_regs
->mac_control
= flags
;
717 /* Set all Ethernet station address registers to their initial values */
718 ethsah
= ((((u_int
)(dev
->dev_addr
[5]) << 8) & (u_int
)0x0000FF00) |
719 (((u_int
)(dev
->dev_addr
[4]) << 0) & (u_int
)0x000000FF));
721 ethsal
= ((((u_int
)(dev
->dev_addr
[3]) << 24) & (u_int
)0xFF000000) |
722 (((u_int
)(dev
->dev_addr
[2]) << 16) & (u_int
)0x00FF0000) |
723 (((u_int
)(dev
->dev_addr
[1]) << 8) & (u_int
)0x0000FF00) |
724 (((u_int
)(dev
->dev_addr
[0]) << 0) & (u_int
)0x000000FF) );
726 sp
->eth_regs
->mac_addr
[0] = ethsah
;
727 sp
->eth_regs
->mac_addr
[1] = ethsal
;
735 static int ar2313_init(struct net_device
*dev
)
737 struct ar2313_private
*sp
= dev
->priv
;
741 * Allocate descriptors
743 if (ar2313_allocate_descriptors(dev
)) {
744 printk("%s: %s: ar2313_allocate_descriptors failed\n",
745 dev
->name
, __FUNCTION__
);
751 * Get the memory for the skb rings.
753 if(sp
->rx_skb
== NULL
) {
754 sp
->rx_skb
= kmalloc(sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
, GFP_KERNEL
);
756 printk("%s: %s: rx_skb kmalloc failed\n",
757 dev
->name
, __FUNCTION__
);
762 memset(sp
->rx_skb
, 0, sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
);
764 if(sp
->tx_skb
== NULL
) {
765 sp
->tx_skb
= kmalloc(sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
, GFP_KERNEL
);
767 printk("%s: %s: tx_skb kmalloc failed\n",
768 dev
->name
, __FUNCTION__
);
773 memset(sp
->tx_skb
, 0, sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
);
776 * Set tx_csm before we start receiving interrupts, otherwise
777 * the interrupt handler might think it is supposed to process
778 * tx ints before we are up and running, which may cause a null
779 * pointer access in the int handler.
787 * Zero the stats before starting the interface
789 memset(&sp
->stats
, 0, sizeof(sp
->stats
));
792 * We load the ring here as there seem to be no way to tell the
793 * firmware to wipe the ring without re-initializing it.
795 ar2313_load_rx_ring(dev
, RX_RING_SIZE
);
800 ar2313_reset_reg(dev
);
805 ecode
= request_irq(dev
->irq
, &ar2313_interrupt
, IRQF_SHARED
| IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
, dev
->name
, dev
);
807 printk(KERN_WARNING
"%s: %s: Requested IRQ %d is busy\n",
808 dev
->name
, __FUNCTION__
, dev
->irq
);
813 tasklet_enable(&sp
->rx_tasklet
);
818 ar2313_init_cleanup(dev
);
825 * Loading rings is safe without holding the spin lock since this is
826 * done only before the device is enabled, thus no interrupts are
827 * generated and by the interrupt handler/tasklet handler.
829 static void ar2313_load_rx_ring(struct net_device
*dev
, int nr_bufs
)
832 struct ar2313_private
*sp
= ((struct net_device
*)dev
)->priv
;
837 for (i
= 0; i
< nr_bufs
; i
++) {
841 if (sp
->rx_skb
[idx
]) {
843 printk(KERN_INFO
"ar2313 rx refill full\n");
848 // partha: create additional room for the second GRE fragment
849 skb
= alloc_skb(AR2313_BUFSIZE
+128, GFP_ATOMIC
);
851 printk("\n\n\n\n %s: No memory in system\n\n\n\n", __FUNCTION__
);
854 // partha: create additional room in the front for tx pkt capture
855 skb_reserve(skb
, 32);
858 * Make sure IP header starts on a fresh cache line.
861 skb_reserve(skb
, RX_OFFSET
);
862 sp
->rx_skb
[idx
] = skb
;
864 rd
= (ar2313_descr_t
*) &sp
->rx_ring
[idx
];
866 /* initialize dma descriptor */
867 rd
->devcs
= ((AR2313_BUFSIZE
<< DMA_RX1_BSIZE_SHIFT
) |
869 rd
->addr
= virt_to_phys(skb
->data
);
870 rd
->descr
= virt_to_phys(&sp
->rx_ring
[(idx
+1) & (AR2313_DESCR_ENTRIES
-1)]);
871 rd
->status
= DMA_RX_OWN
;
878 printk(KERN_INFO
"Out of memory when allocating standard receive buffers\n");
887 #define AR2313_MAX_PKTS_PER_CALL 64
889 static int ar2313_rx_int(struct net_device
*dev
)
891 struct ar2313_private
*sp
= dev
->priv
;
892 struct sk_buff
*skb
, *skb_new
;
893 ar2313_descr_t
*rxdesc
;
901 /* process at most the entire ring and then wait for another interrupt */
904 rxdesc
= &sp
->rx_ring
[idx
];
905 status
= rxdesc
->status
;
906 if (status
& DMA_RX_OWN
) {
907 /* SiByte owns descriptor or descr not yet filled in */
912 if (++pkts
> AR2313_MAX_PKTS_PER_CALL
) {
918 printk("index %d\n", idx
);
919 printk("RX status %08x\n", rxdesc
->status
);
920 printk("RX devcs %08x\n", rxdesc
->devcs
);
921 printk("RX addr %08x\n", rxdesc
->addr
);
922 printk("RX descr %08x\n", rxdesc
->descr
);
925 if ((status
& (DMA_RX_ERROR
|DMA_RX_ERR_LENGTH
)) &&
926 (!(status
& DMA_RX_LONG
))){
928 printk("%s: rx ERROR %08x\n", __FUNCTION__
, status
);
930 sp
->stats
.rx_errors
++;
931 sp
->stats
.rx_dropped
++;
933 /* add statistics counters */
934 if (status
& DMA_RX_ERR_CRC
) sp
->stats
.rx_crc_errors
++;
935 if (status
& DMA_RX_ERR_COL
) sp
->stats
.rx_over_errors
++;
936 if (status
& DMA_RX_ERR_LENGTH
)
937 sp
->stats
.rx_length_errors
++;
938 if (status
& DMA_RX_ERR_RUNT
) sp
->stats
.rx_over_errors
++;
939 if (status
& DMA_RX_ERR_DESC
) sp
->stats
.rx_over_errors
++;
942 /* alloc new buffer. */
943 skb_new
= dev_alloc_skb(AR2313_BUFSIZE
+ RX_OFFSET
+ 128);
944 if (skb_new
!= NULL
) {
946 skb
= sp
->rx_skb
[idx
];
948 skb_put(skb
, ((status
>> DMA_RX_LEN_SHIFT
) & 0x3fff) - CRC_LEN
);
951 if ((dev
->am_pkt_handler
== NULL
) ||
952 (dev
->am_pkt_handler(skb
, dev
) == 0)) {
954 sp
->stats
.rx_bytes
+= skb
->len
;
955 skb
->protocol
= eth_type_trans(skb
, dev
);
956 /* pass the packet to upper layers */
959 if (dev
->asap_netif_rx
)
960 dev
->asap_netif_rx(skb
);
969 skb_reserve(skb_new
, RX_OFFSET
+32);
970 /* reset descriptor's curr_addr */
971 rxdesc
->addr
= virt_to_phys(skb_new
->data
);
973 sp
->stats
.rx_packets
++;
974 sp
->rx_skb
[idx
] = skb_new
;
977 sp
->stats
.rx_dropped
++;
981 rxdesc
->devcs
= ((AR2313_BUFSIZE
<< DMA_RX1_BSIZE_SHIFT
) |
983 rxdesc
->status
= DMA_RX_OWN
;
994 static void ar2313_tx_int(struct net_device
*dev
)
996 struct ar2313_private
*sp
= dev
->priv
;
999 ar2313_descr_t
*txdesc
;
1000 unsigned int status
=0;
1004 while (idx
!= sp
->tx_prd
) {
1006 txdesc
= &sp
->tx_ring
[idx
];
1009 printk("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
1010 dev
->name
, sp
->tx_csm
, idx
, sp
->tx_prd
,
1011 txdesc
->status
, txdesc
->devcs
, txdesc
->addr
, txdesc
->descr
);
1014 if ((status
= txdesc
->status
) & DMA_TX_OWN
) {
1015 /* ar2313 dma still owns descr */
1018 /* done with this descriptor */
1019 dma_unmap_single(NULL
, txdesc
->addr
, txdesc
->devcs
& DMA_TX1_BSIZE_MASK
, DMA_TO_DEVICE
);
1022 if (status
& DMA_TX_ERROR
){
1023 sp
->stats
.tx_errors
++;
1024 sp
->stats
.tx_dropped
++;
1025 if(status
& DMA_TX_ERR_UNDER
)
1026 sp
->stats
.tx_fifo_errors
++;
1027 if(status
& DMA_TX_ERR_HB
)
1028 sp
->stats
.tx_heartbeat_errors
++;
1029 if(status
& (DMA_TX_ERR_LOSS
|
1031 sp
->stats
.tx_carrier_errors
++;
1032 if (status
& (DMA_TX_ERR_LATE
|
1036 sp
->stats
.tx_aborted_errors
++;
1039 sp
->stats
.tx_packets
++;
1042 skb
= sp
->tx_skb
[idx
];
1043 sp
->tx_skb
[idx
] = NULL
;
1044 idx
= DSC_NEXT(idx
);
1045 sp
->stats
.tx_bytes
+= skb
->len
;
1046 dev_kfree_skb_irq(skb
);
1056 rx_tasklet_func(unsigned long data
)
1058 struct net_device
*dev
= (struct net_device
*) data
;
1059 struct ar2313_private
*sp
= dev
->priv
;
1061 if (sp
->unloading
) {
1065 if (ar2313_rx_int(dev
)) {
1066 tasklet_hi_schedule(&sp
->rx_tasklet
);
1069 unsigned long flags
;
1070 spin_lock_irqsave(&sp
->lock
, flags
);
1071 sp
->dma_regs
->intr_ena
|= DMA_STATUS_RI
;
1072 spin_unlock_irqrestore(&sp
->lock
, flags
);
1077 rx_schedule(struct net_device
*dev
)
1079 struct ar2313_private
*sp
= dev
->priv
;
1081 sp
->dma_regs
->intr_ena
&= ~DMA_STATUS_RI
;
1083 tasklet_hi_schedule(&sp
->rx_tasklet
);
1086 static irqreturn_t
ar2313_interrupt(int irq
, void *dev_id
)
1088 struct net_device
*dev
= (struct net_device
*)dev_id
;
1089 struct ar2313_private
*sp
= dev
->priv
;
1090 unsigned int status
, enabled
;
1092 /* clear interrupt */
1094 * Don't clear RI bit if currently disabled.
1096 status
= sp
->dma_regs
->status
;
1097 enabled
= sp
->dma_regs
->intr_ena
;
1098 sp
->dma_regs
->status
= status
& enabled
;
1100 if (status
& DMA_STATUS_NIS
) {
1103 * Don't schedule rx processing if interrupt
1104 * is already disabled.
1106 if (status
& enabled
& DMA_STATUS_RI
) {
1107 /* receive interrupt */
1110 if (status
& DMA_STATUS_TI
) {
1111 /* transmit interrupt */
1116 if (status
& DMA_STATUS_AIS
) {
1118 printk("%s: AIS set %08x & %x\n", __FUNCTION__
,
1119 status
, (DMA_STATUS_FBE
| DMA_STATUS_TPS
));
1121 /* abnormal status */
1122 if (status
& (DMA_STATUS_FBE
| DMA_STATUS_TPS
)) {
1123 ar2313_restart(dev
);
1130 static int ar2313_open(struct net_device
*dev
)
1132 struct ar2313_private
*sp
;
1137 netif_start_queue(dev
);
1139 sp
->eth_regs
->mac_control
|= MAC_CONTROL_RE
;
1141 AR2313_MOD_INC_USE_COUNT
;
1146 static void ar2313_halt(struct net_device
*dev
)
1148 struct ar2313_private
*sp
= dev
->priv
;
1151 tasklet_disable(&sp
->rx_tasklet
);
1154 sp
->eth_regs
->mac_control
&= ~(MAC_CONTROL_RE
| /* disable Receives */
1155 MAC_CONTROL_TE
); /* disable Transmits */
1157 sp
->dma_regs
->control
= 0;
1158 sp
->dma_regs
->bus_mode
= DMA_BUS_MODE_SWR
;
1160 /* place phy and MAC in reset */
1161 *sp
->int_regs
|= (ar_int_mac_mask
| ar_int_phy_mask
);
1163 /* free buffers on tx ring */
1164 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
1165 struct sk_buff
*skb
;
1166 ar2313_descr_t
*txdesc
;
1168 txdesc
= &sp
->tx_ring
[j
];
1171 skb
= sp
->tx_skb
[j
];
1174 sp
->tx_skb
[j
] = NULL
;
1180 * close should do nothing. Here's why. It's called when
1181 * 'ifconfig bond0 down' is run. If it calls free_irq then
1182 * the irq is gone forever ! When bond0 is made 'up' again,
1183 * the ar2313_open () does not call request_irq (). Worse,
1184 * the call to ar2313_halt() generates a WDOG reset due to
1185 * the write to 'sp->int_regs' and the box reboots.
1186 * Commenting this out is good since it allows the
1187 * system to resume when bond0 is made up again.
1189 static int ar2313_close(struct net_device
*dev
)
1193 * Disable interrupts
1195 disable_irq(dev
->irq
);
1198 * Without (or before) releasing irq and stopping hardware, this
1199 * is an absolute non-sense, by the way. It will be reset instantly
1202 netif_stop_queue(dev
);
1204 /* stop the MAC and DMA engines */
1207 /* release the interrupt */
1208 free_irq(dev
->irq
, dev
);
1211 AR2313_MOD_DEC_USE_COUNT
;
1215 static int ar2313_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1217 struct ar2313_private
*sp
= dev
->priv
;
1222 td
= &sp
->tx_ring
[idx
];
1224 if (td
->status
& DMA_TX_OWN
) {
1226 printk("%s: No space left to Tx\n", __FUNCTION__
);
1228 /* free skbuf and lie to the caller that we sent it out */
1229 sp
->stats
.tx_dropped
++;
1232 /* restart transmitter in case locked */
1233 sp
->dma_regs
->xmt_poll
= 0;
1237 /* Setup the transmit descriptor. */
1238 td
->devcs
= ((skb
->len
<< DMA_TX1_BSIZE_SHIFT
) |
1239 (DMA_TX1_LS
|DMA_TX1_IC
|DMA_TX1_CHAINED
));
1240 td
->addr
= dma_map_single(NULL
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
1241 td
->status
= DMA_TX_OWN
;
1243 /* kick transmitter last */
1244 sp
->dma_regs
->xmt_poll
= 0;
1247 printk("index %d\n", idx
);
1248 printk("TX status %08x\n", td
->status
);
1249 printk("TX devcs %08x\n", td
->devcs
);
1250 printk("TX addr %08x\n", td
->addr
);
1251 printk("TX descr %08x\n", td
->descr
);
1254 sp
->tx_skb
[idx
] = skb
;
1255 idx
= DSC_NEXT(idx
);
1258 //dev->trans_start = jiffies;
1263 static int netdev_get_ecmd(struct net_device
*dev
, struct ethtool_cmd
*ecmd
)
1265 struct ar2313_private
*np
= dev
->priv
;
1269 (SUPPORTED_10baseT_Half
| SUPPORTED_10baseT_Full
|
1270 SUPPORTED_100baseT_Half
| SUPPORTED_100baseT_Full
|
1271 SUPPORTED_Autoneg
| SUPPORTED_TP
| SUPPORTED_MII
);
1273 ecmd
->port
= PORT_TP
;
1274 /* only supports internal transceiver */
1275 ecmd
->transceiver
= XCVR_INTERNAL
;
1276 /* not sure what this is for */
1277 ecmd
->phy_address
= 1;
1279 ecmd
->advertising
= ADVERTISED_MII
;
1280 tmp
= armiiread(np
->phy
, MII_ADVERTISE
);
1281 if (tmp
& ADVERTISE_10HALF
)
1282 ecmd
->advertising
|= ADVERTISED_10baseT_Half
;
1283 if (tmp
& ADVERTISE_10FULL
)
1284 ecmd
->advertising
|= ADVERTISED_10baseT_Full
;
1285 if (tmp
& ADVERTISE_100HALF
)
1286 ecmd
->advertising
|= ADVERTISED_100baseT_Half
;
1287 if (tmp
& ADVERTISE_100FULL
)
1288 ecmd
->advertising
|= ADVERTISED_100baseT_Full
;
1290 tmp
= armiiread(np
->phy
, MII_BMCR
);
1291 if (tmp
& BMCR_ANENABLE
) {
1292 ecmd
->advertising
|= ADVERTISED_Autoneg
;
1293 ecmd
->autoneg
= AUTONEG_ENABLE
;
1295 ecmd
->autoneg
= AUTONEG_DISABLE
;
1298 if (ecmd
->autoneg
== AUTONEG_ENABLE
) {
1299 tmp
= armiiread(np
->phy
, MII_LPA
);
1300 if (tmp
& (LPA_100FULL
|LPA_10FULL
)) {
1301 ecmd
->duplex
= DUPLEX_FULL
;
1303 ecmd
->duplex
= DUPLEX_HALF
;
1305 if (tmp
& (LPA_100FULL
|LPA_100HALF
)) {
1306 ecmd
->speed
= SPEED_100
;
1308 ecmd
->speed
= SPEED_10
;
1311 if (tmp
& BMCR_FULLDPLX
) {
1312 ecmd
->duplex
= DUPLEX_FULL
;
1314 ecmd
->duplex
= DUPLEX_HALF
;
1316 if (tmp
& BMCR_SPEED100
) {
1317 ecmd
->speed
= SPEED_100
;
1319 ecmd
->speed
= SPEED_10
;
1323 /* ignore maxtxpkt, maxrxpkt for now */
1328 static int netdev_set_ecmd(struct net_device
*dev
, struct ethtool_cmd
*ecmd
)
1330 struct ar2313_private
*np
= dev
->priv
;
1333 if (ecmd
->speed
!= SPEED_10
&& ecmd
->speed
!= SPEED_100
)
1335 if (ecmd
->duplex
!= DUPLEX_HALF
&& ecmd
->duplex
!= DUPLEX_FULL
)
1337 if (ecmd
->port
!= PORT_TP
)
1339 if (ecmd
->transceiver
!= XCVR_INTERNAL
)
1341 if (ecmd
->autoneg
!= AUTONEG_DISABLE
&& ecmd
->autoneg
!= AUTONEG_ENABLE
)
1343 /* ignore phy_address, maxtxpkt, maxrxpkt for now */
1345 /* WHEW! now lets bang some bits */
1347 tmp
= armiiread(np
->phy
, MII_BMCR
);
1348 if (ecmd
->autoneg
== AUTONEG_ENABLE
) {
1349 /* turn on autonegotiation */
1350 tmp
|= BMCR_ANENABLE
;
1351 printk("%s: Enabling auto-neg\n", dev
->name
);
1353 /* turn off auto negotiation, set speed and duplexity */
1354 tmp
&= ~(BMCR_ANENABLE
| BMCR_SPEED100
| BMCR_FULLDPLX
);
1355 if (ecmd
->speed
== SPEED_100
)
1356 tmp
|= BMCR_SPEED100
;
1357 if (ecmd
->duplex
== DUPLEX_FULL
)
1358 tmp
|= BMCR_FULLDPLX
;
1359 printk("%s: Hard coding %d/%s\n", dev
->name
,
1360 (ecmd
->speed
== SPEED_100
)? 100:10,
1361 (ecmd
->duplex
== DUPLEX_FULL
)? "full":"half");
1363 armiiwrite(np
->phy
, MII_BMCR
, tmp
);
1368 static int netdev_ethtool_ioctl(struct net_device
*dev
, void *useraddr
)
1370 struct ar2313_private
*np
= dev
->priv
;
1373 if (get_user(cmd
, (u32
*)useraddr
))
1378 case ETHTOOL_GSET
: {
1379 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1380 spin_lock_irq(&np
->lock
);
1381 netdev_get_ecmd(dev
, &ecmd
);
1382 spin_unlock_irq(&np
->lock
);
1383 if (copy_to_user(useraddr
, &ecmd
, sizeof(ecmd
)))
1388 case ETHTOOL_SSET
: {
1389 struct ethtool_cmd ecmd
;
1391 if (copy_from_user(&ecmd
, useraddr
, sizeof(ecmd
)))
1393 spin_lock_irq(&np
->lock
);
1394 r
= netdev_set_ecmd(dev
, &ecmd
);
1395 spin_unlock_irq(&np
->lock
);
1398 /* restart autonegotiation */
1399 case ETHTOOL_NWAY_RST
: {
1402 /* if autoneg is off, it's an error */
1403 tmp
= armiiread(np
->phy
, MII_BMCR
);
1404 if (tmp
& BMCR_ANENABLE
) {
1405 tmp
|= (BMCR_ANRESTART
);
1406 armiiwrite(np
->phy
, MII_BMCR
, tmp
);
1411 /* get link status */
1412 case ETHTOOL_GLINK
: {
1413 struct ethtool_value edata
= {ETHTOOL_GLINK
};
1414 edata
.data
= (armiiread(np
->phy
, MII_BMSR
)&BMSR_LSTATUS
) ? 1:0;
1415 if (copy_to_user(useraddr
, &edata
, sizeof(edata
)))
1424 static int ar2313_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1426 struct mii_ioctl_data
*data
= (struct mii_ioctl_data
*)&ifr
->ifr_data
;
1429 case SIOCDEVPRIVATE
: {
1430 struct ar2313_cmd scmd
;
1432 if (copy_from_user(&scmd
, ifr
->ifr_data
, sizeof(scmd
)))
1436 printk("%s: ioctl devprivate c=%d a=%x l=%d m=%d d=%x,%x\n",
1437 dev
->name
, scmd
.cmd
,
1438 scmd
.address
, scmd
.length
,
1439 scmd
.mailbox
, scmd
.data
[0], scmd
.data
[1]);
1443 case AR2313_READ_DATA
:
1445 scmd
.data
[0] = *((u32
*)scmd
.address
);
1446 } else if(scmd
.length
==2) {
1447 scmd
.data
[0] = *((u16
*)scmd
.address
);
1448 } else if (scmd
.length
==1) {
1449 scmd
.data
[0] = *((u8
*)scmd
.address
);
1453 if(copy_to_user(ifr
->ifr_data
, &scmd
, sizeof(scmd
)))
1457 case AR2313_WRITE_DATA
:
1459 *((u32
*)scmd
.address
) = scmd
.data
[0];
1460 } else if(scmd
.length
==2) {
1461 *((u16
*)scmd
.address
) = scmd
.data
[0];
1462 } else if (scmd
.length
==1) {
1463 *((u8
*)scmd
.address
) = scmd
.data
[0];
1469 case AR2313_GET_VERSION
:
1470 // SAMEER: sprintf((char*) &scmd, "%s", ARUBA_VERSION);
1471 if(copy_to_user(ifr
->ifr_data
, &scmd
, sizeof(scmd
)))
1482 return netdev_ethtool_ioctl(dev
, (void *) ifr
->ifr_data
);
1484 case SIOCGMIIPHY
: /* Get address of MII PHY in use. */
1488 case SIOCGMIIREG
: /* Read MII PHY register. */
1489 case SIOCDEVPRIVATE
+1: /* for binary compat, remove in 2.5 */
1490 data
->val_out
= armiiread(data
->phy_id
& 0x1f,
1491 data
->reg_num
& 0x1f);
1493 case SIOCSMIIREG
: /* Write MII PHY register. */
1494 case SIOCDEVPRIVATE
+2: /* for binary compat, remove in 2.5 */
1495 if (!capable(CAP_NET_ADMIN
))
1497 armiiwrite(data
->phy_id
& 0x1f,
1498 data
->reg_num
& 0x1f, data
->val_in
);
1502 if (copy_from_user(dev
->dev_addr
, ifr
->ifr_data
, sizeof(dev
->dev_addr
)))
1507 if (copy_to_user(ifr
->ifr_data
, dev
->dev_addr
, sizeof(dev
->dev_addr
)))
1518 static struct net_device_stats
*ar2313_get_stats(struct net_device
*dev
)
1520 struct ar2313_private
*sp
= dev
->priv
;
1525 armiiread(short phy
, short reg
)
1527 volatile ETHERNET_STRUCT
* ethernet
;
1529 ethernet
= (volatile ETHERNET_STRUCT
*)(ar_eth_base
); /* always MAC 0 */
1530 ethernet
->mii_addr
= ((reg
<< MII_ADDR_REG_SHIFT
) |
1531 (phy
<< MII_ADDR_PHY_SHIFT
));
1532 while (ethernet
->mii_addr
& MII_ADDR_BUSY
);
1533 return (ethernet
->mii_data
>> MII_DATA_SHIFT
);
1537 armiiwrite(short phy
, short reg
, short data
)
1539 volatile ETHERNET_STRUCT
* ethernet
;
1541 ethernet
= (volatile ETHERNET_STRUCT
*)(ar_eth_base
); /* always MAC 0 */
1542 while (ethernet
->mii_addr
& MII_ADDR_BUSY
);
1543 ethernet
->mii_data
= data
<< MII_DATA_SHIFT
;
1544 ethernet
->mii_addr
= ((reg
<< MII_ADDR_REG_SHIFT
) |
1545 (phy
<< MII_ADDR_PHY_SHIFT
) |