if_de.c revision 7925
1/*-
2 * Copyright (c) 1994, 1995 Matt Thomas (matt@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 * $Id: if_de.c,v 1.22 1995/04/17 08:16:14 davidg Exp $
25 *
26 */
27
28/*
29 * DEC DC21040 PCI Ethernet Controller
30 *
31 * Written by Matt Thomas
32 * BPF support code stolen directly from if_ec.c
33 *
34 *   This driver supports the DEC DE435 or any other PCI
35 *   board which support DC21040.
36 */
37
38#define __IF_DE_C__  "pl2 95/03/21"
39#include "de.h"
40#if NDE > 0
41
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/mbuf.h>
45#include <sys/protosw.h>
46#include <sys/socket.h>
47#include <sys/ioctl.h>
48#include <sys/errno.h>
49#include <sys/malloc.h>
50#include <sys/kernel.h>
51#include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
52#include <sys/devconf.h>
53#include <machine/clock.h>
54
55#include <net/if.h>
56#include <net/if_types.h>
57#include <net/if_dl.h>
58#include <net/route.h>
59
60#include "bpfilter.h"
61#if NBPFILTER > 0
62#include <net/bpf.h>
63#include <net/bpfdesc.h>
64#endif
65
66#ifdef INET
67#include <netinet/in.h>
68#include <netinet/in_systm.h>
69#include <netinet/in_var.h>
70#include <netinet/ip.h>
71#include <netinet/if_ether.h>
72#endif
73
74#ifdef NS
75#include <netns/ns.h>
76#include <netns/ns_if.h>
77#endif
78
79#include <vm/vm.h>
80#include <vm/vm_kern.h>
81#include <vm/vm_param.h>
82
83
84#include <pci.h>
85#if NPCI > 0
86#include <pci/pcivar.h>
87#endif
88
89#include <pci/dc21040.h>
90
91/*
92 * This module supports
93 *	the DEC DC21040 PCI Ethernet Controller.
94 *	the DEC DC21140 PCI Fast Ethernet Controller.
95 */
96
97typedef struct {
98    unsigned long addr;
99    unsigned long length;
100} tulip_addrvec_t;
101
102typedef struct {
103    tulip_desc_t *ri_first;
104    tulip_desc_t *ri_last;
105    tulip_desc_t *ri_nextin;
106    tulip_desc_t *ri_nextout;
107    int ri_max;
108    int ri_free;
109} tulip_ringinfo_t;
110
111typedef struct {
112    volatile tulip_uint32_t *csr_busmode;		/* CSR0 */
113    volatile tulip_uint32_t *csr_txpoll;		/* CSR1 */
114    volatile tulip_uint32_t *csr_rxpoll;		/* CSR2 */
115    volatile tulip_uint32_t *csr_rxlist;		/* CSR3 */
116    volatile tulip_uint32_t *csr_txlist;		/* CSR4 */
117    volatile tulip_uint32_t *csr_status;		/* CSR5 */
118    volatile tulip_uint32_t *csr_command;		/* CSR6 */
119    volatile tulip_uint32_t *csr_intr;			/* CSR7 */
120    volatile tulip_uint32_t *csr_missed_frame;		/* CSR8 */
121
122    /* DC21040 specific registers */
123
124    volatile tulip_sint32_t *csr_enetrom;		/* CSR9 */
125    volatile tulip_uint32_t *csr_reserved;		/* CSR10 */
126    volatile tulip_uint32_t *csr_full_duplex;		/* CSR11 */
127    volatile tulip_uint32_t *csr_sia_status;		/* CSR12 */
128    volatile tulip_uint32_t *csr_sia_connectivity;	/* CSR13 */
129    volatile tulip_uint32_t *csr_sia_tx_rx;		/* CSR14 */
130    volatile tulip_uint32_t *csr_sia_general;		/* CSR15 */
131
132    /* DC21140/DC21041 specific registers */
133
134    volatile tulip_uint32_t *csr_srom_mii;		/* CSR9 */
135    volatile tulip_uint32_t *csr_gp_timer;		/* CSR11 */
136    volatile tulip_uint32_t *csr_gp;			/* CSR12 */
137    volatile tulip_uint32_t *csr_watchdog;		/* CSR15 */
138} tulip_regfile_t;
139
140/*
141 * The DC21040 has a stupid restriction in that the receive
142 * buffers must be longword aligned.  But since Ethernet
143 * headers are not a multiple of longwords in size this forces
144 * the data to non-longword aligned.  Since IP requires the
145 * data to be longword aligned, we need to copy it after it has
146 * been DMA'ed in our memory.
147 *
148 * Since we have to copy it anyways, we might as well as allocate
149 * dedicated receive space for the input.  This allows to use a
150 * small receive buffer size and more ring entries to be able to
151 * better keep with a flood of tiny Ethernet packets.
152 *
153 * The receive space MUST ALWAYS be a multiple of the page size.
154 * And the number of receive descriptors multiplied by the size
155 * of the receive buffers must equal the recevive space.  This
156 * is so that we can manipulate the page tables so that even if a
157 * packet wraps around the end of the receive space, we can
158 * treat it as virtually contiguous.
159 *
160 * The above used to be true (the stupid restriction is still true)
161 * but we gone to directly DMA'ing into MBUFs because with 100Mb
162 * cards the copying is just too much of a hit.
163 */
164#define	TULIP_RXDESCS		16
165#define	TULIP_TXDESCS		128
166#define	TULIP_RXQ_TARGET	8
167
168typedef enum {
169    TULIP_DC21040_GENERIC,
170    TULIP_DC21140_DEC_EB,
171    TULIP_DC21140_DEC_DE500
172} tulip_board_t;
173
174typedef struct _tulip_softc_t tulip_softc_t;
175
176typedef struct {
177    tulip_board_t bd_type;
178    const char *bd_description;
179    int (*bd_media_probe)(tulip_softc_t *sc);
180    void (*bd_media_select)(tulip_softc_t *sc);
181} tulip_boardsw_t;
182
183
184struct _tulip_softc_t {
185    struct arpcom tulip_ac;
186    tulip_regfile_t tulip_csrs;
187    unsigned tulip_flags;
188#define	TULIP_WANTSETUP		0x01
189#define	TULIP_WANTHASH		0x02
190#define	TULIP_DOINGSETUP	0x04
191#define	TULIP_ALTPHYS		0x08	/* use AUI */
192    unsigned char tulip_rombuf[128];
193    tulip_uint32_t tulip_setupbuf[192/sizeof(tulip_uint32_t)];
194    tulip_uint32_t tulip_setupdata[192/sizeof(tulip_uint32_t)];
195    tulip_uint32_t tulip_intrmask;
196    tulip_uint32_t tulip_cmdmode;
197    tulip_uint32_t tulip_revinfo;
198    const tulip_boardsw_t *tulip_boardsw;
199#if NBPFILTER > 0
200    caddr_t tulip_bpf;			/* BPF context */
201#endif
202    struct ifqueue tulip_txq;
203    struct ifqueue tulip_rxq;
204    tulip_ringinfo_t tulip_rxinfo;
205    tulip_ringinfo_t tulip_txinfo;
206};
207
208#ifndef IFF_ALTPHYS
209#define	IFF_ALTPHYS	IFF_LINK2		/* In case it isn't defined */
210#endif
211typedef enum { TULIP_DC21040, TULIP_DC21140, TULIP_DC21041 } tulip_chipid_t;
212const char *tulip_chipdescs[] = {
213    "DC21040 [10Mb/s]",
214    "DC21140 [10-100Mb/s]",
215    "DC21041 [10Mb/s]"
216};
217
218tulip_softc_t *tulips[NDE];
219tulip_chipid_t tulip_chipids[NDE];
220
221#define	tulip_if	tulip_ac.ac_if
222#define	tulip_unit	tulip_ac.ac_if.if_unit
223#define	tulip_name	tulip_ac.ac_if.if_name
224#define	tulip_hwaddr	tulip_ac.ac_enaddr
225
226#define	TULIP_CRC32_POLY	0xEDB88320UL	/* CRC-32 Poly -- Little Endian */
227#define	TULIP_CHECK_RXCRC	0
228#define	TULIP_MAX_TXSEG		30
229
230#define	TULIP_ADDREQUAL(a1, a2) \
231	(((u_short *)a1)[0] == ((u_short *)a2)[0] \
232	 && ((u_short *)a1)[1] == ((u_short *)a2)[1] \
233	 && ((u_short *)a1)[2] == ((u_short *)a2)[2])
234#define	TULIP_ADDRBRDCST(a1) \
235	(((u_short *)a1)[0] == 0xFFFFU \
236	 && ((u_short *)a1)[1] == 0xFFFFU \
237	 && ((u_short *)a1)[2] == 0xFFFFU)
238
239static void tulip_start(struct ifnet *ifp);
240static void tulip_rx_intr(tulip_softc_t *sc);
241static void tulip_addr_filter(tulip_softc_t *sc);
242
243#if __FreeBSD__ > 1
244#define	TULIP_IFRESET_ARGS	int unit
245#define	TULIP_RESET(sc)		tulip_reset((sc)->tulip_unit)
246#else
247#define	TULIP_IFRESET_ARGS	int unit, int uban
248#define	TULIP_RESET(sc)		tulip_reset((sc)->tulip_unit, 0)
249#endif
250
251
252static int
253tulip_dc21040_media_probe(
254    tulip_softc_t *sc)
255{
256    int cnt;
257
258    *sc->tulip_csrs.csr_sia_connectivity = 0;
259    *sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_10BASET;
260    for (cnt = 0; cnt < 2400; cnt++) {
261	if ((*sc->tulip_csrs.csr_sia_status & TULIP_SIASTS_LINKFAIL) == 0)
262	    break;
263	DELAY(1000);
264    }
265    return (*sc->tulip_csrs.csr_sia_status & TULIP_SIASTS_LINKFAIL) != 0;
266}
267
268static void
269tulip_dc21040_media_select(
270    tulip_softc_t *sc)
271{
272    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT;
273    *sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_RESET;
274    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
275	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
276	    printf("%s%d: enabling Thinwire/AUI port\n",
277		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
278	*sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_AUI;
279	sc->tulip_flags |= TULIP_ALTPHYS;
280    } else {
281	if (sc->tulip_flags & TULIP_ALTPHYS)
282	    printf("%s%d: enabling 10baseT/UTP port\n",
283		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
284	*sc->tulip_csrs.csr_sia_connectivity = TULIP_SIACONN_10BASET;
285	sc->tulip_flags &= ~TULIP_ALTPHYS;
286    }
287}
288
289static const tulip_boardsw_t tulip_dc21040_boardsw = {
290    TULIP_DC21040_GENERIC,
291    "",
292    tulip_dc21040_media_probe,
293    tulip_dc21040_media_select
294};
295
296static int
297tulip_dc21140_evalboard_media_probe(
298    tulip_softc_t *sc)
299{
300    *sc->tulip_csrs.csr_gp = TULIP_GP_EB_PINS;
301    *sc->tulip_csrs.csr_gp = TULIP_GP_EB_INIT;
302    *sc->tulip_csrs.csr_command |= TULIP_CMD_PORTSELECT
303	|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_MUSTBEONE;
304    *sc->tulip_csrs.csr_command &= ~TULIP_CMD_TXTHRSHLDCTL;
305    DELAY(1000000);
306    return (*sc->tulip_csrs.csr_gp & TULIP_GP_EB_OK100) != 0;
307}
308
309static void
310tulip_dc21140_evalboard_media_select(
311    tulip_softc_t *sc)
312{
313    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE;
314    *sc->tulip_csrs.csr_gp = TULIP_GP_EB_PINS;
315    *sc->tulip_csrs.csr_gp = TULIP_GP_EB_INIT;
316    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
317	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
318	    printf("%s%d: enabling 100baseTX UTP port\n",
319		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
320	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
321	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
322	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
323	sc->tulip_flags |= TULIP_ALTPHYS;
324    } else {
325	if (sc->tulip_flags & TULIP_ALTPHYS)
326	    printf("%s%d: enabling 10baseT UTP port\n",
327		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
328	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
329			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
330	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
331	sc->tulip_flags &= ~TULIP_ALTPHYS;
332    }
333}
334
335static const tulip_boardsw_t tulip_dc21140_eb_boardsw = {
336    TULIP_DC21140_DEC_EB,
337    "",
338    tulip_dc21140_evalboard_media_probe,
339    tulip_dc21140_evalboard_media_select
340};
341
342static int
343tulip_dc21140_de500_media_probe(
344    tulip_softc_t *sc)
345{
346    *sc->tulip_csrs.csr_gp = TULIP_GP_DE500_PINS;
347    *sc->tulip_csrs.csr_gp = TULIP_GP_DE500_HALFDUPLEX;
348    if ((*sc->tulip_csrs.csr_gp & (TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10)) != (TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10))
349	return (*sc->tulip_csrs.csr_gp & TULIP_GP_DE500_NOTOK_100) == 0;
350    *sc->tulip_csrs.csr_gp = TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100;
351    *sc->tulip_csrs.csr_command |= TULIP_CMD_PORTSELECT
352	|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER|TULIP_CMD_MUSTBEONE;
353    *sc->tulip_csrs.csr_command &= ~TULIP_CMD_TXTHRSHLDCTL;
354    DELAY(1000000);
355    return (*sc->tulip_csrs.csr_gp & TULIP_GP_DE500_NOTOK_100) == 0;
356}
357
358static void
359tulip_dc21140_de500_media_select(
360    tulip_softc_t *sc)
361{
362    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE;
363    *sc->tulip_csrs.csr_gp = TULIP_GP_DE500_PINS;
364    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
365	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
366	    printf("%s%d: enabling 100baseTX UTP port\n",
367		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
368	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
369	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
370	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
371	sc->tulip_flags |= TULIP_ALTPHYS;
372	*sc->tulip_csrs.csr_gp = TULIP_GP_DE500_HALFDUPLEX
373	    |TULIP_GP_DE500_FORCE_100;
374    } else {
375	if (sc->tulip_flags & TULIP_ALTPHYS)
376	    printf("%s%d: enabling 10baseT UTP port\n",
377		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
378	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
379			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
380	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
381	sc->tulip_flags &= ~TULIP_ALTPHYS;
382	*sc->tulip_csrs.csr_gp = TULIP_GP_DE500_HALFDUPLEX;
383    }
384}
385
386static const tulip_boardsw_t tulip_dc21140_de500_boardsw = {
387    TULIP_DC21140_DEC_DE500, "Digital DE500 ",
388    tulip_dc21140_de500_media_probe,
389    tulip_dc21140_de500_media_select
390};
391
392static void
393tulip_reset(
394    TULIP_IFRESET_ARGS)
395{
396    tulip_softc_t *sc = tulips[unit];
397    tulip_ringinfo_t *ri;
398    tulip_desc_t *di;
399
400    *sc->tulip_csrs.csr_busmode = TULIP_BUSMODE_SWRESET;
401    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
402		   33MHz that comes to two microseconds but wait a
403		   bit longer anyways) */
404
405    (*sc->tulip_boardsw->bd_media_select)(sc);
406
407    *sc->tulip_csrs.csr_txlist = vtophys(&sc->tulip_txinfo.ri_first[0]);
408    *sc->tulip_csrs.csr_rxlist = vtophys(&sc->tulip_rxinfo.ri_first[0]);
409    *sc->tulip_csrs.csr_intr = 0;
410    *sc->tulip_csrs.csr_busmode = TULIP_BUSMODE_BURSTLEN_8LW
411	|TULIP_BUSMODE_CACHE_ALIGN8
412	|(BYTE_ORDER != LITTLE_ENDIAN
413	  ? TULIP_BUSMODE_BIGENDIAN
414	  : 0);
415
416    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
417    /*
418     * Free all the mbufs that were on the transmit ring.
419     */
420    for (;;) {
421	struct mbuf *m;
422	IF_DEQUEUE(&sc->tulip_txq, m);
423	if (m == NULL)
424	    break;
425	m_freem(m);
426    }
427
428    ri = &sc->tulip_txinfo;
429    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
430    ri->ri_free = ri->ri_max;
431    for (di = ri->ri_first; di < ri->ri_last; di++)
432	di->d_status = 0;
433
434    /*
435     * We need to collect all the mbufs were on the
436     * receive ring before we reinit it either to put
437     * them back on or to know if we have to allocate
438     * more.
439     */
440    ri = &sc->tulip_rxinfo;
441    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
442    ri->ri_free = ri->ri_max;
443    for (di = ri->ri_first; di < ri->ri_last; di++) {
444	di->d_status = 0;
445	di->d_length1 = 0; di->d_addr1 = 0;
446	di->d_length2 = 0; di->d_addr2 = 0;
447    }
448    for (;;) {
449	struct mbuf *m;
450	IF_DEQUEUE(&sc->tulip_rxq, m);
451	if (m == NULL)
452	    break;
453	m_freem(m);
454    }
455
456    sc->tulip_intrmask = TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
457	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
458	    |TULIP_STS_TXBABBLE|TULIP_STS_LINKFAIL|TULIP_STS_RXSTOPPED;
459    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP);
460    tulip_addr_filter(sc);
461}
462
463static void
464tulip_init(
465    int unit)
466{
467    tulip_softc_t *sc = tulips[unit];
468
469    if (sc->tulip_if.if_flags & IFF_UP) {
470	sc->tulip_if.if_flags |= IFF_RUNNING;
471	if (sc->tulip_if.if_flags & IFF_PROMISC) {
472	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
473	} else {
474	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
475	    if (sc->tulip_if.if_flags & IFF_ALLMULTI) {
476		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
477	    } else {
478		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
479	    }
480	}
481	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
482	if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
483	    tulip_rx_intr(sc);
484	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
485	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
486	} else {
487	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
488	    tulip_start(&sc->tulip_if);
489	}
490	sc->tulip_cmdmode |= TULIP_CMD_THRSHLD160;
491	*sc->tulip_csrs.csr_intr = sc->tulip_intrmask;
492	*sc->tulip_csrs.csr_command = sc->tulip_cmdmode;
493    } else {
494	TULIP_RESET(sc);
495	sc->tulip_if.if_flags &= ~IFF_RUNNING;
496    }
497}
498
499
500#if TULIP_CHECK_RXCRC
501static unsigned
502tulip_crc32(
503    u_char *addr,
504    int len)
505{
506    unsigned int crc = 0xFFFFFFFF;
507    static unsigned int crctbl[256];
508    int idx;
509    static int done;
510    /*
511     * initialize the multicast address CRC table
512     */
513    for (idx = 0; !done && idx < 256; idx++) {
514	unsigned int tmp = idx;
515	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
516	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
517	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
518	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
519	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
520	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
521	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
522	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
523	crctbl[idx] = tmp;
524    }
525    done = 1;
526
527    while (len-- > 0)
528	crc = (crc >> 8) ^ crctbl[*addr++] ^ crctbl[crc & 0xFF];
529
530    return crc;
531}
532#endif
533
534static void
535tulip_rx_intr(
536    tulip_softc_t *sc)
537{
538    tulip_ringinfo_t *ri = &sc->tulip_rxinfo;
539    struct ifnet *ifp = &sc->tulip_if;
540
541    for (;;) {
542	struct ether_header eh;
543	tulip_desc_t *eop = ri->ri_nextin;
544	int total_len = 0;
545	struct mbuf *m = NULL;
546	int accept = 0;
547
548	if (sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
549	     goto queue_mbuf;
550
551	if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
552	    break;
553
554	total_len = ((eop->d_status >> 16) & 0x7FF) - 4;
555	IF_DEQUEUE(&sc->tulip_rxq, m);
556	if ((eop->d_status & TULIP_DSTS_ERRSUM) == 0) {
557
558#if TULIP_CHECK_RXCRC
559	    unsigned crc = tulip_crc32(mtod(m, unsigned char *), total_len);
560	    if (~crc != *((unsigned *) &bufaddr[total_len])) {
561		printf("de0: bad rx crc: %08x [rx] != %08x\n",
562		       *((unsigned *) &bufaddr[total_len]), ~crc);
563		goto next;
564	    }
565#endif
566	    eh = *mtod(m, struct ether_header *);
567#if NBPFILTER > 0
568	    if (sc->tulip_bpf != NULL) {
569		bpf_tap(sc->tulip_bpf, mtod(m, caddr_t), total_len);
570		if ((eh.ether_dhost[0] & 1) == 0 &&
571		    !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr))
572		    goto next;
573	    } else if (!TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr)
574		    && !TULIP_ADDRBRDCST(eh.ether_dhost)) {
575		    goto next;
576	    }
577#endif
578	    accept = 1;
579	} else {
580	    ifp->if_ierrors++;
581	}
582      next:
583	ifp->if_ipackets++;
584	if (++ri->ri_nextin == ri->ri_last)
585	    ri->ri_nextin = ri->ri_first;
586      queue_mbuf:
587	/*
588	 * Either we are priming the TULIP with mbufs (m == NULL)
589	 * or we are about to accept an mbuf for the upper layers
590	 * so we need to allocate an mbuf to replace it.  If we
591	 * can't replace, then count it as an input error and reuse
592	 * the mbuf.
593	 */
594	if (accept || m == NULL) {
595	    struct mbuf *m0;
596	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
597	    if (m0 != NULL) {
598		MCLGET(m0, M_DONTWAIT);
599		if ((m0->m_flags & M_EXT) == 0) {
600		    m_freem(m0);
601		    m0 = NULL;
602		}
603	    }
604	    if (accept) {
605		if (m0 != NULL) {
606		    m->m_pkthdr.rcvif = ifp;
607		    m->m_data += sizeof(struct ether_header);
608		    m->m_len = m->m_pkthdr.len = total_len;
609		    ether_input(ifp, &eh, m);
610		    m = m0;
611		} else {
612		    ifp->if_ierrors++;
613		}
614	    } else {
615		m = m0;
616	    }
617	}
618	if (m == NULL)
619	    break;
620	/*
621	 * Now give the buffer to the TULIP and save in our
622	 * receive queue.
623	 */
624	ri->ri_nextout->d_length1 = MCLBYTES - 4;
625	ri->ri_nextout->d_addr1 = vtophys(mtod(m, caddr_t));
626	ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
627	if (++ri->ri_nextout == ri->ri_last)
628	    ri->ri_nextout = ri->ri_first;
629	IF_ENQUEUE(&sc->tulip_rxq, m);
630    }
631}
632
633static int
634tulip_tx_intr(
635    tulip_softc_t *sc)
636{
637    tulip_ringinfo_t *ri = &sc->tulip_txinfo;
638    struct mbuf *m;
639    int xmits = 0;
640
641    while (ri->ri_free < ri->ri_max) {
642	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
643	    break;
644
645	if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxLASTSEG) {
646	    if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxSETUPPKT) {
647		/*
648		 * We've just finished processing a setup packet.
649		 * Mark that we can finished it.  If there's not
650		 * another pending, startup the TULIP receiver.
651		 * Make sure we ack the RXSTOPPED so we won't get
652		 * an abormal interrupt indication.
653		 */
654		sc->tulip_flags &= ~TULIP_DOINGSETUP;
655		if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
656		    tulip_rx_intr(sc);
657		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
658		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
659		    *sc->tulip_csrs.csr_status = TULIP_STS_RXSTOPPED;
660		    *sc->tulip_csrs.csr_command = sc->tulip_cmdmode;
661		    *sc->tulip_csrs.csr_intr = sc->tulip_intrmask;
662		}
663	   } else {
664		IF_DEQUEUE(&sc->tulip_txq, m);
665		m_freem(m);
666		sc->tulip_if.if_collisions +=
667		    (ri->ri_nextin->d_status & TULIP_DSTS_TxCOLLMASK)
668			>> TULIP_DSTS_V_TxCOLLCNT;
669		if (ri->ri_nextin->d_status & TULIP_DSTS_ERRSUM)
670		    sc->tulip_if.if_oerrors++;
671		xmits++;
672	    }
673	}
674
675	if (++ri->ri_nextin == ri->ri_last)
676	    ri->ri_nextin = ri->ri_first;
677	ri->ri_free++;
678	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
679    }
680    sc->tulip_if.if_opackets += xmits;
681    return xmits;
682}
683
684static int
685tulip_txsegment(
686    tulip_softc_t *sc,
687    struct mbuf *m,
688    tulip_addrvec_t *avp,
689    size_t maxseg)
690{
691    int segcnt;
692
693    for (segcnt = 0; m; m = m->m_next) {
694	int len = m->m_len;
695	caddr_t addr = mtod(m, caddr_t);
696	unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
697
698	while (len > 0) {
699	    unsigned slen = min(len, clsize);
700	    if (segcnt < maxseg) {
701		avp->addr = vtophys(addr);
702		avp->length = slen;
703	    }
704	    len -= slen;
705	    addr += slen;
706	    clsize = CLBYTES;
707	    avp++;
708	    segcnt++;
709	}
710    }
711    if (segcnt >= maxseg) {
712	printf("%s%d: tulip_txsegment: extremely fragmented packet encountered (%d segments)\n",
713	       sc->tulip_name, sc->tulip_unit, segcnt);
714	return -1;
715    }
716    avp->addr = 0;
717    avp->length = 0;
718    return segcnt;
719}
720
721static void
722tulip_start(
723    struct ifnet *ifp)
724{
725    tulip_softc_t *sc = (tulip_softc_t *) ifp;
726    struct ifqueue *ifq = &ifp->if_snd;
727    tulip_ringinfo_t *ri = &sc->tulip_txinfo;
728    tulip_desc_t *sop, *eop;
729    struct mbuf *m;
730    tulip_addrvec_t addrvec[TULIP_MAX_TXSEG+1], *avp;
731    int segcnt;
732    tulip_uint32_t d_status;
733
734    if ((ifp->if_flags & IFF_RUNNING) == 0)
735	return;
736
737    for (;;) {
738	if (sc->tulip_flags & TULIP_WANTSETUP) {
739	    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
740		ifp->if_flags |= IFF_OACTIVE;
741		return;
742	    }
743	    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
744		   sizeof(sc->tulip_setupbuf));
745	    sc->tulip_flags &= ~TULIP_WANTSETUP;
746	    sc->tulip_flags |= TULIP_DOINGSETUP;
747	    ri->ri_free--;
748	    ri->ri_nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
749	    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
750		    |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
751	    if (sc->tulip_flags & TULIP_WANTHASH)
752		ri->ri_nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
753	    ri->ri_nextout->d_length1 = sizeof(sc->tulip_setupbuf);
754	    ri->ri_nextout->d_addr1 = vtophys(sc->tulip_setupbuf);
755	    ri->ri_nextout->d_length2 = 0;
756	    ri->ri_nextout->d_addr2 = 0;
757	    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
758	    *sc->tulip_csrs.csr_txpoll = 1;
759	    /*
760	     * Advance the ring for the next transmit packet.
761	     */
762	    if (++ri->ri_nextout == ri->ri_last)
763		ri->ri_nextout = ri->ri_first;
764	}
765
766	IF_DEQUEUE(ifq, m);
767	if (m == NULL)
768	    break;
769
770	/*
771	 * First find out how many and which different pages
772	 * the mbuf data occupies.  Then check to see if we
773	 * have enough descriptor space in our transmit ring
774	 * to actually send it.
775	 */
776	segcnt = tulip_txsegment(sc, m, addrvec,
777				 min(ri->ri_max - 1, TULIP_MAX_TXSEG));
778	if (segcnt < 0) {
779	    struct mbuf *m0;
780	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
781	    if (m0 != NULL) {
782		if (m->m_pkthdr.len > MHLEN) {
783		    MCLGET(m0, M_DONTWAIT);
784		    if ((m0->m_flags & M_EXT) == 0) {
785			m_freem(m);
786			continue;
787		    }
788		}
789		m_copydata(m, 0, m0->m_pkthdr.len, mtod(m0, caddr_t));
790		m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
791		m_freem(m);
792		IF_PREPEND(ifq, m0);
793		continue;
794	    } else {
795		m_freem(m);
796		continue;
797	    }
798	}
799	if (ri->ri_free - 2 <= (segcnt + 1) >> 1)
800	    break;
801
802	ri->ri_free -= (segcnt + 1) >> 1;
803	/*
804	 * Now we fill in our transmit descriptors.  This is
805	 * a bit reminiscent of going on the Ark two by two
806	 * since each descriptor for the TULIP can describe
807	 * two buffers.  So we advance through the address
808	 * vector two entries at a time to to fill each
809	 * descriptor.  Clear the first and last segment bits
810	 * in each descriptor (actually just clear everything
811	 * but the end-of-ring or chain bits) to make sure
812	 * we don't get messed up by previously sent packets.
813	 */
814	sop = ri->ri_nextout;
815	d_status = 0;
816	avp = addrvec;
817	do {
818	    eop = ri->ri_nextout;
819	    eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
820	    eop->d_status = d_status;
821	    eop->d_addr1 = avp->addr; eop->d_length1 = avp->length; avp++;
822	    eop->d_addr2 = avp->addr; eop->d_length2 = avp->length; avp++;
823	    d_status = TULIP_DSTS_OWNER;
824	    if (++ri->ri_nextout == ri->ri_last)
825		ri->ri_nextout = ri->ri_first;
826	} while ((segcnt -= 2) > 0);
827#if NBPFILTER > 0
828	    if (sc->tulip_bpf != NULL)
829		bpf_mtap(sc->tulip_bpf, m);
830#endif
831	/*
832	 * The descriptors have been filled in.  Mark the first
833	 * and last segments, indicate we want a transmit complete
834	 * interrupt, give the descriptors to the TULIP, and tell
835	 * it to transmit!
836	 */
837
838	IF_ENQUEUE(&sc->tulip_txq, m);
839	eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
840	sop->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
841	sop->d_status = TULIP_DSTS_OWNER;
842
843	*sc->tulip_csrs.csr_txpoll = 1;
844    }
845    if (m != NULL) {
846	ifp->if_flags |= IFF_OACTIVE;
847	IF_PREPEND(ifq, m);
848    }
849}
850
851static int
852tulip_intr(
853    tulip_softc_t *sc)
854{
855    tulip_uint32_t csr;
856    int progress=0;
857
858    while ((csr = *sc->tulip_csrs.csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR)) {
859	progress = 1;
860	*sc->tulip_csrs.csr_status = csr & sc->tulip_intrmask;
861
862	if (csr & TULIP_STS_SYSERROR) {
863	    if ((csr & TULIP_STS_ERRORMASK) == TULIP_STS_ERR_PARITY) {
864		TULIP_RESET(sc);
865		tulip_init(sc->tulip_unit);
866		break;
867	    }
868	}
869	if (csr & TULIP_STS_ABNRMLINTR) {
870	    printf("%s%d: abnormal interrupt: 0x%05x [0x%05x]\n",
871		   sc->tulip_name, sc->tulip_unit, csr, csr & sc->tulip_intrmask);
872	    *sc->tulip_csrs.csr_command = sc->tulip_cmdmode;
873	}
874	if (csr & TULIP_STS_RXINTR)
875	    tulip_rx_intr(sc);
876	if (sc->tulip_txinfo.ri_free < sc->tulip_txinfo.ri_max) {
877	    tulip_tx_intr(sc);
878	    tulip_start(&sc->tulip_if);
879	}
880    }
881    return (progress);
882}
883
884/*
885 *
886 */
887
888void
889tulip_delay_300ns(
890    tulip_softc_t *sc)
891{
892    *sc->tulip_csrs.csr_busmode; *sc->tulip_csrs.csr_busmode;
893    *sc->tulip_csrs.csr_busmode; *sc->tulip_csrs.csr_busmode;
894
895    *sc->tulip_csrs.csr_busmode; *sc->tulip_csrs.csr_busmode;
896    *sc->tulip_csrs.csr_busmode; *sc->tulip_csrs.csr_busmode;
897
898    *sc->tulip_csrs.csr_busmode; *sc->tulip_csrs.csr_busmode;
899    *sc->tulip_csrs.csr_busmode; *sc->tulip_csrs.csr_busmode;
900}
901
902#define EMIT    do { *sc->tulip_csrs.csr_srom_mii = csr; tulip_delay_300ns(sc); } while (0)
903
904
905void
906tulip_idle_srom(
907    tulip_softc_t *sc)
908{
909    unsigned bit, csr;
910
911    csr  = SROMSEL | SROMRD; EMIT;
912    csr ^= SROMCS; EMIT;
913    csr ^= SROMCLKON; EMIT;
914
915    /*
916     * Write 25 cycles of 0 which will force the SROM to be idle.
917     */
918    for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
919        csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
920        csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
921    }
922    csr ^= SROMCLKOFF; EMIT;
923    csr ^= SROMCS; EMIT; EMIT;
924    csr  = 0; EMIT;
925}
926
927
928void
929tulip_read_srom(
930    tulip_softc_t *sc)
931{
932    int idx;
933    const unsigned bitwidth = SROM_BITWIDTH;
934    const unsigned cmdmask = (SROMCMD_RD << bitwidth);
935    const unsigned msb = 1 << (bitwidth + 3 - 1);
936    unsigned lastidx = (1 << bitwidth) - 1;
937    int lowbit = 0;
938
939    tulip_idle_srom(sc);
940
941    for (idx = 0; idx <= lastidx; idx++) {
942        unsigned lastbit, data, bits, bit, csr;
943        csr  = SROMSEL | SROMRD;        EMIT;
944        csr ^= SROMCSON;                EMIT;
945        csr ^=            SROMCLKON;    EMIT;
946
947        lastbit = 0;
948        for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
949            const unsigned thisbit = bits & msb;
950            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
951            if (thisbit != lastbit) {
952                csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
953            }
954            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
955            lastbit = thisbit;
956        }
957        csr ^= SROMCLKOFF; EMIT;
958
959        for (data = 0, bits = 0; bits < 16; bits++) {
960            data <<= 1;
961            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
962            data |= *sc->tulip_csrs.csr_srom_mii & SROMDIN ? 1 : 0;
963            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
964        }
965	sc->tulip_rombuf[idx*2] = data & 0xFF;
966	sc->tulip_rombuf[idx*2+1] = data >> 8;
967        csr  = SROMSEL | SROMRD; EMIT;
968        csr  = 0; EMIT;
969    }
970}
971
972#define	tulip_mchash(mca)	(tulip_crc32(mca, 6) & 0x1FF)
973#define	tulip_srom_crcok(databuf)	( \
974    (tulip_crc32(databuf, 126) & 0xFFFF) == \
975     ((databuf)[126] | ((databuf)[127] << 8)))
976
977static unsigned
978tulip_crc32(
979    const unsigned char *databuf,
980    size_t datalen)
981{
982    u_int idx, bit, data, crc = 0xFFFFFFFFUL;
983
984    for (idx = 0; idx < datalen; idx++)
985        for (data = *databuf++, bit = 0; bit < 8; bit++, data >>= 1)
986            crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0);
987    return crc;
988}
989
990
991/*
992 *  This is the standard method of reading the DEC Address ROMS.
993 */
994static int
995tulip_read_macaddr(
996    tulip_softc_t *sc)
997{
998    int cksum, rom_cksum, idx;
999    tulip_sint32_t csr;
1000    unsigned char tmpbuf[8];
1001    static u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
1002
1003    if (tulip_chipids[sc->tulip_unit] == TULIP_DC21040) {
1004	*sc->tulip_csrs.csr_enetrom = 1;
1005	sc->tulip_boardsw = &tulip_dc21040_boardsw;
1006	for (idx = 0; idx < 32; idx++) {
1007	    int cnt = 0;
1008	    while ((csr = *sc->tulip_csrs.csr_enetrom) < 0 && cnt < 10000)
1009		cnt++;
1010	    sc->tulip_rombuf[idx] = csr & 0xFF;
1011	}
1012    } else {
1013	/*
1014	 * Assume all DC21140 board are compatible with the
1015	 * DEC 10/100 evaluation board.  Not really valid but ...
1016	 */
1017	if (tulip_chipids[sc->tulip_unit] == TULIP_DC21140)
1018	    sc->tulip_boardsw = &tulip_dc21140_eb_boardsw;
1019	tulip_read_srom(sc);
1020	if (tulip_srom_crcok(sc->tulip_rombuf)) {
1021	    /*
1022	     * New SROM format.  Copy out the Ethernet address.
1023	     * If it contains a DE500-XA string, then it must be
1024	     * a DE500-XA.
1025	     */
1026	    bcopy(sc->tulip_rombuf + 20, sc->tulip_hwaddr, 6);
1027	    if (bcmp(sc->tulip_rombuf + 29, "DE500-XA", 8) == 0)
1028		sc->tulip_boardsw = &tulip_dc21140_de500_boardsw;
1029	    if (sc->tulip_boardsw == NULL)
1030		return -6;
1031	    return 0;
1032	}
1033    }
1034
1035
1036    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
1037	/*
1038	 * Some folks don't use the standard ethernet rom format
1039	 * but instead just put the address in the first 6 bytes
1040	 * of the rom and let the rest be all 0xffs.  (Can we say
1041	 * ZNYX???)
1042	 */
1043	for (idx = 6; idx < 32; idx++) {
1044	    if (sc->tulip_rombuf[idx] != 0xFF)
1045		return -4;
1046	}
1047	/*
1048	 * Make sure the address is not multicast or locally assigned
1049	 * that the OUI is not 00-00-00.
1050	 */
1051	if ((sc->tulip_rombuf[0] & 3) != 0)
1052	    return -4;
1053	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
1054		&& sc->tulip_rombuf[2] == 0)
1055	    return -4;
1056	bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
1057	return 0;
1058    }
1059    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
1060	return -3;
1061
1062    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
1063    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
1064    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
1065    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
1066    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
1067	return -2;
1068
1069    bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
1070
1071    cksum = *(u_short *) &sc->tulip_hwaddr[0];
1072    cksum *= 2;
1073    if (cksum > 65535) cksum -= 65535;
1074    cksum += *(u_short *) &sc->tulip_hwaddr[2];
1075    if (cksum > 65535) cksum -= 65535;
1076    cksum *= 2;
1077    if (cksum > 65535) cksum -= 65535;
1078    cksum += *(u_short *) &sc->tulip_hwaddr[4];
1079    if (cksum >= 65535) cksum -= 65535;
1080
1081    rom_cksum = *(u_short *) &sc->tulip_rombuf[6];
1082
1083    if (cksum != rom_cksum)
1084	return -1;
1085    return 0;
1086}
1087
1088static void
1089tulip_addr_filter(
1090    tulip_softc_t *sc)
1091{
1092    tulip_uint32_t *sp = sc->tulip_setupdata;
1093    struct ether_multistep step;
1094    struct ether_multi *enm;
1095    int i;
1096
1097    sc->tulip_flags &= ~TULIP_WANTHASH;
1098    sc->tulip_flags |= TULIP_WANTSETUP;
1099    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
1100    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
1101    if (sc->tulip_ac.ac_multicnt > 14) {
1102	unsigned hash;
1103	/*
1104	 * If we have more than 14 multicasts, we have
1105	 * go into hash perfect mode (512 bit multicast
1106	 * hash and one perfect hardware).
1107	 */
1108
1109	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
1110	hash = tulip_mchash(etherbroadcastaddr);
1111	sp[hash >> 4] |= 1 << (hash & 0xF);
1112	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
1113	while (enm != NULL) {
1114	    hash = tulip_mchash(enm->enm_addrlo);
1115	    sp[hash >> 4] |= 1 << (hash & 0xF);
1116	    ETHER_NEXT_MULTI(step, enm);
1117	}
1118	sc->tulip_cmdmode |= TULIP_WANTHASH;
1119	sp[39] = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
1120	sp[40] = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
1121	sp[41] = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
1122    } else {
1123	/*
1124	 * Else can get perfect filtering for 16 addresses.
1125	 */
1126	i = 0;
1127	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
1128	for (; enm != NULL; i++) {
1129	    *sp++ = ((u_short *) enm->enm_addrlo)[0];
1130	    *sp++ = ((u_short *) enm->enm_addrlo)[1];
1131	    *sp++ = ((u_short *) enm->enm_addrlo)[2];
1132	    ETHER_NEXT_MULTI(step, enm);
1133	}
1134	/*
1135	 * If an IP address is enabled, turn on broadcast
1136	 */
1137	if (sc->tulip_ac.ac_ipaddr.s_addr != 0) {
1138	    i++;
1139	    *sp++ = 0xFFFF;
1140	    *sp++ = 0xFFFF;
1141	    *sp++ = 0xFFFF;
1142	}
1143	/*
1144	 * Pad the rest with our hardware address
1145	 */
1146	for (; i < 16; i++) {
1147	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
1148	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
1149	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
1150	}
1151    }
1152}
1153
1154/*extern void arp_ifinit(struct arpcom *, struct ifaddr*);*/
1155
1156static int
1157tulip_ioctl(
1158    struct ifnet *ifp,
1159    int cmd,
1160    caddr_t data)
1161{
1162    tulip_softc_t *sc = tulips[ifp->if_unit];
1163    struct ifaddr *ifa = (struct ifaddr *)data;
1164    struct ifreq *ifr = (struct ifreq *) data;
1165    int s, error = 0;
1166
1167    s = splimp();
1168
1169    switch (cmd) {
1170	case SIOCSIFADDR: {
1171
1172	    ifp->if_flags |= IFF_UP;
1173	    switch(ifa->ifa_addr->sa_family) {
1174#ifdef INET
1175		case AF_INET: {
1176		    ((struct arpcom *)ifp)->ac_ipaddr = IA_SIN(ifa)->sin_addr;
1177		    tulip_addr_filter(sc);	/* reset multicast filtering */
1178		    (*ifp->if_init)(ifp->if_unit);
1179		    arp_ifinit((struct arpcom *)ifp, ifa);
1180		    break;
1181		}
1182#endif /* INET */
1183
1184#ifdef NS
1185		/* This magic copied from if_is.c; I don't use XNS,
1186		 * so I have no way of telling if this actually
1187		 * works or not.
1188		 */
1189		case AF_NS: {
1190		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1191		    if (ns_nullhost(*ina)) {
1192			ina->x_host = *(union ns_host *)(sc->tulip_ac.ac_enaddr);
1193		    } else {
1194			ifp->if_flags &= ~IFF_RUNNING;
1195			bcopy((caddr_t)ina->x_host.c_host,
1196			      (caddr_t)sc->tulip_ac.ac_enaddr,
1197			      sizeof sc->tulip_ac.ac_enaddr);
1198		    }
1199
1200		    (*ifp->if_init)(ifp->if_unit);
1201		    break;
1202		}
1203#endif /* NS */
1204
1205		default: {
1206		    (*ifp->if_init)(ifp->if_unit);
1207		    break;
1208		}
1209	    }
1210	    break;
1211	}
1212
1213	case SIOCSIFFLAGS: {
1214	    /*
1215	     * Changing the connection forces a reset.
1216	     */
1217	    if (sc->tulip_flags & TULIP_ALTPHYS) {
1218		if ((ifp->if_flags & IFF_ALTPHYS) == 0)
1219		    TULIP_RESET(sc);
1220	    } else {
1221		if (ifp->if_flags & IFF_ALTPHYS)
1222		    TULIP_RESET(sc);
1223	    }
1224	    (*ifp->if_init)(ifp->if_unit);
1225	    break;
1226	}
1227
1228	case SIOCADDMULTI:
1229	case SIOCDELMULTI: {
1230	    /*
1231	     * Update multicast listeners
1232	     */
1233	    if (cmd == SIOCADDMULTI)
1234		error = ether_addmulti(ifr, &sc->tulip_ac);
1235	    else
1236		error = ether_delmulti(ifr, &sc->tulip_ac);
1237
1238	    if (error == ENETRESET) {
1239		tulip_addr_filter(sc);		/* reset multicast filtering */
1240		(*ifp->if_init)(ifp->if_unit);
1241		error = 0;
1242	    }
1243	    break;
1244	}
1245	case SIOCSIFMTU:
1246	    /*
1247	     * Set the interface MTU.
1248	     */
1249	    if (ifr->ifr_mtu > ETHERMTU) {
1250		error = EINVAL;
1251	    } else {
1252		ifp->if_mtu = ifr->ifr_mtu;
1253	    }
1254	    break;
1255
1256	default: {
1257	    error = EINVAL;
1258	    break;
1259	}
1260    }
1261
1262    splx(s);
1263    return error;
1264}
1265
1266static void
1267tulip_attach(
1268    tulip_softc_t *sc)
1269{
1270    struct ifnet *ifp = &sc->tulip_if;
1271
1272    if ((*sc->tulip_boardsw->bd_media_probe)(sc)) {
1273	ifp->if_flags |= IFF_ALTPHYS;
1274    } else {
1275	sc->tulip_flags |= TULIP_ALTPHYS;
1276    }
1277
1278    TULIP_RESET(sc);
1279
1280    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
1281    ifp->if_init = tulip_init;
1282    ifp->if_ioctl = tulip_ioctl;
1283    ifp->if_output = ether_output;
1284    ifp->if_reset = tulip_reset;
1285    ifp->if_start = tulip_start;
1286
1287    printf("%s%d: %s%s pass %d.%d Ethernet address %s\n",
1288	   sc->tulip_name, sc->tulip_unit,
1289	   sc->tulip_boardsw->bd_description,
1290	   tulip_chipdescs[tulip_chipids[sc->tulip_unit]],
1291	   (sc->tulip_revinfo & 0xF0) >> 4,
1292	   sc->tulip_revinfo & 0x0F,
1293	   ether_sprintf(sc->tulip_hwaddr));
1294
1295    if_attach(ifp);
1296
1297#if NBPFILTER > 0
1298    bpfattach(&sc->tulip_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
1299#endif
1300}
1301
1302static void
1303tulip_initcsrs(
1304    tulip_softc_t *sc,
1305    volatile tulip_uint32_t *va_csrs,
1306    size_t csr_size)
1307{
1308    sc->tulip_csrs.csr_busmode		= va_csrs +  0 * csr_size;
1309    sc->tulip_csrs.csr_txpoll		= va_csrs +  1 * csr_size;
1310    sc->tulip_csrs.csr_rxpoll		= va_csrs +  2 * csr_size;
1311    sc->tulip_csrs.csr_rxlist		= va_csrs +  3 * csr_size;
1312    sc->tulip_csrs.csr_txlist		= va_csrs +  4 * csr_size;
1313    sc->tulip_csrs.csr_status		= va_csrs +  5 * csr_size;
1314    sc->tulip_csrs.csr_command		= va_csrs +  6 * csr_size;
1315    sc->tulip_csrs.csr_intr		= va_csrs +  7 * csr_size;
1316    sc->tulip_csrs.csr_missed_frame	= va_csrs +  8 * csr_size;
1317    if (tulip_chipids[sc->tulip_unit] == TULIP_DC21040) {
1318	sc->tulip_csrs.csr_enetrom		= va_csrs +  9 * csr_size;
1319	sc->tulip_csrs.csr_reserved		= va_csrs + 10 * csr_size;
1320	sc->tulip_csrs.csr_full_duplex		= va_csrs + 11 * csr_size;
1321	sc->tulip_csrs.csr_sia_status		= va_csrs + 12 * csr_size;
1322	sc->tulip_csrs.csr_sia_connectivity	= va_csrs + 13 * csr_size;
1323	sc->tulip_csrs.csr_sia_tx_rx 		= va_csrs + 14 * csr_size;
1324	sc->tulip_csrs.csr_sia_general		= va_csrs + 15 * csr_size;
1325    } else if (tulip_chipids[sc->tulip_unit] == TULIP_DC21140) {
1326	if (sc->tulip_revinfo < 0x10)
1327	    sc->tulip_csrs.csr_enetrom	= va_csrs +  9 * csr_size;
1328	else
1329	    sc->tulip_csrs.csr_srom_mii = va_csrs +  9 * csr_size;
1330
1331	sc->tulip_csrs.csr_gp_timer	= va_csrs + 11 * csr_size;
1332	sc->tulip_csrs.csr_gp		= va_csrs + 12 * csr_size;
1333	sc->tulip_csrs.csr_watchdog	= va_csrs + 15 * csr_size;
1334    }
1335}
1336
1337static void
1338tulip_initring(
1339    tulip_softc_t *sc,
1340    tulip_ringinfo_t *ri,
1341    tulip_desc_t *descs,
1342    int ndescs)
1343{
1344    ri->ri_max = ndescs;
1345    ri->ri_first = descs;
1346    ri->ri_last = ri->ri_first + ri->ri_max;
1347    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
1348    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
1349}
1350
1351#if NPCI > 0
1352/*
1353 * This is the PCI configuration support.  Since the DC21040 is available
1354 * on both EISA and PCI boards, one must be careful in how defines the
1355 * DC21040 in the config file.
1356 */
1357static char* tulip_pci_probe (pcici_t config_id, pcidi_t device_id);
1358static void  tulip_pci_attach(pcici_t config_id, int unit);
1359static u_long tulip_pci_count;
1360static int tulip_pci_shutdown(struct kern_devconf *, int);
1361
1362struct pci_device dedevice = {
1363    "de",
1364    tulip_pci_probe,
1365    tulip_pci_attach,
1366   &tulip_pci_count,
1367    tulip_pci_shutdown,
1368};
1369
1370DATA_SET (pcidevice_set, dedevice);
1371
1372#define	PCI_CFID	0x00	/* Configuration ID */
1373#define	PCI_CFCS	0x04	/* Configurtion Command/Status */
1374#define	PCI_CFRV	0x08	/* Configuration Revision */
1375#define	PCI_CFLT	0x0c	/* Configuration Latency Timer */
1376#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
1377#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
1378#define	PCI_CFIT	0x3c	/* Configuration Interrupt */
1379#define	PCI_CFDA	0x40	/* Configuration Driver Area */
1380
1381#define	TULIP_PCI_CSRSIZE	(8 / sizeof(tulip_uint32_t))
1382static char*
1383tulip_pci_probe(
1384    pcici_t config_id,
1385    pcidi_t device_id)
1386{
1387    if (device_id == 0x00021011ul) {
1388	if (tulip_pci_count < NDE)
1389	    tulip_chipids[tulip_pci_count] = TULIP_DC21040;
1390	return "Digital DC21040 Ethernet";
1391    }
1392    if (device_id == 0x00141011ul) {
1393	if (tulip_pci_count < NDE)
1394	    tulip_chipids[tulip_pci_count] = TULIP_DC21041;
1395	return "Digital DC21041 Ethernet";
1396    }
1397    if (device_id == 0x00091011ul) {
1398	if (tulip_pci_count < NDE)
1399	    tulip_chipids[tulip_pci_count] = TULIP_DC21140;
1400	return "Digital DC21140 Fast Ethernet";
1401    }
1402    return NULL;
1403}
1404
1405static void
1406tulip_pci_attach(
1407    pcici_t config_id,
1408    int unit)
1409{
1410    tulip_softc_t *sc;
1411    int retval, idx, revinfo;
1412    vm_offset_t va_csrs, pa_csrs;
1413    tulip_desc_t *rxdescs, *txdescs;
1414
1415    if (unit >= NDE) {
1416	printf("de%d: not configured; kernel is built for only %d device%s.\n",
1417	       unit, NDE, NDE == 1 ? "" : "s");
1418	return;
1419    }
1420
1421    revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
1422    if (tulip_chipids[unit] == TULIP_DC21040 && revinfo < 0x20) {
1423	printf("de%d: not configured; DC21040 pass 2.0 required (%d.%d found)\n",
1424	       unit, revinfo >> 4, revinfo & 0x0f);
1425	return;
1426    } else if (tulip_chipids[unit] == TULIP_DC21140 && revinfo < 0x11) {
1427	printf("de%d: not configured; DC21140 pass 1.1 required (%d.%d found)\n",
1428	       unit, revinfo >> 4, revinfo & 0x0f);
1429	return;
1430    }
1431
1432
1433    sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
1434    if (sc == NULL)
1435	return;
1436
1437    rxdescs = (tulip_desc_t *)
1438	malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
1439    if (rxdescs == NULL) {
1440	free((caddr_t) sc, M_DEVBUF);
1441	return;
1442    }
1443
1444    txdescs = (tulip_desc_t *)
1445	malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
1446    if (txdescs == NULL) {
1447	free((caddr_t) rxdescs, M_DEVBUF);
1448	free((caddr_t) sc, M_DEVBUF);
1449	return;
1450    }
1451
1452    bzero(sc, sizeof(*sc));				/* Zero out the softc*/
1453
1454    sc->tulip_unit = unit;
1455    sc->tulip_name = "de";
1456    retval = pci_map_mem(config_id, PCI_CBMA, &va_csrs, &pa_csrs);
1457    if (!retval) {
1458	free((caddr_t) txdescs, M_DEVBUF);
1459	free((caddr_t) rxdescs, M_DEVBUF);
1460	free((caddr_t) sc, M_DEVBUF);
1461	return;
1462    }
1463    tulips[unit] = sc;
1464    sc->tulip_revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
1465    tulip_initcsrs(sc, (volatile tulip_uint32_t *) va_csrs, TULIP_PCI_CSRSIZE);
1466    tulip_initring(sc, &sc->tulip_rxinfo, rxdescs, TULIP_RXDESCS);
1467    tulip_initring(sc, &sc->tulip_txinfo, txdescs, TULIP_TXDESCS);
1468    if ((retval = tulip_read_macaddr(sc)) < 0) {
1469	printf("de%d: can't read ENET ROM (why=%d) (", sc->tulip_unit, retval);
1470	for (idx = 0; idx < 32; idx++)
1471	    printf("%02x", sc->tulip_rombuf[idx]);
1472	printf("\n");
1473	printf("%s%d: %s%s pass %d.%d Ethernet address %s\n",
1474	       sc->tulip_name, sc->tulip_unit,
1475	       (sc->tulip_boardsw != NULL ? sc->tulip_boardsw->bd_description : ""),
1476	       tulip_chipdescs[tulip_chipids[sc->tulip_unit]],
1477	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F,
1478	       "unknown");
1479    } else {
1480	TULIP_RESET(sc);
1481	tulip_attach(sc);
1482	pci_map_int (config_id, tulip_intr, (void*) sc, &net_imask);
1483    }
1484}
1485
1486static int
1487tulip_pci_shutdown(
1488    struct kern_devconf *kdc,
1489    int force)
1490{
1491    if (kdc->kdc_unit < NDE) {
1492	tulip_softc_t *sc = tulips[kdc->kdc_unit];
1493	*sc->tulip_csrs.csr_busmode = TULIP_BUSMODE_SWRESET;
1494	DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
1495			   33MHz that comes to two microseconds but wait a
1496			   bit longer anyways) */
1497    }
1498    (void) dev_detach(kdc);
1499    return 0;
1500}
1501
1502#endif /* NPCI > 0 */
1503#endif /* NDE > 0 */
1504