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