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