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