More meth updates.
[linux-2.6/linux-mips.git] / drivers / net / sk_mca.c
blob35c4a06c27242581680b4116c892e5cca9e9e2e9
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 General 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 initialization 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/string.h>
86 #include <linux/errno.h>
87 #include <linux/ioport.h>
88 #include <linux/slab.h>
89 #include <linux/interrupt.h>
90 #include <linux/delay.h>
91 #include <linux/time.h>
92 #include <linux/mca.h>
93 #include <linux/init.h>
94 #include <linux/module.h>
95 #include <linux/version.h>
96 #include <linux/netdevice.h>
97 #include <linux/etherdevice.h>
98 #include <linux/skbuff.h>
100 #include <asm/processor.h>
101 #include <asm/bitops.h>
102 #include <asm/io.h>
104 #define _SK_MCA_DRIVER_
105 #include "sk_mca.h"
107 /* ------------------------------------------------------------------------
108 * global static data - not more since we can handle multiple boards and
109 * have to pack all state info into the device struct!
110 * ------------------------------------------------------------------------ */
112 static char *MediaNames[Media_Count] =
113 { "10Base2", "10BaseT", "10Base5", "Unknown" };
115 static unsigned char poly[] =
116 { 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0,
117 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
120 /* ------------------------------------------------------------------------
121 * private subfunctions
122 * ------------------------------------------------------------------------ */
124 /* dump parts of shared memory - only needed during debugging */
126 #ifdef DEBUG
127 static void dumpmem(struct SKMCA_NETDEV *dev, u32 start, u32 len)
129 int z;
131 for (z = 0; z < len; z++) {
132 if ((z & 15) == 0)
133 printk("%04x:", z);
134 printk(" %02x", SKMCA_READB(dev->mem_start + start + z));
135 if ((z & 15) == 15)
136 printk("\n");
140 /* print exact time - ditto */
142 static void PrTime(void)
144 struct timeval tv;
146 do_gettimeofday(&tv);
147 printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);
149 #endif
151 /* deduce resources out of POS registers */
153 static void __init getaddrs(int slot, int junior, int *base, int *irq,
154 skmca_medium * medium)
156 u_char pos0, pos1, pos2;
158 if (junior) {
159 pos0 = mca_read_stored_pos(slot, 2);
160 *base = ((pos0 & 0x0e) << 13) + 0xc0000;
161 *irq = ((pos0 & 0x10) >> 4) + 10;
162 *medium = Media_Unknown;
163 } else {
164 /* reset POS 104 Bits 0+1 so the shared memory region goes to the
165 configured area between 640K and 1M. Afterwards, enable the MC2.
166 I really don't know what rode SK to do this... */
168 mca_write_pos(slot, 4,
169 mca_read_stored_pos(slot, 4) & 0xfc);
170 mca_write_pos(slot, 2,
171 mca_read_stored_pos(slot, 2) | 0x01);
173 pos1 = mca_read_stored_pos(slot, 3);
174 pos2 = mca_read_stored_pos(slot, 4);
175 *base = ((pos1 & 0x07) << 14) + 0xc0000;
176 switch (pos2 & 0x0c) {
177 case 0:
178 *irq = 3;
179 break;
180 case 4:
181 *irq = 5;
182 break;
183 case 8:
184 *irq = -10;
185 break;
186 case 12:
187 *irq = -11;
188 break;
190 *medium = (pos2 >> 6) & 3;
194 /* check for both cards:
195 When the MC2 is turned off, it was configured for more than 15MB RAM,
196 is disabled and won't get detected using the standard probe. We
197 therefore have to scan the slots manually :-( */
199 static int __init dofind(int *junior, int firstslot)
201 int slot;
202 unsigned int id;
204 for (slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++) {
205 id = mca_read_stored_pos(slot, 0)
206 + (((unsigned int) mca_read_stored_pos(slot, 1)) << 8);
208 *junior = 0;
209 if (id == SKNET_MCA_ID)
210 return slot;
211 *junior = 1;
212 if (id == SKNET_JUNIOR_MCA_ID)
213 return slot;
215 return MCA_NOTFOUND;
218 /* reset the whole board */
220 static void ResetBoard(struct SKMCA_NETDEV *dev)
222 skmca_priv *priv = (skmca_priv *) dev->priv;
224 SKMCA_WRITEB(CTRL_RESET_ON, priv->ctrladdr);
225 udelay(10);
226 SKMCA_WRITEB(CTRL_RESET_OFF, priv->ctrladdr);
229 /* wait for LANCE interface to become not busy */
231 static int WaitLANCE(struct SKMCA_NETDEV *dev)
233 skmca_priv *priv = (skmca_priv *) dev->priv;
234 int t = 0;
236 while ((SKMCA_READB(priv->ctrladdr) & STAT_IO_BUSY) ==
237 STAT_IO_BUSY) {
238 udelay(1);
239 if (++t > 1000) {
240 printk("%s: LANCE access timeout", dev->name);
241 return 0;
245 return 1;
248 /* set LANCE register - must be atomic */
250 static void SetLANCE(struct SKMCA_NETDEV *dev, u16 addr, u16 value)
252 skmca_priv *priv = (skmca_priv *) dev->priv;
253 unsigned long flags;
255 /* disable interrupts */
257 save_flags(flags);
258 cli();
260 /* wait until no transfer is pending */
262 WaitLANCE(dev);
264 /* transfer register address to RAP */
266 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
267 priv->ctrladdr);
268 SKMCA_WRITEW(addr, priv->ioregaddr);
269 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
270 udelay(1);
271 WaitLANCE(dev);
273 /* transfer data to register */
275 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA,
276 priv->ctrladdr);
277 SKMCA_WRITEW(value, priv->ioregaddr);
278 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
279 udelay(1);
280 WaitLANCE(dev);
282 /* reenable interrupts */
284 restore_flags(flags);
287 /* get LANCE register */
289 static u16 GetLANCE(struct SKMCA_NETDEV *dev, u16 addr)
291 skmca_priv *priv = (skmca_priv *) dev->priv;
292 unsigned long flags;
293 unsigned int res;
295 /* disable interrupts */
297 save_flags(flags);
298 cli();
300 /* wait until no transfer is pending */
302 WaitLANCE(dev);
304 /* transfer register address to RAP */
306 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP,
307 priv->ctrladdr);
308 SKMCA_WRITEW(addr, priv->ioregaddr);
309 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
310 udelay(1);
311 WaitLANCE(dev);
313 /* transfer data from register */
315 SKMCA_WRITEB(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA,
316 priv->ctrladdr);
317 SKMCA_WRITEB(IOCMD_GO, priv->cmdaddr);
318 udelay(1);
319 WaitLANCE(dev);
320 res = SKMCA_READW(priv->ioregaddr);
322 /* reenable interrupts */
324 restore_flags(flags);
326 return res;
329 /* build up descriptors in shared RAM */
331 static void InitDscrs(struct SKMCA_NETDEV *dev)
333 u32 bufaddr;
335 /* Set up Tx descriptors. The board has only 16K RAM so bits 16..23
336 are always 0. */
338 bufaddr = RAM_DATABASE;
340 LANCE_TxDescr descr;
341 int z;
343 for (z = 0; z < TXCOUNT; z++) {
344 descr.LowAddr = bufaddr;
345 descr.Flags = 0;
346 descr.Len = 0xf000;
347 descr.Status = 0;
348 SKMCA_TOIO(dev->mem_start + RAM_TXBASE +
349 (z * sizeof(LANCE_TxDescr)), &descr,
350 sizeof(LANCE_TxDescr));
351 SKMCA_SETIO(dev->mem_start + bufaddr, 0,
352 RAM_BUFSIZE);
353 bufaddr += RAM_BUFSIZE;
357 /* do the same for the Rx descriptors */
360 LANCE_RxDescr descr;
361 int z;
363 for (z = 0; z < RXCOUNT; z++) {
364 descr.LowAddr = bufaddr;
365 descr.Flags = RXDSCR_FLAGS_OWN;
366 descr.MaxLen = -RAM_BUFSIZE;
367 descr.Len = 0;
368 SKMCA_TOIO(dev->mem_start + RAM_RXBASE +
369 (z * sizeof(LANCE_RxDescr)), &descr,
370 sizeof(LANCE_RxDescr));
371 SKMCA_SETIO(dev->mem_start + bufaddr, 0,
372 RAM_BUFSIZE);
373 bufaddr += RAM_BUFSIZE;
378 /* calculate the hash bit position for a given multicast address
379 taken more or less directly from the AMD datasheet... */
381 static void UpdateCRC(unsigned char *CRC, int bit)
383 int j;
385 /* shift CRC one bit */
387 memmove(CRC + 1, CRC, 32 * sizeof(unsigned char));
388 CRC[0] = 0;
390 /* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */
392 if (bit ^ CRC[32])
393 for (j = 0; j < 32; j++)
394 CRC[j] ^= poly[j];
397 static unsigned int GetHash(char *address)
399 unsigned char CRC[33];
400 int i, byte, hashcode;
402 /* a multicast address has bit 0 in the first byte set */
404 if ((address[0] & 1) == 0)
405 return -1;
407 /* initialize CRC */
409 memset(CRC, 1, sizeof(CRC));
411 /* loop through address bits */
413 for (byte = 0; byte < 6; byte++)
414 for (i = 0; i < 8; i++)
415 UpdateCRC(CRC, (address[byte] >> i) & 1);
417 /* hashcode is the 6 least significant bits of the CRC */
419 hashcode = 0;
420 for (i = 0; i < 6; i++)
421 hashcode = (hashcode << 1) + CRC[i];
422 return hashcode;
425 /* feed ready-built initialization block into LANCE */
427 static void InitLANCE(struct SKMCA_NETDEV *dev)
429 skmca_priv *priv = (skmca_priv *) dev->priv;
431 /* build up descriptors. */
433 InitDscrs(dev);
435 /* next RX descriptor to be read is the first one. Since the LANCE
436 will start from the beginning after initialization, we have to
437 reset out pointers too. */
439 priv->nextrx = 0;
441 /* no TX descriptors active */
443 priv->nexttxput = priv->nexttxdone = priv->txbusy = 0;
445 /* set up the LANCE bus control register - constant for SKnet boards */
447 SetLANCE(dev, LANCE_CSR3,
448 CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD);
450 /* write address of initialization block into LANCE */
452 SetLANCE(dev, LANCE_CSR1, RAM_INITBASE & 0xffff);
453 SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >> 16) & 0xff);
455 /* we don't get ready until the LANCE has read the init block */
457 #if (LINUX_VERSION_CODE >= 0x02032a)
458 netif_stop_queue(dev);
459 #else
460 dev->tbusy = 1;
461 #endif
463 /* let LANCE read the initialization block. LANCE is ready
464 when we receive the corresponding interrupt. */
466 SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);
469 /* stop the LANCE so we can reinitialize it */
471 static void StopLANCE(struct SKMCA_NETDEV *dev)
473 /* can't take frames any more */
475 #if (LINUX_VERSION_CODE >= 0x02032a)
476 netif_stop_queue(dev);
477 #else
478 dev->tbusy = 1;
479 #endif
481 /* disable interrupts, stop it */
483 SetLANCE(dev, LANCE_CSR0, CSR0_STOP);
486 /* initialize card and LANCE for proper operation */
488 static void InitBoard(struct SKMCA_NETDEV *dev)
490 LANCE_InitBlock block;
492 /* Lay out the shared RAM - first we create the init block for the LANCE.
493 We do not overwrite it later because we need it again when we switch
494 promiscous mode on/off. */
496 block.Mode = 0;
497 if (dev->flags & IFF_PROMISC)
498 block.Mode |= LANCE_INIT_PROM;
499 memcpy(block.PAdr, dev->dev_addr, 6);
500 memset(block.LAdrF, 0, sizeof(block.LAdrF));
501 block.RdrP = (RAM_RXBASE & 0xffffff) | (LRXCOUNT << 29);
502 block.TdrP = (RAM_TXBASE & 0xffffff) | (LTXCOUNT << 29);
504 SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
506 /* initialize LANCE. Implicitly sets up other structures in RAM. */
508 InitLANCE(dev);
511 /* deinitialize card and LANCE */
513 static void DeinitBoard(struct SKMCA_NETDEV *dev)
515 /* stop LANCE */
517 StopLANCE(dev);
519 /* reset board */
521 ResetBoard(dev);
524 /* probe for device's irq */
526 static int __init ProbeIRQ(struct SKMCA_NETDEV *dev)
528 unsigned long imaskval, njiffies, irq;
529 u16 csr0val;
531 /* enable all interrupts */
533 imaskval = probe_irq_on();
535 /* initialize the board. Wait for interrupt 'Initialization done'. */
537 ResetBoard(dev);
538 InitBoard(dev);
540 njiffies = jiffies + 100;
541 do {
542 csr0val = GetLANCE(dev, LANCE_CSR0);
544 while (((csr0val & CSR0_IDON) == 0) && (jiffies != njiffies));
546 /* turn of interrupts again */
548 irq = probe_irq_off(imaskval);
550 /* if we found something, ack the interrupt */
552 if (irq)
553 SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_IDON);
555 /* back to idle state */
557 DeinitBoard(dev);
559 return irq;
562 /* ------------------------------------------------------------------------
563 * interrupt handler(s)
564 * ------------------------------------------------------------------------ */
566 /* LANCE has read initialization block -> start it */
568 static u16 irqstart_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
570 /* now we're ready to transmit */
572 #if (LINUX_VERSION_CODE >= 0x02032a)
573 netif_wake_queue(dev);
574 #else
575 dev->tbusy = 0;
576 #endif
578 /* reset IDON bit, start LANCE */
580 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT);
581 return GetLANCE(dev, LANCE_CSR0);
584 /* did we lose blocks due to a FIFO overrun ? */
586 static u16 irqmiss_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
588 skmca_priv *priv = (skmca_priv *) dev->priv;
590 /* update statistics */
592 priv->stat.rx_fifo_errors++;
594 /* reset MISS bit */
596 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_MISS);
597 return GetLANCE(dev, LANCE_CSR0);
600 /* receive interrupt */
602 static u16 irqrx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
604 skmca_priv *priv = (skmca_priv *) dev->priv;
605 LANCE_RxDescr descr;
606 unsigned int descraddr;
608 /* run through queue until we reach a descriptor we do not own */
610 descraddr = RAM_RXBASE + (priv->nextrx * sizeof(LANCE_RxDescr));
611 while (1) {
612 /* read descriptor */
613 SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
614 sizeof(LANCE_RxDescr));
616 /* if we reach a descriptor we do not own, we're done */
617 if ((descr.Flags & RXDSCR_FLAGS_OWN) != 0)
618 break;
620 #ifdef DEBUG
621 PrTime();
622 printk("Receive packet on descr %d len %d\n", priv->nextrx,
623 descr.Len);
624 #endif
626 /* erroneous packet ? */
627 if ((descr.Flags & RXDSCR_FLAGS_ERR) != 0) {
628 priv->stat.rx_errors++;
629 if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
630 priv->stat.rx_crc_errors++;
631 else if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
632 priv->stat.rx_frame_errors++;
633 else if ((descr.Flags & RXDSCR_FLAGS_OFLO) != 0)
634 priv->stat.rx_fifo_errors++;
637 /* good packet ? */
638 else {
639 struct sk_buff *skb;
641 skb = dev_alloc_skb(descr.Len + 2);
642 if (skb == NULL)
643 priv->stat.rx_dropped++;
644 else {
645 SKMCA_FROMIO(skb_put(skb, descr.Len),
646 dev->mem_start +
647 descr.LowAddr, descr.Len);
648 skb->dev = dev;
649 skb->protocol = eth_type_trans(skb, dev);
650 skb->ip_summed = CHECKSUM_NONE;
651 priv->stat.rx_packets++;
652 priv->stat.rx_bytes += descr.Len;
653 netif_rx(skb);
654 dev->last_rx = jiffies;
658 /* give descriptor back to LANCE */
659 descr.Len = 0;
660 descr.Flags |= RXDSCR_FLAGS_OWN;
662 /* update descriptor in shared RAM */
663 SKMCA_TOIO(dev->mem_start + descraddr, &descr,
664 sizeof(LANCE_RxDescr));
666 /* go to next descriptor */
667 priv->nextrx++;
668 descraddr += sizeof(LANCE_RxDescr);
669 if (priv->nextrx >= RXCOUNT) {
670 priv->nextrx = 0;
671 descraddr = RAM_RXBASE;
675 /* reset RINT bit */
677 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_RINT);
678 return GetLANCE(dev, LANCE_CSR0);
681 /* transmit interrupt */
683 static u16 irqtx_handler(struct SKMCA_NETDEV *dev, u16 oldcsr0)
685 skmca_priv *priv = (skmca_priv *) dev->priv;
686 LANCE_TxDescr descr;
687 unsigned int descraddr;
689 /* check descriptors at most until no busy one is left */
691 descraddr =
692 RAM_TXBASE + (priv->nexttxdone * sizeof(LANCE_TxDescr));
693 while (priv->txbusy > 0) {
694 /* read descriptor */
695 SKMCA_FROMIO(&descr, dev->mem_start + descraddr,
696 sizeof(LANCE_TxDescr));
698 /* if the LANCE still owns this one, we've worked out all sent packets */
699 if ((descr.Flags & TXDSCR_FLAGS_OWN) != 0)
700 break;
702 #ifdef DEBUG
703 PrTime();
704 printk("Send packet done on descr %d\n", priv->nexttxdone);
705 #endif
707 /* update statistics */
708 if ((descr.Flags & TXDSCR_FLAGS_ERR) == 0) {
709 priv->stat.tx_packets++;
710 priv->stat.tx_bytes++;
711 } else {
712 priv->stat.tx_errors++;
713 if ((descr.Status & TXDSCR_STATUS_UFLO) != 0) {
714 priv->stat.tx_fifo_errors++;
715 InitLANCE(dev);
717 else
718 if ((descr.Status & TXDSCR_STATUS_LCOL) !=
719 0) priv->stat.tx_window_errors++;
720 else if ((descr.Status & TXDSCR_STATUS_LCAR) != 0)
721 priv->stat.tx_carrier_errors++;
722 else if ((descr.Status & TXDSCR_STATUS_RTRY) != 0)
723 priv->stat.tx_aborted_errors++;
726 /* go to next descriptor */
727 priv->nexttxdone++;
728 descraddr += sizeof(LANCE_TxDescr);
729 if (priv->nexttxdone >= TXCOUNT) {
730 priv->nexttxdone = 0;
731 descraddr = RAM_TXBASE;
733 priv->txbusy--;
736 /* reset TX interrupt bit */
738 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_TINT);
739 oldcsr0 = GetLANCE(dev, LANCE_CSR0);
741 /* at least one descriptor is freed. Therefore we can accept
742 a new one */
743 /* inform upper layers we're in business again */
745 #if (LINUX_VERSION_CODE >= 0x02032a)
746 netif_wake_queue(dev);
747 #else
748 dev->tbusy = 0;
749 mark_bh(NET_BH);
750 #endif
752 return oldcsr0;
755 /* general interrupt entry */
757 static irqreturn_t irq_handler(int irq, void *device, struct pt_regs *regs)
759 struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) device;
760 u16 csr0val;
762 /* read CSR0 to get interrupt cause */
764 csr0val = GetLANCE(dev, LANCE_CSR0);
766 /* in case we're not meant... */
768 if ((csr0val & CSR0_INTR) == 0)
769 return IRQ_NONE;
771 #if (LINUX_VERSION_CODE >= 0x02032a)
772 #if 0
773 set_bit(LINK_STATE_RXSEM, &dev->state);
774 #endif
775 #else
776 dev->interrupt = 1;
777 #endif
779 /* loop through the interrupt bits until everything is clear */
781 do {
782 if ((csr0val & CSR0_IDON) != 0)
783 csr0val = irqstart_handler(dev, csr0val);
784 if ((csr0val & CSR0_RINT) != 0)
785 csr0val = irqrx_handler(dev, csr0val);
786 if ((csr0val & CSR0_MISS) != 0)
787 csr0val = irqmiss_handler(dev, csr0val);
788 if ((csr0val & CSR0_TINT) != 0)
789 csr0val = irqtx_handler(dev, csr0val);
790 if ((csr0val & CSR0_MERR) != 0) {
791 SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_MERR);
792 csr0val = GetLANCE(dev, LANCE_CSR0);
794 if ((csr0val & CSR0_BABL) != 0) {
795 SetLANCE(dev, LANCE_CSR0, csr0val | CSR0_BABL);
796 csr0val = GetLANCE(dev, LANCE_CSR0);
799 while ((csr0val & CSR0_INTR) != 0);
801 #if (LINUX_VERSION_CODE >= 0x02032a)
802 #if 0
803 clear_bit(LINK_STATE_RXSEM, &dev->state);
804 #endif
805 #else
806 dev->interrupt = 0;
807 #endif
808 return IRQ_HANDLED;
811 /* ------------------------------------------------------------------------
812 * driver methods
813 * ------------------------------------------------------------------------ */
815 /* MCA info */
817 static int skmca_getinfo(char *buf, int slot, void *d)
819 int len = 0, i;
820 struct SKMCA_NETDEV *dev = (struct SKMCA_NETDEV *) d;
821 skmca_priv *priv;
823 /* can't say anything about an uninitialized device... */
825 if (dev == NULL)
826 return len;
827 if (dev->priv == NULL)
828 return len;
829 priv = (skmca_priv *) dev->priv;
831 /* print info */
833 len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
834 len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
835 dev->mem_end - 1);
836 len +=
837 sprintf(buf + len, "Transceiver: %s\n",
838 MediaNames[priv->medium]);
839 len += sprintf(buf + len, "Device: %s\n", dev->name);
840 len += sprintf(buf + len, "MAC address:");
841 for (i = 0; i < 6; i++)
842 len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
843 buf[len++] = '\n';
844 buf[len] = 0;
846 return len;
849 /* open driver. Means also initialization and start of LANCE */
851 static int skmca_open(struct SKMCA_NETDEV *dev)
853 int result;
854 skmca_priv *priv = (skmca_priv *) dev->priv;
856 /* register resources - only necessary for IRQ */
857 result =
858 request_irq(priv->realirq, irq_handler,
859 SA_SHIRQ | SA_SAMPLE_RANDOM, "sk_mca", dev);
860 if (result != 0) {
861 printk("%s: failed to register irq %d\n", dev->name,
862 dev->irq);
863 return result;
865 dev->irq = priv->realirq;
867 /* set up the card and LANCE */
869 InitBoard(dev);
871 /* set up flags */
873 #if (LINUX_VERSION_CODE >= 0x02032a)
874 netif_start_queue(dev);
875 #else
876 dev->interrupt = 0;
877 dev->tbusy = 0;
878 dev->start = 0;
879 MOD_INC_USE_COUNT;
880 #endif
882 return 0;
885 /* close driver. Shut down board and free allocated resources */
887 static int skmca_close(struct SKMCA_NETDEV *dev)
889 /* turn off board */
890 DeinitBoard(dev);
892 /* release resources */
893 if (dev->irq != 0)
894 free_irq(dev->irq, dev);
895 dev->irq = 0;
897 #if (LINUX_VERSION_CODE < 0x02032a)
898 MOD_DEC_USE_COUNT;
899 #endif
901 return 0;
904 /* transmit a block. */
906 static int skmca_tx(struct sk_buff *skb, struct SKMCA_NETDEV *dev)
908 skmca_priv *priv = (skmca_priv *) dev->priv;
909 LANCE_TxDescr descr;
910 unsigned int address;
911 int tmplen, retval = 0;
912 unsigned long flags;
914 /* if we get called with a NULL descriptor, the Ethernet layer thinks
915 our card is stuck an we should reset it. We'll do this completely: */
917 if (skb == NULL) {
918 DeinitBoard(dev);
919 InitBoard(dev);
920 return 0; /* don't try to free the block here ;-) */
923 /* is there space in the Tx queue ? If no, the upper layer gave us a
924 packet in spite of us not being ready and is really in trouble.
925 We'll do the dropping for him: */
926 if (priv->txbusy >= TXCOUNT) {
927 priv->stat.tx_dropped++;
928 retval = -EIO;
929 goto tx_done;
932 /* get TX descriptor */
933 address = RAM_TXBASE + (priv->nexttxput * sizeof(LANCE_TxDescr));
934 SKMCA_FROMIO(&descr, dev->mem_start + address,
935 sizeof(LANCE_TxDescr));
937 /* enter packet length as 2s complement - assure minimum length */
938 tmplen = skb->len;
939 if (tmplen < 60)
940 tmplen = 60;
941 descr.Len = 65536 - tmplen;
943 /* copy filler into RAM - in case we're filling up...
944 we're filling a bit more than necessary, but that doesn't harm
945 since the buffer is far larger... */
946 if (tmplen > skb->len) {
947 char *fill = "NetBSD is a nice OS too! ";
948 unsigned int destoffs = 0, l = strlen(fill);
950 while (destoffs < tmplen) {
951 SKMCA_TOIO(dev->mem_start + descr.LowAddr +
952 destoffs, fill, l);
953 destoffs += l;
957 /* do the real data copying */
958 SKMCA_TOIO(dev->mem_start + descr.LowAddr, skb->data, skb->len);
960 /* hand descriptor over to LANCE - this is the first and last chunk */
961 descr.Flags =
962 TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
964 #ifdef DEBUG
965 PrTime();
966 printk("Send packet on descr %d len %d\n", priv->nexttxput,
967 skb->len);
968 #endif
970 /* one more descriptor busy */
971 save_flags(flags);
972 cli();
973 priv->nexttxput++;
974 if (priv->nexttxput >= TXCOUNT)
975 priv->nexttxput = 0;
976 priv->txbusy++;
978 /* are we saturated ? */
980 if (priv->txbusy >= TXCOUNT)
981 #if (LINUX_VERSION_CODE >= 0x02032a)
982 netif_stop_queue(dev);
983 #else
984 dev->tbusy = 1;
985 #endif
987 /* write descriptor back to RAM */
988 SKMCA_TOIO(dev->mem_start + address, &descr,
989 sizeof(LANCE_TxDescr));
991 /* if no descriptors were active, give the LANCE a hint to read it
992 immediately */
994 if (priv->txbusy == 0)
995 SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
997 restore_flags(flags);
999 tx_done:
1001 dev_kfree_skb(skb);
1003 return retval;
1006 /* return pointer to Ethernet statistics */
1008 static struct net_device_stats *skmca_stats(struct SKMCA_NETDEV *dev)
1010 skmca_priv *priv = (skmca_priv *) dev->priv;
1012 return &(priv->stat);
1015 /* we don't support runtime reconfiguration, since an MCA card can
1016 be unambigously identified by its POS registers. */
1018 static int skmca_config(struct SKMCA_NETDEV *dev, struct ifmap *map)
1020 return 0;
1023 /* switch receiver mode. We use the LANCE's multicast filter to prefilter
1024 multicast addresses. */
1026 static void skmca_set_multicast_list(struct SKMCA_NETDEV *dev)
1028 LANCE_InitBlock block;
1030 /* first stop the LANCE... */
1031 StopLANCE(dev);
1033 /* ...then modify the initialization block... */
1034 SKMCA_FROMIO(&block, dev->mem_start + RAM_INITBASE, sizeof(block));
1035 if (dev->flags & IFF_PROMISC)
1036 block.Mode |= LANCE_INIT_PROM;
1037 else
1038 block.Mode &= ~LANCE_INIT_PROM;
1040 if (dev->flags & IFF_ALLMULTI) { /* get all multicasts */
1041 memset(block.LAdrF, 8, 0xff);
1042 } else { /* get selected/no multicasts */
1044 struct dev_mc_list *mptr;
1045 int code;
1047 memset(block.LAdrF, 8, 0x00);
1048 for (mptr = dev->mc_list; mptr != NULL; mptr = mptr->next) {
1049 code = GetHash(mptr->dmi_addr);
1050 block.LAdrF[(code >> 3) & 7] |= 1 << (code & 7);
1054 SKMCA_TOIO(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
1056 /* ...then reinit LANCE with the correct flags */
1057 InitLANCE(dev);
1060 /* ------------------------------------------------------------------------
1061 * hardware check
1062 * ------------------------------------------------------------------------ */
1064 static int startslot; /* counts through slots when probing multiple devices */
1066 int __init skmca_probe(struct SKMCA_NETDEV *dev)
1068 int force_detect = 0;
1069 int junior, slot, i;
1070 int base = 0, irq = 0;
1071 skmca_priv *priv;
1072 skmca_medium medium;
1074 /* can't work without an MCA bus ;-) */
1076 if (MCA_bus == 0)
1077 return -ENODEV;
1079 SET_MODULE_OWNER(dev);
1081 /* start address of 1 --> forced detection */
1083 if (dev->mem_start == 1)
1084 force_detect = 1;
1086 /* search through slots */
1088 if (dev != NULL) {
1089 base = dev->mem_start;
1090 irq = dev->irq;
1092 slot = dofind(&junior, startslot);
1094 while (slot != -1) {
1095 /* deduce card addresses */
1097 getaddrs(slot, junior, &base, &irq, &medium);
1099 #if LINUX_VERSION_CODE >= 0x020300
1100 /* slot already in use ? */
1102 if (mca_is_adapter_used(slot)) {
1103 slot = dofind(&junior, slot + 1);
1104 continue;
1106 #endif
1108 /* were we looking for something different ? */
1110 if ((dev->irq != 0) || (dev->mem_start != 0)) {
1111 if ((dev->irq != 0) && (dev->irq != irq)) {
1112 slot = dofind(&junior, slot + 1);
1113 continue;
1115 if ((dev->mem_start != 0)
1116 && (dev->mem_start != base)) {
1117 slot = dofind(&junior, slot + 1);
1118 continue;
1122 /* found something that matches */
1124 break;
1127 /* nothing found ? */
1129 if (slot == -1)
1130 return ((base != 0) || (irq != 0)) ? ENXIO : ENODEV;
1132 /* make procfs entries */
1134 if (junior)
1135 mca_set_adapter_name(slot,
1136 "SKNET junior MC2 Ethernet Adapter");
1137 else
1138 mca_set_adapter_name(slot, "SKNET MC2+ Ethernet Adapter");
1139 mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
1141 mca_mark_as_used(slot);
1143 /* announce success */
1144 printk("%s: SKNet %s adapter found in slot %d\n", dev->name,
1145 junior ? "Junior MC2" : "MC2+", slot + 1);
1147 /* allocate structure */
1148 priv = dev->priv =
1149 (skmca_priv *) kmalloc(sizeof(skmca_priv), GFP_KERNEL);
1150 if (!priv)
1151 return -ENOMEM;
1152 priv->slot = slot;
1153 priv->macbase = base + 0x3fc0;
1154 priv->ioregaddr = base + 0x3ff0;
1155 priv->ctrladdr = base + 0x3ff2;
1156 priv->cmdaddr = base + 0x3ff3;
1157 priv->medium = medium;
1158 memset(&(priv->stat), 0, sizeof(struct net_device_stats));
1160 /* set base + irq for this device (irq not allocated so far) */
1161 dev->irq = 0;
1162 dev->mem_start = base;
1163 dev->mem_end = base + 0x4000;
1165 /* autoprobe ? */
1166 if (irq < 0) {
1167 int nirq;
1169 printk
1170 ("%s: ambigous POS bit combination, must probe for IRQ...\n",
1171 dev->name);
1172 nirq = ProbeIRQ(dev);
1173 if (nirq <= 0)
1174 printk("%s: IRQ probe failed, assuming IRQ %d",
1175 dev->name, priv->realirq = -irq);
1176 else
1177 priv->realirq = nirq;
1178 } else
1179 priv->realirq = irq;
1181 /* set methods */
1182 dev->open = skmca_open;
1183 dev->stop = skmca_close;
1184 dev->set_config = skmca_config;
1185 dev->hard_start_xmit = skmca_tx;
1186 dev->do_ioctl = NULL;
1187 dev->get_stats = skmca_stats;
1188 dev->set_multicast_list = skmca_set_multicast_list;
1189 dev->flags |= IFF_MULTICAST;
1191 /* generic setup */
1192 ether_setup(dev);
1194 /* copy out MAC address */
1195 for (i = 0; i < 6; i++)
1196 dev->dev_addr[i] = SKMCA_READB(priv->macbase + (i << 1));
1198 /* print config */
1199 printk("%s: IRQ %d, memory %#lx-%#lx, "
1200 "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
1201 dev->name, priv->realirq, dev->mem_start, dev->mem_end - 1,
1202 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1203 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1204 printk("%s: %s medium\n", dev->name, MediaNames[priv->medium]);
1206 /* reset board */
1208 ResetBoard(dev);
1210 startslot = slot + 1;
1212 return 0;
1215 /* ------------------------------------------------------------------------
1216 * modularization support
1217 * ------------------------------------------------------------------------ */
1219 #ifdef MODULE
1220 MODULE_LICENSE("GPL");
1222 #define DEVMAX 5
1224 #if (LINUX_VERSION_CODE >= 0x020369)
1225 static struct SKMCA_NETDEV moddevs[DEVMAX] =
1226 { {" ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1227 {" ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1228 {" ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1229 {" ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1230 {" ", 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
1232 #else
1233 static char NameSpace[8 * DEVMAX];
1234 static struct SKMCA_NETDEV moddevs[DEVMAX] =
1235 { {NameSpace + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1236 {NameSpace + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1237 {NameSpace + 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1238 {NameSpace + 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1239 {NameSpace + 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}
1241 #endif
1243 int irq;
1244 int io;
1246 int init_module(void)
1248 int z, res;
1250 startslot = 0;
1251 for (z = 0; z < DEVMAX; z++) {
1252 strcpy(moddevs[z].name, " ");
1253 res = register_netdev(moddevs + z);
1254 if (res != 0)
1255 return (z > 0) ? 0 : -EIO;
1258 return 0;
1261 void cleanup_module(void)
1263 struct SKMCA_NETDEV *dev;
1264 skmca_priv *priv;
1265 int z;
1267 for (z = 0; z < DEVMAX; z++) {
1268 dev = moddevs + z;
1269 if (dev->priv != NULL) {
1270 priv = (skmca_priv *) dev->priv;
1271 DeinitBoard(dev);
1272 if (dev->irq != 0)
1273 free_irq(dev->irq, dev);
1274 dev->irq = 0;
1275 unregister_netdev(dev);
1276 mca_mark_as_unused(priv->slot);
1277 mca_set_adapter_procfn(priv->slot, NULL, NULL);
1278 kfree(dev->priv);
1279 dev->priv = NULL;
1283 #endif /* MODULE */