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