File: /usr/src/linux/drivers/net/sk_mca.c

1     /* 
2     net-3-driver for the SKNET MCA-based cards
3     
4     This is an extension to the Linux operating system, and is covered by the
5     same GNU General Public License that covers that work.
6     
7     Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de, aarnold@elsa.de)
8     
9     This driver is based both on the 3C523 driver and the SK_G16 driver.
10     
11     paper sources:
12       'PC Hardware: Aufbau, Funktionsweise, Programmierung' by 
13       Hans-Peter Messmer for the basic Microchannel stuff
14       
15       'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
16       for help on Ethernet driver programming
17     
18       'Ethernet/IEEE 802.3 Family 1992 World Network Data Book/Handbook' by AMD
19       for documentation on the AM7990 LANCE
20     
21       'SKNET Personal Technisches Manual', Version 1.2 by Schneider&Koch
22       for documentation on the Junior board
23     
24       'SK-NET MC2+ Technical Manual", Version 1.1 by Schneider&Koch for
25       documentation on the MC2 bord
26       
27       A big thank you to the S&K support for providing me so quickly with
28       documentation!
29     
30       Also see http://www.syskonnect.com/
31     
32       Missing things:
33     
34       -> set debug level via ioctl instead of compile-time switches
35       -> I didn't follow the development of the 2.1.x kernels, so my
36          assumptions about which things changed with which kernel version 
37          are probably nonsense
38     
39     History:
40       May 16th, 1999
41       	startup
42       May 22st, 1999
43     	added private structure, methods
44             begun building data structures in RAM
45       May 23nd, 1999
46     	can receive frames, send frames
47       May 24th, 1999
48             modularized intialization of LANCE
49             loadable as module
50     	still Tx problem :-(
51       May 26th, 1999
52       	MC2 works
53       	support for multiple devices
54       	display media type for MC2+
55       May 28th, 1999
56     	fixed problem in GetLANCE leaving interrupts turned off
57             increase TX queue to 4 packets to improve send performance
58       May 29th, 1999
59     	a few corrections in statistics, caught rcvr overruns 
60             reinitialization of LANCE/board in critical situations
61             MCA info implemented
62     	implemented LANCE multicast filter
63       Jun 6th, 1999
64     	additions for Linux 2.2
65       Dec 25th, 1999
66       	unfortunately there seem to be newer MC2+ boards that react
67       	on IRQ 3/5/9/10 instead of 3/5/10/11, so we have to autoprobe
68       	in questionable cases...
69       Dec 28th, 1999
70     	integrated patches from David Weinehall & Bill Wendling for 2.3
71     	kernels (isa_...functions).  Things are defined in a way that
72             it still works with 2.0.x 8-)
73       Dec 30th, 1999
74     	added handling of the remaining interrupt conditions.  That
75             should cure the spurious hangs.
76       Jan 30th, 2000
77     	newer kernels automatically probe more than one board, so the
78     	'startslot' as a variable is also needed here
79       June 1st, 2000
80     	added changes for recent 2.3 kernels
81     
82      *************************************************************************/
83     
84     #include <linux/kernel.h>
85     #include <linux/sched.h>
86     #include <linux/string.h>
87     #include <linux/errno.h>
88     #include <linux/ioport.h>
89     #include <linux/slab.h>
90     #include <linux/interrupt.h>
91     #include <linux/delay.h>
92     #include <linux/time.h>
93     #include <linux/mca.h>
94     #include <linux/init.h>
95     #include <asm/processor.h>
96     #include <asm/bitops.h>
97     #include <asm/io.h>
98     
99     #include <linux/module.h>
100     #include <linux/version.h>
101     
102     #include <linux/netdevice.h>
103     #include <linux/etherdevice.h>
104     #include <linux/skbuff.h>
105     
106     #define _SK_MCA_DRIVER_
107     #include "sk_mca.h"
108     
109     /* ------------------------------------------------------------------------
110      * global static data - not more since we can handle multiple boards and
111      * have to pack all state info into the device struct!
112      * ------------------------------------------------------------------------ */
113     
114     static char *MediaNames[Media_Count] =
115         { "10Base2", "10BaseT", "10Base5", "Unknown" };
116     
117     static unsigned char poly[] =
118         { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0,
119     	1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
120     };
121     
122     /* ------------------------------------------------------------------------
123      * private subfunctions
124      * ------------------------------------------------------------------------ */
125     
126     /* dump parts of shared memory - only needed during debugging */
127     
128     #ifdef DEBUG
129     static void dumpmem(struct SKMCA_NETDEV *dev, u32 start, u32 len)
130     {
131     	int z;
132     
133     	for (z = 0; z < len; z++) {
134     		if ((z & 15) == 0)
135     			printk("%04x:", z);
136     		printk(" %02x", SKMCA_READB(dev->mem_start + start + z));
137     		if ((z & 15) == 15)
138     			printk("\n");
139     	}
140     }
141     
142     /* print exact time - ditto */
143     
144     static void PrTime(void)
145     {
146     	struct timeval tv;
147     
148     	do_gettimeofday(&tv);
149     	printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);
150     }
151     #endif
152     
153     /* deduce resources out of POS registers */
154     
155     static void __init getaddrs(int slot, int junior, int *base, int *irq,
156     		     skmca_medium * medium)
157     {
158     	u_char pos0, pos1, pos2;
159     
160     	if (junior) {
161     		pos0 = mca_read_stored_pos(slot, 2);
162     		*base = ((pos0 & 0x0e) << 13) + 0xc0000;
163     		*irq = ((pos0 & 0x10) >> 4) + 10;
164     		*medium = Media_Unknown;
165     	} else {
166     		/* reset POS 104 Bits 0+1 so the shared memory region goes to the
167     		   configured area between 640K and 1M.  Afterwards, enable the MC2.
168     		   I really don't know what rode SK to do this... */
169     
170     		mca_write_pos(slot, 4,
171     			      mca_read_stored_pos(slot, 4) & 0xfc);
172     		mca_write_pos(slot, 2,
173     			      mca_read_stored_pos(slot, 2) | 0x01);
174     
175     		pos1 = mca_read_stored_pos(slot, 3);
176     		pos2 = mca_read_stored_pos(slot, 4);
177     		*base = ((pos1 & 0x07) << 14) + 0xc0000;
178     		switch (pos2 & 0x0c) {
179     		case 0:
180     			*irq = 3;
181     			break;
182     		case 4:
183     			*irq = 5;
184     			break;
185     		case 8:
186     			*irq = -10;
187     			break;
188     		case 12:
189     			*irq = -11;
190     			break;
191     		}
192     		*medium = (pos2 >> 6) & 3;
193     	}
194     }
195     
196     /* check for both cards:
197        When the MC2 is turned off, it was configured for more than 15MB RAM,
198        is disabled and won't get detected using the standard probe.  We
199        therefore have to scan the slots manually :-( */
200     
201     static int __init dofind(int *junior, int firstslot)
202     {
203     	int slot;
204     	unsigned int id;
205     
206     	for (slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++) {
207     		id = mca_read_stored_pos(slot, 0)
208     		    + (((unsigned int) mca_read_stored_pos(slot, 1)) << 8);
209     
210     		*junior = 0;
211     		if (id == SKNET_MCA_ID)
212     			return slot;
213     		*junior = 1;
214     		if (id == SKNET_JUNIOR_MCA_ID)
215     			return slot;
216     	}
217     	return MCA_NOTFOUND;
218     }
219     
220     /* reset the whole board */
221     
222     static void ResetBoard(struct SKMCA_NETDEV *dev)
223     {
224     	skmca_priv *priv = (skmca_priv *) dev->priv;
225     
226     	SKMCA_WRITEB(CTRL_RESET_ON, priv->ctrladdr);
227     	udelay(10);
228     	SKMCA_WRITEB(CTRL_RESET_OFF, priv->ctrladdr);
229     }
230     
231     /* wait for LANCE interface to become not busy */
232     
233     static int WaitLANCE(struct SKMCA_NETDEV *dev)
234     {
235     	skmca_priv *priv = (skmca_priv *) dev->priv;
236     	int t = 0;
237     
238     	while ((SKMCA_READB(priv->ctrladdr) & STAT_IO_BUSY) ==
239     	       STAT_IO_BUSY) {
240     		udelay(1);
241     		if (++t > 1000) {
242     			printk("%s: LANCE access timeout", dev->name);
243     			return 0;
244     		}
245     	}
246     
247     	return 1;
248     }
249     
250     /* set LANCE register - must be atomic */
251     
252     static void SetLANCE(struct SKMCA_NETDEV *dev, u16 addr, u16 value)
253     {
254     	skmca_priv *priv = (skmca_priv *) dev->priv;
255     	unsigned long flags;
256     
257     	/* disable interrupts */
258     
259     	save_flags(flags);
260     	cli();
261     
262     	/* wait until no transfer is pending */
263     
264     	WaitLANCE(dev);
265     
266     	/* transfer register address to RAP */
267     
268     	SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
269     		     priv->ctrladdr);
270     	SKMCA_WRITEW(addr, priv->ioregaddr);
271     	SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
272     	udelay(1);
273     	WaitLANCE(dev);
274     
275     	/* transfer data to register */
276     
277     	SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA,
278     		     priv->ctrladdr);
279     	SKMCA_WRITEW(value, priv->ioregaddr);
280     	SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
281     	udelay(1);
282     	WaitLANCE(dev);
283     
284     	/* reenable interrupts */
285     
286     	restore_flags(flags);
287     }
288     
289     /* get LANCE register */
290     
291     static u16 GetLANCE(struct SKMCA_NETDEV *dev, u16 addr)
292     {
293     	skmca_priv *priv = (skmca_priv *) dev->priv;
294     	unsigned long flags;
295     	unsigned int res;
296     
297     	/* disable interrupts */
298     
299     	save_flags(flags);
300     	cli();
301     
302     	/* wait until no transfer is pending */
303     
304     	WaitLANCE(dev);
305     
306     	/* transfer register address to RAP */
307     
308     	SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
309     		     priv->ctrladdr);
310     	SKMCA_WRITEW(addr, priv->ioregaddr);
311     	SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
312     	udelay(1);
313     	WaitLANCE(dev);
314     
315     	/* transfer data from register */
316     
317     	SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA,
318     		     priv->ctrladdr);
319     	SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
320     	udelay(1);
321     	WaitLANCE(dev);
322     	res = SKMCA_READW(priv->ioregaddr);
323     
324     	/* reenable interrupts */
325     
326     	restore_flags(flags);
327     
328     	return res;
329     }
330     
331     /* build up descriptors in shared RAM */
332     
333     static void InitDscrs(struct SKMCA_NETDEV *dev)
334     {
335     	u32 bufaddr;
336     
337     	/* Set up Tx descriptors. The board has only 16K RAM so bits 16..23
338     	   are always 0. */
339     
340     	bufaddr = RAM_DATABASE;
341     	{
342     		LANCE_TxDescr descr;
343     		int z;
344     
345     		for (z = 0; z < TXCOUNT; z++) {
346     			descr.LowAddr = bufaddr;
347     			descr.Flags = 0;
348     			descr.Len = 0xf000;
349     			descr.Status = 0;
350     			SKMCA_TOIO(dev->mem_start + RAM_TXBASE +
351     				   (z * sizeof(LANCE_TxDescr)), &descr,
352     				   sizeof(LANCE_TxDescr));
353     			SKMCA_SETIO(dev->mem_start + bufaddr, 0,
354     				    RAM_BUFSIZE);
355     			bufaddr += RAM_BUFSIZE;
356     		}
357     	}
358     
359     	/* do the same for the Rx descriptors */
360     
361     	{
362     		LANCE_RxDescr descr;
363     		int z;
364     
365     		for (z = 0; z < RXCOUNT; z++) {
366     			descr.LowAddr = bufaddr;
367     			descr.Flags = RXDSCR_FLAGS_OWN;
368     			descr.MaxLen = -RAM_BUFSIZE;
369     			descr.Len = 0;
370     			SKMCA_TOIO(dev->mem_start + RAM_RXBASE +
371     				   (z * sizeof(LANCE_RxDescr)), &descr,
372     				   sizeof(LANCE_RxDescr));
373     			SKMCA_SETIO(dev->mem_start + bufaddr, 0,
374     				    RAM_BUFSIZE);
375     			bufaddr += RAM_BUFSIZE;
376     		}
377     	}
378     }
379     
380     /* calculate the hash bit position for a given multicast address
381        taken more or less directly from the AMD datasheet... */
382     
383     static void UpdateCRC(unsigned char *CRC, int bit)
384     {
385     	int j;
386     
387     	/* shift CRC one bit */
388     
389     	memmove(CRC + 1, CRC, 32 * sizeof(unsigned char));
390     	CRC[0] = 0;
391     
392     	/* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */
393     
394     	if (bit ^ CRC[32])
395     		for (j = 0; j < 32; j++)
396     			CRC[j] ^= poly[j];
397     }
398     
399     static unsigned int GetHash(char *address)
400     {
401     	unsigned char CRC[33];
402     	int i, byte, hashcode;
403     
404     	/* a multicast address has bit 0 in the first byte set */
405     
406     	if ((address[0] & 1) == 0)
407     		return -1;
408     
409     	/* initialize CRC */
410     
411     	memset(CRC, 1, sizeof(CRC));
412     
413     	/* loop through address bits */
414     
415     	for (byte = 0; byte < 6; byte++)
416     		for (i = 0; i < 8; i++)
417     			UpdateCRC(CRC, (address[byte] >> i) & 1);
418     
419     	/* hashcode is the 6 least significant bits of the CRC */
420     
421     	hashcode = 0;
422     	for (i = 0; i < 6; i++)
423     		hashcode = (hashcode << 1) + CRC[i];
424     	return hashcode;
425     }
426     
427     /* feed ready-built initialization block into LANCE */
428     
429     static void InitLANCE(struct SKMCA_NETDEV *dev)
430     {
431     	skmca_priv *priv = (skmca_priv *) dev->priv;
432     
433     	/* build up descriptors. */
434     
435     	InitDscrs(dev);
436     
437     	/* next RX descriptor to be read is the first one.  Since the LANCE
438     	   will start from the beginning after initialization, we have to 
439     	   reset out pointers too. */
440     
441     	priv->nextrx = 0;
442     
443     	/* no TX descriptors active */
444     
445     	priv->nexttxput = priv->nexttxdone = priv->txbusy = 0;
446     
447     	/* set up the LANCE bus control register - constant for SKnet boards */
448     
449     	SetLANCE(dev, LANCE_CSR3,
450     		 CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD);
451     
452     	/* write address of initialization block into LANCE */
453     
454     	SetLANCE(dev, LANCE_CSR1, RAM_INITBASE & 0xffff);
455     	SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >> 16) & 0xff);
456     
457     	/* we don't get ready until the LANCE has read the init block */
458     
459     #if (LINUX_VERSION_CODE >= 0x02032a)
460     	netif_stop_queue(dev);
461     #else
462     	dev->tbusy = 1;
463     #endif
464     
465     	/* let LANCE read the initialization block.  LANCE is ready
466     	   when we receive the corresponding interrupt. */
467     
468     	SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);
469     }
470     
471     /* stop the LANCE so we can reinitialize it */
472     
473     static void StopLANCE(struct SKMCA_NETDEV *dev)
474     {
475     	/* can't take frames any more */
476     
477     #if (LINUX_VERSION_CODE >= 0x02032a)
478     	netif_stop_queue(dev);
479     #else
480     	dev->tbusy = 1;
481     #endif
482     
483     	/* disable interrupts, stop it */
484     
485     	SetLANCE(dev, LANCE_CSR0, CSR0_STOP);
486     }
487     
488     /* initialize card and LANCE for proper operation */
489     
490     static void InitBoard(struct SKMCA_NETDEV *dev)
491     {
492     	LANCE_InitBlock block;
493     
494     	/* Lay out the shared RAM - first we create the init block for the LANCE.
495     	   We do not overwrite it later because we need it again when we switch
496     	   promiscous mode on/off. */
497     
498     	block.Mode = 0;
499     	if (dev->flags & IFF_PROMISC)
500     		block.Mode |= LANCE_INIT_PROM;
501     	memcpy(block.PAdr, dev->dev_addr, 6);
502     	memset(block.LAdrF, 0, sizeof(block.LAdrF));
503     	block.RdrP = (RAM_RXBASE & 0xffffff) | (LRXCOUNT << 29);
504     	block.TdrP = (RAM_TXBASE & 0xffffff) | (LTXCOUNT << 29);
505     
506     	SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
507     
508     	/* initialize LANCE. Implicitly sets up other structures in RAM. */
509     
510     	InitLANCE(dev);
511     }
512     
513     /* deinitialize card and LANCE */
514     
515     static void DeinitBoard(struct SKMCA_NETDEV *dev)
516     {
517     	/* stop LANCE */
518     
519     	StopLANCE(dev);
520     
521     	/* reset board */
522     
523     	ResetBoard(dev);
524     }
525     
526     /* probe for device's irq */
527     
528     static int __init ProbeIRQ(struct SKMCA_NETDEV *dev)
529     {
530     	unsigned long imaskval, njiffies, irq;
531     	u16 csr0val;
532     
533     	/* enable all interrupts */
534     
535     	imaskval = probe_irq_on();
536     
537     	/* initialize the board. Wait for interrupt 'Initialization done'. */
538     
539     	ResetBoard(dev);
540     	InitBoard(dev);
541     
542     	njiffies = jiffies + 100;
543     	do {
544     		csr0val = GetLANCE(dev, LANCE_CSR0);
545     	}
546     	while (((csr0val & CSR0_IDON) == 0) && (jiffies != njiffies));
547     
548     	/* turn of interrupts again */
549     
550     	irq = probe_irq_off(imaskval);
551     
552     	/* if we found something, ack the interrupt */
553     
554     	if (irq)
555     		SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_IDON);
556     
557     	/* back to idle state */
558     
559     	DeinitBoard(dev);
560     
561     	return irq;
562     }
563     
564     /* ------------------------------------------------------------------------
565      * interrupt handler(s)
566      * ------------------------------------------------------------------------ */
567     
568     /* LANCE has read initialization block -> start it */
569     
570     static u16 irqstart_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
571     {
572     	/* now we're ready to transmit */
573     
574     #if (LINUX_VERSION_CODE >= 0x02032a)
575     	netif_wake_queue(dev);
576     #else
577     	dev->tbusy = 0;
578     #endif
579     
580     	/* reset IDON bit, start LANCE */
581     
582     	SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT);
583     	return GetLANCE(dev, LANCE_CSR0);
584     }
585     
586     /* did we loose blocks due to a FIFO overrun ? */
587     
588     static u16 irqmiss_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
589     {
590     	skmca_priv *priv = (skmca_priv *) dev->priv;
591     
592     	/* update statistics */
593     
594     	priv->stat.rx_fifo_errors++;
595     
596     	/* reset MISS bit */
597     
598     	SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_MISS);
599     	return GetLANCE(dev, LANCE_CSR0);
600     }
601     
602     /* receive interrupt */
603     
604     static u16 irqrx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
605     {
606     	skmca_priv *priv = (skmca_priv *) dev->priv;
607     	LANCE_RxDescr descr;
608     	unsigned int descraddr;
609     
610     	/* run through queue until we reach a descriptor we do not own */
611     
612     	descraddr = RAM_RXBASE + (priv->nextrx * sizeof(LANCE_RxDescr));
613     	while (1) {
614     		/* read descriptor */
615     		SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
616     			     sizeof(LANCE_RxDescr));
617     
618     		/* if we reach a descriptor we do not own, we're done */
619     		if ((descr.Flags & RXDSCR_FLAGS_OWN) != 0)
620     			break;
621     
622     #ifdef DEBUG
623     		PrTime();
624     		printk("Receive packet on descr %d len %d\n", priv->nextrx,
625     		       descr.Len);
626     #endif
627     
628     		/* erroneous packet ? */
629     		if ((descr.Flags & RXDSCR_FLAGS_ERR) != 0) {
630     			priv->stat.rx_errors++;
631     			if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
632     				priv->stat.rx_crc_errors++;
633     			else if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
634     				priv->stat.rx_frame_errors++;
635     			else if ((descr.Flags & RXDSCR_FLAGS_OFLO) != 0)
636     				priv->stat.rx_fifo_errors++;
637     		}
638     
639     		/* good packet ? */
640     		else {
641     			struct sk_buff *skb;
642     
643     			skb = dev_alloc_skb(descr.Len + 2);
644     			if (skb == NULL)
645     				priv->stat.rx_dropped++;
646     			else {
647     				SKMCA_FROMIO(skb_put(skb, descr.Len),
648     					     dev->mem_start +
649     					     descr.LowAddr, descr.Len);
650     				skb->dev = dev;
651     				skb->protocol = eth_type_trans(skb, dev);
652     				skb->ip_summed = CHECKSUM_NONE;
653     				priv->stat.rx_packets++;
654     #if LINUX_VERSION_CODE >= 0x020119	/* byte counters for >= 2.1.25 */
655     				priv->stat.rx_bytes += descr.Len;
656     #endif
657     				netif_rx(skb);
658     				dev->last_rx = jiffies;
659     			}
660     		}
661     
662     		/* give descriptor back to LANCE */
663     		descr.Len = 0;
664     		descr.Flags |= RXDSCR_FLAGS_OWN;
665     
666     		/* update descriptor in shared RAM */
667     		SKMCA_TOIO(dev->mem_start + descraddr, &descr,
668     			   sizeof(LANCE_RxDescr));
669     
670     		/* go to next descriptor */
671     		priv->nextrx++;
672     		descraddr += sizeof(LANCE_RxDescr);
673     		if (priv->nextrx >= RXCOUNT) {
674     			priv->nextrx = 0;
675     			descraddr = RAM_RXBASE;
676     		}
677     	}
678     
679     	/* reset RINT bit */
680     
681     	SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_RINT);
682     	return GetLANCE(dev, LANCE_CSR0);
683     }
684     
685     /* transmit interrupt */
686     
687     static u16 irqtx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
688     {
689     	skmca_priv *priv = (skmca_priv *) dev->priv;
690     	LANCE_TxDescr descr;
691     	unsigned int descraddr;
692     
693     	/* check descriptors at most until no busy one is left */
694     
695     	descraddr =
696     	    RAM_TXBASE + (priv->nexttxdone * sizeof(LANCE_TxDescr));
697     	while (priv->txbusy > 0) {
698     		/* read descriptor */
699     		SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
700     			     sizeof(LANCE_TxDescr));
701     
702     		/* if the LANCE still owns this one, we've worked out all sent packets */
703     		if ((descr.Flags & TXDSCR_FLAGS_OWN) != 0)
704     			break;
705     
706     #ifdef DEBUG
707     		PrTime();
708     		printk("Send packet done on descr %d\n", priv->nexttxdone);
709     #endif
710     
711     		/* update statistics */
712     		if ((descr.Flags & TXDSCR_FLAGS_ERR) == 0) {
713     			priv->stat.tx_packets++;
714     #if LINUX_VERSION_CODE >= 0x020119	/* byte counters for >= 2.1.25 */
715     			priv->stat.tx_bytes++;
716     #endif
717     		} else {
718     			priv->stat.tx_errors++;
719     			if ((descr.Status & TXDSCR_STATUS_UFLO) != 0) {
720     				priv->stat.tx_fifo_errors++;
721     				InitLANCE(dev);
722     			}
723     				else
724     			    if ((descr.Status & TXDSCR_STATUS_LCOL) !=
725     				0) priv->stat.tx_window_errors++;
726     			else if ((descr.Status & TXDSCR_STATUS_LCAR) != 0)
727     				priv->stat.tx_carrier_errors++;
728     			else if ((descr.Status & TXDSCR_STATUS_RTRY) != 0)
729     				priv->stat.tx_aborted_errors++;
730     		}
731     
732     		/* go to next descriptor */
733     		priv->nexttxdone++;
734     		descraddr += sizeof(LANCE_TxDescr);
735     		if (priv->nexttxdone >= TXCOUNT) {
736     			priv->nexttxdone = 0;
737     			descraddr = RAM_TXBASE;
738     		}
739     		priv->txbusy--;
740     	}
741     
742     	/* reset TX interrupt bit */
743     
744     	SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_TINT);
745     	oldcsr0 = GetLANCE(dev, LANCE_CSR0);
746     
747     	/* at least one descriptor is freed.  Therefore we can accept
748     	   a new one */
749     	/* inform upper layers we're in business again */
750     
751     #if (LINUX_VERSION_CODE >= 0x02032a)
752     	netif_wake_queue(dev);
753     #else
754     	dev->tbusy = 0;
755     	mark_bh(NET_BH);
756     #endif
757     
758     	return oldcsr0;
759     }
760     
761     /* general interrupt entry */
762     
763     static void irq_handler(int irq, void *device, struct pt_regs *regs)
764     {
765     	struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) device;
766     	u16 csr0val;
767     
768     	/* read CSR0 to get interrupt cause */
769     
770     	csr0val = GetLANCE(dev, LANCE_CSR0);
771     
772     	/* in case we're not meant... */
773     
774     	if ((csr0val & CSR0_INTR) == 0)
775     		return;
776     
777     #if (LINUX_VERSION_CODE >= 0x02032a)
778     #if 0
779     	set_bit(LINK_STATE_RXSEM, &dev->state);
780     #endif
781     #else
782     	dev->interrupt = 1;
783     #endif
784     
785     	/* loop through the interrupt bits until everything is clear */
786     
787     	do {
788     		if ((csr0val & CSR0_IDON) != 0)
789     			csr0val = irqstart_handler(dev, csr0val);
790     		if ((csr0val & CSR0_RINT) != 0)
791     			csr0val = irqrx_handler(dev, csr0val);
792     		if ((csr0val & CSR0_MISS) != 0)
793     			csr0val = irqmiss_handler(dev, csr0val);
794     		if ((csr0val & CSR0_TINT) != 0)
795     			csr0val = irqtx_handler(dev, csr0val);
796     		if ((csr0val & CSR0_MERR) != 0) {
797     			SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_MERR);
798     			csr0val = GetLANCE(dev, LANCE_CSR0);
799     		}
800     		if ((csr0val & CSR0_BABL) != 0) {
801     			SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_BABL);
802     			csr0val = GetLANCE(dev, LANCE_CSR0);
803     		}
804     	}
805     	while ((csr0val & CSR0_INTR) != 0);
806     
807     #if (LINUX_VERSION_CODE >= 0x02032a)
808     #if 0
809     	clear_bit(LINK_STATE_RXSEM, &dev->state);
810     #endif
811     #else
812     	dev->interrupt = 0;
813     #endif
814     }
815     
816     /* ------------------------------------------------------------------------
817      * driver methods
818      * ------------------------------------------------------------------------ */
819     
820     /* MCA info */
821     
822     static int skmca_getinfo(char *buf, int slot, void *d)
823     {
824     	int len = 0, i;
825     	struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) d;
826     	skmca_priv *priv;
827     
828     	/* can't say anything about an uninitialized device... */
829     
830     	if (dev == NULL)
831     		return len;
832     	if (dev->priv == NULL)
833     		return len;
834     	priv = (skmca_priv *) dev->priv;
835     
836     	/* print info */
837     
838     	len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
839     	len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
840     		       dev->mem_end - 1);
841     	len +=
842     	    sprintf(buf + len, "Transceiver: %s\n",
843     		    MediaNames[priv->medium]);
844     	len += sprintf(buf + len, "Device: %s\n", dev->name);
845     	len += sprintf(buf + len, "MAC address:");
846     	for (i = 0; i < 6; i++)
847     		len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
848     	buf[len++] = '\n';
849     	buf[len] = 0;
850     
851     	return len;
852     }
853     
854     /* open driver.  Means also initialization and start of LANCE */
855     
856     static int skmca_open(struct SKMCA_NETDEV *dev)
857     {
858     	int result;
859     	skmca_priv *priv = (skmca_priv *) dev->priv;
860     
861     	/* register resources - only necessary for IRQ */
862     	result =
863     	    request_irq(priv->realirq, irq_handler,
864     			SA_SHIRQ | SA_SAMPLE_RANDOM, "sk_mca", dev);
865     	if (result != 0) {
866     		printk("%s: failed to register irq %d\n", dev->name,
867     		       dev->irq);
868     		return result;
869     	}
870     	dev->irq = priv->realirq;
871     
872     	/* set up the card and LANCE */
873     
874     	InitBoard(dev);
875     
876     	/* set up flags */
877     
878     #if (LINUX_VERSION_CODE >= 0x02032a)
879     	netif_start_queue(dev);
880     #else
881     	dev->interrupt = 0;
882     	dev->tbusy = 0;
883     	dev->start = 0;
884     	MOD_INC_USE_COUNT;
885     #endif
886     
887     	return 0;
888     }
889     
890     /* close driver.  Shut down board and free allocated resources */
891     
892     static int skmca_close(struct SKMCA_NETDEV *dev)
893     {
894     	/* turn off board */
895     	DeinitBoard(dev);
896     
897     	/* release resources */
898     	if (dev->irq != 0)
899     		free_irq(dev->irq, dev);
900     	dev->irq = 0;
901     
902     #if (LINUX_VERSION_CODE < 0x02032a)
903     	MOD_DEC_USE_COUNT;
904     #endif
905     
906     	return 0;
907     }
908     
909     /* transmit a block. */
910     
911     static int skmca_tx(struct sk_buff *skb, struct SKMCA_NETDEV *dev)
912     {
913     	skmca_priv *priv = (skmca_priv *) dev->priv;
914     	LANCE_TxDescr descr;
915     	unsigned int address;
916     	int tmplen, retval = 0;
917     	unsigned long flags;
918     
919     	/* if we get called with a NULL descriptor, the Ethernet layer thinks 
920     	   our card is stuck an we should reset it.  We'll do this completely: */
921     
922     	if (skb == NULL) {
923     		DeinitBoard(dev);
924     		InitBoard(dev);
925     		return 0;	/* don't try to free the block here ;-) */
926     	}
927     
928     	/* is there space in the Tx queue ? If no, the upper layer gave us a
929     	   packet in spite of us not being ready and is really in trouble.
930     	   We'll do the dropping for him: */
931     	if (priv->txbusy >= TXCOUNT) {
932     		priv->stat.tx_dropped++;
933     		retval = -EIO;
934     		goto tx_done;
935     	}
936     
937     	/* get TX descriptor */
938     	address = RAM_TXBASE + (priv->nexttxput * sizeof(LANCE_TxDescr));
939     	SKMCA_FROMIO(&descr, dev->mem_start + address,
940     		     sizeof(LANCE_TxDescr));
941     
942     	/* enter packet length as 2s complement - assure minimum length */
943     	tmplen = skb->len;
944     	if (tmplen < 60)
945     		tmplen = 60;
946     	descr.Len = 65536 - tmplen;
947     
948     	/* copy filler into RAM - in case we're filling up... 
949     	   we're filling a bit more than necessary, but that doesn't harm
950     	   since the buffer is far larger... */
951     	if (tmplen > skb->len) {
952     		char *fill = "NetBSD is a nice OS too! ";
953     		unsigned int destoffs = 0, l = strlen(fill);
954     
955     		while (destoffs < tmplen) {
956     			SKMCA_TOIO(dev->mem_start + descr.LowAddr +
957     				   destoffs, fill, l);
958     			destoffs += l;
959     		}
960     	}
961     
962     	/* do the real data copying */
963     	SKMCA_TOIO(dev->mem_start + descr.LowAddr, skb->data, skb->len);
964     
965     	/* hand descriptor over to LANCE - this is the first and last chunk */
966     	descr.Flags =
967     	    TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
968     
969     #ifdef DEBUG
970     	PrTime();
971     	printk("Send packet on descr %d len %d\n", priv->nexttxput,
972     	       skb->len);
973     #endif
974     
975     	/* one more descriptor busy */
976     	save_flags(flags);
977     	cli();
978     	priv->nexttxput++;
979     	if (priv->nexttxput >= TXCOUNT)
980     		priv->nexttxput = 0;
981     	priv->txbusy++;
982     
983     	/* are we saturated ? */
984     
985     	if (priv->txbusy >= TXCOUNT)
986     #if (LINUX_VERSION_CODE >= 0x02032a)
987     		netif_stop_queue(dev);
988     #else
989     		dev->tbusy = 1;
990     #endif
991     
992     	/* write descriptor back to RAM */
993     	SKMCA_TOIO(dev->mem_start + address, &descr,
994     		   sizeof(LANCE_TxDescr));
995     
996     	/* if no descriptors were active, give the LANCE a hint to read it
997     	   immediately */
998     
999     	if (priv->txbusy == 0)
1000     		SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
1001     
1002     	restore_flags(flags);
1003     
1004           tx_done:
1005     
1006     	/* When did that change exactly ? */
1007     
1008     #if LINUX_VERSION_CODE >= 0x020200
1009     	dev_kfree_skb(skb);
1010     #else
1011     	dev_kfree_skb(skb, FREE_WRITE);
1012     #endif
1013     	return retval;
1014     }
1015     
1016     /* return pointer to Ethernet statistics */
1017     
1018     static struct net_device_stats *skmca_stats(struct SKMCA_NETDEV *dev)
1019     {
1020     	skmca_priv *priv = (skmca_priv *) dev->priv;
1021     
1022     	return &(priv->stat);
1023     }
1024     
1025     /* we don't support runtime reconfiguration, since an MCA card can
1026        be unambigously identified by its POS registers. */
1027     
1028     static int skmca_config(struct SKMCA_NETDEV *dev, struct ifmap *map)
1029     {
1030     	return 0;
1031     }
1032     
1033     /* switch receiver mode.  We use the LANCE's multicast filter to prefilter
1034        multicast addresses. */
1035     
1036     static void skmca_set_multicast_list(struct SKMCA_NETDEV *dev)
1037     {
1038     	LANCE_InitBlock block;
1039     
1040     	/* first stop the LANCE... */
1041     	StopLANCE(dev);
1042     
1043     	/* ...then modify the initialization block... */
1044     	SKMCA_FROMIO(&block, dev->mem_start + RAM_INITBASE, sizeof(block));
1045     	if (dev->flags & IFF_PROMISC)
1046     		block.Mode |= LANCE_INIT_PROM;
1047     	else
1048     		block.Mode &= ~LANCE_INIT_PROM;
1049     
1050     	if (dev->flags & IFF_ALLMULTI) {	/* get all multicasts */
1051     		memset(block.LAdrF, 8, 0xff);
1052     	} else {		/* get selected/no multicasts */
1053     
1054     		struct dev_mc_list *mptr;
1055     		int code;
1056     
1057     		memset(block.LAdrF, 8, 0x00);
1058     		for (mptr = dev->mc_list; mptr != NULL; mptr = mptr->next) {
1059     			code = GetHash(mptr->dmi_addr);
1060     			block.LAdrF[(code >> 3) & 7] |= 1 << (code & 7);
1061     		}
1062     	}
1063     
1064     	SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
1065     
1066     	/* ...then reinit LANCE with the correct flags */
1067     	InitLANCE(dev);
1068     }
1069     
1070     /* ------------------------------------------------------------------------
1071      * hardware check
1072      * ------------------------------------------------------------------------ */
1073     
1074     static int startslot;		/* counts through slots when probing multiple devices */
1075     
1076     int __init skmca_probe(struct SKMCA_NETDEV *dev)
1077     {
1078     	int force_detect = 0;
1079     	int junior, slot, i;
1080     	int base = 0, irq = 0;
1081     	skmca_priv *priv;
1082     	skmca_medium medium;
1083     
1084     	/* can't work without an MCA bus ;-) */
1085     
1086     	if (MCA_bus == 0)
1087     		return -ENODEV;
1088     
1089     	SET_MODULE_OWNER(dev);
1090     
1091     	/* start address of 1 --> forced detection */
1092     
1093     	if (dev->mem_start == 1)
1094     		force_detect = 1;
1095     
1096     	/* search through slots */
1097     
1098     	if (dev != NULL) {
1099     		base = dev->mem_start;
1100     		irq = dev->irq;
1101     	}
1102     	slot = dofind(&junior, startslot);
1103     
1104     	while (slot != -1) {
1105     		/* deduce card addresses */
1106     
1107     		getaddrs(slot, junior, &base, &irq, &medium);
1108     
1109     #if LINUX_VERSION_CODE >= 0x020300
1110     		/* slot already in use ? */
1111     
1112     		if (mca_is_adapter_used(slot)) {
1113     			slot = dofind(&junior, slot + 1);
1114     			continue;
1115     		}
1116     #endif
1117     
1118     		/* were we looking for something different ? */
1119     
1120     		if ((dev->irq != 0) || (dev->mem_start != 0)) {
1121     			if ((dev->irq != 0) && (dev->irq != irq)) {
1122     				slot = dofind(&junior, slot + 1);
1123     				continue;
1124     			}
1125     			if ((dev->mem_start != 0)
1126     			    && (dev->mem_start != base)) {
1127     				slot = dofind(&junior, slot + 1);
1128     				continue;
1129     			}
1130     		}
1131     
1132     		/* found something that matches */
1133     
1134     		break;
1135     	}
1136     
1137     	/* nothing found ? */
1138     
1139     	if (slot == -1)
1140     		return ((base != 0) || (irq != 0)) ? ENXIO : ENODEV;
1141     
1142     	/* make procfs entries */
1143     
1144     	if (junior)
1145     		mca_set_adapter_name(slot,
1146     				     "SKNET junior MC2 Ethernet Adapter");
1147     	else
1148     		mca_set_adapter_name(slot, "SKNET MC2+ Ethernet Adapter");
1149     	mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
1150     
1151     #if LINUX_VERSION_CODE >= 0x020200
1152     	mca_mark_as_used(slot);
1153     #endif
1154     
1155     	/* announce success */
1156     	printk("%s: SKNet %s adapter found in slot %d\n", dev->name,
1157     	       junior ? "Junior MC2" : "MC2+", slot + 1);
1158     
1159     	/* allocate structure */
1160     	priv = dev->priv =
1161     	    (skmca_priv *) kmalloc(sizeof(skmca_priv), GFP_KERNEL);
1162     	if (!priv)
1163     		return -ENOMEM;
1164     	priv->slot = slot;
1165     	priv->macbase = base + 0x3fc0;
1166     	priv->ioregaddr = base + 0x3ff0;
1167     	priv->ctrladdr = base + 0x3ff2;
1168     	priv->cmdaddr = base + 0x3ff3;
1169     	priv->medium = medium;
1170     	memset(&(priv->stat), 0, sizeof(struct net_device_stats));
1171     
1172     	/* set base + irq for this device (irq not allocated so far) */
1173     	dev->irq = 0;
1174     	dev->mem_start = base;
1175     	dev->mem_end = base + 0x4000;
1176     
1177     	/* autoprobe ? */
1178     	if (irq < 0) {
1179     		int nirq;
1180     
1181     		printk
1182     		    ("%s: ambigous POS bit combination, must probe for IRQ...\n",
1183     		     dev->name);
1184     		nirq = ProbeIRQ(dev);
1185     		if (nirq <= 0)
1186     			printk("%s: IRQ probe failed, assuming IRQ %d",
1187     			       dev->name, priv->realirq = -irq);
1188     		else
1189     			priv->realirq = nirq;
1190     	} else
1191     		priv->realirq = irq;
1192     
1193     	/* set methods */
1194     	dev->open = skmca_open;
1195     	dev->stop = skmca_close;
1196     	dev->set_config = skmca_config;
1197     	dev->hard_start_xmit = skmca_tx;
1198     	dev->do_ioctl = NULL;
1199     	dev->get_stats = skmca_stats;
1200     	dev->set_multicast_list = skmca_set_multicast_list;
1201     	dev->flags |= IFF_MULTICAST;
1202     
1203     	/* generic setup */
1204     	ether_setup(dev);
1205     
1206     	/* copy out MAC address */
1207     	for (i = 0; i < 6; i++)
1208     		dev->dev_addr[i] = SKMCA_READB(priv->macbase + (i << 1));
1209     
1210     	/* print config */
1211     	printk("%s: IRQ %d, memory %#lx-%#lx, "
1212     	       "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
1213     	       dev->name, priv->realirq, dev->mem_start, dev->mem_end - 1,
1214     	       dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1215     	       dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1216     	printk("%s: %s medium\n", dev->name, MediaNames[priv->medium]);
1217     
1218     	/* reset board */
1219     
1220     	ResetBoard(dev);
1221     
1222     	startslot = slot + 1;
1223     
1224     	return 0;
1225     }
1226     
1227     /* ------------------------------------------------------------------------
1228      * modularization support
1229      * ------------------------------------------------------------------------ */
1230     
1231     #ifdef MODULE
1232     
1233     #define DEVMAX 5
1234     
1235     #if (LINUX_VERSION_CODE >= 0x020369)
1236     static struct SKMCA_NETDEV moddevs[DEVMAX] =
1237         { {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1238     {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1239     {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1240     {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1241     {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
1242     };
1243     #else
1244     static char NameSpace[8 * DEVMAX];
1245     static struct SKMCA_NETDEV moddevs[DEVMAX] =
1246         { {NameSpace + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1247     {NameSpace + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1248     {NameSpace + 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1249     {NameSpace + 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1250     {NameSpace + 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
1251     };
1252     #endif
1253     
1254     int irq;
1255     int io;
1256     
1257     int init_module(void)
1258     {
1259     	int z, res;
1260     
1261     	startslot = 0;
1262     	for (z = 0; z < DEVMAX; z++) {
1263     		strcpy(moddevs[z].name, "     ");
1264     		res = register_netdev(moddevs + z);
1265     		if (res != 0)
1266     			return (z > 0) ? 0 : -EIO;
1267     	}
1268     
1269     	return 0;
1270     }
1271     
1272     void cleanup_module(void)
1273     {
1274     	struct SKMCA_NETDEV *dev;
1275     	skmca_priv *priv;
1276     	int z;
1277     
1278     	if (MOD_IN_USE) {
1279     		printk("cannot unload, module in use\n");
1280     		return;
1281     	}
1282     
1283     	for (z = 0; z < DEVMAX; z++) {
1284     		dev = moddevs + z;
1285     		if (dev->priv != NULL) {
1286     			priv = (skmca_priv *) dev->priv;
1287     			DeinitBoard(dev);
1288     			if (dev->irq != 0)
1289     				free_irq(dev->irq, dev);
1290     			dev->irq = 0;
1291     			unregister_netdev(dev);
1292     #if LINUX_VERSION_CODE >= 0x020200
1293     			mca_mark_as_unused(priv->slot);
1294     #endif
1295     			mca_set_adapter_procfn(priv->slot, NULL, NULL);
1296     			kfree(dev->priv);
1297     			dev->priv = NULL;
1298     		}
1299     	}
1300     }
1301     #endif				/* MODULE */
1302