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