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/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 #define AR2313_MTU 1692
54 #define AR2313_PRIOS 1
55 #define AR2313_QUEUES (2*AR2313_PRIOS)
56 #define AR2313_DESCR_ENTRIES 64
67 #define min(a,b) (((a)<(b))?(a):(b))
70 #ifndef SMP_CACHE_BYTES
71 #define SMP_CACHE_BYTES L1_CACHE_BYTES
74 #define AR2313_MBOX_SET_BIT 0x8
76 #define BOARD_IDX_STATIC 0
77 #define BOARD_IDX_OVERFLOW -1
83 * New interrupt handler strategy:
85 * An old interrupt handler worked using the traditional method of
86 * replacing an skbuff with a new one when a packet arrives. However
87 * the rx rings do not need to contain a static number of buffer
88 * descriptors, thus it makes sense to move the memory allocation out
89 * of the main interrupt handler and do it in a bottom half handler
90 * and only allocate new buffers when the number of buffers in the
91 * ring is below a certain threshold. In order to avoid starving the
92 * NIC under heavy load it is however necessary to force allocation
93 * when hitting a minimum threshold. The strategy for alloction is as
96 * RX_LOW_BUF_THRES - allocate buffers in the bottom half
97 * RX_PANIC_LOW_THRES - we are very low on buffers, allocate
98 * the buffers in the interrupt handler
99 * RX_RING_THRES - maximum number of buffers in the rx ring
101 * One advantagous side effect of this allocation approach is that the
102 * entire rx processing can be done without holding any spin lock
103 * since the rx rings and registers are totally independent of the tx
104 * ring and its registers. This of course includes the kmalloc's of
105 * new skb's. Thus start_xmit can run in parallel with rx processing
106 * and the memory allocation on SMP systems.
108 * Note that running the skb reallocation in a bottom half opens up
109 * another can of races which needs to be handled properly. In
110 * particular it can happen that the interrupt handler tries to run
111 * the reallocation while the bottom half is either running on another
112 * CPU or was interrupted on the same CPU. To get around this the
113 * driver uses bitops to prevent the reallocation routines from being
116 * TX handling can also be done without holding any spin lock, wheee
117 * this is fun! since tx_csm is only written to by the interrupt
122 * Threshold values for RX buffer allocation - the low water marks for
123 * when to start refilling the rings are set to 75% of the ring
124 * sizes. It seems to make sense to refill the rings entirely from the
125 * intrrupt handler once it gets below the panic threshold, that way
126 * we don't risk that the refilling is moved to another CPU when the
127 * one running the interrupt handler just got the slab code hot in its
130 #define RX_RING_SIZE AR2313_DESCR_ENTRIES
131 #define RX_PANIC_THRES (RX_RING_SIZE/4)
132 #define RX_LOW_THRES ((3*RX_RING_SIZE)/4)
136 #define AR2313_BUFSIZE (AR2313_MTU + ETH_HLEN + CRC_LEN + RX_OFFSET)
139 MODULE_LICENSE("GPL");
140 MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@openwrt.org>");
141 MODULE_DESCRIPTION("AR2313 Ethernet driver");
144 #define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
147 static short armiiread(struct net_device *dev, short phy, short reg);
148 static void armiiwrite(struct net_device *dev, short phy, short reg,
151 static void ar2313_tx_timeout(struct net_device *dev);
153 static void ar2313_halt(struct net_device *dev);
154 static void rx_tasklet_func(unsigned long data);
155 static void ar2313_multicast_list(struct net_device *dev);
158 #define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
162 int __init ar2313_probe(struct platform_device *pdev)
164 struct net_device *dev;
165 struct ar2313_private *sp;
166 struct resource *res;
167 unsigned long ar_eth_base;
170 dev = alloc_etherdev(sizeof(struct ar2313_private));
174 "ar2313: Unable to allocate net_device structure!\n");
178 SET_MODULE_OWNER(dev);
179 platform_set_drvdata(pdev, dev);
183 sp->cfg = pdev->dev.platform_data;
185 sprintf(buf, "eth%d_membase", pdev->id);
186 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, buf);
191 ar_eth_base = res->start;
192 sp->phy = sp->cfg->phy;
194 sprintf(buf, "eth%d_irq", pdev->id);
195 dev->irq = platform_get_irq_byname(pdev, buf);
197 spin_lock_init(&sp->lock);
199 /* initialize func pointers */
200 dev->open = &ar2313_open;
201 dev->stop = &ar2313_close;
202 dev->hard_start_xmit = &ar2313_start_xmit;
204 dev->get_stats = &ar2313_get_stats;
205 dev->set_multicast_list = &ar2313_multicast_list;
207 dev->tx_timeout = ar2313_tx_timeout;
208 dev->watchdog_timeo = AR2313_TX_TIMEOUT;
210 dev->do_ioctl = &ar2313_ioctl;
212 // SAMEER: do we need this?
213 dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA;
215 tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long) dev);
216 tasklet_disable(&sp->rx_tasklet);
219 ioremap_nocache(virt_to_phys(ar_eth_base), sizeof(*sp->eth_regs));
221 printk("Can't remap eth registers\n");
226 * When there's only one MAC, PHY regs are typically on ENET0,
227 * even though the MAC might be on ENET1.
228 * Needto remap PHY regs separately in this case
230 if (virt_to_phys(ar_eth_base) == virt_to_phys(sp->phy_regs))
231 sp->phy_regs = sp->eth_regs;
234 ioremap_nocache(virt_to_phys(sp->cfg->phy_base),
235 sizeof(*sp->phy_regs));
237 printk("Can't remap phy registers\n");
243 ioremap_nocache(virt_to_phys(ar_eth_base + 0x1000),
244 sizeof(*sp->dma_regs));
245 dev->base_addr = (unsigned int) sp->dma_regs;
247 printk("Can't remap DMA registers\n");
251 sp->int_regs = ioremap_nocache(virt_to_phys(sp->cfg->reset_base), 4);
253 printk("Can't remap INTERRUPT registers\n");
257 strncpy(sp->name, "Atheros AR231x", sizeof(sp->name) - 1);
258 sp->name[sizeof(sp->name) - 1] = '\0';
259 memcpy(dev->dev_addr, sp->cfg->macaddr, 6);
260 sp->board_idx = BOARD_IDX_STATIC;
262 if (ar2313_init(dev)) {
264 * ar2313_init() calls ar2313_init_cleanup() on error.
270 if (register_netdev(dev)) {
271 printk("%s: register_netdev failed\n", __func__);
275 printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
277 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
278 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5], dev->irq);
280 /* start link poll timer */
281 ar2313_setup_timer(dev);
287 static void ar2313_dump_regs(struct net_device *dev)
289 unsigned int *ptr, i;
290 struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
292 ptr = (unsigned int *) sp->eth_regs;
293 for (i = 0; i < (sizeof(ETHERNET_STRUCT) / sizeof(unsigned int));
295 printk("ENET: %08x = %08x\n", (int) ptr, *ptr);
298 ptr = (unsigned int *) sp->dma_regs;
299 for (i = 0; i < (sizeof(DMA) / sizeof(unsigned int)); i++, ptr++) {
300 printk("DMA: %08x = %08x\n", (int) ptr, *ptr);
303 ptr = (unsigned int *) sp->int_regs;
304 for (i = 0; i < (sizeof(INTERRUPT) / sizeof(unsigned int)); i++, ptr++) {
305 printk("INT: %08x = %08x\n", (int) ptr, *ptr);
308 for (i = 0; i < AR2313_DESCR_ENTRIES; i++) {
309 ar2313_descr_t *td = &sp->tx_ring[i];
310 printk("Tx desc %2d: %08x %08x %08x %08x\n", i,
311 td->status, td->devcs, td->addr, td->descr);
317 static void ar2313_tx_timeout(struct net_device *dev)
319 struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
323 printk("Tx timeout\n");
325 spin_lock_irqsave(&sp->lock, flags);
327 spin_unlock_irqrestore(&sp->lock, flags);
332 static void printMcList(struct net_device *dev)
334 struct dev_mc_list *list = dev->mc_list;
337 printk("%d MC ADDR ", num);
338 for (i = 0; i < list->dmi_addrlen; i++) {
339 printk(":%02x", list->dmi_addr[i]);
348 * Set or clear the multicast filter for this adaptor.
349 * THIS IS ABSOLUTE CRAP, disabled
351 static void ar2313_multicast_list(struct net_device *dev)
354 * Always listen to broadcasts and
355 * treat IFF bits independently
357 struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
358 unsigned int recognise;
360 recognise = sp->eth_regs->mac_control;
362 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
363 recognise |= MAC_CONTROL_PR;
365 recognise &= ~MAC_CONTROL_PR;
368 if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
371 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__,
374 recognise |= MAC_CONTROL_PM; /* all multicast */
375 } else if (dev->mc_count > 0) {
378 printk("%s: mc_count %d\n", __FUNCTION__, dev->mc_count);
380 recognise |= MAC_CONTROL_PM; /* for the time being */
383 printk("%s: setting %08x to %08x\n", __FUNCTION__, (int) sp->eth_regs,
387 sp->eth_regs->mac_control = recognise;
390 static void rx_tasklet_cleanup(struct net_device *dev)
392 struct ar2313_private *sp = dev->priv;
395 * Tasklet may be scheduled. Need to get it removed from the list
396 * since we're about to free the struct.
400 tasklet_enable(&sp->rx_tasklet);
401 tasklet_kill(&sp->rx_tasklet);
404 static int __exit ar2313_remove(struct platform_device *pdev)
406 struct net_device *dev = platform_get_drvdata(pdev);
407 rx_tasklet_cleanup(dev);
408 ar2313_init_cleanup(dev);
409 unregister_netdev(dev);
416 * Restart the AR2313 ethernet controller.
418 static int ar2313_restart(struct net_device *dev)
420 /* disable interrupts */
421 disable_irq(dev->irq);
429 /* enable interrupts */
430 enable_irq(dev->irq);
435 static struct platform_driver ar2313_driver = {
436 .driver.name = "ar531x-eth",
437 .probe = ar2313_probe,
438 .remove = ar2313_remove,
441 int __init ar2313_module_init(void)
443 return platform_driver_register(&ar2313_driver);
446 void __exit ar2313_module_cleanup(void)
448 platform_driver_unregister(&ar2313_driver);
451 module_init(ar2313_module_init);
452 module_exit(ar2313_module_cleanup);
455 static void ar2313_free_descriptors(struct net_device *dev)
457 struct ar2313_private *sp = dev->priv;
458 if (sp->rx_ring != NULL) {
459 kfree((void *) KSEG0ADDR(sp->rx_ring));
466 static int ar2313_allocate_descriptors(struct net_device *dev)
468 struct ar2313_private *sp = dev->priv;
471 ar2313_descr_t *space;
473 if (sp->rx_ring != NULL) {
474 printk("%s: already done.\n", __FUNCTION__);
479 (sizeof(ar2313_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES));
480 space = kmalloc(size, GFP_KERNEL);
484 /* invalidate caches */
485 dma_cache_inv((unsigned int) space, size);
487 /* now convert pointer to KSEG1 */
488 space = (ar2313_descr_t *) KSEG1ADDR(space);
490 memset((void *) space, 0, size);
493 space += AR2313_DESCR_ENTRIES;
496 space += AR2313_DESCR_ENTRIES;
498 /* Initialize the transmit Descriptors */
499 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
500 ar2313_descr_t *td = &sp->tx_ring[j];
502 td->devcs = DMA_TX1_CHAINED;
506 tx_ring[(j + 1) & (AR2313_DESCR_ENTRIES - 1)]);
514 * Generic cleanup handling data allocated during init. Used when the
515 * module is unloaded or if an error occurs during initialization
517 static void ar2313_init_cleanup(struct net_device *dev)
519 struct ar2313_private *sp = dev->priv;
523 ar2313_free_descriptors(dev);
526 iounmap((void *) sp->eth_regs);
528 iounmap((void *) sp->dma_regs);
531 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
534 sp->rx_skb[j] = NULL;
543 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
546 sp->tx_skb[j] = NULL;
555 static int ar2313_setup_timer(struct net_device *dev)
557 struct ar2313_private *sp = dev->priv;
559 init_timer(&sp->link_timer);
561 sp->link_timer.function = ar2313_link_timer_fn;
562 sp->link_timer.data = (int) dev;
563 sp->link_timer.expires = jiffies + HZ;
565 add_timer(&sp->link_timer);
570 static void ar2313_link_timer_fn(unsigned long data)
572 struct net_device *dev = (struct net_device *) data;
573 struct ar2313_private *sp = dev->priv;
575 // see if the link status changed
576 // This was needed to make sure we set the PHY to the
577 // autonegotiated value of half or full duplex.
578 ar2313_check_link(dev);
580 // Loop faster when we don't have link.
581 // This was needed to speed up the AP bootstrap time.
583 mod_timer(&sp->link_timer, jiffies + HZ / 2);
585 mod_timer(&sp->link_timer, jiffies + LINK_TIMER);
589 static void ar2313_check_link(struct net_device *dev)
591 struct ar2313_private *sp = dev->priv;
594 phyData = armiiread(dev, sp->phy, MII_BMSR);
595 if (sp->phyData != phyData) {
596 if (phyData & BMSR_LSTATUS) {
597 /* link is present, ready link partner ability to deterine
603 reg = armiiread(dev, sp->phy, MII_BMCR);
604 if (reg & BMCR_ANENABLE) {
605 /* auto neg enabled */
606 reg = armiiread(dev, sp->phy, MII_LPA);
607 duplex = (reg & (LPA_100FULL | LPA_10FULL)) ? 1 : 0;
609 /* no auto neg, just read duplex config */
610 duplex = (reg & BMCR_FULLDPLX) ? 1 : 0;
613 printk(KERN_INFO "%s: Configuring MAC for %s duplex\n",
614 dev->name, (duplex) ? "full" : "half");
618 sp->eth_regs->mac_control =
620 mac_control | MAC_CONTROL_F) & ~MAC_CONTROL_DRO);
623 sp->eth_regs->mac_control =
625 mac_control | MAC_CONTROL_DRO) & ~MAC_CONTROL_F);
631 sp->phyData = phyData;
635 static int ar2313_reset_reg(struct net_device *dev)
637 struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
638 unsigned int ethsal, ethsah;
641 *sp->int_regs |= sp->cfg->reset_mac;
643 *sp->int_regs &= ~sp->cfg->reset_mac;
645 *sp->int_regs |= sp->cfg->reset_phy;
647 *sp->int_regs &= ~sp->cfg->reset_phy;
650 sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
652 sp->dma_regs->bus_mode =
653 ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
655 /* enable interrupts */
656 sp->dma_regs->intr_ena = (DMA_STATUS_AIS |
659 DMA_STATUS_TI | DMA_STATUS_FBE);
660 sp->dma_regs->xmt_base = virt_to_phys(sp->tx_ring);
661 sp->dma_regs->rcv_base = virt_to_phys(sp->rx_ring);
662 sp->dma_regs->control =
663 (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
665 sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
666 sp->eth_regs->vlan_tag = (0x8100);
668 /* Enable Ethernet Interface */
669 flags = (MAC_CONTROL_TE | /* transmit enable */
670 MAC_CONTROL_PM | /* pass mcast */
671 MAC_CONTROL_F | /* full duplex */
672 MAC_CONTROL_HBD); /* heart beat disabled */
674 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
675 flags |= MAC_CONTROL_PR;
677 sp->eth_regs->mac_control = flags;
679 /* Set all Ethernet station address registers to their initial values */
680 ethsah = ((((u_int) (dev->dev_addr[5]) << 8) & (u_int) 0x0000FF00) |
681 (((u_int) (dev->dev_addr[4]) << 0) & (u_int) 0x000000FF));
683 ethsal = ((((u_int) (dev->dev_addr[3]) << 24) & (u_int) 0xFF000000) |
684 (((u_int) (dev->dev_addr[2]) << 16) & (u_int) 0x00FF0000) |
685 (((u_int) (dev->dev_addr[1]) << 8) & (u_int) 0x0000FF00) |
686 (((u_int) (dev->dev_addr[0]) << 0) & (u_int) 0x000000FF));
688 sp->eth_regs->mac_addr[0] = ethsah;
689 sp->eth_regs->mac_addr[1] = ethsal;
697 static int ar2313_init(struct net_device *dev)
699 struct ar2313_private *sp = dev->priv;
703 * Allocate descriptors
705 if (ar2313_allocate_descriptors(dev)) {
706 printk("%s: %s: ar2313_allocate_descriptors failed\n",
707 dev->name, __FUNCTION__);
713 * Get the memory for the skb rings.
715 if (sp->rx_skb == NULL) {
717 kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
720 printk("%s: %s: rx_skb kmalloc failed\n",
721 dev->name, __FUNCTION__);
726 memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
728 if (sp->tx_skb == NULL) {
730 kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES,
733 printk("%s: %s: tx_skb kmalloc failed\n",
734 dev->name, __FUNCTION__);
739 memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
742 * Set tx_csm before we start receiving interrupts, otherwise
743 * the interrupt handler might think it is supposed to process
744 * tx ints before we are up and running, which may cause a null
745 * pointer access in the int handler.
753 * Zero the stats before starting the interface
755 memset(&sp->stats, 0, sizeof(sp->stats));
758 * We load the ring here as there seem to be no way to tell the
759 * firmware to wipe the ring without re-initializing it.
761 ar2313_load_rx_ring(dev, RX_RING_SIZE);
766 ar2313_reset_reg(dev);
772 request_irq(dev->irq, &ar2313_interrupt,
773 IRQF_SHARED | IRQF_DISABLED | IRQF_SAMPLE_RANDOM,
776 printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
777 dev->name, __FUNCTION__, dev->irq);
782 tasklet_enable(&sp->rx_tasklet);
787 ar2313_init_cleanup(dev);
794 * Loading rings is safe without holding the spin lock since this is
795 * done only before the device is enabled, thus no interrupts are
796 * generated and by the interrupt handler/tasklet handler.
798 static void ar2313_load_rx_ring(struct net_device *dev, int nr_bufs)
801 struct ar2313_private *sp = ((struct net_device *) dev)->priv;
806 for (i = 0; i < nr_bufs; i++) {
810 if (sp->rx_skb[idx]) {
812 printk(KERN_INFO "ar2313 rx refill full\n");
816 // partha: create additional room for the second GRE fragment
817 skb = alloc_skb(AR2313_BUFSIZE + 128, GFP_ATOMIC);
819 printk("\n\n\n\n %s: No memory in system\n\n\n\n",
823 // partha: create additional room in the front for tx pkt capture
824 skb_reserve(skb, 32);
827 * Make sure IP header starts on a fresh cache line.
830 skb_reserve(skb, RX_OFFSET);
831 sp->rx_skb[idx] = skb;
833 rd = (ar2313_descr_t *) & sp->rx_ring[idx];
835 /* initialize dma descriptor */
836 rd->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
838 rd->addr = virt_to_phys(skb->data);
841 rx_ring[(idx + 1) & (AR2313_DESCR_ENTRIES - 1)]);
842 rd->status = DMA_RX_OWN;
850 "Out of memory when allocating standard receive buffers\n");
859 #define AR2313_MAX_PKTS_PER_CALL 64
861 static int ar2313_rx_int(struct net_device *dev)
863 struct ar2313_private *sp = dev->priv;
864 struct sk_buff *skb, *skb_new;
865 ar2313_descr_t *rxdesc;
873 /* process at most the entire ring and then wait for another interrupt
877 rxdesc = &sp->rx_ring[idx];
878 status = rxdesc->status;
879 if (status & DMA_RX_OWN) {
880 /* SiByte owns descriptor or descr not yet filled in */
885 if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
890 printk("index %d\n", idx);
891 printk("RX status %08x\n", rxdesc->status);
892 printk("RX devcs %08x\n", rxdesc->devcs);
893 printk("RX addr %08x\n", rxdesc->addr);
894 printk("RX descr %08x\n", rxdesc->descr);
897 if ((status & (DMA_RX_ERROR | DMA_RX_ERR_LENGTH)) &&
898 (!(status & DMA_RX_LONG))) {
900 printk("%s: rx ERROR %08x\n", __FUNCTION__, status);
902 sp->stats.rx_errors++;
903 sp->stats.rx_dropped++;
905 /* add statistics counters */
906 if (status & DMA_RX_ERR_CRC)
907 sp->stats.rx_crc_errors++;
908 if (status & DMA_RX_ERR_COL)
909 sp->stats.rx_over_errors++;
910 if (status & DMA_RX_ERR_LENGTH)
911 sp->stats.rx_length_errors++;
912 if (status & DMA_RX_ERR_RUNT)
913 sp->stats.rx_over_errors++;
914 if (status & DMA_RX_ERR_DESC)
915 sp->stats.rx_over_errors++;
918 /* alloc new buffer. */
919 skb_new = dev_alloc_skb(AR2313_BUFSIZE + RX_OFFSET + 128);
920 if (skb_new != NULL) {
922 skb = sp->rx_skb[idx];
925 ((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
927 sp->stats.rx_bytes += skb->len;
928 skb->protocol = eth_type_trans(skb, dev);
929 /* pass the packet to upper layers */
934 skb_reserve(skb_new, RX_OFFSET + 32);
935 /* reset descriptor's curr_addr */
936 rxdesc->addr = virt_to_phys(skb_new->data);
938 sp->stats.rx_packets++;
939 sp->rx_skb[idx] = skb_new;
941 sp->stats.rx_dropped++;
945 rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
947 rxdesc->status = DMA_RX_OWN;
958 static void ar2313_tx_int(struct net_device *dev)
960 struct ar2313_private *sp = dev->priv;
963 ar2313_descr_t *txdesc;
964 unsigned int status = 0;
968 while (idx != sp->tx_prd) {
970 txdesc = &sp->tx_ring[idx];
974 ("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n",
975 dev->name, sp->tx_csm, idx, sp->tx_prd, txdesc->status,
976 txdesc->devcs, txdesc->addr, txdesc->descr);
979 if ((status = txdesc->status) & DMA_TX_OWN) {
980 /* ar2313 dma still owns descr */
983 /* done with this descriptor */
984 dma_unmap_single(NULL, txdesc->addr,
985 txdesc->devcs & DMA_TX1_BSIZE_MASK,
989 if (status & DMA_TX_ERROR) {
990 sp->stats.tx_errors++;
991 sp->stats.tx_dropped++;
992 if (status & DMA_TX_ERR_UNDER)
993 sp->stats.tx_fifo_errors++;
994 if (status & DMA_TX_ERR_HB)
995 sp->stats.tx_heartbeat_errors++;
996 if (status & (DMA_TX_ERR_LOSS | DMA_TX_ERR_LINK))
997 sp->stats.tx_carrier_errors++;
998 if (status & (DMA_TX_ERR_LATE |
1000 DMA_TX_ERR_JABBER | DMA_TX_ERR_DEFER))
1001 sp->stats.tx_aborted_errors++;
1004 sp->stats.tx_packets++;
1007 skb = sp->tx_skb[idx];
1008 sp->tx_skb[idx] = NULL;
1009 idx = DSC_NEXT(idx);
1010 sp->stats.tx_bytes += skb->len;
1011 dev_kfree_skb_irq(skb);
1020 static void rx_tasklet_func(unsigned long data)
1022 struct net_device *dev = (struct net_device *) data;
1023 struct ar2313_private *sp = dev->priv;
1025 if (sp->unloading) {
1029 if (ar2313_rx_int(dev)) {
1030 tasklet_hi_schedule(&sp->rx_tasklet);
1032 unsigned long flags;
1033 spin_lock_irqsave(&sp->lock, flags);
1034 sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1035 spin_unlock_irqrestore(&sp->lock, flags);
1039 static void rx_schedule(struct net_device *dev)
1041 struct ar2313_private *sp = dev->priv;
1043 sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1045 tasklet_hi_schedule(&sp->rx_tasklet);
1048 static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1050 struct net_device *dev = (struct net_device *) dev_id;
1051 struct ar2313_private *sp = dev->priv;
1052 unsigned int status, enabled;
1054 /* clear interrupt */
1056 * Don't clear RI bit if currently disabled.
1058 status = sp->dma_regs->status;
1059 enabled = sp->dma_regs->intr_ena;
1060 sp->dma_regs->status = status & enabled;
1062 if (status & DMA_STATUS_NIS) {
1065 * Don't schedule rx processing if interrupt
1066 * is already disabled.
1068 if (status & enabled & DMA_STATUS_RI) {
1069 /* receive interrupt */
1072 if (status & DMA_STATUS_TI) {
1073 /* transmit interrupt */
1078 if (status & DMA_STATUS_AIS) {
1080 printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1081 status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1083 /* abnormal status */
1084 if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1085 ar2313_restart(dev);
1092 static int ar2313_open(struct net_device *dev)
1094 struct ar2313_private *sp;
1099 netif_start_queue(dev);
1101 sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1106 static void ar2313_halt(struct net_device *dev)
1108 struct ar2313_private *sp = dev->priv;
1111 tasklet_disable(&sp->rx_tasklet);
1114 sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1115 MAC_CONTROL_TE); /* disable Transmits */
1117 sp->dma_regs->control = 0;
1118 sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1120 /* place phy and MAC in reset */
1121 *sp->int_regs |= (sp->cfg->reset_mac | sp->cfg->reset_phy);
1123 /* free buffers on tx ring */
1124 for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1125 struct sk_buff *skb;
1126 ar2313_descr_t *txdesc;
1128 txdesc = &sp->tx_ring[j];
1131 skb = sp->tx_skb[j];
1134 sp->tx_skb[j] = NULL;
1140 * close should do nothing. Here's why. It's called when
1141 * 'ifconfig bond0 down' is run. If it calls free_irq then
1142 * the irq is gone forever ! When bond0 is made 'up' again,
1143 * the ar2313_open () does not call request_irq (). Worse,
1144 * the call to ar2313_halt() generates a WDOG reset due to
1145 * the write to 'sp->int_regs' and the box reboots.
1146 * Commenting this out is good since it allows the
1147 * system to resume when bond0 is made up again.
1149 static int ar2313_close(struct net_device *dev)
1153 * Disable interrupts
1155 disable_irq(dev->irq);
1158 * Without (or before) releasing irq and stopping hardware, this
1159 * is an absolute non-sense, by the way. It will be reset instantly
1162 netif_stop_queue(dev);
1164 /* stop the MAC and DMA engines */
1167 /* release the interrupt */
1168 free_irq(dev->irq, dev);
1174 static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1176 struct ar2313_private *sp = dev->priv;
1181 td = &sp->tx_ring[idx];
1183 if (td->status & DMA_TX_OWN) {
1185 printk("%s: No space left to Tx\n", __FUNCTION__);
1187 /* free skbuf and lie to the caller that we sent it out */
1188 sp->stats.tx_dropped++;
1191 /* restart transmitter in case locked */
1192 sp->dma_regs->xmt_poll = 0;
1196 /* Setup the transmit descriptor. */
1197 td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) |
1198 (DMA_TX1_LS | DMA_TX1_IC | DMA_TX1_CHAINED));
1199 td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
1200 td->status = DMA_TX_OWN;
1202 /* kick transmitter last */
1203 sp->dma_regs->xmt_poll = 0;
1206 printk("index %d\n", idx);
1207 printk("TX status %08x\n", td->status);
1208 printk("TX devcs %08x\n", td->devcs);
1209 printk("TX addr %08x\n", td->addr);
1210 printk("TX descr %08x\n", td->descr);
1213 sp->tx_skb[idx] = skb;
1214 idx = DSC_NEXT(idx);
1220 static int netdev_get_ecmd(struct net_device *dev,
1221 struct ethtool_cmd *ecmd)
1223 struct ar2313_private *np = dev->priv;
1227 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1228 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1229 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
1231 ecmd->port = PORT_TP;
1232 /* only supports internal transceiver */
1233 ecmd->transceiver = XCVR_INTERNAL;
1234 /* not sure what this is for */
1235 ecmd->phy_address = 1;
1237 ecmd->advertising = ADVERTISED_MII;
1238 tmp = armiiread(dev, np->phy, MII_ADVERTISE);
1239 if (tmp & ADVERTISE_10HALF)
1240 ecmd->advertising |= ADVERTISED_10baseT_Half;
1241 if (tmp & ADVERTISE_10FULL)
1242 ecmd->advertising |= ADVERTISED_10baseT_Full;
1243 if (tmp & ADVERTISE_100HALF)
1244 ecmd->advertising |= ADVERTISED_100baseT_Half;
1245 if (tmp & ADVERTISE_100FULL)
1246 ecmd->advertising |= ADVERTISED_100baseT_Full;
1248 tmp = armiiread(dev, np->phy, MII_BMCR);
1249 if (tmp & BMCR_ANENABLE) {
1250 ecmd->advertising |= ADVERTISED_Autoneg;
1251 ecmd->autoneg = AUTONEG_ENABLE;
1253 ecmd->autoneg = AUTONEG_DISABLE;
1256 if (ecmd->autoneg == AUTONEG_ENABLE) {
1257 tmp = armiiread(dev, np->phy, MII_LPA);
1258 if (tmp & (LPA_100FULL | LPA_10FULL)) {
1259 ecmd->duplex = DUPLEX_FULL;
1261 ecmd->duplex = DUPLEX_HALF;
1263 if (tmp & (LPA_100FULL | LPA_100HALF)) {
1264 ecmd->speed = SPEED_100;
1266 ecmd->speed = SPEED_10;
1269 if (tmp & BMCR_FULLDPLX) {
1270 ecmd->duplex = DUPLEX_FULL;
1272 ecmd->duplex = DUPLEX_HALF;
1274 if (tmp & BMCR_SPEED100) {
1275 ecmd->speed = SPEED_100;
1277 ecmd->speed = SPEED_10;
1281 /* ignore maxtxpkt, maxrxpkt for now */
1286 static int netdev_set_ecmd(struct net_device *dev,
1287 struct ethtool_cmd *ecmd)
1289 struct ar2313_private *np = dev->priv;
1292 if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
1294 if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1296 if (ecmd->port != PORT_TP)
1298 if (ecmd->transceiver != XCVR_INTERNAL)
1300 if (ecmd->autoneg != AUTONEG_DISABLE
1301 && ecmd->autoneg != AUTONEG_ENABLE)
1303 /* ignore phy_address, maxtxpkt, maxrxpkt for now */
1305 /* WHEW! now lets bang some bits */
1307 tmp = armiiread(dev, np->phy, MII_BMCR);
1308 if (ecmd->autoneg == AUTONEG_ENABLE) {
1309 /* turn on autonegotiation */
1310 tmp |= BMCR_ANENABLE;
1311 printk("%s: Enabling auto-neg\n", dev->name);
1313 /* turn off auto negotiation, set speed and duplexity */
1314 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1315 if (ecmd->speed == SPEED_100)
1316 tmp |= BMCR_SPEED100;
1317 if (ecmd->duplex == DUPLEX_FULL)
1318 tmp |= BMCR_FULLDPLX;
1319 printk("%s: Hard coding %d/%s\n", dev->name,
1320 (ecmd->speed == SPEED_100) ? 100 : 10,
1321 (ecmd->duplex == DUPLEX_FULL) ? "full" : "half");
1323 armiiwrite(dev, np->phy, MII_BMCR, tmp);
1328 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1330 struct ar2313_private *np = dev->priv;
1333 if (get_user(cmd, (u32 *) useraddr))
1339 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1340 spin_lock_irq(&np->lock);
1341 netdev_get_ecmd(dev, &ecmd);
1342 spin_unlock_irq(&np->lock);
1343 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1349 struct ethtool_cmd ecmd;
1351 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1353 spin_lock_irq(&np->lock);
1354 r = netdev_set_ecmd(dev, &ecmd);
1355 spin_unlock_irq(&np->lock);
1358 /* restart autonegotiation */
1359 case ETHTOOL_NWAY_RST:{
1362 /* if autoneg is off, it's an error */
1363 tmp = armiiread(dev, np->phy, MII_BMCR);
1364 if (tmp & BMCR_ANENABLE) {
1365 tmp |= (BMCR_ANRESTART);
1366 armiiwrite(dev, np->phy, MII_BMCR, tmp);
1371 /* get link status */
1372 case ETHTOOL_GLINK:{
1373 struct ethtool_value edata = { ETHTOOL_GLINK };
1375 (armiiread(dev, np->phy, MII_BMSR) & BMSR_LSTATUS) ? 1 : 0;
1376 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1385 static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1387 struct mii_ioctl_data *data = (struct mii_ioctl_data *) &ifr->ifr_data;
1392 return netdev_ethtool_ioctl(dev, (void *) ifr->ifr_data);
1394 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1398 case SIOCGMIIREG: /* Read MII PHY register. */
1399 data->val_out = armiiread(dev, data->phy_id & 0x1f,
1400 data->reg_num & 0x1f);
1402 case SIOCSMIIREG: /* Write MII PHY register. */
1403 if (!capable(CAP_NET_ADMIN))
1405 armiiwrite(dev, data->phy_id & 0x1f,
1406 data->reg_num & 0x1f, data->val_in);
1411 (dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1417 (ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1428 static struct net_device_stats *ar2313_get_stats(struct net_device *dev)
1430 struct ar2313_private *sp = dev->priv;
1435 #define MII_ADDR(phy, reg) \
1436 ((reg << MII_ADDR_REG_SHIFT) | (phy << MII_ADDR_PHY_SHIFT))
1438 static short armiiread(struct net_device *dev, short phy, short reg)
1440 struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1441 volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1443 ethernet->mii_addr = MII_ADDR(phy, reg);
1444 while (ethernet->mii_addr & MII_ADDR_BUSY);
1445 return (ethernet->mii_data >> MII_DATA_SHIFT);
1449 armiiwrite(struct net_device *dev, short phy, short reg, short data)
1451 struct ar2313_private *sp = (struct ar2313_private *) dev->priv;
1452 volatile ETHERNET_STRUCT *ethernet = sp->phy_regs;
1454 while (ethernet->mii_addr & MII_ADDR_BUSY);
1455 ethernet->mii_data = data << MII_DATA_SHIFT;
1456 ethernet->mii_addr = MII_ADDR(phy, reg) | MII_ADDR_WRITE;