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