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