patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / net / ibmlana.c
1 /* 
2 net-3-driver for the IBM LAN Adapter/A
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 SK_MCA driver, which is itself based on the
11 SK_G16 and 3C523 driver.
12
13 paper sources:
14   'PC Hardware: Aufbau, Funktionsweise, Programmierung' by 
15   Hans-Peter Messmer for the basic Microchannel stuff
16   
17   'Linux Geraetetreiber' by Allesandro Rubini, Kalle Dalheimer
18   for help on Ethernet driver programming
19
20   'DP83934CVUL-20/25 MHz SONIC-T Ethernet Controller Datasheet' by National
21   Semiconductor for info on the MAC chip
22
23   'LAN Technical Reference Ethernet Adapter Interface Version 1 Release 1.0
24    Document Number SC30-3661-00' by IBM for info on the adapter itself
25
26   Also see http://www.natsemi.com/
27
28 special acknowledgements to:
29   - Bob Eager for helping me out with documentation from IBM
30   - Jim Shorney for his endless patience with me while I was using 
31     him as a beta tester to trace down the address filter bug ;-)
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   Nov 6th, 1999
42         startup from SK_MCA driver
43   Dec 6th, 1999
44         finally got docs about the card.  A big thank you to Bob Eager!
45   Dec 12th, 1999
46         first packet received
47   Dec 13th, 1999
48         recv queue done, tcpdump works
49   Dec 15th, 1999
50         transmission part works
51   Dec 28th, 1999
52         added usage of the isa_functions for Linux 2.3 .  Things should
53         still work with 2.0.x....
54   Jan 28th, 2000
55         in Linux 2.2.13, the version.h file mysteriously didn't get
56         included.  Added a workaround for this.  Futhermore, it now
57         not only compiles as a modules ;-)
58   Jan 30th, 2000
59         newer kernels automatically probe more than one board, so the
60         'startslot' as a variable is also needed here
61   Apr 12th, 2000
62         the interrupt mask register is not set 'hard' instead of individually
63         setting registers, since this seems to set bits that shouldn't be
64         set
65   May 21st, 2000
66         reset interrupt status immediately after CAM load
67         add a recovery delay after releasing the chip's reset line
68   May 24th, 2000
69         finally found the bug in the address filter setup - damned signed
70         chars!
71   June 1st, 2000
72         corrected version codes, added support for the latest 2.3 changes
73   Oct 28th, 2002
74         cleaned up for the 2.5 tree <alan@redhat.com>
75
76  *************************************************************************/
77
78 #include <linux/kernel.h>
79 #include <linux/string.h>
80 #include <linux/errno.h>
81 #include <linux/ioport.h>
82 #include <linux/slab.h>
83 #include <linux/interrupt.h>
84 #include <linux/delay.h>
85 #include <linux/time.h>
86 #include <linux/mca-legacy.h>
87 #include <linux/module.h>
88 #include <linux/netdevice.h>
89 #include <linux/etherdevice.h>
90 #include <linux/skbuff.h>
91
92 #include <asm/processor.h>
93 #include <asm/bitops.h>
94 #include <asm/io.h>
95
96 #define _IBM_LANA_DRIVER_
97 #include "ibmlana.h"
98
99 #undef DEBUG
100
101 /* ------------------------------------------------------------------------
102  * global static data - not more since we can handle multiple boards and
103  * have to pack all state info into the device struct!
104  * ------------------------------------------------------------------------ */
105
106 static char *MediaNames[Media_Count] = {
107         "10BaseT", "10Base5", "Unknown", "10Base2"
108 };
109
110 /* ------------------------------------------------------------------------
111  * private subfunctions
112  * ------------------------------------------------------------------------ */
113
114 #ifdef DEBUG
115   /* dump all registers */
116
117 static void dumpregs(struct net_device *dev)
118 {
119         int z;
120
121         for (z = 0; z < 160; z += 2) {
122                 if (!(z & 15))
123                         printk("REGS: %04x:", z);
124                 printk(" %04x", inw(dev->base_addr + z));
125                 if ((z & 15) == 14)
126                         printk("\n");
127         }
128 }
129
130 /* dump parts of shared memory - only needed during debugging */
131
132 static void dumpmem(struct net_device *dev, u32 start, u32 len)
133 {
134         int z;
135
136         printk("Address %04x:\n", start);
137         for (z = 0; z < len; z++) {
138                 if ((z & 15) == 0)
139                         printk("%04x:", z);
140                 printk(" %02x", isa_readb(dev->mem_start + start + z));
141                 if ((z & 15) == 15)
142                         printk("\n");
143         }
144         if ((z & 15) != 0)
145                 printk("\n");
146 }
147
148 /* print exact time - ditto */
149
150 static void PrTime(void)
151 {
152         struct timeval tv;
153
154         do_gettimeofday(&tv);
155         printk("%9d:%06d: ", (int) tv.tv_sec, (int) tv.tv_usec);
156 }
157 #endif                          /* DEBUG */
158
159 /* deduce resources out of POS registers */
160
161 static void getaddrs(int slot, int *base, int *memlen, int *iobase,
162                      int *irq, ibmlana_medium * medium)
163 {
164         u_char pos0, pos1;
165
166         pos0 = mca_read_stored_pos(slot, 2);
167         pos1 = mca_read_stored_pos(slot, 3);
168
169         *base = 0xc0000 + ((pos1 & 0xf0) << 9);
170         *memlen = (pos1 & 0x01) ? 0x8000 : 0x4000;
171         *iobase = (pos0 & 0xe0) << 7;
172         switch (pos0 & 0x06) {
173         case 0:
174                 *irq = 5;
175                 break;
176         case 2:
177                 *irq = 15;
178                 break;
179         case 4:
180                 *irq = 10;
181                 break;
182         case 6:
183                 *irq = 11;
184                 break;
185         }
186         *medium = (pos0 & 0x18) >> 3;
187 }
188
189 /* wait on register value with mask and timeout */
190
191 static int wait_timeout(struct net_device *dev, int regoffs, u16 mask,
192                         u16 value, int timeout)
193 {
194         unsigned long fin = jiffies + timeout;
195
196         while (time_before(jiffies,fin))
197                 if ((inw(dev->base_addr + regoffs) & mask) == value)
198                         return 1;
199
200         return 0;
201 }
202
203
204 /* reset the whole board */
205
206 static void ResetBoard(struct net_device *dev)
207 {
208         unsigned char bcmval;
209
210         /* read original board control value */
211
212         bcmval = inb(dev->base_addr + BCMREG);
213
214         /* set reset bit for a while */
215
216         bcmval |= BCMREG_RESET;
217         outb(bcmval, dev->base_addr + BCMREG);
218         udelay(10);
219         bcmval &= ~BCMREG_RESET;
220         outb(bcmval, dev->base_addr + BCMREG);
221
222         /* switch over to RAM again */
223
224         bcmval |= BCMREG_RAMEN | BCMREG_RAMWIN;
225         outb(bcmval, dev->base_addr + BCMREG);
226 }
227
228 /* calculate RAM layout & set up descriptors in RAM */
229
230 static void InitDscrs(struct net_device *dev)
231 {
232         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
233         u32 addr, baddr, raddr;
234         int z;
235         tda_t tda;
236         rda_t rda;
237         rra_t rra;
238
239         /* initialize RAM */
240
241         isa_memset_io(dev->mem_start, 0xaa,
242                       dev->mem_start - dev->mem_start);
243
244         /* setup n TX descriptors - independent of RAM size */
245
246         priv->tdastart = addr = 0;
247         priv->txbufstart = baddr = sizeof(tda_t) * TXBUFCNT;
248         for (z = 0; z < TXBUFCNT; z++) {
249                 tda.status = 0;
250                 tda.config = 0;
251                 tda.length = 0;
252                 tda.fragcount = 1;
253                 tda.startlo = baddr;
254                 tda.starthi = 0;
255                 tda.fraglength = 0;
256                 if (z == TXBUFCNT - 1)
257                         tda.link = priv->tdastart;
258                 else
259                         tda.link = addr + sizeof(tda_t);
260                 tda.link |= 1;
261                 isa_memcpy_toio(dev->mem_start + addr, &tda, sizeof(tda_t));
262                 addr += sizeof(tda_t);
263                 baddr += PKTSIZE;
264         }
265
266         /* calculate how many receive buffers fit into remaining memory */
267
268         priv->rxbufcnt = (dev->mem_end - dev->mem_start - baddr) / (sizeof(rra_t) + sizeof(rda_t) + PKTSIZE);
269
270         /* calculate receive addresses */
271
272         priv->rrastart = raddr = priv->txbufstart + (TXBUFCNT * PKTSIZE);
273         priv->rdastart = addr = priv->rrastart + (priv->rxbufcnt * sizeof(rra_t));
274         priv->rxbufstart = baddr = priv->rdastart + (priv->rxbufcnt * sizeof(rda_t));
275         
276         for (z = 0; z < priv->rxbufcnt; z++) {
277                 rra.startlo = baddr;
278                 rra.starthi = 0;
279                 rra.cntlo = PKTSIZE >> 1;
280                 rra.cnthi = 0;
281                 isa_memcpy_toio(dev->mem_start + raddr, &rra, sizeof(rra_t));
282
283                 rda.status = 0;
284                 rda.length = 0;
285                 rda.startlo = 0;
286                 rda.starthi = 0;
287                 rda.seqno = 0;
288                 if (z < priv->rxbufcnt - 1)
289                         rda.link = addr + sizeof(rda_t);
290                 else
291                         rda.link = 1;
292                 rda.inuse = 1;
293                 isa_memcpy_toio(dev->mem_start + addr, &rda, sizeof(rda_t));
294
295                 baddr += PKTSIZE;
296                 raddr += sizeof(rra_t);
297                 addr += sizeof(rda_t);
298         }
299
300         /* initialize current pointers */
301
302         priv->nextrxdescr = 0;
303         priv->lastrxdescr = priv->rxbufcnt - 1;
304         priv->nexttxdescr = 0;
305         priv->currtxdescr = 0;
306         priv->txusedcnt = 0;
307         memset(priv->txused, 0, sizeof(priv->txused));
308 }
309
310 /* set up Rx + Tx descriptors in SONIC */
311
312 static int InitSONIC(struct net_device *dev)
313 {
314         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
315
316         /* set up start & end of resource area */
317
318         outw(0, SONIC_URRA);
319         outw(priv->rrastart, dev->base_addr + SONIC_RSA);
320         outw(priv->rrastart + (priv->rxbufcnt * sizeof(rra_t)), dev->base_addr + SONIC_REA);
321         outw(priv->rrastart, dev->base_addr + SONIC_RRP);
322         outw(priv->rrastart, dev->base_addr + SONIC_RWP);
323
324         /* set EOBC so that only one packet goes into one buffer */
325
326         outw((PKTSIZE - 4) >> 1, dev->base_addr + SONIC_EOBC);
327
328         /* let SONIC read the first RRA descriptor */
329
330         outw(CMDREG_RRRA, dev->base_addr + SONIC_CMDREG);
331         if (!wait_timeout(dev, SONIC_CMDREG, CMDREG_RRRA, 0, 2)) {
332                 printk(KERN_ERR "%s: SONIC did not respond on RRRA command - giving up.", dev->name);
333                 return 0;
334         }
335
336         /* point SONIC to the first RDA */
337
338         outw(0, dev->base_addr + SONIC_URDA);
339         outw(priv->rdastart, dev->base_addr + SONIC_CRDA);
340
341         /* set upper half of TDA address */
342
343         outw(0, dev->base_addr + SONIC_UTDA);
344
345         return 1;
346 }
347
348 /* stop SONIC so we can reinitialize it */
349
350 static void StopSONIC(struct net_device *dev)
351 {
352         /* disable interrupts */
353
354         outb(inb(dev->base_addr + BCMREG) & (~BCMREG_IEN), dev->base_addr + BCMREG);
355         outb(0, dev->base_addr + SONIC_IMREG);
356
357         /* reset the SONIC */
358
359         outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
360         udelay(10);
361         outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
362 }
363
364 /* initialize card and SONIC for proper operation */
365
366 static void putcam(camentry_t * cams, int *camcnt, char *addr)
367 {
368         camentry_t *pcam = cams + (*camcnt);
369         u8 *uaddr = (u8 *) addr;
370
371         pcam->index = *camcnt;
372         pcam->addr0 = (((u16) uaddr[1]) << 8) | uaddr[0];
373         pcam->addr1 = (((u16) uaddr[3]) << 8) | uaddr[2];
374         pcam->addr2 = (((u16) uaddr[5]) << 8) | uaddr[4];
375         (*camcnt)++;
376 }
377
378 static void InitBoard(struct net_device *dev)
379 {
380         int camcnt;
381         camentry_t cams[16];
382         u32 cammask;
383         struct dev_mc_list *mcptr;
384         u16 rcrval;
385
386         /* reset the SONIC */
387
388         outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
389         udelay(10);
390
391         /* clear all spurious interrupts */
392
393         outw(inw(dev->base_addr + SONIC_ISREG), dev->base_addr + SONIC_ISREG);
394
395         /* set up the SONIC's bus interface - constant for this adapter -
396            must be done while the SONIC is in reset */
397
398         outw(DCREG_USR1 | DCREG_USR0 | DCREG_WC1 | DCREG_DW32, dev->base_addr + SONIC_DCREG);
399         outw(0, dev->base_addr + SONIC_DCREG2);
400
401         /* remove reset form the SONIC */
402
403         outw(0, dev->base_addr + SONIC_CMDREG);
404         udelay(10);
405
406         /* data sheet requires URRA to be programmed before setting up the CAM contents */
407
408         outw(0, dev->base_addr + SONIC_URRA);
409
410         /* program the CAM entry 0 to the device address */
411
412         camcnt = 0;
413         putcam(cams, &camcnt, dev->dev_addr);
414
415         /* start putting the multicast addresses into the CAM list.  Stop if
416            it is full. */
417
418         for (mcptr = dev->mc_list; mcptr != NULL; mcptr = mcptr->next) {
419                 putcam(cams, &camcnt, mcptr->dmi_addr);
420                 if (camcnt == 16)
421                         break;
422         }
423
424         /* calculate CAM mask */
425
426         cammask = (1 << camcnt) - 1;
427
428         /* feed CDA into SONIC, initialize RCR value (always get broadcasts) */
429
430         isa_memcpy_toio(dev->mem_start, cams, sizeof(camentry_t) * camcnt);
431         isa_memcpy_toio(dev->mem_start + (sizeof(camentry_t) * camcnt), &cammask, sizeof(cammask));
432
433 #ifdef DEBUG
434         printk("CAM setup:\n");
435         dumpmem(dev, 0, sizeof(camentry_t) * camcnt + sizeof(cammask));
436 #endif
437
438         outw(0, dev->base_addr + SONIC_CAMPTR);
439         outw(camcnt, dev->base_addr + SONIC_CAMCNT);
440         outw(CMDREG_LCAM, dev->base_addr + SONIC_CMDREG);
441         if (!wait_timeout(dev, SONIC_CMDREG, CMDREG_LCAM, 0, 2)) {
442                 printk(KERN_ERR "%s:SONIC did not respond on LCAM command - giving up.", dev->name);
443                 return;
444         } else {
445                 /* clear interrupt condition */
446
447                 outw(ISREG_LCD, dev->base_addr + SONIC_ISREG);
448
449 #ifdef DEBUG
450                 printk("Loading CAM done, address pointers %04x:%04x\n",
451                        inw(dev->base_addr + SONIC_URRA),
452                        inw(dev->base_addr + SONIC_CAMPTR));
453                 {
454                         int z;
455
456                         printk("\n-->CAM: PTR %04x CNT %04x\n",
457                                inw(dev->base_addr + SONIC_CAMPTR),
458                                inw(dev->base_addr + SONIC_CAMCNT));
459                         outw(CMDREG_RST, dev->base_addr + SONIC_CMDREG);
460                         for (z = 0; z < camcnt; z++) {
461                                 outw(z, dev->base_addr + SONIC_CAMEPTR);
462                                 printk("Entry %d: %04x %04x %04x\n", z,
463                                        inw(dev->base_addr + SONIC_CAMADDR0),
464                                        inw(dev->base_addr + SONIC_CAMADDR1),
465                                        inw(dev->base_addr + SONIC_CAMADDR2));
466                         }
467                         outw(0, dev->base_addr + SONIC_CMDREG);
468                 }
469 #endif
470         }
471
472         rcrval = RCREG_BRD | RCREG_LB_NONE;
473
474         /* if still multicast addresses left or ALLMULTI is set, set the multicast
475            enable bit */
476
477         if ((dev->flags & IFF_ALLMULTI) || (mcptr != NULL))
478                 rcrval |= RCREG_AMC;
479
480         /* promiscous mode ? */
481
482         if (dev->flags & IFF_PROMISC)
483                 rcrval |= RCREG_PRO;
484
485         /* program receive mode */
486
487         outw(rcrval, dev->base_addr + SONIC_RCREG);
488 #ifdef DEBUG
489         printk("\nRCRVAL: %04x\n", rcrval);
490 #endif
491
492         /* set up descriptors in shared memory + feed them into SONIC registers */
493
494         InitDscrs(dev);
495         if (!InitSONIC(dev))
496                 return;
497
498         /* reset all pending interrupts */
499
500         outw(0xffff, dev->base_addr + SONIC_ISREG);
501
502         /* enable transmitter + receiver interrupts */
503
504         outw(CMDREG_RXEN, dev->base_addr + SONIC_CMDREG);
505         outw(IMREG_PRXEN | IMREG_RBEEN | IMREG_PTXEN | IMREG_TXEREN, dev->base_addr + SONIC_IMREG);
506
507         /* turn on card interrupts */
508
509         outb(inb(dev->base_addr + BCMREG) | BCMREG_IEN, dev->base_addr + BCMREG);
510
511 #ifdef DEBUG
512         printk("Register dump after initialization:\n");
513         dumpregs(dev);
514 #endif
515 }
516
517 /* start transmission of a descriptor */
518
519 static void StartTx(struct net_device *dev, int descr)
520 {
521         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
522         int addr;
523
524         addr = priv->tdastart + (descr * sizeof(tda_t));
525
526         /* put descriptor address into SONIC */
527
528         outw(addr, dev->base_addr + SONIC_CTDA);
529
530         /* trigger transmitter */
531
532         priv->currtxdescr = descr;
533         outw(CMDREG_TXP, dev->base_addr + SONIC_CMDREG);
534 }
535
536 /* ------------------------------------------------------------------------
537  * interrupt handler(s)
538  * ------------------------------------------------------------------------ */
539
540 /* receive buffer area exhausted */
541
542 static void irqrbe_handler(struct net_device *dev)
543 {
544         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
545
546         /* point the SONIC back to the RRA start */
547
548         outw(priv->rrastart, dev->base_addr + SONIC_RRP);
549         outw(priv->rrastart, dev->base_addr + SONIC_RWP);
550 }
551
552 /* receive interrupt */
553
554 static void irqrx_handler(struct net_device *dev)
555 {
556         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
557         rda_t rda;
558         u32 rdaaddr, lrdaaddr;
559
560         /* loop until ... */
561
562         while (1) {
563                 /* read descriptor that was next to be filled by SONIC */
564
565                 rdaaddr = priv->rdastart + (priv->nextrxdescr * sizeof(rda_t));
566                 lrdaaddr = priv->rdastart + (priv->lastrxdescr * sizeof(rda_t));
567                 isa_memcpy_fromio(&rda, dev->mem_start + rdaaddr, sizeof(rda_t));
568
569                 /* iron out upper word halves of fields we use - SONIC will duplicate 
570                    bits 0..15 to 16..31 */
571
572                 rda.status &= 0xffff;
573                 rda.length &= 0xffff;
574                 rda.startlo &= 0xffff;
575
576                 /* stop if the SONIC still owns it, i.e. there is no data for us */
577
578                 if (rda.inuse)
579                         break;
580
581                 /* good packet? */
582
583                 else if (rda.status & RCREG_PRX) {
584                         struct sk_buff *skb;
585
586                         /* fetch buffer */
587
588                         skb = dev_alloc_skb(rda.length + 2);
589                         if (skb == NULL)
590                                 priv->stat.rx_dropped++;
591                         else {
592                                 /* copy out data */
593
594                                 isa_memcpy_fromio(skb_put(skb, rda.length),
595                                                dev->mem_start +
596                                                rda.startlo, rda.length);
597
598                                 /* set up skb fields */
599
600                                 skb->dev = dev;
601                                 skb->protocol = eth_type_trans(skb, dev);
602                                 skb->ip_summed = CHECKSUM_NONE;
603
604                                 /* bookkeeping */
605                                 dev->last_rx = jiffies;
606                                 priv->stat.rx_packets++;
607                                 priv->stat.rx_bytes += rda.length;
608
609                                 /* pass to the upper layers */
610                                 netif_rx(skb);
611                         }
612                 }
613
614                 /* otherwise check error status bits and increase statistics */
615
616                 else {
617                         priv->stat.rx_errors++;
618                         if (rda.status & RCREG_FAER)
619                                 priv->stat.rx_frame_errors++;
620                         if (rda.status & RCREG_CRCR)
621                                 priv->stat.rx_crc_errors++;
622                 }
623
624                 /* descriptor processed, will become new last descriptor in queue */
625
626                 rda.link = 1;
627                 rda.inuse = 1;
628                 isa_memcpy_toio(dev->mem_start + rdaaddr, &rda,
629                              sizeof(rda_t));
630
631                 /* set up link and EOL = 0 in currently last descriptor. Only write
632                    the link field since the SONIC may currently already access the
633                    other fields. */
634
635                 isa_memcpy_toio(dev->mem_start + lrdaaddr + 20, &rdaaddr, 4);
636
637                 /* advance indices */
638
639                 priv->lastrxdescr = priv->nextrxdescr;
640                 if ((++priv->nextrxdescr) >= priv->rxbufcnt)
641                         priv->nextrxdescr = 0;
642         }
643 }
644
645 /* transmit interrupt */
646
647 static void irqtx_handler(struct net_device *dev)
648 {
649         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
650         tda_t tda;
651
652         /* fetch descriptor (we forgot the size ;-) */
653         isa_memcpy_fromio(&tda, dev->mem_start + priv->tdastart + (priv->currtxdescr * sizeof(tda_t)), sizeof(tda_t));
654
655         /* update statistics */
656         priv->stat.tx_packets++;
657         priv->stat.tx_bytes += tda.length;
658
659         /* update our pointers */
660         priv->txused[priv->currtxdescr] = 0;
661         priv->txusedcnt--;
662
663         /* if there are more descriptors present in RAM, start them */
664         if (priv->txusedcnt > 0)
665                 StartTx(dev, (priv->currtxdescr + 1) % TXBUFCNT);
666
667         /* tell the upper layer we can go on transmitting */
668         netif_wake_queue(dev);
669 }
670
671 static void irqtxerr_handler(struct net_device *dev)
672 {
673         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
674         tda_t tda;
675
676         /* fetch descriptor to check status */
677         isa_memcpy_fromio(&tda, dev->mem_start + priv->tdastart + (priv->currtxdescr * sizeof(tda_t)), sizeof(tda_t));
678
679         /* update statistics */
680         priv->stat.tx_errors++;
681         if (tda.status & (TCREG_NCRS | TCREG_CRSL))
682                 priv->stat.tx_carrier_errors++;
683         if (tda.status & TCREG_EXC)
684                 priv->stat.tx_aborted_errors++;
685         if (tda.status & TCREG_OWC)
686                 priv->stat.tx_window_errors++;
687         if (tda.status & TCREG_FU)
688                 priv->stat.tx_fifo_errors++;
689
690         /* update our pointers */
691         priv->txused[priv->currtxdescr] = 0;
692         priv->txusedcnt--;
693
694         /* if there are more descriptors present in RAM, start them */
695         if (priv->txusedcnt > 0)
696                 StartTx(dev, (priv->currtxdescr + 1) % TXBUFCNT);
697
698         /* tell the upper layer we can go on transmitting */
699         netif_wake_queue(dev);
700 }
701
702 /* general interrupt entry */
703
704 static irqreturn_t irq_handler(int irq, void *device, struct pt_regs *regs)
705 {
706         struct net_device *dev = (struct net_device *) device;
707         u16 ival;
708
709         /* in case we're not meant... */
710         if (!(inb(dev->base_addr + BCMREG) & BCMREG_IPEND))
711                 return IRQ_NONE;
712
713         /* loop through the interrupt bits until everything is clear */
714         while (1) {
715                 ival = inw(dev->base_addr + SONIC_ISREG);
716
717                 if (ival & ISREG_RBE) {
718                         irqrbe_handler(dev);
719                         outw(ISREG_RBE, dev->base_addr + SONIC_ISREG);
720                 }
721                 if (ival & ISREG_PKTRX) {
722                         irqrx_handler(dev);
723                         outw(ISREG_PKTRX, dev->base_addr + SONIC_ISREG);
724                 }
725                 if (ival & ISREG_TXDN) {
726                         irqtx_handler(dev);
727                         outw(ISREG_TXDN, dev->base_addr + SONIC_ISREG);
728                 }
729                 if (ival & ISREG_TXER) {
730                         irqtxerr_handler(dev);
731                         outw(ISREG_TXER, dev->base_addr + SONIC_ISREG);
732                 }
733                 break;
734         }
735         return IRQ_HANDLED;
736 }
737
738 /* ------------------------------------------------------------------------
739  * driver methods
740  * ------------------------------------------------------------------------ */
741
742 /* MCA info */
743
744 static int ibmlana_getinfo(char *buf, int slot, void *d)
745 {
746         int len = 0, i;
747         struct net_device *dev = (struct net_device *) d;
748         ibmlana_priv *priv;
749
750         /* can't say anything about an uninitialized device... */
751
752         if (dev == NULL)
753                 return len;
754         if (dev->priv == NULL)
755                 return len;
756         priv = (ibmlana_priv *) dev->priv;
757
758         /* print info */
759
760         len += sprintf(buf + len, "IRQ: %d\n", priv->realirq);
761         len += sprintf(buf + len, "I/O: %#lx\n", dev->base_addr);
762         len += sprintf(buf + len, "Memory: %#lx-%#lx\n", dev->mem_start, dev->mem_end - 1);
763         len += sprintf(buf + len, "Transceiver: %s\n", MediaNames[priv->medium]);
764         len += sprintf(buf + len, "Device: %s\n", dev->name);
765         len += sprintf(buf + len, "MAC address:");
766         for (i = 0; i < 6; i++)
767                 len += sprintf(buf + len, " %02x", dev->dev_addr[i]);
768         buf[len++] = '\n';
769         buf[len] = 0;
770
771         return len;
772 }
773
774 /* open driver.  Means also initialization and start of LANCE */
775
776 static int ibmlana_open(struct net_device *dev)
777 {
778         int result;
779         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
780
781         /* register resources - only necessary for IRQ */
782
783         result = request_irq(priv->realirq, irq_handler, SA_SHIRQ | SA_SAMPLE_RANDOM, dev->name, dev);
784         if (result != 0) {
785                 printk(KERN_ERR "%s: failed to register irq %d\n", dev->name, dev->irq);
786                 return result;
787         }
788         dev->irq = priv->realirq;
789
790         /* set up the card and SONIC */
791         InitBoard(dev);
792
793         /* initialize operational flags */
794         netif_start_queue(dev);
795         return 0;
796 }
797
798 /* close driver.  Shut down board and free allocated resources */
799
800 static int ibmlana_close(struct net_device *dev)
801 {
802         /* turn off board */
803
804         /* release resources */
805         if (dev->irq != 0)
806                 free_irq(dev->irq, dev);
807         dev->irq = 0;
808         return 0;
809 }
810
811 /* transmit a block. */
812
813 static int ibmlana_tx(struct sk_buff *skb, struct net_device *dev)
814 {
815         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
816         int retval = 0, tmplen, addr;
817         unsigned long flags;
818         tda_t tda;
819         int baddr;
820
821         /* find out if there are free slots for a frame to transmit. If not,
822            the upper layer is in deep desperation and we simply ignore the frame. */
823
824         if (priv->txusedcnt >= TXBUFCNT) {
825                 retval = -EIO;
826                 priv->stat.tx_dropped++;
827                 goto tx_done;
828         }
829
830         /* copy the frame data into the next free transmit buffer - fillup missing */
831         tmplen = skb->len;
832         if (tmplen < 60)
833                 tmplen = 60;
834         baddr = priv->txbufstart + (priv->nexttxdescr * PKTSIZE);
835         isa_memcpy_toio(dev->mem_start + baddr, skb->data, skb->len);
836
837         /* copy filler into RAM - in case we're filling up... 
838            we're filling a bit more than necessary, but that doesn't harm
839            since the buffer is far larger... 
840            Sorry Linus for the filler string but I couldn't resist ;-) */
841
842         if (tmplen > skb->len) {
843                 char *fill = "NetBSD is a nice OS too! ";
844                 unsigned int destoffs = skb->len, l = strlen(fill);
845
846                 while (destoffs < tmplen) {
847                         isa_memcpy_toio(dev->mem_start + baddr + destoffs, fill, l);
848                         destoffs += l;
849                 }
850         }
851
852         /* set up the new frame descriptor */
853         addr = priv->tdastart + (priv->nexttxdescr * sizeof(tda_t));
854         isa_memcpy_fromio(&tda, dev->mem_start + addr, sizeof(tda_t));
855         tda.length = tda.fraglength = tmplen;
856         isa_memcpy_toio(dev->mem_start + addr, &tda, sizeof(tda_t));
857
858         /* if there were no active descriptors, trigger the SONIC */
859         spin_lock_irqsave(&priv->lock, flags);
860
861         priv->txusedcnt++;
862         priv->txused[priv->nexttxdescr] = 1;
863
864         /* are all transmission slots used up ? */
865         if (priv->txusedcnt >= TXBUFCNT)
866                 netif_stop_queue(dev);
867
868         if (priv->txusedcnt == 1)
869                 StartTx(dev, priv->nexttxdescr);
870         priv->nexttxdescr = (priv->nexttxdescr + 1) % TXBUFCNT;
871
872         spin_unlock_irqrestore(&priv->lock, flags);
873 tx_done:
874         dev_kfree_skb(skb);
875         return retval;
876 }
877
878 /* return pointer to Ethernet statistics */
879
880 static struct net_device_stats *ibmlana_stats(struct net_device *dev)
881 {
882         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
883         return &priv->stat;
884 }
885
886 /* we don't support runtime reconfiguration, since am MCA card can
887    be unambigously identified by its POS registers. */
888
889 static int ibmlana_config(struct net_device *dev, struct ifmap *map)
890 {
891         return 0;
892 }
893
894 /* switch receiver mode. */
895
896 static void ibmlana_set_multicast_list(struct net_device *dev)
897 {
898         /* first stop the SONIC... */
899         StopSONIC(dev);
900         /* ...then reinit it with the new flags */
901         InitBoard(dev);
902 }
903
904 /* ------------------------------------------------------------------------
905  * hardware check
906  * ------------------------------------------------------------------------ */
907
908 static int startslot;           /* counts through slots when probing multiple devices */
909
910 static int ibmlana_probe(struct net_device *dev)
911 {
912         int force_detect = 0;
913         int slot, z;
914         int base = 0, irq = 0, iobase = 0, memlen = 0;
915         ibmlana_priv *priv;
916         ibmlana_medium medium;
917
918         SET_MODULE_OWNER(dev);
919
920         /* can't work without an MCA bus ;-) */
921         if (MCA_bus == 0)
922                 return -ENODEV;
923
924         /* start address of 1 --> forced detection */
925         if (dev->mem_start == 1)
926                 force_detect = 1;
927
928         base = dev->mem_start;
929         irq = dev->irq;
930
931         for (slot = startslot; (slot = mca_find_adapter(IBM_LANA_ID, slot)) != -1; slot++) {
932                 /* deduce card addresses */
933                 getaddrs(slot, &base, &memlen, &iobase, &irq, &medium);
934
935                 /* slot already in use ? */
936                 if (mca_is_adapter_used(slot))
937                         continue;
938                 /* were we looking for something different ? */
939                 if (dev->irq && dev->irq != irq)
940                         continue;
941                 if (dev->mem_start && dev->mem_start != base)
942                         continue;
943                 /* found something that matches */
944                 break;
945         }
946
947         /* nothing found ? */
948         if (slot == -1)
949                 return (base != 0 || irq != 0) ? -ENXIO : -ENODEV;
950
951         /* announce success */
952         printk(KERN_INFO "%s: IBM LAN Adapter/A found in slot %d\n", dev->name, slot + 1);
953
954         /* try to obtain I/O range */
955         if (!request_region(iobase, IBM_LANA_IORANGE, dev->name)) {
956                 printk(KERN_ERR "%s: cannot allocate I/O range at %#x!\n", dev->name, iobase);
957                 startslot = slot + 1;
958                 return -EBUSY;
959         }
960
961         /* make procfs entries */
962         mca_set_adapter_name(slot, "IBM LAN Adapter/A");
963         mca_set_adapter_procfn(slot, (MCA_ProcFn) ibmlana_getinfo, dev);
964
965         mca_mark_as_used(slot);
966
967         /* allocate structure */
968         priv = dev->priv;
969         priv->slot = slot;
970         priv->realirq = irq;
971         priv->medium = medium;
972         spin_lock_init(&priv->lock);
973
974         /* set base + irq for this device (irq not allocated so far) */
975
976         dev->irq = 0;
977         dev->mem_start = base;
978         dev->mem_end = base + memlen;
979         dev->base_addr = iobase;
980
981         /* set methods */
982
983         dev->open = ibmlana_open;
984         dev->stop = ibmlana_close;
985         dev->set_config = ibmlana_config;
986         dev->hard_start_xmit = ibmlana_tx;
987         dev->do_ioctl = NULL;
988         dev->get_stats = ibmlana_stats;
989         dev->set_multicast_list = ibmlana_set_multicast_list;
990         dev->flags |= IFF_MULTICAST;
991
992         /* copy out MAC address */
993
994         for (z = 0; z < sizeof(dev->dev_addr); z++)
995                 dev->dev_addr[z] = inb(dev->base_addr + MACADDRPROM + z);
996
997         /* print config */
998
999         printk(KERN_INFO "%s: IRQ %d, I/O %#lx, memory %#lx-%#lx, "
1000                "MAC address %02x:%02x:%02x:%02x:%02x:%02x.\n",
1001                dev->name, priv->realirq, dev->base_addr,
1002                dev->mem_start, dev->mem_end - 1,
1003                dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
1004                dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
1005         printk(KERN_INFO "%s: %s medium\n", dev->name, MediaNames[priv->medium]);
1006
1007         /* reset board */
1008
1009         ResetBoard(dev);
1010
1011         /* next probe will start at next slot */
1012
1013         startslot = slot + 1;
1014
1015         return 0;
1016 }
1017
1018 /* ------------------------------------------------------------------------
1019  * modularization support
1020  * ------------------------------------------------------------------------ */
1021
1022 #ifdef MODULE
1023
1024 #define DEVMAX 5
1025
1026 static struct net_device *moddevs[DEVMAX];
1027 static int irq;
1028 static int io;
1029
1030 MODULE_PARM(irq, "i");
1031 MODULE_PARM(io, "i");
1032 MODULE_PARM_DESC(irq, "IBM LAN/A IRQ number");
1033 MODULE_PARM_DESC(io, "IBM LAN/A I/O base address");
1034 MODULE_LICENSE("GPL");
1035
1036 int init_module(void)
1037 {
1038         int z;
1039
1040         startslot = 0;
1041         for (z = 0; z < DEVMAX; z++) {
1042                 struct net_device *dev = alloc_etherdev(sizeof(ibmlana_priv));
1043                 if (!dev)
1044                         break;
1045                 dev->irq = irq;
1046                 dev->base_addr = io;
1047                 if (ibmlana_probe(dev)) {
1048                         free_netdev(dev);
1049                         break;
1050                 }
1051                 if (register_netdev(dev)) {
1052                         ibmlana_priv *priv = dev->priv;
1053                         release_region(dev->base_addr, IBM_LANA_IORANGE);
1054                         mca_mark_as_unused(priv->slot);
1055                         mca_set_adapter_name(priv->slot, "");
1056                         mca_set_adapter_procfn(priv->slot, NULL, NULL);
1057                         free_netdev(dev);
1058                         break;
1059                 }
1060                 moddevs[z] = dev;
1061         }
1062         return (z > 0) ? 0 : -EIO;
1063 }
1064
1065 void cleanup_module(void)
1066 {
1067         int z;
1068         for (z = 0; z < DEVMAX; z++) {
1069                 struct net_device *dev = moddevs[z];
1070                 if (dev) {
1071                         ibmlana_priv *priv = (ibmlana_priv *) dev->priv;
1072                         unregister_netdev(dev);
1073                         /*DeinitBoard(dev); */
1074                         release_region(dev->base_addr, IBM_LANA_IORANGE);
1075                         mca_mark_as_unused(priv->slot);
1076                         mca_set_adapter_name(priv->slot, "");
1077                         mca_set_adapter_procfn(priv->slot, NULL, NULL);
1078                         free_netdev(dev);
1079                 }
1080         }
1081 }
1082 #endif                          /* MODULE */