82e9e94d5c5de89667706a8050570bcecbac23e2
[openwrt.git] / target / linux / atheros-2.6 / files / drivers / net / ar2313 / ar2313.c
1 /*
2  * ar2313.c: Linux driver for the Atheros AR231z Ethernet device.
3  *
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>
7  *
8  * Thanks to Atheros for providing hardware and documentation
9  * enabling me to write this driver.
10  *
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.
15  *
16  * Additional credits:
17  *      This code is taken from John Taylor's Sibyte driver and then 
18  *      modified for the AR2313.
19  */
20
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>
33 #include <linux/mm.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>
42
43 #include <net/sock.h>
44 #include <net/ip.h>
45
46 #include <asm/system.h>
47 #include <asm/io.h>
48 #include <asm/irq.h>
49 #include <asm/byteorder.h>
50 #include <asm/uaccess.h>
51 #include <asm/bootinfo.h>
52
53 #include <ar531x_platform.h>
54
55 #undef INDEX_DEBUG
56 #define DEBUG     0
57 #define DEBUG_TX  0
58 #define DEBUG_RX  0
59 #define DEBUG_INT 0
60 #define DEBUG_MC  0
61 #define DEBUG_ERR 1
62
63 #ifndef min
64 #define min(a,b)        (((a)<(b))?(a):(b))
65 #endif
66
67 #ifndef SMP_CACHE_BYTES
68 #define SMP_CACHE_BYTES L1_CACHE_BYTES
69 #endif
70
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
75 #else
76 #define AR2313_MOD_INC_USE_COUNT        {do{} while(0);}
77 #define AR2313_MOD_DEC_USE_COUNT        {do{} while(0);}
78 #endif
79
80 #define PHYSADDR(a)             ((_ACAST32_ (a)) & 0x1fffffff)
81
82 static char ifname[5] = "bond";
83
84 module_param_string(ifname, ifname, 5, 0);
85
86 #define AR2313_MBOX_SET_BIT  0x8
87
88 #define BOARD_IDX_STATIC        0
89 #define BOARD_IDX_OVERFLOW      -1
90
91 #include "ar2313_msg.h"
92 #include "platform.h"
93 #include "dma.h"
94 #include "ar2313.h"
95
96 /*
97  * New interrupt handler strategy:
98  *
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
108  * follows:
109  *
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
114  *
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.
121  *
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
128  * reentered.
129  *
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
132  * handler.
133  */
134
135 /*
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
142  * cache.
143  */
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)
147 #define CRC_LEN                 4
148 #define RX_OFFSET               2
149
150 #define AR2313_BUFSIZE          (AR2313_MTU + ETH_HLEN + CRC_LEN + RX_OFFSET)
151
152 #ifdef MODULE
153 MODULE_AUTHOR("Sameer Dekate <sdekate@arubanetworks.com>, Imre Kaloz <kaloz@openwrt.org>, Felix Fietkau <nbd@openwrt.org>");
154 MODULE_DESCRIPTION("AR2313 Ethernet driver");
155 #endif
156
157 #if DEBUG
158 static char version[] __initdata = 
159   "ar2313.c: v0.03 2006/07/12  sdekate@arubanetworks.com\n";
160 #endif /* DEBUG */
161
162 #define virt_to_phys(x) ((u32)(x) & 0x1fffffff)
163
164 // prototypes
165 static short armiiread(short phy, short reg);
166 static void armiiwrite(short phy, short reg, short data);
167 #ifdef TX_TIMEOUT
168 static void ar2313_tx_timeout(struct net_device *dev);
169 #endif
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);
173
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;
180
181 #ifndef ERR
182 #define ERR(fmt, args...) printk("%s: " fmt, __func__, ##args)
183 #endif
184
185
186 int __init ar2313_probe(struct platform_device *pdev)
187 {
188         struct net_device *dev;
189         struct ar2313_private *sp;
190         struct ar531x_eth *cfg;
191         struct resource *res;
192         int version_disp;
193         char name[64] ;
194
195         if (probed)
196             return -ENODEV;
197         probed++;
198
199         version_disp = 0;
200         sprintf(name, "%s%%d", ifname) ;
201         dev = alloc_etherdev(sizeof(struct ar2313_private));
202
203         if (dev == NULL) {
204         printk(KERN_ERR "ar2313: Unable to allocate net_device structure!\n");
205         return -ENOMEM;
206         }
207
208         SET_MODULE_OWNER(dev);
209         platform_set_drvdata(pdev, dev);
210
211         sp = dev->priv;
212         sp->dev = dev;
213         cfg = pdev->dev.platform_data;
214
215         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "eth_membase");
216         if (!res)
217                 return -ENODEV;
218         
219         sp->link = 0;
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;
225         sp->phy = cfg->phy;
226
227         dev->irq = platform_get_irq_byname(pdev, "eth_irq");
228
229         spin_lock_init(&sp->lock);
230
231         /* initialize func pointers */
232         dev->open = &ar2313_open;
233         dev->stop = &ar2313_close;
234         dev->hard_start_xmit = &ar2313_start_xmit;
235
236         dev->get_stats = &ar2313_get_stats;
237         dev->set_multicast_list = &ar2313_multicast_list;
238 #ifdef TX_TIMEOUT
239         dev->tx_timeout = ar2313_tx_timeout;
240         dev->watchdog_timeo = AR2313_TX_TIMEOUT;
241 #endif
242         dev->do_ioctl = &ar2313_ioctl;
243
244         // SAMEER: do we need this?
245         dev->features |= NETIF_F_SG | NETIF_F_HIGHDMA;
246
247         tasklet_init(&sp->rx_tasklet, rx_tasklet_func, (unsigned long) dev);
248         tasklet_disable(&sp->rx_tasklet);
249
250         /* display version info if adapter is found */
251         if (!version_disp) {
252             /* set display flag to TRUE so that */
253             /* we only display this string ONCE */
254         version_disp = 1;
255 #if DEBUG
256         printk(version);
257 #endif /* DEBUG */
258         }
259
260 #if 0
261         request_region(PHYSADDR(ar_eth_base), ETHERNET_SIZE*ETHERNET_MACS,
262                        "AR2313ENET");
263 #endif
264
265         sp->eth_regs = ioremap_nocache(PHYSADDR(ar_eth_base), sizeof(*sp->eth_regs));
266         if (!sp->eth_regs) {
267                 printk("Can't remap eth registers\n");
268                 return(-ENXIO);
269         }
270
271         sp->dma_regs = ioremap_nocache(PHYSADDR(ar_eth_base + 0x1000), sizeof(*sp->dma_regs));
272         dev->base_addr = (unsigned int) sp->dma_regs;
273         if (!sp->dma_regs) {
274                 printk("Can't remap DMA registers\n");
275                 return(-ENXIO);
276         }
277
278         sp->int_regs = ioremap_nocache(PHYSADDR(ar_int_base), 4);
279         if (!sp->int_regs) {
280                 printk("Can't remap INTERRUPT registers\n");
281                 return(-ENXIO);
282         }
283
284         strncpy(sp->name, "Atheros AR2313", sizeof (sp->name) - 1);
285         sp->name [sizeof (sp->name) - 1] = '\0';
286
287         {
288                 /* XXX: Will have to rewrite this part later */
289                 char *configstart;
290                 unsigned char def_mac[6] = {0, 0xaa, 0xbb, 0xcc, 0xdd, 0xee};
291
292                 configstart = (char *) cfg->board_config;
293
294                 if (!configstart) {
295                         printk("no valid mac found, using defaults");
296                         memcpy(dev->dev_addr, def_mac, 6);
297                 } else {
298                         memcpy(dev->dev_addr, ((u8 *)configstart)+102, 6);
299             }
300         }
301
302         sp->board_idx = BOARD_IDX_STATIC;
303
304         if (ar2313_init(dev)) {
305             /*
306              * ar2313_init() calls ar2313_init_cleanup() on error.
307              */
308             kfree(dev);
309             return -ENODEV;
310         }
311
312         if (register_netdev(dev)){
313           printk("%s: register_netdev failed\n", __func__);
314           return -1;
315         }
316
317         printk("%s: %s: %02x:%02x:%02x:%02x:%02x:%02x, irq %d\n",
318                dev->name, sp->name, 
319                dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
320                dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5],
321                dev->irq);
322
323         /* start link poll timer */
324         ar2313_setup_timer(dev);
325
326         return 0;
327 }
328
329 #if 0
330 static void ar2313_dump_regs(struct net_device *dev)
331 {
332         unsigned int *ptr, i;
333         struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
334
335         ptr = (unsigned int *)sp->eth_regs;
336         for(i=0; i< (sizeof(ETHERNET_STRUCT)/ sizeof(unsigned int)); i++, ptr++) {
337             printk("ENET: %08x = %08x\n", (int)ptr, *ptr);
338         }
339
340         ptr = (unsigned int *)sp->dma_regs;
341         for(i=0; i< (sizeof(DMA)/ sizeof(unsigned int)); i++, ptr++) {
342             printk("DMA: %08x = %08x\n", (int)ptr, *ptr);
343         }
344
345         ptr = (unsigned int *)sp->int_regs;
346         for(i=0; i< (sizeof(INTERRUPT)/ sizeof(unsigned int)); i++, ptr++){
347             printk("INT: %08x = %08x\n", (int)ptr, *ptr);
348         }
349
350         for (i = 0; i < AR2313_DESCR_ENTRIES; i++) {
351         ar2313_descr_t *td = &sp->tx_ring[i];
352             printk("Tx desc %2d: %08x %08x %08x %08x\n", i,
353                    td->status, td->devcs, td->addr, td->descr);
354         }
355 }
356 #endif
357
358 #ifdef TX_TIMEOUT
359 static void
360 ar2313_tx_timeout(struct net_device *dev)
361 {
362         struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
363         unsigned long flags;
364         
365 #if DEBUG_TX
366         printk("Tx timeout\n");
367 #endif
368         spin_lock_irqsave(&sp->lock, flags);
369         ar2313_restart(dev);
370         spin_unlock_irqrestore(&sp->lock, flags);
371 }
372 #endif
373
374 #if DEBUG_MC
375 static void
376 printMcList(struct net_device *dev)
377 {
378         struct dev_mc_list *list = dev->mc_list;
379         int num=0, i;
380         while(list){
381                 printk("%d MC ADDR ", num);
382                 for(i=0;i<list->dmi_addrlen;i++) {
383                     printk(":%02x", list->dmi_addr[i]);
384                 }
385         list = list->next;
386                 printk("\n");
387         }
388 }
389 #endif
390
391 /*
392  * Set or clear the multicast filter for this adaptor.
393  * THIS IS ABSOLUTE CRAP, disabled
394  */
395 static void
396 ar2313_multicast_list(struct net_device *dev)
397 {   
398         /* 
399          * Always listen to broadcasts and 
400          * treat IFF bits independently 
401          */
402         struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
403         unsigned int recognise;
404
405         recognise = sp->eth_regs->mac_control;
406
407         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
408                 recognise |= MAC_CONTROL_PR;
409         } else {
410                 recognise &= ~MAC_CONTROL_PR;
411         }
412
413         if ((dev->flags & IFF_ALLMULTI) || (dev->mc_count > 15)) {
414 #if DEBUG_MC
415         printMcList(dev);
416                 printk("%s: all MULTICAST mc_count %d\n", __FUNCTION__, dev->mc_count);
417 #endif
418                 recognise |= MAC_CONTROL_PM;/* all multicast */
419         } else if (dev->mc_count > 0) {
420 #if DEBUG_MC
421         printMcList(dev);
422                 printk("%s: mc_count %d\n", __FUNCTION__, dev->mc_count);
423 #endif
424                 recognise |= MAC_CONTROL_PM; /* for the time being */
425         }
426 #if DEBUG_MC
427         printk("%s: setting %08x to %08x\n", __FUNCTION__, (int)sp->eth_regs, recognise);
428 #endif
429         
430         sp->eth_regs->mac_control = recognise;
431 }
432
433 static void rx_tasklet_cleanup(struct net_device *dev)
434 {
435         struct ar2313_private *sp = dev->priv;
436
437         /*
438          * Tasklet may be scheduled. Need to get it removed from the list
439          * since we're about to free the struct.
440          */
441
442         sp->unloading = 1;
443         tasklet_enable(&sp->rx_tasklet);
444         tasklet_kill(&sp->rx_tasklet);
445 }
446
447 static int __exit ar2313_remove(struct platform_device *pdev)
448 {
449         struct net_device *dev = platform_get_drvdata(pdev);
450         rx_tasklet_cleanup(dev);
451         ar2313_init_cleanup(dev);
452         unregister_netdev(dev);
453         kfree(dev);
454         return 0;
455 }
456
457
458 /*
459  * Restart the AR2313 ethernet controller. 
460  */
461 static int ar2313_restart(struct net_device *dev)
462 {
463         /* disable interrupts */
464         disable_irq(dev->irq);
465
466         /* stop mac */
467         ar2313_halt(dev);
468         
469         /* initialize */
470         ar2313_init(dev);
471         
472         /* enable interrupts */
473         enable_irq(dev->irq);
474         
475         return 0;
476 }
477
478 static struct platform_driver ar2313_driver = {
479         .driver.name = "ar531x-eth",
480         .probe = ar2313_probe,
481         .remove = ar2313_remove,
482 };
483
484 int __init ar2313_module_init(void)
485 {
486         return platform_driver_register(&ar2313_driver);
487 }
488
489 void __exit ar2313_module_cleanup(void)
490 {
491         platform_driver_unregister(&ar2313_driver);
492 }
493
494 module_init(ar2313_module_init);
495 module_exit(ar2313_module_cleanup);
496
497
498 static void ar2313_free_descriptors(struct net_device *dev)
499 {
500         struct ar2313_private *sp = dev->priv;
501         if (sp->rx_ring != NULL) {
502         kfree((void*)KSEG0ADDR(sp->rx_ring));
503         sp->rx_ring = NULL;
504         sp->tx_ring = NULL;
505         }
506 }
507
508
509 static int ar2313_allocate_descriptors(struct net_device *dev)
510 {
511         struct ar2313_private *sp = dev->priv;
512         int size;
513         int j;
514         ar2313_descr_t *space;
515
516         if(sp->rx_ring != NULL){
517         printk("%s: already done.\n", __FUNCTION__);
518         return 0;
519         }
520
521         size = (sizeof(ar2313_descr_t) * (AR2313_DESCR_ENTRIES * AR2313_QUEUES));
522         space = kmalloc(size, GFP_KERNEL);
523         if (space == NULL)
524             return 1;
525
526         /* invalidate caches */
527         dma_cache_inv((unsigned int)space, size);
528
529         /* now convert pointer to KSEG1 */
530         space = (ar2313_descr_t *)KSEG1ADDR(space);
531
532         memset((void *)space, 0, size);
533
534         sp->rx_ring = space;
535         space += AR2313_DESCR_ENTRIES;
536
537         sp->tx_ring = space;
538         space += AR2313_DESCR_ENTRIES;
539
540         /* Initialize the transmit Descriptors */
541         for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
542         ar2313_descr_t *td = &sp->tx_ring[j];
543         td->status = 0;
544         td->devcs  = DMA_TX1_CHAINED;
545         td->addr   = 0;
546         td->descr  = K1_TO_PHYS(&sp->tx_ring[(j+1) & (AR2313_DESCR_ENTRIES-1)]);
547         }
548
549         return 0;
550 }
551
552
553 /*
554  * Generic cleanup handling data allocated during init. Used when the
555  * module is unloaded or if an error occurs during initialization
556  */
557 static void ar2313_init_cleanup(struct net_device *dev)
558 {
559         struct ar2313_private *sp = dev->priv;
560         struct sk_buff *skb;
561         int j;
562
563         ar2313_free_descriptors(dev);
564
565         if (sp->eth_regs) iounmap((void*)sp->eth_regs);
566         if (sp->dma_regs) iounmap((void*)sp->dma_regs);
567
568         if (sp->rx_skb) {
569         for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
570             skb = sp->rx_skb[j];
571             if (skb) {
572                 sp->rx_skb[j] = NULL;
573                 dev_kfree_skb(skb);
574             }
575         }
576         kfree(sp->rx_skb);
577         sp->rx_skb = NULL;
578         }
579
580         if (sp->tx_skb) {
581         for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
582             skb = sp->tx_skb[j];
583             if (skb) {
584                 sp->tx_skb[j] = NULL;
585                 dev_kfree_skb(skb);
586             }
587         }
588         kfree(sp->tx_skb);
589         sp->tx_skb = NULL;
590         }
591 }
592
593 static int ar2313_setup_timer(struct net_device *dev)
594 {
595         struct ar2313_private *sp = dev->priv; 
596
597         init_timer(&sp->link_timer);
598
599         sp->link_timer.function = ar2313_link_timer_fn;
600         sp->link_timer.data = (int) dev;
601         sp->link_timer.expires = jiffies + HZ;
602
603         add_timer(&sp->link_timer);
604         return 0;
605
606 }
607
608 static void ar2313_link_timer_fn(unsigned long data)
609 {
610         struct net_device *dev = (struct net_device *) data;
611         struct ar2313_private *sp = dev->priv; 
612
613         // see if the link status changed
614         // This was needed to make sure we set the PHY to the
615         // autonegotiated value of half or full duplex.
616         ar2313_check_link(dev);
617         
618         // Loop faster when we don't have link. 
619         // This was needed to speed up the AP bootstrap time.
620         if(sp->link == 0) {
621                 mod_timer(&sp->link_timer, jiffies + HZ/2);
622         } else {
623                 mod_timer(&sp->link_timer, jiffies + LINK_TIMER);
624         }
625 }
626
627 static void ar2313_check_link(struct net_device *dev)
628 {
629         struct ar2313_private *sp = dev->priv;
630         u16 phyData;
631
632         phyData = armiiread(sp->phy, MII_BMSR);
633         if (sp->phyData != phyData) {
634         if (phyData & BMSR_LSTATUS) {
635                 /* link is present, ready link partner ability to deterine duplexity */
636                 int duplex = 0;
637                 u16 reg;
638
639                 sp->link = 1;
640                 reg = armiiread(sp->phy, MII_BMCR);
641                 if (reg & BMCR_ANENABLE) {
642                     /* auto neg enabled */
643                     reg = armiiread(sp->phy, MII_LPA);
644                     duplex = (reg & (LPA_100FULL|LPA_10FULL))? 1:0;
645                 } else {
646                     /* no auto neg, just read duplex config */
647                     duplex = (reg & BMCR_FULLDPLX)? 1:0;
648                 }
649
650                 printk(KERN_INFO "%s: Configuring MAC for %s duplex\n", dev->name,
651                        (duplex)? "full":"half");
652
653                 if (duplex) {
654                     /* full duplex */
655                     sp->eth_regs->mac_control = ((sp->eth_regs->mac_control | MAC_CONTROL_F) &
656                                                  ~MAC_CONTROL_DRO);
657                 } else {
658                     /* half duplex */
659                     sp->eth_regs->mac_control = ((sp->eth_regs->mac_control | MAC_CONTROL_DRO) &
660                                                  ~MAC_CONTROL_F);
661                 }
662                 } else {
663                 /* no link */
664                 sp->link = 0;
665                 }
666             sp->phyData = phyData;
667         }
668 }
669   
670 static int
671 ar2313_reset_reg(struct net_device *dev)
672 {
673         struct ar2313_private *sp = (struct ar2313_private *)dev->priv;
674         unsigned int ethsal, ethsah;
675         unsigned int flags;
676
677         *sp->int_regs |= ar_int_mac_mask;
678         mdelay(10);
679         *sp->int_regs &= ~ar_int_mac_mask;
680         mdelay(10);
681         *sp->int_regs |= ar_int_phy_mask;
682         mdelay(10);
683         *sp->int_regs &= ~ar_int_phy_mask;
684         mdelay(10);
685
686         sp->dma_regs->bus_mode = (DMA_BUS_MODE_SWR);
687         mdelay(10);
688         sp->dma_regs->bus_mode = ((32 << DMA_BUS_MODE_PBL_SHIFT) | DMA_BUS_MODE_BLE);
689
690         /* enable interrupts */
691         sp->dma_regs->intr_ena = (DMA_STATUS_AIS |
692                               DMA_STATUS_NIS |
693                               DMA_STATUS_RI  |
694                               DMA_STATUS_TI  |
695                               DMA_STATUS_FBE);
696         sp->dma_regs->xmt_base = K1_TO_PHYS(sp->tx_ring);
697         sp->dma_regs->rcv_base = K1_TO_PHYS(sp->rx_ring);
698         sp->dma_regs->control = (DMA_CONTROL_SR | DMA_CONTROL_ST | DMA_CONTROL_SF);
699         
700         sp->eth_regs->flow_control = (FLOW_CONTROL_FCE);
701         sp->eth_regs->vlan_tag = (0x8100);
702
703         /* Enable Ethernet Interface */
704         flags = (MAC_CONTROL_TE  | /* transmit enable */
705              MAC_CONTROL_PM  | /* pass mcast */
706              MAC_CONTROL_F   | /* full duplex */
707              MAC_CONTROL_HBD); /* heart beat disabled */
708
709         if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
710         flags |= MAC_CONTROL_PR;
711         }
712         sp->eth_regs->mac_control = flags;
713
714         /* Set all Ethernet station address registers to their initial values */
715         ethsah = ((((u_int)(dev->dev_addr[5]) << 8) & (u_int)0x0000FF00) |
716               (((u_int)(dev->dev_addr[4]) << 0) & (u_int)0x000000FF));
717
718         ethsal = ((((u_int)(dev->dev_addr[3]) << 24) & (u_int)0xFF000000) |
719                   (((u_int)(dev->dev_addr[2]) << 16) & (u_int)0x00FF0000) |
720                   (((u_int)(dev->dev_addr[1]) <<  8) & (u_int)0x0000FF00) |
721                   (((u_int)(dev->dev_addr[0]) <<  0) & (u_int)0x000000FF) );
722
723         sp->eth_regs->mac_addr[0] = ethsah;
724         sp->eth_regs->mac_addr[1] = ethsal;
725
726         mdelay(10);
727
728         return(0);
729 }
730
731
732 static int ar2313_init(struct net_device *dev)
733 {
734         struct ar2313_private *sp = dev->priv;
735         int ecode=0;
736
737         /*
738          * Allocate descriptors
739          */
740         if (ar2313_allocate_descriptors(dev)) {
741         printk("%s: %s: ar2313_allocate_descriptors failed\n", 
742                         dev->name, __FUNCTION__);
743         ecode = -EAGAIN;
744             goto init_error;
745         }
746
747         /*
748          * Get the memory for the skb rings.
749          */
750         if(sp->rx_skb == NULL) {
751         sp->rx_skb = kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES, GFP_KERNEL);
752         if (!(sp->rx_skb)) {
753             printk("%s: %s: rx_skb kmalloc failed\n", 
754                             dev->name, __FUNCTION__);
755             ecode = -EAGAIN;
756             goto init_error;
757         }
758         }
759         memset(sp->rx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
760
761         if(sp->tx_skb == NULL) {
762         sp->tx_skb = kmalloc(sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES, GFP_KERNEL);
763         if (!(sp->tx_skb)) {
764             printk("%s: %s: tx_skb kmalloc failed\n", 
765                             dev->name, __FUNCTION__);
766             ecode = -EAGAIN;
767             goto init_error;
768         }
769         }
770         memset(sp->tx_skb, 0, sizeof(struct sk_buff *) * AR2313_DESCR_ENTRIES);
771
772         /*
773          * Set tx_csm before we start receiving interrupts, otherwise
774          * the interrupt handler might think it is supposed to process
775          * tx ints before we are up and running, which may cause a null
776          * pointer access in the int handler.
777          */
778         sp->rx_skbprd = 0;
779         sp->cur_rx = 0;
780         sp->tx_prd = 0;
781         sp->tx_csm = 0;
782
783         /*
784          * Zero the stats before starting the interface
785          */
786         memset(&sp->stats, 0, sizeof(sp->stats));
787
788         /*
789          * We load the ring here as there seem to be no way to tell the
790          * firmware to wipe the ring without re-initializing it.
791          */
792         ar2313_load_rx_ring(dev, RX_RING_SIZE);
793
794         /* 
795          * Init hardware
796          */
797         ar2313_reset_reg(dev);
798
799         /*
800          * Get the IRQ
801          */
802         ecode = request_irq(dev->irq, &ar2313_interrupt, IRQF_SHARED | IRQF_DISABLED | IRQF_SAMPLE_RANDOM, dev->name, dev);
803         if (ecode) {
804             printk(KERN_WARNING "%s: %s: Requested IRQ %d is busy\n",
805                dev->name, __FUNCTION__, dev->irq);
806         goto init_error;
807         }
808
809
810         tasklet_enable(&sp->rx_tasklet);
811
812         return 0;
813
814  init_error:
815         ar2313_init_cleanup(dev);
816         return ecode;
817 }
818
819 /*
820  * Load the rx ring.
821  *
822  * Loading rings is safe without holding the spin lock since this is
823  * done only before the device is enabled, thus no interrupts are
824  * generated and by the interrupt handler/tasklet handler.
825  */
826 static void ar2313_load_rx_ring(struct net_device *dev, int nr_bufs)
827 {
828
829         struct ar2313_private *sp = ((struct net_device *)dev)->priv;
830         short i, idx;
831
832         idx = sp->rx_skbprd;
833
834         for (i = 0; i < nr_bufs; i++) {
835             struct sk_buff *skb;
836         ar2313_descr_t *rd;
837
838         if (sp->rx_skb[idx]) {
839 #if DEBUG_RX
840             printk(KERN_INFO "ar2313 rx refill full\n");
841 #endif /* DEBUG */
842             break;
843         }
844
845             // partha: create additional room for the second GRE fragment
846         skb = alloc_skb(AR2313_BUFSIZE+128, GFP_ATOMIC);
847         if (!skb) {
848             printk("\n\n\n\n %s: No memory in system\n\n\n\n", __FUNCTION__);
849             break;
850         }
851             // partha: create additional room in the front for tx pkt capture
852             skb_reserve(skb, 32);
853
854         /*
855          * Make sure IP header starts on a fresh cache line.
856          */
857         skb->dev = dev;
858         skb_reserve(skb, RX_OFFSET);
859         sp->rx_skb[idx] = skb;
860
861         rd = (ar2313_descr_t *) &sp->rx_ring[idx];
862
863         /* initialize dma descriptor */
864         rd->devcs  = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) |
865                           DMA_RX1_CHAINED);
866         rd->addr   = virt_to_phys(skb->data);
867         rd->descr  = virt_to_phys(&sp->rx_ring[(idx+1) & (AR2313_DESCR_ENTRIES-1)]);
868         rd->status = DMA_RX_OWN;
869
870         idx = DSC_NEXT(idx);
871         }
872
873         if (!i) {
874 #if DEBUG_ERR
875             printk(KERN_INFO "Out of memory when allocating standard receive buffers\n");
876 #endif /* DEBUG */
877         } else {
878         sp->rx_skbprd = idx;
879         }
880
881         return;
882 }
883
884 #define AR2313_MAX_PKTS_PER_CALL        64
885
886 static int ar2313_rx_int(struct net_device *dev)
887 {
888         struct ar2313_private *sp = dev->priv;
889         struct sk_buff *skb, *skb_new;
890         ar2313_descr_t *rxdesc;
891         unsigned int status;
892         u32 idx;
893         int pkts = 0;
894         int rval;
895
896         idx = sp->cur_rx;
897
898         /* process at most the entire ring and then wait for another interrupt */
899         while(1) {
900
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 */
905                 rval = 0;
906             break;
907         }
908
909             if (++pkts > AR2313_MAX_PKTS_PER_CALL) {
910                 rval = 1;
911                 break;
912             }
913
914 #if DEBUG_RX
915         printk("index %d\n", idx);
916         printk("RX status %08x\n", rxdesc->status);
917         printk("RX devcs  %08x\n", rxdesc->devcs );
918         printk("RX addr   %08x\n", rxdesc->addr  );
919         printk("RX descr  %08x\n", rxdesc->descr );
920 #endif
921
922             if ((status & (DMA_RX_ERROR|DMA_RX_ERR_LENGTH)) &&
923             (!(status & DMA_RX_LONG))){
924 #if DEBUG_RX
925             printk("%s: rx ERROR %08x\n", __FUNCTION__, status);
926 #endif
927             sp->stats.rx_errors++;
928             sp->stats.rx_dropped++;
929
930             /* add statistics counters */
931             if (status & DMA_RX_ERR_CRC)    sp->stats.rx_crc_errors++;
932             if (status & DMA_RX_ERR_COL)    sp->stats.rx_over_errors++;
933             if (status & DMA_RX_ERR_LENGTH)
934                     sp->stats.rx_length_errors++;
935             if (status & DMA_RX_ERR_RUNT)   sp->stats.rx_over_errors++;
936             if (status & DMA_RX_ERR_DESC)   sp->stats.rx_over_errors++;
937
938         } else {
939             /* alloc new buffer. */
940             skb_new = dev_alloc_skb(AR2313_BUFSIZE + RX_OFFSET + 128);
941             if (skb_new != NULL) {
942
943                     skb = sp->rx_skb[idx];
944                 /* set skb */
945                     skb_put(skb, ((status >> DMA_RX_LEN_SHIFT) & 0x3fff) - CRC_LEN);
946
947 #ifdef CONFIG_MERLOT
948                 if ((dev->am_pkt_handler == NULL) || 
949                         (dev->am_pkt_handler(skb, dev) == 0)) {
950 #endif
951                       sp->stats.rx_bytes += skb->len;
952                       skb->protocol = eth_type_trans(skb, dev);
953                       /* pass the packet to upper layers */
954
955 #ifdef CONFIG_MERLOT
956                       if (dev->asap_netif_rx)
957                           dev->asap_netif_rx(skb);
958                       else
959 #endif
960                       netif_rx(skb);
961 #ifdef CONFIG_MERLOT
962                 }
963 #endif
964                 skb_new->dev = dev;
965                 /* 16 bit align */
966                 skb_reserve(skb_new, RX_OFFSET+32);
967                 /* reset descriptor's curr_addr */
968                 rxdesc->addr = virt_to_phys(skb_new->data); 
969
970                 sp->stats.rx_packets++;
971                 sp->rx_skb[idx] = skb_new;
972
973             } else {
974                 sp->stats.rx_dropped++;
975             }
976         }
977
978         rxdesc->devcs = ((AR2313_BUFSIZE << DMA_RX1_BSIZE_SHIFT) | 
979                              DMA_RX1_CHAINED);
980         rxdesc->status = DMA_RX_OWN;
981
982         idx = DSC_NEXT(idx);
983         }
984
985         sp->cur_rx = idx;
986
987         return rval;
988 }
989
990
991 static void ar2313_tx_int(struct net_device *dev)
992 {
993         struct ar2313_private *sp = dev->priv;
994         u32 idx;
995         struct sk_buff *skb;
996         ar2313_descr_t *txdesc;
997         unsigned int status=0;
998
999         idx = sp->tx_csm;
1000
1001         while (idx != sp->tx_prd) {
1002
1003         txdesc = &sp->tx_ring[idx];
1004
1005 #if DEBUG_TX
1006         printk("%s: TXINT: csm=%d idx=%d prd=%d status=%x devcs=%x addr=%08x descr=%x\n", 
1007                 dev->name, sp->tx_csm, idx, sp->tx_prd,
1008                 txdesc->status, txdesc->devcs, txdesc->addr, txdesc->descr);
1009 #endif /* DEBUG */
1010
1011         if ((status = txdesc->status) & DMA_TX_OWN) {
1012             /* ar2313 dma still owns descr */
1013             break;
1014         }
1015         /* done with this descriptor */
1016         dma_unmap_single(NULL, txdesc->addr, txdesc->devcs & DMA_TX1_BSIZE_MASK, DMA_TO_DEVICE);
1017         txdesc->status = 0;
1018
1019         if (status & DMA_TX_ERROR){
1020                 sp->stats.tx_errors++;
1021                 sp->stats.tx_dropped++;
1022                 if(status & DMA_TX_ERR_UNDER)
1023                         sp->stats.tx_fifo_errors++;
1024                 if(status & DMA_TX_ERR_HB)
1025                         sp->stats.tx_heartbeat_errors++;
1026                 if(status & (DMA_TX_ERR_LOSS |
1027                                  DMA_TX_ERR_LINK))
1028                         sp->stats.tx_carrier_errors++;
1029                     if (status & (DMA_TX_ERR_LATE|
1030                                   DMA_TX_ERR_COL |
1031                                   DMA_TX_ERR_JABBER |
1032                                   DMA_TX_ERR_DEFER))
1033                         sp->stats.tx_aborted_errors++;
1034         } else {
1035                 /* transmit OK */
1036                 sp->stats.tx_packets++;
1037         }
1038
1039         skb = sp->tx_skb[idx];
1040         sp->tx_skb[idx] = NULL;
1041         idx = DSC_NEXT(idx);
1042         sp->stats.tx_bytes += skb->len;
1043         dev_kfree_skb_irq(skb);
1044         }
1045
1046         sp->tx_csm = idx;
1047
1048         return;
1049 }
1050
1051
1052 static void
1053 rx_tasklet_func(unsigned long data)
1054 {
1055         struct net_device *dev = (struct net_device *) data;
1056         struct ar2313_private *sp = dev->priv;
1057
1058         if (sp->unloading) {
1059             return;
1060         }
1061
1062             if (ar2313_rx_int(dev)) {
1063                 tasklet_hi_schedule(&sp->rx_tasklet);
1064             }
1065             else {
1066                 unsigned long flags;
1067                 spin_lock_irqsave(&sp->lock, flags);
1068                 sp->dma_regs->intr_ena |= DMA_STATUS_RI;
1069                 spin_unlock_irqrestore(&sp->lock, flags);
1070             }
1071 }
1072
1073 static void
1074 rx_schedule(struct net_device *dev)
1075 {
1076         struct ar2313_private *sp = dev->priv;
1077
1078         sp->dma_regs->intr_ena &= ~DMA_STATUS_RI;
1079
1080         tasklet_hi_schedule(&sp->rx_tasklet);
1081 }
1082
1083 static irqreturn_t ar2313_interrupt(int irq, void *dev_id)
1084 {
1085         struct net_device *dev = (struct net_device *)dev_id;
1086         struct ar2313_private *sp = dev->priv;
1087         unsigned int status, enabled;
1088
1089         /* clear interrupt */
1090         /*
1091          * Don't clear RI bit if currently disabled.
1092          */
1093         status = sp->dma_regs->status;
1094         enabled = sp->dma_regs->intr_ena;
1095         sp->dma_regs->status = status & enabled;
1096
1097         if (status & DMA_STATUS_NIS) {
1098         /* normal status */
1099             /*
1100              * Don't schedule rx processing if interrupt
1101              * is already disabled.
1102              */
1103         if (status & enabled & DMA_STATUS_RI) {
1104             /* receive interrupt */
1105             rx_schedule(dev);
1106         }
1107         if (status & DMA_STATUS_TI) {
1108            /* transmit interrupt */
1109             ar2313_tx_int(dev);
1110         }
1111         }
1112
1113         if (status & DMA_STATUS_AIS) {
1114 #if DEBUG_INT
1115             printk("%s: AIS set %08x & %x\n", __FUNCTION__,
1116                    status, (DMA_STATUS_FBE | DMA_STATUS_TPS));
1117 #endif
1118         /* abnormal status */
1119         if (status & (DMA_STATUS_FBE | DMA_STATUS_TPS)) {
1120             ar2313_restart(dev);
1121         }
1122         }
1123         return IRQ_HANDLED;
1124 }
1125
1126
1127 static int ar2313_open(struct net_device *dev)
1128 {
1129         struct ar2313_private *sp;
1130
1131         sp = dev->priv;
1132
1133         dev->mtu = 1500;
1134         netif_start_queue(dev);
1135
1136         sp->eth_regs->mac_control |= MAC_CONTROL_RE;
1137
1138         AR2313_MOD_INC_USE_COUNT;
1139
1140         return 0;
1141 }
1142
1143 static void ar2313_halt(struct net_device *dev)
1144 {
1145         struct ar2313_private *sp = dev->priv;
1146         int j;
1147
1148         tasklet_disable(&sp->rx_tasklet);
1149
1150         /* kill the MAC */
1151         sp->eth_regs->mac_control &= ~(MAC_CONTROL_RE | /* disable Receives */
1152                                        MAC_CONTROL_TE); /* disable Transmits */
1153         /* stop dma */
1154         sp->dma_regs->control = 0;
1155         sp->dma_regs->bus_mode = DMA_BUS_MODE_SWR;
1156
1157         /* place phy and MAC in reset */
1158         *sp->int_regs |= (ar_int_mac_mask | ar_int_phy_mask);
1159
1160         /* free buffers on tx ring */
1161         for (j = 0; j < AR2313_DESCR_ENTRIES; j++) {
1162         struct sk_buff *skb;
1163         ar2313_descr_t *txdesc;
1164
1165         txdesc = &sp->tx_ring[j];
1166         txdesc->descr = 0;
1167
1168         skb = sp->tx_skb[j];
1169         if (skb) {
1170             dev_kfree_skb(skb);
1171             sp->tx_skb[j] = NULL;
1172         }
1173         }
1174 }
1175
1176 /*
1177  * close should do nothing. Here's why. It's called when
1178  * 'ifconfig bond0 down' is run. If it calls free_irq then
1179  * the irq is gone forever ! When bond0 is made 'up' again,
1180  * the ar2313_open () does not call request_irq (). Worse,
1181  * the call to ar2313_halt() generates a WDOG reset due to
1182  * the write to 'sp->int_regs' and the box reboots.
1183  * Commenting this out is good since it allows the
1184  * system to resume when bond0 is made up again.
1185  */
1186 static int ar2313_close(struct net_device *dev)
1187 {
1188 #if 0
1189         /*
1190          * Disable interrupts
1191          */
1192         disable_irq(dev->irq);
1193                 
1194         /*
1195          * Without (or before) releasing irq and stopping hardware, this
1196          * is an absolute non-sense, by the way. It will be reset instantly
1197          * by the first irq.
1198          */
1199         netif_stop_queue(dev);
1200
1201         /* stop the MAC and DMA engines */
1202         ar2313_halt(dev);
1203
1204         /* release the interrupt */
1205         free_irq(dev->irq, dev);
1206
1207 #endif
1208         AR2313_MOD_DEC_USE_COUNT;
1209         return 0;
1210 }
1211
1212 static int ar2313_start_xmit(struct sk_buff *skb, struct net_device *dev)
1213 {
1214         struct ar2313_private *sp = dev->priv;
1215         ar2313_descr_t *td;
1216         u32 idx;
1217
1218         idx = sp->tx_prd;
1219         td = &sp->tx_ring[idx];
1220
1221         if (td->status & DMA_TX_OWN) {
1222 #if DEBUG_TX
1223         printk("%s: No space left to Tx\n", __FUNCTION__);
1224 #endif
1225             /* free skbuf and lie to the caller that we sent it out */
1226             sp->stats.tx_dropped++;
1227         dev_kfree_skb(skb);
1228
1229             /* restart transmitter in case locked */
1230             sp->dma_regs->xmt_poll = 0;
1231         return 0;
1232         }
1233
1234         /* Setup the transmit descriptor. */
1235         td->devcs = ((skb->len << DMA_TX1_BSIZE_SHIFT) | 
1236                      (DMA_TX1_LS|DMA_TX1_IC|DMA_TX1_CHAINED));
1237         td->addr = dma_map_single(NULL, skb->data, skb->len, DMA_TO_DEVICE);
1238         td->status = DMA_TX_OWN;
1239
1240         /* kick transmitter last */
1241         sp->dma_regs->xmt_poll = 0;
1242
1243 #if DEBUG_TX
1244         printk("index %d\n", idx);
1245         printk("TX status %08x\n", td->status);
1246         printk("TX devcs  %08x\n", td->devcs );
1247         printk("TX addr   %08x\n", td->addr  );
1248         printk("TX descr  %08x\n", td->descr );
1249 #endif
1250
1251         sp->tx_skb[idx] = skb;
1252         idx = DSC_NEXT(idx);
1253         sp->tx_prd = idx;
1254
1255         //dev->trans_start = jiffies;
1256
1257         return 0;
1258 }
1259
1260 static int netdev_get_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1261 {
1262         struct ar2313_private *np = dev->priv;
1263         u32 tmp;
1264
1265         ecmd->supported = 
1266                 (SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full |
1267                 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full |
1268                 SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII);
1269         
1270             ecmd->port = PORT_TP;
1271         /* only supports internal transceiver */
1272         ecmd->transceiver = XCVR_INTERNAL;
1273         /* not sure what this is for */
1274         ecmd->phy_address = 1;
1275
1276         ecmd->advertising = ADVERTISED_MII;
1277         tmp = armiiread(np->phy, MII_ADVERTISE);
1278         if (tmp & ADVERTISE_10HALF)
1279                 ecmd->advertising |= ADVERTISED_10baseT_Half;
1280         if (tmp & ADVERTISE_10FULL)
1281                 ecmd->advertising |= ADVERTISED_10baseT_Full;
1282         if (tmp & ADVERTISE_100HALF)
1283                 ecmd->advertising |= ADVERTISED_100baseT_Half;
1284         if (tmp & ADVERTISE_100FULL)
1285                 ecmd->advertising |= ADVERTISED_100baseT_Full;
1286
1287         tmp = armiiread(np->phy, MII_BMCR);
1288         if (tmp & BMCR_ANENABLE) {
1289                 ecmd->advertising |= ADVERTISED_Autoneg;
1290                 ecmd->autoneg = AUTONEG_ENABLE;
1291         } else {
1292                 ecmd->autoneg = AUTONEG_DISABLE;
1293         }
1294
1295             if (ecmd->autoneg == AUTONEG_ENABLE) {
1296                 tmp = armiiread(np->phy, MII_LPA);
1297                 if (tmp & (LPA_100FULL|LPA_10FULL)) {
1298                     ecmd->duplex = DUPLEX_FULL;
1299                 } else {
1300                     ecmd->duplex = DUPLEX_HALF;
1301                 }
1302                 if (tmp & (LPA_100FULL|LPA_100HALF)) {
1303                 ecmd->speed = SPEED_100;
1304                 } else {
1305                 ecmd->speed = SPEED_10;
1306                 }
1307             } else {
1308                 if (tmp & BMCR_FULLDPLX) {
1309                     ecmd->duplex = DUPLEX_FULL;
1310                 } else {
1311                     ecmd->duplex = DUPLEX_HALF;
1312                 }
1313                 if (tmp & BMCR_SPEED100) {
1314                 ecmd->speed = SPEED_100;
1315                 } else {
1316                 ecmd->speed = SPEED_10;
1317                 }
1318         }
1319
1320         /* ignore maxtxpkt, maxrxpkt for now */
1321
1322         return 0;
1323 }
1324
1325 static int netdev_set_ecmd(struct net_device *dev, struct ethtool_cmd *ecmd)
1326 {
1327         struct ar2313_private *np = dev->priv;
1328         u32 tmp;
1329
1330         if (ecmd->speed != SPEED_10 && ecmd->speed != SPEED_100)
1331                 return -EINVAL;
1332         if (ecmd->duplex != DUPLEX_HALF && ecmd->duplex != DUPLEX_FULL)
1333                 return -EINVAL;
1334         if (ecmd->port != PORT_TP)
1335                 return -EINVAL;
1336         if (ecmd->transceiver != XCVR_INTERNAL)
1337                 return -EINVAL;
1338         if (ecmd->autoneg != AUTONEG_DISABLE && ecmd->autoneg != AUTONEG_ENABLE)
1339                 return -EINVAL;
1340         /* ignore phy_address, maxtxpkt, maxrxpkt for now */
1341         
1342         /* WHEW! now lets bang some bits */
1343         
1344         tmp = armiiread(np->phy, MII_BMCR);
1345         if (ecmd->autoneg == AUTONEG_ENABLE) {
1346                 /* turn on autonegotiation */
1347                 tmp |= BMCR_ANENABLE;
1348                     printk("%s: Enabling auto-neg\n", dev->name);
1349         } else {
1350                 /* turn off auto negotiation, set speed and duplexity */
1351                 tmp &= ~(BMCR_ANENABLE | BMCR_SPEED100 | BMCR_FULLDPLX);
1352                 if (ecmd->speed == SPEED_100)
1353                         tmp |= BMCR_SPEED100;
1354                 if (ecmd->duplex == DUPLEX_FULL)
1355                         tmp |= BMCR_FULLDPLX;
1356                     printk("%s: Hard coding %d/%s\n", dev->name, 
1357                            (ecmd->speed == SPEED_100)? 100:10,
1358                            (ecmd->duplex == DUPLEX_FULL)? "full":"half");
1359         }
1360         armiiwrite(np->phy, MII_BMCR, tmp);
1361             np->phyData = 0;
1362         return 0;
1363 }
1364
1365 static int netdev_ethtool_ioctl(struct net_device *dev, void *useraddr)
1366 {
1367         struct ar2313_private *np = dev->priv;
1368         u32 cmd;
1369         
1370         if (get_user(cmd, (u32 *)useraddr))
1371                 return -EFAULT;
1372
1373             switch (cmd) {
1374         /* get settings */
1375         case ETHTOOL_GSET: {
1376                 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1377                 spin_lock_irq(&np->lock);
1378                 netdev_get_ecmd(dev, &ecmd);
1379                 spin_unlock_irq(&np->lock);
1380                 if (copy_to_user(useraddr, &ecmd, sizeof(ecmd)))
1381                         return -EFAULT;
1382                 return 0;
1383         }
1384         /* set settings */
1385         case ETHTOOL_SSET: {
1386                 struct ethtool_cmd ecmd;
1387                 int r;
1388                 if (copy_from_user(&ecmd, useraddr, sizeof(ecmd)))
1389                         return -EFAULT;
1390                 spin_lock_irq(&np->lock);
1391                 r = netdev_set_ecmd(dev, &ecmd);
1392                 spin_unlock_irq(&np->lock);
1393                 return r;
1394         }
1395         /* restart autonegotiation */
1396         case ETHTOOL_NWAY_RST: {
1397                 int tmp;
1398                 int r = -EINVAL;
1399                 /* if autoneg is off, it's an error */
1400                 tmp = armiiread(np->phy, MII_BMCR);
1401                 if (tmp & BMCR_ANENABLE) {
1402                         tmp |= (BMCR_ANRESTART);
1403                         armiiwrite(np->phy, MII_BMCR, tmp);
1404                         r = 0;
1405                 }
1406                 return r;
1407         }
1408         /* get link status */
1409         case ETHTOOL_GLINK: {
1410                 struct ethtool_value edata = {ETHTOOL_GLINK};
1411                 edata.data = (armiiread(np->phy, MII_BMSR)&BMSR_LSTATUS) ? 1:0;
1412                 if (copy_to_user(useraddr, &edata, sizeof(edata)))
1413                         return -EFAULT;
1414                 return 0;
1415         }
1416             }
1417         
1418         return -EOPNOTSUPP;
1419 }
1420
1421 static int ar2313_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1422 {
1423         struct mii_ioctl_data *data = (struct mii_ioctl_data *)&ifr->ifr_data;
1424
1425         switch (cmd) {
1426         case SIOCDEVPRIVATE: {
1427             struct ar2313_cmd scmd;
1428
1429             if (copy_from_user(&scmd, ifr->ifr_data, sizeof(scmd)))
1430             return -EFAULT;
1431
1432 #if DEBUG
1433         printk("%s: ioctl devprivate c=%d a=%x l=%d m=%d d=%x,%x\n",
1434                dev->name, scmd.cmd,
1435                scmd.address, scmd.length,
1436                scmd.mailbox, scmd.data[0], scmd.data[1]);
1437 #endif /* DEBUG */
1438
1439         switch (scmd.cmd) {
1440         case AR2313_READ_DATA:
1441             if(scmd.length==4){
1442                 scmd.data[0] = *((u32*)scmd.address);
1443             } else if(scmd.length==2) {
1444                 scmd.data[0] = *((u16*)scmd.address);
1445             } else if (scmd.length==1) {
1446                 scmd.data[0] = *((u8*)scmd.address);
1447             } else {
1448                 return -EOPNOTSUPP;
1449             }
1450             if(copy_to_user(ifr->ifr_data, &scmd, sizeof(scmd)))
1451                 return -EFAULT;
1452             break;
1453
1454         case AR2313_WRITE_DATA:
1455             if(scmd.length==4){
1456                 *((u32*)scmd.address) = scmd.data[0];
1457             } else if(scmd.length==2) {
1458                 *((u16*)scmd.address) = scmd.data[0];
1459             } else if (scmd.length==1) {
1460                 *((u8*)scmd.address) = scmd.data[0];
1461             } else {
1462                 return -EOPNOTSUPP;
1463             }
1464             break;
1465
1466         case AR2313_GET_VERSION:
1467             // SAMEER: sprintf((char*) &scmd, "%s", ARUBA_VERSION);
1468             if(copy_to_user(ifr->ifr_data, &scmd, sizeof(scmd)))
1469                 return -EFAULT;
1470             break;
1471
1472         default:
1473             return -EOPNOTSUPP;
1474         }
1475         return 0;
1476         }
1477           
1478         case SIOCETHTOOL:
1479             return netdev_ethtool_ioctl(dev, (void *) ifr->ifr_data);
1480
1481         case SIOCGMIIPHY:               /* Get address of MII PHY in use. */
1482             data->phy_id = 1;
1483             /* Fall Through */
1484
1485         case SIOCGMIIREG:               /* Read MII PHY register. */
1486         case SIOCDEVPRIVATE+1:  /* for binary compat, remove in 2.5 */
1487             data->val_out = armiiread(data->phy_id & 0x1f, 
1488                                       data->reg_num & 0x1f);
1489             return 0;
1490         case SIOCSMIIREG:               /* Write MII PHY register. */
1491         case SIOCDEVPRIVATE+2:  /* for binary compat, remove in 2.5 */
1492             if (!capable(CAP_NET_ADMIN))
1493                 return -EPERM;
1494             armiiwrite(data->phy_id & 0x1f, 
1495                        data->reg_num & 0x1f, data->val_in);
1496             return 0;
1497
1498         case SIOCSIFHWADDR:
1499             if (copy_from_user(dev->dev_addr, ifr->ifr_data, sizeof(dev->dev_addr)))
1500                 return -EFAULT;
1501             return 0;
1502
1503         case SIOCGIFHWADDR:
1504             if (copy_to_user(ifr->ifr_data, dev->dev_addr, sizeof(dev->dev_addr)))
1505                 return -EFAULT;
1506             return 0;
1507
1508         default:
1509             break;
1510         }
1511
1512         return -EOPNOTSUPP;
1513 }
1514
1515 static struct net_device_stats *ar2313_get_stats(struct net_device *dev)
1516 {
1517   struct ar2313_private *sp = dev->priv;
1518         return &sp->stats;
1519 }
1520
1521 static short
1522 armiiread(short phy, short reg)
1523 {
1524   volatile ETHERNET_STRUCT * ethernet;
1525
1526   ethernet = (volatile ETHERNET_STRUCT *)(ar_eth_base); /* always MAC 0 */
1527   ethernet->mii_addr = ((reg << MII_ADDR_REG_SHIFT) |
1528                             (phy << MII_ADDR_PHY_SHIFT));
1529   while (ethernet->mii_addr & MII_ADDR_BUSY);
1530   return (ethernet->mii_data >> MII_DATA_SHIFT);
1531 }
1532
1533 static void
1534 armiiwrite(short phy, short reg, short data)
1535 {
1536   volatile ETHERNET_STRUCT * ethernet;
1537
1538   ethernet = (volatile ETHERNET_STRUCT *)(ar_eth_base); /* always MAC 0 */
1539   while (ethernet->mii_addr & MII_ADDR_BUSY);
1540   ethernet->mii_data = data << MII_DATA_SHIFT;
1541   ethernet->mii_addr = ((reg << MII_ADDR_REG_SHIFT) |
1542                             (phy << MII_ADDR_PHY_SHIFT) |
1543                             MII_ADDR_WRITE);
1544 }
1545