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