if_de.c revision 9349
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.29 1995/06/11 19:31:49 rgrimes 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 or DC21140 (mostly).
36 */
37
38#if defined(__FreeBSD__)
39#include "de.h"
40#endif
41#if NDE > 0 || !defined(__FreeBSD__)
42
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/mbuf.h>
46#include <sys/protosw.h>
47#include <sys/socket.h>
48#include <sys/ioctl.h>
49#include <sys/errno.h>
50#include <sys/malloc.h>
51#include <sys/kernel.h>
52#include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
53#if defined(__FreeBSD__)
54#include <sys/devconf.h>
55#include <machine/clock.h>
56#elif defined(__bsdi__) || defined(__NetBSD__)
57#include <sys/device.h>
58#endif
59
60#include <net/if.h>
61#include <net/if_types.h>
62#include <net/if_dl.h>
63#include <net/route.h>
64
65#include "bpfilter.h"
66#if NBPFILTER > 0
67#include <net/bpf.h>
68#include <net/bpfdesc.h>
69#endif
70
71#ifdef INET
72#include <netinet/in.h>
73#include <netinet/in_systm.h>
74#include <netinet/in_var.h>
75#include <netinet/ip.h>
76#include <netinet/if_ether.h>
77#endif
78
79#ifdef NS
80#include <netns/ns.h>
81#include <netns/ns_if.h>
82#endif
83
84#include <vm/vm.h>
85#include <vm/vm_kern.h>
86#include <vm/vm_param.h>
87
88#if defined(__FreeBSD__)
89#include <pci.h>
90#if NPCI > 0
91#include <pci/pcivar.h>
92#include <pci/dc21040.h>
93#endif
94#endif
95
96#if defined(__bsdi__)
97#include <i386/pci/pci.h>
98#include <i386/pci/dc21040.h>
99#include <i386/isa/isa.h>
100#include <i386/isa/icu.h>
101#include <i386/isa/dma.h>
102#include <i386/isa/isavar.h>
103#endif
104
105#if defined(__NetBSD__)
106#include <dev/pci/pcivar.h>
107#include <dev/ic/dc21040.h>
108#endif
109
110/*
111 * This module supports
112 *	the DEC DC21040 PCI Ethernet Controller.
113 *	the DEC DC21140 PCI Fast Ethernet Controller.
114 */
115
116typedef struct {
117    unsigned long addr;
118    unsigned long length;
119} tulip_addrvec_t;
120
121typedef struct {
122    tulip_desc_t *ri_first;
123    tulip_desc_t *ri_last;
124    tulip_desc_t *ri_nextin;
125    tulip_desc_t *ri_nextout;
126    int ri_max;
127    int ri_free;
128} tulip_ringinfo_t;
129
130typedef struct {
131    volatile tulip_uint32_t *csr_busmode;		/* CSR0 */
132    volatile tulip_uint32_t *csr_txpoll;		/* CSR1 */
133    volatile tulip_uint32_t *csr_rxpoll;		/* CSR2 */
134    volatile tulip_uint32_t *csr_rxlist;		/* CSR3 */
135    volatile tulip_uint32_t *csr_txlist;		/* CSR4 */
136    volatile tulip_uint32_t *csr_status;		/* CSR5 */
137    volatile tulip_uint32_t *csr_command;		/* CSR6 */
138    volatile tulip_uint32_t *csr_intr;			/* CSR7 */
139    volatile tulip_uint32_t *csr_missed_frame;		/* CSR8 */
140
141    /* DC21040 specific registers */
142
143    volatile tulip_sint32_t *csr_enetrom;		/* CSR9 */
144    volatile tulip_uint32_t *csr_reserved;		/* CSR10 */
145    volatile tulip_uint32_t *csr_full_duplex;		/* CSR11 */
146    volatile tulip_uint32_t *csr_sia_status;		/* CSR12 */
147    volatile tulip_uint32_t *csr_sia_connectivity;	/* CSR13 */
148    volatile tulip_uint32_t *csr_sia_tx_rx;		/* CSR14 */
149    volatile tulip_uint32_t *csr_sia_general;		/* CSR15 */
150
151    /* DC21140/DC21041 specific registers */
152
153    volatile tulip_uint32_t *csr_srom_mii;		/* CSR9 */
154    volatile tulip_uint32_t *csr_gp_timer;		/* CSR11 */
155    volatile tulip_uint32_t *csr_gp;			/* CSR12 */
156    volatile tulip_uint32_t *csr_watchdog;		/* CSR15 */
157} tulip_regfile_t;
158
159/*
160 * macros to read and write CSRs.  Note that the "0 +" in
161 * READ_CSR is to prevent the macro from being an lvalue
162 * and WRITE_CSR shouldn't be assigned from.
163 */
164#define	TULIP_READ_CSR(sc, csr)		(0 + *(sc)->tulip_csrs.csr)
165#ifndef __alpha__
166#define	TULIP_WRITE_CSR(sc, csr, val) \
167	    ((void)(*(sc)->tulip_csrs.csr = (val)))
168#else
169#define	TULIP_WRITE_CSR(sc, csr, val) \
170	    ((void)(*(sc)->tulip_csrs.csr = (val), MB()))
171#endif
172
173/*
174 * The DC21040 has a stupid restriction in that the receive
175 * buffers must be longword aligned.  But since Ethernet
176 * headers are not a multiple of longwords in size this forces
177 * the data to non-longword aligned.  Since IP requires the
178 * data to be longword aligned, we need to copy it after it has
179 * been DMA'ed in our memory.
180 *
181 * Since we have to copy it anyways, we might as well as allocate
182 * dedicated receive space for the input.  This allows to use a
183 * small receive buffer size and more ring entries to be able to
184 * better keep with a flood of tiny Ethernet packets.
185 *
186 * The receive space MUST ALWAYS be a multiple of the page size.
187 * And the number of receive descriptors multiplied by the size
188 * of the receive buffers must equal the recevive space.  This
189 * is so that we can manipulate the page tables so that even if a
190 * packet wraps around the end of the receive space, we can
191 * treat it as virtually contiguous.
192 *
193 * The above used to be true (the stupid restriction is still true)
194 * but we gone to directly DMA'ing into MBUFs because with 100Mb
195 * cards the copying is just too much of a hit.
196 */
197#if defined(__alpha__)
198#define	TULIP_COPY_RXDATA	1
199#endif
200
201
202#define	TULIP_RXDESCS		16
203#define	TULIP_TXDESCS		128
204#define	TULIP_RXQ_TARGET	8
205
206typedef enum {
207    TULIP_DC21040_GENERIC,
208    TULIP_DC21140_DEC_EB,
209    TULIP_DC21140_DEC_DE500,
210    TULIP_DC21140_COGENT_EM100
211} tulip_board_t;
212
213typedef struct _tulip_softc_t tulip_softc_t;
214
215typedef struct {
216    tulip_board_t bd_type;
217    const char *bd_description;
218    int (*bd_media_probe)(tulip_softc_t *sc);
219    void (*bd_media_select)(tulip_softc_t *sc);
220} tulip_boardsw_t;
221
222typedef enum { TULIP_DC21040, TULIP_DC21140, TULIP_DC21041 } tulip_chipid_t;
223
224struct _tulip_softc_t {
225#if defined(__bsdi__)
226    struct device tulip_dev;		/* base device */
227    struct isadev tulip_id;		/* ISA device */
228    struct intrhand tulip_ih;		/* intrrupt vectoring */
229    struct atshutdown tulip_ats;	/* shutdown routine */
230#endif
231#if defined(__NetBSD__)
232    struct device tulip_dev;		/* base device */
233    void *tulip_ih;			/* intrrupt vectoring */
234    /* XXX no shutdown routine? */
235#endif
236    struct arpcom tulip_ac;
237    tulip_regfile_t tulip_csrs;
238    unsigned tulip_flags;
239#define	TULIP_WANTSETUP		0x01
240#define	TULIP_WANTHASH		0x02
241#define	TULIP_DOINGSETUP	0x04
242#define	TULIP_ALTPHYS		0x08	/* use AUI */
243    unsigned char tulip_rombuf[128];
244    tulip_uint32_t tulip_setupbuf[192/sizeof(tulip_uint32_t)];
245    tulip_uint32_t tulip_setupdata[192/sizeof(tulip_uint32_t)];
246    tulip_uint32_t tulip_intrmask;
247    tulip_uint32_t tulip_cmdmode;
248    tulip_uint32_t tulip_revinfo;
249    tulip_chipid_t tulip_chipid;
250    const tulip_boardsw_t *tulip_boardsw;
251    struct ifqueue tulip_txq;
252    struct ifqueue tulip_rxq;
253    tulip_ringinfo_t tulip_rxinfo;
254    tulip_ringinfo_t tulip_txinfo;
255};
256
257#ifndef IFF_ALTPHYS
258#define	IFF_ALTPHYS	IFF_LINK0		/* In case it isn't defined */
259#endif
260static const char *tulip_chipdescs[] = {
261    "DC21040 [10Mb/s]",
262    "DC21140 [10-100Mb/s]",
263    "DC21041 [10Mb/s]"
264};
265
266#if defined(__FreeBSD__)
267typedef void ifnet_ret_t;
268typedef int ioctl_cmd_t;
269tulip_softc_t *tulips[NDE];
270#define	TULIP_UNIT_TO_SOFTC(unit)	(tulips[unit])
271#endif
272#if defined(__bsdi__)
273typedef int ifnet_ret_t;
274typedef int ioctl_cmd_t;
275extern struct cfdriver decd;
276#define	TULIP_UNIT_TO_SOFTC(unit)	((tulip_softc_t *) decd.cd_devs[unit])
277#endif
278#if defined(__NetBSD__)
279typedef void ifnet_ret_t;
280typedef u_long ioctl_cmd_t;
281extern struct cfdriver decd;
282#define	TULIP_UNIT_TO_SOFTC(unit)	((tulip_softc_t *) decd.cd_devs[unit])
283#endif
284
285#define	tulip_if	tulip_ac.ac_if
286#define	tulip_unit	tulip_ac.ac_if.if_unit
287#define	tulip_name	tulip_ac.ac_if.if_name
288#define	tulip_bpf	tulip_ac.ac_if.if_bpf
289#define	tulip_hwaddr	tulip_ac.ac_enaddr
290
291#define	TULIP_CRC32_POLY	0xEDB88320UL	/* CRC-32 Poly -- Little Endian */
292#define	TULIP_CHECK_RXCRC	0
293#define	TULIP_MAX_TXSEG		30
294
295#define	TULIP_ADDREQUAL(a1, a2) \
296	(((u_short *)a1)[0] == ((u_short *)a2)[0] \
297	 && ((u_short *)a1)[1] == ((u_short *)a2)[1] \
298	 && ((u_short *)a1)[2] == ((u_short *)a2)[2])
299#define	TULIP_ADDRBRDCST(a1) \
300	(((u_short *)a1)[0] == 0xFFFFU \
301	 && ((u_short *)a1)[1] == 0xFFFFU \
302	 && ((u_short *)a1)[2] == 0xFFFFU)
303
304static ifnet_ret_t tulip_start(struct ifnet *ifp);
305static void tulip_rx_intr(tulip_softc_t *sc);
306static void tulip_addr_filter(tulip_softc_t *sc);
307
308
309static int
310tulip_dc21040_media_probe(
311    tulip_softc_t * const sc)
312{
313    int cnt;
314
315    TULIP_WRITE_CSR(sc, csr_sia_connectivity, 0);
316    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
317    for (cnt = 0; cnt < 2400; cnt++) {
318	if ((TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
319	    break;
320	DELAY(1000);
321    }
322    return (TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) != 0;
323}
324
325static void
326tulip_dc21040_media_select(
327    tulip_softc_t * const sc)
328{
329    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT;
330    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
331    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
332	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
333	    printf("%s%d: enabling Thinwire/AUI port\n",
334		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
335	TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_AUI);
336	sc->tulip_flags |= TULIP_ALTPHYS;
337    } else {
338	if (sc->tulip_flags & TULIP_ALTPHYS)
339	    printf("%s%d: enabling 10baseT/UTP port\n",
340		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
341	TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
342	sc->tulip_flags &= ~TULIP_ALTPHYS;
343    }
344}
345
346static const tulip_boardsw_t tulip_dc21040_boardsw = {
347    TULIP_DC21040_GENERIC,
348    "",
349    tulip_dc21040_media_probe,
350    tulip_dc21040_media_select
351};
352
353static int
354tulip_dc21140_evalboard_media_probe(
355    tulip_softc_t * const sc)
356{
357    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_PINS);
358    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_INIT);
359    TULIP_WRITE_CSR(sc, csr_command,
360	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
361	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
362    TULIP_WRITE_CSR(sc, csr_command,
363	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
364    DELAY(1000000);
365    return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_EB_OK100) != 0;
366}
367
368static void
369tulip_dc21140_evalboard_media_select(
370    tulip_softc_t * const sc)
371{
372    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE;
373    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_PINS);
374    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_INIT);
375    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
376	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
377	    printf("%s%d: enabling 100baseTX UTP port\n",
378		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
379	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
380	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
381	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
382	sc->tulip_flags |= TULIP_ALTPHYS;
383    } else {
384	if (sc->tulip_flags & TULIP_ALTPHYS)
385	    printf("%s%d: enabling 10baseT UTP port\n",
386		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
387	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
388			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
389	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
390	sc->tulip_flags &= ~TULIP_ALTPHYS;
391    }
392}
393
394static const tulip_boardsw_t tulip_dc21140_eb_boardsw = {
395    TULIP_DC21140_DEC_EB,
396    "",
397    tulip_dc21140_evalboard_media_probe,
398    tulip_dc21140_evalboard_media_select
399};
400
401
402static int
403tulip_dc21140_cogent_em100_media_probe(
404    tulip_softc_t * const sc)
405{
406    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_PINS);
407    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_INIT);
408    TULIP_WRITE_CSR(sc, csr_command,
409	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
410	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
411    TULIP_WRITE_CSR(sc, csr_command,
412	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
413    return 1;
414}
415
416static void
417tulip_dc21140_cogent_em100_media_select(
418    tulip_softc_t * const sc)
419{
420    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE;
421    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_PINS);
422    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_INIT);
423    if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
424	printf("%s%d: enabling 100baseTX UTP port\n",
425	       sc->tulip_if.if_name, sc->tulip_if.if_unit);
426    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
427	|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
428    sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
429    sc->tulip_flags |= TULIP_ALTPHYS;
430}
431
432static const tulip_boardsw_t tulip_dc21140_cogent_em100_boardsw = {
433    TULIP_DC21140_COGENT_EM100,
434    "Cogent EM100",
435    tulip_dc21140_cogent_em100_media_probe,
436    tulip_dc21140_cogent_em100_media_select
437};
438
439static int
440tulip_dc21140_de500_media_probe(
441    tulip_softc_t * const sc)
442{
443    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_PINS);
444    DELAY(1000);
445    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_HALFDUPLEX);
446    if ((TULIP_READ_CSR(sc, csr_gp) &
447	(TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10)) !=
448	(TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10))
449	return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_DE500_NOTOK_100) != 0;
450    TULIP_WRITE_CSR(sc, csr_gp,
451	TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100);
452    TULIP_WRITE_CSR(sc, csr_command,
453	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
454	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
455    TULIP_WRITE_CSR(sc, csr_command,
456	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
457    DELAY(1000000);
458    return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_DE500_NOTOK_100) != 0;
459}
460
461static void
462tulip_dc21140_de500_media_select(
463    tulip_softc_t * const sc)
464{
465    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE;
466    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_PINS);
467    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
468	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
469	    printf("%s%d: enabling 100baseTX UTP port\n",
470		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
471	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
472	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
473	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
474	sc->tulip_flags |= TULIP_ALTPHYS;
475	TULIP_WRITE_CSR(sc, csr_gp,
476	    TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100);
477    } else {
478	if (sc->tulip_flags & TULIP_ALTPHYS)
479	    printf("%s%d: enabling 10baseT UTP port\n",
480		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
481	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
482			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
483	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
484	sc->tulip_flags &= ~TULIP_ALTPHYS;
485	TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_HALFDUPLEX);
486    }
487}
488
489static const tulip_boardsw_t tulip_dc21140_de500_boardsw = {
490    TULIP_DC21140_DEC_DE500, "Digital DE500 ",
491    tulip_dc21140_de500_media_probe,
492    tulip_dc21140_de500_media_select
493};
494
495static void
496tulip_reset(
497    tulip_softc_t * const sc)
498{
499    tulip_ringinfo_t *ri;
500    tulip_desc_t *di;
501
502    TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
503    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
504		   33MHz that comes to two microseconds but wait a
505		   bit longer anyways) */
506
507    (*sc->tulip_boardsw->bd_media_select)(sc);
508
509    TULIP_WRITE_CSR(sc, csr_txlist, vtophys(&sc->tulip_txinfo.ri_first[0]));
510    TULIP_WRITE_CSR(sc, csr_rxlist, vtophys(&sc->tulip_rxinfo.ri_first[0]));
511    TULIP_WRITE_CSR(sc, csr_intr, 0);
512    TULIP_WRITE_CSR(sc, csr_busmode,
513        TULIP_BUSMODE_BURSTLEN_8LW|TULIP_BUSMODE_CACHE_ALIGN8
514	|(BYTE_ORDER != LITTLE_ENDIAN ? TULIP_BUSMODE_BIGENDIAN : 0));
515
516    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
517    /*
518     * Free all the mbufs that were on the transmit ring.
519     */
520    for (;;) {
521	struct mbuf *m;
522	IF_DEQUEUE(&sc->tulip_txq, m);
523	if (m == NULL)
524	    break;
525	m_freem(m);
526    }
527
528    ri = &sc->tulip_txinfo;
529    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
530    ri->ri_free = ri->ri_max;
531    for (di = ri->ri_first; di < ri->ri_last; di++)
532	di->d_status = 0;
533
534    /*
535     * We need to collect all the mbufs were on the
536     * receive ring before we reinit it either to put
537     * them back on or to know if we have to allocate
538     * more.
539     */
540    ri = &sc->tulip_rxinfo;
541    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
542    ri->ri_free = ri->ri_max;
543    for (di = ri->ri_first; di < ri->ri_last; di++) {
544	di->d_status = 0;
545	di->d_length1 = 0; di->d_addr1 = 0;
546	di->d_length2 = 0; di->d_addr2 = 0;
547    }
548    for (;;) {
549	struct mbuf *m;
550	IF_DEQUEUE(&sc->tulip_rxq, m);
551	if (m == NULL)
552	    break;
553	m_freem(m);
554    }
555
556    sc->tulip_intrmask = TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
557	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
558	    |TULIP_STS_TXBABBLE|TULIP_STS_LINKFAIL|TULIP_STS_RXSTOPPED;
559    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP);
560    tulip_addr_filter(sc);
561}
562
563static void
564tulip_init(
565    tulip_softc_t * const sc)
566{
567    if (sc->tulip_if.if_flags & IFF_UP) {
568	sc->tulip_if.if_flags |= IFF_RUNNING;
569	if (sc->tulip_if.if_flags & IFF_PROMISC) {
570	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
571	} else {
572	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
573	    if (sc->tulip_if.if_flags & IFF_ALLMULTI) {
574		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
575	    } else {
576		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
577	    }
578	}
579	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
580	if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
581	    tulip_rx_intr(sc);
582	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
583	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
584	} else {
585	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
586	    tulip_start(&sc->tulip_if);
587	}
588	sc->tulip_cmdmode |= TULIP_CMD_THRSHLD160;
589	TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
590	TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
591    } else {
592	tulip_reset(sc);
593	sc->tulip_if.if_flags &= ~IFF_RUNNING;
594    }
595}
596
597
598#if TULIP_CHECK_RXCRC
599static unsigned
600tulip_crc32(
601    u_char *addr,
602    int len)
603{
604    unsigned int crc = 0xFFFFFFFF;
605    static unsigned int crctbl[256];
606    int idx;
607    static int done;
608    /*
609     * initialize the multicast address CRC table
610     */
611    for (idx = 0; !done && idx < 256; idx++) {
612	unsigned int tmp = idx;
613	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
614	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
615	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
616	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
617	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
618	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
619	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
620	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
621	crctbl[idx] = tmp;
622    }
623    done = 1;
624
625    while (len-- > 0)
626	crc = (crc >> 8) ^ crctbl[*addr++] ^ crctbl[crc & 0xFF];
627
628    return crc;
629}
630#endif
631
632static void
633tulip_rx_intr(
634    tulip_softc_t * const sc)
635{
636    tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
637    struct ifnet * const ifp = &sc->tulip_if;
638
639    for (;;) {
640	struct ether_header eh;
641	tulip_desc_t *eop = ri->ri_nextin;
642	int total_len = 0;
643	struct mbuf *m = NULL;
644	int accept = 0;
645
646	if (sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
647	     goto queue_mbuf;
648
649	if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
650	    break;
651
652	total_len = ((eop->d_status >> 16) & 0x7FF) - 4;
653	IF_DEQUEUE(&sc->tulip_rxq, m);
654	if ((eop->d_status & TULIP_DSTS_ERRSUM) == 0) {
655
656#if TULIP_CHECK_RXCRC
657	    unsigned crc = tulip_crc32(mtod(m, unsigned char *), total_len);
658	    if (~crc != *((unsigned *) &bufaddr[total_len])) {
659		printf("%s%d: bad rx crc: %08x [rx] != %08x\n",
660		       sc->tulip_name, sc->tulip_unit,
661		       *((unsigned *) &bufaddr[total_len]), ~crc);
662		goto next;
663	    }
664#endif
665	    eh = *mtod(m, struct ether_header *);
666#if NBPFILTER > 0
667	    if (sc->tulip_bpf != NULL)
668		bpf_tap(sc->tulip_bpf, mtod(m, caddr_t), total_len);
669#endif
670	    if ((sc->tulip_if.if_flags & IFF_PROMISC)
671		    && (eh.ether_dhost[0] & 1) == 0
672		    && !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr))
673		    goto next;
674	    accept = 1;
675	    total_len -= sizeof(struct ether_header);
676	} else {
677	    ifp->if_ierrors++;
678	}
679      next:
680	ifp->if_ipackets++;
681	if (++ri->ri_nextin == ri->ri_last)
682	    ri->ri_nextin = ri->ri_first;
683      queue_mbuf:
684	/*
685	 * Either we are priming the TULIP with mbufs (m == NULL)
686	 * or we are about to accept an mbuf for the upper layers
687	 * so we need to allocate an mbuf to replace it.  If we
688	 * can't replace, then count it as an input error and reuse
689	 * the mbuf.
690	 */
691	if (accept || m == NULL) {
692	    struct mbuf *m0;
693	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
694	    if (m0 != NULL) {
695#if defined(TULIP_COPY_RXDATA)
696		if (!accept || total_len >= MHLEN) {
697#endif
698		    MCLGET(m0, M_DONTWAIT);
699		    if ((m0->m_flags & M_EXT) == 0) {
700			m_freem(m0);
701			m0 = NULL;
702		    }
703#if defined(TULIP_COPY_RXDATA)
704		}
705#endif
706	    }
707	    if (accept) {
708		if (m0 != NULL) {
709#if defined(__bsdi__)
710		    eh.ether_type = ntohs(eh.ether_type);
711#endif
712#if !defined(TULIP_COPY_RXDATA)
713		    m->m_data += sizeof(struct ether_header);
714		    m->m_len = m->m_pkthdr.len = total_len;
715		    m->m_pkthdr.rcvif = ifp;
716		    ether_input(ifp, &eh, m);
717		    m = m0;
718#else
719		    bcopy(mtod(m, caddr_t) + sizeof(struct ether_header),
720			  mtod(m0, caddr_t), total_len);
721		    m0->m_len = m0->m_pkthdr.len = total_len;
722		    m0->m_pkthdr.rcvif = ifp;
723		    ether_input(ifp, &eh, m0);
724#endif
725		} else {
726		    ifp->if_ierrors++;
727		}
728	    } else {
729		m = m0;
730	    }
731	}
732	if (m == NULL)
733	    break;
734	/*
735	 * Now give the buffer to the TULIP and save in our
736	 * receive queue.
737	 */
738	ri->ri_nextout->d_length1 = MCLBYTES - 4;
739	ri->ri_nextout->d_addr1 = vtophys(mtod(m, caddr_t));
740	ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
741	if (++ri->ri_nextout == ri->ri_last)
742	    ri->ri_nextout = ri->ri_first;
743	IF_ENQUEUE(&sc->tulip_rxq, m);
744    }
745}
746
747static int
748tulip_tx_intr(
749    tulip_softc_t * const sc)
750{
751    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
752    struct mbuf *m;
753    int xmits = 0;
754
755    while (ri->ri_free < ri->ri_max) {
756	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
757	    break;
758
759	if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxLASTSEG) {
760	    if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxSETUPPKT) {
761		/*
762		 * We've just finished processing a setup packet.
763		 * Mark that we can finished it.  If there's not
764		 * another pending, startup the TULIP receiver.
765		 * Make sure we ack the RXSTOPPED so we won't get
766		 * an abormal interrupt indication.
767		 */
768		sc->tulip_flags &= ~TULIP_DOINGSETUP;
769		if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
770		    tulip_rx_intr(sc);
771		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
772		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
773		    TULIP_WRITE_CSR(sc, csr_status, TULIP_STS_RXSTOPPED);
774		    TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
775		    TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
776		}
777	   } else {
778		IF_DEQUEUE(&sc->tulip_txq, m);
779		m_freem(m);
780		sc->tulip_if.if_collisions +=
781		    (ri->ri_nextin->d_status & TULIP_DSTS_TxCOLLMASK)
782			>> TULIP_DSTS_V_TxCOLLCNT;
783		if (ri->ri_nextin->d_status & TULIP_DSTS_ERRSUM)
784		    sc->tulip_if.if_oerrors++;
785		xmits++;
786	    }
787	}
788
789	if (++ri->ri_nextin == ri->ri_last)
790	    ri->ri_nextin = ri->ri_first;
791	ri->ri_free++;
792	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
793    }
794    sc->tulip_if.if_opackets += xmits;
795    return xmits;
796}
797
798static ifnet_ret_t
799tulip_start(
800    struct ifnet * const ifp)
801{
802    tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(ifp->if_unit);
803    struct ifqueue * const ifq = &ifp->if_snd;
804    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
805    struct mbuf *m, *m0, *next_m0;
806
807    if ((ifp->if_flags & IFF_RUNNING) == 0)
808	return;
809
810    for (;;) {
811	tulip_desc_t *eop, *nextout;
812	int segcnt, free, recopy;
813	tulip_uint32_t d_status;
814
815	if (sc->tulip_flags & TULIP_WANTSETUP) {
816	    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
817		ifp->if_flags |= IFF_OACTIVE;
818		return;
819	    }
820	    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
821		   sizeof(sc->tulip_setupbuf));
822	    sc->tulip_flags &= ~TULIP_WANTSETUP;
823	    sc->tulip_flags |= TULIP_DOINGSETUP;
824	    ri->ri_free--;
825	    ri->ri_nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
826	    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
827		    |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
828	    if (sc->tulip_flags & TULIP_WANTHASH)
829		ri->ri_nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
830	    ri->ri_nextout->d_length1 = sizeof(sc->tulip_setupbuf);
831	    ri->ri_nextout->d_addr1 = vtophys(sc->tulip_setupbuf);
832	    ri->ri_nextout->d_length2 = 0;
833	    ri->ri_nextout->d_addr2 = 0;
834	    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
835	    TULIP_WRITE_CSR(sc, csr_txpoll, 1);
836	    /*
837	     * Advance the ring for the next transmit packet.
838	     */
839	    if (++ri->ri_nextout == ri->ri_last)
840		ri->ri_nextout = ri->ri_first;
841	    /*
842	     * Make sure the next descriptor is owned by us since it
843	     * may have been set up above if we ran out of room in the
844	     * ring.
845	     */
846	    ri->ri_nextout->d_status = 0;
847	}
848
849	IF_DEQUEUE(ifq, m);
850	if (m == NULL)
851	    break;
852
853	/*
854	 * Now we try to fill in our transmit descriptors.  This is
855	 * a bit reminiscent of going on the Ark two by two
856	 * since each descriptor for the TULIP can describe
857	 * two buffers.  So we advance through packet filling
858	 * each of the two entries at a time to to fill each
859	 * descriptor.  Clear the first and last segment bits
860	 * in each descriptor (actually just clear everything
861	 * but the end-of-ring or chain bits) to make sure
862	 * we don't get messed up by previously sent packets.
863	 *
864	 * We may fail to put the entire packet on the ring if
865	 * there is either not enough ring entries free or if the
866	 * packet has more than MAX_TXSEG segments.  In the former
867	 * case we will just wait for the ring to empty.  In the
868	 * latter case we have to recopy.
869	 */
870	d_status = 0;
871	recopy = 0;
872	eop = nextout = ri->ri_nextout;
873	m0 = m;
874	segcnt = 0;
875	free = ri->ri_free;
876	do {
877	    int len = m0->m_len;
878	    caddr_t addr = mtod(m0, caddr_t);
879	    unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
880
881	    next_m0 = m0->m_next;
882	    while (len > 0) {
883		unsigned slen = min(len, clsize);
884
885		segcnt++;
886		if (segcnt > TULIP_MAX_TXSEG) {
887		    recopy = 1;
888		    next_m0 = NULL; /* to break out of outside loop */
889		    break;
890		}
891		if (segcnt & 1) {
892		    if (--free == 0) {
893			/*
894			 * There's no more room but since nothing
895			 * has been committed at this point, just
896			 * show output is active, put back the
897			 * mbuf and return.
898			 */
899			ifp->if_flags |= IFF_OACTIVE;
900			IF_PREPEND(ifq, m);
901			return;
902		    }
903		    eop = nextout;
904		    if (++nextout == ri->ri_last)
905			nextout = ri->ri_first;
906		    eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
907		    eop->d_status = d_status;
908		    eop->d_addr1 = vtophys(addr); eop->d_length1 = slen;
909		} else {
910		    /*
911		     *  Fill in second half of descriptor
912		     */
913		    eop->d_addr2 = vtophys(addr); eop->d_length2 = slen;
914		}
915		d_status = TULIP_DSTS_OWNER;
916		len -= slen;
917		addr += slen;
918		clsize = CLBYTES;
919	    }
920	} while ((m0 = next_m0) != NULL);
921
922	/*
923	 * The packet exceeds the number of transmit buffer
924	 * entries that we can use for one packet, so we have
925	 * recopy it into one mbuf and then try again.
926	 */
927	if (recopy) {
928	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
929	    if (m0 != NULL) {
930		if (m->m_pkthdr.len > MHLEN) {
931		    MCLGET(m0, M_DONTWAIT);
932		    if ((m0->m_flags & M_EXT) == 0) {
933			m_freem(m);
934			m_freem(m0);
935			continue;
936		    }
937		}
938		m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
939		m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
940		IF_PREPEND(ifq, m0);
941	    }
942	    m_freem(m);
943	    continue;
944	}
945
946	/*
947	 * The descriptors have been filled in.  Now get ready
948	 * to transmit.
949	 */
950#if NBPFILTER > 0
951	if (sc->tulip_bpf != NULL)
952	    bpf_mtap(sc->tulip_bpf, m);
953#endif
954	IF_ENQUEUE(&sc->tulip_txq, m);
955
956	/*
957	 * Make sure the next descriptor after this packet is owned
958	 * by us since it may have been set up above if we ran out
959	 * of room in the ring.
960	 */
961	nextout->d_status = 0;
962
963	/*
964	 * If we only used the first segment of the last descriptor,
965	 * make sure the second segment will not be used.
966	 */
967	if (segcnt & 1) {
968	    eop->d_addr2 = 0;
969	    eop->d_length2 = 0;
970	}
971
972	/*
973	 * Mark the last and first segments, indicate we want a transmit
974	 * complete interrupt, give the descriptors to the TULIP, and tell
975	 * it to transmit!
976	 */
977	eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
978
979	/*
980	 * Note that ri->ri_nextout is still the start of the packet
981	 * and until we set the OWNER bit, we can still back out of
982	 * everything we have done.
983	 */
984	ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
985	ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
986
987	/*
988	 * This advances the ring for us.
989	 */
990	ri->ri_nextout = nextout;
991	ri->ri_free = free;
992
993	TULIP_WRITE_CSR(sc, csr_txpoll, 1);
994    }
995    if (m != NULL) {
996	ifp->if_flags |= IFF_OACTIVE;
997	IF_PREPEND(ifq, m);
998    }
999}
1000
1001static int
1002tulip_intr(
1003    void *arg)
1004{
1005    tulip_softc_t * const sc = (tulip_softc_t *) arg;
1006    tulip_uint32_t csr;
1007    int progress=0;
1008
1009    while ((csr = TULIP_READ_CSR(sc, csr_status)) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR)) {
1010	progress = 1;
1011	TULIP_WRITE_CSR(sc, csr_status, csr & sc->tulip_intrmask);
1012
1013	if (csr & TULIP_STS_SYSERROR) {
1014	    if ((csr & TULIP_STS_ERRORMASK) == TULIP_STS_ERR_PARITY) {
1015		tulip_reset(sc);
1016		tulip_init(sc);
1017		break;
1018	    }
1019	}
1020	if (csr & TULIP_STS_ABNRMLINTR) {
1021	    printf("%s%d: abnormal interrupt: 0x%05x [0x%05x]\n",
1022		   sc->tulip_name, sc->tulip_unit, csr, csr & sc->tulip_intrmask);
1023	    TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
1024	}
1025	if (csr & TULIP_STS_RXINTR)
1026	    tulip_rx_intr(sc);
1027	if (sc->tulip_txinfo.ri_free < sc->tulip_txinfo.ri_max) {
1028	    tulip_tx_intr(sc);
1029	    tulip_start(&sc->tulip_if);
1030	}
1031    }
1032    return (progress);
1033}
1034
1035/*
1036 *
1037 */
1038
1039static void
1040tulip_delay_300ns(
1041    tulip_softc_t * const sc)
1042{
1043    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1044    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1045
1046    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1047    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1048
1049    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1050    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1051}
1052
1053#define EMIT    do { TULIP_WRITE_CSR(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
1054
1055static void
1056tulip_idle_srom(
1057    tulip_softc_t * const sc)
1058{
1059    unsigned bit, csr;
1060
1061    csr  = SROMSEL | SROMRD; EMIT;
1062    csr ^= SROMCS; EMIT;
1063    csr ^= SROMCLKON; EMIT;
1064
1065    /*
1066     * Write 25 cycles of 0 which will force the SROM to be idle.
1067     */
1068    for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
1069        csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1070        csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1071    }
1072    csr ^= SROMCLKOFF; EMIT;
1073    csr ^= SROMCS; EMIT; EMIT;
1074    csr  = 0; EMIT;
1075}
1076
1077
1078static void
1079tulip_read_srom(
1080    tulip_softc_t * const sc)
1081{
1082    int idx;
1083    const unsigned bitwidth = SROM_BITWIDTH;
1084    const unsigned cmdmask = (SROMCMD_RD << bitwidth);
1085    const unsigned msb = 1 << (bitwidth + 3 - 1);
1086    unsigned lastidx = (1 << bitwidth) - 1;
1087
1088    tulip_idle_srom(sc);
1089
1090    for (idx = 0; idx <= lastidx; idx++) {
1091        unsigned lastbit, data, bits, bit, csr;
1092        csr  = SROMSEL | SROMRD;        EMIT;
1093        csr ^= SROMCSON;                EMIT;
1094        csr ^=            SROMCLKON;    EMIT;
1095
1096        lastbit = 0;
1097        for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
1098            const unsigned thisbit = bits & msb;
1099            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1100            if (thisbit != lastbit) {
1101                csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
1102            }
1103            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1104            lastbit = thisbit;
1105        }
1106        csr ^= SROMCLKOFF; EMIT;
1107
1108        for (data = 0, bits = 0; bits < 16; bits++) {
1109            data <<= 1;
1110            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1111            data |= TULIP_READ_CSR(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
1112            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1113        }
1114	sc->tulip_rombuf[idx*2] = data & 0xFF;
1115	sc->tulip_rombuf[idx*2+1] = data >> 8;
1116        csr  = SROMSEL | SROMRD; EMIT;
1117        csr  = 0; EMIT;
1118    }
1119}
1120
1121#define	tulip_mchash(mca)	(tulip_crc32(mca, 6) & 0x1FF)
1122#define	tulip_srom_crcok(databuf)	( \
1123    (tulip_crc32(databuf, 126) & 0xFFFF) == \
1124     ((databuf)[126] | ((databuf)[127] << 8)))
1125
1126static unsigned
1127tulip_crc32(
1128    const unsigned char *databuf,
1129    size_t datalen)
1130{
1131    u_int idx, bit, data, crc = 0xFFFFFFFFUL;
1132
1133    for (idx = 0; idx < datalen; idx++)
1134        for (data = *databuf++, bit = 0; bit < 8; bit++, data >>= 1)
1135            crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0);
1136    return crc;
1137}
1138
1139
1140/*
1141 *  This is the standard method of reading the DEC Address ROMS.
1142 */
1143static int
1144tulip_read_macaddr(
1145    tulip_softc_t *sc)
1146{
1147    int cksum, rom_cksum, idx;
1148    tulip_sint32_t csr;
1149    unsigned char tmpbuf[8];
1150    static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
1151
1152    if (sc->tulip_chipid == TULIP_DC21040) {
1153	TULIP_WRITE_CSR(sc, csr_enetrom, 1);
1154	sc->tulip_boardsw = &tulip_dc21040_boardsw;
1155	for (idx = 0; idx < 32; idx++) {
1156	    int cnt = 0;
1157	    while ((csr = TULIP_READ_CSR(sc, csr_enetrom)) < 0 && cnt < 10000)
1158		cnt++;
1159	    sc->tulip_rombuf[idx] = csr & 0xFF;
1160	}
1161    } else {
1162	/*
1163	 * Assume all DC21140 board are compatible with the
1164	 * DEC 10/100 evaluation board.  Not really valid but ...
1165	 */
1166	if (sc->tulip_chipid == TULIP_DC21140)
1167	    sc->tulip_boardsw = &tulip_dc21140_eb_boardsw;
1168	tulip_read_srom(sc);
1169	if (tulip_srom_crcok(sc->tulip_rombuf)) {
1170	    /*
1171	     * New SROM format.  Copy out the Ethernet address.
1172	     * If it contains a DE500-XA string, then it must be
1173	     * a DE500-XA.
1174	     */
1175	    bcopy(sc->tulip_rombuf + 20, sc->tulip_hwaddr, 6);
1176	    if (bcmp(sc->tulip_rombuf + 29, "DE500-XA", 8) == 0)
1177		sc->tulip_boardsw = &tulip_dc21140_de500_boardsw;
1178	    if (sc->tulip_boardsw == NULL)
1179		return -6;
1180	    return 0;
1181	}
1182    }
1183
1184
1185    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
1186	/*
1187	 * Some folks don't use the standard ethernet rom format
1188	 * but instead just put the address in the first 6 bytes
1189	 * of the rom and let the rest be all 0xffs.  (Can we say
1190	 * ZNYX???)
1191	 */
1192	for (idx = 6; idx < 32; idx++) {
1193	    if (sc->tulip_rombuf[idx] != 0xFF)
1194		return -4;
1195	}
1196	/*
1197	 * Make sure the address is not multicast or locally assigned
1198	 * that the OUI is not 00-00-00.
1199	 */
1200	if ((sc->tulip_rombuf[0] & 3) != 0)
1201	    return -4;
1202	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
1203		&& sc->tulip_rombuf[2] == 0)
1204	    return -4;
1205	bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
1206	return 0;
1207    }
1208    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
1209	return -3;
1210
1211    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
1212    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
1213    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
1214    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
1215    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
1216	return -2;
1217
1218    bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
1219
1220    cksum = *(u_short *) &sc->tulip_hwaddr[0];
1221    cksum *= 2;
1222    if (cksum > 65535) cksum -= 65535;
1223    cksum += *(u_short *) &sc->tulip_hwaddr[2];
1224    if (cksum > 65535) cksum -= 65535;
1225    cksum *= 2;
1226    if (cksum > 65535) cksum -= 65535;
1227    cksum += *(u_short *) &sc->tulip_hwaddr[4];
1228    if (cksum >= 65535) cksum -= 65535;
1229
1230    rom_cksum = *(u_short *) &sc->tulip_rombuf[6];
1231
1232    if (cksum != rom_cksum)
1233	return -1;
1234
1235    if (sc->tulip_chipid == TULIP_DC21140) {
1236	if (sc->tulip_hwaddr[0] == TULIP_OUI_COGENT_0
1237		&& sc->tulip_hwaddr[1] == TULIP_OUI_COGENT_1
1238		&& sc->tulip_hwaddr[2] == TULIP_OUI_COGENT_2) {
1239	    if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100_ID)
1240		sc->tulip_boardsw = &tulip_dc21140_cogent_em100_boardsw;
1241	}
1242    }
1243
1244    return 0;
1245}
1246
1247static void
1248tulip_addr_filter(
1249    tulip_softc_t * const sc)
1250{
1251    tulip_uint32_t *sp = sc->tulip_setupdata;
1252    struct ether_multistep step;
1253    struct ether_multi *enm;
1254    int i;
1255
1256    sc->tulip_flags &= ~TULIP_WANTHASH;
1257    sc->tulip_flags |= TULIP_WANTSETUP;
1258    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
1259    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
1260    if (sc->tulip_ac.ac_multicnt > 14) {
1261	unsigned hash;
1262	/*
1263	 * If we have more than 14 multicasts, we have
1264	 * go into hash perfect mode (512 bit multicast
1265	 * hash and one perfect hardware).
1266	 */
1267
1268	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
1269	hash = tulip_mchash(etherbroadcastaddr);
1270	sp[hash >> 4] |= 1 << (hash & 0xF);
1271	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
1272	while (enm != NULL) {
1273	    hash = tulip_mchash(enm->enm_addrlo);
1274	    sp[hash >> 4] |= 1 << (hash & 0xF);
1275	    ETHER_NEXT_MULTI(step, enm);
1276	}
1277	sc->tulip_flags |= TULIP_WANTHASH;
1278	sp[39] = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
1279	sp[40] = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
1280	sp[41] = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
1281    } else {
1282	/*
1283	 * Else can get perfect filtering for 16 addresses.
1284	 */
1285	i = 0;
1286	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
1287	for (; enm != NULL; i++) {
1288	    *sp++ = ((u_short *) enm->enm_addrlo)[0];
1289	    *sp++ = ((u_short *) enm->enm_addrlo)[1];
1290	    *sp++ = ((u_short *) enm->enm_addrlo)[2];
1291	    ETHER_NEXT_MULTI(step, enm);
1292	}
1293	/*
1294	 * If an IP address is enabled, turn on broadcast
1295	 */
1296	if (sc->tulip_ac.ac_ipaddr.s_addr != 0) {
1297	    i++;
1298	    *sp++ = 0xFFFF;
1299	    *sp++ = 0xFFFF;
1300	    *sp++ = 0xFFFF;
1301	}
1302	/*
1303	 * Pad the rest with our hardware address
1304	 */
1305	for (; i < 16; i++) {
1306	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
1307	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
1308	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
1309	}
1310    }
1311}
1312
1313/*extern void arp_ifinit(struct arpcom *, struct ifaddr*);*/
1314
1315static int
1316tulip_ioctl(
1317    struct ifnet * const ifp,
1318    ioctl_cmd_t cmd,
1319    caddr_t data)
1320{
1321    tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(ifp->if_unit);
1322    struct ifaddr *ifa = (struct ifaddr *)data;
1323    struct ifreq *ifr = (struct ifreq *) data;
1324    int s, error = 0;
1325
1326    s = splimp();
1327
1328    switch (cmd) {
1329	case SIOCSIFADDR: {
1330
1331	    ifp->if_flags |= IFF_UP;
1332	    switch(ifa->ifa_addr->sa_family) {
1333#ifdef INET
1334		case AF_INET: {
1335		    ((struct arpcom *)ifp)->ac_ipaddr = IA_SIN(ifa)->sin_addr;
1336		    tulip_addr_filter(sc);	/* reset multicast filtering */
1337		    tulip_init(sc);
1338#if defined(__FreeBSD__) || defined(__NetBSD__)
1339		    arp_ifinit((struct arpcom *)ifp, ifa);
1340#elif defined(__bsdi__)
1341		    arpwhohas((struct arpcom *)ifp, &IA_SIN(ifa)->sin_addr);
1342#endif
1343		    break;
1344		}
1345#endif /* INET */
1346
1347#ifdef NS
1348		/*
1349		 * This magic copied from if_is.c; I don't use XNS,
1350		 * so I have no way of telling if this actually
1351		 * works or not.
1352		 */
1353		case AF_NS: {
1354		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1355		    if (ns_nullhost(*ina)) {
1356			ina->x_host = *(union ns_host *)(sc->tulip_ac.ac_enaddr);
1357		    } else {
1358			ifp->if_flags &= ~IFF_RUNNING;
1359			bcopy((caddr_t)ina->x_host.c_host,
1360			      (caddr_t)sc->tulip_ac.ac_enaddr,
1361			      sizeof sc->tulip_ac.ac_enaddr);
1362		    }
1363
1364		    tulip_init(sc);
1365		    break;
1366		}
1367#endif /* NS */
1368
1369		default: {
1370		    tulip_init(sc);
1371		    break;
1372		}
1373	    }
1374	    break;
1375	}
1376
1377	case SIOCSIFFLAGS: {
1378	    /*
1379	     * Changing the connection forces a reset.
1380	     */
1381	    if (sc->tulip_flags & TULIP_ALTPHYS) {
1382		if ((ifp->if_flags & IFF_ALTPHYS) == 0)
1383		    tulip_reset(sc);
1384	    } else {
1385		if (ifp->if_flags & IFF_ALTPHYS)
1386		    tulip_reset(sc);
1387	    }
1388	    tulip_init(sc);
1389	    break;
1390	}
1391
1392	case SIOCADDMULTI:
1393	case SIOCDELMULTI: {
1394	    /*
1395	     * Update multicast listeners
1396	     */
1397	    if (cmd == SIOCADDMULTI)
1398		error = ether_addmulti(ifr, &sc->tulip_ac);
1399	    else
1400		error = ether_delmulti(ifr, &sc->tulip_ac);
1401
1402	    if (error == ENETRESET) {
1403		tulip_addr_filter(sc);		/* reset multicast filtering */
1404		tulip_init(sc);
1405		error = 0;
1406	    }
1407	    break;
1408	}
1409#if defined(SIOCSIFMTU)
1410#if !defined(ifr_mtu)
1411#define ifr_mtu ifr_metric
1412#endif
1413	case SIOCSIFMTU:
1414	    /*
1415	     * Set the interface MTU.
1416	     */
1417	    if (ifr->ifr_mtu > ETHERMTU) {
1418		error = EINVAL;
1419	    } else {
1420		ifp->if_mtu = ifr->ifr_mtu;
1421	    }
1422	    break;
1423#endif
1424
1425	default: {
1426	    error = EINVAL;
1427	    break;
1428	}
1429    }
1430
1431    splx(s);
1432    return error;
1433}
1434
1435static void
1436tulip_attach(
1437    tulip_softc_t * const sc)
1438{
1439    struct ifnet * const ifp = &sc->tulip_if;
1440
1441    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
1442    ifp->if_ioctl = tulip_ioctl;
1443    ifp->if_output = ether_output;
1444    ifp->if_start = tulip_start;
1445
1446#ifdef __FreeBSD__
1447    printf("%s%d", sc->tulip_name, sc->tulip_unit);
1448#endif
1449    printf(": %s%s pass %d.%d Ethernet address %s\n",
1450	   sc->tulip_boardsw->bd_description,
1451	   tulip_chipdescs[sc->tulip_chipid],
1452	   (sc->tulip_revinfo & 0xF0) >> 4,
1453	   sc->tulip_revinfo & 0x0F,
1454	   ether_sprintf(sc->tulip_hwaddr));
1455
1456    if ((*sc->tulip_boardsw->bd_media_probe)(sc)) {
1457	ifp->if_flags |= IFF_ALTPHYS;
1458    } else {
1459	sc->tulip_flags |= TULIP_ALTPHYS;
1460    }
1461
1462    tulip_reset(sc);
1463
1464    if_attach(ifp);
1465#if defined(__NetBSD__)
1466    ether_ifattach(ifp);
1467#endif
1468
1469#if NBPFILTER > 0
1470    bpfattach(&sc->tulip_bpf, ifp, DLT_EN10MB, sizeof(struct ether_header));
1471#endif
1472}
1473
1474static void
1475tulip_initcsrs(
1476    tulip_softc_t * const sc,
1477    volatile tulip_uint32_t *va_csrs,
1478    size_t csr_size)
1479{
1480    sc->tulip_csrs.csr_busmode		= va_csrs +  0 * csr_size;
1481    sc->tulip_csrs.csr_txpoll		= va_csrs +  1 * csr_size;
1482    sc->tulip_csrs.csr_rxpoll		= va_csrs +  2 * csr_size;
1483    sc->tulip_csrs.csr_rxlist		= va_csrs +  3 * csr_size;
1484    sc->tulip_csrs.csr_txlist		= va_csrs +  4 * csr_size;
1485    sc->tulip_csrs.csr_status		= va_csrs +  5 * csr_size;
1486    sc->tulip_csrs.csr_command		= va_csrs +  6 * csr_size;
1487    sc->tulip_csrs.csr_intr		= va_csrs +  7 * csr_size;
1488    sc->tulip_csrs.csr_missed_frame	= va_csrs +  8 * csr_size;
1489    if (sc->tulip_chipid == TULIP_DC21040) {
1490	sc->tulip_csrs.csr_enetrom		= (tulip_sint32_t *) va_csrs +  9 * csr_size;
1491	sc->tulip_csrs.csr_reserved		= va_csrs + 10 * csr_size;
1492	sc->tulip_csrs.csr_full_duplex		= va_csrs + 11 * csr_size;
1493	sc->tulip_csrs.csr_sia_status		= va_csrs + 12 * csr_size;
1494	sc->tulip_csrs.csr_sia_connectivity	= va_csrs + 13 * csr_size;
1495	sc->tulip_csrs.csr_sia_tx_rx 		= va_csrs + 14 * csr_size;
1496	sc->tulip_csrs.csr_sia_general		= va_csrs + 15 * csr_size;
1497    } else if (sc->tulip_chipid == TULIP_DC21140 || sc->tulip_chipid == TULIP_DC21041) {
1498	sc->tulip_csrs.csr_srom_mii	= va_csrs +  9 * csr_size;
1499	sc->tulip_csrs.csr_gp_timer	= va_csrs + 11 * csr_size;
1500	sc->tulip_csrs.csr_gp		= va_csrs + 12 * csr_size;
1501	sc->tulip_csrs.csr_watchdog	= va_csrs + 15 * csr_size;
1502    }
1503}
1504
1505static void
1506tulip_initring(
1507    tulip_softc_t * const sc,
1508    tulip_ringinfo_t * const ri,
1509    tulip_desc_t *descs,
1510    int ndescs)
1511{
1512    ri->ri_max = ndescs;
1513    ri->ri_first = descs;
1514    ri->ri_last = ri->ri_first + ri->ri_max;
1515    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
1516    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
1517}
1518
1519/*
1520 * This is the PCI configuration support.  Since the DC21040 is available
1521 * on both EISA and PCI boards, one must be careful in how defines the
1522 * DC21040 in the config file.
1523 */
1524
1525#define	PCI_CFID	0x00	/* Configuration ID */
1526#define	PCI_CFCS	0x04	/* Configurtion Command/Status */
1527#define	PCI_CFRV	0x08	/* Configuration Revision */
1528#define	PCI_CFLT	0x0c	/* Configuration Latency Timer */
1529#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
1530#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
1531#define	PCI_CFIT	0x3c	/* Configuration Interrupt */
1532#define	PCI_CFDA	0x40	/* Configuration Driver Area */
1533
1534#if defined(__alpha__)
1535#define	TULIP_PCI_CSRSIZE	(256 / sizeof(tulip_uint32_t))
1536#define	TULIP_PCI_CSROFFSET	(24 / sizeof(tulip_uint32_t))
1537#elif defined(__i386__)
1538#define	TULIP_PCI_CSRSIZE	(8 / sizeof(tulip_uint32_t))
1539#define	TULIP_PCI_CSROFFSET	0
1540#endif
1541
1542#if defined(__FreeBSD__)
1543
1544#define	TULIP_PCI_ATTACH_ARGS	pcici_t config_id, int unit
1545
1546static int
1547tulip_pci_shutdown(
1548    struct kern_devconf * const kdc,
1549    int force)
1550{
1551    if (kdc->kdc_unit < NDE) {
1552	tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(kdc->kdc_unit);
1553	TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1554	DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
1555			   33MHz that comes to two microseconds but wait a
1556			   bit longer anyways) */
1557    }
1558    (void) dev_detach(kdc);
1559    return 0;
1560}
1561
1562static char*
1563tulip_pci_probe(
1564    pcici_t config_id,
1565    pcidi_t device_id)
1566{
1567    if (device_id == 0x00021011ul)
1568	return "Digital DC21040 Ethernet";
1569    if (device_id == 0x00141011ul)
1570	return "Digital DC21041 Ethernet";
1571    if (device_id == 0x00091011ul)
1572	return "Digital DC21140 Fast Ethernet";
1573    return NULL;
1574}
1575
1576static void  tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
1577static u_long tulip_pci_count;
1578
1579struct pci_device dedevice = {
1580    "de",
1581    tulip_pci_probe,
1582    tulip_pci_attach,
1583   &tulip_pci_count,
1584    tulip_pci_shutdown,
1585};
1586
1587DATA_SET (pcidevice_set, dedevice);
1588#endif /* __FreeBSD__ */
1589
1590#if defined(__bsdi__)
1591#define	TULIP_PCI_ATTACH_ARGS	struct device *parent, struct device *self, void *aux
1592
1593static void
1594tulip_pci_shutdown(
1595    void *arg)
1596{
1597    tulip_softc_t * const sc = (tulip_softc_t *) arg;
1598    TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1599    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
1600			   33MHz that comes to two microseconds but wait a
1601			   bit longer anyways) */
1602}
1603
1604static int
1605tulip_pci_match(
1606    pci_devaddr_t *pa)
1607{
1608    int irq;
1609    unsigned id;
1610
1611    id = pci_inl(pa, PCI_VENDOR_ID);
1612    if ((id & 0xFFFF) != 0x1011)
1613	return 0;
1614    id >>= 16;
1615    if (id != 2 && id != 9 && id != 0x14)
1616	return 0;
1617    irq = pci_inl(pa, PCI_I_PIN) & 0xFF;
1618    if (irq == 0 || irq >= 16)
1619	return 0;
1620
1621    return 1;
1622}
1623
1624static int
1625tulip_pci_probe(
1626    struct device *parent,
1627    struct cfdata *cf,
1628    void *aux)
1629{
1630    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
1631    unsigned irq;
1632    pci_devaddr_t *pa;
1633
1634    pa = pci_scan(tulip_pci_match);
1635    if (pa == NULL)
1636	return 0;
1637
1638    irq = (1 << (pci_inl(pa, PCI_I_PIN) & 0xFF));
1639
1640    if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
1641	printf("fpa%d: error: desired IRQ of %d does not match device's actual IRQ of %d,\n",
1642	       cf->cf_unit,
1643	       ffs(ia->ia_irq) - 1, ffs(irq) - 1);
1644	return 0;
1645    }
1646    if (ia->ia_irq == IRQUNK) {
1647	if ((irq = isa_irqalloc(irq)) == 0)
1648	    return 0;
1649	ia->ia_irq = irq;
1650    }
1651
1652    /* PCI bus masters don't use host DMA channels */
1653    ia->ia_drq = DRQNONE;
1654
1655    /* Get the memory base address; assume the BIOS set it up correctly */
1656    ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
1657    pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
1658    ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
1659    pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
1660
1661    /* Disable I/O space access */
1662    pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
1663    ia->ia_iobase = 0;
1664    ia->ia_iosize = 0;
1665
1666    ia->ia_aux = (void *) pa;
1667    return 1;
1668}
1669
1670static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
1671
1672struct cfdriver decd = {
1673    0, "de", tulip_pci_probe, tulip_pci_attach, DV_IFNET, sizeof(tulip_softc_t)
1674};
1675
1676#endif /* __bsdi__ */
1677
1678#if defined(__NetBSD__)
1679#define	TULIP_PCI_ATTACH_ARGS	struct device *parent, struct device *self, void *aux
1680
1681#if 0						/* XXX! */
1682static void
1683tulip_pci_shutdown(
1684    void *arg)
1685{
1686    tulip_softc_t * const sc = (tulip_softc_t *) arg;
1687    TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1688    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
1689			   33MHz that comes to two microseconds but wait a
1690			   bit longer anyways) */
1691}
1692#endif
1693
1694static int
1695tulip_pci_probe(
1696    struct device *parent,
1697    void *match,
1698    void *aux)
1699{
1700    struct pci_attach_args *pa = (struct pci_attach_args *) aux;
1701
1702    if (pa->pa_id == 0x00021011ul
1703	    || pa->pa_id == 0x00141011ul
1704	    || pa->pa_id == 0x00091011ul)
1705	return 1;
1706
1707    return 0;
1708}
1709
1710static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
1711
1712struct cfdriver decd = {
1713    0, "de", tulip_pci_probe, tulip_pci_attach, DV_IFNET, sizeof(tulip_softc_t)
1714};
1715
1716#endif /* __NetBSD__ */
1717
1718static void
1719tulip_pci_attach(
1720    TULIP_PCI_ATTACH_ARGS)
1721{
1722#if defined(__FreeBSD__)
1723    tulip_softc_t *sc;
1724#endif
1725#if defined(__bsdi__)
1726    tulip_softc_t * const sc = (tulip_softc_t *) self;
1727    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
1728    pci_devaddr_t *pa = (pci_devaddr_t *) ia->ia_aux;
1729    int unit = sc->tulip_dev.dv_unit;
1730#endif
1731#if defined(__NetBSD__)
1732    tulip_softc_t * const sc = (tulip_softc_t *) self;
1733    struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
1734    int unit = sc->tulip_dev.dv_unit;
1735#endif
1736    int retval, idx, revinfo, id;
1737    vm_offset_t va_csrs, pa_csrs;
1738    tulip_desc_t *rxdescs, *txdescs;
1739    tulip_chipid_t chipid;
1740
1741#if defined(__FreeBSD__)
1742    if (unit >= NDE) {
1743	printf("de%d: not configured; kernel is built for only %d device%s.\n",
1744	       unit, NDE, NDE == 1 ? "" : "s");
1745	return;
1746    }
1747#endif
1748
1749#if defined(__FreeBSD__)
1750    revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
1751    id = pci_conf_read(config_id, PCI_CFID);
1752#endif
1753#if defined(__bsdi__)
1754    revinfo = pci_inl(pa, PCI_CFRV) & 0xFF;
1755    id = pci_inl(pa, PCI_CFID);
1756#endif
1757#if defined(__NetBSD__)
1758    revinfo = pci_conf_read(pa->pa_tag, PCI_CFRV) & 0xFF;
1759    id = pa->pa_id;
1760#endif
1761
1762    if (id == 0x00021011ul) chipid = TULIP_DC21040;
1763    else if (id == 0x00091011) chipid = TULIP_DC21140;
1764    else if (id == 0x00141011) chipid = TULIP_DC21041;
1765    else return;
1766
1767    if (chipid == TULIP_DC21040 && revinfo < 0x20) {
1768#ifdef __FreeBSD__
1769	printf("de%d", unit);
1770#endif
1771	printf(": not configured; DC21040 pass 2.0 required (%d.%d found)\n",
1772	       revinfo >> 4, revinfo & 0x0f);
1773	return;
1774    } else if (chipid == TULIP_DC21140 && revinfo < 0x11) {
1775#ifdef __FreeBSD__
1776	printf("de%d", unit);
1777#endif
1778	printf(": not configured; DC21140 pass 1.1 required (%d.%d found)\n",
1779	       revinfo >> 4, revinfo & 0x0f);
1780	return;
1781    }
1782
1783#if defined(__FreeBSD__)
1784    sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
1785    if (sc == NULL)
1786	return;
1787    bzero(sc, sizeof(*sc));				/* Zero out the softc*/
1788#endif
1789
1790    rxdescs = (tulip_desc_t *)
1791	malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
1792    if (rxdescs == NULL) {
1793#if defined(__FreeBSD__)
1794	free((caddr_t) sc, M_DEVBUF);
1795#endif
1796	return;
1797    }
1798
1799    txdescs = (tulip_desc_t *)
1800	malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
1801    if (txdescs == NULL) {
1802	free((caddr_t) rxdescs, M_DEVBUF);
1803#if defined(__FreeBSD__)
1804	free((caddr_t) sc, M_DEVBUF);
1805#endif
1806	return;
1807    }
1808
1809    sc->tulip_chipid = chipid;
1810    sc->tulip_unit = unit;
1811    sc->tulip_name = "de";
1812#if defined(__FreeBSD__)
1813    retval = pci_map_mem(config_id, PCI_CBMA, &va_csrs, &pa_csrs);
1814    if (!retval) {
1815	free((caddr_t) txdescs, M_DEVBUF);
1816	free((caddr_t) rxdescs, M_DEVBUF);
1817	free((caddr_t) sc, M_DEVBUF);
1818	return;
1819    }
1820    tulips[unit] = sc;
1821#endif
1822#if defined(__bsdi__)
1823    va_csrs = (vm_offset_t) mapphys((vm_offset_t) ia->ia_maddr, ia->ia_msize);
1824#endif
1825#if defined(__NetBSD__)
1826    if (pci_map_mem(pa->pa_tag, PCI_CBMA, &va_csrs, &pa_csrs)) {
1827	free((caddr_t) txdescs, M_DEVBUF);
1828	free((caddr_t) rxdescs, M_DEVBUF);
1829	return;
1830    }
1831#endif
1832    sc->tulip_revinfo = revinfo;
1833    tulip_initcsrs(sc, ((volatile tulip_uint32_t *) va_csrs) + TULIP_PCI_CSROFFSET,
1834		   TULIP_PCI_CSRSIZE);
1835    tulip_initring(sc, &sc->tulip_rxinfo, rxdescs, TULIP_RXDESCS);
1836    tulip_initring(sc, &sc->tulip_txinfo, txdescs, TULIP_TXDESCS);
1837    if ((retval = tulip_read_macaddr(sc)) < 0) {
1838#ifdef __FreeBSD__
1839	printf("%s%d", sc->tulip_name, sc->tulip_unit);
1840#endif
1841	printf(": can't read ENET ROM (why=%d) (", retval);
1842	for (idx = 0; idx < 32; idx++)
1843	    printf("%02x", sc->tulip_rombuf[idx]);
1844	printf("\n");
1845	printf("%s%d: %s%s pass %d.%d Ethernet address %s\n",
1846	       sc->tulip_name, sc->tulip_unit,
1847	       (sc->tulip_boardsw != NULL ? sc->tulip_boardsw->bd_description : ""),
1848	       tulip_chipdescs[sc->tulip_chipid],
1849	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F,
1850	       "unknown");
1851    } else {
1852	tulip_reset(sc);
1853	tulip_attach(sc);
1854#if defined(__NetBSD__)
1855	sc->tulip_ih = pci_map_int(pa->pa_tag, PCI_IPL_NET, tulip_intr, sc);
1856	if (sc->tulip_ih == NULL) {
1857	    printf("%s%d: couldn't map interrupt\n",
1858		   sc->tulip_name, sc->tulip_unit);
1859	    return;
1860	}
1861#endif
1862#if defined(__FreeBSD__)
1863	pci_map_int (config_id, tulip_intr, (void*) sc, &net_imask);
1864#endif
1865#if defined(__bsdi__)
1866	isa_establish(&sc->tulip_id, &sc->tulip_dev);
1867
1868	sc->tulip_ih.ih_fun = tulip_intr;
1869	sc->tulip_ih.ih_arg = (void *)sc;
1870	intr_establish(ia->ia_irq, &sc->tulip_ih, DV_NET);
1871
1872	sc->tulip_ats.func = tulip_pci_shutdown;
1873	sc->tulip_ats.arg = (void *) sc;
1874	atshutdown(&sc->tulip_ats, ATSH_ADD);
1875#endif
1876    }
1877}
1878#endif /* NDE > 0 */
1879