Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / dev / netif / le / if_le.c
1 /*-
2  * Copyright (c) 1994 Matt Thomas (thomas@lkg.dec.com)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. The name of the author may not be used to endorse or promote products
11  *    derived from this software withough specific prior written permission
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * $FreeBSD: src/sys/i386/isa/if_le.c,v 1.56.2.4 2002/06/05 23:24:10 paul Exp $
25  */
26
27 /*
28  * DEC EtherWORKS 2 Ethernet Controllers
29  * DEC EtherWORKS 3 Ethernet Controllers
30  *
31  * Written by Matt Thomas
32  * BPF support code stolen directly from if_ec.c
33  *
34  *   This driver supports the DEPCA, DE100, DE101, DE200, DE201,
35  *   DE2002, DE203, DE204, DE205, and DE422 cards.
36  */
37
38 #include "le.h"
39 #include "opt_inet.h"
40 #include "opt_ipx.h"
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/mbuf.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/malloc.h>
49
50 #include <net/ethernet.h>
51 #include <net/if.h>
52 #include <net/if_types.h>
53 #include <net/if_dl.h>
54
55 #include <netinet/in.h>
56 #include <netinet/if_ether.h>
57
58
59 #include <machine/clock.h>
60
61 #include <i386/isa/isa_device.h>
62 #include <i386/isa/icu.h>
63
64 #include <vm/vm.h>
65 #include <vm/pmap.h>
66
67 #include <net/bpf.h>
68
69 /* Forward declarations */
70 typedef struct le_softc le_softc_t;
71 typedef struct le_board le_board_t;
72
73 typedef u_short le_mcbits_t;
74 #define LE_MC_NBPW_LOG2         4
75 #define LE_MC_NBPW              (1 << LE_MC_NBPW_LOG2)
76 \f
77 #if !defined(LE_NOLEMAC)
78 /*
79  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80  *
81  * Start of DEC EtherWORKS III (LEMAC) dependent structures
82  *
83  */
84 #include <i386/isa/ic/lemac.h>          /* Include LEMAC definitions */
85
86 static int lemac_probe(le_softc_t *sc, const le_board_t *bd, int *msize);
87
88 struct le_lemac_info {
89     u_int lemac__lastpage;              /* last 2K page */
90     u_int lemac__memmode;               /* Are we in 2K, 32K, or 64K mode */
91     u_int lemac__membase;               /* Physical address of start of RAM */
92     u_int lemac__txctl;                 /* Transmit Control Byte */
93     u_int lemac__txmax;                 /* Maximum # of outstanding transmits */
94     le_mcbits_t lemac__mctbl[LEMAC_MCTBL_SIZE/sizeof(le_mcbits_t)];
95                                         /* local copy of multicast table */
96     u_char lemac__eeprom[LEMAC_EEP_SIZE]; /* local copy eeprom */
97     char lemac__prodname[LEMAC_EEP_PRDNMSZ+1]; /* prodname name */
98 #define lemac_lastpage          le_un.un_lemac.lemac__lastpage
99 #define lemac_memmode           le_un.un_lemac.lemac__memmode
100 #define lemac_membase           le_un.un_lemac.lemac__membase
101 #define lemac_txctl             le_un.un_lemac.lemac__txctl
102 #define lemac_txmax             le_un.un_lemac.lemac__txmax
103 #define lemac_mctbl             le_un.un_lemac.lemac__mctbl
104 #define lemac_eeprom            le_un.un_lemac.lemac__eeprom
105 #define lemac_prodname          le_un.un_lemac.lemac__prodname
106 };
107 #endif /* !defined(LE_NOLEMAC) */
108 \f
109 #if !defined(LE_NOLANCE)
110 /*
111  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
112  *
113  * Start of DEC EtherWORKS II (LANCE) dependent structures
114  *
115  */
116
117 #include <i386/isa/ic/am7990.h>
118
119 #ifndef LN_DOSTATS
120 #define LN_DOSTATS      1
121 #endif
122
123 static int depca_probe(le_softc_t *sc, const le_board_t *bd, int *msize);
124
125 typedef struct lance_descinfo lance_descinfo_t;
126 typedef struct lance_ring lance_ring_t;
127
128 typedef unsigned lance_addr_t;
129
130 struct lance_descinfo {
131     caddr_t di_addr;                    /* address of descriptor */
132     lance_addr_t di_bufaddr;            /* LANCE address of buffer owned by descriptor */
133     unsigned di_buflen;                 /* size of buffer owned by descriptor */
134     struct mbuf *di_mbuf;               /* mbuf being transmitted/received */
135 };
136
137 struct lance_ring {
138     lance_descinfo_t *ri_first;         /* Pointer to first descriptor in ring */
139     lance_descinfo_t *ri_last;          /* Pointer to last + 1 descriptor in ring */
140     lance_descinfo_t *ri_nextin;        /* Pointer to next one to be given to HOST */
141     lance_descinfo_t *ri_nextout;       /* Pointer to next one to be given to LANCE */
142     unsigned ri_max;                    /* Size of Ring - 1 */
143     unsigned ri_free;                   /* Number of free rings entires (owned by HOST) */
144     lance_addr_t ri_heap;                       /* Start of RAM for this ring */
145     lance_addr_t ri_heapend;            /* End + 1 of RAM for this ring */
146     lance_addr_t ri_outptr;                     /* Pointer to first output byte */
147     unsigned ri_outsize;                /* Space remaining for output */
148 };
149
150 struct le_lance_info {
151     unsigned lance__csr1;               /* LANCE Address of init block (low 16) */
152     unsigned lance__csr2;               /* LANCE Address of init block (high 8) */
153     unsigned lance__csr3;               /* Copy of CSR3 */
154     unsigned lance__rap;                /* IO Port Offset of RAP */
155     unsigned lance__rdp;                /* IO Port Offset of RDP */
156     unsigned lance__ramoffset;          /* Offset to valid LANCE RAM */
157     unsigned lance__ramsize;            /* Amount of RAM shared by LANCE */
158     unsigned lance__rxbufsize;          /* Size of a receive buffer */
159     ln_initb_t lance__initb;            /* local copy of LANCE initblock */
160     ln_initb_t *lance__raminitb;        /* copy to board's LANCE initblock (debugging) */
161     ln_desc_t *lance__ramdesc;          /* copy to board's LANCE descriptors (debugging) */
162     lance_ring_t lance__rxinfo;         /* Receive ring information */
163     lance_ring_t lance__txinfo;         /* Transmit ring information */
164 #define lance_csr1              le_un.un_lance.lance__csr1
165 #define lance_csr2              le_un.un_lance.lance__csr2
166 #define lance_csr3              le_un.un_lance.lance__csr3
167 #define lance_rap               le_un.un_lance.lance__rap
168 #define lance_rdp               le_un.un_lance.lance__rdp
169 #define lance_ramoffset         le_un.un_lance.lance__ramoffset
170 #define lance_ramsize           le_un.un_lance.lance__ramsize
171 #define lance_rxbufsize         le_un.un_lance.lance__rxbufsize
172 #define lance_initb             le_un.un_lance.lance__initb
173 #define lance_raminitb          le_un.un_lance.lance__raminitb
174 #define lance_ramdesc           le_un.un_lance.lance__ramdesc
175 #define lance_rxinfo            le_un.un_lance.lance__rxinfo
176 #define lance_txinfo            le_un.un_lance.lance__txinfo
177 };
178 #endif /* !defined(LE_NOLANCE) */
179 \f
180 /*
181  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
182  *
183  * Start of Common Code
184  *
185  */
186
187 static void (*le_intrvec[NLE])(le_softc_t *sc);
188
189 /*
190  * Ethernet status, per interface.
191  */
192 struct le_softc {
193     struct arpcom le_ac;                /* Common Ethernet/ARP Structure */
194     void (*if_init) __P((void *));/* Interface init routine */
195     void (*if_reset) __P((le_softc_t *));/* Interface reset routine */
196     caddr_t le_membase;                 /* Starting memory address (virtual) */
197     unsigned le_iobase;                 /* Starting I/O base address */
198     unsigned le_irq;                    /* Interrupt Request Value */
199     unsigned le_flags;                  /* local copy of if_flags */
200 #define LE_BRDCSTONLY   0x01000000      /* If only broadcast is enabled */
201     u_int le_mcmask;                    /* bit mask for CRC-32 for multicast hash */
202     le_mcbits_t *le_mctbl;              /* pointer to multicast table */
203     const char *le_prodname;            /* product name DE20x-xx */
204     u_char le_hwaddr[6];                /* local copy of hwaddr */
205     union {
206 #if !defined(LE_NOLEMAC)
207         struct le_lemac_info un_lemac;  /* LEMAC specific information */
208 #endif
209 #if !defined(LE_NOLANCE)
210         struct le_lance_info un_lance;  /* Am7990 specific information */
211 #endif
212     } le_un;
213 };
214 #define le_if           le_ac.ac_if
215
216
217 static int le_probe(struct isa_device *dvp);
218 static int le_attach(struct isa_device *dvp);
219 static ointhand2_t le_intr;
220 static int le_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
221 static void le_input(le_softc_t *sc, caddr_t seg1, size_t total_len,
222                      size_t len2, caddr_t seg2);
223 static void le_multi_filter(le_softc_t *sc);
224 static void le_multi_op(le_softc_t *sc, const u_char *mca, int oper_flg);
225 static int le_read_macaddr(le_softc_t *sc, int ioreg, int skippat);
226
227 #define LE_CRC32_POLY           0xEDB88320UL    /* CRC-32 Poly -- Little Endian */
228
229 struct le_board {
230     int (*bd_probe)(le_softc_t *sc, const le_board_t *bd, int *msize);
231 };
232
233
234 static le_softc_t le_softc[NLE];
235
236 static const le_board_t le_boards[] = {
237 #if !defined(LE_NOLEMAC)
238     { lemac_probe },                    /* DE20[345] */
239 #endif
240 #if !defined(LE_NOLANCE)
241     { depca_probe },                    /* DE{20[012],422} */
242 #endif
243     { NULL }                            /* Must Be Last! */
244 };
245
246 /*
247  * This tells the autoconf code how to set us up.
248  */
249 struct isa_driver ledriver = {
250     le_probe, le_attach, "le",
251 };
252
253 static unsigned le_intrs[NLE];
254
255 #define LE_ADDREQUAL(a1, a2) \
256         (((u_short *)a1)[0] == ((u_short *)a2)[0] \
257          || ((u_short *)a1)[1] == ((u_short *)a2)[1] \
258          || ((u_short *)a1)[2] == ((u_short *)a2)[2])
259 #define LE_ADDRBRDCST(a1) \
260         (((u_short *)a1)[0] == 0xFFFFU \
261          || ((u_short *)a1)[1] == 0xFFFFU \
262          || ((u_short *)a1)[2] == 0xFFFFU)
263
264 #define LE_INL(sc, reg) \
265 ({ u_int data; \
266         __asm __volatile("inl %1, %0": "=a" (data): "d" ((u_short)((sc)->le_iobase + (reg)))); \
267         data; })
268
269
270 #define LE_OUTL(sc, reg, data) \
271         ({__asm __volatile("outl %0, %1"::"a" ((u_int)(data)), "d" ((u_short)((sc)->le_iobase + (reg))));})
272
273 #define LE_INW(sc, reg) \
274 ({ u_short data; \
275         __asm __volatile("inw %1, %0": "=a" (data): "d" ((u_short)((sc)->le_iobase + (reg)))); \
276         data; })
277
278
279 #define LE_OUTW(sc, reg, data) \
280         ({__asm __volatile("outw %0, %1"::"a" ((u_short)(data)), "d" ((u_short)((sc)->le_iobase + (reg))));})
281
282 #define LE_INB(sc, reg) \
283 ({ u_char data; \
284         __asm __volatile("inb %1, %0": "=a" (data): "d" ((u_short)((sc)->le_iobase + (reg)))); \
285         data; })
286
287
288 #define LE_OUTB(sc, reg, data) \
289         ({__asm __volatile("outb %0, %1"::"a" ((u_char)(data)), "d" ((u_short)((sc)->le_iobase + (reg))));})
290
291 #define MEMCPY(to, from, len)           bcopy(from, to, len)
292 #define MEMSET(where, what, howmuch)    bzero(where, howmuch)
293 #define MEMCMP(l, r, len)               bcmp(l, r, len)
294 \f
295
296 static int
297 le_probe(
298     struct isa_device *dvp)
299 {
300     le_softc_t *sc = &le_softc[dvp->id_unit];
301     const le_board_t *bd;
302     int iospace;
303
304     if (dvp->id_unit >= NLE) {
305         printf("%s%d not configured -- too many devices\n",
306                ledriver.name, dvp->id_unit);
307         return 0;
308     }
309
310     sc->le_iobase = dvp->id_iobase;
311     sc->le_membase = (u_char *) dvp->id_maddr;
312     sc->le_irq = dvp->id_irq;
313     sc->le_if.if_name = ledriver.name;
314     sc->le_if.if_unit = dvp->id_unit;
315
316     /*
317      * Find and Initialize board..
318      */
319
320     sc->le_flags &= ~(IFF_UP|IFF_ALLMULTI);
321
322     for (bd = le_boards; bd->bd_probe != NULL; bd++) {
323         if ((iospace = (*bd->bd_probe)(sc, bd, &dvp->id_msize)) != 0) {
324             return iospace;
325         }
326     }
327
328     return 0;
329 }
330 \f
331 static int
332 le_attach(
333     struct isa_device *dvp)
334 {
335     le_softc_t *sc = &le_softc[dvp->id_unit];
336     struct ifnet *ifp = &sc->le_if;
337
338     dvp->id_ointr = le_intr;
339     ifp->if_softc = sc;
340     ifp->if_mtu = ETHERMTU;
341     printf("%s%d: %s ethernet address %6D\n",
342            ifp->if_name, ifp->if_unit,
343            sc->le_prodname,
344            sc->le_ac.ac_enaddr, ":");
345
346     ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
347     ifp->if_output = ether_output;
348     ifp->if_ioctl = le_ioctl;
349     ifp->if_type = IFT_ETHER;
350     ifp->if_addrlen = 6;
351     ifp->if_hdrlen = 14;
352     ifp->if_init = sc->if_init;
353
354     ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
355
356     return 1;
357 }
358 \f
359 static void
360 le_intr(
361     int unit)
362 {
363     int s = splimp();
364
365     le_intrs[unit]++;
366     (*le_intrvec[unit])(&le_softc[unit]);
367
368     splx(s);
369 }
370
371 #define LE_XTRA         0
372
373 static void
374 le_input(
375     le_softc_t *sc,
376     caddr_t seg1,
377     size_t total_len,
378     size_t len1,
379     caddr_t seg2)
380 {
381     struct ether_header eh;
382     struct mbuf *m;
383
384     if (total_len - sizeof(eh) > ETHERMTU
385             || total_len - sizeof(eh) < ETHERMIN) {
386         sc->le_if.if_ierrors++;
387         return;
388     }
389     MEMCPY(&eh, seg1, sizeof(eh));
390
391     seg1 += sizeof(eh); total_len -= sizeof(eh); len1 -= sizeof(eh);
392
393     MGETHDR(m, M_DONTWAIT, MT_DATA);
394     if (m == NULL) {
395         sc->le_if.if_ierrors++;
396         return;
397     }
398     m->m_pkthdr.len = total_len;
399     m->m_pkthdr.rcvif = &sc->le_if;
400     if (total_len + LE_XTRA > MHLEN /* >= MINCLSIZE */) {
401         MCLGET(m, M_DONTWAIT);
402         if ((m->m_flags & M_EXT) == 0) {
403             m_free(m);
404             sc->le_if.if_ierrors++;
405             return;
406         }
407     } else if (total_len + LE_XTRA > MHLEN && MINCLSIZE == (MHLEN+MLEN)) {
408         MGET(m->m_next, M_DONTWAIT, MT_DATA);
409         if (m->m_next == NULL) {
410             m_free(m);
411             sc->le_if.if_ierrors++;
412             return;
413         }
414         m->m_next->m_len = total_len - MHLEN - LE_XTRA;
415         len1 = total_len = MHLEN - LE_XTRA;
416         MEMCPY(mtod(m->m_next, caddr_t), &seg1[MHLEN-LE_XTRA], m->m_next->m_len);
417     } else if (total_len + LE_XTRA > MHLEN) {
418         panic("le_input: pkt of unknown length");
419     }
420     m->m_data += LE_XTRA;
421     m->m_len = total_len;
422     MEMCPY(mtod(m, caddr_t), seg1, len1);
423     if (seg2 != NULL)
424         MEMCPY(mtod(m, caddr_t) + len1, seg2, total_len - len1);
425     ether_input(&sc->le_if, &eh, m);
426 }
427 \f
428 static int
429 le_ioctl(
430     struct ifnet *ifp,
431     u_long cmd,
432     caddr_t data)
433 {
434     le_softc_t *sc = ifp->if_softc;
435     int s, error = 0;
436
437     if ((sc->le_flags & IFF_UP) == 0)
438         return EIO;
439
440     s = splimp();
441
442     switch (cmd) {
443         case SIOCSIFADDR:
444         case SIOCGIFADDR:
445         case SIOCSIFMTU:
446                 error = ether_ioctl(ifp, cmd, data);
447                 break;
448
449         case SIOCSIFFLAGS: {
450             sc->if_init(sc);
451             break;
452         }
453
454         case SIOCADDMULTI:
455         case SIOCDELMULTI:
456             /*
457              * Update multicast listeners
458              */
459                 sc->if_init(sc);
460                 error = 0;
461                 break;
462
463         default: {
464             error = EINVAL;
465         }
466     }
467
468     splx(s);
469     return error;
470 }
471 \f
472 /*
473  *  This is the standard method of reading the DEC Address ROMS.
474  *  I don't understand it but it does work.
475  */
476 static int
477 le_read_macaddr(
478     le_softc_t *sc,
479     int ioreg,
480     int skippat)
481 {
482     int cksum, rom_cksum;
483
484     if (!skippat) {
485         int idx, idx2, found, octet;
486         static u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
487         idx2 = found = 0;
488
489         for (idx = 0; idx < 32; idx++) {
490             octet = LE_INB(sc, ioreg);
491
492             if (octet == testpat[idx2]) {
493                 if (++idx2 == sizeof testpat) {
494                     ++found;
495                     break;
496                 }
497             } else {
498                 idx2 = 0;
499             }
500         }
501
502         if (!found)
503             return -1;
504     }
505
506     cksum = 0;
507     sc->le_hwaddr[0] = LE_INB(sc, ioreg);
508     sc->le_hwaddr[1] = LE_INB(sc, ioreg);
509
510     cksum = *(u_short *) &sc->le_hwaddr[0];
511
512     sc->le_hwaddr[2] = LE_INB(sc, ioreg);
513     sc->le_hwaddr[3] = LE_INB(sc, ioreg);
514     cksum *= 2;
515     if (cksum > 65535) cksum -= 65535;
516     cksum += *(u_short *) &sc->le_hwaddr[2];
517     if (cksum > 65535) cksum -= 65535;
518
519     sc->le_hwaddr[4] = LE_INB(sc, ioreg);
520     sc->le_hwaddr[5] = LE_INB(sc, ioreg);
521     cksum *= 2;
522     if (cksum > 65535) cksum -= 65535;
523     cksum += *(u_short *) &sc->le_hwaddr[4];
524     if (cksum >= 65535) cksum -= 65535;
525
526     rom_cksum = LE_INB(sc, ioreg);
527     rom_cksum |= LE_INB(sc, ioreg) << 8;
528
529     if (cksum != rom_cksum)
530         return -1;
531     return 0;
532 }
533 \f
534 static void
535 le_multi_filter(
536     le_softc_t *sc)
537 {
538     struct ifmultiaddr *ifma;
539
540     MEMSET(sc->le_mctbl, 0, (sc->le_mcmask + 1) / 8);
541
542     if (sc->le_if.if_flags & IFF_ALLMULTI) {
543         sc->le_flags |= IFF_MULTICAST|IFF_ALLMULTI;
544         return;
545     }
546     sc->le_flags &= ~IFF_MULTICAST;
547     /* if (interface has had an address assigned) { */
548         le_multi_op(sc, etherbroadcastaddr, TRUE);
549         sc->le_flags |= LE_BRDCSTONLY|IFF_MULTICAST;
550     /* } */
551
552     sc->le_flags |= IFF_MULTICAST;
553
554     for (ifma = sc->le_ac.ac_if.if_multiaddrs.lh_first; ifma;
555          ifma = ifma->ifma_link.le_next) {
556             if (ifma->ifma_addr->sa_family != AF_LINK)
557                     continue;
558
559             le_multi_op(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr), 1);
560             sc->le_flags &= ~LE_BRDCSTONLY;
561     }
562 }
563 \f
564 static void
565 le_multi_op(
566     le_softc_t *sc,
567     const u_char *mca,
568     int enable)
569 {
570     u_int idx, bit, data, crc = 0xFFFFFFFFUL;
571
572 #ifdef __alpha
573     for (data = *(__unaligned u_long *) mca, bit = 0; bit < 48; bit++, data >>=
574 1)
575         crc = (crc >> 1) ^ (((crc ^ data) & 1) ? LE_CRC32_POLY : 0);
576 #else
577     for (idx = 0; idx < 6; idx++)
578         for (data = *mca++, bit = 0; bit < 8; bit++, data >>= 1)
579             crc = (crc >> 1) ^ (((crc ^ data) & 1) ? LE_CRC32_POLY : 0);
580 #endif
581     /*
582      * The following two line convert the N bit index into a longword index
583      * and a longword mask.
584      */
585     crc &= sc->le_mcmask;
586     bit = 1 << (crc & (LE_MC_NBPW -1));
587     idx = crc >> (LE_MC_NBPW_LOG2);
588
589     /*
590      * Set or clear hash filter bit in our table.
591      */
592     if (enable) {
593         sc->le_mctbl[idx] |= bit;               /* Set Bit */
594     } else {
595         sc->le_mctbl[idx] &= ~bit;              /* Clear Bit */
596     }
597 }
598 \f
599 #if !defined(LE_NOLEMAC)
600 /*
601  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
602  *
603  * Start of DEC EtherWORKS III (LEMAC) dependent code
604  *
605  */
606
607 #define LEMAC_INTR_ENABLE(sc) \
608         LE_OUTB(sc, LEMAC_REG_IC, LE_INB(sc, LEMAC_REG_IC) | LEMAC_IC_ALL)
609
610 #define LEMAC_INTR_DISABLE(sc) \
611         LE_OUTB(sc, LEMAC_REG_IC, LE_INB(sc, LEMAC_REG_IC) & ~LEMAC_IC_ALL)
612
613 #define LEMAC_64K_MODE(mbase)   (((mbase) >= 0x0A) && ((mbase) <= 0x0F))
614 #define LEMAC_32K_MODE(mbase)   (((mbase) >= 0x14) && ((mbase) <= 0x1F))
615 #define LEMAC_2K_MODE(mbase)    ( (mbase) >= 0x40)
616
617 static void lemac_init(void *xsc);
618 static void lemac_start(struct ifnet *ifp);
619 static void lemac_reset(le_softc_t *sc);
620 static void lemac_intr(le_softc_t *sc);
621 static void lemac_rne_intr(le_softc_t *sc);
622 static void lemac_tne_intr(le_softc_t *sc);
623 static void lemac_txd_intr(le_softc_t *sc, unsigned cs_value);
624 static void lemac_rxd_intr(le_softc_t *sc, unsigned cs_value);
625 static int  lemac_read_eeprom(le_softc_t *sc);
626 static void lemac_init_adapmem(le_softc_t *sc);
627
628 #define LE_MCBITS_ALL_1S        ((le_mcbits_t)~(le_mcbits_t)0)
629
630 static const le_mcbits_t lemac_allmulti_mctbl[16] =  {
631     LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S,
632     LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S,
633     LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S,
634     LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S, LE_MCBITS_ALL_1S,
635 };
636 /*
637  * An IRQ mapping table.  Less space than switch statement.
638  */
639 static const int lemac_irqs[] = { IRQ5, IRQ10, IRQ11, IRQ15 };
640
641 /*
642  * Some tuning/monitoring variables.
643  */
644 static unsigned lemac_deftxmax = 16;    /* see lemac_max above */
645 static unsigned lemac_txnospc = 0;      /* total # of tranmit starvations */
646
647 static unsigned lemac_tne_intrs = 0;    /* total # of tranmit done intrs */
648 static unsigned lemac_rne_intrs = 0;    /* total # of receive done intrs */
649 static unsigned lemac_txd_intrs = 0;    /* total # of tranmit error intrs */
650 static unsigned lemac_rxd_intrs = 0;    /* total # of receive error intrs */
651 \f
652
653 static int
654 lemac_probe(
655     le_softc_t *sc,
656     const le_board_t *bd,
657     int *msize)
658 {
659     int irq, portval;
660
661     LE_OUTB(sc, LEMAC_REG_IOP, LEMAC_IOP_EEINIT);
662     DELAY(LEMAC_EEP_DELAY);
663
664     /*
665      *  Read Ethernet address if card is present.
666      */
667     if (le_read_macaddr(sc, LEMAC_REG_APD, 0) < 0)
668         return 0;
669
670     MEMCPY(sc->le_ac.ac_enaddr, sc->le_hwaddr, 6);
671     /*
672      *  Clear interrupts and set IRQ.
673      */
674
675     portval = LE_INB(sc, LEMAC_REG_IC) & LEMAC_IC_IRQMSK;
676     irq = lemac_irqs[portval >> 5];
677     LE_OUTB(sc, LEMAC_REG_IC, portval);
678
679     /*
680      *  Make sure settings match.
681      */
682
683     if (irq != sc->le_irq) {
684         printf("%s%d: lemac configuration error: expected IRQ 0x%x actual 0x%x\n",
685                sc->le_if.if_name, sc->le_if.if_unit, sc->le_irq, irq);
686         return 0;
687     }
688
689     /*
690      * Try to reset the unit
691      */
692     sc->if_init = lemac_init;
693     sc->le_if.if_start = lemac_start;
694     sc->if_reset = lemac_reset;
695     sc->lemac_memmode = 2;
696     sc->if_reset(sc);
697     if ((sc->le_flags & IFF_UP) == 0)
698         return 0;
699
700     /*
701      *  Check for correct memory base configuration.
702      */
703     if (vtophys(sc->le_membase) != sc->lemac_membase) {
704         printf("%s%d: lemac configuration error: expected iomem 0x%x actual 0x%x\n",
705                sc->le_if.if_name, sc->le_if.if_unit,
706                vtophys(sc->le_membase), sc->lemac_membase);
707         return 0;
708     }
709
710     sc->le_prodname = sc->lemac_prodname;
711     sc->le_mctbl = sc->lemac_mctbl;
712     sc->le_mcmask = (1 << LEMAC_MCTBL_BITS) - 1;
713     sc->lemac_txmax = lemac_deftxmax;
714     *msize = 2048;
715     le_intrvec[sc->le_if.if_unit] = lemac_intr;
716
717     return LEMAC_IOSPACE;
718 }
719 \f
720 /*
721  * Do a hard reset of the board;
722  */
723 static void
724 lemac_reset(
725     le_softc_t *sc)
726 {
727     int portval, cksum;
728
729     /*
730      * Initialize board..
731      */
732
733     sc->le_flags &= IFF_UP;
734     sc->le_if.if_flags &= ~IFF_OACTIVE;
735     LEMAC_INTR_DISABLE(sc);
736
737     LE_OUTB(sc, LEMAC_REG_IOP, LEMAC_IOP_EEINIT);
738     DELAY(LEMAC_EEP_DELAY);
739
740     /* Disable Interrupts */
741     /* LE_OUTB(sc, LEMAC_REG_IC, LE_INB(sc, LEMAC_REG_IC) & ICR_IRQ_SEL); */
742
743     /*
744      * Read EEPROM information.  NOTE - the placement of this function
745      * is important because functions hereafter may rely on information
746      * read from the EEPROM.
747      */
748     if ((cksum = lemac_read_eeprom(sc)) != LEMAC_EEP_CKSUM) {
749         printf("%s%d: reset: EEPROM checksum failed (0x%x)\n",
750                sc->le_if.if_name, sc->le_if.if_unit, cksum);
751         return;
752     }
753
754     /*
755      *  Force to 2K mode if not already configured.
756      */
757
758     portval = LE_INB(sc, LEMAC_REG_MBR);
759     if (!LEMAC_2K_MODE(portval)) {
760         if (LEMAC_64K_MODE(portval)) {
761             portval = (((portval * 2) & 0xF) << 4);
762             sc->lemac_memmode = 64;
763         } else if (LEMAC_32K_MODE(portval)) {
764             portval = ((portval & 0xF) << 4);
765             sc->lemac_memmode = 32;
766         }
767         LE_OUTB(sc, LEMAC_REG_MBR, portval);
768     }
769     sc->lemac_membase = portval * (2 * 1024) + (512 * 1024);
770
771     /*
772      *  Initialize Free Memory Queue, Init mcast table with broadcast.
773      */
774
775     lemac_init_adapmem(sc);
776     sc->le_flags |= IFF_UP;
777     return;
778 }
779 \f
780 static void
781 lemac_init(
782     void *xsc)
783 {
784     le_softc_t *sc = (le_softc_t *)xsc;
785     int s;
786
787     if ((sc->le_flags & IFF_UP) == 0)
788         return;
789
790     s = splimp();
791
792     /*
793      * If the interface has the up flag
794      */
795     if (sc->le_if.if_flags & IFF_UP) {
796         int saved_cs = LE_INB(sc, LEMAC_REG_CS);
797         LE_OUTB(sc, LEMAC_REG_CS, saved_cs | (LEMAC_CS_TXD | LEMAC_CS_RXD));
798         LE_OUTB(sc, LEMAC_REG_PA0, sc->le_ac.ac_enaddr[0]);
799         LE_OUTB(sc, LEMAC_REG_PA1, sc->le_ac.ac_enaddr[1]);
800         LE_OUTB(sc, LEMAC_REG_PA2, sc->le_ac.ac_enaddr[2]);
801         LE_OUTB(sc, LEMAC_REG_PA3, sc->le_ac.ac_enaddr[3]);
802         LE_OUTB(sc, LEMAC_REG_PA4, sc->le_ac.ac_enaddr[4]);
803         LE_OUTB(sc, LEMAC_REG_PA5, sc->le_ac.ac_enaddr[5]);
804
805         LE_OUTB(sc, LEMAC_REG_IC, LE_INB(sc, LEMAC_REG_IC) | LEMAC_IC_IE);
806
807         if (sc->le_if.if_flags & IFF_PROMISC) {
808             LE_OUTB(sc, LEMAC_REG_CS, LEMAC_CS_MCE | LEMAC_CS_PME);
809         } else {
810             LEMAC_INTR_DISABLE(sc);
811             le_multi_filter(sc);
812             LE_OUTB(sc, LEMAC_REG_MPN, 0);
813             if ((sc->le_flags | sc->le_if.if_flags) & IFF_ALLMULTI) {
814                 MEMCPY(&sc->le_membase[LEMAC_MCTBL_OFF], lemac_allmulti_mctbl, sizeof(lemac_allmulti_mctbl));
815             } else {
816                 MEMCPY(&sc->le_membase[LEMAC_MCTBL_OFF], sc->lemac_mctbl, sizeof(sc->lemac_mctbl));
817             }
818             LE_OUTB(sc, LEMAC_REG_CS, LEMAC_CS_MCE);
819         }
820
821         LE_OUTB(sc, LEMAC_REG_CTL, LE_INB(sc, LEMAC_REG_CTL) ^ LEMAC_CTL_LED);
822
823         LEMAC_INTR_ENABLE(sc);
824         sc->le_if.if_flags |= IFF_RUNNING;
825     } else {
826         LE_OUTB(sc, LEMAC_REG_CS, LEMAC_CS_RXD|LEMAC_CS_TXD);
827
828         LEMAC_INTR_DISABLE(sc);
829         sc->le_if.if_flags &= ~IFF_RUNNING;
830     }
831     splx(s);
832 }
833 \f
834 /*
835  * What to do upon receipt of an interrupt.
836  */
837 static void
838 lemac_intr(
839     le_softc_t *sc)
840 {
841     int cs_value;
842
843     LEMAC_INTR_DISABLE(sc);     /* Mask interrupts */
844
845     /*
846      * Determine cause of interrupt.  Receive events take
847      * priority over Transmit.
848      */
849
850     cs_value = LE_INB(sc, LEMAC_REG_CS);
851
852     /*
853      * Check for Receive Queue not being empty.
854      * Check for Transmit Done Queue not being empty.
855      */
856
857     if (cs_value & LEMAC_CS_RNE)
858         lemac_rne_intr(sc);
859     if (cs_value & LEMAC_CS_TNE)
860         lemac_tne_intr(sc);
861
862     /*
863      * Check for Transmitter Disabled.
864      * Check for Receiver Disabled.
865      */
866
867     if (cs_value & LEMAC_CS_TXD)
868         lemac_txd_intr(sc, cs_value);
869     if (cs_value & LEMAC_CS_RXD)
870         lemac_rxd_intr(sc, cs_value);
871
872     /*
873      * Toggle LED and unmask interrupts.
874      */
875
876     LE_OUTB(sc, LEMAC_REG_CTL, LE_INB(sc, LEMAC_REG_CTL) ^ LEMAC_CTL_LED);
877     LEMAC_INTR_ENABLE(sc);              /* Unmask interrupts */
878 }
879 \f
880 static void
881 lemac_rne_intr(
882     le_softc_t *sc)
883 {
884     int rxcount, rxlen, rxpg;
885     u_char *rxptr;
886
887     lemac_rne_intrs++;
888     rxcount = LE_INB(sc, LEMAC_REG_RQC);
889     while (rxcount--) {
890         rxpg = LE_INB(sc, LEMAC_REG_RQ);
891         LE_OUTB(sc, LEMAC_REG_MPN, rxpg);
892
893         rxptr = sc->le_membase;
894         sc->le_if.if_ipackets++;
895         if (*rxptr & LEMAC_RX_OK) {
896
897             /*
898              * Get receive length - subtract out checksum.
899              */
900
901             rxlen = ((*(u_int *)rxptr >> 8) & 0x7FF) - 4;
902             le_input(sc, rxptr + sizeof(u_int), rxlen, rxlen, NULL);
903         } else { /* end if (*rxptr & LEMAC_RX_OK) */
904             sc->le_if.if_ierrors++;
905         }
906         LE_OUTB(sc, LEMAC_REG_FMQ, rxpg);  /* Return this page to Free Memory Queue */
907     }  /* end while (recv_count--) */
908
909     return;
910 }
911 \f
912 static void
913 lemac_rxd_intr(
914     le_softc_t *sc,
915     unsigned cs_value)
916 {
917     /*
918      * Handle CS_RXD (Receiver disabled) here.
919      *
920      * Check Free Memory Queue Count. If not equal to zero
921      * then just turn Receiver back on. If it is equal to
922      * zero then check to see if transmitter is disabled.
923      * Process transmit TXD loop once more.  If all else
924      * fails then do software init (0xC0 to EEPROM Init)
925      * and rebuild Free Memory Queue.
926      */
927
928     lemac_rxd_intrs++;
929
930     /*
931      *  Re-enable Receiver.
932      */
933
934     cs_value &= ~LEMAC_CS_RXD;
935     LE_OUTB(sc, LEMAC_REG_CS, cs_value);
936
937     if (LE_INB(sc, LEMAC_REG_FMC) > 0)
938         return;
939
940     if (cs_value & LEMAC_CS_TXD)
941         lemac_txd_intr(sc, cs_value);
942
943     if ((LE_INB(sc, LEMAC_REG_CS) & LEMAC_CS_RXD) == 0)
944         return;
945
946     printf("%s%d: fatal RXD error, attempting recovery\n",
947            sc->le_if.if_name, sc->le_if.if_unit);
948
949     sc->if_reset(sc);
950     if (sc->le_flags & IFF_UP) {
951         lemac_init(sc);
952         return;
953     }
954
955     /*
956      *  Error during initializion.  Mark card as disabled.
957      */
958     printf("%s%d: recovery failed -- board disabled\n",
959            sc->le_if.if_name, sc->le_if.if_unit);
960     return;
961 }
962 \f
963 static void
964 lemac_start(
965     struct ifnet *ifp)
966 {
967     le_softc_t *sc = (le_softc_t *) ifp;
968     struct ifqueue *ifq = &ifp->if_snd;
969
970     if ((ifp->if_flags & IFF_RUNNING) == 0)
971         return;
972
973     LEMAC_INTR_DISABLE(sc);
974
975     while (ifq->ifq_head != NULL) {
976         struct mbuf  *m;
977         int tx_pg;
978         u_int txhdr, txoff;
979
980         if (LE_INB(sc, LEMAC_REG_TQC) >= sc->lemac_txmax) {
981             ifp->if_flags |= IFF_OACTIVE;
982             break;
983         }
984
985         tx_pg = LE_INB(sc, LEMAC_REG_FMQ);      /* get free memory page */
986         /*
987          * Check for good transmit page.
988          */
989         if (tx_pg == 0 || tx_pg > sc->lemac_lastpage) {
990             lemac_txnospc++;
991             ifp->if_flags |= IFF_OACTIVE;
992             break;
993         }
994
995         IF_DEQUEUE(ifq, m);
996         LE_OUTB(sc, LEMAC_REG_MPN, tx_pg);      /* Shift 2K window. */
997
998         /*
999          * The first four bytes of each transmit buffer are for
1000          * control information.  The first byte is the control
1001          * byte, then the length (why not word aligned?), then
1002          * the off to the buffer.
1003          */
1004
1005         txoff = (mtod(m, u_int) & (sizeof(u_long) - 1)) + LEMAC_TX_HDRSZ;
1006         txhdr = sc->lemac_txctl | (m->m_pkthdr.len << 8) | (txoff << 24);
1007         *(u_int *) sc->le_membase = txhdr;
1008
1009         /*
1010          * Copy the packet to the board
1011          */
1012
1013         m_copydata(m, 0, m->m_pkthdr.len, sc->le_membase + txoff);
1014
1015         LE_OUTB(sc, LEMAC_REG_TQ, tx_pg);       /* tell chip to transmit this packet */
1016
1017         if (sc->le_if.if_bpf)
1018                 bpf_mtap(&sc->le_if, m);
1019
1020         m_freem(m);                     /* free the mbuf */
1021     }
1022     LEMAC_INTR_ENABLE(sc);
1023 }
1024 \f
1025 static void
1026 lemac_tne_intr(
1027     le_softc_t *sc)
1028 {
1029     int txsts, txcount = LE_INB(sc, LEMAC_REG_TDC);
1030
1031     lemac_tne_intrs++;
1032     while (txcount--) {
1033         txsts = LE_INB(sc, LEMAC_REG_TDQ);
1034         sc->le_if.if_opackets++;                /* another one done */
1035         if ((txsts & LEMAC_TDQ_COL) != LEMAC_TDQ_NOCOL)
1036             sc->le_if.if_collisions++;
1037     }
1038     sc->le_if.if_flags &= ~IFF_OACTIVE;
1039     lemac_start(&sc->le_if);
1040 }
1041
1042 static void
1043 lemac_txd_intr(
1044     le_softc_t *sc,
1045     unsigned cs_value)
1046 {
1047     /*
1048      * Read transmit status, remove transmit buffer from
1049      * transmit queue and place on free memory queue,
1050      * then reset transmitter.
1051      * Increment appropriate counters.
1052      */
1053
1054     lemac_txd_intrs++;
1055     sc->le_if.if_oerrors++;
1056     if (LE_INB(sc, LEMAC_REG_TS) & LEMAC_TS_ECL)
1057         sc->le_if.if_collisions++;
1058     sc->le_if.if_flags &= ~IFF_OACTIVE;
1059
1060     LE_OUTB(sc, LEMAC_REG_FMQ, LE_INB(sc, LEMAC_REG_TQ));
1061                                 /* Get Page number and write it back out */
1062
1063     LE_OUTB(sc, LEMAC_REG_CS, cs_value & ~LEMAC_CS_TXD);
1064                                 /* Turn back on transmitter */
1065     return;
1066 }
1067 \f
1068 static int
1069 lemac_read_eeprom(
1070     le_softc_t *sc)
1071 {
1072     int word_off, cksum;
1073
1074     u_char *ep;
1075
1076     cksum = 0;
1077     ep = sc->lemac_eeprom;
1078     for (word_off = 0; word_off < LEMAC_EEP_SIZE / 2; word_off++) {
1079         LE_OUTB(sc, LEMAC_REG_PI1, word_off);
1080         LE_OUTB(sc, LEMAC_REG_IOP, LEMAC_IOP_EEREAD);
1081
1082         DELAY(LEMAC_EEP_DELAY);
1083
1084         *ep = LE_INB(sc, LEMAC_REG_EE1);        cksum += *ep++;
1085         *ep = LE_INB(sc, LEMAC_REG_EE2);        cksum += *ep++;
1086     }
1087
1088     /*
1089      *  Set up Transmit Control Byte for use later during transmit.
1090      */
1091
1092     sc->lemac_txctl |= LEMAC_TX_FLAGS;
1093
1094     if ((sc->lemac_eeprom[LEMAC_EEP_SWFLAGS] & LEMAC_EEP_SW_SQE) == 0)
1095         sc->lemac_txctl &= ~LEMAC_TX_SQE;
1096
1097     if (sc->lemac_eeprom[LEMAC_EEP_SWFLAGS] & LEMAC_EEP_SW_LAB)
1098         sc->lemac_txctl |= LEMAC_TX_LAB;
1099
1100     MEMCPY(sc->lemac_prodname, &sc->lemac_eeprom[LEMAC_EEP_PRDNM], LEMAC_EEP_PRDNMSZ);
1101     sc->lemac_prodname[LEMAC_EEP_PRDNMSZ] = '\0';
1102
1103     return cksum % 256;
1104 }
1105 \f
1106 static void
1107 lemac_init_adapmem(
1108     le_softc_t *sc)
1109 {
1110     int pg, conf;
1111
1112     conf = LE_INB(sc, LEMAC_REG_CNF);
1113
1114     if ((sc->lemac_eeprom[LEMAC_EEP_SETUP] & LEMAC_EEP_ST_DRAM) == 0) {
1115         sc->lemac_lastpage = 63;
1116         conf &= ~LEMAC_CNF_DRAM;
1117     } else {
1118         sc->lemac_lastpage = 127;
1119         conf |= LEMAC_CNF_DRAM;
1120     }
1121
1122     LE_OUTB(sc, LEMAC_REG_CNF, conf);
1123
1124     for (pg = 1; pg <= sc->lemac_lastpage; pg++)
1125         LE_OUTB(sc, LEMAC_REG_FMQ, pg);
1126
1127     return;
1128 }
1129 #endif /* !defined(LE_NOLEMAC) */
1130 \f
1131 #if !defined(LE_NOLANCE)
1132 /*
1133  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1134  *
1135  * Start of DEPCA (DE200/DE201/DE202/DE422 etal) support.
1136  *
1137  */
1138 static void depca_intr(le_softc_t *sc);
1139 static int  lance_init_adapmem(le_softc_t *sc);
1140 static int  lance_init_ring(le_softc_t *sc, ln_ring_t *rp, lance_ring_t *ri,
1141                             unsigned ndescs, unsigned bufoffset,
1142                             unsigned descoffset);
1143 static void lance_init(void *xsc);
1144 static void lance_reset(le_softc_t *sc);
1145 static void lance_intr(le_softc_t *sc);
1146 static int  lance_rx_intr(le_softc_t *sc);
1147 static void lance_start(struct ifnet *ifp);
1148 static int  lance_tx_intr(le_softc_t *sc);
1149
1150 #define LN_BUFSIZE              /* 380 */ 304   /* 1520 / 4 */
1151 #define LN_TXDESC_RATIO         2048
1152 #define LN_DESC_MAX             128
1153
1154 #if LN_DOSTATS
1155 static struct {
1156     unsigned lance_rx_misses;
1157     unsigned lance_rx_badcrc;
1158     unsigned lance_rx_badalign;
1159     unsigned lance_rx_badframe;
1160     unsigned lance_rx_buferror;
1161     unsigned lance_tx_deferred;
1162     unsigned lance_tx_single_collisions;
1163     unsigned lance_tx_multiple_collisions;
1164     unsigned lance_tx_excessive_collisions;
1165     unsigned lance_tx_late_collisions;
1166
1167     unsigned lance_memory_errors;
1168     unsigned lance_inits;
1169     unsigned lance_tx_intrs;
1170     unsigned lance_tx_nospc[2];
1171     unsigned lance_tx_drains[2];
1172     unsigned lance_tx_orphaned;
1173     unsigned lance_tx_adoptions;
1174     unsigned lance_tx_emptied;
1175     unsigned lance_tx_deftxint;
1176     unsigned lance_tx_buferror;
1177     unsigned lance_high_txoutptr;
1178     unsigned lance_low_txheapsize;
1179     unsigned lance_low_txfree;
1180     unsigned lance_tx_intr_hidescs;
1181     /* unsigned lance_tx_intr_descs[LN_DESC_MAX]; */
1182
1183     unsigned lance_rx_intrs;
1184     unsigned lance_rx_badsop;
1185     unsigned lance_rx_contig;
1186     unsigned lance_rx_noncontig;
1187     unsigned lance_rx_intr_hidescs;
1188     unsigned lance_rx_ndescs[4096 / LN_BUFSIZE];
1189     /* unsigned lance_rx_intr_descs[LN_DESC_MAX]; */
1190 } lance_stats;
1191
1192 #define LN_STAT(stat)   (lance_stats.lance_ ## stat)
1193 #define LN_MINSTAT(stat, val)   (LN_STAT(stat > (val)) ? LN_STAT(stat = (val)) : 0)
1194 #define LN_MAXSTAT(stat, val)   (LN_STAT(stat < (val)) ? LN_STAT(stat = (val)) : 0)
1195
1196 #else
1197 #define LN_STAT(stat)   0
1198 #define LN_MINSTAT(stat, val)   0
1199 #define LN_MAXSTAT(stat, val)   0
1200 #endif
1201
1202 #define LN_SELCSR(sc, csrno)            (LE_OUTW(sc, sc->lance_rap, csrno))
1203 #define LN_INQCSR(sc)                   (LE_INW(sc, sc->lance_rap))
1204
1205 #define LN_WRCSR(sc, val)               (LE_OUTW(sc, sc->lance_rdp, val))
1206 #define LN_RDCSR(sc)                    (LE_INW(sc, sc->lance_rdp))
1207
1208
1209 #define LN_ZERO(sc, vaddr, len)         bzero(vaddr, len)
1210 #define LN_COPYTO(sc, from, to, len)    bcopy(from, to, len)
1211
1212 #define LN_SETFLAG(sc, vaddr, val) \
1213         (((volatile u_char *) vaddr)[3] = (val))
1214
1215 #define LN_PUTDESC(sc, desc, vaddr) \
1216         (((volatile u_short *) vaddr)[0] = ((u_short *) desc)[0], \
1217          ((volatile u_short *) vaddr)[2] = ((u_short *) desc)[2], \
1218          ((volatile u_short *) vaddr)[1] = ((u_short *) desc)[1])
1219
1220 /*
1221  * Only get the descriptor flags and length/status.  All else
1222  * read-only.
1223  */
1224 #define LN_GETDESC(sc, desc, vaddr) \
1225         (((u_short *) desc)[1] = ((volatile u_short *) vaddr)[1], \
1226          ((u_short *) desc)[3] = ((volatile u_short *) vaddr)[3])
1227
1228 \f
1229 /*
1230  *  These definitions are specific to the DEC "DEPCA-style" NICs.
1231  *      (DEPCA, DE10x, DE20[012], DE422)
1232  *
1233  */
1234 #define DEPCA_REG_NICSR         0               /* (RW;16) NI Control / Status */
1235 #define DEPCA_REG_RDP           4               /* (RW:16) LANCE RDP (data) register */
1236 #define DEPCA_REG_RAP           6               /* (RW:16) LANCE RAP (address) register */
1237 #define DEPCA_REG_ADDRROM       12              /* (R : 8) DEPCA Ethernet Address ROM */
1238 #define DEPCA_IOSPACE           16              /* DEPCAs use 16 bytes of IO space */
1239
1240 #define DEPCA_NICSR_LED         0x0001          /* Light the LED on the back of the DEPCA */
1241 #define DEPCA_NICSR_ENABINTR    0x0002          /* Enable Interrupts */
1242 #define DEPCA_NICSR_MASKINTR    0x0004          /* Mask Interrupts */
1243 #define DEPCA_NICSR_AAC         0x0008          /* Address Counter Clear */
1244 #define DEPCA_NICSR_REMOTEBOOT  0x0010          /* Remote Boot Enabled (ignored) */
1245 #define DEPCA_NICSR_32KRAM      0x0020          /* DEPCA LANCE RAM size 64K (C) / 32K (S) */
1246 #define DEPCA_NICSR_LOW32K      0x0040          /* Bank Select (A15 = !This Bit) */
1247 #define DEPCA_NICSR_SHE         0x0080          /* Shared RAM Enabled (ie hide ROM) */
1248 #define DEPCA_NICSR_BOOTTMO     0x0100          /* Remote Boot Timeout (ignored) */
1249
1250 #define DEPCA_RDNICSR(sc)       (LE_INW(sc, DEPCA_REG_NICSR))
1251 #define DEPCA_WRNICSR(sc, val)  (LE_OUTW(sc, DEPCA_REG_NICSR, val))
1252
1253 #define DEPCA_IDSTR_OFFSET      0xC006          /* ID String Offset */
1254
1255 #define DEPCA_REG_EISAID        0x80
1256 #define DEPCA_EISAID_MASK       0xf0ffffff
1257 #define DEPCA_EISAID_DE422      0x2042A310
1258
1259 typedef enum {
1260     DEPCA_CLASSIC,
1261     DEPCA_DE100, DEPCA_DE101,
1262     DEPCA_EE100,
1263     DEPCA_DE200, DEPCA_DE201, DEPCA_DE202,
1264     DEPCA_DE422,
1265     DEPCA_UNKNOWN
1266 } depca_t;
1267
1268 static const char *depca_signatures[] = {
1269     "DEPCA",
1270     "DE100", "DE101",
1271     "EE100",
1272     "DE200", "DE201", "DE202",
1273     "DE422",
1274     NULL
1275 };
1276 \f
1277 static int
1278 depca_probe(
1279     le_softc_t *sc,
1280     const le_board_t *bd,
1281     int *msize)
1282 {
1283     unsigned nicsr, idx, idstr_offset = DEPCA_IDSTR_OFFSET;
1284
1285     /*
1286      *  Find out how memory we are dealing with.  Adjust
1287      *  the ID string offset approriately if we are at
1288      *  32K.  Make sure the ROM is enabled.
1289      */
1290     nicsr = DEPCA_RDNICSR(sc);
1291     nicsr &= ~(DEPCA_NICSR_SHE|DEPCA_NICSR_LED|DEPCA_NICSR_ENABINTR);
1292
1293     if (nicsr & DEPCA_NICSR_32KRAM) {
1294         /*
1295          * Make we are going to read the upper
1296          * 32K so we do read the ROM.
1297          */
1298         sc->lance_ramsize = 32 * 1024;
1299         nicsr &= ~DEPCA_NICSR_LOW32K;
1300         sc->lance_ramoffset = 32 * 1024;
1301         idstr_offset -= sc->lance_ramsize;
1302     } else {
1303         sc->lance_ramsize = 64 * 1024;
1304         sc->lance_ramoffset = 0;
1305     }
1306     DEPCA_WRNICSR(sc, nicsr);
1307
1308     sc->le_prodname = NULL;
1309     for (idx = 0; depca_signatures[idx] != NULL; idx++) {
1310         if (bcmp(depca_signatures[idx], sc->le_membase + idstr_offset, 5) == 0) {
1311             sc->le_prodname = depca_signatures[idx];
1312             break;
1313         }
1314     }
1315
1316     if (sc->le_prodname == NULL) {
1317         /*
1318          * Try to get the EISA device if it's a DE422.
1319          */
1320         if (sc->le_iobase > 0x1000 && (sc->le_iobase & 0x0F00) == 0x0C00
1321             && (LE_INL(sc, DEPCA_REG_EISAID) & DEPCA_EISAID_MASK)
1322              == DEPCA_EISAID_DE422) {
1323             sc->le_prodname = "DE422";
1324         } else {
1325             return 0;
1326         }
1327     }
1328     if (idx == DEPCA_CLASSIC)
1329         sc->lance_ramsize -= 16384;     /* Can't use the ROM area on a DEPCA */
1330
1331     /*
1332      * Try to read the address ROM.
1333      *   Stop the LANCE, reset the Address ROM Counter (AAC),
1334      *   read the NICSR to "clock" in the reset, and then
1335      *   re-enable the Address ROM Counter.  Now read the
1336      *   address ROM.
1337      */
1338     sc->lance_rdp = DEPCA_REG_RDP;
1339     sc->lance_rap = DEPCA_REG_RAP;
1340     sc->lance_csr3 = LN_CSR3_ALE;
1341     sc->le_mctbl = sc->lance_initb.ln_multi_mask;
1342     sc->le_mcmask = LN_MC_MASK;
1343     LN_SELCSR(sc, LN_CSR0);
1344     LN_WRCSR(sc, LN_CSR0_STOP);
1345
1346     if (idx < DEPCA_DE200) {
1347         DEPCA_WRNICSR(sc, DEPCA_RDNICSR(sc) & ~DEPCA_NICSR_AAC);
1348         DEPCA_WRNICSR(sc, DEPCA_RDNICSR(sc) | DEPCA_NICSR_AAC);
1349     }
1350
1351     if (le_read_macaddr(sc, DEPCA_REG_ADDRROM, idx == DEPCA_CLASSIC) < 0)
1352         return 0;
1353
1354     MEMCPY(sc->le_ac.ac_enaddr, sc->le_hwaddr, 6);
1355     /*
1356      * Renable shared RAM.
1357      */
1358     DEPCA_WRNICSR(sc, DEPCA_RDNICSR(sc) | DEPCA_NICSR_SHE);
1359
1360     le_intrvec[sc->le_if.if_unit] = depca_intr;
1361     if (!lance_init_adapmem(sc))
1362         return 0;
1363
1364     sc->if_reset = lance_reset;
1365     sc->if_init = lance_init;
1366     sc->le_if.if_start = lance_start;
1367     DEPCA_WRNICSR(sc, DEPCA_NICSR_SHE | DEPCA_NICSR_ENABINTR);
1368     sc->if_reset(sc);
1369
1370     LN_STAT(low_txfree = sc->lance_txinfo.ri_max);
1371     LN_STAT(low_txheapsize = 0xFFFFFFFF);
1372     *msize = sc->lance_ramsize;
1373     return DEPCA_IOSPACE;
1374 }
1375
1376 static void
1377 depca_intr(
1378     le_softc_t *sc)
1379 {
1380     DEPCA_WRNICSR(sc, DEPCA_RDNICSR(sc) ^ DEPCA_NICSR_LED);
1381     lance_intr(sc);
1382 }
1383 \f
1384 /*
1385  * Here's as good a place to describe our paritioning of the
1386  * LANCE shared RAM space.  (NOTE: this driver does not yet support
1387  * the concept of a LANCE being able to DMA).
1388  *
1389  * First is the 24 (00:23) bytes for LANCE Initialization Block
1390  * Next are the recieve descriptors.  The number is calculated from
1391  * how many LN_BUFSIZE buffers we can allocate (this number must
1392  * be a power of 2).  Next are the transmit descriptors.  The amount
1393  * of transmit descriptors is derived from the size of the RAM
1394  * divided by 1K.  Now come the receive buffers (one for each receive
1395  * descriptor).  Finally is the transmit heap.  (no fixed buffers are
1396  * allocated so as to make the most use of the limited space).
1397  */
1398 static int
1399 lance_init_adapmem(
1400     le_softc_t *sc)
1401 {
1402     lance_addr_t rxbufoffset;
1403     lance_addr_t rxdescoffset, txdescoffset;
1404     unsigned rxdescs, txdescs;
1405
1406     /*
1407      * First calculate how many descriptors we heap.
1408      * Note this assumes the ramsize is a power of two.
1409      */
1410     sc->lance_rxbufsize = LN_BUFSIZE;
1411     rxdescs = 1;
1412     while (rxdescs * sc->lance_rxbufsize < sc->lance_ramsize)
1413         rxdescs *= 2;
1414     rxdescs /= 2;
1415     if (rxdescs > LN_DESC_MAX) {
1416         sc->lance_rxbufsize *= rxdescs / LN_DESC_MAX;
1417         rxdescs = LN_DESC_MAX;
1418     }
1419     txdescs = sc->lance_ramsize / LN_TXDESC_RATIO;
1420     if (txdescs > LN_DESC_MAX)
1421         txdescs = LN_DESC_MAX;
1422
1423     /*
1424      * Now calculate where everything goes in memory
1425      */
1426     rxdescoffset = sizeof(ln_initb_t);
1427     txdescoffset = rxdescoffset + sizeof(ln_desc_t) * rxdescs;
1428     rxbufoffset  = txdescoffset + sizeof(ln_desc_t) * txdescs;
1429
1430     sc->le_mctbl = (le_mcbits_t *) sc->lance_initb.ln_multi_mask;
1431     /*
1432      * Remember these for debugging.
1433      */
1434     sc->lance_raminitb = (ln_initb_t *) sc->le_membase;
1435     sc->lance_ramdesc = (ln_desc_t *) (sc->le_membase + rxdescoffset);
1436
1437     /*
1438      * Initialize the rings.
1439      */
1440     if (!lance_init_ring(sc, &sc->lance_initb.ln_rxring, &sc->lance_rxinfo,
1441                    rxdescs, rxbufoffset, rxdescoffset))
1442         return 0;
1443     sc->lance_rxinfo.ri_heap = rxbufoffset;
1444     sc->lance_rxinfo.ri_heapend = rxbufoffset + sc->lance_rxbufsize * rxdescs;
1445
1446     if (!lance_init_ring(sc, &sc->lance_initb.ln_txring, &sc->lance_txinfo,
1447                    txdescs, 0, txdescoffset))
1448         return 0;
1449     sc->lance_txinfo.ri_heap = sc->lance_rxinfo.ri_heapend;
1450     sc->lance_txinfo.ri_heapend = sc->lance_ramsize;
1451
1452     /*
1453      * Set CSR1 and CSR2 to the address of the init block (which
1454      * for us is always 0.
1455      */
1456     sc->lance_csr1 = LN_ADDR_LO(0 + sc->lance_ramoffset);
1457     sc->lance_csr2 = LN_ADDR_HI(0 + sc->lance_ramoffset);
1458     return 1;
1459 }
1460 \f
1461 static int
1462 lance_init_ring(
1463     le_softc_t *sc,
1464     ln_ring_t *rp,
1465     lance_ring_t *ri,
1466     unsigned ndescs,
1467     lance_addr_t bufoffset,
1468     lance_addr_t descoffset)
1469 {
1470     lance_descinfo_t *di;
1471
1472     /*
1473      * Initialize the ring pointer in the LANCE InitBlock
1474      */
1475     rp->r_addr_lo = LN_ADDR_LO(descoffset + sc->lance_ramoffset);
1476     rp->r_addr_hi = LN_ADDR_HI(descoffset + sc->lance_ramoffset);
1477     rp->r_log2_size = ffs(ndescs) - 1;
1478
1479     /*
1480      * Allocate the ring entry descriptors and initialize
1481      * our ring information data structure.  All these are
1482      * our copies and do not live in the LANCE RAM.
1483      */
1484     ri->ri_first = (lance_descinfo_t *) malloc(ndescs * sizeof(*di), M_DEVBUF, M_NOWAIT);
1485     if (ri->ri_first == NULL) {
1486         printf("lance_init_ring: malloc(%d) failed\n", ndescs * sizeof(*di));
1487         return 0;
1488     }
1489     ri->ri_free = ri->ri_max = ndescs;
1490     ri->ri_last = ri->ri_first + ri->ri_max;
1491     for (di = ri->ri_first; di < ri->ri_last; di++) {
1492         di->di_addr = sc->le_membase + descoffset;
1493         di->di_mbuf = NULL;
1494         if (bufoffset) {
1495             di->di_bufaddr = bufoffset;
1496             di->di_buflen = sc->lance_rxbufsize;
1497             bufoffset += sc->lance_rxbufsize;
1498         }
1499         descoffset += sizeof(ln_desc_t);
1500     }
1501     return 1;
1502 }
1503 \f
1504 static void
1505 lance_dumpcsrs(
1506     le_softc_t *sc,
1507     const char *id)
1508 {
1509     printf("%s%d: %s: nicsr=%04x",
1510            sc->le_if.if_name, sc->le_if.if_unit,
1511            id, DEPCA_RDNICSR(sc));
1512     LN_SELCSR(sc, LN_CSR0); printf(" csr0=%04x", LN_RDCSR(sc));
1513     LN_SELCSR(sc, LN_CSR1); printf(" csr1=%04x", LN_RDCSR(sc));
1514     LN_SELCSR(sc, LN_CSR2); printf(" csr2=%04x", LN_RDCSR(sc));
1515     LN_SELCSR(sc, LN_CSR3); printf(" csr3=%04x\n", LN_RDCSR(sc));
1516     LN_SELCSR(sc, LN_CSR0);
1517 }
1518
1519 static void
1520 lance_reset(
1521     le_softc_t *sc)
1522 {
1523     register int cnt, csr;
1524
1525     /* lance_dumpcsrs(sc, "lance_reset: start"); */
1526
1527     LN_WRCSR(sc, LN_RDCSR(sc) & ~LN_CSR0_ENABINTR);
1528     LN_WRCSR(sc, LN_CSR0_STOP);
1529     DELAY(100);
1530
1531     sc->le_flags &= ~IFF_UP;
1532     sc->le_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1533
1534     le_multi_filter(sc);                /* initialize the multicast table */
1535     if ((sc->le_flags | sc->le_if.if_flags) & IFF_ALLMULTI) {
1536         sc->lance_initb.ln_multi_mask[0] = 0xFFFFU;
1537         sc->lance_initb.ln_multi_mask[1] = 0xFFFFU;
1538         sc->lance_initb.ln_multi_mask[2] = 0xFFFFU;
1539         sc->lance_initb.ln_multi_mask[3] = 0xFFFFU;
1540     }
1541     sc->lance_initb.ln_physaddr[0] = ((u_short *) sc->le_ac.ac_enaddr)[0];
1542     sc->lance_initb.ln_physaddr[1] = ((u_short *) sc->le_ac.ac_enaddr)[1];
1543     sc->lance_initb.ln_physaddr[2] = ((u_short *) sc->le_ac.ac_enaddr)[2];
1544     if (sc->le_if.if_flags & IFF_PROMISC) {
1545         sc->lance_initb.ln_mode |= LN_MODE_PROMISC;
1546     } else {
1547         sc->lance_initb.ln_mode &= ~LN_MODE_PROMISC;
1548     }
1549     /*
1550      * We force the init block to be at the start
1551      * of the LANCE's RAM buffer.
1552      */
1553     LN_COPYTO(sc, &sc->lance_initb, sc->le_membase, sizeof(sc->lance_initb));
1554     LN_SELCSR(sc, LN_CSR1); LN_WRCSR(sc, sc->lance_csr1);
1555     LN_SELCSR(sc, LN_CSR2); LN_WRCSR(sc, sc->lance_csr2);
1556     LN_SELCSR(sc, LN_CSR3); LN_WRCSR(sc, sc->lance_csr3);
1557
1558     /* lance_dumpcsrs(sc, "lance_reset: preinit"); */
1559
1560     /*
1561      * clear INITDONE and INIT the chip
1562      */
1563     LN_SELCSR(sc, LN_CSR0);
1564     LN_WRCSR(sc, LN_CSR0_INIT|LN_CSR0_INITDONE);
1565
1566     csr = 0;
1567     cnt = 100;
1568     while (cnt-- > 0) {
1569         if (((csr = LN_RDCSR(sc)) & LN_CSR0_INITDONE) != 0)
1570             break;
1571         DELAY(10000);
1572     }
1573
1574     if ((csr & LN_CSR0_INITDONE) == 0) {    /* make sure we got out okay */
1575         lance_dumpcsrs(sc, "lance_reset: reset failure");
1576     } else {
1577         /* lance_dumpcsrs(sc, "lance_reset: end"); */
1578         sc->le_if.if_flags |= IFF_UP;
1579         sc->le_flags |= IFF_UP;
1580     }
1581 }
1582 \f
1583 static void
1584 lance_init(
1585     void *xsc)
1586 {
1587     le_softc_t *sc = (le_softc_t *)xsc;
1588     lance_ring_t *ri;
1589     lance_descinfo_t *di;
1590     ln_desc_t desc;
1591
1592     LN_STAT(inits++);
1593     if (sc->le_if.if_flags & IFF_RUNNING) {
1594         sc->if_reset(sc);
1595         lance_tx_intr(sc);
1596         /*
1597          * If we were running, requeue any pending transmits.
1598          */
1599         ri = &sc->lance_txinfo;
1600         di = ri->ri_nextout;
1601         while (ri->ri_free < ri->ri_max) {
1602             if (--di == ri->ri_first)
1603                 di = ri->ri_nextout - 1;
1604             if (di->di_mbuf == NULL)
1605                 break;
1606             IF_PREPEND(&sc->le_if.if_snd, di->di_mbuf);
1607             di->di_mbuf = NULL;
1608             ri->ri_free++;
1609         }
1610     } else {
1611         sc->if_reset(sc);
1612     }
1613
1614     /*
1615      * Reset the transmit ring.  Make sure we own all the buffers.
1616      * Also reset the transmit heap.
1617      */
1618     sc->le_if.if_flags &= ~IFF_OACTIVE;
1619     ri = &sc->lance_txinfo;
1620     for (di = ri->ri_first; di < ri->ri_last; di++) {
1621         if (di->di_mbuf != NULL) {
1622             m_freem(di->di_mbuf);
1623             di->di_mbuf = NULL;
1624         }
1625         desc.d_flag = 0;
1626         desc.d_addr_lo = LN_ADDR_LO(ri->ri_heap + sc->lance_ramoffset);
1627         desc.d_addr_hi = LN_ADDR_HI(ri->ri_heap + sc->lance_ramoffset);
1628         desc.d_buflen = 0;
1629         LN_PUTDESC(sc, &desc, di->di_addr);
1630     }
1631     ri->ri_nextin = ri->ri_nextout = ri->ri_first;
1632     ri->ri_free = ri->ri_max;
1633     ri->ri_outptr = ri->ri_heap;
1634     ri->ri_outsize = ri->ri_heapend - ri->ri_heap;
1635
1636     ri = &sc->lance_rxinfo;
1637     desc.d_flag = LN_DFLAG_OWNER;
1638     desc.d_buflen = 0 - sc->lance_rxbufsize;
1639     for (di = ri->ri_first; di < ri->ri_last; di++) {
1640         desc.d_addr_lo = LN_ADDR_LO(di->di_bufaddr + sc->lance_ramoffset);
1641         desc.d_addr_hi = LN_ADDR_HI(di->di_bufaddr + sc->lance_ramoffset);
1642         LN_PUTDESC(sc, &desc, di->di_addr);
1643     }
1644     ri->ri_nextin = ri->ri_nextout = ri->ri_first;
1645     ri->ri_outptr = ri->ri_heap;
1646     ri->ri_outsize = ri->ri_heapend - ri->ri_heap;
1647     ri->ri_free = 0;
1648
1649     if (sc->le_if.if_flags & IFF_UP) {
1650         sc->le_if.if_flags |= IFF_RUNNING;
1651         LN_WRCSR(sc, LN_CSR0_START|LN_CSR0_INITDONE|LN_CSR0_ENABINTR);
1652         /* lance_dumpcsrs(sc, "lance_init: up"); */
1653         lance_start(&sc->le_if);
1654     } else {
1655         /* lance_dumpcsrs(sc, "lance_init: down"); */
1656         sc->le_if.if_flags &= ~IFF_RUNNING;
1657     }
1658 }
1659 \f
1660 static void
1661 lance_intr(
1662     le_softc_t *sc)
1663 {
1664     unsigned oldcsr;
1665
1666     oldcsr = LN_RDCSR(sc);
1667     oldcsr &= ~LN_CSR0_ENABINTR;
1668     LN_WRCSR(sc, oldcsr);
1669     LN_WRCSR(sc, LN_CSR0_ENABINTR);
1670
1671     if (oldcsr & LN_CSR0_ERRSUM) {
1672         if (oldcsr & LN_CSR0_MISS) {
1673             /*
1674              *  LN_CSR0_MISS is signaled when the LANCE receiver
1675              *  loses a packet because it doesn't own a receive
1676              *  descriptor. Rev. D LANCE chips, which are no
1677              *  longer used, require a chip reset as described
1678              *  below.
1679              */
1680             LN_STAT(rx_misses++);
1681         }
1682         if (oldcsr & LN_CSR0_MEMERROR) {
1683             LN_STAT(memory_errors++);
1684             if (oldcsr & (LN_CSR0_RXON|LN_CSR0_TXON)) {
1685                 lance_init(sc);
1686                 return;
1687             }
1688         }
1689     }
1690
1691     if ((oldcsr & LN_CSR0_RXINT) && lance_rx_intr(sc)) {
1692         lance_init(sc);
1693         return;
1694     }
1695
1696     if (oldcsr & LN_CSR0_TXINT) {
1697         if (lance_tx_intr(sc))
1698             lance_start(&sc->le_if);
1699     }
1700
1701     if (oldcsr == (LN_CSR0_PENDINTR|LN_CSR0_RXON|LN_CSR0_TXON))
1702         printf("%s%d: lance_intr: stray interrupt\n",
1703                sc->le_if.if_name, sc->le_if.if_unit);
1704 }
1705 \f
1706 static int
1707 lance_rx_intr(
1708     le_softc_t *sc)
1709 {
1710     lance_ring_t *ri = &sc->lance_rxinfo;
1711     lance_descinfo_t *eop;
1712     ln_desc_t desc;
1713     int ndescs, total_len, rxdescs;
1714
1715     LN_STAT(rx_intrs++);
1716
1717     for (rxdescs = 0;;) {
1718         /*
1719          * Now to try to find the end of this packet chain.
1720          */
1721         for (ndescs = 1, eop = ri->ri_nextin;; ndescs++) {
1722             /*
1723              * If we don't own this descriptor, the packet ain't
1724              * all here so return because we are done.
1725              */
1726             LN_GETDESC(sc, &desc, eop->di_addr);
1727             if (desc.d_flag & LN_DFLAG_OWNER)
1728                 return 0;
1729             /*
1730              * In case we have missed a packet and gotten the
1731              * LANCE confused, make sure we are pointing at the
1732              * start of a packet. If we aren't, something is really
1733              * strange so reinit the LANCE.
1734              */
1735             if (desc.d_flag & LN_DFLAG_RxBUFERROR) {
1736                 LN_STAT(rx_buferror++);
1737                 return 1;
1738             }
1739             if ((desc.d_flag & LN_DFLAG_SOP) && eop != ri->ri_nextin) {
1740                 LN_STAT(rx_badsop++);
1741                 return 1;
1742             }
1743             if (desc.d_flag & LN_DFLAG_EOP)
1744                 break;
1745             if (++eop == ri->ri_last)
1746                 eop = ri->ri_first;
1747         }
1748
1749         total_len = (desc.d_status & LN_DSTS_RxLENMASK) - 4;
1750         if ((desc.d_flag & LN_DFLAG_RxERRSUM) == 0) {
1751             /*
1752              * Valid Packet -- If the SOP is less than or equal to the EOP
1753              * or the length is less than the receive buffer size, then the
1754              * packet is contiguous in memory and can be copied in one shot.
1755              * Otherwise we need to copy two segments to get the entire
1756              * packet.
1757              */
1758             if (ri->ri_nextin <= eop || total_len <= ri->ri_heapend - ri->ri_nextin->di_bufaddr) {
1759                 le_input(sc, sc->le_membase + ri->ri_nextin->di_bufaddr,
1760                          total_len, total_len, NULL);
1761                 LN_STAT(rx_contig++);
1762             } else {
1763                 le_input(sc, sc->le_membase + ri->ri_nextin->di_bufaddr,
1764                          total_len,
1765                          ri->ri_heapend - ri->ri_nextin->di_bufaddr,
1766                          sc->le_membase + ri->ri_first->di_bufaddr);
1767                 LN_STAT(rx_noncontig++);
1768             }
1769         } else {
1770             /*
1771              * If the packet is bad, increment the
1772              * counters.
1773              */
1774             sc->le_if.if_ierrors++;
1775             if (desc.d_flag & LN_DFLAG_RxBADCRC)
1776                 LN_STAT(rx_badcrc++);
1777             if (desc.d_flag & LN_DFLAG_RxOVERFLOW)
1778                 LN_STAT(rx_badalign++);
1779             if (desc.d_flag & LN_DFLAG_RxFRAMING)
1780                 LN_STAT(rx_badframe++);
1781         }
1782         sc->le_if.if_ipackets++;
1783         LN_STAT(rx_ndescs[ndescs-1]++);
1784         rxdescs += ndescs;
1785         while (ndescs-- > 0) {
1786             LN_SETFLAG(sc, ri->ri_nextin->di_addr, LN_DFLAG_OWNER);
1787             if (++ri->ri_nextin == ri->ri_last)
1788                 ri->ri_nextin = ri->ri_first;
1789         }
1790     }
1791     /* LN_STAT(rx_intr_descs[rxdescs]++); */
1792     LN_MAXSTAT(rx_intr_hidescs, rxdescs);
1793
1794     return 0;
1795 }
1796 \f
1797 static void
1798 lance_start(
1799     struct ifnet *ifp)
1800 {
1801     le_softc_t *sc = (le_softc_t *) ifp;
1802     struct ifqueue *ifq = &ifp->if_snd;
1803     lance_ring_t *ri = &sc->lance_txinfo;
1804     lance_descinfo_t *di;
1805     ln_desc_t desc;
1806     unsigned len, slop;
1807     struct mbuf *m, *m0;
1808     caddr_t bp;
1809
1810     if ((ifp->if_flags & IFF_RUNNING) == 0)
1811         return;
1812
1813     for (;;) {
1814         IF_DEQUEUE(ifq, m);
1815         if (m == NULL)
1816             break;
1817
1818         /*
1819          * Make the packet meets the minimum size for Ethernet.
1820          * The slop is so that we also use an even number of longwards.
1821          */
1822         len = ETHERMIN + sizeof(struct ether_header);
1823         if (m->m_pkthdr.len > len)
1824             len = m->m_pkthdr.len;
1825
1826         slop = (8 - len) & 3;
1827         /*
1828          * If there are no free ring entries (there must be always
1829          * one owned by the host), or there's not enough space for
1830          * this packet, or this packet would wrap around the end
1831          * of LANCE RAM then wait for the transmits to empty for
1832          * space and ring entries to become available.
1833          */
1834         if (ri->ri_free == 1 || len + slop > ri->ri_outsize) {
1835             /*
1836              * Try to see if we can free up anything off the transit ring.
1837              */
1838             if (lance_tx_intr(sc) > 0) {
1839                 LN_STAT(tx_drains[0]++);
1840                 IF_PREPEND(ifq, m);
1841                 continue;
1842             }
1843             LN_STAT(tx_nospc[0]++);
1844             break;
1845         }
1846
1847         if (len + slop > ri->ri_heapend - ri->ri_outptr) {
1848             /*
1849              * Since the packet won't fit in the end of the transmit
1850              * heap, see if there is space at the beginning of the transmit
1851              * heap.  If not, try again when there is space.
1852              */
1853             LN_STAT(tx_orphaned++);
1854             slop += ri->ri_heapend - ri->ri_outptr;
1855             if (len + slop > ri->ri_outsize) {
1856                 LN_STAT(tx_nospc[1]++);
1857                 break;
1858             }
1859             /*
1860              * Point to the beginning of the heap
1861              */
1862             ri->ri_outptr = ri->ri_heap;
1863             LN_STAT(tx_adoptions++);
1864         }
1865
1866         /*
1867          * Initialize the descriptor (saving the buffer address,
1868          * buffer length, and mbuf) and write the packet out
1869          * to the board.
1870          */
1871         di = ri->ri_nextout;
1872         di->di_bufaddr = ri->ri_outptr;
1873         di->di_buflen = len + slop;
1874         di->di_mbuf = m;
1875         bp = sc->le_membase + di->di_bufaddr;
1876         for (m0 = m; m0 != NULL; m0 = m0->m_next) {
1877             LN_COPYTO(sc, mtod(m0, caddr_t), bp, m0->m_len);
1878             bp += m0->m_len;
1879         }
1880         /*
1881          * Zero out the remainder if needed (< ETHERMIN).
1882          */
1883         if (m->m_pkthdr.len < len)
1884             LN_ZERO(sc, bp, len - m->m_pkthdr.len);
1885
1886         /*
1887          * Finally, copy out the descriptor and tell the
1888          * LANCE to transmit!.
1889          */
1890         desc.d_buflen = 0 - len;
1891         desc.d_addr_lo = LN_ADDR_LO(di->di_bufaddr + sc->lance_ramoffset);
1892         desc.d_addr_hi = LN_ADDR_HI(di->di_bufaddr + sc->lance_ramoffset);
1893         desc.d_flag = LN_DFLAG_SOP|LN_DFLAG_EOP|LN_DFLAG_OWNER;
1894         LN_PUTDESC(sc, &desc, di->di_addr);
1895         LN_WRCSR(sc, LN_CSR0_TXDEMAND|LN_CSR0_ENABINTR);
1896
1897         /*
1898          * Do our bookkeeping with our transmit heap.
1899          * (if we wrap, point back to the beginning).
1900          */
1901         ri->ri_outptr += di->di_buflen;
1902         ri->ri_outsize -= di->di_buflen;
1903         LN_MAXSTAT(high_txoutptr, ri->ri_outptr);
1904         LN_MINSTAT(low_txheapsize, ri->ri_outsize);
1905
1906         if (ri->ri_outptr == ri->ri_heapend)
1907             ri->ri_outptr = ri->ri_heap;
1908
1909         ri->ri_free--;
1910         if (++ri->ri_nextout == ri->ri_last)
1911             ri->ri_nextout = ri->ri_first;
1912         LN_MINSTAT(low_txfree, ri->ri_free);
1913     }
1914     if (m != NULL) {
1915         ifp->if_flags |= IFF_OACTIVE;
1916         IF_PREPEND(ifq, m);
1917     }
1918 }
1919 \f
1920 static int
1921 lance_tx_intr(
1922     le_softc_t *sc)
1923 {
1924     lance_ring_t *ri = &sc->lance_txinfo;
1925     unsigned xmits;
1926
1927     LN_STAT(tx_intrs++);
1928     for (xmits = 0; ri->ri_free < ri->ri_max; ) {
1929         ln_desc_t desc;
1930
1931         LN_GETDESC(sc, &desc, ri->ri_nextin->di_addr);
1932         if (desc.d_flag & LN_DFLAG_OWNER)
1933             break;
1934
1935         if (desc.d_flag & (LN_DFLAG_TxONECOLL|LN_DFLAG_TxMULTCOLL))
1936             sc->le_if.if_collisions++;
1937         if (desc.d_flag & LN_DFLAG_TxDEFERRED)
1938             LN_STAT(tx_deferred++);
1939         if (desc.d_flag & LN_DFLAG_TxONECOLL)
1940             LN_STAT(tx_single_collisions++);
1941         if (desc.d_flag & LN_DFLAG_TxMULTCOLL)
1942             LN_STAT(tx_multiple_collisions++);
1943
1944         if (desc.d_flag & LN_DFLAG_TxERRSUM) {
1945             if (desc.d_status & (LN_DSTS_TxUNDERFLOW|LN_DSTS_TxBUFERROR|
1946                                  LN_DSTS_TxEXCCOLL|LN_DSTS_TxLATECOLL)) {
1947                 if (desc.d_status & LN_DSTS_TxEXCCOLL) {
1948                     unsigned tdr;
1949                     LN_STAT(tx_excessive_collisions++);
1950                     if ((tdr = (desc.d_status & LN_DSTS_TxTDRMASK)) > 0) {
1951                         tdr *= 100;
1952                         printf("%s%d: lance: warning: excessive collisions: TDR %dns (%d-%dm)\n",
1953                                sc->le_if.if_name, sc->le_if.if_unit,
1954                                tdr, (tdr*99)/1000, (tdr*117)/1000);
1955                     }
1956                 }
1957                 if (desc.d_status & LN_DSTS_TxBUFERROR)
1958                     LN_STAT(tx_buferror++);
1959                 sc->le_if.if_oerrors++;
1960                 if ((desc.d_status & LN_DSTS_TxLATECOLL) == 0) {
1961                     lance_init(sc);
1962                     return 0;
1963                 } else {
1964                     LN_STAT(tx_late_collisions++);
1965                 }
1966             }
1967         }
1968         m_freem(ri->ri_nextin->di_mbuf);
1969         ri->ri_nextin->di_mbuf = NULL;
1970         sc->le_if.if_opackets++;
1971         ri->ri_free++;
1972         ri->ri_outsize += ri->ri_nextin->di_buflen;
1973         if (++ri->ri_nextin == ri->ri_last)
1974             ri->ri_nextin = ri->ri_first;
1975         sc->le_if.if_flags &= ~IFF_OACTIVE;
1976         xmits++;
1977     }
1978     if (ri->ri_free == ri->ri_max)
1979         LN_STAT(tx_emptied++);
1980     /* LN_STAT(tx_intr_descs[xmits]++); */
1981     LN_MAXSTAT(tx_intr_hidescs, xmits);
1982     return xmits;
1983 }
1984 #endif /* !defined(LE_NOLANCE) */