File: /usr/src/linux/drivers/net/declance.c

1     /*     
2      *    Lance ethernet driver for the MIPS processor based
3      *      DECstation family
4      *
5      *
6      *      adopted from sunlance.c by Richard van den Berg
7      *
8      *      additional sources:
9      *      - PMAD-AA TURBOchannel Ethernet Module Functional Specification,
10      *        Revision 1.2
11      *
12      *      History:
13      *
14      *      v0.001: The kernel accepts the code and it shows the hardware address.
15      *
16      *      v0.002: Removed most sparc stuff, left only some module and dma stuff.
17      *
18      *      v0.003: Enhanced base address calculation from proposals by
19      *      Harald Koerfgen and Thomas Riemer.
20      *
21      *      v0.004: lance-regs is pointing at the right addresses, added prom
22      *      check. First start of address mapping and DMA.
23      *
24      *      v0.005: started to play around with LANCE-DMA. This driver will not work
25      *      for non IOASIC lances. HK
26      *
27      *      v0.006: added pointer arrays to lance_private and setup routine for them
28      *      in dec_lance_init. HK
29      *
30      *      v0.007: Big shit. The LANCE seems to use a different DMA mechanism to access
31      *      the init block. This looks like one (short) word at a time, but the smallest
32      *      amount the IOASIC can transfer is a (long) word. So we have a 2-2 padding here.
33      *      Changed lance_init_block accordingly. The 16-16 padding for the buffers
34      *      seems to be correct. HK
35      *
36      *     v0.008 - mods to make PMAX_LANCE work. 01/09/1999 triemer
37      */
38     
39     #undef DEBUG_DRIVER
40     
41     static char *version =
42     "declance.c: v0.008 by Linux Mips DECstation task force\n";
43     
44     static char *lancestr = "LANCE";
45     
46     /*
47      * card types
48      */
49     #define ASIC_LANCE 1
50     #define PMAD_LANCE 2
51     #define PMAX_LANCE 3
52     
53     #include <linux/init.h>
54     #include <linux/kernel.h>
55     #include <linux/netdevice.h>
56     
57     #include <asm/dec/interrupts.h>
58     #include <asm/dec/ioasic_ints.h>
59     #include <asm/dec/ioasic_addrs.h>
60     #include <asm/dec/machtype.h>
61     #include <asm/dec/tc.h>
62     #include <asm/dec/kn01.h>
63     #include <asm/wbflush.h>
64     #include <asm/addrspace.h>
65     
66     #include <linux/config.h>
67     #include <linux/errno.h>
68     #include <linux/hdreg.h>
69     #include <linux/ioport.h>
70     #include <linux/sched.h>
71     #include <linux/mm.h>
72     #include <linux/stddef.h>
73     #include <linux/string.h>
74     #include <linux/unistd.h>
75     #include <linux/ptrace.h>
76     #include <linux/slab.h>
77     #include <linux/user.h>
78     #include <linux/utsname.h>
79     #include <linux/a.out.h>
80     #include <linux/tty.h>
81     #include <linux/delay.h>
82     #include <asm/io.h>
83     #include <linux/etherdevice.h>
84     
85     #ifndef CONFIG_TC
86     unsigned long system_base;
87     unsigned long dmaptr;
88     #endif
89     static int type;
90     
91     #define CRC_POLYNOMIAL_BE 0x04c11db7UL	/* Ethernet CRC, big endian */
92     #define CRC_POLYNOMIAL_LE 0xedb88320UL	/* Ethernet CRC, little endian */
93     
94     #define LE_CSR0 0
95     #define LE_CSR1 1
96     #define LE_CSR2 2
97     #define LE_CSR3 3
98     
99     #define LE_MO_PROM      0x8000	/* Enable promiscuous mode */
100     
101     #define	LE_C0_ERR	0x8000	/* Error: set if BAB, SQE, MISS or ME is set */
102     #define	LE_C0_BABL	0x4000	/* BAB:  Babble: tx timeout. */
103     #define	LE_C0_CERR	0x2000	/* SQE:  Signal quality error */
104     #define	LE_C0_MISS	0x1000	/* MISS: Missed a packet */
105     #define	LE_C0_MERR	0x0800	/* ME:   Memory error */
106     #define	LE_C0_RINT	0x0400	/* Received interrupt */
107     #define	LE_C0_TINT	0x0200	/* Transmitter Interrupt */
108     #define	LE_C0_IDON	0x0100	/* IFIN: Init finished. */
109     #define	LE_C0_INTR	0x0080	/* Interrupt or error */
110     #define	LE_C0_INEA	0x0040	/* Interrupt enable */
111     #define	LE_C0_RXON	0x0020	/* Receiver on */
112     #define	LE_C0_TXON	0x0010	/* Transmitter on */
113     #define	LE_C0_TDMD	0x0008	/* Transmitter demand */
114     #define	LE_C0_STOP	0x0004	/* Stop the card */
115     #define	LE_C0_STRT	0x0002	/* Start the card */
116     #define	LE_C0_INIT	0x0001	/* Init the card */
117     
118     #define	LE_C3_BSWP	0x4	/* SWAP */
119     #define	LE_C3_ACON	0x2	/* ALE Control */
120     #define	LE_C3_BCON	0x1	/* Byte control */
121     
122     /* Receive message descriptor 1 */
123     #define LE_R1_OWN       0x80	/* Who owns the entry */
124     #define LE_R1_ERR       0x40	/* Error: if FRA, OFL, CRC or BUF is set */
125     #define LE_R1_FRA       0x20	/* FRA: Frame error */
126     #define LE_R1_OFL       0x10	/* OFL: Frame overflow */
127     #define LE_R1_CRC       0x08	/* CRC error */
128     #define LE_R1_BUF       0x04	/* BUF: Buffer error */
129     #define LE_R1_SOP       0x02	/* Start of packet */
130     #define LE_R1_EOP       0x01	/* End of packet */
131     #define LE_R1_POK       0x03	/* Packet is complete: SOP + EOP */
132     
133     #define LE_T1_OWN       0x80	/* Lance owns the packet */
134     #define LE_T1_ERR       0x40	/* Error summary */
135     #define LE_T1_EMORE     0x10	/* Error: more than one retry needed */
136     #define LE_T1_EONE      0x08	/* Error: one retry needed */
137     #define LE_T1_EDEF      0x04	/* Error: deferred */
138     #define LE_T1_SOP       0x02	/* Start of packet */
139     #define LE_T1_EOP       0x01	/* End of packet */
140     #define LE_T1_POK	0x03	/* Packet is complete: SOP + EOP */
141     
142     #define LE_T3_BUF       0x8000	/* Buffer error */
143     #define LE_T3_UFL       0x4000	/* Error underflow */
144     #define LE_T3_LCOL      0x1000	/* Error late collision */
145     #define LE_T3_CLOS      0x0800	/* Error carrier loss */
146     #define LE_T3_RTY       0x0400	/* Error retry */
147     #define LE_T3_TDR       0x03ff	/* Time Domain Reflectometry counter */
148     
149     /* Define: 2^4 Tx buffers and 2^4 Rx buffers */
150     
151     #ifndef LANCE_LOG_TX_BUFFERS
152     #define LANCE_LOG_TX_BUFFERS 4
153     #define LANCE_LOG_RX_BUFFERS 4
154     #endif
155     
156     #define TX_RING_SIZE			(1 << (LANCE_LOG_TX_BUFFERS))
157     #define TX_RING_MOD_MASK		(TX_RING_SIZE - 1)
158     
159     #define RX_RING_SIZE			(1 << (LANCE_LOG_RX_BUFFERS))
160     #define RX_RING_MOD_MASK		(RX_RING_SIZE - 1)
161     
162     #define PKT_BUF_SZ		1536
163     #define RX_BUFF_SIZE            PKT_BUF_SZ
164     #define TX_BUFF_SIZE            PKT_BUF_SZ
165     
166     #undef TEST_HITS
167     #define DEBUG_DRIVER 1
168     
169     #define ZERO 0
170     
171     /* The DS2000/3000 have a linear 64 KB buffer.
172     
173      * The PMAD-AA has 128 kb buffer on-board. 
174      *
175      * The IOASIC LANCE devices use a shared memory region. This region as seen 
176      * from the CPU is (max) 128 KB long and has to be on an 128 KB boundary.
177      * The LANCE sees this as a 64 KB long continuous memory region.
178      *
179      * The LANCE's DMA address is used as an index in this buffer and DMA takes
180      * place in bursts of eight 16-Bit words which are packed into four 32-Bit words
181      * by the IOASIC. This leads to a strange padding: 16 bytes of valid data followed
182      * by a 16 byte gap :-(.
183      */
184     
185     struct lance_rx_desc {
186     	unsigned short rmd0;	/* low address of packet */
187     	short gap0;
188     	unsigned char rmd1_hadr;	/* high address of packet */
189     	unsigned char rmd1_bits;	/* descriptor bits */
190     	short gap1;
191     	short length;		/* This length is 2s complement (negative)!
192     				   * Buffer length
193     				 */
194     	short gap2;
195     	unsigned short mblength;	/* This is the actual number of bytes received */
196     	short gap3;
197     };
198     
199     struct lance_tx_desc {
200     	unsigned short tmd0;	/* low address of packet */
201     	short gap0;
202     	unsigned char tmd1_hadr;	/* high address of packet */
203     	unsigned char tmd1_bits;	/* descriptor bits */
204     	short gap1;
205     	short length;		/* Length is 2s complement (negative)! */
206     	short gap2;
207     	unsigned short misc;
208     	short gap3;
209     };
210     
211     
212     /* First part of the LANCE initialization block, described in databook. */
213     struct lance_init_block {
214     	unsigned short mode;	/* Pre-set mode (reg. 15) */
215     	short gap0;
216     
217     	unsigned char phys_addr[12];	/* Physical ethernet address
218     					   * only 0, 1, 4, 5, 8, 9 are valid
219     					   * 2, 3, 6, 7, 10, 11 are gaps
220     					 */
221     	unsigned short filter[8];	/* Multicast filter.
222     					   * only 0, 2, 4, 6 are valid
223     					   * 1, 3, 5, 7 are gaps
224     					 */
225     
226     	/* Receive and transmit ring base, along with extra bits. */
227     	unsigned short rx_ptr;	/* receive descriptor addr */
228     	short gap1;
229     	unsigned short rx_len;	/* receive len and high addr */
230     	short gap2;
231     	unsigned short tx_ptr;	/* transmit descriptor addr */
232     	short gap3;
233     	unsigned short tx_len;	/* transmit len and high addr */
234     	short gap4;
235     	char gap5[16];
236     
237     	/* The buffer descriptors */
238     	struct lance_rx_desc brx_ring[RX_RING_SIZE];
239     	struct lance_tx_desc btx_ring[TX_RING_SIZE];
240     };
241     
242     #define BUF_OFFSET_CPU sizeof(struct lance_init_block)
243     #define BUF_OFFSET_LNC (sizeof(struct lance_init_block)>>1)
244     
245     #define libdesc_offset(rt, elem) \
246     ((__u32)(((unsigned long)(&(((struct lance_init_block *)0)->rt[elem])))))
247     
248     /*
249      * This works *only* for the ring descriptors
250      */
251     #define LANCE_ADDR(x) (PHYSADDR(x) >> 1)
252     
253     struct lance_private {
254     	char *name;
255     	volatile struct lance_regs *ll;
256     	volatile struct lance_init_block *init_block;
257     	volatile unsigned long *dma_ptr_reg;
258     
259     	spinlock_t	lock;
260     
261     	int rx_new, tx_new;
262     	int rx_old, tx_old;
263     
264     	struct net_device_stats stats;
265     
266     	unsigned short busmaster_regval;
267     
268     	struct net_device *dev;	/* Backpointer        */
269     	struct lance_private *next_module;
270     	struct timer_list       multicast_timer;
271     
272     	/* Pointers to the ring buffers as seen from the CPU */
273     	char *rx_buf_ptr_cpu[RX_RING_SIZE];
274     	char *tx_buf_ptr_cpu[TX_RING_SIZE];
275     
276     	/* Pointers to the ring buffers as seen from the LANCE */
277     	char *rx_buf_ptr_lnc[RX_RING_SIZE];
278     	char *tx_buf_ptr_lnc[TX_RING_SIZE];
279     };
280     
281     #define TX_BUFFS_AVAIL ((lp->tx_old<=lp->tx_new)?\
282     			lp->tx_old+TX_RING_MOD_MASK-lp->tx_new:\
283     			lp->tx_old - lp->tx_new-1)
284     
285     /* The lance control ports are at an absolute address, machine and tc-slot
286      * dependant.
287      * DECstations do only 32-bit access and the LANCE uses 16 bit addresses,
288      * so we have to give the structure an extra member making rap pointing
289      * at the right address
290      */
291     struct lance_regs {
292     	volatile unsigned short rdp;	/* register data port */
293     	unsigned short pad;
294     	volatile unsigned short rap;	/* register address port */
295     };
296     
297     int dec_lance_debug = 2;
298     
299     /*
300        #ifdef MODULE
301        static struct lance_private *root_lance_dev = NULL;
302        #endif
303      */
304     
305     static inline void writereg(volatile unsigned short *regptr, short value)
306     {
307     	*regptr = value;
308     	wbflush();
309     }
310     
311     /* Load the CSR registers */
312     static void load_csrs(struct lance_private *lp)
313     {
314     	volatile struct lance_regs *ll = lp->ll;
315     	int leptr;
316     
317     	/* The address space as seen from the LANCE
318     	 * begins at address 0. HK
319     	 */
320     	leptr = 0;
321     
322     	writereg(&ll->rap, LE_CSR1);
323     	writereg(&ll->rdp, (leptr & 0xFFFF));
324     	writereg(&ll->rap, LE_CSR2);
325     	writereg(&ll->rdp, leptr >> 16);
326     	writereg(&ll->rap, LE_CSR3);
327     	writereg(&ll->rdp, lp->busmaster_regval);
328     
329     	/* Point back to csr0 */
330     	writereg(&ll->rap, LE_CSR0);
331     }
332     
333     /*
334      * Our specialized copy routines
335      *
336      */
337     void cp_to_buf(void *to, const void *from, __kernel_size_t len)
338     {
339     	unsigned short *tp, *fp, clen;
340     	unsigned char *rtp, *rfp;
341     
342     	if (type == PMAX_LANCE) {
343     		clen = len >> 1;
344     		tp = (unsigned short *) to;
345     		fp = (unsigned short *) from;
346     
347     		while (clen--) {
348     			*tp++ = *fp++;
349     			tp++;
350     		}
351     
352     		clen = len & 1;
353     		rtp = (unsigned char *) tp;
354     		rfp = (unsigned char *) fp;
355     		while (clen--) {
356     			*rtp++ = *rfp++;
357     		}
358     	} else {
359     		/*
360     		 * copy 16 Byte chunks
361     		 */
362     		clen = len >> 4;
363     		tp = (unsigned short *) to;
364     		fp = (unsigned short *) from;
365     		while (clen--) {
366     			*tp++ = *fp++;
367     			*tp++ = *fp++;
368     			*tp++ = *fp++;
369     			*tp++ = *fp++;
370     			*tp++ = *fp++;
371     			*tp++ = *fp++;
372     			*tp++ = *fp++;
373     			*tp++ = *fp++;
374     			tp += 8;
375     		}
376     
377     		/*
378     		 * do the rest, if any.
379     		 */
380     		clen = len & 15;
381     		rtp = (unsigned char *) tp;
382     		rfp = (unsigned char *) fp;
383     		while (clen--) {
384     			*rtp++ = *rfp++;
385     		}
386     	}
387     
388     	wbflush();
389     }
390     
391     void cp_from_buf(void *to, unsigned char *from, int len)
392     {
393     	unsigned short *tp, *fp, clen;
394     	unsigned char *rtp, *rfp;
395     
396     	if (type == PMAX_LANCE) {
397     		clen = len >> 1;
398     		tp = (unsigned short *) to;
399     		fp = (unsigned short *) from;
400     		while (clen--) {
401     			*tp++ = *fp++;
402     			fp++;
403     		}
404     
405     		clen = len & 1;
406     
407     		rtp = (unsigned char *) tp;
408     		rfp = (unsigned char *) fp;
409     
410     		while (clen--) {
411     			*rtp++ = *rfp++;
412     		}
413     	} else {
414     
415     		/*
416     		 * copy 16 Byte chunks
417     		 */
418     		clen = len >> 4;
419     		tp = (unsigned short *) to;
420     		fp = (unsigned short *) from;
421     		while (clen--) {
422     			*tp++ = *fp++;
423     			*tp++ = *fp++;
424     			*tp++ = *fp++;
425     			*tp++ = *fp++;
426     			*tp++ = *fp++;
427     			*tp++ = *fp++;
428     			*tp++ = *fp++;
429     			*tp++ = *fp++;
430     			fp += 8;
431     		}
432     
433     		/*
434     		 * do the rest, if any.
435     		 */
436     		clen = len & 15;
437     		rtp = (unsigned char *) tp;
438     		rfp = (unsigned char *) fp;
439     		while (clen--) {
440     			*rtp++ = *rfp++;
441     		}
442     
443     
444     	}
445     
446     }
447     
448     /* Setup the Lance Rx and Tx rings */
449     static void lance_init_ring(struct net_device *dev)
450     {
451     	struct lance_private *lp = (struct lance_private *) dev->priv;
452     	volatile struct lance_init_block *ib;
453     	int leptr;
454     	int i;
455     
456     	ib = (struct lance_init_block *) (dev->mem_start);
457     
458     	/* Lock out other processes while setting up hardware */
459     	netif_stop_queue(dev);
460     	lp->rx_new = lp->tx_new = 0;
461     	lp->rx_old = lp->tx_old = 0;
462     
463     	/* Copy the ethernet address to the lance init block.
464     	 * XXX bit 0 of the physical address registers has to be zero
465     	 */
466     	ib->phys_addr[0] = dev->dev_addr[0];
467     	ib->phys_addr[1] = dev->dev_addr[1];
468     	ib->phys_addr[4] = dev->dev_addr[2];
469     	ib->phys_addr[5] = dev->dev_addr[3];
470     	ib->phys_addr[8] = dev->dev_addr[4];
471     	ib->phys_addr[9] = dev->dev_addr[5];
472     	/* Setup the initialization block */
473     
474     	/* Setup rx descriptor pointer */
475     	leptr = LANCE_ADDR(libdesc_offset(brx_ring, 0));
476     	ib->rx_len = (LANCE_LOG_RX_BUFFERS << 13) | (leptr >> 16);
477     	ib->rx_ptr = leptr;
478     	if (ZERO)
479     		printk("RX ptr: %8.8x(%8.8x)\n", leptr, libdesc_offset(brx_ring, 0));
480     
481     	/* Setup tx descriptor pointer */
482     	leptr = LANCE_ADDR(libdesc_offset(btx_ring, 0));
483     	ib->tx_len = (LANCE_LOG_TX_BUFFERS << 13) | (leptr >> 16);
484     	ib->tx_ptr = leptr;
485     	if (ZERO)
486     		printk("TX ptr: %8.8x(%8.8x)\n", leptr, libdesc_offset(btx_ring, 0));
487     
488     	if (ZERO)
489     		printk("TX rings:\n");
490     
491     	/* Setup the Tx ring entries */
492     	for (i = 0; i < TX_RING_SIZE; i++) {
493     		leptr = (int) lp->tx_buf_ptr_lnc[i];
494     		ib->btx_ring[i].tmd0 = leptr;
495     		ib->btx_ring[i].tmd1_hadr = leptr >> 16;
496     		ib->btx_ring[i].tmd1_bits = 0;
497     		ib->btx_ring[i].length = 0xf000;	/* The ones required by tmd2 */
498     		ib->btx_ring[i].misc = 0;
499     		if (i < 3 && ZERO)
500     			printk("%d: 0x%8.8x(0x%8.8x)\n", i, leptr, (int) lp->tx_buf_ptr_cpu[i]);
501     	}
502     
503     	/* Setup the Rx ring entries */
504     	if (ZERO)
505     		printk("RX rings:\n");
506     	for (i = 0; i < RX_RING_SIZE; i++) {
507     		leptr = (int) lp->rx_buf_ptr_lnc[i];
508     		ib->brx_ring[i].rmd0 = leptr;
509     		ib->brx_ring[i].rmd1_hadr = leptr >> 16;
510     		ib->brx_ring[i].rmd1_bits = LE_R1_OWN;
511     		ib->brx_ring[i].length = -RX_BUFF_SIZE | 0xf000;
512     		ib->brx_ring[i].mblength = 0;
513     		if (i < 3 && ZERO)
514     			printk("%d: 0x%8.8x(0x%8.8x)\n", i, leptr, (int) lp->rx_buf_ptr_cpu[i]);
515     	}
516     	wbflush();
517     }
518     
519     static int init_restart_lance(struct lance_private *lp)
520     {
521     	volatile struct lance_regs *ll = lp->ll;
522     	int i;
523     
524     	writereg(&ll->rap, LE_CSR0);
525     	writereg(&ll->rdp, LE_C0_INIT);
526     
527     	/* Wait for the lance to complete initialization */
528     	for (i = 0; (i < 100) && !(ll->rdp & LE_C0_IDON); i++) {
529     		udelay(10);
530     	}
531     	if ((i == 100) || (ll->rdp & LE_C0_ERR)) {
532     		printk("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, ll->rdp);
533     		return -1;
534     	}
535     	if ((ll->rdp & LE_C0_ERR)) {
536     		printk("LANCE unopened after %d ticks, csr0=%4.4x.\n", i, ll->rdp);
537     		return -1;
538     	}
539     	writereg(&ll->rdp, LE_C0_IDON);
540     	writereg(&ll->rdp, LE_C0_STRT);
541     	writereg(&ll->rdp, LE_C0_INEA);
542     
543     	return 0;
544     }
545     
546     static int lance_rx(struct net_device *dev)
547     {
548     	struct lance_private *lp = (struct lance_private *) dev->priv;
549     	volatile struct lance_init_block *ib;
550     	volatile struct lance_rx_desc *rd = 0;
551     	unsigned char bits;
552     	int len = 0;
553     	struct sk_buff *skb = 0;
554     	ib = (struct lance_init_block *) (dev->mem_start);
555     
556     #ifdef TEST_HITS
557     	{
558     	int i;
559     
560     	printk("[");
561     	for (i = 0; i < RX_RING_SIZE; i++) {
562     		if (i == lp->rx_new)
563     			printk("%s",
564     			       ib->brx_ring[i].rmd1_bits & LE_R1_OWN ? "_" : "X");
565     		else
566     			printk("%s",
567     			       ib->brx_ring[i].rmd1_bits & LE_R1_OWN ? "." : "1");
568     	}
569     	printk("]");
570     	}
571     #endif
572     
573     
574     	for (rd = &ib->brx_ring[lp->rx_new];
575     	     !((bits = rd->rmd1_bits) & LE_R1_OWN);
576     	     rd = &ib->brx_ring[lp->rx_new]) {
577     
578     		/* We got an incomplete frame? */
579     		if ((bits & LE_R1_POK) != LE_R1_POK) {
580     			lp->stats.rx_over_errors++;
581     			lp->stats.rx_errors++;
582     		} else if (bits & LE_R1_ERR) {
583     			/* Count only the end frame as a rx error,
584     			 * not the beginning
585     			 */
586     			if (bits & LE_R1_BUF)
587     				lp->stats.rx_fifo_errors++;
588     			if (bits & LE_R1_CRC)
589     				lp->stats.rx_crc_errors++;
590     			if (bits & LE_R1_OFL)
591     				lp->stats.rx_over_errors++;
592     			if (bits & LE_R1_FRA)
593     				lp->stats.rx_frame_errors++;
594     			if (bits & LE_R1_EOP)
595     				lp->stats.rx_errors++;
596     		} else {
597     			len = (rd->mblength & 0xfff) - 4;
598     			skb = dev_alloc_skb(len + 2);
599     
600     			if (skb == 0) {
601     				printk("%s: Memory squeeze, deferring packet.\n",
602     				       dev->name);
603     				lp->stats.rx_dropped++;
604     				rd->mblength = 0;
605     				rd->rmd1_bits = LE_R1_OWN;
606     				lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
607     				return 0;
608     			}
609     			lp->stats.rx_bytes += len;
610     
611     			skb->dev = dev;
612     			skb_reserve(skb, 2);	/* 16 byte align */
613     			skb_put(skb, len);	/* make room */
614     
615     			cp_from_buf(skb->data,
616     				 (char *) lp->rx_buf_ptr_cpu[lp->rx_new],
617     				    len);
618     
619     			skb->protocol = eth_type_trans(skb, dev);
620     			netif_rx(skb);
621     			dev->last_rx = jiffies;
622     			lp->stats.rx_packets++;
623     		}
624     
625     		/* Return the packet to the pool */
626     		rd->mblength = 0;
627     		rd->length = -RX_BUFF_SIZE | 0xf000;
628     		rd->rmd1_bits = LE_R1_OWN;
629     		lp->rx_new = (lp->rx_new + 1) & RX_RING_MOD_MASK;
630     	}
631     	return 0;
632     }
633     
634     static void lance_tx(struct net_device *dev)
635     {
636     	struct lance_private *lp = (struct lance_private *) dev->priv;
637     	volatile struct lance_init_block *ib;
638     	volatile struct lance_regs *ll = lp->ll;
639     	volatile struct lance_tx_desc *td;
640     	int i, j;
641     	int status;
642     	ib = (struct lance_init_block *) (dev->mem_start);
643     	j = lp->tx_old;
644     
645     	spin_lock(&lp->lock);
646     
647     	for (i = j; i != lp->tx_new; i = j) {
648     		td = &ib->btx_ring[i];
649     		/* If we hit a packet not owned by us, stop */
650     		if (td->tmd1_bits & LE_T1_OWN)
651     			break;
652     
653     		if (td->tmd1_bits & LE_T1_ERR) {
654     			status = td->misc;
655     
656     			lp->stats.tx_errors++;
657     			if (status & LE_T3_RTY)
658     				lp->stats.tx_aborted_errors++;
659     			if (status & LE_T3_LCOL)
660     				lp->stats.tx_window_errors++;
661     
662     			if (status & LE_T3_CLOS) {
663     				lp->stats.tx_carrier_errors++;
664     				printk("%s: Carrier Lost\n", dev->name);
665     				/* Stop the lance */
666     				writereg(&ll->rap, LE_CSR0);
667     				writereg(&ll->rdp, LE_C0_STOP);
668     				lance_init_ring(dev);
669     				load_csrs(lp);
670     				init_restart_lance(lp);
671     				goto out;
672     			}
673     			/* Buffer errors and underflows turn off the
674     			 * transmitter, restart the adapter.
675     			 */
676     			if (status & (LE_T3_BUF | LE_T3_UFL)) {
677     				lp->stats.tx_fifo_errors++;
678     
679     				printk("%s: Tx: ERR_BUF|ERR_UFL, restarting\n",
680     				       dev->name);
681     				/* Stop the lance */
682     				writereg(&ll->rap, LE_CSR0);
683     				writereg(&ll->rdp, LE_C0_STOP);
684     				lance_init_ring(dev);
685     				load_csrs(lp);
686     				init_restart_lance(lp);
687     				goto out;
688     			}
689     		} else if ((td->tmd1_bits & LE_T1_POK) == LE_T1_POK) {
690     			/*
691     			 * So we don't count the packet more than once.
692     			 */
693     			td->tmd1_bits &= ~(LE_T1_POK);
694     
695     			/* One collision before packet was sent. */
696     			if (td->tmd1_bits & LE_T1_EONE)
697     				lp->stats.collisions++;
698     
699     			/* More than one collision, be optimistic. */
700     			if (td->tmd1_bits & LE_T1_EMORE)
701     				lp->stats.collisions += 2;
702     
703     			lp->stats.tx_packets++;
704     		}
705     		j = (j + 1) & TX_RING_MOD_MASK;
706     	}
707     	lp->tx_old = j;
708     out:
709     	if (netif_queue_stopped(dev) &&
710     	    TX_BUFFS_AVAIL > 0)
711     		netif_wake_queue(dev);
712     
713     	spin_unlock(&lp->lock);
714     }
715     
716     static void lance_interrupt(const int irq, void *dev_id, struct pt_regs *regs)
717     {
718     	struct net_device *dev = (struct net_device *) dev_id;
719     	struct lance_private *lp = (struct lance_private *) dev->priv;
720     	volatile struct lance_regs *ll = lp->ll;
721     	int csr0;
722     
723     	writereg(&ll->rap, LE_CSR0);
724     	csr0 = ll->rdp;
725     
726     	/* Acknowledge all the interrupt sources ASAP */
727     	writereg(&ll->rdp, csr0 & (LE_C0_INTR | LE_C0_TINT | LE_C0_RINT));
728     
729     	if ((csr0 & LE_C0_ERR)) {
730     		/* Clear the error condition */
731     		writereg(&ll->rdp, LE_C0_BABL | LE_C0_ERR | LE_C0_MISS |
732     			 LE_C0_CERR | LE_C0_MERR);
733     	}
734     	if (csr0 & LE_C0_RINT)
735     		lance_rx(dev);
736     
737     	if (csr0 & LE_C0_TINT)
738     		lance_tx(dev);
739     
740     	if (csr0 & LE_C0_BABL)
741     		lp->stats.tx_errors++;
742     
743     	if (csr0 & LE_C0_MISS)
744     		lp->stats.rx_errors++;
745     
746     	if (csr0 & LE_C0_MERR) {
747     		volatile unsigned long int_stat = *(unsigned long *) (system_base + IOCTL + SIR);
748     
749     		printk("%s: Memory error, status %04x\n", dev->name, csr0);
750     
751     		if (int_stat & LANCE_DMA_MEMRDERR) {
752     			printk("%s: DMA error\n", dev->name);
753     			int_stat |= LANCE_DMA_MEMRDERR;
754     			/*
755     			 * re-enable LANCE DMA
756     			 */
757     			*(unsigned long *) (system_base + IOCTL + SSR) |= (1 << 16);
758     			wbflush();
759     		}
760     		writereg(&ll->rdp, LE_C0_STOP);
761     
762     		lance_init_ring(dev);
763     		load_csrs(lp);
764     		init_restart_lance(lp);
765     		netif_wake_queue(dev);
766     	}
767     
768     	writereg(&ll->rdp, LE_C0_INEA);
769     	writereg(&ll->rdp, LE_C0_INEA);
770     }
771     
772     struct net_device *last_dev = 0;
773     
774     static int lance_open(struct net_device *dev)
775     {
776     	volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
777     	struct lance_private *lp = (struct lance_private *) dev->priv;
778     	volatile struct lance_regs *ll = lp->ll;
779     	int status = 0;
780     
781     	last_dev = dev;
782     
783     	/* Stop the Lance */
784     	writereg(&ll->rap, LE_CSR0);
785     	writereg(&ll->rdp, LE_C0_STOP);
786     
787     	/* Set mode and clear multicast filter only at device open,
788     	 * so that lance_init_ring() called at any error will not
789     	 * forget multicast filters.
790     	 *
791     	 * BTW it is common bug in all lance drivers! --ANK
792     	 */
793     	ib->mode = 0;
794     	ib->filter [0] = 0;
795     	ib->filter [2] = 0;
796     
797     	lance_init_ring(dev);
798     	load_csrs(lp);
799     
800     	netif_start_queue(dev);
801     
802     	/* Associate IRQ with lance_interrupt */
803     	if (request_irq(dev->irq, &lance_interrupt, 0, lp->name, dev)) {
804     		printk("Lance: Can't get irq %d\n", dev->irq);
805     		return -EAGAIN;
806     	}
807     
808     	status = init_restart_lance(lp);
809     
810     	/*
811     	 * if (!status)
812     	 *      MOD_INC_USE_COUNT;
813     	 */
814     
815     	return status;
816     }
817     
818     static int lance_close(struct net_device *dev)
819     {
820     	struct lance_private *lp = (struct lance_private *) dev->priv;
821     	volatile struct lance_regs *ll = lp->ll;
822     
823     	netif_stop_queue(dev);
824     	del_timer_sync(&lp->multicast_timer);
825     
826     	/* Stop the card */
827     	writereg(&ll->rap, LE_CSR0);
828     	writereg(&ll->rdp, LE_C0_STOP);
829     
830     	free_irq(dev->irq, (void *) dev);
831     	/*
832     	   MOD_DEC_USE_COUNT;
833     	 */
834     	return 0;
835     }
836     
837     static inline int lance_reset(struct net_device *dev)
838     {
839     	struct lance_private *lp = (struct lance_private *) dev->priv;
840     	volatile struct lance_regs *ll = lp->ll;
841     	int status;
842     
843     	/* Stop the lance */
844     	writereg(&ll->rap, LE_CSR0);
845     	writereg(&ll->rdp, LE_C0_STOP);
846     
847     	lance_init_ring(dev);
848     	load_csrs(lp);
849     	dev->trans_start = jiffies;
850     	status = init_restart_lance(lp);
851     	return status;
852     }
853     
854     static void lance_tx_timeout(struct net_device *dev)
855     {
856     	struct lance_private *lp = (struct lance_private *) dev->priv;
857     	volatile struct lance_regs *ll = lp->ll;
858     
859     	printk(KERN_ERR "%s: transmit timed out, status %04x, reset\n",
860     			       dev->name, ll->rdp);
861     			lance_reset(dev);
862     	netif_wake_queue(dev);
863     }
864     
865     static int lance_start_xmit(struct sk_buff *skb, struct net_device *dev)
866     {
867     	struct lance_private *lp = (struct lance_private *) dev->priv;
868     	volatile struct lance_regs *ll = lp->ll;
869     	volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
870     	int entry, skblen, len;
871     
872     	skblen = skb->len;
873     
874     	len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
875     
876     	lp->stats.tx_bytes += len;
877     
878     	entry = lp->tx_new & TX_RING_MOD_MASK;
879     	ib->btx_ring[entry].length = (-len);
880     	ib->btx_ring[entry].misc = 0;
881     
882     	cp_to_buf((char *) lp->tx_buf_ptr_cpu[entry], skb->data, skblen);
883     
884     	/* Clear the slack of the packet, do I need this? */
885     	/* For a firewall its a good idea - AC */
886     /*
887        if (len != skblen)
888        memset ((char *) &ib->tx_buf [entry][skblen], 0, (len - skblen) << 1);
889      */
890     
891     	/* Now, give the packet to the lance */
892     	ib->btx_ring[entry].tmd1_bits = (LE_T1_POK | LE_T1_OWN);
893     	lp->tx_new = (lp->tx_new + 1) & TX_RING_MOD_MASK;
894     
895     	if (TX_BUFFS_AVAIL <= 0)
896     		netif_stop_queue(dev);
897     
898     	/* Kick the lance: transmit now */
899     	writereg(&ll->rdp, LE_C0_INEA | LE_C0_TDMD);
900     
901     	spin_unlock_irq(&lp->lock);
902     
903     	dev->trans_start = jiffies;
904     	dev_kfree_skb(skb);
905     
906      	return 0;
907     }
908     
909     static struct net_device_stats *lance_get_stats(struct net_device *dev)
910     {
911     	struct lance_private *lp = (struct lance_private *) dev->priv;
912     
913     	return &lp->stats;
914     }
915     
916     static void lance_load_multicast(struct net_device *dev)
917     {
918     	volatile struct lance_init_block *ib = (struct lance_init_block *) (dev->mem_start);
919     	volatile u16 *mcast_table = (u16 *) & ib->filter;
920     	struct dev_mc_list *dmi = dev->mc_list;
921     	char *addrs;
922     	int i, j, bit, byte;
923     	u32 crc, poly = CRC_POLYNOMIAL_BE;
924     
925     	/* set all multicast bits */
926     	if (dev->flags & IFF_ALLMULTI) {
927     		ib->filter[0] = 0xffff;
928     		ib->filter[2] = 0xffff;
929     		ib->filter[4] = 0xffff;
930     		ib->filter[6] = 0xffff;
931     		return;
932     	}
933     	/* clear the multicast filter */
934     	ib->filter[0] = 0;
935     	ib->filter[2] = 0;
936     	ib->filter[4] = 0;
937     	ib->filter[6] = 0;
938     
939     	/* Add addresses */
940     	for (i = 0; i < dev->mc_count; i++) {
941     		addrs = dmi->dmi_addr;
942     		dmi = dmi->next;
943     
944     		/* multicast address? */
945     		if (!(*addrs & 1))
946     			continue;
947     
948     		crc = 0xffffffff;
949     		for (byte = 0; byte < 6; byte++)
950     			for (bit = *addrs++, j = 0; j < 8; j++, bit >>= 1) {
951     				int test;
952     
953     				test = ((bit ^ crc) & 0x01);
954     				crc >>= 1;
955     
956     				if (test) {
957     					crc = crc ^ poly;
958     				}
959     			}
960     
961     		crc = crc >> 26;
962     		mcast_table[crc >> 3] |= 1 << (crc & 0xf);
963     	}
964     	return;
965     }
966     
967     static void lance_set_multicast(struct net_device *dev)
968     {
969     	struct lance_private *lp = (struct lance_private *) dev->priv;
970     	volatile struct lance_init_block *ib;
971     	volatile struct lance_regs *ll = lp->ll;
972     
973     	ib = (struct lance_init_block *) (dev->mem_start);
974     
975     	if (!netif_running(dev))
976     		return;
977     
978     	if (lp->tx_old != lp->tx_new) {
979     		mod_timer(&lp->multicast_timer, jiffies + 4);
980     		netif_wake_queue(dev);
981     		return;
982     	}
983     
984     	netif_stop_queue(dev);
985     
986     	writereg(&ll->rap, LE_CSR0);
987     	writereg(&ll->rdp, LE_C0_STOP);
988     
989     	lance_init_ring(dev);
990     
991     	if (dev->flags & IFF_PROMISC) {
992     		ib->mode |= LE_MO_PROM;
993     	} else {
994     		ib->mode &= ~LE_MO_PROM;
995     		lance_load_multicast(dev);
996     	}
997     	load_csrs(lp);
998     	init_restart_lance(lp);
999     	netif_wake_queue(dev);
1000     }
1001     
1002     static void lance_set_multicast_retry(unsigned long _opaque)
1003     {
1004     	struct net_device *dev = (struct net_device *) _opaque;
1005     
1006     	lance_set_multicast(dev);
1007     }
1008     
1009     static int __init dec_lance_init(struct net_device *dev, const int type)
1010     {
1011     	static unsigned version_printed;
1012     	struct net_device *dev;
1013     	struct lance_private *lp;
1014     	volatile struct lance_regs *ll;
1015     	int i, ret;
1016     	unsigned long esar_base;
1017     	unsigned char *esar;
1018     
1019     #ifndef CONFIG_TC
1020     	system_base = KN01_LANCE_BASE;
1021     #else
1022     	int slot;
1023     #endif
1024     
1025     	if (dec_lance_debug && version_printed++ == 0)
1026     		printk(version);
1027     
1028     	dev = init_etherdev(0, sizeof(struct lance_private));
1029     	if (!dev)
1030     		return -ENOMEM;
1031     
1032     	/* init_etherdev ensures the data structures used by the LANCE are aligned. */
1033     	lp = (struct lance_private *) dev->priv;
1034     	spin_lock_init(&lp->lock);
1035     
1036     	switch (type) {
1037     #ifdef CONFIG_TC
1038     	case ASIC_LANCE:
1039     		dev->base_addr = system_base + LANCE;
1040     
1041     		/* buffer space for the on-board LANCE shared memory */
1042     		/*
1043     		 * FIXME: ugly hack!
1044     		 */
1045     		dev->mem_start = KSEG1ADDR(0x00020000);
1046     		dev->mem_end = dev->mem_start + 0x00020000;
1047     		dev->irq = ETHER;
1048     		esar_base = system_base + ESAR;
1049     	
1050     		/* Workaround crash with booting KN04 2.1k from Disk */
1051     		memset(dev->mem_start, 0, dev->mem_end - dev->mem_start);
1052     
1053     		/*
1054     		 * setup the pointer arrays, this sucks [tm] :-(
1055     		 */
1056     		for (i = 0; i < RX_RING_SIZE; i++) {
1057     			lp->rx_buf_ptr_cpu[i] = (char *) (dev->mem_start + BUF_OFFSET_CPU
1058     						 + 2 * i * RX_BUFF_SIZE);
1059     			lp->rx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
1060     						     + i * RX_BUFF_SIZE);
1061     		}
1062     		for (i = 0; i < TX_RING_SIZE; i++) {
1063     			lp->tx_buf_ptr_cpu[i] = (char *) (dev->mem_start + BUF_OFFSET_CPU
1064     					+ 2 * RX_RING_SIZE * RX_BUFF_SIZE
1065     						 + 2 * i * TX_BUFF_SIZE);
1066     			lp->tx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
1067     					    + RX_RING_SIZE * RX_BUFF_SIZE
1068     						     + i * TX_BUFF_SIZE);
1069     		}
1070     
1071     		/*
1072     		 * setup and enable IOASIC LANCE DMA
1073     		 */
1074     		lp->dma_ptr_reg = (unsigned long *) (system_base + IOCTL + LANCE_DMA_P);
1075     		*(lp->dma_ptr_reg) = PHYSADDR(dev->mem_start) << 3;
1076     		*(unsigned long *) (system_base + IOCTL + SSR) |= (1 << 16);
1077     		wbflush();
1078     
1079     		break;
1080     	case PMAD_LANCE:
1081     		slot = search_tc_card("PMAD-AA");
1082     		claim_tc_card(slot);
1083     
1084     		dev->mem_start = get_tc_base_addr(slot);
1085     		dev->base_addr = dev->mem_start + 0x100000;
1086     		dev->irq = get_tc_irq_nr(slot);
1087     		esar_base = dev->mem_start + 0x1c0002;
1088     		break;
1089     #endif
1090     	case PMAX_LANCE:
1091     		dev->irq = ETHER;
1092     		dev->base_addr = KN01_LANCE_BASE;
1093     		dev->mem_start = KN01_LANCE_BASE + 0x01000000;
1094     		esar_base = KN01_RTC_BASE + 1;
1095     		/*
1096     		 * setup the pointer arrays, this sucks [tm] :-(
1097     		 */
1098     		for (i = 0; i < RX_RING_SIZE; i++) {
1099     			lp->rx_buf_ptr_cpu[i] =
1100     			    (char *) (dev->mem_start + BUF_OFFSET_CPU
1101     				      + 2 * i * RX_BUFF_SIZE);
1102     
1103     			lp->rx_buf_ptr_lnc[i] =
1104     			    (char *) (BUF_OFFSET_LNC
1105     				      + i * RX_BUFF_SIZE);
1106     
1107     		}
1108     		for (i = 0; i < TX_RING_SIZE; i++) {
1109     			lp->tx_buf_ptr_cpu[i] =
1110     			    (char *) (dev->mem_start + BUF_OFFSET_CPU
1111     				      + 2 * RX_RING_SIZE * RX_BUFF_SIZE
1112     				      + 2 * i * TX_BUFF_SIZE);
1113     			lp->tx_buf_ptr_lnc[i] = (char *) (BUF_OFFSET_LNC
1114     					    + RX_RING_SIZE * RX_BUFF_SIZE
1115     						     + i * TX_BUFF_SIZE);
1116     
1117     		}
1118     		break;
1119     	default:
1120     		printk("declance_init called with unknown type\n");
1121     		ret = -ENODEV;
1122     		goto err_out;
1123     	}
1124     
1125     	ll = (struct lance_regs *) dev->base_addr;
1126     	esar = (unsigned char *) esar_base;
1127     
1128     	/* prom checks */
1129     	/* First, check for test pattern */
1130     	if (esar[0x60] != 0xff && esar[0x64] != 0x00 &&
1131     	    esar[0x68] != 0x55 && esar[0x6c] != 0xaa) {
1132     		printk("Ethernet station address prom not found!\n");
1133     		ret = -ENODEV;
1134     		goto err_out;
1135     	}
1136     	/* Check the prom contents */
1137     	for (i = 0; i < 8; i++) {
1138     		if (esar[i * 4] != esar[0x3c - i * 4] &&
1139     		    esar[i * 4] != esar[0x40 + i * 4] &&
1140     		    esar[0x3c - i * 4] != esar[0x40 + i * 4]) {
1141     			printk("Something is wrong with the ethernet "
1142     			       "station address prom!\n");
1143     			ret = -ENODEV;
1144     			goto err_out;
1145     		}
1146     	}
1147     
1148     	/* Copy the ethernet address to the device structure, later to the
1149     	 * lance initialization block so the lance gets it every time it's
1150     	 * (re)initialized.
1151     	 */
1152     	switch (type) {
1153     	case ASIC_LANCE:
1154     		printk("%s: IOASIC onboard LANCE, addr = ", dev->name);
1155     		break;
1156     	case PMAD_LANCE:
1157     		printk("%s: PMAD-AA, addr = ", dev->name);
1158     		break;
1159     	case PMAX_LANCE:
1160     		printk("%s: PMAX onboard LANCE, addr = ", dev->name);
1161     		break;
1162     	}
1163     	for (i = 0; i < 6; i++) {
1164     		dev->dev_addr[i] = esar[i * 4];
1165     		printk("%2.2x%c", dev->dev_addr[i], i == 5 ? ',' : ':');
1166     	}
1167     
1168     	printk(" irq = %d\n", dev->irq);
1169     
1170     	lp->dev = dev;
1171     	dev->open = &lance_open;
1172     	dev->stop = &lance_close;
1173     	dev->hard_start_xmit = &lance_start_xmit;
1174     	dev->tx_timeout = &lance_tx_timeout;
1175     	dev->watchdog_timeo = 5*HZ;
1176     	dev->get_stats = &lance_get_stats;
1177     	dev->set_multicast_list = &lance_set_multicast;
1178     
1179     	/* lp->ll is the location of the registers for lance card */
1180     	lp->ll = ll;
1181     
1182     	lp->name = lancestr;
1183     
1184     	/* busmaster_regval (CSR3) should be zero according to the PMAD-AA
1185     	 * specification.
1186     	 */
1187     	lp->busmaster_regval = 0;
1188     
1189     	dev->dma = 0;
1190     
1191     	ether_setup(dev);
1192     
1193     	/* We cannot sleep if the chip is busy during a
1194     	 * multicast list update event, because such events
1195     	 * can occur from interrupts (ex. IPv6).  So we
1196     	 * use a timer to try again later when necessary. -DaveM
1197     	 */
1198     	init_timer(&lp->multicast_timer);
1199     	lp->multicast_timer.data = (unsigned long) dev;
1200     	lp->multicast_timer.function = &lance_set_multicast_retry;
1201     
1202     #ifdef MODULE
1203     	dev->ifindex = dev_new_index();
1204     	lp->next_module = root_lance_dev;
1205     	root_lance_dev = lp;
1206     #endif
1207     	return 0;
1208     
1209     err_out:
1210     	unregister_netdev(dev);
1211     	kfree(dev);
1212     	return ret;
1213     }
1214     
1215     
1216     /* Find all the lance cards on the system and initialize them */
1217     static int __init dec_lance_probe(void)
1218     {
1219     	struct net_device *dev = NULL;
1220     	static int called;
1221     
1222     #ifdef MODULE
1223     	root_lance_dev = NULL;
1224     #endif
1225     
1226     
1227     #ifdef CONFIG_TC
1228     	int slot = -1;
1229     
1230     	if (TURBOCHANNEL) {
1231     		if (IOASIC && !called) {
1232     			called = 1;
1233     			type = ASIC_LANCE;
1234     		} else {
1235     			if ((slot = search_tc_card("PMAD-AA")) >= 0) {
1236     				type = PMAD_LANCE;
1237     			} else {
1238     				return -ENODEV;
1239     			}
1240     		}
1241     	} else {
1242     		if (!called) {
1243     			called = 1;
1244     			type = PMAX_LANCE;
1245     		} else {
1246     			return -ENODEV;
1247     		}
1248     	}
1249     #else
1250     	if (!called && !TURBOCHANNEL) {
1251     		called = 1;
1252     		type = PMAX_LANCE;
1253     	} else {
1254     		return -ENODEV;
1255     	}
1256     #endif
1257     
1258     	return dec_lance_init(dev, type);
1259     }
1260     
1261     static void __exit dec_lance_cleanup(void)
1262     {
1263     #ifdef MODULE
1264        struct lance_private *lp;
1265     
1266        while (root_lance_dev) {
1267        lp = root_lance_dev->next_module;
1268     
1269        unregister_netdev(root_lance_dev->dev);
1270        kfree(root_lance_dev->dev);
1271        root_lance_dev = lp;
1272        }
1273     #endif /* MODULE */
1274     }
1275     
1276     module_init(dec_lance_probe);
1277     module_exit(dec_lance_cleanup);
1278