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