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