if_de.c revision 20060
1/*-
2 * Copyright (c) 1994, 1995, 1996 Matt Thomas (matt@3am-software.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.55 1996/11/10 13:36:46 davidg Exp $
25 *
26 */
27
28/*
29 * DEC 21040 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 21040, 21041, or 21140 (mostly).
36 */
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/mbuf.h>
41#include <sys/protosw.h>
42#include <sys/socket.h>
43#include <sys/ioctl.h>
44#include <sys/errno.h>
45#include <sys/malloc.h>
46#include <sys/kernel.h>
47#include <sys/proc.h>	/* only for declaration of wakeup() used by vm.h */
48#if defined(__FreeBSD__)
49#include <machine/clock.h>
50#elif defined(__bsdi__) || defined(__NetBSD__)
51#include <sys/device.h>
52#endif
53
54#include <net/if.h>
55#include <net/if_dl.h>
56#include <net/if_mib.h>
57#include <net/if_types.h>
58#include <net/route.h>
59#include <net/netisr.h>
60
61#include "bpfilter.h"
62#if NBPFILTER > 0
63#include <net/bpf.h>
64#include <net/bpfdesc.h>
65#endif
66
67#ifdef INET
68#include <netinet/in.h>
69#include <netinet/in_systm.h>
70#include <netinet/in_var.h>
71#include <netinet/ip.h>
72#include <netinet/if_ether.h>
73#endif
74
75#ifdef NS
76#include <netns/ns.h>
77#include <netns/ns_if.h>
78#endif
79
80#include <vm/vm.h>
81#include <vm/vm_param.h>
82#include <vm/vm_kern.h>
83
84#if defined(__FreeBSD__)
85#include <vm/pmap.h>
86#include "pci.h"
87#if NPCI > 0
88#include <pci/pcivar.h>
89#include <pci/dc21040.h>
90#endif
91#endif /* __FreeBSD__ */
92
93#if defined(__bsdi__)
94#include <i386/pci/pci.h>
95#include <i386/pci/ic/dc21040.h>
96#include <i386/isa/isa.h>
97#include <i386/isa/icu.h>
98#include <i386/isa/dma.h>
99#include <i386/isa/isavar.h>
100#if _BSDI_VERSION < 199510
101#include <eisa.h>
102#else
103#define	NEISA 0
104#endif
105#if NEISA > 0 && _BSDI_VERSION >= 199401
106#include <i386/eisa/eisa.h>
107#define	TULIP_EISA
108#endif
109#endif /* __bsdi__ */
110
111#if defined(__NetBSD__)
112#include <machine/bus.h>
113#if defined(__alpha__)
114#include <machine/intr.h>
115#endif
116#include <dev/pci/pcireg.h>
117#include <dev/pci/pcivar.h>
118#include <dev/ic/dc21040reg.h>
119#endif /* __NetBSD__ */
120
121/*
122 * Intel CPUs should use I/O mapped access.
123 */
124#if defined(__i386__) || defined(TULIP_EISA)
125#define	TULIP_IOMAPPED
126#endif
127
128#if 0
129/*
130 * This turns on all sort of debugging stuff and make the
131 * driver much larger.
132 */
133#define TULIP_DEBUG
134#endif
135
136#if 0
137#define	TULIP_USE_SOFTINTR
138#endif
139
140/*
141 * This module supports
142 *	the DEC 21040 PCI Ethernet Controller.
143 *	the DEC 21041 PCI Ethernet Controller.
144 *	the DEC 21140 PCI Fast Ethernet Controller.
145 */
146
147#ifdef TULIP_IOMAPPED
148#define	TULIP_EISA_CSRSIZE	16
149#define	TULIP_EISA_CSROFFSET	0
150#define	TULIP_PCI_CSRSIZE	8
151#define	TULIP_PCI_CSROFFSET	0
152
153#if defined(__NetBSD__)
154typedef bus_io_size_t tulip_csrptr_t;
155
156#define TULIP_CSR_READ(sc, csr) \
157    bus_io_read_4((sc)->tulip_bc, (sc)->tulip_ioh, (sc)->tulip_csrs.csr)
158#define TULIP_CSR_WRITE(sc, csr, val) \
159    bus_io_write_4((sc)->tulip_bc, (sc)->tulip_ioh, (sc)->tulip_csrs.csr, (val))
160
161#define TULIP_CSR_READBYTE(sc, csr) \
162    bus_io_read_1((sc)->tulip_bc, (sc)->tulip_ioh, (sc)->tulip_csrs.csr)
163#define TULIP_CSR_WRITEBYTE(sc, csr, val) \
164    bus_io_write_1((sc)->tulip_bc, (sc)->tulip_ioh, (sc)->tulip_csrs.csr, (val))
165#else
166typedef u_int16_t tulip_csrptr_t;
167
168#define	TULIP_CSR_READ(sc, csr)			(inl((sc)->tulip_csrs.csr))
169#define	TULIP_CSR_WRITE(sc, csr, val)   	outl((sc)->tulip_csrs.csr, val)
170
171#define	TULIP_CSR_READBYTE(sc, csr)		(inb((sc)->tulip_csrs.csr))
172#define	TULIP_CSR_WRITEBYTE(sc, csr, val)	outb((sc)->tulip_csrs.csr, val)
173#endif /* __NetBSD__ */
174
175#else /* TULIP_IOMAPPED */
176
177#define	TULIP_PCI_CSRSIZE	8
178#define	TULIP_PCI_CSROFFSET	0
179
180#if defined(__NetBSD__)
181typedef bus_mem_size_t tulip_csrptr_t;
182
183#define TULIP_CSR_READ(sc, csr) \
184    bus_mem_read_4((sc)->tulip_bc, (sc)->tulip_memh, (sc)->tulip_csrs.csr)
185#define TULIP_CSR_WRITE(sc, csr, val) \
186    bus_mem_write_4((sc)->tulip_bc, (sc)->tulip_memh, (sc)->tulip_csrs.csr, \
187      (val))
188#else
189typedef volatile u_int32_t *tulip_csrptr_t;
190
191/*
192 * macros to read and write CSRs.  Note that the "0 +" in
193 * READ_CSR is to prevent the macro from being an lvalue
194 * and WRITE_CSR shouldn't be assigned from.
195 */
196#define	TULIP_CSR_READ(sc, csr)		(0 + *(sc)->tulip_csrs.csr)
197#define	TULIP_CSR_WRITE(sc, csr, val)	((void)(*(sc)->tulip_csrs.csr = (val)))
198#endif /* __NetBSD__ */
199
200#endif /* TULIP_IOMAPPED */
201
202/*
203 * This structure contains "pointers" for the registers on
204 * the various 21x4x chips.  CSR0 through CSR8 are common
205 * to all chips.  After that, it gets messy...
206 */
207typedef struct {
208    tulip_csrptr_t csr_busmode;			/* CSR0 */
209    tulip_csrptr_t csr_txpoll;			/* CSR1 */
210    tulip_csrptr_t csr_rxpoll;			/* CSR2 */
211    tulip_csrptr_t csr_rxlist;			/* CSR3 */
212    tulip_csrptr_t csr_txlist;			/* CSR4 */
213    tulip_csrptr_t csr_status;			/* CSR5 */
214    tulip_csrptr_t csr_command;			/* CSR6 */
215    tulip_csrptr_t csr_intr;			/* CSR7 */
216    tulip_csrptr_t csr_missed_frames;		/* CSR8 */
217
218    /* 21040 specific registers */
219
220    tulip_csrptr_t csr_enetrom;			/* CSR9 */
221    tulip_csrptr_t csr_reserved;		/* CSR10 */
222    tulip_csrptr_t csr_full_duplex;		/* CSR11 */
223
224    /* 21040/21041 common registers */
225
226    tulip_csrptr_t csr_sia_status;		/* CSR12 */
227    tulip_csrptr_t csr_sia_connectivity;	/* CSR13 */
228    tulip_csrptr_t csr_sia_tx_rx;		/* CSR14 */
229    tulip_csrptr_t csr_sia_general;		/* CSR15 */
230
231    /* 21140/21041 common registers */
232
233    tulip_csrptr_t csr_srom_mii;		/* CSR9 */
234    tulip_csrptr_t csr_gp_timer;		/* CSR11 */
235
236    /* 21140 specific registers */
237
238    tulip_csrptr_t csr_gp;			/* CSR12 */
239    tulip_csrptr_t csr_watchdog;		/* CSR15 */
240
241    /* 21041 specific registers */
242
243    tulip_csrptr_t csr_bootrom;			/* CSR10 */
244} tulip_regfile_t;
245
246/*
247 * While 21x4x allows chaining of its descriptors, this driver
248 * doesn't take advantage of it.  We keep the descriptors in a
249 * traditional FIFO ring.
250 */
251typedef struct {
252    tulip_desc_t *ri_first;	/* first entry in ring */
253    tulip_desc_t *ri_last;	/* one after last entry */
254    tulip_desc_t *ri_nextin;	/* next to processed by host */
255    tulip_desc_t *ri_nextout;	/* next to processed by adapter */
256    int ri_max;
257    int ri_free;
258} tulip_ringinfo_t;
259
260/*
261 * The 21040 has a stupid restriction in that the receive
262 * buffers must be longword aligned.  But since Ethernet
263 * headers are not a multiple of longwords in size this forces
264 * the data to non-longword aligned.  Since IP requires the
265 * data to be longword aligned, we need to copy it after it has
266 * been DMA'ed in our memory.
267 *
268 * Since we have to copy it anyways, we might as well as allocate
269 * dedicated receive space for the input.  This allows to use a
270 * small receive buffer size and more ring entries to be able to
271 * better keep with a flood of tiny Ethernet packets.
272 *
273 * The receive space MUST ALWAYS be a multiple of the page size.
274 * And the number of receive descriptors multiplied by the size
275 * of the receive buffers must equal the recevive space.  This
276 * is so that we can manipulate the page tables so that even if a
277 * packet wraps around the end of the receive space, we can
278 * treat it as virtually contiguous.
279 *
280 * The above used to be true (the stupid restriction is still true)
281 * but we gone to directly DMA'ing into MBUFs (unless it's on an
282 * architecture which can't handle unaligned accesses) because with
283 * 100Mb/s cards the copying is just too much of a hit.
284 */
285#if defined(__alpha__)
286#define	TULIP_COPY_RXDATA	1
287#endif
288
289#define	TULIP_TXTIMER		3
290#define	TULIP_RXDESCS		48
291#define	TULIP_TXDESCS		128
292#define	TULIP_RXQ_TARGET	32
293#if TULIP_RXQ_TARGET >= TULIP_RXDESCS
294#error TULIP_RXQ_TARGET must be less than TULIP_RXDESCS
295#endif
296#define	TULIP_RX_BUFLEN		((MCLBYTES < 2048 ? MCLBYTES : 2048) - 16)
297
298/*
299 * Forward reference to make C happy.
300 */
301typedef struct _tulip_softc_t tulip_softc_t;
302
303/*
304 * The various controllers support.  Technically the DE425 is just
305 * a 21040 on EISA.  But since it remarkably difference from normal
306 * 21040s, we give it its own chip id.
307 */
308
309typedef enum {
310    TULIP_21040, TULIP_DE425,
311    TULIP_21041,
312    TULIP_21140, TULIP_21140A, TULIP_21142,
313    TULIP_21143,
314    TULIP_CHIPID_UNKNOWN
315} tulip_chipid_t;
316
317/*
318 * Various physical media types supported.
319 * BNCAUI is BNC or AUI since on the 21040 you can't really tell
320 * which is in use.
321 */
322typedef enum {
323    TULIP_MEDIA_UNKNOWN,
324    TULIP_MEDIA_10BASET,
325    TULIP_MEDIA_BNC,
326    TULIP_MEDIA_AUI,
327    TULIP_MEDIA_BNCAUI,
328    TULIP_MEDIA_10BASET_FD,
329    TULIP_MEDIA_100BASETX,
330    TULIP_MEDIA_100BASETX_FD,
331    TULIP_MEDIA_100BASET4,
332    TULIP_MEDIA_100BASEFX,
333    TULIP_MEDIA_100BASEFX_FD,
334    TULIP_MEDIA_MAX
335} tulip_media_t;
336
337/*
338 * Some boards need to treated specially.  The following enumeration
339 * identifies the cards with quirks (or those we just want to single
340 * out for special merit or scorn).
341 */
342typedef enum {
343    TULIP_21040_GENERIC,		/* Generic 21040 (works with most any board) */
344    TULIP_21040_ZX314_MASTER,		/* ZNYX ZX314 Master 21040 (it has the interrupt line) */
345    TULIP_21040_ZX314_SLAVE,		/* ZNYX ZX314 Slave 21040 (its interrupt is tied to the master's */
346    TULIP_21140_DEC_EB,			/* Digital Semicondutor 21140 Evaluation Board */
347    TULIP_21140_DEC_DE500,		/* Digital DE500-?? 10/100 */
348    TULIP_21140_SMC_9332,		/* SMC 9332 */
349    TULIP_21140_COGENT_EM100,		/* Cogent EM100 100 only */
350    TULIP_21140_ZNYX_ZX34X,		/* ZNYX ZX342 10/100 */
351    TULIP_21041_GENERIC,		/* Generic 21041 card */
352    TULIP_21041_DEC_DE450		/* Digital DE450 */
353} tulip_board_t;
354
355/*
356 * This data structure is used to abstract out the quirks.
357 * media_probe  = tries to determine the media type.
358 * media_select = enables the current media (or autosenses)
359 * media_preset = 21140, etal requires bit to set before the
360 *		  the software reset; hence pre-set.  Should be
361 *		  pre-reset but that's ugly.
362 * mii_probe	= probe for PHY devices connected via the MII interface
363 *		  on 21140, etal.
364 */
365
366typedef struct {
367    tulip_board_t bd_type;
368    const char *bd_description;
369    int (*bd_media_probe)(tulip_softc_t *sc);
370    void (*bd_media_select)(tulip_softc_t *sc);
371    void (*bd_media_preset)(tulip_softc_t *sc);
372    void (*bd_mii_probe)(tulip_softc_t *sc);
373} tulip_boardsw_t;
374
375/*
376 * The next few declarations are for MII/PHY based board.
377 *
378 *    The first enumeration identifies a superset of various datums
379 * that can be obtained from various PHY chips.  Not all PHYs will
380 * support all datums.
381 *    The modedata structure indicates what register contains
382 * a datum, what mask is applied the register contents, and what the
383 * result should be.
384 *    The attr structure records information about a supported PHY.
385 *    The phy structure records information about a PHY instance.
386 */
387
388typedef enum {
389    PHY_MODE_10T,
390    PHY_MODE_100TX,
391    PHY_MODE_100T4,
392    PHY_MODE_FULLDUPLEX,
393    PHY_MODE_MAX
394} phy_mode_t;
395
396typedef struct {
397    unsigned short pm_regno;
398    unsigned short pm_mask;
399    unsigned short pm_value;
400} phy_modedata_t;
401
402typedef struct {
403    const char *attr_name;
404    unsigned attr_id;
405    unsigned short attr_flags;
406#define	PHY_NEED_HARD_RESET	0x0001
407#define	PHY_DUAL_CYCLE_TA	0x0002
408    phy_modedata_t attr_modes[PHY_MODE_MAX];
409} phy_attr_t;
410
411typedef struct _tulip_phy_t {
412    const struct _tulip_phy_t *phy_next;
413    const phy_attr_t *phy_attr;
414    unsigned phy_devaddr;
415    unsigned phy_status;
416} tulip_phy_t;
417
418/*
419 * Various probe states used when trying to autosense the media.
420 * While we could try to autosense on the 21040, it a pain and so
421 * until someone complain we won't.  However, the 21041 and MII
422 * 2114x do support autosense.
423 */
424
425typedef enum {
426    TULIP_PROBE_INACTIVE, TULIP_PROBE_10BASET, TULIP_PROBE_AUI,
427    TULIP_PROBE_BNC, TULIP_PROBE_PHYRESET, TULIP_PROBE_PHYAUTONEG,
428    TULIP_PROBE_MEDIATEST, TULIP_PROBE_FAILED
429} tulip_probe_state_t;
430
431typedef struct {
432    /*
433     * Transmit Statistics
434     */
435    u_int32_t dot3StatsSingleCollisionFrames;
436    u_int32_t dot3StatsMultipleCollisionFrames;
437    u_int32_t dot3StatsSQETestErrors;
438    u_int32_t dot3StatsDeferredTransmissions;
439    u_int32_t dot3StatsLateCollisions;
440    u_int32_t dot3StatsExcessiveCollisions;
441    u_int32_t dot3StatsInternalMacTransmitErrors;
442    u_int32_t dot3StatsCarrierSenseErrors;
443    /*
444     * Receive Statistics
445     */
446    u_int32_t dot3StatsMissedFrames;	/* not in rfc1650! */
447    u_int32_t dot3StatsAlignmentErrors;
448    u_int32_t dot3StatsFCSErrors;
449    u_int32_t dot3StatsFrameTooLongs;
450    u_int32_t dot3StatsInternalMacReceiveErrors;
451} tulip_dot3_stats_t;
452
453/*
454 * Now to important stuff.  This is softc structure (where does softc
455 * come from??? No idea) for the tulip device.
456 *
457 */
458struct _tulip_softc_t {
459#if defined(__bsdi__)
460    struct device tulip_dev;		/* base device */
461    struct isadev tulip_id;		/* ISA device */
462    struct intrhand tulip_ih;		/* intrrupt vectoring */
463    struct atshutdown tulip_ats;	/* shutdown hook */
464#if _BSDI_VERSION < 199401
465    caddr_t tulip_bpf;			/* for BPF */
466#else
467    prf_t tulip_pf;			/* printf function */
468#endif
469#endif
470#if defined(__NetBSD__)
471    struct device tulip_dev;		/* base device */
472    void *tulip_ih;			/* intrrupt vectoring */
473    void *tulip_ats;			/* shutdown hook */
474    bus_chipset_tag_t tulip_bc;
475    pci_chipset_tag_t tulip_pc;
476#ifdef TULIP_IOMAPPED
477    bus_io_handle_t tulip_ioh;		/* I/O region handle */
478#else
479    bus_io_handle_t tulip_memh;		/* memory region handle */
480#endif
481#endif
482    struct arpcom tulip_ac;
483    tulip_regfile_t tulip_csrs;
484    unsigned tulip_flags;
485#define	TULIP_WANTSETUP		0x00000001
486#define	TULIP_WANTHASH		0x00000002
487#define	TULIP_DOINGSETUP	0x00000004
488#define	TULIP_ALTPHYS		0x00000008
489#define	TULIP_PRINTMEDIA	0x00000010
490#define	TULIP_TXPROBE_ACTIVE	0x00000020
491#define	TULIP_TXPROBE_OK	0x00000040
492#define	TULIP_WANTRXACT		0x00000080
493#define	TULIP_RXACT		0x00000100
494#define	TULIP_INRESET		0x00000200
495#define	TULIP_NEEDRESET		0x00000400
496#define	TULIP_SQETEST		0x00000800
497#define	TULIP_ROMOK		0x00001000
498#define	TULIP_SLAVEDROM		0x00002000
499#define	TULIP_SLAVEDINTR	0x00004000
500#define	TULIP_SHAREDINTR	0x00008000
501#define	TULIP_LINKSUSPECT	0x00010000
502#define	TULIP_LINKUP		0x00020000
503#define	TULIP_RXBUFSLOW		0x00040000
504#define	TULIP_NOMESSAGES	0x00080000
505#define	TULIP_SYSTEMERROR	0x00100000
506#define	TULIP_DEVICEPROBE	0x00200000
507#define	TULIP_FAKEGPTIMEOUT	0x00400000
508    /* only 10 bits! */
509    unsigned char tulip_rombuf[128];
510    u_int32_t tulip_setupbuf[192/sizeof(u_int32_t)];
511    u_int32_t tulip_setupdata[192/sizeof(u_int32_t)];
512    u_int32_t tulip_intrmask;	/* our copy of csr_intr */
513    u_int32_t tulip_cmdmode;	/* our copy of csr_cmdmode */
514    u_int32_t tulip_revinfo;	/* revision of chip */
515    u_int32_t tulip_gpticks;	/* number of gpticks unless timeout */
516    u_int32_t tulip_last_system_error : 3;	/* last system error (only value is TULIP_SYSTEMERROR is also set) */
517    u_int32_t tulip_txtimer : 2;	/* transmission timer */
518    u_int32_t tulip_system_errors;	/* number of system errors encountered */
519    u_int32_t tulip_statusbits;	/* status bits from CSR5 that may need to be printed */
520    u_int32_t tulip_abilities;	/* remote system's abiltities (as defined in IEEE 802.3u) */
521    /* u_int32_t tulip_bus; XXX */
522    tulip_media_t tulip_media;		/* current media type */
523    tulip_probe_state_t tulip_probe_state;	/* current media probe state */
524    tulip_chipid_t tulip_chipid;	/* type of chip we are using */
525    const char *tulip_boardid;		/* string for board ID */
526    char tulip_boardidbuf[16];		/* buffer for board ID */
527    const tulip_boardsw_t *tulip_boardsw; /* board/chip characteristics */
528    tulip_softc_t *tulip_slaves;	/* slaved devices (ZX3xx) */
529    tulip_phy_t *tulip_phys;		/* 802.3 PHY devices */
530#if defined(TULIP_DEBUG)
531    /*
532     * Debugging/Statistical information
533     */
534    struct {
535	u_int32_t dbg_intrs;
536	u_int32_t dbg_msdelay;
537	u_int32_t dbg_gpticks;
538	enum {
539	    TULIP_GPTMR_10MB,
540	    TULIP_GPTMR_10MB_MII,
541	    TULIP_GPTMR_100MB_MII
542	} dbg_gprate;
543	u_int32_t dbg_gpintrs;
544	u_int32_t dbg_gpintrs_hz;
545	u_int32_t dbg_link_downed;
546	u_int32_t dbg_link_suspected;
547	u_int16_t dbg_phyregs[32][4];
548	u_int32_t dbg_rxlowbufs;
549	u_int32_t dbg_rxintrs;
550	u_int32_t dbg_last_rxintrs;
551	u_int32_t dbg_high_rxintrs_hz;
552	u_int32_t dbg_rxpktsperintr[TULIP_RXDESCS];
553    } tulip_dbg;
554#endif
555    struct ifqueue tulip_txq;
556    struct ifqueue tulip_rxq;
557    struct ifmib_iso_8802_3 tulip_dot3stats;
558    tulip_ringinfo_t tulip_rxinfo;
559    tulip_ringinfo_t tulip_txinfo;
560    tulip_desc_t tulip_rxdescs[TULIP_RXDESCS];
561    tulip_desc_t tulip_txdescs[TULIP_TXDESCS];
562};
563
564static const char * const tulip_chipdescs[] = {
565    "21040 [10Mb/s]",
566#if defined(TULIP_EISA)
567    "DE425 [10Mb/s]",
568#else
569    NULL,
570#endif
571    "21041 [10Mb/s]",
572    "21140 [10-100Mb/s]",
573    "21140A [10-100Mb/s]",
574    "21142 [10-100Mb/s]",
575};
576
577#define chip(x) DOT3CHIPSET(dot3VendorDigital, dot3ChipSetDigital##x)
578static u_int32_t const tulip_chip2mib[] = {
579	chip(DC21040), chip(DC21040), chip(DC21041), chip(DC21140),
580	chip(DC21140A), chip(DC21142)
581};
582#undef chip
583
584static const char * const tulip_mediums[] = {
585    "unknown",			/* TULIP_MEDIA_UNKNOWN */
586    "10baseT",			/* TULIP_MEDIA_10BASET */
587    "BNC",			/* TULIP_MEDIA_BNC */
588    "AUI",			/* TULIP_MEDIA_AUI */
589    "BNC/AUI",			/* TULIP_MEDIA_BNCAUI */
590    "Full Duplex 10baseT",	/* TULIP_MEDIA_10BASET_FD */
591    "100baseTX",		/* TULIP_MEDIA_100BASET */
592    "Full Duplex 100baseTX",	/* TULIP_MEDIA_100BASET_FD */
593    "100baseT4",		/* TULIP_MEDIA_100BASET4 */
594};
595
596static const tulip_media_t tulip_phy_statuses[] = {
597    TULIP_MEDIA_10BASET, TULIP_MEDIA_10BASET_FD,
598    TULIP_MEDIA_100BASETX, TULIP_MEDIA_100BASETX_FD,
599    TULIP_MEDIA_100BASET4
600};
601
602static const char * const tulip_system_errors[] = {
603    "parity error",
604    "master abort",
605    "target abort",
606    "reserved #3",
607    "reserved #4",
608    "reserved #5",
609    "reserved #6",
610    "reserved #7",
611};
612
613static const char * const tulip_status_bits[] = {
614    NULL,
615    "transmit process stopped",
616    NULL,
617    "transmit jabber timeout",
618
619    NULL,
620    "transmit underflow",
621    NULL,
622    "receive underflow",
623
624    "receive process stopped",
625    "receive watchdog timeout",
626    NULL,
627    NULL,
628
629    "link failure",
630    NULL,
631    NULL,
632};
633
634#ifndef IFF_ALTPHYS
635#define	IFF_ALTPHYS	IFF_LINK2		/* In case it isn't defined */
636#endif
637
638#ifndef IFF_FULLDUPLEX
639#define	IFF_FULLDUPLEX	IFF_LINK1
640#endif
641
642#ifndef	IFF_NOAUTONEG
643#if IFF_ALTPHYS == IFF_LINK2
644#define	IFF_NOAUTONEG	IFF_LINK0
645#else
646#define	IFF_NOAUTONEG	IFF_LINK2
647#endif
648#endif
649
650#if (IFF_ALTPHYS&IFF_FULLDUPLEX&IFF_NOAUTONEG) != 0
651#error IFF_ALTPHYS, IFF_FULLDUPLEX, IFF_NOAUTONEG overlap
652#endif
653
654/*
655 * This driver supports a maximum of 32 tulip boards.
656 * This should be enough for the forseeable future.
657 */
658#define	TULIP_MAX_DEVICES	32
659
660#if defined(TULIP_USE_SOFTINTR)
661static u_int32_t tulip_softintr_mask;
662static int tulip_softintr_last_unit;
663static int tulip_softintr_max_unit;
664static void tulip_softintr(void);
665#endif
666
667
668#if defined(__FreeBSD__)
669typedef void ifnet_ret_t;
670typedef int ioctl_cmd_t;
671tulip_softc_t *tulips[TULIP_MAX_DEVICES];
672#if BSD >= 199506
673#define TULIP_IFP_TO_SOFTC(ifp) ((tulip_softc_t *)((ifp)->if_softc))
674#if NBPFILTER > 0
675#define	TULIP_BPF_MTAP(sc, m)	bpf_mtap(&(sc)->tulip_if, m)
676#define	TULIP_BPF_TAP(sc, p, l)	bpf_tap(&(sc)->tulip_if, p, l)
677#define	TULIP_BPF_ATTACH(sc)	bpfattach(&(sc)->tulip_if, DLT_EN10MB, sizeof(struct ether_header))
678#endif
679#define	tulip_intrfunc_t	void
680#define	TULIP_VOID_INTRFUNC
681#define	IFF_NOTRAILERS		0
682#define	CLBYTES			PAGE_SIZE
683#if 0
684#define	TULIP_KVATOPHYS(sc, va)	kvtop(va)
685#endif
686#define	TULIP_EADDR_FMT		"%6D"
687#define	TULIP_EADDR_ARGS(addr)	addr, ":"
688#else
689extern int bootverbose;
690#define TULIP_IFP_TO_SOFTC(ifp)         (TULIP_UNIT_TO_SOFTC((ifp)->if_unit))
691#endif
692#if defined(TULIP_USE_SOFTINTR)
693NETISR_SET(NETISR_DE, tulip_softintr);
694#endif
695#define	TULIP_UNIT_TO_SOFTC(unit)	(tulips[unit])
696#define	TULIP_BURSTSIZE(unit)		pci_max_burst_len
697#define	loudprintf			if (bootverbose) printf
698#endif
699
700#if defined(__bsdi__)
701typedef int ifnet_ret_t;
702typedef int ioctl_cmd_t;
703extern struct cfdriver decd;
704#define	TULIP_UNIT_TO_SOFTC(unit)	((tulip_softc_t *) decd.cd_devs[unit])
705#define TULIP_IFP_TO_SOFTC(ifp)		(TULIP_UNIT_TO_SOFTC((ifp)->if_unit))
706#if _BSDI_VERSION >= 199510
707#if 0
708#define	TULIP_BURSTSIZE(unit)		log2_burst_size
709#endif
710#define	loudprintf			aprint_verbose
711#define	printf				(*sc->tulip_pf)
712#elif _BSDI_VERSION <= 199401
713#define	DRQNONE				0
714#define	loudprintf			printf
715static void
716arp_ifinit(
717    struct arpcom *ac,
718    struct ifaddr *ifa)
719{
720    ac->ac_ipaddr = IA_SIN(ifa)->sin_addr;
721    arpwhohas(ac, &ac->ac_ipaddr);
722}
723#endif
724#endif	/* __bsdi__ */
725
726#if defined(__NetBSD__)
727typedef void ifnet_ret_t;
728typedef u_long ioctl_cmd_t;
729extern struct cfattach de_ca;
730extern struct cfdriver de_cd;
731#define	TULIP_UNIT_TO_SOFTC(unit)	((tulip_softc_t *) de_cd.cd_devs[unit])
732#define TULIP_IFP_TO_SOFTC(ifp)         ((tulip_softc_t *)((ifp)->if_softc))
733#define	tulip_xname			tulip_ac.ac_if.if_xname
734#define	tulip_unit			tulip_dev.dv_unit
735#define	loudprintf			printf
736#define	TULIP_PRINTF_FMT		"%s"
737#define	TULIP_PRINTF_ARGS		sc->tulip_xname
738#if defined(__alpha__)
739/* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
740#define TULIP_KVATOPHYS(va)		(vtophys(va) | 0x40000000)
741#endif
742#endif	/* __NetBSD__ */
743
744#ifndef TULIP_PRINTF_FMT
745#define	TULIP_PRINTF_FMT		"%s%d"
746#endif
747#ifndef TULIP_PRINTF_ARGS
748#define	TULIP_PRINTF_ARGS		sc->tulip_name, sc->tulip_unit
749#endif
750
751#ifndef TULIP_BURSTSIZE
752#define	TULIP_BURSTSIZE(unit)		3
753#endif
754
755#define	tulip_if	tulip_ac.ac_if
756#ifndef tulip_unit
757#define	tulip_unit	tulip_ac.ac_if.if_unit
758#endif
759#define	tulip_name	tulip_ac.ac_if.if_name
760#define	tulip_hwaddr	tulip_ac.ac_enaddr
761
762#if !defined(tulip_bpf) && (!defined(__bsdi__) || _BSDI_VERSION >= 199401)
763#define	tulip_bpf	tulip_ac.ac_if.if_bpf
764#endif
765
766#if !defined(tulip_intrfunc_t)
767#define	tulip_intrfunc_t	int
768#endif
769
770#if !defined(TULIP_KVATOPHYS)
771#define	TULIP_KVATOPHYS(sc, va)	vtophys(va)
772#endif
773
774/*
775 * While I think FreeBSD's 2.2 change to the bpf is a nice simplification,
776 * it does add yet more conditional code to this driver.  Sigh.
777 */
778#if !defined(TULIP_BPF_MTAP) && NBPFILTER > 0
779#define	TULIP_BPF_MTAP(sc, m)	bpf_mtap((sc)->tulip_bpf, m)
780#define	TULIP_BPF_TAP(sc, p, l)	bpf_tap((sc)->tulip_bpf, p, l)
781#define	TULIP_BPF_ATTACH(sc)	bpfattach(&(sc)->tulip_bpf, &(sc)->tulip_if, DLT_EN10MB, sizeof(struct ether_header))
782#endif
783
784/*
785 * However, this change to FreeBSD I am much less enamored with.
786 */
787#if !defined(TULIP_EADDR_FMT)
788#define	TULIP_EADDR_FMT		"%s"
789#define	TULIP_EADDR_ARGS(addr)	ether_sprintf(addr)
790#endif
791
792#define	TULIP_CRC32_POLY	0xEDB88320UL	/* CRC-32 Poly -- Little Endian */
793#define	TULIP_MAX_TXSEG		30
794
795#define	TULIP_ADDREQUAL(a1, a2) \
796	(((u_int16_t *)a1)[0] == ((u_int16_t *)a2)[0] \
797	 && ((u_int16_t *)a1)[1] == ((u_int16_t *)a2)[1] \
798	 && ((u_int16_t *)a1)[2] == ((u_int16_t *)a2)[2])
799#define	TULIP_ADDRBRDCST(a1) \
800	(((u_int16_t *)a1)[0] == 0xFFFFU \
801	 && ((u_int16_t *)a1)[1] == 0xFFFFU \
802	 && ((u_int16_t *)a1)[2] == 0xFFFFU)
803
804typedef int tulip_spl_t;
805
806static tulip_intrfunc_t tulip_intr_shared(void *arg);
807static tulip_intrfunc_t tulip_intr_normal(void *arg);
808static void tulip_reset(tulip_softc_t * const sc);
809static ifnet_ret_t tulip_ifstart(struct ifnet *ifp);
810static void tulip_rx_intr(tulip_softc_t * const sc);
811static void tulip_addr_filter(tulip_softc_t * const sc);
812static unsigned tulip_mii_readreg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno);
813static void tulip_mii_writereg(tulip_softc_t * const sc, unsigned devaddr, unsigned regno, unsigned data);
814
815static int
816tulip_21040_media_probe(
817    tulip_softc_t * const sc)
818{
819    int cnt;
820
821    TULIP_CSR_WRITE(sc, csr_sia_connectivity, 0);
822    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
823    for (cnt = 0; cnt < 2400; cnt++) {
824	if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) == 0)
825	    break;
826	DELAY(1000);
827    }
828    sc->tulip_if.if_baudrate = 10000000;
829    return (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) != 0;
830}
831
832static void
833tulip_21040_media_select(
834    tulip_softc_t * const sc)
835{
836    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
837	|TULIP_CMD_BACKOFFCTR;
838    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
839    sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
840    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
841	TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_AUI);
842	sc->tulip_media = TULIP_MEDIA_BNCAUI;
843	sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
844	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
845	    sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
846    } else {
847	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
848	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
849	    sc->tulip_media = TULIP_MEDIA_10BASET_FD;
850	    sc->tulip_flags &= ~TULIP_SQETEST;
851	} else {
852	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
853	    sc->tulip_media = TULIP_MEDIA_10BASET;
854	}
855	if (sc->tulip_flags & TULIP_ALTPHYS)
856	    sc->tulip_flags ^= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
857	TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
858    }
859}
860
861static int
862tulip_21040_10baset_only_media_probe(
863    tulip_softc_t * const sc)
864{
865    TULIP_CSR_WRITE(sc, csr_sia_connectivity, 0);
866    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
867    sc->tulip_if.if_baudrate = 10000000;
868    return 0;
869}
870
871static void
872tulip_21040_10baset_only_media_select(
873    tulip_softc_t * const sc)
874{
875    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
876	|TULIP_CMD_BACKOFFCTR;
877    sc->tulip_flags |= TULIP_LINKUP;
878    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
879    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_10BASET);
880    if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
881	sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
882	sc->tulip_media = TULIP_MEDIA_10BASET_FD;
883	sc->tulip_flags &= ~TULIP_SQETEST;
884    } else {
885	sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
886	sc->tulip_media = TULIP_MEDIA_10BASET;
887	sc->tulip_flags |= TULIP_SQETEST;
888    }
889    if (sc->tulip_flags & TULIP_ALTPHYS)
890	sc->tulip_flags ^= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
891    sc->tulip_flags &= ~TULIP_ALTPHYS;
892}
893
894static int
895tulip_21040_auibnc_only_media_probe(
896    tulip_softc_t * const sc)
897{
898    TULIP_CSR_WRITE(sc, csr_sia_connectivity, 0);
899    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_AUI);
900    sc->tulip_if.if_baudrate = 10000000;
901    sc->tulip_flags |= TULIP_SQETEST|TULIP_LINKUP;
902    return 0;
903}
904
905static void
906tulip_21040_auibnc_only_media_select(
907    tulip_softc_t * const sc)
908{
909    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_THRSHLD160
910	|TULIP_CMD_BACKOFFCTR;
911    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
912    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_AUI);
913    if (sc->tulip_if.if_flags & IFF_FULLDUPLEX)
914	sc->tulip_if.if_flags &= ~IFF_FULLDUPLEX;
915    sc->tulip_media = TULIP_MEDIA_BNCAUI;
916    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
917    if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
918	sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
919    sc->tulip_flags &= ~TULIP_ALTPHYS;
920}
921
922static const tulip_boardsw_t tulip_21040_boardsw = {
923    TULIP_21040_GENERIC,
924    "",
925    tulip_21040_media_probe,
926    tulip_21040_media_select,
927    NULL,
928    NULL
929};
930
931static const tulip_boardsw_t tulip_21040_10baset_only_boardsw = {
932    TULIP_21040_GENERIC,
933    "",
934    tulip_21040_10baset_only_media_probe,
935    tulip_21040_10baset_only_media_select,
936    NULL,
937    NULL
938};
939
940static const tulip_boardsw_t tulip_21040_auibnc_only_boardsw = {
941    TULIP_21040_GENERIC,
942    "",
943    tulip_21040_auibnc_only_media_probe,
944    tulip_21040_auibnc_only_media_select,
945    NULL,
946    NULL
947};
948
949static const tulip_boardsw_t tulip_21040_zx314_master_boardsw = {
950    TULIP_21040_ZX314_MASTER,
951    "ZNYX ZX314 ",
952    tulip_21040_10baset_only_media_probe,
953    tulip_21040_10baset_only_media_select
954};
955
956static const tulip_boardsw_t tulip_21040_zx314_slave_boardsw = {
957    TULIP_21040_ZX314_SLAVE,
958    "ZNYX ZX314 ",
959    tulip_21040_10baset_only_media_probe,
960    tulip_21040_10baset_only_media_select
961};
962
963static const phy_attr_t tulip_phy_attrlist[] = {
964    { "NS DP83840", 0x20005c00, 0,	/* 08-00-17 */
965      {
966	{ 0x19, 0x40, 0x40 },	/* 10TX */
967	{ 0x19, 0x40, 0x00 },	/* 100TX */
968      }
969    },
970    { "Seeq 80C240", 0x0281F400, 0,	/* 00-A0-7D */
971      {
972	{ 0x12, 0x10, 0x00 },	/* 10T */
973	{ },			/* 100TX */
974	{ 0x12, 0x10, 0x10 },	/* 100T4 */
975	{ 0x12, 0x08, 0x08 },	/* FULL_DUPLEX */
976      }
977    },
978    { NULL }
979};
980
981static void
982tulip_21140_mii_probe(
983    tulip_softc_t * const sc)
984{
985    unsigned devaddr;
986
987    for (devaddr = 31; devaddr > 0; devaddr--) {
988	unsigned status = tulip_mii_readreg(sc, devaddr, PHYREG_STATUS);
989	unsigned media;
990	unsigned id;
991	const phy_attr_t *attr;
992	tulip_phy_t *phy;
993	const char *sep;
994	if (status == 0 || status == 0xFFFF || status < PHYSTS_10BASET)
995	    continue;
996	if ((status & PHYSTS_EXTENDED_REGS) == 0) {
997	    loudprintf(TULIP_PRINTF_FMT "(phy%d): skipping (no extended register set)\n",
998		   TULIP_PRINTF_ARGS, devaddr);
999	    continue;
1000	}
1001	id = (tulip_mii_readreg(sc, devaddr, PHYREG_IDLOW) << 16) |
1002	    tulip_mii_readreg(sc, devaddr, PHYREG_IDHIGH);
1003	for (attr = tulip_phy_attrlist; attr->attr_name != NULL; attr++) {
1004	    if ((id & ~0x0F) == attr->attr_id)
1005		break;
1006	}
1007	if (attr->attr_name == NULL) {
1008	    loudprintf(TULIP_PRINTF_FMT "(phy%d): skipping (unrecogized id 0x%08x)\n",
1009		   TULIP_PRINTF_ARGS, devaddr, id & ~0x0F);
1010	    continue;
1011	}
1012
1013	MALLOC(phy, tulip_phy_t *, sizeof(tulip_phy_t), M_DEVBUF, M_NOWAIT);
1014	if (phy == NULL) {
1015	    loudprintf(TULIP_PRINTF_FMT "(phy%d): skipping (memory allocation failed)\n",
1016		   TULIP_PRINTF_ARGS, devaddr);
1017	    continue;
1018	}
1019	phy->phy_attr = attr;
1020	phy->phy_devaddr = devaddr;
1021	phy->phy_status = status;
1022	phy->phy_next = sc->tulip_phys;
1023	sc->tulip_phys = phy;
1024
1025	loudprintf(TULIP_PRINTF_FMT "(phy%d): model = %s%s\n",
1026	       TULIP_PRINTF_ARGS,
1027	       phy->phy_devaddr, phy->phy_attr->attr_name,
1028	       (phy->phy_status & PHYSTS_CAN_AUTONEG)
1029	           ? " (supports media autonegotiation)"
1030	           : "");
1031	loudprintf(TULIP_PRINTF_FMT "(phy%d): media = ",
1032	       TULIP_PRINTF_ARGS, phy->phy_devaddr);
1033	for (media = 11, sep = ""; media < 16; media++) {
1034	    if (status & (1 << media)) {
1035		loudprintf("%s%s", sep, tulip_mediums[tulip_phy_statuses[media-11]]);
1036		sep = ", ";
1037	    }
1038	}
1039	loudprintf("\n");
1040    }
1041}
1042
1043/*
1044 * The general purpose timer of the 21140/21140a/21142 is kind
1045 * of strange.  It can run on one of 3 speeds depending on the mode
1046 * of the chip.
1047 *
1048 *	10Mb/s port	204.8  microseconds (also speed of 21041 timer)
1049 *	100Mb/s MII	 81.92 microseconds
1050 *	10Mb/s MII	819.2  microseconds
1051 *
1052 * So we use a tick of a 819.2 microseconds and bias the number of ticks
1053 * required based on the mode in which we are running.  2560/3125 = .8192
1054 * so we use the reciprocal to scale the ms delay to 21140 ticks.
1055 */
1056static void
1057tulip_21140_gp_timer_set(
1058    tulip_softc_t * const sc,
1059    unsigned msdelay)
1060{
1061    u_int32_t cmdmode = TULIP_CSR_READ(sc, csr_command);
1062#ifdef TULIP_DEBUG
1063    sc->tulip_dbg.dbg_msdelay = msdelay;
1064#endif
1065    if ((cmdmode & TULIP_CMD_PORTSELECT) == 0) {
1066	msdelay *= 4;
1067#ifdef TULIP_DEBUG
1068	sc->tulip_dbg.dbg_gprate = TULIP_GPTMR_10MB_MII;
1069#endif
1070    } else if ((cmdmode & TULIP_CMD_TXTHRSHLDCTL) == 0) {
1071	msdelay *= 10;
1072#ifdef TULIP_DEBUG
1073	sc->tulip_dbg.dbg_gprate = TULIP_GPTMR_100MB_MII;
1074    } else {
1075	sc->tulip_dbg.dbg_gprate = TULIP_GPTMR_10MB;
1076#endif
1077    }
1078#if 0
1079    if (sc->tulip_chipid == TULIP_21140A)
1080	msdelay *= 10;
1081#endif
1082    TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_GPTIMEOUT);
1083    TULIP_CSR_WRITE(sc, csr_gp_timer, (msdelay * 313 + 128) / 256);
1084    if (sc->tulip_flags & TULIP_DEVICEPROBE) {
1085	sc->tulip_flags |= TULIP_FAKEGPTIMEOUT;
1086    } else {
1087	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
1088	sc->tulip_flags &= ~TULIP_FAKEGPTIMEOUT;
1089    }
1090#ifdef TULIP_DEBUG
1091    sc->tulip_dbg.dbg_gpticks = (msdelay * 313 + 128) / 256;
1092#endif
1093}
1094
1095static int
1096tulip_21140_map_abilities(
1097    tulip_softc_t * const sc,
1098    const tulip_phy_t * const phy,
1099    unsigned abilities)
1100{
1101    sc->tulip_abilities = abilities;
1102    if (abilities & PHYSTS_100BASETX_FD) {
1103	sc->tulip_media = TULIP_MEDIA_100BASETX_FD;
1104    } else if (abilities & PHYSTS_100BASETX) {
1105	sc->tulip_media = TULIP_MEDIA_100BASETX;
1106    } else if (abilities & PHYSTS_100BASET4) {
1107	sc->tulip_media = TULIP_MEDIA_100BASET4;
1108    } else if (abilities & PHYSTS_10BASET_FD) {
1109	sc->tulip_media = TULIP_MEDIA_10BASET_FD;
1110    } else if (abilities & PHYSTS_10BASET) {
1111	sc->tulip_media = TULIP_MEDIA_10BASET;
1112    } else {
1113	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1114	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1115	return 1;
1116    }
1117    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
1118    sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1119    sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_NEEDRESET;
1120    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1121    return 0;
1122}
1123
1124static void
1125tulip_21140_autonegotiate(
1126    tulip_softc_t * const sc,
1127    const tulip_phy_t * const phy)
1128{
1129    u_int32_t data;
1130
1131    if (sc->tulip_flags & TULIP_INRESET) {
1132	sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1133    }
1134    if (sc->tulip_if.if_flags & IFF_NOAUTONEG) {
1135	sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1136	data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_CONTROL);
1137	if (data & PHYCTL_AUTONEG_ENABLE) {
1138	    data &= ~PHYCTL_AUTONEG_ENABLE;
1139	    tulip_mii_writereg(sc, phy->phy_devaddr, PHYREG_CONTROL, data);
1140	}
1141	return;
1142    }
1143
1144  again:
1145    switch (sc->tulip_probe_state) {
1146        case TULIP_PROBE_INACTIVE: {
1147	    sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
1148	    tulip_mii_writereg(sc, phy->phy_devaddr, PHYREG_CONTROL, PHYCTL_RESET);
1149	    sc->tulip_gpticks = 10;
1150	    sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_GPTIMEOUT|TULIP_STS_NORMALINTR;
1151	    sc->tulip_probe_state = TULIP_PROBE_PHYRESET;
1152	    goto again;
1153	}
1154        case TULIP_PROBE_PHYRESET: {
1155	    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_CONTROL);
1156	    if (data & PHYCTL_RESET) {
1157		if (--sc->tulip_gpticks > 0) {
1158		    tulip_21140_gp_timer_set(sc, 100);
1159		    return;
1160		}
1161		printf(TULIP_PRINTF_FMT "(phy%d): error: reset of PHY never completed!\n",
1162			   TULIP_PRINTF_ARGS, phy->phy_devaddr);
1163		sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1164		sc->tulip_probe_state = TULIP_PROBE_FAILED;
1165		sc->tulip_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
1166		sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
1167		return;
1168	    }
1169	    if ((phy->phy_status & PHYSTS_CAN_AUTONEG) == 0
1170		    && (sc->tulip_if.if_flags & IFF_NOAUTONEG)) {
1171#ifdef TULIP_DEBUG
1172		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation disabled\n",
1173			   TULIP_PRINTF_ARGS, phy->phy_devaddr);
1174#endif
1175		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1176		return;
1177	    }
1178	    if (tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_AUTONEG_ADVERTISEMENT) != ((phy->phy_status >> 6) | 0x01))
1179		tulip_mii_writereg(sc, phy->phy_devaddr, PHYREG_AUTONEG_ADVERTISEMENT, (phy->phy_status >> 6) | 0x01);
1180	    tulip_mii_writereg(sc, phy->phy_devaddr, PHYREG_CONTROL, data|PHYCTL_AUTONEG_RESTART|PHYCTL_AUTONEG_ENABLE);
1181	    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_CONTROL);
1182#ifdef TULIP_DEBUG
1183	    if ((data & PHYCTL_AUTONEG_ENABLE) == 0)
1184		loudprintf(TULIP_PRINTF_FMT "(phy%d): oops: enable autonegotiation failed: 0x%04x\n",
1185			   TULIP_PRINTF_ARGS, phy->phy_devaddr, data);
1186	    else
1187		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation restarted: 0x%04x\n",
1188			   TULIP_PRINTF_ARGS, phy->phy_devaddr, data);
1189#endif
1190	    sc->tulip_probe_state = TULIP_PROBE_PHYAUTONEG;
1191	    sc->tulip_gpticks = 60;
1192	    goto again;
1193	}
1194        case TULIP_PROBE_PHYAUTONEG: {
1195	    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_STATUS);
1196	    if ((data & PHYSTS_AUTONEG_DONE) == 0) {
1197		if (--sc->tulip_gpticks > 0) {
1198		    tulip_21140_gp_timer_set(sc, 100);
1199		    return;
1200		}
1201#ifdef TULIP_DEBUG
1202		loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation timeout: sts=0x%04x, ctl=0x%04x\n",
1203			   TULIP_PRINTF_ARGS, phy->phy_devaddr, data,
1204			   tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_CONTROL));
1205#endif
1206		sc->tulip_probe_state = TULIP_PROBE_MEDIATEST;
1207		return;
1208	    }
1209	    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_AUTONEG_ABILITIES);
1210#ifdef TULIP_DEBUG
1211	    loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation complete: 0x%04x\n",
1212		       TULIP_PRINTF_ARGS, phy->phy_devaddr, data);
1213#endif
1214	    data = (data << 6) & phy->phy_status;
1215	    tulip_21140_map_abilities(sc, phy, data);
1216	    return;
1217	}
1218    }
1219#ifdef TULIP_DEBUG
1220    loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation failure: state = %d\n",
1221	       TULIP_PRINTF_ARGS, phy->phy_devaddr, sc->tulip_probe_state);
1222#endif
1223}
1224
1225static tulip_media_t
1226tulip_21140_phy_readspecific(
1227    tulip_softc_t * const sc,
1228    const tulip_phy_t * const phy)
1229{
1230    const phy_attr_t * const attr = phy->phy_attr;
1231    unsigned data;
1232    unsigned idx = 0;
1233    static const tulip_media_t table[] = {
1234	TULIP_MEDIA_UNKNOWN,
1235	TULIP_MEDIA_10BASET,
1236	TULIP_MEDIA_100BASETX,
1237	TULIP_MEDIA_100BASET4,
1238	TULIP_MEDIA_UNKNOWN,
1239	TULIP_MEDIA_10BASET_FD,
1240	TULIP_MEDIA_100BASETX_FD,
1241	TULIP_MEDIA_UNKNOWN
1242    };
1243
1244    /*
1245     * Don't read phy specific registers if link is not up.
1246     */
1247    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_STATUS);
1248    if ((data & PHYSTS_LINK_UP) == 0)
1249	return TULIP_MEDIA_UNKNOWN;
1250
1251    if (attr->attr_modes[PHY_MODE_100TX].pm_regno) {
1252	const phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100TX];
1253	data = tulip_mii_readreg(sc, phy->phy_devaddr, pm->pm_regno);
1254	if ((data & pm->pm_mask) == pm->pm_value)
1255	    idx = 2;
1256    }
1257    if (idx == 0 && attr->attr_modes[PHY_MODE_100T4].pm_regno) {
1258	const phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_100T4];
1259	data = tulip_mii_readreg(sc, phy->phy_devaddr, pm->pm_regno);
1260	if ((data & pm->pm_mask) == pm->pm_value)
1261	    idx = 3;
1262    }
1263    if (idx == 0 && attr->attr_modes[PHY_MODE_10T].pm_regno) {
1264	const phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_10T];
1265	data = tulip_mii_readreg(sc, phy->phy_devaddr, pm->pm_regno);
1266	if ((data & pm->pm_mask) == pm->pm_value)
1267	    idx = 1;
1268    }
1269    if (idx != 0 && attr->attr_modes[PHY_MODE_FULLDUPLEX].pm_regno) {
1270	const phy_modedata_t * const pm = &attr->attr_modes[PHY_MODE_FULLDUPLEX];
1271	data = tulip_mii_readreg(sc, phy->phy_devaddr, pm->pm_regno);
1272	idx += ((data & pm->pm_mask) == pm->pm_value ? 4 : 0);
1273    }
1274    return table[idx];
1275}
1276
1277static void
1278tulip_21140_mii_link_monitor(
1279    tulip_softc_t * const sc,
1280    const tulip_phy_t * const phy)
1281{
1282    u_int32_t data;
1283
1284    tulip_21140_gp_timer_set(sc, 425);
1285    /*
1286     * Have we seen some packets?  If so, the link must be good.
1287     */
1288    if ((sc->tulip_flags & (TULIP_RXACT|TULIP_LINKSUSPECT|TULIP_LINKUP)) == (TULIP_RXACT|TULIP_LINKUP)) {
1289	sc->tulip_flags &= ~TULIP_RXACT;
1290	return;
1291    }
1292
1293    /*
1294     * Read the PHY status register.
1295     */
1296    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_STATUS);
1297    if ((sc->tulip_if.if_flags & IFF_NOAUTONEG) == 0 && (data & PHYSTS_AUTONEG_DONE)) {
1298	/*
1299	 * If autonegotiation hasn't been disabled and the PHY has complete
1300	 * autonegotiation, see the if the remote systems abilities have changed.
1301	 * If so, upgrade or downgrade as appropriate.
1302	 */
1303	unsigned abilities = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_AUTONEG_ABILITIES);
1304	abilities = (abilities << 6) & phy->phy_status;
1305	if (abilities != sc->tulip_abilities) {
1306	    sc->tulip_flags |= TULIP_PRINTMEDIA;
1307#ifdef TULIP_DEBUG
1308	    loudprintf(TULIP_PRINTF_FMT "(phy%d): autonegotiation changed: 0x%04x -> 0x%04x\n",
1309		       TULIP_PRINTF_ARGS, phy->phy_devaddr,
1310		       sc->tulip_abilities, abilities);
1311#endif
1312	    tulip_21140_map_abilities(sc, phy, abilities);
1313	    return;
1314	}
1315    }
1316    /*
1317     * The link is now up.  If was down, say its back up.
1318     */
1319    if ((data & (PHYSTS_LINK_UP|PHYSTS_REMOTE_FAULT)) == PHYSTS_LINK_UP) {
1320	if ((sc->tulip_if.if_flags & IFF_NOAUTONEG) == 0) {
1321	    tulip_media_t media = tulip_21140_phy_readspecific(sc, phy);
1322	    if (media != sc->tulip_media && media != TULIP_MEDIA_UNKNOWN) {
1323		sc->tulip_media = media;
1324		sc->tulip_flags |= TULIP_PRINTMEDIA;
1325	    }
1326	}
1327	sc->tulip_gpticks = 0;
1328	if (sc->tulip_flags & TULIP_PRINTMEDIA) {
1329	    printf(TULIP_PRINTF_FMT ": %senabling %s port\n",
1330		   TULIP_PRINTF_ARGS,
1331		   (sc->tulip_flags & TULIP_LINKUP) ? "" : "link up: ",
1332		   tulip_mediums[sc->tulip_media]);
1333	} else if ((sc->tulip_flags & TULIP_LINKUP) == 0) {
1334	    printf(TULIP_PRINTF_FMT ": link up\n", TULIP_PRINTF_ARGS);
1335	}
1336	sc->tulip_flags &= ~(TULIP_PRINTMEDIA|TULIP_LINKSUSPECT|TULIP_RXACT);
1337	sc->tulip_flags |= TULIP_LINKUP;
1338	return;
1339    }
1340    /*
1341     * The link may be down.  Mark it as suspect.  If suspect for 12 ticks,
1342     * mark it down.  If autonegotiation is not disabled, restart the media
1343     * probe to see if the media has changed.
1344     */
1345    if ((sc->tulip_flags & TULIP_LINKSUSPECT) == 0) {
1346	sc->tulip_flags |= TULIP_LINKSUSPECT;
1347	sc->tulip_flags &= ~TULIP_LINKUP;
1348	sc->tulip_gpticks = 12;
1349#ifdef TULIP_DEBUG
1350	sc->tulip_dbg.dbg_link_suspected++;
1351#endif
1352	return;
1353    }
1354    if (--sc->tulip_gpticks > 0)
1355	return;
1356    if (sc->tulip_flags & TULIP_LINKSUSPECT) {
1357	printf(TULIP_PRINTF_FMT ": link down: cable problem?\n", TULIP_PRINTF_ARGS);
1358	sc->tulip_flags &= ~TULIP_LINKSUSPECT;
1359#ifdef TULIP_DEBUG
1360	sc->tulip_dbg.dbg_link_downed++;
1361#endif
1362    }
1363    if (sc->tulip_if.if_flags & IFF_NOAUTONEG)
1364	return;
1365    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1366    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1367    tulip_21140_autonegotiate(sc, phy);
1368}
1369
1370static void
1371tulip_21140_nomii_media_preset(
1372    tulip_softc_t * const sc)
1373{
1374    sc->tulip_flags &= ~TULIP_SQETEST;
1375    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1376	sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
1377	    |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
1378	sc->tulip_if.if_baudrate = 100000000;
1379    } else {
1380	sc->tulip_cmdmode &= ~(TULIP_CMD_PORTSELECT
1381			       |TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER);
1382	sc->tulip_if.if_baudrate = 10000000;
1383	if ((sc->tulip_cmdmode & TULIP_CMD_FULLDUPLEX) == 0)
1384	    sc->tulip_flags |= TULIP_SQETEST;
1385    }
1386    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1387}
1388
1389static void
1390tulip_21140_mii_media_preset(
1391    tulip_softc_t * const sc)
1392{
1393    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT;
1394    sc->tulip_flags &= ~TULIP_SQETEST;
1395    if (sc->tulip_media != TULIP_MEDIA_UNKNOWN) {
1396	switch (sc->tulip_media) {
1397	    case TULIP_MEDIA_10BASET: {
1398		sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1399		sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1400		sc->tulip_if.if_baudrate = 10000000;
1401		sc->tulip_flags |= TULIP_SQETEST;
1402		break;
1403	    }
1404	    case TULIP_MEDIA_10BASET_FD: {
1405		sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL;
1406		sc->tulip_if.if_baudrate = 10000000;
1407		break;
1408	    }
1409	    case TULIP_MEDIA_100BASET4:
1410	    case TULIP_MEDIA_100BASETX: {
1411		sc->tulip_cmdmode &= ~(TULIP_CMD_FULLDUPLEX|TULIP_CMD_TXTHRSHLDCTL);
1412		sc->tulip_if.if_baudrate = 100000000;
1413		break;
1414	    }
1415	    case TULIP_MEDIA_100BASETX_FD: {
1416		sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1417		sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1418		sc->tulip_if.if_baudrate = 100000000;
1419		break;
1420	    }
1421	}
1422    }
1423    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1424}
1425
1426static void
1427tulip_21140_nomii_100only_media_preset(
1428    tulip_softc_t * const sc)
1429{
1430    sc->tulip_cmdmode |= TULIP_CMD_PORTSELECT
1431	|TULIP_CMD_PCSFUNCTION|TULIP_CMD_SCRAMBLER;
1432    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1433}
1434
1435
1436static int
1437tulip_21140_evalboard_media_probe(
1438    tulip_softc_t * const sc)
1439{
1440    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1441    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1442    TULIP_CSR_WRITE(sc, csr_command,
1443	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1444	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1445    TULIP_CSR_WRITE(sc, csr_command,
1446	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1447    DELAY(1000000);
1448    return (TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_EB_OK100) != 0;
1449}
1450
1451static void
1452tulip_21140_evalboard_media_select(
1453    tulip_softc_t * const sc)
1454{
1455    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
1456	|TULIP_CMD_BACKOFFCTR;
1457    sc->tulip_flags |= TULIP_LINKUP;
1458    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_PINS);
1459    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EB_INIT);
1460    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1461	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
1462	    sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1463	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1464	sc->tulip_media = TULIP_MEDIA_100BASETX;
1465	sc->tulip_flags &= ~TULIP_SQETEST;
1466    } else {
1467	if (sc->tulip_flags & TULIP_ALTPHYS)
1468	    sc->tulip_flags ^= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1469	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1470	sc->tulip_media = TULIP_MEDIA_10BASET;
1471	sc->tulip_flags |= TULIP_SQETEST;
1472    }
1473#ifdef BIG_PACKET
1474    if (sc->tulip_if.if_mtu > ETHERMTU) {
1475	TULIP_CSR_WRITE(sc, csr_watchdog, TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE);
1476    }
1477#endif
1478}
1479
1480static const tulip_boardsw_t tulip_21140_eb_boardsw = {
1481    TULIP_21140_DEC_EB,
1482    "",
1483    tulip_21140_evalboard_media_probe,
1484    tulip_21140_evalboard_media_select,
1485    tulip_21140_nomii_media_preset,
1486};
1487
1488static int
1489tulip_21140_smc9332_media_probe(
1490    tulip_softc_t * const sc)
1491{
1492    int idx, cnt = 0;
1493    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT|TULIP_CMD_MUSTBEONE);
1494    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
1495    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
1496		   33MHz that comes to two microseconds but wait a
1497		   bit longer anyways) */
1498    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT |
1499	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1500    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS);
1501    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
1502    DELAY(200000);
1503    for (idx = 1000; idx > 0; idx--) {
1504	u_int32_t csr = TULIP_CSR_READ(sc, csr_gp);
1505	if ((csr & (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) == (TULIP_GP_SMC_9332_OK10|TULIP_GP_SMC_9332_OK100)) {
1506	    if (++cnt > 100)
1507		break;
1508	} else if ((csr & TULIP_GP_SMC_9332_OK10) == 0) {
1509	    break;
1510	} else {
1511	    cnt = 0;
1512	}
1513	DELAY(1000);
1514    }
1515    return cnt > 100;
1516}
1517
1518static void
1519tulip_21140_smc9332_media_select(
1520    tulip_softc_t * const sc)
1521{
1522    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
1523	|TULIP_CMD_BACKOFFCTR;
1524    sc->tulip_flags |= TULIP_LINKUP;
1525    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_PINS);
1526    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_SMC_9332_INIT);
1527    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1528	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
1529	    sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1530	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1531	sc->tulip_media = TULIP_MEDIA_100BASETX;
1532	sc->tulip_flags &= ~TULIP_SQETEST;
1533    } else {
1534	if (sc->tulip_flags & TULIP_ALTPHYS)
1535	    sc->tulip_flags ^= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1536	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1537	sc->tulip_media = TULIP_MEDIA_10BASET;
1538	sc->tulip_flags |= TULIP_SQETEST;
1539    }
1540#ifdef BIG_PACKET
1541    if (sc->tulip_if.if_mtu > ETHERMTU) {
1542	TULIP_CSR_WRITE(sc, csr_watchdog, TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE);
1543    }
1544#endif
1545}
1546
1547static const tulip_boardsw_t tulip_21140_smc9332_boardsw = {
1548    TULIP_21140_SMC_9332,
1549    "SMC 9332 ",
1550    tulip_21140_smc9332_media_probe,
1551    tulip_21140_smc9332_media_select,
1552    tulip_21140_nomii_media_preset,
1553};
1554
1555static int
1556tulip_21140_cogent_em100_media_probe(
1557    tulip_softc_t * const sc)
1558{
1559    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
1560    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
1561    TULIP_CSR_WRITE(sc, csr_command,
1562	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1563	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1564    TULIP_CSR_WRITE(sc, csr_command,
1565	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1566    return 1;
1567}
1568
1569static void
1570tulip_21140_cogent_em100_media_select(
1571    tulip_softc_t * const sc)
1572{
1573    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
1574	|TULIP_CMD_BACKOFFCTR;
1575    sc->tulip_flags |= TULIP_LINKUP;
1576    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_PINS);
1577    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_EM100_INIT);
1578    if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
1579	sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1580    sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1581    sc->tulip_media = TULIP_MEDIA_100BASETX;
1582#ifdef BIG_PACKET
1583    if (sc->tulip_if.if_mtu > ETHERMTU) {
1584	TULIP_CSR_WRITE(sc, csr_watchdog, TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE);
1585    }
1586#endif
1587}
1588
1589static const tulip_boardsw_t tulip_21140_cogent_em100_boardsw = {
1590    TULIP_21140_COGENT_EM100,
1591    "Cogent EM100 ",
1592    tulip_21140_cogent_em100_media_probe,
1593    tulip_21140_cogent_em100_media_select,
1594    tulip_21140_nomii_100only_media_preset
1595};
1596
1597
1598static int
1599tulip_21140_znyx_zx34x_media_probe(
1600    tulip_softc_t * const sc)
1601{
1602    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
1603    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
1604    TULIP_CSR_WRITE(sc, csr_command,
1605	TULIP_CSR_READ(sc, csr_command) | TULIP_CMD_PORTSELECT |
1606	TULIP_CMD_PCSFUNCTION | TULIP_CMD_SCRAMBLER | TULIP_CMD_MUSTBEONE);
1607    TULIP_CSR_WRITE(sc, csr_command,
1608	TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1609    DELAY(1000000);
1610
1611    return (TULIP_CSR_READ(sc, csr_gp) & TULIP_GP_ZX34X_OK10);
1612}
1613
1614static void
1615tulip_21140_znyx_zx34x_media_select(
1616    tulip_softc_t * const sc)
1617{
1618    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
1619	|TULIP_CMD_BACKOFFCTR;
1620    sc->tulip_flags |= TULIP_LINKUP;
1621    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_PINS);
1622    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_ZX34X_INIT);
1623    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1624	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
1625	    sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1626	sc->tulip_cmdmode &= ~TULIP_CMD_TXTHRSHLDCTL;
1627	sc->tulip_media = TULIP_MEDIA_100BASETX;
1628    } else {
1629	if (sc->tulip_flags & TULIP_ALTPHYS)
1630	    sc->tulip_flags ^= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1631	sc->tulip_cmdmode |= TULIP_CMD_TXTHRSHLDCTL;
1632	sc->tulip_media = TULIP_MEDIA_10BASET;
1633    }
1634#ifdef BIG_PACKET
1635    if (sc->tulip_if.if_mtu > ETHERMTU) {
1636	TULIP_CSR_WRITE(sc, csr_watchdog, TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE);
1637    }
1638#endif
1639}
1640
1641static const tulip_boardsw_t tulip_21140_znyx_zx34x_boardsw = {
1642    TULIP_21140_ZNYX_ZX34X,
1643    "ZNYX ZX34X ",
1644    tulip_21140_znyx_zx34x_media_probe,
1645    tulip_21140_znyx_zx34x_media_select,
1646    tulip_21140_nomii_media_preset,
1647};
1648
1649static const struct {
1650    unsigned short value_gp;
1651    unsigned short value_phyctl;
1652} tulip_21140_de500_csrvalues[] = {
1653    { TULIP_GP_DE500_HALFDUPLEX, 0 },	/* TULIP_MEDIA_UNKNOWN */
1654    { TULIP_GP_DE500_HALFDUPLEX, 0 },	/* TULIP_MEDIA_10BASET */
1655    { /* n/a */ },			/* TULIP_MEDIA_BNC */
1656    { /* n/a */ },			/* TULIP_MEDIA_AUI */
1657    { /* n/a */ },			/* TULIP_MEDIA_BNCAUI */
1658    { 0, PHYCTL_FULL_DUPLEX },		/* TULIP_MEDIA_10BASET_FD */
1659    { TULIP_GP_DE500_HALFDUPLEX|	/* TULIP_MEDIA_100BASET */
1660      TULIP_GP_DE500_FORCE_100, PHYCTL_SELECT_100MB },
1661    { TULIP_GP_DE500_FORCE_100,		/* TULIP_MEDIA_100BASET_FD */
1662      PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX },
1663    { TULIP_GP_DE500_HALFDUPLEX|	/* TULIP_MEDIA_100BASET4 */
1664      TULIP_GP_DE500_FORCE_100, PHYCTL_SELECT_100MB },
1665};
1666
1667static void
1668tulip_21140_de500_media_select(
1669    tulip_softc_t * const sc)
1670{
1671    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1672	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
1673	    sc->tulip_media = TULIP_MEDIA_100BASETX_FD;
1674	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1675	} else {
1676	    sc->tulip_media = TULIP_MEDIA_100BASETX;
1677	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1678	}
1679	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0)
1680	    sc->tulip_flags |= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1681    } else {
1682	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
1683	    sc->tulip_media = TULIP_MEDIA_10BASET_FD;
1684	    sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
1685	} else {
1686	    sc->tulip_media = TULIP_MEDIA_10BASET;
1687	    sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
1688	}
1689	if (sc->tulip_flags & TULIP_ALTPHYS)
1690	    sc->tulip_flags ^= TULIP_PRINTMEDIA|TULIP_ALTPHYS;
1691    }
1692}
1693
1694static int
1695tulip_21140_de500xa_media_probe(
1696    tulip_softc_t * const sc)
1697{
1698    int idx;
1699
1700    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_DE500_PINS);
1701    DELAY(500);
1702    TULIP_CSR_WRITE(sc, csr_gp,
1703		    TULIP_GP_DE500_HALFDUPLEX|TULIP_GP_DE500_FORCE_100);
1704    DELAY(1000);
1705    TULIP_CSR_WRITE(sc, csr_command,
1706		    TULIP_CSR_READ(sc, csr_command)
1707		    |TULIP_CMD_PORTSELECT|TULIP_CMD_PCSFUNCTION
1708		    |TULIP_CMD_SCRAMBLER|TULIP_CMD_MUSTBEONE);
1709    TULIP_CSR_WRITE(sc, csr_command,
1710		    TULIP_CSR_READ(sc, csr_command) & ~TULIP_CMD_TXTHRSHLDCTL);
1711    for (idx = 2400; idx > 0; idx--) {
1712	u_int32_t data;
1713	DELAY(1000);
1714	data = ~TULIP_CSR_READ(sc, csr_gp);
1715	if ((data & (TULIP_GP_DE500_LINK_PASS|TULIP_GP_DE500_SYM_LINK)) == (TULIP_GP_DE500_SYM_LINK|TULIP_GP_DE500_LINK_PASS))
1716	    return 1;
1717    }
1718    return 0;
1719}
1720
1721static void
1722tulip_21140_de500xa_media_select(
1723    tulip_softc_t * const sc)
1724{
1725    sc->tulip_cmdmode |= TULIP_CMD_STOREFWD|TULIP_CMD_MUSTBEONE
1726	|TULIP_CMD_BACKOFFCTR;
1727    sc->tulip_flags |= TULIP_LINKUP;
1728    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_DE500_PINS);
1729    tulip_21140_de500_media_select(sc);
1730    TULIP_CSR_WRITE(sc, csr_gp, tulip_21140_de500_csrvalues[sc->tulip_media].value_gp);
1731#ifdef BIG_PACKET
1732    if (sc->tulip_if.if_mtu > ETHERMTU) {
1733	TULIP_CSR_WRITE(sc, csr_watchdog, TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE);
1734    }
1735#endif
1736}
1737
1738static const tulip_boardsw_t tulip_21140_de500xa_boardsw = {
1739    TULIP_21140_DEC_DE500, "Digital DE500-XA ",
1740    tulip_21140_de500xa_media_probe,
1741    tulip_21140_de500xa_media_select,
1742    tulip_21140_nomii_media_preset,
1743};
1744
1745static int
1746tulip_21140_de500aa_media_probe(
1747    tulip_softc_t * const sc)
1748{
1749    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_DE500_PINS);
1750    TULIP_CSR_WRITE(sc, csr_gp, TULIP_GP_DE500_PHY_RESET);
1751    DELAY(1000);
1752    TULIP_CSR_WRITE(sc, csr_gp, 0);
1753
1754    TULIP_CSR_WRITE(sc, csr_command, TULIP_CMD_PORTSELECT);
1755    return 0;
1756}
1757
1758static void
1759tulip_21140_de500aa_media_select(
1760    tulip_softc_t * const sc)
1761{
1762    const tulip_phy_t *phy = sc->tulip_phys;
1763    u_int32_t data;
1764
1765    if (phy == NULL)
1766	return;
1767
1768    /*
1769     * Defer autosensing until out of device probe (will be
1770     * triggered by ifwatchdog or ifioctl).
1771     */
1772
1773    if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
1774	tulip_media_t old_media;
1775	if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST)
1776	    tulip_21140_autonegotiate(sc, phy);
1777	if (sc->tulip_probe_state != TULIP_PROBE_MEDIATEST)
1778	    return;
1779	old_media = sc->tulip_media;
1780	if (sc->tulip_if.if_flags & IFF_NOAUTONEG) {
1781	    tulip_21140_de500_media_select(sc);
1782	} else {
1783	    sc->tulip_media = tulip_21140_phy_readspecific(sc, phy);
1784	    if (sc->tulip_media == TULIP_MEDIA_UNKNOWN) {
1785		sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1786		tulip_21140_autonegotiate(sc, phy);
1787		return;
1788	    }
1789	    sc->tulip_flags |= TULIP_PRINTMEDIA;
1790	}
1791	sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1792	sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1793	sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
1794	if (sc->tulip_flags & TULIP_INRESET)
1795	    goto in_reset;
1796	if (sc->tulip_media != old_media)
1797	    sc->tulip_flags |= TULIP_NEEDRESET;
1798	return;
1799    }
1800    if ((sc->tulip_flags & TULIP_INRESET) == 0) {
1801	tulip_21140_mii_link_monitor(sc, phy);
1802	return;
1803    }
1804  in_reset:
1805    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1806	sc->tulip_flags |= TULIP_ALTPHYS;
1807    } else {
1808	sc->tulip_flags &= ~TULIP_ALTPHYS;
1809    }
1810    sc->tulip_gpticks = 8;
1811    sc->tulip_intrmask |= TULIP_STS_ABNRMLINTR|TULIP_STS_GPTIMEOUT|TULIP_STS_NORMALINTR;
1812    tulip_21140_gp_timer_set(sc, 425);
1813    data = tulip_mii_readreg(sc, phy->phy_devaddr, PHYREG_CONTROL);
1814    if ((data & PHYCTL_AUTONEG_ENABLE) == 0) {
1815	data &= ~(PHYCTL_SELECT_100MB|PHYCTL_FULL_DUPLEX);
1816	data |= tulip_21140_de500_csrvalues[sc->tulip_media].value_phyctl;
1817	tulip_mii_writereg(sc, phy->phy_devaddr, PHYREG_CONTROL, data);
1818    }
1819}
1820
1821static const tulip_boardsw_t tulip_21140_de500aa_boardsw = {
1822    TULIP_21140_DEC_DE500, "Digital DE500-AA ",
1823    tulip_21140_de500aa_media_probe,
1824    tulip_21140_de500aa_media_select,
1825    tulip_21140_mii_media_preset,
1826    tulip_21140_mii_probe,
1827};
1828
1829static int
1830tulip_21041_media_probe(
1831    tulip_softc_t * const sc)
1832{
1833    sc->tulip_if.if_baudrate = 10000000;
1834    return 0;
1835}
1836
1837#ifdef BIG_PACKET
1838#define TULIP_21041_SIAGEN_WATCHDOG	(sc->tulip_if.if_mtu > ETHERMTU ? TULIP_WATCHDOG_RXDISABLE|TULIP_WATCHDOG_TXDISABLE : 0)
1839#else
1840#define	TULIP_21041_SIAGEN_WATCHDOG	0
1841#endif
1842
1843static void
1844tulip_21041_media_select(
1845    tulip_softc_t * const sc)
1846{
1847    sc->tulip_cmdmode |= TULIP_CMD_CAPTREFFCT|TULIP_CMD_ENHCAPTEFFCT
1848	|TULIP_CMD_THRSHLD160|TULIP_CMD_BACKOFFCTR;
1849    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_GPTIMEOUT|TULIP_STS_TXINTR
1850	|TULIP_STS_ABNRMLINTR|TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL;
1851    if (sc->tulip_if.if_flags & IFF_ALTPHYS) {
1852	if ((sc->tulip_flags & TULIP_ALTPHYS) == 0) {
1853	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1854	    sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE);
1855	    sc->tulip_flags |= TULIP_ALTPHYS|TULIP_WANTRXACT;
1856	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1857	}
1858    } else {
1859	if (sc->tulip_flags & TULIP_ALTPHYS) {
1860	    sc->tulip_media = TULIP_MEDIA_UNKNOWN;
1861	    sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE|TULIP_ALTPHYS);
1862	    sc->tulip_flags |= TULIP_WANTRXACT;
1863	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1864	}
1865    }
1866
1867    if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_LINKFAIL) {
1868	if (sc->tulip_media == TULIP_MEDIA_BNC) {
1869	    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
1870	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1871	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_BNC);
1872	    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_BNC);
1873	    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_BNC|TULIP_21041_SIAGEN_WATCHDOG);
1874	    return;
1875	} else if (sc->tulip_media == TULIP_MEDIA_AUI) {
1876	    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
1877	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1878	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_AUI);
1879	    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_AUI);
1880	    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_AUI|TULIP_21041_SIAGEN_WATCHDOG);
1881	    return;
1882	}
1883	/*
1884	 * If we've been reset, the SIA is reset.  Restart the probe.
1885	 */
1886	if (sc->tulip_probe_state == TULIP_PROBE_10BASET
1887		&& (sc->tulip_flags & TULIP_INRESET))
1888	    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1889
1890	/*
1891	 * Reset OACTIVE in case were being called from tulip_reset.
1892	 */
1893	sc->tulip_if.if_flags |= IFF_OACTIVE;
1894	switch (sc->tulip_probe_state) {
1895	    case TULIP_PROBE_INACTIVE: {
1896		sc->tulip_gpticks = 200;
1897		sc->tulip_probe_state = TULIP_PROBE_10BASET;
1898		TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1899		TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1900		TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_10BASET);
1901		if (sc->tulip_cmdmode & TULIP_CMD_FULLDUPLEX)
1902		    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_10BASET_FD);
1903		else
1904		    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_10BASET);
1905		TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_10BASET|TULIP_21041_SIAGEN_WATCHDOG);
1906		TULIP_CSR_WRITE(sc, csr_gp_timer, 12000000 / 204800); /* 120 ms */
1907		TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_GPTIMEOUT);
1908		break;
1909	    }
1910	    case TULIP_PROBE_10BASET: {
1911		if (--sc->tulip_gpticks > 0) {
1912		    if ((TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_OTHERRXACTIVITY) == 0) {
1913			TULIP_CSR_WRITE(sc, csr_gp_timer, 12000000 / 204800); /* 120 ms */
1914			break;
1915		    }
1916		}
1917		sc->tulip_gpticks = 4;
1918		if (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_OTHERRXACTIVITY) {
1919		    sc->tulip_probe_state = TULIP_PROBE_BNC;
1920		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1921		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_BNC);
1922		    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_BNC);
1923		    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_BNC|TULIP_21041_SIAGEN_WATCHDOG);
1924		    TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_SIASTS_OTHERRXACTIVITY);
1925		    TULIP_CSR_WRITE(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
1926		} else {
1927		    sc->tulip_probe_state = TULIP_PROBE_AUI;
1928		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
1929		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_AUI);
1930		    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_AUI);
1931		    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_AUI|TULIP_21041_SIAGEN_WATCHDOG);
1932		    TULIP_CSR_WRITE(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
1933		}
1934		break;
1935	    }
1936	    case TULIP_PROBE_BNC:
1937	    case TULIP_PROBE_AUI: {
1938		if (sc->tulip_flags & TULIP_TXPROBE_OK) {
1939		    sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
1940		    sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE);
1941		    sc->tulip_flags |= TULIP_LINKUP;
1942		    TULIP_CSR_WRITE(sc, csr_gp_timer, 0); /* disable */
1943		    if (sc->tulip_probe_state == TULIP_PROBE_AUI) {
1944			if (sc->tulip_media != TULIP_MEDIA_AUI) {
1945			    sc->tulip_media = TULIP_MEDIA_AUI;
1946			    sc->tulip_flags |= TULIP_PRINTMEDIA;
1947			}
1948		    } else if (sc->tulip_probe_state == TULIP_PROBE_BNC) {
1949			if (sc->tulip_media != TULIP_MEDIA_BNC) {
1950			    sc->tulip_media = TULIP_MEDIA_BNC;
1951			    sc->tulip_flags |= TULIP_PRINTMEDIA;
1952			}
1953		    }
1954		    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1955		    sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
1956		    break;
1957		}
1958		if ((sc->tulip_flags & TULIP_WANTRXACT) == 0
1959		    || (TULIP_CSR_READ(sc, csr_sia_status) & TULIP_SIASTS_RXACTIVITY)) {
1960		    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0) {
1961			struct mbuf *m;
1962			/*
1963			 * Before we are sure this is the right media we need
1964			 * to send a small packet to make sure there's carrier.
1965			 * Strangely, BNC and AUI will 'see" receive data if
1966			 * either is connected so the transmit is the only way
1967			 * to verify the connectivity.
1968			 */
1969			MGETHDR(m, M_DONTWAIT, MT_DATA);
1970			if (m == NULL) {
1971			    TULIP_CSR_WRITE(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
1972			    break;
1973			}
1974			/*
1975			 * Construct a LLC TEST message which will point to ourselves.
1976			 */
1977			bcopy(sc->tulip_hwaddr, mtod(m, struct ether_header *)->ether_dhost, 6);
1978			bcopy(sc->tulip_hwaddr, mtod(m, struct ether_header *)->ether_shost, 6);
1979			mtod(m, struct ether_header *)->ether_type = htons(3);
1980			mtod(m, unsigned char *)[14] = 0;
1981			mtod(m, unsigned char *)[15] = 0;
1982			mtod(m, unsigned char *)[16] = 0xE3;	/* LLC Class1 TEST (no poll) */
1983			m->m_len = m->m_pkthdr.len = sizeof(struct ether_header) + 3;
1984			/*
1985			 * send it!
1986			 */
1987			sc->tulip_flags |= TULIP_TXPROBE_ACTIVE;
1988			sc->tulip_flags &= ~TULIP_TXPROBE_OK;
1989			sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
1990			TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
1991			IF_PREPEND(&sc->tulip_if.if_snd, m);
1992			tulip_ifstart(&sc->tulip_if);
1993			break;
1994		    }
1995		    sc->tulip_flags &= ~TULIP_TXPROBE_ACTIVE;
1996		}
1997		/*
1998		 * Take 2 passes through before deciding to not
1999		 * wait for receive activity.  Then take another
2000		 * two passes before spitting out a warning.
2001		 */
2002		if (sc->tulip_gpticks > 0 && --sc->tulip_gpticks == 0) {
2003		    if (sc->tulip_flags & TULIP_WANTRXACT) {
2004			sc->tulip_flags &= ~TULIP_WANTRXACT;
2005			sc->tulip_gpticks = 4;
2006		    } else {
2007			printf(TULIP_PRINTF_FMT ": autosense failed: cable problem?\n",
2008			       TULIP_PRINTF_ARGS);
2009		    }
2010		}
2011		/*
2012		 * Since this media failed to probe, try the other one.
2013		 */
2014		if (sc->tulip_probe_state == TULIP_PROBE_AUI) {
2015		    sc->tulip_probe_state = TULIP_PROBE_BNC;
2016		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
2017		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_BNC);
2018		    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_BNC);
2019		    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_BNC|TULIP_21041_SIAGEN_WATCHDOG);
2020		    TULIP_CSR_WRITE(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
2021		} else {
2022		    sc->tulip_probe_state = TULIP_PROBE_AUI;
2023		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
2024		    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_AUI);
2025		    TULIP_CSR_WRITE(sc, csr_sia_tx_rx,        TULIP_21041_SIATXRX_AUI);
2026		    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_AUI|TULIP_21041_SIAGEN_WATCHDOG);
2027		    TULIP_CSR_WRITE(sc, csr_gp_timer, 100000000 / 204800); /* 100 ms */
2028		}
2029		break;
2030	    }
2031	}
2032    } else {
2033	/*
2034	 * If the link has passed LinkPass, 10baseT is the
2035	 * proper media to use.
2036	 */
2037	if (sc->tulip_if.if_flags & IFF_FULLDUPLEX) {
2038	    if (sc->tulip_media != TULIP_MEDIA_10BASET_FD) {
2039		sc->tulip_media = TULIP_MEDIA_10BASET_FD;
2040		sc->tulip_flags |= TULIP_PRINTMEDIA;
2041		sc->tulip_cmdmode |= TULIP_CMD_FULLDUPLEX;
2042	    }
2043	} else {
2044	    if (sc->tulip_media != TULIP_MEDIA_10BASET) {
2045		sc->tulip_media = TULIP_MEDIA_10BASET;
2046		sc->tulip_flags |= TULIP_PRINTMEDIA;
2047		sc->tulip_cmdmode &= ~TULIP_CMD_FULLDUPLEX;
2048	    }
2049	}
2050	if (sc->tulip_flags & (TULIP_INRESET|TULIP_PRINTMEDIA)) {
2051	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
2052	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_21041_SIACONN_10BASET);
2053	    if (sc->tulip_cmdmode & TULIP_CMD_FULLDUPLEX)
2054		TULIP_CSR_WRITE(sc, csr_sia_tx_rx,    TULIP_21041_SIATXRX_10BASET_FD);
2055	    else
2056		TULIP_CSR_WRITE(sc, csr_sia_tx_rx,    TULIP_21041_SIATXRX_10BASET);
2057	    TULIP_CSR_WRITE(sc, csr_sia_general,      TULIP_21041_SIAGEN_10BASET|TULIP_21041_SIAGEN_WATCHDOG);
2058	}
2059	TULIP_CSR_WRITE(sc, csr_gp_timer, 0); /* disable */
2060	sc->tulip_gpticks = 0;
2061	sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
2062	sc->tulip_intrmask &= ~TULIP_STS_GPTIMEOUT;
2063	sc->tulip_flags |= TULIP_LINKUP;
2064	sc->tulip_flags &= ~(TULIP_TXPROBE_OK|TULIP_TXPROBE_ACTIVE);
2065	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
2066	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
2067    }
2068    if (sc->tulip_flags & TULIP_DEVICEPROBE) {
2069	sc->tulip_flags |= TULIP_FAKEGPTIMEOUT;
2070    } else {
2071	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2072	sc->tulip_flags &= ~TULIP_FAKEGPTIMEOUT;
2073    }
2074}
2075
2076static const tulip_boardsw_t tulip_21041_boardsw = {
2077    TULIP_21041_GENERIC,
2078    "",
2079    tulip_21041_media_probe,
2080    tulip_21041_media_select
2081};
2082
2083static void
2084tulip_reset(
2085    tulip_softc_t * const sc)
2086{
2087    tulip_ringinfo_t *ri;
2088    tulip_desc_t *di;
2089
2090    /*
2091     * Brilliant.  Simply brilliant.  When switching modes/speeds
2092     * on a 2114*, you need to set the appriopriate MII/PCS/SCL/PS
2093     * bits in CSR6 and then do a software reset to get the 21140
2094     * to properly reset its internal pathways to the right places.
2095     *   Grrrr.
2096     */
2097    if (sc->tulip_boardsw->bd_media_preset != NULL)
2098	(*sc->tulip_boardsw->bd_media_preset)(sc);
2099
2100    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
2101    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
2102		   33MHz that comes to two microseconds but wait a
2103		   bit longer anyways) */
2104
2105    sc->tulip_flags |= TULIP_INRESET;
2106    sc->tulip_flags &= ~(TULIP_NEEDRESET|TULIP_RXBUFSLOW);
2107    sc->tulip_intrmask = 0;
2108    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2109
2110    TULIP_CSR_WRITE(sc, csr_txlist, TULIP_KVATOPHYS(sc, &sc->tulip_txinfo.ri_first[0]));
2111    TULIP_CSR_WRITE(sc, csr_rxlist, TULIP_KVATOPHYS(sc, &sc->tulip_rxinfo.ri_first[0]));
2112    TULIP_CSR_WRITE(sc, csr_busmode,
2113	(1 << (TULIP_BURSTSIZE(sc->tulip_unit) + 8))
2114	|TULIP_BUSMODE_CACHE_ALIGN8
2115	|(BYTE_ORDER != LITTLE_ENDIAN ? TULIP_BUSMODE_BIGENDIAN : 0));
2116
2117    sc->tulip_txtimer = 0;
2118    sc->tulip_txq.ifq_maxlen = TULIP_TXDESCS;
2119    sc->tulip_if.if_flags &= ~IFF_OACTIVE;
2120    /*
2121     * Free all the mbufs that were on the transmit ring.
2122     */
2123    for (;;) {
2124	struct mbuf *m;
2125	IF_DEQUEUE(&sc->tulip_txq, m);
2126	if (m == NULL)
2127	    break;
2128	m_freem(m);
2129    }
2130
2131    ri = &sc->tulip_txinfo;
2132    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
2133    ri->ri_free = ri->ri_max;
2134    for (di = ri->ri_first; di < ri->ri_last; di++)
2135	di->d_status = 0;
2136
2137    /*
2138     * We need to collect all the mbufs were on the
2139     * receive ring before we reinit it either to put
2140     * them back on or to know if we have to allocate
2141     * more.
2142     */
2143    ri = &sc->tulip_rxinfo;
2144    ri->ri_nextin = ri->ri_nextout = ri->ri_first;
2145    ri->ri_free = ri->ri_max;
2146    for (di = ri->ri_first; di < ri->ri_last; di++) {
2147	di->d_status = 0;
2148	di->d_length1 = 0; di->d_addr1 = 0;
2149	di->d_length2 = 0; di->d_addr2 = 0;
2150    }
2151    for (;;) {
2152	struct mbuf *m;
2153	IF_DEQUEUE(&sc->tulip_rxq, m);
2154	if (m == NULL)
2155	    break;
2156	m_freem(m);
2157    }
2158
2159    (*sc->tulip_boardsw->bd_media_select)(sc);
2160#ifdef TULIP_DEBUG
2161    if ((sc->tulip_flags & (TULIP_DEVICEPROBE|TULIP_NEEDRESET)) == TULIP_NEEDRESET)
2162	printf(TULIP_PRINTF_FMT ": tulip_reset: additional reset needed?!?\n",
2163	       TULIP_PRINTF_ARGS);
2164#endif
2165    if ((sc->tulip_flags & (TULIP_LINKUP|TULIP_PRINTMEDIA)) == (TULIP_LINKUP|TULIP_PRINTMEDIA)) {
2166	printf(TULIP_PRINTF_FMT ": enabling %s port\n",
2167	       TULIP_PRINTF_ARGS,
2168	       tulip_mediums[sc->tulip_media]);
2169	sc->tulip_flags &= ~TULIP_PRINTMEDIA;
2170    }
2171    if (sc->tulip_chipid == TULIP_21041)
2172	TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
2173
2174    sc->tulip_intrmask |= TULIP_STS_NORMALINTR|TULIP_STS_RXINTR|TULIP_STS_TXINTR
2175	|TULIP_STS_ABNRMLINTR|TULIP_STS_SYSERROR|TULIP_STS_TXSTOPPED
2176	    |TULIP_STS_TXBABBLE|TULIP_STS_LINKFAIL|TULIP_STS_RXSTOPPED;
2177    sc->tulip_flags &= ~(TULIP_DOINGSETUP|TULIP_WANTSETUP|TULIP_INRESET
2178			 |TULIP_RXACT);
2179    tulip_addr_filter(sc);
2180}
2181
2182static void
2183tulip_init(
2184    tulip_softc_t * const sc)
2185{
2186    if (sc->tulip_if.if_flags & IFF_UP) {
2187	if ((sc->tulip_if.if_flags & IFF_RUNNING) == 0) {
2188	    /* initialize the media */
2189	    tulip_reset(sc);
2190	}
2191	sc->tulip_if.if_flags |= IFF_RUNNING;
2192	if (sc->tulip_if.if_flags & IFF_PROMISC) {
2193	    sc->tulip_cmdmode |= TULIP_CMD_PROMISCUOUS;
2194	} else {
2195	    sc->tulip_cmdmode &= ~TULIP_CMD_PROMISCUOUS;
2196	    if (sc->tulip_if.if_flags & IFF_ALLMULTI) {
2197		sc->tulip_cmdmode |= TULIP_CMD_ALLMULTI;
2198	    } else {
2199		sc->tulip_cmdmode &= ~TULIP_CMD_ALLMULTI;
2200	    }
2201	}
2202	sc->tulip_cmdmode |= TULIP_CMD_TXRUN;
2203	if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
2204	    tulip_rx_intr(sc);
2205	    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
2206	    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
2207	} else {
2208	    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
2209	    tulip_ifstart(&sc->tulip_if);
2210	}
2211	TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2212	TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
2213    } else {
2214	sc->tulip_if.if_flags &= ~IFF_RUNNING;
2215	tulip_reset(sc);
2216    }
2217}
2218
2219static void
2220tulip_rx_intr(
2221    tulip_softc_t * const sc)
2222{
2223    tulip_ringinfo_t * const ri = &sc->tulip_rxinfo;
2224    struct ifnet * const ifp = &sc->tulip_if;
2225    int fillok = 1;
2226#ifdef TULIP_DEBUG
2227    int cnt = 0;
2228#endif
2229
2230    for (;;) {
2231	struct ether_header eh;
2232	tulip_desc_t *eop = ri->ri_nextin;
2233	int total_len = 0, last_offset = 0;
2234	struct mbuf *ms = NULL, *me = NULL;
2235	int accept = 0;
2236
2237	if (fillok && sc->tulip_rxq.ifq_len < TULIP_RXQ_TARGET)
2238	    goto queue_mbuf;
2239
2240#ifdef TULIP_DEBUG
2241	if (cnt == ri->ri_max)
2242	    break;
2243#endif
2244	/*
2245	 * If the TULIP has no descriptors, there can't be any receive
2246	 * descriptors to process.
2247 	 */
2248	if (eop == ri->ri_nextout)
2249	    break;
2250
2251	/*
2252	 * 90% of the packets will fit in one descriptor.  So we optimize
2253	 * for that case.
2254	 */
2255	if ((((volatile tulip_desc_t *) eop)->d_status & (TULIP_DSTS_OWNER|TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) == (TULIP_DSTS_RxFIRSTDESC|TULIP_DSTS_RxLASTDESC)) {
2256	    IF_DEQUEUE(&sc->tulip_rxq, ms);
2257	    me = ms;
2258	} else {
2259	    /*
2260	     * If still owned by the TULIP, don't touch it.
2261	     */
2262	    if (((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER)
2263		break;
2264
2265	    /*
2266	     * It is possible (though improbable unless the BIG_PACKET support
2267	     * is enabled or MCLBYTES < 1518) for a received packet to cross
2268	     * more than one receive descriptor.
2269	     */
2270	    while ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_RxLASTDESC) == 0) {
2271		if (++eop == ri->ri_last)
2272		    eop = ri->ri_first;
2273		if (eop == ri->ri_nextout || ((((volatile tulip_desc_t *) eop)->d_status & TULIP_DSTS_OWNER))) {
2274#ifdef TULIP_DEBUG
2275		    sc->tulip_dbg.dbg_rxintrs++;
2276		    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
2277#endif
2278		    return;
2279		}
2280		total_len++;
2281	    }
2282
2283	    /*
2284	     * Dequeue the first buffer for the start of the packet.  Hopefully
2285	     * this will be the only one we need to dequeue.  However, if the
2286	     * packet consumed multiple descriptors, then we need to dequeue
2287	     * those buffers and chain to the starting mbuf.  All buffers but
2288	     * the last buffer have the same length so we can set that now.
2289	     * (we add to last_offset instead of multiplying since we normally
2290	     * won't go into the loop and thereby saving a ourselves from
2291	     * doing a multiplication by 0 in the normal case).
2292	     */
2293	    IF_DEQUEUE(&sc->tulip_rxq, ms);
2294	    for (me = ms; total_len > 0; total_len--) {
2295		me->m_len = TULIP_RX_BUFLEN;
2296		last_offset += TULIP_RX_BUFLEN;
2297		IF_DEQUEUE(&sc->tulip_rxq, me->m_next);
2298		me = me->m_next;
2299	    }
2300	}
2301
2302	/*
2303	 *  Now get the size of received packet (minus the CRC).
2304	 */
2305	total_len = ((eop->d_status >> 16) & 0x7FFF) - 4;
2306	if ((eop->d_status & TULIP_DSTS_ERRSUM) == 0
2307#ifdef BIG_PACKET
2308	     || (total_len <= sc->tulip_if.if_mtu + sizeof(struct ether_header) &&
2309		 (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxRUNT|
2310				  TULIP_DSTS_RxCOLLSEEN|TULIP_DSTS_RxBADCRC|
2311				  TULIP_DSTS_RxOVERFLOW)) == 0)
2312#endif
2313		) {
2314	    me->m_len = total_len - last_offset;
2315	    eh = *mtod(ms, struct ether_header *);
2316#if NBPFILTER > 0
2317	    if (sc->tulip_bpf != NULL)
2318		if (me == ms)
2319		    TULIP_BPF_TAP(sc, mtod(ms, caddr_t), total_len);
2320		else
2321		    TULIP_BPF_MTAP(sc, ms);
2322#endif
2323	    if ((sc->tulip_if.if_flags & IFF_PROMISC)
2324		    && (eh.ether_dhost[0] & 1) == 0
2325		    && !TULIP_ADDREQUAL(eh.ether_dhost, sc->tulip_ac.ac_enaddr))
2326		    goto next;
2327	    accept = 1;
2328	    sc->tulip_flags |= TULIP_RXACT;
2329	    total_len -= sizeof(struct ether_header);
2330	} else {
2331	    ifp->if_ierrors++;
2332	    if (eop->d_status & (TULIP_DSTS_RxBADLENGTH|TULIP_DSTS_RxOVERFLOW|TULIP_DSTS_RxWATCHDOG)) {
2333		sc->tulip_dot3stats.dot3StatsInternalMacReceiveErrors++;
2334	    } else {
2335		const char *error = NULL;
2336		if (eop->d_status & TULIP_DSTS_RxTOOLONG) {
2337		    sc->tulip_dot3stats.dot3StatsFrameTooLongs++;
2338		    error = "frame too long";
2339		}
2340		if (eop->d_status & TULIP_DSTS_RxBADCRC) {
2341		    if (eop->d_status & TULIP_DSTS_RxDRBBLBIT) {
2342			sc->tulip_dot3stats.dot3StatsAlignmentErrors++;
2343			error = "alignment error";
2344		    } else {
2345			sc->tulip_dot3stats.dot3StatsFCSErrors++;
2346			error = "bad crc";
2347		    }
2348		}
2349#ifdef DIAGNOSTIC
2350		if (error != NULL && (sc->tulip_flags & TULIP_NOMESSAGES) == 0) {
2351		    printf(TULIP_PRINTF_FMT ": receive: " TULIP_EADDR_FMT ": %s\n",
2352			   TULIP_PRINTF_ARGS,
2353			   TULIP_EADDR_ARGS(mtod(ms, u_char *) + 6),
2354			   error);
2355		    sc->tulip_flags |= TULIP_NOMESSAGES;
2356		}
2357#endif
2358	    }
2359	}
2360      next:
2361#ifdef TULIP_DEBUG
2362	cnt++;
2363#endif
2364	ifp->if_ipackets++;
2365	if (++eop == ri->ri_last)
2366	    eop = ri->ri_first;
2367	ri->ri_nextin = eop;
2368      queue_mbuf:
2369	/*
2370	 * Either we are priming the TULIP with mbufs (m == NULL)
2371	 * or we are about to accept an mbuf for the upper layers
2372	 * so we need to allocate an mbuf to replace it.  If we
2373	 * can't replace it, send up it anyways.  This may cause
2374	 * us to drop packets in the future but that's better than
2375	 * being caught in livelock.
2376	 *
2377	 * Note that if this packet crossed multiple descriptors
2378	 * we don't even try to reallocate all the mbufs here.
2379	 * Instead we rely on the test of the beginning of
2380	 * the loop to refill for the extra consumed mbufs.
2381	 */
2382	if (accept || ms == NULL) {
2383	    struct mbuf *m0;
2384	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
2385	    if (m0 != NULL) {
2386#if defined(TULIP_COPY_RXDATA)
2387		if (!accept || total_len >= MHLEN) {
2388#endif
2389		    MCLGET(m0, M_DONTWAIT);
2390		    if ((m0->m_flags & M_EXT) == 0) {
2391			m_freem(m0);
2392			m0 = NULL;
2393		    }
2394#if defined(TULIP_COPY_RXDATA)
2395		}
2396#endif
2397	    }
2398	    if (accept) {
2399#if defined(__bsdi__)
2400		eh.ether_type = ntohs(eh.ether_type);
2401#endif
2402#if !defined(TULIP_COPY_RXDATA)
2403		ms->m_data += sizeof(struct ether_header);
2404		ms->m_len -= sizeof(struct ether_header);
2405		ms->m_pkthdr.len = total_len;
2406		ms->m_pkthdr.rcvif = ifp;
2407		ether_input(ifp, &eh, ms);
2408#else
2409#ifdef BIG_PACKET
2410#error BIG_PACKET is incompatible with TULIP_COPY_RXDATA
2411#endif
2412		if (ms == me)
2413		    bcopy(mtod(ms, caddr_t) + sizeof(struct ether_header),
2414			  mtod(m0, caddr_t), total_len);
2415		else
2416		    m_copydata(ms, 0, total_len, mtod(m0, caddr_t));
2417		m0->m_len = m0->m_pkthdr.len = total_len;
2418		m0->m_pkthdr.rcvif = ifp;
2419		ether_input(ifp, &eh, m0);
2420		m0 = ms;
2421#endif
2422	    }
2423	    ms = m0;
2424	}
2425	if (ms == NULL) {
2426	    /*
2427	     * Couldn't allocate a new buffer.  Don't bother
2428	     * trying to replenish the receive queue.
2429	     */
2430	    fillok = 0;
2431	    sc->tulip_flags |= TULIP_RXBUFSLOW;
2432#ifdef TULIP_DEBUG
2433	    sc->tulip_dbg.dbg_rxlowbufs++;
2434#endif
2435	    continue;
2436	}
2437	/*
2438	 * Now give the buffer(s) to the TULIP and save in our
2439	 * receive queue.
2440	 */
2441	do {
2442	    ri->ri_nextout->d_length1 = TULIP_RX_BUFLEN;
2443	    ri->ri_nextout->d_addr1 = TULIP_KVATOPHYS(sc, mtod(ms, caddr_t));
2444	    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
2445	    if (++ri->ri_nextout == ri->ri_last)
2446		ri->ri_nextout = ri->ri_first;
2447	    me = ms->m_next;
2448	    ms->m_next = NULL;
2449	    IF_ENQUEUE(&sc->tulip_rxq, ms);
2450	} while ((ms = me) != NULL);
2451
2452	if (sc->tulip_rxq.ifq_len >= TULIP_RXQ_TARGET)
2453	    sc->tulip_flags &= ~TULIP_RXBUFSLOW;
2454    }
2455
2456#ifdef TULIP_DEBUG
2457    sc->tulip_dbg.dbg_rxintrs++;
2458    sc->tulip_dbg.dbg_rxpktsperintr[cnt]++;
2459#endif
2460}
2461
2462static int
2463tulip_tx_intr(
2464    tulip_softc_t * const sc)
2465{
2466    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
2467    struct mbuf *m;
2468    int xmits = 0;
2469
2470    while (ri->ri_free < ri->ri_max) {
2471	if (((volatile tulip_desc_t *) ri->ri_nextin)->d_status & TULIP_DSTS_OWNER)
2472	    break;
2473
2474	if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxLASTSEG) {
2475	    if (ri->ri_nextin->d_flag & TULIP_DFLAG_TxSETUPPKT) {
2476		/*
2477		 * We've just finished processing a setup packet.
2478		 * Mark that we can finished it.  If there's not
2479		 * another pending, startup the TULIP receiver.
2480		 * Make sure we ack the RXSTOPPED so we won't get
2481		 * an abormal interrupt indication.
2482		 */
2483		sc->tulip_flags &= ~TULIP_DOINGSETUP;
2484		if ((sc->tulip_flags & TULIP_WANTSETUP) == 0) {
2485		    tulip_rx_intr(sc);
2486		    sc->tulip_cmdmode |= TULIP_CMD_RXRUN;
2487		    sc->tulip_intrmask |= TULIP_STS_RXSTOPPED;
2488		    TULIP_CSR_WRITE(sc, csr_status, TULIP_STS_RXSTOPPED);
2489		    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
2490			TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
2491		    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2492		}
2493	    } else {
2494		tulip_desc_t * eop = ri->ri_nextin;
2495		IF_DEQUEUE(&sc->tulip_txq, m);
2496		m_freem(m);
2497		xmits++;
2498		if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
2499		    if ((eop->d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxEXCCOLL)) == 0)
2500			sc->tulip_flags |= TULIP_TXPROBE_OK;
2501		    (*sc->tulip_boardsw->bd_media_select)(sc);
2502		    if (sc->tulip_chipid == TULIP_21041)
2503			TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
2504		} else {
2505		    if (eop->d_status & TULIP_DSTS_ERRSUM) {
2506			sc->tulip_if.if_oerrors++;
2507			if (eop->d_status & TULIP_DSTS_TxEXCCOLL)
2508			    sc->tulip_dot3stats.dot3StatsExcessiveCollisions++;
2509			if (eop->d_status & TULIP_DSTS_TxLATECOLL)
2510			    sc->tulip_dot3stats.dot3StatsLateCollisions++;
2511			if (eop->d_status & (TULIP_DSTS_TxNOCARR|TULIP_DSTS_TxCARRLOSS))
2512			    sc->tulip_dot3stats.dot3StatsCarrierSenseErrors++;
2513			if (eop->d_status & (TULIP_DSTS_TxUNDERFLOW|TULIP_DSTS_TxBABBLE))
2514			    sc->tulip_dot3stats.dot3StatsInternalMacTransmitErrors++;
2515		    } else {
2516			u_int32_t collisions =
2517			    (eop->d_status & TULIP_DSTS_TxCOLLMASK)
2518				>> TULIP_DSTS_V_TxCOLLCNT;
2519			sc->tulip_if.if_collisions += collisions;
2520			if (collisions == 1)
2521			    sc->tulip_dot3stats.dot3StatsSingleCollisionFrames++;
2522			else if (collisions > 1)
2523			    sc->tulip_dot3stats.dot3StatsMultipleCollisionFrames++;
2524			else if (eop->d_status & TULIP_DSTS_TxDEFERRED)
2525			    sc->tulip_dot3stats.dot3StatsDeferredTransmissions++;
2526			/*
2527			 * SQE is only valid for 10baseT/BNC/AUI when not
2528			 * running in full-duplex.  In order to speed up the
2529			 * test, the corresponding bit in tulip_flags needs to
2530			 * set as well to get us to count SQE Test Errors.
2531			 */
2532			if (eop->d_status & TULIP_DSTS_TxNOHRTBT & sc->tulip_flags)
2533			    sc->tulip_dot3stats.dot3StatsSQETestErrors++;
2534		    }
2535		}
2536	    }
2537	}
2538
2539	if (++ri->ri_nextin == ri->ri_last)
2540	    ri->ri_nextin = ri->ri_first;
2541	ri->ri_free++;
2542	sc->tulip_if.if_flags &= ~IFF_OACTIVE;
2543    }
2544    /*
2545     * If nothing left to transmit, disable the timer.
2546     * Else if progress, reset the timer back to 2 ticks.
2547     */
2548    if (ri->ri_free == ri->ri_max || (sc->tulip_flags & TULIP_TXPROBE_ACTIVE))
2549	sc->tulip_txtimer = 0;
2550    else if (xmits > 0)
2551	sc->tulip_txtimer = TULIP_TXTIMER;
2552    sc->tulip_if.if_opackets += xmits;
2553    return xmits;
2554}
2555
2556static void
2557tulip_print_abnormal_interrupt(
2558    tulip_softc_t * const sc,
2559    u_int32_t csr)
2560{
2561    const char * const *msgp = tulip_status_bits;
2562    const char *sep;
2563
2564    csr &= (1 << (sizeof(tulip_status_bits)/sizeof(tulip_status_bits[0]))) - 1;
2565    printf(TULIP_PRINTF_FMT ": abnormal interrupt:", TULIP_PRINTF_ARGS);
2566    for (sep = " "; csr != 0; csr >>= 1, msgp++) {
2567	if ((csr & 1) && *msgp != NULL) {
2568	    printf("%s%s", sep, *msgp);
2569	    sep = ", ";
2570	}
2571    }
2572    printf("\n");
2573}
2574
2575static void
2576tulip_intr_handler(
2577    tulip_softc_t * const sc,
2578    int *progress_p)
2579{
2580    u_int32_t csr;
2581
2582    while ((csr = TULIP_CSR_READ(sc, csr_status)) & sc->tulip_intrmask) {
2583	*progress_p = 1;
2584	TULIP_CSR_WRITE(sc, csr_status, csr);
2585
2586	if (csr & TULIP_STS_SYSERROR) {
2587	    sc->tulip_last_system_error = (csr & TULIP_STS_ERRORMASK) >> TULIP_STS_ERR_SHIFT;
2588	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
2589		sc->tulip_flags |= TULIP_SYSTEMERROR;
2590	    } else {
2591		printf(TULIP_PRINTF_FMT ": system error: %s\n",
2592		       TULIP_PRINTF_ARGS,
2593		       tulip_system_errors[sc->tulip_last_system_error]);
2594	    }
2595	    sc->tulip_flags |= TULIP_NEEDRESET;
2596	    sc->tulip_system_errors++;
2597	    break;
2598	}
2599	if (csr & (TULIP_STS_GPTIMEOUT|TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL)) {
2600#if defined(TULIP_DEBUG)
2601	    sc->tulip_dbg.dbg_gpintrs++;
2602#endif
2603	    if (sc->tulip_chipid == TULIP_21041) {
2604		(*sc->tulip_boardsw->bd_media_select)(sc);
2605		if (csr & (TULIP_STS_LINKPASS|TULIP_STS_LINKFAIL))
2606		    csr &= ~TULIP_STS_ABNRMLINTR;
2607		TULIP_CSR_WRITE(sc, csr_sia_status, TULIP_CSR_READ(sc, csr_sia_status));
2608	    } else if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
2609		(*sc->tulip_boardsw->bd_media_select)(sc);
2610		csr &= ~(TULIP_STS_ABNRMLINTR|TULIP_STS_GPTIMEOUT);
2611	    }
2612	    if ((sc->tulip_flags & (TULIP_LINKUP|TULIP_PRINTMEDIA)) == (TULIP_LINKUP|TULIP_PRINTMEDIA)) {
2613		printf(TULIP_PRINTF_FMT ": enabling %s port\n",
2614		       TULIP_PRINTF_ARGS,
2615		       tulip_mediums[sc->tulip_media]);
2616		sc->tulip_flags &= ~TULIP_PRINTMEDIA;
2617	    }
2618	}
2619	if (csr & TULIP_STS_ABNRMLINTR) {
2620	    u_int32_t tmp = csr & sc->tulip_intrmask
2621		& ~(TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR);
2622	    if (sc->tulip_flags & TULIP_NOMESSAGES) {
2623		sc->tulip_statusbits |= tmp;
2624	    } else {
2625		tulip_print_abnormal_interrupt(sc, tmp);
2626		sc->tulip_flags |= TULIP_NOMESSAGES;
2627	    }
2628	    TULIP_CSR_WRITE(sc, csr_command, sc->tulip_cmdmode);
2629	}
2630	if (csr & (TULIP_STS_RXINTR|TULIP_STS_RXNOBUF)) {
2631	    tulip_rx_intr(sc);
2632	    if (csr & TULIP_STS_RXNOBUF)
2633		sc->tulip_dot3stats.dot3StatsMissedFrames +=
2634		    TULIP_CSR_READ(sc, csr_missed_frames) & 0xFFFF;
2635	}
2636	if (sc->tulip_txinfo.ri_free < sc->tulip_txinfo.ri_max) {
2637	    tulip_tx_intr(sc);
2638	    if ((sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
2639		tulip_ifstart(&sc->tulip_if);
2640	}
2641    }
2642    if (sc->tulip_flags & TULIP_NEEDRESET) {
2643	tulip_reset(sc);
2644	tulip_init(sc);
2645    }
2646}
2647
2648#if defined(TULIP_USE_SOFTINTR)
2649/*
2650 * This is a experimental idea to alleviate problems due to interrupt
2651 * livelock.  What is interrupt livelock?  It's when you spend all your
2652 * time servicing device interrupts and never drop below device ipl
2653 * to do "useful" work.
2654 *
2655 * So what we do here is see if the device needs service and if so,
2656 * disable interrupts (dismiss the interrupt), place it in a list of devices
2657 * needing service, and issue a network software interrupt.
2658 *
2659 * When our network software interrupt routine gets called, we simply
2660 * walk done the list of devices that we have created and deal with them
2661 * at splnet/splsoftnet.
2662 *
2663 */
2664static void
2665tulip_hardintr_handler(
2666    tulip_softc_t * const sc,
2667    int *progress_p)
2668{
2669    if (TULIP_CSR_READ(sc, csr_status) & (TULIP_STS_NORMALINTR|TULIP_STS_ABNRMLINTR) == 0)
2670	return;
2671    *progress_p = 1;
2672    /*
2673     * disable interrupts
2674     */
2675    TULIP_CSR_WRITE(sc, csr_intr, 0);
2676    /*
2677     * mark it as needing a software interrupt
2678     */
2679    tulip_softintr_mask |= (1U << sc->tulip_unit);
2680}
2681
2682static void
2683tulip_softintr(
2684    void)
2685{
2686    u_int32_t softintr_mask, mask;
2687    int progress = 0;
2688    int unit;
2689    tulip_spl_t s;
2690
2691    /*
2692     * Copy mask to local copy and reset global one to 0.
2693     */
2694    s = splimp();
2695    softintr_mask = tulip_softintr_mask;
2696    tulip_softintr_mask = 0;
2697    splx(s);
2698
2699    /*
2700     * Optimize for the single unit case.
2701     */
2702    if (tulip_softintr_max_unit == 0) {
2703	if (softintr_mask & 1) {
2704	    tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(0);
2705	    /*
2706	     * Handle the "interrupt" and then reenable interrupts
2707	     */
2708	    tulip_intr_handler(sc, &progress);
2709	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2710	}
2711	return;
2712    }
2713
2714    /*
2715     * Handle all "queued" interrupts in a round robin fashion.
2716     * This is done so as not to favor a particular interface.
2717     */
2718    unit = tulip_softintr_last_unit;
2719    mask = (1U << unit);
2720    while (softintr_mask != 0) {
2721	if (tulip_softintr_max_unit == unit) {
2722	    unit  = 0; mask   = 1;
2723	} else {
2724	    unit += 1; mask <<= 1;
2725	}
2726	if (softintr_mask & mask) {
2727	    tulip_softc_t * const sc = TULIP_UNIT_TO_SOFTC(unit);
2728	    /*
2729	     * Handle the "interrupt" and then reenable interrupts
2730	     */
2731	    tulip_intr_handler(sc, &progress);
2732	    TULIP_CSR_WRITE(sc, csr_intr, sc->tulip_intrmask);
2733	    softintr_mask ^= mask;
2734	}
2735    }
2736
2737    /*
2738     * Save where we ending up.
2739     */
2740    tulip_softintr_last_unit = unit;
2741}
2742#endif	/* TULIP_USE_SOFTINTR */
2743
2744static tulip_intrfunc_t
2745tulip_intr_shared(
2746    void *arg)
2747{
2748    tulip_softc_t * sc;
2749    int progress = 0;
2750
2751    for (sc = (tulip_softc_t *) arg; sc != NULL; sc = sc->tulip_slaves) {
2752#if defined(TULIP_DEBUG)
2753	sc->tulip_dbg.dbg_intrs++;
2754#endif
2755#if defined(TULIP_USE_SOFTINTR)
2756	tulip_hardintr_handler(sc, &progress);
2757#else
2758	tulip_intr_handler(sc, &progress);
2759#endif
2760    }
2761#if defined(TULIP_USE_SOFTINTR)
2762    if (progress)
2763	schednetisr(NETISR_DE);
2764#endif
2765#if !defined(TULIP_VOID_INTRFUNC)
2766    return progress;
2767#endif
2768}
2769
2770static tulip_intrfunc_t
2771tulip_intr_normal(
2772    void *arg)
2773{
2774    tulip_softc_t * sc = (tulip_softc_t *) arg;
2775    int progress = 0;
2776
2777#if defined(TULIP_DEBUG)
2778    sc->tulip_dbg.dbg_intrs++;
2779#endif
2780#if defined(TULIP_USE_SOFTINTR)
2781    tulip_hardintr_handler(sc, &progress);
2782    if (progress)
2783	schednetisr(NETISR_DE);
2784#else
2785    tulip_intr_handler(sc, &progress);
2786#endif
2787#if !defined(TULIP_VOID_INTRFUNC)
2788    return progress;
2789#endif
2790}
2791
2792/*
2793 *
2794 */
2795
2796static void
2797tulip_delay_300ns(
2798    tulip_softc_t * const sc)
2799{
2800    int idx;
2801    for (idx = (300 / 33) + 1; idx > 0; idx--)
2802	TULIP_CSR_READ(sc, csr_busmode);
2803}
2804
2805#define EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
2806
2807static void
2808tulip_srom_idle(
2809    tulip_softc_t * const sc)
2810{
2811    unsigned bit, csr;
2812
2813    csr  = SROMSEL ; EMIT;
2814    csr  = SROMSEL | SROMRD; EMIT;
2815    csr ^= SROMCS; EMIT;
2816    csr ^= SROMCLKON; EMIT;
2817
2818    /*
2819     * Write 25 cycles of 0 which will force the SROM to be idle.
2820     */
2821    for (bit = 3 + SROM_BITWIDTH + 16; bit > 0; bit--) {
2822        csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
2823        csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
2824    }
2825    csr ^= SROMCLKOFF; EMIT;
2826    csr ^= SROMCS; EMIT;
2827    csr  = 0; EMIT;
2828}
2829
2830
2831static void
2832tulip_srom_read(
2833    tulip_softc_t * const sc)
2834{
2835    int idx;
2836    const unsigned bitwidth = SROM_BITWIDTH;
2837    const unsigned cmdmask = (SROMCMD_RD << bitwidth);
2838    const unsigned msb = 1 << (bitwidth + 3 - 1);
2839    unsigned lastidx = (1 << bitwidth) - 1;
2840
2841    tulip_srom_idle(sc);
2842
2843    for (idx = 0; idx <= lastidx; idx++) {
2844        unsigned lastbit, data, bits, bit, csr;
2845	csr  = SROMSEL ;	        EMIT;
2846        csr  = SROMSEL | SROMRD;        EMIT;
2847        csr ^= SROMCSON;                EMIT;
2848        csr ^=            SROMCLKON;    EMIT;
2849
2850        lastbit = 0;
2851        for (bits = idx|cmdmask, bit = bitwidth + 3; bit > 0; bit--, bits <<= 1) {
2852            const unsigned thisbit = bits & msb;
2853            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
2854            if (thisbit != lastbit) {
2855                csr ^= SROMDOUT; EMIT;  /* clock low; invert data */
2856            } else {
2857		EMIT;
2858	    }
2859            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
2860            lastbit = thisbit;
2861        }
2862        csr ^= SROMCLKOFF; EMIT;
2863
2864        for (data = 0, bits = 0; bits < 16; bits++) {
2865            data <<= 1;
2866            csr ^= SROMCLKON; EMIT;     /* clock high; data valid */
2867            data |= TULIP_CSR_READ(sc, csr_srom_mii) & SROMDIN ? 1 : 0;
2868            csr ^= SROMCLKOFF; EMIT;    /* clock low; data not valid */
2869        }
2870	sc->tulip_rombuf[idx*2] = data & 0xFF;
2871	sc->tulip_rombuf[idx*2+1] = data >> 8;
2872	csr  = SROMSEL | SROMRD; EMIT;
2873	csr  = 0; EMIT;
2874    }
2875    tulip_srom_idle(sc);
2876}
2877
2878#define MII_EMIT    do { TULIP_CSR_WRITE(sc, csr_srom_mii, csr); tulip_delay_300ns(sc); } while (0)
2879
2880static void
2881tulip_mii_sendbits(
2882    tulip_softc_t * const sc,
2883    unsigned data,
2884    unsigned bits)
2885{
2886    unsigned msb = 1 << (bits - 1);
2887    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2888    unsigned lastbit = (csr & MII_DOUT) ? msb : 0;
2889
2890    csr |= MII_WR; MII_EMIT;  		/* clock low; assert write */
2891
2892    for (; bits > 0; bits--, data <<= 1) {
2893	const unsigned thisbit = data & msb;
2894	if (thisbit != lastbit) {
2895	    csr ^= MII_DOUT; MII_EMIT;  /* clock low; invert data */
2896	}
2897	csr ^= MII_CLKON; MII_EMIT;     /* clock high; data valid */
2898	lastbit = thisbit;
2899	csr ^= MII_CLKOFF; MII_EMIT;    /* clock low; data not valid */
2900    }
2901}
2902
2903static void
2904tulip_mii_turnaround(
2905    tulip_softc_t * const sc,
2906    unsigned cmd)
2907{
2908    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2909
2910    if (cmd == MII_WRCMD) {
2911	csr |= MII_DOUT; MII_EMIT;	/* clock low; change data */
2912	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
2913	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
2914	csr ^= MII_DOUT; MII_EMIT;	/* clock low; change data */
2915    } else {
2916	csr |= MII_RD; MII_EMIT;	/* clock low; switch to read */
2917    }
2918    csr ^= MII_CLKON; MII_EMIT;		/* clock high; data valid */
2919    csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
2920}
2921
2922static unsigned
2923tulip_mii_readbits(
2924    tulip_softc_t * const sc)
2925{
2926    unsigned data;
2927    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2928    int idx;
2929
2930    for (idx = 0, data = 0; idx < 16; idx++) {
2931	data <<= 1;	/* this is NOOP on the first pass through */
2932	csr ^= MII_CLKON; MII_EMIT;	/* clock high; data valid */
2933	if (TULIP_CSR_READ(sc, csr_srom_mii) & MII_DIN)
2934	    data |= 1;
2935	csr ^= MII_CLKOFF; MII_EMIT;	/* clock low; data not valid */
2936    }
2937    csr ^= MII_RD; MII_EMIT;		/* clock low; turn off read */
2938
2939    return data;
2940}
2941
2942static unsigned
2943tulip_mii_readreg(
2944    tulip_softc_t * const sc,
2945    unsigned devaddr,
2946    unsigned regno)
2947{
2948    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2949    unsigned data;
2950
2951    csr &= ~(MII_RD|MII_CLK); MII_EMIT;
2952    tulip_mii_sendbits(sc, MII_PREAMBLE, 32);
2953    tulip_mii_sendbits(sc, MII_RDCMD, 8);
2954    tulip_mii_sendbits(sc, devaddr, 5);
2955    tulip_mii_sendbits(sc, regno, 5);
2956    tulip_mii_turnaround(sc, MII_RDCMD);
2957
2958    data = tulip_mii_readbits(sc);
2959#ifdef TULIP_DEBUG
2960    sc->tulip_dbg.dbg_phyregs[regno][0] = data;
2961    sc->tulip_dbg.dbg_phyregs[regno][1]++;
2962#endif
2963    return data;
2964}
2965
2966static void
2967tulip_mii_writereg(
2968    tulip_softc_t * const sc,
2969    unsigned devaddr,
2970    unsigned regno,
2971    unsigned data)
2972{
2973    unsigned csr = TULIP_CSR_READ(sc, csr_srom_mii) & (MII_RD|MII_DOUT|MII_CLK);
2974    csr &= ~(MII_RD|MII_CLK); MII_EMIT;
2975    tulip_mii_sendbits(sc, MII_PREAMBLE, 32);
2976    tulip_mii_sendbits(sc, MII_WRCMD, 8);
2977    tulip_mii_sendbits(sc, devaddr, 5);
2978    tulip_mii_sendbits(sc, regno, 5);
2979    tulip_mii_turnaround(sc, MII_WRCMD);
2980    tulip_mii_sendbits(sc, data, 16);
2981#ifdef TULIP_DEBUG
2982    sc->tulip_dbg.dbg_phyregs[regno][2] = data;
2983    sc->tulip_dbg.dbg_phyregs[regno][3]++;
2984#endif
2985}
2986
2987#define	tulip_mchash(mca)	(tulip_crc32(mca, 6) & 0x1FF)
2988#define	tulip_srom_crcok(databuf)	( \
2989    ((tulip_crc32(databuf, 126) & 0xFFFF) ^ 0xFFFF)== \
2990     ((databuf)[126] | ((databuf)[127] << 8)))
2991
2992static unsigned
2993tulip_crc32(
2994    const unsigned char *databuf,
2995    size_t datalen)
2996{
2997    u_int idx, bit, data, crc = 0xFFFFFFFFUL;
2998
2999    for (idx = 0; idx < datalen; idx++)
3000        for (data = *databuf++, bit = 0; bit < 8; bit++, data >>= 1)
3001            crc = (crc >> 1) ^ (((crc ^ data) & 1) ? TULIP_CRC32_POLY : 0);
3002    return crc;
3003}
3004
3005static void
3006tulip_identify_smc_nic(
3007    tulip_softc_t *sc)
3008{
3009    u_int32_t id1, id2, ei;
3010    int auibnc = 0, utp = 0;
3011    char *cp;
3012
3013    if (sc->tulip_chipid == TULIP_21041)
3014	return;
3015    if (sc->tulip_chipid == TULIP_21140) {
3016	sc->tulip_boardsw = &tulip_21140_smc9332_boardsw;
3017	return;
3018    }
3019    id1 = sc->tulip_rombuf[0x60] | (sc->tulip_rombuf[0x61] << 8);
3020    id2 = sc->tulip_rombuf[0x62] | (sc->tulip_rombuf[0x63] << 8);
3021    ei  = sc->tulip_rombuf[0x66] | (sc->tulip_rombuf[0x67] << 8);
3022
3023    strcpy(sc->tulip_boardidbuf, "SMC 8432");
3024    cp = &sc->tulip_boardidbuf[8];
3025    if ((id1 & 1) == 0)
3026	*cp++ = 'B', auibnc = 1;
3027    if ((id1 & 0xFF) > 0x32)
3028	*cp++ = 'T', utp = 1;
3029    if ((id1 & 0x4000) == 0)
3030	*cp++ = 'A', auibnc = 1;
3031    if (id2 == 0x15) {
3032	sc->tulip_boardidbuf[7] = '4';
3033	*cp++ = '-';
3034	*cp++ = 'C';
3035	*cp++ = 'H';
3036	*cp++ = (ei ? '2' : '1');
3037    }
3038    *cp++ = ' ';
3039    *cp = '\0';
3040    if (utp && !auibnc)
3041	sc->tulip_boardsw = &tulip_21040_10baset_only_boardsw;
3042    else if (!utp && auibnc)
3043	sc->tulip_boardsw = &tulip_21040_auibnc_only_boardsw;
3044}
3045
3046/*
3047 * This deals with the vagaries of the address roms and the
3048 * brain-deadness that various vendors commit in using them.
3049 */
3050static int
3051tulip_read_macaddr(
3052    tulip_softc_t *sc)
3053{
3054    int cksum, rom_cksum, idx;
3055    u_int32_t csr;
3056    unsigned char tmpbuf[8];
3057    static const u_char testpat[] = { 0xFF, 0, 0x55, 0xAA, 0xFF, 0, 0x55, 0xAA };
3058
3059    if (sc->tulip_chipid == TULIP_21040) {
3060	TULIP_CSR_WRITE(sc, csr_enetrom, 1);
3061	for (idx = 0; idx < sizeof(sc->tulip_rombuf); idx++) {
3062	    int cnt = 0;
3063	    while (((csr = TULIP_CSR_READ(sc, csr_enetrom)) & 0x80000000L) && cnt < 10000)
3064		cnt++;
3065	    sc->tulip_rombuf[idx] = csr & 0xFF;
3066	}
3067	sc->tulip_boardsw = &tulip_21040_boardsw;
3068#if defined(TULIP_EISA)
3069    } else if (sc->tulip_chipid == TULIP_DE425) {
3070	int cnt;
3071	for (idx = 0, cnt = 0; idx < sizeof(testpat) && cnt < 32; cnt++) {
3072	    tmpbuf[idx] = TULIP_CSR_READBYTE(sc, csr_enetrom);
3073	    if (tmpbuf[idx] == testpat[idx])
3074		++idx;
3075	    else
3076		idx = 0;
3077	}
3078	for (idx = 0; idx < 32; idx++)
3079	    sc->tulip_rombuf[idx] = TULIP_CSR_READBYTE(sc, csr_enetrom);
3080	sc->tulip_boardsw = &tulip_21040_boardsw;
3081#endif /* TULIP_EISA */
3082    } else {
3083	int new_srom_fmt = 0;
3084	/*
3085	 * Thankfully all 21041's act the same.
3086	 * Assume all 21140 board are compatible with the
3087	 * DEC 10/100 evaluation board.  Not really valid but
3088	 * it's the best we can do until every one switches to
3089	 * the new SROM format.
3090	 */
3091	if (sc->tulip_chipid == TULIP_21041)
3092	    sc->tulip_boardsw = &tulip_21041_boardsw;
3093	else
3094	    sc->tulip_boardsw = &tulip_21140_eb_boardsw;
3095	tulip_srom_read(sc);
3096	if (tulip_srom_crcok(sc->tulip_rombuf)) {
3097	    /*
3098	     * SROM CRC is valid therefore it must be in the
3099	     * new format.
3100	     */
3101	    new_srom_fmt = 1;
3102	} else if (sc->tulip_rombuf[126] == 0xff && sc->tulip_rombuf[127] == 0xFF) {
3103	    /*
3104	     * No checksum is present.  See if the SROM id checks out;
3105	     * the first 18 bytes should be 0 followed by a 1 followed
3106	     * by the number of adapters (which we don't deal with yet).
3107	     */
3108	    for (idx = 0; idx < 18; idx++) {
3109		if (sc->tulip_rombuf[idx] != 0)
3110		    break;
3111	    }
3112	    if (idx == 18 && sc->tulip_rombuf[18] == 1 && sc->tulip_rombuf[19] != 0)
3113		new_srom_fmt = 2;
3114	}
3115	if (new_srom_fmt) {
3116	    int copy_name = 0;
3117	    /*
3118	     * New SROM format.  Copy out the Ethernet address.
3119	     * If it contains a DE500-XA string, then it must be
3120	     * a DE500-XA.
3121	     */
3122	    bcopy(sc->tulip_rombuf + 20, sc->tulip_hwaddr, 6);
3123	    if (bcmp(sc->tulip_rombuf + 29, "DE500-XA", 8) == 0) {
3124		sc->tulip_boardsw = &tulip_21140_de500xa_boardsw;
3125		copy_name = 1;
3126	    } else if (bcmp(sc->tulip_rombuf + 29, "DE500-AA", 8) == 0) {
3127		sc->tulip_boardsw = &tulip_21140_de500aa_boardsw;
3128		copy_name = 1;
3129	    } else if (bcmp(sc->tulip_rombuf + 29, "DE450", 5) == 0) {
3130		copy_name = 1;
3131	    }
3132	    if (copy_name) {
3133		bcopy(sc->tulip_rombuf + 29, sc->tulip_boardidbuf, 8);
3134		sc->tulip_boardidbuf[8] = ' ';
3135		sc->tulip_boardid = sc->tulip_boardidbuf;
3136	    }
3137	    if (sc->tulip_boardsw == NULL)
3138		return -6;
3139	    goto check_oui;
3140	}
3141    }
3142
3143
3144    if (bcmp(&sc->tulip_rombuf[0], &sc->tulip_rombuf[16], 8) != 0) {
3145	/*
3146	 * Some folks don't use the standard ethernet rom format
3147	 * but instead just put the address in the first 6 bytes
3148	 * of the rom and let the rest be all 0xffs.  (Can we say
3149	 * ZNYX???) (well sometimes they put in a checksum so we'll
3150	 * start at 8).
3151	 */
3152	for (idx = 8; idx < 32; idx++) {
3153	    if (sc->tulip_rombuf[idx] != 0xFF)
3154		return -4;
3155	}
3156	/*
3157	 * Make sure the address is not multicast or locally assigned
3158	 * that the OUI is not 00-00-00.
3159	 */
3160	if ((sc->tulip_rombuf[0] & 3) != 0)
3161	    return -4;
3162	if (sc->tulip_rombuf[0] == 0 && sc->tulip_rombuf[1] == 0
3163		&& sc->tulip_rombuf[2] == 0)
3164	    return -4;
3165	bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
3166	sc->tulip_flags |= TULIP_ROMOK;
3167	goto check_oui;
3168    } else {
3169	/*
3170	 * A number of makers of multiport boards (ZNYX and Cogent)
3171	 * only put on one address ROM on their 21040 boards.  So
3172	 * if the ROM is all zeros and this is a 21040, look at the
3173	 * previous configured boards (as long as they are on the same
3174	 * PCI bus and the bus number is non-zero) until we find the
3175	 * master board with address ROM.  We then use its address ROM
3176	 * as the base for this board.  (we add our relative board
3177	 * to the last byte of its address).
3178	 */
3179	if (sc->tulip_chipid == TULIP_21040 /* && sc->tulip_bus != 0 XXX */) {
3180	    for (idx = 0; idx < 32; idx++) {
3181		if (sc->tulip_rombuf[idx] != 0)
3182		    break;
3183	    }
3184	    if (idx == 32) {
3185		int root_unit;
3186		tulip_softc_t *root_sc = NULL;
3187		for (root_unit = sc->tulip_unit - 1; root_unit >= 0; root_unit--) {
3188		    root_sc = TULIP_UNIT_TO_SOFTC(root_unit);
3189		    if (root_sc == NULL || (root_sc->tulip_flags & (TULIP_ROMOK|TULIP_SLAVEDROM)) == TULIP_ROMOK)
3190			break;
3191		    root_sc = NULL;
3192		}
3193		if (root_sc != NULL
3194			/* && root_sc->tulip_bus == sc->tulip_bus XXX */) {
3195		    bcopy(root_sc->tulip_hwaddr, sc->tulip_hwaddr, 6);
3196		    sc->tulip_hwaddr[5] += sc->tulip_unit - root_sc->tulip_unit;
3197		    sc->tulip_flags |= TULIP_SLAVEDROM;
3198		    if (root_sc->tulip_boardsw->bd_type == TULIP_21040_ZX314_MASTER) {
3199			sc->tulip_boardsw = &tulip_21040_zx314_slave_boardsw;
3200			/*
3201			 * Now for a truly disgusting kludge: all 4 21040s on
3202			 * the ZX314 share the same INTA line so the mapping
3203			 * setup by the BIOS on the PCI bridge is worthless.
3204			 * Rather than reprogramming the value in the config
3205			 * register, we will handle this internally.
3206			 */
3207			sc->tulip_slaves = root_sc->tulip_slaves;
3208			root_sc->tulip_slaves = sc;
3209			sc->tulip_flags |= TULIP_SLAVEDINTR;
3210		    }
3211		    return 0;
3212		}
3213	    }
3214	}
3215    }
3216
3217    /*
3218     * This is the standard DEC address ROM test.
3219     */
3220
3221    if (bcmp(&sc->tulip_rombuf[24], testpat, 8) != 0)
3222	return -3;
3223
3224    tmpbuf[0] = sc->tulip_rombuf[15]; tmpbuf[1] = sc->tulip_rombuf[14];
3225    tmpbuf[2] = sc->tulip_rombuf[13]; tmpbuf[3] = sc->tulip_rombuf[12];
3226    tmpbuf[4] = sc->tulip_rombuf[11]; tmpbuf[5] = sc->tulip_rombuf[10];
3227    tmpbuf[6] = sc->tulip_rombuf[9];  tmpbuf[7] = sc->tulip_rombuf[8];
3228    if (bcmp(&sc->tulip_rombuf[0], tmpbuf, 8) != 0)
3229	return -2;
3230
3231    bcopy(sc->tulip_rombuf, sc->tulip_hwaddr, 6);
3232
3233    cksum = *(u_int16_t *) &sc->tulip_hwaddr[0];
3234    cksum *= 2;
3235    if (cksum > 65535) cksum -= 65535;
3236    cksum += *(u_int16_t *) &sc->tulip_hwaddr[2];
3237    if (cksum > 65535) cksum -= 65535;
3238    cksum *= 2;
3239    if (cksum > 65535) cksum -= 65535;
3240    cksum += *(u_int16_t *) &sc->tulip_hwaddr[4];
3241    if (cksum >= 65535) cksum -= 65535;
3242
3243    rom_cksum = *(u_int16_t *) &sc->tulip_rombuf[6];
3244
3245    if (cksum != rom_cksum)
3246	return -1;
3247
3248  check_oui:
3249    /*
3250     * Check for various boards based on OUI.  Did I say braindead?
3251     */
3252   if (sc->tulip_hwaddr[0] == TULIP_OUI_COGENT_0
3253	    && sc->tulip_hwaddr[1] == TULIP_OUI_COGENT_1
3254	    && sc->tulip_hwaddr[2] == TULIP_OUI_COGENT_2) {
3255	if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
3256	    if (sc->tulip_rombuf[32] == TULIP_COGENT_EM100_ID)
3257		sc->tulip_boardsw = &tulip_21140_cogent_em100_boardsw;
3258	}
3259    } else if (sc->tulip_hwaddr[0] == TULIP_OUI_ZNYX_0
3260	    && sc->tulip_hwaddr[1] == TULIP_OUI_ZNYX_1
3261	    && sc->tulip_hwaddr[2] == TULIP_OUI_ZNYX_2) {
3262	if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
3263	    /* this at least works for the zx342 from Znyx */
3264	    sc->tulip_boardsw = &tulip_21140_znyx_zx34x_boardsw;
3265	} else if (sc->tulip_chipid == TULIP_21040
3266	        && (sc->tulip_hwaddr[3] & ~3) == 0xF0
3267	        && (sc->tulip_hwaddr[5] & 3) == 0) {
3268	    sc->tulip_boardsw = &tulip_21040_zx314_master_boardsw;
3269	    sc->tulip_flags |= TULIP_SHAREDINTR;
3270	}
3271    } else if (sc->tulip_hwaddr[0] == TULIP_OUI_SMC_0
3272	       && sc->tulip_hwaddr[1] == TULIP_OUI_SMC_1
3273	       && sc->tulip_hwaddr[2] == TULIP_OUI_SMC_2) {
3274	tulip_identify_smc_nic(sc);
3275    }
3276
3277    if (sc->tulip_boardidbuf[0] != '\0')
3278	sc->tulip_boardid = sc->tulip_boardidbuf;
3279    else
3280	sc->tulip_boardid = sc->tulip_boardsw->bd_description;
3281    sc->tulip_flags |= TULIP_ROMOK;
3282    return 0;
3283}
3284
3285static void
3286tulip_addr_filter(
3287    tulip_softc_t * const sc)
3288{
3289    u_int32_t *sp = sc->tulip_setupdata;
3290    struct ether_multistep step;
3291    struct ether_multi *enm;
3292    int i = 0;
3293
3294    sc->tulip_flags &= ~TULIP_WANTHASH;
3295    sc->tulip_flags |= TULIP_WANTSETUP;
3296    sc->tulip_cmdmode &= ~TULIP_CMD_RXRUN;
3297    sc->tulip_intrmask &= ~TULIP_STS_RXSTOPPED;
3298    if (sc->tulip_ac.ac_multicnt > 14) {
3299	unsigned hash;
3300	/*
3301	 * If we have more than 14 multicasts, we have
3302	 * go into hash perfect mode (512 bit multicast
3303	 * hash and one perfect hardware).
3304	 */
3305
3306	bzero(sc->tulip_setupdata, sizeof(sc->tulip_setupdata));
3307	hash = tulip_mchash(etherbroadcastaddr);
3308	sp[hash >> 4] |= 1 << (hash & 0xF);
3309	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
3310	while (enm != NULL) {
3311	    hash = tulip_mchash(enm->enm_addrlo);
3312	    sp[hash >> 4] |= 1 << (hash & 0xF);
3313	    ETHER_NEXT_MULTI(step, enm);
3314	}
3315	sc->tulip_flags |= TULIP_WANTHASH;
3316	sp[39] = ((u_int16_t *) sc->tulip_ac.ac_enaddr)[0];
3317	sp[40] = ((u_int16_t *) sc->tulip_ac.ac_enaddr)[1];
3318	sp[41] = ((u_int16_t *) sc->tulip_ac.ac_enaddr)[2];
3319    } else {
3320	/*
3321	 * Else can get perfect filtering for 16 addresses.
3322	 */
3323	ETHER_FIRST_MULTI(step, &sc->tulip_ac, enm);
3324	for (; enm != NULL; i++) {
3325	    *sp++ = ((u_int16_t *) enm->enm_addrlo)[0];
3326	    *sp++ = ((u_int16_t *) enm->enm_addrlo)[1];
3327	    *sp++ = ((u_int16_t *) enm->enm_addrlo)[2];
3328	    ETHER_NEXT_MULTI(step, enm);
3329	}
3330	/*
3331	 * Add the broadcast address.
3332	 */
3333	i++;
3334	*sp++ = 0xFFFF;
3335	*sp++ = 0xFFFF;
3336	*sp++ = 0xFFFF;
3337	/*
3338	 * Pad the rest with our hardware address
3339	 */
3340	for (; i < 16; i++) {
3341	    *sp++ = ((u_int16_t *) sc->tulip_ac.ac_enaddr)[0];
3342	    *sp++ = ((u_int16_t *) sc->tulip_ac.ac_enaddr)[1];
3343	    *sp++ = ((u_int16_t *) sc->tulip_ac.ac_enaddr)[2];
3344	}
3345    }
3346}
3347
3348/*
3349 * This routine is entered at splnet() and thereby imposes no problems
3350 * when TULIP_USE_SOFTINTR is defined or not.
3351 */
3352static int
3353tulip_ifioctl(
3354    struct ifnet * const ifp,
3355    ioctl_cmd_t cmd,
3356    caddr_t data)
3357{
3358    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
3359    struct ifreq *ifr = (struct ifreq *) data;
3360    tulip_spl_t s;
3361    int error = 0;
3362
3363#if defined(TULIP_USE_SOFTINTR)
3364    s = splnet();
3365#else
3366    s = splimp();
3367#endif
3368    switch (cmd) {
3369	case SIOCSIFADDR:
3370	case SIOCGIFADDR:
3371	    ether_ioctl(ifp, cmd, data);
3372	    break;
3373
3374	case SIOCSIFFLAGS: {
3375	    /*
3376	     * Changing the connection forces a reset.
3377	     */
3378	    if (sc->tulip_flags & TULIP_ALTPHYS) {
3379		if ((ifp->if_flags & IFF_ALTPHYS) == 0) {
3380		    sc->tulip_flags |= TULIP_NEEDRESET;
3381		}
3382	    } else {
3383		if (ifp->if_flags & IFF_ALTPHYS) {
3384		    sc->tulip_flags |= TULIP_NEEDRESET;
3385		}
3386	    }
3387	    if (sc->tulip_flags & TULIP_NEEDRESET) {
3388		sc->tulip_media = TULIP_MEDIA_UNKNOWN;
3389		sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
3390		sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TXPROBE_OK|TULIP_WANTRXACT);
3391		tulip_reset(sc);
3392	    }
3393	    tulip_init(sc);
3394	    break;
3395	}
3396
3397	case SIOCADDMULTI:
3398	case SIOCDELMULTI: {
3399	    /*
3400	     * Update multicast listeners
3401	     */
3402	    if (cmd == SIOCADDMULTI)
3403		error = ether_addmulti(ifr, &sc->tulip_ac);
3404	    else
3405		error = ether_delmulti(ifr, &sc->tulip_ac);
3406
3407	    if (error == ENETRESET) {
3408		tulip_addr_filter(sc);		/* reset multicast filtering */
3409		tulip_init(sc);
3410		error = 0;
3411	    }
3412	    break;
3413	}
3414#if defined(SIOCSIFMTU)
3415#if !defined(ifr_mtu)
3416#define ifr_mtu ifr_metric
3417#endif
3418	case SIOCSIFMTU:
3419	    /*
3420	     * Set the interface MTU.
3421	     */
3422	    if (ifr->ifr_mtu > ETHERMTU
3423#ifdef BIG_PACKET
3424		    && sc->tulip_chipid != TULIP_21140
3425		    && sc->tulip_chipid != TULIP_21140A
3426		    && sc->tulip_chipid != TULIP_21041
3427#endif
3428		) {
3429		error = EINVAL;
3430		break;
3431	    }
3432	    ifp->if_mtu = ifr->ifr_mtu;
3433#ifdef BIG_PACKET
3434	    tulip_reset(sc);
3435	    tulip_init(sc);
3436#endif
3437	    break;
3438#endif /* SIOCSIFMTU */
3439
3440	default: {
3441	    error = EINVAL;
3442	    break;
3443	}
3444    }
3445
3446    splx(s);
3447    return error;
3448}
3449
3450/*
3451 * This routine gets called at splimp (from ether_output).  This might pose
3452 * a problem for TULIP_USE_SOFTINTR if ether_output is called at splimp
3453 * from another driver.
3454 */
3455static ifnet_ret_t
3456tulip_ifstart(
3457    struct ifnet * const ifp)
3458{
3459    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
3460    struct ifqueue * const ifq = &ifp->if_snd;
3461    tulip_ringinfo_t * const ri = &sc->tulip_txinfo;
3462    struct mbuf *m, *m0, *next_m0;
3463
3464    if ((ifp->if_flags & IFF_RUNNING) == 0
3465	    && (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) == 0)
3466	return;
3467
3468    for (;;) {
3469	tulip_desc_t *eop, *nextout;
3470	int segcnt, free, recopy;
3471	u_int32_t d_status;
3472
3473	if (sc->tulip_flags & TULIP_WANTSETUP) {
3474	    if ((sc->tulip_flags & TULIP_DOINGSETUP) || ri->ri_free == 1) {
3475		ifp->if_flags |= IFF_OACTIVE;
3476		return;
3477	    }
3478	    bcopy(sc->tulip_setupdata, sc->tulip_setupbuf,
3479		   sizeof(sc->tulip_setupbuf));
3480	    sc->tulip_flags &= ~TULIP_WANTSETUP;
3481	    sc->tulip_flags |= TULIP_DOINGSETUP;
3482	    ri->ri_free--;
3483	    ri->ri_nextout->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
3484	    ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG|TULIP_DFLAG_TxLASTSEG
3485		    |TULIP_DFLAG_TxSETUPPKT|TULIP_DFLAG_TxWANTINTR;
3486	    if (sc->tulip_flags & TULIP_WANTHASH)
3487		ri->ri_nextout->d_flag |= TULIP_DFLAG_TxHASHFILT;
3488	    ri->ri_nextout->d_length1 = sizeof(sc->tulip_setupbuf);
3489	    ri->ri_nextout->d_addr1 = TULIP_KVATOPHYS(sc, sc->tulip_setupbuf);
3490	    ri->ri_nextout->d_length2 = 0;
3491	    ri->ri_nextout->d_addr2 = 0;
3492	    ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
3493	    TULIP_CSR_WRITE(sc, csr_txpoll, 1);
3494	    /*
3495	     * Advance the ring for the next transmit packet.
3496	     */
3497	    if (++ri->ri_nextout == ri->ri_last)
3498		ri->ri_nextout = ri->ri_first;
3499	    /*
3500	     * Make sure the next descriptor is owned by us since it
3501	     * may have been set up above if we ran out of room in the
3502	     * ring.
3503	     */
3504	    ri->ri_nextout->d_status = 0;
3505	}
3506
3507	IF_DEQUEUE(ifq, m);
3508	if (m == NULL)
3509	    break;
3510
3511#if defined(TULIP_DEBUG)
3512	if ((sc->tulip_cmdmode & TULIP_CMD_TXRUN) == 0) {
3513	    printf(TULIP_PRINTF_FMT ": ifstart%s: tx not running\n",
3514		   TULIP_PRINTF_ARGS,
3515		   (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) ? "(probe)" : "");
3516	    ifp->if_flags |= IFF_OACTIVE;
3517	    IF_PREPEND(ifq, m);
3518	    return;
3519	}
3520#endif
3521
3522	/*
3523	 * Now we try to fill in our transmit descriptors.  This is
3524	 * a bit reminiscent of going on the Ark two by two
3525	 * since each descriptor for the TULIP can describe
3526	 * two buffers.  So we advance through packet filling
3527	 * each of the two entries at a time to to fill each
3528	 * descriptor.  Clear the first and last segment bits
3529	 * in each descriptor (actually just clear everything
3530	 * but the end-of-ring or chain bits) to make sure
3531	 * we don't get messed up by previously sent packets.
3532	 *
3533	 * We may fail to put the entire packet on the ring if
3534	 * there is either not enough ring entries free or if the
3535	 * packet has more than MAX_TXSEG segments.  In the former
3536	 * case we will just wait for the ring to empty.  In the
3537	 * latter case we have to recopy.
3538	 */
3539	d_status = 0;
3540	recopy = 0;
3541	eop = nextout = ri->ri_nextout;
3542	m0 = m;
3543	segcnt = 0;
3544	free = ri->ri_free;
3545	do {
3546	    int len = m0->m_len;
3547	    caddr_t addr = mtod(m0, caddr_t);
3548	    unsigned clsize = CLBYTES - (((u_long) addr) & (CLBYTES-1));
3549
3550	    next_m0 = m0->m_next;
3551	    while (len > 0) {
3552		unsigned slen = min(len, clsize);
3553#ifdef BIG_PACKET
3554		int partial = 0;
3555		if (slen >= 2048)
3556		    slen = 2040, partial = 1;
3557#endif
3558		segcnt++;
3559		if (segcnt > TULIP_MAX_TXSEG) {
3560		    recopy = 1;
3561		    next_m0 = NULL; /* to break out of outside loop */
3562		    break;
3563		}
3564		if (segcnt & 1) {
3565		    if (--free == 0) {
3566			/*
3567			 * There's no more room but since nothing
3568			 * has been committed at this point, just
3569			 * show output is active, put back the
3570			 * mbuf and return.
3571			 */
3572			ifp->if_flags |= IFF_OACTIVE;
3573			IF_PREPEND(ifq, m);
3574			return;
3575		    }
3576		    eop = nextout;
3577		    if (++nextout == ri->ri_last)
3578			nextout = ri->ri_first;
3579		    eop->d_flag &= TULIP_DFLAG_ENDRING|TULIP_DFLAG_CHAIN;
3580		    eop->d_status = d_status;
3581		    eop->d_addr1 = TULIP_KVATOPHYS(sc, addr);
3582		    eop->d_length1 = slen;
3583		} else {
3584		    /*
3585		     *  Fill in second half of descriptor
3586		     */
3587		    eop->d_addr2 = TULIP_KVATOPHYS(sc, addr);
3588		    eop->d_length2 = slen;
3589		}
3590		d_status = TULIP_DSTS_OWNER;
3591		len -= slen;
3592		addr += slen;
3593#ifdef BIG_PACKET
3594		if (partial)
3595		    continue;
3596#endif
3597		clsize = CLBYTES;
3598	    }
3599	} while ((m0 = next_m0) != NULL);
3600
3601	/*
3602	 * The packet exceeds the number of transmit buffer
3603	 * entries that we can use for one packet, so we have
3604	 * recopy it into one mbuf and then try again.
3605	 */
3606	if (recopy) {
3607	    MGETHDR(m0, M_DONTWAIT, MT_DATA);
3608	    if (m0 != NULL) {
3609		if (m->m_pkthdr.len > MHLEN) {
3610		    MCLGET(m0, M_DONTWAIT);
3611		    if ((m0->m_flags & M_EXT) == 0) {
3612			m_freem(m);
3613			m_freem(m0);
3614			continue;
3615		    }
3616		}
3617		m_copydata(m, 0, m->m_pkthdr.len, mtod(m0, caddr_t));
3618		m0->m_pkthdr.len = m0->m_len = m->m_pkthdr.len;
3619		IF_PREPEND(ifq, m0);
3620	    }
3621	    m_freem(m);
3622	    continue;
3623	}
3624
3625	/*
3626	 * The descriptors have been filled in.  Now get ready
3627	 * to transmit.
3628	 */
3629#if NBPFILTER > 0
3630	if (sc->tulip_bpf != NULL)
3631	    TULIP_BPF_MTAP(sc, m);
3632#endif
3633	IF_ENQUEUE(&sc->tulip_txq, m);
3634
3635	/*
3636	 * Make sure the next descriptor after this packet is owned
3637	 * by us since it may have been set up above if we ran out
3638	 * of room in the ring.
3639	 */
3640	nextout->d_status = 0;
3641
3642	/*
3643	 * If we only used the first segment of the last descriptor,
3644	 * make sure the second segment will not be used.
3645	 */
3646	if (segcnt & 1) {
3647	    eop->d_addr2 = 0;
3648	    eop->d_length2 = 0;
3649	}
3650
3651	/*
3652	 * Mark the last and first segments, indicate we want a transmit
3653	 * complete interrupt, give the descriptors to the TULIP, and tell
3654	 * it to transmit!
3655	 */
3656	eop->d_flag |= TULIP_DFLAG_TxLASTSEG|TULIP_DFLAG_TxWANTINTR;
3657
3658	/*
3659	 * Note that ri->ri_nextout is still the start of the packet
3660	 * and until we set the OWNER bit, we can still back out of
3661	 * everything we have done.
3662	 */
3663	ri->ri_nextout->d_flag |= TULIP_DFLAG_TxFIRSTSEG;
3664	ri->ri_nextout->d_status = TULIP_DSTS_OWNER;
3665
3666	/*
3667	 * This advances the ring for us.
3668	 */
3669	ri->ri_nextout = nextout;
3670	ri->ri_free = free;
3671
3672	TULIP_CSR_WRITE(sc, csr_txpoll, 1);
3673
3674	if (sc->tulip_flags & TULIP_TXPROBE_ACTIVE) {
3675	    ifp->if_flags |= IFF_OACTIVE;
3676	    return;
3677	}
3678	if (sc->tulip_txtimer == 0)
3679	    sc->tulip_txtimer = TULIP_TXTIMER;
3680    }
3681    if (m != NULL) {
3682	ifp->if_flags |= IFF_OACTIVE;
3683	IF_PREPEND(ifq, m);
3684    }
3685}
3686
3687/*
3688 * Even though this routine runs at splimp, it does not break
3689 * our use of splnet (splsoftnet under NetBSD) for the majority
3690 * of this driver (if TULIP_USE_SOFTINTR defined) since
3691 * if_watcbog is called from if_watchdog which is called from
3692 * splsoftclock which is below splnet.
3693 */
3694static void
3695tulip_ifwatchdog(
3696    struct ifnet *ifp)
3697{
3698    tulip_softc_t * const sc = TULIP_IFP_TO_SOFTC(ifp);
3699
3700#if defined(TULIP_DEBUG)
3701    u_int32_t rxintrs = sc->tulip_dbg.dbg_rxintrs - sc->tulip_dbg.dbg_last_rxintrs;
3702    if (rxintrs > sc->tulip_dbg.dbg_high_rxintrs_hz)
3703	sc->tulip_dbg.dbg_high_rxintrs_hz = rxintrs;
3704    sc->tulip_dbg.dbg_last_rxintrs = sc->tulip_dbg.dbg_rxintrs;
3705    sc->tulip_dbg.dbg_gpintrs_hz = sc->tulip_dbg.dbg_gpintrs;
3706    sc->tulip_dbg.dbg_gpintrs = 0;
3707#endif /* TULIP_DEBUG */
3708
3709    sc->tulip_if.if_timer = 1;
3710    /*
3711     * These should be rare so do a bulk test up front so we can just skip
3712     * them if needed.
3713     */
3714    if (sc->tulip_flags & (TULIP_SYSTEMERROR|TULIP_RXBUFSLOW|TULIP_FAKEGPTIMEOUT|TULIP_NOMESSAGES)) {
3715	/*
3716	 * This for those devices that need to autosense.  Interrupts are not
3717	 * allowed during device probe so we fake one here to start the
3718	 * autosense.  Do this before the others since it can effect their
3719	 * state.
3720	 */
3721	if (sc->tulip_flags & TULIP_FAKEGPTIMEOUT)
3722	    (*sc->tulip_boardsw->bd_media_select)(sc);
3723
3724	/*
3725	 * If the number of receive buffer is low, try to refill
3726	 */
3727	if (sc->tulip_flags & TULIP_RXBUFSLOW)
3728	    tulip_rx_intr(sc);
3729
3730	if (sc->tulip_flags & TULIP_SYSTEMERROR) {
3731	    printf(TULIP_PRINTF_FMT ": %d system errors: last was %s\n",
3732		   TULIP_PRINTF_ARGS, sc->tulip_system_errors,
3733		   tulip_system_errors[sc->tulip_last_system_error]);
3734	}
3735	if (sc->tulip_statusbits) {
3736	    tulip_print_abnormal_interrupt(sc, sc->tulip_statusbits);
3737	    sc->tulip_statusbits = 0;
3738	}
3739
3740	sc->tulip_flags &= ~(TULIP_NOMESSAGES|TULIP_SYSTEMERROR);
3741    }
3742
3743    if (sc->tulip_txtimer && --sc->tulip_txtimer == 0) {
3744	printf(TULIP_PRINTF_FMT ": transmission timeout\n", TULIP_PRINTF_ARGS);
3745	sc->tulip_media = TULIP_MEDIA_UNKNOWN;
3746	sc->tulip_probe_state = TULIP_PROBE_INACTIVE;
3747	sc->tulip_flags &= ~(TULIP_TXPROBE_ACTIVE|TULIP_TXPROBE_OK|TULIP_WANTRXACT|TULIP_LINKUP|TULIP_LINKSUSPECT);
3748	tulip_reset(sc);
3749	tulip_init(sc);
3750    }
3751}
3752#if defined(__bsdi__) || (defined(__FreeBSD__) && BSD < 199506)
3753static ifnet_ret_t
3754tulip_ifwatchdog_wrapper(
3755    int unit)
3756{
3757    tulip_ifwatchdog(&TULIP_UNIT_TO_SOFTC(unit)->tulip_if);
3758}
3759#define	tulip_ifwatchdog	tulip_ifwatchdog_wrapper
3760#endif
3761
3762/*
3763 * All printf's are real as of now!
3764 */
3765#ifdef printf
3766#undef printf
3767#endif
3768#if !defined(IFF_NOTRAILERS)
3769#define IFF_NOTRAILERS		0
3770#endif
3771
3772static void
3773tulip_attach(
3774    tulip_softc_t * const sc)
3775{
3776    struct ifnet * const ifp = &sc->tulip_if;
3777
3778    ifp->if_flags = IFF_BROADCAST|IFF_SIMPLEX|IFF_NOTRAILERS|IFF_MULTICAST;
3779    ifp->if_ioctl = tulip_ifioctl;
3780    ifp->if_start = tulip_ifstart;
3781    ifp->if_watchdog = tulip_ifwatchdog;
3782    ifp->if_init = (if_init_f_t*)tulip_init;
3783    ifp->if_timer = 1;
3784#if !defined(__bsdi__) || _BSDI_VERSION < 199401
3785    ifp->if_output = ether_output;
3786#endif
3787#if defined(__bsdi__) && _BSDI_VERSION < 199401
3788    ifp->if_mtu = ETHERMTU;
3789#endif
3790
3791#if defined(__bsdi__) && _BSDI_VERSION >= 199510
3792    aprint_naive(": DEC Ethernet");
3793    aprint_normal(": %s%s", sc->tulip_boardid,
3794        tulip_chipdescs[sc->tulip_chipid]);
3795    aprint_verbose(" pass %d.%d", (sc->tulip_revinfo & 0xF0) >> 4,
3796        sc->tulip_revinfo & 0x0F);
3797    printf("\n");
3798    sc->tulip_pf = aprint_normal;
3799    aprint_normal(TULIP_PRINTF_FMT ": address " TULIP_EADDR_FMT "\n",
3800		  TULIP_PRINTF_ARGS,
3801		  TULIP_EADDR_ARGS(sc->tulip_hwaddr));
3802#else
3803    printf(
3804#if defined(__bsdi__)
3805	   "\n"
3806#endif
3807	   TULIP_PRINTF_FMT ": %s%s pass %d.%d\n",
3808	   TULIP_PRINTF_ARGS,
3809	   sc->tulip_boardid,
3810	   tulip_chipdescs[sc->tulip_chipid],
3811	   (sc->tulip_revinfo & 0xF0) >> 4,
3812	   sc->tulip_revinfo & 0x0F);
3813    printf(TULIP_PRINTF_FMT ": address " TULIP_EADDR_FMT "\n",
3814	   TULIP_PRINTF_ARGS,
3815	   TULIP_EADDR_ARGS(sc->tulip_hwaddr));
3816#endif
3817
3818    sc->tulip_dot3stats.dot3Compliance = DOT3COMPLIANCE_STATS;
3819    sc->tulip_dot3stats.dot3StatsEtherChipSet =
3820	    tulip_chip2mib[sc->tulip_chipid];
3821
3822    if (sc->tulip_boardsw->bd_mii_probe != NULL)
3823	(*sc->tulip_boardsw->bd_mii_probe)(sc);
3824
3825    if ((*sc->tulip_boardsw->bd_media_probe)(sc)) {
3826	ifp->if_flags |= IFF_ALTPHYS;
3827    } else {
3828	sc->tulip_flags |= TULIP_ALTPHYS;
3829    }
3830
3831    sc->tulip_flags |= TULIP_DEVICEPROBE;
3832    tulip_reset(sc);
3833    sc->tulip_flags &= ~TULIP_DEVICEPROBE;
3834
3835    ifp->if_linkmib = &sc->tulip_dot3stats;
3836    ifp->if_linkmiblen = sizeof sc->tulip_dot3stats;
3837
3838#if defined(__bsdi__) && _BSDI_VERSION >= 199510
3839    sc->tulip_pf = printf;
3840    ether_attach(ifp);
3841#else
3842    if_attach(ifp);
3843#if defined(__NetBSD__) || (defined(__FreeBSD__) && BSD >= 199506)
3844    ether_ifattach(ifp);
3845#endif
3846#endif /* __bsdi__ */
3847
3848#if NBPFILTER > 0
3849    TULIP_BPF_ATTACH(sc);
3850#endif
3851}
3852
3853static void
3854tulip_initcsrs(
3855    tulip_softc_t * const sc,
3856    tulip_csrptr_t csr_base,
3857    size_t csr_size)
3858{
3859    sc->tulip_csrs.csr_busmode		= csr_base +  0 * csr_size;
3860    sc->tulip_csrs.csr_txpoll		= csr_base +  1 * csr_size;
3861    sc->tulip_csrs.csr_rxpoll		= csr_base +  2 * csr_size;
3862    sc->tulip_csrs.csr_rxlist		= csr_base +  3 * csr_size;
3863    sc->tulip_csrs.csr_txlist		= csr_base +  4 * csr_size;
3864    sc->tulip_csrs.csr_status		= csr_base +  5 * csr_size;
3865    sc->tulip_csrs.csr_command		= csr_base +  6 * csr_size;
3866    sc->tulip_csrs.csr_intr		= csr_base +  7 * csr_size;
3867    sc->tulip_csrs.csr_missed_frames	= csr_base +  8 * csr_size;
3868    if (sc->tulip_chipid == TULIP_21040) {
3869	sc->tulip_csrs.csr_enetrom		= csr_base +  9 * csr_size;
3870	sc->tulip_csrs.csr_reserved		= csr_base + 10 * csr_size;
3871	sc->tulip_csrs.csr_full_duplex		= csr_base + 11 * csr_size;
3872	sc->tulip_csrs.csr_sia_status		= csr_base + 12 * csr_size;
3873	sc->tulip_csrs.csr_sia_connectivity	= csr_base + 13 * csr_size;
3874	sc->tulip_csrs.csr_sia_tx_rx 		= csr_base + 14 * csr_size;
3875	sc->tulip_csrs.csr_sia_general		= csr_base + 15 * csr_size;
3876#if defined(TULIP_EISA)
3877    } else if (sc->tulip_chipid == TULIP_DE425) {
3878	sc->tulip_csrs.csr_enetrom		= csr_base + DE425_ENETROM_OFFSET;
3879	sc->tulip_csrs.csr_reserved		= csr_base + 10 * csr_size;
3880	sc->tulip_csrs.csr_full_duplex		= csr_base + 11 * csr_size;
3881	sc->tulip_csrs.csr_sia_status		= csr_base + 12 * csr_size;
3882	sc->tulip_csrs.csr_sia_connectivity	= csr_base + 13 * csr_size;
3883	sc->tulip_csrs.csr_sia_tx_rx 		= csr_base + 14 * csr_size;
3884	sc->tulip_csrs.csr_sia_general		= csr_base + 15 * csr_size;
3885#endif /* TULIP_EISA */
3886    } else if (sc->tulip_chipid == TULIP_21140 || sc->tulip_chipid == TULIP_21140A) {
3887	sc->tulip_csrs.csr_srom_mii		= csr_base +  9 * csr_size;
3888	sc->tulip_csrs.csr_gp_timer		= csr_base + 11 * csr_size;
3889	sc->tulip_csrs.csr_gp			= csr_base + 12 * csr_size;
3890	sc->tulip_csrs.csr_watchdog		= csr_base + 15 * csr_size;
3891    } else if (sc->tulip_chipid == TULIP_21041) {
3892	sc->tulip_csrs.csr_srom_mii		= csr_base +  9 * csr_size;
3893	sc->tulip_csrs.csr_bootrom		= csr_base + 10 * csr_size;
3894	sc->tulip_csrs.csr_gp_timer		= csr_base + 11 * csr_size;
3895	sc->tulip_csrs.csr_sia_status		= csr_base + 12 * csr_size;
3896	sc->tulip_csrs.csr_sia_connectivity	= csr_base + 13 * csr_size;
3897	sc->tulip_csrs.csr_sia_tx_rx 		= csr_base + 14 * csr_size;
3898	sc->tulip_csrs.csr_sia_general		= csr_base + 15 * csr_size;
3899    }
3900}
3901
3902static void
3903tulip_initring(
3904    tulip_softc_t * const sc,
3905    tulip_ringinfo_t * const ri,
3906    tulip_desc_t *descs,
3907    int ndescs)
3908{
3909    ri->ri_max = ndescs;
3910    ri->ri_first = descs;
3911    ri->ri_last = ri->ri_first + ri->ri_max;
3912    bzero((caddr_t) ri->ri_first, sizeof(ri->ri_first[0]) * ri->ri_max);
3913    ri->ri_last[-1].d_flag = TULIP_DFLAG_ENDRING;
3914}
3915
3916/*
3917 * This is the PCI configuration support.  Since the 21040 is available
3918 * on both EISA and PCI boards, one must be careful in how defines the
3919 * 21040 in the config file.
3920 */
3921
3922#define	PCI_CFID	0x00	/* Configuration ID */
3923#define	PCI_CFCS	0x04	/* Configurtion Command/Status */
3924#define	PCI_CFRV	0x08	/* Configuration Revision */
3925#define	PCI_CFLT	0x0c	/* Configuration Latency Timer */
3926#define	PCI_CBIO	0x10	/* Configuration Base IO Address */
3927#define	PCI_CBMA	0x14	/* Configuration Base Memory Address */
3928#define	PCI_CFIT	0x3c	/* Configuration Interrupt */
3929#define	PCI_CFDA	0x40	/* Configuration Driver Area */
3930
3931#if defined(TULIP_EISA)
3932static const int tulip_eisa_irqs[4] = { IRQ5, IRQ9, IRQ10, IRQ11 };
3933#endif
3934
3935#if defined(__FreeBSD__)
3936
3937#define	TULIP_PCI_ATTACH_ARGS	pcici_t config_id, int unit
3938
3939static void
3940tulip_shutdown(
3941    int howto,
3942    void *sc)
3943{
3944    TULIP_CSR_WRITE((tulip_softc_t *) sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3945    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
3946		   33MHz that comes to two microseconds but wait a
3947		   bit longer anyways) */
3948}
3949
3950static char*
3951tulip_pci_probe(
3952    pcici_t config_id,
3953    pcidi_t device_id)
3954{
3955    if (PCI_VENDORID(device_id) != DEC_VENDORID)
3956	return NULL;
3957    if (PCI_CHIPID(device_id) == CHIPID_21040)
3958	return "Digital 21040 Ethernet";
3959    if (PCI_CHIPID(device_id) == CHIPID_21041)
3960	return "Digital 21041 Ethernet";
3961    if (PCI_CHIPID(device_id) == CHIPID_21140) {
3962	u_int32_t revinfo = pci_conf_read(config_id, PCI_CFRV) & 0xFF;
3963	if (revinfo >= 0x20)
3964	    return "Digital 21140A Fast Ethernet";
3965	else
3966	    return "Digital 21140 Fast Ethernet";
3967
3968    }
3969    return NULL;
3970}
3971
3972static void  tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
3973static u_long tulip_pci_count;
3974
3975struct pci_device dedevice = {
3976    "de",
3977    tulip_pci_probe,
3978    tulip_pci_attach,
3979   &tulip_pci_count,
3980    NULL
3981};
3982
3983DATA_SET (pcidevice_set, dedevice);
3984#endif /* __FreeBSD__ */
3985
3986#if defined(__bsdi__)
3987#define	TULIP_PCI_ATTACH_ARGS	struct device * const parent, struct device * const self, void * const aux
3988
3989static void
3990tulip_shutdown(
3991    void *arg)
3992{
3993    tulip_softc_t * const sc = (tulip_softc_t *) arg;
3994    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
3995    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
3996			   33MHz that comes to two microseconds but wait a
3997			   bit longer anyways) */
3998}
3999
4000static int
4001tulip_pci_match(
4002    pci_devaddr_t *pa)
4003{
4004    int irq;
4005    unsigned id;
4006
4007    id = pci_inl(pa, PCI_VENDOR_ID);
4008    if (PCI_VENDORID(id) != DEC_VENDORID)
4009	return 0;
4010    id = PCI_CHIPID(id);
4011    if (id != CHIPID_21040 && id != CHIPID_21041 && id != CHIPID_21140)
4012	return 0;
4013    irq = pci_inl(pa, PCI_I_LINE) & 0xFF;
4014    if (irq == 0 || irq >= 16) {
4015	printf("de?: invalid IRQ %d; skipping\n", irq);
4016	return 0;
4017    }
4018    return 1;
4019}
4020
4021static int
4022tulip_probe(
4023    struct device *parent,
4024    struct cfdata *cf,
4025    void *aux)
4026{
4027    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
4028    unsigned irq, slot;
4029    pci_devaddr_t *pa;
4030
4031#if _BSDI_VERSION >= 199401
4032    switch (ia->ia_bustype) {
4033    case BUS_PCI:
4034#endif
4035	pa = pci_scan(tulip_pci_match);
4036	if (pa == NULL)
4037	    return 0;
4038
4039	irq = (1 << (pci_inl(pa, PCI_I_LINE) & 0xFF));
4040
4041	/* Get the base address; assume the BIOS set it up correctly */
4042#if defined(TULIP_IOMAPPED)
4043	ia->ia_maddr = NULL;
4044	ia->ia_msize = 0;
4045	ia->ia_iobase = pci_inl(pa, PCI_CBIO) & ~7;
4046	pci_outl(pa, PCI_CBIO, 0xFFFFFFFF);
4047	ia->ia_iosize = ((~pci_inl(pa, PCI_CBIO)) | 7) + 1;
4048	pci_outl(pa, PCI_CBIO, (int) ia->ia_iobase);
4049
4050	/* Disable memory space access */
4051	pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~2);
4052#else
4053	ia->ia_maddr = (caddr_t) (pci_inl(pa, PCI_CBMA) & ~7);
4054	pci_outl(pa, PCI_CBMA, 0xFFFFFFFF);
4055	ia->ia_msize = ((~pci_inl(pa, PCI_CBMA)) | 7) + 1;
4056	pci_outl(pa, PCI_CBMA, (int) ia->ia_maddr);
4057	ia->ia_iobase = 0;
4058	ia->ia_iosize = 0;
4059
4060	/* Disable I/O space access */
4061	pci_outl(pa, PCI_COMMAND, pci_inl(pa, PCI_COMMAND) & ~1);
4062#endif /* TULIP_IOMAPPED */
4063
4064	ia->ia_aux = (void *) pa;
4065#if _BSDI_VERSION >= 199401
4066	break;
4067
4068#if defined(TULIP_EISA)
4069    case BUS_EISA: {
4070	unsigned tmp;
4071
4072	if ((slot = eisa_match(cf, ia)) == 0)
4073	    return 0;
4074	ia->ia_iobase = slot << 12;
4075	ia->ia_iosize = EISA_NPORT;
4076	eisa_slotalloc(slot);
4077	tmp = inb(ia->ia_iobase + DE425_CFG0);
4078	irq = tulip_eisa_irqs[(tmp >> 1) & 0x03];
4079	/*
4080	 * Until BSD/OS likes level interrupts, force
4081	 * the DE425 into edge-triggered mode.
4082	 */
4083	if ((tmp & 1) == 0)
4084	    outb(ia->ia_iobase + DE425_CFG0, tmp | 1);
4085	/*
4086	 * CBIO needs to map to the EISA slot
4087	 * enable I/O access and Master
4088	 */
4089	outl(ia->ia_iobase + DE425_CBIO, ia->ia_iobase);
4090	outl(ia->ia_iobase + DE425_CFCS, 5 | inl(ia->ia_iobase + DE425_CFCS));
4091	ia->ia_aux = NULL;
4092	break;
4093    }
4094#endif /* TULIP_EISA */
4095    default:
4096	return 0;
4097    }
4098#endif
4099
4100    /* PCI bus masters don't use host DMA channels */
4101    ia->ia_drq = DRQNONE;
4102
4103    if (ia->ia_irq != IRQUNK && irq != ia->ia_irq) {
4104	printf("de%d: error: desired IRQ of %d does not match device's "
4105	    "actual IRQ of %d,\n",
4106	       cf->cf_unit,
4107	       ffs(ia->ia_irq) - 1, ffs(irq) - 1);
4108	return 0;
4109    }
4110    if (ia->ia_irq == IRQUNK)
4111	ia->ia_irq = irq;
4112#ifdef IRQSHARE
4113    ia->ia_irq |= IRQSHARE;
4114#endif
4115    return 1;
4116}
4117
4118static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
4119
4120#if defined(TULIP_EISA)
4121static char *tulip_eisa_ids[] = {
4122    "DEC4250",
4123    NULL
4124};
4125#endif
4126
4127struct cfdriver decd = {
4128    0, "de", tulip_probe, tulip_pci_attach,
4129#if _BSDI_VERSION >= 199401
4130    DV_IFNET,
4131#endif
4132    sizeof(tulip_softc_t),
4133#if defined(TULIP_EISA)
4134    tulip_eisa_ids
4135#endif
4136};
4137
4138#endif /* __bsdi__ */
4139
4140#if defined(__NetBSD__)
4141#define	TULIP_PCI_ATTACH_ARGS	struct device * const parent, struct device * const self, void * const aux
4142
4143static void
4144tulip_pci_shutdown(
4145    void *arg)
4146{
4147    tulip_softc_t * const sc = (tulip_softc_t *) arg;
4148    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
4149    DELAY(10);	/* Wait 10 microseconds (actually 50 PCI cycles but at
4150			   33MHz that comes to two microseconds but wait a
4151			   bit longer anyways) */
4152}
4153
4154static int
4155tulip_pci_probe(
4156    struct device *parent,
4157    void *match,
4158    void *aux)
4159{
4160    struct pci_attach_args *pa = (struct pci_attach_args *) aux;
4161
4162    if (PCI_VENDORID(pa->pa_id) != DEC_VENDORID)
4163	return 0;
4164    if (PCI_CHIPID(pa->pa_id) == CHIPID_21040
4165	    || PCI_CHIPID(pa->pa_id) == CHIPID_21041
4166	    || PCI_CHIPID(pa->pa_id) == CHIPID_21140)
4167	return 1;
4168
4169    return 0;
4170}
4171
4172static void tulip_pci_attach(TULIP_PCI_ATTACH_ARGS);
4173
4174struct cfattach de_ca = {
4175    sizeof(tulip_softc_t), tulip_pci_probe, tulip_pci_attach
4176};
4177
4178struct cfdriver de_cd = {
4179    0, "de", DV_IFNET
4180};
4181
4182#endif /* __NetBSD__ */
4183
4184static void
4185tulip_pci_attach(
4186    TULIP_PCI_ATTACH_ARGS)
4187{
4188#if defined(__FreeBSD__)
4189    tulip_softc_t *sc;
4190#define	PCI_CONF_WRITE(r, v)	pci_conf_write(config_id, (r), (v))
4191#define	PCI_CONF_READ(r)	pci_conf_read(config_id, (r))
4192#endif
4193#if defined(__bsdi__)
4194    tulip_softc_t * const sc = (tulip_softc_t *) self;
4195    struct isa_attach_args * const ia = (struct isa_attach_args *) aux;
4196    pci_devaddr_t *pa = (pci_devaddr_t *) ia->ia_aux;
4197    const int unit = sc->tulip_dev.dv_unit;
4198#define	PCI_CONF_WRITE(r, v)	pci_outl(pa, (r), (v))
4199#define	PCI_CONF_READ(r)	pci_inl(pa, (r))
4200#endif
4201#if defined(__NetBSD__)
4202    tulip_softc_t * const sc = (tulip_softc_t *) self;
4203    struct pci_attach_args * const pa = (struct pci_attach_args *) aux;
4204    const int unit = sc->tulip_dev.dv_unit;
4205#if defined(TULIP_IOMAPPED)
4206    bus_io_addr_t iobase;
4207    bus_io_size_t iosize;
4208#else
4209    bus_mem_addr_t membase;
4210    bus_mem_size_t memsize;
4211#endif
4212#define	PCI_CONF_WRITE(r, v)	pci_conf_write(pa->pa_pc, pa->pa_tag, (r), (v))
4213#define	PCI_CONF_READ(r)	pci_conf_read(pa->pa_pc, pa->pa_tag, (r))
4214#endif /* __NetBSD__ */
4215    int retval, idx;
4216    u_int32_t revinfo, cfdainfo, id;
4217#if !defined(TULIP_IOMAPPED) && defined(__FreeBSD__)
4218    vm_offset_t pa_csrs;
4219#endif
4220    unsigned csroffset = TULIP_PCI_CSROFFSET;
4221    unsigned csrsize = TULIP_PCI_CSRSIZE;
4222    tulip_csrptr_t csr_base;
4223    tulip_chipid_t chipid = TULIP_CHIPID_UNKNOWN;
4224
4225    if (unit >= TULIP_MAX_DEVICES) {
4226#ifdef __FreeBSD__
4227	printf("de%d", unit);
4228#endif
4229	printf(": not configured; limit of %d reached or exceeded\n",
4230	       TULIP_MAX_DEVICES);
4231	return;
4232    }
4233
4234#if defined(__bsdi__)
4235    if (pa != NULL) {
4236	revinfo = pci_inl(pa, PCI_CFRV) & 0xFF;
4237	id = pci_inl(pa, PCI_CFID);
4238	cfdainfo = pci_inl(pa, PCI_CFDA);
4239#if defined(TULIP_EISA)
4240    } else {
4241	revinfo = inl(ia->ia_iobase + DE425_CFRV) & 0xFF;
4242	csroffset = TULIP_EISA_CSROFFSET;
4243	csrsize = TULIP_EISA_CSRSIZE;
4244	chipid = TULIP_DE425;
4245	cfdainfo = 0;
4246#endif
4247    }
4248#else /* __bsdi__ */
4249    revinfo  = PCI_CONF_READ(PCI_CFRV) & 0xFF;
4250    id       = PCI_CONF_READ(PCI_CFID);
4251    cfdainfo = PCI_CONF_READ(PCI_CFDA);
4252#endif
4253
4254    if (PCI_VENDORID(id) == DEC_VENDORID) {
4255	if (PCI_CHIPID(id) == CHIPID_21040) chipid = TULIP_21040;
4256	else if (PCI_CHIPID(id) == CHIPID_21140) {
4257	    chipid = (revinfo >= 0x20) ? TULIP_21140A : TULIP_21140;
4258	}
4259	else if (PCI_CHIPID(id) == CHIPID_21041) chipid = TULIP_21041;
4260    }
4261    if (chipid == TULIP_CHIPID_UNKNOWN)
4262	return;
4263
4264    if ((chipid == TULIP_21040 || chipid == TULIP_DE425) && revinfo < 0x20) {
4265#ifdef __FreeBSD__
4266	printf("de%d", unit);
4267#endif
4268	printf(": not configured; 21040 pass 2.0 required (%d.%d found)\n",
4269	       revinfo >> 4, revinfo & 0x0f);
4270	return;
4271    } else if (chipid == TULIP_21140 && revinfo < 0x11) {
4272#ifndef __FreeBSD__
4273	printf("\n");
4274#endif
4275	printf("de%d: not configured; 21140 pass 1.1 required (%d.%d found)\n",
4276	       unit, revinfo >> 4, revinfo & 0x0f);
4277	return;
4278    }
4279
4280    if ((chipid == TULIP_21041 || chipid == TULIP_21140A)
4281	&& (cfdainfo & (TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE))) {
4282	cfdainfo &= ~(TULIP_CFDA_SLEEP|TULIP_CFDA_SNOOZE);
4283	PCI_CONF_WRITE(PCI_CFDA, cfdainfo);
4284	printf("de%d: waking device from sleep/snooze mode\n", unit);
4285	DELAY(11*1000);
4286    }
4287
4288
4289#if defined(__FreeBSD__)
4290    sc = (tulip_softc_t *) malloc(sizeof(*sc), M_DEVBUF, M_NOWAIT);
4291    if (sc == NULL)
4292	return;
4293    bzero(sc, sizeof(*sc));				/* Zero out the softc*/
4294#endif
4295
4296    sc->tulip_chipid = chipid;
4297#if defined(__NetBSD__)
4298    bcopy(self->dv_xname, sc->tulip_if.if_xname, IFNAMSIZ);
4299    sc->tulip_if.if_softc = sc;
4300    sc->tulip_bc = pa->pa_bc;
4301    sc->tulip_pc = pa->pa_pc;
4302#else
4303    sc->tulip_unit = unit;
4304    sc->tulip_name = "de";
4305#endif
4306    sc->tulip_revinfo = revinfo;
4307#if defined(__FreeBSD__)
4308#if BSD >= 199506
4309    sc->tulip_if.if_softc = sc;
4310#endif
4311#if defined(TULIP_IOMAPPED)
4312    retval = pci_map_port(config_id, PCI_CBIO, &csr_base);
4313#else
4314    retval = pci_map_mem(config_id, PCI_CBMA, (vm_offset_t *) &csr_base, &pa_csrs);
4315#endif
4316    if (!retval) {
4317	free((caddr_t) sc, M_DEVBUF);
4318	return;
4319    }
4320    tulips[unit] = sc;
4321#endif /* __FreeBSD__ */
4322
4323#if defined(__bsdi__)
4324#if defined(TULIP_IOMAPPED)
4325    csr_base = ia->ia_iobase;
4326#else
4327    csr_base = (vm_offset_t) mapphys((vm_offset_t) ia->ia_maddr, ia->ia_msize);
4328#endif
4329#endif /* __bsdi__ */
4330
4331#if defined(__NetBSD__)
4332    csr_base = 0;
4333#if defined(TULIP_IOMAPPED)
4334    if (pci_io_find(pa->pa_pc, pa->pa_tag, PCI_CBIO, &iobase, &iosize)
4335	|| bus_io_map(pa->pa_bc, iobase, iosize, &sc->tulip_ioh))
4336	return;
4337#else
4338    if (pci_mem_find(pa->pa_pc, pa->pa_tag, PCI_CBMA, &membase, &memsize, NULL)
4339	|| bus_mem_map(pa->pa_bc, membase, memsize, 0, &sc->tulip_memh))
4340	return;
4341#endif
4342#endif /* __NetBSD__ */
4343
4344    tulip_initcsrs(sc, csr_base + csroffset, csrsize);
4345    tulip_initring(sc, &sc->tulip_rxinfo, sc->tulip_rxdescs, TULIP_RXDESCS);
4346    tulip_initring(sc, &sc->tulip_txinfo, sc->tulip_txdescs, TULIP_TXDESCS);
4347
4348    /*
4349     * Make sure there won't be any interrupts or such...
4350     */
4351    TULIP_CSR_WRITE(sc, csr_busmode, TULIP_BUSMODE_SWRESET);
4352    DELAY(100);	/* Wait 10 microseconds (actually 50 PCI cycles but at
4353		   33MHz that comes to two microseconds but wait a
4354		   bit longer anyways) */
4355
4356    if ((retval = tulip_read_macaddr(sc)) < 0) {
4357#ifdef __FreeBSD__
4358	printf(TULIP_PRINTF_FMT, TULIP_PRINTF_ARGS);
4359#endif
4360	printf(": can't read ENET ROM (why=%d) (", retval);
4361	for (idx = 0; idx < 32; idx++)
4362	    printf("%02x", sc->tulip_rombuf[idx]);
4363	printf("\n");
4364	printf(TULIP_PRINTF_FMT ": %s%s pass %d.%d\n",
4365	       TULIP_PRINTF_ARGS,
4366	       (sc->tulip_boardid != NULL ? sc->tulip_boardid : ""),
4367	       tulip_chipdescs[sc->tulip_chipid],
4368	       (sc->tulip_revinfo & 0xF0) >> 4, sc->tulip_revinfo & 0x0F);
4369	printf(TULIP_PRINTF_FMT ": address unknown\n", TULIP_PRINTF_ARGS);
4370    } else {
4371	int s;
4372	tulip_intrfunc_t (*intr_rtn)(void *) = tulip_intr_normal;
4373
4374	if (sc->tulip_flags & TULIP_SHAREDINTR)
4375	    intr_rtn = tulip_intr_shared;
4376
4377#if defined(__NetBSD__)
4378	if ((sc->tulip_flags & TULIP_SLAVEDINTR) == 0) {
4379	    pci_intr_handle_t intrhandle;
4380	    const char *intrstr;
4381
4382	    if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
4383			     pa->pa_intrline, &intrhandle)) {
4384		printf(": couldn't map interrupt\n");
4385		return;
4386	    }
4387	    intrstr = pci_intr_string(pa->pa_pc, intrhandle);
4388	    sc->tulip_ih = pci_intr_establish(pa->pa_pc, intrhandle, IPL_NET,
4389					      intr_rtn, sc);
4390	    if (sc->tulip_ih == NULL)
4391		printf(": couldn't establish interrupt");
4392	    if (intrstr != NULL)
4393		printf(" at %s", intrstr);
4394	    printf("\n");
4395	    if (sc->tulip_ih == NULL)
4396		return;
4397	}
4398	sc->tulip_ats = shutdownhook_establish(tulip_pci_shutdown, sc);
4399	if (sc->tulip_ats == NULL)
4400	    printf("\n%s: warning: couldn't establish shutdown hook\n",
4401		   sc->tulip_xname);
4402#endif
4403#if defined(__FreeBSD__)
4404	if ((sc->tulip_flags & TULIP_SLAVEDINTR) == 0) {
4405	    if (!pci_map_int (config_id, intr_rtn, (void*) sc, &net_imask)) {
4406		printf(TULIP_PRINTF_FMT ": couldn't map interrupt\n",
4407		       TULIP_PRINTF_ARGS);
4408		return;
4409	    }
4410	}
4411	at_shutdown(tulip_shutdown, sc, SHUTDOWN_POST_SYNC);
4412#endif
4413#if defined(__bsdi__)
4414	if ((sc->tulip_flags & TULIP_SLAVEDINTR) == 0) {
4415	    isa_establish(&sc->tulip_id, &sc->tulip_dev);
4416
4417	    sc->tulip_ih.ih_fun = intr_rtn;
4418	    sc->tulip_ih.ih_arg = (void *) sc;
4419	    intr_establish(ia->ia_irq, &sc->tulip_ih, DV_NET);
4420	}
4421
4422	sc->tulip_ats.func = tulip_shutdown;
4423	sc->tulip_ats.arg = (void *) sc;
4424	atshutdown(&sc->tulip_ats, ATSH_ADD);
4425#endif
4426#if defined(TULIP_USE_SOFTINTR)
4427	if (sc->tulip_unit > tulip_softintr_max_unit)
4428	    tulip_softintr_max_unit = sc->tulip_unit;
4429#endif
4430#if defined(TULIP_DEBUG)
4431	if (sc->tulip_chipid == TULIP_21041) {
4432	    TULIP_CSR_WRITE(sc, csr_sia_connectivity, TULIP_SIACONN_RESET);
4433	    DELAY(1000);
4434	    PCI_CONF_WRITE(PCI_CFDA, TULIP_CFDA_SLEEP);
4435	    DELAY(20000);
4436	    PCI_CONF_WRITE(PCI_CFDA, 0);
4437	    DELAY(20000);
4438	}
4439#endif
4440
4441	s = splimp();
4442	tulip_reset(sc);
4443	tulip_attach(sc);
4444	splx(s);
4445    }
4446}
4447