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