xref: /freebsd/sys/dev/dc/if_dc.c (revision bf645417629c28dc024f71b8662d5d7f0ef44d4e)
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  * Knowing which media is available on a given card is tough: you're
7796f2e892SBill Paul  * supposed to go slogging through the EEPROM looking for media
7896f2e892SBill Paul  * description structures. Unfortunately, some card vendors that use
7996f2e892SBill Paul  * the 21143 don't obey the DEC SROM spec correctly, which means that
8096f2e892SBill Paul  * what you find in the EEPROM may not agree with reality. Fortunately,
8196f2e892SBill Paul  * the 21143 provides us a way to get around this issue: lurking in
8296f2e892SBill Paul  * PCI configuration space is the Configuration Wake-Up Command Register.
8396f2e892SBill Paul  * This register is loaded with a value from the EEPROM when wake on LAN
8496f2e892SBill Paul  * mode is enabled; this value tells us quite clearly what kind of media
8596f2e892SBill Paul  * is attached to the NIC. The main purpose of this register is to tell
8696f2e892SBill Paul  * the NIC what media to scan when in wake on LAN mode, however by
8796f2e892SBill Paul  * forcibly enabling wake on LAN mode, we can use to learn what kind of
8896f2e892SBill Paul  * media a given NIC has available and adapt ourselves accordingly.
8996f2e892SBill Paul  *
9096f2e892SBill Paul  * Of course, if the media description blocks in the EEPROM are bogus.
9196f2e892SBill Paul  * what are the odds that the CWUC aren't bogus as well, right? Well,
9296f2e892SBill Paul  * the CWUC value is more likely to be correct since wake on LAN mode
9396f2e892SBill Paul  * won't work correctly without it, and wake on LAN is a big selling
9496f2e892SBill Paul  * point these days. It's also harder to screw up a single byte than
9596f2e892SBill Paul  * a whole media descriptor block.
9696f2e892SBill Paul  *
9796f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
9896f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
9996f2e892SBill Paul  * handled separately due to its different register offsets and the
10096f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
10196f2e892SBill Paul  * here, but I'm not thrilled about it.
10296f2e892SBill Paul  *
10396f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
10496f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
10596f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
10696f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
10796f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
10896f2e892SBill Paul  */
10996f2e892SBill Paul 
11096f2e892SBill Paul #include <sys/param.h>
11196f2e892SBill Paul #include <sys/systm.h>
11296f2e892SBill Paul #include <sys/sockio.h>
11396f2e892SBill Paul #include <sys/mbuf.h>
11496f2e892SBill Paul #include <sys/malloc.h>
11596f2e892SBill Paul #include <sys/kernel.h>
11696f2e892SBill Paul #include <sys/socket.h>
11796f2e892SBill Paul 
11896f2e892SBill Paul #include <net/if.h>
11996f2e892SBill Paul #include <net/if_arp.h>
12096f2e892SBill Paul #include <net/ethernet.h>
12196f2e892SBill Paul #include <net/if_dl.h>
12296f2e892SBill Paul #include <net/if_media.h>
12396f2e892SBill Paul 
12496f2e892SBill Paul #include <net/bpf.h>
12596f2e892SBill Paul 
12696f2e892SBill Paul #include <vm/vm.h>              /* for vtophys */
12796f2e892SBill Paul #include <vm/pmap.h>            /* for vtophys */
12896f2e892SBill Paul #include <machine/clock.h>      /* for DELAY */
12996f2e892SBill Paul #include <machine/bus_pio.h>
13096f2e892SBill Paul #include <machine/bus_memio.h>
13196f2e892SBill Paul #include <machine/bus.h>
13296f2e892SBill Paul #include <machine/resource.h>
13396f2e892SBill Paul #include <sys/bus.h>
13496f2e892SBill Paul #include <sys/rman.h>
13596f2e892SBill Paul 
13696f2e892SBill Paul #include <dev/mii/mii.h>
13796f2e892SBill Paul #include <dev/mii/miivar.h>
13896f2e892SBill Paul 
13996f2e892SBill Paul #include <pci/pcireg.h>
14096f2e892SBill Paul #include <pci/pcivar.h>
14196f2e892SBill Paul 
14296f2e892SBill Paul #define DC_USEIOSPACE
14396f2e892SBill Paul 
14496f2e892SBill Paul #include <pci/if_dcreg.h>
14596f2e892SBill Paul 
14695a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14795a16455SPeter Wemm 
14896f2e892SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
14996f2e892SBill Paul #include "miibus_if.h"
15096f2e892SBill Paul 
15196f2e892SBill Paul #ifndef lint
15296f2e892SBill Paul static const char rcsid[] =
15396f2e892SBill Paul   "$FreeBSD$";
15496f2e892SBill Paul #endif
15596f2e892SBill Paul 
15696f2e892SBill Paul /*
15796f2e892SBill Paul  * Various supported device vendors/types and their names.
15896f2e892SBill Paul  */
15996f2e892SBill Paul static struct dc_type dc_devs[] = {
16096f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
16196f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
16296f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
16396f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
16496f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
16596f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
16688d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
16788d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
16896f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
16996f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
17096f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
17196f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
17296f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
17396f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
17496f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
17596f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
17696f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
17796f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
17896f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
17996f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
18096f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
18196f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
18296f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
18396f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
18496f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18596f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
18696f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18779d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
18879d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18996f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
190ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
191ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
19296f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
19396f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
19496f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
19596f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
19696f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
19796f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1989ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1999ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
20096f2e892SBill Paul 	{ 0, 0, NULL }
20196f2e892SBill Paul };
20296f2e892SBill Paul 
20396f2e892SBill Paul static int dc_probe		__P((device_t));
20496f2e892SBill Paul static int dc_attach		__P((device_t));
20596f2e892SBill Paul static int dc_detach		__P((device_t));
20696f2e892SBill Paul static void dc_acpi		__P((device_t));
20796f2e892SBill Paul static struct dc_type *dc_devtype	__P((device_t));
20896f2e892SBill Paul static int dc_newbuf		__P((struct dc_softc *, int, struct mbuf *));
20996f2e892SBill Paul static int dc_encap		__P((struct dc_softc *, struct mbuf *,
21096f2e892SBill Paul 					u_int32_t *));
211fda39fd0SBill Paul static int dc_coal		__P((struct dc_softc *, struct mbuf **));
21296f2e892SBill Paul static void dc_pnic_rx_bug_war	__P((struct dc_softc *, int));
21373bf949cSBill Paul static int dc_rx_resync		__P((struct dc_softc *));
21496f2e892SBill Paul static void dc_rxeof		__P((struct dc_softc *));
21596f2e892SBill Paul static void dc_txeof		__P((struct dc_softc *));
21696f2e892SBill Paul static void dc_tick		__P((void *));
21796f2e892SBill Paul static void dc_intr		__P((void *));
21896f2e892SBill Paul static void dc_start		__P((struct ifnet *));
21996f2e892SBill Paul static int dc_ioctl		__P((struct ifnet *, u_long, caddr_t));
22096f2e892SBill Paul static void dc_init		__P((void *));
22196f2e892SBill Paul static void dc_stop		__P((struct dc_softc *));
22296f2e892SBill Paul static void dc_watchdog		__P((struct ifnet *));
22396f2e892SBill Paul static void dc_shutdown		__P((device_t));
22496f2e892SBill Paul static int dc_ifmedia_upd	__P((struct ifnet *));
22596f2e892SBill Paul static void dc_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
22696f2e892SBill Paul 
22796f2e892SBill Paul static void dc_delay		__P((struct dc_softc *));
22896f2e892SBill Paul static void dc_eeprom_idle	__P((struct dc_softc *));
22996f2e892SBill Paul static void dc_eeprom_putbyte	__P((struct dc_softc *, int));
23096f2e892SBill Paul static void dc_eeprom_getword	__P((struct dc_softc *, int, u_int16_t *));
23196f2e892SBill Paul static void dc_eeprom_getword_pnic
23296f2e892SBill Paul 				__P((struct dc_softc *, int, u_int16_t *));
23396f2e892SBill Paul static void dc_read_eeprom	__P((struct dc_softc *, caddr_t, int,
23496f2e892SBill Paul 							int, int));
23596f2e892SBill Paul 
23696f2e892SBill Paul static void dc_mii_writebit	__P((struct dc_softc *, int));
23796f2e892SBill Paul static int dc_mii_readbit	__P((struct dc_softc *));
23896f2e892SBill Paul static void dc_mii_sync		__P((struct dc_softc *));
23996f2e892SBill Paul static void dc_mii_send		__P((struct dc_softc *, u_int32_t, int));
24096f2e892SBill Paul static int dc_mii_readreg	__P((struct dc_softc *, struct dc_mii_frame *));
24196f2e892SBill Paul static int dc_mii_writereg	__P((struct dc_softc *, struct dc_mii_frame *));
24296f2e892SBill Paul static int dc_miibus_readreg	__P((device_t, int, int));
24396f2e892SBill Paul static int dc_miibus_writereg	__P((device_t, int, int, int));
24496f2e892SBill Paul static void dc_miibus_statchg	__P((device_t));
245f43d9309SBill Paul static void dc_miibus_mediainit	__P((device_t));
24696f2e892SBill Paul 
24796f2e892SBill Paul static void dc_setcfg		__P((struct dc_softc *, int));
24896f2e892SBill Paul static u_int32_t dc_crc_le	__P((struct dc_softc *, caddr_t));
24996f2e892SBill Paul static u_int32_t dc_crc_be	__P((caddr_t));
25096f2e892SBill Paul static void dc_setfilt_21143	__P((struct dc_softc *));
25196f2e892SBill Paul static void dc_setfilt_asix	__P((struct dc_softc *));
25296f2e892SBill Paul static void dc_setfilt_admtek	__P((struct dc_softc *));
25396f2e892SBill Paul 
25496f2e892SBill Paul static void dc_setfilt		__P((struct dc_softc *));
25596f2e892SBill Paul 
25696f2e892SBill Paul static void dc_reset		__P((struct dc_softc *));
25796f2e892SBill Paul static int dc_list_rx_init	__P((struct dc_softc *));
25896f2e892SBill Paul static int dc_list_tx_init	__P((struct dc_softc *));
25996f2e892SBill Paul 
26096f2e892SBill Paul #ifdef DC_USEIOSPACE
26196f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
26296f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
26396f2e892SBill Paul #else
26496f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
26596f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
26696f2e892SBill Paul #endif
26796f2e892SBill Paul 
26896f2e892SBill Paul static device_method_t dc_methods[] = {
26996f2e892SBill Paul 	/* Device interface */
27096f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
27196f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
27296f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
27396f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
27496f2e892SBill Paul 
27596f2e892SBill Paul 	/* bus interface */
27696f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
27796f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
27896f2e892SBill Paul 
27996f2e892SBill Paul 	/* MII interface */
28096f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
28196f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
28296f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
283f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
28496f2e892SBill Paul 
28596f2e892SBill Paul 	{ 0, 0 }
28696f2e892SBill Paul };
28796f2e892SBill Paul 
28896f2e892SBill Paul static driver_t dc_driver = {
28996f2e892SBill Paul 	"dc",
29096f2e892SBill Paul 	dc_methods,
29196f2e892SBill Paul 	sizeof(struct dc_softc)
29296f2e892SBill Paul };
29396f2e892SBill Paul 
29496f2e892SBill Paul static devclass_t dc_devclass;
29596f2e892SBill Paul 
29696f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
29796f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
29896f2e892SBill Paul 
29996f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
30096f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
30196f2e892SBill Paul 
30296f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
30396f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
30496f2e892SBill Paul 
30596f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
30696f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
30796f2e892SBill Paul 
30896f2e892SBill Paul static void dc_delay(sc)
30996f2e892SBill Paul 	struct dc_softc		*sc;
31096f2e892SBill Paul {
31196f2e892SBill Paul 	int			idx;
31296f2e892SBill Paul 
31396f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
31496f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
31596f2e892SBill Paul }
31696f2e892SBill Paul 
31796f2e892SBill Paul static void dc_eeprom_idle(sc)
31896f2e892SBill Paul 	struct dc_softc		*sc;
31996f2e892SBill Paul {
32096f2e892SBill Paul 	register int		i;
32196f2e892SBill Paul 
32296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
32396f2e892SBill Paul 	dc_delay(sc);
32496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
32596f2e892SBill Paul 	dc_delay(sc);
32696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
32796f2e892SBill Paul 	dc_delay(sc);
32896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
32996f2e892SBill Paul 	dc_delay(sc);
33096f2e892SBill Paul 
33196f2e892SBill Paul 	for (i = 0; i < 25; i++) {
33296f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33396f2e892SBill Paul 		dc_delay(sc);
33496f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33596f2e892SBill Paul 		dc_delay(sc);
33696f2e892SBill Paul 	}
33796f2e892SBill Paul 
33896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33996f2e892SBill Paul 	dc_delay(sc);
34096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
34196f2e892SBill Paul 	dc_delay(sc);
34296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
34396f2e892SBill Paul 
34496f2e892SBill Paul 	return;
34596f2e892SBill Paul }
34696f2e892SBill Paul 
34796f2e892SBill Paul /*
34896f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
34996f2e892SBill Paul  */
35096f2e892SBill Paul static void dc_eeprom_putbyte(sc, addr)
35196f2e892SBill Paul 	struct dc_softc		*sc;
35296f2e892SBill Paul 	int			addr;
35396f2e892SBill Paul {
35496f2e892SBill Paul 	register int		d, i;
35596f2e892SBill Paul 
35696f2e892SBill Paul 	/*
35796f2e892SBill Paul 	 * The AN985 has a 93C66 EEPROM on it instead of
35896f2e892SBill Paul 	 * a 93C46. It uses a different bit sequence for
35996f2e892SBill Paul 	 * specifying the "read" opcode.
36096f2e892SBill Paul 	 */
36196f2e892SBill Paul 	if (DC_IS_CENTAUR(sc))
36296f2e892SBill Paul 		d = addr | (DC_EECMD_READ << 2);
36396f2e892SBill Paul 	else
36496f2e892SBill Paul 		d = addr | DC_EECMD_READ;
36596f2e892SBill Paul 
36696f2e892SBill Paul 	/*
36796f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
36896f2e892SBill Paul 	 */
36996f2e892SBill Paul 	for (i = 0x400; i; i >>= 1) {
37096f2e892SBill Paul 		if (d & i) {
37196f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
37296f2e892SBill Paul 		} else {
37396f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
37496f2e892SBill Paul 		}
37596f2e892SBill Paul 		dc_delay(sc);
37696f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
37796f2e892SBill Paul 		dc_delay(sc);
37896f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
37996f2e892SBill Paul 		dc_delay(sc);
38096f2e892SBill Paul 	}
38196f2e892SBill Paul 
38296f2e892SBill Paul 	return;
38396f2e892SBill Paul }
38496f2e892SBill Paul 
38596f2e892SBill Paul /*
38696f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
38796f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
38896f2e892SBill Paul  * the EEPROM.
38996f2e892SBill Paul  */
39096f2e892SBill Paul static void dc_eeprom_getword_pnic(sc, addr, dest)
39196f2e892SBill Paul 	struct dc_softc		*sc;
39296f2e892SBill Paul 	int			addr;
39396f2e892SBill Paul 	u_int16_t		*dest;
39496f2e892SBill Paul {
39596f2e892SBill Paul 	register int		i;
39696f2e892SBill Paul 	u_int32_t		r;
39796f2e892SBill Paul 
39896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
39996f2e892SBill Paul 
40096f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
40196f2e892SBill Paul 		DELAY(1);
40296f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
40396f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
40496f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
40596f2e892SBill Paul 			return;
40696f2e892SBill Paul 		}
40796f2e892SBill Paul 	}
40896f2e892SBill Paul 
40996f2e892SBill Paul 	return;
41096f2e892SBill Paul }
41196f2e892SBill Paul 
41296f2e892SBill Paul /*
41396f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
41496f2e892SBill Paul  */
41596f2e892SBill Paul static void dc_eeprom_getword(sc, addr, dest)
41696f2e892SBill Paul 	struct dc_softc		*sc;
41796f2e892SBill Paul 	int			addr;
41896f2e892SBill Paul 	u_int16_t		*dest;
41996f2e892SBill Paul {
42096f2e892SBill Paul 	register int		i;
42196f2e892SBill Paul 	u_int16_t		word = 0;
42296f2e892SBill Paul 
42396f2e892SBill Paul 	/* Force EEPROM to idle state. */
42496f2e892SBill Paul 	dc_eeprom_idle(sc);
42596f2e892SBill Paul 
42696f2e892SBill Paul 	/* Enter EEPROM access mode. */
42796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
42896f2e892SBill Paul 	dc_delay(sc);
42996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
43096f2e892SBill Paul 	dc_delay(sc);
43196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43296f2e892SBill Paul 	dc_delay(sc);
43396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
43496f2e892SBill Paul 	dc_delay(sc);
43596f2e892SBill Paul 
43696f2e892SBill Paul 	/*
43796f2e892SBill Paul 	 * Send address of word we want to read.
43896f2e892SBill Paul 	 */
43996f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
44096f2e892SBill Paul 
44196f2e892SBill Paul 	/*
44296f2e892SBill Paul 	 * Start reading bits from EEPROM.
44396f2e892SBill Paul 	 */
44496f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
44596f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
44696f2e892SBill Paul 		dc_delay(sc);
44796f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
44896f2e892SBill Paul 			word |= i;
44996f2e892SBill Paul 		dc_delay(sc);
45096f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
45196f2e892SBill Paul 		dc_delay(sc);
45296f2e892SBill Paul 	}
45396f2e892SBill Paul 
45496f2e892SBill Paul 	/* Turn off EEPROM access mode. */
45596f2e892SBill Paul 	dc_eeprom_idle(sc);
45696f2e892SBill Paul 
45796f2e892SBill Paul 	*dest = word;
45896f2e892SBill Paul 
45996f2e892SBill Paul 	return;
46096f2e892SBill Paul }
46196f2e892SBill Paul 
46296f2e892SBill Paul /*
46396f2e892SBill Paul  * Read a sequence of words from the EEPROM.
46496f2e892SBill Paul  */
46596f2e892SBill Paul static void dc_read_eeprom(sc, dest, off, cnt, swap)
46696f2e892SBill Paul 	struct dc_softc		*sc;
46796f2e892SBill Paul 	caddr_t			dest;
46896f2e892SBill Paul 	int			off;
46996f2e892SBill Paul 	int			cnt;
47096f2e892SBill Paul 	int			swap;
47196f2e892SBill Paul {
47296f2e892SBill Paul 	int			i;
47396f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
47496f2e892SBill Paul 
47596f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
47696f2e892SBill Paul 		if (DC_IS_PNIC(sc))
47796f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
47896f2e892SBill Paul 		else
47996f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
48096f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
48196f2e892SBill Paul 		if (swap)
48296f2e892SBill Paul 			*ptr = ntohs(word);
48396f2e892SBill Paul 		else
48496f2e892SBill Paul 			*ptr = word;
48596f2e892SBill Paul 	}
48696f2e892SBill Paul 
48796f2e892SBill Paul 	return;
48896f2e892SBill Paul }
48996f2e892SBill Paul 
49096f2e892SBill Paul /*
49196f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
49296f2e892SBill Paul  * Application Notes pp.19-21.
49396f2e892SBill Paul  */
49496f2e892SBill Paul /*
49596f2e892SBill Paul  * Write a bit to the MII bus.
49696f2e892SBill Paul  */
49796f2e892SBill Paul static void dc_mii_writebit(sc, bit)
49896f2e892SBill Paul 	struct dc_softc		*sc;
49996f2e892SBill Paul 	int			bit;
50096f2e892SBill Paul {
50196f2e892SBill Paul 	if (bit)
50296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
50396f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
50496f2e892SBill Paul 	else
50596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
50696f2e892SBill Paul 
50796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
50896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
50996f2e892SBill Paul 
51096f2e892SBill Paul 	return;
51196f2e892SBill Paul }
51296f2e892SBill Paul 
51396f2e892SBill Paul /*
51496f2e892SBill Paul  * Read a bit from the MII bus.
51596f2e892SBill Paul  */
51696f2e892SBill Paul static int dc_mii_readbit(sc)
51796f2e892SBill Paul 	struct dc_softc		*sc;
51896f2e892SBill Paul {
51996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
52096f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
52196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
52296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
52396f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
52496f2e892SBill Paul 		return(1);
52596f2e892SBill Paul 
52696f2e892SBill Paul 	return(0);
52796f2e892SBill Paul }
52896f2e892SBill Paul 
52996f2e892SBill Paul /*
53096f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
53196f2e892SBill Paul  */
53296f2e892SBill Paul static void dc_mii_sync(sc)
53396f2e892SBill Paul 	struct dc_softc		*sc;
53496f2e892SBill Paul {
53596f2e892SBill Paul 	register int		i;
53696f2e892SBill Paul 
53796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
53896f2e892SBill Paul 
53996f2e892SBill Paul 	for (i = 0; i < 32; i++)
54096f2e892SBill Paul 		dc_mii_writebit(sc, 1);
54196f2e892SBill Paul 
54296f2e892SBill Paul 	return;
54396f2e892SBill Paul }
54496f2e892SBill Paul 
54596f2e892SBill Paul /*
54696f2e892SBill Paul  * Clock a series of bits through the MII.
54796f2e892SBill Paul  */
54896f2e892SBill Paul static void dc_mii_send(sc, bits, cnt)
54996f2e892SBill Paul 	struct dc_softc		*sc;
55096f2e892SBill Paul 	u_int32_t		bits;
55196f2e892SBill Paul 	int			cnt;
55296f2e892SBill Paul {
55396f2e892SBill Paul 	int			i;
55496f2e892SBill Paul 
55596f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
55696f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
55796f2e892SBill Paul }
55896f2e892SBill Paul 
55996f2e892SBill Paul /*
56096f2e892SBill Paul  * Read an PHY register through the MII.
56196f2e892SBill Paul  */
56296f2e892SBill Paul static int dc_mii_readreg(sc, frame)
56396f2e892SBill Paul 	struct dc_softc		*sc;
56496f2e892SBill Paul 	struct dc_mii_frame	*frame;
56596f2e892SBill Paul 
56696f2e892SBill Paul {
56796f2e892SBill Paul 	int			i, ack, s;
56896f2e892SBill Paul 
56996f2e892SBill Paul 	s = splimp();
57096f2e892SBill Paul 
57196f2e892SBill Paul 	/*
57296f2e892SBill Paul 	 * Set up frame for RX.
57396f2e892SBill Paul 	 */
57496f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
57596f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
57696f2e892SBill Paul 	frame->mii_turnaround = 0;
57796f2e892SBill Paul 	frame->mii_data = 0;
57896f2e892SBill Paul 
57996f2e892SBill Paul 	/*
58096f2e892SBill Paul 	 * Sync the PHYs.
58196f2e892SBill Paul 	 */
58296f2e892SBill Paul 	dc_mii_sync(sc);
58396f2e892SBill Paul 
58496f2e892SBill Paul 	/*
58596f2e892SBill Paul 	 * Send command/address info.
58696f2e892SBill Paul 	 */
58796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
58896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
58996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
59096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
59196f2e892SBill Paul 
59296f2e892SBill Paul #ifdef notdef
59396f2e892SBill Paul 	/* Idle bit */
59496f2e892SBill Paul 	dc_mii_writebit(sc, 1);
59596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
59696f2e892SBill Paul #endif
59796f2e892SBill Paul 
59896f2e892SBill Paul 	/* Check for ack */
59996f2e892SBill Paul 	ack = dc_mii_readbit(sc);
60096f2e892SBill Paul 
60196f2e892SBill Paul 	/*
60296f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
60396f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
60496f2e892SBill Paul 	 */
60596f2e892SBill Paul 	if (ack) {
60696f2e892SBill Paul 		for(i = 0; i < 16; i++) {
60796f2e892SBill Paul 			dc_mii_readbit(sc);
60896f2e892SBill Paul 		}
60996f2e892SBill Paul 		goto fail;
61096f2e892SBill Paul 	}
61196f2e892SBill Paul 
61296f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
61396f2e892SBill Paul 		if (!ack) {
61496f2e892SBill Paul 			if (dc_mii_readbit(sc))
61596f2e892SBill Paul 				frame->mii_data |= i;
61696f2e892SBill Paul 		}
61796f2e892SBill Paul 	}
61896f2e892SBill Paul 
61996f2e892SBill Paul fail:
62096f2e892SBill Paul 
62196f2e892SBill Paul 	dc_mii_writebit(sc, 0);
62296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
62396f2e892SBill Paul 
62496f2e892SBill Paul 	splx(s);
62596f2e892SBill Paul 
62696f2e892SBill Paul 	if (ack)
62796f2e892SBill Paul 		return(1);
62896f2e892SBill Paul 	return(0);
62996f2e892SBill Paul }
63096f2e892SBill Paul 
63196f2e892SBill Paul /*
63296f2e892SBill Paul  * Write to a PHY register through the MII.
63396f2e892SBill Paul  */
63496f2e892SBill Paul static int dc_mii_writereg(sc, frame)
63596f2e892SBill Paul 	struct dc_softc		*sc;
63696f2e892SBill Paul 	struct dc_mii_frame	*frame;
63796f2e892SBill Paul 
63896f2e892SBill Paul {
63996f2e892SBill Paul 	int			s;
64096f2e892SBill Paul 
64196f2e892SBill Paul 	s = splimp();
64296f2e892SBill Paul 	/*
64396f2e892SBill Paul 	 * Set up frame for TX.
64496f2e892SBill Paul 	 */
64596f2e892SBill Paul 
64696f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
64796f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
64896f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
64996f2e892SBill Paul 
65096f2e892SBill Paul 	/*
65196f2e892SBill Paul 	 * Sync the PHYs.
65296f2e892SBill Paul 	 */
65396f2e892SBill Paul 	dc_mii_sync(sc);
65496f2e892SBill Paul 
65596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
65696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
65796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
65896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
65996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
66096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
66196f2e892SBill Paul 
66296f2e892SBill Paul 	/* Idle bit. */
66396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
66496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
66596f2e892SBill Paul 
66696f2e892SBill Paul 	splx(s);
66796f2e892SBill Paul 
66896f2e892SBill Paul 	return(0);
66996f2e892SBill Paul }
67096f2e892SBill Paul 
67196f2e892SBill Paul static int dc_miibus_readreg(dev, phy, reg)
67296f2e892SBill Paul 	device_t		dev;
67396f2e892SBill Paul 	int			phy, reg;
67496f2e892SBill Paul {
67596f2e892SBill Paul 	struct dc_mii_frame	frame;
67696f2e892SBill Paul 	struct dc_softc		*sc;
67796f2e892SBill Paul 	int			i, rval, phy_reg;
67896f2e892SBill Paul 
67996f2e892SBill Paul 	sc = device_get_softc(dev);
68096f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
68196f2e892SBill Paul 
68296f2e892SBill Paul 	/*
68396f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
68496f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
68596f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
68696f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
68796f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
68896f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
68996f2e892SBill Paul 	 * that the PHY is at MII address 1.
69096f2e892SBill Paul 	 */
69196f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
69296f2e892SBill Paul 		return(0);
69396f2e892SBill Paul 
69496f2e892SBill Paul 	if (sc->dc_pmode == DC_PMODE_SYM) {
69596f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
69696f2e892SBill Paul 			switch(reg) {
69796f2e892SBill Paul 			case MII_BMSR:
69896f2e892SBill Paul 			/*
69996f2e892SBill Paul 			 * Fake something to make the probe
70096f2e892SBill Paul 			 * code think there's a PHY here.
70196f2e892SBill Paul 			 */
70296f2e892SBill Paul 				return(BMSR_MEDIAMASK);
70396f2e892SBill Paul 				break;
70496f2e892SBill Paul 			case MII_PHYIDR1:
70596f2e892SBill Paul 				if (DC_IS_PNIC(sc))
70696f2e892SBill Paul 					return(DC_VENDORID_LO);
70796f2e892SBill Paul 				return(DC_VENDORID_DEC);
70896f2e892SBill Paul 				break;
70996f2e892SBill Paul 			case MII_PHYIDR2:
71096f2e892SBill Paul 				if (DC_IS_PNIC(sc))
71196f2e892SBill Paul 					return(DC_DEVICEID_82C168);
71296f2e892SBill Paul 				return(DC_DEVICEID_21143);
71396f2e892SBill Paul 				break;
71496f2e892SBill Paul 			default:
71596f2e892SBill Paul 				return(0);
71696f2e892SBill Paul 				break;
71796f2e892SBill Paul 			}
71896f2e892SBill Paul 		} else
71996f2e892SBill Paul 			return(0);
72096f2e892SBill Paul 	}
72196f2e892SBill Paul 
72296f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
72396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
72496f2e892SBill Paul 		    (phy << 23) | (reg << 18));
72596f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
72696f2e892SBill Paul 			DELAY(1);
72796f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
72896f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
72996f2e892SBill Paul 				rval &= 0xFFFF;
73096f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
73196f2e892SBill Paul 			}
73296f2e892SBill Paul 		}
73396f2e892SBill Paul 		return(0);
73496f2e892SBill Paul 	}
73596f2e892SBill Paul 
73696f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
73796f2e892SBill Paul 		switch(reg) {
73896f2e892SBill Paul 		case MII_BMCR:
73996f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
74096f2e892SBill Paul 			break;
74196f2e892SBill Paul 		case MII_BMSR:
74296f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
74396f2e892SBill Paul 			break;
74496f2e892SBill Paul 		case MII_PHYIDR1:
74596f2e892SBill Paul 			phy_reg = DC_AL_VENID;
74696f2e892SBill Paul 			break;
74796f2e892SBill Paul 		case MII_PHYIDR2:
74896f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
74996f2e892SBill Paul 			break;
75096f2e892SBill Paul 		case MII_ANAR:
75196f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
75296f2e892SBill Paul 			break;
75396f2e892SBill Paul 		case MII_ANLPAR:
75496f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
75596f2e892SBill Paul 			break;
75696f2e892SBill Paul 		case MII_ANER:
75796f2e892SBill Paul 			phy_reg = DC_AL_ANER;
75896f2e892SBill Paul 			break;
75996f2e892SBill Paul 		default:
76096f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
76196f2e892SBill Paul 			    sc->dc_unit, reg);
76296f2e892SBill Paul 			return(0);
76396f2e892SBill Paul 			break;
76496f2e892SBill Paul 		}
76596f2e892SBill Paul 
76696f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
76796f2e892SBill Paul 
76896f2e892SBill Paul 		if (rval == 0xFFFF)
76996f2e892SBill Paul 			return(0);
77096f2e892SBill Paul 		return(rval);
77196f2e892SBill Paul 	}
77296f2e892SBill Paul 
77396f2e892SBill Paul 	frame.mii_phyaddr = phy;
77496f2e892SBill Paul 	frame.mii_regaddr = reg;
775f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
776f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
77796f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
778f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
77996f2e892SBill Paul 
78096f2e892SBill Paul 	return(frame.mii_data);
78196f2e892SBill Paul }
78296f2e892SBill Paul 
78396f2e892SBill Paul static int dc_miibus_writereg(dev, phy, reg, data)
78496f2e892SBill Paul 	device_t		dev;
78596f2e892SBill Paul 	int			phy, reg, data;
78696f2e892SBill Paul {
78796f2e892SBill Paul 	struct dc_softc		*sc;
78896f2e892SBill Paul 	struct dc_mii_frame	frame;
78996f2e892SBill Paul 	int			i, phy_reg;
79096f2e892SBill Paul 
79196f2e892SBill Paul 	sc = device_get_softc(dev);
79296f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
79396f2e892SBill Paul 
79496f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
79596f2e892SBill Paul 		return(0);
79696f2e892SBill Paul 
79796f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
79896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
79996f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
80096f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
80196f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
80296f2e892SBill Paul 				break;
80396f2e892SBill Paul 		}
80496f2e892SBill Paul 		return(0);
80596f2e892SBill Paul 	}
80696f2e892SBill Paul 
80796f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
80896f2e892SBill Paul 		switch(reg) {
80996f2e892SBill Paul 		case MII_BMCR:
81096f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
81196f2e892SBill Paul 			break;
81296f2e892SBill Paul 		case MII_BMSR:
81396f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
81496f2e892SBill Paul 			break;
81596f2e892SBill Paul 		case MII_PHYIDR1:
81696f2e892SBill Paul 			phy_reg = DC_AL_VENID;
81796f2e892SBill Paul 			break;
81896f2e892SBill Paul 		case MII_PHYIDR2:
81996f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
82096f2e892SBill Paul 			break;
82196f2e892SBill Paul 		case MII_ANAR:
82296f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
82396f2e892SBill Paul 			break;
82496f2e892SBill Paul 		case MII_ANLPAR:
82596f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
82696f2e892SBill Paul 			break;
82796f2e892SBill Paul 		case MII_ANER:
82896f2e892SBill Paul 			phy_reg = DC_AL_ANER;
82996f2e892SBill Paul 			break;
83096f2e892SBill Paul 		default:
83196f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
83296f2e892SBill Paul 			    sc->dc_unit, reg);
83396f2e892SBill Paul 			return(0);
83496f2e892SBill Paul 			break;
83596f2e892SBill Paul 		}
83696f2e892SBill Paul 
83796f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
83896f2e892SBill Paul 		return(0);
83996f2e892SBill Paul 	}
84096f2e892SBill Paul 
84196f2e892SBill Paul 	frame.mii_phyaddr = phy;
84296f2e892SBill Paul 	frame.mii_regaddr = reg;
84396f2e892SBill Paul 	frame.mii_data = data;
84496f2e892SBill Paul 
845f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
846f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
84796f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
848f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
84996f2e892SBill Paul 
85096f2e892SBill Paul 	return(0);
85196f2e892SBill Paul }
85296f2e892SBill Paul 
85396f2e892SBill Paul static void dc_miibus_statchg(dev)
85496f2e892SBill Paul 	device_t		dev;
85596f2e892SBill Paul {
85696f2e892SBill Paul 	struct dc_softc		*sc;
85796f2e892SBill Paul 	struct mii_data		*mii;
858f43d9309SBill Paul 	struct ifmedia		*ifm;
85996f2e892SBill Paul 
86096f2e892SBill Paul 	sc = device_get_softc(dev);
86196f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
86296f2e892SBill Paul 		return;
86396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
864f43d9309SBill Paul 	ifm = &mii->mii_media;
865f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
866f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
867f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
868f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
869f43d9309SBill Paul 	} else {
87096f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
87196f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
872f43d9309SBill Paul 	}
873f43d9309SBill Paul 
874f43d9309SBill Paul 	return;
875f43d9309SBill Paul }
876f43d9309SBill Paul 
877f43d9309SBill Paul /*
878f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
879f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
880f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
881f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
882f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
883f43d9309SBill Paul  * with it itself. *sigh*
884f43d9309SBill Paul  */
885f43d9309SBill Paul static void dc_miibus_mediainit(dev)
886f43d9309SBill Paul 	device_t		dev;
887f43d9309SBill Paul {
888f43d9309SBill Paul 	struct dc_softc		*sc;
889f43d9309SBill Paul 	struct mii_data		*mii;
890f43d9309SBill Paul 	struct ifmedia		*ifm;
891f43d9309SBill Paul 	int			rev;
892f43d9309SBill Paul 
893f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
894f43d9309SBill Paul 
895f43d9309SBill Paul 	sc = device_get_softc(dev);
896f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
897f43d9309SBill Paul 	ifm = &mii->mii_media;
898f43d9309SBill Paul 
899f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
900f43d9309SBill Paul 		ifmedia_add(ifm, IFM_ETHER|IFM_homePNA, 0, NULL);
90196f2e892SBill Paul 
90296f2e892SBill Paul 	return;
90396f2e892SBill Paul }
90496f2e892SBill Paul 
90596f2e892SBill Paul #define DC_POLY		0xEDB88320
90679d11e09SBill Paul #define DC_BITS_512	9
90779d11e09SBill Paul #define DC_BITS_128	7
90879d11e09SBill Paul #define DC_BITS_64	6
90996f2e892SBill Paul 
91096f2e892SBill Paul static u_int32_t dc_crc_le(sc, addr)
91196f2e892SBill Paul 	struct dc_softc		*sc;
91296f2e892SBill Paul 	caddr_t			addr;
91396f2e892SBill Paul {
91496f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
91596f2e892SBill Paul 
91696f2e892SBill Paul 	/* Compute CRC for the address value. */
91796f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
91896f2e892SBill Paul 
91996f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
92096f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
92196f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
92296f2e892SBill Paul 	}
92396f2e892SBill Paul 
92479d11e09SBill Paul 	/*
92579d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
92679d11e09SBill Paul 	 * chips is only 128 bits wide.
92779d11e09SBill Paul 	 */
92879d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
92979d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
93096f2e892SBill Paul 
93179d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
93279d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
93379d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
93479d11e09SBill Paul 
93579d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
93696f2e892SBill Paul }
93796f2e892SBill Paul 
93896f2e892SBill Paul /*
93996f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
94096f2e892SBill Paul  */
94196f2e892SBill Paul static u_int32_t dc_crc_be(addr)
94296f2e892SBill Paul 	caddr_t			addr;
94396f2e892SBill Paul {
94496f2e892SBill Paul 	u_int32_t		crc, carry;
94596f2e892SBill Paul 	int			i, j;
94696f2e892SBill Paul 	u_int8_t		c;
94796f2e892SBill Paul 
94896f2e892SBill Paul 	/* Compute CRC for the address value. */
94996f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
95096f2e892SBill Paul 
95196f2e892SBill Paul 	for (i = 0; i < 6; i++) {
95296f2e892SBill Paul 		c = *(addr + i);
95396f2e892SBill Paul 		for (j = 0; j < 8; j++) {
95496f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
95596f2e892SBill Paul 			crc <<= 1;
95696f2e892SBill Paul 			c >>= 1;
95796f2e892SBill Paul 			if (carry)
95896f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
95996f2e892SBill Paul 		}
96096f2e892SBill Paul 	}
96196f2e892SBill Paul 
96296f2e892SBill Paul 	/* return the filter bit position */
96396f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
96496f2e892SBill Paul }
96596f2e892SBill Paul 
96696f2e892SBill Paul /*
96796f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
96896f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
96996f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
97096f2e892SBill Paul  *
97196f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
97296f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
97396f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
97496f2e892SBill Paul  * we need that too.
97596f2e892SBill Paul  */
97696f2e892SBill Paul void dc_setfilt_21143(sc)
97796f2e892SBill Paul 	struct dc_softc		*sc;
97896f2e892SBill Paul {
97996f2e892SBill Paul 	struct dc_desc		*sframe;
98096f2e892SBill Paul 	u_int32_t		h, *sp;
98196f2e892SBill Paul 	struct ifmultiaddr	*ifma;
98296f2e892SBill Paul 	struct ifnet		*ifp;
98396f2e892SBill Paul 	int			i;
98496f2e892SBill Paul 
98596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
98696f2e892SBill Paul 
98796f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
98896f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
98996f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
99096f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
99196f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
99296f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
99396f2e892SBill Paul 
99496f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
99596f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
99696f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
99796f2e892SBill Paul 
99896f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
99996f2e892SBill Paul 
100096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
100196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
100296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
100396f2e892SBill Paul 	else
100496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
100596f2e892SBill Paul 
100696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
100796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100896f2e892SBill Paul 	else
100996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
101096f2e892SBill Paul 
101196f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
101296f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
101396f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
101496f2e892SBill Paul 			continue;
101596f2e892SBill Paul 		h = dc_crc_le(sc,
101696f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
101796f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
101896f2e892SBill Paul 	}
101996f2e892SBill Paul 
102096f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
102196f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
102296f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
102396f2e892SBill Paul 	}
102496f2e892SBill Paul 
102596f2e892SBill Paul 	/* Set our MAC address */
102696f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
102796f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
102896f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
102996f2e892SBill Paul 
103096f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
103196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
103296f2e892SBill Paul 
103396f2e892SBill Paul 	/*
103496f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
103596f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
103696f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
103796f2e892SBill Paul 	 * medicine.
103896f2e892SBill Paul 	 */
103996f2e892SBill Paul 	DELAY(10000);
104096f2e892SBill Paul 
104196f2e892SBill Paul 	ifp->if_timer = 5;
104296f2e892SBill Paul 
104396f2e892SBill Paul 	return;
104496f2e892SBill Paul }
104596f2e892SBill Paul 
104696f2e892SBill Paul void dc_setfilt_admtek(sc)
104796f2e892SBill Paul 	struct dc_softc		*sc;
104896f2e892SBill Paul {
104996f2e892SBill Paul 	struct ifnet		*ifp;
105096f2e892SBill Paul 	int			h = 0;
105196f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
105296f2e892SBill Paul 	struct ifmultiaddr	*ifma;
105396f2e892SBill Paul 
105496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
105596f2e892SBill Paul 
105696f2e892SBill Paul 	/* Init our MAC address */
105796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
105896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
105996f2e892SBill Paul 
106096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
106196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
106296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106396f2e892SBill Paul 	else
106496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106596f2e892SBill Paul 
106696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
106796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106896f2e892SBill Paul 	else
106996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
107096f2e892SBill Paul 
107196f2e892SBill Paul 	/* first, zot all the existing hash bits */
107296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
107396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
107496f2e892SBill Paul 
107596f2e892SBill Paul 	/*
107696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
107796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
107896f2e892SBill Paul 	 */
107996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
108096f2e892SBill Paul 		return;
108196f2e892SBill Paul 
108296f2e892SBill Paul 	/* now program new ones */
108396f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
108496f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
108596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
108696f2e892SBill Paul 			continue;
108796f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
108896f2e892SBill Paul 		if (h < 32)
108996f2e892SBill Paul 			hashes[0] |= (1 << h);
109096f2e892SBill Paul 		else
109196f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
109296f2e892SBill Paul 	}
109396f2e892SBill Paul 
109496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
109596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
109696f2e892SBill Paul 
109796f2e892SBill Paul 	return;
109896f2e892SBill Paul }
109996f2e892SBill Paul 
110096f2e892SBill Paul void dc_setfilt_asix(sc)
110196f2e892SBill Paul 	struct dc_softc		*sc;
110296f2e892SBill Paul {
110396f2e892SBill Paul 	struct ifnet		*ifp;
110496f2e892SBill Paul 	int			h = 0;
110596f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
110696f2e892SBill Paul 	struct ifmultiaddr	*ifma;
110796f2e892SBill Paul 
110896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
110996f2e892SBill Paul 
111096f2e892SBill Paul         /* Init our MAC address */
111196f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
111296f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
111396f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
111496f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
111596f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
111696f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
111796f2e892SBill Paul 
111896f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
111996f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
112096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112196f2e892SBill Paul 	else
112296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112396f2e892SBill Paul 
112496f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
112596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112696f2e892SBill Paul 	else
112796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
112896f2e892SBill Paul 
112996f2e892SBill Paul 	/*
113096f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
113196f2e892SBill Paul 	 * of broadcast frames.
113296f2e892SBill Paul 	 */
113396f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
113496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113596f2e892SBill Paul 	else
113696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
113796f2e892SBill Paul 
113896f2e892SBill Paul 	/* first, zot all the existing hash bits */
113996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
114096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
114296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
114396f2e892SBill Paul 
114496f2e892SBill Paul 	/*
114596f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
114696f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
114796f2e892SBill Paul 	 */
114896f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
114996f2e892SBill Paul 		return;
115096f2e892SBill Paul 
115196f2e892SBill Paul 	/* now program new ones */
115296f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
115396f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
115496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
115596f2e892SBill Paul 			continue;
115696f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
115796f2e892SBill Paul 		if (h < 32)
115896f2e892SBill Paul 			hashes[0] |= (1 << h);
115996f2e892SBill Paul 		else
116096f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
116196f2e892SBill Paul 	}
116296f2e892SBill Paul 
116396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
116496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
116596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
116696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
116796f2e892SBill Paul 
116896f2e892SBill Paul 	return;
116996f2e892SBill Paul }
117096f2e892SBill Paul 
117196f2e892SBill Paul static void dc_setfilt(sc)
117296f2e892SBill Paul 	struct dc_softc		*sc;
117396f2e892SBill Paul {
117496f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
117596f2e892SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc))
117696f2e892SBill Paul 		dc_setfilt_21143(sc);
117796f2e892SBill Paul 
117896f2e892SBill Paul 	if (DC_IS_ASIX(sc))
117996f2e892SBill Paul 		dc_setfilt_asix(sc);
118096f2e892SBill Paul 
118196f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
118296f2e892SBill Paul 		dc_setfilt_admtek(sc);
118396f2e892SBill Paul 
118496f2e892SBill Paul 	return;
118596f2e892SBill Paul }
118696f2e892SBill Paul 
118796f2e892SBill Paul /*
118896f2e892SBill Paul  * In order to fiddle with the
118996f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
119096f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
119196f2e892SBill Paul  */
119296f2e892SBill Paul static void dc_setcfg(sc, media)
119396f2e892SBill Paul 	struct dc_softc		*sc;
119496f2e892SBill Paul 	int			media;
119596f2e892SBill Paul {
119696f2e892SBill Paul 	int			i, restart = 0;
119796f2e892SBill Paul 	u_int32_t		isr;
119896f2e892SBill Paul 
119996f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
120096f2e892SBill Paul 		return;
120196f2e892SBill Paul 
120296f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
120396f2e892SBill Paul 		restart = 1;
120496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
120596f2e892SBill Paul 
120696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
120796f2e892SBill Paul 			DELAY(10);
120896f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
120996f2e892SBill Paul 			if (isr & DC_ISR_TX_IDLE ||
121096f2e892SBill Paul 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED)
121196f2e892SBill Paul 				break;
121296f2e892SBill Paul 		}
121396f2e892SBill Paul 
121496f2e892SBill Paul 		if (i == DC_TIMEOUT)
121596f2e892SBill Paul 			printf("dc%d: failed to force tx and "
121696f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
121796f2e892SBill Paul 
121896f2e892SBill Paul 	}
121996f2e892SBill Paul 
122096f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1221042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1222042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
122396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
12248273d5f8SBill Paul 			int	watchdogreg;
12258273d5f8SBill Paul 
1226bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
12278273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
12288273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
12298273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
12308273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
1231bf645417SBill Paul 			} else {
1232bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1233bf645417SBill Paul 			}
123496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
123596f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
123696f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
123796f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
123896f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
123988d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
124096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
124196f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
124296f2e892SBill Paul 		} else {
124396f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
124496f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
124596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
124696f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
124796f2e892SBill Paul 			}
1248318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1249318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1250318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
125196f2e892SBill Paul 		}
125296f2e892SBill Paul 	}
125396f2e892SBill Paul 
125496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1255042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1256042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
125796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
12588273d5f8SBill Paul 			int	watchdogreg;
12598273d5f8SBill Paul 
12608273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
12618273d5f8SBill Paul 			watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
12628273d5f8SBill Paul 			watchdogreg &= ~DC_WDOG_CTLWREN;
12638273d5f8SBill Paul 			watchdogreg |= DC_WDOG_JABBERDIS;
12648273d5f8SBill Paul 			CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
126596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
126696f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
126796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
126896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
126988d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
127096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
127196f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
127296f2e892SBill Paul 		} else {
127396f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
127496f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
127596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
127696f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
127796f2e892SBill Paul 			}
127896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1279318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
128096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
128196f2e892SBill Paul 		}
128296f2e892SBill Paul 	}
128396f2e892SBill Paul 
1284f43d9309SBill Paul 	/*
1285f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1286f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1287f43d9309SBill Paul 	 * on the external MII port.
1288f43d9309SBill Paul 	 */
1289f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
1290f43d9309SBill Paul 		if (IFM_SUBTYPE(media) == IFM_homePNA) {
1291f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1292f43d9309SBill Paul 			sc->dc_link = 1;
1293f43d9309SBill Paul 		} else {
1294f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1295f43d9309SBill Paul 		}
1296f43d9309SBill Paul 	}
1297f43d9309SBill Paul 
129896f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
129996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
130096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
130196f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
130296f2e892SBill Paul 	} else {
130396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
130496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
130596f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
130696f2e892SBill Paul 	}
130796f2e892SBill Paul 
130896f2e892SBill Paul 	if (restart)
130996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
131096f2e892SBill Paul 
131196f2e892SBill Paul 	return;
131296f2e892SBill Paul }
131396f2e892SBill Paul 
131496f2e892SBill Paul static void dc_reset(sc)
131596f2e892SBill Paul 	struct dc_softc		*sc;
131696f2e892SBill Paul {
131796f2e892SBill Paul 	register int		i;
131896f2e892SBill Paul 
131996f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
132096f2e892SBill Paul 
132196f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
132296f2e892SBill Paul 		DELAY(10);
132396f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
132496f2e892SBill Paul 			break;
132596f2e892SBill Paul 	}
132696f2e892SBill Paul 
132796f2e892SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc)) {
132896f2e892SBill Paul 		DELAY(10000);
132996f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
133096f2e892SBill Paul 		i = 0;
133196f2e892SBill Paul 	}
133296f2e892SBill Paul 
133396f2e892SBill Paul 	if (i == DC_TIMEOUT)
133496f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
133596f2e892SBill Paul 
133696f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
133796f2e892SBill Paul 	DELAY(1000);
133896f2e892SBill Paul 
133996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
134096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
134196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
134296f2e892SBill Paul 
134391cc2adbSBill Paul 	/*
134491cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
134591cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
134691cc2adbSBill Paul 	 * into a state where it will never come out of reset
134791cc2adbSBill Paul 	 * until we reset the whole chip again.
134891cc2adbSBill Paul 	 */
134991cc2adbSBill Paul 	if (DC_IS_INTEL(sc))
135091cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
135191cc2adbSBill Paul 
135296f2e892SBill Paul         return;
135396f2e892SBill Paul }
135496f2e892SBill Paul 
135596f2e892SBill Paul static struct dc_type *dc_devtype(dev)
135696f2e892SBill Paul 	device_t		dev;
135796f2e892SBill Paul {
135896f2e892SBill Paul 	struct dc_type		*t;
135996f2e892SBill Paul 	u_int32_t		rev;
136096f2e892SBill Paul 
136196f2e892SBill Paul 	t = dc_devs;
136296f2e892SBill Paul 
136396f2e892SBill Paul 	while(t->dc_name != NULL) {
136496f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
136596f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
136696f2e892SBill Paul 			/* Check the PCI revision */
136796f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
136896f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
136996f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
137096f2e892SBill Paul 				t++;
137196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
137296f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
137396f2e892SBill Paul 				t++;
137496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
137579d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
137679d11e09SBill Paul 				t++;
137779d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
137896f2e892SBill Paul 			    rev >= DC_REVISION_98725)
137996f2e892SBill Paul 				t++;
138096f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
138196f2e892SBill Paul 			    rev >= DC_REVISION_88141)
138296f2e892SBill Paul 				t++;
138396f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
138496f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
138596f2e892SBill Paul 				t++;
138688d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
138788d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
138888d739dcSBill Paul 				t++;
138996f2e892SBill Paul 			return(t);
139096f2e892SBill Paul 		}
139196f2e892SBill Paul 		t++;
139296f2e892SBill Paul 	}
139396f2e892SBill Paul 
139496f2e892SBill Paul 	return(NULL);
139596f2e892SBill Paul }
139696f2e892SBill Paul 
139796f2e892SBill Paul /*
139896f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
139996f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
140096f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
140196f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
140296f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
140396f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
140496f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
140596f2e892SBill Paul  */
140696f2e892SBill Paul static int dc_probe(dev)
140796f2e892SBill Paul 	device_t		dev;
140896f2e892SBill Paul {
140996f2e892SBill Paul 	struct dc_type		*t;
141096f2e892SBill Paul 
141196f2e892SBill Paul 	t = dc_devtype(dev);
141296f2e892SBill Paul 
141396f2e892SBill Paul 	if (t != NULL) {
141496f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
141596f2e892SBill Paul 		return(0);
141696f2e892SBill Paul 	}
141796f2e892SBill Paul 
141896f2e892SBill Paul 	return(ENXIO);
141996f2e892SBill Paul }
142096f2e892SBill Paul 
142196f2e892SBill Paul static void dc_acpi(dev)
142296f2e892SBill Paul 	device_t		dev;
142396f2e892SBill Paul {
142496f2e892SBill Paul 	u_int32_t		r, cptr;
142596f2e892SBill Paul 	int			unit;
142696f2e892SBill Paul 
142796f2e892SBill Paul 	unit = device_get_unit(dev);
142896f2e892SBill Paul 
142996f2e892SBill Paul 	/* Find the location of the capabilities block */
143096f2e892SBill Paul 	cptr = pci_read_config(dev, DC_PCI_CCAP, 4) & 0xFF;
143196f2e892SBill Paul 
143296f2e892SBill Paul 	r = pci_read_config(dev, cptr, 4) & 0xFF;
143396f2e892SBill Paul 	if (r == 0x01) {
143496f2e892SBill Paul 
143596f2e892SBill Paul 		r = pci_read_config(dev, cptr + 4, 4);
143696f2e892SBill Paul 		if (r & DC_PSTATE_D3) {
143796f2e892SBill Paul 			u_int32_t		iobase, membase, irq;
143896f2e892SBill Paul 
143996f2e892SBill Paul 			/* Save important PCI config data. */
144096f2e892SBill Paul 			iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
144196f2e892SBill Paul 			membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
144296f2e892SBill Paul 			irq = pci_read_config(dev, DC_PCI_CFIT, 4);
144396f2e892SBill Paul 
144496f2e892SBill Paul 			/* Reset the power state. */
144596f2e892SBill Paul 			printf("dc%d: chip is in D%d power mode "
144696f2e892SBill Paul 			    "-- setting to D0\n", unit, r & DC_PSTATE_D3);
144796f2e892SBill Paul 			r &= 0xFFFFFFFC;
144896f2e892SBill Paul 			pci_write_config(dev, cptr + 4, r, 4);
144996f2e892SBill Paul 
145096f2e892SBill Paul 			/* Restore PCI config data. */
145196f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
145296f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
145396f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFIT, irq, 4);
145496f2e892SBill Paul 		}
145596f2e892SBill Paul 	}
145696f2e892SBill Paul 	return;
145796f2e892SBill Paul }
145896f2e892SBill Paul 
145996f2e892SBill Paul /*
146096f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
146196f2e892SBill Paul  * setup and ethernet/BPF attach.
146296f2e892SBill Paul  */
146396f2e892SBill Paul static int dc_attach(dev)
146496f2e892SBill Paul 	device_t		dev;
146596f2e892SBill Paul {
146696f2e892SBill Paul 	int			s;
146796f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
146896f2e892SBill Paul 	u_int32_t		command;
146996f2e892SBill Paul 	struct dc_softc		*sc;
147096f2e892SBill Paul 	struct ifnet		*ifp;
147196f2e892SBill Paul 	u_int32_t		revision;
147296f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
147396f2e892SBill Paul 
147496f2e892SBill Paul 	s = splimp();
147596f2e892SBill Paul 
147696f2e892SBill Paul 	sc = device_get_softc(dev);
147796f2e892SBill Paul 	unit = device_get_unit(dev);
147896f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
147996f2e892SBill Paul 
148096f2e892SBill Paul 	/*
148196f2e892SBill Paul 	 * Handle power management nonsense.
148296f2e892SBill Paul 	 */
148396f2e892SBill Paul 	dc_acpi(dev);
148496f2e892SBill Paul 
148596f2e892SBill Paul 	/*
148696f2e892SBill Paul 	 * Map control/status registers.
148796f2e892SBill Paul 	 */
1488c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
148996f2e892SBill Paul 	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
1490c48cc9ceSPeter Wemm 	pci_write_config(dev, PCIR_COMMAND, command, 4);
1491c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
149296f2e892SBill Paul 
149396f2e892SBill Paul #ifdef DC_USEIOSPACE
149496f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
149596f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
149696f2e892SBill Paul 		error = ENXIO;
149796f2e892SBill Paul 		goto fail;
149896f2e892SBill Paul 	}
149996f2e892SBill Paul #else
150096f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
150196f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
150296f2e892SBill Paul 		error = ENXIO;
150396f2e892SBill Paul 		goto fail;
150496f2e892SBill Paul 	}
150596f2e892SBill Paul #endif
150696f2e892SBill Paul 
150796f2e892SBill Paul 	rid = DC_RID;
150896f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
150996f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
151096f2e892SBill Paul 
151196f2e892SBill Paul 	if (sc->dc_res == NULL) {
151296f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
151396f2e892SBill Paul 		error = ENXIO;
151496f2e892SBill Paul 		goto fail;
151596f2e892SBill Paul 	}
151696f2e892SBill Paul 
151796f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
151896f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
151996f2e892SBill Paul 
152096f2e892SBill Paul 	/* Allocate interrupt */
152196f2e892SBill Paul 	rid = 0;
152296f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
152396f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
152496f2e892SBill Paul 
152596f2e892SBill Paul 	if (sc->dc_irq == NULL) {
152696f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
152796f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
152896f2e892SBill Paul 		error = ENXIO;
152996f2e892SBill Paul 		goto fail;
153096f2e892SBill Paul 	}
153196f2e892SBill Paul 
153296f2e892SBill Paul 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET,
153396f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
153496f2e892SBill Paul 
153596f2e892SBill Paul 	if (error) {
153696f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
153796f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
153896f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
153996f2e892SBill Paul 		goto fail;
154096f2e892SBill Paul 	}
154196f2e892SBill Paul 
154296f2e892SBill Paul 	/* Need this info to decide on a chip type. */
154396f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
154496f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
154596f2e892SBill Paul 
154696f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
154796f2e892SBill Paul 	case DC_DEVICEID_21143:
154896f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
154996f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1550042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
155196f2e892SBill Paul 		break;
155296f2e892SBill Paul 	case DC_DEVICEID_DM9100:
155396f2e892SBill Paul 	case DC_DEVICEID_DM9102:
155496f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1555fda39fd0SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_USE_TX_INTR;
155696f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
155796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
155896f2e892SBill Paul 		break;
155996f2e892SBill Paul 	case DC_DEVICEID_AL981:
156096f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
156196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
156296f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
156396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
156496f2e892SBill Paul 		break;
156596f2e892SBill Paul 	case DC_DEVICEID_AN985:
156696f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
156796f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
156896f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
156996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
157096f2e892SBill Paul 		break;
157196f2e892SBill Paul 	case DC_DEVICEID_98713:
157296f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
157396f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
157496f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
157596f2e892SBill Paul 		}
1576318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
157796f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1578318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1579318b02fdSBill Paul 		}
1580318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
158196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
158296f2e892SBill Paul 		break;
158396f2e892SBill Paul 	case DC_DEVICEID_987x5:
15849ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
158579d11e09SBill Paul 		/*
158679d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
158779d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
158879d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
158979d11e09SBill Paul 		 * get the right number of bits out of the
159079d11e09SBill Paul 		 * CRC routine.
159179d11e09SBill Paul 		 */
159279d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
159379d11e09SBill Paul 		    revision < DC_REVISION_98725)
159479d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
159596f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
159696f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1597318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
159896f2e892SBill Paul 		break;
1599ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1600ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1601ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1602ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
1603ead7cde9SBill Paul 		break;
160496f2e892SBill Paul 	case DC_DEVICEID_82C115:
160596f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
160679d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR|DC_128BIT_HASH;
1607318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
160896f2e892SBill Paul 		break;
160996f2e892SBill Paul 	case DC_DEVICEID_82C168:
161096f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
161191cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
161296f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
161396f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
161496f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
161596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
161696f2e892SBill Paul 		break;
161796f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
161896f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
161996f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
162096f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
162196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
162296f2e892SBill Paul 		break;
162396f2e892SBill Paul 	default:
162496f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
162596f2e892SBill Paul 		    sc->dc_info->dc_did);
162696f2e892SBill Paul 		break;
162796f2e892SBill Paul 	}
162896f2e892SBill Paul 
162996f2e892SBill Paul 	/* Save the cache line size. */
163088d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
163188d739dcSBill Paul 		sc->dc_cachesize = 0;
163288d739dcSBill Paul 	else
163388d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
163488d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
163596f2e892SBill Paul 
163696f2e892SBill Paul 	/* Reset the adapter. */
163796f2e892SBill Paul 	dc_reset(sc);
163896f2e892SBill Paul 
163996f2e892SBill Paul 	/* Take 21143 out of snooze mode */
164096f2e892SBill Paul 	if (DC_IS_INTEL(sc)) {
164196f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
164296f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
164396f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
164496f2e892SBill Paul 	}
164596f2e892SBill Paul 
164696f2e892SBill Paul 	/*
164796f2e892SBill Paul 	 * Try to learn something about the supported media.
164896f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
164996f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
165096f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
165196f2e892SBill Paul 	 * Intel 21143.
165296f2e892SBill Paul 	 */
165396f2e892SBill Paul 	if (DC_IS_INTEL(sc)) {
165496f2e892SBill Paul 		u_int32_t		media, cwuc;
165596f2e892SBill Paul 		cwuc = pci_read_config(dev, DC_PCI_CWUC, 4);
165696f2e892SBill Paul 		cwuc |= DC_CWUC_FORCE_WUL;
165796f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CWUC, cwuc, 4);
165896f2e892SBill Paul 		DELAY(10000);
165996f2e892SBill Paul 		media = pci_read_config(dev, DC_PCI_CWUC, 4);
166096f2e892SBill Paul 		cwuc &= ~DC_CWUC_FORCE_WUL;
166196f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CWUC, cwuc, 4);
166296f2e892SBill Paul 		DELAY(10000);
166396f2e892SBill Paul 		if (media & DC_CWUC_MII_ABILITY)
166496f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
1665042c8f6eSBill Paul 		if (media & DC_CWUC_SYM_ABILITY) {
166696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
1667042c8f6eSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1668042c8f6eSBill Paul 		}
166996f2e892SBill Paul 		/*
167096f2e892SBill Paul 		 * If none of the bits are set, then this NIC
167196f2e892SBill Paul 		 * isn't meant to support 'wake up LAN' mode.
167296f2e892SBill Paul 		 * This is usually only the case on multiport
167396f2e892SBill Paul 		 * cards, and these cards almost always have
167496f2e892SBill Paul 		 * MII transceivers.
167596f2e892SBill Paul 		 */
167696f2e892SBill Paul 		if (media == 0)
167796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
167896f2e892SBill Paul 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
167996f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
168096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
168196f2e892SBill Paul 		else
168296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
168396f2e892SBill Paul 	} else if (!sc->dc_pmode)
168496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
168596f2e892SBill Paul 
168696f2e892SBill Paul 	/*
168796f2e892SBill Paul 	 * Get station address from the EEPROM.
168896f2e892SBill Paul 	 */
168996f2e892SBill Paul 	switch(sc->dc_type) {
169096f2e892SBill Paul 	case DC_TYPE_98713:
169196f2e892SBill Paul 	case DC_TYPE_98713A:
169296f2e892SBill Paul 	case DC_TYPE_987x5:
169396f2e892SBill Paul 	case DC_TYPE_PNICII:
169496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
169596f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
169696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
169796f2e892SBill Paul 		break;
169896f2e892SBill Paul 	case DC_TYPE_PNIC:
169996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
170096f2e892SBill Paul 		break;
170196f2e892SBill Paul 	case DC_TYPE_DM9102:
170296f2e892SBill Paul 	case DC_TYPE_21143:
170396f2e892SBill Paul 	case DC_TYPE_ASIX:
170496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
170596f2e892SBill Paul 		break;
170696f2e892SBill Paul 	case DC_TYPE_AL981:
170796f2e892SBill Paul 	case DC_TYPE_AN985:
170896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
170996f2e892SBill Paul 		break;
171096f2e892SBill Paul 	default:
171196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
171296f2e892SBill Paul 		break;
171396f2e892SBill Paul 	}
171496f2e892SBill Paul 
171596f2e892SBill Paul 	/*
171696f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
171796f2e892SBill Paul 	 */
171896f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
171996f2e892SBill Paul 
172096f2e892SBill Paul 	sc->dc_unit = unit;
172196f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
172296f2e892SBill Paul 
172396f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
172496f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
172596f2e892SBill Paul 
172696f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
172796f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
172896f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
172996f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
173096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
173196f2e892SBill Paul 		error = ENXIO;
173296f2e892SBill Paul 		goto fail;
173396f2e892SBill Paul 	}
173496f2e892SBill Paul 
173596f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
173696f2e892SBill Paul 
173796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
173896f2e892SBill Paul 	ifp->if_softc = sc;
173996f2e892SBill Paul 	ifp->if_unit = unit;
174096f2e892SBill Paul 	ifp->if_name = "dc";
174196f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
174296f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
174396f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
174496f2e892SBill Paul 	ifp->if_output = ether_output;
174596f2e892SBill Paul 	ifp->if_start = dc_start;
174696f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
174796f2e892SBill Paul 	ifp->if_init = dc_init;
174896f2e892SBill Paul 	ifp->if_baudrate = 10000000;
174996f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
175096f2e892SBill Paul 
175196f2e892SBill Paul 	/*
175296f2e892SBill Paul 	 * Do MII setup.
175396f2e892SBill Paul 	 */
175496f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
175596f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
175696f2e892SBill Paul 
175796f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
175896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_SYM;
1759042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
176096f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
176196f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
176296f2e892SBill Paul 		error = 0;
176396f2e892SBill Paul 	}
176496f2e892SBill Paul 
176596f2e892SBill Paul 	if (error) {
176696f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
176796f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
176896f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
176996f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
177096f2e892SBill Paul 		error = ENXIO;
177196f2e892SBill Paul 		goto fail;
177296f2e892SBill Paul 	}
177396f2e892SBill Paul 
177496f2e892SBill Paul 	/*
177521b8ebd9SArchie Cobbs 	 * Call MI attach routine.
177696f2e892SBill Paul 	 */
177721b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
177896f2e892SBill Paul 	callout_handle_init(&sc->dc_stat_ch);
177996f2e892SBill Paul 
1780510a809eSMike Smith #ifdef __alpha__
1781510a809eSMike Smith         sc->dc_srm_media = 0;
1782510a809eSMike Smith 
1783510a809eSMike Smith 	/* Remember the SRM console media setting */
1784510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
1785510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
1786510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
1787510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
1788510a809eSMike Smith 		case 3:
1789510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
1790510a809eSMike Smith 			break;
1791510a809eSMike Smith 		case 4:
1792510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
1793510a809eSMike Smith 			break;
1794510a809eSMike Smith 		case 5:
1795510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
1796510a809eSMike Smith 			break;
1797510a809eSMike Smith 		case 6:
1798510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
1799510a809eSMike Smith 			break;
1800510a809eSMike Smith 		}
1801510a809eSMike Smith 		if (sc->dc_srm_media)
1802510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
1803510a809eSMike Smith 	}
1804510a809eSMike Smith #endif
1805510a809eSMike Smith 
1806510a809eSMike Smith 
180796f2e892SBill Paul fail:
180896f2e892SBill Paul 	splx(s);
180996f2e892SBill Paul 
181096f2e892SBill Paul 	return(error);
181196f2e892SBill Paul }
181296f2e892SBill Paul 
181396f2e892SBill Paul static int dc_detach(dev)
181496f2e892SBill Paul 	device_t		dev;
181596f2e892SBill Paul {
181696f2e892SBill Paul 	struct dc_softc		*sc;
181796f2e892SBill Paul 	struct ifnet		*ifp;
181896f2e892SBill Paul 	int			s;
181996f2e892SBill Paul 
182096f2e892SBill Paul 	s = splimp();
182196f2e892SBill Paul 
182296f2e892SBill Paul 	sc = device_get_softc(dev);
182396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
182496f2e892SBill Paul 
182596f2e892SBill Paul 	dc_stop(sc);
182621b8ebd9SArchie Cobbs 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
182796f2e892SBill Paul 
182896f2e892SBill Paul 	bus_generic_detach(dev);
182996f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
183096f2e892SBill Paul 
183196f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
183296f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
183396f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
183496f2e892SBill Paul 
183596f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
183696f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
183796f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
183896f2e892SBill Paul 
183996f2e892SBill Paul 	splx(s);
184096f2e892SBill Paul 
184196f2e892SBill Paul 	return(0);
184296f2e892SBill Paul }
184396f2e892SBill Paul 
184496f2e892SBill Paul /*
184596f2e892SBill Paul  * Initialize the transmit descriptors.
184696f2e892SBill Paul  */
184796f2e892SBill Paul static int dc_list_tx_init(sc)
184896f2e892SBill Paul 	struct dc_softc		*sc;
184996f2e892SBill Paul {
185096f2e892SBill Paul 	struct dc_chain_data	*cd;
185196f2e892SBill Paul 	struct dc_list_data	*ld;
185296f2e892SBill Paul 	int			i;
185396f2e892SBill Paul 
185496f2e892SBill Paul 	cd = &sc->dc_cdata;
185596f2e892SBill Paul 	ld = sc->dc_ldata;
185696f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
185796f2e892SBill Paul 		if (i == (DC_TX_LIST_CNT - 1)) {
185896f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
185996f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[0]);
186096f2e892SBill Paul 		} else {
186196f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
186296f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[i + 1]);
186396f2e892SBill Paul 		}
186496f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
186596f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
186696f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
186796f2e892SBill Paul 	}
186896f2e892SBill Paul 
186996f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
187096f2e892SBill Paul 
187196f2e892SBill Paul 	return(0);
187296f2e892SBill Paul }
187396f2e892SBill Paul 
187496f2e892SBill Paul 
187596f2e892SBill Paul /*
187696f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
187796f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
187896f2e892SBill Paul  * points back to the first.
187996f2e892SBill Paul  */
188096f2e892SBill Paul static int dc_list_rx_init(sc)
188196f2e892SBill Paul 	struct dc_softc		*sc;
188296f2e892SBill Paul {
188396f2e892SBill Paul 	struct dc_chain_data	*cd;
188496f2e892SBill Paul 	struct dc_list_data	*ld;
188596f2e892SBill Paul 	int			i;
188696f2e892SBill Paul 
188796f2e892SBill Paul 	cd = &sc->dc_cdata;
188896f2e892SBill Paul 	ld = sc->dc_ldata;
188996f2e892SBill Paul 
189096f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
189196f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
189296f2e892SBill Paul 			return(ENOBUFS);
189396f2e892SBill Paul 		if (i == (DC_RX_LIST_CNT - 1)) {
189496f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
189596f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[0]);
189696f2e892SBill Paul 		} else {
189796f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
189896f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[i + 1]);
189996f2e892SBill Paul 		}
190096f2e892SBill Paul 	}
190196f2e892SBill Paul 
190296f2e892SBill Paul 	cd->dc_rx_prod = 0;
190396f2e892SBill Paul 
190496f2e892SBill Paul 	return(0);
190596f2e892SBill Paul }
190696f2e892SBill Paul 
190796f2e892SBill Paul /*
190896f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
190996f2e892SBill Paul  */
191096f2e892SBill Paul static int dc_newbuf(sc, i, m)
191196f2e892SBill Paul 	struct dc_softc		*sc;
191296f2e892SBill Paul 	int			i;
191396f2e892SBill Paul 	struct mbuf		*m;
191496f2e892SBill Paul {
191596f2e892SBill Paul 	struct mbuf		*m_new = NULL;
191696f2e892SBill Paul 	struct dc_desc		*c;
191796f2e892SBill Paul 
191896f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
191996f2e892SBill Paul 
192096f2e892SBill Paul 	if (m == NULL) {
192196f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
192296f2e892SBill Paul 		if (m_new == NULL) {
192396f2e892SBill Paul 			printf("dc%d: no memory for rx list "
192496f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
192596f2e892SBill Paul 			return(ENOBUFS);
192696f2e892SBill Paul 		}
192796f2e892SBill Paul 
192896f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
192996f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
193096f2e892SBill Paul 			printf("dc%d: no memory for rx list "
193196f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
193296f2e892SBill Paul 			m_freem(m_new);
193396f2e892SBill Paul 			return(ENOBUFS);
193496f2e892SBill Paul 		}
193596f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
193696f2e892SBill Paul 	} else {
193796f2e892SBill Paul 		m_new = m;
193896f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
193996f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
194096f2e892SBill Paul 	}
194196f2e892SBill Paul 
194296f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
194396f2e892SBill Paul 
194496f2e892SBill Paul 	/*
194596f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
194696f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
194796f2e892SBill Paul 	 * 82c169 chips.
194896f2e892SBill Paul 	 */
194996f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
195096f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
195196f2e892SBill Paul 
195296f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
195396f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
195496f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
195596f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
195696f2e892SBill Paul 
195796f2e892SBill Paul 	return(0);
195896f2e892SBill Paul }
195996f2e892SBill Paul 
196096f2e892SBill Paul /*
196196f2e892SBill Paul  * Grrrrr.
196296f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
196396f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
196496f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
196596f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
196696f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
196796f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
196896f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
196996f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
197096f2e892SBill Paul  *
197196f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
197296f2e892SBill Paul  * Here's what we know:
197396f2e892SBill Paul  *
197496f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
197596f2e892SBill Paul  *   descriptors uploaded.
197696f2e892SBill Paul  *
197796f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
197896f2e892SBill Paul  *   total data upload.
197996f2e892SBill Paul  *
198096f2e892SBill Paul  * - We know the size of the desired received frame because it will be
198196f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
198296f2e892SBill Paul  *
198396f2e892SBill Paul  * Here's what we do:
198496f2e892SBill Paul  *
198596f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
198696f2e892SBill Paul  *   This means that we know that the buffer contents should be all
198796f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
198896f2e892SBill Paul  *
198996f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
199096f2e892SBill Paul  *   ethernet CRC at the end.
199196f2e892SBill Paul  *
199296f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
199396f2e892SBill Paul  *
199496f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
199596f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
199696f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
199796f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
199896f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
199996f2e892SBill Paul  *   we won't be fooled.
200096f2e892SBill Paul  *
200196f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
200296f2e892SBill Paul  *   that value from the current pointer location. This brings us
200396f2e892SBill Paul  *   to the start of the actual received packet.
200496f2e892SBill Paul  *
200596f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
200696f2e892SBill Paul  *   frame length.
200796f2e892SBill Paul  *
200896f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
200996f2e892SBill Paul  * the time.
201096f2e892SBill Paul  */
201196f2e892SBill Paul 
201296f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
201396f2e892SBill Paul static void dc_pnic_rx_bug_war(sc, idx)
201496f2e892SBill Paul 	struct dc_softc		*sc;
201596f2e892SBill Paul 	int			idx;
201696f2e892SBill Paul {
201796f2e892SBill Paul 	struct dc_desc		*cur_rx;
201896f2e892SBill Paul 	struct dc_desc		*c = NULL;
201996f2e892SBill Paul 	struct mbuf		*m = NULL;
202096f2e892SBill Paul 	unsigned char		*ptr;
202196f2e892SBill Paul 	int			i, total_len;
202296f2e892SBill Paul 	u_int32_t		rxstat = 0;
202396f2e892SBill Paul 
202496f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
202596f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
202696f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
202796f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
202896f2e892SBill Paul 
202996f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
203096f2e892SBill Paul 	while (1) {
203196f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
203296f2e892SBill Paul 		rxstat = c->dc_status;
203396f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
203496f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
203596f2e892SBill Paul 		ptr += DC_RXLEN;
203696f2e892SBill Paul 		/* If this is the last buffer, break out. */
203796f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
203896f2e892SBill Paul 			break;
203996f2e892SBill Paul 		dc_newbuf(sc, i, m);
204096f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
204196f2e892SBill Paul 	}
204296f2e892SBill Paul 
204396f2e892SBill Paul 	/* Find the length of the actual receive frame. */
204496f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
204596f2e892SBill Paul 
204696f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
204796f2e892SBill Paul 	while(*ptr == 0x00)
204896f2e892SBill Paul 		ptr--;
204996f2e892SBill Paul 
205096f2e892SBill Paul 	/* Round off. */
205196f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
205296f2e892SBill Paul 		ptr -= 1;
205396f2e892SBill Paul 
205496f2e892SBill Paul 	/* Now find the start of the frame. */
205596f2e892SBill Paul 	ptr -= total_len;
205696f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
205796f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
205896f2e892SBill Paul 
205996f2e892SBill Paul 	/*
206096f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
206196f2e892SBill Paul 	 * the status word to make it look like a successful
206296f2e892SBill Paul  	 * frame reception.
206396f2e892SBill Paul 	 */
206496f2e892SBill Paul 	dc_newbuf(sc, i, m);
206596f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
206696f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
206796f2e892SBill Paul 
206896f2e892SBill Paul 	return;
206996f2e892SBill Paul }
207096f2e892SBill Paul 
207196f2e892SBill Paul /*
207273bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
207373bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
207473bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
207573bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
207673bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
207773bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
207873bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
207973bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
208073bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
208173bf949cSBill Paul  */
208273bf949cSBill Paul static int dc_rx_resync(sc)
208373bf949cSBill Paul 	struct dc_softc		*sc;
208473bf949cSBill Paul {
208573bf949cSBill Paul 	int			i, pos;
208673bf949cSBill Paul 	struct dc_desc		*cur_rx;
208773bf949cSBill Paul 
208873bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
208973bf949cSBill Paul 
209073bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
209173bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
209273bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
209373bf949cSBill Paul 			break;
209473bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
209573bf949cSBill Paul 	}
209673bf949cSBill Paul 
209773bf949cSBill Paul 	/* If the ring really is empty, then just return. */
209873bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
209973bf949cSBill Paul 		return(0);
210073bf949cSBill Paul 
210173bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
210273bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
210373bf949cSBill Paul 
210473bf949cSBill Paul 	return(EAGAIN);
210573bf949cSBill Paul }
210673bf949cSBill Paul 
210773bf949cSBill Paul /*
210896f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
210996f2e892SBill Paul  * the higher level protocols.
211096f2e892SBill Paul  */
211196f2e892SBill Paul static void dc_rxeof(sc)
211296f2e892SBill Paul 	struct dc_softc		*sc;
211396f2e892SBill Paul {
211496f2e892SBill Paul         struct ether_header	*eh;
211596f2e892SBill Paul         struct mbuf		*m;
211696f2e892SBill Paul         struct ifnet		*ifp;
211796f2e892SBill Paul 	struct dc_desc		*cur_rx;
211896f2e892SBill Paul 	int			i, total_len = 0;
211996f2e892SBill Paul 	u_int32_t		rxstat;
212096f2e892SBill Paul 
212196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
212296f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
212396f2e892SBill Paul 
212496f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
212596f2e892SBill Paul 		struct mbuf		*m0 = NULL;
212696f2e892SBill Paul 
212796f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
212896f2e892SBill Paul 		rxstat = cur_rx->dc_status;
212996f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
213096f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
213196f2e892SBill Paul 
213296f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
213396f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
213496f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
213596f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
213696f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
213796f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
213896f2e892SBill Paul 					continue;
213996f2e892SBill Paul 				}
214096f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
214196f2e892SBill Paul 				rxstat = cur_rx->dc_status;
214296f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
214396f2e892SBill Paul 			}
214496f2e892SBill Paul 		}
214596f2e892SBill Paul 
214696f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
214796f2e892SBill Paul 
214896f2e892SBill Paul 		/*
214996f2e892SBill Paul 		 * If an error occurs, update stats, clear the
215096f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
215196f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
215296f2e892SBill Paul 	 	 * comes up in the ring.
215396f2e892SBill Paul 		 */
215496f2e892SBill Paul 		if (rxstat & DC_RXSTAT_RXERR) {
215596f2e892SBill Paul 			ifp->if_ierrors++;
215696f2e892SBill Paul 			if (rxstat & DC_RXSTAT_COLLSEEN)
215796f2e892SBill Paul 				ifp->if_collisions++;
215896f2e892SBill Paul 			dc_newbuf(sc, i, m);
215996f2e892SBill Paul 			if (rxstat & DC_RXSTAT_CRCERR) {
216096f2e892SBill Paul 				DC_INC(i, DC_RX_LIST_CNT);
216196f2e892SBill Paul 				continue;
216296f2e892SBill Paul 			} else {
216396f2e892SBill Paul 				dc_init(sc);
216496f2e892SBill Paul 				return;
216596f2e892SBill Paul 			}
216696f2e892SBill Paul 		}
216796f2e892SBill Paul 
216896f2e892SBill Paul 		/* No errors; receive the packet. */
216996f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
217096f2e892SBill Paul 
217196f2e892SBill Paul 		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
217296f2e892SBill Paul 		    total_len + ETHER_ALIGN, 0, ifp, NULL);
217396f2e892SBill Paul 		dc_newbuf(sc, i, m);
217496f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
217596f2e892SBill Paul 		if (m0 == NULL) {
217696f2e892SBill Paul 			ifp->if_ierrors++;
217796f2e892SBill Paul 			continue;
217896f2e892SBill Paul 		}
217996f2e892SBill Paul 		m_adj(m0, ETHER_ALIGN);
218096f2e892SBill Paul 		m = m0;
218196f2e892SBill Paul 
218296f2e892SBill Paul 		ifp->if_ipackets++;
218396f2e892SBill Paul 		eh = mtod(m, struct ether_header *);
218496f2e892SBill Paul 
218596f2e892SBill Paul 		/* Remove header from mbuf and pass it on. */
218696f2e892SBill Paul 		m_adj(m, sizeof(struct ether_header));
218796f2e892SBill Paul 		ether_input(ifp, eh, m);
218896f2e892SBill Paul 	}
218996f2e892SBill Paul 
219096f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
219196f2e892SBill Paul }
219296f2e892SBill Paul 
219396f2e892SBill Paul /*
219496f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
219596f2e892SBill Paul  * the list buffers.
219696f2e892SBill Paul  */
219796f2e892SBill Paul 
219896f2e892SBill Paul static void dc_txeof(sc)
219996f2e892SBill Paul 	struct dc_softc		*sc;
220096f2e892SBill Paul {
220196f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
220296f2e892SBill Paul 	struct ifnet		*ifp;
220396f2e892SBill Paul 	int			idx;
220496f2e892SBill Paul 
220596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
220696f2e892SBill Paul 
220796f2e892SBill Paul 	/* Clear the timeout timer. */
220896f2e892SBill Paul 	ifp->if_timer = 0;
220996f2e892SBill Paul 
221096f2e892SBill Paul 	/*
221196f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
221296f2e892SBill Paul 	 * frames that have been transmitted.
221396f2e892SBill Paul 	 */
221496f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
221596f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
221696f2e892SBill Paul 		u_int32_t		txstat;
221796f2e892SBill Paul 
221896f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
221996f2e892SBill Paul 		txstat = cur_tx->dc_status;
222096f2e892SBill Paul 
222196f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
222296f2e892SBill Paul 			break;
222396f2e892SBill Paul 
222496f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
222596f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
222696f2e892SBill Paul 			sc->dc_cdata.dc_tx_cnt--;
222796f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
222896f2e892SBill Paul 				/*
222996f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
223096f2e892SBill Paul 				 * that it will sometimes generate a TX
223196f2e892SBill Paul 				 * underrun error while DMAing the RX
223296f2e892SBill Paul 				 * filter setup frame. If we detect this,
223396f2e892SBill Paul 				 * we have to send the setup frame again,
223496f2e892SBill Paul 				 * or else the filter won't be programmed
223596f2e892SBill Paul 				 * correctly.
223696f2e892SBill Paul 				 */
223796f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
223896f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
223996f2e892SBill Paul 						dc_setfilt(sc);
224096f2e892SBill Paul 				}
224196f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
224296f2e892SBill Paul 			}
224396f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
224496f2e892SBill Paul 			continue;
224596f2e892SBill Paul 		}
224696f2e892SBill Paul 
224796f2e892SBill Paul 		if (/*sc->dc_type == DC_TYPE_21143 &&*/
224896f2e892SBill Paul 		    sc->dc_pmode == DC_PMODE_MII &&
224996f2e892SBill Paul 		    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
225096f2e892SBill Paul 		    DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
225196f2e892SBill Paul 			txstat &= ~DC_TXSTAT_ERRSUM;
225296f2e892SBill Paul 
225396f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
225496f2e892SBill Paul 			ifp->if_oerrors++;
225596f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
225696f2e892SBill Paul 				ifp->if_collisions++;
225796f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
225896f2e892SBill Paul 				ifp->if_collisions++;
225996f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
226096f2e892SBill Paul 				dc_init(sc);
226196f2e892SBill Paul 				return;
226296f2e892SBill Paul 			}
226396f2e892SBill Paul 		}
226496f2e892SBill Paul 
226596f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
226696f2e892SBill Paul 
226796f2e892SBill Paul 		ifp->if_opackets++;
226896f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
226996f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
227096f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
227196f2e892SBill Paul 		}
227296f2e892SBill Paul 
227396f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
227496f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
227596f2e892SBill Paul 	}
227696f2e892SBill Paul 
227796f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
227896f2e892SBill Paul 	if (cur_tx != NULL)
227996f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
228096f2e892SBill Paul 
228196f2e892SBill Paul 	return;
228296f2e892SBill Paul }
228396f2e892SBill Paul 
228496f2e892SBill Paul static void dc_tick(xsc)
228596f2e892SBill Paul 	void			*xsc;
228696f2e892SBill Paul {
228796f2e892SBill Paul 	struct dc_softc		*sc;
228896f2e892SBill Paul 	struct mii_data		*mii;
228996f2e892SBill Paul 	struct ifnet		*ifp;
229096f2e892SBill Paul 	int			s;
229196f2e892SBill Paul 	u_int32_t		r;
229296f2e892SBill Paul 
229396f2e892SBill Paul 	s = splimp();
229496f2e892SBill Paul 
229596f2e892SBill Paul 	sc = xsc;
229696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
229796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
229896f2e892SBill Paul 
229996f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2300318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2301318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2302318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2303318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
230496f2e892SBill Paul 				sc->dc_link = 0;
2305318b02fdSBill Paul 				mii_mediachg(mii);
2306318b02fdSBill Paul 			}
2307318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2308318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2309318b02fdSBill Paul 				sc->dc_link = 0;
2310318b02fdSBill Paul 				mii_mediachg(mii);
2311318b02fdSBill Paul 			}
2312d675147eSBill Paul 			if (sc->dc_link == 0)
231396f2e892SBill Paul 				mii_tick(mii);
231496f2e892SBill Paul 		} else {
2315318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
231696f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2317042c8f6eSBill Paul 			    sc->dc_cdata.dc_tx_cnt == 0)
231896f2e892SBill Paul 				mii_tick(mii);
2319042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2320042c8f6eSBill Paul 					sc->dc_link = 0;
232196f2e892SBill Paul 		}
232296f2e892SBill Paul 	} else
232396f2e892SBill Paul 		mii_tick(mii);
232496f2e892SBill Paul 
232596f2e892SBill Paul 	/*
232696f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
232796f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
232896f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
232996f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
233096f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
233196f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
233296f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
233396f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
233496f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
233596f2e892SBill Paul 	 * a screeching halt for several seconds.
233696f2e892SBill Paul 	 *
233796f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
233896f2e892SBill Paul 	 * any packets until a link has been established. After the
233996f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
234096f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
234196f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
234296f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
234396f2e892SBill Paul 	 */
234496f2e892SBill Paul 	if (!sc->dc_link) {
234596f2e892SBill Paul 		mii_pollstat(mii);
234696f2e892SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
234796f2e892SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
234896f2e892SBill Paul 			sc->dc_link++;
234996f2e892SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
235096f2e892SBill Paul 				dc_start(ifp);
235196f2e892SBill Paul 		}
235296f2e892SBill Paul 	}
235396f2e892SBill Paul 
2354318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2355318b02fdSBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz/10);
2356318b02fdSBill Paul 	else
235796f2e892SBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz);
235896f2e892SBill Paul 
235996f2e892SBill Paul 	splx(s);
236096f2e892SBill Paul 
236196f2e892SBill Paul 	return;
236296f2e892SBill Paul }
236396f2e892SBill Paul 
236496f2e892SBill Paul static void dc_intr(arg)
236596f2e892SBill Paul 	void			*arg;
236696f2e892SBill Paul {
236796f2e892SBill Paul 	struct dc_softc		*sc;
236896f2e892SBill Paul 	struct ifnet		*ifp;
236996f2e892SBill Paul 	u_int32_t		status;
237096f2e892SBill Paul 
237196f2e892SBill Paul 	sc = arg;
237296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
237396f2e892SBill Paul 
237496f2e892SBill Paul 	/* Supress unwanted interrupts */
237596f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
237696f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
237796f2e892SBill Paul 			dc_stop(sc);
237896f2e892SBill Paul 		return;
237996f2e892SBill Paul 	}
238096f2e892SBill Paul 
238196f2e892SBill Paul 	/* Disable interrupts. */
238296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
238396f2e892SBill Paul 
238496f2e892SBill Paul 	while((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) {
238596f2e892SBill Paul 
238696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
238796f2e892SBill Paul 
238873bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
238973bf949cSBill Paul 			int		curpkts;
239073bf949cSBill Paul 			curpkts = ifp->if_ipackets;
239196f2e892SBill Paul 			dc_rxeof(sc);
239273bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
239373bf949cSBill Paul 				while(dc_rx_resync(sc))
239473bf949cSBill Paul 					dc_rxeof(sc);
239573bf949cSBill Paul 			}
239673bf949cSBill Paul 		}
239796f2e892SBill Paul 
239896f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
239996f2e892SBill Paul 			dc_txeof(sc);
240096f2e892SBill Paul 
240196f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
240296f2e892SBill Paul 			dc_txeof(sc);
240396f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
240496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
240596f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
240696f2e892SBill Paul 			}
240796f2e892SBill Paul 		}
240896f2e892SBill Paul 
240996f2e892SBill Paul 		if (status & DC_ISR_TX_UNDERRUN) {
241096f2e892SBill Paul 			u_int32_t		cfg;
241196f2e892SBill Paul 
241296f2e892SBill Paul 			printf("dc%d: TX underrun -- ", sc->dc_unit);
241396f2e892SBill Paul 			if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc))
241496f2e892SBill Paul 				dc_init(sc);
241596f2e892SBill Paul 			cfg = CSR_READ_4(sc, DC_NETCFG);
241696f2e892SBill Paul 			cfg &= ~DC_NETCFG_TX_THRESH;
241796f2e892SBill Paul 			if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
241896f2e892SBill Paul 				printf("using store and forward mode\n");
241996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
242091cc2adbSBill Paul 			} else if (sc->dc_flags & DC_TX_STORENFWD) {
242191cc2adbSBill Paul 				printf("resetting\n");
242296f2e892SBill Paul 			} else {
242396f2e892SBill Paul 				sc->dc_txthresh += 0x4000;
242496f2e892SBill Paul 				printf("increasing TX threshold\n");
242596f2e892SBill Paul 				CSR_WRITE_4(sc, DC_NETCFG, cfg);
242696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
242796f2e892SBill Paul 				DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
242896f2e892SBill Paul 			}
242996f2e892SBill Paul 		}
243096f2e892SBill Paul 
243196f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
243273bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
243373bf949cSBill Paul 			int		curpkts;
243473bf949cSBill Paul 			curpkts = ifp->if_ipackets;
243596f2e892SBill Paul 			dc_rxeof(sc);
243673bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
243773bf949cSBill Paul 				while(dc_rx_resync(sc))
243873bf949cSBill Paul 					dc_rxeof(sc);
243973bf949cSBill Paul 			}
244073bf949cSBill Paul 		}
244196f2e892SBill Paul 
244296f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
244396f2e892SBill Paul 			dc_reset(sc);
244496f2e892SBill Paul 			dc_init(sc);
244596f2e892SBill Paul 		}
244696f2e892SBill Paul 	}
244796f2e892SBill Paul 
244896f2e892SBill Paul 	/* Re-enable interrupts. */
244996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
245096f2e892SBill Paul 
245196f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
245296f2e892SBill Paul 		dc_start(ifp);
245396f2e892SBill Paul 
245496f2e892SBill Paul 	return;
245596f2e892SBill Paul }
245696f2e892SBill Paul 
245796f2e892SBill Paul /*
245896f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
245996f2e892SBill Paul  * pointers to the fragment pointers.
246096f2e892SBill Paul  */
246196f2e892SBill Paul static int dc_encap(sc, m_head, txidx)
246296f2e892SBill Paul 	struct dc_softc		*sc;
246396f2e892SBill Paul 	struct mbuf		*m_head;
246496f2e892SBill Paul 	u_int32_t		*txidx;
246596f2e892SBill Paul {
246696f2e892SBill Paul 	struct dc_desc		*f = NULL;
246796f2e892SBill Paul 	struct mbuf		*m;
246896f2e892SBill Paul 	int			frag, cur, cnt = 0;
246996f2e892SBill Paul 
247096f2e892SBill Paul 	/*
247196f2e892SBill Paul  	 * Start packing the mbufs in this chain into
247296f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
247396f2e892SBill Paul  	 * of fragments or hit the end of the mbuf chain.
247496f2e892SBill Paul 	 */
247596f2e892SBill Paul 	m = m_head;
247696f2e892SBill Paul 	cur = frag = *txidx;
247796f2e892SBill Paul 
247896f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
247996f2e892SBill Paul 		if (m->m_len != 0) {
248096f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
248196f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
248296f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
248396f2e892SBill Paul 					return(ENOBUFS);
248496f2e892SBill Paul 			}
248596f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
248696f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
248796f2e892SBill Paul 				return(ENOBUFS);
248896f2e892SBill Paul 
248996f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
249096f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
249196f2e892SBill Paul 			if (cnt == 0) {
249296f2e892SBill Paul 				f->dc_status = 0;
249396f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
249496f2e892SBill Paul 			} else
249596f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
249696f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
249796f2e892SBill Paul 			cur = frag;
249896f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
249996f2e892SBill Paul 			cnt++;
250096f2e892SBill Paul 		}
250196f2e892SBill Paul 	}
250296f2e892SBill Paul 
250396f2e892SBill Paul 	if (m != NULL)
250496f2e892SBill Paul 		return(ENOBUFS);
250596f2e892SBill Paul 
250696f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
250796f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
250896f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
250996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
251096f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
251191cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
251291cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
251396f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
251496f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
251596f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
251696f2e892SBill Paul 	*txidx = frag;
251796f2e892SBill Paul 
251896f2e892SBill Paul 	return(0);
251996f2e892SBill Paul }
252096f2e892SBill Paul 
252196f2e892SBill Paul /*
2522fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
2523fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
2524fda39fd0SBill Paul  * do scatter/gather correctly.
2525fda39fd0SBill Paul  */
2526fda39fd0SBill Paul static int dc_coal(sc, m_head)
2527fda39fd0SBill Paul 	struct dc_softc		*sc;
2528fda39fd0SBill Paul 	struct mbuf		**m_head;
2529fda39fd0SBill Paul {
2530fda39fd0SBill Paul         struct mbuf		*m_new, *m;
2531fda39fd0SBill Paul 
2532fda39fd0SBill Paul 	m = *m_head;
2533fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
2534fda39fd0SBill Paul 	if (m_new == NULL) {
2535fda39fd0SBill Paul 		printf("dc%d: no memory for tx list", sc->dc_unit);
2536fda39fd0SBill Paul 		return(ENOBUFS);
2537fda39fd0SBill Paul 	}
2538fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
2539fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
2540fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
2541fda39fd0SBill Paul 			m_freem(m_new);
2542fda39fd0SBill Paul 			printf("dc%d: no memory for tx list", sc->dc_unit);
2543fda39fd0SBill Paul 			return(ENOBUFS);
2544fda39fd0SBill Paul 		}
2545fda39fd0SBill Paul 	}
2546fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
2547fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
2548fda39fd0SBill Paul 	m_freem(m);
2549fda39fd0SBill Paul 	*m_head = m_new;
2550fda39fd0SBill Paul 
2551fda39fd0SBill Paul 	return(0);
2552fda39fd0SBill Paul }
2553fda39fd0SBill Paul 
2554fda39fd0SBill Paul /*
255596f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
255696f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
255796f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
255896f2e892SBill Paul  * physical addresses.
255996f2e892SBill Paul  */
256096f2e892SBill Paul 
256196f2e892SBill Paul static void dc_start(ifp)
256296f2e892SBill Paul 	struct ifnet		*ifp;
256396f2e892SBill Paul {
256496f2e892SBill Paul 	struct dc_softc		*sc;
256596f2e892SBill Paul 	struct mbuf		*m_head = NULL;
256696f2e892SBill Paul 	int			idx;
256796f2e892SBill Paul 
256896f2e892SBill Paul 	sc = ifp->if_softc;
256996f2e892SBill Paul 
257096f2e892SBill Paul 	if (!sc->dc_link)
257196f2e892SBill Paul 		return;
257296f2e892SBill Paul 
257396f2e892SBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
257496f2e892SBill Paul 		return;
257596f2e892SBill Paul 
257696f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
257796f2e892SBill Paul 
257896f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
257996f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
258096f2e892SBill Paul 		if (m_head == NULL)
258196f2e892SBill Paul 			break;
258296f2e892SBill Paul 
2583fda39fd0SBill Paul 		if (sc->dc_flags & DC_TX_COALESCE) {
2584fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
2585fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
2586fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
2587fda39fd0SBill Paul 				break;
2588fda39fd0SBill Paul 			}
2589fda39fd0SBill Paul 		}
2590fda39fd0SBill Paul 
259196f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
259296f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
259396f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
259496f2e892SBill Paul 			break;
259596f2e892SBill Paul 		}
259696f2e892SBill Paul 
259796f2e892SBill Paul 		/*
259896f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
259996f2e892SBill Paul 		 * to him.
260096f2e892SBill Paul 		 */
260196f2e892SBill Paul 		if (ifp->if_bpf)
260296f2e892SBill Paul 			bpf_mtap(ifp, m_head);
260396f2e892SBill Paul 	}
260496f2e892SBill Paul 
260596f2e892SBill Paul 	/* Transmit */
260696f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
260796f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
260896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
260996f2e892SBill Paul 
261096f2e892SBill Paul 	/*
261196f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
261296f2e892SBill Paul 	 */
261396f2e892SBill Paul 	ifp->if_timer = 5;
261496f2e892SBill Paul 
261596f2e892SBill Paul 	return;
261696f2e892SBill Paul }
261796f2e892SBill Paul 
261896f2e892SBill Paul static void dc_init(xsc)
261996f2e892SBill Paul 	void			*xsc;
262096f2e892SBill Paul {
262196f2e892SBill Paul 	struct dc_softc		*sc = xsc;
262296f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
262396f2e892SBill Paul 	struct mii_data		*mii;
262496f2e892SBill Paul 	int			s;
262596f2e892SBill Paul 
262696f2e892SBill Paul 	s = splimp();
262796f2e892SBill Paul 
262896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
262996f2e892SBill Paul 
263096f2e892SBill Paul 	/*
263196f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
263296f2e892SBill Paul 	 */
263396f2e892SBill Paul 	dc_stop(sc);
263496f2e892SBill Paul 	dc_reset(sc);
263596f2e892SBill Paul 
263696f2e892SBill Paul 	/*
263796f2e892SBill Paul 	 * Set cache alignment and burst length.
263896f2e892SBill Paul 	 */
263988d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
264096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
264196f2e892SBill Paul 	else
264296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
264396f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
264496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
264596f2e892SBill Paul 	} else {
264696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
264796f2e892SBill Paul 	}
264896f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
264996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
265096f2e892SBill Paul 	switch(sc->dc_cachesize) {
265196f2e892SBill Paul 	case 32:
265296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
265396f2e892SBill Paul 		break;
265496f2e892SBill Paul 	case 16:
265596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
265696f2e892SBill Paul 		break;
265796f2e892SBill Paul 	case 8:
265896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
265996f2e892SBill Paul 		break;
266096f2e892SBill Paul 	case 0:
266196f2e892SBill Paul 	default:
266296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
266396f2e892SBill Paul 		break;
266496f2e892SBill Paul 	}
266596f2e892SBill Paul 
266696f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
266796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
266896f2e892SBill Paul 	else {
266996f2e892SBill Paul 		if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
267096f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
267196f2e892SBill Paul 		} else {
267296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
267396f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
267496f2e892SBill Paul 		}
267596f2e892SBill Paul 	}
267696f2e892SBill Paul 
267796f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
267896f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
267996f2e892SBill Paul 
268096f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
268196f2e892SBill Paul 		/*
268296f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
268396f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
268496f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
268596f2e892SBill Paul 		 * document the meaning of these bits so there's no way
268696f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
268796f2e892SBill Paul 		 * number all its own; the rest all use a different one.
268896f2e892SBill Paul 		 */
268996f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
269096f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
269196f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
269296f2e892SBill Paul 		else
269396f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
269496f2e892SBill Paul 	}
269596f2e892SBill Paul 
269696f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
269796f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_72BYTES);
269896f2e892SBill Paul 
269996f2e892SBill Paul 	/* Init circular RX list. */
270096f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
270196f2e892SBill Paul 		printf("dc%d: initialization failed: no "
270296f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
270396f2e892SBill Paul 		dc_stop(sc);
270496f2e892SBill Paul 		(void)splx(s);
270596f2e892SBill Paul 		return;
270696f2e892SBill Paul 	}
270796f2e892SBill Paul 
270896f2e892SBill Paul 	/*
270996f2e892SBill Paul 	 * Init tx descriptors.
271096f2e892SBill Paul 	 */
271196f2e892SBill Paul 	dc_list_tx_init(sc);
271296f2e892SBill Paul 
271396f2e892SBill Paul 	/*
271496f2e892SBill Paul 	 * Load the address of the RX list.
271596f2e892SBill Paul 	 */
271696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
271796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
271896f2e892SBill Paul 
271996f2e892SBill Paul 	/*
272096f2e892SBill Paul 	 * Enable interrupts.
272196f2e892SBill Paul 	 */
272296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
272396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
272496f2e892SBill Paul 
272596f2e892SBill Paul 	/* Enable transmitter. */
272696f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
272796f2e892SBill Paul 
272896f2e892SBill Paul 	/*
272996f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
273096f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
273196f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
273296f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
273396f2e892SBill Paul 	 */
273496f2e892SBill Paul 	dc_setfilt(sc);
273596f2e892SBill Paul 
273696f2e892SBill Paul 	/* Enable receiver. */
273796f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
273896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
273996f2e892SBill Paul 
274096f2e892SBill Paul 	mii_mediachg(mii);
274196f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
274296f2e892SBill Paul 
274396f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
274496f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
274596f2e892SBill Paul 
274696f2e892SBill Paul 	(void)splx(s);
274796f2e892SBill Paul 
2748318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY)
2749318b02fdSBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz/10);
2750318b02fdSBill Paul 	else
275196f2e892SBill Paul 		sc->dc_stat_ch = timeout(dc_tick, sc, hz);
275296f2e892SBill Paul 
2753510a809eSMike Smith #ifdef __alpha__
2754510a809eSMike Smith         if(sc->dc_srm_media) {
2755510a809eSMike Smith 		struct ifreq ifr;
2756510a809eSMike Smith 
2757510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
2758510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
2759510a809eSMike Smith 		sc->dc_srm_media = 0;
2760510a809eSMike Smith 	}
2761510a809eSMike Smith #endif
276296f2e892SBill Paul 	return;
276396f2e892SBill Paul }
276496f2e892SBill Paul 
276596f2e892SBill Paul /*
276696f2e892SBill Paul  * Set media options.
276796f2e892SBill Paul  */
276896f2e892SBill Paul static int dc_ifmedia_upd(ifp)
276996f2e892SBill Paul 	struct ifnet		*ifp;
277096f2e892SBill Paul {
277196f2e892SBill Paul 	struct dc_softc		*sc;
277296f2e892SBill Paul 	struct mii_data		*mii;
2773f43d9309SBill Paul 	struct ifmedia		*ifm;
277496f2e892SBill Paul 
277596f2e892SBill Paul 	sc = ifp->if_softc;
277696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
277796f2e892SBill Paul 	mii_mediachg(mii);
2778f43d9309SBill Paul 	ifm = &mii->mii_media;
2779f43d9309SBill Paul 
2780f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
2781f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA)
2782f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
2783f43d9309SBill Paul 	else
278496f2e892SBill Paul 		sc->dc_link = 0;
278596f2e892SBill Paul 
278696f2e892SBill Paul 	return(0);
278796f2e892SBill Paul }
278896f2e892SBill Paul 
278996f2e892SBill Paul /*
279096f2e892SBill Paul  * Report current media status.
279196f2e892SBill Paul  */
279296f2e892SBill Paul static void dc_ifmedia_sts(ifp, ifmr)
279396f2e892SBill Paul 	struct ifnet		*ifp;
279496f2e892SBill Paul 	struct ifmediareq	*ifmr;
279596f2e892SBill Paul {
279696f2e892SBill Paul 	struct dc_softc		*sc;
279796f2e892SBill Paul 	struct mii_data		*mii;
2798f43d9309SBill Paul 	struct ifmedia		*ifm;
279996f2e892SBill Paul 
280096f2e892SBill Paul 	sc = ifp->if_softc;
280196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
280296f2e892SBill Paul 	mii_pollstat(mii);
2803f43d9309SBill Paul 	ifm = &mii->mii_media;
2804f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
2805f43d9309SBill Paul 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
2806f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
2807f43d9309SBill Paul 			ifmr->ifm_status = 0;
2808f43d9309SBill Paul 			return;
2809f43d9309SBill Paul 		}
2810f43d9309SBill Paul 	}
281196f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
281296f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
281396f2e892SBill Paul 
281496f2e892SBill Paul 	return;
281596f2e892SBill Paul }
281696f2e892SBill Paul 
281796f2e892SBill Paul static int dc_ioctl(ifp, command, data)
281896f2e892SBill Paul 	struct ifnet		*ifp;
281996f2e892SBill Paul 	u_long			command;
282096f2e892SBill Paul 	caddr_t			data;
282196f2e892SBill Paul {
282296f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
282396f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
282496f2e892SBill Paul 	struct mii_data		*mii;
282596f2e892SBill Paul 	int			s, error = 0;
282696f2e892SBill Paul 
282796f2e892SBill Paul 	s = splimp();
282896f2e892SBill Paul 
282996f2e892SBill Paul 	switch(command) {
283096f2e892SBill Paul 	case SIOCSIFADDR:
283196f2e892SBill Paul 	case SIOCGIFADDR:
283296f2e892SBill Paul 	case SIOCSIFMTU:
283396f2e892SBill Paul 		error = ether_ioctl(ifp, command, data);
283496f2e892SBill Paul 		break;
283596f2e892SBill Paul 	case SIOCSIFFLAGS:
283696f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
283796f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
283896f2e892SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
283996f2e892SBill Paul 			    !(sc->dc_if_flags & IFF_PROMISC)) {
284096f2e892SBill Paul 				dc_setfilt(sc);
284196f2e892SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
284296f2e892SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
284396f2e892SBill Paul 			    sc->dc_if_flags & IFF_PROMISC) {
284496f2e892SBill Paul 				dc_setfilt(sc);
284596f2e892SBill Paul 			} else if (!(ifp->if_flags & IFF_RUNNING)) {
284696f2e892SBill Paul 				sc->dc_txthresh = 0;
284796f2e892SBill Paul 				dc_init(sc);
284896f2e892SBill Paul 			}
284996f2e892SBill Paul 		} else {
285096f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
285196f2e892SBill Paul 				dc_stop(sc);
285296f2e892SBill Paul 		}
285396f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
285496f2e892SBill Paul 		error = 0;
285596f2e892SBill Paul 		break;
285696f2e892SBill Paul 	case SIOCADDMULTI:
285796f2e892SBill Paul 	case SIOCDELMULTI:
285896f2e892SBill Paul 		dc_setfilt(sc);
285996f2e892SBill Paul 		error = 0;
286096f2e892SBill Paul 		break;
286196f2e892SBill Paul 	case SIOCGIFMEDIA:
286296f2e892SBill Paul 	case SIOCSIFMEDIA:
286396f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
286496f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2865510a809eSMike Smith #ifdef __alpha__
2866510a809eSMike Smith 		if (sc->dc_srm_media)
2867510a809eSMike Smith 			sc->dc_srm_media = 0;
2868510a809eSMike Smith #endif
286996f2e892SBill Paul 		break;
287096f2e892SBill Paul 	default:
287196f2e892SBill Paul 		error = EINVAL;
287296f2e892SBill Paul 		break;
287396f2e892SBill Paul 	}
287496f2e892SBill Paul 
287596f2e892SBill Paul 	(void)splx(s);
287696f2e892SBill Paul 
287796f2e892SBill Paul 	return(error);
287896f2e892SBill Paul }
287996f2e892SBill Paul 
288096f2e892SBill Paul static void dc_watchdog(ifp)
288196f2e892SBill Paul 	struct ifnet		*ifp;
288296f2e892SBill Paul {
288396f2e892SBill Paul 	struct dc_softc		*sc;
288496f2e892SBill Paul 
288596f2e892SBill Paul 	sc = ifp->if_softc;
288696f2e892SBill Paul 
288796f2e892SBill Paul 	ifp->if_oerrors++;
288896f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
288996f2e892SBill Paul 
289096f2e892SBill Paul 	dc_stop(sc);
289196f2e892SBill Paul 	dc_reset(sc);
289296f2e892SBill Paul 	dc_init(sc);
289396f2e892SBill Paul 
289496f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
289596f2e892SBill Paul 		dc_start(ifp);
289696f2e892SBill Paul 
289796f2e892SBill Paul 	return;
289896f2e892SBill Paul }
289996f2e892SBill Paul 
290096f2e892SBill Paul /*
290196f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
290296f2e892SBill Paul  * RX and TX lists.
290396f2e892SBill Paul  */
290496f2e892SBill Paul static void dc_stop(sc)
290596f2e892SBill Paul 	struct dc_softc		*sc;
290696f2e892SBill Paul {
290796f2e892SBill Paul 	register int		i;
290896f2e892SBill Paul 	struct ifnet		*ifp;
290996f2e892SBill Paul 
291096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
291196f2e892SBill Paul 	ifp->if_timer = 0;
291296f2e892SBill Paul 
291396f2e892SBill Paul 	untimeout(dc_tick, sc, sc->dc_stat_ch);
291496f2e892SBill Paul 
291596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
291696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
291796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
291896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
291996f2e892SBill Paul 	sc->dc_link = 0;
292096f2e892SBill Paul 
292196f2e892SBill Paul 	/*
292296f2e892SBill Paul 	 * Free data in the RX lists.
292396f2e892SBill Paul 	 */
292496f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
292596f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
292696f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
292796f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
292896f2e892SBill Paul 		}
292996f2e892SBill Paul 	}
293096f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
293196f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
293296f2e892SBill Paul 
293396f2e892SBill Paul 	/*
293496f2e892SBill Paul 	 * Free the TX list buffers.
293596f2e892SBill Paul 	 */
293696f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
293796f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
293896f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
293996f2e892SBill Paul 			    DC_TXCTL_SETUP) {
294096f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
294196f2e892SBill Paul 				continue;
294296f2e892SBill Paul 			}
294396f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
294496f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
294596f2e892SBill Paul 		}
294696f2e892SBill Paul 	}
294796f2e892SBill Paul 
294896f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
294996f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
295096f2e892SBill Paul 
295196f2e892SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
295296f2e892SBill Paul 
295396f2e892SBill Paul 	return;
295496f2e892SBill Paul }
295596f2e892SBill Paul 
295696f2e892SBill Paul /*
295796f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
295896f2e892SBill Paul  * get confused by errant DMAs when rebooting.
295996f2e892SBill Paul  */
296096f2e892SBill Paul static void dc_shutdown(dev)
296196f2e892SBill Paul 	device_t		dev;
296296f2e892SBill Paul {
296396f2e892SBill Paul 	struct dc_softc		*sc;
296496f2e892SBill Paul 
296596f2e892SBill Paul 	sc = device_get_softc(dev);
296696f2e892SBill Paul 
296796f2e892SBill Paul 	dc_stop(sc);
296896f2e892SBill Paul 
296996f2e892SBill Paul 	return;
297096f2e892SBill Paul }
2971