if_de.c revision 13937
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.42 1996/01/26 09:29:26 phk 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, DC21041, 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_param.h>
86#include <vm/pmap.h>
87#include <vm/vm_kern.h>
88
89#if defined(__FreeBSD__)
90#include <pci.h>
91#if NPCI > 0
92#include <pci/pcivar.h>
93#include <pci/dc21040.h>
94#endif
95#endif /* __FreeBSD__ */
96
97#if defined(__bsdi__)
98#include <i386/pci/pci.h>
99#include <i386/pci/ic/dc21040.h>
100#include <i386/isa/isa.h>
101#include <i386/isa/icu.h>
102#include <i386/isa/dma.h>
103#include <i386/isa/isavar.h>
104#include <eisa.h>
105#if NEISA > 0
106#include <i386/eisa/eisa.h>
107#define	TULIP_EISA
108#endif
109#endif /* __bsdi__ */
110
111#if defined(__NetBSD__)
112#include <dev/pci/pcivar.h>
113#include <dev/ic/dc21040reg.h>
114#if defined(__i386__)
115#include <i386/isa/isa_machdep.h>
116#endif
117#endif /* __NetBSD__ */
118
119/*
120 * Intel CPUs should use I/O mapped access.
121 *    (NetBSD doesn't support it yet)
122 */
123#if defined(__i386__) && !defined(__NetBSD__)
124#define	TULIP_IOMAPPED
125#endif
126
127/*
128 * This module supports
129 *	the DEC DC21040 PCI Ethernet Controller.
130 *	the DEC DC21041 PCI Ethernet Controller.
131 *	the DEC DC21140 PCI Fast Ethernet Controller.
132 */
133
134typedef struct {
135    tulip_desc_t *ri_first;
136    tulip_desc_t *ri_last;
137    tulip_desc_t *ri_nextin;
138    tulip_desc_t *ri_nextout;
139    int ri_max;
140    int ri_free;
141} tulip_ringinfo_t;
142
143#ifdef TULIP_IOMAPPED
144typedef tulip_uint16_t tulip_csrptr_t;
145
146#define	TULIP_EISA_CSRSIZE	16
147#define	TULIP_EISA_CSROFFSET	0
148#define	TULIP_PCI_CSRSIZE	8
149#define	TULIP_PCI_CSROFFSET	0
150
151#define	TULIP_READ_CSR(sc, csr)			(inl((sc)->tulip_csrs.csr))
152#define	TULIP_WRITE_CSR(sc, csr, val)   	outl((sc)->tulip_csrs.csr, val)
153
154#define	TULIP_READ_CSRBYTE(sc, csr)		(inb((sc)->tulip_csrs.csr))
155#define	TULIP_WRITE_CSRBYTE(sc, csr, val)	outb((sc)->tulip_csrs.csr, val)
156
157#else /* TULIP_IOMAPPED */
158
159typedef volatile tulip_uint32_t *tulip_csrptr_t;
160
161#if defined(__alpha__)
162#define	TULIP_PCI_CSRSIZE	(256 / sizeof(tulip_uint32_t))
163#define	TULIP_PCI_CSROFFSET	(24 / sizeof(tulip_uint32_t))
164#elif defined(__i386__)
165#define	TULIP_PCI_CSRSIZE	(8 / sizeof(tulip_uint32_t))
166#define	TULIP_PCI_CSROFFSET	0
167#endif
168
169/*
170 * macros to read and write CSRs.  Note that the "0 +" in
171 * READ_CSR is to prevent the macro from being an lvalue
172 * and WRITE_CSR shouldn't be assigned from.
173 */
174#define	TULIP_READ_CSR(sc, csr)		(0 + *(sc)->tulip_csrs.csr)
175#ifndef __alpha__
176#define	TULIP_WRITE_CSR(sc, csr, val) \
177	    ((void)(*(sc)->tulip_csrs.csr = (val)))
178#else
179#define	TULIP_WRITE_CSR(sc, csr, val) \
180	    ((void)(*(sc)->tulip_csrs.csr = (val), MB()))
181#endif
182
183#endif /* TULIP_IOMAPPED */
184
185typedef struct {
186    tulip_csrptr_t csr_busmode;			/* CSR0 */
187    tulip_csrptr_t csr_txpoll;			/* CSR1 */
188    tulip_csrptr_t csr_rxpoll;			/* CSR2 */
189    tulip_csrptr_t csr_rxlist;			/* CSR3 */
190    tulip_csrptr_t csr_txlist;			/* CSR4 */
191    tulip_csrptr_t csr_status;			/* CSR5 */
192    tulip_csrptr_t csr_command;			/* CSR6 */
193    tulip_csrptr_t csr_intr;			/* CSR7 */
194    tulip_csrptr_t csr_missed_frame;		/* CSR8 */
195
196    /* DC21040 specific registers */
197
198    tulip_csrptr_t csr_enetrom;			/* CSR9 */
199    tulip_csrptr_t csr_reserved;		/* CSR10 */
200    tulip_csrptr_t csr_full_duplex;		/* CSR11 */
201
202    /* DC21040/DC21041 common registers */
203
204    tulip_csrptr_t csr_sia_status;		/* CSR12 */
205    tulip_csrptr_t csr_sia_connectivity;	/* CSR13 */
206    tulip_csrptr_t csr_sia_tx_rx;		/* CSR14 */
207    tulip_csrptr_t csr_sia_general;		/* CSR15 */
208
209    /* DC21140/DC21041 common registers */
210
211    tulip_csrptr_t csr_srom_mii;		/* CSR9 */
212    tulip_csrptr_t csr_gp_timer;		/* CSR11 */
213
214    /* DC21140 specific registers */
215
216    tulip_csrptr_t csr_gp;			/* CSR12 */
217    tulip_csrptr_t csr_watchdog;		/* CSR15 */
218
219    /* DC21041 specific registers */
220
221    tulip_csrptr_t csr_bootrom;			/* CSR10 */
222} tulip_regfile_t;
223
224/*
225 * The DC21040 has a stupid restriction in that the receive
226 * buffers must be longword aligned.  But since Ethernet
227 * headers are not a multiple of longwords in size this forces
228 * the data to non-longword aligned.  Since IP requires the
229 * data to be longword aligned, we need to copy it after it has
230 * been DMA'ed in our memory.
231 *
232 * Since we have to copy it anyways, we might as well as allocate
233 * dedicated receive space for the input.  This allows to use a
234 * small receive buffer size and more ring entries to be able to
235 * better keep with a flood of tiny Ethernet packets.
236 *
237 * The receive space MUST ALWAYS be a multiple of the page size.
238 * And the number of receive descriptors multiplied by the size
239 * of the receive buffers must equal the recevive space.  This
240 * is so that we can manipulate the page tables so that even if a
241 * packet wraps around the end of the receive space, we can
242 * treat it as virtually contiguous.
243 *
244 * The above used to be true (the stupid restriction is still true)
245 * but we gone to directly DMA'ing into MBUFs because with 100Mb
246 * cards the copying is just too much of a hit.
247 */
248#if defined(__alpha__)
249#define	TULIP_COPY_RXDATA	1
250#endif
251
252#define	TULIP_RXDESCS		16
253#define	TULIP_TXDESCS		128
254#define	TULIP_RXQ_TARGET	8
255
256typedef enum {
257    TULIP_DC21040_GENERIC,
258    TULIP_DC21040_ZX314_MASTER,
259    TULIP_DC21040_ZX314_SLAVE,
260    TULIP_DC21140_DEC_EB,
261    TULIP_DC21140_DEC_DE500,
262    TULIP_DC21140_COGENT_EM100,
263    TULIP_DC21140_ZNYX_ZX34X,
264    TULIP_DC21041_GENERIC,
265    TULIP_DC21041_DE450
266} tulip_board_t;
267
268typedef struct _tulip_softc_t tulip_softc_t;
269
270typedef struct {
271    tulip_board_t bd_type;
272    const char *bd_description;
273    int (*bd_media_probe)(tulip_softc_t *sc);
274    void (*bd_media_select)(tulip_softc_t *sc);
275} tulip_boardsw_t;
276
277typedef enum {
278    TULIP_DC21040, TULIP_DC21140,
279    TULIP_DC21041, TULIP_DE425,
280    TULIP_CHIPID_UNKNOWN
281} tulip_chipid_t;
282
283typedef enum {
284    TULIP_PROBE_INACTIVE, TULIP_PROBE_10BASET, TULIP_PROBE_AUI,
285    TULIP_PROBE_BNC
286} tulip_probe_state_t;
287
288typedef enum {
289    TULIP_MEDIA_UNKNOWN, TULIP_MEDIA_10BASET,
290    TULIP_MEDIA_BNC, TULIP_MEDIA_AUI,
291    TULIP_MEDIA_BNCAUI, TULIP_MEDIA_100BASET
292} tulip_media_t;
293
294struct _tulip_softc_t {
295#if defined(__bsdi__)
296    struct device tulip_dev;		/* base device */
297    struct isadev tulip_id;		/* ISA device */
298    struct intrhand tulip_ih;		/* intrrupt vectoring */
299    struct atshutdown tulip_ats;	/* shutdown hook */
300#endif
301#if defined(__NetBSD__)
302    struct device tulip_dev;		/* base device */
303    void *tulip_ih;			/* intrrupt vectoring */
304    void *tulip_ats;			/* shutdown hook */
305#endif
306    struct arpcom tulip_ac;
307    tulip_regfile_t tulip_csrs;
308    unsigned tulip_flags;
309#define	TULIP_WANTSETUP		0x0001
310#define	TULIP_WANTHASH		0x0002
311#define	TULIP_DOINGSETUP	0x0004
312#define	TULIP_ALTPHYS		0x0008	/* use AUI */
313#define	TULIP_TXPROBE_ACTIVE	0x0010
314#define	TULIP_TXPROBE_OK	0x0020
315#define	TULIP_INRESET		0x0040
316#define	TULIP_WANTRXACT		0x0080
317#define	TULIP_SLAVEDROM		0x0100
318#define	TULIP_ROMOK		0x0200
319    unsigned char tulip_rombuf[128];
320    tulip_uint32_t tulip_setupbuf[192/sizeof(tulip_uint32_t)];
321    tulip_uint32_t tulip_setupdata[192/sizeof(tulip_uint32_t)];
322    tulip_uint32_t tulip_intrmask;
323    tulip_uint32_t tulip_cmdmode;
324    tulip_uint32_t tulip_revinfo;
325    tulip_uint32_t tulip_gpticks;
326    /* tulip_uint32_t tulip_bus; XXX */
327    tulip_media_t tulip_media;
328    tulip_probe_state_t tulip_probe_state;
329    tulip_chipid_t tulip_chipid;
330    const tulip_boardsw_t *tulip_boardsw;
331    tulip_softc_t *tulip_slaves;
332    struct ifqueue tulip_txq;
333    struct ifqueue tulip_rxq;
334    tulip_ringinfo_t tulip_rxinfo;
335    tulip_ringinfo_t tulip_txinfo;
336};
337
338#ifndef IFF_ALTPHYS
339#define	IFF_ALTPHYS	IFF_LINK0		/* In case it isn't defined */
340#endif
341static const char *tulip_chipdescs[] = {
342    "DC21040 [10Mb/s]",
343    "DC21140 [10-100Mb/s]",
344    "DC21041 [10Mb/s]",
345#if defined(TULIP_EISA)
346    "DE425 [10Mb/s]"
347#endif
348};
349
350#if defined(__FreeBSD__)
351typedef void ifnet_ret_t;
352typedef int ioctl_cmd_t;
353static tulip_softc_t *tulips[NDE];
354#define	TULIP_UNIT_TO_SOFTC(unit)	(tulips[unit])
355#endif
356#if defined(__bsdi__)
357typedef int ifnet_ret_t;
358typedef int ioctl_cmd_t;
359extern struct cfdriver decd;
360#define	TULIP_UNIT_TO_SOFTC(unit)	((tulip_softc_t *) decd.cd_devs[unit])
361#define	TULIP_BURSTSIZE(unit)		3
362#endif
363#if defined(__NetBSD__)
364typedef void ifnet_ret_t;
365typedef u_long ioctl_cmd_t;
366extern struct cfdriver decd;
367#define	TULIP_UNIT_TO_SOFTC(unit)	((tulip_softc_t *) decd.cd_devs[unit])
368#endif
369
370#ifndef TULIP_BURSTSIZE
371#define	TULIP_BURSTSIZE(unit)		3
372#endif
373
374#define	tulip_if	tulip_ac.ac_if
375#define	tulip_unit	tulip_ac.ac_if.if_unit
376#define	tulip_name	tulip_ac.ac_if.if_name
377#define	tulip_bpf	tulip_ac.ac_if.if_bpf
378#define	tulip_hwaddr	tulip_ac.ac_enaddr
379
380#define	TULIP_CRC32_POLY	0xEDB88320UL	/* CRC-32 Poly -- Little Endian */
381#define	TULIP_CHECK_RXCRC	0
382#define	TULIP_MAX_TXSEG		30
383
384#define	TULIP_ADDREQUAL(a1, a2) \
385	(((u_short *)a1)[0] == ((u_short *)a2)[0] \
386	 && ((u_short *)a1)[1] == ((u_short *)a2)[1] \
387	 && ((u_short *)a1)[2] == ((u_short *)a2)[2])
388#define	TULIP_ADDRBRDCST(a1) \
389	(((u_short *)a1)[0] == 0xFFFFU \
390	 && ((u_short *)a1)[1] == 0xFFFFU \
391	 && ((u_short *)a1)[2] == 0xFFFFU)
392
393static ifnet_ret_t tulip_start(struct ifnet *ifp);
394static void tulip_rx_intr(tulip_softc_t *sc);
395static void tulip_addr_filter(tulip_softc_t *sc);
396
397
398static int
399tulip_dc21040_media_probe(
400    tulip_softc_t * const sc)
401{
402    int cnt;
403
404    TULIP_WRITE_CSR(sc, csr_sia_connectivity, 0);
405    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
406    for (cnt = 0; cnt < 2400; cnt++) {
407	if ((TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
408	    break;
409	DELAY(1000);
410    }
411    return (TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) != 0;
412}
413
414static void
415tulip_dc21040_media_select(
416    tulip_softc_t * const sc)
417{
418    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
419	|TULIP_CMD_BACKOFFCTR;
420    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
421    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
422	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
423	    printf("%s%d: enabling Thinwire/AUI port\n",
424		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
425	TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_AUI);
426	sc->tulip_flags |= TULIP_ALTPHYS;
427    } else {
428	if (sc->tulip_flags & TULIP_ALTPHYS)
429	    printf("%s%d: enabling 10baseT/UTP port\n",
430		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
431	TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
432	sc->tulip_flags &= ~TULIP_ALTPHYS;
433	sc->tulip_media = TULIP_MEDIA_10BASET;
434    }
435}
436
437static const tulip_boardsw_t tulip_dc21040_boardsw = {
438    TULIP_DC21040_GENERIC,
439    "",
440    tulip_dc21040_media_probe,
441    tulip_dc21040_media_select
442};
443
444static int
445tulip_zx314_media_probe(
446    tulip_softc_t * const sc)
447{
448    TULIP_WRITE_CSR(sc, csr_sia_connectivity, 0);
449    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
450    return 0;
451}
452
453static void
454tulip_zx314_media_select(
455    tulip_softc_t * const sc)
456{
457    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
458	|TULIP_CMD_BACKOFFCTR;
459    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
460    if (sc->tulip_flags & TULIP_ALTPHYS)
461	printf("%s%d: enabling 10baseT/UTP port\n",
462	       sc->tulip_if.if_name, sc->tulip_if.if_unit);
463    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
464    sc->tulip_flags &= ~TULIP_ALTPHYS;
465    sc->tulip_media = TULIP_MEDIA_10BASET;
466}
467
468
469static const tulip_boardsw_t tulip_dc21040_zx314_master_boardsw = {
470    TULIP_DC21040_ZX314_MASTER,
471    "ZNYX ZX314 ",
472    tulip_zx314_media_probe,
473    tulip_zx314_media_select
474};
475
476static const tulip_boardsw_t tulip_dc21040_zx314_slave_boardsw = {
477    TULIP_DC21040_ZX314_SLAVE,
478    "ZNYX ZX314 ",
479    tulip_zx314_media_probe,
480    tulip_zx314_media_select
481};
482
483static int
484tulip_dc21140_evalboard_media_probe(
485    tulip_softc_t * const sc)
486{
487    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_PINS);
488    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_INIT);
489    TULIP_WRITE_CSR(sc, csr_command,
490	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
491	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
492    TULIP_WRITE_CSR(sc, csr_command,
493	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
494    DELAY(1000000);
495    return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_EB_OK100) != 0;
496}
497
498static void
499tulip_dc21140_evalboard_media_select(
500    tulip_softc_t * const sc)
501{
502    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
503	|TULIP_CMD_BACKOFFCTR;
504    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_PINS);
505    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EB_INIT);
506    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
507	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
508	    printf("%s%d: enabling 100baseTX UTP port\n",
509		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
510	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
511	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
512	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
513	sc->tulip_flags |= TULIP_ALTPHYS;
514	sc->tulip_media = TULIP_MEDIA_100BASET;
515    } else {
516	if (sc->tulip_flags & TULIP_ALTPHYS)
517	    printf("%s%d: enabling 10baseT UTP port\n",
518		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
519	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
520			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
521	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
522	sc->tulip_flags &= ~TULIP_ALTPHYS;
523	sc->tulip_media = TULIP_MEDIA_10BASET;
524    }
525}
526
527static const tulip_boardsw_t tulip_dc21140_eb_boardsw = {
528    TULIP_DC21140_DEC_EB,
529    "",
530    tulip_dc21140_evalboard_media_probe,
531    tulip_dc21140_evalboard_media_select
532};
533
534static int
535tulip_dc21140_cogent_em100_media_probe(
536    tulip_softc_t * const sc)
537{
538    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_PINS);
539    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_INIT);
540    TULIP_WRITE_CSR(sc, csr_command,
541	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
542	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
543    TULIP_WRITE_CSR(sc, csr_command,
544	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
545    return 1;
546}
547
548static void
549tulip_dc21140_cogent_em100_media_select(
550    tulip_softc_t * const sc)
551{
552    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
553	|TULIP_CMD_BACKOFFCTR;
554    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_PINS);
555    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_EM100_INIT);
556    if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
557	printf("%s%d: enabling 100baseTX UTP port\n",
558	       sc->tulip_if.if_name, sc->tulip_if.if_unit);
559    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
560	|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
561    sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
562    sc->tulip_flags |= TULIP_ALTPHYS;
563    sc->tulip_media = TULIP_MEDIA_100BASET;
564}
565
566static const tulip_boardsw_t tulip_dc21140_cogent_em100_boardsw = {
567    TULIP_DC21140_COGENT_EM100,
568    "Cogent EM100 ",
569    tulip_dc21140_cogent_em100_media_probe,
570    tulip_dc21140_cogent_em100_media_select
571};
572
573
574static int
575tulip_dc21140_znyx_zx34x_media_probe(
576    tulip_softc_t * const sc)
577{
578    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_ZX34X_PINS);
579    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_ZX34X_INIT);
580    TULIP_WRITE_CSR(sc, csr_command,
581	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
582	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
583    TULIP_WRITE_CSR(sc, csr_command,
584	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
585    DELAY(1000000);
586
587    return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_ZX34X_OK10);
588}
589
590static void
591tulip_dc21140_znyx_zx34x_media_select(
592    tulip_softc_t * const sc)
593{
594    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
595	|TULIP_CMD_BACKOFFCTR;
596    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_ZX34X_PINS);
597    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_ZX34X_INIT);
598    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
599	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
600	    printf("%s%d: enabling 100baseTX UTP port\n",
601		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
602	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
603	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
604	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
605	sc->tulip_flags |= TULIP_ALTPHYS;
606	sc->tulip_media = TULIP_MEDIA_100BASET;
607    } else {
608	if (sc->tulip_flags & TULIP_ALTPHYS)
609	    printf("%s%d: enabling 10baseT UTP port\n",
610		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
611	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
612			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
613	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
614	sc->tulip_flags &= ~TULIP_ALTPHYS;
615	sc->tulip_media = TULIP_MEDIA_10BASET;
616    }
617}
618
619static const tulip_boardsw_t tulip_dc21140_znyx_zx34x_boardsw = {
620    TULIP_DC21140_ZNYX_ZX34X,
621    "ZNYX ZX34X ",
622    tulip_dc21140_znyx_zx34x_media_probe,
623    tulip_dc21140_znyx_zx34x_media_select
624};
625
626static int
627tulip_dc21140_de500_media_probe(
628    tulip_softc_t * const sc)
629{
630    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_PINS);
631    DELAY(1000);
632    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_HALFDUPLEX);
633    if ((TULIP_READ_CSR(sc, csr_gp) &
634	(TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10)) !=
635	(TULIP_GP_DE500_NOTOK_100|TULIP_GP_DE500_NOTOK_10))
636	return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_DE500_NOTOK_100) != 0;
637    TULIP_WRITE_CSR(sc, csr_gp,
638	TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100);
639    TULIP_WRITE_CSR(sc, csr_command,
640	TULIP_READ_CSR(sc, csr_command) | TULIP_CMD_PORTSELECT |
641	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
642    TULIP_WRITE_CSR(sc, csr_command,
643	TULIP_READ_CSR(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
644    DELAY(1000000);
645    return (TULIP_READ_CSR(sc, csr_gp) & TULIP_GP_DE500_NOTOK_100) != 0;
646}
647
648static void
649tulip_dc21140_de500_media_select(
650    tulip_softc_t * const sc)
651{
652    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
653	|TULIP_CMD_BACKOFFCTR;
654    TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_PINS);
655    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
656	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
657	    printf("%s%d: enabling 100baseTX UTP port\n",
658		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
659	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
660	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
661	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
662	sc->tulip_flags |= TULIP_ALTPHYS;
663	sc->tulip_media = TULIP_MEDIA_100BASET;
664	TULIP_WRITE_CSR(sc, csr_gp,
665	    TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100);
666    } else {
667	if (sc->tulip_flags & TULIP_ALTPHYS)
668	    printf("%s%d: enabling 10baseT UTP port\n",
669		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
670	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
671			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
672	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
673	sc->tulip_flags &= ~TULIP_ALTPHYS;
674	sc->tulip_media = TULIP_MEDIA_10BASET;
675	TULIP_WRITE_CSR(sc, csr_gp, TULIP_GP_DE500_HALFDUPLEX);
676    }
677}
678
679static const tulip_boardsw_t tulip_dc21140_de500_boardsw = {
680    TULIP_DC21140_DEC_DE500, "Digital DE500 ",
681    tulip_dc21140_de500_media_probe,
682    tulip_dc21140_de500_media_select
683};
684
685static int
686tulip_dc21041_media_probe(
687    tulip_softc_t * const sc)
688{
689    return 0;
690}
691
692static void
693tulip_dc21041_media_select(
694    tulip_softc_t * const sc)
695{
696    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
697	/* |TULIP_CMD_FULLDUPLEX */ |TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
698    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_GPTIMEOUT
699	|TULIP_STS_ABNRMLINTR|TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
700    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
701	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0) {
702	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
703	    sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE);
704	    sc->tulip_flags |= TULIP_ALTPHYS|TULIP_WANTRXACT;
705	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
706	}
707    } else {
708	if (sc->tulip_flags & TULIP_ALTPHYS) {
709	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
710	    sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE|TULIP_ALTPHYS);
711	    sc->tulip_flags |= TULIP_WANTRXACT;
712	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
713	}
714    }
715
716    if (TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) {
717	if (sc->tulip_media == TULIP_MEDIA_10BASET) {
718	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
719	} else if (sc->tulip_media == TULIP_MEDIA_BNC) {
720	    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
721	    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
722	    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_BNC);
723	    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_BNC);
724	    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_BNC);
725	    return;
726	} else if (sc->tulip_media == TULIP_MEDIA_AUI) {
727	    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
728	    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
729	    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_AUI);
730	    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_AUI);
731	    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_AUI);
732	    return;
733	}
734
735	switch (sc->tulip_probe_state) {
736	    case TULIP_PROBE_INACTIVE: {
737		TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
738		sc->tulip_if.if_flags |= IFF_OACTIVE;
739		sc->tulip_gpticks = 200;
740		sc->tulip_probe_state = TULIP_PROBE_10BASET;
741		TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
742		TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_10BASET);
743		TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_10BASET);
744		TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_10BASET);
745		TULIP_WRITE_CSR(sc, csr_gp_timer, 12000000 / 204800); /* 120 ms */
746		break;
747	    }
748	    case TULIP_PROBE_10BASET: {
749		if (--sc->tulip_gpticks > 0) {
750		    if ((TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
751			TULIP_WRITE_CSR(sc, csr_gp_timer, 12000000 / 204800); /* 120 ms */
752			TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
753			break;
754		    }
755		}
756		sc->tulip_gpticks = 4;
757		if (TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_OTHERRXACTIVITY) {
758		    sc->tulip_probe_state = TULIP_PROBE_BNC;
759		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
760		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_BNC);
761		    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_BNC);
762		    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_BNC);
763		    TULIP_WRITE_CSR(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
764		} else {
765		    sc->tulip_probe_state = TULIP_PROBE_AUI;
766		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
767		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_AUI);
768		    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_AUI);
769		    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_AUI);
770		    TULIP_WRITE_CSR(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
771		}
772		break;
773	    }
774	    case TULIP_PROBE_BNC:
775	    case TULIP_PROBE_AUI: {
776		if (sc->tulip_flags & TULIP_TXPROBE_OK) {
777		    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
778		    sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE);
779		    TULIP_WRITE_CSR(sc, csr_gp_timer, 0); /* disable */
780		    if ((sc->tulip_probe_state == TULIP_PROBE_AUI
781			 && sc->tulip_media != TULIP_MEDIA_AUI)
782			|| (sc->tulip_probe_state == TULIP_PROBE_BNC
783			    && sc->tulip_media != TULIP_MEDIA_AUI)) {
784			printf("%s%d: enabling %s port\n",
785			       sc->tulip_if.if_name, sc->tulip_if.if_unit,
786			       sc->tulip_probe_state == TULIP_PROBE_BNC
787			           ? "Thinwire/BNC" : "AUI");
788			if (sc->tulip_probe_state == TULIP_PROBE_AUI)
789			    sc->tulip_media = TULIP_MEDIA_AUI;
790			else if (sc->tulip_probe_state == TULIP_PROBE_BNC)
791			    sc->tulip_media = TULIP_MEDIA_BNC;
792		    }
793		    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
794		    break;
795		}
796		if ((sc->tulip_flags & TULIP_WANTRXACT) == 0
797		    || (TULIP_READ_CSR(sc, csr_sia_status) & TULIP_SIASTS_RXACTIVITY)) {
798		    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0) {
799			struct mbuf *m;
800			/*
801			 * Before we are sure this is the right media we need
802			 * to send a small packet to make sure there's carrier.
803			 * Strangely, BNC and AUI will "see" receive data if
804			 * either is connected so the transmit is the only way
805			 * to verify the connectivity.
806			 */
807			MGETHDR(m, M_DONTWAIT, MT_DATA);
808			if (m == NULL) {
809			    TULIP_WRITE_CSR(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
810			    break;
811			}
812			/*
813			 * Construct a LLC TEST message which will point to ourselves.
814			 */
815			bcopy(sc->tulip_hwaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
816			bcopy(sc->tulip_hwaddr, mtod(m, struct ether_header *)->ether_shost, 6);
817			mtod(m, struct ether_header *)->ether_type = htons(3);
818			mtod(m, unsigned char *)[14] = 0;
819			mtod(m, unsigned char *)[15] = 0;
820			mtod(m, unsigned char *)[16] = 0xE3;	/* LLC Class1 TEST (no poll) */
821			m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
822			/*
823			 * send it!
824			 */
825			sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
826			sc->tulip_flags &= ~TULIP_TXPROBE_OK;
827			sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
828			TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
829			IF_PREPEND(&sc->tulip_if.if_snd, m);
830			tulip_start(&sc->tulip_if);
831			break;
832		    }
833		    sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
834		}
835		/*
836		 * Take 2 passes through before deciding to not
837		 * wait for receive activity.  Then take another
838		 * two passes before spitting out a warning.
839		 */
840		if (sc->tulip_gpticks > 0 && --sc->tulip_gpticks == 0) {
841		    if (sc->tulip_flags & TULIP_WANTRXACT) {
842			sc->tulip_flags &= ~TULIP_WANTRXACT;
843			sc->tulip_gpticks = 4;
844		    } else {
845			printf("%s%d: autosense failed: cable problem?\n",
846			       sc->tulip_name, sc->tulip_unit);
847		    }
848		}
849		/*
850		 * Since this media failed to probe, try the other one.
851		 */
852		if (sc->tulip_probe_state == TULIP_PROBE_AUI) {
853		    sc->tulip_probe_state = TULIP_PROBE_BNC;
854		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
855		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_BNC);
856		    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_BNC);
857		    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_BNC);
858		    TULIP_WRITE_CSR(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
859		} else {
860		    sc->tulip_probe_state = TULIP_PROBE_AUI;
861		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
862		    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_AUI);
863		    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_AUI);
864		    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_AUI);
865		    TULIP_WRITE_CSR(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
866		}
867		TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
868		break;
869	    }
870	}
871    } else {
872	/*
873	 * If the link has passed LinkPass, 10baseT is the
874	 * proper media to use.
875	 */
876	if (sc->tulip_media != TULIP_MEDIA_10BASET)
877	    printf("%s%d: enabling 10baseT/UTP port\n",
878		   sc->tulip_if.if_name, sc->tulip_if.if_unit);
879	if (sc->tulip_media != TULIP_MEDIA_10BASET
880		|| (sc->tulip_flags & TULIP_INRESET)) {
881	    sc->tulip_media = TULIP_MEDIA_10BASET;
882	    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
883	    TULIP_WRITE_CSR(sc, csr_sia_connectivity, TULIP_DC21041_SIACONN_10BASET);
884	    TULIP_WRITE_CSR(sc, csr_sia_tx_rx,        TULIP_DC21041_SIATXRX_10BASET);
885	    TULIP_WRITE_CSR(sc, csr_sia_general,      TULIP_DC21041_SIAGEN_10BASET);
886	}
887	TULIP_WRITE_CSR(sc, csr_gp_timer, 0); /* disable */
888	sc->tulip_gpticks = 1;
889	sc->tulip_probe_state = TULIP_PROBE_10BASET;
890	sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
891	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
892    }
893    TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
894}
895
896static const tulip_boardsw_t tulip_dc21041_boardsw = {
897    TULIP_DC21041_GENERIC,
898    "",
899    tulip_dc21041_media_probe,
900    tulip_dc21041_media_select
901};
902
903static const tulip_boardsw_t tulip_dc21041_de450_boardsw = {
904    TULIP_DC21041_DE450,
905    "Digital DE450 ",
906    tulip_dc21041_media_probe,
907    tulip_dc21041_media_select
908};
909
910static void
911tulip_reset(
912    tulip_softc_t * const sc)
913{
914    tulip_ringinfo_t *ri;
915    tulip_desc_t *di;
916
917    TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
918    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
919		   33MHz that comes to two microseconds but wait a
920		   bit longer anyways) */
921
922    sc->tulip_flags |= TULIP_INRESET;
923    sc->tulip_intrmask = 0;
924    TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
925
926    TULIP_WRITE_CSR(sc, csr_txlist, vtophys(&sc->tulip_txinfo.ri_first[0]));
927    TULIP_WRITE_CSR(sc, csr_rxlist, vtophys(&sc->tulip_rxinfo.ri_first[0]));
928    TULIP_WRITE_CSR(sc, csr_busmode,
929        (1 << (TULIP_BURSTSIZE(sc->tulip_unit) + 8))
930	|TULIP_BUSMODE_CACHE_ALIGN8
931	|(BYTE_ORDER != LITTLE_ENDIAN ? TULIP_BUSMODE_BIGENDIAN : 0));
932
933    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
934    /*
935     * Free all the mbufs that were on the transmit ring.
936     */
937    for (;;) {
938	struct mbuf *m;
939	IF_DEQUEUE(&sc->tulip_txq, m);
940	if (m == NULL)
941	    break;
942	m_freem(m);
943    }
944
945    ri = &sc->tulip_txinfo;
946    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
947    ri->ri_free = ri->ri_max;
948    for (di = ri->ri_first; di < ri->ri_last; di++)
949	di->d_status = 0;
950
951    /*
952     * We need to collect all the mbufs were on the
953     * receive ring before we reinit it either to put
954     * them back on or to know if we have to allocate
955     * more.
956     */
957    ri = &sc->tulip_rxinfo;
958    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
959    ri->ri_free = ri->ri_max;
960    for (di = ri->ri_first; di < ri->ri_last; di++) {
961	di->d_status = 0;
962	di->d_length1 = 0; di->d_addr1 = 0;
963	di->d_length2 = 0; di->d_addr2 = 0;
964    }
965    for (;;) {
966	struct mbuf *m;
967	IF_DEQUEUE(&sc->tulip_rxq, m);
968	if (m == NULL)
969	    break;
970	m_freem(m);
971    }
972
973    (*sc->tulip_boardsw->bd_media_select)(sc);
974
975    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
976	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
977	    |TULIP_STS_TXBABBLE|TULIP_STS_LINKFAIL|TULIP_STS_RXSTOPPED;
978    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET);
979    tulip_addr_filter(sc);
980}
981
982static void
983tulip_init(
984    tulip_softc_t * const sc)
985{
986    if (sc->tulip_if.if_flags & IFF_UP) {
987	sc->tulip_if.if_flags |= IFF_RUNNING;
988	if (sc->tulip_if.if_flags & IFF_PROMISC) {
989	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
990	} else {
991	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
992	    if (sc->tulip_if.if_flags & IFF_ALLMULTI) {
993		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
994	    } else {
995		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
996	    }
997	}
998	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
999	if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
1000	    tulip_rx_intr(sc);
1001	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
1002	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
1003	} else {
1004	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
1005	    tulip_start(&sc->tulip_if);
1006	}
1007	TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
1008	TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
1009    } else {
1010	tulip_reset(sc);
1011	sc->tulip_if.if_flags &= ~IFF_RUNNING;
1012    }
1013}
1014
1015
1016#if TULIP_CHECK_RXCRC
1017static unsigned
1018tulip_crc32(
1019    u_char *addr,
1020    int len)
1021{
1022    unsigned int crc = 0xFFFFFFFF;
1023    static unsigned int crctbl[256];
1024    int idx;
1025    static int done;
1026    /*
1027     * initialize the multicast address CRC table
1028     */
1029    for (idx = 0; !done && idx < 256; idx++) {
1030	unsigned int tmp = idx;
1031	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1032	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1033	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1034	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1035	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1036	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1037	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1038	tmp = (tmp >> 1) ^ (tmp & 1 ? TULIP_CRC32_POLY : 0);	/* XOR */
1039	crctbl[idx] = tmp;
1040    }
1041    done = 1;
1042
1043    while (len-- > 0)
1044	crc = (crc >> 8) ^ crctbl[*addr++] ^ crctbl[crc & 0xFF];
1045
1046    return crc;
1047}
1048#endif
1049
1050static void
1051tulip_rx_intr(
1052    tulip_softc_t * const sc)
1053{
1054    tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
1055    struct ifnet * const ifp = &sc->tulip_if;
1056
1057    for (;;) {
1058	struct ether_header eh;
1059	tulip_desc_t *eop = ri->ri_nextin;
1060	int total_len = 0;
1061	struct mbuf *m = NULL;
1062	int accept = 0;
1063
1064	if (sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
1065	     goto queue_mbuf;
1066
1067	if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
1068	    break;
1069
1070	total_len = ((eop->d_status >> 16) & 0x7FF) - 4;
1071	IF_DEQUEUE(&sc->tulip_rxq, m);
1072	if ((eop->d_status & TULIP_DSTS_ERRSUM) == 0) {
1073
1074#if TULIP_CHECK_RXCRC
1075	    unsigned crc = tulip_crc32(mtod(m, unsigned char *), total_len);
1076	    if (~crc != *((unsigned *) &bufaddr[total_len])) {
1077		printf("%s%d: bad rx crc: %08x [rx] != %08x\n",
1078		       sc->tulip_name, sc->tulip_unit,
1079		       *((unsigned *) &bufaddr[total_len]), ~crc);
1080		goto next;
1081	    }
1082#endif
1083	    eh = *mtod(m, struct ether_header *);
1084#if NBPFILTER > 0
1085	    if (sc->tulip_bpf != NULL)
1086		bpf_tap(ifp, mtod(m, caddr_t), total_len);
1087#endif
1088	    if ((sc->tulip_if.if_flags & IFF_PROMISC)
1089		    && (eh.ether_dhost[0] & 1) == 0
1090		    && !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr))
1091		    goto next;
1092	    accept = 1;
1093	    total_len -= sizeof(struct ether_header);
1094	} else {
1095	    ifp->if_ierrors++;
1096	}
1097      next:
1098	ifp->if_ipackets++;
1099	if (++ri->ri_nextin == ri->ri_last)
1100	    ri->ri_nextin = ri->ri_first;
1101      queue_mbuf:
1102	/*
1103	 * Either we are priming the TULIP with mbufs (m == NULL)
1104	 * or we are about to accept an mbuf for the upper layers
1105	 * so we need to allocate an mbuf to replace it.  If we
1106	 * can't replace, then count it as an input error and reuse
1107	 * the mbuf.
1108	 */
1109	if (accept || m == NULL) {
1110	    struct mbuf *m0;
1111	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
1112	    if (m0 != NULL) {
1113#if defined(TULIP_COPY_RXDATA)
1114		if (!accept || total_len >= MHLEN) {
1115#endif
1116		    MCLGET(m0, M_DONTWAIT);
1117		    if ((m0->m_flags & M_EXT) == 0) {
1118			m_freem(m0);
1119			m0 = NULL;
1120		    }
1121#if defined(TULIP_COPY_RXDATA)
1122		}
1123#endif
1124	    }
1125	    if (accept) {
1126		if (m0 != NULL) {
1127#if defined(__bsdi__)
1128		    eh.ether_type = ntohs(eh.ether_type);
1129#endif
1130#if !defined(TULIP_COPY_RXDATA)
1131		    m->m_data += sizeof(struct ether_header);
1132		    m->m_len = m->m_pkthdr.len = total_len;
1133		    m->m_pkthdr.rcvif = ifp;
1134		    ether_input(ifp, &eh, m);
1135		    m = m0;
1136#else
1137		    bcopy(mtod(m, caddr_t) + sizeof(struct ether_header),
1138			  mtod(m0, caddr_t), total_len);
1139		    m0->m_len = m0->m_pkthdr.len = total_len;
1140		    m0->m_pkthdr.rcvif = ifp;
1141		    ether_input(ifp, &eh, m0);
1142#endif
1143		} else {
1144		    ifp->if_ierrors++;
1145		}
1146	    } else {
1147		m = m0;
1148	    }
1149	}
1150	if (m == NULL)
1151	    break;
1152	/*
1153	 * Now give the buffer to the TULIP and save in our
1154	 * receive queue.
1155	 */
1156	ri->ri_nextout->d_length1 = MCLBYTES - 4;
1157	ri->ri_nextout->d_addr1 = vtophys(mtod(m, caddr_t));
1158	ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
1159	if (++ri->ri_nextout == ri->ri_last)
1160	    ri->ri_nextout = ri->ri_first;
1161	IF_ENQUEUE(&sc->tulip_rxq, m);
1162    }
1163}
1164
1165static int
1166tulip_tx_intr(
1167    tulip_softc_t * const sc)
1168{
1169    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
1170    struct mbuf *m;
1171    int xmits = 0;
1172
1173    while (ri->ri_free < ri->ri_max) {
1174	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
1175	    break;
1176
1177	if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxLASTSEG) {
1178	    if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxSETUPPKT) {
1179		/*
1180		 * We've just finished processing a setup packet.
1181		 * Mark that we can finished it.  If there's not
1182		 * another pending, startup the TULIP receiver.
1183		 * Make sure we ack the RXSTOPPED so we won't get
1184		 * an abormal interrupt indication.
1185		 */
1186		sc->tulip_flags &= ~TULIP_DOINGSETUP;
1187		if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
1188		    tulip_rx_intr(sc);
1189		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
1190		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
1191		    TULIP_WRITE_CSR(sc, csr_status, TULIP_STS_RXSTOPPED);
1192		    TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
1193		    TULIP_WRITE_CSR(sc, csr_intr, sc->tulip_intrmask);
1194		}
1195	   } else {
1196		IF_DEQUEUE(&sc->tulip_txq, m);
1197		m_freem(m);
1198		xmits++;
1199		if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
1200		    if ((ri->ri_nextin->d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) == 0)
1201			sc->tulip_flags |= TULIP_TXPROBE_OK;
1202		    (*sc->tulip_boardsw->bd_media_select)(sc);
1203		} else {
1204		    sc->tulip_if.if_collisions +=
1205			(ri->ri_nextin->d_status & TULIP_DSTS_TxCOLLMASK)
1206			    >> TULIP_DSTS_V_TxCOLLCNT;
1207		    if (ri->ri_nextin->d_status & TULIP_DSTS_ERRSUM)
1208			sc->tulip_if.if_oerrors++;
1209		}
1210	    }
1211	}
1212
1213	if (++ri->ri_nextin == ri->ri_last)
1214	    ri->ri_nextin = ri->ri_first;
1215	ri->ri_free++;
1216	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
1217    }
1218    sc->tulip_if.if_opackets += xmits;
1219    return xmits;
1220}
1221
1222static ifnet_ret_t
1223tulip_start(
1224    struct ifnet * const ifp)
1225{
1226    tulip_softc_t * const sc = ifp->if_softc;
1227    struct ifqueue * const ifq = &ifp->if_snd;
1228    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
1229    struct mbuf *m, *m0, *next_m0;
1230
1231    if ((ifp->if_flags & IFF_RUNNING) == 0
1232	    && (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
1233	return;
1234
1235    for (;;) {
1236	tulip_desc_t *eop, *nextout;
1237	int segcnt, free, recopy;
1238	tulip_uint32_t d_status;
1239
1240	if (sc->tulip_flags & TULIP_WANTSETUP) {
1241	    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
1242		ifp->if_flags |= IFF_OACTIVE;
1243		return;
1244	    }
1245	    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
1246		   sizeof(sc->tulip_setupbuf));
1247	    sc->tulip_flags &= ~TULIP_WANTSETUP;
1248	    sc->tulip_flags |= TULIP_DOINGSETUP;
1249	    ri->ri_free--;
1250	    ri->ri_nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
1251	    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
1252		    |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
1253	    if (sc->tulip_flags & TULIP_WANTHASH)
1254		ri->ri_nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
1255	    ri->ri_nextout->d_length1 = sizeof(sc->tulip_setupbuf);
1256	    ri->ri_nextout->d_addr1 = vtophys(sc->tulip_setupbuf);
1257	    ri->ri_nextout->d_length2 = 0;
1258	    ri->ri_nextout->d_addr2 = 0;
1259	    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
1260	    TULIP_WRITE_CSR(sc, csr_txpoll, 1);
1261	    /*
1262	     * Advance the ring for the next transmit packet.
1263	     */
1264	    if (++ri->ri_nextout == ri->ri_last)
1265		ri->ri_nextout = ri->ri_first;
1266	    /*
1267	     * Make sure the next descriptor is owned by us since it
1268	     * may have been set up above if we ran out of room in the
1269	     * ring.
1270	     */
1271	    ri->ri_nextout->d_status = 0;
1272	}
1273
1274	IF_DEQUEUE(ifq, m);
1275	if (m == NULL)
1276	    break;
1277
1278	/*
1279	 * Now we try to fill in our transmit descriptors.  This is
1280	 * a bit reminiscent of going on the Ark two by two
1281	 * since each descriptor for the TULIP can describe
1282	 * two buffers.  So we advance through packet filling
1283	 * each of the two entries at a time to to fill each
1284	 * descriptor.  Clear the first and last segment bits
1285	 * in each descriptor (actually just clear everything
1286	 * but the end-of-ring or chain bits) to make sure
1287	 * we don't get messed up by previously sent packets.
1288	 *
1289	 * We may fail to put the entire packet on the ring if
1290	 * there is either not enough ring entries free or if the
1291	 * packet has more than MAX_TXSEG segments.  In the former
1292	 * case we will just wait for the ring to empty.  In the
1293	 * latter case we have to recopy.
1294	 */
1295	d_status = 0;
1296	recopy = 0;
1297	eop = nextout = ri->ri_nextout;
1298	m0 = m;
1299	segcnt = 0;
1300	free = ri->ri_free;
1301	do {
1302	    int len = m0->m_len;
1303	    caddr_t addr = mtod(m0, caddr_t);
1304	    unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
1305
1306	    next_m0 = m0->m_next;
1307	    while (len > 0) {
1308		unsigned slen = min(len, clsize);
1309
1310		segcnt++;
1311		if (segcnt > TULIP_MAX_TXSEG) {
1312		    recopy = 1;
1313		    next_m0 = NULL; /* to break out of outside loop */
1314		    break;
1315		}
1316		if (segcnt & 1) {
1317		    if (--free == 0) {
1318			/*
1319			 * There's no more room but since nothing
1320			 * has been committed at this point, just
1321			 * show output is active, put back the
1322			 * mbuf and return.
1323			 */
1324			ifp->if_flags |= IFF_OACTIVE;
1325			IF_PREPEND(ifq, m);
1326			return;
1327		    }
1328		    eop = nextout;
1329		    if (++nextout == ri->ri_last)
1330			nextout = ri->ri_first;
1331		    eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
1332		    eop->d_status = d_status;
1333		    eop->d_addr1 = vtophys(addr); eop->d_length1 = slen;
1334		} else {
1335		    /*
1336		     *  Fill in second half of descriptor
1337		     */
1338		    eop->d_addr2 = vtophys(addr); eop->d_length2 = slen;
1339		}
1340		d_status = TULIP_DSTS_OWNER;
1341		len -= slen;
1342		addr += slen;
1343		clsize = CLBYTES;
1344	    }
1345	} while ((m0 = next_m0) != NULL);
1346
1347	/*
1348	 * The packet exceeds the number of transmit buffer
1349	 * entries that we can use for one packet, so we have
1350	 * recopy it into one mbuf and then try again.
1351	 */
1352	if (recopy) {
1353	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
1354	    if (m0 != NULL) {
1355		if (m->m_pkthdr.len > MHLEN) {
1356		    MCLGET(m0, M_DONTWAIT);
1357		    if ((m0->m_flags & M_EXT) == 0) {
1358			m_freem(m);
1359			m_freem(m0);
1360			continue;
1361		    }
1362		}
1363		m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
1364		m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
1365		IF_PREPEND(ifq, m0);
1366	    }
1367	    m_freem(m);
1368	    continue;
1369	}
1370
1371	/*
1372	 * The descriptors have been filled in.  Now get ready
1373	 * to transmit.
1374	 */
1375#if NBPFILTER > 0
1376	if (sc->tulip_bpf != NULL)
1377	    bpf_mtap(ifp, m);
1378#endif
1379	IF_ENQUEUE(&sc->tulip_txq, m);
1380
1381	/*
1382	 * Make sure the next descriptor after this packet is owned
1383	 * by us since it may have been set up above if we ran out
1384	 * of room in the ring.
1385	 */
1386	nextout->d_status = 0;
1387
1388	/*
1389	 * If we only used the first segment of the last descriptor,
1390	 * make sure the second segment will not be used.
1391	 */
1392	if (segcnt & 1) {
1393	    eop->d_addr2 = 0;
1394	    eop->d_length2 = 0;
1395	}
1396
1397	/*
1398	 * Mark the last and first segments, indicate we want a transmit
1399	 * complete interrupt, give the descriptors to the TULIP, and tell
1400	 * it to transmit!
1401	 */
1402	eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
1403
1404	/*
1405	 * Note that ri->ri_nextout is still the start of the packet
1406	 * and until we set the OWNER bit, we can still back out of
1407	 * everything we have done.
1408	 */
1409	ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
1410	ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
1411
1412	/*
1413	 * This advances the ring for us.
1414	 */
1415	ri->ri_nextout = nextout;
1416	ri->ri_free = free;
1417
1418	TULIP_WRITE_CSR(sc, csr_txpoll, 1);
1419    }
1420    if (m != NULL) {
1421	ifp->if_flags |= IFF_OACTIVE;
1422	IF_PREPEND(ifq, m);
1423    }
1424}
1425
1426static void
1427tulip_intr(
1428    void *arg)
1429{
1430    tulip_softc_t * sc = (tulip_softc_t *) arg;
1431    tulip_uint32_t csr;
1432#if defined(__bsdi__)
1433    int progress = 1;
1434#else
1435    int progress = 0;
1436#endif
1437
1438    do {
1439	while ((csr = TULIP_READ_CSR(sc, csr_status)) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR)) {
1440	    progress = 1;
1441	    TULIP_WRITE_CSR(sc, csr_status, csr & sc->tulip_intrmask);
1442
1443	    if (csr & TULIP_STS_SYSERROR) {
1444		if ((csr & TULIP_STS_ERRORMASK) == TULIP_STS_ERR_PARITY) {
1445		    tulip_reset(sc);
1446		    tulip_init(sc);
1447		    break;
1448		}
1449	    }
1450	    if (csr & (TULIP_STS_GPTIMEOUT|TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL)) {
1451		if (sc->tulip_chipid == TULIP_DC21041) {
1452		    (*sc->tulip_boardsw->bd_media_select)(sc);
1453		    if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL))
1454			csr &= ~TULIP_STS_ABNRMLINTR;
1455		}
1456	    }
1457	    if (csr & TULIP_STS_ABNRMLINTR) {
1458		printf("%s%d: abnormal interrupt: 0x%05x [0x%05x]\n",
1459		       sc->tulip_name, sc->tulip_unit, csr, csr & sc->tulip_intrmask);
1460		TULIP_WRITE_CSR(sc, csr_command, sc->tulip_cmdmode);
1461	    }
1462	    if (csr & TULIP_STS_RXINTR)
1463		tulip_rx_intr(sc);
1464	    if (sc->tulip_txinfo.ri_free < sc->tulip_txinfo.ri_max) {
1465		tulip_tx_intr(sc);
1466		tulip_start(&sc->tulip_if);
1467	    }
1468	}
1469    } while ((sc = sc->tulip_slaves) != NULL);
1470}
1471
1472/*
1473 *
1474 */
1475
1476static void
1477tulip_delay_300ns(
1478    tulip_softc_t * const sc)
1479{
1480    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1481    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1482
1483    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1484    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1485
1486    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1487    TULIP_READ_CSR(sc, csr_busmode); TULIP_READ_CSR(sc, csr_busmode);
1488}
1489
1490#define EMIT    do { TULIP_WRITE_CSR(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
1491
1492static void
1493tulip_idle_srom(
1494    tulip_softc_t * const sc)
1495{
1496    unsigned bit, csr;
1497
1498    csr  = SROMSEL | SROMRD; EMIT;
1499    csr ^= SROMCS; EMIT;
1500    csr ^= SROMCLKON; EMIT;
1501
1502    /*
1503     * Write 25 cycles of 0 which will force the SROM to be idle.
1504     */
1505    for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
1506        csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1507        csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1508    }
1509    csr ^= SROMCLKOFF; EMIT;
1510    csr ^= SROMCS; EMIT; EMIT;
1511    csr  = 0; EMIT;
1512}
1513
1514
1515static void
1516tulip_read_srom(
1517    tulip_softc_t * const sc)
1518{
1519    int idx;
1520    const unsigned bitwidth = SROM_BITWIDTH;
1521    const unsigned cmdmask = (SROMCMD_RD << bitwidth);
1522    const unsigned msb = 1 << (bitwidth + 3 - 1);
1523    unsigned lastidx = (1 << bitwidth) - 1;
1524
1525    tulip_idle_srom(sc);
1526
1527    for (idx = 0; idx <= lastidx; idx++) {
1528        unsigned lastbit, data, bits, bit, csr;
1529        csr  = SROMSEL | SROMRD;        EMIT;
1530        csr ^= SROMCSON;                EMIT;
1531        csr ^=            SROMCLKON;    EMIT;
1532
1533        lastbit = 0;
1534        for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
1535            const unsigned thisbit = bits & msb;
1536            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1537            if (thisbit != lastbit) {
1538                csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
1539            }
1540            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1541            lastbit = thisbit;
1542        }
1543        csr ^= SROMCLKOFF; EMIT;
1544
1545        for (data = 0, bits = 0; bits < 16; bits++) {
1546            data <<= 1;
1547            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
1548            data |= TULIP_READ_CSR(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
1549            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
1550        }
1551	sc->tulip_rombuf[idx*2] = data & 0xFF;
1552	sc->tulip_rombuf[idx*2+1] = data >> 8;
1553        csr  = SROMSEL | SROMRD; EMIT;
1554        csr  = 0; EMIT;
1555    }
1556}
1557
1558#define	tulip_mchash(mca)	(tulip_crc32(mca, 6) & 0x1FF)
1559#define	tulip_srom_crcok(databuf)	( \
1560    ((tulip_crc32(databuf, 126) & 0xFFFF) ^ 0xFFFF)== \
1561     ((databuf)[126] | ((databuf)[127] << 8)))
1562
1563static unsigned
1564tulip_crc32(
1565    const unsigned char *databuf,
1566    size_t datalen)
1567{
1568    u_int idx, bit, data, crc = 0xFFFFFFFFUL;
1569
1570    for (idx = 0; idx < datalen; idx++)
1571        for (data = *databuf++, bit = 0; bit < 8; bit++, data >>= 1)
1572            crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0);
1573    return crc;
1574}
1575
1576
1577/*
1578 * This deals with the vagaries of the address roms and the
1579 * brain-deadness that various vendors commit in using them.
1580 */
1581static int
1582tulip_read_macaddr(
1583    tulip_softc_t *sc)
1584{
1585    int cksum, rom_cksum, idx;
1586    tulip_uint32_t csr;
1587    unsigned char tmpbuf[8];
1588    static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
1589
1590    if (sc->tulip_chipid == TULIP_DC21040) {
1591	TULIP_WRITE_CSR(sc, csr_enetrom, 1);
1592	for (idx = 0; idx < 32; idx++) {
1593	    int cnt = 0;
1594	    while (((csr = TULIP_READ_CSR(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
1595		cnt++;
1596	    sc->tulip_rombuf[idx] = csr & 0xFF;
1597	}
1598	sc->tulip_boardsw = &tulip_dc21040_boardsw;
1599#if defined(TULIP_EISA)
1600    } else if (sc->tulip_chipid == TULIP_DE425) {
1601	int cnt;
1602	for (idx = 0, cnt = 0; idx < sizeof(testpat) && cnt < 32; cnt++) {
1603	    tmpbuf[idx] = TULIP_READ_CSRBYTE(sc, csr_enetrom);
1604	    if (tmpbuf[idx] == testpat[idx])
1605		++idx;
1606	    else
1607		idx = 0;
1608	}
1609	for (idx = 0; idx < 32; idx++)
1610	    sc->tulip_rombuf[idx] = TULIP_READ_CSRBYTE(sc, csr_enetrom);
1611	sc->tulip_boardsw = &tulip_dc21040_boardsw;
1612#endif /* TULIP_EISA */
1613    } else {
1614	int new_srom_fmt = 0;
1615	/*
1616	 * Assume all DC21140 board are compatible with the
1617	 * DEC 10/100 evaluation board.  Not really valid but
1618	 * it's the best we can do until every one switches to
1619	 * the new SROM format.
1620	 */
1621	if (sc->tulip_chipid == TULIP_DC21140)
1622	    sc->tulip_boardsw = &tulip_dc21140_eb_boardsw;
1623	/*
1624	 * Thankfully all DC21041's act the same.
1625	 */
1626	if (sc->tulip_chipid == TULIP_DC21041)
1627	    sc->tulip_boardsw = &tulip_dc21041_boardsw;
1628	tulip_read_srom(sc);
1629	if (tulip_srom_crcok(sc->tulip_rombuf)) {
1630	    /*
1631	     * SROM CRC is valid therefore it must be in the
1632	     * new format.
1633	     */
1634	    new_srom_fmt = 1;
1635	} else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
1636	    /*
1637	     * No checksum is present.  See if the SROM id checks out;
1638	     * the first 18 bytes should be 0 followed by a 1 followed
1639	     * by the number of adapters (which we don't deal with yet).
1640	     */
1641	    for (idx = 0; idx < 18; idx++) {
1642		if (sc->tulip_rombuf[idx] != 0)
1643		    break;
1644	    }
1645	    if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
1646		new_srom_fmt = 2;
1647	}
1648	if (new_srom_fmt) {
1649	    /*
1650	     * New SROM format.  Copy out the Ethernet address.
1651	     * If it contains a DE500-XA string, then it must be
1652	     * a DE500-XA.
1653	     */
1654	    bcopy(sc->tulip_rombuf + 20, sc->tulip_hwaddr, 6);
1655	    if (bcmp(sc->tulip_rombuf + 29, "DE500-XA", 8) == 0)
1656		sc->tulip_boardsw = &tulip_dc21140_de500_boardsw;
1657	    if (bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0)
1658		sc->tulip_boardsw = &tulip_dc21041_de450_boardsw;
1659	    if (sc->tulip_boardsw == NULL)
1660		return -6;
1661	    sc->tulip_flags |= TULIP_ROMOK;
1662	    return 0;
1663	}
1664    }
1665
1666
1667    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
1668	/*
1669	 * Some folks don't use the standard ethernet rom format
1670	 * but instead just put the address in the first 6 bytes
1671	 * of the rom and let the rest be all 0xffs.  (Can we say
1672	 * ZNYX???)
1673	 */
1674	for (idx = 6; idx < 32; idx++) {
1675	    if (sc->tulip_rombuf[idx] != 0xFF)
1676		return -4;
1677	}
1678	/*
1679	 * Make sure the address is not multicast or locally assigned
1680	 * that the OUI is not 00-00-00.
1681	 */
1682	if ((sc->tulip_rombuf[0] & 3) != 0)
1683	    return -4;
1684	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
1685		&& sc->tulip_rombuf[2] == 0)
1686	    return -4;
1687	bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
1688	sc->tulip_flags |= TULIP_ROMOK;
1689	if (sc->tulip_hwaddr[0] == TULIP_OUI_ZNYX_0
1690		&& sc->tulip_hwaddr[1] == TULIP_OUI_ZNYX_1
1691		&& sc->tulip_hwaddr[2] == TULIP_OUI_ZNYX_2
1692	        && (sc->tulip_hwaddr[3] & ~3) == 0xF0) {
1693	    /*
1694	     * Now if the OUI is ZNYX and hwaddr[3] == 0xF0 .. 0xF3
1695	     * then it's a ZX314 Master port.
1696	     */
1697	    sc->tulip_boardsw = &tulip_dc21040_zx314_master_boardsw;
1698	}
1699	return 0;
1700    } else {
1701	/*
1702	 * A number of makers of multiport boards (ZNYX and Cogent)
1703	 * only put on one address ROM on their DC21040 boards.  So
1704	 * if the ROM is all zeros and this is a DC21040, look at the
1705	 * previous configured boards (as long as they are on the same
1706	 * PCI bus and the bus number is non-zero) until we find the
1707	 * master board with address ROM.  We then use its address ROM
1708	 * as the base for this board.  (we add our relative board
1709	 * to the last byte of its address).
1710	 */
1711	if (sc->tulip_chipid == TULIP_DC21040 /* && sc->tulip_bus != 0 XXX */) {
1712	    for (idx = 0; idx < 32; idx++) {
1713		if (sc->tulip_rombuf[idx] != 0)
1714		    break;
1715	    }
1716	    if (idx == 32) {
1717		int root_unit;
1718		tulip_softc_t *root_sc = NULL;
1719		for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
1720		    root_sc = TULIP_UNIT_TO_SOFTC(root_unit);
1721		    if (root_sc == NULL || (root_sc->tulip_flags & (TULIP_ROMOK|TULIP_SLAVEDROM)) == TULIP_ROMOK)
1722			break;
1723		    root_sc = NULL;
1724		}
1725		if (root_sc != NULL
1726			/* && root_sc->tulip_bus == sc->tulip_bus XXX */) {
1727		    bcopy(root_sc->tulip_hwaddr, sc->tulip_hwaddr, 6);
1728		    sc->tulip_hwaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
1729		    sc->tulip_flags |= TULIP_SLAVEDROM;
1730		    if (root_sc->tulip_boardsw->bd_type == TULIP_DC21040_ZX314_MASTER) {
1731			sc->tulip_boardsw = &tulip_dc21040_zx314_slave_boardsw;
1732			/*
1733			 * Now for a truly disgusting kludge: all 4 DC21040s on
1734			 * the ZX314 share the same INTA line so the mapping
1735			 * setup by the BIOS on the PCI bridge is worthless.
1736			 * Rather than reprogramming the value in the config
1737			 * register, we will handle this internally.
1738			 */
1739			sc->tulip_slaves = root_sc->tulip_slaves;
1740			root_sc->tulip_slaves = sc;
1741		    }
1742		    return 0;
1743		}
1744	    }
1745	}
1746    }
1747
1748    /*
1749     * This is the standard DEC address ROM test.
1750     */
1751
1752    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
1753	return -3;
1754
1755    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
1756    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
1757    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
1758    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
1759    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
1760	return -2;
1761
1762    bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
1763
1764    cksum = *(u_short *) &sc->tulip_hwaddr[0];
1765    cksum *= 2;
1766    if (cksum > 65535) cksum -= 65535;
1767    cksum += *(u_short *) &sc->tulip_hwaddr[2];
1768    if (cksum > 65535) cksum -= 65535;
1769    cksum *= 2;
1770    if (cksum > 65535) cksum -= 65535;
1771    cksum += *(u_short *) &sc->tulip_hwaddr[4];
1772    if (cksum >= 65535) cksum -= 65535;
1773
1774    rom_cksum = *(u_short *) &sc->tulip_rombuf[6];
1775
1776    if (cksum != rom_cksum)
1777	return -1;
1778
1779    /*
1780     * Check for various boards based on OUI.  Did I say braindead?
1781     */
1782
1783    if (sc->tulip_chipid == TULIP_DC21140) {
1784	if (sc->tulip_hwaddr[0] == TULIP_OUI_COGENT_0
1785		&& sc->tulip_hwaddr[1] == TULIP_OUI_COGENT_1
1786		&& sc->tulip_hwaddr[2] == TULIP_OUI_COGENT_2) {
1787	    if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100_ID)
1788		sc->tulip_boardsw = &tulip_dc21140_cogent_em100_boardsw;
1789	}
1790	if (sc->tulip_hwaddr[0] == TULIP_OUI_ZNYX_0
1791		&& sc->tulip_hwaddr[1] == TULIP_OUI_ZNYX_1
1792		&& sc->tulip_hwaddr[2] == TULIP_OUI_ZNYX_2) {
1793	    /* this at least works for the zx342 from Znyx */
1794	    sc->tulip_boardsw = &tulip_dc21140_znyx_zx34x_boardsw;
1795	}
1796    }
1797
1798    sc->tulip_flags |= TULIP_ROMOK;
1799    return 0;
1800}
1801
1802static void
1803tulip_addr_filter(
1804    tulip_softc_t * const sc)
1805{
1806    tulip_uint32_t *sp = sc->tulip_setupdata;
1807    struct ether_multistep step;
1808    struct ether_multi *enm;
1809    int i;
1810
1811    sc->tulip_flags &= ~TULIP_WANTHASH;
1812    sc->tulip_flags |= TULIP_WANTSETUP;
1813    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
1814    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
1815    if (sc->tulip_ac.ac_multicnt > 14) {
1816	unsigned hash;
1817	/*
1818	 * If we have more than 14 multicasts, we have
1819	 * go into hash perfect mode (512 bit multicast
1820	 * hash and one perfect hardware).
1821	 */
1822
1823	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
1824	hash = tulip_mchash(etherbroadcastaddr);
1825	sp[hash >> 4] |= 1 << (hash & 0xF);
1826	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
1827	while (enm != NULL) {
1828	    hash = tulip_mchash(enm->enm_addrlo);
1829	    sp[hash >> 4] |= 1 << (hash & 0xF);
1830	    ETHER_NEXT_MULTI(step, enm);
1831	}
1832	sc->tulip_flags |= TULIP_WANTHASH;
1833	sp[39] = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
1834	sp[40] = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
1835	sp[41] = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
1836    } else {
1837	/*
1838	 * Else can get perfect filtering for 16 addresses.
1839	 */
1840	i = 0;
1841	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
1842	for (; enm != NULL; i++) {
1843	    *sp++ = ((u_short *) enm->enm_addrlo)[0];
1844	    *sp++ = ((u_short *) enm->enm_addrlo)[1];
1845	    *sp++ = ((u_short *) enm->enm_addrlo)[2];
1846	    ETHER_NEXT_MULTI(step, enm);
1847	}
1848	/*
1849	 * Add the broadcast address.
1850	 */
1851	i++;
1852	*sp++ = 0xFFFF;
1853	*sp++ = 0xFFFF;
1854	*sp++ = 0xFFFF;
1855	/*
1856	 * Pad the rest with our hardware address
1857	 */
1858	for (; i < 16; i++) {
1859	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[0];
1860	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[1];
1861	    *sp++ = ((u_short *) sc->tulip_ac.ac_enaddr)[2];
1862	}
1863    }
1864}
1865
1866static int
1867tulip_ioctl(
1868    struct ifnet * const ifp,
1869    ioctl_cmd_t cmd,
1870    caddr_t data)
1871{
1872    tulip_softc_t * const sc = ifp->if_softc;
1873    struct ifaddr *ifa = (struct ifaddr *)data;
1874    struct ifreq *ifr = (struct ifreq *) data;
1875    int s, error = 0;
1876
1877    s = splimp();
1878
1879    switch (cmd) {
1880	case SIOCSIFADDR: {
1881
1882	    ifp->if_flags |= IFF_UP;
1883	    switch(ifa->ifa_addr->sa_family) {
1884#ifdef INET
1885		case AF_INET: {
1886		    sc->tulip_ac.ac_ipaddr = IA_SIN(ifa)->sin_addr;
1887		    tulip_init(sc);
1888#if defined(__FreeBSD__) || defined(__NetBSD__)
1889		    arp_ifinit(&sc->tulip_ac, ifa);
1890#elif defined(__bsdi__)
1891		    arpwhohas(&sc->tulip_ac, &IA_SIN(ifa)->sin_addr);
1892#endif
1893		    break;
1894		}
1895#endif /* INET */
1896
1897#ifdef NS
1898		/*
1899		 * This magic copied from if_is.c; I don't use XNS,
1900		 * so I have no way of telling if this actually
1901		 * works or not.
1902		 */
1903		case AF_NS: {
1904		    struct ns_addr *ina = &(IA_SNS(ifa)->sns_addr);
1905		    if (ns_nullhost(*ina)) {
1906			ina->x_host = *(union ns_host *)(sc->tulip_ac.ac_enaddr);
1907		    } else {
1908			ifp->if_flags &= ~IFF_RUNNING;
1909			bcopy((caddr_t)ina->x_host.c_host,
1910			      (caddr_t)sc->tulip_ac.ac_enaddr,
1911			      sizeof sc->tulip_ac.ac_enaddr);
1912		    }
1913
1914		    tulip_init(sc);
1915		    break;
1916		}
1917#endif /* NS */
1918
1919		default: {
1920		    tulip_init(sc);
1921		    break;
1922		}
1923	    }
1924	    break;
1925	}
1926	case SIOCGIFADDR: {
1927	    bcopy((caddr_t) sc->tulip_ac.ac_enaddr,
1928		  (caddr_t) ((struct sockaddr *)&ifr->ifr_data)->sa_data,
1929		  6);
1930	    break;
1931	}
1932
1933	case SIOCSIFFLAGS: {
1934	    /*
1935	     * Changing the connection forces a reset.
1936	     */
1937	    if (sc->tulip_flags & TULIP_ALTPHYS) {
1938		if ((ifp->if_flags & IFF_ALTPHYS) == 0)
1939		    tulip_reset(sc);
1940	    } else {
1941		if (ifp->if_flags & IFF_ALTPHYS)
1942		    tulip_reset(sc);
1943	    }
1944	    tulip_init(sc);
1945	    break;
1946	}
1947
1948	case SIOCADDMULTI:
1949	case SIOCDELMULTI: {
1950	    /*
1951	     * Update multicast listeners
1952	     */
1953	    if (cmd == SIOCADDMULTI)
1954		error = ether_addmulti(ifr, &sc->tulip_ac);
1955	    else
1956		error = ether_delmulti(ifr, &sc->tulip_ac);
1957
1958	    if (error == ENETRESET) {
1959		tulip_addr_filter(sc);		/* reset multicast filtering */
1960		tulip_init(sc);
1961		error = 0;
1962	    }
1963	    break;
1964	}
1965#if defined(SIOCSIFMTU)
1966#if !defined(ifr_mtu)
1967#define ifr_mtu ifr_metric
1968#endif
1969	case SIOCSIFMTU:
1970	    /*
1971	     * Set the interface MTU.
1972	     */
1973	    if (ifr->ifr_mtu > ETHERMTU) {
1974		error = EINVAL;
1975		break;
1976	    }
1977	    ifp->if_mtu = ifr->ifr_mtu;
1978	    break;
1979#endif
1980
1981	default: {
1982	    error = EINVAL;
1983	    break;
1984	}
1985    }
1986
1987    splx(s);
1988    return error;
1989}
1990
1991static void
1992tulip_attach(
1993    tulip_softc_t * const sc)
1994{
1995    struct ifnet * const ifp = &sc->tulip_if;
1996
1997    ifp->if_softc = sc;
1998    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_MULTICAST;
1999    ifp->if_ioctl = tulip_ioctl;
2000    ifp->if_output = ether_output;
2001    ifp->if_start = tulip_start;
2002
2003#ifdef __FreeBSD__
2004    printf("%s%d", sc->tulip_name, sc->tulip_unit);
2005#endif
2006    printf(": %s%s pass %d.%d Ethernet address %6D\n",
2007	   sc->tulip_boardsw->bd_description,
2008	   tulip_chipdescs[sc->tulip_chipid],
2009	   (sc->tulip_revinfo & 0xF0) >> 4,
2010	   sc->tulip_revinfo & 0x0F,
2011	   sc->tulip_hwaddr, ":");
2012
2013    if ((*sc->tulip_boardsw->bd_media_probe)(sc)) {
2014	ifp->if_flags |= IFF_ALTPHYS;
2015    } else {
2016	sc->tulip_flags |= TULIP_ALTPHYS;
2017    }
2018
2019    tulip_reset(sc);
2020
2021    if_attach(ifp);
2022    ether_ifattach(ifp);
2023
2024#if NBPFILTER > 0
2025    bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
2026#endif
2027}
2028
2029static void
2030tulip_initcsrs(
2031    tulip_softc_t * const sc,
2032    tulip_csrptr_t csr_base,
2033    size_t csr_size)
2034{
2035    sc->tulip_csrs.csr_busmode		= csr_base +  0 * csr_size;
2036    sc->tulip_csrs.csr_txpoll		= csr_base +  1 * csr_size;
2037    sc->tulip_csrs.csr_rxpoll		= csr_base +  2 * csr_size;
2038    sc->tulip_csrs.csr_rxlist		= csr_base +  3 * csr_size;
2039    sc->tulip_csrs.csr_txlist		= csr_base +  4 * csr_size;
2040    sc->tulip_csrs.csr_status		= csr_base +  5 * csr_size;
2041    sc->tulip_csrs.csr_command		= csr_base +  6 * csr_size;
2042    sc->tulip_csrs.csr_intr		= csr_base +  7 * csr_size;
2043    sc->tulip_csrs.csr_missed_frame	= csr_base +  8 * csr_size;
2044    if (sc->tulip_chipid == TULIP_DC21040) {
2045	sc->tulip_csrs.csr_enetrom		= csr_base +  9 * csr_size;
2046	sc->tulip_csrs.csr_reserved		= csr_base + 10 * csr_size;
2047	sc->tulip_csrs.csr_full_duplex		= csr_base + 11 * csr_size;
2048	sc->tulip_csrs.csr_sia_status		= csr_base + 12 * csr_size;
2049	sc->tulip_csrs.csr_sia_connectivity	= csr_base + 13 * csr_size;
2050	sc->tulip_csrs.csr_sia_tx_rx 		= csr_base + 14 * csr_size;
2051	sc->tulip_csrs.csr_sia_general		= csr_base + 15 * csr_size;
2052#if defined(TULIP_EISA)
2053    } else if (sc->tulip_chipid == TULIP_DE425) {
2054	sc->tulip_csrs.csr_enetrom		= csr_base + DE425_ENETROM_OFFSET;
2055	sc->tulip_csrs.csr_reserved		= csr_base + 10 * csr_size;
2056	sc->tulip_csrs.csr_full_duplex		= csr_base + 11 * csr_size;
2057	sc->tulip_csrs.csr_sia_status		= csr_base + 12 * csr_size;
2058	sc->tulip_csrs.csr_sia_connectivity	= csr_base + 13 * csr_size;
2059	sc->tulip_csrs.csr_sia_tx_rx 		= csr_base + 14 * csr_size;
2060	sc->tulip_csrs.csr_sia_general		= csr_base + 15 * csr_size;
2061#endif /* TULIP_EISA */
2062    } else if (sc->tulip_chipid == TULIP_DC21140) {
2063	sc->tulip_csrs.csr_srom_mii		= csr_base +  9 * csr_size;
2064	sc->tulip_csrs.csr_gp_timer		= csr_base + 11 * csr_size;
2065	sc->tulip_csrs.csr_gp			= csr_base + 12 * csr_size;
2066	sc->tulip_csrs.csr_watchdog		= csr_base + 15 * csr_size;
2067    } else if (sc->tulip_chipid == TULIP_DC21041) {
2068	sc->tulip_csrs.csr_srom_mii		= csr_base +  9 * csr_size;
2069	sc->tulip_csrs.csr_bootrom		= csr_base + 10 * csr_size;
2070	sc->tulip_csrs.csr_gp_timer		= csr_base + 11 * csr_size;
2071	sc->tulip_csrs.csr_sia_status		= csr_base + 12 * csr_size;
2072	sc->tulip_csrs.csr_sia_connectivity	= csr_base + 13 * csr_size;
2073	sc->tulip_csrs.csr_sia_tx_rx 		= csr_base + 14 * csr_size;
2074	sc->tulip_csrs.csr_sia_general		= csr_base + 15 * csr_size;
2075    }
2076}
2077
2078static void
2079tulip_initring(
2080    tulip_softc_t * const sc,
2081    tulip_ringinfo_t * const ri,
2082    tulip_desc_t *descs,
2083    int ndescs)
2084{
2085    ri->ri_max = ndescs;
2086    ri->ri_first = descs;
2087    ri->ri_last = ri->ri_first + ri->ri_max;
2088    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
2089    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
2090}
2091
2092/*
2093 * This is the PCI configuration support.  Since the DC21040 is available
2094 * on both EISA and PCI boards, one must be careful in how defines the
2095 * DC21040 in the config file.
2096 */
2097
2098#define	PCI_CFID	0x00	/* Configuration ID */
2099#define	PCI_CFCS	0x04	/* Configurtion Command/Status */
2100#define	PCI_CFRV	0x08	/* Configuration Revision */
2101#define	PCI_CFLT	0x0c	/* Configuration Latency Timer */
2102#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
2103#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
2104#define	PCI_CFIT	0x3c	/* Configuration Interrupt */
2105#define	PCI_CFDA	0x40	/* Configuration Driver Area */
2106
2107#if defined(TULIP_EISA)
2108static const int tulip_eisa_irqs[4] = { IRQ5, IRQ9, IRQ10, IRQ11 };
2109#endif
2110
2111#if defined(__FreeBSD__)
2112
2113#define	TULIP_PCI_ATTACH_ARGS	pcici_t config_id, int unit
2114
2115static int
2116tulip_pci_shutdown(
2117    struct kern_devconf * const kdc,
2118    int force)
2119{
2120    if (kdc->kdc_unit < NDE) {
2121	tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(kdc->kdc_unit);
2122	TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2123	DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
2124			   33MHz that comes to two microseconds but wait a
2125			   bit longer anyways) */
2126    }
2127    (void) dev_detach(kdc);
2128    return 0;
2129}
2130
2131static char*
2132tulip_pci_probe(
2133    pcici_t config_id,
2134    pcidi_t device_id)
2135{
2136    if (PCI_VENDORID(device_id) != DEC_VENDORID)
2137	return NULL;
2138    if (PCI_CHIPID(device_id) == DC21040_CHIPID)
2139	return "Digital DC21040 Ethernet";
2140    if (PCI_CHIPID(device_id) == DC21041_CHIPID)
2141	return "Digital DC21041 Ethernet";
2142    if (PCI_CHIPID(device_id) == DC21140_CHIPID)
2143	return "Digital DC21140 Fast Ethernet";
2144    return NULL;
2145}
2146
2147static void  tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
2148static u_long tulip_pci_count;
2149
2150static struct pci_device dedevice = {
2151    "de",
2152    tulip_pci_probe,
2153    tulip_pci_attach,
2154   &tulip_pci_count,
2155    tulip_pci_shutdown,
2156};
2157
2158DATA_SET (pcidevice_set, dedevice);
2159#endif /* __FreeBSD__ */
2160
2161#if defined(__bsdi__)
2162#define	TULIP_PCI_ATTACH_ARGS	struct device * const parent, struct device * const self, void * const aux
2163
2164static void
2165tulip_shutdown(
2166    void *arg)
2167{
2168    tulip_softc_t * const sc = (tulip_softc_t *) arg;
2169    TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2170    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
2171			   33MHz that comes to two microseconds but wait a
2172			   bit longer anyways) */
2173}
2174
2175static int
2176tulip_pci_match(
2177    pci_devaddr_t *pa)
2178{
2179    int irq;
2180    unsigned id;
2181
2182    id = pci_inl(pa, PCI_VENDOR_ID);
2183    if (PCI_VENDORID(id) != DEC_VENDORID)
2184	return 0;
2185    id = PCI_CHIPID(id);
2186    if (id != DC21040_CHIPID && id != DC21041_CHIPID && id != DC21140_CHIPID)
2187	return 0;
2188    irq = pci_inl(pa, PCI_I_LINE) & 0xFF;
2189    if (irq == 0 || irq >= 16) {
2190	printf("de?: invalid IRQ %d; skipping\n", irq);
2191	return 0;
2192    }
2193    return 1;
2194}
2195
2196static int
2197tulip_probe(
2198    struct device *parent,
2199    struct cfdata *cf,
2200    void *aux)
2201{
2202    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
2203    unsigned irq, slot;
2204    pci_devaddr_t *pa;
2205
2206#if defined(TULIP_EISA)
2207    if ((slot = eisa_match(cf, ia)) != 0) {
2208	unsigned tmp;
2209	ia->ia_iobase = slot << 12;
2210	ia->ia_iosize = EISA_NPORT;
2211	eisa_slotalloc(slot);
2212	tmp = inb(ia->ia_iobase + DE425_CFG0);
2213	irq = tulip_eisa_irqs[(tmp >> 1) & 0x03];
2214	/*
2215	 * Until BSD/OS likes level interrupts, force
2216	 * the DE425 into edge-triggered mode.
2217	 */
2218	if ((tmp & 1) == 0)
2219	    outb(ia->ia_iobase + DE425_CFG0, tmp | 1);
2220	/*
2221	 * CBIO needs to map to the EISA slot
2222	 * enable I/O access and Master
2223	 */
2224	outl(ia->ia_iobase + DE425_CBIO, ia->ia_iobase);
2225	outl(ia->ia_iobase + DE425_CFCS, 5 | inl(ia->ia_iobase + DE425_CFCS));
2226	ia->ia_aux = NULL;
2227    } else {
2228#endif /* TULIP_EISA */
2229	pa = pci_scan(tulip_pci_match);
2230	if (pa == NULL)
2231	    return 0;
2232
2233	irq = (1 << (pci_inl(pa, PCI_I_LINE) & 0xFF));
2234
2235	/* Get the base address; assume the BIOS set it up correctly */
2236#if defined(TULIP_IOMAPPED)
2237	ia->ia_maddr = NULL;
2238	ia->ia_msize = 0;
2239	ia->ia_iobase = pci_inl(pa, PCI_CBIO) & ~7;
2240	pci_outl(pa, PCI_CBIO, 0xFFFFFFFF);
2241	ia->ia_iosize = ((~pci_inl(pa, PCI_CBIO)) | 7) + 1;
2242	pci_outl(pa, PCI_CBIO, (int) ia->ia_iobase);
2243
2244	/* Disable memory space access */
2245	pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~2);
2246#else
2247	ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
2248	pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
2249	ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
2250	pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
2251	ia->ia_iobase = 0;
2252	ia->ia_iosize = 0;
2253
2254	/* Disable I/O space access */
2255	pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
2256#endif /* TULIP_IOMAPPED */
2257
2258	ia->ia_aux = (void *) pa;
2259#if defined(TULIP_EISA)
2260    }
2261#endif
2262
2263    /* PCI bus masters don't use host DMA channels */
2264    ia->ia_drq = DRQNONE;
2265
2266    if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
2267	printf("de%d: error: desired IRQ of %d does not match device's actual IRQ of %d,\n",
2268	       cf->cf_unit,
2269	       ffs(ia->ia_irq) - 1, ffs(irq) - 1);
2270	return 0;
2271    }
2272    if (ia->ia_irq == IRQUNK && (ia->ia_irq = isa_irqalloc(irq)) == 0) {
2273	printf("de%d: warning: IRQ %d is shared\n", cf->cf_unit, ffs(irq) - 1);
2274	ia->ia_irq = irq;
2275    }
2276    return 1;
2277}
2278
2279static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
2280
2281#if defined(TULIP_EISA)
2282static char *tulip_eisa_ids[] = {
2283    "DEC4250",
2284    NULL
2285};
2286#endif
2287
2288struct cfdriver decd = {
2289    0, "de", tulip_probe, tulip_pci_attach, DV_IFNET, sizeof(tulip_softc_t),
2290#if defined(TULIP_EISA)
2291    tulip_eisa_ids
2292#endif
2293};
2294
2295#endif /* __bsdi__ */
2296
2297#if defined(__NetBSD__)
2298#define	TULIP_PCI_ATTACH_ARGS	struct device * const parent, struct device * const self, void * const aux
2299
2300static void
2301tulip_pci_shutdown(
2302    void *arg)
2303{
2304    tulip_softc_t * const sc = (tulip_softc_t *) arg;
2305    TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2306    DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
2307			   33MHz that comes to two microseconds but wait a
2308			   bit longer anyways) */
2309}
2310
2311static int
2312tulip_pci_probe(
2313    struct device *parent,
2314    void *match,
2315    void *aux)
2316{
2317    struct pci_attach_args *pa = (struct pci_attach_args *) aux;
2318
2319    if (PCI_VENDORID(pa->pa_id) != DEC_VENDORID)
2320	return 0;
2321    if (PCI_CHIPID(pa->pa_id) == DC21040_CHIPID
2322	    || PCI_CHIPID(pa->pa_id) == DC21041_CHIPID
2323	    || PCI_CHIPID(pa->pa_id) == DC21140_CHIPID)
2324	return 1;
2325
2326    return 0;
2327}
2328
2329static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
2330
2331struct cfdriver decd = {
2332    0, "de", tulip_pci_probe, tulip_pci_attach, DV_IFNET, sizeof(tulip_softc_t)
2333};
2334
2335#endif /* __NetBSD__ */
2336
2337static void
2338tulip_pci_attach(
2339    TULIP_PCI_ATTACH_ARGS)
2340{
2341#if defined(__FreeBSD__)
2342    tulip_softc_t *sc;
2343#endif
2344#if defined(__bsdi__)
2345    tulip_softc_t * const sc = (tulip_softc_t *) self;
2346    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
2347    pci_devaddr_t *pa = (pci_devaddr_t *) ia->ia_aux;
2348    int unit = sc->tulip_dev.dv_unit;
2349#endif
2350#if defined(__NetBSD__)
2351    tulip_softc_t * const sc = (tulip_softc_t *) self;
2352    struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
2353    int unit = sc->tulip_dev.dv_unit;
2354#endif
2355    int retval, idx, revinfo, id;
2356#if !defined(TULIP_IOMAPPED) && !defined(__bsdi__)
2357    vm_offset_t pa_csrs;
2358#endif
2359    unsigned csroffset = TULIP_PCI_CSROFFSET;
2360    unsigned csrsize = TULIP_PCI_CSRSIZE;
2361    tulip_csrptr_t csr_base;
2362    tulip_desc_t *rxdescs, *txdescs;
2363    tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
2364
2365#if defined(__FreeBSD__)
2366    if (unit >= NDE) {
2367	printf("de%d: not configured; kernel is built for only %d device%s.\n",
2368	       unit, NDE, NDE == 1 ? "" : "s");
2369	return;
2370    }
2371#endif
2372
2373#if defined(__FreeBSD__)
2374    revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
2375    id = pci_conf_read(config_id, PCI_CFID);
2376#endif
2377#if defined(__bsdi__)
2378    if (pa != NULL) {
2379	revinfo = pci_inl(pa, PCI_CFRV) & 0xFF;
2380	id = pci_inl(pa, PCI_CFID);
2381#if defined(TULIP_EISA)
2382    } else {
2383	revinfo = inl(ia->ia_iobase + DE425_CFRV) & 0xFF;
2384	csroffset = TULIP_EISA_CSROFFSET;
2385	csrsize = TULIP_EISA_CSRSIZE;
2386	chipid = TULIP_DE425;
2387#endif
2388    }
2389#endif
2390#if defined(__NetBSD__)
2391    revinfo = pci_conf_read(pa->pa_tag, PCI_CFRV) & 0xFF;
2392    id = pa->pa_id;
2393#endif
2394
2395    if (PCI_VENDORID(id) == DEC_VENDORID) {
2396	if (PCI_CHIPID(id) == DC21040_CHIPID) chipid = TULIP_DC21040;
2397	else if (PCI_CHIPID(id) == DC21140_CHIPID) chipid = TULIP_DC21140;
2398	else if (PCI_CHIPID(id) == DC21041_CHIPID) chipid = TULIP_DC21041;
2399    }
2400    if (chipid == TULIP_CHIPID_UNKNOWN)
2401	return;
2402
2403    if ((chipid == TULIP_DC21040 || chipid == TULIP_DE425) && revinfo < 0x20) {
2404#ifdef __FreeBSD__
2405	printf("de%d", unit);
2406#endif
2407	printf(": not configured; DC21040 pass 2.0 required (%d.%d found)\n",
2408	       revinfo >> 4, revinfo & 0x0f);
2409	return;
2410    } else if (chipid == TULIP_DC21140 && revinfo < 0x11) {
2411#ifdef __FreeBSD__
2412	printf("de%d", unit);
2413#endif
2414	printf(": not configured; DC21140 pass 1.1 required (%d.%d found)\n",
2415	       revinfo >> 4, revinfo & 0x0f);
2416	return;
2417    }
2418
2419#if defined(__FreeBSD__)
2420    sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
2421    if (sc == NULL)
2422	return;
2423    bzero(sc, sizeof(*sc));				/* Zero out the softc*/
2424#endif
2425
2426    rxdescs = (tulip_desc_t *)
2427	malloc(sizeof(tulip_desc_t) * TULIP_RXDESCS, M_DEVBUF, M_NOWAIT);
2428    if (rxdescs == NULL) {
2429#if defined(__FreeBSD__)
2430	free((caddr_t) sc, M_DEVBUF);
2431#endif
2432	return;
2433    }
2434
2435    txdescs = (tulip_desc_t *)
2436	malloc(sizeof(tulip_desc_t) * TULIP_TXDESCS, M_DEVBUF, M_NOWAIT);
2437    if (txdescs == NULL) {
2438	free((caddr_t) rxdescs, M_DEVBUF);
2439#if defined(__FreeBSD__)
2440	free((caddr_t) sc, M_DEVBUF);
2441#endif
2442	return;
2443    }
2444
2445    sc->tulip_chipid = chipid;
2446    sc->tulip_unit = unit;
2447    sc->tulip_name = "de";
2448    sc->tulip_revinfo = revinfo;
2449#if defined(__FreeBSD__)
2450#if defined(TULIP_IOMAPPED)
2451    retval = pci_map_port(config_id, PCI_CBIO, &csr_base);
2452#else
2453    retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &csr_base, &pa_csrs);
2454#endif
2455    if (!retval) {
2456	free((caddr_t) txdescs, M_DEVBUF);
2457	free((caddr_t) rxdescs, M_DEVBUF);
2458	free((caddr_t) sc, M_DEVBUF);
2459	return;
2460    }
2461    tulips[unit] = sc;
2462#endif /* __FreeBSD__ */
2463
2464#if defined(__bsdi__)
2465#if defined(TULIP_IOMAPPED)
2466    csr_base = ia->ia_iobase;
2467#else
2468    csr_base = (vm_offset_t) mapphys((vm_offset_t) ia->ia_maddr, ia->ia_msize);
2469#endif
2470#endif /* __bsdi__ */
2471
2472#if defined(__NetBSD__)
2473#if defined(TULIP_IOMAPPED)
2474    retval = pci_map_io(pa->pa_tag, PCI_CBIO, &csr_base);
2475#else
2476    retval = pci_map_mem(pa->pa_tag, PCI_CBMA, (vm_offset_t *) &csr_base, &pa_csrs);
2477#endif
2478    if (retval) {
2479	free((caddr_t) txdescs, M_DEVBUF);
2480	free((caddr_t) rxdescs, M_DEVBUF);
2481	return;
2482    }
2483#endif /* __NetBSD__ */
2484
2485    tulip_initcsrs(sc, csr_base + csroffset, csrsize);
2486    tulip_initring(sc, &sc->tulip_rxinfo, rxdescs, TULIP_RXDESCS);
2487    tulip_initring(sc, &sc->tulip_txinfo, txdescs, TULIP_TXDESCS);
2488    if ((retval = tulip_read_macaddr(sc)) < 0) {
2489#ifdef __FreeBSD__
2490	printf("%s%d", sc->tulip_name, sc->tulip_unit);
2491#endif
2492	printf(": can't read ENET ROM (why=%d) (", retval);
2493	for (idx = 0; idx < 32; idx++)
2494	    printf("%02x", sc->tulip_rombuf[idx]);
2495	printf("\n");
2496	printf("%s%d: %s%s pass %d.%d Ethernet address %s\n",
2497	       sc->tulip_name, sc->tulip_unit,
2498	       (sc->tulip_boardsw != NULL ? sc->tulip_boardsw->bd_description : ""),
2499	       tulip_chipdescs[sc->tulip_chipid],
2500	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F,
2501	       "unknown");
2502    } else {
2503	/*
2504	 * Make sure there won't be any interrupts or such...
2505	 */
2506	TULIP_WRITE_CSR(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2507	DELAY(10);	/* Wait 10 microsends (actually 50 PCI cycles but at
2508			   33MHz that comes to two microseconds but wait a
2509			   bit longer anyways) */
2510#if defined(__NetBSD__)
2511	if (sc->tulip_boardsw->bd_type != TULIP_DC21040_ZX314_SLAVE) {
2512	    sc->tulip_ih = pci_map_int(pa->pa_tag, PCI_IPL_NET, tulip_intr, sc);
2513	    if (sc->tulip_ih == NULL) {
2514		printf("%s%d: couldn't map interrupt\n",
2515		       sc->tulip_name, sc->tulip_unit);
2516		return;
2517	    }
2518#if defined(__i386__)
2519	    /* gross but netbsd won't print the irq otherwise */
2520	    printf(" irq %d", ((struct intrhand *) sc->tulip_ih)->ih_irq);
2521#endif
2522	}
2523	sc->tulip_ats = shutdownhook_establish(tulip_pci_shutdown, sc);
2524	if (sc->tulip_ats == NULL)
2525	    printf("%s%d: warning: couldn't establish shutdown hook\n",
2526		   sc->tulip_name, sc->tulip_unit);
2527#endif
2528#if defined(__FreeBSD__)
2529	if (sc->tulip_boardsw->bd_type != TULIP_DC21040_ZX314_SLAVE) {
2530	    if (!pci_map_int(config_id, tulip_intr, (void*) sc, &net_imask)) {
2531		printf("%s%d: couldn't map interrupt\n",
2532			sc->tulip_name, sc->tulip_unit);
2533		return;
2534	    }
2535	}
2536#endif
2537#if defined(__bsdi__)
2538	if (sc->tulip_boardsw->bd_type != TULIP_DC21040_ZX314_SLAVE) {
2539	    isa_establish(&sc->tulip_id, &sc->tulip_dev);
2540
2541	    sc->tulip_ih.ih_fun = tulip_intr;
2542	    sc->tulip_ih.ih_arg = (void *)sc;
2543	    intr_establish(ia->ia_irq, &sc->tulip_ih, DV_NET);
2544	}
2545
2546	sc->tulip_ats.func = tulip_shutdown;
2547	sc->tulip_ats.arg = (void *) sc;
2548	atshutdown(&sc->tulip_ats, ATSH_ADD);
2549#endif
2550	tulip_reset(sc);
2551	tulip_attach(sc);
2552    }
2553}
2554#endif /* NDE > 0 */
2555