* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / sk_mca.c
blobcdee2d6e68863216b17eb1c89718ce8021402887
1 /*
2 net-3-driver for the SKNET MCA-based cards
4 This is an extension to the Linux operating system, and is covered by the
5 same Gnu Public License that covers that work.
7 Copyright 1999 by Alfred Arnold (alfred@ccac.rwth-aachen.de, aarnold@elsa.de)
9 This driver is based both on the 3C523 driver and the SK_G16 driver.
11 paper sources:
12 'PC Hardware: Aufbau, Funktionsweise, Programmierung' by
13 Hans-Peter Messmer for the basic Microchannel stuff
15 'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
16 for help on Ethernet driver programming
18 'Ethernet/IEEE 802.3 Family 1992 World Network Data Book/Handbook' by AMD
19 for documentation on the AM7990 LANCE
21 'SKNET Personal Technisches Manual', Version 1.2 by Schneider&Koch
22 for documentation on the Junior board
24 'SK-NET MC2+ Technical Manual", Version 1.1 by Schneider&Koch for
25 documentation on the MC2 bord
27 A big thank you to the S&K support for providing me so quickly with
28 documentation!
30 Also see http://www.syskonnect.com/
32 Missing things:
34 -> set debug level via ioctl instead of compile-time switches
35 -> I didn't follow the development of the 2.1.x kernels, so my
36 assumptions about which things changed with which kernel version
37 are probably nonsense
39 History:
40 May 16th, 1999
41 startup
42 May 22st, 1999
43 added private structure, methods
44 begun building data structures in RAM
45 May 23nd, 1999
46 can receive frames, send frames
47 May 24th, 1999
48 modularized intialization of LANCE
49 loadable as module
50 still Tx problem :-(
51 May 26th, 1999
52 MC2 works
53 support for multiple devices
54 display media type for MC2+
55 May 28th, 1999
56 fixed problem in GetLANCE leaving interrupts turned off
57 increase TX queue to 4 packets to improve send performance
58 May 29th, 1999
59 a few corrections in statistics, caught rcvr overruns
60 reinitialization of LANCE/board in critical situations
61 MCA info implemented
62 implemented LANCE multicast filter
63 Jun 6th, 1999
64 additions for Linux 2.2
65 Aug 2nd, 1999
66 small fixes (David Weinehall)
68 *************************************************************************/
70 #include <linux/module.h>
71 #include <linux/version.h>
73 #include <linux/kernel.h>
74 #include <linux/version.h>
75 #include <linux/sched.h>
76 #include <linux/string.h>
77 #include <linux/errno.h>
78 #include <linux/ioport.h>
79 #include <linux/malloc.h>
80 #include <linux/interrupt.h>
81 #include <linux/delay.h>
82 #include <linux/time.h>
83 #include <linux/mca.h>
84 #include <asm/processor.h>
85 #include <asm/bitops.h>
86 #include <asm/io.h>
88 #include <linux/netdevice.h>
89 #include <linux/etherdevice.h>
90 #include <linux/skbuff.h>
92 #define _SK_MCA_DRIVER_
93 #include "sk_mca.h"
95 /* ------------------------------------------------------------------------
96 * global static data - not more since we can handle multiple boards and
97 * have to pack all state info into the device struct!
98 * ------------------------------------------------------------------------ */
100 static char *MediaNames[Media_Count]=
101 {"10Base2", "10BaseT", "10Base5", "Unknown"};
103 static unsigned char poly[] =
104 {1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0,
105 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0};
107 /* ------------------------------------------------------------------------
108 * private subfunctions
109 * ------------------------------------------------------------------------ */
111 /* dump parts of shared memory - only needed during debugging */
113 #ifdef DEBUG
114 static void dumpmem(struct net_device *dev, u32 start, u32 len)
116 int z;
118 for (z = 0; z < len; z++)
120 if ((z & 15) == 0)
121 printk("%04x:", z);
122 printk(" %02x", readb(dev->mem_start + start + z));
123 if ((z & 15) == 15)
124 printk("\n");
128 /* print exact time - ditto */
130 static void PrTime(void)
132 struct timeval tv;
134 do_gettimeofday(&tv);
135 printk("%9d:%06d: ", tv.tv_sec, tv.tv_usec);
137 #endif
139 /* deduce resources out of POS registers */
141 static void getaddrs(int slot, int junior, int *base, int *irq,
142 skmca_medium *medium)
144 u_char pos0, pos1, pos2;
146 if (junior)
148 pos0 = mca_read_stored_pos(slot, 2);
149 *base = ((pos0 & 0x0e) << 13) + 0xc0000;
150 *irq = ((pos0 & 0x10) >> 4) + 10;
151 *medium = Media_Unknown;
153 else
155 /* reset POS 104 Bits 0+1 so the shared memory region goes to the
156 configured area between 640K and 1M. Afterwards, enable the MC2.
157 I really don't know what rode SK to do this... */
159 mca_write_pos(slot, 4, mca_read_stored_pos(slot, 4) & 0xfc);
160 mca_write_pos(slot, 2, mca_read_stored_pos(slot, 2) | 0x01);
162 pos1 = mca_read_stored_pos(slot, 3);
163 pos2 = mca_read_stored_pos(slot, 4);
164 *base = ((pos1 & 0x07) << 14) + 0xc0000;
165 switch (pos2 & 0x0c)
167 case 0: *irq = 3; break;
168 case 4: *irq = 5; break;
169 case 8: *irq = 10; break;
170 case 12: *irq = 11; break;
172 *medium = (pos2 >> 6) & 3;
176 /* check for both cards:
177 When the MC2 is turned off, it was configured for more than 15MB RAM,
178 is disabled and won't get detected using the standard probe. We
179 therefore have to scan the slots manually :-( */
181 static int dofind(int *junior, int firstslot)
183 int slot;
184 unsigned int id;
186 for (slot = firstslot; slot < MCA_MAX_SLOT_NR; slot++)
188 id = mca_read_stored_pos(slot, 0)
189 + (((unsigned int) mca_read_stored_pos(slot, 1)) << 8);
191 *junior = 0;
192 if (id == SKNET_MCA_ID)
193 return slot;
194 *junior = 1;
195 if (id == SKNET_JUNIOR_MCA_ID)
196 return slot;
198 return MCA_NOTFOUND;
201 /* reset the whole board */
203 static void ResetBoard(struct net_device *dev)
205 skmca_priv *priv = (skmca_priv*) dev->priv;
207 writeb(CTRL_RESET_ON, priv->ctrladdr);
208 udelay(10);
209 writeb(CTRL_RESET_OFF, priv->ctrladdr);
212 /* set LANCE register - must be atomic */
214 static void SetLANCE(struct net_device *dev, u16 addr, u16 value)
216 skmca_priv *priv = (skmca_priv*) dev->priv;
217 unsigned long flags;
219 /* disable interrupts */
221 save_flags(flags);
222 cli();
224 /* wait until no transfer is pending */
226 while ((readb(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY);
228 /* transfer register address to RAP */
230 writeb(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP, priv->ctrladdr);
231 writew(addr, priv->ioregaddr);
232 writeb(IOCMD_GO, priv->cmdaddr);
233 udelay(1);
234 while ((readb(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY);
236 /* transfer data to register */
238 writeb(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_DATA, priv->ctrladdr);
239 writew(value, priv->ioregaddr);
240 writeb(IOCMD_GO, priv->cmdaddr);
241 udelay(1);
242 while ((readb(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY);
244 /* reenable interrupts */
246 restore_flags(flags);
249 /* get LANCE register */
251 static u16 GetLANCE(struct net_device *dev, u16 addr)
253 skmca_priv *priv = (skmca_priv*) dev->priv;
254 unsigned long flags;
255 unsigned int res;
257 /* disable interrupts */
259 save_flags(flags);
260 cli();
262 /* wait until no transfer is pending */
264 while ((readb(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY);
266 /* transfer register address to RAP */
268 writeb(CTRL_RESET_OFF | CTRL_RW_WRITE | CTRL_ADR_RAP, priv->ctrladdr);
269 writew(addr, priv->ioregaddr);
270 writeb(IOCMD_GO, priv->cmdaddr);
271 udelay(1);
272 while ((readb(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY);
274 /* transfer data from register */
276 writeb(CTRL_RESET_OFF | CTRL_RW_READ | CTRL_ADR_DATA, priv->ctrladdr);
277 writeb(IOCMD_GO, priv->cmdaddr);
278 udelay(1);
279 while ((readb(priv->ctrladdr) & STAT_IO_BUSY) == STAT_IO_BUSY);
280 res = readw(priv->ioregaddr);
282 /* reenable interrupts */
284 restore_flags(flags);
286 return res;
289 /* build up descriptors in shared RAM */
291 static void InitDscrs(struct net_device *dev)
293 u32 bufaddr;
295 /* Set up Tx descriptors. The board has only 16K RAM so bits 16..23
296 are always 0. */
298 bufaddr = RAM_DATABASE;
300 LANCE_TxDescr descr;
301 int z;
303 for (z = 0; z < TXCOUNT; z++)
305 descr.LowAddr = bufaddr;
306 descr.Flags = 0;
307 descr.Len = 0xf000;
308 descr.Status = 0;
309 memcpy_toio(dev->mem_start + RAM_TXBASE + (z * sizeof(LANCE_TxDescr)),
310 &descr, sizeof(LANCE_TxDescr));
311 memset_io(dev->mem_start + bufaddr, 0, RAM_BUFSIZE);
312 bufaddr += RAM_BUFSIZE;
316 /* do the same for the Rx descriptors */
319 LANCE_RxDescr descr;
320 int z;
322 for (z = 0; z < RXCOUNT; z++)
324 descr.LowAddr = bufaddr;
325 descr.Flags = RXDSCR_FLAGS_OWN;
326 descr.MaxLen = -RAM_BUFSIZE;
327 descr.Len = 0;
328 memcpy_toio(dev->mem_start + RAM_RXBASE + (z * sizeof(LANCE_RxDescr)),
329 &descr, sizeof(LANCE_RxDescr));
330 memset_io(dev->mem_start + bufaddr, 0, RAM_BUFSIZE);
331 bufaddr += RAM_BUFSIZE;
336 /* calculate the hash bit position for a given multicast address
337 taken more or less directly from the AMD datasheet... */
339 static void UpdateCRC(unsigned char *CRC, int bit)
341 int j;
343 /* shift CRC one bit */
345 memmove(CRC + 1, CRC, 32 * sizeof(unsigned char));
346 CRC[0] = 0;
348 /* if bit XOR controlbit = 1, set CRC = CRC XOR polynomial */
350 if (bit ^ CRC[32])
351 for (j = 0; j < 32; j++)
352 CRC[j] ^= poly[j];
355 static unsigned int GetHash(char *address)
357 unsigned char CRC[33];
358 int i, byte, hashcode;
360 /* a multicast address has bit 0 in the first byte set */
362 if ((address[0] & 1) == 0)
363 return -1;
365 /* initialize CRC */
367 memset(CRC, 1, sizeof(CRC));
369 /* loop through address bits */
371 for (byte = 0; byte < 6; byte++)
372 for (i = 0; i < 8; i++)
373 UpdateCRC(CRC, (address[byte] >> i) & 1);
375 /* hashcode is the 6 least significant bits of the CRC */
377 hashcode = 0;
378 for (i = 0; i < 6; i++)
379 hashcode = (hashcode << 1) + CRC[i];
380 return hashcode;
383 /* feed ready-built initialization block into LANCE */
385 static void InitLANCE(struct net_device *dev)
387 skmca_priv *priv = (skmca_priv*) dev->priv;
389 /* build up descriptors. */
391 InitDscrs(dev);
393 /* next RX descriptor to be read is the first one. Since the LANCE
394 will start from the beginning after initialization, we have to
395 reset out pointers too. */
397 priv->nextrx = 0;
399 /* no TX descriptors active */
401 priv->nexttxput = priv->nexttxdone = priv->txbusy = 0;
403 /* set up the LANCE bus control register - constant for SKnet boards */
405 SetLANCE(dev, LANCE_CSR3, CSR3_BSWAP_OFF | CSR3_ALE_LOW | CSR3_BCON_HOLD);
407 /* write address of initialization block into LANCE */
409 SetLANCE(dev, LANCE_CSR1, RAM_INITBASE & 0xffff);
410 SetLANCE(dev, LANCE_CSR2, (RAM_INITBASE >> 16) & 0xff);
412 /* we don't get ready until the LANCE has read the init block */
414 dev->tbusy = 1;
416 /* let LANCE read the initialization block. LANCE is ready
417 when we receive the corresponding interrupt. */
419 SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_INIT);
422 /* stop the LANCE so we can reinitialize it */
424 static void StopLANCE(struct net_device *dev)
426 /* can't take frames any more */
428 dev->tbusy = 1;
430 /* disable interrupts, stop it */
432 SetLANCE(dev, LANCE_CSR0, CSR0_STOP);
435 /* initialize card and LANCE for proper operation */
437 static void InitBoard(struct net_device *dev)
439 LANCE_InitBlock block;
441 /* Lay out the shared RAM - first we create the init block for the LANCE.
442 We do not overwrite it later because we need it again when we switch
443 promiscous mode on/off. */
445 block.Mode = 0;
446 if (dev->flags & IFF_PROMISC)
447 block.Mode |= LANCE_INIT_PROM;
448 memcpy(block.PAdr, dev->dev_addr, 6);
449 memset(block.LAdrF, 0, sizeof(block.LAdrF));
450 block.RdrP = (RAM_RXBASE & 0xffffff) | (LRXCOUNT << 29);
451 block.TdrP = (RAM_TXBASE & 0xffffff) | (LTXCOUNT << 29);
453 memcpy_toio(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
455 /* initialize LANCE. Implicitly sets up other structures in RAM. */
457 InitLANCE(dev);
460 /* deinitialize card and LANCE */
462 static void DeinitBoard(struct net_device *dev)
464 /* stop LANCE */
466 StopLANCE(dev);
468 /* reset board */
470 ResetBoard(dev);
473 /* ------------------------------------------------------------------------
474 * interrupt handler(s)
475 * ------------------------------------------------------------------------ */
477 /* LANCE has read initializazion block -> start it */
479 static u16 irqstart_handler(struct net_device *dev, u16 oldcsr0)
481 /* now we're ready to transmit */
483 dev->tbusy = 0;
485 /* reset IDON bit, start LANCE */
487 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_IDON | CSR0_STRT);
488 return GetLANCE(dev, LANCE_CSR0);
491 /* receive interrupt */
493 static u16 irqrx_handler(struct net_device *dev, u16 oldcsr0)
495 skmca_priv *priv = (skmca_priv*) dev->priv;
496 LANCE_RxDescr descr;
497 unsigned int descraddr;
499 /* did we loose blocks due to a FIFO overrun ? */
501 if (oldcsr0 & CSR0_MISS)
502 priv->stat.rx_fifo_errors++;
504 /* run through queue until we reach a descriptor we do not own */
506 descraddr = RAM_RXBASE + (priv->nextrx * sizeof(LANCE_RxDescr));
507 while (1)
509 /* read descriptor */
510 memcpy_fromio(&descr, dev->mem_start + descraddr, sizeof(LANCE_RxDescr));
512 /* if we reach a descriptor we do not own, we're done */
513 if ((descr.Flags & RXDSCR_FLAGS_OWN) != 0)
514 break;
516 #ifdef DEBUG
517 PrTime(); printk("Receive packet on descr %d len %d\n", priv->nextrx, descr.Len);
518 #endif
520 /* erroneous packet ? */
521 if ((descr.Flags & RXDSCR_FLAGS_ERR) != 0)
523 priv->stat.rx_errors++;
524 if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
525 priv->stat.rx_crc_errors++;
526 else if ((descr.Flags & RXDSCR_FLAGS_CRC) != 0)
527 priv->stat.rx_frame_errors++;
528 else if ((descr.Flags & RXDSCR_FLAGS_OFLO) != 0)
529 priv->stat.rx_fifo_errors++;
532 /* good packet ? */
533 else
535 struct sk_buff *skb;
537 skb = dev_alloc_skb(descr.Len + 2);
538 if (skb == NULL)
539 priv->stat.rx_dropped++;
540 else
542 memcpy_fromio(skb_put(skb, descr.Len),
543 dev->mem_start + descr.LowAddr, descr.Len);
544 skb->dev = dev;
545 skb->protocol = eth_type_trans(skb, dev);
546 skb->ip_summed = CHECKSUM_NONE;
547 priv->stat.rx_packets++;
548 #if LINUX_VERSION_CODE >= 0x020119 /* byte counters for >= 2.1.25 */
549 priv->stat.rx_bytes += descr.Len;
550 #endif
551 netif_rx(skb);
555 /* give descriptor back to LANCE */
556 descr.Len = 0;
557 descr.Flags |= RXDSCR_FLAGS_OWN;
559 /* update descriptor in shared RAM */
560 memcpy_toio(dev->mem_start + descraddr, &descr, sizeof(LANCE_RxDescr));
562 /* go to next descriptor */
563 priv->nextrx++; descraddr += sizeof(LANCE_RxDescr);
564 if (priv->nextrx >= RXCOUNT)
566 priv->nextrx = 0;
567 descraddr = RAM_RXBASE;
571 /* reset RINT bit */
573 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_RINT);
574 return GetLANCE(dev, LANCE_CSR0);
577 /* transmit interrupt */
579 static u16 irqtx_handler(struct net_device *dev, u16 oldcsr0)
581 skmca_priv *priv = (skmca_priv*) dev->priv;
582 LANCE_TxDescr descr;
583 unsigned int descraddr;
585 /* check descriptors at most until no busy one is left */
587 descraddr = RAM_TXBASE + (priv->nexttxdone * sizeof(LANCE_TxDescr));
588 while (priv->txbusy > 0)
590 /* read descriptor */
591 memcpy_fromio(&descr, dev->mem_start + descraddr, sizeof(LANCE_TxDescr));
593 /* if the LANCE still owns this one, we've worked out all sent packets */
594 if ((descr.Flags & TXDSCR_FLAGS_OWN) != 0)
595 break;
597 #ifdef DEBUG
598 PrTime(); printk("Send packet done on descr %d\n", priv->nexttxdone);
599 #endif
601 /* update statistics */
602 if ((descr.Flags & TXDSCR_FLAGS_ERR) == 0)
604 priv->stat.tx_packets++;
605 #if LINUX_VERSION_CODE >= 0x020119 /* byte counters for >= 2.1.25 */
606 priv->stat.tx_bytes++;
607 #endif
609 else
611 priv->stat.tx_errors++;
612 if ((descr.Status & TXDSCR_STATUS_UFLO) != 0)
614 priv->stat.tx_fifo_errors++;
615 InitLANCE(dev);
617 else if ((descr.Status & TXDSCR_STATUS_LCOL) != 0)
618 priv->stat.tx_window_errors++;
619 else if ((descr.Status & TXDSCR_STATUS_LCAR) != 0)
620 priv->stat.tx_carrier_errors++;
621 else if ((descr.Status & TXDSCR_STATUS_RTRY) != 0)
622 priv->stat.tx_aborted_errors++;
625 /* go to next descriptor */
626 priv->nexttxdone++;
627 descraddr += sizeof(LANCE_TxDescr);
628 if (priv->nexttxdone >= TXCOUNT)
630 priv->nexttxdone = 0;
631 descraddr = RAM_TXBASE;
633 priv->txbusy--;
636 /* reset TX interrupt bit */
638 SetLANCE(dev, LANCE_CSR0, oldcsr0 | CSR0_TINT);
639 oldcsr0 = GetLANCE(dev, LANCE_CSR0);
641 /* at least one descriptor is freed. Therefore we can accept
642 a new one */
644 dev->tbusy = 0;
646 /* inform upper layers we're in business again */
648 mark_bh(NET_BH);
650 return oldcsr0;
653 /* general interrupt entry */
655 static void irq_handler(int irq, void *device, struct pt_regs *regs)
657 struct net_device *dev = (struct net_device*) device;
658 u16 csr0val;
660 /* read CSR0 to get interrupt cause */
662 csr0val = GetLANCE(dev, LANCE_CSR0);
664 /* in case we're not meant... */
666 if ((csr0val & CSR0_INTR) == 0)
667 return;
669 dev->interrupt = 1;
671 /* loop through the interrupt bits until everything is clear */
675 if ((csr0val & CSR0_IDON) != 0)
676 csr0val = irqstart_handler(dev, csr0val);
677 if ((csr0val & CSR0_RINT) != 0)
678 csr0val = irqrx_handler(dev, csr0val);
679 if ((csr0val & CSR0_TINT) != 0)
680 csr0val = irqtx_handler(dev, csr0val);
682 while ((csr0val & CSR0_INTR) != 0);
684 dev->interrupt = 0;
687 /* ------------------------------------------------------------------------
688 * driver methods
689 * ------------------------------------------------------------------------ */
691 /* MCA info */
693 static int skmca_getinfo(char *buf, int slot, void *d)
695 int len = 0, i;
696 struct net_device *dev = (struct net_device*) d;
697 skmca_priv *priv;
699 /* can't say anything about an uninitialized device... */
701 if (dev == NULL)
702 return len;
703 if (dev->priv == NULL)
704 return len;
705 priv = (skmca_priv*) dev->priv;
707 /* print info */
709 len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
710 len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start,
711 dev->mem_end - 1);
712 len += sprintf(buf + len, "Transceiver: %s\n", MediaNames[priv->medium]);
713 len += sprintf(buf + len, "Device: %s\n", dev->name);
714 len += sprintf(buf + len, "MAC address:");
715 for (i = 0; i < 6; i ++ )
716 len += sprintf( buf+len, " %02x", dev->dev_addr[i] );
717 buf[len++] = '\n';
718 buf[len] = 0;
720 return len;
723 /* open driver. Means also initialization and start of LANCE */
725 static int skmca_open(struct net_device *dev)
727 int result;
728 skmca_priv *priv = (skmca_priv*) dev->priv;
730 /* register resources - only necessary for IRQ */
731 result = request_irq(priv->realirq, irq_handler, SA_SHIRQ | SA_SAMPLE_RANDOM,
732 "sk_mca", dev);
733 if (result != 0)
735 printk("%s: failed to register irq %d\n", dev->name, dev->irq);
736 return result;
738 dev->irq = priv->realirq;
740 /* set up the card and LANCE */
741 InitBoard(dev);
743 #ifdef MODULE
744 MOD_INC_USE_COUNT;
745 #endif
747 return 0;
750 /* close driver. Shut down board and free allocated resources */
752 static int skmca_close(struct net_device *dev)
754 /* turn off board */
755 DeinitBoard(dev);
757 /* release resources */
758 if (dev->irq != 0)
759 free_irq(dev->irq, dev);
760 dev->irq = 0;
762 #ifdef MODULE
763 MOD_DEC_USE_COUNT;
764 #endif
766 return 0;
769 /* transmit a block. */
771 static int skmca_tx(struct sk_buff *skb, struct net_device *dev)
773 skmca_priv *priv = (skmca_priv*) dev->priv;
774 LANCE_TxDescr descr;
775 unsigned int address;
776 int tmplen, retval = 0;
777 unsigned long flags;
779 /* if we get called with a NULL descriptor, the Ethernet layer thinks
780 our card is stuck an we should reset it. We'll do this completely: */
782 if (skb == NULL)
784 DeinitBoard(dev);
785 InitBoard(dev);
786 return 0; /* don't try to free the block here ;-) */
789 /* is there space in the Tx queue ? If no, the upper layer gave us a
790 packet in spite of us not being ready and is really in trouble.
791 We'll do the dropping for him: */
792 if (priv->txbusy >= TXCOUNT)
794 priv->stat.tx_dropped++;
795 retval = -EIO;
796 goto tx_done;
799 /* get TX descriptor */
800 address = RAM_TXBASE + (priv->nexttxput * sizeof(LANCE_TxDescr));
801 memcpy_fromio(&descr, dev->mem_start + address, sizeof(LANCE_TxDescr));
803 /* enter packet length as 2s complement - assure minimum length */
804 tmplen = skb->len;
805 if (tmplen < 60)
806 tmplen = 60;
807 descr.Len = 65536 - tmplen;
809 /* copy filler into RAM - in case we're filling up...
810 we're filling a bit more than necessary, but that doesn't harm
811 since the buffer is far larger... */
812 if (tmplen > skb->len)
814 char *fill = "NetBSD is a nice OS too! ";
815 unsigned int destoffs = 0, l = strlen(fill);
817 while (destoffs < tmplen)
819 memcpy_toio(dev->mem_start + descr.LowAddr + destoffs, fill, l);
820 destoffs += l;
824 /* do the real data copying */
825 memcpy_toio(dev->mem_start + descr.LowAddr, skb->data, skb->len);
827 /* hand descriptor over to LANCE - this is the first and last chunk */
828 descr.Flags = TXDSCR_FLAGS_OWN | TXDSCR_FLAGS_STP | TXDSCR_FLAGS_ENP;
830 #ifdef DEBUG
831 PrTime(); printk("Send packet on descr %d len %d\n", priv->nexttxput, skb->len);
832 #endif
834 /* one more descriptor busy */
835 save_flags(flags);
836 cli();
837 priv->nexttxput++;
838 if (priv->nexttxput >= TXCOUNT)
839 priv->nexttxput = 0;
840 priv->txbusy++;
841 dev->tbusy = (priv->txbusy >= TXCOUNT);
843 /* write descriptor back to RAM */
844 memcpy_toio(dev->mem_start + address, &descr, sizeof(LANCE_TxDescr));
846 /* if no descriptors were active, give the LANCE a hint to read it
847 immediately */
849 if (priv->txbusy == 0)
850 SetLANCE(dev, LANCE_CSR0, CSR0_INEA | CSR0_TDMD);
852 restore_flags(flags);
854 tx_done:
856 /* When did that change exactly ? */
858 #if LINUX_VERSION_CODE >= 0x020200
859 dev_kfree_skb(skb);
860 #else
861 dev_kfree_skb(skb, FREE_WRITE);
862 #endif
863 return retval;
866 /* return pointer to Ethernet statistics */
868 static struct enet_statistics *skmca_stats(struct net_device *dev)
870 skmca_priv *priv = (skmca_priv*) dev->priv;
872 return &(priv->stat);
875 /* we don't support runtime reconfiguration, since an MCA card can
876 be unambigously identified by its POS registers. */
878 static int skmca_config(struct net_device *dev, struct ifmap *map)
880 return 0;
883 /* switch receiver mode. We use the LANCE's multicast filter to prefilter
884 multicast addresses. */
886 static void skmca_set_multicast_list(struct net_device *dev)
888 LANCE_InitBlock block;
890 /* first stop the LANCE... */
891 StopLANCE(dev);
893 /* ...then modify the initialization block... */
894 memcpy_fromio(&block, dev->mem_start + RAM_INITBASE, sizeof(block));
895 if (dev->flags & IFF_PROMISC)
896 block.Mode |= LANCE_INIT_PROM;
897 else
898 block.Mode &= ~LANCE_INIT_PROM;
900 if (dev->flags & IFF_ALLMULTI) /* get all multicasts */
902 memset(block.LAdrF, 8, 0xff);
904 else /* get selected/no multicasts */
906 struct dev_mc_list *mptr;
907 int code;
909 memset(block.LAdrF, 8, 0x00);
910 for (mptr = dev->mc_list; mptr != NULL; mptr = mptr->next)
912 code = GetHash(mptr->dmi_addr);
913 block.LAdrF[(code >> 3) & 7] |= 1 << (code & 7);
917 memcpy_toio(dev->mem_start + RAM_INITBASE, &block, sizeof(block));
919 /* ...then reinit LANCE with the correct flags */
920 InitLANCE(dev);
923 /* ------------------------------------------------------------------------
924 * hardware check
925 * ------------------------------------------------------------------------ */
927 #ifdef MODULE
928 static int startslot; /* counts through slots when probing multiple devices */
929 #else
930 #define startslot 0 /* otherwise a dummy, since there is only eth0 in-kern*/
931 #endif
933 int skmca_probe(struct net_device *dev)
935 int force_detect = 0;
936 int junior, slot, i;
937 int base = 0, irq = 0;
938 skmca_priv *priv;
939 skmca_medium medium;
941 /* can't work without an MCA bus ;-) */
943 if (MCA_bus == 0)
944 return ENODEV;
946 /* start address of 1 --> forced detection */
948 if (dev->mem_start == 1)
949 force_detect = 1;
951 /* search through slots */
953 if (dev != NULL)
955 base = dev->mem_start;
956 irq = dev->irq;
958 slot = dofind(&junior, startslot);
960 while (slot != -1)
962 /* deduce card addresses */
964 getaddrs(slot, junior, &base, &irq, &medium);
966 #if LINUX_VERSION_CODE >= 0x020200
967 /* slot already in use ? */
969 if (mca_is_adapter_used(slot))
971 slot = dofind(&junior, slot + 1);
972 continue;
974 #endif
976 /* were we looking for something different ? */
978 if ((dev->irq != 0) || (dev->mem_start != 0))
980 if ((dev->irq != 0) && (dev->irq != irq))
982 slot = dofind(&junior, slot + 1);
983 continue;
985 if ((dev->mem_start != 0) && (dev->mem_start != base))
987 slot = dofind(&junior, slot + 1);
988 continue;
992 /* found something that matches */
994 break;
997 /* nothing found ? */
999 if (slot == -1)
1000 return ((base != 0) || (irq != 0)) ? ENXIO : ENODEV;
1002 /* make procfs entries */
1004 if (junior)
1005 mca_set_adapter_name(slot, "SKNET junior MC2 Ethernet Adapter");
1006 else
1007 mca_set_adapter_name(slot, "SKNET MC2+ Ethernet Adapter");
1008 mca_set_adapter_procfn(slot, (MCA_ProcFn) skmca_getinfo, dev);
1010 #if LINUX_VERSION_CODE >= 0x020200
1011 mca_mark_as_used(slot);
1012 #endif
1014 /* announce success */
1015 printk("%s: SKNet %s adapter found in slot %d\n", dev->name,
1016 junior ? "Junior MC2" : "MC2+", slot + 1);
1018 /* allocate structure */
1019 priv = dev->priv = (skmca_priv*) kmalloc(sizeof(skmca_priv), GFP_KERNEL);
1020 priv->slot = slot;
1021 priv->macbase = base + 0x3fc0;
1022 priv->ioregaddr = base + 0x3ff0;
1023 priv->ctrladdr = base + 0x3ff2;
1024 priv->cmdaddr = base + 0x3ff3;
1025 priv->realirq = irq;
1026 priv->medium = medium;
1027 memset(&(priv->stat), 0, sizeof(struct enet_statistics));
1029 /* set base + irq for this device (irq not allocated so far) */
1030 dev->irq = 0;
1031 dev->mem_start = base;
1032 dev->mem_end = base + 0x4000;
1034 /* set methods */
1035 dev->open = skmca_open;
1036 dev->stop = skmca_close;
1037 dev->set_config = skmca_config;
1038 dev->hard_start_xmit = skmca_tx;
1039 dev->do_ioctl = NULL;
1040 dev->get_stats = skmca_stats;
1041 dev->set_multicast_list = skmca_set_multicast_list;
1042 dev->flags |= IFF_MULTICAST;
1044 /* generic setup */
1045 ether_setup(dev);
1046 dev->interrupt = 0;
1047 dev->tbusy = 0;
1048 dev->start = 0;
1050 /* copy out MAC address */
1051 for (i = 0; i < 6; i++)
1052 dev->dev_addr[i] = readb(priv->macbase + (i << 1));
1054 /* print config */
1055 printk("%s: IRQ %d, memory %#lx-%#lx, "
1056 "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
1057 dev->name, priv->realirq, dev->mem_start, dev->mem_end - 1,
1058 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1059 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1060 printk("%s: %s medium\n", dev->name, MediaNames[priv->medium]);
1062 /* reset board */
1064 ResetBoard(dev);
1066 #ifdef MODULE
1067 startslot = slot + 1;
1068 #endif
1070 return 0;
1073 /* ------------------------------------------------------------------------
1074 * modularization support
1075 * ------------------------------------------------------------------------ */
1077 #ifdef MODULE
1079 #define DEVMAX 5
1081 static char NameSpace[8 * DEVMAX];
1082 static struct net_device moddevs[DEVMAX] =
1083 {{NameSpace + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1084 {NameSpace + 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1085 {NameSpace + 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1086 {NameSpace + 24, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe},
1087 {NameSpace + 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, skmca_probe}};
1089 int irq=0;
1090 int io=0;
1092 int init_module(void)
1094 int z, res;
1096 startslot = 0;
1097 for (z = 0; z < DEVMAX; z++)
1099 strcpy(moddevs[z].name, " ");
1100 res = register_netdev(moddevs + z);
1101 if (res != 0)
1102 return (z > 0) ? 0 : -EIO;
1105 return 0;
1108 void cleanup_module(void)
1110 struct net_device *dev;
1111 skmca_priv *priv;
1112 int z;
1114 if (MOD_IN_USE)
1116 printk("cannot unload, module in use\n");
1117 return;
1120 for (z = 0; z < DEVMAX; z++)
1122 dev = moddevs + z;
1123 if (dev->priv != NULL)
1125 priv = (skmca_priv*) dev->priv;
1126 DeinitBoard(dev);
1127 if (dev->irq != 0)
1128 free_irq(dev->irq, dev);
1129 dev->irq = 0;
1130 unregister_netdev(dev);
1131 #if LINUX_VERSION_CODE >= 0x020200
1132 mca_mark_as_unused(priv->slot);
1133 #endif
1134 mca_set_adapter_procfn(priv->slot, NULL, NULL);
1135 kfree_s(dev->priv, sizeof(skmca_priv));
1136 dev->priv = NULL;
1140 #endif /* MODULE */