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