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