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