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