Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / net / sk_mca.c
blob49823291a751026f22802953b29eb75679e36f7a
1 /*
2 net-3-driver for the SKNET MCA-based cards
4 This is an extension to the Linux operating system, and is covered by the
5 same Gnu Public License that covers that work.
7 Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de, aarnold@elsa.de)
9 This driver is based both on the 3C523 driver and the SK_G16 driver.
11 paper sources:
12 'PC Hardware: Aufbau, Funktionsweise, Programmierung' by
13 Hans-Peter Messmer for the basic Microchannel stuff
15 'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
16 for help on Ethernet driver programming
18 'Ethernet/IEEE 802.3 Family 1992 World Network Data Book/Handbook' by AMD
19 for documentation on the AM7990 LANCE
21 'SKNET Personal Technisches Manual', Version 1.2 by Schneider&Koch
22 for documentation on the Junior board
24 'SK-NET MC2+ Technical Manual", Version 1.1 by Schneider&Koch for
25 documentation on the MC2 bord
27 A big thank you to the S&K support for providing me so quickly with
28 documentation!
30 Also see http://www.syskonnect.com/
32 Missing things:
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
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
82 *************************************************************************/
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/malloc.h>
90 #include <linux/interrupt.h>
91 #include <linux/delay.h>
92 #include <linux/time.h>
93 #include <linux/mca.h>
94 #include <asm/processor.h>
95 #include <asm/bitops.h>
96 #include <asm/io.h>
98 #include <linux/module.h>
99 #include <linux/version.h>
101 #include <linux/netdevice.h>
102 #include <linux/etherdevice.h>
103 #include <linux/skbuff.h>
105 #define _SK_MCA_DRIVER_
106 #include"sk_mca.h"
108 /* ------------------------------------------------------------------------
109 * global static data - not more since we can handle multiple boards and
110 * have to pack all state info into the device struct!
111 * ------------------------------------------------------------------------ */
113 static char*MediaNames[Media_Count] =
114 {"10Base2","10BaseT","10Base5","Unknown"};
116 static unsigned char poly[] =
117 {1,1,1,0,1,1,0,1,1,0,1,1,1,0,0,0,
118 1,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0
121 /* ------------------------------------------------------------------------
122 * private subfunctions
123 * ------------------------------------------------------------------------ */
125 /* dump parts of shared memory - only needed during debugging */
127 #ifdef DEBUG
128 static voiddumpmem(struct SKMCA_NETDEV *dev, u32 start, u32 len)
130 int z;
132 for(z =0; z < len; z++) {
133 if((z &15) ==0)
134 printk("%04x:", z);
135 printk(" %02x",SKMCA_READB(dev->mem_start + start + z));
136 if((z &15) ==15)
137 printk("\n");
141 /* print exact time - ditto */
143 static voidPrTime(void)
145 struct timeval tv;
147 do_gettimeofday(&tv);
148 printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);
150 #endif
152 /* deduce resources out of POS registers */
154 static voidgetaddrs(int slot,int junior,int*base,int*irq,
155 skmca_medium * medium)
157 u_char pos0, pos1, pos2;
159 if(junior) {
160 pos0 =mca_read_stored_pos(slot,2);
161 *base = ((pos0 &0x0e) <<13) +0xc0000;
162 *irq = ((pos0 &0x10) >>4) +10;
163 *medium = Media_Unknown;
164 }else{
165 /* reset POS 104 Bits 0+1 so the shared memory region goes to the
166 configured area between 640K and 1M. Afterwards, enable the MC2.
167 I really don't know what rode SK to do this... */
169 mca_write_pos(slot,4,
170 mca_read_stored_pos(slot,4) &0xfc);
171 mca_write_pos(slot,2,
172 mca_read_stored_pos(slot,2) |0x01);
174 pos1 =mca_read_stored_pos(slot,3);
175 pos2 =mca_read_stored_pos(slot,4);
176 *base = ((pos1 &0x07) <<14) +0xc0000;
177 switch(pos2 &0x0c) {
178 case0:
179 *irq =3;
180 break;
181 case4:
182 *irq =5;
183 break;
184 case8:
185 *irq = -10;
186 break;
187 case12:
188 *irq = -11;
189 break;
191 *medium = (pos2 >>6) &3;
195 /* check for both cards:
196 When the MC2 is turned off, it was configured for more than 15MB RAM,
197 is disabled and won't get detected using the standard probe. We
198 therefore have to scan the slots manually :-( */
200 static intdofind(int*junior,int firstslot)
202 int slot;
203 unsigned int id;
205 for(slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++) {
206 id =mca_read_stored_pos(slot,0)
207 + (((unsigned int)mca_read_stored_pos(slot,1)) <<8);
209 *junior =0;
210 if(id == SKNET_MCA_ID)
211 return slot;
212 *junior =1;
213 if(id == SKNET_JUNIOR_MCA_ID)
214 return slot;
216 return MCA_NOTFOUND;
219 /* reset the whole board */
221 static voidResetBoard(struct SKMCA_NETDEV *dev)
223 skmca_priv *priv = (skmca_priv *) dev->priv;
225 SKMCA_WRITEB(CTRL_RESET_ON, priv->ctrladdr);
226 udelay(10);
227 SKMCA_WRITEB(CTRL_RESET_OFF, priv->ctrladdr);
230 /* wait for LANCE interface to become not busy */
232 static intWaitLANCE(struct SKMCA_NETDEV *dev)
234 skmca_priv *priv = (skmca_priv *) dev->priv;
235 int t =0;
237 while((SKMCA_READB(priv->ctrladdr) & STAT_IO_BUSY) ==
238 STAT_IO_BUSY) {
239 udelay(1);
240 if(++t >1000) {
241 printk("%s: LANCE access timeout", dev->name);
242 return0;
246 return1;
249 /* set LANCE register - must be atomic */
251 static voidSetLANCE(struct SKMCA_NETDEV *dev, u16 addr, u16 value)
253 skmca_priv *priv = (skmca_priv *) dev->priv;
254 unsigned long flags;
256 /* disable interrupts */
258 save_flags(flags);
259 cli();
261 /* wait until no transfer is pending */
263 WaitLANCE(dev);
265 /* transfer register address to RAP */
267 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
268 priv->ctrladdr);
269 SKMCA_WRITEW(addr, priv->ioregaddr);
270 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
271 udelay(1);
272 WaitLANCE(dev);
274 /* transfer data to register */
276 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA,
277 priv->ctrladdr);
278 SKMCA_WRITEW(value, priv->ioregaddr);
279 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
280 udelay(1);
281 WaitLANCE(dev);
283 /* reenable interrupts */
285 restore_flags(flags);
288 /* get LANCE register */
290 static u16 GetLANCE(struct SKMCA_NETDEV *dev, u16 addr)
292 skmca_priv *priv = (skmca_priv *) dev->priv;
293 unsigned long flags;
294 unsigned int res;
296 /* disable interrupts */
298 save_flags(flags);
299 cli();
301 /* wait until no transfer is pending */
303 WaitLANCE(dev);
305 /* transfer register address to RAP */
307 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
308 priv->ctrladdr);
309 SKMCA_WRITEW(addr, priv->ioregaddr);
310 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
311 udelay(1);
312 WaitLANCE(dev);
314 /* transfer data from register */
316 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA,
317 priv->ctrladdr);
318 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
319 udelay(1);
320 WaitLANCE(dev);
321 res =SKMCA_READW(priv->ioregaddr);
323 /* reenable interrupts */
325 restore_flags(flags);
327 return res;
330 /* build up descriptors in shared RAM */
332 static voidInitDscrs(struct SKMCA_NETDEV *dev)
334 u32 bufaddr;
336 /* Set up Tx descriptors. The board has only 16K RAM so bits 16..23
337 are always 0. */
339 bufaddr = RAM_DATABASE;
341 LANCE_TxDescr descr;
342 int z;
344 for(z =0; z < TXCOUNT; z++) {
345 descr.LowAddr = bufaddr;
346 descr.Flags =0;
347 descr.Len =0xf000;
348 descr.Status =0;
349 SKMCA_TOIO(dev->mem_start + RAM_TXBASE +
350 (z *sizeof(LANCE_TxDescr)), &descr,
351 sizeof(LANCE_TxDescr));
352 SKMCA_SETIO(dev->mem_start + bufaddr,0,
353 RAM_BUFSIZE);
354 bufaddr += RAM_BUFSIZE;
358 /* do the same for the Rx descriptors */
361 LANCE_RxDescr descr;
362 int z;
364 for(z =0; z < RXCOUNT; z++) {
365 descr.LowAddr = bufaddr;
366 descr.Flags = RXDSCR_FLAGS_OWN;
367 descr.MaxLen = -RAM_BUFSIZE;
368 descr.Len =0;
369 SKMCA_TOIO(dev->mem_start + RAM_RXBASE +
370 (z *sizeof(LANCE_RxDescr)), &descr,
371 sizeof(LANCE_RxDescr));
372 SKMCA_SETIO(dev->mem_start + bufaddr,0,
373 RAM_BUFSIZE);
374 bufaddr += RAM_BUFSIZE;
379 /* calculate the hash bit position for a given multicast address
380 taken more or less directly from the AMD datasheet... */
382 static voidUpdateCRC(unsigned char*CRC,int bit)
384 int j;
386 /* shift CRC one bit */
388 memmove(CRC +1, CRC,32*sizeof(unsigned char));
389 CRC[0] =0;
391 /* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */
393 if(bit ^ CRC[32])
394 for(j =0; j <32; j++)
395 CRC[j] ^= poly[j];
398 static unsigned intGetHash(char*address)
400 unsigned char CRC[33];
401 int i, byte, hashcode;
403 /* a multicast address has bit 0 in the first byte set */
405 if((address[0] &1) ==0)
406 return-1;
408 /* initialize CRC */
410 memset(CRC,1,sizeof(CRC));
412 /* loop through address bits */
414 for(byte =0; byte <6; byte++)
415 for(i =0; i <8; i++)
416 UpdateCRC(CRC, (address[byte] >> i) &1);
418 /* hashcode is the 6 least significant bits of the CRC */
420 hashcode =0;
421 for(i =0; i <6; i++)
422 hashcode = (hashcode <<1) + CRC[i];
423 return hashcode;
426 /* feed ready-built initialization block into LANCE */
428 static voidInitLANCE(struct SKMCA_NETDEV *dev)
430 skmca_priv *priv = (skmca_priv *) dev->priv;
432 /* build up descriptors. */
434 InitDscrs(dev);
436 /* next RX descriptor to be read is the first one. Since the LANCE
437 will start from the beginning after initialization, we have to
438 reset out pointers too. */
440 priv->nextrx =0;
442 /* no TX descriptors active */
444 priv->nexttxput = priv->nexttxdone = priv->txbusy =0;
446 /* set up the LANCE bus control register - constant for SKnet boards */
448 SetLANCE(dev, LANCE_CSR3,
449 CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD);
451 /* write address of initialization block into LANCE */
453 SetLANCE(dev, LANCE_CSR1, RAM_INITBASE &0xffff);
454 SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >>16) &0xff);
456 /* we don't get ready until the LANCE has read the init block */
458 #if (LINUX_VERSION_CODE >= 0x02032a)
459 netif_stop_queue(dev);
460 #else
461 dev->tbusy =1;
462 #endif
464 /* let LANCE read the initialization block. LANCE is ready
465 when we receive the corresponding interrupt. */
467 SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);
470 /* stop the LANCE so we can reinitialize it */
472 static voidStopLANCE(struct SKMCA_NETDEV *dev)
474 /* can't take frames any more */
476 #if (LINUX_VERSION_CODE >= 0x02032a)
477 netif_stop_queue(dev);
478 #else
479 dev->tbusy =1;
480 #endif
482 /* disable interrupts, stop it */
484 SetLANCE(dev, LANCE_CSR0, CSR0_STOP);
487 /* initialize card and LANCE for proper operation */
489 static voidInitBoard(struct SKMCA_NETDEV *dev)
491 LANCE_InitBlock block;
493 /* Lay out the shared RAM - first we create the init block for the LANCE.
494 We do not overwrite it later because we need it again when we switch
495 promiscous mode on/off. */
497 block.Mode =0;
498 if(dev->flags & IFF_PROMISC)
499 block.Mode |= LANCE_INIT_PROM;
500 memcpy(block.PAdr, dev->dev_addr,6);
501 memset(block.LAdrF,0,sizeof(block.LAdrF));
502 block.RdrP = (RAM_RXBASE &0xffffff) | (LRXCOUNT <<29);
503 block.TdrP = (RAM_TXBASE &0xffffff) | (LTXCOUNT <<29);
505 SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block,sizeof(block));
507 /* initialize LANCE. Implicitly sets up other structures in RAM. */
509 InitLANCE(dev);
512 /* deinitialize card and LANCE */
514 static voidDeinitBoard(struct SKMCA_NETDEV *dev)
516 /* stop LANCE */
518 StopLANCE(dev);
520 /* reset board */
522 ResetBoard(dev);
525 /* probe for device's irq */
527 static intProbeIRQ(struct SKMCA_NETDEV *dev)
529 unsigned long imaskval, njiffies, irq;
530 u16 csr0val;
532 /* enable all interrupts */
534 imaskval =probe_irq_on();
536 /* initialize the board. Wait for interrupt 'Initialization done'. */
538 ResetBoard(dev);
539 InitBoard(dev);
541 njiffies = jiffies +100;
543 csr0val =GetLANCE(dev, LANCE_CSR0);
545 while(((csr0val & CSR0_IDON) ==0) && (jiffies != njiffies));
547 /* turn of interrupts again */
549 irq =probe_irq_off(imaskval);
551 /* if we found something, ack the interrupt */
553 if(irq)
554 SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_IDON);
556 /* back to idle state */
558 DeinitBoard(dev);
560 return irq;
563 /* ------------------------------------------------------------------------
564 * interrupt handler(s)
565 * ------------------------------------------------------------------------ */
567 /* LANCE has read initialization block -> start it */
569 static u16 irqstart_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
571 /* now we're ready to transmit */
573 #if (LINUX_VERSION_CODE >= 0x02032a)
574 netif_wake_queue(dev);
575 #else
576 dev->tbusy =0;
577 #endif
579 /* reset IDON bit, start LANCE */
581 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT);
582 returnGetLANCE(dev, LANCE_CSR0);
585 /* did we loose blocks due to a FIFO overrun ? */
587 static u16 irqmiss_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
589 skmca_priv *priv = (skmca_priv *) dev->priv;
591 /* update statistics */
593 priv->stat.rx_fifo_errors++;
595 /* reset MISS bit */
597 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_MISS);
598 returnGetLANCE(dev, LANCE_CSR0);
601 /* receive interrupt */
603 static u16 irqrx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
605 skmca_priv *priv = (skmca_priv *) dev->priv;
606 LANCE_RxDescr descr;
607 unsigned int descraddr;
609 /* run through queue until we reach a descriptor we do not own */
611 descraddr = RAM_RXBASE + (priv->nextrx *sizeof(LANCE_RxDescr));
612 while(1) {
613 /* read descriptor */
614 SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
615 sizeof(LANCE_RxDescr));
617 /* if we reach a descriptor we do not own, we're done */
618 if((descr.Flags & RXDSCR_FLAGS_OWN) !=0)
619 break;
621 #ifdef DEBUG
622 PrTime();
623 printk("Receive packet on descr %d len %d\n", priv->nextrx,
624 descr.Len);
625 #endif
627 /* erroneous packet ? */
628 if((descr.Flags & RXDSCR_FLAGS_ERR) !=0) {
629 priv->stat.rx_errors++;
630 if((descr.Flags & RXDSCR_FLAGS_CRC) !=0)
631 priv->stat.rx_crc_errors++;
632 else if((descr.Flags & RXDSCR_FLAGS_CRC) !=0)
633 priv->stat.rx_frame_errors++;
634 else if((descr.Flags & RXDSCR_FLAGS_OFLO) !=0)
635 priv->stat.rx_fifo_errors++;
638 /* good packet ? */
639 else{
640 struct sk_buff *skb;
642 skb =dev_alloc_skb(descr.Len +2);
643 if(skb == NULL)
644 priv->stat.rx_dropped++;
645 else{
646 SKMCA_FROMIO(skb_put(skb, descr.Len),
647 dev->mem_start +
648 descr.LowAddr, descr.Len);
649 skb->dev = dev;
650 skb->protocol =eth_type_trans(skb, dev);
651 skb->ip_summed = CHECKSUM_NONE;
652 priv->stat.rx_packets++;
653 #if LINUX_VERSION_CODE >= 0x020119/* byte counters for >= 2.1.25 */
654 priv->stat.rx_bytes += descr.Len;
655 #endif
656 netif_rx(skb);
660 /* give descriptor back to LANCE */
661 descr.Len =0;
662 descr.Flags |= RXDSCR_FLAGS_OWN;
664 /* update descriptor in shared RAM */
665 SKMCA_TOIO(dev->mem_start + descraddr, &descr,
666 sizeof(LANCE_RxDescr));
668 /* go to next descriptor */
669 priv->nextrx++;
670 descraddr +=sizeof(LANCE_RxDescr);
671 if(priv->nextrx >= RXCOUNT) {
672 priv->nextrx =0;
673 descraddr = RAM_RXBASE;
677 /* reset RINT bit */
679 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_RINT);
680 returnGetLANCE(dev, LANCE_CSR0);
683 /* transmit interrupt */
685 static u16 irqtx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
687 skmca_priv *priv = (skmca_priv *) dev->priv;
688 LANCE_TxDescr descr;
689 unsigned int descraddr;
691 /* check descriptors at most until no busy one is left */
693 descraddr =
694 RAM_TXBASE + (priv->nexttxdone *sizeof(LANCE_TxDescr));
695 while(priv->txbusy >0) {
696 /* read descriptor */
697 SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
698 sizeof(LANCE_TxDescr));
700 /* if the LANCE still owns this one, we've worked out all sent packets */
701 if((descr.Flags & TXDSCR_FLAGS_OWN) !=0)
702 break;
704 #ifdef DEBUG
705 PrTime();
706 printk("Send packet done on descr %d\n", priv->nexttxdone);
707 #endif
709 /* update statistics */
710 if((descr.Flags & TXDSCR_FLAGS_ERR) ==0) {
711 priv->stat.tx_packets++;
712 #if LINUX_VERSION_CODE >= 0x020119/* byte counters for >= 2.1.25 */
713 priv->stat.tx_bytes++;
714 #endif
715 }else{
716 priv->stat.tx_errors++;
717 if((descr.Status & TXDSCR_STATUS_UFLO) !=0) {
718 priv->stat.tx_fifo_errors++;
719 InitLANCE(dev);
721 else
722 if((descr.Status & TXDSCR_STATUS_LCOL) !=
723 0) priv->stat.tx_window_errors++;
724 else if((descr.Status & TXDSCR_STATUS_LCAR) !=0)
725 priv->stat.tx_carrier_errors++;
726 else if((descr.Status & TXDSCR_STATUS_RTRY) !=0)
727 priv->stat.tx_aborted_errors++;
730 /* go to next descriptor */
731 priv->nexttxdone++;
732 descraddr +=sizeof(LANCE_TxDescr);
733 if(priv->nexttxdone >= TXCOUNT) {
734 priv->nexttxdone =0;
735 descraddr = RAM_TXBASE;
737 priv->txbusy--;
740 /* reset TX interrupt bit */
742 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_TINT);
743 oldcsr0 =GetLANCE(dev, LANCE_CSR0);
745 /* at least one descriptor is freed. Therefore we can accept
746 a new one */
747 /* inform upper layers we're in business again */
749 #if (LINUX_VERSION_CODE >= 0x02032a)
750 netif_wake_queue(dev);
751 #else
752 dev->tbusy =0;
753 mark_bh(NET_BH);
754 #endif
756 return oldcsr0;
759 /* general interrupt entry */
761 static voidirq_handler(int irq,void*device,struct pt_regs *regs)
763 struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) device;
764 u16 csr0val;
766 /* read CSR0 to get interrupt cause */
768 csr0val =GetLANCE(dev, LANCE_CSR0);
770 /* in case we're not meant... */
772 if((csr0val & CSR0_INTR) ==0)
773 return;
775 #if (LINUX_VERSION_CODE >= 0x02032a)
776 #if 0
777 set_bit(LINK_STATE_RXSEM, &dev->state);
778 #endif
779 #else
780 dev->interrupt =1;
781 #endif
783 /* loop through the interrupt bits until everything is clear */
786 if((csr0val & CSR0_IDON) !=0)
787 csr0val =irqstart_handler(dev, csr0val);
788 if((csr0val & CSR0_RINT) !=0)
789 csr0val =irqrx_handler(dev, csr0val);
790 if((csr0val & CSR0_MISS) !=0)
791 csr0val =irqmiss_handler(dev, csr0val);
792 if((csr0val & CSR0_TINT) !=0)
793 csr0val =irqtx_handler(dev, csr0val);
794 if((csr0val & CSR0_MERR) !=0) {
795 SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_MERR);
796 csr0val =GetLANCE(dev, LANCE_CSR0);
798 if((csr0val & CSR0_BABL) !=0) {
799 SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_BABL);
800 csr0val =GetLANCE(dev, LANCE_CSR0);
803 while((csr0val & CSR0_INTR) !=0);
805 #if (LINUX_VERSION_CODE >= 0x02032a)
806 #if 0
807 clear_bit(LINK_STATE_RXSEM, &dev->state);
808 #endif
809 #else
810 dev->interrupt =0;
811 #endif
814 /* ------------------------------------------------------------------------
815 * driver methods
816 * ------------------------------------------------------------------------ */
818 /* MCA info */
820 static intskmca_getinfo(char*buf,int slot,void*d)
822 int len =0, i;
823 struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) d;
824 skmca_priv *priv;
826 /* can't say anything about an uninitialized device... */
828 if(dev == NULL)
829 return len;
830 if(dev->priv == NULL)
831 return len;
832 priv = (skmca_priv *) dev->priv;
834 /* print info */
836 len +=sprintf(buf + len,"IRQ: %d\n", priv->realirq);
837 len +=sprintf(buf + len,"Memory: %#lx-%#lx\n", dev->mem_start,
838 dev->mem_end -1);
839 len +=
840 sprintf(buf + len,"Transceiver: %s\n",
841 MediaNames[priv->medium]);
842 len +=sprintf(buf + len,"Device: %s\n", dev->name);
843 len +=sprintf(buf + len,"MAC address:");
844 for(i =0; i <6; i++)
845 len +=sprintf(buf + len," %02x", dev->dev_addr[i]);
846 buf[len++] ='\n';
847 buf[len] =0;
849 return len;
852 /* open driver. Means also initialization and start of LANCE */
854 static intskmca_open(struct SKMCA_NETDEV *dev)
856 int result;
857 skmca_priv *priv = (skmca_priv *) dev->priv;
859 /* register resources - only necessary for IRQ */
860 result =
861 request_irq(priv->realirq, irq_handler,
862 SA_SHIRQ | SA_SAMPLE_RANDOM,"sk_mca", dev);
863 if(result !=0) {
864 printk("%s: failed to register irq %d\n", dev->name,
865 dev->irq);
866 return result;
868 dev->irq = priv->realirq;
870 /* set up the card and LANCE */
872 InitBoard(dev);
874 /* set up flags */
876 #if (LINUX_VERSION_CODE >= 0x02032a)
877 netif_start_queue(dev);
878 #else
879 dev->interrupt =0;
880 dev->tbusy =0;
881 dev->start =0;
882 MOD_INC_USE_COUNT;
883 #endif
885 return0;
888 /* close driver. Shut down board and free allocated resources */
890 static intskmca_close(struct SKMCA_NETDEV *dev)
892 /* turn off board */
893 DeinitBoard(dev);
895 /* release resources */
896 if(dev->irq !=0)
897 free_irq(dev->irq, dev);
898 dev->irq =0;
900 #if (LINUX_VERSION_CODE < 0x02032a)
901 MOD_DEC_USE_COUNT;
902 #endif
904 return0;
907 /* transmit a block. */
909 static intskmca_tx(struct sk_buff *skb,struct SKMCA_NETDEV *dev)
911 skmca_priv *priv = (skmca_priv *) dev->priv;
912 LANCE_TxDescr descr;
913 unsigned int address;
914 int tmplen, retval =0;
915 unsigned long flags;
917 /* if we get called with a NULL descriptor, the Ethernet layer thinks
918 our card is stuck an we should reset it. We'll do this completely: */
920 if(skb == NULL) {
921 DeinitBoard(dev);
922 InitBoard(dev);
923 return0;/* don't try to free the block here ;-) */
926 /* is there space in the Tx queue ? If no, the upper layer gave us a
927 packet in spite of us not being ready and is really in trouble.
928 We'll do the dropping for him: */
929 if(priv->txbusy >= TXCOUNT) {
930 priv->stat.tx_dropped++;
931 retval = -EIO;
932 goto tx_done;
935 /* get TX descriptor */
936 address = RAM_TXBASE + (priv->nexttxput *sizeof(LANCE_TxDescr));
937 SKMCA_FROMIO(&descr, dev->mem_start + address,
938 sizeof(LANCE_TxDescr));
940 /* enter packet length as 2s complement - assure minimum length */
941 tmplen = skb->len;
942 if(tmplen <60)
943 tmplen =60;
944 descr.Len =65536- tmplen;
946 /* copy filler into RAM - in case we're filling up...
947 we're filling a bit more than necessary, but that doesn't harm
948 since the buffer is far larger... */
949 if(tmplen > skb->len) {
950 char*fill ="NetBSD is a nice OS too! ";
951 unsigned int destoffs =0, l =strlen(fill);
953 while(destoffs < tmplen) {
954 SKMCA_TOIO(dev->mem_start + descr.LowAddr +
955 destoffs, fill, l);
956 destoffs += l;
960 /* do the real data copying */
961 SKMCA_TOIO(dev->mem_start + descr.LowAddr, skb->data, skb->len);
963 /* hand descriptor over to LANCE - this is the first and last chunk */
964 descr.Flags =
965 TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
967 #ifdef DEBUG
968 PrTime();
969 printk("Send packet on descr %d len %d\n", priv->nexttxput,
970 skb->len);
971 #endif
973 /* one more descriptor busy */
974 save_flags(flags);
975 cli();
976 priv->nexttxput++;
977 if(priv->nexttxput >= TXCOUNT)
978 priv->nexttxput =0;
979 priv->txbusy++;
981 /* are we saturated ? */
983 if(priv->txbusy >= TXCOUNT)
984 #if (LINUX_VERSION_CODE >= 0x02032a)
985 netif_stop_queue(dev);
986 #else
987 dev->tbusy =1;
988 #endif
990 /* write descriptor back to RAM */
991 SKMCA_TOIO(dev->mem_start + address, &descr,
992 sizeof(LANCE_TxDescr));
994 /* if no descriptors were active, give the LANCE a hint to read it
995 immediately */
997 if(priv->txbusy ==0)
998 SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
1000 restore_flags(flags);
1002 tx_done:
1004 /* When did that change exactly ? */
1006 #if LINUX_VERSION_CODE >= 0x020200
1007 dev_kfree_skb(skb);
1008 #else
1009 dev_kfree_skb(skb, FREE_WRITE);
1010 #endif
1011 return retval;
1014 /* return pointer to Ethernet statistics */
1016 static struct net_device_stats *skmca_stats(struct SKMCA_NETDEV *dev)
1018 skmca_priv *priv = (skmca_priv *) dev->priv;
1020 return&(priv->stat);
1023 /* we don't support runtime reconfiguration, since an MCA card can
1024 be unambigously identified by its POS registers. */
1026 static intskmca_config(struct SKMCA_NETDEV *dev,struct ifmap *map)
1028 return0;
1031 /* switch receiver mode. We use the LANCE's multicast filter to prefilter
1032 multicast addresses. */
1034 static voidskmca_set_multicast_list(struct SKMCA_NETDEV *dev)
1036 LANCE_InitBlock block;
1038 /* first stop the LANCE... */
1039 StopLANCE(dev);
1041 /* ...then modify the initialization block... */
1042 SKMCA_FROMIO(&block, dev->mem_start + RAM_INITBASE,sizeof(block));
1043 if(dev->flags & IFF_PROMISC)
1044 block.Mode |= LANCE_INIT_PROM;
1045 else
1046 block.Mode &= ~LANCE_INIT_PROM;
1048 if(dev->flags & IFF_ALLMULTI) {/* get all multicasts */
1049 memset(block.LAdrF,8,0xff);
1050 }else{/* get selected/no multicasts */
1052 struct dev_mc_list *mptr;
1053 int code;
1055 memset(block.LAdrF,8,0x00);
1056 for(mptr = dev->mc_list; mptr != NULL; mptr = mptr->next) {
1057 code =GetHash(mptr->dmi_addr);
1058 block.LAdrF[(code >>3) &7] |=1<< (code &7);
1062 SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block,sizeof(block));
1064 /* ...then reinit LANCE with the correct flags */
1065 InitLANCE(dev);
1068 /* ------------------------------------------------------------------------
1069 * hardware check
1070 * ------------------------------------------------------------------------ */
1072 static int startslot;/* counts through slots when probing multiple devices */
1074 intskmca_probe(struct SKMCA_NETDEV *dev)
1076 int force_detect =0;
1077 int junior, slot, i;
1078 int base =0, irq =0;
1079 skmca_priv *priv;
1080 skmca_medium medium;
1082 /* can't work without an MCA bus ;-) */
1084 if(MCA_bus ==0)
1085 return-ENODEV;
1087 SET_MODULE_OWNER(dev);
1089 /* start address of 1 --> forced detection */
1091 if(dev->mem_start ==1)
1092 force_detect =1;
1094 /* search through slots */
1096 if(dev != NULL) {
1097 base = dev->mem_start;
1098 irq = dev->irq;
1100 slot =dofind(&junior, startslot);
1102 while(slot != -1) {
1103 /* deduce card addresses */
1105 getaddrs(slot, junior, &base, &irq, &medium);
1107 #if LINUX_VERSION_CODE >= 0x020300
1108 /* slot already in use ? */
1110 if(mca_is_adapter_used(slot)) {
1111 slot =dofind(&junior, slot +1);
1112 continue;
1114 #endif
1116 /* were we looking for something different ? */
1118 if((dev->irq !=0) || (dev->mem_start !=0)) {
1119 if((dev->irq !=0) && (dev->irq != irq)) {
1120 slot =dofind(&junior, slot +1);
1121 continue;
1123 if((dev->mem_start !=0)
1124 && (dev->mem_start != base)) {
1125 slot =dofind(&junior, slot +1);
1126 continue;
1130 /* found something that matches */
1132 break;
1135 /* nothing found ? */
1137 if(slot == -1)
1138 return((base !=0) || (irq !=0)) ? ENXIO : ENODEV;
1140 /* make procfs entries */
1142 if(junior)
1143 mca_set_adapter_name(slot,
1144 "SKNET junior MC2 Ethernet Adapter");
1145 else
1146 mca_set_adapter_name(slot,"SKNET MC2+ Ethernet Adapter");
1147 mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
1149 #if LINUX_VERSION_CODE >= 0x020200
1150 mca_mark_as_used(slot);
1151 #endif
1153 /* announce success */
1154 printk("%s: SKNet %s adapter found in slot %d\n", dev->name,
1155 junior ?"Junior MC2":"MC2+", slot +1);
1157 /* allocate structure */
1158 priv = dev->priv =
1159 (skmca_priv *)kmalloc(sizeof(skmca_priv), GFP_KERNEL);
1160 priv->slot = slot;
1161 priv->macbase = base +0x3fc0;
1162 priv->ioregaddr = base +0x3ff0;
1163 priv->ctrladdr = base +0x3ff2;
1164 priv->cmdaddr = base +0x3ff3;
1165 priv->medium = medium;
1166 memset(&(priv->stat),0,sizeof(struct net_device_stats));
1168 /* set base + irq for this device (irq not allocated so far) */
1169 dev->irq =0;
1170 dev->mem_start = base;
1171 dev->mem_end = base +0x4000;
1173 /* autoprobe ? */
1174 if(irq <0) {
1175 int nirq;
1177 printk
1178 ("%s: ambigous POS bit combination, must probe for IRQ...\n",
1179 dev->name);
1180 nirq =ProbeIRQ(dev);
1181 if(nirq <=0)
1182 printk("%s: IRQ probe failed, assuming IRQ %d",
1183 dev->name, priv->realirq = -irq);
1184 else
1185 priv->realirq = nirq;
1186 }else
1187 priv->realirq = irq;
1189 /* set methods */
1190 dev->open = skmca_open;
1191 dev->stop = skmca_close;
1192 dev->set_config = skmca_config;
1193 dev->hard_start_xmit = skmca_tx;
1194 dev->do_ioctl = NULL;
1195 dev->get_stats = skmca_stats;
1196 dev->set_multicast_list = skmca_set_multicast_list;
1197 dev->flags |= IFF_MULTICAST;
1199 /* generic setup */
1200 ether_setup(dev);
1202 /* copy out MAC address */
1203 for(i =0; i <6; i++)
1204 dev->dev_addr[i] =SKMCA_READB(priv->macbase + (i <<1));
1206 /* print config */
1207 printk("%s: IRQ %d, memory %#lx-%#lx, "
1208 "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
1209 dev->name, priv->realirq, dev->mem_start, dev->mem_end -1,
1210 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1211 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1212 printk("%s: %s medium\n", dev->name, MediaNames[priv->medium]);
1214 /* reset board */
1216 ResetBoard(dev);
1218 startslot = slot +1;
1220 return0;
1223 /* ------------------------------------------------------------------------
1224 * modularization support
1225 * ------------------------------------------------------------------------ */
1227 #ifdef MODULE
1229 #define DEVMAX 5
1231 #if (LINUX_VERSION_CODE >= 0x020369)
1232 static struct SKMCA_NETDEV moddevs[DEVMAX] =
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 {" ",0,0,0,0,0,0,0,0,0, NULL, skmca_probe}
1239 #else
1240 static char NameSpace[8* DEVMAX];
1241 static struct SKMCA_NETDEV moddevs[DEVMAX] =
1242 { {NameSpace +0,0,0,0,0,0,0,0,0,0, NULL, skmca_probe},
1243 {NameSpace +8,0,0,0,0,0,0,0,0,0, NULL, skmca_probe},
1244 {NameSpace +16,0,0,0,0,0,0,0,0,0, NULL, skmca_probe},
1245 {NameSpace +24,0,0,0,0,0,0,0,0,0, NULL, skmca_probe},
1246 {NameSpace +32,0,0,0,0,0,0,0,0,0, NULL, skmca_probe}
1248 #endif
1250 int irq =0;
1251 int io =0;
1253 intinit_module(void)
1255 int z, res;
1257 startslot =0;
1258 for(z =0; z < DEVMAX; z++) {
1259 strcpy(moddevs[z].name," ");
1260 res =register_netdev(moddevs + z);
1261 if(res !=0)
1262 return(z >0) ?0: -EIO;
1265 return0;
1268 voidcleanup_module(void)
1270 struct SKMCA_NETDEV *dev;
1271 skmca_priv *priv;
1272 int z;
1274 if(MOD_IN_USE) {
1275 printk("cannot unload, module in use\n");
1276 return;
1279 for(z =0; z < DEVMAX; z++) {
1280 dev = moddevs + z;
1281 if(dev->priv != NULL) {
1282 priv = (skmca_priv *) dev->priv;
1283 DeinitBoard(dev);
1284 if(dev->irq !=0)
1285 free_irq(dev->irq, dev);
1286 dev->irq =0;
1287 unregister_netdev(dev);
1288 #if LINUX_VERSION_CODE >= 0x020200
1289 mca_mark_as_unused(priv->slot);
1290 #endif
1291 mca_set_adapter_procfn(priv->slot, NULL, NULL);
1292 kfree(dev->priv);
1293 dev->priv = NULL;
1297 #endif/* MODULE */
close