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