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