xref: /freebsd/sys/dev/dc/if_dc.c (revision feb78939ee98e8e562376c487b6f51b61419c956)
196f2e892SBill Paul /*
296f2e892SBill Paul  * Copyright (c) 1997, 1998, 1999
396f2e892SBill Paul  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
496f2e892SBill Paul  *
596f2e892SBill Paul  * Redistribution and use in source and binary forms, with or without
696f2e892SBill Paul  * modification, are permitted provided that the following conditions
796f2e892SBill Paul  * are met:
896f2e892SBill Paul  * 1. Redistributions of source code must retain the above copyright
996f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer.
1096f2e892SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1196f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1296f2e892SBill Paul  *    documentation and/or other materials provided with the distribution.
1396f2e892SBill Paul  * 3. All advertising materials mentioning features or use of this software
1496f2e892SBill Paul  *    must display the following acknowledgement:
1596f2e892SBill Paul  *	This product includes software developed by Bill Paul.
1696f2e892SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1796f2e892SBill Paul  *    may be used to endorse or promote products derived from this software
1896f2e892SBill Paul  *    without specific prior written permission.
1996f2e892SBill Paul  *
2096f2e892SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2196f2e892SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2296f2e892SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2396f2e892SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2496f2e892SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2596f2e892SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2696f2e892SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2796f2e892SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2896f2e892SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2996f2e892SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3096f2e892SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3196f2e892SBill Paul  *
3296f2e892SBill Paul  * $FreeBSD$
3396f2e892SBill Paul  */
3496f2e892SBill Paul 
3596f2e892SBill Paul /*
3696f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
3796f2e892SBill Paul  * series chips and several workalikes including the following:
3896f2e892SBill Paul  *
39ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
4096f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
4196f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4296f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4396f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4496f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
4596f2e892SBill Paul  * ADMtek AN985 (www.admtek.com.tw)
4688d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
479ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
48feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
4996f2e892SBill Paul  *
5096f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5196f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5296f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5396f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5496f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
5596f2e892SBill Paul  * instead of 512.
5696f2e892SBill Paul  *
5796f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
5896f2e892SBill Paul  * Electrical Engineering Department
5996f2e892SBill Paul  * Columbia University, New York City
6096f2e892SBill Paul  */
6196f2e892SBill Paul 
6296f2e892SBill Paul /*
6396f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6496f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6596f2e892SBill Paul  * three kinds of media attachments:
6696f2e892SBill Paul  *
6796f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
6896f2e892SBill Paul  *   autonegotiation provided by an external PHY.
6996f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7096f2e892SBill Paul  * o 10baseT port.
7196f2e892SBill Paul  * o AUI/BNC port.
7296f2e892SBill Paul  *
7396f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7496f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7596f2e892SBill Paul  * autosensing configuration.
7696f2e892SBill Paul  *
7796f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
7896f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
7996f2e892SBill Paul  * handled separately due to its different register offsets and the
8096f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8196f2e892SBill Paul  * here, but I'm not thrilled about it.
8296f2e892SBill Paul  *
8396f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8496f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8596f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
8696f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
8796f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
8896f2e892SBill Paul  */
8996f2e892SBill Paul 
9096f2e892SBill Paul #include <sys/param.h>
9196f2e892SBill Paul #include <sys/systm.h>
9296f2e892SBill Paul #include <sys/sockio.h>
9396f2e892SBill Paul #include <sys/mbuf.h>
9496f2e892SBill Paul #include <sys/malloc.h>
9596f2e892SBill Paul #include <sys/kernel.h>
9696f2e892SBill Paul #include <sys/socket.h>
9796f2e892SBill Paul 
9896f2e892SBill Paul #include <net/if.h>
9996f2e892SBill Paul #include <net/if_arp.h>
10096f2e892SBill Paul #include <net/ethernet.h>
10196f2e892SBill Paul #include <net/if_dl.h>
10296f2e892SBill Paul #include <net/if_media.h>
10396f2e892SBill Paul 
10496f2e892SBill Paul #include <net/bpf.h>
10596f2e892SBill Paul 
10696f2e892SBill Paul #include <vm/vm.h>              /* for vtophys */
10796f2e892SBill Paul #include <vm/pmap.h>            /* for vtophys */
10896f2e892SBill Paul #include <machine/bus_pio.h>
10996f2e892SBill Paul #include <machine/bus_memio.h>
11096f2e892SBill Paul #include <machine/bus.h>
11196f2e892SBill Paul #include <machine/resource.h>
112d1ce9105SBill Paul #include <machine/mutex.h>
11396f2e892SBill Paul #include <sys/bus.h>
11496f2e892SBill Paul #include <sys/rman.h>
11596f2e892SBill Paul 
11696f2e892SBill Paul #include <dev/mii/mii.h>
11796f2e892SBill Paul #include <dev/mii/miivar.h>
11896f2e892SBill Paul 
11996f2e892SBill Paul #include <pci/pcireg.h>
12096f2e892SBill Paul #include <pci/pcivar.h>
12196f2e892SBill Paul 
12296f2e892SBill Paul #define DC_USEIOSPACE
1235c1cfac4SBill Paul #ifdef __alpha__
1245c1cfac4SBill Paul #define SRM_MEDIA
1255c1cfac4SBill Paul #endif
12696f2e892SBill Paul 
12796f2e892SBill Paul #include <pci/if_dcreg.h>
12896f2e892SBill Paul 
12995a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
13095a16455SPeter Wemm 
13196f2e892SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
13296f2e892SBill Paul #include "miibus_if.h"
13396f2e892SBill Paul 
13496f2e892SBill Paul #ifndef lint
13596f2e892SBill Paul static const char rcsid[] =
13696f2e892SBill Paul   "$FreeBSD$";
13796f2e892SBill Paul #endif
13896f2e892SBill Paul 
13996f2e892SBill Paul /*
14096f2e892SBill Paul  * Various supported device vendors/types and their names.
14196f2e892SBill Paul  */
14296f2e892SBill Paul static struct dc_type dc_devs[] = {
14396f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
14496f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
14596f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
14696f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
14796f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
14896f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
14988d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15088d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
15196f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
15296f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
15396f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
15496f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
15596f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
15696f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
15796f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
15896f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
15996f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16096f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
16196f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16296f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
16396f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
16496f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
16596f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
16696f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
16796f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
16896f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
16996f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17079d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
17179d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17296f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
173ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
174ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
17596f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
17696f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
17796f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
17896f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
17996f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18096f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1819ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1829ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
183feb78939SJonathan Chen     	{ DC_VENDORID_XIRCOM, DC_DEVICEID_X3201,
184feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
18596f2e892SBill Paul 	{ 0, 0, NULL }
18696f2e892SBill Paul };
18796f2e892SBill Paul 
18896f2e892SBill Paul static int dc_probe		__P((device_t));
18996f2e892SBill Paul static int dc_attach		__P((device_t));
19096f2e892SBill Paul static int dc_detach		__P((device_t));
19196f2e892SBill Paul static void dc_acpi		__P((device_t));
19296f2e892SBill Paul static struct dc_type *dc_devtype	__P((device_t));
19396f2e892SBill Paul static int dc_newbuf		__P((struct dc_softc *, int, struct mbuf *));
19496f2e892SBill Paul static int dc_encap		__P((struct dc_softc *, struct mbuf *,
19596f2e892SBill Paul 					u_int32_t *));
196fda39fd0SBill Paul static int dc_coal		__P((struct dc_softc *, struct mbuf **));
19796f2e892SBill Paul static void dc_pnic_rx_bug_war	__P((struct dc_softc *, int));
19873bf949cSBill Paul static int dc_rx_resync		__P((struct dc_softc *));
19996f2e892SBill Paul static void dc_rxeof		__P((struct dc_softc *));
20096f2e892SBill Paul static void dc_txeof		__P((struct dc_softc *));
20196f2e892SBill Paul static void dc_tick		__P((void *));
20296f2e892SBill Paul static void dc_intr		__P((void *));
20396f2e892SBill Paul static void dc_start		__P((struct ifnet *));
20496f2e892SBill Paul static int dc_ioctl		__P((struct ifnet *, u_long, caddr_t));
20596f2e892SBill Paul static void dc_init		__P((void *));
20696f2e892SBill Paul static void dc_stop		__P((struct dc_softc *));
20796f2e892SBill Paul static void dc_watchdog		__P((struct ifnet *));
20896f2e892SBill Paul static void dc_shutdown		__P((device_t));
20996f2e892SBill Paul static int dc_ifmedia_upd	__P((struct ifnet *));
21096f2e892SBill Paul static void dc_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
21196f2e892SBill Paul 
21296f2e892SBill Paul static void dc_delay		__P((struct dc_softc *));
21396f2e892SBill Paul static void dc_eeprom_idle	__P((struct dc_softc *));
21496f2e892SBill Paul static void dc_eeprom_putbyte	__P((struct dc_softc *, int));
21596f2e892SBill Paul static void dc_eeprom_getword	__P((struct dc_softc *, int, u_int16_t *));
21696f2e892SBill Paul static void dc_eeprom_getword_pnic
21796f2e892SBill Paul 				__P((struct dc_softc *, int, u_int16_t *));
218feb78939SJonathan Chen static void dc_eeprom_getword_xircom
219feb78939SJonathan Chen 				__P((struct dc_softc *, int, u_int16_t *));
22096f2e892SBill Paul static void dc_read_eeprom	__P((struct dc_softc *, caddr_t, int,
22196f2e892SBill Paul 							int, int));
22296f2e892SBill Paul 
22396f2e892SBill Paul static void dc_mii_writebit	__P((struct dc_softc *, int));
22496f2e892SBill Paul static int dc_mii_readbit	__P((struct dc_softc *));
22596f2e892SBill Paul static void dc_mii_sync		__P((struct dc_softc *));
22696f2e892SBill Paul static void dc_mii_send		__P((struct dc_softc *, u_int32_t, int));
22796f2e892SBill Paul static int dc_mii_readreg	__P((struct dc_softc *, struct dc_mii_frame *));
22896f2e892SBill Paul static int dc_mii_writereg	__P((struct dc_softc *, struct dc_mii_frame *));
22996f2e892SBill Paul static int dc_miibus_readreg	__P((device_t, int, int));
23096f2e892SBill Paul static int dc_miibus_writereg	__P((device_t, int, int, int));
23196f2e892SBill Paul static void dc_miibus_statchg	__P((device_t));
232f43d9309SBill Paul static void dc_miibus_mediainit	__P((device_t));
23396f2e892SBill Paul 
23496f2e892SBill Paul static void dc_setcfg		__P((struct dc_softc *, int));
23596f2e892SBill Paul static u_int32_t dc_crc_le	__P((struct dc_softc *, caddr_t));
23696f2e892SBill Paul static u_int32_t dc_crc_be	__P((caddr_t));
23796f2e892SBill Paul static void dc_setfilt_21143	__P((struct dc_softc *));
23896f2e892SBill Paul static void dc_setfilt_asix	__P((struct dc_softc *));
23996f2e892SBill Paul static void dc_setfilt_admtek	__P((struct dc_softc *));
240feb78939SJonathan Chen static void dc_setfilt_xircom	__P((struct dc_softc *));
24196f2e892SBill Paul 
24296f2e892SBill Paul static void dc_setfilt		__P((struct dc_softc *));
24396f2e892SBill Paul 
24496f2e892SBill Paul static void dc_reset		__P((struct dc_softc *));
24596f2e892SBill Paul static int dc_list_rx_init	__P((struct dc_softc *));
24696f2e892SBill Paul static int dc_list_tx_init	__P((struct dc_softc *));
24796f2e892SBill Paul 
2485c1cfac4SBill Paul static void dc_parse_21143_srom	__P((struct dc_softc *));
2495c1cfac4SBill Paul static void dc_decode_leaf_sia	__P((struct dc_softc *,
2505c1cfac4SBill Paul 				    struct dc_eblock_sia *));
2515c1cfac4SBill Paul static void dc_decode_leaf_mii	__P((struct dc_softc *,
2525c1cfac4SBill Paul 				    struct dc_eblock_mii *));
2535c1cfac4SBill Paul static void dc_decode_leaf_sym	__P((struct dc_softc *,
2545c1cfac4SBill Paul 				    struct dc_eblock_sym *));
2555c1cfac4SBill Paul static void dc_apply_fixup	__P((struct dc_softc *, int));
2565c1cfac4SBill Paul 
25796f2e892SBill Paul #ifdef DC_USEIOSPACE
25896f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
25996f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
26096f2e892SBill Paul #else
26196f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
26296f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
26396f2e892SBill Paul #endif
26496f2e892SBill Paul 
26596f2e892SBill Paul static device_method_t dc_methods[] = {
26696f2e892SBill Paul 	/* Device interface */
26796f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
26896f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
26996f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
27096f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
27196f2e892SBill Paul 
27296f2e892SBill Paul 	/* bus interface */
27396f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
27496f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
27596f2e892SBill Paul 
27696f2e892SBill Paul 	/* MII interface */
27796f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
27896f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
27996f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
280f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
28196f2e892SBill Paul 
28296f2e892SBill Paul 	{ 0, 0 }
28396f2e892SBill Paul };
28496f2e892SBill Paul 
28596f2e892SBill Paul static driver_t dc_driver = {
28696f2e892SBill Paul 	"dc",
28796f2e892SBill Paul 	dc_methods,
28896f2e892SBill Paul 	sizeof(struct dc_softc)
28996f2e892SBill Paul };
29096f2e892SBill Paul 
29196f2e892SBill Paul static devclass_t dc_devclass;
29296f2e892SBill Paul 
293feb78939SJonathan Chen DRIVER_MODULE(if_dc, cardbus, dc_driver, dc_devclass, 0, 0);
29496f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
29596f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
29696f2e892SBill Paul 
29796f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
29896f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
29996f2e892SBill Paul 
30096f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
30196f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
30296f2e892SBill Paul 
30396f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
30496f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
30596f2e892SBill Paul 
30696f2e892SBill Paul static void dc_delay(sc)
30796f2e892SBill Paul 	struct dc_softc		*sc;
30896f2e892SBill Paul {
30996f2e892SBill Paul 	int			idx;
31096f2e892SBill Paul 
31196f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
31296f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
31396f2e892SBill Paul }
31496f2e892SBill Paul 
31596f2e892SBill Paul static void dc_eeprom_idle(sc)
31696f2e892SBill Paul 	struct dc_softc		*sc;
31796f2e892SBill Paul {
31896f2e892SBill Paul 	register int		i;
31996f2e892SBill Paul 
32096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
32196f2e892SBill Paul 	dc_delay(sc);
32296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
32396f2e892SBill Paul 	dc_delay(sc);
32496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
32596f2e892SBill Paul 	dc_delay(sc);
32696f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
32796f2e892SBill Paul 	dc_delay(sc);
32896f2e892SBill Paul 
32996f2e892SBill Paul 	for (i = 0; i < 25; i++) {
33096f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33196f2e892SBill Paul 		dc_delay(sc);
33296f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33396f2e892SBill Paul 		dc_delay(sc);
33496f2e892SBill Paul 	}
33596f2e892SBill Paul 
33696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33796f2e892SBill Paul 	dc_delay(sc);
33896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
33996f2e892SBill Paul 	dc_delay(sc);
34096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
34196f2e892SBill Paul 
34296f2e892SBill Paul 	return;
34396f2e892SBill Paul }
34496f2e892SBill Paul 
34596f2e892SBill Paul /*
34696f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
34796f2e892SBill Paul  */
34896f2e892SBill Paul static void dc_eeprom_putbyte(sc, addr)
34996f2e892SBill Paul 	struct dc_softc		*sc;
35096f2e892SBill Paul 	int			addr;
35196f2e892SBill Paul {
35296f2e892SBill Paul 	register int		d, i;
35396f2e892SBill Paul 
35496f2e892SBill Paul 	/*
35596f2e892SBill Paul 	 * The AN985 has a 93C66 EEPROM on it instead of
35696f2e892SBill Paul 	 * a 93C46. It uses a different bit sequence for
35796f2e892SBill Paul 	 * specifying the "read" opcode.
35896f2e892SBill Paul 	 */
35996f2e892SBill Paul 	if (DC_IS_CENTAUR(sc))
36096f2e892SBill Paul 		d = addr | (DC_EECMD_READ << 2);
36196f2e892SBill Paul 	else
36296f2e892SBill Paul 		d = addr | DC_EECMD_READ;
36396f2e892SBill Paul 
36496f2e892SBill Paul 	/*
36596f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
36696f2e892SBill Paul 	 */
36796f2e892SBill Paul 	for (i = 0x400; i; i >>= 1) {
36896f2e892SBill Paul 		if (d & i) {
36996f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
37096f2e892SBill Paul 		} else {
37196f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
37296f2e892SBill Paul 		}
37396f2e892SBill Paul 		dc_delay(sc);
37496f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
37596f2e892SBill Paul 		dc_delay(sc);
37696f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
37796f2e892SBill Paul 		dc_delay(sc);
37896f2e892SBill Paul 	}
37996f2e892SBill Paul 
38096f2e892SBill Paul 	return;
38196f2e892SBill Paul }
38296f2e892SBill Paul 
38396f2e892SBill Paul /*
38496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
38596f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
38696f2e892SBill Paul  * the EEPROM.
38796f2e892SBill Paul  */
38896f2e892SBill Paul static void dc_eeprom_getword_pnic(sc, addr, dest)
38996f2e892SBill Paul 	struct dc_softc		*sc;
39096f2e892SBill Paul 	int			addr;
39196f2e892SBill Paul 	u_int16_t		*dest;
39296f2e892SBill Paul {
39396f2e892SBill Paul 	register int		i;
39496f2e892SBill Paul 	u_int32_t		r;
39596f2e892SBill Paul 
39696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
39796f2e892SBill Paul 
39896f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
39996f2e892SBill Paul 		DELAY(1);
40096f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
40196f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
40296f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
40396f2e892SBill Paul 			return;
40496f2e892SBill Paul 		}
40596f2e892SBill Paul 	}
40696f2e892SBill Paul 
40796f2e892SBill Paul 	return;
40896f2e892SBill Paul }
40996f2e892SBill Paul 
41096f2e892SBill Paul /*
41196f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
412feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
413feb78939SJonathan Chen  * the EEPROM, too.
414feb78939SJonathan Chen  */
415feb78939SJonathan Chen static void dc_eeprom_getword_xircom(sc, addr, dest)
416feb78939SJonathan Chen 	struct dc_softc		*sc;
417feb78939SJonathan Chen 	int			addr;
418feb78939SJonathan Chen 	u_int16_t		*dest;
419feb78939SJonathan Chen {
420feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
421feb78939SJonathan Chen 
422feb78939SJonathan Chen 	addr *= 2;
423feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
424feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff;
425feb78939SJonathan Chen 	addr += 1;
426feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
427feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff) << 8;
428feb78939SJonathan Chen 
429feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
430feb78939SJonathan Chen 	return;
431feb78939SJonathan Chen }
432feb78939SJonathan Chen 
433feb78939SJonathan Chen /*
434feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
43596f2e892SBill Paul  */
43696f2e892SBill Paul static void dc_eeprom_getword(sc, addr, dest)
43796f2e892SBill Paul 	struct dc_softc		*sc;
43896f2e892SBill Paul 	int			addr;
43996f2e892SBill Paul 	u_int16_t		*dest;
44096f2e892SBill Paul {
44196f2e892SBill Paul 	register int		i;
44296f2e892SBill Paul 	u_int16_t		word = 0;
44396f2e892SBill Paul 
44496f2e892SBill Paul 	/* Force EEPROM to idle state. */
44596f2e892SBill Paul 	dc_eeprom_idle(sc);
44696f2e892SBill Paul 
44796f2e892SBill Paul 	/* Enter EEPROM access mode. */
44896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
44996f2e892SBill Paul 	dc_delay(sc);
45096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
45196f2e892SBill Paul 	dc_delay(sc);
45296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
45396f2e892SBill Paul 	dc_delay(sc);
45496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
45596f2e892SBill Paul 	dc_delay(sc);
45696f2e892SBill Paul 
45796f2e892SBill Paul 	/*
45896f2e892SBill Paul 	 * Send address of word we want to read.
45996f2e892SBill Paul 	 */
46096f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
46196f2e892SBill Paul 
46296f2e892SBill Paul 	/*
46396f2e892SBill Paul 	 * Start reading bits from EEPROM.
46496f2e892SBill Paul 	 */
46596f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
46696f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
46796f2e892SBill Paul 		dc_delay(sc);
46896f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
46996f2e892SBill Paul 			word |= i;
47096f2e892SBill Paul 		dc_delay(sc);
47196f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
47296f2e892SBill Paul 		dc_delay(sc);
47396f2e892SBill Paul 	}
47496f2e892SBill Paul 
47596f2e892SBill Paul 	/* Turn off EEPROM access mode. */
47696f2e892SBill Paul 	dc_eeprom_idle(sc);
47796f2e892SBill Paul 
47896f2e892SBill Paul 	*dest = word;
47996f2e892SBill Paul 
48096f2e892SBill Paul 	return;
48196f2e892SBill Paul }
48296f2e892SBill Paul 
48396f2e892SBill Paul /*
48496f2e892SBill Paul  * Read a sequence of words from the EEPROM.
48596f2e892SBill Paul  */
48696f2e892SBill Paul static void dc_read_eeprom(sc, dest, off, cnt, swap)
48796f2e892SBill Paul 	struct dc_softc		*sc;
48896f2e892SBill Paul 	caddr_t			dest;
48996f2e892SBill Paul 	int			off;
49096f2e892SBill Paul 	int			cnt;
49196f2e892SBill Paul 	int			swap;
49296f2e892SBill Paul {
49396f2e892SBill Paul 	int			i;
49496f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
49596f2e892SBill Paul 
49696f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
49796f2e892SBill Paul 		if (DC_IS_PNIC(sc))
49896f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
499feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
500feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
50196f2e892SBill Paul 		else
50296f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
50396f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
50496f2e892SBill Paul 		if (swap)
50596f2e892SBill Paul 			*ptr = ntohs(word);
50696f2e892SBill Paul 		else
50796f2e892SBill Paul 			*ptr = word;
50896f2e892SBill Paul 	}
50996f2e892SBill Paul 
51096f2e892SBill Paul 	return;
51196f2e892SBill Paul }
51296f2e892SBill Paul 
51396f2e892SBill Paul /*
51496f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
51596f2e892SBill Paul  * Application Notes pp.19-21.
51696f2e892SBill Paul  */
51796f2e892SBill Paul /*
51896f2e892SBill Paul  * Write a bit to the MII bus.
51996f2e892SBill Paul  */
52096f2e892SBill Paul static void dc_mii_writebit(sc, bit)
52196f2e892SBill Paul 	struct dc_softc		*sc;
52296f2e892SBill Paul 	int			bit;
52396f2e892SBill Paul {
52496f2e892SBill Paul 	if (bit)
52596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
52696f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
52796f2e892SBill Paul 	else
52896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
52996f2e892SBill Paul 
53096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
53196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
53296f2e892SBill Paul 
53396f2e892SBill Paul 	return;
53496f2e892SBill Paul }
53596f2e892SBill Paul 
53696f2e892SBill Paul /*
53796f2e892SBill Paul  * Read a bit from the MII bus.
53896f2e892SBill Paul  */
53996f2e892SBill Paul static int dc_mii_readbit(sc)
54096f2e892SBill Paul 	struct dc_softc		*sc;
54196f2e892SBill Paul {
54296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
54396f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
54496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
54596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
54696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
54796f2e892SBill Paul 		return(1);
54896f2e892SBill Paul 
54996f2e892SBill Paul 	return(0);
55096f2e892SBill Paul }
55196f2e892SBill Paul 
55296f2e892SBill Paul /*
55396f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
55496f2e892SBill Paul  */
55596f2e892SBill Paul static void dc_mii_sync(sc)
55696f2e892SBill Paul 	struct dc_softc		*sc;
55796f2e892SBill Paul {
55896f2e892SBill Paul 	register int		i;
55996f2e892SBill Paul 
56096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
56196f2e892SBill Paul 
56296f2e892SBill Paul 	for (i = 0; i < 32; i++)
56396f2e892SBill Paul 		dc_mii_writebit(sc, 1);
56496f2e892SBill Paul 
56596f2e892SBill Paul 	return;
56696f2e892SBill Paul }
56796f2e892SBill Paul 
56896f2e892SBill Paul /*
56996f2e892SBill Paul  * Clock a series of bits through the MII.
57096f2e892SBill Paul  */
57196f2e892SBill Paul static void dc_mii_send(sc, bits, cnt)
57296f2e892SBill Paul 	struct dc_softc		*sc;
57396f2e892SBill Paul 	u_int32_t		bits;
57496f2e892SBill Paul 	int			cnt;
57596f2e892SBill Paul {
57696f2e892SBill Paul 	int			i;
57796f2e892SBill Paul 
57896f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
57996f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
58096f2e892SBill Paul }
58196f2e892SBill Paul 
58296f2e892SBill Paul /*
58396f2e892SBill Paul  * Read an PHY register through the MII.
58496f2e892SBill Paul  */
58596f2e892SBill Paul static int dc_mii_readreg(sc, frame)
58696f2e892SBill Paul 	struct dc_softc		*sc;
58796f2e892SBill Paul 	struct dc_mii_frame	*frame;
58896f2e892SBill Paul 
58996f2e892SBill Paul {
590d1ce9105SBill Paul 	int			i, ack;
59196f2e892SBill Paul 
592d1ce9105SBill Paul 	DC_LOCK(sc);
59396f2e892SBill Paul 
59496f2e892SBill Paul 	/*
59596f2e892SBill Paul 	 * Set up frame for RX.
59696f2e892SBill Paul 	 */
59796f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
59896f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
59996f2e892SBill Paul 	frame->mii_turnaround = 0;
60096f2e892SBill Paul 	frame->mii_data = 0;
60196f2e892SBill Paul 
60296f2e892SBill Paul 	/*
60396f2e892SBill Paul 	 * Sync the PHYs.
60496f2e892SBill Paul 	 */
60596f2e892SBill Paul 	dc_mii_sync(sc);
60696f2e892SBill Paul 
60796f2e892SBill Paul 	/*
60896f2e892SBill Paul 	 * Send command/address info.
60996f2e892SBill Paul 	 */
61096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
61196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
61296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
61396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
61496f2e892SBill Paul 
61596f2e892SBill Paul #ifdef notdef
61696f2e892SBill Paul 	/* Idle bit */
61796f2e892SBill Paul 	dc_mii_writebit(sc, 1);
61896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
61996f2e892SBill Paul #endif
62096f2e892SBill Paul 
62196f2e892SBill Paul 	/* Check for ack */
62296f2e892SBill Paul 	ack = dc_mii_readbit(sc);
62396f2e892SBill Paul 
62496f2e892SBill Paul 	/*
62596f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
62696f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
62796f2e892SBill Paul 	 */
62896f2e892SBill Paul 	if (ack) {
62996f2e892SBill Paul 		for(i = 0; i < 16; i++) {
63096f2e892SBill Paul 			dc_mii_readbit(sc);
63196f2e892SBill Paul 		}
63296f2e892SBill Paul 		goto fail;
63396f2e892SBill Paul 	}
63496f2e892SBill Paul 
63596f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
63696f2e892SBill Paul 		if (!ack) {
63796f2e892SBill Paul 			if (dc_mii_readbit(sc))
63896f2e892SBill Paul 				frame->mii_data |= i;
63996f2e892SBill Paul 		}
64096f2e892SBill Paul 	}
64196f2e892SBill Paul 
64296f2e892SBill Paul fail:
64396f2e892SBill Paul 
64496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
64596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
64696f2e892SBill Paul 
647d1ce9105SBill Paul 	DC_UNLOCK(sc);
64896f2e892SBill Paul 
64996f2e892SBill Paul 	if (ack)
65096f2e892SBill Paul 		return(1);
65196f2e892SBill Paul 	return(0);
65296f2e892SBill Paul }
65396f2e892SBill Paul 
65496f2e892SBill Paul /*
65596f2e892SBill Paul  * Write to a PHY register through the MII.
65696f2e892SBill Paul  */
65796f2e892SBill Paul static int dc_mii_writereg(sc, frame)
65896f2e892SBill Paul 	struct dc_softc		*sc;
65996f2e892SBill Paul 	struct dc_mii_frame	*frame;
66096f2e892SBill Paul 
66196f2e892SBill Paul {
662d1ce9105SBill Paul 	DC_LOCK(sc);
66396f2e892SBill Paul 	/*
66496f2e892SBill Paul 	 * Set up frame for TX.
66596f2e892SBill Paul 	 */
66696f2e892SBill Paul 
66796f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
66896f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
66996f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
67096f2e892SBill Paul 
67196f2e892SBill Paul 	/*
67296f2e892SBill Paul 	 * Sync the PHYs.
67396f2e892SBill Paul 	 */
67496f2e892SBill Paul 	dc_mii_sync(sc);
67596f2e892SBill Paul 
67696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
67796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
67896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
67996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
68096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
68196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
68296f2e892SBill Paul 
68396f2e892SBill Paul 	/* Idle bit. */
68496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
68596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
68696f2e892SBill Paul 
687d1ce9105SBill Paul 	DC_UNLOCK(sc);
68896f2e892SBill Paul 
68996f2e892SBill Paul 	return(0);
69096f2e892SBill Paul }
69196f2e892SBill Paul 
69296f2e892SBill Paul static int dc_miibus_readreg(dev, phy, reg)
69396f2e892SBill Paul 	device_t		dev;
69496f2e892SBill Paul 	int			phy, reg;
69596f2e892SBill Paul {
69696f2e892SBill Paul 	struct dc_mii_frame	frame;
69796f2e892SBill Paul 	struct dc_softc		*sc;
69896f2e892SBill Paul 	int			i, rval, phy_reg;
69996f2e892SBill Paul 
70096f2e892SBill Paul 	sc = device_get_softc(dev);
70196f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
70296f2e892SBill Paul 
70396f2e892SBill Paul 	/*
70496f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
70596f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
70696f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
70796f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
70896f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
70996f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
71096f2e892SBill Paul 	 * that the PHY is at MII address 1.
71196f2e892SBill Paul 	 */
71296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
71396f2e892SBill Paul 		return(0);
71496f2e892SBill Paul 
7155c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
71696f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
71796f2e892SBill Paul 			switch(reg) {
71896f2e892SBill Paul 			case MII_BMSR:
71996f2e892SBill Paul 			/*
72096f2e892SBill Paul 			 * Fake something to make the probe
72196f2e892SBill Paul 			 * code think there's a PHY here.
72296f2e892SBill Paul 			 */
72396f2e892SBill Paul 				return(BMSR_MEDIAMASK);
72496f2e892SBill Paul 				break;
72596f2e892SBill Paul 			case MII_PHYIDR1:
72696f2e892SBill Paul 				if (DC_IS_PNIC(sc))
72796f2e892SBill Paul 					return(DC_VENDORID_LO);
72896f2e892SBill Paul 				return(DC_VENDORID_DEC);
72996f2e892SBill Paul 				break;
73096f2e892SBill Paul 			case MII_PHYIDR2:
73196f2e892SBill Paul 				if (DC_IS_PNIC(sc))
73296f2e892SBill Paul 					return(DC_DEVICEID_82C168);
73396f2e892SBill Paul 				return(DC_DEVICEID_21143);
73496f2e892SBill Paul 				break;
73596f2e892SBill Paul 			default:
73696f2e892SBill Paul 				return(0);
73796f2e892SBill Paul 				break;
73896f2e892SBill Paul 			}
73996f2e892SBill Paul 		} else
74096f2e892SBill Paul 			return(0);
74196f2e892SBill Paul 	}
74296f2e892SBill Paul 
74396f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
74496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
74596f2e892SBill Paul 		    (phy << 23) | (reg << 18));
74696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
74796f2e892SBill Paul 			DELAY(1);
74896f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
74996f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
75096f2e892SBill Paul 				rval &= 0xFFFF;
75196f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
75296f2e892SBill Paul 			}
75396f2e892SBill Paul 		}
75496f2e892SBill Paul 		return(0);
75596f2e892SBill Paul 	}
75696f2e892SBill Paul 
75796f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
75896f2e892SBill Paul 		switch(reg) {
75996f2e892SBill Paul 		case MII_BMCR:
76096f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
76196f2e892SBill Paul 			break;
76296f2e892SBill Paul 		case MII_BMSR:
76396f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
76496f2e892SBill Paul 			break;
76596f2e892SBill Paul 		case MII_PHYIDR1:
76696f2e892SBill Paul 			phy_reg = DC_AL_VENID;
76796f2e892SBill Paul 			break;
76896f2e892SBill Paul 		case MII_PHYIDR2:
76996f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
77096f2e892SBill Paul 			break;
77196f2e892SBill Paul 		case MII_ANAR:
77296f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
77396f2e892SBill Paul 			break;
77496f2e892SBill Paul 		case MII_ANLPAR:
77596f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
77696f2e892SBill Paul 			break;
77796f2e892SBill Paul 		case MII_ANER:
77896f2e892SBill Paul 			phy_reg = DC_AL_ANER;
77996f2e892SBill Paul 			break;
78096f2e892SBill Paul 		default:
78196f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
78296f2e892SBill Paul 			    sc->dc_unit, reg);
78396f2e892SBill Paul 			return(0);
78496f2e892SBill Paul 			break;
78596f2e892SBill Paul 		}
78696f2e892SBill Paul 
78796f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
78896f2e892SBill Paul 
78996f2e892SBill Paul 		if (rval == 0xFFFF)
79096f2e892SBill Paul 			return(0);
79196f2e892SBill Paul 		return(rval);
79296f2e892SBill Paul 	}
79396f2e892SBill Paul 
79496f2e892SBill Paul 	frame.mii_phyaddr = phy;
79596f2e892SBill Paul 	frame.mii_regaddr = reg;
796f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
797f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
79896f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
799f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
80096f2e892SBill Paul 
80196f2e892SBill Paul 	return(frame.mii_data);
80296f2e892SBill Paul }
80396f2e892SBill Paul 
80496f2e892SBill Paul static int dc_miibus_writereg(dev, phy, reg, data)
80596f2e892SBill Paul 	device_t		dev;
80696f2e892SBill Paul 	int			phy, reg, data;
80796f2e892SBill Paul {
80896f2e892SBill Paul 	struct dc_softc		*sc;
80996f2e892SBill Paul 	struct dc_mii_frame	frame;
81096f2e892SBill Paul 	int			i, phy_reg;
81196f2e892SBill Paul 
81296f2e892SBill Paul 	sc = device_get_softc(dev);
81396f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
81496f2e892SBill Paul 
81596f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
81696f2e892SBill Paul 		return(0);
81796f2e892SBill Paul 
81896f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
81996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
82096f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
82196f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
82296f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
82396f2e892SBill Paul 				break;
82496f2e892SBill Paul 		}
82596f2e892SBill Paul 		return(0);
82696f2e892SBill Paul 	}
82796f2e892SBill Paul 
82896f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
82996f2e892SBill Paul 		switch(reg) {
83096f2e892SBill Paul 		case MII_BMCR:
83196f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
83296f2e892SBill Paul 			break;
83396f2e892SBill Paul 		case MII_BMSR:
83496f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
83596f2e892SBill Paul 			break;
83696f2e892SBill Paul 		case MII_PHYIDR1:
83796f2e892SBill Paul 			phy_reg = DC_AL_VENID;
83896f2e892SBill Paul 			break;
83996f2e892SBill Paul 		case MII_PHYIDR2:
84096f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
84196f2e892SBill Paul 			break;
84296f2e892SBill Paul 		case MII_ANAR:
84396f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
84496f2e892SBill Paul 			break;
84596f2e892SBill Paul 		case MII_ANLPAR:
84696f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
84796f2e892SBill Paul 			break;
84896f2e892SBill Paul 		case MII_ANER:
84996f2e892SBill Paul 			phy_reg = DC_AL_ANER;
85096f2e892SBill Paul 			break;
85196f2e892SBill Paul 		default:
85296f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
85396f2e892SBill Paul 			    sc->dc_unit, reg);
85496f2e892SBill Paul 			return(0);
85596f2e892SBill Paul 			break;
85696f2e892SBill Paul 		}
85796f2e892SBill Paul 
85896f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
85996f2e892SBill Paul 		return(0);
86096f2e892SBill Paul 	}
86196f2e892SBill Paul 
86296f2e892SBill Paul 	frame.mii_phyaddr = phy;
86396f2e892SBill Paul 	frame.mii_regaddr = reg;
86496f2e892SBill Paul 	frame.mii_data = data;
86596f2e892SBill Paul 
866f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
867f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
86896f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
869f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
87096f2e892SBill Paul 
87196f2e892SBill Paul 	return(0);
87296f2e892SBill Paul }
87396f2e892SBill Paul 
87496f2e892SBill Paul static void dc_miibus_statchg(dev)
87596f2e892SBill Paul 	device_t		dev;
87696f2e892SBill Paul {
87796f2e892SBill Paul 	struct dc_softc		*sc;
87896f2e892SBill Paul 	struct mii_data		*mii;
879f43d9309SBill Paul 	struct ifmedia		*ifm;
88096f2e892SBill Paul 
88196f2e892SBill Paul 	sc = device_get_softc(dev);
88296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
88396f2e892SBill Paul 		return;
8845c1cfac4SBill Paul 
88596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
886f43d9309SBill Paul 	ifm = &mii->mii_media;
887f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
888f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
889f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
890f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
891f43d9309SBill Paul 	} else {
89296f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
89396f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
894f43d9309SBill Paul 	}
895f43d9309SBill Paul 
896f43d9309SBill Paul 	return;
897f43d9309SBill Paul }
898f43d9309SBill Paul 
899f43d9309SBill Paul /*
900f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
901f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
902f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
903f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
904f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
905f43d9309SBill Paul  * with it itself. *sigh*
906f43d9309SBill Paul  */
907f43d9309SBill Paul static void dc_miibus_mediainit(dev)
908f43d9309SBill Paul 	device_t		dev;
909f43d9309SBill Paul {
910f43d9309SBill Paul 	struct dc_softc		*sc;
911f43d9309SBill Paul 	struct mii_data		*mii;
912f43d9309SBill Paul 	struct ifmedia		*ifm;
913f43d9309SBill Paul 	int			rev;
914f43d9309SBill Paul 
915f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
916f43d9309SBill Paul 
917f43d9309SBill Paul 	sc = device_get_softc(dev);
918f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
919f43d9309SBill Paul 	ifm = &mii->mii_media;
920f43d9309SBill Paul 
921f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
922f43d9309SBill Paul 		ifmedia_add(ifm, IFM_ETHER|IFM_homePNA, 0, NULL);
92396f2e892SBill Paul 
92496f2e892SBill Paul 	return;
92596f2e892SBill Paul }
92696f2e892SBill Paul 
92796f2e892SBill Paul #define DC_POLY		0xEDB88320
92879d11e09SBill Paul #define DC_BITS_512	9
92979d11e09SBill Paul #define DC_BITS_128	7
93079d11e09SBill Paul #define DC_BITS_64	6
93196f2e892SBill Paul 
93296f2e892SBill Paul static u_int32_t dc_crc_le(sc, addr)
93396f2e892SBill Paul 	struct dc_softc		*sc;
93496f2e892SBill Paul 	caddr_t			addr;
93596f2e892SBill Paul {
93696f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
93796f2e892SBill Paul 
93896f2e892SBill Paul 	/* Compute CRC for the address value. */
93996f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
94096f2e892SBill Paul 
94196f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
94296f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
94396f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
94496f2e892SBill Paul 	}
94596f2e892SBill Paul 
94679d11e09SBill Paul 	/*
94779d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
94879d11e09SBill Paul 	 * chips is only 128 bits wide.
94979d11e09SBill Paul 	 */
95079d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
95179d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
95296f2e892SBill Paul 
95379d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
95479d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
95579d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
95679d11e09SBill Paul 
957feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
958feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
959feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
960feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
961feb78939SJonathan Chen 			return (crc & 0x0F) + (crc	& 0x70)*3 + (14 << 4);
962feb78939SJonathan Chen 		else
963feb78939SJonathan Chen 			return (crc & 0x1F) + ((crc>>1) & 0xF0)*3 + (12 << 4);
964feb78939SJonathan Chen 	}
965feb78939SJonathan Chen 
96679d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
96796f2e892SBill Paul }
96896f2e892SBill Paul 
96996f2e892SBill Paul /*
97096f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
97196f2e892SBill Paul  */
97296f2e892SBill Paul static u_int32_t dc_crc_be(addr)
97396f2e892SBill Paul 	caddr_t			addr;
97496f2e892SBill Paul {
97596f2e892SBill Paul 	u_int32_t		crc, carry;
97696f2e892SBill Paul 	int			i, j;
97796f2e892SBill Paul 	u_int8_t		c;
97896f2e892SBill Paul 
97996f2e892SBill Paul 	/* Compute CRC for the address value. */
98096f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
98196f2e892SBill Paul 
98296f2e892SBill Paul 	for (i = 0; i < 6; i++) {
98396f2e892SBill Paul 		c = *(addr + i);
98496f2e892SBill Paul 		for (j = 0; j < 8; j++) {
98596f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
98696f2e892SBill Paul 			crc <<= 1;
98796f2e892SBill Paul 			c >>= 1;
98896f2e892SBill Paul 			if (carry)
98996f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
99096f2e892SBill Paul 		}
99196f2e892SBill Paul 	}
99296f2e892SBill Paul 
99396f2e892SBill Paul 	/* return the filter bit position */
99496f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
99596f2e892SBill Paul }
99696f2e892SBill Paul 
99796f2e892SBill Paul /*
99896f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
99996f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
100096f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
100196f2e892SBill Paul  *
100296f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
100396f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
100496f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
100596f2e892SBill Paul  * we need that too.
100696f2e892SBill Paul  */
100796f2e892SBill Paul void dc_setfilt_21143(sc)
100896f2e892SBill Paul 	struct dc_softc		*sc;
100996f2e892SBill Paul {
101096f2e892SBill Paul 	struct dc_desc		*sframe;
101196f2e892SBill Paul 	u_int32_t		h, *sp;
101296f2e892SBill Paul 	struct ifmultiaddr	*ifma;
101396f2e892SBill Paul 	struct ifnet		*ifp;
101496f2e892SBill Paul 	int			i;
101596f2e892SBill Paul 
101696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
101796f2e892SBill Paul 
101896f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
101996f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
102096f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
102196f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
102296f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
102396f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
102496f2e892SBill Paul 
102596f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
102696f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
102796f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
102896f2e892SBill Paul 
102996f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
103096f2e892SBill Paul 
103196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
103296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
103396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
103496f2e892SBill Paul 	else
103596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
103696f2e892SBill Paul 
103796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
103896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
103996f2e892SBill Paul 	else
104096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
104196f2e892SBill Paul 
104296f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
104396f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
104496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
104596f2e892SBill Paul 			continue;
104696f2e892SBill Paul 		h = dc_crc_le(sc,
104796f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
104896f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
104996f2e892SBill Paul 	}
105096f2e892SBill Paul 
105196f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
105296f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
105396f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
105496f2e892SBill Paul 	}
105596f2e892SBill Paul 
105696f2e892SBill Paul 	/* Set our MAC address */
105796f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
105896f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
105996f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
106096f2e892SBill Paul 
106196f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
106296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
106396f2e892SBill Paul 
106496f2e892SBill Paul 	/*
106596f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
106696f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
106796f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
106896f2e892SBill Paul 	 * medicine.
106996f2e892SBill Paul 	 */
107096f2e892SBill Paul 	DELAY(10000);
107196f2e892SBill Paul 
107296f2e892SBill Paul 	ifp->if_timer = 5;
107396f2e892SBill Paul 
107496f2e892SBill Paul 	return;
107596f2e892SBill Paul }
107696f2e892SBill Paul 
107796f2e892SBill Paul void dc_setfilt_admtek(sc)
107896f2e892SBill Paul 	struct dc_softc		*sc;
107996f2e892SBill Paul {
108096f2e892SBill Paul 	struct ifnet		*ifp;
108196f2e892SBill Paul 	int			h = 0;
108296f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
108396f2e892SBill Paul 	struct ifmultiaddr	*ifma;
108496f2e892SBill Paul 
108596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
108696f2e892SBill Paul 
108796f2e892SBill Paul 	/* Init our MAC address */
108896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
108996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
109096f2e892SBill Paul 
109196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
109296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
109396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
109496f2e892SBill Paul 	else
109596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
109696f2e892SBill Paul 
109796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
109896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
109996f2e892SBill Paul 	else
110096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
110196f2e892SBill Paul 
110296f2e892SBill Paul 	/* first, zot all the existing hash bits */
110396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
110496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
110596f2e892SBill Paul 
110696f2e892SBill Paul 	/*
110796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
110896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
110996f2e892SBill Paul 	 */
111096f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
111196f2e892SBill Paul 		return;
111296f2e892SBill Paul 
111396f2e892SBill Paul 	/* now program new ones */
111496f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
111596f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
111696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
111796f2e892SBill Paul 			continue;
111896f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
111996f2e892SBill Paul 		if (h < 32)
112096f2e892SBill Paul 			hashes[0] |= (1 << h);
112196f2e892SBill Paul 		else
112296f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
112396f2e892SBill Paul 	}
112496f2e892SBill Paul 
112596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
112696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
112796f2e892SBill Paul 
112896f2e892SBill Paul 	return;
112996f2e892SBill Paul }
113096f2e892SBill Paul 
113196f2e892SBill Paul void dc_setfilt_asix(sc)
113296f2e892SBill Paul 	struct dc_softc		*sc;
113396f2e892SBill Paul {
113496f2e892SBill Paul 	struct ifnet		*ifp;
113596f2e892SBill Paul 	int			h = 0;
113696f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
113796f2e892SBill Paul 	struct ifmultiaddr	*ifma;
113896f2e892SBill Paul 
113996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
114096f2e892SBill Paul 
114196f2e892SBill Paul         /* Init our MAC address */
114296f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
114396f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
114496f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
114596f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
114696f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
114796f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
114896f2e892SBill Paul 
114996f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
115096f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
115196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
115296f2e892SBill Paul 	else
115396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
115496f2e892SBill Paul 
115596f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
115696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
115796f2e892SBill Paul 	else
115896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
115996f2e892SBill Paul 
116096f2e892SBill Paul 	/*
116196f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
116296f2e892SBill Paul 	 * of broadcast frames.
116396f2e892SBill Paul 	 */
116496f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
116596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
116696f2e892SBill Paul 	else
116796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
116896f2e892SBill Paul 
116996f2e892SBill Paul 	/* first, zot all the existing hash bits */
117096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
117196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
117296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
117396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
117496f2e892SBill Paul 
117596f2e892SBill Paul 	/*
117696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
117796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
117896f2e892SBill Paul 	 */
117996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
118096f2e892SBill Paul 		return;
118196f2e892SBill Paul 
118296f2e892SBill Paul 	/* now program new ones */
118396f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
118496f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
118596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
118696f2e892SBill Paul 			continue;
118796f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
118896f2e892SBill Paul 		if (h < 32)
118996f2e892SBill Paul 			hashes[0] |= (1 << h);
119096f2e892SBill Paul 		else
119196f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
119296f2e892SBill Paul 	}
119396f2e892SBill Paul 
119496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
119596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
119696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
119796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
119896f2e892SBill Paul 
119996f2e892SBill Paul 	return;
120096f2e892SBill Paul }
120196f2e892SBill Paul 
1202feb78939SJonathan Chen void dc_setfilt_xircom(sc)
1203feb78939SJonathan Chen 	struct dc_softc		*sc;
1204feb78939SJonathan Chen {
1205feb78939SJonathan Chen 	struct dc_desc		*sframe;
1206feb78939SJonathan Chen 	u_int32_t		h, *sp;
1207feb78939SJonathan Chen 	struct ifmultiaddr	*ifma;
1208feb78939SJonathan Chen 	struct ifnet		*ifp;
1209feb78939SJonathan Chen 	int			i;
1210feb78939SJonathan Chen 
1211feb78939SJonathan Chen 	ifp = &sc->arpcom.ac_if;
1212feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
1213feb78939SJonathan Chen 
1214feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1215feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1216feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1217feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
1218feb78939SJonathan Chen 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
1219feb78939SJonathan Chen 	bzero((char *)sp, DC_SFRAME_LEN);
1220feb78939SJonathan Chen 
1221feb78939SJonathan Chen 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
1222feb78939SJonathan Chen 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
1223feb78939SJonathan Chen 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
1224feb78939SJonathan Chen 
1225feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
1226feb78939SJonathan Chen 
1227feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1228feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1229feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1230feb78939SJonathan Chen 	else
1231feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1232feb78939SJonathan Chen 
1233feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1234feb78939SJonathan Chen  		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1235feb78939SJonathan Chen 	else
1236feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1237feb78939SJonathan Chen 
1238feb78939SJonathan Chen 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
1239feb78939SJonathan Chen 	    ifma = ifma->ifma_link.le_next) {
1240feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1241feb78939SJonathan Chen 			continue;
1242feb78939SJonathan Chen 		h = dc_crc_le(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1243feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1244feb78939SJonathan Chen 	}
1245feb78939SJonathan Chen 
1246feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1247feb78939SJonathan Chen 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
1248feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1249feb78939SJonathan Chen 	}
1250feb78939SJonathan Chen 
1251feb78939SJonathan Chen 	/* Set our MAC address */
1252feb78939SJonathan Chen 	sp[0] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
1253feb78939SJonathan Chen 	sp[1] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
1254feb78939SJonathan Chen 	sp[2] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
1255feb78939SJonathan Chen 
1256feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1257feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1258feb78939SJonathan Chen 	ifp->if_flags |= IFF_RUNNING;
1259feb78939SJonathan Chen 	sframe->dc_status = DC_TXSTAT_OWN;
1260feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1261feb78939SJonathan Chen 
1262feb78939SJonathan Chen 	/*
1263feb78939SJonathan Chen 	 * wait some time...
1264feb78939SJonathan Chen 	 */
1265feb78939SJonathan Chen 	DELAY(1000);
1266feb78939SJonathan Chen 
1267feb78939SJonathan Chen 	ifp->if_timer = 5;
1268feb78939SJonathan Chen 
1269feb78939SJonathan Chen 	return;
1270feb78939SJonathan Chen }
1271feb78939SJonathan Chen 
127296f2e892SBill Paul static void dc_setfilt(sc)
127396f2e892SBill Paul 	struct dc_softc		*sc;
127496f2e892SBill Paul {
127596f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
127696f2e892SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc))
127796f2e892SBill Paul 		dc_setfilt_21143(sc);
127896f2e892SBill Paul 
127996f2e892SBill Paul 	if (DC_IS_ASIX(sc))
128096f2e892SBill Paul 		dc_setfilt_asix(sc);
128196f2e892SBill Paul 
128296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
128396f2e892SBill Paul 		dc_setfilt_admtek(sc);
128496f2e892SBill Paul 
1285feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1286feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
1287feb78939SJonathan Chen 
128896f2e892SBill Paul  	return;
128996f2e892SBill Paul }
129096f2e892SBill Paul 
129196f2e892SBill Paul /*
129296f2e892SBill Paul  * In order to fiddle with the
129396f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
129496f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
129596f2e892SBill Paul  */
129696f2e892SBill Paul static void dc_setcfg(sc, media)
129796f2e892SBill Paul 	struct dc_softc		*sc;
129896f2e892SBill Paul 	int			media;
129996f2e892SBill Paul {
130096f2e892SBill Paul 	int			i, restart = 0;
130196f2e892SBill Paul 	u_int32_t		isr;
130296f2e892SBill Paul 
130396f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
130496f2e892SBill Paul 		return;
130596f2e892SBill Paul 
130696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
130796f2e892SBill Paul 		restart = 1;
130896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
130996f2e892SBill Paul 
131096f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
131196f2e892SBill Paul 			DELAY(10);
131296f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
131396f2e892SBill Paul 			if (isr & DC_ISR_TX_IDLE ||
131496f2e892SBill Paul 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED)
131596f2e892SBill Paul 				break;
131696f2e892SBill Paul 		}
131796f2e892SBill Paul 
131896f2e892SBill Paul 		if (i == DC_TIMEOUT)
131996f2e892SBill Paul 			printf("dc%d: failed to force tx and "
132096f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
132196f2e892SBill Paul 
132296f2e892SBill Paul 	}
132396f2e892SBill Paul 
132496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1325042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1326042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
132796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
13288273d5f8SBill Paul 			int	watchdogreg;
13298273d5f8SBill Paul 
1330bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
13318273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
13328273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
13338273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
13348273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
13354c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1336bf645417SBill Paul 			} else {
1337bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1338bf645417SBill Paul 			}
133996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
134096f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
134196f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
134296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
134396f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
134488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
134596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
134696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
134796f2e892SBill Paul 		} else {
134896f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
134996f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
135096f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
135196f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
135296f2e892SBill Paul 			}
1353318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1354318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1355318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
13565c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
13575c1cfac4SBill Paul 				dc_apply_fixup(sc,
13585c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
13595c1cfac4SBill Paul 				    IFM_100_TX|IFM_FDX : IFM_100_TX);
136096f2e892SBill Paul 		}
136196f2e892SBill Paul 	}
136296f2e892SBill Paul 
136396f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1364042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1365042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
136696f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
13678273d5f8SBill Paul 			int	watchdogreg;
13688273d5f8SBill Paul 
13698273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
13704c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
13718273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
13728273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
13738273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
13748273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
13754c2efe27SBill Paul 			} else {
13764c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
13774c2efe27SBill Paul 			}
137896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
137996f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
138096f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
138196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
138288d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
138396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
138496f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
138596f2e892SBill Paul 		} else {
138696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
138796f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
138896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
138996f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
139096f2e892SBill Paul 			}
139196f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1392318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
139396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
13945c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
13955c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
13965c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
13975c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
13985c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
13995c1cfac4SBill Paul 				else
14005c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14015c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14025c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14035c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14045c1cfac4SBill Paul 				dc_apply_fixup(sc,
14055c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14065c1cfac4SBill Paul 				    IFM_10_T|IFM_FDX : IFM_10_T);
14075c1cfac4SBill Paul 				DELAY(20000);
14085c1cfac4SBill Paul 			}
140996f2e892SBill Paul 		}
141096f2e892SBill Paul 	}
141196f2e892SBill Paul 
1412f43d9309SBill Paul 	/*
1413f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1414f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1415f43d9309SBill Paul 	 * on the external MII port.
1416f43d9309SBill Paul 	 */
1417f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
1418f43d9309SBill Paul 		if (IFM_SUBTYPE(media) == IFM_homePNA) {
1419f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1420f43d9309SBill Paul 			sc->dc_link = 1;
1421f43d9309SBill Paul 		} else {
1422f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1423f43d9309SBill Paul 		}
1424f43d9309SBill Paul 	}
1425f43d9309SBill Paul 
142696f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
142796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
142896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
142996f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
143096f2e892SBill Paul 	} else {
143196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
143296f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
143396f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
143496f2e892SBill Paul 	}
143596f2e892SBill Paul 
143696f2e892SBill Paul 	if (restart)
143796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
143896f2e892SBill Paul 
143996f2e892SBill Paul 	return;
144096f2e892SBill Paul }
144196f2e892SBill Paul 
144296f2e892SBill Paul static void dc_reset(sc)
144396f2e892SBill Paul 	struct dc_softc		*sc;
144496f2e892SBill Paul {
144596f2e892SBill Paul 	register int		i;
144696f2e892SBill Paul 
144796f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
144896f2e892SBill Paul 
144996f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
145096f2e892SBill Paul 		DELAY(10);
145196f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
145296f2e892SBill Paul 			break;
145396f2e892SBill Paul 	}
145496f2e892SBill Paul 
1455feb78939SJonathan Chen 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
145696f2e892SBill Paul 		DELAY(10000);
145796f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
145896f2e892SBill Paul 		i = 0;
145996f2e892SBill Paul 	}
146096f2e892SBill Paul 
146196f2e892SBill Paul 	if (i == DC_TIMEOUT)
146296f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
146396f2e892SBill Paul 
146496f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
146596f2e892SBill Paul 	DELAY(1000);
146696f2e892SBill Paul 
146796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
146896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
146996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
147096f2e892SBill Paul 
147191cc2adbSBill Paul 	/*
147291cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
147391cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
147491cc2adbSBill Paul 	 * into a state where it will never come out of reset
147591cc2adbSBill Paul 	 * until we reset the whole chip again.
147691cc2adbSBill Paul 	 */
14775c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
147891cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14795c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
14805c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
14815c1cfac4SBill Paul 	}
148291cc2adbSBill Paul 
148396f2e892SBill Paul         return;
148496f2e892SBill Paul }
148596f2e892SBill Paul 
148696f2e892SBill Paul static struct dc_type *dc_devtype(dev)
148796f2e892SBill Paul 	device_t		dev;
148896f2e892SBill Paul {
148996f2e892SBill Paul 	struct dc_type		*t;
149096f2e892SBill Paul 	u_int32_t		rev;
149196f2e892SBill Paul 
149296f2e892SBill Paul 	t = dc_devs;
149396f2e892SBill Paul 
149496f2e892SBill Paul 	while(t->dc_name != NULL) {
149596f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
149696f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
149796f2e892SBill Paul 			/* Check the PCI revision */
149896f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
149996f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
150096f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
150196f2e892SBill Paul 				t++;
150296f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
150396f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
150496f2e892SBill Paul 				t++;
150596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
150679d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
150779d11e09SBill Paul 				t++;
150879d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
150996f2e892SBill Paul 			    rev >= DC_REVISION_98725)
151096f2e892SBill Paul 				t++;
151196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
151296f2e892SBill Paul 			    rev >= DC_REVISION_88141)
151396f2e892SBill Paul 				t++;
151496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
151596f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
151696f2e892SBill Paul 				t++;
151788d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
151888d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
151988d739dcSBill Paul 				t++;
152096f2e892SBill Paul 			return(t);
152196f2e892SBill Paul 		}
152296f2e892SBill Paul 		t++;
152396f2e892SBill Paul 	}
152496f2e892SBill Paul 
152596f2e892SBill Paul 	return(NULL);
152696f2e892SBill Paul }
152796f2e892SBill Paul 
152896f2e892SBill Paul /*
152996f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
153096f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
153196f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
153296f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
153396f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
153496f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
153596f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
153696f2e892SBill Paul  */
153796f2e892SBill Paul static int dc_probe(dev)
153896f2e892SBill Paul 	device_t		dev;
153996f2e892SBill Paul {
154096f2e892SBill Paul 	struct dc_type		*t;
154196f2e892SBill Paul 
154296f2e892SBill Paul 	t = dc_devtype(dev);
154396f2e892SBill Paul 
154496f2e892SBill Paul 	if (t != NULL) {
154596f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
154696f2e892SBill Paul 		return(0);
154796f2e892SBill Paul 	}
154896f2e892SBill Paul 
154996f2e892SBill Paul 	return(ENXIO);
155096f2e892SBill Paul }
155196f2e892SBill Paul 
155296f2e892SBill Paul static void dc_acpi(dev)
155396f2e892SBill Paul 	device_t		dev;
155496f2e892SBill Paul {
155596f2e892SBill Paul 	u_int32_t		r, cptr;
155696f2e892SBill Paul 	int			unit;
155796f2e892SBill Paul 
155896f2e892SBill Paul 	unit = device_get_unit(dev);
155996f2e892SBill Paul 
156096f2e892SBill Paul 	/* Find the location of the capabilities block */
156196f2e892SBill Paul 	cptr = pci_read_config(dev, DC_PCI_CCAP, 4) & 0xFF;
156296f2e892SBill Paul 
156396f2e892SBill Paul 	r = pci_read_config(dev, cptr, 4) & 0xFF;
156496f2e892SBill Paul 	if (r == 0x01) {
156596f2e892SBill Paul 
156696f2e892SBill Paul 		r = pci_read_config(dev, cptr + 4, 4);
156796f2e892SBill Paul 		if (r & DC_PSTATE_D3) {
156896f2e892SBill Paul 			u_int32_t		iobase, membase, irq;
156996f2e892SBill Paul 
157096f2e892SBill Paul 			/* Save important PCI config data. */
157196f2e892SBill Paul 			iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
157296f2e892SBill Paul 			membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
157396f2e892SBill Paul 			irq = pci_read_config(dev, DC_PCI_CFIT, 4);
157496f2e892SBill Paul 
157596f2e892SBill Paul 			/* Reset the power state. */
157696f2e892SBill Paul 			printf("dc%d: chip is in D%d power mode "
157796f2e892SBill Paul 			    "-- setting to D0\n", unit, r & DC_PSTATE_D3);
157896f2e892SBill Paul 			r &= 0xFFFFFFFC;
157996f2e892SBill Paul 			pci_write_config(dev, cptr + 4, r, 4);
158096f2e892SBill Paul 
158196f2e892SBill Paul 			/* Restore PCI config data. */
158296f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
158396f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
158496f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFIT, irq, 4);
158596f2e892SBill Paul 		}
158696f2e892SBill Paul 	}
158796f2e892SBill Paul 	return;
158896f2e892SBill Paul }
158996f2e892SBill Paul 
15905c1cfac4SBill Paul static void dc_apply_fixup(sc, media)
15915c1cfac4SBill Paul 	struct dc_softc		*sc;
15925c1cfac4SBill Paul 	int			media;
15935c1cfac4SBill Paul {
15945c1cfac4SBill Paul 	struct dc_mediainfo	*m;
15955c1cfac4SBill Paul 	u_int8_t		*p;
15965c1cfac4SBill Paul 	int			i;
15975c1cfac4SBill Paul 	u_int8_t		reg;
15985c1cfac4SBill Paul 
15995c1cfac4SBill Paul 	m = sc->dc_mi;
16005c1cfac4SBill Paul 
16015c1cfac4SBill Paul 	while (m != NULL) {
16025c1cfac4SBill Paul 		if (m->dc_media == media)
16035c1cfac4SBill Paul 			break;
16045c1cfac4SBill Paul 		m = m->dc_next;
16055c1cfac4SBill Paul 	}
16065c1cfac4SBill Paul 
16075c1cfac4SBill Paul 	if (m == NULL)
16085c1cfac4SBill Paul 		return;
16095c1cfac4SBill Paul 
16105c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16115c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16125c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16135c1cfac4SBill Paul 	}
16145c1cfac4SBill Paul 
16155c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16165c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16175c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16185c1cfac4SBill Paul 	}
16195c1cfac4SBill Paul 
16205c1cfac4SBill Paul 	return;
16215c1cfac4SBill Paul }
16225c1cfac4SBill Paul 
16235c1cfac4SBill Paul static void dc_decode_leaf_sia(sc, l)
16245c1cfac4SBill Paul 	struct dc_softc		*sc;
16255c1cfac4SBill Paul 	struct dc_eblock_sia	*l;
16265c1cfac4SBill Paul {
16275c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16285c1cfac4SBill Paul 
16295c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
16305c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
16315c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
16325c1cfac4SBill Paul 
16335c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
16345c1cfac4SBill Paul 		m->dc_media = IFM_10_T|IFM_FDX;
16355c1cfac4SBill Paul 
16365c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
16375c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
16385c1cfac4SBill Paul 
16395c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
16405c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
16415c1cfac4SBill Paul 
16425c1cfac4SBill Paul 	m->dc_gp_len = 2;
16435c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
16445c1cfac4SBill Paul 
16455c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16465c1cfac4SBill Paul 	sc->dc_mi = m;
16475c1cfac4SBill Paul 
16485c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
16495c1cfac4SBill Paul 
16505c1cfac4SBill Paul 	return;
16515c1cfac4SBill Paul }
16525c1cfac4SBill Paul 
16535c1cfac4SBill Paul static void dc_decode_leaf_sym(sc, l)
16545c1cfac4SBill Paul 	struct dc_softc		*sc;
16555c1cfac4SBill Paul 	struct dc_eblock_sym	*l;
16565c1cfac4SBill Paul {
16575c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16585c1cfac4SBill Paul 
16595c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
16605c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16615c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16625c1cfac4SBill Paul 
16635c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16645c1cfac4SBill Paul 		m->dc_media = IFM_100_TX|IFM_FDX;
16655c1cfac4SBill Paul 
16665c1cfac4SBill Paul 	m->dc_gp_len = 2;
16675c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
16685c1cfac4SBill Paul 
16695c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16705c1cfac4SBill Paul 	sc->dc_mi = m;
16715c1cfac4SBill Paul 
16725c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
16735c1cfac4SBill Paul 
16745c1cfac4SBill Paul 	return;
16755c1cfac4SBill Paul }
16765c1cfac4SBill Paul 
16775c1cfac4SBill Paul static void dc_decode_leaf_mii(sc, l)
16785c1cfac4SBill Paul 	struct dc_softc		*sc;
16795c1cfac4SBill Paul 	struct dc_eblock_mii	*l;
16805c1cfac4SBill Paul {
16815c1cfac4SBill Paul 	u_int8_t		*p;
16825c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16835c1cfac4SBill Paul 
16845c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
16855c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
16865c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
16875c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
16885c1cfac4SBill Paul 
16895c1cfac4SBill Paul 	p = (u_int8_t *)l;
16905c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
16915c1cfac4SBill Paul 	m->dc_gp_ptr = p;
16925c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
16935c1cfac4SBill Paul 	m->dc_reset_len = *p;
16945c1cfac4SBill Paul 	p++;
16955c1cfac4SBill Paul 	m->dc_reset_ptr = p;
16965c1cfac4SBill Paul 
16975c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16985c1cfac4SBill Paul 	sc->dc_mi = m;
16995c1cfac4SBill Paul 
17005c1cfac4SBill Paul 	return;
17015c1cfac4SBill Paul }
17025c1cfac4SBill Paul 
17035c1cfac4SBill Paul static void dc_parse_21143_srom(sc)
17045c1cfac4SBill Paul 	struct dc_softc		*sc;
17055c1cfac4SBill Paul {
17065c1cfac4SBill Paul 	struct dc_leaf_hdr	*lhdr;
17075c1cfac4SBill Paul 	struct dc_eblock_hdr	*hdr;
17085c1cfac4SBill Paul 	int			i, loff;
17095c1cfac4SBill Paul 	char			*ptr;
17105c1cfac4SBill Paul 
17115c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17125c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17135c1cfac4SBill Paul 
17145c1cfac4SBill Paul 	ptr = (char *)lhdr;
17155c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17165c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17175c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17185c1cfac4SBill Paul 		switch(hdr->dc_type) {
17195c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17205c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17215c1cfac4SBill Paul 			break;
17225c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
17235c1cfac4SBill Paul 			dc_decode_leaf_sia(sc, (struct dc_eblock_sia *)hdr);
17245c1cfac4SBill Paul 			break;
17255c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
17265c1cfac4SBill Paul 			dc_decode_leaf_sym(sc, (struct dc_eblock_sym *)hdr);
17275c1cfac4SBill Paul 			break;
17285c1cfac4SBill Paul 		default:
17295c1cfac4SBill Paul 			/* Don't care. Yet. */
17305c1cfac4SBill Paul 			break;
17315c1cfac4SBill Paul 		}
17325c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17335c1cfac4SBill Paul 		ptr++;
17345c1cfac4SBill Paul 	}
17355c1cfac4SBill Paul 
17365c1cfac4SBill Paul 	return;
17375c1cfac4SBill Paul }
17385c1cfac4SBill Paul 
173996f2e892SBill Paul /*
174096f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
174196f2e892SBill Paul  * setup and ethernet/BPF attach.
174296f2e892SBill Paul  */
174396f2e892SBill Paul static int dc_attach(dev)
174496f2e892SBill Paul 	device_t		dev;
174596f2e892SBill Paul {
1746d1ce9105SBill Paul 	int			tmp = 0;
174796f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
174896f2e892SBill Paul 	u_int32_t		command;
174996f2e892SBill Paul 	struct dc_softc		*sc;
175096f2e892SBill Paul 	struct ifnet		*ifp;
175196f2e892SBill Paul 	u_int32_t		revision;
175296f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
175396f2e892SBill Paul 
175496f2e892SBill Paul 	sc = device_get_softc(dev);
175596f2e892SBill Paul 	unit = device_get_unit(dev);
175696f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
175796f2e892SBill Paul 
175896f2e892SBill Paul 	/*
175996f2e892SBill Paul 	 * Handle power management nonsense.
176096f2e892SBill Paul 	 */
176196f2e892SBill Paul 	dc_acpi(dev);
176296f2e892SBill Paul 
176396f2e892SBill Paul 	/*
176496f2e892SBill Paul 	 * Map control/status registers.
176596f2e892SBill Paul 	 */
1766c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
176796f2e892SBill Paul 	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1768c48cc9ceSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, command, 4);
1769c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
177096f2e892SBill Paul 
177196f2e892SBill Paul #ifdef DC_USEIOSPACE
177296f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
177396f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
177496f2e892SBill Paul 		error = ENXIO;
177596f2e892SBill Paul 		goto fail;
177696f2e892SBill Paul 	}
177796f2e892SBill Paul #else
177896f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
177996f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
178096f2e892SBill Paul 		error = ENXIO;
178196f2e892SBill Paul 		goto fail;
178296f2e892SBill Paul 	}
178396f2e892SBill Paul #endif
178496f2e892SBill Paul 
178596f2e892SBill Paul 	rid = DC_RID;
178696f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
178796f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
178896f2e892SBill Paul 
178996f2e892SBill Paul 	if (sc->dc_res == NULL) {
179096f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
179196f2e892SBill Paul 		error = ENXIO;
179296f2e892SBill Paul 		goto fail;
179396f2e892SBill Paul 	}
179496f2e892SBill Paul 
179596f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
179696f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
179796f2e892SBill Paul 
179896f2e892SBill Paul 	/* Allocate interrupt */
179996f2e892SBill Paul 	rid = 0;
180096f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
180196f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
180296f2e892SBill Paul 
180396f2e892SBill Paul 	if (sc->dc_irq == NULL) {
180496f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
180596f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
180696f2e892SBill Paul 		error = ENXIO;
180796f2e892SBill Paul 		goto fail;
180896f2e892SBill Paul 	}
180996f2e892SBill Paul 
181096f2e892SBill Paul 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET,
181196f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
181296f2e892SBill Paul 
181396f2e892SBill Paul 	if (error) {
181496f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
181596f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
181696f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
181796f2e892SBill Paul 		goto fail;
181896f2e892SBill Paul 	}
181996f2e892SBill Paul 
18201e856a7bSBill Paul 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_DEF);
1821d1ce9105SBill Paul 	DC_LOCK(sc);
182296f2e892SBill Paul 	/* Need this info to decide on a chip type. */
182396f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
182496f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
182596f2e892SBill Paul 
182696f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
182796f2e892SBill Paul 	case DC_DEVICEID_21143:
182896f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
182996f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1830042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
18315c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
18325c1cfac4SBill Paul 		dc_read_eeprom(sc, (caddr_t)&sc->dc_srom, 0, 512, 0);
183396f2e892SBill Paul 		break;
183496f2e892SBill Paul 	case DC_DEVICEID_DM9100:
183596f2e892SBill Paul 	case DC_DEVICEID_DM9102:
183696f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1837fda39fd0SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_USE_TX_INTR;
18385c1cfac4SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_TX_ONE;
183996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
184096f2e892SBill Paul 		break;
184196f2e892SBill Paul 	case DC_DEVICEID_AL981:
184296f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
184396f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
184496f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
184596f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
184696f2e892SBill Paul 		break;
184796f2e892SBill Paul 	case DC_DEVICEID_AN985:
184896f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
184996f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
185096f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
185196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
185296f2e892SBill Paul 		break;
185396f2e892SBill Paul 	case DC_DEVICEID_98713:
185496f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
185596f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
185696f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
185796f2e892SBill Paul 		}
1858318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
185996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1860318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1861318b02fdSBill Paul 		}
1862318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
186396f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
186496f2e892SBill Paul 		break;
186596f2e892SBill Paul 	case DC_DEVICEID_987x5:
18669ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
186779d11e09SBill Paul 		/*
186879d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
186979d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
187079d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
187179d11e09SBill Paul 		 * get the right number of bits out of the
187279d11e09SBill Paul 		 * CRC routine.
187379d11e09SBill Paul 		 */
187479d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
187579d11e09SBill Paul 		    revision < DC_REVISION_98725)
187679d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
187796f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
187896f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1879318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
188096f2e892SBill Paul 		break;
1881ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1882ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1883ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1884ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
1885ead7cde9SBill Paul 		break;
188696f2e892SBill Paul 	case DC_DEVICEID_82C115:
188796f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
188879d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR|DC_128BIT_HASH;
1889318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
189096f2e892SBill Paul 		break;
189196f2e892SBill Paul 	case DC_DEVICEID_82C168:
189296f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
189391cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
189496f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
189596f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
189696f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
189796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
189896f2e892SBill Paul 		break;
189996f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
190096f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
190196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
190296f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
190396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
190496f2e892SBill Paul 		break;
1905feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
1906feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
1907feb78939SJonathan Chen 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE;
1908feb78939SJonathan Chen 		/*
1909feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1910feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
1911feb78939SJonathan Chen 		 */
1912feb78939SJonathan Chen 		break;
191396f2e892SBill Paul 	default:
191496f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
191596f2e892SBill Paul 		    sc->dc_info->dc_did);
191696f2e892SBill Paul 		break;
191796f2e892SBill Paul 	}
191896f2e892SBill Paul 
191996f2e892SBill Paul 	/* Save the cache line size. */
192088d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
192188d739dcSBill Paul 		sc->dc_cachesize = 0;
192288d739dcSBill Paul 	else
192388d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
192488d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
192596f2e892SBill Paul 
192696f2e892SBill Paul 	/* Reset the adapter. */
192796f2e892SBill Paul 	dc_reset(sc);
192896f2e892SBill Paul 
192996f2e892SBill Paul 	/* Take 21143 out of snooze mode */
1930feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
193196f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
193296f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
193396f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
193496f2e892SBill Paul 	}
193596f2e892SBill Paul 
193696f2e892SBill Paul 	/*
193796f2e892SBill Paul 	 * Try to learn something about the supported media.
193896f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
193996f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
194096f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
194196f2e892SBill Paul 	 * Intel 21143.
194296f2e892SBill Paul 	 */
19435c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
19445c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
19455c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
194696f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
194796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
194896f2e892SBill Paul 		else
194996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
195096f2e892SBill Paul 	} else if (!sc->dc_pmode)
195196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
195296f2e892SBill Paul 
195396f2e892SBill Paul 	/*
195496f2e892SBill Paul 	 * Get station address from the EEPROM.
195596f2e892SBill Paul 	 */
195696f2e892SBill Paul 	switch(sc->dc_type) {
195796f2e892SBill Paul 	case DC_TYPE_98713:
195896f2e892SBill Paul 	case DC_TYPE_98713A:
195996f2e892SBill Paul 	case DC_TYPE_987x5:
196096f2e892SBill Paul 	case DC_TYPE_PNICII:
196196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
196296f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
196396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
196496f2e892SBill Paul 		break;
196596f2e892SBill Paul 	case DC_TYPE_PNIC:
196696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
196796f2e892SBill Paul 		break;
196896f2e892SBill Paul 	case DC_TYPE_DM9102:
196996f2e892SBill Paul 	case DC_TYPE_21143:
197096f2e892SBill Paul 	case DC_TYPE_ASIX:
197196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
197296f2e892SBill Paul 		break;
197396f2e892SBill Paul 	case DC_TYPE_AL981:
197496f2e892SBill Paul 	case DC_TYPE_AN985:
197596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
197696f2e892SBill Paul 		break;
1977feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
1978feb78939SJonathan Chen 		dc_read_eeprom(sc, (caddr_t)&eaddr, 3, 3, 0);
1979feb78939SJonathan Chen 		break;
198096f2e892SBill Paul 	default:
198196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
198296f2e892SBill Paul 		break;
198396f2e892SBill Paul 	}
198496f2e892SBill Paul 
198596f2e892SBill Paul 	/*
198696f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
198796f2e892SBill Paul 	 */
198896f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
198996f2e892SBill Paul 
199096f2e892SBill Paul 	sc->dc_unit = unit;
199196f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
199296f2e892SBill Paul 
199396f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
199496f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
199596f2e892SBill Paul 
199696f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
199796f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
199896f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
199996f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
200096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
200196f2e892SBill Paul 		error = ENXIO;
200296f2e892SBill Paul 		goto fail;
200396f2e892SBill Paul 	}
200496f2e892SBill Paul 
200596f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
200696f2e892SBill Paul 
200796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
200896f2e892SBill Paul 	ifp->if_softc = sc;
200996f2e892SBill Paul 	ifp->if_unit = unit;
201096f2e892SBill Paul 	ifp->if_name = "dc";
2011feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
201296f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
201396f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
201496f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
201596f2e892SBill Paul 	ifp->if_output = ether_output;
201696f2e892SBill Paul 	ifp->if_start = dc_start;
201796f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
201896f2e892SBill Paul 	ifp->if_init = dc_init;
201996f2e892SBill Paul 	ifp->if_baudrate = 10000000;
202096f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
202196f2e892SBill Paul 
202296f2e892SBill Paul 	/*
20235c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
20245c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
20255c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
20265c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
20275c1cfac4SBill Paul 	 * driver instead.
202896f2e892SBill Paul 	 */
20295c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
20305c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
20315c1cfac4SBill Paul 		tmp = sc->dc_pmode;
20325c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20335c1cfac4SBill Paul 	}
20345c1cfac4SBill Paul 
203596f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
203696f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
203796f2e892SBill Paul 
203896f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
20395c1cfac4SBill Paul 		sc->dc_pmode = tmp;
20405c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
204196f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2042042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
204396f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
204496f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
204578999dd1SBill Paul 		/*
204678999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
204778999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
204878999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
204978999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
205078999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
205178999dd1SBill Paul 		 */
205278999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
205378999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
205496f2e892SBill Paul 		error = 0;
205596f2e892SBill Paul 	}
205696f2e892SBill Paul 
205796f2e892SBill Paul 	if (error) {
205896f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
205996f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
206096f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
206196f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
206296f2e892SBill Paul 		error = ENXIO;
206396f2e892SBill Paul 		goto fail;
206496f2e892SBill Paul 	}
206596f2e892SBill Paul 
2066feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
2067feb78939SJonathan Chen 		/*
2068feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
2069feb78939SJonathan Chen 		 * can talk to the MII.
2070feb78939SJonathan Chen 		 */
2071feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2072feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2073feb78939SJonathan Chen 		DELAY(10);
2074feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2075feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2076feb78939SJonathan Chen 		DELAY(10);
2077feb78939SJonathan Chen 	}
2078feb78939SJonathan Chen 
207996f2e892SBill Paul 	/*
208021b8ebd9SArchie Cobbs 	 * Call MI attach routine.
208196f2e892SBill Paul 	 */
208221b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
208396f2e892SBill Paul 	callout_handle_init(&sc->dc_stat_ch);
208496f2e892SBill Paul 
20855c1cfac4SBill Paul #ifdef SRM_MEDIA
2086510a809eSMike Smith         sc->dc_srm_media = 0;
2087510a809eSMike Smith 
2088510a809eSMike Smith 	/* Remember the SRM console media setting */
2089510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2090510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2091510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
2092510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2093510a809eSMike Smith 		case 3:
2094510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2095510a809eSMike Smith 			break;
2096510a809eSMike Smith 		case 4:
2097510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2098510a809eSMike Smith 			break;
2099510a809eSMike Smith 		case 5:
2100510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2101510a809eSMike Smith 			break;
2102510a809eSMike Smith 		case 6:
2103510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2104510a809eSMike Smith 			break;
2105510a809eSMike Smith 		}
2106510a809eSMike Smith 		if (sc->dc_srm_media)
2107510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2108510a809eSMike Smith 	}
2109510a809eSMike Smith #endif
2110510a809eSMike Smith 
2111d1ce9105SBill Paul 	DC_UNLOCK(sc);
2112d1ce9105SBill Paul 	return(0);
2113510a809eSMike Smith 
211496f2e892SBill Paul fail:
2115d1ce9105SBill Paul 	DC_UNLOCK(sc);
2116d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
211796f2e892SBill Paul 	return(error);
211896f2e892SBill Paul }
211996f2e892SBill Paul 
212096f2e892SBill Paul static int dc_detach(dev)
212196f2e892SBill Paul 	device_t		dev;
212296f2e892SBill Paul {
212396f2e892SBill Paul 	struct dc_softc		*sc;
212496f2e892SBill Paul 	struct ifnet		*ifp;
21255c1cfac4SBill Paul 	struct dc_mediainfo	*m;
212696f2e892SBill Paul 
212796f2e892SBill Paul 	sc = device_get_softc(dev);
2128d1ce9105SBill Paul 
2129d1ce9105SBill Paul 	DC_LOCK(sc);
2130d1ce9105SBill Paul 
213196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
213296f2e892SBill Paul 
213396f2e892SBill Paul 	dc_stop(sc);
213421b8ebd9SArchie Cobbs 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
213596f2e892SBill Paul 
213696f2e892SBill Paul 	bus_generic_detach(dev);
213796f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
213896f2e892SBill Paul 
213996f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
214096f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
214196f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
214296f2e892SBill Paul 
214396f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
214496f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
214596f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
214696f2e892SBill Paul 
21475c1cfac4SBill Paul 	while(sc->dc_mi != NULL) {
21485c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
21495c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
21505c1cfac4SBill Paul 		sc->dc_mi = m;
21515c1cfac4SBill Paul 	}
21525c1cfac4SBill Paul 
2153d1ce9105SBill Paul 	DC_UNLOCK(sc);
2154d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
215596f2e892SBill Paul 
215696f2e892SBill Paul 	return(0);
215796f2e892SBill Paul }
215896f2e892SBill Paul 
215996f2e892SBill Paul /*
216096f2e892SBill Paul  * Initialize the transmit descriptors.
216196f2e892SBill Paul  */
216296f2e892SBill Paul static int dc_list_tx_init(sc)
216396f2e892SBill Paul 	struct dc_softc		*sc;
216496f2e892SBill Paul {
216596f2e892SBill Paul 	struct dc_chain_data	*cd;
216696f2e892SBill Paul 	struct dc_list_data	*ld;
216796f2e892SBill Paul 	int			i;
216896f2e892SBill Paul 
216996f2e892SBill Paul 	cd = &sc->dc_cdata;
217096f2e892SBill Paul 	ld = sc->dc_ldata;
217196f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
217296f2e892SBill Paul 		if (i == (DC_TX_LIST_CNT - 1)) {
217396f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
217496f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[0]);
217596f2e892SBill Paul 		} else {
217696f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
217796f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[i + 1]);
217896f2e892SBill Paul 		}
217996f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
218096f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
218196f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
218296f2e892SBill Paul 	}
218396f2e892SBill Paul 
218496f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
218596f2e892SBill Paul 
218696f2e892SBill Paul 	return(0);
218796f2e892SBill Paul }
218896f2e892SBill Paul 
218996f2e892SBill Paul 
219096f2e892SBill Paul /*
219196f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
219296f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
219396f2e892SBill Paul  * points back to the first.
219496f2e892SBill Paul  */
219596f2e892SBill Paul static int dc_list_rx_init(sc)
219696f2e892SBill Paul 	struct dc_softc		*sc;
219796f2e892SBill Paul {
219896f2e892SBill Paul 	struct dc_chain_data	*cd;
219996f2e892SBill Paul 	struct dc_list_data	*ld;
220096f2e892SBill Paul 	int			i;
220196f2e892SBill Paul 
220296f2e892SBill Paul 	cd = &sc->dc_cdata;
220396f2e892SBill Paul 	ld = sc->dc_ldata;
220496f2e892SBill Paul 
220596f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
220696f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
220796f2e892SBill Paul 			return(ENOBUFS);
220896f2e892SBill Paul 		if (i == (DC_RX_LIST_CNT - 1)) {
220996f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
221096f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[0]);
221196f2e892SBill Paul 		} else {
221296f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
221396f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[i + 1]);
221496f2e892SBill Paul 		}
221596f2e892SBill Paul 	}
221696f2e892SBill Paul 
221796f2e892SBill Paul 	cd->dc_rx_prod = 0;
221896f2e892SBill Paul 
221996f2e892SBill Paul 	return(0);
222096f2e892SBill Paul }
222196f2e892SBill Paul 
222296f2e892SBill Paul /*
222396f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
222496f2e892SBill Paul  */
222596f2e892SBill Paul static int dc_newbuf(sc, i, m)
222696f2e892SBill Paul 	struct dc_softc		*sc;
222796f2e892SBill Paul 	int			i;
222896f2e892SBill Paul 	struct mbuf		*m;
222996f2e892SBill Paul {
223096f2e892SBill Paul 	struct mbuf		*m_new = NULL;
223196f2e892SBill Paul 	struct dc_desc		*c;
223296f2e892SBill Paul 
223396f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
223496f2e892SBill Paul 
223596f2e892SBill Paul 	if (m == NULL) {
223696f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
223796f2e892SBill Paul 		if (m_new == NULL) {
223896f2e892SBill Paul 			printf("dc%d: no memory for rx list "
223996f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
224096f2e892SBill Paul 			return(ENOBUFS);
224196f2e892SBill Paul 		}
224296f2e892SBill Paul 
224396f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
224496f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
224596f2e892SBill Paul 			printf("dc%d: no memory for rx list "
224696f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
224796f2e892SBill Paul 			m_freem(m_new);
224896f2e892SBill Paul 			return(ENOBUFS);
224996f2e892SBill Paul 		}
225096f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
225196f2e892SBill Paul 	} else {
225296f2e892SBill Paul 		m_new = m;
225396f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
225496f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
225596f2e892SBill Paul 	}
225696f2e892SBill Paul 
225796f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
225896f2e892SBill Paul 
225996f2e892SBill Paul 	/*
226096f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
226196f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
226296f2e892SBill Paul 	 * 82c169 chips.
226396f2e892SBill Paul 	 */
226496f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
226596f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
226696f2e892SBill Paul 
226796f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
226896f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
226996f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
227096f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
227196f2e892SBill Paul 
227296f2e892SBill Paul 	return(0);
227396f2e892SBill Paul }
227496f2e892SBill Paul 
227596f2e892SBill Paul /*
227696f2e892SBill Paul  * Grrrrr.
227796f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
227896f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
227996f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
228096f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
228196f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
228296f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
228396f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
228496f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
228596f2e892SBill Paul  *
228696f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
228796f2e892SBill Paul  * Here's what we know:
228896f2e892SBill Paul  *
228996f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
229096f2e892SBill Paul  *   descriptors uploaded.
229196f2e892SBill Paul  *
229296f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
229396f2e892SBill Paul  *   total data upload.
229496f2e892SBill Paul  *
229596f2e892SBill Paul  * - We know the size of the desired received frame because it will be
229696f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
229796f2e892SBill Paul  *
229896f2e892SBill Paul  * Here's what we do:
229996f2e892SBill Paul  *
230096f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
230196f2e892SBill Paul  *   This means that we know that the buffer contents should be all
230296f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
230396f2e892SBill Paul  *
230496f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
230596f2e892SBill Paul  *   ethernet CRC at the end.
230696f2e892SBill Paul  *
230796f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
230896f2e892SBill Paul  *
230996f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
231096f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
231196f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
231296f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
231396f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
231496f2e892SBill Paul  *   we won't be fooled.
231596f2e892SBill Paul  *
231696f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
231796f2e892SBill Paul  *   that value from the current pointer location. This brings us
231896f2e892SBill Paul  *   to the start of the actual received packet.
231996f2e892SBill Paul  *
232096f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
232196f2e892SBill Paul  *   frame length.
232296f2e892SBill Paul  *
232396f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
232496f2e892SBill Paul  * the time.
232596f2e892SBill Paul  */
232696f2e892SBill Paul 
232796f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
232896f2e892SBill Paul static void dc_pnic_rx_bug_war(sc, idx)
232996f2e892SBill Paul 	struct dc_softc		*sc;
233096f2e892SBill Paul 	int			idx;
233196f2e892SBill Paul {
233296f2e892SBill Paul 	struct dc_desc		*cur_rx;
233396f2e892SBill Paul 	struct dc_desc		*c = NULL;
233496f2e892SBill Paul 	struct mbuf		*m = NULL;
233596f2e892SBill Paul 	unsigned char		*ptr;
233696f2e892SBill Paul 	int			i, total_len;
233796f2e892SBill Paul 	u_int32_t		rxstat = 0;
233896f2e892SBill Paul 
233996f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
234096f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
234196f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
234296f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
234396f2e892SBill Paul 
234496f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
234596f2e892SBill Paul 	while (1) {
234696f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
234796f2e892SBill Paul 		rxstat = c->dc_status;
234896f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
234996f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
235096f2e892SBill Paul 		ptr += DC_RXLEN;
235196f2e892SBill Paul 		/* If this is the last buffer, break out. */
235296f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
235396f2e892SBill Paul 			break;
235496f2e892SBill Paul 		dc_newbuf(sc, i, m);
235596f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
235696f2e892SBill Paul 	}
235796f2e892SBill Paul 
235896f2e892SBill Paul 	/* Find the length of the actual receive frame. */
235996f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
236096f2e892SBill Paul 
236196f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
236296f2e892SBill Paul 	while(*ptr == 0x00)
236396f2e892SBill Paul 		ptr--;
236496f2e892SBill Paul 
236596f2e892SBill Paul 	/* Round off. */
236696f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
236796f2e892SBill Paul 		ptr -= 1;
236896f2e892SBill Paul 
236996f2e892SBill Paul 	/* Now find the start of the frame. */
237096f2e892SBill Paul 	ptr -= total_len;
237196f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
237296f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
237396f2e892SBill Paul 
237496f2e892SBill Paul 	/*
237596f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
237696f2e892SBill Paul 	 * the status word to make it look like a successful
237796f2e892SBill Paul  	 * frame reception.
237896f2e892SBill Paul 	 */
237996f2e892SBill Paul 	dc_newbuf(sc, i, m);
238096f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
238196f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
238296f2e892SBill Paul 
238396f2e892SBill Paul 	return;
238496f2e892SBill Paul }
238596f2e892SBill Paul 
238696f2e892SBill Paul /*
238773bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
238873bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
238973bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
239073bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
239173bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
239273bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
239373bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
239473bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
239573bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
239673bf949cSBill Paul  */
239773bf949cSBill Paul static int dc_rx_resync(sc)
239873bf949cSBill Paul 	struct dc_softc		*sc;
239973bf949cSBill Paul {
240073bf949cSBill Paul 	int			i, pos;
240173bf949cSBill Paul 	struct dc_desc		*cur_rx;
240273bf949cSBill Paul 
240373bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
240473bf949cSBill Paul 
240573bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
240673bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
240773bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
240873bf949cSBill Paul 			break;
240973bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
241073bf949cSBill Paul 	}
241173bf949cSBill Paul 
241273bf949cSBill Paul 	/* If the ring really is empty, then just return. */
241373bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
241473bf949cSBill Paul 		return(0);
241573bf949cSBill Paul 
241673bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
241773bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
241873bf949cSBill Paul 
241973bf949cSBill Paul 	return(EAGAIN);
242073bf949cSBill Paul }
242173bf949cSBill Paul 
242273bf949cSBill Paul /*
242396f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
242496f2e892SBill Paul  * the higher level protocols.
242596f2e892SBill Paul  */
242696f2e892SBill Paul static void dc_rxeof(sc)
242796f2e892SBill Paul 	struct dc_softc		*sc;
242896f2e892SBill Paul {
242996f2e892SBill Paul         struct ether_header	*eh;
243096f2e892SBill Paul         struct mbuf		*m;
243196f2e892SBill Paul         struct ifnet		*ifp;
243296f2e892SBill Paul 	struct dc_desc		*cur_rx;
243396f2e892SBill Paul 	int			i, total_len = 0;
243496f2e892SBill Paul 	u_int32_t		rxstat;
243596f2e892SBill Paul 
243696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
243796f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
243896f2e892SBill Paul 
243996f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
244096f2e892SBill Paul 		struct mbuf		*m0 = NULL;
244196f2e892SBill Paul 
244296f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
244396f2e892SBill Paul 		rxstat = cur_rx->dc_status;
244496f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
244596f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
244696f2e892SBill Paul 
244796f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
244896f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
244996f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
245096f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
245196f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
245296f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
245396f2e892SBill Paul 					continue;
245496f2e892SBill Paul 				}
245596f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
245696f2e892SBill Paul 				rxstat = cur_rx->dc_status;
245796f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
245896f2e892SBill Paul 			}
245996f2e892SBill Paul 		}
246096f2e892SBill Paul 
246196f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
246296f2e892SBill Paul 
246396f2e892SBill Paul 		/*
246496f2e892SBill Paul 		 * If an error occurs, update stats, clear the
246596f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
246696f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
246796f2e892SBill Paul 	 	 * comes up in the ring.
246896f2e892SBill Paul 		 */
246996f2e892SBill Paul 		if (rxstat & DC_RXSTAT_RXERR) {
247096f2e892SBill Paul 			ifp->if_ierrors++;
247196f2e892SBill Paul 			if (rxstat & DC_RXSTAT_COLLSEEN)
247296f2e892SBill Paul 				ifp->if_collisions++;
247396f2e892SBill Paul 			dc_newbuf(sc, i, m);
247496f2e892SBill Paul 			if (rxstat & DC_RXSTAT_CRCERR) {
247596f2e892SBill Paul 				DC_INC(i, DC_RX_LIST_CNT);
247696f2e892SBill Paul 				continue;
247796f2e892SBill Paul 			} else {
247896f2e892SBill Paul 				dc_init(sc);
247996f2e892SBill Paul 				return;
248096f2e892SBill Paul 			}
248196f2e892SBill Paul 		}
248296f2e892SBill Paul 
248396f2e892SBill Paul 		/* No errors; receive the packet. */
248496f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
248596f2e892SBill Paul 
248696f2e892SBill Paul 		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
248796f2e892SBill Paul 		    total_len + ETHER_ALIGN, 0, ifp, NULL);
248896f2e892SBill Paul 		dc_newbuf(sc, i, m);
248996f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
249096f2e892SBill Paul 		if (m0 == NULL) {
249196f2e892SBill Paul 			ifp->if_ierrors++;
249296f2e892SBill Paul 			continue;
249396f2e892SBill Paul 		}
249496f2e892SBill Paul 		m_adj(m0, ETHER_ALIGN);
249596f2e892SBill Paul 		m = m0;
249696f2e892SBill Paul 
249796f2e892SBill Paul 		ifp->if_ipackets++;
249896f2e892SBill Paul 		eh = mtod(m, struct ether_header *);
249996f2e892SBill Paul 
250096f2e892SBill Paul 		/* Remove header from mbuf and pass it on. */
250196f2e892SBill Paul 		m_adj(m, sizeof(struct ether_header));
250296f2e892SBill Paul 		ether_input(ifp, eh, m);
250396f2e892SBill Paul 	}
250496f2e892SBill Paul 
250596f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
250696f2e892SBill Paul }
250796f2e892SBill Paul 
250896f2e892SBill Paul /*
250996f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
251096f2e892SBill Paul  * the list buffers.
251196f2e892SBill Paul  */
251296f2e892SBill Paul 
251396f2e892SBill Paul static void dc_txeof(sc)
251496f2e892SBill Paul 	struct dc_softc		*sc;
251596f2e892SBill Paul {
251696f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
251796f2e892SBill Paul 	struct ifnet		*ifp;
251896f2e892SBill Paul 	int			idx;
251996f2e892SBill Paul 
252096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
252196f2e892SBill Paul 
252296f2e892SBill Paul 	/* Clear the timeout timer. */
252396f2e892SBill Paul 	ifp->if_timer = 0;
252496f2e892SBill Paul 
252596f2e892SBill Paul 	/*
252696f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
252796f2e892SBill Paul 	 * frames that have been transmitted.
252896f2e892SBill Paul 	 */
252996f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
253096f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
253196f2e892SBill Paul 		u_int32_t		txstat;
253296f2e892SBill Paul 
253396f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
253496f2e892SBill Paul 		txstat = cur_tx->dc_status;
253596f2e892SBill Paul 
253696f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
253796f2e892SBill Paul 			break;
253896f2e892SBill Paul 
253996f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
254096f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
254196f2e892SBill Paul 			sc->dc_cdata.dc_tx_cnt--;
254296f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
254396f2e892SBill Paul 				/*
254496f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
254596f2e892SBill Paul 				 * that it will sometimes generate a TX
254696f2e892SBill Paul 				 * underrun error while DMAing the RX
254796f2e892SBill Paul 				 * filter setup frame. If we detect this,
254896f2e892SBill Paul 				 * we have to send the setup frame again,
254996f2e892SBill Paul 				 * or else the filter won't be programmed
255096f2e892SBill Paul 				 * correctly.
255196f2e892SBill Paul 				 */
255296f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
255396f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
255496f2e892SBill Paul 						dc_setfilt(sc);
255596f2e892SBill Paul 				}
255696f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
255796f2e892SBill Paul 			}
255896f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
255996f2e892SBill Paul 			continue;
256096f2e892SBill Paul 		}
256196f2e892SBill Paul 
2562feb78939SJonathan Chen 		if (DC_IS_XIRCOM(sc)) {
2563feb78939SJonathan Chen 			/*
2564feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2565feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
2566feb78939SJonathan Chen 			 * even when the carrier is there. wtf?!? */
2567feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2568feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2569feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
2570feb78939SJonathan Chen 						   DC_TXSTAT_NOCARRIER)))
2571feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2572feb78939SJonathan Chen 		} else {
257396f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
257496f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
257596f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
257696f2e892SBill Paul 						   DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
257796f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2578feb78939SJonathan Chen 		}
257996f2e892SBill Paul 
258096f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
258196f2e892SBill Paul 			ifp->if_oerrors++;
258296f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
258396f2e892SBill Paul 				ifp->if_collisions++;
258496f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
258596f2e892SBill Paul 				ifp->if_collisions++;
258696f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
258796f2e892SBill Paul 				dc_init(sc);
258896f2e892SBill Paul 				return;
258996f2e892SBill Paul 			}
259096f2e892SBill Paul 		}
259196f2e892SBill Paul 
259296f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
259396f2e892SBill Paul 
259496f2e892SBill Paul 		ifp->if_opackets++;
259596f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
259696f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
259796f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
259896f2e892SBill Paul 		}
259996f2e892SBill Paul 
260096f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
260196f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
260296f2e892SBill Paul 	}
260396f2e892SBill Paul 
260496f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
260596f2e892SBill Paul 	if (cur_tx != NULL)
260696f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
260796f2e892SBill Paul 
260896f2e892SBill Paul 	return;
260996f2e892SBill Paul }
261096f2e892SBill Paul 
261196f2e892SBill Paul static void dc_tick(xsc)
261296f2e892SBill Paul 	void			*xsc;
261396f2e892SBill Paul {
261496f2e892SBill Paul 	struct dc_softc		*sc;
261596f2e892SBill Paul 	struct mii_data		*mii;
261696f2e892SBill Paul 	struct ifnet		*ifp;
261796f2e892SBill Paul 	u_int32_t		r;
261896f2e892SBill Paul 
261996f2e892SBill Paul 	sc = xsc;
2620d1ce9105SBill Paul 	DC_LOCK(sc);
262196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
262296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
262396f2e892SBill Paul 
262496f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2625318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2626318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2627318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2628318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
262996f2e892SBill Paul 				sc->dc_link = 0;
2630318b02fdSBill Paul 				mii_mediachg(mii);
2631318b02fdSBill Paul 			}
2632318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2633318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2634318b02fdSBill Paul 				sc->dc_link = 0;
2635318b02fdSBill Paul 				mii_mediachg(mii);
2636318b02fdSBill Paul 			}
2637d675147eSBill Paul 			if (sc->dc_link == 0)
263896f2e892SBill Paul 				mii_tick(mii);
263996f2e892SBill Paul 		} else {
2640318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
264196f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2642042c8f6eSBill Paul 			    sc->dc_cdata.dc_tx_cnt == 0)
264396f2e892SBill Paul 				mii_tick(mii);
2644042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2645042c8f6eSBill Paul 					sc->dc_link = 0;
264696f2e892SBill Paul 		}
264796f2e892SBill Paul 	} else
264896f2e892SBill Paul 		mii_tick(mii);
264996f2e892SBill Paul 
265096f2e892SBill Paul 	/*
265196f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
265296f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
265396f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
265496f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
265596f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
265696f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
265796f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
265896f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
265996f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
266096f2e892SBill Paul 	 * a screeching halt for several seconds.
266196f2e892SBill Paul 	 *
266296f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
266396f2e892SBill Paul 	 * any packets until a link has been established. After the
266496f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
266596f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
266696f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
266796f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
266896f2e892SBill Paul 	 */
266996f2e892SBill Paul 	if (!sc->dc_link) {
267096f2e892SBill Paul 		mii_pollstat(mii);
267196f2e892SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
267296f2e892SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
267396f2e892SBill Paul 			sc->dc_link++;
267496f2e892SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
267596f2e892SBill Paul 				dc_start(ifp);
267696f2e892SBill Paul 		}
267796f2e892SBill Paul 	}
267896f2e892SBill Paul 
2679318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2680318b02fdSBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz/10);
2681318b02fdSBill Paul 	else
268296f2e892SBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz);
268396f2e892SBill Paul 
2684d1ce9105SBill Paul 	DC_UNLOCK(sc);
268596f2e892SBill Paul 
268696f2e892SBill Paul 	return;
268796f2e892SBill Paul }
268896f2e892SBill Paul 
268996f2e892SBill Paul static void dc_intr(arg)
269096f2e892SBill Paul 	void			*arg;
269196f2e892SBill Paul {
269296f2e892SBill Paul 	struct dc_softc		*sc;
269396f2e892SBill Paul 	struct ifnet		*ifp;
269496f2e892SBill Paul 	u_int32_t		status;
269596f2e892SBill Paul 
269696f2e892SBill Paul 	sc = arg;
2697d1ce9105SBill Paul 	DC_LOCK(sc);
269896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
269996f2e892SBill Paul 
270096f2e892SBill Paul 	/* Supress unwanted interrupts */
270196f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
270296f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
270396f2e892SBill Paul 			dc_stop(sc);
2704d1ce9105SBill Paul 		DC_UNLOCK(sc);
270596f2e892SBill Paul 		return;
270696f2e892SBill Paul 	}
270796f2e892SBill Paul 
270896f2e892SBill Paul 	/* Disable interrupts. */
270996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
271096f2e892SBill Paul 
2711feb78939SJonathan Chen 	while(((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
2712feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
271396f2e892SBill Paul 
271496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
271596f2e892SBill Paul 
271673bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
271773bf949cSBill Paul 			int		curpkts;
271873bf949cSBill Paul 			curpkts = ifp->if_ipackets;
271996f2e892SBill Paul 			dc_rxeof(sc);
272073bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
272173bf949cSBill Paul 				while(dc_rx_resync(sc))
272273bf949cSBill Paul 					dc_rxeof(sc);
272373bf949cSBill Paul 			}
272473bf949cSBill Paul 		}
272596f2e892SBill Paul 
272696f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
272796f2e892SBill Paul 			dc_txeof(sc);
272896f2e892SBill Paul 
272996f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
273096f2e892SBill Paul 			dc_txeof(sc);
273196f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
273296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
273396f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
273496f2e892SBill Paul 			}
273596f2e892SBill Paul 		}
273696f2e892SBill Paul 
273796f2e892SBill Paul 		if (status & DC_ISR_TX_UNDERRUN) {
273896f2e892SBill Paul 			u_int32_t		cfg;
273996f2e892SBill Paul 
274096f2e892SBill Paul 			printf("dc%d: TX underrun -- ", sc->dc_unit);
274196f2e892SBill Paul 			if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc))
274296f2e892SBill Paul 				dc_init(sc);
274396f2e892SBill Paul 			cfg = CSR_READ_4(sc, DC_NETCFG);
274496f2e892SBill Paul 			cfg &= ~DC_NETCFG_TX_THRESH;
274596f2e892SBill Paul 			if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
274696f2e892SBill Paul 				printf("using store and forward mode\n");
274796f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
274891cc2adbSBill Paul 			} else if (sc->dc_flags & DC_TX_STORENFWD) {
274991cc2adbSBill Paul 				printf("resetting\n");
275096f2e892SBill Paul 			} else {
275196f2e892SBill Paul 				sc->dc_txthresh += 0x4000;
275296f2e892SBill Paul 				printf("increasing TX threshold\n");
275396f2e892SBill Paul 				CSR_WRITE_4(sc, DC_NETCFG, cfg);
275496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
275596f2e892SBill Paul 				DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
275696f2e892SBill Paul 			}
275796f2e892SBill Paul 		}
275896f2e892SBill Paul 
275996f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
276073bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
276173bf949cSBill Paul 			int		curpkts;
276273bf949cSBill Paul 			curpkts = ifp->if_ipackets;
276396f2e892SBill Paul 			dc_rxeof(sc);
276473bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
276573bf949cSBill Paul 				while(dc_rx_resync(sc))
276673bf949cSBill Paul 					dc_rxeof(sc);
276773bf949cSBill Paul 			}
276873bf949cSBill Paul 		}
276996f2e892SBill Paul 
277096f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
277196f2e892SBill Paul 			dc_reset(sc);
277296f2e892SBill Paul 			dc_init(sc);
277396f2e892SBill Paul 		}
277496f2e892SBill Paul 	}
277596f2e892SBill Paul 
277696f2e892SBill Paul 	/* Re-enable interrupts. */
277796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
277896f2e892SBill Paul 
277996f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
278096f2e892SBill Paul 		dc_start(ifp);
278196f2e892SBill Paul 
2782d1ce9105SBill Paul 	DC_UNLOCK(sc);
2783d1ce9105SBill Paul 
278496f2e892SBill Paul 	return;
278596f2e892SBill Paul }
278696f2e892SBill Paul 
278796f2e892SBill Paul /*
278896f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
278996f2e892SBill Paul  * pointers to the fragment pointers.
279096f2e892SBill Paul  */
279196f2e892SBill Paul static int dc_encap(sc, m_head, txidx)
279296f2e892SBill Paul 	struct dc_softc		*sc;
279396f2e892SBill Paul 	struct mbuf		*m_head;
279496f2e892SBill Paul 	u_int32_t		*txidx;
279596f2e892SBill Paul {
279696f2e892SBill Paul 	struct dc_desc		*f = NULL;
279796f2e892SBill Paul 	struct mbuf		*m;
279896f2e892SBill Paul 	int			frag, cur, cnt = 0;
279996f2e892SBill Paul 
280096f2e892SBill Paul 	/*
280196f2e892SBill Paul  	 * Start packing the mbufs in this chain into
280296f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
280396f2e892SBill Paul  	 * of fragments or hit the end of the mbuf chain.
280496f2e892SBill Paul 	 */
280596f2e892SBill Paul 	m = m_head;
280696f2e892SBill Paul 	cur = frag = *txidx;
280796f2e892SBill Paul 
280896f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
280996f2e892SBill Paul 		if (m->m_len != 0) {
281096f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
281196f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
281296f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
281396f2e892SBill Paul 					return(ENOBUFS);
281496f2e892SBill Paul 			}
281596f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
281696f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
281796f2e892SBill Paul 				return(ENOBUFS);
281896f2e892SBill Paul 
281996f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
282096f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
282196f2e892SBill Paul 			if (cnt == 0) {
282296f2e892SBill Paul 				f->dc_status = 0;
282396f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
282496f2e892SBill Paul 			} else
282596f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
282696f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
282796f2e892SBill Paul 			cur = frag;
282896f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
282996f2e892SBill Paul 			cnt++;
283096f2e892SBill Paul 		}
283196f2e892SBill Paul 	}
283296f2e892SBill Paul 
283396f2e892SBill Paul 	if (m != NULL)
283496f2e892SBill Paul 		return(ENOBUFS);
283596f2e892SBill Paul 
283696f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
283796f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
283896f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
283996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
284096f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
284191cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
284291cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
284396f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
284496f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
284596f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
284696f2e892SBill Paul 	*txidx = frag;
284796f2e892SBill Paul 
284896f2e892SBill Paul 	return(0);
284996f2e892SBill Paul }
285096f2e892SBill Paul 
285196f2e892SBill Paul /*
2852fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
2853fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
2854fda39fd0SBill Paul  * do scatter/gather correctly.
2855fda39fd0SBill Paul  */
2856fda39fd0SBill Paul static int dc_coal(sc, m_head)
2857fda39fd0SBill Paul 	struct dc_softc		*sc;
2858fda39fd0SBill Paul 	struct mbuf		**m_head;
2859fda39fd0SBill Paul {
2860fda39fd0SBill Paul         struct mbuf		*m_new, *m;
2861fda39fd0SBill Paul 
2862fda39fd0SBill Paul 	m = *m_head;
2863fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
2864fda39fd0SBill Paul 	if (m_new == NULL) {
2865fda39fd0SBill Paul 		printf("dc%d: no memory for tx list", sc->dc_unit);
2866fda39fd0SBill Paul 		return(ENOBUFS);
2867fda39fd0SBill Paul 	}
2868fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
2869fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
2870fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
2871fda39fd0SBill Paul 			m_freem(m_new);
2872fda39fd0SBill Paul 			printf("dc%d: no memory for tx list", sc->dc_unit);
2873fda39fd0SBill Paul 			return(ENOBUFS);
2874fda39fd0SBill Paul 		}
2875fda39fd0SBill Paul 	}
2876fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
2877fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
2878fda39fd0SBill Paul 	m_freem(m);
2879fda39fd0SBill Paul 	*m_head = m_new;
2880fda39fd0SBill Paul 
2881fda39fd0SBill Paul 	return(0);
2882fda39fd0SBill Paul }
2883fda39fd0SBill Paul 
2884fda39fd0SBill Paul /*
288596f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
288696f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
288796f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
288896f2e892SBill Paul  * physical addresses.
288996f2e892SBill Paul  */
289096f2e892SBill Paul 
289196f2e892SBill Paul static void dc_start(ifp)
289296f2e892SBill Paul 	struct ifnet		*ifp;
289396f2e892SBill Paul {
289496f2e892SBill Paul 	struct dc_softc		*sc;
289596f2e892SBill Paul 	struct mbuf		*m_head = NULL;
289696f2e892SBill Paul 	int			idx;
289796f2e892SBill Paul 
289896f2e892SBill Paul 	sc = ifp->if_softc;
289996f2e892SBill Paul 
2900d1ce9105SBill Paul 	DC_LOCK(sc);
290196f2e892SBill Paul 
2902d1ce9105SBill Paul 	if (!sc->dc_link) {
2903d1ce9105SBill Paul 		DC_UNLOCK(sc);
290496f2e892SBill Paul 		return;
2905d1ce9105SBill Paul 	}
2906d1ce9105SBill Paul 
2907d1ce9105SBill Paul 	if (ifp->if_flags & IFF_OACTIVE) {
2908d1ce9105SBill Paul 		DC_UNLOCK(sc);
2909d1ce9105SBill Paul 		return;
2910d1ce9105SBill Paul 	}
291196f2e892SBill Paul 
291296f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
291396f2e892SBill Paul 
291496f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
291596f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
291696f2e892SBill Paul 		if (m_head == NULL)
291796f2e892SBill Paul 			break;
291896f2e892SBill Paul 
2919fda39fd0SBill Paul 		if (sc->dc_flags & DC_TX_COALESCE) {
2920fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
2921fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
2922fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
2923fda39fd0SBill Paul 				break;
2924fda39fd0SBill Paul 			}
2925fda39fd0SBill Paul 		}
2926fda39fd0SBill Paul 
292796f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
292896f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
292996f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
293096f2e892SBill Paul 			break;
293196f2e892SBill Paul 		}
293296f2e892SBill Paul 
293396f2e892SBill Paul 		/*
293496f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
293596f2e892SBill Paul 		 * to him.
293696f2e892SBill Paul 		 */
293796f2e892SBill Paul 		if (ifp->if_bpf)
293896f2e892SBill Paul 			bpf_mtap(ifp, m_head);
29395c1cfac4SBill Paul 
29405c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
29415c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
29425c1cfac4SBill Paul 			break;
29435c1cfac4SBill Paul 		}
294496f2e892SBill Paul 	}
294596f2e892SBill Paul 
294696f2e892SBill Paul 	/* Transmit */
294796f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
294896f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
294996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
295096f2e892SBill Paul 
295196f2e892SBill Paul 	/*
295296f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
295396f2e892SBill Paul 	 */
295496f2e892SBill Paul 	ifp->if_timer = 5;
295596f2e892SBill Paul 
2956d1ce9105SBill Paul 	DC_UNLOCK(sc);
2957d1ce9105SBill Paul 
295896f2e892SBill Paul 	return;
295996f2e892SBill Paul }
296096f2e892SBill Paul 
296196f2e892SBill Paul static void dc_init(xsc)
296296f2e892SBill Paul 	void			*xsc;
296396f2e892SBill Paul {
296496f2e892SBill Paul 	struct dc_softc		*sc = xsc;
296596f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
296696f2e892SBill Paul 	struct mii_data		*mii;
296796f2e892SBill Paul 
2968d1ce9105SBill Paul 	DC_LOCK(sc);
296996f2e892SBill Paul 
297096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
297196f2e892SBill Paul 
297296f2e892SBill Paul 	/*
297396f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
297496f2e892SBill Paul 	 */
297596f2e892SBill Paul 	dc_stop(sc);
297696f2e892SBill Paul 	dc_reset(sc);
297796f2e892SBill Paul 
297896f2e892SBill Paul 	/*
297996f2e892SBill Paul 	 * Set cache alignment and burst length.
298096f2e892SBill Paul 	 */
298188d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
298296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
298396f2e892SBill Paul 	else
298496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
298596f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
298696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
298796f2e892SBill Paul 	} else {
298896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
298996f2e892SBill Paul 	}
299096f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
299196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
299296f2e892SBill Paul 	switch(sc->dc_cachesize) {
299396f2e892SBill Paul 	case 32:
299496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
299596f2e892SBill Paul 		break;
299696f2e892SBill Paul 	case 16:
299796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
299896f2e892SBill Paul 		break;
299996f2e892SBill Paul 	case 8:
300096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
300196f2e892SBill Paul 		break;
300296f2e892SBill Paul 	case 0:
300396f2e892SBill Paul 	default:
300496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
300596f2e892SBill Paul 		break;
300696f2e892SBill Paul 	}
300796f2e892SBill Paul 
300896f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
300996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
301096f2e892SBill Paul 	else {
301196f2e892SBill Paul 		if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
301296f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
301396f2e892SBill Paul 		} else {
301496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
301596f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
301696f2e892SBill Paul 		}
301796f2e892SBill Paul 	}
301896f2e892SBill Paul 
301996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
302096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
302196f2e892SBill Paul 
302296f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
302396f2e892SBill Paul 		/*
302496f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
302596f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
302696f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
302796f2e892SBill Paul 		 * document the meaning of these bits so there's no way
302896f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
302996f2e892SBill Paul 		 * number all its own; the rest all use a different one.
303096f2e892SBill Paul 		 */
303196f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
303296f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
303396f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
303496f2e892SBill Paul 		else
303596f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
303696f2e892SBill Paul 	}
303796f2e892SBill Paul 
3038feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3039feb78939SJonathan Chen 		/*
3040feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3041feb78939SJonathan Chen 		 * can talk to the MII.
3042feb78939SJonathan Chen 		 */
3043feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3044feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3045feb78939SJonathan Chen 		DELAY(10);
3046feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3047feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3048feb78939SJonathan Chen 		DELAY(10);
3049feb78939SJonathan Chen 	}
3050feb78939SJonathan Chen 
305196f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
305296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_72BYTES);
305396f2e892SBill Paul 
305496f2e892SBill Paul 	/* Init circular RX list. */
305596f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
305696f2e892SBill Paul 		printf("dc%d: initialization failed: no "
305796f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
305896f2e892SBill Paul 		dc_stop(sc);
3059d1ce9105SBill Paul 		DC_UNLOCK(sc);
306096f2e892SBill Paul 		return;
306196f2e892SBill Paul 	}
306296f2e892SBill Paul 
306396f2e892SBill Paul 	/*
306496f2e892SBill Paul 	 * Init tx descriptors.
306596f2e892SBill Paul 	 */
306696f2e892SBill Paul 	dc_list_tx_init(sc);
306796f2e892SBill Paul 
306896f2e892SBill Paul 	/*
306996f2e892SBill Paul 	 * Load the address of the RX list.
307096f2e892SBill Paul 	 */
307196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
307296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
307396f2e892SBill Paul 
307496f2e892SBill Paul 	/*
307596f2e892SBill Paul 	 * Enable interrupts.
307696f2e892SBill Paul 	 */
307796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
307896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
307996f2e892SBill Paul 
308096f2e892SBill Paul 	/* Enable transmitter. */
308196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
308296f2e892SBill Paul 
308396f2e892SBill Paul 	/*
3084918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3085918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3086918434c8SBill Paul 	 * link and activity indications.
3087918434c8SBill Paul 	 */
308878999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3089918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3090918434c8SBill Paul 		    DC_WDOG_CTLWREN|DC_WDOG_LINK|DC_WDOG_ACTIVITY);
309178999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3092918434c8SBill Paul 	}
3093918434c8SBill Paul 
3094918434c8SBill Paul 	/*
309596f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
309696f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
309796f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
309896f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
309996f2e892SBill Paul 	 */
310096f2e892SBill Paul 	dc_setfilt(sc);
310196f2e892SBill Paul 
310296f2e892SBill Paul 	/* Enable receiver. */
310396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
310496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
310596f2e892SBill Paul 
310696f2e892SBill Paul 	mii_mediachg(mii);
310796f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
310896f2e892SBill Paul 
310996f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
311096f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
311196f2e892SBill Paul 
3112857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
3113857fd445SBill Paul 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_homePNA)
3114857fd445SBill Paul 		sc->dc_link = 1;
3115857fd445SBill Paul 	else {
3116318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3117318b02fdSBill Paul 			sc->dc_stat_ch = timeout(dc_tick, sc, hz/10);
3118318b02fdSBill Paul 		else
311996f2e892SBill Paul 			sc->dc_stat_ch = timeout(dc_tick, sc, hz);
3120857fd445SBill Paul 	}
312196f2e892SBill Paul 
31225c1cfac4SBill Paul #ifdef SRM_MEDIA
3123510a809eSMike Smith         if(sc->dc_srm_media) {
3124510a809eSMike Smith 		struct ifreq ifr;
3125510a809eSMike Smith 
3126510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
3127510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3128510a809eSMike Smith 		sc->dc_srm_media = 0;
3129510a809eSMike Smith 	}
3130510a809eSMike Smith #endif
3131d1ce9105SBill Paul 	DC_UNLOCK(sc);
313296f2e892SBill Paul 	return;
313396f2e892SBill Paul }
313496f2e892SBill Paul 
313596f2e892SBill Paul /*
313696f2e892SBill Paul  * Set media options.
313796f2e892SBill Paul  */
313896f2e892SBill Paul static int dc_ifmedia_upd(ifp)
313996f2e892SBill Paul 	struct ifnet		*ifp;
314096f2e892SBill Paul {
314196f2e892SBill Paul 	struct dc_softc		*sc;
314296f2e892SBill Paul 	struct mii_data		*mii;
3143f43d9309SBill Paul 	struct ifmedia		*ifm;
314496f2e892SBill Paul 
314596f2e892SBill Paul 	sc = ifp->if_softc;
314696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
314796f2e892SBill Paul 	mii_mediachg(mii);
3148f43d9309SBill Paul 	ifm = &mii->mii_media;
3149f43d9309SBill Paul 
3150f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
3151f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA)
3152f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3153f43d9309SBill Paul 	else
315496f2e892SBill Paul 		sc->dc_link = 0;
315596f2e892SBill Paul 
315696f2e892SBill Paul 	return(0);
315796f2e892SBill Paul }
315896f2e892SBill Paul 
315996f2e892SBill Paul /*
316096f2e892SBill Paul  * Report current media status.
316196f2e892SBill Paul  */
316296f2e892SBill Paul static void dc_ifmedia_sts(ifp, ifmr)
316396f2e892SBill Paul 	struct ifnet		*ifp;
316496f2e892SBill Paul 	struct ifmediareq	*ifmr;
316596f2e892SBill Paul {
316696f2e892SBill Paul 	struct dc_softc		*sc;
316796f2e892SBill Paul 	struct mii_data		*mii;
3168f43d9309SBill Paul 	struct ifmedia		*ifm;
316996f2e892SBill Paul 
317096f2e892SBill Paul 	sc = ifp->if_softc;
317196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
317296f2e892SBill Paul 	mii_pollstat(mii);
3173f43d9309SBill Paul 	ifm = &mii->mii_media;
3174f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
3175f43d9309SBill Paul 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
3176f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3177f43d9309SBill Paul 			ifmr->ifm_status = 0;
3178f43d9309SBill Paul 			return;
3179f43d9309SBill Paul 		}
3180f43d9309SBill Paul 	}
318196f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
318296f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
318396f2e892SBill Paul 
318496f2e892SBill Paul 	return;
318596f2e892SBill Paul }
318696f2e892SBill Paul 
318796f2e892SBill Paul static int dc_ioctl(ifp, command, data)
318896f2e892SBill Paul 	struct ifnet		*ifp;
318996f2e892SBill Paul 	u_long			command;
319096f2e892SBill Paul 	caddr_t			data;
319196f2e892SBill Paul {
319296f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
319396f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
319496f2e892SBill Paul 	struct mii_data		*mii;
3195d1ce9105SBill Paul 	int			error = 0;
319696f2e892SBill Paul 
3197d1ce9105SBill Paul 	DC_LOCK(sc);
319896f2e892SBill Paul 
319996f2e892SBill Paul 	switch(command) {
320096f2e892SBill Paul 	case SIOCSIFADDR:
320196f2e892SBill Paul 	case SIOCGIFADDR:
320296f2e892SBill Paul 	case SIOCSIFMTU:
320396f2e892SBill Paul 		error = ether_ioctl(ifp, command, data);
320496f2e892SBill Paul 		break;
320596f2e892SBill Paul 	case SIOCSIFFLAGS:
320696f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
320796f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
320896f2e892SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
320996f2e892SBill Paul 			    !(sc->dc_if_flags & IFF_PROMISC)) {
321096f2e892SBill Paul 				dc_setfilt(sc);
321196f2e892SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
321296f2e892SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
321396f2e892SBill Paul 			    sc->dc_if_flags & IFF_PROMISC) {
321496f2e892SBill Paul 				dc_setfilt(sc);
321596f2e892SBill Paul 			} else if (!(ifp->if_flags & IFF_RUNNING)) {
321696f2e892SBill Paul 				sc->dc_txthresh = 0;
321796f2e892SBill Paul 				dc_init(sc);
321896f2e892SBill Paul 			}
321996f2e892SBill Paul 		} else {
322096f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
322196f2e892SBill Paul 				dc_stop(sc);
322296f2e892SBill Paul 		}
322396f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
322496f2e892SBill Paul 		error = 0;
322596f2e892SBill Paul 		break;
322696f2e892SBill Paul 	case SIOCADDMULTI:
322796f2e892SBill Paul 	case SIOCDELMULTI:
322896f2e892SBill Paul 		dc_setfilt(sc);
322996f2e892SBill Paul 		error = 0;
323096f2e892SBill Paul 		break;
323196f2e892SBill Paul 	case SIOCGIFMEDIA:
323296f2e892SBill Paul 	case SIOCSIFMEDIA:
323396f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
323496f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
32355c1cfac4SBill Paul #ifdef SRM_MEDIA
3236510a809eSMike Smith 		if (sc->dc_srm_media)
3237510a809eSMike Smith 			sc->dc_srm_media = 0;
3238510a809eSMike Smith #endif
323996f2e892SBill Paul 		break;
324096f2e892SBill Paul 	default:
324196f2e892SBill Paul 		error = EINVAL;
324296f2e892SBill Paul 		break;
324396f2e892SBill Paul 	}
324496f2e892SBill Paul 
3245d1ce9105SBill Paul 	DC_UNLOCK(sc);
324696f2e892SBill Paul 
324796f2e892SBill Paul 	return(error);
324896f2e892SBill Paul }
324996f2e892SBill Paul 
325096f2e892SBill Paul static void dc_watchdog(ifp)
325196f2e892SBill Paul 	struct ifnet		*ifp;
325296f2e892SBill Paul {
325396f2e892SBill Paul 	struct dc_softc		*sc;
325496f2e892SBill Paul 
325596f2e892SBill Paul 	sc = ifp->if_softc;
325696f2e892SBill Paul 
3257d1ce9105SBill Paul 	DC_LOCK(sc);
3258d1ce9105SBill Paul 
325996f2e892SBill Paul 	ifp->if_oerrors++;
326096f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
326196f2e892SBill Paul 
326296f2e892SBill Paul 	dc_stop(sc);
326396f2e892SBill Paul 	dc_reset(sc);
326496f2e892SBill Paul 	dc_init(sc);
326596f2e892SBill Paul 
326696f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
326796f2e892SBill Paul 		dc_start(ifp);
326896f2e892SBill Paul 
3269d1ce9105SBill Paul 	DC_UNLOCK(sc);
3270d1ce9105SBill Paul 
327196f2e892SBill Paul 	return;
327296f2e892SBill Paul }
327396f2e892SBill Paul 
327496f2e892SBill Paul /*
327596f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
327696f2e892SBill Paul  * RX and TX lists.
327796f2e892SBill Paul  */
327896f2e892SBill Paul static void dc_stop(sc)
327996f2e892SBill Paul 	struct dc_softc		*sc;
328096f2e892SBill Paul {
328196f2e892SBill Paul 	register int		i;
328296f2e892SBill Paul 	struct ifnet		*ifp;
328396f2e892SBill Paul 
3284d1ce9105SBill Paul 	DC_LOCK(sc);
3285d1ce9105SBill Paul 
328696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
328796f2e892SBill Paul 	ifp->if_timer = 0;
328896f2e892SBill Paul 
328996f2e892SBill Paul 	untimeout(dc_tick, sc, sc->dc_stat_ch);
329096f2e892SBill Paul 
329196f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
329296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
329396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
329496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
329596f2e892SBill Paul 	sc->dc_link = 0;
329696f2e892SBill Paul 
329796f2e892SBill Paul 	/*
329896f2e892SBill Paul 	 * Free data in the RX lists.
329996f2e892SBill Paul 	 */
330096f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
330196f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
330296f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
330396f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
330496f2e892SBill Paul 		}
330596f2e892SBill Paul 	}
330696f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
330796f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
330896f2e892SBill Paul 
330996f2e892SBill Paul 	/*
331096f2e892SBill Paul 	 * Free the TX list buffers.
331196f2e892SBill Paul 	 */
331296f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
331396f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
331496f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
331596f2e892SBill Paul 			    DC_TXCTL_SETUP) {
331696f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
331796f2e892SBill Paul 				continue;
331896f2e892SBill Paul 			}
331996f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
332096f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
332196f2e892SBill Paul 		}
332296f2e892SBill Paul 	}
332396f2e892SBill Paul 
332496f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
332596f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
332696f2e892SBill Paul 
332796f2e892SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
332896f2e892SBill Paul 
3329d1ce9105SBill Paul 	DC_UNLOCK(sc);
3330d1ce9105SBill Paul 
333196f2e892SBill Paul 	return;
333296f2e892SBill Paul }
333396f2e892SBill Paul 
333496f2e892SBill Paul /*
333596f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
333696f2e892SBill Paul  * get confused by errant DMAs when rebooting.
333796f2e892SBill Paul  */
333896f2e892SBill Paul static void dc_shutdown(dev)
333996f2e892SBill Paul 	device_t		dev;
334096f2e892SBill Paul {
334196f2e892SBill Paul 	struct dc_softc		*sc;
334296f2e892SBill Paul 
334396f2e892SBill Paul 	sc = device_get_softc(dev);
334496f2e892SBill Paul 
334596f2e892SBill Paul 	dc_stop(sc);
334696f2e892SBill Paul 
334796f2e892SBill Paul 	return;
334896f2e892SBill Paul }
3349