2 * ar2313.c: Linux driver for the Atheros AR231x 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-2007 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/phy.h>
40 #include <linux/ethtool.h>
41 #include <linux/ctype.h>
42 #include <linux/platform_device.h>
47 #include <asm/system.h>
50 #include <asm/byteorder.h>
51 #include <asm/uaccess.h>
52 #include <asm/bootinfo.h>
54 #define AR2313_MTU 1692
55 #define AR2313_PRIOS 1
56 #define AR2313_QUEUES (2*AR2313_PRIOS)
57 #define AR2313_DESCR_ENTRIES 64
68 #define min(a,b) (((a)<(b))?(a):(b))
71 #ifndef SMP_CACHE_BYTES
72 #define SMP_CACHE_BYTES L1_CACHE_BYTES
75 #define AR2313_MBOX_SET_BIT 0x8
77 #define BOARD_IDX_STATIC 0
78 #define BOARD_IDX_OVERFLOW -1
84 * New interrupt handler strategy:
86 * An old interrupt handler worked using the traditional method of
87 * replacing an skbuff with a new one when a packet arrives. However
88 * the rx rings do not need to contain a static number of buffer
89 * descriptors, thus it makes sense to move the memory allocation out
90 * of the main interrupt handler and do it in a bottom half handler
91 * and only allocate new buffers when the number of buffers in the
92 * ring is below a certain threshold. In order to avoid starving the
93 * NIC under heavy load it is however necessary to force allocation
94 * when hitting a minimum threshold. The strategy for alloction is as
97 * RX_LOW_BUF_THRES - allocate buffers in the bottom half
98 * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
99 * the buffers in the interrupt handler
100 * RX_RING_THRES - maximum number of buffers in the rx ring
102 * One advantagous side effect of this allocation approach is that the
103 * entire rx processing can be done without holding any spin lock
104 * since the rx rings and registers are totally independent of the tx
105 * ring and its registers. This of course includes the kmalloc's of
106 * new skb's. Thus start_xmit can run in parallel with rx processing
107 * and the memory allocation on SMP systems.
109 * Note that running the skb reallocation in a bottom half opens up
110 * another can of races which needs to be handled properly. In
111 * particular it can happen that the interrupt handler tries to run
112 * the reallocation while the bottom half is either running on another
113 * CPU or was interrupted on the same CPU. To get around this the
114 * driver uses bitops to prevent the reallocation routines from being
117 * TX handling can also be done without holding any spin lock, wheee
118 * this is fun! since tx_csm is only written to by the interrupt
123 * Threshold values for RX buffer allocation - the low water marks for
124 * when to start refilling the rings are set to 75% of the ring
125 * sizes. It seems to make sense to refill the rings entirely from the
126 * intrrupt handler once it gets below the panic threshold, that way
127 * we don't risk that the refilling is moved to another CPU when the
128 * one running the interrupt handler just got the slab code hot in its
131 #define RX_RING_SIZE AR2313_DESCR_ENTRIES
132 #define RX_PANIC_THRES (RX_RING_SIZE/4)
133 #define RX_LOW_THRES ((3*RX_RING_SIZE)/4)
137 #define AR2313_BUFSIZE (AR2313_MTU + ETH_HLEN + CRC_LEN + RX_OFFSET)
140 MODULE_LICENSE("GPL");
141 MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@openwrt.org>");
142 MODULE_DESCRIPTION("AR2313 Ethernet driver");
145 #define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
149 static void ar2313_tx_timeout(struct net_device
*dev
);
151 static void ar2313_halt(struct net_device
*dev
);
152 static void rx_tasklet_func(unsigned long data
);
153 static void rx_tasklet_cleanup(struct net_device
*dev
);
154 static void ar2313_multicast_list(struct net_device
*dev
);
156 static int mdiobus_read(struct mii_bus
*bus
, int phy_addr
, int regnum
);
157 static int mdiobus_write(struct mii_bus
*bus
, int phy_addr
, int regnum
, u16 value
);
158 static int mdiobus_reset(struct mii_bus
*bus
);
159 static int mdiobus_probe (struct net_device
*dev
);
160 static void ar2313_adjust_link(struct net_device
*dev
);
163 #define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
167 int __init
ar2313_probe(struct platform_device
*pdev
)
169 struct net_device
*dev
;
170 struct ar2313_private
*sp
;
171 struct resource
*res
;
172 unsigned long ar_eth_base
;
175 dev
= alloc_etherdev(sizeof(struct ar2313_private
));
179 "ar2313: Unable to allocate net_device structure!\n");
183 SET_MODULE_OWNER(dev
);
184 platform_set_drvdata(pdev
, dev
);
188 sp
->cfg
= pdev
->dev
.platform_data
;
190 sprintf(buf
, "eth%d_membase", pdev
->id
);
191 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, buf
);
196 ar_eth_base
= res
->start
;
197 sp
->phy
= sp
->cfg
->phy
;
199 sprintf(buf
, "eth%d_irq", pdev
->id
);
200 dev
->irq
= platform_get_irq_byname(pdev
, buf
);
202 spin_lock_init(&sp
->lock
);
204 /* initialize func pointers */
205 dev
->open
= &ar2313_open
;
206 dev
->stop
= &ar2313_close
;
207 dev
->hard_start_xmit
= &ar2313_start_xmit
;
209 dev
->get_stats
= &ar2313_get_stats
;
210 dev
->set_multicast_list
= &ar2313_multicast_list
;
212 dev
->tx_timeout
= ar2313_tx_timeout
;
213 dev
->watchdog_timeo
= AR2313_TX_TIMEOUT
;
215 dev
->do_ioctl
= &ar2313_ioctl
;
217 // SAMEER: do we need this?
218 dev
->features
|= NETIF_F_SG
| NETIF_F_HIGHDMA
;
220 tasklet_init(&sp
->rx_tasklet
, rx_tasklet_func
, (unsigned long) dev
);
221 tasklet_disable(&sp
->rx_tasklet
);
224 ioremap_nocache(virt_to_phys(ar_eth_base
), sizeof(*sp
->eth_regs
));
226 printk("Can't remap eth registers\n");
231 * When there's only one MAC, PHY regs are typically on ENET0,
232 * even though the MAC might be on ENET1.
233 * Needto remap PHY regs separately in this case
235 if (virt_to_phys(ar_eth_base
) == virt_to_phys(sp
->phy_regs
))
236 sp
->phy_regs
= sp
->eth_regs
;
239 ioremap_nocache(virt_to_phys(sp
->cfg
->phy_base
),
240 sizeof(*sp
->phy_regs
));
242 printk("Can't remap phy registers\n");
248 ioremap_nocache(virt_to_phys(ar_eth_base
+ 0x1000),
249 sizeof(*sp
->dma_regs
));
250 dev
->base_addr
= (unsigned int) sp
->dma_regs
;
252 printk("Can't remap DMA registers\n");
256 sp
->int_regs
= ioremap_nocache(virt_to_phys(sp
->cfg
->reset_base
), 4);
258 printk("Can't remap INTERRUPT registers\n");
262 strncpy(sp
->name
, "Atheros AR231x", sizeof(sp
->name
) - 1);
263 sp
->name
[sizeof(sp
->name
) - 1] = '\0';
264 memcpy(dev
->dev_addr
, sp
->cfg
->macaddr
, 6);
265 sp
->board_idx
= BOARD_IDX_STATIC
;
267 if (ar2313_init(dev
)) {
269 * ar2313_init() calls ar2313_init_cleanup() on error.
275 if (register_netdev(dev
)) {
276 printk("%s: register_netdev failed\n", __func__
);
280 printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
282 dev
->dev_addr
[0], dev
->dev_addr
[1], dev
->dev_addr
[2],
283 dev
->dev_addr
[3], dev
->dev_addr
[4], dev
->dev_addr
[5], dev
->irq
);
285 sp
->mii_bus
.priv
= dev
;
286 sp
->mii_bus
.read
= mdiobus_read
;
287 sp
->mii_bus
.write
= mdiobus_write
;
288 sp
->mii_bus
.reset
= mdiobus_reset
;
289 sp
->mii_bus
.name
= "ar2313_eth_mii";
291 sp
->mii_bus
.irq
= kmalloc(sizeof(int), GFP_KERNEL
);
292 *sp
->mii_bus
.irq
= PHY_POLL
;
294 mdiobus_register(&sp
->mii_bus
);
296 if (mdiobus_probe(dev
) != 0) {
297 printk(KERN_ERR
"ar2313: mdiobus_probe failed");
298 rx_tasklet_cleanup(dev
);
299 ar2313_init_cleanup(dev
);
300 unregister_netdev(dev
);
303 /* start link poll timer */
304 ar2313_setup_timer(dev
);
311 static void ar2313_dump_regs(struct net_device
*dev
)
313 unsigned int *ptr
, i
;
314 struct ar2313_private
*sp
= (struct ar2313_private
*) dev
->priv
;
316 ptr
= (unsigned int *) sp
->eth_regs
;
317 for (i
= 0; i
< (sizeof(ETHERNET_STRUCT
) / sizeof(unsigned int));
319 printk("ENET: %08x = %08x\n", (int) ptr
, *ptr
);
322 ptr
= (unsigned int *) sp
->dma_regs
;
323 for (i
= 0; i
< (sizeof(DMA
) / sizeof(unsigned int)); i
++, ptr
++) {
324 printk("DMA: %08x = %08x\n", (int) ptr
, *ptr
);
327 ptr
= (unsigned int *) sp
->int_regs
;
328 for (i
= 0; i
< (sizeof(INTERRUPT
) / sizeof(unsigned int)); i
++, ptr
++) {
329 printk("INT: %08x = %08x\n", (int) ptr
, *ptr
);
332 for (i
= 0; i
< AR2313_DESCR_ENTRIES
; i
++) {
333 ar2313_descr_t
*td
= &sp
->tx_ring
[i
];
334 printk("Tx desc %2d: %08x %08x %08x %08x\n", i
,
335 td
->status
, td
->devcs
, td
->addr
, td
->descr
);
341 static void ar2313_tx_timeout(struct net_device
*dev
)
343 struct ar2313_private
*sp
= (struct ar2313_private
*) dev
->priv
;
347 printk("Tx timeout\n");
349 spin_lock_irqsave(&sp
->lock
, flags
);
351 spin_unlock_irqrestore(&sp
->lock
, flags
);
356 static void printMcList(struct net_device
*dev
)
358 struct dev_mc_list
*list
= dev
->mc_list
;
361 printk("%d MC ADDR ", num
);
362 for (i
= 0; i
< list
->dmi_addrlen
; i
++) {
363 printk(":%02x", list
->dmi_addr
[i
]);
372 * Set or clear the multicast filter for this adaptor.
373 * THIS IS ABSOLUTE CRAP, disabled
375 static void ar2313_multicast_list(struct net_device
*dev
)
378 * Always listen to broadcasts and
379 * treat IFF bits independently
381 struct ar2313_private
*sp
= (struct ar2313_private
*) dev
->priv
;
382 unsigned int recognise
;
384 recognise
= sp
->eth_regs
->mac_control
;
386 if (dev
->flags
& IFF_PROMISC
) { /* set promiscuous mode */
387 recognise
|= MAC_CONTROL_PR
;
389 recognise
&= ~MAC_CONTROL_PR
;
392 if ((dev
->flags
& IFF_ALLMULTI
) || (dev
->mc_count
> 15)) {
395 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__
,
398 recognise
|= MAC_CONTROL_PM
; /* all multicast */
399 } else if (dev
->mc_count
> 0) {
402 printk("%s: mc_count %d\n", __FUNCTION__
, dev
->mc_count
);
404 recognise
|= MAC_CONTROL_PM
; /* for the time being */
407 printk("%s: setting %08x to %08x\n", __FUNCTION__
, (int) sp
->eth_regs
,
411 sp
->eth_regs
->mac_control
= recognise
;
414 static void rx_tasklet_cleanup(struct net_device
*dev
)
416 struct ar2313_private
*sp
= dev
->priv
;
419 * Tasklet may be scheduled. Need to get it removed from the list
420 * since we're about to free the struct.
424 tasklet_enable(&sp
->rx_tasklet
);
425 tasklet_kill(&sp
->rx_tasklet
);
428 static int __exit
ar2313_remove(struct platform_device
*pdev
)
430 struct net_device
*dev
= platform_get_drvdata(pdev
);
431 rx_tasklet_cleanup(dev
);
432 ar2313_init_cleanup(dev
);
433 unregister_netdev(dev
);
440 * Restart the AR2313 ethernet controller.
442 static int ar2313_restart(struct net_device
*dev
)
444 /* disable interrupts */
445 disable_irq(dev
->irq
);
453 /* enable interrupts */
454 enable_irq(dev
->irq
);
459 static struct platform_driver ar2313_driver
= {
460 .driver
.name
= "ar531x-eth",
461 .probe
= ar2313_probe
,
462 .remove
= ar2313_remove
,
465 int __init
ar2313_module_init(void)
467 return platform_driver_register(&ar2313_driver
);
470 void __exit
ar2313_module_cleanup(void)
472 platform_driver_unregister(&ar2313_driver
);
475 module_init(ar2313_module_init
);
476 module_exit(ar2313_module_cleanup
);
479 static void ar2313_free_descriptors(struct net_device
*dev
)
481 struct ar2313_private
*sp
= dev
->priv
;
482 if (sp
->rx_ring
!= NULL
) {
483 kfree((void *) KSEG0ADDR(sp
->rx_ring
));
490 static int ar2313_allocate_descriptors(struct net_device
*dev
)
492 struct ar2313_private
*sp
= dev
->priv
;
495 ar2313_descr_t
*space
;
497 if (sp
->rx_ring
!= NULL
) {
498 printk("%s: already done.\n", __FUNCTION__
);
503 (sizeof(ar2313_descr_t
) * (AR2313_DESCR_ENTRIES
* AR2313_QUEUES
));
504 space
= kmalloc(size
, GFP_KERNEL
);
508 /* invalidate caches */
509 dma_cache_inv((unsigned int) space
, size
);
511 /* now convert pointer to KSEG1 */
512 space
= (ar2313_descr_t
*) KSEG1ADDR(space
);
514 memset((void *) space
, 0, size
);
517 space
+= AR2313_DESCR_ENTRIES
;
520 space
+= AR2313_DESCR_ENTRIES
;
522 /* Initialize the transmit Descriptors */
523 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
524 ar2313_descr_t
*td
= &sp
->tx_ring
[j
];
526 td
->devcs
= DMA_TX1_CHAINED
;
530 tx_ring
[(j
+ 1) & (AR2313_DESCR_ENTRIES
- 1)]);
538 * Generic cleanup handling data allocated during init. Used when the
539 * module is unloaded or if an error occurs during initialization
541 static void ar2313_init_cleanup(struct net_device
*dev
)
543 struct ar2313_private
*sp
= dev
->priv
;
547 ar2313_free_descriptors(dev
);
550 iounmap((void *) sp
->eth_regs
);
552 iounmap((void *) sp
->dma_regs
);
555 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
558 sp
->rx_skb
[j
] = NULL
;
567 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
570 sp
->tx_skb
[j
] = NULL
;
579 static int ar2313_setup_timer(struct net_device
*dev
)
581 struct ar2313_private
*sp
= dev
->priv
;
583 init_timer(&sp
->link_timer
);
585 sp
->link_timer
.function
= ar2313_link_timer_fn
;
586 sp
->link_timer
.data
= (int) dev
;
587 sp
->link_timer
.expires
= jiffies
+ HZ
;
589 add_timer(&sp
->link_timer
);
594 static void ar2313_link_timer_fn(unsigned long data
)
596 struct net_device
*dev
= (struct net_device
*) data
;
597 struct ar2313_private
*sp
= dev
->priv
;
599 // see if the link status changed
600 // This was needed to make sure we set the PHY to the
601 // autonegotiated value of half or full duplex.
602 ar2313_check_link(dev
);
604 // Loop faster when we don't have link.
605 // This was needed to speed up the AP bootstrap time.
607 mod_timer(&sp
->link_timer
, jiffies
+ HZ
/ 2);
609 mod_timer(&sp
->link_timer
, jiffies
+ LINK_TIMER
);
613 static void ar2313_check_link(struct net_device
*dev
)
615 struct ar2313_private
*sp
= dev
->priv
;
618 phyData
= mdiobus_read(&sp
->mii_bus
, sp
->phy
, MII_BMSR
);
619 if (sp
->phyData
!= phyData
) {
620 if (phyData
& BMSR_LSTATUS
) {
621 /* link is present, ready link partner ability to deterine
627 reg
= mdiobus_read(&sp
->mii_bus
, sp
->phy
, MII_BMCR
);
628 if (reg
& BMCR_ANENABLE
) {
629 /* auto neg enabled */
630 reg
= mdiobus_read(&sp
->mii_bus
, sp
->phy
, MII_LPA
);
631 duplex
= (reg
& (LPA_100FULL
| LPA_10FULL
)) ? 1 : 0;
633 /* no auto neg, just read duplex config */
634 duplex
= (reg
& BMCR_FULLDPLX
) ? 1 : 0;
637 printk(KERN_INFO
"%s: Configuring MAC for %s duplex\n",
638 dev
->name
, (duplex
) ? "full" : "half");
642 sp
->eth_regs
->mac_control
=
644 mac_control
| MAC_CONTROL_F
) & ~MAC_CONTROL_DRO
);
647 sp
->eth_regs
->mac_control
=
649 mac_control
| MAC_CONTROL_DRO
) & ~MAC_CONTROL_F
);
655 sp
->phyData
= phyData
;
659 static int ar2313_reset_reg(struct net_device
*dev
)
661 struct ar2313_private
*sp
= (struct ar2313_private
*) dev
->priv
;
662 unsigned int ethsal
, ethsah
;
665 *sp
->int_regs
|= sp
->cfg
->reset_mac
;
667 *sp
->int_regs
&= ~sp
->cfg
->reset_mac
;
669 *sp
->int_regs
|= sp
->cfg
->reset_phy
;
671 *sp
->int_regs
&= ~sp
->cfg
->reset_phy
;
674 sp
->dma_regs
->bus_mode
= (DMA_BUS_MODE_SWR
);
676 sp
->dma_regs
->bus_mode
=
677 ((32 << DMA_BUS_MODE_PBL_SHIFT
) | DMA_BUS_MODE_BLE
);
679 /* enable interrupts */
680 sp
->dma_regs
->intr_ena
= (DMA_STATUS_AIS
|
683 DMA_STATUS_TI
| DMA_STATUS_FBE
);
684 sp
->dma_regs
->xmt_base
= virt_to_phys(sp
->tx_ring
);
685 sp
->dma_regs
->rcv_base
= virt_to_phys(sp
->rx_ring
);
686 sp
->dma_regs
->control
=
687 (DMA_CONTROL_SR
| DMA_CONTROL_ST
| DMA_CONTROL_SF
);
689 sp
->eth_regs
->flow_control
= (FLOW_CONTROL_FCE
);
690 sp
->eth_regs
->vlan_tag
= (0x8100);
692 /* Enable Ethernet Interface */
693 flags
= (MAC_CONTROL_TE
| /* transmit enable */
694 MAC_CONTROL_PM
| /* pass mcast */
695 MAC_CONTROL_F
| /* full duplex */
696 MAC_CONTROL_HBD
); /* heart beat disabled */
698 if (dev
->flags
& IFF_PROMISC
) { /* set promiscuous mode */
699 flags
|= MAC_CONTROL_PR
;
701 sp
->eth_regs
->mac_control
= flags
;
703 /* Set all Ethernet station address registers to their initial values */
704 ethsah
= ((((u_int
) (dev
->dev_addr
[5]) << 8) & (u_int
) 0x0000FF00) |
705 (((u_int
) (dev
->dev_addr
[4]) << 0) & (u_int
) 0x000000FF));
707 ethsal
= ((((u_int
) (dev
->dev_addr
[3]) << 24) & (u_int
) 0xFF000000) |
708 (((u_int
) (dev
->dev_addr
[2]) << 16) & (u_int
) 0x00FF0000) |
709 (((u_int
) (dev
->dev_addr
[1]) << 8) & (u_int
) 0x0000FF00) |
710 (((u_int
) (dev
->dev_addr
[0]) << 0) & (u_int
) 0x000000FF));
712 sp
->eth_regs
->mac_addr
[0] = ethsah
;
713 sp
->eth_regs
->mac_addr
[1] = ethsal
;
721 static int ar2313_init(struct net_device
*dev
)
723 struct ar2313_private
*sp
= dev
->priv
;
727 * Allocate descriptors
729 if (ar2313_allocate_descriptors(dev
)) {
730 printk("%s: %s: ar2313_allocate_descriptors failed\n",
731 dev
->name
, __FUNCTION__
);
737 * Get the memory for the skb rings.
739 if (sp
->rx_skb
== NULL
) {
741 kmalloc(sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
,
744 printk("%s: %s: rx_skb kmalloc failed\n",
745 dev
->name
, __FUNCTION__
);
750 memset(sp
->rx_skb
, 0, sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
);
752 if (sp
->tx_skb
== NULL
) {
754 kmalloc(sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
,
757 printk("%s: %s: tx_skb kmalloc failed\n",
758 dev
->name
, __FUNCTION__
);
763 memset(sp
->tx_skb
, 0, sizeof(struct sk_buff
*) * AR2313_DESCR_ENTRIES
);
766 * Set tx_csm before we start receiving interrupts, otherwise
767 * the interrupt handler might think it is supposed to process
768 * tx ints before we are up and running, which may cause a null
769 * pointer access in the int handler.
777 * Zero the stats before starting the interface
779 memset(&sp
->stats
, 0, sizeof(sp
->stats
));
782 * We load the ring here as there seem to be no way to tell the
783 * firmware to wipe the ring without re-initializing it.
785 ar2313_load_rx_ring(dev
, RX_RING_SIZE
);
790 ar2313_reset_reg(dev
);
796 request_irq(dev
->irq
, &ar2313_interrupt
,
797 IRQF_SHARED
| IRQF_DISABLED
| IRQF_SAMPLE_RANDOM
,
800 printk(KERN_WARNING
"%s: %s: Requested IRQ %d is busy\n",
801 dev
->name
, __FUNCTION__
, dev
->irq
);
806 tasklet_enable(&sp
->rx_tasklet
);
811 ar2313_init_cleanup(dev
);
818 * Loading rings is safe without holding the spin lock since this is
819 * done only before the device is enabled, thus no interrupts are
820 * generated and by the interrupt handler/tasklet handler.
822 static void ar2313_load_rx_ring(struct net_device
*dev
, int nr_bufs
)
825 struct ar2313_private
*sp
= ((struct net_device
*) dev
)->priv
;
830 for (i
= 0; i
< nr_bufs
; i
++) {
834 if (sp
->rx_skb
[idx
]) {
836 printk(KERN_INFO
"ar2313 rx refill full\n");
840 // partha: create additional room for the second GRE fragment
841 skb
= alloc_skb(AR2313_BUFSIZE
+ 128, GFP_ATOMIC
);
843 printk("\n\n\n\n %s: No memory in system\n\n\n\n",
847 // partha: create additional room in the front for tx pkt capture
848 skb_reserve(skb
, 32);
851 * Make sure IP header starts on a fresh cache line.
854 skb_reserve(skb
, RX_OFFSET
);
855 sp
->rx_skb
[idx
] = skb
;
857 rd
= (ar2313_descr_t
*) & sp
->rx_ring
[idx
];
859 /* initialize dma descriptor */
860 rd
->devcs
= ((AR2313_BUFSIZE
<< DMA_RX1_BSIZE_SHIFT
) |
862 rd
->addr
= virt_to_phys(skb
->data
);
865 rx_ring
[(idx
+ 1) & (AR2313_DESCR_ENTRIES
- 1)]);
866 rd
->status
= DMA_RX_OWN
;
874 "Out of memory when allocating standard receive buffers\n");
883 #define AR2313_MAX_PKTS_PER_CALL 64
885 static int ar2313_rx_int(struct net_device
*dev
)
887 struct ar2313_private
*sp
= dev
->priv
;
888 struct sk_buff
*skb
, *skb_new
;
889 ar2313_descr_t
*rxdesc
;
897 /* 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
) {
914 printk("index %d\n", idx
);
915 printk("RX status %08x\n", rxdesc
->status
);
916 printk("RX devcs %08x\n", rxdesc
->devcs
);
917 printk("RX addr %08x\n", rxdesc
->addr
);
918 printk("RX descr %08x\n", rxdesc
->descr
);
921 if ((status
& (DMA_RX_ERROR
| DMA_RX_ERR_LENGTH
)) &&
922 (!(status
& DMA_RX_LONG
))) {
924 printk("%s: rx ERROR %08x\n", __FUNCTION__
, status
);
926 sp
->stats
.rx_errors
++;
927 sp
->stats
.rx_dropped
++;
929 /* add statistics counters */
930 if (status
& DMA_RX_ERR_CRC
)
931 sp
->stats
.rx_crc_errors
++;
932 if (status
& DMA_RX_ERR_COL
)
933 sp
->stats
.rx_over_errors
++;
934 if (status
& DMA_RX_ERR_LENGTH
)
935 sp
->stats
.rx_length_errors
++;
936 if (status
& DMA_RX_ERR_RUNT
)
937 sp
->stats
.rx_over_errors
++;
938 if (status
& DMA_RX_ERR_DESC
)
939 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
];
949 ((status
>> DMA_RX_LEN_SHIFT
) & 0x3fff) - CRC_LEN
);
951 sp
->stats
.rx_bytes
+= skb
->len
;
952 skb
->protocol
= eth_type_trans(skb
, dev
);
953 /* pass the packet to upper layers */
958 skb_reserve(skb_new
, RX_OFFSET
+ 32);
959 /* reset descriptor's curr_addr */
960 rxdesc
->addr
= virt_to_phys(skb_new
->data
);
962 sp
->stats
.rx_packets
++;
963 sp
->rx_skb
[idx
] = skb_new
;
965 sp
->stats
.rx_dropped
++;
969 rxdesc
->devcs
= ((AR2313_BUFSIZE
<< DMA_RX1_BSIZE_SHIFT
) |
971 rxdesc
->status
= DMA_RX_OWN
;
982 static void ar2313_tx_int(struct net_device
*dev
)
984 struct ar2313_private
*sp
= dev
->priv
;
987 ar2313_descr_t
*txdesc
;
988 unsigned int status
= 0;
992 while (idx
!= sp
->tx_prd
) {
994 txdesc
= &sp
->tx_ring
[idx
];
998 ("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
999 dev
->name
, sp
->tx_csm
, idx
, sp
->tx_prd
, txdesc
->status
,
1000 txdesc
->devcs
, txdesc
->addr
, txdesc
->descr
);
1003 if ((status
= txdesc
->status
) & DMA_TX_OWN
) {
1004 /* ar2313 dma still owns descr */
1007 /* done with this descriptor */
1008 dma_unmap_single(NULL
, txdesc
->addr
,
1009 txdesc
->devcs
& DMA_TX1_BSIZE_MASK
,
1013 if (status
& DMA_TX_ERROR
) {
1014 sp
->stats
.tx_errors
++;
1015 sp
->stats
.tx_dropped
++;
1016 if (status
& DMA_TX_ERR_UNDER
)
1017 sp
->stats
.tx_fifo_errors
++;
1018 if (status
& DMA_TX_ERR_HB
)
1019 sp
->stats
.tx_heartbeat_errors
++;
1020 if (status
& (DMA_TX_ERR_LOSS
| DMA_TX_ERR_LINK
))
1021 sp
->stats
.tx_carrier_errors
++;
1022 if (status
& (DMA_TX_ERR_LATE
|
1024 DMA_TX_ERR_JABBER
| DMA_TX_ERR_DEFER
))
1025 sp
->stats
.tx_aborted_errors
++;
1028 sp
->stats
.tx_packets
++;
1031 skb
= sp
->tx_skb
[idx
];
1032 sp
->tx_skb
[idx
] = NULL
;
1033 idx
= DSC_NEXT(idx
);
1034 sp
->stats
.tx_bytes
+= skb
->len
;
1035 dev_kfree_skb_irq(skb
);
1044 static void rx_tasklet_func(unsigned long data
)
1046 struct net_device
*dev
= (struct net_device
*) data
;
1047 struct ar2313_private
*sp
= dev
->priv
;
1049 if (sp
->unloading
) {
1053 if (ar2313_rx_int(dev
)) {
1054 tasklet_hi_schedule(&sp
->rx_tasklet
);
1056 unsigned long flags
;
1057 spin_lock_irqsave(&sp
->lock
, flags
);
1058 sp
->dma_regs
->intr_ena
|= DMA_STATUS_RI
;
1059 spin_unlock_irqrestore(&sp
->lock
, flags
);
1063 static void rx_schedule(struct net_device
*dev
)
1065 struct ar2313_private
*sp
= dev
->priv
;
1067 sp
->dma_regs
->intr_ena
&= ~DMA_STATUS_RI
;
1069 tasklet_hi_schedule(&sp
->rx_tasklet
);
1072 static irqreturn_t
ar2313_interrupt(int irq
, void *dev_id
)
1074 struct net_device
*dev
= (struct net_device
*) dev_id
;
1075 struct ar2313_private
*sp
= dev
->priv
;
1076 unsigned int status
, enabled
;
1078 /* clear interrupt */
1080 * Don't clear RI bit if currently disabled.
1082 status
= sp
->dma_regs
->status
;
1083 enabled
= sp
->dma_regs
->intr_ena
;
1084 sp
->dma_regs
->status
= status
& enabled
;
1086 if (status
& DMA_STATUS_NIS
) {
1089 * Don't schedule rx processing if interrupt
1090 * is already disabled.
1092 if (status
& enabled
& DMA_STATUS_RI
) {
1093 /* receive interrupt */
1096 if (status
& DMA_STATUS_TI
) {
1097 /* transmit interrupt */
1102 if (status
& DMA_STATUS_AIS
) {
1104 printk("%s: AIS set %08x & %x\n", __FUNCTION__
,
1105 status
, (DMA_STATUS_FBE
| DMA_STATUS_TPS
));
1107 /* abnormal status */
1108 if (status
& (DMA_STATUS_FBE
| DMA_STATUS_TPS
)) {
1109 ar2313_restart(dev
);
1116 static int ar2313_open(struct net_device
*dev
)
1118 struct ar2313_private
*sp
;
1123 netif_start_queue(dev
);
1125 sp
->eth_regs
->mac_control
|= MAC_CONTROL_RE
;
1130 static void ar2313_halt(struct net_device
*dev
)
1132 struct ar2313_private
*sp
= dev
->priv
;
1135 tasklet_disable(&sp
->rx_tasklet
);
1138 sp
->eth_regs
->mac_control
&= ~(MAC_CONTROL_RE
| /* disable Receives */
1139 MAC_CONTROL_TE
); /* disable Transmits */
1141 sp
->dma_regs
->control
= 0;
1142 sp
->dma_regs
->bus_mode
= DMA_BUS_MODE_SWR
;
1144 /* place phy and MAC in reset */
1145 *sp
->int_regs
|= (sp
->cfg
->reset_mac
| sp
->cfg
->reset_phy
);
1147 /* free buffers on tx ring */
1148 for (j
= 0; j
< AR2313_DESCR_ENTRIES
; j
++) {
1149 struct sk_buff
*skb
;
1150 ar2313_descr_t
*txdesc
;
1152 txdesc
= &sp
->tx_ring
[j
];
1155 skb
= sp
->tx_skb
[j
];
1158 sp
->tx_skb
[j
] = NULL
;
1164 * close should do nothing. Here's why. It's called when
1165 * 'ifconfig bond0 down' is run. If it calls free_irq then
1166 * the irq is gone forever ! When bond0 is made 'up' again,
1167 * the ar2313_open () does not call request_irq (). Worse,
1168 * the call to ar2313_halt() generates a WDOG reset due to
1169 * the write to 'sp->int_regs' and the box reboots.
1170 * Commenting this out is good since it allows the
1171 * system to resume when bond0 is made up again.
1173 static int ar2313_close(struct net_device
*dev
)
1177 * Disable interrupts
1179 disable_irq(dev
->irq
);
1182 * Without (or before) releasing irq and stopping hardware, this
1183 * is an absolute non-sense, by the way. It will be reset instantly
1186 netif_stop_queue(dev
);
1188 /* stop the MAC and DMA engines */
1191 /* release the interrupt */
1192 free_irq(dev
->irq
, dev
);
1198 static int ar2313_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1200 struct ar2313_private
*sp
= dev
->priv
;
1205 td
= &sp
->tx_ring
[idx
];
1207 if (td
->status
& DMA_TX_OWN
) {
1209 printk("%s: No space left to Tx\n", __FUNCTION__
);
1211 /* free skbuf and lie to the caller that we sent it out */
1212 sp
->stats
.tx_dropped
++;
1215 /* restart transmitter in case locked */
1216 sp
->dma_regs
->xmt_poll
= 0;
1220 /* Setup the transmit descriptor. */
1221 td
->devcs
= ((skb
->len
<< DMA_TX1_BSIZE_SHIFT
) |
1222 (DMA_TX1_LS
| DMA_TX1_IC
| DMA_TX1_CHAINED
));
1223 td
->addr
= dma_map_single(NULL
, skb
->data
, skb
->len
, DMA_TO_DEVICE
);
1224 td
->status
= DMA_TX_OWN
;
1226 /* kick transmitter last */
1227 sp
->dma_regs
->xmt_poll
= 0;
1230 printk("index %d\n", idx
);
1231 printk("TX status %08x\n", td
->status
);
1232 printk("TX devcs %08x\n", td
->devcs
);
1233 printk("TX addr %08x\n", td
->addr
);
1234 printk("TX descr %08x\n", td
->descr
);
1237 sp
->tx_skb
[idx
] = skb
;
1238 idx
= DSC_NEXT(idx
);
1244 static int ar2313_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
1246 struct mii_ioctl_data
*data
= (struct mii_ioctl_data
*) &ifr
->ifr_data
;
1247 struct ar2313_private
*sp
= dev
->priv
;
1253 spin_lock_irq(&sp
->lock
);
1254 ret
= phy_ethtool_ioctl(sp
->phy_dev
, (void *) ifr
->ifr_data
);
1255 spin_unlock_irq(&sp
->lock
);
1260 (dev
->dev_addr
, ifr
->ifr_data
, sizeof(dev
->dev_addr
)))
1266 (ifr
->ifr_data
, dev
->dev_addr
, sizeof(dev
->dev_addr
)))
1273 return phy_mii_ioctl(sp
->phy_dev
, data
, cmd
);
1282 static struct net_device_stats
*ar2313_get_stats(struct net_device
*dev
)
1284 struct ar2313_private
*sp
= dev
->priv
;
1289 static void ar2313_adjust_link(struct net_device
*dev
)
1291 printk(KERN_ERR
" ar2313_adjust_link implementation missing\n");
1294 #define MII_ADDR(phy, reg) \
1295 ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
1298 mdiobus_read(struct mii_bus
*bus
, int phy_addr
, int regnum
)
1300 struct net_device
*const dev
= bus
->priv
;
1301 struct ar2313_private
*sp
= (struct ar2313_private
*) dev
->priv
;
1302 volatile ETHERNET_STRUCT
*ethernet
= sp
->phy_regs
;
1304 ethernet
->mii_addr
= MII_ADDR(phy_addr
, regnum
);
1305 while (ethernet
->mii_addr
& MII_ADDR_BUSY
);
1306 return (ethernet
->mii_data
>> MII_DATA_SHIFT
);
1310 mdiobus_write(struct mii_bus
*bus
, int phy_addr
, int regnum
,
1313 struct net_device
*const dev
= bus
->priv
;
1314 struct ar2313_private
*sp
= (struct ar2313_private
*) dev
->priv
;
1315 volatile ETHERNET_STRUCT
*ethernet
= sp
->phy_regs
;
1317 while (ethernet
->mii_addr
& MII_ADDR_BUSY
);
1318 ethernet
->mii_data
= value
<< MII_DATA_SHIFT
;
1319 ethernet
->mii_addr
= MII_ADDR(phy_addr
, regnum
) | MII_ADDR_WRITE
;
1324 static int mdiobus_reset(struct mii_bus
*bus
)
1326 struct net_device
*const dev
= bus
->priv
;
1328 ar2313_reset_reg(dev
);
1333 static int mdiobus_probe (struct net_device
*dev
)
1335 struct ar2313_private
*const sp
= (struct ar2313_private
*) dev
->priv
;
1336 struct phy_device
*phydev
= NULL
;
1339 /* find the first (lowest address) PHY on the current MAC's MII bus */
1340 for (phy_addr
= 0; phy_addr
< PHY_MAX_ADDR
; phy_addr
++)
1341 if (sp
->mii_bus
.phy_map
[phy_addr
]) {
1342 phydev
= sp
->mii_bus
.phy_map
[phy_addr
];
1343 break; /* break out with first one found */
1347 printk (KERN_ERR
"ar2313:%s: no PHY found\n", dev
->name
);
1351 /* now we are supposed to have a proper phydev, to attach to... */
1353 BUG_ON(phydev
->attached_dev
);
1355 phydev
= phy_connect(dev
, phydev
->dev
.bus_id
, &ar2313_adjust_link
, 0,
1356 PHY_INTERFACE_MODE_MII
);
1358 if (IS_ERR(phydev
)) {
1359 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
1360 return PTR_ERR(phydev
);
1363 /* mask with MAC supported features */
1364 phydev
->supported
&= (SUPPORTED_10baseT_Half
1365 | SUPPORTED_10baseT_Full
1366 | SUPPORTED_100baseT_Half
1367 | SUPPORTED_100baseT_Full
1369 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
1373 phydev
->advertising
= phydev
->supported
;
1376 //sp->old_speed = 0;
1377 //sp->old_duplex = -1;
1378 sp
->phy_dev
= phydev
;
1380 printk(KERN_INFO
"%s: attached PHY driver [%s] "
1381 "(mii_bus:phy_addr=%s)\n",
1382 dev
->name
, phydev
->drv
->name
, phydev
->dev
.bus_id
);