File: /usr/src/linux/drivers/net/atarilance.c
1 /* atarilance.c: Ethernet driver for VME Lance cards on the Atari */
2 /*
3 Written 1995/96 by Roman Hodek (Roman.Hodek@informatik.uni-erlangen.de)
4
5 This software may be used and distributed according to the terms
6 of the GNU General Public License, incorporated herein by reference.
7
8 This drivers was written with the following sources of reference:
9 - The driver for the Riebl Lance card by the TU Vienna.
10 - The modified TUW driver for PAM's VME cards
11 - The PC-Linux driver for Lance cards (but this is for bus master
12 cards, not the shared memory ones)
13 - The Amiga Ariadne driver
14
15 v1.0: (in 1.2.13pl4/0.9.13)
16 Initial version
17 v1.1: (in 1.2.13pl5)
18 more comments
19 deleted some debugging stuff
20 optimized register access (keep AREG pointing to CSR0)
21 following AMD, CSR0_STRT should be set only after IDON is detected
22 use memcpy() for data transfers, that also employs long word moves
23 better probe procedure for 24-bit systems
24 non-VME-RieblCards need extra delays in memcpy
25 must also do write test, since 0xfxe00000 may hit ROM
26 use 8/32 tx/rx buffers, which should give better NFS performance;
27 this is made possible by shifting the last packet buffer after the
28 RieblCard reserved area
29 v1.2: (in 1.2.13pl8)
30 again fixed probing for the Falcon; 0xfe01000 hits phys. 0x00010000
31 and thus RAM, in case of no Lance found all memory contents have to
32 be restored!
33 Now possible to compile as module.
34 v1.3: 03/30/96 Jes Sorensen, Roman (in 1.3)
35 Several little 1.3 adaptions
36 When the lance is stopped it jumps back into little-endian
37 mode. It is therefore necessary to put it back where it
38 belongs, in big endian mode, in order to make things work.
39 This might be the reason why multicast-mode didn't work
40 before, but I'm not able to test it as I only got an Amiga
41 (we had similar problems with the A2065 driver).
42
43 */
44
45 static char version[] = "atarilance.c: v1.3 04/04/96 "
46 "Roman.Hodek@informatik.uni-erlangen.de\n";
47
48 #include <linux/module.h>
49
50 #include <linux/stddef.h>
51 #include <linux/kernel.h>
52 #include <linux/sched.h>
53 #include <linux/string.h>
54 #include <linux/ptrace.h>
55 #include <linux/errno.h>
56 #include <linux/slab.h>
57 #include <linux/interrupt.h>
58 #include <linux/init.h>
59
60 #include <asm/setup.h>
61 #include <asm/irq.h>
62 #include <asm/atarihw.h>
63 #include <asm/atariints.h>
64 #include <asm/bitops.h>
65 #include <asm/io.h>
66
67 #include <linux/netdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/skbuff.h>
70
71 /* Debug level:
72 * 0 = silent, print only serious errors
73 * 1 = normal, print error messages
74 * 2 = debug, print debug infos
75 * 3 = debug, print even more debug infos (packet data)
76 */
77
78 #define LANCE_DEBUG 1
79
80 #ifdef LANCE_DEBUG
81 static int lance_debug = LANCE_DEBUG;
82 #else
83 static int lance_debug = 1;
84 #endif
85 MODULE_PARM(lance_debug, "i");
86 MODULE_PARM_DESC(lance_debug, "atarilance debug level (0-3)");
87
88 /* Print debug messages on probing? */
89 #undef LANCE_DEBUG_PROBE
90
91 #define DPRINTK(n,a) \
92 do { \
93 if (lance_debug >= n) \
94 printk a; \
95 } while( 0 )
96
97 #ifdef LANCE_DEBUG_PROBE
98 # define PROBE_PRINT(a) printk a
99 #else
100 # define PROBE_PRINT(a)
101 #endif
102
103 /* These define the number of Rx and Tx buffers as log2. (Only powers
104 * of two are valid)
105 * Much more rx buffers (32) are reserved than tx buffers (8), since receiving
106 * is more time critical then sending and packets may have to remain in the
107 * board's memory when main memory is low.
108 */
109
110 #define TX_LOG_RING_SIZE 3
111 #define RX_LOG_RING_SIZE 5
112
113 /* These are the derived values */
114
115 #define TX_RING_SIZE (1 << TX_LOG_RING_SIZE)
116 #define TX_RING_LEN_BITS (TX_LOG_RING_SIZE << 5)
117 #define TX_RING_MOD_MASK (TX_RING_SIZE - 1)
118
119 #define RX_RING_SIZE (1 << RX_LOG_RING_SIZE)
120 #define RX_RING_LEN_BITS (RX_LOG_RING_SIZE << 5)
121 #define RX_RING_MOD_MASK (RX_RING_SIZE - 1)
122
123 #define TX_TIMEOUT 20
124
125 /* The LANCE Rx and Tx ring descriptors. */
126 struct lance_rx_head {
127 unsigned short base; /* Low word of base addr */
128 volatile unsigned char flag;
129 unsigned char base_hi; /* High word of base addr (unused) */
130 short buf_length; /* This length is 2s complement! */
131 volatile short msg_length; /* This length is "normal". */
132 };
133
134 struct lance_tx_head {
135 unsigned short base; /* Low word of base addr */
136 volatile unsigned char flag;
137 unsigned char base_hi; /* High word of base addr (unused) */
138 short length; /* Length is 2s complement! */
139 volatile short misc;
140 };
141
142 struct ringdesc {
143 unsigned short adr_lo; /* Low 16 bits of address */
144 unsigned char len; /* Length bits */
145 unsigned char adr_hi; /* High 8 bits of address (unused) */
146 };
147
148 /* The LANCE initialization block, described in databook. */
149 struct lance_init_block {
150 unsigned short mode; /* Pre-set mode */
151 unsigned char hwaddr[6]; /* Physical ethernet address */
152 unsigned filter[2]; /* Multicast filter (unused). */
153 /* Receive and transmit ring base, along with length bits. */
154 struct ringdesc rx_ring;
155 struct ringdesc tx_ring;
156 };
157
158 /* The whole layout of the Lance shared memory */
159 struct lance_memory {
160 struct lance_init_block init;
161 struct lance_tx_head tx_head[TX_RING_SIZE];
162 struct lance_rx_head rx_head[RX_RING_SIZE];
163 char packet_area[0]; /* packet data follow after the
164 * init block and the ring
165 * descriptors and are located
166 * at runtime */
167 };
168
169 /* RieblCard specifics:
170 * The original TOS driver for these cards reserves the area from offset
171 * 0xee70 to 0xeebb for storing configuration data. Of interest to us is the
172 * Ethernet address there, and the magic for verifying the data's validity.
173 * The reserved area isn't touch by packet buffers. Furthermore, offset 0xfffe
174 * is reserved for the interrupt vector number.
175 */
176 #define RIEBL_RSVD_START 0xee70
177 #define RIEBL_RSVD_END 0xeec0
178 #define RIEBL_MAGIC 0x09051990
179 #define RIEBL_MAGIC_ADDR ((unsigned long *)(((char *)MEM) + 0xee8a))
180 #define RIEBL_HWADDR_ADDR ((unsigned char *)(((char *)MEM) + 0xee8e))
181 #define RIEBL_IVEC_ADDR ((unsigned short *)(((char *)MEM) + 0xfffe))
182
183 /* This is a default address for the old RieblCards without a battery
184 * that have no ethernet address at boot time. 00:00:36:04 is the
185 * prefix for Riebl cards, the 00:00 at the end is arbitrary.
186 */
187
188 static unsigned char OldRieblDefHwaddr[6] = {
189 0x00, 0x00, 0x36, 0x04, 0x00, 0x00
190 };
191
192
193 /* I/O registers of the Lance chip */
194
195 struct lance_ioreg {
196 /* base+0x0 */ volatile unsigned short data;
197 /* base+0x2 */ volatile unsigned short addr;
198 unsigned char _dummy1[3];
199 /* base+0x7 */ volatile unsigned char ivec;
200 unsigned char _dummy2[5];
201 /* base+0xd */ volatile unsigned char eeprom;
202 unsigned char _dummy3;
203 /* base+0xf */ volatile unsigned char mem;
204 };
205
206 /* Types of boards this driver supports */
207
208 enum lance_type {
209 OLD_RIEBL, /* old Riebl card without battery */
210 NEW_RIEBL, /* new Riebl card with battery */
211 PAM_CARD /* PAM card with EEPROM */
212 };
213
214 static char *lance_names[] = {
215 "Riebl-Card (without battery)",
216 "Riebl-Card (with battery)",
217 "PAM intern card"
218 };
219
220 /* The driver's private device structure */
221
222 struct lance_private {
223 enum lance_type cardtype;
224 struct lance_ioreg *iobase;
225 struct lance_memory *mem;
226 int cur_rx, cur_tx; /* The next free ring entry */
227 int dirty_tx; /* Ring entries to be freed. */
228 /* copy function */
229 void *(*memcpy_f)( void *, const void *, size_t );
230 struct net_device_stats stats;
231 /* This must be long for set_bit() */
232 long tx_full;
233 spinlock_t devlock;
234 };
235
236 /* I/O register access macros */
237
238 #define MEM lp->mem
239 #define DREG IO->data
240 #define AREG IO->addr
241 #define REGA(a) ( AREG = (a), DREG )
242
243 /* Definitions for packet buffer access: */
244 #define PKT_BUF_SZ 1544
245 /* Get the address of a packet buffer corresponding to a given buffer head */
246 #define PKTBUF_ADDR(head) (((unsigned char *)(MEM)) + (head)->base)
247
248 /* Possible memory/IO addresses for probing */
249
250 struct lance_addr {
251 unsigned long memaddr;
252 unsigned long ioaddr;
253 int slow_flag;
254 } lance_addr_list[] = {
255 { 0xfe010000, 0xfe00fff0, 0 }, /* RieblCard VME in TT */
256 { 0xffc10000, 0xffc0fff0, 0 }, /* RieblCard VME in MegaSTE
257 (highest byte stripped) */
258 { 0xffe00000, 0xffff7000, 1 }, /* RieblCard in ST
259 (highest byte stripped) */
260 { 0xffd00000, 0xffff7000, 1 }, /* RieblCard in ST with hw modif. to
261 avoid conflict with ROM
262 (highest byte stripped) */
263 { 0xffcf0000, 0xffcffff0, 0 }, /* PAMCard VME in TT and MSTE
264 (highest byte stripped) */
265 { 0xfecf0000, 0xfecffff0, 0 }, /* Rhotron's PAMCard VME in TT and MSTE
266 (highest byte stripped) */
267 };
268
269 #define N_LANCE_ADDR (sizeof(lance_addr_list)/sizeof(*lance_addr_list))
270
271
272 /* Definitions for the Lance */
273
274 /* tx_head flags */
275 #define TMD1_ENP 0x01 /* end of packet */
276 #define TMD1_STP 0x02 /* start of packet */
277 #define TMD1_DEF 0x04 /* deferred */
278 #define TMD1_ONE 0x08 /* one retry needed */
279 #define TMD1_MORE 0x10 /* more than one retry needed */
280 #define TMD1_ERR 0x40 /* error summary */
281 #define TMD1_OWN 0x80 /* ownership (set: chip owns) */
282
283 #define TMD1_OWN_CHIP TMD1_OWN
284 #define TMD1_OWN_HOST 0
285
286 /* tx_head misc field */
287 #define TMD3_TDR 0x03FF /* Time Domain Reflectometry counter */
288 #define TMD3_RTRY 0x0400 /* failed after 16 retries */
289 #define TMD3_LCAR 0x0800 /* carrier lost */
290 #define TMD3_LCOL 0x1000 /* late collision */
291 #define TMD3_UFLO 0x4000 /* underflow (late memory) */
292 #define TMD3_BUFF 0x8000 /* buffering error (no ENP) */
293
294 /* rx_head flags */
295 #define RMD1_ENP 0x01 /* end of packet */
296 #define RMD1_STP 0x02 /* start of packet */
297 #define RMD1_BUFF 0x04 /* buffer error */
298 #define RMD1_CRC 0x08 /* CRC error */
299 #define RMD1_OFLO 0x10 /* overflow */
300 #define RMD1_FRAM 0x20 /* framing error */
301 #define RMD1_ERR 0x40 /* error summary */
302 #define RMD1_OWN 0x80 /* ownership (set: ship owns) */
303
304 #define RMD1_OWN_CHIP RMD1_OWN
305 #define RMD1_OWN_HOST 0
306
307 /* register names */
308 #define CSR0 0 /* mode/status */
309 #define CSR1 1 /* init block addr (low) */
310 #define CSR2 2 /* init block addr (high) */
311 #define CSR3 3 /* misc */
312 #define CSR8 8 /* address filter */
313 #define CSR15 15 /* promiscuous mode */
314
315 /* CSR0 */
316 /* (R=readable, W=writeable, S=set on write, C=clear on write) */
317 #define CSR0_INIT 0x0001 /* initialize (RS) */
318 #define CSR0_STRT 0x0002 /* start (RS) */
319 #define CSR0_STOP 0x0004 /* stop (RS) */
320 #define CSR0_TDMD 0x0008 /* transmit demand (RS) */
321 #define CSR0_TXON 0x0010 /* transmitter on (R) */
322 #define CSR0_RXON 0x0020 /* receiver on (R) */
323 #define CSR0_INEA 0x0040 /* interrupt enable (RW) */
324 #define CSR0_INTR 0x0080 /* interrupt active (R) */
325 #define CSR0_IDON 0x0100 /* initialization done (RC) */
326 #define CSR0_TINT 0x0200 /* transmitter interrupt (RC) */
327 #define CSR0_RINT 0x0400 /* receiver interrupt (RC) */
328 #define CSR0_MERR 0x0800 /* memory error (RC) */
329 #define CSR0_MISS 0x1000 /* missed frame (RC) */
330 #define CSR0_CERR 0x2000 /* carrier error (no heartbeat :-) (RC) */
331 #define CSR0_BABL 0x4000 /* babble: tx-ed too many bits (RC) */
332 #define CSR0_ERR 0x8000 /* error (RC) */
333
334 /* CSR3 */
335 #define CSR3_BCON 0x0001 /* byte control */
336 #define CSR3_ACON 0x0002 /* ALE control */
337 #define CSR3_BSWP 0x0004 /* byte swap (1=big endian) */
338
339
340
341 /***************************** Prototypes *****************************/
342
343 static int addr_accessible( volatile void *regp, int wordflag, int
344 writeflag );
345 static unsigned long lance_probe1( struct net_device *dev, struct lance_addr
346 *init_rec );
347 static int lance_open( struct net_device *dev );
348 static void lance_init_ring( struct net_device *dev );
349 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev );
350 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp );
351 static int lance_rx( struct net_device *dev );
352 static int lance_close( struct net_device *dev );
353 static struct net_device_stats *lance_get_stats( struct net_device *dev );
354 static void set_multicast_list( struct net_device *dev );
355 static int lance_set_mac_address( struct net_device *dev, void *addr );
356 static void lance_tx_timeout (struct net_device *dev);
357
358 /************************* End of Prototypes **************************/
359
360
361
362
363
364 static void *slow_memcpy( void *dst, const void *src, size_t len )
365
366 { char *cto = dst;
367 const char *cfrom = src;
368
369 while( len-- ) {
370 *cto++ = *cfrom++;
371 MFPDELAY();
372 }
373 return( dst );
374 }
375
376
377 int __init atarilance_probe( struct net_device *dev )
378 {
379 int i;
380 static int found;
381
382 SET_MODULE_OWNER(dev);
383
384 if (!MACH_IS_ATARI || found)
385 /* Assume there's only one board possible... That seems true, since
386 * the Riebl/PAM board's address cannot be changed. */
387 return( ENODEV );
388
389 for( i = 0; i < N_LANCE_ADDR; ++i ) {
390 if (lance_probe1( dev, &lance_addr_list[i] )) {
391 found = 1;
392 return( 0 );
393 }
394 }
395
396 return( ENODEV );
397 }
398
399
400 /* Derived from hwreg_present() in atari/config.c: */
401
402 static int __init addr_accessible( volatile void *regp, int wordflag, int writeflag )
403 {
404 int ret;
405 long flags;
406 long *vbr, save_berr;
407
408 save_flags(flags);
409 cli();
410
411 __asm__ __volatile__ ( "movec %/vbr,%0" : "=r" (vbr) : );
412 save_berr = vbr[2];
413
414 __asm__ __volatile__
415 ( "movel %/sp,%/d1\n\t"
416 "movel #Lberr,%2@\n\t"
417 "moveq #0,%0\n\t"
418 "tstl %3\n\t"
419 "bne 1f\n\t"
420 "moveb %1@,%/d0\n\t"
421 "nop \n\t"
422 "bra 2f\n"
423 "1: movew %1@,%/d0\n\t"
424 "nop \n"
425 "2: tstl %4\n\t"
426 "beq 2f\n\t"
427 "tstl %3\n\t"
428 "bne 1f\n\t"
429 "clrb %1@\n\t"
430 "nop \n\t"
431 "moveb %/d0,%1@\n\t"
432 "nop \n\t"
433 "bra 2f\n"
434 "1: clrw %1@\n\t"
435 "nop \n\t"
436 "movew %/d0,%1@\n\t"
437 "nop \n"
438 "2: moveq #1,%0\n"
439 "Lberr: movel %/d1,%/sp"
440 : "=&d" (ret)
441 : "a" (regp), "a" (&vbr[2]), "rm" (wordflag), "rm" (writeflag)
442 : "d0", "d1", "memory"
443 );
444
445 vbr[2] = save_berr;
446 restore_flags(flags);
447
448 return( ret );
449 }
450
451
452 static unsigned long __init lance_probe1( struct net_device *dev,
453 struct lance_addr *init_rec )
454 {
455 volatile unsigned short *memaddr =
456 (volatile unsigned short *)init_rec->memaddr;
457 volatile unsigned short *ioaddr =
458 (volatile unsigned short *)init_rec->ioaddr;
459 struct lance_private *lp;
460 struct lance_ioreg *IO;
461 int i;
462 static int did_version;
463 unsigned short save1, save2;
464
465 PROBE_PRINT(( "Probing for Lance card at mem %#lx io %#lx\n",
466 (long)memaddr, (long)ioaddr ));
467
468 /* Test whether memory readable and writable */
469 PROBE_PRINT(( "lance_probe1: testing memory to be accessible\n" ));
470 if (!addr_accessible( memaddr, 1, 1 )) goto probe_fail;
471
472 /* Written values should come back... */
473 PROBE_PRINT(( "lance_probe1: testing memory to be writable (1)\n" ));
474 save1 = *memaddr;
475 *memaddr = 0x0001;
476 if (*memaddr != 0x0001) goto probe_fail;
477 PROBE_PRINT(( "lance_probe1: testing memory to be writable (2)\n" ));
478 *memaddr = 0x0000;
479 if (*memaddr != 0x0000) goto probe_fail;
480 *memaddr = save1;
481
482 /* First port should be readable and writable */
483 PROBE_PRINT(( "lance_probe1: testing ioport to be accessible\n" ));
484 if (!addr_accessible( ioaddr, 1, 1 )) goto probe_fail;
485
486 /* and written values should be readable */
487 PROBE_PRINT(( "lance_probe1: testing ioport to be writeable\n" ));
488 save2 = ioaddr[1];
489 ioaddr[1] = 0x0001;
490 if (ioaddr[1] != 0x0001) goto probe_fail;
491
492 /* The CSR0_INIT bit should not be readable */
493 PROBE_PRINT(( "lance_probe1: testing CSR0 register function (1)\n" ));
494 save1 = ioaddr[0];
495 ioaddr[1] = CSR0;
496 ioaddr[0] = CSR0_INIT | CSR0_STOP;
497 if (ioaddr[0] != CSR0_STOP) {
498 ioaddr[0] = save1;
499 ioaddr[1] = save2;
500 goto probe_fail;
501 }
502 PROBE_PRINT(( "lance_probe1: testing CSR0 register function (2)\n" ));
503 ioaddr[0] = CSR0_STOP;
504 if (ioaddr[0] != CSR0_STOP) {
505 ioaddr[0] = save1;
506 ioaddr[1] = save2;
507 goto probe_fail;
508 }
509
510 /* Now ok... */
511 PROBE_PRINT(( "lance_probe1: Lance card detected\n" ));
512 goto probe_ok;
513
514 probe_fail:
515 return( 0 );
516
517 probe_ok:
518 init_etherdev( dev, sizeof(struct lance_private) );
519 if (!dev->priv) {
520 dev->priv = kmalloc( sizeof(struct lance_private), GFP_KERNEL );
521 if (!dev->priv)
522 return 0;
523 }
524 lp = (struct lance_private *)dev->priv;
525 MEM = (struct lance_memory *)memaddr;
526 IO = lp->iobase = (struct lance_ioreg *)ioaddr;
527 dev->base_addr = (unsigned long)ioaddr; /* informational only */
528 lp->memcpy_f = init_rec->slow_flag ? slow_memcpy : memcpy;
529
530 REGA( CSR0 ) = CSR0_STOP;
531
532 /* Now test for type: If the eeprom I/O port is readable, it is a
533 * PAM card */
534 if (addr_accessible( &(IO->eeprom), 0, 0 )) {
535 /* Switch back to Ram */
536 i = IO->mem;
537 lp->cardtype = PAM_CARD;
538 }
539 else if (*RIEBL_MAGIC_ADDR == RIEBL_MAGIC) {
540 lp->cardtype = NEW_RIEBL;
541 }
542 else
543 lp->cardtype = OLD_RIEBL;
544
545 if (lp->cardtype == PAM_CARD ||
546 memaddr == (unsigned short *)0xffe00000) {
547 /* PAMs card and Riebl on ST use level 5 autovector */
548 request_irq(IRQ_AUTO_5, lance_interrupt, IRQ_TYPE_PRIO,
549 "PAM/Riebl-ST Ethernet", dev);
550 dev->irq = (unsigned short)IRQ_AUTO_5;
551 }
552 else {
553 /* For VME-RieblCards, request a free VME int;
554 * (This must be unsigned long, since dev->irq is short and the
555 * IRQ_MACHSPEC bit would be cut off...)
556 */
557 unsigned long irq = atari_register_vme_int();
558 if (!irq) {
559 printk( "Lance: request for VME interrupt failed\n" );
560 return( 0 );
561 }
562 request_irq(irq, lance_interrupt, IRQ_TYPE_PRIO,
563 "Riebl-VME Ethernet", dev);
564 dev->irq = irq;
565 }
566
567 printk("%s: %s at io %#lx, mem %#lx, irq %d%s, hwaddr ",
568 dev->name, lance_names[lp->cardtype],
569 (unsigned long)ioaddr,
570 (unsigned long)memaddr,
571 dev->irq,
572 init_rec->slow_flag ? " (slow memcpy)" : "" );
573
574 /* Get the ethernet address */
575 switch( lp->cardtype ) {
576 case OLD_RIEBL:
577 /* No ethernet address! (Set some default address) */
578 memcpy( dev->dev_addr, OldRieblDefHwaddr, 6 );
579 break;
580 case NEW_RIEBL:
581 lp->memcpy_f( dev->dev_addr, RIEBL_HWADDR_ADDR, 6 );
582 break;
583 case PAM_CARD:
584 i = IO->eeprom;
585 for( i = 0; i < 6; ++i )
586 dev->dev_addr[i] =
587 ((((unsigned short *)MEM)[i*2] & 0x0f) << 4) |
588 ((((unsigned short *)MEM)[i*2+1] & 0x0f));
589 i = IO->mem;
590 break;
591 }
592 for( i = 0; i < 6; ++i )
593 printk( "%02x%s", dev->dev_addr[i], (i < 5) ? ":" : "\n" );
594 if (lp->cardtype == OLD_RIEBL) {
595 printk( "%s: Warning: This is a default ethernet address!\n",
596 dev->name );
597 printk( " Use \"ifconfig hw ether ...\" to set the address.\n" );
598 }
599
600 lp->devlock = SPIN_LOCK_UNLOCKED;
601
602 MEM->init.mode = 0x0000; /* Disable Rx and Tx. */
603 for( i = 0; i < 6; i++ )
604 MEM->init.hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
605 MEM->init.filter[0] = 0x00000000;
606 MEM->init.filter[1] = 0x00000000;
607 MEM->init.rx_ring.adr_lo = offsetof( struct lance_memory, rx_head );
608 MEM->init.rx_ring.adr_hi = 0;
609 MEM->init.rx_ring.len = RX_RING_LEN_BITS;
610 MEM->init.tx_ring.adr_lo = offsetof( struct lance_memory, tx_head );
611 MEM->init.tx_ring.adr_hi = 0;
612 MEM->init.tx_ring.len = TX_RING_LEN_BITS;
613
614 if (lp->cardtype == PAM_CARD)
615 IO->ivec = IRQ_SOURCE_TO_VECTOR(dev->irq);
616 else
617 *RIEBL_IVEC_ADDR = IRQ_SOURCE_TO_VECTOR(dev->irq);
618
619 if (did_version++ == 0)
620 DPRINTK( 1, ( version ));
621
622 /* The LANCE-specific entries in the device structure. */
623 dev->open = &lance_open;
624 dev->hard_start_xmit = &lance_start_xmit;
625 dev->stop = &lance_close;
626 dev->get_stats = &lance_get_stats;
627 dev->set_multicast_list = &set_multicast_list;
628 dev->set_mac_address = &lance_set_mac_address;
629
630 /* XXX MSch */
631 dev->tx_timeout = lance_tx_timeout;
632 dev->watchdog_timeo = TX_TIMEOUT;
633
634
635 #if 0
636 dev->start = 0;
637 #endif
638
639 memset( &lp->stats, 0, sizeof(lp->stats) );
640
641 return( 1 );
642 }
643
644
645 static int lance_open( struct net_device *dev )
646
647 { struct lance_private *lp = (struct lance_private *)dev->priv;
648 struct lance_ioreg *IO = lp->iobase;
649 int i;
650
651 DPRINTK( 2, ( "%s: lance_open()\n", dev->name ));
652
653 lance_init_ring(dev);
654 /* Re-initialize the LANCE, and start it when done. */
655
656 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
657 REGA( CSR2 ) = 0;
658 REGA( CSR1 ) = 0;
659 REGA( CSR0 ) = CSR0_INIT;
660 /* From now on, AREG is kept to point to CSR0 */
661
662 i = 1000000;
663 while (--i > 0)
664 if (DREG & CSR0_IDON)
665 break;
666 if (i < 0 || (DREG & CSR0_ERR)) {
667 DPRINTK( 2, ( "lance_open(): opening %s failed, i=%d, csr0=%04x\n",
668 dev->name, i, DREG ));
669 DREG = CSR0_STOP;
670 return( -EIO );
671 }
672 DREG = CSR0_IDON;
673 DREG = CSR0_STRT;
674 DREG = CSR0_INEA;
675
676 netif_start_queue (dev);
677
678 DPRINTK( 2, ( "%s: LANCE is open, csr0 %04x\n", dev->name, DREG ));
679
680 return( 0 );
681 }
682
683
684 /* Initialize the LANCE Rx and Tx rings. */
685
686 static void lance_init_ring( struct net_device *dev )
687
688 { struct lance_private *lp = (struct lance_private *)dev->priv;
689 int i;
690 unsigned offset;
691
692 lp->tx_full = 0;
693 lp->cur_rx = lp->cur_tx = 0;
694 lp->dirty_tx = 0;
695
696 offset = offsetof( struct lance_memory, packet_area );
697
698 /* If the packet buffer at offset 'o' would conflict with the reserved area
699 * of RieblCards, advance it */
700 #define CHECK_OFFSET(o) \
701 do { \
702 if (lp->cardtype == OLD_RIEBL || lp->cardtype == NEW_RIEBL) { \
703 if (((o) < RIEBL_RSVD_START) ? (o)+PKT_BUF_SZ > RIEBL_RSVD_START \
704 : (o) < RIEBL_RSVD_END) \
705 (o) = RIEBL_RSVD_END; \
706 } \
707 } while(0)
708
709 for( i = 0; i < TX_RING_SIZE; i++ ) {
710 CHECK_OFFSET(offset);
711 MEM->tx_head[i].base = offset;
712 MEM->tx_head[i].flag = TMD1_OWN_HOST;
713 MEM->tx_head[i].base_hi = 0;
714 MEM->tx_head[i].length = 0;
715 MEM->tx_head[i].misc = 0;
716 offset += PKT_BUF_SZ;
717 }
718
719 for( i = 0; i < RX_RING_SIZE; i++ ) {
720 CHECK_OFFSET(offset);
721 MEM->rx_head[i].base = offset;
722 MEM->rx_head[i].flag = TMD1_OWN_CHIP;
723 MEM->rx_head[i].base_hi = 0;
724 MEM->rx_head[i].buf_length = -PKT_BUF_SZ;
725 MEM->rx_head[i].msg_length = 0;
726 offset += PKT_BUF_SZ;
727 }
728 }
729
730
731 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
732
733
734 static void lance_tx_timeout (struct net_device *dev)
735 {
736 struct lance_private *lp = (struct lance_private *) dev->priv;
737 struct lance_ioreg *IO = lp->iobase;
738
739 AREG = CSR0;
740 DPRINTK( 1, ( "%s: transmit timed out, status %04x, resetting.\n",
741 dev->name, DREG ));
742 DREG = CSR0_STOP;
743 /*
744 * Always set BSWP after a STOP as STOP puts it back into
745 * little endian mode.
746 */
747 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
748 lp->stats.tx_errors++;
749 #ifndef final_version
750 { int i;
751 DPRINTK( 2, ( "Ring data: dirty_tx %d cur_tx %d%s cur_rx %d\n",
752 lp->dirty_tx, lp->cur_tx,
753 lp->tx_full ? " (full)" : "",
754 lp->cur_rx ));
755 for( i = 0 ; i < RX_RING_SIZE; i++ )
756 DPRINTK( 2, ( "rx #%d: base=%04x blen=%04x mlen=%04x\n",
757 i, MEM->rx_head[i].base,
758 -MEM->rx_head[i].buf_length,
759 MEM->rx_head[i].msg_length ));
760 for( i = 0 ; i < TX_RING_SIZE; i++ )
761 DPRINTK( 2, ( "tx #%d: base=%04x len=%04x misc=%04x\n",
762 i, MEM->tx_head[i].base,
763 -MEM->tx_head[i].length,
764 MEM->tx_head[i].misc ));
765 }
766 #endif
767 /* XXX MSch: maybe purge/reinit ring here */
768 /* lance_restart, essentially */
769 lance_init_ring(dev);
770 REGA( CSR0 ) = CSR0_INEA | CSR0_INIT | CSR0_STRT;
771 dev->trans_start = jiffies;
772 netif_wake_queue (dev);
773 }
774
775 /* XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */
776
777 static int lance_start_xmit( struct sk_buff *skb, struct net_device *dev )
778
779 { struct lance_private *lp = (struct lance_private *)dev->priv;
780 struct lance_ioreg *IO = lp->iobase;
781 int entry, len;
782 struct lance_tx_head *head;
783 unsigned long flags;
784
785 DPRINTK( 2, ( "%s: lance_start_xmit() called, csr0 %4.4x.\n",
786 dev->name, DREG ));
787
788 netif_stop_queue (dev);
789
790 /* Fill in a Tx ring entry */
791 if (lance_debug >= 3) {
792 u_char *p;
793 int i;
794 printk( "%s: TX pkt type 0x%04x from ", dev->name,
795 ((u_short *)skb->data)[6]);
796 for( p = &((u_char *)skb->data)[6], i = 0; i < 6; i++ )
797 printk("%02x%s", *p++, i != 5 ? ":" : "" );
798 printk(" to ");
799 for( p = (u_char *)skb->data, i = 0; i < 6; i++ )
800 printk("%02x%s", *p++, i != 5 ? ":" : "" );
801 printk(" data at 0x%08x len %d\n", (int)skb->data,
802 (int)skb->len );
803 }
804
805 /* We're not prepared for the int until the last flags are set/reset. And
806 * the int may happen already after setting the OWN_CHIP... */
807 spin_lock_irqsave (&lp->devlock, flags);
808
809 /* Mask to ring buffer boundary. */
810 entry = lp->cur_tx & TX_RING_MOD_MASK;
811 head = &(MEM->tx_head[entry]);
812
813 /* Caution: the write order is important here, set the "ownership" bits
814 * last.
815 */
816
817 /* The old LANCE chips doesn't automatically pad buffers to min. size. */
818 len = (ETH_ZLEN < skb->len) ? skb->len : ETH_ZLEN;
819 /* PAM-Card has a bug: Can only send packets with even number of bytes! */
820 if (lp->cardtype == PAM_CARD && (len & 1))
821 ++len;
822
823 head->length = -len;
824 head->misc = 0;
825 lp->memcpy_f( PKTBUF_ADDR(head), (void *)skb->data, skb->len );
826 head->flag = TMD1_OWN_CHIP | TMD1_ENP | TMD1_STP;
827 lp->stats.tx_bytes += skb->len;
828 dev_kfree_skb( skb );
829 lp->cur_tx++;
830 while( lp->cur_tx >= TX_RING_SIZE && lp->dirty_tx >= TX_RING_SIZE ) {
831 lp->cur_tx -= TX_RING_SIZE;
832 lp->dirty_tx -= TX_RING_SIZE;
833 }
834
835 /* Trigger an immediate send poll. */
836 DREG = CSR0_INEA | CSR0_TDMD;
837 dev->trans_start = jiffies;
838
839 if ((MEM->tx_head[(entry+1) & TX_RING_MOD_MASK].flag & TMD1_OWN) ==
840 TMD1_OWN_HOST)
841 netif_start_queue (dev);
842 else
843 lp->tx_full = 1;
844 spin_unlock_irqrestore (&lp->devlock, flags);
845
846 return 0;
847 }
848
849 /* The LANCE interrupt handler. */
850
851 static void lance_interrupt( int irq, void *dev_id, struct pt_regs *fp)
852 {
853 struct net_device *dev = dev_id;
854 struct lance_private *lp;
855 struct lance_ioreg *IO;
856 int csr0, boguscnt = 10;
857
858 if (dev == NULL) {
859 DPRINTK( 1, ( "lance_interrupt(): interrupt for unknown device.\n" ));
860 return;
861 }
862
863 lp = (struct lance_private *)dev->priv;
864 IO = lp->iobase;
865 spin_lock (&lp->devlock);
866
867 AREG = CSR0;
868
869 while( ((csr0 = DREG) & (CSR0_ERR | CSR0_TINT | CSR0_RINT)) &&
870 --boguscnt >= 0) {
871 /* Acknowledge all of the current interrupt sources ASAP. */
872 DREG = csr0 & ~(CSR0_INIT | CSR0_STRT | CSR0_STOP |
873 CSR0_TDMD | CSR0_INEA);
874
875 DPRINTK( 2, ( "%s: interrupt csr0=%04x new csr=%04x.\n",
876 dev->name, csr0, DREG ));
877
878 if (csr0 & CSR0_RINT) /* Rx interrupt */
879 lance_rx( dev );
880
881 if (csr0 & CSR0_TINT) { /* Tx-done interrupt */
882 int dirty_tx = lp->dirty_tx;
883
884 while( dirty_tx < lp->cur_tx) {
885 int entry = dirty_tx & TX_RING_MOD_MASK;
886 int status = MEM->tx_head[entry].flag;
887
888 if (status & TMD1_OWN_CHIP)
889 break; /* It still hasn't been Txed */
890
891 MEM->tx_head[entry].flag = 0;
892
893 if (status & TMD1_ERR) {
894 /* There was an major error, log it. */
895 int err_status = MEM->tx_head[entry].misc;
896 lp->stats.tx_errors++;
897 if (err_status & TMD3_RTRY) lp->stats.tx_aborted_errors++;
898 if (err_status & TMD3_LCAR) lp->stats.tx_carrier_errors++;
899 if (err_status & TMD3_LCOL) lp->stats.tx_window_errors++;
900 if (err_status & TMD3_UFLO) {
901 /* Ackk! On FIFO errors the Tx unit is turned off! */
902 lp->stats.tx_fifo_errors++;
903 /* Remove this verbosity later! */
904 DPRINTK( 1, ( "%s: Tx FIFO error! Status %04x\n",
905 dev->name, csr0 ));
906 /* Restart the chip. */
907 DREG = CSR0_STRT;
908 }
909 } else {
910 if (status & (TMD1_MORE | TMD1_ONE | TMD1_DEF))
911 lp->stats.collisions++;
912 lp->stats.tx_packets++;
913 }
914
915 /* XXX MSch: free skb?? */
916 dirty_tx++;
917 }
918
919 #ifndef final_version
920 if (lp->cur_tx - dirty_tx >= TX_RING_SIZE) {
921 DPRINTK( 0, ( "out-of-sync dirty pointer,"
922 " %d vs. %d, full=%d.\n",
923 dirty_tx, lp->cur_tx, lp->tx_full ));
924 dirty_tx += TX_RING_SIZE;
925 }
926 #endif
927
928 if (lp->tx_full && (netif_queue_stopped(dev))
929 && dirty_tx > lp->cur_tx - TX_RING_SIZE + 2) {
930 /* The ring is no longer full, clear tbusy. */
931 lp->tx_full = 0;
932 netif_wake_queue (dev);
933 }
934
935 lp->dirty_tx = dirty_tx;
936 }
937
938 /* Log misc errors. */
939 if (csr0 & CSR0_BABL) lp->stats.tx_errors++; /* Tx babble. */
940 if (csr0 & CSR0_MISS) lp->stats.rx_errors++; /* Missed a Rx frame. */
941 if (csr0 & CSR0_MERR) {
942 DPRINTK( 1, ( "%s: Bus master arbitration failure (?!?), "
943 "status %04x.\n", dev->name, csr0 ));
944 /* Restart the chip. */
945 DREG = CSR0_STRT;
946 }
947 }
948
949 /* Clear any other interrupt, and set interrupt enable. */
950 DREG = CSR0_BABL | CSR0_CERR | CSR0_MISS | CSR0_MERR |
951 CSR0_IDON | CSR0_INEA;
952
953 DPRINTK( 2, ( "%s: exiting interrupt, csr0=%#04x.\n",
954 dev->name, DREG ));
955
956 spin_unlock (&lp->devlock);
957 }
958
959
960 static int lance_rx( struct net_device *dev )
961
962 { struct lance_private *lp = (struct lance_private *)dev->priv;
963 int entry = lp->cur_rx & RX_RING_MOD_MASK;
964 int i;
965
966 DPRINTK( 2, ( "%s: rx int, flag=%04x\n", dev->name,
967 MEM->rx_head[entry].flag ));
968
969 /* If we own the next entry, it's a new packet. Send it up. */
970 while( (MEM->rx_head[entry].flag & RMD1_OWN) == RMD1_OWN_HOST ) {
971 struct lance_rx_head *head = &(MEM->rx_head[entry]);
972 int status = head->flag;
973
974 if (status != (RMD1_ENP|RMD1_STP)) { /* There was an error. */
975 /* There is a tricky error noted by John Murphy,
976 <murf@perftech.com> to Russ Nelson: Even with full-sized
977 buffers it's possible for a jabber packet to use two
978 buffers, with only the last correctly noting the error. */
979 if (status & RMD1_ENP) /* Only count a general error at the */
980 lp->stats.rx_errors++; /* end of a packet.*/
981 if (status & RMD1_FRAM) lp->stats.rx_frame_errors++;
982 if (status & RMD1_OFLO) lp->stats.rx_over_errors++;
983 if (status & RMD1_CRC) lp->stats.rx_crc_errors++;
984 if (status & RMD1_BUFF) lp->stats.rx_fifo_errors++;
985 head->flag &= (RMD1_ENP|RMD1_STP);
986 } else {
987 /* Malloc up new buffer, compatible with net-3. */
988 short pkt_len = head->msg_length & 0xfff;
989 struct sk_buff *skb;
990
991 if (pkt_len < 60) {
992 printk( "%s: Runt packet!\n", dev->name );
993 lp->stats.rx_errors++;
994 }
995 else {
996 skb = dev_alloc_skb( pkt_len+2 );
997 if (skb == NULL) {
998 DPRINTK( 1, ( "%s: Memory squeeze, deferring packet.\n",
999 dev->name ));
1000 for( i = 0; i < RX_RING_SIZE; i++ )
1001 if (MEM->rx_head[(entry+i) & RX_RING_MOD_MASK].flag &
1002 RMD1_OWN_CHIP)
1003 break;
1004
1005 if (i > RX_RING_SIZE - 2) {
1006 lp->stats.rx_dropped++;
1007 head->flag |= RMD1_OWN_CHIP;
1008 lp->cur_rx++;
1009 }
1010 break;
1011 }
1012
1013 if (lance_debug >= 3) {
1014 u_char *data = PKTBUF_ADDR(head), *p;
1015 printk( "%s: RX pkt type 0x%04x from ", dev->name,
1016 ((u_short *)data)[6]);
1017 for( p = &data[6], i = 0; i < 6; i++ )
1018 printk("%02x%s", *p++, i != 5 ? ":" : "" );
1019 printk(" to ");
1020 for( p = data, i = 0; i < 6; i++ )
1021 printk("%02x%s", *p++, i != 5 ? ":" : "" );
1022 printk(" data %02x %02x %02x %02x %02x %02x %02x %02x "
1023 "len %d\n",
1024 data[15], data[16], data[17], data[18],
1025 data[19], data[20], data[21], data[22],
1026 pkt_len );
1027 }
1028
1029 skb->dev = dev;
1030 skb_reserve( skb, 2 ); /* 16 byte align */
1031 skb_put( skb, pkt_len ); /* Make room */
1032 lp->memcpy_f( skb->data, PKTBUF_ADDR(head), pkt_len );
1033 skb->protocol = eth_type_trans( skb, dev );
1034 netif_rx( skb );
1035 dev->last_rx = jiffies;
1036 lp->stats.rx_packets++;
1037 lp->stats.rx_bytes += pkt_len;
1038 }
1039 }
1040
1041 head->flag |= RMD1_OWN_CHIP;
1042 entry = (++lp->cur_rx) & RX_RING_MOD_MASK;
1043 }
1044 lp->cur_rx &= RX_RING_MOD_MASK;
1045
1046 /* From lance.c (Donald Becker): */
1047 /* We should check that at least two ring entries are free. If not,
1048 we should free one and mark stats->rx_dropped++. */
1049
1050 return 0;
1051 }
1052
1053
1054 static int lance_close( struct net_device *dev )
1055
1056 { struct lance_private *lp = (struct lance_private *)dev->priv;
1057 struct lance_ioreg *IO = lp->iobase;
1058
1059 netif_stop_queue (dev);
1060
1061 AREG = CSR0;
1062
1063 DPRINTK( 2, ( "%s: Shutting down ethercard, status was %2.2x.\n",
1064 dev->name, DREG ));
1065
1066 /* We stop the LANCE here -- it occasionally polls
1067 memory if we don't. */
1068 DREG = CSR0_STOP;
1069
1070 return 0;
1071 }
1072
1073
1074 static struct net_device_stats *lance_get_stats( struct net_device *dev )
1075
1076 { struct lance_private *lp = (struct lance_private *)dev->priv;
1077
1078 return &lp->stats;
1079 }
1080
1081
1082 /* Set or clear the multicast filter for this adaptor.
1083 num_addrs == -1 Promiscuous mode, receive all packets
1084 num_addrs == 0 Normal mode, clear multicast list
1085 num_addrs > 0 Multicast mode, receive normal and MC packets, and do
1086 best-effort filtering.
1087 */
1088
1089 static void set_multicast_list( struct net_device *dev )
1090
1091 { struct lance_private *lp = (struct lance_private *)dev->priv;
1092 struct lance_ioreg *IO = lp->iobase;
1093
1094 if (netif_running(dev))
1095 /* Only possible if board is already started */
1096 return;
1097
1098 /* We take the simple way out and always enable promiscuous mode. */
1099 DREG = CSR0_STOP; /* Temporarily stop the lance. */
1100
1101 if (dev->flags & IFF_PROMISC) {
1102 /* Log any net taps. */
1103 DPRINTK( 1, ( "%s: Promiscuous mode enabled.\n", dev->name ));
1104 REGA( CSR15 ) = 0x8000; /* Set promiscuous mode */
1105 } else {
1106 short multicast_table[4];
1107 int num_addrs = dev->mc_count;
1108 int i;
1109 /* We don't use the multicast table, but rely on upper-layer
1110 * filtering. */
1111 memset( multicast_table, (num_addrs == 0) ? 0 : -1,
1112 sizeof(multicast_table) );
1113 for( i = 0; i < 4; i++ )
1114 REGA( CSR8+i ) = multicast_table[i];
1115 REGA( CSR15 ) = 0; /* Unset promiscuous mode */
1116 }
1117
1118 /*
1119 * Always set BSWP after a STOP as STOP puts it back into
1120 * little endian mode.
1121 */
1122 REGA( CSR3 ) = CSR3_BSWP | (lp->cardtype == PAM_CARD ? CSR3_ACON : 0);
1123
1124 /* Resume normal operation and reset AREG to CSR0 */
1125 REGA( CSR0 ) = CSR0_IDON | CSR0_INEA | CSR0_STRT;
1126 }
1127
1128
1129 /* This is needed for old RieblCards and possible for new RieblCards */
1130
1131 static int lance_set_mac_address( struct net_device *dev, void *addr )
1132
1133 { struct lance_private *lp = (struct lance_private *)dev->priv;
1134 struct sockaddr *saddr = addr;
1135 int i;
1136
1137 if (lp->cardtype != OLD_RIEBL && lp->cardtype != NEW_RIEBL)
1138 return( -EOPNOTSUPP );
1139
1140 if (netif_running(dev)) {
1141 /* Only possible while card isn't started */
1142 DPRINTK( 1, ( "%s: hwaddr can be set only while card isn't open.\n",
1143 dev->name ));
1144 return( -EIO );
1145 }
1146
1147 memcpy( dev->dev_addr, saddr->sa_data, dev->addr_len );
1148 for( i = 0; i < 6; i++ )
1149 MEM->init.hwaddr[i] = dev->dev_addr[i^1]; /* <- 16 bit swap! */
1150 lp->memcpy_f( RIEBL_HWADDR_ADDR, dev->dev_addr, 6 );
1151 /* set also the magic for future sessions */
1152 *RIEBL_MAGIC_ADDR = RIEBL_MAGIC;
1153
1154 return( 0 );
1155 }
1156
1157
1158 #ifdef MODULE
1159 static struct net_device atarilance_dev;
1160
1161 int init_module(void)
1162
1163 { int err;
1164
1165 atarilance_dev.init = atarilance_probe;
1166 if ((err = register_netdev( &atarilance_dev ))) {
1167 if (err == -EIO) {
1168 printk( "No Atari Lance board found. Module not loaded.\n");
1169 }
1170 return( err );
1171 }
1172 return( 0 );
1173 }
1174
1175 void cleanup_module(void)
1176
1177 {
1178 unregister_netdev( &atarilance_dev );
1179 }
1180
1181 #endif /* MODULE */
1182
1183
1184 /*
1185 * Local variables:
1186 * c-indent-level: 4
1187 * tab-width: 4
1188 * End:
1189 */
1190