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