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