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