xref: /freebsd/sys/dev/dc/if_dc.c (revision 5c1cfac46e47ae5f83346be09703ae711e47b4a4)
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)
4896f2e892SBill Paul  *
4996f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5096f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5196f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5296f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5396f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
5496f2e892SBill Paul  * instead of 512.
5596f2e892SBill Paul  *
5696f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
5796f2e892SBill Paul  * Electrical Engineering Department
5896f2e892SBill Paul  * Columbia University, New York City
5996f2e892SBill Paul  */
6096f2e892SBill Paul 
6196f2e892SBill Paul /*
6296f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6396f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6496f2e892SBill Paul  * three kinds of media attachments:
6596f2e892SBill Paul  *
6696f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
6796f2e892SBill Paul  *   autonegotiation provided by an external PHY.
6896f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
6996f2e892SBill Paul  * o 10baseT port.
7096f2e892SBill Paul  * o AUI/BNC port.
7196f2e892SBill Paul  *
7296f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7396f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7496f2e892SBill Paul  * autosensing configuration.
7596f2e892SBill Paul  *
7696f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
7796f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
7896f2e892SBill Paul  * handled separately due to its different register offsets and the
7996f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8096f2e892SBill Paul  * here, but I'm not thrilled about it.
8196f2e892SBill Paul  *
8296f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8396f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8496f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
8596f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
8696f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
8796f2e892SBill Paul  */
8896f2e892SBill Paul 
8996f2e892SBill Paul #include <sys/param.h>
9096f2e892SBill Paul #include <sys/systm.h>
9196f2e892SBill Paul #include <sys/sockio.h>
9296f2e892SBill Paul #include <sys/mbuf.h>
9396f2e892SBill Paul #include <sys/malloc.h>
9496f2e892SBill Paul #include <sys/kernel.h>
9596f2e892SBill Paul #include <sys/socket.h>
9696f2e892SBill Paul 
9796f2e892SBill Paul #include <net/if.h>
9896f2e892SBill Paul #include <net/if_arp.h>
9996f2e892SBill Paul #include <net/ethernet.h>
10096f2e892SBill Paul #include <net/if_dl.h>
10196f2e892SBill Paul #include <net/if_media.h>
10296f2e892SBill Paul 
10396f2e892SBill Paul #include <net/bpf.h>
10496f2e892SBill Paul 
10596f2e892SBill Paul #include <vm/vm.h>              /* for vtophys */
10696f2e892SBill Paul #include <vm/pmap.h>            /* for vtophys */
10796f2e892SBill Paul #include <machine/clock.h>      /* for DELAY */
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>
11296f2e892SBill Paul #include <sys/bus.h>
11396f2e892SBill Paul #include <sys/rman.h>
11496f2e892SBill Paul 
11596f2e892SBill Paul #include <dev/mii/mii.h>
11696f2e892SBill Paul #include <dev/mii/miivar.h>
11796f2e892SBill Paul 
11896f2e892SBill Paul #include <pci/pcireg.h>
11996f2e892SBill Paul #include <pci/pcivar.h>
12096f2e892SBill Paul 
12196f2e892SBill Paul #define DC_USEIOSPACE
1225c1cfac4SBill Paul #ifdef __alpha__
1235c1cfac4SBill Paul #define SRM_MEDIA
1245c1cfac4SBill Paul #endif
12596f2e892SBill Paul 
12696f2e892SBill Paul #include <pci/if_dcreg.h>
12796f2e892SBill Paul 
12895a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
12995a16455SPeter Wemm 
13096f2e892SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
13196f2e892SBill Paul #include "miibus_if.h"
13296f2e892SBill Paul 
13396f2e892SBill Paul #ifndef lint
13496f2e892SBill Paul static const char rcsid[] =
13596f2e892SBill Paul   "$FreeBSD$";
13696f2e892SBill Paul #endif
13796f2e892SBill Paul 
13896f2e892SBill Paul /*
13996f2e892SBill Paul  * Various supported device vendors/types and their names.
14096f2e892SBill Paul  */
14196f2e892SBill Paul static struct dc_type dc_devs[] = {
14296f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
14396f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
14496f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
14596f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
14696f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
14796f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
14888d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
14988d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
15096f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
15196f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
15296f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
15396f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
15496f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
15596f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
15696f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
15796f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
15896f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
15996f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
16096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16196f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
16296f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
16396f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
16496f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
16596f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
16696f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
16796f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
16896f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
16979d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
17079d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17196f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
172ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
173ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
17496f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
17596f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
17696f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
17796f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
17896f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
17996f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1809ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1819ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
18296f2e892SBill Paul 	{ 0, 0, NULL }
18396f2e892SBill Paul };
18496f2e892SBill Paul 
18596f2e892SBill Paul static int dc_probe		__P((device_t));
18696f2e892SBill Paul static int dc_attach		__P((device_t));
18796f2e892SBill Paul static int dc_detach		__P((device_t));
18896f2e892SBill Paul static void dc_acpi		__P((device_t));
18996f2e892SBill Paul static struct dc_type *dc_devtype	__P((device_t));
19096f2e892SBill Paul static int dc_newbuf		__P((struct dc_softc *, int, struct mbuf *));
19196f2e892SBill Paul static int dc_encap		__P((struct dc_softc *, struct mbuf *,
19296f2e892SBill Paul 					u_int32_t *));
193fda39fd0SBill Paul static int dc_coal		__P((struct dc_softc *, struct mbuf **));
19496f2e892SBill Paul static void dc_pnic_rx_bug_war	__P((struct dc_softc *, int));
19573bf949cSBill Paul static int dc_rx_resync		__P((struct dc_softc *));
19696f2e892SBill Paul static void dc_rxeof		__P((struct dc_softc *));
19796f2e892SBill Paul static void dc_txeof		__P((struct dc_softc *));
19896f2e892SBill Paul static void dc_tick		__P((void *));
19996f2e892SBill Paul static void dc_intr		__P((void *));
20096f2e892SBill Paul static void dc_start		__P((struct ifnet *));
20196f2e892SBill Paul static int dc_ioctl		__P((struct ifnet *, u_long, caddr_t));
20296f2e892SBill Paul static void dc_init		__P((void *));
20396f2e892SBill Paul static void dc_stop		__P((struct dc_softc *));
20496f2e892SBill Paul static void dc_watchdog		__P((struct ifnet *));
20596f2e892SBill Paul static void dc_shutdown		__P((device_t));
20696f2e892SBill Paul static int dc_ifmedia_upd	__P((struct ifnet *));
20796f2e892SBill Paul static void dc_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
20896f2e892SBill Paul 
20996f2e892SBill Paul static void dc_delay		__P((struct dc_softc *));
21096f2e892SBill Paul static void dc_eeprom_idle	__P((struct dc_softc *));
21196f2e892SBill Paul static void dc_eeprom_putbyte	__P((struct dc_softc *, int));
21296f2e892SBill Paul static void dc_eeprom_getword	__P((struct dc_softc *, int, u_int16_t *));
21396f2e892SBill Paul static void dc_eeprom_getword_pnic
21496f2e892SBill Paul 				__P((struct dc_softc *, int, u_int16_t *));
21596f2e892SBill Paul static void dc_read_eeprom	__P((struct dc_softc *, caddr_t, int,
21696f2e892SBill Paul 							int, int));
21796f2e892SBill Paul 
21896f2e892SBill Paul static void dc_mii_writebit	__P((struct dc_softc *, int));
21996f2e892SBill Paul static int dc_mii_readbit	__P((struct dc_softc *));
22096f2e892SBill Paul static void dc_mii_sync		__P((struct dc_softc *));
22196f2e892SBill Paul static void dc_mii_send		__P((struct dc_softc *, u_int32_t, int));
22296f2e892SBill Paul static int dc_mii_readreg	__P((struct dc_softc *, struct dc_mii_frame *));
22396f2e892SBill Paul static int dc_mii_writereg	__P((struct dc_softc *, struct dc_mii_frame *));
22496f2e892SBill Paul static int dc_miibus_readreg	__P((device_t, int, int));
22596f2e892SBill Paul static int dc_miibus_writereg	__P((device_t, int, int, int));
22696f2e892SBill Paul static void dc_miibus_statchg	__P((device_t));
227f43d9309SBill Paul static void dc_miibus_mediainit	__P((device_t));
22896f2e892SBill Paul 
22996f2e892SBill Paul static void dc_setcfg		__P((struct dc_softc *, int));
23096f2e892SBill Paul static u_int32_t dc_crc_le	__P((struct dc_softc *, caddr_t));
23196f2e892SBill Paul static u_int32_t dc_crc_be	__P((caddr_t));
23296f2e892SBill Paul static void dc_setfilt_21143	__P((struct dc_softc *));
23396f2e892SBill Paul static void dc_setfilt_asix	__P((struct dc_softc *));
23496f2e892SBill Paul static void dc_setfilt_admtek	__P((struct dc_softc *));
23596f2e892SBill Paul 
23696f2e892SBill Paul static void dc_setfilt		__P((struct dc_softc *));
23796f2e892SBill Paul 
23896f2e892SBill Paul static void dc_reset		__P((struct dc_softc *));
23996f2e892SBill Paul static int dc_list_rx_init	__P((struct dc_softc *));
24096f2e892SBill Paul static int dc_list_tx_init	__P((struct dc_softc *));
24196f2e892SBill Paul 
2425c1cfac4SBill Paul static void dc_parse_21143_srom	__P((struct dc_softc *));
2435c1cfac4SBill Paul static void dc_decode_leaf_sia	__P((struct dc_softc *,
2445c1cfac4SBill Paul 				    struct dc_eblock_sia *));
2455c1cfac4SBill Paul static void dc_decode_leaf_mii	__P((struct dc_softc *,
2465c1cfac4SBill Paul 				    struct dc_eblock_mii *));
2475c1cfac4SBill Paul static void dc_decode_leaf_sym	__P((struct dc_softc *,
2485c1cfac4SBill Paul 				    struct dc_eblock_sym *));
2495c1cfac4SBill Paul static void dc_apply_fixup	__P((struct dc_softc *, int));
2505c1cfac4SBill Paul 
25196f2e892SBill Paul #ifdef DC_USEIOSPACE
25296f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
25396f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
25496f2e892SBill Paul #else
25596f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
25696f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
25796f2e892SBill Paul #endif
25896f2e892SBill Paul 
25996f2e892SBill Paul static device_method_t dc_methods[] = {
26096f2e892SBill Paul 	/* Device interface */
26196f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
26296f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
26396f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
26496f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
26596f2e892SBill Paul 
26696f2e892SBill Paul 	/* bus interface */
26796f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
26896f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
26996f2e892SBill Paul 
27096f2e892SBill Paul 	/* MII interface */
27196f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
27296f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
27396f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
274f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
27596f2e892SBill Paul 
27696f2e892SBill Paul 	{ 0, 0 }
27796f2e892SBill Paul };
27896f2e892SBill Paul 
27996f2e892SBill Paul static driver_t dc_driver = {
28096f2e892SBill Paul 	"dc",
28196f2e892SBill Paul 	dc_methods,
28296f2e892SBill Paul 	sizeof(struct dc_softc)
28396f2e892SBill Paul };
28496f2e892SBill Paul 
28596f2e892SBill Paul static devclass_t dc_devclass;
28696f2e892SBill Paul 
28796f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
28896f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
28996f2e892SBill Paul 
29096f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
29196f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
29296f2e892SBill Paul 
29396f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
29496f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
29596f2e892SBill Paul 
29696f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
29796f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
29896f2e892SBill Paul 
29996f2e892SBill Paul static void dc_delay(sc)
30096f2e892SBill Paul 	struct dc_softc		*sc;
30196f2e892SBill Paul {
30296f2e892SBill Paul 	int			idx;
30396f2e892SBill Paul 
30496f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
30596f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
30696f2e892SBill Paul }
30796f2e892SBill Paul 
30896f2e892SBill Paul static void dc_eeprom_idle(sc)
30996f2e892SBill Paul 	struct dc_softc		*sc;
31096f2e892SBill Paul {
31196f2e892SBill Paul 	register int		i;
31296f2e892SBill Paul 
31396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
31496f2e892SBill Paul 	dc_delay(sc);
31596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
31696f2e892SBill Paul 	dc_delay(sc);
31796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
31896f2e892SBill Paul 	dc_delay(sc);
31996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
32096f2e892SBill Paul 	dc_delay(sc);
32196f2e892SBill Paul 
32296f2e892SBill Paul 	for (i = 0; i < 25; i++) {
32396f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
32496f2e892SBill Paul 		dc_delay(sc);
32596f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
32696f2e892SBill Paul 		dc_delay(sc);
32796f2e892SBill Paul 	}
32896f2e892SBill Paul 
32996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33096f2e892SBill Paul 	dc_delay(sc);
33196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
33296f2e892SBill Paul 	dc_delay(sc);
33396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
33496f2e892SBill Paul 
33596f2e892SBill Paul 	return;
33696f2e892SBill Paul }
33796f2e892SBill Paul 
33896f2e892SBill Paul /*
33996f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
34096f2e892SBill Paul  */
34196f2e892SBill Paul static void dc_eeprom_putbyte(sc, addr)
34296f2e892SBill Paul 	struct dc_softc		*sc;
34396f2e892SBill Paul 	int			addr;
34496f2e892SBill Paul {
34596f2e892SBill Paul 	register int		d, i;
34696f2e892SBill Paul 
34796f2e892SBill Paul 	/*
34896f2e892SBill Paul 	 * The AN985 has a 93C66 EEPROM on it instead of
34996f2e892SBill Paul 	 * a 93C46. It uses a different bit sequence for
35096f2e892SBill Paul 	 * specifying the "read" opcode.
35196f2e892SBill Paul 	 */
35296f2e892SBill Paul 	if (DC_IS_CENTAUR(sc))
35396f2e892SBill Paul 		d = addr | (DC_EECMD_READ << 2);
35496f2e892SBill Paul 	else
35596f2e892SBill Paul 		d = addr | DC_EECMD_READ;
35696f2e892SBill Paul 
35796f2e892SBill Paul 	/*
35896f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
35996f2e892SBill Paul 	 */
36096f2e892SBill Paul 	for (i = 0x400; i; i >>= 1) {
36196f2e892SBill Paul 		if (d & i) {
36296f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
36396f2e892SBill Paul 		} else {
36496f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
36596f2e892SBill Paul 		}
36696f2e892SBill Paul 		dc_delay(sc);
36796f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
36896f2e892SBill Paul 		dc_delay(sc);
36996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
37096f2e892SBill Paul 		dc_delay(sc);
37196f2e892SBill Paul 	}
37296f2e892SBill Paul 
37396f2e892SBill Paul 	return;
37496f2e892SBill Paul }
37596f2e892SBill Paul 
37696f2e892SBill Paul /*
37796f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
37896f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
37996f2e892SBill Paul  * the EEPROM.
38096f2e892SBill Paul  */
38196f2e892SBill Paul static void dc_eeprom_getword_pnic(sc, addr, dest)
38296f2e892SBill Paul 	struct dc_softc		*sc;
38396f2e892SBill Paul 	int			addr;
38496f2e892SBill Paul 	u_int16_t		*dest;
38596f2e892SBill Paul {
38696f2e892SBill Paul 	register int		i;
38796f2e892SBill Paul 	u_int32_t		r;
38896f2e892SBill Paul 
38996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
39096f2e892SBill Paul 
39196f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
39296f2e892SBill Paul 		DELAY(1);
39396f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
39496f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
39596f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
39696f2e892SBill Paul 			return;
39796f2e892SBill Paul 		}
39896f2e892SBill Paul 	}
39996f2e892SBill Paul 
40096f2e892SBill Paul 	return;
40196f2e892SBill Paul }
40296f2e892SBill Paul 
40396f2e892SBill Paul /*
40496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
40596f2e892SBill Paul  */
40696f2e892SBill Paul static void dc_eeprom_getword(sc, addr, dest)
40796f2e892SBill Paul 	struct dc_softc		*sc;
40896f2e892SBill Paul 	int			addr;
40996f2e892SBill Paul 	u_int16_t		*dest;
41096f2e892SBill Paul {
41196f2e892SBill Paul 	register int		i;
41296f2e892SBill Paul 	u_int16_t		word = 0;
41396f2e892SBill Paul 
41496f2e892SBill Paul 	/* Force EEPROM to idle state. */
41596f2e892SBill Paul 	dc_eeprom_idle(sc);
41696f2e892SBill Paul 
41796f2e892SBill Paul 	/* Enter EEPROM access mode. */
41896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
41996f2e892SBill Paul 	dc_delay(sc);
42096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
42196f2e892SBill Paul 	dc_delay(sc);
42296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42396f2e892SBill Paul 	dc_delay(sc);
42496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
42596f2e892SBill Paul 	dc_delay(sc);
42696f2e892SBill Paul 
42796f2e892SBill Paul 	/*
42896f2e892SBill Paul 	 * Send address of word we want to read.
42996f2e892SBill Paul 	 */
43096f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
43196f2e892SBill Paul 
43296f2e892SBill Paul 	/*
43396f2e892SBill Paul 	 * Start reading bits from EEPROM.
43496f2e892SBill Paul 	 */
43596f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
43696f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
43796f2e892SBill Paul 		dc_delay(sc);
43896f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
43996f2e892SBill Paul 			word |= i;
44096f2e892SBill Paul 		dc_delay(sc);
44196f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
44296f2e892SBill Paul 		dc_delay(sc);
44396f2e892SBill Paul 	}
44496f2e892SBill Paul 
44596f2e892SBill Paul 	/* Turn off EEPROM access mode. */
44696f2e892SBill Paul 	dc_eeprom_idle(sc);
44796f2e892SBill Paul 
44896f2e892SBill Paul 	*dest = word;
44996f2e892SBill Paul 
45096f2e892SBill Paul 	return;
45196f2e892SBill Paul }
45296f2e892SBill Paul 
45396f2e892SBill Paul /*
45496f2e892SBill Paul  * Read a sequence of words from the EEPROM.
45596f2e892SBill Paul  */
45696f2e892SBill Paul static void dc_read_eeprom(sc, dest, off, cnt, swap)
45796f2e892SBill Paul 	struct dc_softc		*sc;
45896f2e892SBill Paul 	caddr_t			dest;
45996f2e892SBill Paul 	int			off;
46096f2e892SBill Paul 	int			cnt;
46196f2e892SBill Paul 	int			swap;
46296f2e892SBill Paul {
46396f2e892SBill Paul 	int			i;
46496f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
46596f2e892SBill Paul 
46696f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
46796f2e892SBill Paul 		if (DC_IS_PNIC(sc))
46896f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
46996f2e892SBill Paul 		else
47096f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
47196f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
47296f2e892SBill Paul 		if (swap)
47396f2e892SBill Paul 			*ptr = ntohs(word);
47496f2e892SBill Paul 		else
47596f2e892SBill Paul 			*ptr = word;
47696f2e892SBill Paul 	}
47796f2e892SBill Paul 
47896f2e892SBill Paul 	return;
47996f2e892SBill Paul }
48096f2e892SBill Paul 
48196f2e892SBill Paul /*
48296f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
48396f2e892SBill Paul  * Application Notes pp.19-21.
48496f2e892SBill Paul  */
48596f2e892SBill Paul /*
48696f2e892SBill Paul  * Write a bit to the MII bus.
48796f2e892SBill Paul  */
48896f2e892SBill Paul static void dc_mii_writebit(sc, bit)
48996f2e892SBill Paul 	struct dc_softc		*sc;
49096f2e892SBill Paul 	int			bit;
49196f2e892SBill Paul {
49296f2e892SBill Paul 	if (bit)
49396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
49496f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
49596f2e892SBill Paul 	else
49696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
49796f2e892SBill Paul 
49896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
49996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
50096f2e892SBill Paul 
50196f2e892SBill Paul 	return;
50296f2e892SBill Paul }
50396f2e892SBill Paul 
50496f2e892SBill Paul /*
50596f2e892SBill Paul  * Read a bit from the MII bus.
50696f2e892SBill Paul  */
50796f2e892SBill Paul static int dc_mii_readbit(sc)
50896f2e892SBill Paul 	struct dc_softc		*sc;
50996f2e892SBill Paul {
51096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
51196f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
51296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
51396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
51496f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
51596f2e892SBill Paul 		return(1);
51696f2e892SBill Paul 
51796f2e892SBill Paul 	return(0);
51896f2e892SBill Paul }
51996f2e892SBill Paul 
52096f2e892SBill Paul /*
52196f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
52296f2e892SBill Paul  */
52396f2e892SBill Paul static void dc_mii_sync(sc)
52496f2e892SBill Paul 	struct dc_softc		*sc;
52596f2e892SBill Paul {
52696f2e892SBill Paul 	register int		i;
52796f2e892SBill Paul 
52896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
52996f2e892SBill Paul 
53096f2e892SBill Paul 	for (i = 0; i < 32; i++)
53196f2e892SBill Paul 		dc_mii_writebit(sc, 1);
53296f2e892SBill Paul 
53396f2e892SBill Paul 	return;
53496f2e892SBill Paul }
53596f2e892SBill Paul 
53696f2e892SBill Paul /*
53796f2e892SBill Paul  * Clock a series of bits through the MII.
53896f2e892SBill Paul  */
53996f2e892SBill Paul static void dc_mii_send(sc, bits, cnt)
54096f2e892SBill Paul 	struct dc_softc		*sc;
54196f2e892SBill Paul 	u_int32_t		bits;
54296f2e892SBill Paul 	int			cnt;
54396f2e892SBill Paul {
54496f2e892SBill Paul 	int			i;
54596f2e892SBill Paul 
54696f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
54796f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
54896f2e892SBill Paul }
54996f2e892SBill Paul 
55096f2e892SBill Paul /*
55196f2e892SBill Paul  * Read an PHY register through the MII.
55296f2e892SBill Paul  */
55396f2e892SBill Paul static int dc_mii_readreg(sc, frame)
55496f2e892SBill Paul 	struct dc_softc		*sc;
55596f2e892SBill Paul 	struct dc_mii_frame	*frame;
55696f2e892SBill Paul 
55796f2e892SBill Paul {
55896f2e892SBill Paul 	int			i, ack, s;
55996f2e892SBill Paul 
56096f2e892SBill Paul 	s = splimp();
56196f2e892SBill Paul 
56296f2e892SBill Paul 	/*
56396f2e892SBill Paul 	 * Set up frame for RX.
56496f2e892SBill Paul 	 */
56596f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
56696f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
56796f2e892SBill Paul 	frame->mii_turnaround = 0;
56896f2e892SBill Paul 	frame->mii_data = 0;
56996f2e892SBill Paul 
57096f2e892SBill Paul 	/*
57196f2e892SBill Paul 	 * Sync the PHYs.
57296f2e892SBill Paul 	 */
57396f2e892SBill Paul 	dc_mii_sync(sc);
57496f2e892SBill Paul 
57596f2e892SBill Paul 	/*
57696f2e892SBill Paul 	 * Send command/address info.
57796f2e892SBill Paul 	 */
57896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
57996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
58096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
58196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
58296f2e892SBill Paul 
58396f2e892SBill Paul #ifdef notdef
58496f2e892SBill Paul 	/* Idle bit */
58596f2e892SBill Paul 	dc_mii_writebit(sc, 1);
58696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
58796f2e892SBill Paul #endif
58896f2e892SBill Paul 
58996f2e892SBill Paul 	/* Check for ack */
59096f2e892SBill Paul 	ack = dc_mii_readbit(sc);
59196f2e892SBill Paul 
59296f2e892SBill Paul 	/*
59396f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
59496f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
59596f2e892SBill Paul 	 */
59696f2e892SBill Paul 	if (ack) {
59796f2e892SBill Paul 		for(i = 0; i < 16; i++) {
59896f2e892SBill Paul 			dc_mii_readbit(sc);
59996f2e892SBill Paul 		}
60096f2e892SBill Paul 		goto fail;
60196f2e892SBill Paul 	}
60296f2e892SBill Paul 
60396f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
60496f2e892SBill Paul 		if (!ack) {
60596f2e892SBill Paul 			if (dc_mii_readbit(sc))
60696f2e892SBill Paul 				frame->mii_data |= i;
60796f2e892SBill Paul 		}
60896f2e892SBill Paul 	}
60996f2e892SBill Paul 
61096f2e892SBill Paul fail:
61196f2e892SBill Paul 
61296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
61396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
61496f2e892SBill Paul 
61596f2e892SBill Paul 	splx(s);
61696f2e892SBill Paul 
61796f2e892SBill Paul 	if (ack)
61896f2e892SBill Paul 		return(1);
61996f2e892SBill Paul 	return(0);
62096f2e892SBill Paul }
62196f2e892SBill Paul 
62296f2e892SBill Paul /*
62396f2e892SBill Paul  * Write to a PHY register through the MII.
62496f2e892SBill Paul  */
62596f2e892SBill Paul static int dc_mii_writereg(sc, frame)
62696f2e892SBill Paul 	struct dc_softc		*sc;
62796f2e892SBill Paul 	struct dc_mii_frame	*frame;
62896f2e892SBill Paul 
62996f2e892SBill Paul {
63096f2e892SBill Paul 	int			s;
63196f2e892SBill Paul 
63296f2e892SBill Paul 	s = splimp();
63396f2e892SBill Paul 	/*
63496f2e892SBill Paul 	 * Set up frame for TX.
63596f2e892SBill Paul 	 */
63696f2e892SBill Paul 
63796f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
63896f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
63996f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
64096f2e892SBill Paul 
64196f2e892SBill Paul 	/*
64296f2e892SBill Paul 	 * Sync the PHYs.
64396f2e892SBill Paul 	 */
64496f2e892SBill Paul 	dc_mii_sync(sc);
64596f2e892SBill Paul 
64696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
64796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
64896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
64996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
65096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
65196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
65296f2e892SBill Paul 
65396f2e892SBill Paul 	/* Idle bit. */
65496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
65596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
65696f2e892SBill Paul 
65796f2e892SBill Paul 	splx(s);
65896f2e892SBill Paul 
65996f2e892SBill Paul 	return(0);
66096f2e892SBill Paul }
66196f2e892SBill Paul 
66296f2e892SBill Paul static int dc_miibus_readreg(dev, phy, reg)
66396f2e892SBill Paul 	device_t		dev;
66496f2e892SBill Paul 	int			phy, reg;
66596f2e892SBill Paul {
66696f2e892SBill Paul 	struct dc_mii_frame	frame;
66796f2e892SBill Paul 	struct dc_softc		*sc;
66896f2e892SBill Paul 	int			i, rval, phy_reg;
66996f2e892SBill Paul 
67096f2e892SBill Paul 	sc = device_get_softc(dev);
67196f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
67296f2e892SBill Paul 
67396f2e892SBill Paul 	/*
67496f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
67596f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
67696f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
67796f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
67896f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
67996f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
68096f2e892SBill Paul 	 * that the PHY is at MII address 1.
68196f2e892SBill Paul 	 */
68296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
68396f2e892SBill Paul 		return(0);
68496f2e892SBill Paul 
6855c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
68696f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
68796f2e892SBill Paul 			switch(reg) {
68896f2e892SBill Paul 			case MII_BMSR:
68996f2e892SBill Paul 			/*
69096f2e892SBill Paul 			 * Fake something to make the probe
69196f2e892SBill Paul 			 * code think there's a PHY here.
69296f2e892SBill Paul 			 */
69396f2e892SBill Paul 				return(BMSR_MEDIAMASK);
69496f2e892SBill Paul 				break;
69596f2e892SBill Paul 			case MII_PHYIDR1:
69696f2e892SBill Paul 				if (DC_IS_PNIC(sc))
69796f2e892SBill Paul 					return(DC_VENDORID_LO);
69896f2e892SBill Paul 				return(DC_VENDORID_DEC);
69996f2e892SBill Paul 				break;
70096f2e892SBill Paul 			case MII_PHYIDR2:
70196f2e892SBill Paul 				if (DC_IS_PNIC(sc))
70296f2e892SBill Paul 					return(DC_DEVICEID_82C168);
70396f2e892SBill Paul 				return(DC_DEVICEID_21143);
70496f2e892SBill Paul 				break;
70596f2e892SBill Paul 			default:
70696f2e892SBill Paul 				return(0);
70796f2e892SBill Paul 				break;
70896f2e892SBill Paul 			}
70996f2e892SBill Paul 		} else
71096f2e892SBill Paul 			return(0);
71196f2e892SBill Paul 	}
71296f2e892SBill Paul 
71396f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
71496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
71596f2e892SBill Paul 		    (phy << 23) | (reg << 18));
71696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
71796f2e892SBill Paul 			DELAY(1);
71896f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
71996f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
72096f2e892SBill Paul 				rval &= 0xFFFF;
72196f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
72296f2e892SBill Paul 			}
72396f2e892SBill Paul 		}
72496f2e892SBill Paul 		return(0);
72596f2e892SBill Paul 	}
72696f2e892SBill Paul 
72796f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
72896f2e892SBill Paul 		switch(reg) {
72996f2e892SBill Paul 		case MII_BMCR:
73096f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
73196f2e892SBill Paul 			break;
73296f2e892SBill Paul 		case MII_BMSR:
73396f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
73496f2e892SBill Paul 			break;
73596f2e892SBill Paul 		case MII_PHYIDR1:
73696f2e892SBill Paul 			phy_reg = DC_AL_VENID;
73796f2e892SBill Paul 			break;
73896f2e892SBill Paul 		case MII_PHYIDR2:
73996f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
74096f2e892SBill Paul 			break;
74196f2e892SBill Paul 		case MII_ANAR:
74296f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
74396f2e892SBill Paul 			break;
74496f2e892SBill Paul 		case MII_ANLPAR:
74596f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
74696f2e892SBill Paul 			break;
74796f2e892SBill Paul 		case MII_ANER:
74896f2e892SBill Paul 			phy_reg = DC_AL_ANER;
74996f2e892SBill Paul 			break;
75096f2e892SBill Paul 		default:
75196f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
75296f2e892SBill Paul 			    sc->dc_unit, reg);
75396f2e892SBill Paul 			return(0);
75496f2e892SBill Paul 			break;
75596f2e892SBill Paul 		}
75696f2e892SBill Paul 
75796f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
75896f2e892SBill Paul 
75996f2e892SBill Paul 		if (rval == 0xFFFF)
76096f2e892SBill Paul 			return(0);
76196f2e892SBill Paul 		return(rval);
76296f2e892SBill Paul 	}
76396f2e892SBill Paul 
76496f2e892SBill Paul 	frame.mii_phyaddr = phy;
76596f2e892SBill Paul 	frame.mii_regaddr = reg;
766f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
767f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
76896f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
769f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
77096f2e892SBill Paul 
77196f2e892SBill Paul 	return(frame.mii_data);
77296f2e892SBill Paul }
77396f2e892SBill Paul 
77496f2e892SBill Paul static int dc_miibus_writereg(dev, phy, reg, data)
77596f2e892SBill Paul 	device_t		dev;
77696f2e892SBill Paul 	int			phy, reg, data;
77796f2e892SBill Paul {
77896f2e892SBill Paul 	struct dc_softc		*sc;
77996f2e892SBill Paul 	struct dc_mii_frame	frame;
78096f2e892SBill Paul 	int			i, phy_reg;
78196f2e892SBill Paul 
78296f2e892SBill Paul 	sc = device_get_softc(dev);
78396f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
78496f2e892SBill Paul 
78596f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
78696f2e892SBill Paul 		return(0);
78796f2e892SBill Paul 
78896f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
78996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
79096f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
79196f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
79296f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
79396f2e892SBill Paul 				break;
79496f2e892SBill Paul 		}
79596f2e892SBill Paul 		return(0);
79696f2e892SBill Paul 	}
79796f2e892SBill Paul 
79896f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
79996f2e892SBill Paul 		switch(reg) {
80096f2e892SBill Paul 		case MII_BMCR:
80196f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80296f2e892SBill Paul 			break;
80396f2e892SBill Paul 		case MII_BMSR:
80496f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
80596f2e892SBill Paul 			break;
80696f2e892SBill Paul 		case MII_PHYIDR1:
80796f2e892SBill Paul 			phy_reg = DC_AL_VENID;
80896f2e892SBill Paul 			break;
80996f2e892SBill Paul 		case MII_PHYIDR2:
81096f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
81196f2e892SBill Paul 			break;
81296f2e892SBill Paul 		case MII_ANAR:
81396f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
81496f2e892SBill Paul 			break;
81596f2e892SBill Paul 		case MII_ANLPAR:
81696f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
81796f2e892SBill Paul 			break;
81896f2e892SBill Paul 		case MII_ANER:
81996f2e892SBill Paul 			phy_reg = DC_AL_ANER;
82096f2e892SBill Paul 			break;
82196f2e892SBill Paul 		default:
82296f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
82396f2e892SBill Paul 			    sc->dc_unit, reg);
82496f2e892SBill Paul 			return(0);
82596f2e892SBill Paul 			break;
82696f2e892SBill Paul 		}
82796f2e892SBill Paul 
82896f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
82996f2e892SBill Paul 		return(0);
83096f2e892SBill Paul 	}
83196f2e892SBill Paul 
83296f2e892SBill Paul 	frame.mii_phyaddr = phy;
83396f2e892SBill Paul 	frame.mii_regaddr = reg;
83496f2e892SBill Paul 	frame.mii_data = data;
83596f2e892SBill Paul 
836f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
837f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
83896f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
839f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
84096f2e892SBill Paul 
84196f2e892SBill Paul 	return(0);
84296f2e892SBill Paul }
84396f2e892SBill Paul 
84496f2e892SBill Paul static void dc_miibus_statchg(dev)
84596f2e892SBill Paul 	device_t		dev;
84696f2e892SBill Paul {
84796f2e892SBill Paul 	struct dc_softc		*sc;
84896f2e892SBill Paul 	struct mii_data		*mii;
849f43d9309SBill Paul 	struct ifmedia		*ifm;
85096f2e892SBill Paul 
85196f2e892SBill Paul 	sc = device_get_softc(dev);
85296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
85396f2e892SBill Paul 		return;
8545c1cfac4SBill Paul 
85596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
856f43d9309SBill Paul 	ifm = &mii->mii_media;
857f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
858f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
859f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
860f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
861f43d9309SBill Paul 	} else {
86296f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
86396f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
864f43d9309SBill Paul 	}
865f43d9309SBill Paul 
866f43d9309SBill Paul 	return;
867f43d9309SBill Paul }
868f43d9309SBill Paul 
869f43d9309SBill Paul /*
870f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
871f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
872f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
873f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
874f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
875f43d9309SBill Paul  * with it itself. *sigh*
876f43d9309SBill Paul  */
877f43d9309SBill Paul static void dc_miibus_mediainit(dev)
878f43d9309SBill Paul 	device_t		dev;
879f43d9309SBill Paul {
880f43d9309SBill Paul 	struct dc_softc		*sc;
881f43d9309SBill Paul 	struct mii_data		*mii;
882f43d9309SBill Paul 	struct ifmedia		*ifm;
883f43d9309SBill Paul 	int			rev;
884f43d9309SBill Paul 
885f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
886f43d9309SBill Paul 
887f43d9309SBill Paul 	sc = device_get_softc(dev);
888f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
889f43d9309SBill Paul 	ifm = &mii->mii_media;
890f43d9309SBill Paul 
891f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
892f43d9309SBill Paul 		ifmedia_add(ifm, IFM_ETHER|IFM_homePNA, 0, NULL);
89396f2e892SBill Paul 
89496f2e892SBill Paul 	return;
89596f2e892SBill Paul }
89696f2e892SBill Paul 
89796f2e892SBill Paul #define DC_POLY		0xEDB88320
89879d11e09SBill Paul #define DC_BITS_512	9
89979d11e09SBill Paul #define DC_BITS_128	7
90079d11e09SBill Paul #define DC_BITS_64	6
90196f2e892SBill Paul 
90296f2e892SBill Paul static u_int32_t dc_crc_le(sc, addr)
90396f2e892SBill Paul 	struct dc_softc		*sc;
90496f2e892SBill Paul 	caddr_t			addr;
90596f2e892SBill Paul {
90696f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
90796f2e892SBill Paul 
90896f2e892SBill Paul 	/* Compute CRC for the address value. */
90996f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
91096f2e892SBill Paul 
91196f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
91296f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
91396f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
91496f2e892SBill Paul 	}
91596f2e892SBill Paul 
91679d11e09SBill Paul 	/*
91779d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
91879d11e09SBill Paul 	 * chips is only 128 bits wide.
91979d11e09SBill Paul 	 */
92079d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
92179d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
92296f2e892SBill Paul 
92379d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
92479d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
92579d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
92679d11e09SBill Paul 
92779d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
92896f2e892SBill Paul }
92996f2e892SBill Paul 
93096f2e892SBill Paul /*
93196f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
93296f2e892SBill Paul  */
93396f2e892SBill Paul static u_int32_t dc_crc_be(addr)
93496f2e892SBill Paul 	caddr_t			addr;
93596f2e892SBill Paul {
93696f2e892SBill Paul 	u_int32_t		crc, carry;
93796f2e892SBill Paul 	int			i, j;
93896f2e892SBill Paul 	u_int8_t		c;
93996f2e892SBill Paul 
94096f2e892SBill Paul 	/* Compute CRC for the address value. */
94196f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
94296f2e892SBill Paul 
94396f2e892SBill Paul 	for (i = 0; i < 6; i++) {
94496f2e892SBill Paul 		c = *(addr + i);
94596f2e892SBill Paul 		for (j = 0; j < 8; j++) {
94696f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
94796f2e892SBill Paul 			crc <<= 1;
94896f2e892SBill Paul 			c >>= 1;
94996f2e892SBill Paul 			if (carry)
95096f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
95196f2e892SBill Paul 		}
95296f2e892SBill Paul 	}
95396f2e892SBill Paul 
95496f2e892SBill Paul 	/* return the filter bit position */
95596f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
95696f2e892SBill Paul }
95796f2e892SBill Paul 
95896f2e892SBill Paul /*
95996f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
96096f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
96196f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
96296f2e892SBill Paul  *
96396f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
96496f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
96596f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
96696f2e892SBill Paul  * we need that too.
96796f2e892SBill Paul  */
96896f2e892SBill Paul void dc_setfilt_21143(sc)
96996f2e892SBill Paul 	struct dc_softc		*sc;
97096f2e892SBill Paul {
97196f2e892SBill Paul 	struct dc_desc		*sframe;
97296f2e892SBill Paul 	u_int32_t		h, *sp;
97396f2e892SBill Paul 	struct ifmultiaddr	*ifma;
97496f2e892SBill Paul 	struct ifnet		*ifp;
97596f2e892SBill Paul 	int			i;
97696f2e892SBill Paul 
97796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
97896f2e892SBill Paul 
97996f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
98096f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
98196f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
98296f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
98396f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
98496f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
98596f2e892SBill Paul 
98696f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
98796f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
98896f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
98996f2e892SBill Paul 
99096f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
99196f2e892SBill Paul 
99296f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
99396f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
99496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99596f2e892SBill Paul 	else
99696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99796f2e892SBill Paul 
99896f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
99996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100096f2e892SBill Paul 	else
100196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100296f2e892SBill Paul 
100396f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
100496f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
100596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100696f2e892SBill Paul 			continue;
100796f2e892SBill Paul 		h = dc_crc_le(sc,
100896f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
100996f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
101096f2e892SBill Paul 	}
101196f2e892SBill Paul 
101296f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
101396f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
101496f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
101596f2e892SBill Paul 	}
101696f2e892SBill Paul 
101796f2e892SBill Paul 	/* Set our MAC address */
101896f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
101996f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
102096f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
102196f2e892SBill Paul 
102296f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
102396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
102496f2e892SBill Paul 
102596f2e892SBill Paul 	/*
102696f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
102796f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
102896f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
102996f2e892SBill Paul 	 * medicine.
103096f2e892SBill Paul 	 */
103196f2e892SBill Paul 	DELAY(10000);
103296f2e892SBill Paul 
103396f2e892SBill Paul 	ifp->if_timer = 5;
103496f2e892SBill Paul 
103596f2e892SBill Paul 	return;
103696f2e892SBill Paul }
103796f2e892SBill Paul 
103896f2e892SBill Paul void dc_setfilt_admtek(sc)
103996f2e892SBill Paul 	struct dc_softc		*sc;
104096f2e892SBill Paul {
104196f2e892SBill Paul 	struct ifnet		*ifp;
104296f2e892SBill Paul 	int			h = 0;
104396f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
104496f2e892SBill Paul 	struct ifmultiaddr	*ifma;
104596f2e892SBill Paul 
104696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
104796f2e892SBill Paul 
104896f2e892SBill Paul 	/* Init our MAC address */
104996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
105096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
105196f2e892SBill Paul 
105296f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
105396f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
105496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105596f2e892SBill Paul 	else
105696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105796f2e892SBill Paul 
105896f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
105996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106096f2e892SBill Paul 	else
106196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106296f2e892SBill Paul 
106396f2e892SBill Paul 	/* first, zot all the existing hash bits */
106496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
106596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
106696f2e892SBill Paul 
106796f2e892SBill Paul 	/*
106896f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
106996f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
107096f2e892SBill Paul 	 */
107196f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
107296f2e892SBill Paul 		return;
107396f2e892SBill Paul 
107496f2e892SBill Paul 	/* now program new ones */
107596f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
107696f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
107796f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
107896f2e892SBill Paul 			continue;
107996f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
108096f2e892SBill Paul 		if (h < 32)
108196f2e892SBill Paul 			hashes[0] |= (1 << h);
108296f2e892SBill Paul 		else
108396f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
108496f2e892SBill Paul 	}
108596f2e892SBill Paul 
108696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
108796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
108896f2e892SBill Paul 
108996f2e892SBill Paul 	return;
109096f2e892SBill Paul }
109196f2e892SBill Paul 
109296f2e892SBill Paul void dc_setfilt_asix(sc)
109396f2e892SBill Paul 	struct dc_softc		*sc;
109496f2e892SBill Paul {
109596f2e892SBill Paul 	struct ifnet		*ifp;
109696f2e892SBill Paul 	int			h = 0;
109796f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
109896f2e892SBill Paul 	struct ifmultiaddr	*ifma;
109996f2e892SBill Paul 
110096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
110196f2e892SBill Paul 
110296f2e892SBill Paul         /* Init our MAC address */
110396f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
110496f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
110596f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
110696f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
110796f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
110896f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
110996f2e892SBill Paul 
111096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
111196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
111296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111396f2e892SBill Paul 	else
111496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111596f2e892SBill Paul 
111696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
111796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111896f2e892SBill Paul 	else
111996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112096f2e892SBill Paul 
112196f2e892SBill Paul 	/*
112296f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
112396f2e892SBill Paul 	 * of broadcast frames.
112496f2e892SBill Paul 	 */
112596f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
112696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
112796f2e892SBill Paul 	else
112896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
112996f2e892SBill Paul 
113096f2e892SBill Paul 	/* first, zot all the existing hash bits */
113196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
113296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
113396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
113496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
113596f2e892SBill Paul 
113696f2e892SBill Paul 	/*
113796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
113896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
113996f2e892SBill Paul 	 */
114096f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
114196f2e892SBill Paul 		return;
114296f2e892SBill Paul 
114396f2e892SBill Paul 	/* now program new ones */
114496f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
114596f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
114696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
114796f2e892SBill Paul 			continue;
114896f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
114996f2e892SBill Paul 		if (h < 32)
115096f2e892SBill Paul 			hashes[0] |= (1 << h);
115196f2e892SBill Paul 		else
115296f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
115396f2e892SBill Paul 	}
115496f2e892SBill Paul 
115596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
115696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
115796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
115896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
115996f2e892SBill Paul 
116096f2e892SBill Paul 	return;
116196f2e892SBill Paul }
116296f2e892SBill Paul 
116396f2e892SBill Paul static void dc_setfilt(sc)
116496f2e892SBill Paul 	struct dc_softc		*sc;
116596f2e892SBill Paul {
116696f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
116796f2e892SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc))
116896f2e892SBill Paul 		dc_setfilt_21143(sc);
116996f2e892SBill Paul 
117096f2e892SBill Paul 	if (DC_IS_ASIX(sc))
117196f2e892SBill Paul 		dc_setfilt_asix(sc);
117296f2e892SBill Paul 
117396f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
117496f2e892SBill Paul 		dc_setfilt_admtek(sc);
117596f2e892SBill Paul 
117696f2e892SBill Paul 	return;
117796f2e892SBill Paul }
117896f2e892SBill Paul 
117996f2e892SBill Paul /*
118096f2e892SBill Paul  * In order to fiddle with the
118196f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
118296f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
118396f2e892SBill Paul  */
118496f2e892SBill Paul static void dc_setcfg(sc, media)
118596f2e892SBill Paul 	struct dc_softc		*sc;
118696f2e892SBill Paul 	int			media;
118796f2e892SBill Paul {
118896f2e892SBill Paul 	int			i, restart = 0;
118996f2e892SBill Paul 	u_int32_t		isr;
119096f2e892SBill Paul 
119196f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
119296f2e892SBill Paul 		return;
119396f2e892SBill Paul 
119496f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
119596f2e892SBill Paul 		restart = 1;
119696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
119796f2e892SBill Paul 
119896f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
119996f2e892SBill Paul 			DELAY(10);
120096f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
120196f2e892SBill Paul 			if (isr & DC_ISR_TX_IDLE ||
120296f2e892SBill Paul 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED)
120396f2e892SBill Paul 				break;
120496f2e892SBill Paul 		}
120596f2e892SBill Paul 
120696f2e892SBill Paul 		if (i == DC_TIMEOUT)
120796f2e892SBill Paul 			printf("dc%d: failed to force tx and "
120896f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
120996f2e892SBill Paul 
121096f2e892SBill Paul 	}
121196f2e892SBill Paul 
121296f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1213042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1214042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
121596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
12168273d5f8SBill Paul 			int	watchdogreg;
12178273d5f8SBill Paul 
1218bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
12198273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
12208273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
12218273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
12228273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
1223bf645417SBill Paul 			} else {
1224bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1225bf645417SBill Paul 			}
122696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
122796f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
122896f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
122996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
123096f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
123188d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
123296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
123396f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
123496f2e892SBill Paul 		} else {
123596f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
123696f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
123796f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
123896f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
123996f2e892SBill Paul 			}
1240318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1241318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1242318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
12435c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
12445c1cfac4SBill Paul 				dc_apply_fixup(sc,
12455c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
12465c1cfac4SBill Paul 				    IFM_100_TX|IFM_FDX : IFM_100_TX);
124796f2e892SBill Paul 		}
124896f2e892SBill Paul 	}
124996f2e892SBill Paul 
125096f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1251042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1252042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
125396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
12548273d5f8SBill Paul 			int	watchdogreg;
12558273d5f8SBill Paul 
12568273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
12578273d5f8SBill Paul 			watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
12588273d5f8SBill Paul 			watchdogreg &= ~DC_WDOG_CTLWREN;
12598273d5f8SBill Paul 			watchdogreg |= DC_WDOG_JABBERDIS;
12608273d5f8SBill Paul 			CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
126196f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
126296f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
126396f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
126496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
126588d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
126696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
126796f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
126896f2e892SBill Paul 		} else {
126996f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
127096f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
127196f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
127296f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
127396f2e892SBill Paul 			}
127496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1275318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
127696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
12775c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
12785c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
12795c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
12805c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
12815c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
12825c1cfac4SBill Paul 				else
12835c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
12845c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
12855c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
12865c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
12875c1cfac4SBill Paul 				dc_apply_fixup(sc,
12885c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
12895c1cfac4SBill Paul 				    IFM_10_T|IFM_FDX : IFM_10_T);
12905c1cfac4SBill Paul 				DELAY(20000);
12915c1cfac4SBill Paul 			}
129296f2e892SBill Paul 		}
129396f2e892SBill Paul 	}
129496f2e892SBill Paul 
1295f43d9309SBill Paul 	/*
1296f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1297f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1298f43d9309SBill Paul 	 * on the external MII port.
1299f43d9309SBill Paul 	 */
1300f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
1301f43d9309SBill Paul 		if (IFM_SUBTYPE(media) == IFM_homePNA) {
1302f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1303f43d9309SBill Paul 			sc->dc_link = 1;
1304f43d9309SBill Paul 		} else {
1305f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1306f43d9309SBill Paul 		}
1307f43d9309SBill Paul 	}
1308f43d9309SBill Paul 
130996f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
131096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
131196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
131296f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
131396f2e892SBill Paul 	} else {
131496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
131596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
131696f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
131796f2e892SBill Paul 	}
131896f2e892SBill Paul 
131996f2e892SBill Paul 	if (restart)
132096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
132196f2e892SBill Paul 
132296f2e892SBill Paul 	return;
132396f2e892SBill Paul }
132496f2e892SBill Paul 
132596f2e892SBill Paul static void dc_reset(sc)
132696f2e892SBill Paul 	struct dc_softc		*sc;
132796f2e892SBill Paul {
132896f2e892SBill Paul 	register int		i;
132996f2e892SBill Paul 
133096f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
133196f2e892SBill Paul 
133296f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
133396f2e892SBill Paul 		DELAY(10);
133496f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
133596f2e892SBill Paul 			break;
133696f2e892SBill Paul 	}
133796f2e892SBill Paul 
133896f2e892SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc)) {
133996f2e892SBill Paul 		DELAY(10000);
134096f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
134196f2e892SBill Paul 		i = 0;
134296f2e892SBill Paul 	}
134396f2e892SBill Paul 
134496f2e892SBill Paul 	if (i == DC_TIMEOUT)
134596f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
134696f2e892SBill Paul 
134796f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
134896f2e892SBill Paul 	DELAY(1000);
134996f2e892SBill Paul 
135096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
135196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
135296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
135396f2e892SBill Paul 
135491cc2adbSBill Paul 	/*
135591cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
135691cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
135791cc2adbSBill Paul 	 * into a state where it will never come out of reset
135891cc2adbSBill Paul 	 * until we reset the whole chip again.
135991cc2adbSBill Paul 	 */
13605c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
136191cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
13625c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
13635c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
13645c1cfac4SBill Paul 	}
136591cc2adbSBill Paul 
136696f2e892SBill Paul         return;
136796f2e892SBill Paul }
136896f2e892SBill Paul 
136996f2e892SBill Paul static struct dc_type *dc_devtype(dev)
137096f2e892SBill Paul 	device_t		dev;
137196f2e892SBill Paul {
137296f2e892SBill Paul 	struct dc_type		*t;
137396f2e892SBill Paul 	u_int32_t		rev;
137496f2e892SBill Paul 
137596f2e892SBill Paul 	t = dc_devs;
137696f2e892SBill Paul 
137796f2e892SBill Paul 	while(t->dc_name != NULL) {
137896f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
137996f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
138096f2e892SBill Paul 			/* Check the PCI revision */
138196f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
138296f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
138396f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
138496f2e892SBill Paul 				t++;
138596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
138696f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
138796f2e892SBill Paul 				t++;
138896f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
138979d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
139079d11e09SBill Paul 				t++;
139179d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
139296f2e892SBill Paul 			    rev >= DC_REVISION_98725)
139396f2e892SBill Paul 				t++;
139496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
139596f2e892SBill Paul 			    rev >= DC_REVISION_88141)
139696f2e892SBill Paul 				t++;
139796f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
139896f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
139996f2e892SBill Paul 				t++;
140088d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
140188d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
140288d739dcSBill Paul 				t++;
140396f2e892SBill Paul 			return(t);
140496f2e892SBill Paul 		}
140596f2e892SBill Paul 		t++;
140696f2e892SBill Paul 	}
140796f2e892SBill Paul 
140896f2e892SBill Paul 	return(NULL);
140996f2e892SBill Paul }
141096f2e892SBill Paul 
141196f2e892SBill Paul /*
141296f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
141396f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
141496f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
141596f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
141696f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
141796f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
141896f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
141996f2e892SBill Paul  */
142096f2e892SBill Paul static int dc_probe(dev)
142196f2e892SBill Paul 	device_t		dev;
142296f2e892SBill Paul {
142396f2e892SBill Paul 	struct dc_type		*t;
142496f2e892SBill Paul 
142596f2e892SBill Paul 	t = dc_devtype(dev);
142696f2e892SBill Paul 
142796f2e892SBill Paul 	if (t != NULL) {
142896f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
142996f2e892SBill Paul 		return(0);
143096f2e892SBill Paul 	}
143196f2e892SBill Paul 
143296f2e892SBill Paul 	return(ENXIO);
143396f2e892SBill Paul }
143496f2e892SBill Paul 
143596f2e892SBill Paul static void dc_acpi(dev)
143696f2e892SBill Paul 	device_t		dev;
143796f2e892SBill Paul {
143896f2e892SBill Paul 	u_int32_t		r, cptr;
143996f2e892SBill Paul 	int			unit;
144096f2e892SBill Paul 
144196f2e892SBill Paul 	unit = device_get_unit(dev);
144296f2e892SBill Paul 
144396f2e892SBill Paul 	/* Find the location of the capabilities block */
144496f2e892SBill Paul 	cptr = pci_read_config(dev, DC_PCI_CCAP, 4) & 0xFF;
144596f2e892SBill Paul 
144696f2e892SBill Paul 	r = pci_read_config(dev, cptr, 4) & 0xFF;
144796f2e892SBill Paul 	if (r == 0x01) {
144896f2e892SBill Paul 
144996f2e892SBill Paul 		r = pci_read_config(dev, cptr + 4, 4);
145096f2e892SBill Paul 		if (r & DC_PSTATE_D3) {
145196f2e892SBill Paul 			u_int32_t		iobase, membase, irq;
145296f2e892SBill Paul 
145396f2e892SBill Paul 			/* Save important PCI config data. */
145496f2e892SBill Paul 			iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
145596f2e892SBill Paul 			membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
145696f2e892SBill Paul 			irq = pci_read_config(dev, DC_PCI_CFIT, 4);
145796f2e892SBill Paul 
145896f2e892SBill Paul 			/* Reset the power state. */
145996f2e892SBill Paul 			printf("dc%d: chip is in D%d power mode "
146096f2e892SBill Paul 			    "-- setting to D0\n", unit, r & DC_PSTATE_D3);
146196f2e892SBill Paul 			r &= 0xFFFFFFFC;
146296f2e892SBill Paul 			pci_write_config(dev, cptr + 4, r, 4);
146396f2e892SBill Paul 
146496f2e892SBill Paul 			/* Restore PCI config data. */
146596f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
146696f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
146796f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFIT, irq, 4);
146896f2e892SBill Paul 		}
146996f2e892SBill Paul 	}
147096f2e892SBill Paul 	return;
147196f2e892SBill Paul }
147296f2e892SBill Paul 
14735c1cfac4SBill Paul static void dc_apply_fixup(sc, media)
14745c1cfac4SBill Paul 	struct dc_softc		*sc;
14755c1cfac4SBill Paul 	int			media;
14765c1cfac4SBill Paul {
14775c1cfac4SBill Paul 	struct dc_mediainfo	*m;
14785c1cfac4SBill Paul 	u_int8_t		*p;
14795c1cfac4SBill Paul 	int			i;
14805c1cfac4SBill Paul 	u_int8_t		reg;
14815c1cfac4SBill Paul 
14825c1cfac4SBill Paul 	m = sc->dc_mi;
14835c1cfac4SBill Paul 
14845c1cfac4SBill Paul 	while (m != NULL) {
14855c1cfac4SBill Paul 		if (m->dc_media == media)
14865c1cfac4SBill Paul 			break;
14875c1cfac4SBill Paul 		m = m->dc_next;
14885c1cfac4SBill Paul 	}
14895c1cfac4SBill Paul 
14905c1cfac4SBill Paul 	if (m == NULL)
14915c1cfac4SBill Paul 		return;
14925c1cfac4SBill Paul 
14935c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
14945c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
14955c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
14965c1cfac4SBill Paul 	}
14975c1cfac4SBill Paul 
14985c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
14995c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
15005c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
15015c1cfac4SBill Paul 	}
15025c1cfac4SBill Paul 
15035c1cfac4SBill Paul 	return;
15045c1cfac4SBill Paul }
15055c1cfac4SBill Paul 
15065c1cfac4SBill Paul static void dc_decode_leaf_sia(sc, l)
15075c1cfac4SBill Paul 	struct dc_softc		*sc;
15085c1cfac4SBill Paul 	struct dc_eblock_sia	*l;
15095c1cfac4SBill Paul {
15105c1cfac4SBill Paul 	struct dc_mediainfo	*m;
15115c1cfac4SBill Paul 
15125c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
15135c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
15145c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
15155c1cfac4SBill Paul 
15165c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
15175c1cfac4SBill Paul 		m->dc_media = IFM_10_T|IFM_FDX;
15185c1cfac4SBill Paul 
15195c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
15205c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
15215c1cfac4SBill Paul 
15225c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
15235c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
15245c1cfac4SBill Paul 
15255c1cfac4SBill Paul 	m->dc_gp_len = 2;
15265c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
15275c1cfac4SBill Paul 
15285c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
15295c1cfac4SBill Paul 	sc->dc_mi = m;
15305c1cfac4SBill Paul 
15315c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
15325c1cfac4SBill Paul 
15335c1cfac4SBill Paul 	return;
15345c1cfac4SBill Paul }
15355c1cfac4SBill Paul 
15365c1cfac4SBill Paul static void dc_decode_leaf_sym(sc, l)
15375c1cfac4SBill Paul 	struct dc_softc		*sc;
15385c1cfac4SBill Paul 	struct dc_eblock_sym	*l;
15395c1cfac4SBill Paul {
15405c1cfac4SBill Paul 	struct dc_mediainfo	*m;
15415c1cfac4SBill Paul 
15425c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
15435c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
15445c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
15455c1cfac4SBill Paul 
15465c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
15475c1cfac4SBill Paul 		m->dc_media = IFM_100_TX|IFM_FDX;
15485c1cfac4SBill Paul 
15495c1cfac4SBill Paul 	m->dc_gp_len = 2;
15505c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
15515c1cfac4SBill Paul 
15525c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
15535c1cfac4SBill Paul 	sc->dc_mi = m;
15545c1cfac4SBill Paul 
15555c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
15565c1cfac4SBill Paul 
15575c1cfac4SBill Paul 	return;
15585c1cfac4SBill Paul }
15595c1cfac4SBill Paul 
15605c1cfac4SBill Paul static void dc_decode_leaf_mii(sc, l)
15615c1cfac4SBill Paul 	struct dc_softc		*sc;
15625c1cfac4SBill Paul 	struct dc_eblock_mii	*l;
15635c1cfac4SBill Paul {
15645c1cfac4SBill Paul 	u_int8_t		*p;
15655c1cfac4SBill Paul 	struct dc_mediainfo	*m;
15665c1cfac4SBill Paul 
15675c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
15685c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
15695c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
15705c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
15715c1cfac4SBill Paul 
15725c1cfac4SBill Paul 	p = (u_int8_t *)l;
15735c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
15745c1cfac4SBill Paul 	m->dc_gp_ptr = p;
15755c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
15765c1cfac4SBill Paul 	m->dc_reset_len = *p;
15775c1cfac4SBill Paul 	p++;
15785c1cfac4SBill Paul 	m->dc_reset_ptr = p;
15795c1cfac4SBill Paul 
15805c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
15815c1cfac4SBill Paul 	sc->dc_mi = m;
15825c1cfac4SBill Paul 
15835c1cfac4SBill Paul 	return;
15845c1cfac4SBill Paul }
15855c1cfac4SBill Paul 
15865c1cfac4SBill Paul static void dc_parse_21143_srom(sc)
15875c1cfac4SBill Paul 	struct dc_softc		*sc;
15885c1cfac4SBill Paul {
15895c1cfac4SBill Paul 	struct dc_leaf_hdr	*lhdr;
15905c1cfac4SBill Paul 	struct dc_eblock_hdr	*hdr;
15915c1cfac4SBill Paul 	int			i, loff;
15925c1cfac4SBill Paul 	char			*ptr;
15935c1cfac4SBill Paul 
15945c1cfac4SBill Paul 	loff = sc->dc_srom[27];
15955c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
15965c1cfac4SBill Paul 
15975c1cfac4SBill Paul 	ptr = (char *)lhdr;
15985c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
15995c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
16005c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
16015c1cfac4SBill Paul 		switch(hdr->dc_type) {
16025c1cfac4SBill Paul 		case DC_EBLOCK_MII:
16035c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
16045c1cfac4SBill Paul 			break;
16055c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
16065c1cfac4SBill Paul 			dc_decode_leaf_sia(sc, (struct dc_eblock_sia *)hdr);
16075c1cfac4SBill Paul 			break;
16085c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
16095c1cfac4SBill Paul 			dc_decode_leaf_sym(sc, (struct dc_eblock_sym *)hdr);
16105c1cfac4SBill Paul 			break;
16115c1cfac4SBill Paul 		default:
16125c1cfac4SBill Paul 			/* Don't care. Yet. */
16135c1cfac4SBill Paul 			break;
16145c1cfac4SBill Paul 		}
16155c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
16165c1cfac4SBill Paul 		ptr++;
16175c1cfac4SBill Paul 	}
16185c1cfac4SBill Paul 
16195c1cfac4SBill Paul 	return;
16205c1cfac4SBill Paul }
16215c1cfac4SBill Paul 
162296f2e892SBill Paul /*
162396f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
162496f2e892SBill Paul  * setup and ethernet/BPF attach.
162596f2e892SBill Paul  */
162696f2e892SBill Paul static int dc_attach(dev)
162796f2e892SBill Paul 	device_t		dev;
162896f2e892SBill Paul {
16295c1cfac4SBill Paul 	int			s, tmp = 0;
163096f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
163196f2e892SBill Paul 	u_int32_t		command;
163296f2e892SBill Paul 	struct dc_softc		*sc;
163396f2e892SBill Paul 	struct ifnet		*ifp;
163496f2e892SBill Paul 	u_int32_t		revision;
163596f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
163696f2e892SBill Paul 
163796f2e892SBill Paul 	s = splimp();
163896f2e892SBill Paul 
163996f2e892SBill Paul 	sc = device_get_softc(dev);
164096f2e892SBill Paul 	unit = device_get_unit(dev);
164196f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
164296f2e892SBill Paul 
164396f2e892SBill Paul 	/*
164496f2e892SBill Paul 	 * Handle power management nonsense.
164596f2e892SBill Paul 	 */
164696f2e892SBill Paul 	dc_acpi(dev);
164796f2e892SBill Paul 
164896f2e892SBill Paul 	/*
164996f2e892SBill Paul 	 * Map control/status registers.
165096f2e892SBill Paul 	 */
1651c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
165296f2e892SBill Paul 	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1653c48cc9ceSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, command, 4);
1654c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
165596f2e892SBill Paul 
165696f2e892SBill Paul #ifdef DC_USEIOSPACE
165796f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
165896f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
165996f2e892SBill Paul 		error = ENXIO;
166096f2e892SBill Paul 		goto fail;
166196f2e892SBill Paul 	}
166296f2e892SBill Paul #else
166396f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
166496f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
166596f2e892SBill Paul 		error = ENXIO;
166696f2e892SBill Paul 		goto fail;
166796f2e892SBill Paul 	}
166896f2e892SBill Paul #endif
166996f2e892SBill Paul 
167096f2e892SBill Paul 	rid = DC_RID;
167196f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
167296f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
167396f2e892SBill Paul 
167496f2e892SBill Paul 	if (sc->dc_res == NULL) {
167596f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
167696f2e892SBill Paul 		error = ENXIO;
167796f2e892SBill Paul 		goto fail;
167896f2e892SBill Paul 	}
167996f2e892SBill Paul 
168096f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
168196f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
168296f2e892SBill Paul 
168396f2e892SBill Paul 	/* Allocate interrupt */
168496f2e892SBill Paul 	rid = 0;
168596f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
168696f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
168796f2e892SBill Paul 
168896f2e892SBill Paul 	if (sc->dc_irq == NULL) {
168996f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
169096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
169196f2e892SBill Paul 		error = ENXIO;
169296f2e892SBill Paul 		goto fail;
169396f2e892SBill Paul 	}
169496f2e892SBill Paul 
169596f2e892SBill Paul 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET,
169696f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
169796f2e892SBill Paul 
169896f2e892SBill Paul 	if (error) {
169996f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
170096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
170196f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
170296f2e892SBill Paul 		goto fail;
170396f2e892SBill Paul 	}
170496f2e892SBill Paul 
170596f2e892SBill Paul 	/* Need this info to decide on a chip type. */
170696f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
170796f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
170896f2e892SBill Paul 
170996f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
171096f2e892SBill Paul 	case DC_DEVICEID_21143:
171196f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
171296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1713042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
17145c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
17155c1cfac4SBill Paul 		dc_read_eeprom(sc, (caddr_t)&sc->dc_srom, 0, 512, 0);
171696f2e892SBill Paul 		break;
171796f2e892SBill Paul 	case DC_DEVICEID_DM9100:
171896f2e892SBill Paul 	case DC_DEVICEID_DM9102:
171996f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1720fda39fd0SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_USE_TX_INTR;
17215c1cfac4SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_TX_ONE;
172296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
172396f2e892SBill Paul 		break;
172496f2e892SBill Paul 	case DC_DEVICEID_AL981:
172596f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
172696f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
172796f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
172896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
172996f2e892SBill Paul 		break;
173096f2e892SBill Paul 	case DC_DEVICEID_AN985:
173196f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
173296f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
173396f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
173496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
173596f2e892SBill Paul 		break;
173696f2e892SBill Paul 	case DC_DEVICEID_98713:
173796f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
173896f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
173996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
174096f2e892SBill Paul 		}
1741318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
174296f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1743318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1744318b02fdSBill Paul 		}
1745318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
174696f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
174796f2e892SBill Paul 		break;
174896f2e892SBill Paul 	case DC_DEVICEID_987x5:
17499ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
175079d11e09SBill Paul 		/*
175179d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
175279d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
175379d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
175479d11e09SBill Paul 		 * get the right number of bits out of the
175579d11e09SBill Paul 		 * CRC routine.
175679d11e09SBill Paul 		 */
175779d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
175879d11e09SBill Paul 		    revision < DC_REVISION_98725)
175979d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
176096f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
176196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1762318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
176396f2e892SBill Paul 		break;
1764ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1765ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1766ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1767ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
1768ead7cde9SBill Paul 		break;
176996f2e892SBill Paul 	case DC_DEVICEID_82C115:
177096f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
177179d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR|DC_128BIT_HASH;
1772318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
177396f2e892SBill Paul 		break;
177496f2e892SBill Paul 	case DC_DEVICEID_82C168:
177596f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
177691cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
177796f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
177896f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
177996f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
178096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
178196f2e892SBill Paul 		break;
178296f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
178396f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
178496f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
178596f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
178696f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
178796f2e892SBill Paul 		break;
178896f2e892SBill Paul 	default:
178996f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
179096f2e892SBill Paul 		    sc->dc_info->dc_did);
179196f2e892SBill Paul 		break;
179296f2e892SBill Paul 	}
179396f2e892SBill Paul 
179496f2e892SBill Paul 	/* Save the cache line size. */
179588d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
179688d739dcSBill Paul 		sc->dc_cachesize = 0;
179788d739dcSBill Paul 	else
179888d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
179988d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
180096f2e892SBill Paul 
180196f2e892SBill Paul 	/* Reset the adapter. */
180296f2e892SBill Paul 	dc_reset(sc);
180396f2e892SBill Paul 
180496f2e892SBill Paul 	/* Take 21143 out of snooze mode */
180596f2e892SBill Paul 	if (DC_IS_INTEL(sc)) {
180696f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
180796f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
180896f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
180996f2e892SBill Paul 	}
181096f2e892SBill Paul 
181196f2e892SBill Paul 	/*
181296f2e892SBill Paul 	 * Try to learn something about the supported media.
181396f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
181496f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
181596f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
181696f2e892SBill Paul 	 * Intel 21143.
181796f2e892SBill Paul 	 */
18185c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
18195c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
18205c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
182196f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
182296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
182396f2e892SBill Paul 		else
182496f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
182596f2e892SBill Paul 	} else if (!sc->dc_pmode)
182696f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
182796f2e892SBill Paul 
182896f2e892SBill Paul 	/*
182996f2e892SBill Paul 	 * Get station address from the EEPROM.
183096f2e892SBill Paul 	 */
183196f2e892SBill Paul 	switch(sc->dc_type) {
183296f2e892SBill Paul 	case DC_TYPE_98713:
183396f2e892SBill Paul 	case DC_TYPE_98713A:
183496f2e892SBill Paul 	case DC_TYPE_987x5:
183596f2e892SBill Paul 	case DC_TYPE_PNICII:
183696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
183796f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
183896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
183996f2e892SBill Paul 		break;
184096f2e892SBill Paul 	case DC_TYPE_PNIC:
184196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
184296f2e892SBill Paul 		break;
184396f2e892SBill Paul 	case DC_TYPE_DM9102:
184496f2e892SBill Paul 	case DC_TYPE_21143:
184596f2e892SBill Paul 	case DC_TYPE_ASIX:
184696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
184796f2e892SBill Paul 		break;
184896f2e892SBill Paul 	case DC_TYPE_AL981:
184996f2e892SBill Paul 	case DC_TYPE_AN985:
185096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
185196f2e892SBill Paul 		break;
185296f2e892SBill Paul 	default:
185396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
185496f2e892SBill Paul 		break;
185596f2e892SBill Paul 	}
185696f2e892SBill Paul 
185796f2e892SBill Paul 	/*
185896f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
185996f2e892SBill Paul 	 */
186096f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
186196f2e892SBill Paul 
186296f2e892SBill Paul 	sc->dc_unit = unit;
186396f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
186496f2e892SBill Paul 
186596f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
186696f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
186796f2e892SBill Paul 
186896f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
186996f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
187096f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
187196f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
187296f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
187396f2e892SBill Paul 		error = ENXIO;
187496f2e892SBill Paul 		goto fail;
187596f2e892SBill Paul 	}
187696f2e892SBill Paul 
187796f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
187896f2e892SBill Paul 
187996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
188096f2e892SBill Paul 	ifp->if_softc = sc;
188196f2e892SBill Paul 	ifp->if_unit = unit;
188296f2e892SBill Paul 	ifp->if_name = "dc";
188396f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
188496f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
188596f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
188696f2e892SBill Paul 	ifp->if_output = ether_output;
188796f2e892SBill Paul 	ifp->if_start = dc_start;
188896f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
188996f2e892SBill Paul 	ifp->if_init = dc_init;
189096f2e892SBill Paul 	ifp->if_baudrate = 10000000;
189196f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
189296f2e892SBill Paul 
189396f2e892SBill Paul 	/*
18945c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
18955c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
18965c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
18975c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
18985c1cfac4SBill Paul 	 * driver instead.
189996f2e892SBill Paul 	 */
19005c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
19015c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
19025c1cfac4SBill Paul 		tmp = sc->dc_pmode;
19035c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19045c1cfac4SBill Paul 	}
19055c1cfac4SBill Paul 
190696f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
190796f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
190896f2e892SBill Paul 
190996f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
19105c1cfac4SBill Paul 		sc->dc_pmode = tmp;
19115c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
191296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
1913042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
191496f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
191596f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
191678999dd1SBill Paul 		/*
191778999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
191878999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
191978999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
192078999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
192178999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
192278999dd1SBill Paul 		 */
192378999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
192478999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
192596f2e892SBill Paul 		error = 0;
192696f2e892SBill Paul 	}
192796f2e892SBill Paul 
192896f2e892SBill Paul 	if (error) {
192996f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
193096f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
193196f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
193296f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
193396f2e892SBill Paul 		error = ENXIO;
193496f2e892SBill Paul 		goto fail;
193596f2e892SBill Paul 	}
193696f2e892SBill Paul 
193796f2e892SBill Paul 	/*
193821b8ebd9SArchie Cobbs 	 * Call MI attach routine.
193996f2e892SBill Paul 	 */
194021b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
194196f2e892SBill Paul 	callout_handle_init(&sc->dc_stat_ch);
194296f2e892SBill Paul 
19435c1cfac4SBill Paul #ifdef SRM_MEDIA
1944510a809eSMike Smith         sc->dc_srm_media = 0;
1945510a809eSMike Smith 
1946510a809eSMike Smith 	/* Remember the SRM console media setting */
1947510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
1948510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
1949510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
1950510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
1951510a809eSMike Smith 		case 3:
1952510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
1953510a809eSMike Smith 			break;
1954510a809eSMike Smith 		case 4:
1955510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
1956510a809eSMike Smith 			break;
1957510a809eSMike Smith 		case 5:
1958510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
1959510a809eSMike Smith 			break;
1960510a809eSMike Smith 		case 6:
1961510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
1962510a809eSMike Smith 			break;
1963510a809eSMike Smith 		}
1964510a809eSMike Smith 		if (sc->dc_srm_media)
1965510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
1966510a809eSMike Smith 	}
1967510a809eSMike Smith #endif
1968510a809eSMike Smith 
1969510a809eSMike Smith 
197096f2e892SBill Paul fail:
197196f2e892SBill Paul 	splx(s);
197296f2e892SBill Paul 
197396f2e892SBill Paul 	return(error);
197496f2e892SBill Paul }
197596f2e892SBill Paul 
197696f2e892SBill Paul static int dc_detach(dev)
197796f2e892SBill Paul 	device_t		dev;
197896f2e892SBill Paul {
197996f2e892SBill Paul 	struct dc_softc		*sc;
198096f2e892SBill Paul 	struct ifnet		*ifp;
198196f2e892SBill Paul 	int			s;
19825c1cfac4SBill Paul 	struct dc_mediainfo	*m;
198396f2e892SBill Paul 
198496f2e892SBill Paul 	s = splimp();
198596f2e892SBill Paul 
198696f2e892SBill Paul 	sc = device_get_softc(dev);
198796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
198896f2e892SBill Paul 
198996f2e892SBill Paul 	dc_stop(sc);
199021b8ebd9SArchie Cobbs 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
199196f2e892SBill Paul 
199296f2e892SBill Paul 	bus_generic_detach(dev);
199396f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
199496f2e892SBill Paul 
199596f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
199696f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
199796f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
199896f2e892SBill Paul 
199996f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
200096f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
200196f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
200296f2e892SBill Paul 
20035c1cfac4SBill Paul 	while(sc->dc_mi != NULL) {
20045c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
20055c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
20065c1cfac4SBill Paul 		sc->dc_mi = m;
20075c1cfac4SBill Paul 	}
20085c1cfac4SBill Paul 
200996f2e892SBill Paul 	splx(s);
201096f2e892SBill Paul 
201196f2e892SBill Paul 	return(0);
201296f2e892SBill Paul }
201396f2e892SBill Paul 
201496f2e892SBill Paul /*
201596f2e892SBill Paul  * Initialize the transmit descriptors.
201696f2e892SBill Paul  */
201796f2e892SBill Paul static int dc_list_tx_init(sc)
201896f2e892SBill Paul 	struct dc_softc		*sc;
201996f2e892SBill Paul {
202096f2e892SBill Paul 	struct dc_chain_data	*cd;
202196f2e892SBill Paul 	struct dc_list_data	*ld;
202296f2e892SBill Paul 	int			i;
202396f2e892SBill Paul 
202496f2e892SBill Paul 	cd = &sc->dc_cdata;
202596f2e892SBill Paul 	ld = sc->dc_ldata;
202696f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
202796f2e892SBill Paul 		if (i == (DC_TX_LIST_CNT - 1)) {
202896f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
202996f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[0]);
203096f2e892SBill Paul 		} else {
203196f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
203296f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[i + 1]);
203396f2e892SBill Paul 		}
203496f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
203596f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
203696f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
203796f2e892SBill Paul 	}
203896f2e892SBill Paul 
203996f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
204096f2e892SBill Paul 
204196f2e892SBill Paul 	return(0);
204296f2e892SBill Paul }
204396f2e892SBill Paul 
204496f2e892SBill Paul 
204596f2e892SBill Paul /*
204696f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
204796f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
204896f2e892SBill Paul  * points back to the first.
204996f2e892SBill Paul  */
205096f2e892SBill Paul static int dc_list_rx_init(sc)
205196f2e892SBill Paul 	struct dc_softc		*sc;
205296f2e892SBill Paul {
205396f2e892SBill Paul 	struct dc_chain_data	*cd;
205496f2e892SBill Paul 	struct dc_list_data	*ld;
205596f2e892SBill Paul 	int			i;
205696f2e892SBill Paul 
205796f2e892SBill Paul 	cd = &sc->dc_cdata;
205896f2e892SBill Paul 	ld = sc->dc_ldata;
205996f2e892SBill Paul 
206096f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
206196f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
206296f2e892SBill Paul 			return(ENOBUFS);
206396f2e892SBill Paul 		if (i == (DC_RX_LIST_CNT - 1)) {
206496f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
206596f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[0]);
206696f2e892SBill Paul 		} else {
206796f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
206896f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[i + 1]);
206996f2e892SBill Paul 		}
207096f2e892SBill Paul 	}
207196f2e892SBill Paul 
207296f2e892SBill Paul 	cd->dc_rx_prod = 0;
207396f2e892SBill Paul 
207496f2e892SBill Paul 	return(0);
207596f2e892SBill Paul }
207696f2e892SBill Paul 
207796f2e892SBill Paul /*
207896f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
207996f2e892SBill Paul  */
208096f2e892SBill Paul static int dc_newbuf(sc, i, m)
208196f2e892SBill Paul 	struct dc_softc		*sc;
208296f2e892SBill Paul 	int			i;
208396f2e892SBill Paul 	struct mbuf		*m;
208496f2e892SBill Paul {
208596f2e892SBill Paul 	struct mbuf		*m_new = NULL;
208696f2e892SBill Paul 	struct dc_desc		*c;
208796f2e892SBill Paul 
208896f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
208996f2e892SBill Paul 
209096f2e892SBill Paul 	if (m == NULL) {
209196f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
209296f2e892SBill Paul 		if (m_new == NULL) {
209396f2e892SBill Paul 			printf("dc%d: no memory for rx list "
209496f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
209596f2e892SBill Paul 			return(ENOBUFS);
209696f2e892SBill Paul 		}
209796f2e892SBill Paul 
209896f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
209996f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
210096f2e892SBill Paul 			printf("dc%d: no memory for rx list "
210196f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
210296f2e892SBill Paul 			m_freem(m_new);
210396f2e892SBill Paul 			return(ENOBUFS);
210496f2e892SBill Paul 		}
210596f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
210696f2e892SBill Paul 	} else {
210796f2e892SBill Paul 		m_new = m;
210896f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
210996f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
211096f2e892SBill Paul 	}
211196f2e892SBill Paul 
211296f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
211396f2e892SBill Paul 
211496f2e892SBill Paul 	/*
211596f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
211696f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
211796f2e892SBill Paul 	 * 82c169 chips.
211896f2e892SBill Paul 	 */
211996f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
212096f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
212196f2e892SBill Paul 
212296f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
212396f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
212496f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
212596f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
212696f2e892SBill Paul 
212796f2e892SBill Paul 	return(0);
212896f2e892SBill Paul }
212996f2e892SBill Paul 
213096f2e892SBill Paul /*
213196f2e892SBill Paul  * Grrrrr.
213296f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
213396f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
213496f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
213596f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
213696f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
213796f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
213896f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
213996f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
214096f2e892SBill Paul  *
214196f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
214296f2e892SBill Paul  * Here's what we know:
214396f2e892SBill Paul  *
214496f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
214596f2e892SBill Paul  *   descriptors uploaded.
214696f2e892SBill Paul  *
214796f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
214896f2e892SBill Paul  *   total data upload.
214996f2e892SBill Paul  *
215096f2e892SBill Paul  * - We know the size of the desired received frame because it will be
215196f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
215296f2e892SBill Paul  *
215396f2e892SBill Paul  * Here's what we do:
215496f2e892SBill Paul  *
215596f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
215696f2e892SBill Paul  *   This means that we know that the buffer contents should be all
215796f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
215896f2e892SBill Paul  *
215996f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
216096f2e892SBill Paul  *   ethernet CRC at the end.
216196f2e892SBill Paul  *
216296f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
216396f2e892SBill Paul  *
216496f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
216596f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
216696f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
216796f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
216896f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
216996f2e892SBill Paul  *   we won't be fooled.
217096f2e892SBill Paul  *
217196f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
217296f2e892SBill Paul  *   that value from the current pointer location. This brings us
217396f2e892SBill Paul  *   to the start of the actual received packet.
217496f2e892SBill Paul  *
217596f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
217696f2e892SBill Paul  *   frame length.
217796f2e892SBill Paul  *
217896f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
217996f2e892SBill Paul  * the time.
218096f2e892SBill Paul  */
218196f2e892SBill Paul 
218296f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
218396f2e892SBill Paul static void dc_pnic_rx_bug_war(sc, idx)
218496f2e892SBill Paul 	struct dc_softc		*sc;
218596f2e892SBill Paul 	int			idx;
218696f2e892SBill Paul {
218796f2e892SBill Paul 	struct dc_desc		*cur_rx;
218896f2e892SBill Paul 	struct dc_desc		*c = NULL;
218996f2e892SBill Paul 	struct mbuf		*m = NULL;
219096f2e892SBill Paul 	unsigned char		*ptr;
219196f2e892SBill Paul 	int			i, total_len;
219296f2e892SBill Paul 	u_int32_t		rxstat = 0;
219396f2e892SBill Paul 
219496f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
219596f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
219696f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
219796f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
219896f2e892SBill Paul 
219996f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
220096f2e892SBill Paul 	while (1) {
220196f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
220296f2e892SBill Paul 		rxstat = c->dc_status;
220396f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
220496f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
220596f2e892SBill Paul 		ptr += DC_RXLEN;
220696f2e892SBill Paul 		/* If this is the last buffer, break out. */
220796f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
220896f2e892SBill Paul 			break;
220996f2e892SBill Paul 		dc_newbuf(sc, i, m);
221096f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
221196f2e892SBill Paul 	}
221296f2e892SBill Paul 
221396f2e892SBill Paul 	/* Find the length of the actual receive frame. */
221496f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
221596f2e892SBill Paul 
221696f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
221796f2e892SBill Paul 	while(*ptr == 0x00)
221896f2e892SBill Paul 		ptr--;
221996f2e892SBill Paul 
222096f2e892SBill Paul 	/* Round off. */
222196f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
222296f2e892SBill Paul 		ptr -= 1;
222396f2e892SBill Paul 
222496f2e892SBill Paul 	/* Now find the start of the frame. */
222596f2e892SBill Paul 	ptr -= total_len;
222696f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
222796f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
222896f2e892SBill Paul 
222996f2e892SBill Paul 	/*
223096f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
223196f2e892SBill Paul 	 * the status word to make it look like a successful
223296f2e892SBill Paul  	 * frame reception.
223396f2e892SBill Paul 	 */
223496f2e892SBill Paul 	dc_newbuf(sc, i, m);
223596f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
223696f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
223796f2e892SBill Paul 
223896f2e892SBill Paul 	return;
223996f2e892SBill Paul }
224096f2e892SBill Paul 
224196f2e892SBill Paul /*
224273bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
224373bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
224473bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
224573bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
224673bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
224773bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
224873bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
224973bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
225073bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
225173bf949cSBill Paul  */
225273bf949cSBill Paul static int dc_rx_resync(sc)
225373bf949cSBill Paul 	struct dc_softc		*sc;
225473bf949cSBill Paul {
225573bf949cSBill Paul 	int			i, pos;
225673bf949cSBill Paul 	struct dc_desc		*cur_rx;
225773bf949cSBill Paul 
225873bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
225973bf949cSBill Paul 
226073bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
226173bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
226273bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
226373bf949cSBill Paul 			break;
226473bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
226573bf949cSBill Paul 	}
226673bf949cSBill Paul 
226773bf949cSBill Paul 	/* If the ring really is empty, then just return. */
226873bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
226973bf949cSBill Paul 		return(0);
227073bf949cSBill Paul 
227173bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
227273bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
227373bf949cSBill Paul 
227473bf949cSBill Paul 	return(EAGAIN);
227573bf949cSBill Paul }
227673bf949cSBill Paul 
227773bf949cSBill Paul /*
227896f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
227996f2e892SBill Paul  * the higher level protocols.
228096f2e892SBill Paul  */
228196f2e892SBill Paul static void dc_rxeof(sc)
228296f2e892SBill Paul 	struct dc_softc		*sc;
228396f2e892SBill Paul {
228496f2e892SBill Paul         struct ether_header	*eh;
228596f2e892SBill Paul         struct mbuf		*m;
228696f2e892SBill Paul         struct ifnet		*ifp;
228796f2e892SBill Paul 	struct dc_desc		*cur_rx;
228896f2e892SBill Paul 	int			i, total_len = 0;
228996f2e892SBill Paul 	u_int32_t		rxstat;
229096f2e892SBill Paul 
229196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
229296f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
229396f2e892SBill Paul 
229496f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
229596f2e892SBill Paul 		struct mbuf		*m0 = NULL;
229696f2e892SBill Paul 
229796f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
229896f2e892SBill Paul 		rxstat = cur_rx->dc_status;
229996f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
230096f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
230196f2e892SBill Paul 
230296f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
230396f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
230496f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
230596f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
230696f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
230796f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
230896f2e892SBill Paul 					continue;
230996f2e892SBill Paul 				}
231096f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
231196f2e892SBill Paul 				rxstat = cur_rx->dc_status;
231296f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
231396f2e892SBill Paul 			}
231496f2e892SBill Paul 		}
231596f2e892SBill Paul 
231696f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
231796f2e892SBill Paul 
231896f2e892SBill Paul 		/*
231996f2e892SBill Paul 		 * If an error occurs, update stats, clear the
232096f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
232196f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
232296f2e892SBill Paul 	 	 * comes up in the ring.
232396f2e892SBill Paul 		 */
232496f2e892SBill Paul 		if (rxstat & DC_RXSTAT_RXERR) {
232596f2e892SBill Paul 			ifp->if_ierrors++;
232696f2e892SBill Paul 			if (rxstat & DC_RXSTAT_COLLSEEN)
232796f2e892SBill Paul 				ifp->if_collisions++;
232896f2e892SBill Paul 			dc_newbuf(sc, i, m);
232996f2e892SBill Paul 			if (rxstat & DC_RXSTAT_CRCERR) {
233096f2e892SBill Paul 				DC_INC(i, DC_RX_LIST_CNT);
233196f2e892SBill Paul 				continue;
233296f2e892SBill Paul 			} else {
233396f2e892SBill Paul 				dc_init(sc);
233496f2e892SBill Paul 				return;
233596f2e892SBill Paul 			}
233696f2e892SBill Paul 		}
233796f2e892SBill Paul 
233896f2e892SBill Paul 		/* No errors; receive the packet. */
233996f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
234096f2e892SBill Paul 
234196f2e892SBill Paul 		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
234296f2e892SBill Paul 		    total_len + ETHER_ALIGN, 0, ifp, NULL);
234396f2e892SBill Paul 		dc_newbuf(sc, i, m);
234496f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
234596f2e892SBill Paul 		if (m0 == NULL) {
234696f2e892SBill Paul 			ifp->if_ierrors++;
234796f2e892SBill Paul 			continue;
234896f2e892SBill Paul 		}
234996f2e892SBill Paul 		m_adj(m0, ETHER_ALIGN);
235096f2e892SBill Paul 		m = m0;
235196f2e892SBill Paul 
235296f2e892SBill Paul 		ifp->if_ipackets++;
235396f2e892SBill Paul 		eh = mtod(m, struct ether_header *);
235496f2e892SBill Paul 
235596f2e892SBill Paul 		/* Remove header from mbuf and pass it on. */
235696f2e892SBill Paul 		m_adj(m, sizeof(struct ether_header));
235796f2e892SBill Paul 		ether_input(ifp, eh, m);
235896f2e892SBill Paul 	}
235996f2e892SBill Paul 
236096f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
236196f2e892SBill Paul }
236296f2e892SBill Paul 
236396f2e892SBill Paul /*
236496f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
236596f2e892SBill Paul  * the list buffers.
236696f2e892SBill Paul  */
236796f2e892SBill Paul 
236896f2e892SBill Paul static void dc_txeof(sc)
236996f2e892SBill Paul 	struct dc_softc		*sc;
237096f2e892SBill Paul {
237196f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
237296f2e892SBill Paul 	struct ifnet		*ifp;
237396f2e892SBill Paul 	int			idx;
237496f2e892SBill Paul 
237596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
237696f2e892SBill Paul 
237796f2e892SBill Paul 	/* Clear the timeout timer. */
237896f2e892SBill Paul 	ifp->if_timer = 0;
237996f2e892SBill Paul 
238096f2e892SBill Paul 	/*
238196f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
238296f2e892SBill Paul 	 * frames that have been transmitted.
238396f2e892SBill Paul 	 */
238496f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
238596f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
238696f2e892SBill Paul 		u_int32_t		txstat;
238796f2e892SBill Paul 
238896f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
238996f2e892SBill Paul 		txstat = cur_tx->dc_status;
239096f2e892SBill Paul 
239196f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
239296f2e892SBill Paul 			break;
239396f2e892SBill Paul 
239496f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
239596f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
239696f2e892SBill Paul 			sc->dc_cdata.dc_tx_cnt--;
239796f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
239896f2e892SBill Paul 				/*
239996f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
240096f2e892SBill Paul 				 * that it will sometimes generate a TX
240196f2e892SBill Paul 				 * underrun error while DMAing the RX
240296f2e892SBill Paul 				 * filter setup frame. If we detect this,
240396f2e892SBill Paul 				 * we have to send the setup frame again,
240496f2e892SBill Paul 				 * or else the filter won't be programmed
240596f2e892SBill Paul 				 * correctly.
240696f2e892SBill Paul 				 */
240796f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
240896f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
240996f2e892SBill Paul 						dc_setfilt(sc);
241096f2e892SBill Paul 				}
241196f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
241296f2e892SBill Paul 			}
241396f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
241496f2e892SBill Paul 			continue;
241596f2e892SBill Paul 		}
241696f2e892SBill Paul 
241796f2e892SBill Paul 		if (/*sc->dc_type == DC_TYPE_21143 &&*/
241896f2e892SBill Paul 		    sc->dc_pmode == DC_PMODE_MII &&
241996f2e892SBill Paul 		    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
242096f2e892SBill Paul 		    DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
242196f2e892SBill Paul 			txstat &= ~DC_TXSTAT_ERRSUM;
242296f2e892SBill Paul 
242396f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
242496f2e892SBill Paul 			ifp->if_oerrors++;
242596f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
242696f2e892SBill Paul 				ifp->if_collisions++;
242796f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
242896f2e892SBill Paul 				ifp->if_collisions++;
242996f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
243096f2e892SBill Paul 				dc_init(sc);
243196f2e892SBill Paul 				return;
243296f2e892SBill Paul 			}
243396f2e892SBill Paul 		}
243496f2e892SBill Paul 
243596f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
243696f2e892SBill Paul 
243796f2e892SBill Paul 		ifp->if_opackets++;
243896f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
243996f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
244096f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
244196f2e892SBill Paul 		}
244296f2e892SBill Paul 
244396f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
244496f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
244596f2e892SBill Paul 	}
244696f2e892SBill Paul 
244796f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
244896f2e892SBill Paul 	if (cur_tx != NULL)
244996f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
245096f2e892SBill Paul 
245196f2e892SBill Paul 	return;
245296f2e892SBill Paul }
245396f2e892SBill Paul 
245496f2e892SBill Paul static void dc_tick(xsc)
245596f2e892SBill Paul 	void			*xsc;
245696f2e892SBill Paul {
245796f2e892SBill Paul 	struct dc_softc		*sc;
245896f2e892SBill Paul 	struct mii_data		*mii;
245996f2e892SBill Paul 	struct ifnet		*ifp;
246096f2e892SBill Paul 	int			s;
246196f2e892SBill Paul 	u_int32_t		r;
246296f2e892SBill Paul 
246396f2e892SBill Paul 	s = splimp();
246496f2e892SBill Paul 
246596f2e892SBill Paul 	sc = xsc;
246696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
246796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
246896f2e892SBill Paul 
246996f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2470318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2471318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2472318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2473318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
247496f2e892SBill Paul 				sc->dc_link = 0;
2475318b02fdSBill Paul 				mii_mediachg(mii);
2476318b02fdSBill Paul 			}
2477318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2478318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2479318b02fdSBill Paul 				sc->dc_link = 0;
2480318b02fdSBill Paul 				mii_mediachg(mii);
2481318b02fdSBill Paul 			}
2482d675147eSBill Paul 			if (sc->dc_link == 0)
248396f2e892SBill Paul 				mii_tick(mii);
248496f2e892SBill Paul 		} else {
2485318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
248696f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2487042c8f6eSBill Paul 			    sc->dc_cdata.dc_tx_cnt == 0)
248896f2e892SBill Paul 				mii_tick(mii);
2489042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2490042c8f6eSBill Paul 					sc->dc_link = 0;
249196f2e892SBill Paul 		}
249296f2e892SBill Paul 	} else
249396f2e892SBill Paul 		mii_tick(mii);
249496f2e892SBill Paul 
249596f2e892SBill Paul 	/*
249696f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
249796f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
249896f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
249996f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
250096f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
250196f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
250296f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
250396f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
250496f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
250596f2e892SBill Paul 	 * a screeching halt for several seconds.
250696f2e892SBill Paul 	 *
250796f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
250896f2e892SBill Paul 	 * any packets until a link has been established. After the
250996f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
251096f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
251196f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
251296f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
251396f2e892SBill Paul 	 */
251496f2e892SBill Paul 	if (!sc->dc_link) {
251596f2e892SBill Paul 		mii_pollstat(mii);
251696f2e892SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
251796f2e892SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
251896f2e892SBill Paul 			sc->dc_link++;
251996f2e892SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
252096f2e892SBill Paul 				dc_start(ifp);
252196f2e892SBill Paul 		}
252296f2e892SBill Paul 	}
252396f2e892SBill Paul 
2524318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2525318b02fdSBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz/10);
2526318b02fdSBill Paul 	else
252796f2e892SBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz);
252896f2e892SBill Paul 
252996f2e892SBill Paul 	splx(s);
253096f2e892SBill Paul 
253196f2e892SBill Paul 	return;
253296f2e892SBill Paul }
253396f2e892SBill Paul 
253496f2e892SBill Paul static void dc_intr(arg)
253596f2e892SBill Paul 	void			*arg;
253696f2e892SBill Paul {
253796f2e892SBill Paul 	struct dc_softc		*sc;
253896f2e892SBill Paul 	struct ifnet		*ifp;
253996f2e892SBill Paul 	u_int32_t		status;
254096f2e892SBill Paul 
254196f2e892SBill Paul 	sc = arg;
254296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
254396f2e892SBill Paul 
254496f2e892SBill Paul 	/* Supress unwanted interrupts */
254596f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
254696f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
254796f2e892SBill Paul 			dc_stop(sc);
254896f2e892SBill Paul 		return;
254996f2e892SBill Paul 	}
255096f2e892SBill Paul 
255196f2e892SBill Paul 	/* Disable interrupts. */
255296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
255396f2e892SBill Paul 
255496f2e892SBill Paul 	while((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) {
255596f2e892SBill Paul 
255696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
255796f2e892SBill Paul 
255873bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
255973bf949cSBill Paul 			int		curpkts;
256073bf949cSBill Paul 			curpkts = ifp->if_ipackets;
256196f2e892SBill Paul 			dc_rxeof(sc);
256273bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
256373bf949cSBill Paul 				while(dc_rx_resync(sc))
256473bf949cSBill Paul 					dc_rxeof(sc);
256573bf949cSBill Paul 			}
256673bf949cSBill Paul 		}
256796f2e892SBill Paul 
256896f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
256996f2e892SBill Paul 			dc_txeof(sc);
257096f2e892SBill Paul 
257196f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
257296f2e892SBill Paul 			dc_txeof(sc);
257396f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
257496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
257596f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
257696f2e892SBill Paul 			}
257796f2e892SBill Paul 		}
257896f2e892SBill Paul 
257996f2e892SBill Paul 		if (status & DC_ISR_TX_UNDERRUN) {
258096f2e892SBill Paul 			u_int32_t		cfg;
258196f2e892SBill Paul 
258296f2e892SBill Paul 			printf("dc%d: TX underrun -- ", sc->dc_unit);
258396f2e892SBill Paul 			if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc))
258496f2e892SBill Paul 				dc_init(sc);
258596f2e892SBill Paul 			cfg = CSR_READ_4(sc, DC_NETCFG);
258696f2e892SBill Paul 			cfg &= ~DC_NETCFG_TX_THRESH;
258796f2e892SBill Paul 			if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
258896f2e892SBill Paul 				printf("using store and forward mode\n");
258996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
259091cc2adbSBill Paul 			} else if (sc->dc_flags & DC_TX_STORENFWD) {
259191cc2adbSBill Paul 				printf("resetting\n");
259296f2e892SBill Paul 			} else {
259396f2e892SBill Paul 				sc->dc_txthresh += 0x4000;
259496f2e892SBill Paul 				printf("increasing TX threshold\n");
259596f2e892SBill Paul 				CSR_WRITE_4(sc, DC_NETCFG, cfg);
259696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
259796f2e892SBill Paul 				DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
259896f2e892SBill Paul 			}
259996f2e892SBill Paul 		}
260096f2e892SBill Paul 
260196f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
260273bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
260373bf949cSBill Paul 			int		curpkts;
260473bf949cSBill Paul 			curpkts = ifp->if_ipackets;
260596f2e892SBill Paul 			dc_rxeof(sc);
260673bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
260773bf949cSBill Paul 				while(dc_rx_resync(sc))
260873bf949cSBill Paul 					dc_rxeof(sc);
260973bf949cSBill Paul 			}
261073bf949cSBill Paul 		}
261196f2e892SBill Paul 
261296f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
261396f2e892SBill Paul 			dc_reset(sc);
261496f2e892SBill Paul 			dc_init(sc);
261596f2e892SBill Paul 		}
261696f2e892SBill Paul 	}
261796f2e892SBill Paul 
261896f2e892SBill Paul 	/* Re-enable interrupts. */
261996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
262096f2e892SBill Paul 
262196f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
262296f2e892SBill Paul 		dc_start(ifp);
262396f2e892SBill Paul 
262496f2e892SBill Paul 	return;
262596f2e892SBill Paul }
262696f2e892SBill Paul 
262796f2e892SBill Paul /*
262896f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
262996f2e892SBill Paul  * pointers to the fragment pointers.
263096f2e892SBill Paul  */
263196f2e892SBill Paul static int dc_encap(sc, m_head, txidx)
263296f2e892SBill Paul 	struct dc_softc		*sc;
263396f2e892SBill Paul 	struct mbuf		*m_head;
263496f2e892SBill Paul 	u_int32_t		*txidx;
263596f2e892SBill Paul {
263696f2e892SBill Paul 	struct dc_desc		*f = NULL;
263796f2e892SBill Paul 	struct mbuf		*m;
263896f2e892SBill Paul 	int			frag, cur, cnt = 0;
263996f2e892SBill Paul 
264096f2e892SBill Paul 	/*
264196f2e892SBill Paul  	 * Start packing the mbufs in this chain into
264296f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
264396f2e892SBill Paul  	 * of fragments or hit the end of the mbuf chain.
264496f2e892SBill Paul 	 */
264596f2e892SBill Paul 	m = m_head;
264696f2e892SBill Paul 	cur = frag = *txidx;
264796f2e892SBill Paul 
264896f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
264996f2e892SBill Paul 		if (m->m_len != 0) {
265096f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
265196f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
265296f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
265396f2e892SBill Paul 					return(ENOBUFS);
265496f2e892SBill Paul 			}
265596f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
265696f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
265796f2e892SBill Paul 				return(ENOBUFS);
265896f2e892SBill Paul 
265996f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
266096f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
266196f2e892SBill Paul 			if (cnt == 0) {
266296f2e892SBill Paul 				f->dc_status = 0;
266396f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
266496f2e892SBill Paul 			} else
266596f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
266696f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
266796f2e892SBill Paul 			cur = frag;
266896f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
266996f2e892SBill Paul 			cnt++;
267096f2e892SBill Paul 		}
267196f2e892SBill Paul 	}
267296f2e892SBill Paul 
267396f2e892SBill Paul 	if (m != NULL)
267496f2e892SBill Paul 		return(ENOBUFS);
267596f2e892SBill Paul 
267696f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
267796f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
267896f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
267996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
268096f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
268191cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
268291cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
268396f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
268496f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
268596f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
268696f2e892SBill Paul 	*txidx = frag;
268796f2e892SBill Paul 
268896f2e892SBill Paul 	return(0);
268996f2e892SBill Paul }
269096f2e892SBill Paul 
269196f2e892SBill Paul /*
2692fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
2693fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
2694fda39fd0SBill Paul  * do scatter/gather correctly.
2695fda39fd0SBill Paul  */
2696fda39fd0SBill Paul static int dc_coal(sc, m_head)
2697fda39fd0SBill Paul 	struct dc_softc		*sc;
2698fda39fd0SBill Paul 	struct mbuf		**m_head;
2699fda39fd0SBill Paul {
2700fda39fd0SBill Paul         struct mbuf		*m_new, *m;
2701fda39fd0SBill Paul 
2702fda39fd0SBill Paul 	m = *m_head;
2703fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
2704fda39fd0SBill Paul 	if (m_new == NULL) {
2705fda39fd0SBill Paul 		printf("dc%d: no memory for tx list", sc->dc_unit);
2706fda39fd0SBill Paul 		return(ENOBUFS);
2707fda39fd0SBill Paul 	}
2708fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
2709fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
2710fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
2711fda39fd0SBill Paul 			m_freem(m_new);
2712fda39fd0SBill Paul 			printf("dc%d: no memory for tx list", sc->dc_unit);
2713fda39fd0SBill Paul 			return(ENOBUFS);
2714fda39fd0SBill Paul 		}
2715fda39fd0SBill Paul 	}
2716fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
2717fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
2718fda39fd0SBill Paul 	m_freem(m);
2719fda39fd0SBill Paul 	*m_head = m_new;
2720fda39fd0SBill Paul 
2721fda39fd0SBill Paul 	return(0);
2722fda39fd0SBill Paul }
2723fda39fd0SBill Paul 
2724fda39fd0SBill Paul /*
272596f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
272696f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
272796f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
272896f2e892SBill Paul  * physical addresses.
272996f2e892SBill Paul  */
273096f2e892SBill Paul 
273196f2e892SBill Paul static void dc_start(ifp)
273296f2e892SBill Paul 	struct ifnet		*ifp;
273396f2e892SBill Paul {
273496f2e892SBill Paul 	struct dc_softc		*sc;
273596f2e892SBill Paul 	struct mbuf		*m_head = NULL;
273696f2e892SBill Paul 	int			idx;
273796f2e892SBill Paul 
273896f2e892SBill Paul 	sc = ifp->if_softc;
273996f2e892SBill Paul 
274096f2e892SBill Paul 	if (!sc->dc_link)
274196f2e892SBill Paul 		return;
274296f2e892SBill Paul 
274396f2e892SBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
274496f2e892SBill Paul 		return;
274596f2e892SBill Paul 
274696f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
274796f2e892SBill Paul 
274896f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
274996f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
275096f2e892SBill Paul 		if (m_head == NULL)
275196f2e892SBill Paul 			break;
275296f2e892SBill Paul 
2753fda39fd0SBill Paul 		if (sc->dc_flags & DC_TX_COALESCE) {
2754fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
2755fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
2756fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
2757fda39fd0SBill Paul 				break;
2758fda39fd0SBill Paul 			}
2759fda39fd0SBill Paul 		}
2760fda39fd0SBill Paul 
276196f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
276296f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
276396f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
276496f2e892SBill Paul 			break;
276596f2e892SBill Paul 		}
276696f2e892SBill Paul 
276796f2e892SBill Paul 		/*
276896f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
276996f2e892SBill Paul 		 * to him.
277096f2e892SBill Paul 		 */
277196f2e892SBill Paul 		if (ifp->if_bpf)
277296f2e892SBill Paul 			bpf_mtap(ifp, m_head);
27735c1cfac4SBill Paul 
27745c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
27755c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
27765c1cfac4SBill Paul 			break;
27775c1cfac4SBill Paul 		}
277896f2e892SBill Paul 	}
277996f2e892SBill Paul 
278096f2e892SBill Paul 	/* Transmit */
278196f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
278296f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
278396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
278496f2e892SBill Paul 
278596f2e892SBill Paul 	/*
278696f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
278796f2e892SBill Paul 	 */
278896f2e892SBill Paul 	ifp->if_timer = 5;
278996f2e892SBill Paul 
279096f2e892SBill Paul 	return;
279196f2e892SBill Paul }
279296f2e892SBill Paul 
279396f2e892SBill Paul static void dc_init(xsc)
279496f2e892SBill Paul 	void			*xsc;
279596f2e892SBill Paul {
279696f2e892SBill Paul 	struct dc_softc		*sc = xsc;
279796f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
279896f2e892SBill Paul 	struct mii_data		*mii;
279996f2e892SBill Paul 	int			s;
280096f2e892SBill Paul 
280196f2e892SBill Paul 	s = splimp();
280296f2e892SBill Paul 
280396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
280496f2e892SBill Paul 
280596f2e892SBill Paul 	/*
280696f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
280796f2e892SBill Paul 	 */
280896f2e892SBill Paul 	dc_stop(sc);
280996f2e892SBill Paul 	dc_reset(sc);
281096f2e892SBill Paul 
281196f2e892SBill Paul 	/*
281296f2e892SBill Paul 	 * Set cache alignment and burst length.
281396f2e892SBill Paul 	 */
281488d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
281596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
281696f2e892SBill Paul 	else
281796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
281896f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
281996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
282096f2e892SBill Paul 	} else {
282196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
282296f2e892SBill Paul 	}
282396f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
282496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
282596f2e892SBill Paul 	switch(sc->dc_cachesize) {
282696f2e892SBill Paul 	case 32:
282796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
282896f2e892SBill Paul 		break;
282996f2e892SBill Paul 	case 16:
283096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
283196f2e892SBill Paul 		break;
283296f2e892SBill Paul 	case 8:
283396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
283496f2e892SBill Paul 		break;
283596f2e892SBill Paul 	case 0:
283696f2e892SBill Paul 	default:
283796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
283896f2e892SBill Paul 		break;
283996f2e892SBill Paul 	}
284096f2e892SBill Paul 
284196f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
284296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
284396f2e892SBill Paul 	else {
284496f2e892SBill Paul 		if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
284596f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
284696f2e892SBill Paul 		} else {
284796f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
284896f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
284996f2e892SBill Paul 		}
285096f2e892SBill Paul 	}
285196f2e892SBill Paul 
285296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
285396f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
285496f2e892SBill Paul 
285596f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
285696f2e892SBill Paul 		/*
285796f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
285896f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
285996f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
286096f2e892SBill Paul 		 * document the meaning of these bits so there's no way
286196f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
286296f2e892SBill Paul 		 * number all its own; the rest all use a different one.
286396f2e892SBill Paul 		 */
286496f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
286596f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
286696f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
286796f2e892SBill Paul 		else
286896f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
286996f2e892SBill Paul 	}
287096f2e892SBill Paul 
287196f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
287296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_72BYTES);
287396f2e892SBill Paul 
287496f2e892SBill Paul 	/* Init circular RX list. */
287596f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
287696f2e892SBill Paul 		printf("dc%d: initialization failed: no "
287796f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
287896f2e892SBill Paul 		dc_stop(sc);
287996f2e892SBill Paul 		(void)splx(s);
288096f2e892SBill Paul 		return;
288196f2e892SBill Paul 	}
288296f2e892SBill Paul 
288396f2e892SBill Paul 	/*
288496f2e892SBill Paul 	 * Init tx descriptors.
288596f2e892SBill Paul 	 */
288696f2e892SBill Paul 	dc_list_tx_init(sc);
288796f2e892SBill Paul 
288896f2e892SBill Paul 	/*
288996f2e892SBill Paul 	 * Load the address of the RX list.
289096f2e892SBill Paul 	 */
289196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
289296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
289396f2e892SBill Paul 
289496f2e892SBill Paul 	/*
289596f2e892SBill Paul 	 * Enable interrupts.
289696f2e892SBill Paul 	 */
289796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
289896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
289996f2e892SBill Paul 
290096f2e892SBill Paul 	/* Enable transmitter. */
290196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
290296f2e892SBill Paul 
290396f2e892SBill Paul 	/*
2904918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
2905918434c8SBill Paul 	 * MII port, program the LED control pins so we get
2906918434c8SBill Paul 	 * link and activity indications.
2907918434c8SBill Paul 	 */
290878999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
2909918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
2910918434c8SBill Paul 		    DC_WDOG_CTLWREN|DC_WDOG_LINK|DC_WDOG_ACTIVITY);
291178999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
2912918434c8SBill Paul 	}
2913918434c8SBill Paul 
2914918434c8SBill Paul 	/*
291596f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
291696f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
291796f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
291896f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
291996f2e892SBill Paul 	 */
292096f2e892SBill Paul 	dc_setfilt(sc);
292196f2e892SBill Paul 
292296f2e892SBill Paul 	/* Enable receiver. */
292396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
292496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
292596f2e892SBill Paul 
292696f2e892SBill Paul 	mii_mediachg(mii);
292796f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
292896f2e892SBill Paul 
292996f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
293096f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
293196f2e892SBill Paul 
293296f2e892SBill Paul 	(void)splx(s);
293396f2e892SBill Paul 
2934857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
2935857fd445SBill Paul 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_homePNA)
2936857fd445SBill Paul 		sc->dc_link = 1;
2937857fd445SBill Paul 	else {
2938318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
2939318b02fdSBill Paul 			sc->dc_stat_ch = timeout(dc_tick, sc, hz/10);
2940318b02fdSBill Paul 		else
294196f2e892SBill Paul 			sc->dc_stat_ch = timeout(dc_tick, sc, hz);
2942857fd445SBill Paul 	}
294396f2e892SBill Paul 
29445c1cfac4SBill Paul #ifdef SRM_MEDIA
2945510a809eSMike Smith         if(sc->dc_srm_media) {
2946510a809eSMike Smith 		struct ifreq ifr;
2947510a809eSMike Smith 
2948510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
2949510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
2950510a809eSMike Smith 		sc->dc_srm_media = 0;
2951510a809eSMike Smith 	}
2952510a809eSMike Smith #endif
295396f2e892SBill Paul 	return;
295496f2e892SBill Paul }
295596f2e892SBill Paul 
295696f2e892SBill Paul /*
295796f2e892SBill Paul  * Set media options.
295896f2e892SBill Paul  */
295996f2e892SBill Paul static int dc_ifmedia_upd(ifp)
296096f2e892SBill Paul 	struct ifnet		*ifp;
296196f2e892SBill Paul {
296296f2e892SBill Paul 	struct dc_softc		*sc;
296396f2e892SBill Paul 	struct mii_data		*mii;
2964f43d9309SBill Paul 	struct ifmedia		*ifm;
296596f2e892SBill Paul 
296696f2e892SBill Paul 	sc = ifp->if_softc;
296796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
296896f2e892SBill Paul 	mii_mediachg(mii);
2969f43d9309SBill Paul 	ifm = &mii->mii_media;
2970f43d9309SBill Paul 
2971f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
2972f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA)
2973f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
2974f43d9309SBill Paul 	else
297596f2e892SBill Paul 		sc->dc_link = 0;
297696f2e892SBill Paul 
297796f2e892SBill Paul 	return(0);
297896f2e892SBill Paul }
297996f2e892SBill Paul 
298096f2e892SBill Paul /*
298196f2e892SBill Paul  * Report current media status.
298296f2e892SBill Paul  */
298396f2e892SBill Paul static void dc_ifmedia_sts(ifp, ifmr)
298496f2e892SBill Paul 	struct ifnet		*ifp;
298596f2e892SBill Paul 	struct ifmediareq	*ifmr;
298696f2e892SBill Paul {
298796f2e892SBill Paul 	struct dc_softc		*sc;
298896f2e892SBill Paul 	struct mii_data		*mii;
2989f43d9309SBill Paul 	struct ifmedia		*ifm;
299096f2e892SBill Paul 
299196f2e892SBill Paul 	sc = ifp->if_softc;
299296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
299396f2e892SBill Paul 	mii_pollstat(mii);
2994f43d9309SBill Paul 	ifm = &mii->mii_media;
2995f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
2996f43d9309SBill Paul 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
2997f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
2998f43d9309SBill Paul 			ifmr->ifm_status = 0;
2999f43d9309SBill Paul 			return;
3000f43d9309SBill Paul 		}
3001f43d9309SBill Paul 	}
300296f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
300396f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
300496f2e892SBill Paul 
300596f2e892SBill Paul 	return;
300696f2e892SBill Paul }
300796f2e892SBill Paul 
300896f2e892SBill Paul static int dc_ioctl(ifp, command, data)
300996f2e892SBill Paul 	struct ifnet		*ifp;
301096f2e892SBill Paul 	u_long			command;
301196f2e892SBill Paul 	caddr_t			data;
301296f2e892SBill Paul {
301396f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
301496f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
301596f2e892SBill Paul 	struct mii_data		*mii;
301696f2e892SBill Paul 	int			s, error = 0;
301796f2e892SBill Paul 
301896f2e892SBill Paul 	s = splimp();
301996f2e892SBill Paul 
302096f2e892SBill Paul 	switch(command) {
302196f2e892SBill Paul 	case SIOCSIFADDR:
302296f2e892SBill Paul 	case SIOCGIFADDR:
302396f2e892SBill Paul 	case SIOCSIFMTU:
302496f2e892SBill Paul 		error = ether_ioctl(ifp, command, data);
302596f2e892SBill Paul 		break;
302696f2e892SBill Paul 	case SIOCSIFFLAGS:
302796f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
302896f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
302996f2e892SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
303096f2e892SBill Paul 			    !(sc->dc_if_flags & IFF_PROMISC)) {
303196f2e892SBill Paul 				dc_setfilt(sc);
303296f2e892SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
303396f2e892SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
303496f2e892SBill Paul 			    sc->dc_if_flags & IFF_PROMISC) {
303596f2e892SBill Paul 				dc_setfilt(sc);
303696f2e892SBill Paul 			} else if (!(ifp->if_flags & IFF_RUNNING)) {
303796f2e892SBill Paul 				sc->dc_txthresh = 0;
303896f2e892SBill Paul 				dc_init(sc);
303996f2e892SBill Paul 			}
304096f2e892SBill Paul 		} else {
304196f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
304296f2e892SBill Paul 				dc_stop(sc);
304396f2e892SBill Paul 		}
304496f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
304596f2e892SBill Paul 		error = 0;
304696f2e892SBill Paul 		break;
304796f2e892SBill Paul 	case SIOCADDMULTI:
304896f2e892SBill Paul 	case SIOCDELMULTI:
304996f2e892SBill Paul 		dc_setfilt(sc);
305096f2e892SBill Paul 		error = 0;
305196f2e892SBill Paul 		break;
305296f2e892SBill Paul 	case SIOCGIFMEDIA:
305396f2e892SBill Paul 	case SIOCSIFMEDIA:
305496f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
305596f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
30565c1cfac4SBill Paul #ifdef SRM_MEDIA
3057510a809eSMike Smith 		if (sc->dc_srm_media)
3058510a809eSMike Smith 			sc->dc_srm_media = 0;
3059510a809eSMike Smith #endif
306096f2e892SBill Paul 		break;
306196f2e892SBill Paul 	default:
306296f2e892SBill Paul 		error = EINVAL;
306396f2e892SBill Paul 		break;
306496f2e892SBill Paul 	}
306596f2e892SBill Paul 
306696f2e892SBill Paul 	(void)splx(s);
306796f2e892SBill Paul 
306896f2e892SBill Paul 	return(error);
306996f2e892SBill Paul }
307096f2e892SBill Paul 
307196f2e892SBill Paul static void dc_watchdog(ifp)
307296f2e892SBill Paul 	struct ifnet		*ifp;
307396f2e892SBill Paul {
307496f2e892SBill Paul 	struct dc_softc		*sc;
307596f2e892SBill Paul 
307696f2e892SBill Paul 	sc = ifp->if_softc;
307796f2e892SBill Paul 
307896f2e892SBill Paul 	ifp->if_oerrors++;
307996f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
308096f2e892SBill Paul 
308196f2e892SBill Paul 	dc_stop(sc);
308296f2e892SBill Paul 	dc_reset(sc);
308396f2e892SBill Paul 	dc_init(sc);
308496f2e892SBill Paul 
308596f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
308696f2e892SBill Paul 		dc_start(ifp);
308796f2e892SBill Paul 
308896f2e892SBill Paul 	return;
308996f2e892SBill Paul }
309096f2e892SBill Paul 
309196f2e892SBill Paul /*
309296f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
309396f2e892SBill Paul  * RX and TX lists.
309496f2e892SBill Paul  */
309596f2e892SBill Paul static void dc_stop(sc)
309696f2e892SBill Paul 	struct dc_softc		*sc;
309796f2e892SBill Paul {
309896f2e892SBill Paul 	register int		i;
309996f2e892SBill Paul 	struct ifnet		*ifp;
310096f2e892SBill Paul 
310196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
310296f2e892SBill Paul 	ifp->if_timer = 0;
310396f2e892SBill Paul 
310496f2e892SBill Paul 	untimeout(dc_tick, sc, sc->dc_stat_ch);
310596f2e892SBill Paul 
310696f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
310796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
310896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
310996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
311096f2e892SBill Paul 	sc->dc_link = 0;
311196f2e892SBill Paul 
311296f2e892SBill Paul 	/*
311396f2e892SBill Paul 	 * Free data in the RX lists.
311496f2e892SBill Paul 	 */
311596f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
311696f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
311796f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
311896f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
311996f2e892SBill Paul 		}
312096f2e892SBill Paul 	}
312196f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
312296f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
312396f2e892SBill Paul 
312496f2e892SBill Paul 	/*
312596f2e892SBill Paul 	 * Free the TX list buffers.
312696f2e892SBill Paul 	 */
312796f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
312896f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
312996f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
313096f2e892SBill Paul 			    DC_TXCTL_SETUP) {
313196f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
313296f2e892SBill Paul 				continue;
313396f2e892SBill Paul 			}
313496f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
313596f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
313696f2e892SBill Paul 		}
313796f2e892SBill Paul 	}
313896f2e892SBill Paul 
313996f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
314096f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
314196f2e892SBill Paul 
314296f2e892SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
314396f2e892SBill Paul 
314496f2e892SBill Paul 	return;
314596f2e892SBill Paul }
314696f2e892SBill Paul 
314796f2e892SBill Paul /*
314896f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
314996f2e892SBill Paul  * get confused by errant DMAs when rebooting.
315096f2e892SBill Paul  */
315196f2e892SBill Paul static void dc_shutdown(dev)
315296f2e892SBill Paul 	device_t		dev;
315396f2e892SBill Paul {
315496f2e892SBill Paul 	struct dc_softc		*sc;
315596f2e892SBill Paul 
315696f2e892SBill Paul 	sc = device_get_softc(dev);
315796f2e892SBill Paul 
315896f2e892SBill Paul 	dc_stop(sc);
315996f2e892SBill Paul 
316096f2e892SBill Paul 	return;
316196f2e892SBill Paul }
3162