1/*
2net-3-driver for the SKNET MCA-based cards
3
4This is an extension to the Linux operating system, and is covered by the
5same GNU General Public License that covers that work.
6
7Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de, aarnold@elsa.de)
8
9This driver is based both on the 3C523 driver and the SK_G16 driver.
10
11paper 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
39History:
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
114static char *MediaNames[Media_Count] =
115    { "10Base2", "10BaseT", "10Base5", "Unknown" };
116
117static 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
129static 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
144static 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
155static 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
201static 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
222static 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
233static 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
252static 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
291static 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
333static 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
383static 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
399static 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
429static 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
473static 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
490static 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
515static 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
528static 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
570static 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
588static 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
604static 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
687static 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
763static 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#else
779	dev->interrupt = 1;
780#endif
781
782	/* loop through the interrupt bits until everything is clear */
783
784	do {
785		if ((csr0val & CSR0_IDON) != 0)
786			csr0val = irqstart_handler(dev, csr0val);
787		if ((csr0val & CSR0_RINT) != 0)
788			csr0val = irqrx_handler(dev, csr0val);
789		if ((csr0val & CSR0_MISS) != 0)
790			csr0val = irqmiss_handler(dev, csr0val);
791		if ((csr0val & CSR0_TINT) != 0)
792			csr0val = irqtx_handler(dev, csr0val);
793		if ((csr0val & CSR0_MERR) != 0) {
794			SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_MERR);
795			csr0val = GetLANCE(dev, LANCE_CSR0);
796		}
797		if ((csr0val & CSR0_BABL) != 0) {
798			SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_BABL);
799			csr0val = GetLANCE(dev, LANCE_CSR0);
800		}
801	}
802	while ((csr0val & CSR0_INTR) != 0);
803
804#if (LINUX_VERSION_CODE >= 0x02032a)
805#else
806	dev->interrupt = 0;
807#endif
808}
809
810/* ------------------------------------------------------------------------
811 * driver methods
812 * ------------------------------------------------------------------------ */
813
814/* MCA info */
815
816static int skmca_getinfo(char *buf, int slot, void *d)
817{
818	int len = 0, i;
819	struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) d;
820	skmca_priv *priv;
821
822	/* can't say anything about an uninitialized device... */
823
824	if (dev == NULL)
825		return len;
826	if (dev->priv == NULL)
827		return len;
828	priv = (skmca_priv *) dev->priv;
829
830	/* print info */
831
832	len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
833	len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
834		       dev->mem_end - 1);
835	len +=
836	    sprintf(buf + len, "Transceiver: %s\n",
837		    MediaNames[priv->medium]);
838	len += sprintf(buf + len, "Device: %s\n", dev->name);
839	len += sprintf(buf + len, "MAC address:");
840	for (i = 0; i < 6; i++)
841		len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
842	buf[len++] = '\n';
843	buf[len] = 0;
844
845	return len;
846}
847
848/* open driver.  Means also initialization and start of LANCE */
849
850static int skmca_open(struct SKMCA_NETDEV *dev)
851{
852	int result;
853	skmca_priv *priv = (skmca_priv *) dev->priv;
854
855	/* register resources - only necessary for IRQ */
856	result =
857	    request_irq(priv->realirq, irq_handler,
858			SA_SHIRQ | SA_SAMPLE_RANDOM, "sk_mca", dev);
859	if (result != 0) {
860		printk("%s: failed to register irq %d\n", dev->name,
861		       dev->irq);
862		return result;
863	}
864	dev->irq = priv->realirq;
865
866	/* set up the card and LANCE */
867
868	InitBoard(dev);
869
870	/* set up flags */
871
872#if (LINUX_VERSION_CODE >= 0x02032a)
873	netif_start_queue(dev);
874#else
875	dev->interrupt = 0;
876	dev->tbusy = 0;
877	dev->start = 0;
878	MOD_INC_USE_COUNT;
879#endif
880
881	return 0;
882}
883
884/* close driver.  Shut down board and free allocated resources */
885
886static int skmca_close(struct SKMCA_NETDEV *dev)
887{
888	/* turn off board */
889	DeinitBoard(dev);
890
891	/* release resources */
892	if (dev->irq != 0)
893		free_irq(dev->irq, dev);
894	dev->irq = 0;
895
896#if (LINUX_VERSION_CODE < 0x02032a)
897	MOD_DEC_USE_COUNT;
898#endif
899
900	return 0;
901}
902
903/* transmit a block. */
904
905static int skmca_tx(struct sk_buff *skb, struct SKMCA_NETDEV *dev)
906{
907	skmca_priv *priv = (skmca_priv *) dev->priv;
908	LANCE_TxDescr descr;
909	unsigned int address;
910	int tmplen, retval = 0;
911	unsigned long flags;
912
913	/* if we get called with a NULL descriptor, the Ethernet layer thinks
914	   our card is stuck an we should reset it.  We'll do this completely: */
915
916	if (skb == NULL) {
917		DeinitBoard(dev);
918		InitBoard(dev);
919		return 0;	/* don't try to free the block here ;-) */
920	}
921
922	/* is there space in the Tx queue ? If no, the upper layer gave us a
923	   packet in spite of us not being ready and is really in trouble.
924	   We'll do the dropping for him: */
925	if (priv->txbusy >= TXCOUNT) {
926		priv->stat.tx_dropped++;
927		retval = -EIO;
928		goto tx_done;
929	}
930
931	/* get TX descriptor */
932	address = RAM_TXBASE + (priv->nexttxput * sizeof(LANCE_TxDescr));
933	SKMCA_FROMIO(&descr, dev->mem_start + address,
934		     sizeof(LANCE_TxDescr));
935
936	/* enter packet length as 2s complement - assure minimum length */
937	tmplen = skb->len;
938	if (tmplen < 60)
939		tmplen = 60;
940	descr.Len = 65536 - tmplen;
941
942	/* copy filler into RAM - in case we're filling up...
943	   we're filling a bit more than necessary, but that doesn't harm
944	   since the buffer is far larger... */
945	if (tmplen > skb->len) {
946		char *fill = "NetBSD is a nice OS too! ";
947		unsigned int destoffs = 0, l = strlen(fill);
948
949		while (destoffs < tmplen) {
950			SKMCA_TOIO(dev->mem_start + descr.LowAddr +
951				   destoffs, fill, l);
952			destoffs += l;
953		}
954	}
955
956	/* do the real data copying */
957	SKMCA_TOIO(dev->mem_start + descr.LowAddr, skb->data, skb->len);
958
959	/* hand descriptor over to LANCE - this is the first and last chunk */
960	descr.Flags =
961	    TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
962
963#ifdef DEBUG
964	PrTime();
965	printk("Send packet on descr %d len %d\n", priv->nexttxput,
966	       skb->len);
967#endif
968
969	/* one more descriptor busy */
970	save_flags(flags);
971	cli();
972	priv->nexttxput++;
973	if (priv->nexttxput >= TXCOUNT)
974		priv->nexttxput = 0;
975	priv->txbusy++;
976
977	/* are we saturated ? */
978
979	if (priv->txbusy >= TXCOUNT)
980#if (LINUX_VERSION_CODE >= 0x02032a)
981		netif_stop_queue(dev);
982#else
983		dev->tbusy = 1;
984#endif
985
986	/* write descriptor back to RAM */
987	SKMCA_TOIO(dev->mem_start + address, &descr,
988		   sizeof(LANCE_TxDescr));
989
990	/* if no descriptors were active, give the LANCE a hint to read it
991	   immediately */
992
993	if (priv->txbusy == 0)
994		SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
995
996	restore_flags(flags);
997
998      tx_done:
999
1000	/* When did that change exactly ? */
1001
1002#if LINUX_VERSION_CODE >= 0x020200
1003	dev_kfree_skb(skb);
1004#else
1005	dev_kfree_skb(skb, FREE_WRITE);
1006#endif
1007	return retval;
1008}
1009
1010/* return pointer to Ethernet statistics */
1011
1012static struct net_device_stats *skmca_stats(struct SKMCA_NETDEV *dev)
1013{
1014	skmca_priv *priv = (skmca_priv *) dev->priv;
1015
1016	return &(priv->stat);
1017}
1018
1019/* we don't support runtime reconfiguration, since an MCA card can
1020   be unambigously identified by its POS registers. */
1021
1022static int skmca_config(struct SKMCA_NETDEV *dev, struct ifmap *map)
1023{
1024	return 0;
1025}
1026
1027/* switch receiver mode.  We use the LANCE's multicast filter to prefilter
1028   multicast addresses. */
1029
1030static void skmca_set_multicast_list(struct SKMCA_NETDEV *dev)
1031{
1032	LANCE_InitBlock block;
1033
1034	/* first stop the LANCE... */
1035	StopLANCE(dev);
1036
1037	/* ...then modify the initialization block... */
1038	SKMCA_FROMIO(&block, dev->mem_start + RAM_INITBASE, sizeof(block));
1039	if (dev->flags & IFF_PROMISC)
1040		block.Mode |= LANCE_INIT_PROM;
1041	else
1042		block.Mode &= ~LANCE_INIT_PROM;
1043
1044	if (dev->flags & IFF_ALLMULTI) {	/* get all multicasts */
1045		memset(block.LAdrF, 8, 0xff);
1046	} else {		/* get selected/no multicasts */
1047
1048		struct dev_mc_list *mptr;
1049		int code;
1050
1051		memset(block.LAdrF, 8, 0x00);
1052		for (mptr = dev->mc_list; mptr != NULL; mptr = mptr->next) {
1053			code = GetHash(mptr->dmi_addr);
1054			block.LAdrF[(code >> 3) & 7] |= 1 << (code & 7);
1055		}
1056	}
1057
1058	SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
1059
1060	/* ...then reinit LANCE with the correct flags */
1061	InitLANCE(dev);
1062}
1063
1064/* ------------------------------------------------------------------------
1065 * hardware check
1066 * ------------------------------------------------------------------------ */
1067
1068static int startslot;		/* counts through slots when probing multiple devices */
1069
1070int __init skmca_probe(struct SKMCA_NETDEV *dev)
1071{
1072	int force_detect = 0;
1073	int junior, slot, i;
1074	int base = 0, irq = 0;
1075	skmca_priv *priv;
1076	skmca_medium medium;
1077
1078	/* can't work without an MCA bus ;-) */
1079
1080	if (MCA_bus == 0)
1081		return -ENODEV;
1082
1083	SET_MODULE_OWNER(dev);
1084
1085	/* start address of 1 --> forced detection */
1086
1087	if (dev->mem_start == 1)
1088		force_detect = 1;
1089
1090	/* search through slots */
1091
1092	if (dev != NULL) {
1093		base = dev->mem_start;
1094		irq = dev->irq;
1095	}
1096	slot = dofind(&junior, startslot);
1097
1098	while (slot != -1) {
1099		/* deduce card addresses */
1100
1101		getaddrs(slot, junior, &base, &irq, &medium);
1102
1103#if LINUX_VERSION_CODE >= 0x020300
1104		/* slot already in use ? */
1105
1106		if (mca_is_adapter_used(slot)) {
1107			slot = dofind(&junior, slot + 1);
1108			continue;
1109		}
1110#endif
1111
1112		/* were we looking for something different ? */
1113
1114		if ((dev->irq != 0) || (dev->mem_start != 0)) {
1115			if ((dev->irq != 0) && (dev->irq != irq)) {
1116				slot = dofind(&junior, slot + 1);
1117				continue;
1118			}
1119			if ((dev->mem_start != 0)
1120			    && (dev->mem_start != base)) {
1121				slot = dofind(&junior, slot + 1);
1122				continue;
1123			}
1124		}
1125
1126		/* found something that matches */
1127
1128		break;
1129	}
1130
1131	/* nothing found ? */
1132
1133	if (slot == -1)
1134		return ((base != 0) || (irq != 0)) ? ENXIO : ENODEV;
1135
1136	/* make procfs entries */
1137
1138	if (junior)
1139		mca_set_adapter_name(slot,
1140				     "SKNET junior MC2 Ethernet Adapter");
1141	else
1142		mca_set_adapter_name(slot, "SKNET MC2+ Ethernet Adapter");
1143	mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
1144
1145#if LINUX_VERSION_CODE >= 0x020200
1146	mca_mark_as_used(slot);
1147#endif
1148
1149	/* announce success */
1150	printk("%s: SKNet %s adapter found in slot %d\n", dev->name,
1151	       junior ? "Junior MC2" : "MC2+", slot + 1);
1152
1153	/* allocate structure */
1154	priv = dev->priv =
1155	    (skmca_priv *) kmalloc(sizeof(skmca_priv), GFP_KERNEL);
1156	if (!priv)
1157		return -ENOMEM;
1158	priv->slot = slot;
1159	priv->macbase = base + 0x3fc0;
1160	priv->ioregaddr = base + 0x3ff0;
1161	priv->ctrladdr = base + 0x3ff2;
1162	priv->cmdaddr = base + 0x3ff3;
1163	priv->medium = medium;
1164	memset(&(priv->stat), 0, sizeof(struct net_device_stats));
1165
1166	/* set base + irq for this device (irq not allocated so far) */
1167	dev->irq = 0;
1168	dev->mem_start = base;
1169	dev->mem_end = base + 0x4000;
1170
1171	/* autoprobe ? */
1172	if (irq < 0) {
1173		int nirq;
1174
1175		printk
1176		    ("%s: ambigous POS bit combination, must probe for IRQ...\n",
1177		     dev->name);
1178		nirq = ProbeIRQ(dev);
1179		if (nirq <= 0)
1180			printk("%s: IRQ probe failed, assuming IRQ %d",
1181			       dev->name, priv->realirq = -irq);
1182		else
1183			priv->realirq = nirq;
1184	} else
1185		priv->realirq = irq;
1186
1187	/* set methods */
1188	dev->open = skmca_open;
1189	dev->stop = skmca_close;
1190	dev->set_config = skmca_config;
1191	dev->hard_start_xmit = skmca_tx;
1192	dev->do_ioctl = NULL;
1193	dev->get_stats = skmca_stats;
1194	dev->set_multicast_list = skmca_set_multicast_list;
1195	dev->flags |= IFF_MULTICAST;
1196
1197	/* generic setup */
1198	ether_setup(dev);
1199
1200	/* copy out MAC address */
1201	for (i = 0; i < 6; i++)
1202		dev->dev_addr[i] = SKMCA_READB(priv->macbase + (i << 1));
1203
1204	/* print config */
1205	printk("%s: IRQ %d, memory %#lx-%#lx, "
1206	       "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
1207	       dev->name, priv->realirq, dev->mem_start, dev->mem_end - 1,
1208	       dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1209	       dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1210	printk("%s: %s medium\n", dev->name, MediaNames[priv->medium]);
1211
1212	/* reset board */
1213
1214	ResetBoard(dev);
1215
1216	startslot = slot + 1;
1217
1218	return 0;
1219}
1220
1221/* ------------------------------------------------------------------------
1222 * modularization support
1223 * ------------------------------------------------------------------------ */
1224
1225#ifdef MODULE
1226MODULE_LICENSE("GPL");
1227
1228#define DEVMAX 5
1229
1230#if (LINUX_VERSION_CODE >= 0x020369)
1231static struct SKMCA_NETDEV moddevs[DEVMAX] =
1232    { {"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1233{"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1234{"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1235{"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1236{"    ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
1237};
1238#else
1239static char NameSpace[8 * DEVMAX];
1240static struct SKMCA_NETDEV moddevs[DEVMAX] =
1241    { {NameSpace + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1242{NameSpace + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1243{NameSpace + 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1244{NameSpace + 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1245{NameSpace + 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
1246};
1247#endif
1248
1249int irq;
1250int io;
1251
1252int init_module(void)
1253{
1254	int z, res;
1255
1256	startslot = 0;
1257	for (z = 0; z < DEVMAX; z++) {
1258		strcpy(moddevs[z].name, "     ");
1259		res = register_netdev(moddevs + z);
1260		if (res != 0)
1261			return (z > 0) ? 0 : -EIO;
1262	}
1263
1264	return 0;
1265}
1266
1267void cleanup_module(void)
1268{
1269	struct SKMCA_NETDEV *dev;
1270	skmca_priv *priv;
1271	int z;
1272
1273	if (MOD_IN_USE) {
1274		printk("cannot unload, module in use\n");
1275		return;
1276	}
1277
1278	for (z = 0; z < DEVMAX; z++) {
1279		dev = moddevs + z;
1280		if (dev->priv != NULL) {
1281			priv = (skmca_priv *) dev->priv;
1282			DeinitBoard(dev);
1283			if (dev->irq != 0)
1284				free_irq(dev->irq, dev);
1285			dev->irq = 0;
1286			unregister_netdev(dev);
1287#if LINUX_VERSION_CODE >= 0x020200
1288			mca_mark_as_unused(priv->slot);
1289#endif
1290			mca_set_adapter_procfn(priv->slot, NULL, NULL);
1291			kfree(dev->priv);
1292			dev->priv = NULL;
1293		}
1294	}
1295}
1296#endif				/* MODULE */
1297