xref: /freebsd/sys/dev/dc/if_dc.c (revision 95a1645553285f84b844f5a1e32e6edf70c58f0d)
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  *
3996f2e892SBill Paul  * Macronix 98713/98715/98725 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)
4796f2e892SBill Paul  *
4896f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
4996f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5096f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5196f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5296f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
5396f2e892SBill Paul  * instead of 512.
5496f2e892SBill Paul  *
5596f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
5696f2e892SBill Paul  * Electrical Engineering Department
5796f2e892SBill Paul  * Columbia University, New York City
5896f2e892SBill Paul  */
5996f2e892SBill Paul 
6096f2e892SBill Paul /*
6196f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6296f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6396f2e892SBill Paul  * three kinds of media attachments:
6496f2e892SBill Paul  *
6596f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
6696f2e892SBill Paul  *   autonegotiation provided by an external PHY.
6796f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
6896f2e892SBill Paul  * o 10baseT port.
6996f2e892SBill Paul  * o AUI/BNC port.
7096f2e892SBill Paul  *
7196f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7296f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7396f2e892SBill Paul  * autosensing configuration.
7496f2e892SBill Paul  *
7596f2e892SBill Paul  * Knowing which media is available on a given card is tough: you're
7696f2e892SBill Paul  * supposed to go slogging through the EEPROM looking for media
7796f2e892SBill Paul  * description structures. Unfortunately, some card vendors that use
7896f2e892SBill Paul  * the 21143 don't obey the DEC SROM spec correctly, which means that
7996f2e892SBill Paul  * what you find in the EEPROM may not agree with reality. Fortunately,
8096f2e892SBill Paul  * the 21143 provides us a way to get around this issue: lurking in
8196f2e892SBill Paul  * PCI configuration space is the Configuration Wake-Up Command Register.
8296f2e892SBill Paul  * This register is loaded with a value from the EEPROM when wake on LAN
8396f2e892SBill Paul  * mode is enabled; this value tells us quite clearly what kind of media
8496f2e892SBill Paul  * is attached to the NIC. The main purpose of this register is to tell
8596f2e892SBill Paul  * the NIC what media to scan when in wake on LAN mode, however by
8696f2e892SBill Paul  * forcibly enabling wake on LAN mode, we can use to learn what kind of
8796f2e892SBill Paul  * media a given NIC has available and adapt ourselves accordingly.
8896f2e892SBill Paul  *
8996f2e892SBill Paul  * Of course, if the media description blocks in the EEPROM are bogus.
9096f2e892SBill Paul  * what are the odds that the CWUC aren't bogus as well, right? Well,
9196f2e892SBill Paul  * the CWUC value is more likely to be correct since wake on LAN mode
9296f2e892SBill Paul  * won't work correctly without it, and wake on LAN is a big selling
9396f2e892SBill Paul  * point these days. It's also harder to screw up a single byte than
9496f2e892SBill Paul  * a whole media descriptor block.
9596f2e892SBill Paul  *
9696f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
9796f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
9896f2e892SBill Paul  * handled separately due to its different register offsets and the
9996f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
10096f2e892SBill Paul  * here, but I'm not thrilled about it.
10196f2e892SBill Paul  *
10296f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
10396f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
10496f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
10596f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
10696f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
10796f2e892SBill Paul  */
10896f2e892SBill Paul 
10996f2e892SBill Paul #include <sys/param.h>
11096f2e892SBill Paul #include <sys/systm.h>
11196f2e892SBill Paul #include <sys/sockio.h>
11296f2e892SBill Paul #include <sys/mbuf.h>
11396f2e892SBill Paul #include <sys/malloc.h>
11496f2e892SBill Paul #include <sys/kernel.h>
11596f2e892SBill Paul #include <sys/socket.h>
11696f2e892SBill Paul 
11796f2e892SBill Paul #include <net/if.h>
11896f2e892SBill Paul #include <net/if_arp.h>
11996f2e892SBill Paul #include <net/ethernet.h>
12096f2e892SBill Paul #include <net/if_dl.h>
12196f2e892SBill Paul #include <net/if_media.h>
12296f2e892SBill Paul 
12396f2e892SBill Paul #include <net/bpf.h>
12496f2e892SBill Paul 
125c8cf61e1SRobert Watson #include "opt_bdg.h"
126c8cf61e1SRobert Watson #ifdef BRIDGE
127c8cf61e1SRobert Watson #include <net/bridge.h>
128c8cf61e1SRobert Watson #endif
129c8cf61e1SRobert Watson 
13096f2e892SBill Paul #include <vm/vm.h>              /* for vtophys */
13196f2e892SBill Paul #include <vm/pmap.h>            /* for vtophys */
13296f2e892SBill Paul #include <machine/clock.h>      /* for DELAY */
13396f2e892SBill Paul #include <machine/bus_pio.h>
13496f2e892SBill Paul #include <machine/bus_memio.h>
13596f2e892SBill Paul #include <machine/bus.h>
13696f2e892SBill Paul #include <machine/resource.h>
13796f2e892SBill Paul #include <sys/bus.h>
13896f2e892SBill Paul #include <sys/rman.h>
13996f2e892SBill Paul 
14096f2e892SBill Paul #include <dev/mii/mii.h>
14196f2e892SBill Paul #include <dev/mii/miivar.h>
14296f2e892SBill Paul 
14396f2e892SBill Paul #include <pci/pcireg.h>
14496f2e892SBill Paul #include <pci/pcivar.h>
14596f2e892SBill Paul 
14696f2e892SBill Paul #define DC_USEIOSPACE
14796f2e892SBill Paul 
14896f2e892SBill Paul #include <pci/if_dcreg.h>
14996f2e892SBill Paul 
15095a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
15195a16455SPeter Wemm 
15296f2e892SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
15396f2e892SBill Paul #include "miibus_if.h"
15496f2e892SBill Paul 
15596f2e892SBill Paul #ifndef lint
15696f2e892SBill Paul static const char rcsid[] =
15796f2e892SBill Paul   "$FreeBSD$";
15896f2e892SBill Paul #endif
15996f2e892SBill Paul 
16096f2e892SBill Paul /*
16196f2e892SBill Paul  * Various supported device vendors/types and their names.
16296f2e892SBill Paul  */
16396f2e892SBill Paul static struct dc_type dc_devs[] = {
16496f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
16596f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
16696f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
16796f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
16896f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
16996f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
17088d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
17188d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
17296f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
17396f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
17496f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
17596f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
17696f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
17796f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
17896f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
17996f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
18096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
18196f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
18296f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
18396f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
18496f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
18596f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
18696f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
18796f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
18896f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18996f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
19096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
19196f2e892SBill Paul 		"Macronix 98725 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" },
19896f2e892SBill Paul 	{ 0, 0, NULL }
19996f2e892SBill Paul };
20096f2e892SBill Paul 
20196f2e892SBill Paul static int dc_probe		__P((device_t));
20296f2e892SBill Paul static int dc_attach		__P((device_t));
20396f2e892SBill Paul static int dc_detach		__P((device_t));
20496f2e892SBill Paul static void dc_acpi		__P((device_t));
20596f2e892SBill Paul static struct dc_type *dc_devtype	__P((device_t));
20696f2e892SBill Paul static int dc_newbuf		__P((struct dc_softc *, int, struct mbuf *));
20796f2e892SBill Paul static int dc_encap		__P((struct dc_softc *, struct mbuf *,
20896f2e892SBill Paul 					u_int32_t *));
209fda39fd0SBill Paul static int dc_coal		__P((struct dc_softc *, struct mbuf **));
21096f2e892SBill Paul static void dc_pnic_rx_bug_war	__P((struct dc_softc *, int));
21173bf949cSBill Paul static int dc_rx_resync		__P((struct dc_softc *));
21296f2e892SBill Paul static void dc_rxeof		__P((struct dc_softc *));
21396f2e892SBill Paul static void dc_txeof		__P((struct dc_softc *));
21496f2e892SBill Paul static void dc_tick		__P((void *));
21596f2e892SBill Paul static void dc_intr		__P((void *));
21696f2e892SBill Paul static void dc_start		__P((struct ifnet *));
21796f2e892SBill Paul static int dc_ioctl		__P((struct ifnet *, u_long, caddr_t));
21896f2e892SBill Paul static void dc_init		__P((void *));
21996f2e892SBill Paul static void dc_stop		__P((struct dc_softc *));
22096f2e892SBill Paul static void dc_watchdog		__P((struct ifnet *));
22196f2e892SBill Paul static void dc_shutdown		__P((device_t));
22296f2e892SBill Paul static int dc_ifmedia_upd	__P((struct ifnet *));
22396f2e892SBill Paul static void dc_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
22496f2e892SBill Paul 
22596f2e892SBill Paul static void dc_delay		__P((struct dc_softc *));
22696f2e892SBill Paul static void dc_eeprom_idle	__P((struct dc_softc *));
22796f2e892SBill Paul static void dc_eeprom_putbyte	__P((struct dc_softc *, int));
22896f2e892SBill Paul static void dc_eeprom_getword	__P((struct dc_softc *, int, u_int16_t *));
22996f2e892SBill Paul static void dc_eeprom_getword_pnic
23096f2e892SBill Paul 				__P((struct dc_softc *, int, u_int16_t *));
23196f2e892SBill Paul static void dc_read_eeprom	__P((struct dc_softc *, caddr_t, int,
23296f2e892SBill Paul 							int, int));
23396f2e892SBill Paul 
23496f2e892SBill Paul static void dc_mii_writebit	__P((struct dc_softc *, int));
23596f2e892SBill Paul static int dc_mii_readbit	__P((struct dc_softc *));
23696f2e892SBill Paul static void dc_mii_sync		__P((struct dc_softc *));
23796f2e892SBill Paul static void dc_mii_send		__P((struct dc_softc *, u_int32_t, int));
23896f2e892SBill Paul static int dc_mii_readreg	__P((struct dc_softc *, struct dc_mii_frame *));
23996f2e892SBill Paul static int dc_mii_writereg	__P((struct dc_softc *, struct dc_mii_frame *));
24096f2e892SBill Paul static int dc_miibus_readreg	__P((device_t, int, int));
24196f2e892SBill Paul static int dc_miibus_writereg	__P((device_t, int, int, int));
24296f2e892SBill Paul static void dc_miibus_statchg	__P((device_t));
243f43d9309SBill Paul static void dc_miibus_mediainit	__P((device_t));
24496f2e892SBill Paul 
24596f2e892SBill Paul static void dc_setcfg		__P((struct dc_softc *, int));
24696f2e892SBill Paul static u_int32_t dc_crc_le	__P((struct dc_softc *, caddr_t));
24796f2e892SBill Paul static u_int32_t dc_crc_be	__P((caddr_t));
24896f2e892SBill Paul static void dc_setfilt_21143	__P((struct dc_softc *));
24996f2e892SBill Paul static void dc_setfilt_asix	__P((struct dc_softc *));
25096f2e892SBill Paul static void dc_setfilt_admtek	__P((struct dc_softc *));
25196f2e892SBill Paul 
25296f2e892SBill Paul static void dc_setfilt		__P((struct dc_softc *));
25396f2e892SBill Paul 
25496f2e892SBill Paul static void dc_reset		__P((struct dc_softc *));
25596f2e892SBill Paul static int dc_list_rx_init	__P((struct dc_softc *));
25696f2e892SBill Paul static int dc_list_tx_init	__P((struct dc_softc *));
25796f2e892SBill Paul 
25896f2e892SBill Paul #ifdef DC_USEIOSPACE
25996f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
26096f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
26196f2e892SBill Paul #else
26296f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
26396f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
26496f2e892SBill Paul #endif
26596f2e892SBill Paul 
26696f2e892SBill Paul static device_method_t dc_methods[] = {
26796f2e892SBill Paul 	/* Device interface */
26896f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
26996f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
27096f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
27196f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
27296f2e892SBill Paul 
27396f2e892SBill Paul 	/* bus interface */
27496f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
27596f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
27696f2e892SBill Paul 
27796f2e892SBill Paul 	/* MII interface */
27896f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
27996f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
28096f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
281f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
28296f2e892SBill Paul 
28396f2e892SBill Paul 	{ 0, 0 }
28496f2e892SBill Paul };
28596f2e892SBill Paul 
28696f2e892SBill Paul static driver_t dc_driver = {
28796f2e892SBill Paul 	"dc",
28896f2e892SBill Paul 	dc_methods,
28996f2e892SBill Paul 	sizeof(struct dc_softc)
29096f2e892SBill Paul };
29196f2e892SBill Paul 
29296f2e892SBill Paul static devclass_t dc_devclass;
29396f2e892SBill Paul 
29496f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
29596f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
29696f2e892SBill Paul 
29796f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
29896f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
29996f2e892SBill Paul 
30096f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
30196f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
30296f2e892SBill Paul 
30396f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
30496f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
30596f2e892SBill Paul 
30696f2e892SBill Paul static void dc_delay(sc)
30796f2e892SBill Paul 	struct dc_softc		*sc;
30896f2e892SBill Paul {
30996f2e892SBill Paul 	int			idx;
31096f2e892SBill Paul 
31196f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
31296f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
31396f2e892SBill Paul }
31496f2e892SBill Paul 
31596f2e892SBill Paul static void dc_eeprom_idle(sc)
31696f2e892SBill Paul 	struct dc_softc		*sc;
31796f2e892SBill Paul {
31896f2e892SBill Paul 	register int		i;
31996f2e892SBill Paul 
32096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
32196f2e892SBill Paul 	dc_delay(sc);
32296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
32396f2e892SBill Paul 	dc_delay(sc);
32496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
32596f2e892SBill Paul 	dc_delay(sc);
32696f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
32796f2e892SBill Paul 	dc_delay(sc);
32896f2e892SBill Paul 
32996f2e892SBill Paul 	for (i = 0; i < 25; i++) {
33096f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33196f2e892SBill Paul 		dc_delay(sc);
33296f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33396f2e892SBill Paul 		dc_delay(sc);
33496f2e892SBill Paul 	}
33596f2e892SBill Paul 
33696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
33796f2e892SBill Paul 	dc_delay(sc);
33896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
33996f2e892SBill Paul 	dc_delay(sc);
34096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
34196f2e892SBill Paul 
34296f2e892SBill Paul 	return;
34396f2e892SBill Paul }
34496f2e892SBill Paul 
34596f2e892SBill Paul /*
34696f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
34796f2e892SBill Paul  */
34896f2e892SBill Paul static void dc_eeprom_putbyte(sc, addr)
34996f2e892SBill Paul 	struct dc_softc		*sc;
35096f2e892SBill Paul 	int			addr;
35196f2e892SBill Paul {
35296f2e892SBill Paul 	register int		d, i;
35396f2e892SBill Paul 
35496f2e892SBill Paul 	/*
35596f2e892SBill Paul 	 * The AN985 has a 93C66 EEPROM on it instead of
35696f2e892SBill Paul 	 * a 93C46. It uses a different bit sequence for
35796f2e892SBill Paul 	 * specifying the "read" opcode.
35896f2e892SBill Paul 	 */
35996f2e892SBill Paul 	if (DC_IS_CENTAUR(sc))
36096f2e892SBill Paul 		d = addr | (DC_EECMD_READ << 2);
36196f2e892SBill Paul 	else
36296f2e892SBill Paul 		d = addr | DC_EECMD_READ;
36396f2e892SBill Paul 
36496f2e892SBill Paul 	/*
36596f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
36696f2e892SBill Paul 	 */
36796f2e892SBill Paul 	for (i = 0x400; i; i >>= 1) {
36896f2e892SBill Paul 		if (d & i) {
36996f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
37096f2e892SBill Paul 		} else {
37196f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
37296f2e892SBill Paul 		}
37396f2e892SBill Paul 		dc_delay(sc);
37496f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
37596f2e892SBill Paul 		dc_delay(sc);
37696f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
37796f2e892SBill Paul 		dc_delay(sc);
37896f2e892SBill Paul 	}
37996f2e892SBill Paul 
38096f2e892SBill Paul 	return;
38196f2e892SBill Paul }
38296f2e892SBill Paul 
38396f2e892SBill Paul /*
38496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
38596f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
38696f2e892SBill Paul  * the EEPROM.
38796f2e892SBill Paul  */
38896f2e892SBill Paul static void dc_eeprom_getword_pnic(sc, addr, dest)
38996f2e892SBill Paul 	struct dc_softc		*sc;
39096f2e892SBill Paul 	int			addr;
39196f2e892SBill Paul 	u_int16_t		*dest;
39296f2e892SBill Paul {
39396f2e892SBill Paul 	register int		i;
39496f2e892SBill Paul 	u_int32_t		r;
39596f2e892SBill Paul 
39696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
39796f2e892SBill Paul 
39896f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
39996f2e892SBill Paul 		DELAY(1);
40096f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
40196f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
40296f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
40396f2e892SBill Paul 			return;
40496f2e892SBill Paul 		}
40596f2e892SBill Paul 	}
40696f2e892SBill Paul 
40796f2e892SBill Paul 	return;
40896f2e892SBill Paul }
40996f2e892SBill Paul 
41096f2e892SBill Paul /*
41196f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
41296f2e892SBill Paul  */
41396f2e892SBill Paul static void dc_eeprom_getword(sc, addr, dest)
41496f2e892SBill Paul 	struct dc_softc		*sc;
41596f2e892SBill Paul 	int			addr;
41696f2e892SBill Paul 	u_int16_t		*dest;
41796f2e892SBill Paul {
41896f2e892SBill Paul 	register int		i;
41996f2e892SBill Paul 	u_int16_t		word = 0;
42096f2e892SBill Paul 
42196f2e892SBill Paul 	/* Force EEPROM to idle state. */
42296f2e892SBill Paul 	dc_eeprom_idle(sc);
42396f2e892SBill Paul 
42496f2e892SBill Paul 	/* Enter EEPROM access mode. */
42596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
42696f2e892SBill Paul 	dc_delay(sc);
42796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
42896f2e892SBill Paul 	dc_delay(sc);
42996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43096f2e892SBill Paul 	dc_delay(sc);
43196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
43296f2e892SBill Paul 	dc_delay(sc);
43396f2e892SBill Paul 
43496f2e892SBill Paul 	/*
43596f2e892SBill Paul 	 * Send address of word we want to read.
43696f2e892SBill Paul 	 */
43796f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
43896f2e892SBill Paul 
43996f2e892SBill Paul 	/*
44096f2e892SBill Paul 	 * Start reading bits from EEPROM.
44196f2e892SBill Paul 	 */
44296f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
44396f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
44496f2e892SBill Paul 		dc_delay(sc);
44596f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
44696f2e892SBill Paul 			word |= i;
44796f2e892SBill Paul 		dc_delay(sc);
44896f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
44996f2e892SBill Paul 		dc_delay(sc);
45096f2e892SBill Paul 	}
45196f2e892SBill Paul 
45296f2e892SBill Paul 	/* Turn off EEPROM access mode. */
45396f2e892SBill Paul 	dc_eeprom_idle(sc);
45496f2e892SBill Paul 
45596f2e892SBill Paul 	*dest = word;
45696f2e892SBill Paul 
45796f2e892SBill Paul 	return;
45896f2e892SBill Paul }
45996f2e892SBill Paul 
46096f2e892SBill Paul /*
46196f2e892SBill Paul  * Read a sequence of words from the EEPROM.
46296f2e892SBill Paul  */
46396f2e892SBill Paul static void dc_read_eeprom(sc, dest, off, cnt, swap)
46496f2e892SBill Paul 	struct dc_softc		*sc;
46596f2e892SBill Paul 	caddr_t			dest;
46696f2e892SBill Paul 	int			off;
46796f2e892SBill Paul 	int			cnt;
46896f2e892SBill Paul 	int			swap;
46996f2e892SBill Paul {
47096f2e892SBill Paul 	int			i;
47196f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
47296f2e892SBill Paul 
47396f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
47496f2e892SBill Paul 		if (DC_IS_PNIC(sc))
47596f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
47696f2e892SBill Paul 		else
47796f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
47896f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
47996f2e892SBill Paul 		if (swap)
48096f2e892SBill Paul 			*ptr = ntohs(word);
48196f2e892SBill Paul 		else
48296f2e892SBill Paul 			*ptr = word;
48396f2e892SBill Paul 	}
48496f2e892SBill Paul 
48596f2e892SBill Paul 	return;
48696f2e892SBill Paul }
48796f2e892SBill Paul 
48896f2e892SBill Paul /*
48996f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
49096f2e892SBill Paul  * Application Notes pp.19-21.
49196f2e892SBill Paul  */
49296f2e892SBill Paul /*
49396f2e892SBill Paul  * Write a bit to the MII bus.
49496f2e892SBill Paul  */
49596f2e892SBill Paul static void dc_mii_writebit(sc, bit)
49696f2e892SBill Paul 	struct dc_softc		*sc;
49796f2e892SBill Paul 	int			bit;
49896f2e892SBill Paul {
49996f2e892SBill Paul 	if (bit)
50096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
50196f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
50296f2e892SBill Paul 	else
50396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
50496f2e892SBill Paul 
50596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
50696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
50796f2e892SBill Paul 
50896f2e892SBill Paul 	return;
50996f2e892SBill Paul }
51096f2e892SBill Paul 
51196f2e892SBill Paul /*
51296f2e892SBill Paul  * Read a bit from the MII bus.
51396f2e892SBill Paul  */
51496f2e892SBill Paul static int dc_mii_readbit(sc)
51596f2e892SBill Paul 	struct dc_softc		*sc;
51696f2e892SBill Paul {
51796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
51896f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
51996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
52096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
52196f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
52296f2e892SBill Paul 		return(1);
52396f2e892SBill Paul 
52496f2e892SBill Paul 	return(0);
52596f2e892SBill Paul }
52696f2e892SBill Paul 
52796f2e892SBill Paul /*
52896f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
52996f2e892SBill Paul  */
53096f2e892SBill Paul static void dc_mii_sync(sc)
53196f2e892SBill Paul 	struct dc_softc		*sc;
53296f2e892SBill Paul {
53396f2e892SBill Paul 	register int		i;
53496f2e892SBill Paul 
53596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
53696f2e892SBill Paul 
53796f2e892SBill Paul 	for (i = 0; i < 32; i++)
53896f2e892SBill Paul 		dc_mii_writebit(sc, 1);
53996f2e892SBill Paul 
54096f2e892SBill Paul 	return;
54196f2e892SBill Paul }
54296f2e892SBill Paul 
54396f2e892SBill Paul /*
54496f2e892SBill Paul  * Clock a series of bits through the MII.
54596f2e892SBill Paul  */
54696f2e892SBill Paul static void dc_mii_send(sc, bits, cnt)
54796f2e892SBill Paul 	struct dc_softc		*sc;
54896f2e892SBill Paul 	u_int32_t		bits;
54996f2e892SBill Paul 	int			cnt;
55096f2e892SBill Paul {
55196f2e892SBill Paul 	int			i;
55296f2e892SBill Paul 
55396f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
55496f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
55596f2e892SBill Paul }
55696f2e892SBill Paul 
55796f2e892SBill Paul /*
55896f2e892SBill Paul  * Read an PHY register through the MII.
55996f2e892SBill Paul  */
56096f2e892SBill Paul static int dc_mii_readreg(sc, frame)
56196f2e892SBill Paul 	struct dc_softc		*sc;
56296f2e892SBill Paul 	struct dc_mii_frame	*frame;
56396f2e892SBill Paul 
56496f2e892SBill Paul {
56596f2e892SBill Paul 	int			i, ack, s;
56696f2e892SBill Paul 
56796f2e892SBill Paul 	s = splimp();
56896f2e892SBill Paul 
56996f2e892SBill Paul 	/*
57096f2e892SBill Paul 	 * Set up frame for RX.
57196f2e892SBill Paul 	 */
57296f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
57396f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
57496f2e892SBill Paul 	frame->mii_turnaround = 0;
57596f2e892SBill Paul 	frame->mii_data = 0;
57696f2e892SBill Paul 
57796f2e892SBill Paul 	/*
57896f2e892SBill Paul 	 * Sync the PHYs.
57996f2e892SBill Paul 	 */
58096f2e892SBill Paul 	dc_mii_sync(sc);
58196f2e892SBill Paul 
58296f2e892SBill Paul 	/*
58396f2e892SBill Paul 	 * Send command/address info.
58496f2e892SBill Paul 	 */
58596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
58696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
58796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
58896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
58996f2e892SBill Paul 
59096f2e892SBill Paul #ifdef notdef
59196f2e892SBill Paul 	/* Idle bit */
59296f2e892SBill Paul 	dc_mii_writebit(sc, 1);
59396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
59496f2e892SBill Paul #endif
59596f2e892SBill Paul 
59696f2e892SBill Paul 	/* Check for ack */
59796f2e892SBill Paul 	ack = dc_mii_readbit(sc);
59896f2e892SBill Paul 
59996f2e892SBill Paul 	/*
60096f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
60196f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
60296f2e892SBill Paul 	 */
60396f2e892SBill Paul 	if (ack) {
60496f2e892SBill Paul 		for(i = 0; i < 16; i++) {
60596f2e892SBill Paul 			dc_mii_readbit(sc);
60696f2e892SBill Paul 		}
60796f2e892SBill Paul 		goto fail;
60896f2e892SBill Paul 	}
60996f2e892SBill Paul 
61096f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
61196f2e892SBill Paul 		if (!ack) {
61296f2e892SBill Paul 			if (dc_mii_readbit(sc))
61396f2e892SBill Paul 				frame->mii_data |= i;
61496f2e892SBill Paul 		}
61596f2e892SBill Paul 	}
61696f2e892SBill Paul 
61796f2e892SBill Paul fail:
61896f2e892SBill Paul 
61996f2e892SBill Paul 	dc_mii_writebit(sc, 0);
62096f2e892SBill Paul 	dc_mii_writebit(sc, 0);
62196f2e892SBill Paul 
62296f2e892SBill Paul 	splx(s);
62396f2e892SBill Paul 
62496f2e892SBill Paul 	if (ack)
62596f2e892SBill Paul 		return(1);
62696f2e892SBill Paul 	return(0);
62796f2e892SBill Paul }
62896f2e892SBill Paul 
62996f2e892SBill Paul /*
63096f2e892SBill Paul  * Write to a PHY register through the MII.
63196f2e892SBill Paul  */
63296f2e892SBill Paul static int dc_mii_writereg(sc, frame)
63396f2e892SBill Paul 	struct dc_softc		*sc;
63496f2e892SBill Paul 	struct dc_mii_frame	*frame;
63596f2e892SBill Paul 
63696f2e892SBill Paul {
63796f2e892SBill Paul 	int			s;
63896f2e892SBill Paul 
63996f2e892SBill Paul 	s = splimp();
64096f2e892SBill Paul 	/*
64196f2e892SBill Paul 	 * Set up frame for TX.
64296f2e892SBill Paul 	 */
64396f2e892SBill Paul 
64496f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
64596f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
64696f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
64796f2e892SBill Paul 
64896f2e892SBill Paul 	/*
64996f2e892SBill Paul 	 * Sync the PHYs.
65096f2e892SBill Paul 	 */
65196f2e892SBill Paul 	dc_mii_sync(sc);
65296f2e892SBill Paul 
65396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
65496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
65596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
65696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
65796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
65896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
65996f2e892SBill Paul 
66096f2e892SBill Paul 	/* Idle bit. */
66196f2e892SBill Paul 	dc_mii_writebit(sc, 0);
66296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
66396f2e892SBill Paul 
66496f2e892SBill Paul 	splx(s);
66596f2e892SBill Paul 
66696f2e892SBill Paul 	return(0);
66796f2e892SBill Paul }
66896f2e892SBill Paul 
66996f2e892SBill Paul static int dc_miibus_readreg(dev, phy, reg)
67096f2e892SBill Paul 	device_t		dev;
67196f2e892SBill Paul 	int			phy, reg;
67296f2e892SBill Paul {
67396f2e892SBill Paul 	struct dc_mii_frame	frame;
67496f2e892SBill Paul 	struct dc_softc		*sc;
67596f2e892SBill Paul 	int			i, rval, phy_reg;
67696f2e892SBill Paul 
67796f2e892SBill Paul 	sc = device_get_softc(dev);
67896f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
67996f2e892SBill Paul 
68096f2e892SBill Paul 	/*
68196f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
68296f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
68396f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
68496f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
68596f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
68696f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
68796f2e892SBill Paul 	 * that the PHY is at MII address 1.
68896f2e892SBill Paul 	 */
68996f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
69096f2e892SBill Paul 		return(0);
69196f2e892SBill Paul 
69296f2e892SBill Paul 	if (sc->dc_pmode == DC_PMODE_SYM) {
69396f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
69496f2e892SBill Paul 			switch(reg) {
69596f2e892SBill Paul 			case MII_BMSR:
69696f2e892SBill Paul 			/*
69796f2e892SBill Paul 			 * Fake something to make the probe
69896f2e892SBill Paul 			 * code think there's a PHY here.
69996f2e892SBill Paul 			 */
70096f2e892SBill Paul 				return(BMSR_MEDIAMASK);
70196f2e892SBill Paul 				break;
70296f2e892SBill Paul 			case MII_PHYIDR1:
70396f2e892SBill Paul 				if (DC_IS_PNIC(sc))
70496f2e892SBill Paul 					return(DC_VENDORID_LO);
70596f2e892SBill Paul 				return(DC_VENDORID_DEC);
70696f2e892SBill Paul 				break;
70796f2e892SBill Paul 			case MII_PHYIDR2:
70896f2e892SBill Paul 				if (DC_IS_PNIC(sc))
70996f2e892SBill Paul 					return(DC_DEVICEID_82C168);
71096f2e892SBill Paul 				return(DC_DEVICEID_21143);
71196f2e892SBill Paul 				break;
71296f2e892SBill Paul 			default:
71396f2e892SBill Paul 				return(0);
71496f2e892SBill Paul 				break;
71596f2e892SBill Paul 			}
71696f2e892SBill Paul 		} else
71796f2e892SBill Paul 			return(0);
71896f2e892SBill Paul 	}
71996f2e892SBill Paul 
72096f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
72196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
72296f2e892SBill Paul 		    (phy << 23) | (reg << 18));
72396f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
72496f2e892SBill Paul 			DELAY(1);
72596f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
72696f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
72796f2e892SBill Paul 				rval &= 0xFFFF;
72896f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
72996f2e892SBill Paul 			}
73096f2e892SBill Paul 		}
73196f2e892SBill Paul 		return(0);
73296f2e892SBill Paul 	}
73396f2e892SBill Paul 
73496f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
73596f2e892SBill Paul 		switch(reg) {
73696f2e892SBill Paul 		case MII_BMCR:
73796f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
73896f2e892SBill Paul 			break;
73996f2e892SBill Paul 		case MII_BMSR:
74096f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
74196f2e892SBill Paul 			break;
74296f2e892SBill Paul 		case MII_PHYIDR1:
74396f2e892SBill Paul 			phy_reg = DC_AL_VENID;
74496f2e892SBill Paul 			break;
74596f2e892SBill Paul 		case MII_PHYIDR2:
74696f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
74796f2e892SBill Paul 			break;
74896f2e892SBill Paul 		case MII_ANAR:
74996f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
75096f2e892SBill Paul 			break;
75196f2e892SBill Paul 		case MII_ANLPAR:
75296f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
75396f2e892SBill Paul 			break;
75496f2e892SBill Paul 		case MII_ANER:
75596f2e892SBill Paul 			phy_reg = DC_AL_ANER;
75696f2e892SBill Paul 			break;
75796f2e892SBill Paul 		default:
75896f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
75996f2e892SBill Paul 			    sc->dc_unit, reg);
76096f2e892SBill Paul 			return(0);
76196f2e892SBill Paul 			break;
76296f2e892SBill Paul 		}
76396f2e892SBill Paul 
76496f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
76596f2e892SBill Paul 
76696f2e892SBill Paul 		if (rval == 0xFFFF)
76796f2e892SBill Paul 			return(0);
76896f2e892SBill Paul 		return(rval);
76996f2e892SBill Paul 	}
77096f2e892SBill Paul 
77196f2e892SBill Paul 	frame.mii_phyaddr = phy;
77296f2e892SBill Paul 	frame.mii_regaddr = reg;
773f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
774f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
77596f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
776f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
77796f2e892SBill Paul 
77896f2e892SBill Paul 	return(frame.mii_data);
77996f2e892SBill Paul }
78096f2e892SBill Paul 
78196f2e892SBill Paul static int dc_miibus_writereg(dev, phy, reg, data)
78296f2e892SBill Paul 	device_t		dev;
78396f2e892SBill Paul 	int			phy, reg, data;
78496f2e892SBill Paul {
78596f2e892SBill Paul 	struct dc_softc		*sc;
78696f2e892SBill Paul 	struct dc_mii_frame	frame;
78796f2e892SBill Paul 	int			i, phy_reg;
78896f2e892SBill Paul 
78996f2e892SBill Paul 	sc = device_get_softc(dev);
79096f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
79196f2e892SBill Paul 
79296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
79396f2e892SBill Paul 		return(0);
79496f2e892SBill Paul 
79596f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
79696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
79796f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
79896f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
79996f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
80096f2e892SBill Paul 				break;
80196f2e892SBill Paul 		}
80296f2e892SBill Paul 		return(0);
80396f2e892SBill Paul 	}
80496f2e892SBill Paul 
80596f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
80696f2e892SBill Paul 		switch(reg) {
80796f2e892SBill Paul 		case MII_BMCR:
80896f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80996f2e892SBill Paul 			break;
81096f2e892SBill Paul 		case MII_BMSR:
81196f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
81296f2e892SBill Paul 			break;
81396f2e892SBill Paul 		case MII_PHYIDR1:
81496f2e892SBill Paul 			phy_reg = DC_AL_VENID;
81596f2e892SBill Paul 			break;
81696f2e892SBill Paul 		case MII_PHYIDR2:
81796f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
81896f2e892SBill Paul 			break;
81996f2e892SBill Paul 		case MII_ANAR:
82096f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
82196f2e892SBill Paul 			break;
82296f2e892SBill Paul 		case MII_ANLPAR:
82396f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
82496f2e892SBill Paul 			break;
82596f2e892SBill Paul 		case MII_ANER:
82696f2e892SBill Paul 			phy_reg = DC_AL_ANER;
82796f2e892SBill Paul 			break;
82896f2e892SBill Paul 		default:
82996f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
83096f2e892SBill Paul 			    sc->dc_unit, reg);
83196f2e892SBill Paul 			return(0);
83296f2e892SBill Paul 			break;
83396f2e892SBill Paul 		}
83496f2e892SBill Paul 
83596f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
83696f2e892SBill Paul 		return(0);
83796f2e892SBill Paul 	}
83896f2e892SBill Paul 
83996f2e892SBill Paul 	frame.mii_phyaddr = phy;
84096f2e892SBill Paul 	frame.mii_regaddr = reg;
84196f2e892SBill Paul 	frame.mii_data = data;
84296f2e892SBill Paul 
843f43d9309SBill Paul 	phy_reg = CSR_READ_4(sc, DC_NETCFG);
844f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
84596f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
846f43d9309SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
84796f2e892SBill Paul 
84896f2e892SBill Paul 	return(0);
84996f2e892SBill Paul }
85096f2e892SBill Paul 
85196f2e892SBill Paul static void dc_miibus_statchg(dev)
85296f2e892SBill Paul 	device_t		dev;
85396f2e892SBill Paul {
85496f2e892SBill Paul 	struct dc_softc		*sc;
85596f2e892SBill Paul 	struct mii_data		*mii;
856f43d9309SBill Paul 	struct ifmedia		*ifm;
85796f2e892SBill Paul 
85896f2e892SBill Paul 	sc = device_get_softc(dev);
85996f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
86096f2e892SBill Paul 		return;
86196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
862f43d9309SBill Paul 	ifm = &mii->mii_media;
863f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
864f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
865f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
866f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
867f43d9309SBill Paul 	} else {
86896f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
86996f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
870f43d9309SBill Paul 	}
871f43d9309SBill Paul 
872f43d9309SBill Paul 	return;
873f43d9309SBill Paul }
874f43d9309SBill Paul 
875f43d9309SBill Paul /*
876f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
877f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
878f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
879f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
880f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
881f43d9309SBill Paul  * with it itself. *sigh*
882f43d9309SBill Paul  */
883f43d9309SBill Paul static void dc_miibus_mediainit(dev)
884f43d9309SBill Paul 	device_t		dev;
885f43d9309SBill Paul {
886f43d9309SBill Paul 	struct dc_softc		*sc;
887f43d9309SBill Paul 	struct mii_data		*mii;
888f43d9309SBill Paul 	struct ifmedia		*ifm;
889f43d9309SBill Paul 	int			rev;
890f43d9309SBill Paul 
891f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
892f43d9309SBill Paul 
893f43d9309SBill Paul 	sc = device_get_softc(dev);
894f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
895f43d9309SBill Paul 	ifm = &mii->mii_media;
896f43d9309SBill Paul 
897f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
898f43d9309SBill Paul 		ifmedia_add(ifm, IFM_ETHER|IFM_homePNA, 0, NULL);
89996f2e892SBill Paul 
90096f2e892SBill Paul 	return;
90196f2e892SBill Paul }
90296f2e892SBill Paul 
90396f2e892SBill Paul #define DC_POLY		0xEDB88320
90496f2e892SBill Paul #define DC_BITS		9
90596f2e892SBill Paul #define DC_BITS_PNIC_II	7
90696f2e892SBill Paul 
90796f2e892SBill Paul static u_int32_t dc_crc_le(sc, addr)
90896f2e892SBill Paul 	struct dc_softc		*sc;
90996f2e892SBill Paul 	caddr_t			addr;
91096f2e892SBill Paul {
91196f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
91296f2e892SBill Paul 
91396f2e892SBill Paul 	/* Compute CRC for the address value. */
91496f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
91596f2e892SBill Paul 
91696f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
91796f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
91896f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
91996f2e892SBill Paul 	}
92096f2e892SBill Paul 
92196f2e892SBill Paul 	/* The hash table on the PNIC II is only 128 bits wide. */
92296f2e892SBill Paul 	if (DC_IS_PNICII(sc))
92396f2e892SBill Paul 		return (crc & ((1 << DC_BITS_PNIC_II) - 1));
92496f2e892SBill Paul 
92596f2e892SBill Paul 	return (crc & ((1 << DC_BITS) - 1));
92696f2e892SBill Paul }
92796f2e892SBill Paul 
92896f2e892SBill Paul /*
92996f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
93096f2e892SBill Paul  */
93196f2e892SBill Paul static u_int32_t dc_crc_be(addr)
93296f2e892SBill Paul 	caddr_t			addr;
93396f2e892SBill Paul {
93496f2e892SBill Paul 	u_int32_t		crc, carry;
93596f2e892SBill Paul 	int			i, j;
93696f2e892SBill Paul 	u_int8_t		c;
93796f2e892SBill Paul 
93896f2e892SBill Paul 	/* Compute CRC for the address value. */
93996f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
94096f2e892SBill Paul 
94196f2e892SBill Paul 	for (i = 0; i < 6; i++) {
94296f2e892SBill Paul 		c = *(addr + i);
94396f2e892SBill Paul 		for (j = 0; j < 8; j++) {
94496f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
94596f2e892SBill Paul 			crc <<= 1;
94696f2e892SBill Paul 			c >>= 1;
94796f2e892SBill Paul 			if (carry)
94896f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
94996f2e892SBill Paul 		}
95096f2e892SBill Paul 	}
95196f2e892SBill Paul 
95296f2e892SBill Paul 	/* return the filter bit position */
95396f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
95496f2e892SBill Paul }
95596f2e892SBill Paul 
95696f2e892SBill Paul /*
95796f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
95896f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
95996f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
96096f2e892SBill Paul  *
96196f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
96296f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
96396f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
96496f2e892SBill Paul  * we need that too.
96596f2e892SBill Paul  */
96696f2e892SBill Paul void dc_setfilt_21143(sc)
96796f2e892SBill Paul 	struct dc_softc		*sc;
96896f2e892SBill Paul {
96996f2e892SBill Paul 	struct dc_desc		*sframe;
97096f2e892SBill Paul 	u_int32_t		h, *sp;
97196f2e892SBill Paul 	struct ifmultiaddr	*ifma;
97296f2e892SBill Paul 	struct ifnet		*ifp;
97396f2e892SBill Paul 	int			i;
97496f2e892SBill Paul 
97596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
97696f2e892SBill Paul 
97796f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
97896f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
97996f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
98096f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
98196f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
98296f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
98396f2e892SBill Paul 
98496f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
98596f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
98696f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
98796f2e892SBill Paul 
98896f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
98996f2e892SBill Paul 
99096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
99196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
99296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99396f2e892SBill Paul 	else
99496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
99596f2e892SBill Paul 
99696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
99796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
99896f2e892SBill Paul 	else
99996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
100096f2e892SBill Paul 
100196f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
100296f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
100396f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
100496f2e892SBill Paul 			continue;
100596f2e892SBill Paul 		h = dc_crc_le(sc,
100696f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
100796f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
100896f2e892SBill Paul 	}
100996f2e892SBill Paul 
101096f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
101196f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
101296f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
101396f2e892SBill Paul 	}
101496f2e892SBill Paul 
101596f2e892SBill Paul 	/* Set our MAC address */
101696f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
101796f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
101896f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
101996f2e892SBill Paul 
102096f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
102196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
102296f2e892SBill Paul 
102396f2e892SBill Paul 	/*
102496f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
102596f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
102696f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
102796f2e892SBill Paul 	 * medicine.
102896f2e892SBill Paul 	 */
102996f2e892SBill Paul 	DELAY(10000);
103096f2e892SBill Paul 
103196f2e892SBill Paul 	ifp->if_timer = 5;
103296f2e892SBill Paul 
103396f2e892SBill Paul 	return;
103496f2e892SBill Paul }
103596f2e892SBill Paul 
103696f2e892SBill Paul void dc_setfilt_admtek(sc)
103796f2e892SBill Paul 	struct dc_softc		*sc;
103896f2e892SBill Paul {
103996f2e892SBill Paul 	struct ifnet		*ifp;
104096f2e892SBill Paul 	int			h = 0;
104196f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
104296f2e892SBill Paul 	struct ifmultiaddr	*ifma;
104396f2e892SBill Paul 
104496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
104596f2e892SBill Paul 
104696f2e892SBill Paul 	/* Init our MAC address */
104796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
104896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
104996f2e892SBill Paul 
105096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
105196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
105296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105396f2e892SBill Paul 	else
105496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
105596f2e892SBill Paul 
105696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
105796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
105896f2e892SBill Paul 	else
105996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
106096f2e892SBill Paul 
106196f2e892SBill Paul 	/* first, zot all the existing hash bits */
106296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
106396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
106496f2e892SBill Paul 
106596f2e892SBill Paul 	/*
106696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
106796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
106896f2e892SBill Paul 	 */
106996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
107096f2e892SBill Paul 		return;
107196f2e892SBill Paul 
107296f2e892SBill Paul 	/* now program new ones */
107396f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
107496f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
107596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
107696f2e892SBill Paul 			continue;
107796f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
107896f2e892SBill Paul 		if (h < 32)
107996f2e892SBill Paul 			hashes[0] |= (1 << h);
108096f2e892SBill Paul 		else
108196f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
108296f2e892SBill Paul 	}
108396f2e892SBill Paul 
108496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
108596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
108696f2e892SBill Paul 
108796f2e892SBill Paul 	return;
108896f2e892SBill Paul }
108996f2e892SBill Paul 
109096f2e892SBill Paul void dc_setfilt_asix(sc)
109196f2e892SBill Paul 	struct dc_softc		*sc;
109296f2e892SBill Paul {
109396f2e892SBill Paul 	struct ifnet		*ifp;
109496f2e892SBill Paul 	int			h = 0;
109596f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
109696f2e892SBill Paul 	struct ifmultiaddr	*ifma;
109796f2e892SBill Paul 
109896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
109996f2e892SBill Paul 
110096f2e892SBill Paul         /* Init our MAC address */
110196f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
110296f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
110396f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
110496f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
110596f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
110696f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
110796f2e892SBill Paul 
110896f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
110996f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
111096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111196f2e892SBill Paul 	else
111296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111396f2e892SBill Paul 
111496f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
111596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111696f2e892SBill Paul 	else
111796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111896f2e892SBill Paul 
111996f2e892SBill Paul 	/*
112096f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
112196f2e892SBill Paul 	 * of broadcast frames.
112296f2e892SBill Paul 	 */
112396f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
112496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
112596f2e892SBill Paul 	else
112696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
112796f2e892SBill Paul 
112896f2e892SBill Paul 	/* first, zot all the existing hash bits */
112996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
113096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
113196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
113296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
113396f2e892SBill Paul 
113496f2e892SBill Paul 	/*
113596f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
113696f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
113796f2e892SBill Paul 	 */
113896f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
113996f2e892SBill Paul 		return;
114096f2e892SBill Paul 
114196f2e892SBill Paul 	/* now program new ones */
114296f2e892SBill Paul 	for (ifma = ifp->if_multiaddrs.lh_first; ifma != NULL;
114396f2e892SBill Paul 	    ifma = ifma->ifma_link.le_next) {
114496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
114596f2e892SBill Paul 			continue;
114696f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
114796f2e892SBill Paul 		if (h < 32)
114896f2e892SBill Paul 			hashes[0] |= (1 << h);
114996f2e892SBill Paul 		else
115096f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
115196f2e892SBill Paul 	}
115296f2e892SBill Paul 
115396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
115496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
115596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
115696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
115796f2e892SBill Paul 
115896f2e892SBill Paul 	return;
115996f2e892SBill Paul }
116096f2e892SBill Paul 
116196f2e892SBill Paul static void dc_setfilt(sc)
116296f2e892SBill Paul 	struct dc_softc		*sc;
116396f2e892SBill Paul {
116496f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
116596f2e892SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc))
116696f2e892SBill Paul 		dc_setfilt_21143(sc);
116796f2e892SBill Paul 
116896f2e892SBill Paul 	if (DC_IS_ASIX(sc))
116996f2e892SBill Paul 		dc_setfilt_asix(sc);
117096f2e892SBill Paul 
117196f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
117296f2e892SBill Paul 		dc_setfilt_admtek(sc);
117396f2e892SBill Paul 
117496f2e892SBill Paul 	return;
117596f2e892SBill Paul }
117696f2e892SBill Paul 
117796f2e892SBill Paul /*
117896f2e892SBill Paul  * In order to fiddle with the
117996f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
118096f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
118196f2e892SBill Paul  */
118296f2e892SBill Paul static void dc_setcfg(sc, media)
118396f2e892SBill Paul 	struct dc_softc		*sc;
118496f2e892SBill Paul 	int			media;
118596f2e892SBill Paul {
118696f2e892SBill Paul 	int			i, restart = 0;
118796f2e892SBill Paul 	u_int32_t		isr;
118896f2e892SBill Paul 
118996f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
119096f2e892SBill Paul 		return;
119196f2e892SBill Paul 
119296f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
119396f2e892SBill Paul 		restart = 1;
119496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
119596f2e892SBill Paul 
119696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
119796f2e892SBill Paul 			DELAY(10);
119896f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
119996f2e892SBill Paul 			if (isr & DC_ISR_TX_IDLE ||
120096f2e892SBill Paul 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED)
120196f2e892SBill Paul 				break;
120296f2e892SBill Paul 		}
120396f2e892SBill Paul 
120496f2e892SBill Paul 		if (i == DC_TIMEOUT)
120596f2e892SBill Paul 			printf("dc%d: failed to force tx and "
120696f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
120796f2e892SBill Paul 
120896f2e892SBill Paul 	}
120996f2e892SBill Paul 
121096f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
121196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
121296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
121396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
121496f2e892SBill Paul 			DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
121596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
121696f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
121796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
121896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
121996f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
122088d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
122196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
122296f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
122396f2e892SBill Paul 		} else {
122496f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
122596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
122696f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
122796f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
122896f2e892SBill Paul 			}
122996f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL|
123096f2e892SBill Paul 			    DC_NETCFG_PCS|DC_NETCFG_SCRAMBLER);
123196f2e892SBill Paul 		}
123296f2e892SBill Paul 	}
123396f2e892SBill Paul 
123496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
123596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
123696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
123796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
123896f2e892SBill Paul 			DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
123996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
124096f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
124196f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
124296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
124388d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
124496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
124596f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
124696f2e892SBill Paul 		} else {
124796f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
124896f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
124996f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
125096f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
125196f2e892SBill Paul 			}
125296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
125396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
125496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
125596f2e892SBill Paul 		}
125696f2e892SBill Paul 	}
125796f2e892SBill Paul 
1258f43d9309SBill Paul 	/*
1259f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1260f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1261f43d9309SBill Paul 	 * on the external MII port.
1262f43d9309SBill Paul 	 */
1263f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
1264f43d9309SBill Paul 		if (IFM_SUBTYPE(media) == IFM_homePNA) {
1265f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1266f43d9309SBill Paul 			sc->dc_link = 1;
1267f43d9309SBill Paul 		} else {
1268f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1269f43d9309SBill Paul 		}
1270f43d9309SBill Paul 	}
1271f43d9309SBill Paul 
127296f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
127396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
127496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
127596f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
127696f2e892SBill Paul 	} else {
127796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
127896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
127996f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
128096f2e892SBill Paul 	}
128196f2e892SBill Paul 
128296f2e892SBill Paul 	if (restart)
128396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
128496f2e892SBill Paul 
128596f2e892SBill Paul 	return;
128696f2e892SBill Paul }
128796f2e892SBill Paul 
128896f2e892SBill Paul static void dc_reset(sc)
128996f2e892SBill Paul 	struct dc_softc		*sc;
129096f2e892SBill Paul {
129196f2e892SBill Paul 	register int		i;
129296f2e892SBill Paul 
129396f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
129496f2e892SBill Paul 
129596f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
129696f2e892SBill Paul 		DELAY(10);
129796f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
129896f2e892SBill Paul 			break;
129996f2e892SBill Paul 	}
130096f2e892SBill Paul 
130196f2e892SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc)) {
130296f2e892SBill Paul 		DELAY(10000);
130396f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
130496f2e892SBill Paul 		i = 0;
130596f2e892SBill Paul 	}
130696f2e892SBill Paul 
130796f2e892SBill Paul 	if (i == DC_TIMEOUT)
130896f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
130996f2e892SBill Paul 
131096f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
131196f2e892SBill Paul 	DELAY(1000);
131296f2e892SBill Paul 
131396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
131496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
131596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
131696f2e892SBill Paul 
131791cc2adbSBill Paul 	/*
131891cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
131991cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
132091cc2adbSBill Paul 	 * into a state where it will never come out of reset
132191cc2adbSBill Paul 	 * until we reset the whole chip again.
132291cc2adbSBill Paul 	 */
132391cc2adbSBill Paul 	if (DC_IS_INTEL(sc))
132491cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
132591cc2adbSBill Paul 
132696f2e892SBill Paul         return;
132796f2e892SBill Paul }
132896f2e892SBill Paul 
132996f2e892SBill Paul static struct dc_type *dc_devtype(dev)
133096f2e892SBill Paul 	device_t		dev;
133196f2e892SBill Paul {
133296f2e892SBill Paul 	struct dc_type		*t;
133396f2e892SBill Paul 	u_int32_t		rev;
133496f2e892SBill Paul 
133596f2e892SBill Paul 	t = dc_devs;
133696f2e892SBill Paul 
133796f2e892SBill Paul 	while(t->dc_name != NULL) {
133896f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
133996f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
134096f2e892SBill Paul 			/* Check the PCI revision */
134196f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
134296f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
134396f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
134496f2e892SBill Paul 				t++;
134596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
134696f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
134796f2e892SBill Paul 				t++;
134896f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
134996f2e892SBill Paul 			    rev >= DC_REVISION_98725)
135096f2e892SBill Paul 				t++;
135196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
135296f2e892SBill Paul 			    rev >= DC_REVISION_88141)
135396f2e892SBill Paul 				t++;
135496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
135596f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
135696f2e892SBill Paul 				t++;
135788d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
135888d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
135988d739dcSBill Paul 				t++;
136096f2e892SBill Paul 			return(t);
136196f2e892SBill Paul 		}
136296f2e892SBill Paul 		t++;
136396f2e892SBill Paul 	}
136496f2e892SBill Paul 
136596f2e892SBill Paul 	return(NULL);
136696f2e892SBill Paul }
136796f2e892SBill Paul 
136896f2e892SBill Paul /*
136996f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
137096f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
137196f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
137296f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
137396f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
137496f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
137596f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
137696f2e892SBill Paul  */
137796f2e892SBill Paul static int dc_probe(dev)
137896f2e892SBill Paul 	device_t		dev;
137996f2e892SBill Paul {
138096f2e892SBill Paul 	struct dc_type		*t;
138196f2e892SBill Paul 
138296f2e892SBill Paul 	t = dc_devtype(dev);
138396f2e892SBill Paul 
138496f2e892SBill Paul 	if (t != NULL) {
138596f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
138696f2e892SBill Paul 		return(0);
138796f2e892SBill Paul 	}
138896f2e892SBill Paul 
138996f2e892SBill Paul 	return(ENXIO);
139096f2e892SBill Paul }
139196f2e892SBill Paul 
139296f2e892SBill Paul static void dc_acpi(dev)
139396f2e892SBill Paul 	device_t		dev;
139496f2e892SBill Paul {
139596f2e892SBill Paul 	u_int32_t		r, cptr;
139696f2e892SBill Paul 	int			unit;
139796f2e892SBill Paul 
139896f2e892SBill Paul 	unit = device_get_unit(dev);
139996f2e892SBill Paul 
140096f2e892SBill Paul 	/* Find the location of the capabilities block */
140196f2e892SBill Paul 	cptr = pci_read_config(dev, DC_PCI_CCAP, 4) & 0xFF;
140296f2e892SBill Paul 
140396f2e892SBill Paul 	r = pci_read_config(dev, cptr, 4) & 0xFF;
140496f2e892SBill Paul 	if (r == 0x01) {
140596f2e892SBill Paul 
140696f2e892SBill Paul 		r = pci_read_config(dev, cptr + 4, 4);
140796f2e892SBill Paul 		if (r & DC_PSTATE_D3) {
140896f2e892SBill Paul 			u_int32_t		iobase, membase, irq;
140996f2e892SBill Paul 
141096f2e892SBill Paul 			/* Save important PCI config data. */
141196f2e892SBill Paul 			iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
141296f2e892SBill Paul 			membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
141396f2e892SBill Paul 			irq = pci_read_config(dev, DC_PCI_CFIT, 4);
141496f2e892SBill Paul 
141596f2e892SBill Paul 			/* Reset the power state. */
141696f2e892SBill Paul 			printf("dc%d: chip is in D%d power mode "
141796f2e892SBill Paul 			    "-- setting to D0\n", unit, r & DC_PSTATE_D3);
141896f2e892SBill Paul 			r &= 0xFFFFFFFC;
141996f2e892SBill Paul 			pci_write_config(dev, cptr + 4, r, 4);
142096f2e892SBill Paul 
142196f2e892SBill Paul 			/* Restore PCI config data. */
142296f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
142396f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
142496f2e892SBill Paul 			pci_write_config(dev, DC_PCI_CFIT, irq, 4);
142596f2e892SBill Paul 		}
142696f2e892SBill Paul 	}
142796f2e892SBill Paul 	return;
142896f2e892SBill Paul }
142996f2e892SBill Paul 
143096f2e892SBill Paul /*
143196f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
143296f2e892SBill Paul  * setup and ethernet/BPF attach.
143396f2e892SBill Paul  */
143496f2e892SBill Paul static int dc_attach(dev)
143596f2e892SBill Paul 	device_t		dev;
143696f2e892SBill Paul {
143796f2e892SBill Paul 	int			s;
143896f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
143996f2e892SBill Paul 	u_int32_t		command;
144096f2e892SBill Paul 	struct dc_softc		*sc;
144196f2e892SBill Paul 	struct ifnet		*ifp;
144296f2e892SBill Paul 	u_int32_t		revision;
144396f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
144496f2e892SBill Paul 
144596f2e892SBill Paul 	s = splimp();
144696f2e892SBill Paul 
144796f2e892SBill Paul 	sc = device_get_softc(dev);
144896f2e892SBill Paul 	unit = device_get_unit(dev);
144996f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
145096f2e892SBill Paul 
145196f2e892SBill Paul 	/*
145296f2e892SBill Paul 	 * Handle power management nonsense.
145396f2e892SBill Paul 	 */
145496f2e892SBill Paul 	dc_acpi(dev);
145596f2e892SBill Paul 
145696f2e892SBill Paul 	/*
145796f2e892SBill Paul 	 * Map control/status registers.
145896f2e892SBill Paul 	 */
145996f2e892SBill Paul 	command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4);
146096f2e892SBill Paul 	command |= (PCIM_CMD_PORTEN|PCIM_CMD_MEMEN|PCIM_CMD_BUSMASTEREN);
146196f2e892SBill Paul 	pci_write_config(dev, PCI_COMMAND_STATUS_REG, command, 4);
146296f2e892SBill Paul 	command = pci_read_config(dev, PCI_COMMAND_STATUS_REG, 4);
146396f2e892SBill Paul 
146496f2e892SBill Paul #ifdef DC_USEIOSPACE
146596f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
146696f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
146796f2e892SBill Paul 		error = ENXIO;
146896f2e892SBill Paul 		goto fail;
146996f2e892SBill Paul 	}
147096f2e892SBill Paul #else
147196f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
147296f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
147396f2e892SBill Paul 		error = ENXIO;
147496f2e892SBill Paul 		goto fail;
147596f2e892SBill Paul 	}
147696f2e892SBill Paul #endif
147796f2e892SBill Paul 
147896f2e892SBill Paul 	rid = DC_RID;
147996f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
148096f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
148196f2e892SBill Paul 
148296f2e892SBill Paul 	if (sc->dc_res == NULL) {
148396f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
148496f2e892SBill Paul 		error = ENXIO;
148596f2e892SBill Paul 		goto fail;
148696f2e892SBill Paul 	}
148796f2e892SBill Paul 
148896f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
148996f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
149096f2e892SBill Paul 
149196f2e892SBill Paul 	/* Allocate interrupt */
149296f2e892SBill Paul 	rid = 0;
149396f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
149496f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
149596f2e892SBill Paul 
149696f2e892SBill Paul 	if (sc->dc_irq == NULL) {
149796f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
149896f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
149996f2e892SBill Paul 		error = ENXIO;
150096f2e892SBill Paul 		goto fail;
150196f2e892SBill Paul 	}
150296f2e892SBill Paul 
150396f2e892SBill Paul 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET,
150496f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
150596f2e892SBill Paul 
150696f2e892SBill Paul 	if (error) {
150796f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
150896f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
150996f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
151096f2e892SBill Paul 		goto fail;
151196f2e892SBill Paul 	}
151296f2e892SBill Paul 
151396f2e892SBill Paul 	/* Need this info to decide on a chip type. */
151496f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
151596f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
151696f2e892SBill Paul 
151796f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
151896f2e892SBill Paul 	case DC_DEVICEID_21143:
151996f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
152096f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
152196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
152296f2e892SBill Paul 		break;
152396f2e892SBill Paul 	case DC_DEVICEID_DM9100:
152496f2e892SBill Paul 	case DC_DEVICEID_DM9102:
152596f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1526fda39fd0SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_USE_TX_INTR;
152796f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
152896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
152996f2e892SBill Paul 		break;
153096f2e892SBill Paul 	case DC_DEVICEID_AL981:
153196f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
153296f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
153396f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
153496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
153596f2e892SBill Paul 		break;
153696f2e892SBill Paul 	case DC_DEVICEID_AN985:
153796f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
153896f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
153996f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
154096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
154196f2e892SBill Paul 		break;
154296f2e892SBill Paul 	case DC_DEVICEID_98713:
154396f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
154496f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
154596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
154696f2e892SBill Paul 			sc->dc_flags |= DC_REDUCED_MII_POLL;
154796f2e892SBill Paul 		}
154896f2e892SBill Paul 		if (revision >= DC_REVISION_98713A)
154996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
155096f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
155196f2e892SBill Paul 		break;
155296f2e892SBill Paul 	case DC_DEVICEID_987x5:
155396f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
155496f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
155596f2e892SBill Paul 		break;
155696f2e892SBill Paul 	case DC_DEVICEID_82C115:
155796f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
155896f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
155996f2e892SBill Paul 		break;
156096f2e892SBill Paul 	case DC_DEVICEID_82C168:
156196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
156291cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
156396f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
156496f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
156596f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
156696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
156796f2e892SBill Paul 		break;
156896f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
156996f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
157096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
157196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
157296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
157396f2e892SBill Paul 		break;
157496f2e892SBill Paul 	default:
157596f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
157696f2e892SBill Paul 		    sc->dc_info->dc_did);
157796f2e892SBill Paul 		break;
157896f2e892SBill Paul 	}
157996f2e892SBill Paul 
158096f2e892SBill Paul 	/* Save the cache line size. */
158188d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
158288d739dcSBill Paul 		sc->dc_cachesize = 0;
158388d739dcSBill Paul 	else
158488d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
158588d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
158696f2e892SBill Paul 
158796f2e892SBill Paul 	/* Reset the adapter. */
158896f2e892SBill Paul 	dc_reset(sc);
158996f2e892SBill Paul 
159096f2e892SBill Paul 	/* Take 21143 out of snooze mode */
159196f2e892SBill Paul 	if (DC_IS_INTEL(sc)) {
159296f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
159396f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
159496f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
159596f2e892SBill Paul 	}
159696f2e892SBill Paul 
159796f2e892SBill Paul 	/*
159896f2e892SBill Paul 	 * Try to learn something about the supported media.
159996f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
160096f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
160196f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
160296f2e892SBill Paul 	 * Intel 21143.
160396f2e892SBill Paul 	 */
160496f2e892SBill Paul 	if (DC_IS_INTEL(sc)) {
160596f2e892SBill Paul 		u_int32_t		media, cwuc;
160696f2e892SBill Paul 		cwuc = pci_read_config(dev, DC_PCI_CWUC, 4);
160796f2e892SBill Paul 		cwuc |= DC_CWUC_FORCE_WUL;
160896f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CWUC, cwuc, 4);
160996f2e892SBill Paul 		DELAY(10000);
161096f2e892SBill Paul 		media = pci_read_config(dev, DC_PCI_CWUC, 4);
161196f2e892SBill Paul 		cwuc &= ~DC_CWUC_FORCE_WUL;
161296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CWUC, cwuc, 4);
161396f2e892SBill Paul 		DELAY(10000);
161496f2e892SBill Paul 		if (media & DC_CWUC_MII_ABILITY)
161596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
161696f2e892SBill Paul 		if (media & DC_CWUC_SYM_ABILITY)
161796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
161896f2e892SBill Paul 		/*
161996f2e892SBill Paul 		 * If none of the bits are set, then this NIC
162096f2e892SBill Paul 		 * isn't meant to support 'wake up LAN' mode.
162196f2e892SBill Paul 		 * This is usually only the case on multiport
162296f2e892SBill Paul 		 * cards, and these cards almost always have
162396f2e892SBill Paul 		 * MII transceivers.
162496f2e892SBill Paul 		 */
162596f2e892SBill Paul 		if (media == 0)
162696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
162796f2e892SBill Paul 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
162896f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
162996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
163096f2e892SBill Paul 		else
163196f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
163296f2e892SBill Paul 	} else if (!sc->dc_pmode)
163396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
163496f2e892SBill Paul 
163596f2e892SBill Paul 	/*
163696f2e892SBill Paul 	 * Get station address from the EEPROM.
163796f2e892SBill Paul 	 */
163896f2e892SBill Paul 	switch(sc->dc_type) {
163996f2e892SBill Paul 	case DC_TYPE_98713:
164096f2e892SBill Paul 	case DC_TYPE_98713A:
164196f2e892SBill Paul 	case DC_TYPE_987x5:
164296f2e892SBill Paul 	case DC_TYPE_PNICII:
164396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
164496f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
164596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
164696f2e892SBill Paul 		break;
164796f2e892SBill Paul 	case DC_TYPE_PNIC:
164896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
164996f2e892SBill Paul 		break;
165096f2e892SBill Paul 	case DC_TYPE_DM9102:
165196f2e892SBill Paul 	case DC_TYPE_21143:
165296f2e892SBill Paul 	case DC_TYPE_ASIX:
165396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
165496f2e892SBill Paul 		break;
165596f2e892SBill Paul 	case DC_TYPE_AL981:
165696f2e892SBill Paul 	case DC_TYPE_AN985:
165796f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
165896f2e892SBill Paul 		break;
165996f2e892SBill Paul 	default:
166096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
166196f2e892SBill Paul 		break;
166296f2e892SBill Paul 	}
166396f2e892SBill Paul 
166496f2e892SBill Paul 	/*
166596f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
166696f2e892SBill Paul 	 */
166796f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
166896f2e892SBill Paul 
166996f2e892SBill Paul 	sc->dc_unit = unit;
167096f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
167196f2e892SBill Paul 
167296f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
167396f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
167496f2e892SBill Paul 
167596f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
167696f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
167796f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
167896f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
167996f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
168096f2e892SBill Paul 		error = ENXIO;
168196f2e892SBill Paul 		goto fail;
168296f2e892SBill Paul 	}
168396f2e892SBill Paul 
168496f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
168596f2e892SBill Paul 
168696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
168796f2e892SBill Paul 	ifp->if_softc = sc;
168896f2e892SBill Paul 	ifp->if_unit = unit;
168996f2e892SBill Paul 	ifp->if_name = "dc";
169096f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
169196f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
169296f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
169396f2e892SBill Paul 	ifp->if_output = ether_output;
169496f2e892SBill Paul 	ifp->if_start = dc_start;
169596f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
169696f2e892SBill Paul 	ifp->if_init = dc_init;
169796f2e892SBill Paul 	ifp->if_baudrate = 10000000;
169896f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
169996f2e892SBill Paul 
170096f2e892SBill Paul 	/*
170196f2e892SBill Paul 	 * Do MII setup.
170296f2e892SBill Paul 	 */
170396f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
170496f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
170596f2e892SBill Paul 
170696f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
170796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_SYM;
170896f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
170996f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
171096f2e892SBill Paul 		error = 0;
171196f2e892SBill Paul 	}
171296f2e892SBill Paul 
171396f2e892SBill Paul 	if (error) {
171496f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
171596f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
171696f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
171796f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
171896f2e892SBill Paul 		error = ENXIO;
171996f2e892SBill Paul 		goto fail;
172096f2e892SBill Paul 	}
172196f2e892SBill Paul 
172296f2e892SBill Paul 	/*
172396f2e892SBill Paul 	 * Call MI attach routines.
172496f2e892SBill Paul 	 */
172596f2e892SBill Paul 	if_attach(ifp);
172696f2e892SBill Paul 	ether_ifattach(ifp);
172796f2e892SBill Paul 	callout_handle_init(&sc->dc_stat_ch);
172896f2e892SBill Paul 
172996f2e892SBill Paul 	bpfattach(ifp, DLT_EN10MB, sizeof(struct ether_header));
173096f2e892SBill Paul 
1731510a809eSMike Smith #ifdef __alpha__
1732510a809eSMike Smith         sc->dc_srm_media = 0;
1733510a809eSMike Smith 
1734510a809eSMike Smith 	/* Remember the SRM console media setting */
1735510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
1736510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
1737510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
1738510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
1739510a809eSMike Smith 		case 3:
1740510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
1741510a809eSMike Smith 			break;
1742510a809eSMike Smith 		case 4:
1743510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
1744510a809eSMike Smith 			break;
1745510a809eSMike Smith 		case 5:
1746510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
1747510a809eSMike Smith 			break;
1748510a809eSMike Smith 		case 6:
1749510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
1750510a809eSMike Smith 			break;
1751510a809eSMike Smith 		}
1752510a809eSMike Smith 		if (sc->dc_srm_media)
1753510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
1754510a809eSMike Smith 	}
1755510a809eSMike Smith #endif
1756510a809eSMike Smith 
1757510a809eSMike Smith 
175896f2e892SBill Paul fail:
175996f2e892SBill Paul 	splx(s);
176096f2e892SBill Paul 
176196f2e892SBill Paul 	return(error);
176296f2e892SBill Paul }
176396f2e892SBill Paul 
176496f2e892SBill Paul static int dc_detach(dev)
176596f2e892SBill Paul 	device_t		dev;
176696f2e892SBill Paul {
176796f2e892SBill Paul 	struct dc_softc		*sc;
176896f2e892SBill Paul 	struct ifnet		*ifp;
176996f2e892SBill Paul 	int			s;
177096f2e892SBill Paul 
177196f2e892SBill Paul 	s = splimp();
177296f2e892SBill Paul 
177396f2e892SBill Paul 	sc = device_get_softc(dev);
177496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
177596f2e892SBill Paul 
177696f2e892SBill Paul 	dc_stop(sc);
177796f2e892SBill Paul 	if_detach(ifp);
177896f2e892SBill Paul 
177996f2e892SBill Paul 	bus_generic_detach(dev);
178096f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
178196f2e892SBill Paul 
178296f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
178396f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
178496f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
178596f2e892SBill Paul 
178696f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
178796f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
178896f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
178996f2e892SBill Paul 
179096f2e892SBill Paul 	splx(s);
179196f2e892SBill Paul 
179296f2e892SBill Paul 	return(0);
179396f2e892SBill Paul }
179496f2e892SBill Paul 
179596f2e892SBill Paul /*
179696f2e892SBill Paul  * Initialize the transmit descriptors.
179796f2e892SBill Paul  */
179896f2e892SBill Paul static int dc_list_tx_init(sc)
179996f2e892SBill Paul 	struct dc_softc		*sc;
180096f2e892SBill Paul {
180196f2e892SBill Paul 	struct dc_chain_data	*cd;
180296f2e892SBill Paul 	struct dc_list_data	*ld;
180396f2e892SBill Paul 	int			i;
180496f2e892SBill Paul 
180596f2e892SBill Paul 	cd = &sc->dc_cdata;
180696f2e892SBill Paul 	ld = sc->dc_ldata;
180796f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
180896f2e892SBill Paul 		if (i == (DC_TX_LIST_CNT - 1)) {
180996f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
181096f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[0]);
181196f2e892SBill Paul 		} else {
181296f2e892SBill Paul 			ld->dc_tx_list[i].dc_next =
181396f2e892SBill Paul 			    vtophys(&ld->dc_tx_list[i + 1]);
181496f2e892SBill Paul 		}
181596f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
181696f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
181796f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
181896f2e892SBill Paul 	}
181996f2e892SBill Paul 
182096f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
182196f2e892SBill Paul 
182296f2e892SBill Paul 	return(0);
182396f2e892SBill Paul }
182496f2e892SBill Paul 
182596f2e892SBill Paul 
182696f2e892SBill Paul /*
182796f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
182896f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
182996f2e892SBill Paul  * points back to the first.
183096f2e892SBill Paul  */
183196f2e892SBill Paul static int dc_list_rx_init(sc)
183296f2e892SBill Paul 	struct dc_softc		*sc;
183396f2e892SBill Paul {
183496f2e892SBill Paul 	struct dc_chain_data	*cd;
183596f2e892SBill Paul 	struct dc_list_data	*ld;
183696f2e892SBill Paul 	int			i;
183796f2e892SBill Paul 
183896f2e892SBill Paul 	cd = &sc->dc_cdata;
183996f2e892SBill Paul 	ld = sc->dc_ldata;
184096f2e892SBill Paul 
184196f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
184296f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
184396f2e892SBill Paul 			return(ENOBUFS);
184496f2e892SBill Paul 		if (i == (DC_RX_LIST_CNT - 1)) {
184596f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
184696f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[0]);
184796f2e892SBill Paul 		} else {
184896f2e892SBill Paul 			ld->dc_rx_list[i].dc_next =
184996f2e892SBill Paul 			    vtophys(&ld->dc_rx_list[i + 1]);
185096f2e892SBill Paul 		}
185196f2e892SBill Paul 	}
185296f2e892SBill Paul 
185396f2e892SBill Paul 	cd->dc_rx_prod = 0;
185496f2e892SBill Paul 
185596f2e892SBill Paul 	return(0);
185696f2e892SBill Paul }
185796f2e892SBill Paul 
185896f2e892SBill Paul /*
185996f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
186096f2e892SBill Paul  */
186196f2e892SBill Paul static int dc_newbuf(sc, i, m)
186296f2e892SBill Paul 	struct dc_softc		*sc;
186396f2e892SBill Paul 	int			i;
186496f2e892SBill Paul 	struct mbuf		*m;
186596f2e892SBill Paul {
186696f2e892SBill Paul 	struct mbuf		*m_new = NULL;
186796f2e892SBill Paul 	struct dc_desc		*c;
186896f2e892SBill Paul 
186996f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
187096f2e892SBill Paul 
187196f2e892SBill Paul 	if (m == NULL) {
187296f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
187396f2e892SBill Paul 		if (m_new == NULL) {
187496f2e892SBill Paul 			printf("dc%d: no memory for rx list "
187596f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
187696f2e892SBill Paul 			return(ENOBUFS);
187796f2e892SBill Paul 		}
187896f2e892SBill Paul 
187996f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
188096f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
188196f2e892SBill Paul 			printf("dc%d: no memory for rx list "
188296f2e892SBill Paul 			    "-- packet dropped!\n", sc->dc_unit);
188396f2e892SBill Paul 			m_freem(m_new);
188496f2e892SBill Paul 			return(ENOBUFS);
188596f2e892SBill Paul 		}
188696f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
188796f2e892SBill Paul 	} else {
188896f2e892SBill Paul 		m_new = m;
188996f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
189096f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
189196f2e892SBill Paul 	}
189296f2e892SBill Paul 
189396f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
189496f2e892SBill Paul 
189596f2e892SBill Paul 	/*
189696f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
189796f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
189896f2e892SBill Paul 	 * 82c169 chips.
189996f2e892SBill Paul 	 */
190096f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
190196f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
190296f2e892SBill Paul 
190396f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
190496f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
190596f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
190696f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
190796f2e892SBill Paul 
190896f2e892SBill Paul 	return(0);
190996f2e892SBill Paul }
191096f2e892SBill Paul 
191196f2e892SBill Paul /*
191296f2e892SBill Paul  * Grrrrr.
191396f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
191496f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
191596f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
191696f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
191796f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
191896f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
191996f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
192096f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
192196f2e892SBill Paul  *
192296f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
192396f2e892SBill Paul  * Here's what we know:
192496f2e892SBill Paul  *
192596f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
192696f2e892SBill Paul  *   descriptors uploaded.
192796f2e892SBill Paul  *
192896f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
192996f2e892SBill Paul  *   total data upload.
193096f2e892SBill Paul  *
193196f2e892SBill Paul  * - We know the size of the desired received frame because it will be
193296f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
193396f2e892SBill Paul  *
193496f2e892SBill Paul  * Here's what we do:
193596f2e892SBill Paul  *
193696f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
193796f2e892SBill Paul  *   This means that we know that the buffer contents should be all
193896f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
193996f2e892SBill Paul  *
194096f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
194196f2e892SBill Paul  *   ethernet CRC at the end.
194296f2e892SBill Paul  *
194396f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
194496f2e892SBill Paul  *
194596f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
194696f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
194796f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
194896f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
194996f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
195096f2e892SBill Paul  *   we won't be fooled.
195196f2e892SBill Paul  *
195296f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
195396f2e892SBill Paul  *   that value from the current pointer location. This brings us
195496f2e892SBill Paul  *   to the start of the actual received packet.
195596f2e892SBill Paul  *
195696f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
195796f2e892SBill Paul  *   frame length.
195896f2e892SBill Paul  *
195996f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
196096f2e892SBill Paul  * the time.
196196f2e892SBill Paul  */
196296f2e892SBill Paul 
196396f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
196496f2e892SBill Paul static void dc_pnic_rx_bug_war(sc, idx)
196596f2e892SBill Paul 	struct dc_softc		*sc;
196696f2e892SBill Paul 	int			idx;
196796f2e892SBill Paul {
196896f2e892SBill Paul 	struct dc_desc		*cur_rx;
196996f2e892SBill Paul 	struct dc_desc		*c = NULL;
197096f2e892SBill Paul 	struct mbuf		*m = NULL;
197196f2e892SBill Paul 	unsigned char		*ptr;
197296f2e892SBill Paul 	int			i, total_len;
197396f2e892SBill Paul 	u_int32_t		rxstat = 0;
197496f2e892SBill Paul 
197596f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
197696f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
197796f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
197896f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
197996f2e892SBill Paul 
198096f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
198196f2e892SBill Paul 	while (1) {
198296f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
198396f2e892SBill Paul 		rxstat = c->dc_status;
198496f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
198596f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
198696f2e892SBill Paul 		ptr += DC_RXLEN;
198796f2e892SBill Paul 		/* If this is the last buffer, break out. */
198896f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
198996f2e892SBill Paul 			break;
199096f2e892SBill Paul 		dc_newbuf(sc, i, m);
199196f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
199296f2e892SBill Paul 	}
199396f2e892SBill Paul 
199496f2e892SBill Paul 	/* Find the length of the actual receive frame. */
199596f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
199696f2e892SBill Paul 
199796f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
199896f2e892SBill Paul 	while(*ptr == 0x00)
199996f2e892SBill Paul 		ptr--;
200096f2e892SBill Paul 
200196f2e892SBill Paul 	/* Round off. */
200296f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
200396f2e892SBill Paul 		ptr -= 1;
200496f2e892SBill Paul 
200596f2e892SBill Paul 	/* Now find the start of the frame. */
200696f2e892SBill Paul 	ptr -= total_len;
200796f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
200896f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
200996f2e892SBill Paul 
201096f2e892SBill Paul 	/*
201196f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
201296f2e892SBill Paul 	 * the status word to make it look like a successful
201396f2e892SBill Paul  	 * frame reception.
201496f2e892SBill Paul 	 */
201596f2e892SBill Paul 	dc_newbuf(sc, i, m);
201696f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
201796f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
201896f2e892SBill Paul 
201996f2e892SBill Paul 	return;
202096f2e892SBill Paul }
202196f2e892SBill Paul 
202296f2e892SBill Paul /*
202373bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
202473bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
202573bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
202673bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
202773bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
202873bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
202973bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
203073bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
203173bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
203273bf949cSBill Paul  */
203373bf949cSBill Paul static int dc_rx_resync(sc)
203473bf949cSBill Paul 	struct dc_softc		*sc;
203573bf949cSBill Paul {
203673bf949cSBill Paul 	int			i, pos;
203773bf949cSBill Paul 	struct dc_desc		*cur_rx;
203873bf949cSBill Paul 
203973bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
204073bf949cSBill Paul 
204173bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
204273bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
204373bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
204473bf949cSBill Paul 			break;
204573bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
204673bf949cSBill Paul 	}
204773bf949cSBill Paul 
204873bf949cSBill Paul 	/* If the ring really is empty, then just return. */
204973bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
205073bf949cSBill Paul 		return(0);
205173bf949cSBill Paul 
205273bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
205373bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
205473bf949cSBill Paul 
205573bf949cSBill Paul 	return(EAGAIN);
205673bf949cSBill Paul }
205773bf949cSBill Paul 
205873bf949cSBill Paul /*
205996f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
206096f2e892SBill Paul  * the higher level protocols.
206196f2e892SBill Paul  */
206296f2e892SBill Paul static void dc_rxeof(sc)
206396f2e892SBill Paul 	struct dc_softc		*sc;
206496f2e892SBill Paul {
206596f2e892SBill Paul         struct ether_header	*eh;
206696f2e892SBill Paul         struct mbuf		*m;
206796f2e892SBill Paul         struct ifnet		*ifp;
206896f2e892SBill Paul 	struct dc_desc		*cur_rx;
206996f2e892SBill Paul 	int			i, total_len = 0;
207096f2e892SBill Paul 	u_int32_t		rxstat;
207196f2e892SBill Paul 
207296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
207396f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
207496f2e892SBill Paul 
207596f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
207696f2e892SBill Paul 		struct mbuf		*m0 = NULL;
207796f2e892SBill Paul 
207896f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
207996f2e892SBill Paul 		rxstat = cur_rx->dc_status;
208096f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
208196f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
208296f2e892SBill Paul 
208396f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
208496f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
208596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
208696f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
208796f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
208896f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
208996f2e892SBill Paul 					continue;
209096f2e892SBill Paul 				}
209196f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
209296f2e892SBill Paul 				rxstat = cur_rx->dc_status;
209396f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
209496f2e892SBill Paul 			}
209596f2e892SBill Paul 		}
209696f2e892SBill Paul 
209796f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
209896f2e892SBill Paul 
209996f2e892SBill Paul 		/*
210096f2e892SBill Paul 		 * If an error occurs, update stats, clear the
210196f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
210296f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
210396f2e892SBill Paul 	 	 * comes up in the ring.
210496f2e892SBill Paul 		 */
210596f2e892SBill Paul 		if (rxstat & DC_RXSTAT_RXERR) {
210696f2e892SBill Paul 			ifp->if_ierrors++;
210796f2e892SBill Paul 			if (rxstat & DC_RXSTAT_COLLSEEN)
210896f2e892SBill Paul 				ifp->if_collisions++;
210996f2e892SBill Paul 			dc_newbuf(sc, i, m);
211096f2e892SBill Paul 			if (rxstat & DC_RXSTAT_CRCERR) {
211196f2e892SBill Paul 				DC_INC(i, DC_RX_LIST_CNT);
211296f2e892SBill Paul 				continue;
211396f2e892SBill Paul 			} else {
211496f2e892SBill Paul 				dc_init(sc);
211596f2e892SBill Paul 				return;
211696f2e892SBill Paul 			}
211796f2e892SBill Paul 		}
211896f2e892SBill Paul 
211996f2e892SBill Paul 		/* No errors; receive the packet. */
212096f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
212196f2e892SBill Paul 
212296f2e892SBill Paul 		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
212396f2e892SBill Paul 		    total_len + ETHER_ALIGN, 0, ifp, NULL);
212496f2e892SBill Paul 		dc_newbuf(sc, i, m);
212596f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
212696f2e892SBill Paul 		if (m0 == NULL) {
212796f2e892SBill Paul 			ifp->if_ierrors++;
212896f2e892SBill Paul 			continue;
212996f2e892SBill Paul 		}
213096f2e892SBill Paul 		m_adj(m0, ETHER_ALIGN);
213196f2e892SBill Paul 		m = m0;
213296f2e892SBill Paul 
213396f2e892SBill Paul 		ifp->if_ipackets++;
213496f2e892SBill Paul 		eh = mtod(m, struct ether_header *);
213596f2e892SBill Paul 
2136c8cf61e1SRobert Watson 		/* Handle BPF listeners. Let the BPF user see the packet */
2137c8cf61e1SRobert Watson 		if (ifp->if_bpf)
2138c8cf61e1SRobert Watson 			bpf_mtap(ifp, m);
2139c8cf61e1SRobert Watson 
2140c8cf61e1SRobert Watson #ifdef BRIDGE
2141c8cf61e1SRobert Watson 		if (do_bridge) {
2142c8cf61e1SRobert Watson 			struct ifnet *bdg_ifp ;
2143c8cf61e1SRobert Watson 			bdg_ifp = bridge_in(m);
2144c8cf61e1SRobert Watson 			if (bdg_ifp != BDG_LOCAL && bdg_ifp != BDG_DROP)
2145c8cf61e1SRobert Watson 				bdg_forward(&m, bdg_ifp);
2146c8cf61e1SRobert Watson 			if (((bdg_ifp != BDG_LOCAL) && (bdg_ifp != BDG_BCAST) &&			    (bdg_ifp != BDG_MCAST)) || bdg_ifp == BDG_DROP) {
2147c8cf61e1SRobert Watson 				m_freem(m);
2148c8cf61e1SRobert Watson 				continue;
2149c8cf61e1SRobert Watson 			}
2150c8cf61e1SRobert Watson 		}
2151c8cf61e1SRobert Watson 
2152c8cf61e1SRobert Watson 	eh = mtod(m, struct ether_header *);
2153c8cf61e1SRobert Watson #endif
2154c8cf61e1SRobert Watson 
2155c8cf61e1SRobert Watson 		/* Don't pass it up to the ether_input() layer unless it's
215696f2e892SBill Paul 		 * a broadcast packet, multicast packet, matches our ethernet
215796f2e892SBill Paul 		 * address or the interface is in promiscuous mode.
215896f2e892SBill Paul 		 */
215996f2e892SBill Paul 		if (ifp->if_bpf) {
216096f2e892SBill Paul 			if (ifp->if_flags & IFF_PROMISC &&
216196f2e892SBill Paul 				(bcmp(eh->ether_dhost, sc->arpcom.ac_enaddr,
216296f2e892SBill Paul 				    ETHER_ADDR_LEN) &&
216396f2e892SBill Paul 				    (eh->ether_dhost[0] & 1) == 0)) {
216496f2e892SBill Paul 				m_freem(m);
216596f2e892SBill Paul 				continue;
216696f2e892SBill Paul 			}
216796f2e892SBill Paul 		}
216896f2e892SBill Paul 
216996f2e892SBill Paul 		/* Remove header from mbuf and pass it on. */
217096f2e892SBill Paul 		m_adj(m, sizeof(struct ether_header));
217196f2e892SBill Paul 		ether_input(ifp, eh, m);
217296f2e892SBill Paul 	}
217396f2e892SBill Paul 
217496f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
217596f2e892SBill Paul 
217696f2e892SBill Paul 	return;
217796f2e892SBill Paul }
217896f2e892SBill Paul 
217996f2e892SBill Paul /*
218096f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
218196f2e892SBill Paul  * the list buffers.
218296f2e892SBill Paul  */
218396f2e892SBill Paul 
218496f2e892SBill Paul static void dc_txeof(sc)
218596f2e892SBill Paul 	struct dc_softc		*sc;
218696f2e892SBill Paul {
218796f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
218896f2e892SBill Paul 	struct ifnet		*ifp;
218996f2e892SBill Paul 	int			idx;
219096f2e892SBill Paul 
219196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
219296f2e892SBill Paul 
219396f2e892SBill Paul 	/* Clear the timeout timer. */
219496f2e892SBill Paul 	ifp->if_timer = 0;
219596f2e892SBill Paul 
219696f2e892SBill Paul 	/*
219796f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
219896f2e892SBill Paul 	 * frames that have been transmitted.
219996f2e892SBill Paul 	 */
220096f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
220196f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
220296f2e892SBill Paul 		u_int32_t		txstat;
220396f2e892SBill Paul 
220496f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
220596f2e892SBill Paul 		txstat = cur_tx->dc_status;
220696f2e892SBill Paul 
220796f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
220896f2e892SBill Paul 			break;
220996f2e892SBill Paul 
221096f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
221196f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
221296f2e892SBill Paul 			sc->dc_cdata.dc_tx_cnt--;
221396f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
221496f2e892SBill Paul 				/*
221596f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
221696f2e892SBill Paul 				 * that it will sometimes generate a TX
221796f2e892SBill Paul 				 * underrun error while DMAing the RX
221896f2e892SBill Paul 				 * filter setup frame. If we detect this,
221996f2e892SBill Paul 				 * we have to send the setup frame again,
222096f2e892SBill Paul 				 * or else the filter won't be programmed
222196f2e892SBill Paul 				 * correctly.
222296f2e892SBill Paul 				 */
222396f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
222496f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
222596f2e892SBill Paul 						dc_setfilt(sc);
222696f2e892SBill Paul 				}
222796f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
222896f2e892SBill Paul 			}
222996f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
223096f2e892SBill Paul 			continue;
223196f2e892SBill Paul 		}
223296f2e892SBill Paul 
223396f2e892SBill Paul 		if (/*sc->dc_type == DC_TYPE_21143 &&*/
223496f2e892SBill Paul 		    sc->dc_pmode == DC_PMODE_MII &&
223596f2e892SBill Paul 		    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
223696f2e892SBill Paul 		    DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
223796f2e892SBill Paul 			txstat &= ~DC_TXSTAT_ERRSUM;
223896f2e892SBill Paul 
223996f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
224096f2e892SBill Paul 			ifp->if_oerrors++;
224196f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
224296f2e892SBill Paul 				ifp->if_collisions++;
224396f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
224496f2e892SBill Paul 				ifp->if_collisions++;
224596f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
224696f2e892SBill Paul 				dc_init(sc);
224796f2e892SBill Paul 				return;
224896f2e892SBill Paul 			}
224996f2e892SBill Paul 		}
225096f2e892SBill Paul 
225196f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
225296f2e892SBill Paul 
225396f2e892SBill Paul 		ifp->if_opackets++;
225496f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
225596f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
225696f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
225796f2e892SBill Paul 		}
225896f2e892SBill Paul 
225996f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
226096f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
226196f2e892SBill Paul 	}
226296f2e892SBill Paul 
226396f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
226496f2e892SBill Paul 	if (cur_tx != NULL)
226596f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
226696f2e892SBill Paul 
226796f2e892SBill Paul 	return;
226896f2e892SBill Paul }
226996f2e892SBill Paul 
227096f2e892SBill Paul static void dc_tick(xsc)
227196f2e892SBill Paul 	void			*xsc;
227296f2e892SBill Paul {
227396f2e892SBill Paul 	struct dc_softc		*sc;
227496f2e892SBill Paul 	struct mii_data		*mii;
227596f2e892SBill Paul 	struct ifnet		*ifp;
227696f2e892SBill Paul 	int			s;
227796f2e892SBill Paul 	u_int32_t		r;
227896f2e892SBill Paul 
227996f2e892SBill Paul 	s = splimp();
228096f2e892SBill Paul 
228196f2e892SBill Paul 	sc = xsc;
228296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
228396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
228496f2e892SBill Paul 
228596f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
228696f2e892SBill Paul 		r = CSR_READ_4(sc, DC_ISR);
228796f2e892SBill Paul 		if (DC_IS_INTEL(sc)) {
2288d675147eSBill Paul 			if (r & DC_ISR_LINKFAIL)
228996f2e892SBill Paul 				sc->dc_link = 0;
2290d675147eSBill Paul 			if (sc->dc_link == 0)
229196f2e892SBill Paul 				mii_tick(mii);
229296f2e892SBill Paul 		} else {
229396f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
229496f2e892SBill Paul 			    sc->dc_cdata.dc_tx_prod == 0)
229596f2e892SBill Paul 				mii_tick(mii);
229696f2e892SBill Paul 		}
229796f2e892SBill Paul 	} else
229896f2e892SBill Paul 		mii_tick(mii);
229996f2e892SBill Paul 
230096f2e892SBill Paul 	/*
230196f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
230296f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
230396f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
230496f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
230596f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
230696f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
230796f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
230896f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
230996f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
231096f2e892SBill Paul 	 * a screeching halt for several seconds.
231196f2e892SBill Paul 	 *
231296f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
231396f2e892SBill Paul 	 * any packets until a link has been established. After the
231496f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
231596f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
231696f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
231796f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
231896f2e892SBill Paul 	 */
231996f2e892SBill Paul 	if (!sc->dc_link) {
232096f2e892SBill Paul 		mii_pollstat(mii);
232196f2e892SBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
232296f2e892SBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
232396f2e892SBill Paul 			sc->dc_link++;
232496f2e892SBill Paul 			if (ifp->if_snd.ifq_head != NULL)
232596f2e892SBill Paul 				dc_start(ifp);
232696f2e892SBill Paul 		}
232796f2e892SBill Paul 	}
232896f2e892SBill Paul 
232996f2e892SBill Paul 	sc->dc_stat_ch = timeout(dc_tick, sc, hz);
233096f2e892SBill Paul 
233196f2e892SBill Paul 	splx(s);
233296f2e892SBill Paul 
233396f2e892SBill Paul 	return;
233496f2e892SBill Paul }
233596f2e892SBill Paul 
233696f2e892SBill Paul static void dc_intr(arg)
233796f2e892SBill Paul 	void			*arg;
233896f2e892SBill Paul {
233996f2e892SBill Paul 	struct dc_softc		*sc;
234096f2e892SBill Paul 	struct ifnet		*ifp;
234196f2e892SBill Paul 	u_int32_t		status;
234296f2e892SBill Paul 
234396f2e892SBill Paul 	sc = arg;
234496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
234596f2e892SBill Paul 
234696f2e892SBill Paul 	/* Supress unwanted interrupts */
234796f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
234896f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
234996f2e892SBill Paul 			dc_stop(sc);
235096f2e892SBill Paul 		return;
235196f2e892SBill Paul 	}
235296f2e892SBill Paul 
235396f2e892SBill Paul 	/* Disable interrupts. */
235496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
235596f2e892SBill Paul 
235696f2e892SBill Paul 	while((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) {
235796f2e892SBill Paul 
235896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
235996f2e892SBill Paul 
236073bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
236173bf949cSBill Paul 			int		curpkts;
236273bf949cSBill Paul 			curpkts = ifp->if_ipackets;
236396f2e892SBill Paul 			dc_rxeof(sc);
236473bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
236573bf949cSBill Paul 				while(dc_rx_resync(sc))
236673bf949cSBill Paul 					dc_rxeof(sc);
236773bf949cSBill Paul 			}
236873bf949cSBill Paul 		}
236996f2e892SBill Paul 
237096f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
237196f2e892SBill Paul 			dc_txeof(sc);
237296f2e892SBill Paul 
237396f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
237496f2e892SBill Paul 			dc_txeof(sc);
237596f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
237696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
237796f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
237896f2e892SBill Paul 			}
237996f2e892SBill Paul 		}
238096f2e892SBill Paul 
238196f2e892SBill Paul 		if (status & DC_ISR_TX_UNDERRUN) {
238296f2e892SBill Paul 			u_int32_t		cfg;
238396f2e892SBill Paul 
238496f2e892SBill Paul 			printf("dc%d: TX underrun -- ", sc->dc_unit);
238596f2e892SBill Paul 			if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc))
238696f2e892SBill Paul 				dc_init(sc);
238796f2e892SBill Paul 			cfg = CSR_READ_4(sc, DC_NETCFG);
238896f2e892SBill Paul 			cfg &= ~DC_NETCFG_TX_THRESH;
238996f2e892SBill Paul 			if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
239096f2e892SBill Paul 				printf("using store and forward mode\n");
239196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
239291cc2adbSBill Paul 			} else if (sc->dc_flags & DC_TX_STORENFWD) {
239391cc2adbSBill Paul 				printf("resetting\n");
239496f2e892SBill Paul 			} else {
239596f2e892SBill Paul 				sc->dc_txthresh += 0x4000;
239696f2e892SBill Paul 				printf("increasing TX threshold\n");
239796f2e892SBill Paul 				CSR_WRITE_4(sc, DC_NETCFG, cfg);
239896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
239996f2e892SBill Paul 				DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
240096f2e892SBill Paul 			}
240196f2e892SBill Paul 		}
240296f2e892SBill Paul 
240396f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
240473bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
240573bf949cSBill Paul 			int		curpkts;
240673bf949cSBill Paul 			curpkts = ifp->if_ipackets;
240796f2e892SBill Paul 			dc_rxeof(sc);
240873bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
240973bf949cSBill Paul 				while(dc_rx_resync(sc))
241073bf949cSBill Paul 					dc_rxeof(sc);
241173bf949cSBill Paul 			}
241273bf949cSBill Paul 		}
241396f2e892SBill Paul 
241496f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
241596f2e892SBill Paul 			dc_reset(sc);
241696f2e892SBill Paul 			dc_init(sc);
241796f2e892SBill Paul 		}
241896f2e892SBill Paul 	}
241996f2e892SBill Paul 
242096f2e892SBill Paul 	/* Re-enable interrupts. */
242196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
242296f2e892SBill Paul 
242396f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
242496f2e892SBill Paul 		dc_start(ifp);
242596f2e892SBill Paul 
242696f2e892SBill Paul 	return;
242796f2e892SBill Paul }
242896f2e892SBill Paul 
242996f2e892SBill Paul /*
243096f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
243196f2e892SBill Paul  * pointers to the fragment pointers.
243296f2e892SBill Paul  */
243396f2e892SBill Paul static int dc_encap(sc, m_head, txidx)
243496f2e892SBill Paul 	struct dc_softc		*sc;
243596f2e892SBill Paul 	struct mbuf		*m_head;
243696f2e892SBill Paul 	u_int32_t		*txidx;
243796f2e892SBill Paul {
243896f2e892SBill Paul 	struct dc_desc		*f = NULL;
243996f2e892SBill Paul 	struct mbuf		*m;
244096f2e892SBill Paul 	int			frag, cur, cnt = 0;
244196f2e892SBill Paul 
244296f2e892SBill Paul 	/*
244396f2e892SBill Paul  	 * Start packing the mbufs in this chain into
244496f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
244596f2e892SBill Paul  	 * of fragments or hit the end of the mbuf chain.
244696f2e892SBill Paul 	 */
244796f2e892SBill Paul 	m = m_head;
244896f2e892SBill Paul 	cur = frag = *txidx;
244996f2e892SBill Paul 
245096f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
245196f2e892SBill Paul 		if (m->m_len != 0) {
245296f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
245396f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
245496f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
245596f2e892SBill Paul 					return(ENOBUFS);
245696f2e892SBill Paul 			}
245796f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
245896f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
245996f2e892SBill Paul 				return(ENOBUFS);
246096f2e892SBill Paul 
246196f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
246296f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
246396f2e892SBill Paul 			if (cnt == 0) {
246496f2e892SBill Paul 				f->dc_status = 0;
246596f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
246696f2e892SBill Paul 			} else
246796f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
246896f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
246996f2e892SBill Paul 			cur = frag;
247096f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
247196f2e892SBill Paul 			cnt++;
247296f2e892SBill Paul 		}
247396f2e892SBill Paul 	}
247496f2e892SBill Paul 
247596f2e892SBill Paul 	if (m != NULL)
247696f2e892SBill Paul 		return(ENOBUFS);
247796f2e892SBill Paul 
247896f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
247996f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
248096f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
248196f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
248296f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
248391cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
248491cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
248596f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
248696f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
248796f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
248896f2e892SBill Paul 	*txidx = frag;
248996f2e892SBill Paul 
249096f2e892SBill Paul 	return(0);
249196f2e892SBill Paul }
249296f2e892SBill Paul 
249396f2e892SBill Paul /*
2494fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
2495fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
2496fda39fd0SBill Paul  * do scatter/gather correctly.
2497fda39fd0SBill Paul  */
2498fda39fd0SBill Paul static int dc_coal(sc, m_head)
2499fda39fd0SBill Paul 	struct dc_softc		*sc;
2500fda39fd0SBill Paul 	struct mbuf		**m_head;
2501fda39fd0SBill Paul {
2502fda39fd0SBill Paul         struct mbuf		*m_new, *m;
2503fda39fd0SBill Paul 
2504fda39fd0SBill Paul 	m = *m_head;
2505fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
2506fda39fd0SBill Paul 	if (m_new == NULL) {
2507fda39fd0SBill Paul 		printf("dc%d: no memory for tx list", sc->dc_unit);
2508fda39fd0SBill Paul 		return(ENOBUFS);
2509fda39fd0SBill Paul 	}
2510fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
2511fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
2512fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
2513fda39fd0SBill Paul 			m_freem(m_new);
2514fda39fd0SBill Paul 			printf("dc%d: no memory for tx list", sc->dc_unit);
2515fda39fd0SBill Paul 			return(ENOBUFS);
2516fda39fd0SBill Paul 		}
2517fda39fd0SBill Paul 	}
2518fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
2519fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
2520fda39fd0SBill Paul 	m_freem(m);
2521fda39fd0SBill Paul 	*m_head = m_new;
2522fda39fd0SBill Paul 
2523fda39fd0SBill Paul 	return(0);
2524fda39fd0SBill Paul }
2525fda39fd0SBill Paul 
2526fda39fd0SBill Paul /*
252796f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
252896f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
252996f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
253096f2e892SBill Paul  * physical addresses.
253196f2e892SBill Paul  */
253296f2e892SBill Paul 
253396f2e892SBill Paul static void dc_start(ifp)
253496f2e892SBill Paul 	struct ifnet		*ifp;
253596f2e892SBill Paul {
253696f2e892SBill Paul 	struct dc_softc		*sc;
253796f2e892SBill Paul 	struct mbuf		*m_head = NULL;
253896f2e892SBill Paul 	int			idx;
253996f2e892SBill Paul 
254096f2e892SBill Paul 	sc = ifp->if_softc;
254196f2e892SBill Paul 
254296f2e892SBill Paul 	if (!sc->dc_link)
254396f2e892SBill Paul 		return;
254496f2e892SBill Paul 
254596f2e892SBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
254696f2e892SBill Paul 		return;
254796f2e892SBill Paul 
254896f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
254996f2e892SBill Paul 
255096f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
255196f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
255296f2e892SBill Paul 		if (m_head == NULL)
255396f2e892SBill Paul 			break;
255496f2e892SBill Paul 
2555fda39fd0SBill Paul 		if (sc->dc_flags & DC_TX_COALESCE) {
2556fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
2557fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
2558fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
2559fda39fd0SBill Paul 				break;
2560fda39fd0SBill Paul 			}
2561fda39fd0SBill Paul 		}
2562fda39fd0SBill Paul 
256396f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
256496f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
256596f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
256696f2e892SBill Paul 			break;
256796f2e892SBill Paul 		}
256896f2e892SBill Paul 
256996f2e892SBill Paul 		/*
257096f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
257196f2e892SBill Paul 		 * to him.
257296f2e892SBill Paul 		 */
257396f2e892SBill Paul 		if (ifp->if_bpf)
257496f2e892SBill Paul 			bpf_mtap(ifp, m_head);
257596f2e892SBill Paul 	}
257696f2e892SBill Paul 
257796f2e892SBill Paul 	/* Transmit */
257896f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
257996f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
258096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
258196f2e892SBill Paul 
258296f2e892SBill Paul 	/*
258396f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
258496f2e892SBill Paul 	 */
258596f2e892SBill Paul 	ifp->if_timer = 5;
258696f2e892SBill Paul 
258796f2e892SBill Paul 	return;
258896f2e892SBill Paul }
258996f2e892SBill Paul 
259096f2e892SBill Paul static void dc_init(xsc)
259196f2e892SBill Paul 	void			*xsc;
259296f2e892SBill Paul {
259396f2e892SBill Paul 	struct dc_softc		*sc = xsc;
259496f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
259596f2e892SBill Paul 	struct mii_data		*mii;
259696f2e892SBill Paul 	int			s;
259796f2e892SBill Paul 
259896f2e892SBill Paul 	s = splimp();
259996f2e892SBill Paul 
260096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
260196f2e892SBill Paul 
260296f2e892SBill Paul 	/*
260396f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
260496f2e892SBill Paul 	 */
260596f2e892SBill Paul 	dc_stop(sc);
260696f2e892SBill Paul 	dc_reset(sc);
260796f2e892SBill Paul 
260896f2e892SBill Paul 	/*
260996f2e892SBill Paul 	 * Set cache alignment and burst length.
261096f2e892SBill Paul 	 */
261188d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
261296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
261396f2e892SBill Paul 	else
261496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
261596f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
261696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
261796f2e892SBill Paul 	} else {
261896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
261996f2e892SBill Paul 	}
262096f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
262196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
262296f2e892SBill Paul 	switch(sc->dc_cachesize) {
262396f2e892SBill Paul 	case 32:
262496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
262596f2e892SBill Paul 		break;
262696f2e892SBill Paul 	case 16:
262796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
262896f2e892SBill Paul 		break;
262996f2e892SBill Paul 	case 8:
263096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
263196f2e892SBill Paul 		break;
263296f2e892SBill Paul 	case 0:
263396f2e892SBill Paul 	default:
263496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
263596f2e892SBill Paul 		break;
263696f2e892SBill Paul 	}
263796f2e892SBill Paul 
263896f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
263996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
264096f2e892SBill Paul 	else {
264196f2e892SBill Paul 		if (sc->dc_txthresh == DC_TXTHRESH_160BYTES) {
264296f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
264396f2e892SBill Paul 		} else {
264496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
264596f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
264696f2e892SBill Paul 		}
264796f2e892SBill Paul 	}
264896f2e892SBill Paul 
264996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
265096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
265196f2e892SBill Paul 
265296f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
265396f2e892SBill Paul 		/*
265496f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
265596f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
265696f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
265796f2e892SBill Paul 		 * document the meaning of these bits so there's no way
265896f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
265996f2e892SBill Paul 		 * number all its own; the rest all use a different one.
266096f2e892SBill Paul 		 */
266196f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
266296f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
266396f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
266496f2e892SBill Paul 		else
266596f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
266696f2e892SBill Paul 	}
266796f2e892SBill Paul 
266896f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
266996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_72BYTES);
267096f2e892SBill Paul 
267196f2e892SBill Paul 	/* Init circular RX list. */
267296f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
267396f2e892SBill Paul 		printf("dc%d: initialization failed: no "
267496f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
267596f2e892SBill Paul 		dc_stop(sc);
267696f2e892SBill Paul 		(void)splx(s);
267796f2e892SBill Paul 		return;
267896f2e892SBill Paul 	}
267996f2e892SBill Paul 
268096f2e892SBill Paul 	/*
268196f2e892SBill Paul 	 * Init tx descriptors.
268296f2e892SBill Paul 	 */
268396f2e892SBill Paul 	dc_list_tx_init(sc);
268496f2e892SBill Paul 
268596f2e892SBill Paul 	/*
268696f2e892SBill Paul 	 * Load the address of the RX list.
268796f2e892SBill Paul 	 */
268896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
268996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
269096f2e892SBill Paul 
269196f2e892SBill Paul 	/*
269296f2e892SBill Paul 	 * Enable interrupts.
269396f2e892SBill Paul 	 */
269496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
269596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
269696f2e892SBill Paul 
269796f2e892SBill Paul 	/* Enable transmitter. */
269896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
269996f2e892SBill Paul 
270096f2e892SBill Paul 	/*
270196f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
270296f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
270396f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
270496f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
270596f2e892SBill Paul 	 */
270696f2e892SBill Paul 	dc_setfilt(sc);
270796f2e892SBill Paul 
270896f2e892SBill Paul 	/* Enable receiver. */
270996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
271096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
271196f2e892SBill Paul 
271296f2e892SBill Paul 	mii_mediachg(mii);
271396f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
271496f2e892SBill Paul 
271596f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
271696f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
271796f2e892SBill Paul 
271896f2e892SBill Paul 	(void)splx(s);
271996f2e892SBill Paul 
272096f2e892SBill Paul 	sc->dc_stat_ch = timeout(dc_tick, sc, hz);
272196f2e892SBill Paul 
2722510a809eSMike Smith #ifdef __alpha__
2723510a809eSMike Smith         if(sc->dc_srm_media) {
2724510a809eSMike Smith 		struct ifreq ifr;
2725510a809eSMike Smith 
2726510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
2727510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
2728510a809eSMike Smith 		sc->dc_srm_media = 0;
2729510a809eSMike Smith 	}
2730510a809eSMike Smith #endif
273196f2e892SBill Paul 	return;
273296f2e892SBill Paul }
273396f2e892SBill Paul 
273496f2e892SBill Paul /*
273596f2e892SBill Paul  * Set media options.
273696f2e892SBill Paul  */
273796f2e892SBill Paul static int dc_ifmedia_upd(ifp)
273896f2e892SBill Paul 	struct ifnet		*ifp;
273996f2e892SBill Paul {
274096f2e892SBill Paul 	struct dc_softc		*sc;
274196f2e892SBill Paul 	struct mii_data		*mii;
2742f43d9309SBill Paul 	struct ifmedia		*ifm;
274396f2e892SBill Paul 
274496f2e892SBill Paul 	sc = ifp->if_softc;
274596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
274696f2e892SBill Paul 	mii_mediachg(mii);
2747f43d9309SBill Paul 	ifm = &mii->mii_media;
2748f43d9309SBill Paul 
2749f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
2750f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA)
2751f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
2752f43d9309SBill Paul 	else
275396f2e892SBill Paul 		sc->dc_link = 0;
275496f2e892SBill Paul 
275596f2e892SBill Paul 	return(0);
275696f2e892SBill Paul }
275796f2e892SBill Paul 
275896f2e892SBill Paul /*
275996f2e892SBill Paul  * Report current media status.
276096f2e892SBill Paul  */
276196f2e892SBill Paul static void dc_ifmedia_sts(ifp, ifmr)
276296f2e892SBill Paul 	struct ifnet		*ifp;
276396f2e892SBill Paul 	struct ifmediareq	*ifmr;
276496f2e892SBill Paul {
276596f2e892SBill Paul 	struct dc_softc		*sc;
276696f2e892SBill Paul 	struct mii_data		*mii;
2767f43d9309SBill Paul 	struct ifmedia		*ifm;
276896f2e892SBill Paul 
276996f2e892SBill Paul 	sc = ifp->if_softc;
277096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
277196f2e892SBill Paul 	mii_pollstat(mii);
2772f43d9309SBill Paul 	ifm = &mii->mii_media;
2773f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
2774f43d9309SBill Paul 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
2775f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
2776f43d9309SBill Paul 			ifmr->ifm_status = 0;
2777f43d9309SBill Paul 			return;
2778f43d9309SBill Paul 		}
2779f43d9309SBill Paul 	}
278096f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
278196f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
278296f2e892SBill Paul 
278396f2e892SBill Paul 	return;
278496f2e892SBill Paul }
278596f2e892SBill Paul 
278696f2e892SBill Paul static int dc_ioctl(ifp, command, data)
278796f2e892SBill Paul 	struct ifnet		*ifp;
278896f2e892SBill Paul 	u_long			command;
278996f2e892SBill Paul 	caddr_t			data;
279096f2e892SBill Paul {
279196f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
279296f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
279396f2e892SBill Paul 	struct mii_data		*mii;
279496f2e892SBill Paul 	int			s, error = 0;
279596f2e892SBill Paul 
279696f2e892SBill Paul 	s = splimp();
279796f2e892SBill Paul 
279896f2e892SBill Paul 	switch(command) {
279996f2e892SBill Paul 	case SIOCSIFADDR:
280096f2e892SBill Paul 	case SIOCGIFADDR:
280196f2e892SBill Paul 	case SIOCSIFMTU:
280296f2e892SBill Paul 		error = ether_ioctl(ifp, command, data);
280396f2e892SBill Paul 		break;
280496f2e892SBill Paul 	case SIOCSIFFLAGS:
280596f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
280696f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
280796f2e892SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
280896f2e892SBill Paul 			    !(sc->dc_if_flags & IFF_PROMISC)) {
280996f2e892SBill Paul 				dc_setfilt(sc);
281096f2e892SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
281196f2e892SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
281296f2e892SBill Paul 			    sc->dc_if_flags & IFF_PROMISC) {
281396f2e892SBill Paul 				dc_setfilt(sc);
281496f2e892SBill Paul 			} else if (!(ifp->if_flags & IFF_RUNNING)) {
281596f2e892SBill Paul 				sc->dc_txthresh = 0;
281696f2e892SBill Paul 				dc_init(sc);
281796f2e892SBill Paul 			}
281896f2e892SBill Paul 		} else {
281996f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
282096f2e892SBill Paul 				dc_stop(sc);
282196f2e892SBill Paul 		}
282296f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
282396f2e892SBill Paul 		error = 0;
282496f2e892SBill Paul 		break;
282596f2e892SBill Paul 	case SIOCADDMULTI:
282696f2e892SBill Paul 	case SIOCDELMULTI:
282796f2e892SBill Paul 		dc_setfilt(sc);
282896f2e892SBill Paul 		error = 0;
282996f2e892SBill Paul 		break;
283096f2e892SBill Paul 	case SIOCGIFMEDIA:
283196f2e892SBill Paul 	case SIOCSIFMEDIA:
283296f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
283396f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2834510a809eSMike Smith #ifdef __alpha__
2835510a809eSMike Smith 		if (sc->dc_srm_media)
2836510a809eSMike Smith 			sc->dc_srm_media = 0;
2837510a809eSMike Smith #endif
283896f2e892SBill Paul 		break;
283996f2e892SBill Paul 	default:
284096f2e892SBill Paul 		error = EINVAL;
284196f2e892SBill Paul 		break;
284296f2e892SBill Paul 	}
284396f2e892SBill Paul 
284496f2e892SBill Paul 	(void)splx(s);
284596f2e892SBill Paul 
284696f2e892SBill Paul 	return(error);
284796f2e892SBill Paul }
284896f2e892SBill Paul 
284996f2e892SBill Paul static void dc_watchdog(ifp)
285096f2e892SBill Paul 	struct ifnet		*ifp;
285196f2e892SBill Paul {
285296f2e892SBill Paul 	struct dc_softc		*sc;
285396f2e892SBill Paul 
285496f2e892SBill Paul 	sc = ifp->if_softc;
285596f2e892SBill Paul 
285696f2e892SBill Paul 	ifp->if_oerrors++;
285796f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
285896f2e892SBill Paul 
285996f2e892SBill Paul 	dc_stop(sc);
286096f2e892SBill Paul 	dc_reset(sc);
286196f2e892SBill Paul 	dc_init(sc);
286296f2e892SBill Paul 
286396f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
286496f2e892SBill Paul 		dc_start(ifp);
286596f2e892SBill Paul 
286696f2e892SBill Paul 	return;
286796f2e892SBill Paul }
286896f2e892SBill Paul 
286996f2e892SBill Paul /*
287096f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
287196f2e892SBill Paul  * RX and TX lists.
287296f2e892SBill Paul  */
287396f2e892SBill Paul static void dc_stop(sc)
287496f2e892SBill Paul 	struct dc_softc		*sc;
287596f2e892SBill Paul {
287696f2e892SBill Paul 	register int		i;
287796f2e892SBill Paul 	struct ifnet		*ifp;
287896f2e892SBill Paul 
287996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
288096f2e892SBill Paul 	ifp->if_timer = 0;
288196f2e892SBill Paul 
288296f2e892SBill Paul 	untimeout(dc_tick, sc, sc->dc_stat_ch);
288396f2e892SBill Paul 
288496f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
288596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
288696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
288796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
288896f2e892SBill Paul 	sc->dc_link = 0;
288996f2e892SBill Paul 
289096f2e892SBill Paul 	/*
289196f2e892SBill Paul 	 * Free data in the RX lists.
289296f2e892SBill Paul 	 */
289396f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
289496f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
289596f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
289696f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
289796f2e892SBill Paul 		}
289896f2e892SBill Paul 	}
289996f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
290096f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
290196f2e892SBill Paul 
290296f2e892SBill Paul 	/*
290396f2e892SBill Paul 	 * Free the TX list buffers.
290496f2e892SBill Paul 	 */
290596f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
290696f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
290796f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
290896f2e892SBill Paul 			    DC_TXCTL_SETUP) {
290996f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
291096f2e892SBill Paul 				continue;
291196f2e892SBill Paul 			}
291296f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
291396f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
291496f2e892SBill Paul 		}
291596f2e892SBill Paul 	}
291696f2e892SBill Paul 
291796f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
291896f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
291996f2e892SBill Paul 
292096f2e892SBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
292196f2e892SBill Paul 
292296f2e892SBill Paul 	return;
292396f2e892SBill Paul }
292496f2e892SBill Paul 
292596f2e892SBill Paul /*
292696f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
292796f2e892SBill Paul  * get confused by errant DMAs when rebooting.
292896f2e892SBill Paul  */
292996f2e892SBill Paul static void dc_shutdown(dev)
293096f2e892SBill Paul 	device_t		dev;
293196f2e892SBill Paul {
293296f2e892SBill Paul 	struct dc_softc		*sc;
293396f2e892SBill Paul 
293496f2e892SBill Paul 	sc = device_get_softc(dev);
293596f2e892SBill Paul 
293696f2e892SBill Paul 	dc_stop(sc);
293796f2e892SBill Paul 
293896f2e892SBill Paul 	return;
293996f2e892SBill Paul }
2940