ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / cris / arch-v10 / drivers / ethernet.c
1 /* $Id: ethernet.c,v 1.17 2003/07/04 08:27:37 starvik Exp $
2  *
3  * e100net.c: A network driver for the ETRAX 100LX network controller.
4  *
5  * Copyright (c) 1998-2002 Axis Communications AB.
6  *
7  * The outline of this driver comes from skeleton.c.
8  *
9  * $Log: ethernet.c,v $
10  * Revision 1.17  2003/07/04 08:27:37  starvik
11  * Merge of Linux 2.5.74
12  *
13  * Revision 1.16  2003/04/24 08:28:22  starvik
14  * New LED behaviour: LED off when no link
15  *
16  * Revision 1.15  2003/04/09 05:20:47  starvik
17  * Merge of Linux 2.5.67
18  *
19  * Revision 1.13  2003/03/06 16:11:01  henriken
20  * Off by one error in group address register setting.
21  *
22  * Revision 1.12  2003/02/27 17:24:19  starvik
23  * Corrected Rev to Revision
24  *
25  * Revision 1.11  2003/01/24 09:53:21  starvik
26  * Oops. Initialize GA to 0, not to 1
27  *
28  * Revision 1.10  2003/01/24 09:50:55  starvik
29  * Initialize GA_0 and GA_1 to 0 to avoid matching of unwanted packets
30  *
31  * Revision 1.9  2002/12/13 07:40:58  starvik
32  * Added basic ethtool interface
33  * Handled out of memory when allocating new buffers
34  *
35  * Revision 1.8  2002/12/11 13:13:57  starvik
36  * Added arch/ to v10 specific includes
37  * Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer)
38  *
39  * Revision 1.7  2002/11/26 09:41:42  starvik
40  * Added e100_set_config (standard interface to set media type)
41  * Added protection against preemptive scheduling
42  * Added standard MII ioctls
43  *
44  * Revision 1.6  2002/11/21 07:18:18  starvik
45  * Timers must be initialized in 2.5.48
46  *
47  * Revision 1.5  2002/11/20 11:56:11  starvik
48  * Merge of Linux 2.5.48
49  *
50  * Revision 1.4  2002/11/18 07:26:46  starvik
51  * Linux 2.5 port of latest Linux 2.4 ethernet driver
52  *
53  * Revision 1.33  2002/10/02 20:16:17  hp
54  * SETF, SETS: Use underscored IO_x_ macros rather than incorrect token concatenation
55  *
56  * Revision 1.32  2002/09/16 06:05:58  starvik
57  * Align memory returned by dev_alloc_skb
58  * Moved handling of sent packets to interrupt to avoid reference counting problem
59  *
60  * Revision 1.31  2002/09/10 13:28:23  larsv
61  * Return -EINVAL for unknown ioctls to avoid confusing tools that tests
62  * for supported functionality by issuing special ioctls, i.e. wireless
63  * extensions.
64  *
65  * Revision 1.30  2002/05/07 18:50:08  johana
66  * Correct spelling in comments.
67  *
68  * Revision 1.29  2002/05/06 05:38:49  starvik
69  * Performance improvements:
70  *    Large packets are not copied (breakpoint set to 256 bytes)
71  *    The cache bug workaround is delayed until half of the receive list
72  *      has been used
73  *    Added transmit list
74  *    Transmit interrupts are only enabled when transmit queue is full
75  *
76  * Revision 1.28.2.1  2002/04/30 08:15:51  starvik
77  * Performance improvements:
78  *   Large packets are not copied (breakpoint set to 256 bytes)
79  *   The cache bug workaround is delayed until half of the receive list
80  *     has been used.
81  *   Added transmit list
82  *   Transmit interrupts are only enabled when transmit queue is full
83  *
84  * Revision 1.28  2002/04/22 11:47:21  johana
85  * Fix according to 2.4.19-pre7. time_after/time_before and
86  * missing end of comment.
87  * The patch has a typo for ethernet.c in e100_clear_network_leds(),
88  *  that is fixed here.
89  *
90  * Revision 1.27  2002/04/12 11:55:11  bjornw
91  * Added TODO
92  *
93  * Revision 1.26  2002/03/15 17:11:02  bjornw
94  * Use prepare_rx_descriptor after the CPU has touched the receiving descs
95  *
96  * Revision 1.25  2002/03/08 13:07:53  bjornw
97  * Unnecessary spinlock removed
98  *
99  * Revision 1.24  2002/02/20 12:57:43  fredriks
100  * Replaced MIN() with min().
101  *
102  * Revision 1.23  2002/02/20 10:58:14  fredriks
103  * Strip the Ethernet checksum (4 bytes) before forwarding a frame to upper layers.
104  *
105  * Revision 1.22  2002/01/30 07:48:22  matsfg
106  * Initiate R_NETWORK_TR_CTRL
107  *
108  * Revision 1.21  2001/11/23 11:54:49  starvik
109  * Added IFF_PROMISC and IFF_ALLMULTI handling in set_multicast_list
110  * Removed compiler warnings
111  *
112  * Revision 1.20  2001/11/12 19:26:00  pkj
113  * * Corrected e100_negotiate() to not assign half to current_duplex when
114  *   it was supposed to compare them...
115  * * Cleaned up failure handling in e100_open().
116  * * Fixed compiler warnings.
117  *
118  * Revision 1.19  2001/11/09 07:43:09  starvik
119  * Added full duplex support
120  * Added ioctl to set speed and duplex
121  * Clear LED timer only runs when LED is lit
122  *
123  * Revision 1.18  2001/10/03 14:40:43  jonashg
124  * Update rx_bytes counter.
125  *
126  * Revision 1.17  2001/06/11 12:43:46  olof
127  * Modified defines for network LED behavior
128  *
129  * Revision 1.16  2001/05/30 06:12:46  markusl
130  * TxDesc.next should not be set to NULL
131  *
132  * Revision 1.15  2001/05/29 10:27:04  markusl
133  * Updated after review remarks:
134  * +Use IO_EXTRACT
135  * +Handle underrun
136  *
137  * Revision 1.14  2001/05/29 09:20:14  jonashg
138  * Use driver name on printk output so one can tell which driver that complains.
139  *
140  * Revision 1.13  2001/05/09 12:35:59  johana
141  * Use DMA_NBR and IRQ_NBR defines from dma.h and irq.h
142  *
143  * Revision 1.12  2001/04/05 11:43:11  tobiasa
144  * Check dev before panic.
145  *
146  * Revision 1.11  2001/04/04 11:21:05  markusl
147  * Updated according to review remarks
148  *
149  * Revision 1.10  2001/03/26 16:03:06  bjornw
150  * Needs linux/config.h
151  *
152  * Revision 1.9  2001/03/19 14:47:48  pkj
153  * * Make sure there is always a pause after the network LEDs are
154  *   changed so they will not look constantly lit during heavy traffic.
155  * * Always use HZ when setting times relative to jiffies.
156  * * Use LED_NETWORK_SET() when setting the network LEDs.
157  *
158  * Revision 1.8  2001/02/27 13:52:48  bjornw
159  * malloc.h -> slab.h
160  *
161  * Revision 1.7  2001/02/23 13:46:38  bjornw
162  * Spellling check
163  *
164  * Revision 1.6  2001/01/26 15:21:04  starvik
165  * Don't disable interrupts while reading MDIO registers (MDIO is slow)
166  * Corrected promiscuous mode
167  * Improved deallocation of IRQs ("ifconfig eth0 down" now works)
168  *
169  * Revision 1.5  2000/11/29 17:22:22  bjornw
170  * Get rid of the udword types legacy stuff
171  *
172  * Revision 1.4  2000/11/22 16:36:09  bjornw
173  * Please marketing by using the correct case when spelling Etrax.
174  *
175  * Revision 1.3  2000/11/21 16:43:04  bjornw
176  * Minor short->int change
177  *
178  * Revision 1.2  2000/11/08 14:27:57  bjornw
179  * 2.4 port
180  *
181  * Revision 1.1  2000/11/06 13:56:00  bjornw
182  * Verbatim copy of the 1.24 version of e100net.c from elinux
183  *
184  * Revision 1.24  2000/10/04 15:55:23  bjornw
185  * * Use virt_to_phys etc. for DMA addresses
186  * * Removed bogus CHECKSUM_UNNECESSARY
187  *
188  *
189  */
190
191 #include <linux/config.h>
192
193 #include <linux/module.h>
194
195 #include <linux/kernel.h>
196 #include <linux/sched.h>
197 #include <linux/delay.h>
198 #include <linux/types.h>
199 #include <linux/fcntl.h>
200 #include <linux/interrupt.h>
201 #include <linux/ptrace.h>
202 #include <linux/ioport.h>
203 #include <linux/in.h>
204 #include <linux/slab.h>
205 #include <linux/string.h>
206 #include <linux/spinlock.h>
207 #include <linux/errno.h>
208 #include <linux/init.h>
209
210 #include <linux/if.h>
211 #include <linux/mii.h>
212 #include <linux/netdevice.h>
213 #include <linux/etherdevice.h>
214 #include <linux/skbuff.h>
215 #include <linux/ethtool.h>
216
217 #include <asm/arch/svinto.h>/* DMA and register descriptions */
218 #include <asm/io.h>         /* LED_* I/O functions */
219 #include <asm/irq.h>
220 #include <asm/dma.h>
221 #include <asm/system.h>
222 #include <asm/bitops.h>
223 #include <asm/ethernet.h>
224 #include <asm/cache.h>
225
226 //#define ETHDEBUG
227 #define D(x)
228
229 /*
230  * The name of the card. Is used for messages and in the requests for
231  * io regions, irqs and dma channels
232  */
233
234 static const char* cardname = "ETRAX 100LX built-in ethernet controller";
235
236 /* A default ethernet address. Highlevel SW will set the real one later */
237
238 static struct sockaddr default_mac = {
239         0,
240         { 0x00, 0x40, 0x8C, 0xCD, 0x00, 0x00 }
241 };
242
243 /* Information that need to be kept for each board. */
244 struct net_local {
245         struct net_device_stats stats;
246
247         /* Tx control lock.  This protects the transmit buffer ring
248          * state along with the "tx full" state of the driver.  This
249          * means all netif_queue flow control actions are protected
250          * by this lock as well.
251          */
252         spinlock_t lock;
253 };
254
255 typedef struct etrax_eth_descr
256 {
257         etrax_dma_descr descr;
258         struct sk_buff* skb;
259 } etrax_eth_descr;
260
261 /* Duplex settings */
262 enum duplex
263 {
264         half,
265         full,
266         autoneg
267 };
268
269 /* Dma descriptors etc. */
270
271 #define MAX_MEDIA_DATA_SIZE 1518
272
273 #define MIN_PACKET_LEN      46
274 #define ETHER_HEAD_LEN      14
275
276 /* 
277 ** MDIO constants.
278 */
279 #define MDIO_BASE_STATUS_REG                0x1
280 #define MDIO_BASE_CONTROL_REG               0x0
281 #define MDIO_BC_NEGOTIATE                0x0200
282 #define MDIO_BC_FULL_DUPLEX_MASK         0x0100
283 #define MDIO_BC_AUTO_NEG_MASK            0x1000
284 #define MDIO_BC_SPEED_SELECT_MASK        0x2000
285 #define MDIO_ADVERTISMENT_REG               0x4
286 #define MDIO_ADVERT_100_FD                0x100
287 #define MDIO_ADVERT_100_HD                0x080
288 #define MDIO_ADVERT_10_FD                 0x040
289 #define MDIO_ADVERT_10_HD                 0x020
290 #define MDIO_LINK_UP_MASK                   0x4
291 #define MDIO_START                          0x1
292 #define MDIO_READ                           0x2
293 #define MDIO_WRITE                          0x1
294 #define MDIO_PREAMBLE              0xfffffffful
295
296 /* Broadcom specific */
297 #define MDIO_AUX_CTRL_STATUS_REG           0x18
298 #define MDIO_FULL_DUPLEX_IND                0x1
299 #define MDIO_SPEED                          0x2
300 #define MDIO_PHYS_ADDR                      0x0
301
302 /* Network flash constants */
303 #define NET_FLASH_TIME                  (HZ/50) /* 20 ms */
304 #define NET_FLASH_PAUSE                (HZ/100) /* 10 ms */
305 #define NET_LINK_UP_CHECK_INTERVAL       (2*HZ) /* 2 s   */
306 #define NET_DUPLEX_CHECK_INTERVAL        (2*HZ) /* 2 s   */
307
308 #define NO_NETWORK_ACTIVITY 0
309 #define NETWORK_ACTIVITY    1
310
311 #define NBR_OF_RX_DESC     64
312 #define NBR_OF_TX_DESC     256
313
314 /* Large packets are sent directly to upper layers while small packets are */
315 /* copied (to reduce memory waste). The following constant decides the breakpoint */
316 #define RX_COPYBREAK 256
317
318 /* Due to a chip bug we need to flush the cache when descriptors are returned */
319 /* to the DMA. To decrease performance impact we return descriptors in chunks. */
320 /* The following constant determines the number of descriptors to return. */
321 #define RX_QUEUE_THRESHOLD  NBR_OF_RX_DESC/2
322
323 #define GET_BIT(bit,val)   (((val) >> (bit)) & 0x01)
324
325 /* Define some macros to access ETRAX 100 registers */
326 #define SETF(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
327                                           IO_FIELD_(reg##_, field##_, val)
328 #define SETS(var, reg, field, val) var = (var & ~IO_MASK_(reg##_, field##_)) | \
329                                           IO_STATE_(reg##_, field##_, _##val)
330
331 static etrax_eth_descr *myNextRxDesc;  /* Points to the next descriptor to
332                                           to be processed */
333 static etrax_eth_descr *myLastRxDesc;  /* The last processed descriptor */
334 static etrax_eth_descr *myPrevRxDesc;  /* The descriptor right before myNextRxDesc */
335
336 static etrax_eth_descr RxDescList[NBR_OF_RX_DESC] __attribute__ ((aligned(32)));
337
338 static etrax_eth_descr* myFirstTxDesc; /* First packet not yet sent */
339 static etrax_eth_descr* myLastTxDesc;  /* End of send queue */
340 static etrax_eth_descr* myNextTxDesc;  /* Next descriptor to use */
341 static etrax_eth_descr TxDescList[NBR_OF_TX_DESC] __attribute__ ((aligned(32)));
342
343 static unsigned int network_rec_config_shadow = 0;
344
345 /* Network speed indication. */
346 static struct timer_list speed_timer = TIMER_INITIALIZER(NULL, 0, 0);
347 static struct timer_list clear_led_timer = TIMER_INITIALIZER(NULL, 0, 0);
348 static int current_speed; /* Speed read from transceiver */
349 static int current_speed_selection; /* Speed selected by user */
350 static unsigned long led_next_time;
351 static int led_active;
352 static int rx_queue_len;
353
354 /* Duplex */
355 static struct timer_list duplex_timer = TIMER_INITIALIZER(NULL, 0, 0);
356 static int full_duplex;
357 static enum duplex current_duplex;
358
359 /* Index to functions, as function prototypes. */
360
361 static int etrax_ethernet_init(void);
362
363 static int e100_open(struct net_device *dev);
364 static int e100_set_mac_address(struct net_device *dev, void *addr);
365 static int e100_send_packet(struct sk_buff *skb, struct net_device *dev);
366 static irqreturn_t e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs *regs);
367 static irqreturn_t e100nw_interrupt(int irq, void *dev_id, struct pt_regs *regs);
368 static void e100_rx(struct net_device *dev);
369 static int e100_close(struct net_device *dev);
370 static int e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd);
371 static int e100_ethtool_ioctl(struct net_device* dev, struct ifreq *ifr);
372 static int e100_set_config(struct net_device* dev, struct ifmap* map);
373 static void e100_tx_timeout(struct net_device *dev);
374 static struct net_device_stats *e100_get_stats(struct net_device *dev);
375 static void set_multicast_list(struct net_device *dev);
376 static void e100_hardware_send_packet(char *buf, int length);
377 static void update_rx_stats(struct net_device_stats *);
378 static void update_tx_stats(struct net_device_stats *);
379
380 static void e100_check_speed(unsigned long dummy);
381 static void e100_set_speed(unsigned long speed);
382 static void e100_check_duplex(unsigned long dummy);
383 static void e100_set_duplex(enum duplex);
384 static void e100_negotiate(void);
385
386 static unsigned short e100_get_mdio_reg(unsigned char reg_num);
387 static void e100_set_mdio_reg(unsigned char reg, unsigned short data);
388 static void e100_send_mdio_cmd(unsigned short cmd, int write_cmd);
389 static void e100_send_mdio_bit(unsigned char bit);
390 static unsigned char e100_receive_mdio_bit(void);
391 static void e100_reset_transceiver(void);
392
393 static void e100_clear_network_leds(unsigned long dummy);
394 static void e100_set_network_leds(int active);
395
396 #define tx_done(dev) (*R_DMA_CH0_CMD == 0)
397
398 /*
399  * Check for a network adaptor of this type, and return '0' if one exists.
400  * If dev->base_addr == 0, probe all likely locations.
401  * If dev->base_addr == 1, always return failure.
402  * If dev->base_addr == 2, allocate space for the device and return success
403  * (detachable devices only).
404  */
405
406 static int __init
407 etrax_ethernet_init(void)
408 {
409         struct net_device *dev;
410         int i, err;
411
412         printk("ETRAX 100LX 10/100MBit ethernet v2.0 (c) 2000-2003 Axis Communications AB\n");
413
414         dev = alloc_etherdev(sizeof(struct net_local));
415         if (!dev)
416                 return -ENOMEM;
417
418         dev->base_addr = (unsigned int)R_NETWORK_SA_0; /* just to have something to show */     
419
420         /* now setup our etrax specific stuff */
421
422         dev->irq = NETWORK_DMA_RX_IRQ_NBR; /* we really use DMATX as well... */
423         dev->dma = NETWORK_RX_DMA_NBR;
424
425         /* fill in our handlers so the network layer can talk to us in the future */
426
427         dev->open               = e100_open;
428         dev->hard_start_xmit    = e100_send_packet;
429         dev->stop               = e100_close;
430         dev->get_stats          = e100_get_stats;
431         dev->set_multicast_list = set_multicast_list;
432         dev->set_mac_address    = e100_set_mac_address;
433         dev->do_ioctl           = e100_ioctl;
434         dev->set_config         = e100_set_config;
435         dev->tx_timeout         = e100_tx_timeout;
436
437         /* Initialise the list of Etrax DMA-descriptors */
438
439         /* Initialise receive descriptors */
440
441         for (i = 0; i < NBR_OF_RX_DESC; i++) {
442                 /* Allocate two extra cachelines to make sure that buffer used by DMA
443                  * does not share cacheline with any other data (to avoid cache bug)
444                  */
445                 RxDescList[i].skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
446                 RxDescList[i].descr.ctrl   = 0;
447                 RxDescList[i].descr.sw_len = MAX_MEDIA_DATA_SIZE;
448                 RxDescList[i].descr.next   = virt_to_phys(&RxDescList[i + 1]);
449                 RxDescList[i].descr.buf    = L1_CACHE_ALIGN(virt_to_phys(RxDescList[i].skb->data));
450                 RxDescList[i].descr.status = 0;
451                 RxDescList[i].descr.hw_len = 0;             
452                 prepare_rx_descriptor(&RxDescList[i].descr);
453         }
454
455         RxDescList[NBR_OF_RX_DESC - 1].descr.ctrl   = d_eol;
456         RxDescList[NBR_OF_RX_DESC - 1].descr.next   = virt_to_phys(&RxDescList[0]);
457         rx_queue_len = 0;
458
459         /* Initialize transmit descriptors */
460         for (i = 0; i < NBR_OF_TX_DESC; i++) {
461                 TxDescList[i].descr.ctrl   = 0;
462                 TxDescList[i].descr.sw_len = 0;
463                 TxDescList[i].descr.next   = virt_to_phys(&TxDescList[i + 1].descr);
464                 TxDescList[i].descr.buf    = 0;
465                 TxDescList[i].descr.status = 0;
466                 TxDescList[i].descr.hw_len = 0;
467                 TxDescList[i].skb = 0;
468         }
469
470         TxDescList[NBR_OF_TX_DESC - 1].descr.ctrl   = d_eol;
471         TxDescList[NBR_OF_TX_DESC - 1].descr.next   = virt_to_phys(&TxDescList[0].descr);
472         
473         /* Initialise initial pointers */
474
475         myNextRxDesc  = &RxDescList[0];
476         myLastRxDesc  = &RxDescList[NBR_OF_RX_DESC - 1];
477         myPrevRxDesc  = &RxDescList[NBR_OF_RX_DESC - 1];
478         myFirstTxDesc = &TxDescList[0];
479         myNextTxDesc  = &TxDescList[0];
480         myLastTxDesc  = &TxDescList[NBR_OF_TX_DESC - 1];
481
482         /* Register device */
483         err = register_netdev(dev);
484         if (err) {
485                 free_netdev(dev);
486                 return err;
487         }
488
489         /* set the default MAC address */
490
491         e100_set_mac_address(dev, &default_mac);
492
493         /* Initialize speed indicator stuff. */
494
495         current_speed = 10;
496         current_speed_selection = 0; /* Auto */
497         speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
498         speed_timer.function = e100_check_speed;
499         add_timer(&speed_timer);
500         
501         clear_led_timer.function = e100_clear_network_leds;
502         
503         full_duplex = 0;
504         current_duplex = autoneg;
505         duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;             
506         duplex_timer.function = e100_check_duplex;
507         add_timer(&duplex_timer);
508
509         /* Initialize group address registers to make sure that no */
510         /* unwanted addresses are matched */
511         *R_NETWORK_GA_0 = 0x00000000;
512         *R_NETWORK_GA_1 = 0x00000000;
513         return 0;
514 }
515
516 /* set MAC address of the interface. called from the core after a
517  * SIOCSIFADDR ioctl, and from the bootup above.
518  */
519
520 static int
521 e100_set_mac_address(struct net_device *dev, void *p)
522 {
523         struct net_local *np = (struct net_local *)dev->priv;
524         struct sockaddr *addr = p;
525         int i;
526
527         spin_lock(&np->lock); /* preemption protection */
528
529         /* remember it */
530
531         memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
532
533         /* Write it to the hardware.
534          * Note the way the address is wrapped:
535          * *R_NETWORK_SA_0 = a0_0 | (a0_1 << 8) | (a0_2 << 16) | (a0_3 << 24);
536          * *R_NETWORK_SA_1 = a0_4 | (a0_5 << 8);
537          */
538         
539         *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
540                 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
541         *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
542         *R_NETWORK_SA_2 = 0;
543
544         /* show it in the log as well */
545
546         printk("%s: changed MAC to ", dev->name);
547
548         for (i = 0; i < 5; i++)
549                 printk("%02X:", dev->dev_addr[i]);
550
551         printk("%02X\n", dev->dev_addr[i]);
552
553         spin_unlock(&np->lock);
554
555         return 0;
556 }
557
558 /*
559  * Open/initialize the board. This is called (in the current kernel)
560  * sometime after booting when the 'ifconfig' program is run.
561  *
562  * This routine should set everything up anew at each open, even
563  * registers that "should" only need to be set once at boot, so that
564  * there is non-reboot way to recover if something goes wrong.
565  */
566
567 static int
568 e100_open(struct net_device *dev)
569 {
570         unsigned long flags;
571
572         /* disable the ethernet interface while we configure it */
573
574         *R_NETWORK_GEN_CONFIG =
575                 IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
576                 IO_STATE(R_NETWORK_GEN_CONFIG, enable, off);
577
578         /* enable the MDIO output pin */
579
580         *R_NETWORK_MGM_CTRL = IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable);
581
582         *R_IRQ_MASK0_CLR =
583                 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
584                 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
585                 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
586         
587         /* clear dma0 and 1 eop and descr irq masks */
588         *R_IRQ_MASK2_CLR =
589                 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
590                 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
591                 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
592                 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
593
594         /* Reset and wait for the DMA channels */
595
596         RESET_DMA(NETWORK_TX_DMA_NBR);
597         RESET_DMA(NETWORK_RX_DMA_NBR);
598         WAIT_DMA(NETWORK_TX_DMA_NBR);
599         WAIT_DMA(NETWORK_RX_DMA_NBR);
600
601         /* Initialise the etrax network controller */
602
603         /* allocate the irq corresponding to the receiving DMA */
604
605         if (request_irq(NETWORK_DMA_RX_IRQ_NBR, e100rxtx_interrupt, 0,
606                         cardname, (void *)dev)) {
607                 goto grace_exit0;
608         }
609
610         /* allocate the irq corresponding to the transmitting DMA */
611
612         if (request_irq(NETWORK_DMA_TX_IRQ_NBR, e100rxtx_interrupt, 0,
613                         cardname, (void *)dev)) {
614                 goto grace_exit1;
615         }
616
617         /* allocate the irq corresponding to the network errors etc */
618
619         if (request_irq(NETWORK_STATUS_IRQ_NBR, e100nw_interrupt, 0,
620                         cardname, (void *)dev)) {
621                 goto grace_exit2;
622         }
623
624         /* give the HW an idea of what MAC address we want */
625
626         *R_NETWORK_SA_0 = dev->dev_addr[0] | (dev->dev_addr[1] << 8) |
627                 (dev->dev_addr[2] << 16) | (dev->dev_addr[3] << 24);
628         *R_NETWORK_SA_1 = dev->dev_addr[4] | (dev->dev_addr[5] << 8);
629         *R_NETWORK_SA_2 = 0;
630
631 #if 0
632         /* use promiscuous mode for testing */
633         *R_NETWORK_GA_0 = 0xffffffff;
634         *R_NETWORK_GA_1 = 0xffffffff;
635
636         *R_NETWORK_REC_CONFIG = 0xd; /* broadcast rec, individ. rec, ma0 enabled */
637 #else
638         SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, broadcast, receive);
639         SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, ma0, enable);
640         SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
641         *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
642 #endif
643
644         *R_NETWORK_GEN_CONFIG =
645                 IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
646                 IO_STATE(R_NETWORK_GEN_CONFIG, enable, on);
647
648         *R_NETWORK_TR_CTRL = 
649                 IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr) |
650                 IO_STATE(R_NETWORK_TR_CTRL, delay, none) |
651                 IO_STATE(R_NETWORK_TR_CTRL, cancel, dont) |
652                 IO_STATE(R_NETWORK_TR_CTRL, cd, enable) |
653                 IO_STATE(R_NETWORK_TR_CTRL, retry, enable) |
654                 IO_STATE(R_NETWORK_TR_CTRL, pad, enable) |
655                 IO_STATE(R_NETWORK_TR_CTRL, crc, enable);
656
657         save_flags(flags);
658         cli();
659
660         /* enable the irq's for ethernet DMA */
661
662         *R_IRQ_MASK2_SET =
663                 IO_STATE(R_IRQ_MASK2_SET, dma1_eop, set); 
664
665         *R_IRQ_MASK0_SET =
666                 IO_STATE(R_IRQ_MASK0_SET, overrun,       set) |
667                 IO_STATE(R_IRQ_MASK0_SET, underrun,      set) |
668                 IO_STATE(R_IRQ_MASK0_SET, excessive_col, set);
669
670         /* make sure the irqs are cleared */
671
672         *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
673         *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
674
675         /* make sure the rec and transmit error counters are cleared */
676
677         (void)*R_REC_COUNTERS;  /* dummy read */
678         (void)*R_TR_COUNTERS;   /* dummy read */
679
680         /* start the receiving DMA channel so we can receive packets from now on */
681
682         *R_DMA_CH1_FIRST = virt_to_phys(myNextRxDesc);
683         *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, start);
684
685         /* Set up transmit DMA channel so it can be restarted later */
686         
687         *R_DMA_CH0_FIRST = 0;
688         *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);
689
690         restore_flags(flags);
691         
692         /* We are now ready to accept transmit requeusts from
693          * the queueing layer of the networking.
694          */
695         netif_start_queue(dev);
696
697         return 0;
698
699 grace_exit2:
700         free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
701 grace_exit1:
702         free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
703 grace_exit0:
704         return -EAGAIN;
705 }
706
707
708 static void
709 e100_check_speed(unsigned long dummy)
710 {
711         unsigned long data;
712         int old_speed = current_speed;
713
714         data = e100_get_mdio_reg(MDIO_BASE_STATUS_REG);
715         if (!(data & MDIO_LINK_UP_MASK)) {
716                 current_speed = 0;
717         } else {
718                 data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
719                 current_speed = (data & MDIO_SPEED ? 100 : 10);
720         }
721         
722         if (old_speed != current_speed)
723                 e100_set_network_leds(NO_NETWORK_ACTIVITY);
724
725         /* Reinitialize the timer. */
726         speed_timer.expires = jiffies + NET_LINK_UP_CHECK_INTERVAL;
727         add_timer(&speed_timer);
728 }
729
730 static void
731 e100_negotiate(void)
732 {
733         unsigned short data = e100_get_mdio_reg(MDIO_ADVERTISMENT_REG);
734
735         /* Discard old speed and duplex settings */
736         data &= ~(MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | 
737                   MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD);
738   
739         switch (current_speed_selection) {
740                 case 10 :
741                         if (current_duplex == full)
742                                 data |= MDIO_ADVERT_10_FD;
743                         else if (current_duplex == half)
744                                 data |= MDIO_ADVERT_10_HD;
745                         else
746                                 data |= MDIO_ADVERT_10_HD |  MDIO_ADVERT_10_FD;
747                         break;
748
749                 case 100 :
750                          if (current_duplex == full)
751                                 data |= MDIO_ADVERT_100_FD;
752                         else if (current_duplex == half)
753                                 data |= MDIO_ADVERT_100_HD;
754                         else
755                                 data |= MDIO_ADVERT_100_HD |  MDIO_ADVERT_100_FD;
756                         break;
757
758                 case 0 : /* Auto */
759                          if (current_duplex == full)
760                                 data |= MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD;
761                         else if (current_duplex == half)
762                                 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_10_HD;
763                         else
764                                 data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
765                         break;
766
767                 default : /* assume autoneg speed and duplex */
768                         data |= MDIO_ADVERT_100_HD | MDIO_ADVERT_100_FD | 
769                                 MDIO_ADVERT_10_FD | MDIO_ADVERT_10_HD;
770         }
771
772         e100_set_mdio_reg(MDIO_ADVERTISMENT_REG, data);
773
774         /* Renegotiate with link partner */
775         data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
776         data |= MDIO_BC_NEGOTIATE;
777
778         e100_set_mdio_reg(MDIO_BASE_CONTROL_REG, data);
779 }
780
781 static void
782 e100_set_speed(unsigned long speed)
783 {
784         current_speed_selection = speed;
785         e100_negotiate();
786 }
787
788 static void
789 e100_check_duplex(unsigned long dummy)
790 {
791         unsigned long data;
792
793         data = e100_get_mdio_reg(MDIO_AUX_CTRL_STATUS_REG);
794         
795         if (data & MDIO_FULL_DUPLEX_IND) {
796                 if (!full_duplex) { /* Duplex changed to full? */
797                         full_duplex = 1;
798                         SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
799                         *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
800                 }
801         } else { /* half */
802                 if (full_duplex) { /* Duplex changed to half? */
803                         full_duplex = 0;
804                         SETF(network_rec_config_shadow, R_NETWORK_REC_CONFIG, duplex, full_duplex);
805                         *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
806                 }
807         }
808
809         /* Reinitialize the timer. */
810         duplex_timer.expires = jiffies + NET_DUPLEX_CHECK_INTERVAL;
811         add_timer(&duplex_timer);
812 }
813
814 static void 
815 e100_set_duplex(enum duplex new_duplex)
816 {
817         current_duplex = new_duplex;
818         e100_negotiate();
819 }
820
821
822 static unsigned short
823 e100_get_mdio_reg(unsigned char reg_num)
824 {
825         unsigned short cmd;    /* Data to be sent on MDIO port */
826         unsigned short data;   /* Data read from MDIO */
827         int bitCounter;
828         
829         /* Start of frame, OP Code, Physical Address, Register Address */
830         cmd = (MDIO_START << 14) | (MDIO_READ << 12) | (MDIO_PHYS_ADDR << 7) |
831                 (reg_num << 2);
832         
833         e100_send_mdio_cmd(cmd, 0);
834         
835         data = 0;
836         
837         /* Data... */
838         for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
839                 data |= (e100_receive_mdio_bit() << bitCounter);
840         }
841
842         return data;
843 }
844
845 static void
846 e100_set_mdio_reg(unsigned char reg, unsigned short data)
847 {
848         int bitCounter;
849         unsigned short cmd;
850
851         cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (MDIO_PHYS_ADDR << 7) |
852               (reg << 2);
853
854         e100_send_mdio_cmd(cmd, 1);
855
856         /* Data... */
857         for (bitCounter=15; bitCounter>=0 ; bitCounter--) {
858                 e100_send_mdio_bit(GET_BIT(bitCounter, data));
859         }
860
861 }
862
863 static void
864 e100_send_mdio_cmd(unsigned short cmd, int write_cmd)
865 {
866         int bitCounter;
867         unsigned char data = 0x2;
868         
869         /* Preamble */
870         for (bitCounter = 31; bitCounter>= 0; bitCounter--)
871                 e100_send_mdio_bit(GET_BIT(bitCounter, MDIO_PREAMBLE));
872
873         for (bitCounter = 15; bitCounter >= 2; bitCounter--)
874                 e100_send_mdio_bit(GET_BIT(bitCounter, cmd));
875
876         /* Turnaround */
877         for (bitCounter = 1; bitCounter >= 0 ; bitCounter--)
878                 if (write_cmd)
879                         e100_send_mdio_bit(GET_BIT(bitCounter, data));
880                 else
881                         e100_receive_mdio_bit();
882 }
883
884 static void
885 e100_send_mdio_bit(unsigned char bit)
886 {
887         *R_NETWORK_MGM_CTRL =
888                 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
889                 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
890         udelay(1);
891         *R_NETWORK_MGM_CTRL =
892                 IO_STATE(R_NETWORK_MGM_CTRL, mdoe, enable) |
893                 IO_MASK(R_NETWORK_MGM_CTRL, mdck) |
894                 IO_FIELD(R_NETWORK_MGM_CTRL, mdio, bit);
895         udelay(1);
896 }
897
898 static unsigned char
899 e100_receive_mdio_bit()
900 {
901         unsigned char bit;
902         *R_NETWORK_MGM_CTRL = 0;
903         bit = IO_EXTRACT(R_NETWORK_STAT, mdio, *R_NETWORK_STAT);
904         udelay(1);
905         *R_NETWORK_MGM_CTRL = IO_MASK(R_NETWORK_MGM_CTRL, mdck);
906         udelay(1);
907         return bit;
908 }
909
910 static void 
911 e100_reset_transceiver(void)
912 {
913         unsigned short cmd;
914         unsigned short data;
915         int bitCounter;
916
917         data = e100_get_mdio_reg(MDIO_BASE_CONTROL_REG);
918
919         cmd = (MDIO_START << 14) | (MDIO_WRITE << 12) | (MDIO_PHYS_ADDR << 7) | (MDIO_BASE_CONTROL_REG << 2);
920
921         e100_send_mdio_cmd(cmd, 1);
922         
923         data |= 0x8000;
924         
925         for (bitCounter = 15; bitCounter >= 0 ; bitCounter--) {
926                 e100_send_mdio_bit(GET_BIT(bitCounter, data));
927         }
928 }
929
930 /* Called by upper layers if they decide it took too long to complete
931  * sending a packet - we need to reset and stuff.
932  */
933
934 static void
935 e100_tx_timeout(struct net_device *dev)
936 {
937         struct net_local *np = (struct net_local *)dev->priv;
938         unsigned long flags;
939
940         spin_lock_irqsave(&np->lock, flags);
941
942         printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name,
943                tx_done(dev) ? "IRQ problem" : "network cable problem");
944         
945         /* remember we got an error */
946         
947         np->stats.tx_errors++; 
948         
949         /* reset the TX DMA in case it has hung on something */
950         
951         RESET_DMA(NETWORK_TX_DMA_NBR);
952         WAIT_DMA(NETWORK_TX_DMA_NBR);
953         
954         /* Reset the transceiver. */
955         
956         e100_reset_transceiver();
957         
958         /* and get rid of the packets that never got an interrupt */
959         while (myFirstTxDesc != myNextTxDesc)
960         {
961                 dev_kfree_skb(myFirstTxDesc->skb);
962                 myFirstTxDesc->skb = 0;
963                 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
964         }
965
966         /* Set up transmit DMA channel so it can be restarted later */
967         *R_DMA_CH0_FIRST = 0;
968         *R_DMA_CH0_DESCR = virt_to_phys(myLastTxDesc);  
969
970         /* tell the upper layers we're ok again */
971         
972         netif_wake_queue(dev);
973         spin_unlock_irqrestore(&np->lock, flags);
974 }
975
976
977 /* This will only be invoked if the driver is _not_ in XOFF state.
978  * What this means is that we need not check it, and that this
979  * invariant will hold if we make sure that the netif_*_queue()
980  * calls are done at the proper times.
981  */
982
983 static int
984 e100_send_packet(struct sk_buff *skb, struct net_device *dev)
985 {
986         struct net_local *np = (struct net_local *)dev->priv;
987         int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
988         unsigned char *buf = skb->data;
989         unsigned long flags;
990         
991 #ifdef ETHDEBUG
992         printk("send packet len %d\n", length);
993 #endif
994         spin_lock_irqsave(&np->lock, flags);  /* protect from tx_interrupt and ourself */
995
996         myNextTxDesc->skb = skb;
997
998         dev->trans_start = jiffies;
999         
1000         e100_hardware_send_packet(buf, length);
1001
1002         myNextTxDesc = phys_to_virt(myNextTxDesc->descr.next);
1003
1004         /* Stop queue if full */
1005         if (myNextTxDesc == myFirstTxDesc) {
1006                 /* Enable transmit interrupt to wake up queue */
1007                 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);                
1008                 *R_IRQ_MASK2_SET = IO_STATE(R_IRQ_MASK2_SET, dma0_eop, set);
1009                 netif_stop_queue(dev);
1010         }
1011
1012         spin_unlock_irqrestore(&np->lock, flags);
1013
1014         return 0;
1015 }
1016
1017 /*
1018  * The typical workload of the driver:
1019  *   Handle the network interface interrupts.
1020  */
1021
1022 static irqreturn_t
1023 e100rxtx_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1024 {
1025         struct net_device *dev = (struct net_device *)dev_id;
1026         struct net_local *np = (struct net_local *)dev->priv;
1027         unsigned long irqbits = *R_IRQ_MASK2_RD;
1028  
1029         /* Handle received packets */
1030         if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma1_eop, active)) {
1031                 /* acknowledge the eop interrupt */
1032
1033                 *R_DMA_CH1_CLR_INTR = IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do);
1034
1035                 /* check if one or more complete packets were indeed received */
1036
1037                 while (*R_DMA_CH1_FIRST != virt_to_phys(myNextRxDesc)) {
1038                         /* Take out the buffer and give it to the OS, then
1039                          * allocate a new buffer to put a packet in.
1040                          */
1041                         e100_rx(dev);
1042                         ((struct net_local *)dev->priv)->stats.rx_packets++;
1043                         /* restart/continue on the channel, for safety */
1044                         *R_DMA_CH1_CMD = IO_STATE(R_DMA_CH1_CMD, cmd, restart);
1045                         /* clear dma channel 1 eop/descr irq bits */
1046                         *R_DMA_CH1_CLR_INTR =
1047                                 IO_STATE(R_DMA_CH1_CLR_INTR, clr_eop, do) |
1048                                 IO_STATE(R_DMA_CH1_CLR_INTR, clr_descr, do);
1049                         
1050                         /* now, we might have gotten another packet
1051                            so we have to loop back and check if so */
1052                 }
1053         }
1054
1055         /* Report any packets that have been sent */
1056         while (myFirstTxDesc != phys_to_virt(*R_DMA_CH0_FIRST) &&
1057                myFirstTxDesc != myNextTxDesc)
1058         {
1059                 np->stats.tx_bytes += myFirstTxDesc->skb->len;
1060                 np->stats.tx_packets++;
1061
1062                 /* dma is ready with the transmission of the data in tx_skb, so now
1063                    we can release the skb memory */
1064                 dev_kfree_skb_irq(myFirstTxDesc->skb);
1065                 myFirstTxDesc->skb = 0;
1066                 myFirstTxDesc = phys_to_virt(myFirstTxDesc->descr.next);
1067         }
1068
1069         if (irqbits & IO_STATE(R_IRQ_MASK2_RD, dma0_eop, active)) {
1070                 /* acknowledge the eop interrupt and wake up queue */
1071                 *R_DMA_CH0_CLR_INTR = IO_STATE(R_DMA_CH0_CLR_INTR, clr_eop, do);
1072                 *R_IRQ_MASK2_CLR = IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr);            
1073                 netif_wake_queue(dev);
1074         }
1075         return IRQ_HANDLED;
1076 }
1077
1078 static irqreturn_t
1079 e100nw_interrupt(int irq, void *dev_id, struct pt_regs * regs)
1080 {
1081         struct net_device *dev = (struct net_device *)dev_id;
1082         struct net_local *np = (struct net_local *)dev->priv;
1083         unsigned long irqbits = *R_IRQ_MASK0_RD;
1084
1085         /* check for underrun irq */
1086         if (irqbits & IO_STATE(R_IRQ_MASK0_RD, underrun, active)) { 
1087                 *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
1088                 np->stats.tx_errors++;
1089                 D(printk("ethernet receiver underrun!\n"));
1090         }
1091
1092         /* check for overrun irq */
1093         if (irqbits & IO_STATE(R_IRQ_MASK0_RD, overrun, active)) { 
1094                 update_rx_stats(&np->stats); /* this will ack the irq */
1095                 D(printk("ethernet receiver overrun!\n"));
1096         }
1097         /* check for excessive collision irq */
1098         if (irqbits & IO_STATE(R_IRQ_MASK0_RD, excessive_col, active)) { 
1099                 *R_NETWORK_TR_CTRL = IO_STATE(R_NETWORK_TR_CTRL, clr_error, clr);
1100                 np->stats.tx_errors++;
1101                 D(printk("ethernet excessive collisions!\n"));
1102         }
1103         return IRQ_HANDLED;
1104 }
1105
1106 /* We have a good packet(s), get it/them out of the buffers. */
1107 static void
1108 e100_rx(struct net_device *dev)
1109 {
1110         struct sk_buff *skb;
1111         int length = 0;
1112         struct net_local *np = (struct net_local *)dev->priv;
1113         unsigned char *skb_data_ptr;
1114 #ifdef ETHDEBUG
1115         int i;
1116 #endif
1117
1118         if (!led_active && time_after(jiffies, led_next_time)) {
1119                 /* light the network leds depending on the current speed. */
1120                 e100_set_network_leds(NETWORK_ACTIVITY);
1121
1122                 /* Set the earliest time we may clear the LED */
1123                 led_next_time = jiffies + NET_FLASH_TIME;
1124                 led_active = 1;
1125                 mod_timer(&clear_led_timer, jiffies + HZ/10);
1126         }
1127
1128         length = myNextRxDesc->descr.hw_len - 4;
1129         ((struct net_local *)dev->priv)->stats.rx_bytes += length;
1130
1131 #ifdef ETHDEBUG
1132         printk("Got a packet of length %d:\n", length);
1133         /* dump the first bytes in the packet */
1134         skb_data_ptr = (unsigned char *)phys_to_virt(myNextRxDesc->descr.buf);
1135         for (i = 0; i < 8; i++) {
1136                 printk("%d: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n", i * 8,
1137                        skb_data_ptr[0],skb_data_ptr[1],skb_data_ptr[2],skb_data_ptr[3],
1138                        skb_data_ptr[4],skb_data_ptr[5],skb_data_ptr[6],skb_data_ptr[7]);
1139                 skb_data_ptr += 8;
1140         }
1141 #endif
1142
1143         if (length < RX_COPYBREAK) {
1144                 /* Small packet, copy data */
1145                 skb = dev_alloc_skb(length - ETHER_HEAD_LEN);
1146                 if (!skb) {
1147                         np->stats.rx_errors++;
1148                         printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1149                         return;
1150                 }
1151
1152                 skb_put(skb, length - ETHER_HEAD_LEN);        /* allocate room for the packet body */
1153                 skb_data_ptr = skb_push(skb, ETHER_HEAD_LEN); /* allocate room for the header */
1154
1155 #ifdef ETHDEBUG
1156                 printk("head = 0x%x, data = 0x%x, tail = 0x%x, end = 0x%x\n",
1157                   skb->head, skb->data, skb->tail, skb->end);
1158                 printk("copying packet to 0x%x.\n", skb_data_ptr);
1159 #endif
1160           
1161                 memcpy(skb_data_ptr, phys_to_virt(myNextRxDesc->descr.buf), length);
1162         }
1163         else {
1164                 /* Large packet, send directly to upper layers and allocate new 
1165                  * memory (aligned to cache line boundary to avoid bug).
1166                  * Before sending the skb to upper layers we must make sure that 
1167                  * skb->data points to the aligned start of the packet. 
1168                  */
1169                 int align;  
1170                 struct sk_buff *new_skb = dev_alloc_skb(MAX_MEDIA_DATA_SIZE + 2 * L1_CACHE_BYTES);
1171                 if (!new_skb) {
1172                         np->stats.rx_errors++;
1173                         printk(KERN_NOTICE "%s: Memory squeeze, dropping packet.\n", dev->name);
1174                         return;
1175                 }
1176                 skb = myNextRxDesc->skb;
1177                 align = (int)phys_to_virt(myNextRxDesc->descr.buf) - (int)skb->data;    
1178                 skb_put(skb, length + align); 
1179                 skb_pull(skb, align); /* Remove alignment bytes */
1180                 myNextRxDesc->skb = new_skb;
1181                 myNextRxDesc->descr.buf = L1_CACHE_ALIGN(virt_to_phys(myNextRxDesc->skb->data));
1182         }
1183
1184         skb->dev = dev;
1185         skb->protocol = eth_type_trans(skb, dev);
1186
1187         /* Send the packet to the upper layers */
1188         netif_rx(skb);
1189
1190         /* Prepare for next packet */
1191         myNextRxDesc->descr.status = 0;
1192         myPrevRxDesc = myNextRxDesc;
1193         myNextRxDesc = phys_to_virt(myNextRxDesc->descr.next);
1194
1195         rx_queue_len++;
1196
1197         /* Check if descriptors should be returned */
1198         if (rx_queue_len == RX_QUEUE_THRESHOLD) {
1199                 flush_etrax_cache();
1200                 myPrevRxDesc->descr.ctrl |= d_eol;
1201                 myLastRxDesc->descr.ctrl &= ~d_eol;
1202                 myLastRxDesc = myPrevRxDesc;
1203                 rx_queue_len = 0;
1204         }
1205 }
1206
1207 /* The inverse routine to net_open(). */
1208 static int
1209 e100_close(struct net_device *dev)
1210 {
1211         struct net_local *np = (struct net_local *)dev->priv;
1212
1213         printk("Closing %s.\n", dev->name);
1214
1215         netif_stop_queue(dev);
1216
1217         *R_NETWORK_GEN_CONFIG =
1218                 IO_STATE(R_NETWORK_GEN_CONFIG, phy,    mii_clk) |
1219                 IO_STATE(R_NETWORK_GEN_CONFIG, enable, off);
1220         
1221         *R_IRQ_MASK0_CLR =
1222                 IO_STATE(R_IRQ_MASK0_CLR, overrun, clr) |
1223                 IO_STATE(R_IRQ_MASK0_CLR, underrun, clr) |
1224                 IO_STATE(R_IRQ_MASK0_CLR, excessive_col, clr);
1225         
1226         *R_IRQ_MASK2_CLR =
1227                 IO_STATE(R_IRQ_MASK2_CLR, dma0_descr, clr) |
1228                 IO_STATE(R_IRQ_MASK2_CLR, dma0_eop, clr) |
1229                 IO_STATE(R_IRQ_MASK2_CLR, dma1_descr, clr) |
1230                 IO_STATE(R_IRQ_MASK2_CLR, dma1_eop, clr);
1231
1232         /* Stop the receiver and the transmitter */
1233
1234         RESET_DMA(NETWORK_TX_DMA_NBR);
1235         RESET_DMA(NETWORK_RX_DMA_NBR);
1236
1237         /* Flush the Tx and disable Rx here. */
1238
1239         free_irq(NETWORK_DMA_RX_IRQ_NBR, (void *)dev);
1240         free_irq(NETWORK_DMA_TX_IRQ_NBR, (void *)dev);
1241         free_irq(NETWORK_STATUS_IRQ_NBR, (void *)dev);
1242
1243         /* Update the statistics here. */
1244
1245         update_rx_stats(&np->stats);
1246         update_tx_stats(&np->stats);
1247
1248         return 0;
1249 }
1250
1251 static int
1252 e100_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1253 {
1254         struct mii_ioctl_data *data = (struct mii_ioctl_data *)&ifr->ifr_data;
1255         struct net_local *np = (struct net_local *)dev->priv;
1256
1257         spin_lock(&np->lock); /* Preempt protection */
1258         switch (cmd) {
1259                 case SIOCETHTOOL:
1260                         return e100_ethtool_ioctl(dev,ifr);
1261                 case SIOCGMIIPHY: /* Get PHY address */
1262                         data->phy_id = MDIO_PHYS_ADDR;
1263                         break;
1264                 case SIOCGMIIREG: /* Read MII register */
1265                         data->val_out = e100_get_mdio_reg(data->reg_num);
1266                         break;
1267                 case SIOCSMIIREG: /* Write MII register */
1268                         e100_set_mdio_reg(data->reg_num, data->val_in);
1269                         break;
1270                 /* The ioctls below should be considered obsolete but are */
1271                 /* still present for compatability with old scripts/apps  */    
1272                 case SET_ETH_SPEED_10:                  /* 10 Mbps */
1273                         e100_set_speed(10);
1274                         break;
1275                 case SET_ETH_SPEED_100:                /* 100 Mbps */
1276                         e100_set_speed(100);
1277                         break;
1278                 case SET_ETH_SPEED_AUTO:              /* Auto negotiate speed */
1279                         e100_set_speed(0);
1280                         break;
1281                 case SET_ETH_DUPLEX_HALF:              /* Hhalf duplex. */
1282                         e100_set_duplex(half);
1283                         break;
1284                 case SET_ETH_DUPLEX_FULL:              /* Full duplex. */
1285                         e100_set_duplex(full);
1286                         break;
1287                 case SET_ETH_DUPLEX_AUTO:             /* Autonegotiate duplex*/
1288                         e100_set_duplex(autoneg);
1289                         break;
1290                 default:
1291                         return -EINVAL;
1292         }
1293         spin_unlock(&np->lock);
1294         return 0;
1295 }
1296
1297 static int
1298 e100_ethtool_ioctl(struct net_device *dev, struct ifreq *ifr)
1299 {
1300         struct ethtool_cmd ecmd;
1301
1302         if (copy_from_user(&ecmd, ifr->ifr_data, sizeof (ecmd)))
1303                 return -EFAULT;
1304
1305         switch (ecmd.cmd) {
1306                 case ETHTOOL_GSET:
1307                 {
1308                         memset((void *) &ecmd, 0, sizeof (ecmd));
1309                         ecmd.supported = 
1310                           SUPPORTED_Autoneg | SUPPORTED_TP | SUPPORTED_MII |
1311                           SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | 
1312                           SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full;
1313                         ecmd.port = PORT_TP;
1314                         ecmd.transceiver = XCVR_EXTERNAL;
1315                         ecmd.phy_address = MDIO_PHYS_ADDR;
1316                         ecmd.speed = current_speed;
1317                         ecmd.duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
1318                         ecmd.advertising = ADVERTISED_TP;
1319                         if (current_duplex == autoneg && current_speed_selection == 0)
1320                                 ecmd.advertising = ADVERTISED_Autoneg;
1321                         else {
1322                                 ecmd.advertising |= 
1323                                   ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full |
1324                                   ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full;
1325                                 if (current_speed_selection == 10)
1326                                         ecmd.advertising &= ~(ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full);
1327                                 else if (current_speed_selection == 100)
1328                                         ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full);
1329                                 if (current_duplex == half)
1330                                         ecmd.advertising &= ~(ADVERTISED_10baseT_Full | ADVERTISED_100baseT_Full);
1331                                 else if (current_duplex == full)
1332                                         ecmd.advertising &= ~(ADVERTISED_10baseT_Half | ADVERTISED_100baseT_Half);
1333                         }
1334                         ecmd.autoneg = AUTONEG_ENABLE;
1335                         if (copy_to_user(ifr->ifr_data, &ecmd, sizeof (ecmd)))
1336                                 return -EFAULT;
1337                 }
1338                 break;
1339                 case ETHTOOL_SSET:
1340                 {
1341                         if (!capable(CAP_NET_ADMIN)) {
1342                                 return -EPERM;
1343                         }
1344                         if (ecmd.autoneg == AUTONEG_ENABLE) {
1345                                 e100_set_duplex(autoneg);
1346                                 e100_set_speed(0);
1347                         } else {
1348                                 e100_set_duplex(ecmd.duplex == DUPLEX_HALF ? half : full);
1349                                 e100_set_speed(ecmd.speed == SPEED_10 ? 10: 100);
1350                         }
1351                 }
1352                 break;
1353                 case ETHTOOL_GDRVINFO:
1354                 {
1355                         struct ethtool_drvinfo info;
1356                         memset((void *) &info, 0, sizeof (info));
1357                         strncpy(info.driver, "ETRAX 100LX", sizeof(info.driver) - 1);
1358                         strncpy(info.version, "$Revision: 1.17 $", sizeof(info.version) - 1);
1359                         strncpy(info.fw_version, "N/A", sizeof(info.fw_version) - 1);
1360                         strncpy(info.bus_info, "N/A", sizeof(info.bus_info) - 1);
1361                         info.regdump_len = 0;
1362                         info.eedump_len = 0;
1363                         info.testinfo_len = 0;
1364                         if (copy_to_user(ifr->ifr_data, &info, sizeof (info)))
1365                                 return -EFAULT;
1366                 }
1367                 break;
1368                 case ETHTOOL_NWAY_RST:
1369                         if (current_duplex == autoneg && current_speed_selection == 0)
1370                                 e100_negotiate();
1371                 break;
1372                 default:
1373                         return -EOPNOTSUPP;
1374                 break;
1375         }
1376         return 0;
1377 }
1378
1379 static int
1380 e100_set_config(struct net_device *dev, struct ifmap *map)
1381 {
1382         struct net_local *np = (struct net_local *)dev->priv;
1383         spin_lock(&np->lock); /* Preempt protection */
1384         
1385         switch(map->port) {
1386                 case IF_PORT_UNKNOWN:
1387                         /* Use autoneg */
1388                         e100_set_speed(0);
1389                         e100_set_duplex(autoneg);
1390                         break;
1391                 case IF_PORT_10BASET:
1392                         e100_set_speed(10);
1393                         e100_set_duplex(autoneg);
1394                         break;
1395                 case IF_PORT_100BASET:
1396                 case IF_PORT_100BASETX:
1397                         e100_set_speed(100);
1398                         e100_set_duplex(autoneg);
1399                         break;
1400                 case IF_PORT_100BASEFX:
1401                 case IF_PORT_10BASE2:
1402                 case IF_PORT_AUI:
1403                         spin_unlock(&np->lock);
1404                         return -EOPNOTSUPP;
1405                         break;
1406                 default:
1407                         printk(KERN_ERR "%s: Invalid media selected", dev->name);
1408                         spin_unlock(&np->lock);
1409                         return -EINVAL;
1410         }
1411         spin_unlock(&np->lock);
1412         return 0;
1413 }
1414
1415 static void
1416 update_rx_stats(struct net_device_stats *es)
1417 {
1418         unsigned long r = *R_REC_COUNTERS;
1419         /* update stats relevant to reception errors */
1420         es->rx_fifo_errors += IO_EXTRACT(R_REC_COUNTERS, congestion, r);
1421         es->rx_crc_errors += IO_EXTRACT(R_REC_COUNTERS, crc_error, r);
1422         es->rx_frame_errors += IO_EXTRACT(R_REC_COUNTERS, alignment_error, r);
1423         es->rx_length_errors += IO_EXTRACT(R_REC_COUNTERS, oversize, r);
1424 }
1425
1426 static void
1427 update_tx_stats(struct net_device_stats *es)
1428 {
1429         unsigned long r = *R_TR_COUNTERS;
1430         /* update stats relevant to transmission errors */
1431         es->collisions +=
1432                 IO_EXTRACT(R_TR_COUNTERS, single_col, r) +
1433                 IO_EXTRACT(R_TR_COUNTERS, multiple_col, r);
1434         es->tx_errors += IO_EXTRACT(R_TR_COUNTERS, deferred, r);
1435 }
1436
1437 /*
1438  * Get the current statistics.
1439  * This may be called with the card open or closed.
1440  */
1441 static struct net_device_stats *
1442 e100_get_stats(struct net_device *dev)
1443 {
1444         struct net_local *lp = (struct net_local *)dev->priv;
1445         unsigned long flags;
1446         spin_lock_irqsave(&lp->lock, flags);
1447
1448         update_rx_stats(&lp->stats);
1449         update_tx_stats(&lp->stats);
1450         
1451         spin_unlock_irqrestore(&lp->lock, flags);
1452         return &lp->stats;
1453 }
1454
1455 /*
1456  * Set or clear the multicast filter for this adaptor.
1457  * num_addrs == -1      Promiscuous mode, receive all packets
1458  * num_addrs == 0       Normal mode, clear multicast list
1459  * num_addrs > 0        Multicast mode, receive normal and MC packets,
1460  *                      and do best-effort filtering.
1461  */
1462 static void
1463 set_multicast_list(struct net_device *dev)
1464 {
1465         struct net_local *lp = (struct net_local *)dev->priv;
1466         int num_addr = dev->mc_count;
1467         unsigned long int lo_bits;
1468         unsigned long int hi_bits;
1469         spin_lock(&lp->lock);
1470         if (dev->flags & IFF_PROMISC)
1471         {
1472                 /* promiscuous mode */
1473                 lo_bits = 0xfffffffful;
1474                 hi_bits = 0xfffffffful;
1475
1476                 /* Enable individual receive */
1477                 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, receive);
1478                 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1479         } else if (dev->flags & IFF_ALLMULTI) {
1480                 /* enable all multicasts */
1481                 lo_bits = 0xfffffffful;
1482                 hi_bits = 0xfffffffful;
1483
1484                 /* Disable individual receive */
1485                 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1486                 *R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
1487         } else if (num_addr == 0) {
1488                 /* Normal, clear the mc list */
1489                 lo_bits = 0x00000000ul;
1490                 hi_bits = 0x00000000ul;
1491
1492                 /* Disable individual receive */
1493                 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1494                 *R_NETWORK_REC_CONFIG =  network_rec_config_shadow;
1495         } else {
1496                 /* MC mode, receive normal and MC packets */
1497                 char hash_ix;
1498                 struct dev_mc_list *dmi = dev->mc_list;
1499                 int i;
1500                 char *baddr;
1501                 lo_bits = 0x00000000ul;
1502                 hi_bits = 0x00000000ul;
1503                 for (i=0; i<num_addr; i++) {
1504                         /* Calculate the hash index for the GA registers */
1505                         
1506                         hash_ix = 0;
1507                         baddr = dmi->dmi_addr;
1508                         hash_ix ^= (*baddr) & 0x3f;
1509                         hash_ix ^= ((*baddr) >> 6) & 0x03;
1510                         ++baddr;
1511                         hash_ix ^= ((*baddr) << 2) & 0x03c;
1512                         hash_ix ^= ((*baddr) >> 4) & 0xf;
1513                         ++baddr;
1514                         hash_ix ^= ((*baddr) << 4) & 0x30;
1515                         hash_ix ^= ((*baddr) >> 2) & 0x3f;
1516                         ++baddr;
1517                         hash_ix ^= (*baddr) & 0x3f;
1518                         hash_ix ^= ((*baddr) >> 6) & 0x03;
1519                         ++baddr;
1520                         hash_ix ^= ((*baddr) << 2) & 0x03c;
1521                         hash_ix ^= ((*baddr) >> 4) & 0xf;
1522                         ++baddr;
1523                         hash_ix ^= ((*baddr) << 4) & 0x30;
1524                         hash_ix ^= ((*baddr) >> 2) & 0x3f;
1525                         
1526                         hash_ix &= 0x3f;
1527                         
1528                         if (hash_ix >= 32) {
1529                                 hi_bits |= (1 << (hash_ix-32));
1530                         }
1531                         else {
1532                                 lo_bits |= (1 << hash_ix);
1533                         }
1534                         dmi = dmi->next;
1535                 }
1536                 /* Disable individual receive */
1537                 SETS(network_rec_config_shadow, R_NETWORK_REC_CONFIG, individual, discard);
1538                 *R_NETWORK_REC_CONFIG = network_rec_config_shadow;
1539         }
1540         *R_NETWORK_GA_0 = lo_bits;
1541         *R_NETWORK_GA_1 = hi_bits;
1542         spin_unlock(&lp->lock);
1543 }
1544
1545 void
1546 e100_hardware_send_packet(char *buf, int length)
1547 {
1548         D(printk("e100 send pack, buf 0x%x len %d\n", buf, length));
1549
1550         if (!led_active && time_after(jiffies, led_next_time)) {
1551                 /* light the network leds depending on the current speed. */
1552                 e100_set_network_leds(NETWORK_ACTIVITY);
1553
1554                 /* Set the earliest time we may clear the LED */
1555                 led_next_time = jiffies + NET_FLASH_TIME;
1556                 led_active = 1;
1557                 mod_timer(&clear_led_timer, jiffies + HZ/10);
1558         }
1559
1560         /* configure the tx dma descriptor */
1561         myNextTxDesc->descr.sw_len = length;
1562         myNextTxDesc->descr.ctrl = d_eop | d_eol | d_wait;
1563         myNextTxDesc->descr.buf = virt_to_phys(buf);
1564
1565         /* Move end of list */
1566         myLastTxDesc->descr.ctrl &= ~d_eol;
1567         myLastTxDesc = myNextTxDesc;
1568
1569         /* Restart DMA channel */
1570         *R_DMA_CH0_CMD = IO_STATE(R_DMA_CH0_CMD, cmd, restart);
1571 }
1572
1573 static void
1574 e100_clear_network_leds(unsigned long dummy)
1575 {
1576         if (led_active && time_after(jiffies, led_next_time)) {
1577                 e100_set_network_leds(NO_NETWORK_ACTIVITY);
1578
1579                 /* Set the earliest time we may set the LED */
1580                 led_next_time = jiffies + NET_FLASH_PAUSE;
1581                 led_active = 0;
1582         }
1583 }
1584
1585 static void
1586 e100_set_network_leds(int active)
1587 {
1588 #if defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK)
1589         int light_leds = (active == NO_NETWORK_ACTIVITY);
1590 #elif defined(CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY)
1591         int light_leds = (active == NETWORK_ACTIVITY);
1592 #else
1593 #error "Define either CONFIG_ETRAX_NETWORK_LED_ON_WHEN_LINK or CONFIG_ETRAX_NETWORK_LED_ON_WHEN_ACTIVITY"
1594 #endif
1595
1596         if (!current_speed) {
1597                 /* Make LED red, link is down */
1598                 LED_NETWORK_SET(LED_OFF);
1599         }
1600         else if (light_leds) {
1601                 if (current_speed == 10) {
1602                         LED_NETWORK_SET(LED_ORANGE);
1603                 } else {
1604                         LED_NETWORK_SET(LED_GREEN);
1605                 }
1606         }
1607         else {
1608                 LED_NETWORK_SET(LED_OFF);
1609         }
1610 }
1611
1612 static int
1613 etrax_init_module(void)
1614 {
1615         return etrax_ethernet_init();
1616 }
1617
1618 module_init(etrax_init_module);