xref: /freebsd/sys/dev/dc/if_dc.c (revision db40c1aef4eb34d7b0799285feb74211bb332217)
196f2e892SBill Paul /*
296f2e892SBill Paul  * Copyright (c) 1997, 1998, 1999
396f2e892SBill Paul  *	Bill Paul <wpaul@ee.columbia.edu>.  All rights reserved.
496f2e892SBill Paul  *
596f2e892SBill Paul  * Redistribution and use in source and binary forms, with or without
696f2e892SBill Paul  * modification, are permitted provided that the following conditions
796f2e892SBill Paul  * are met:
896f2e892SBill Paul  * 1. Redistributions of source code must retain the above copyright
996f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer.
1096f2e892SBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
1196f2e892SBill Paul  *    notice, this list of conditions and the following disclaimer in the
1296f2e892SBill Paul  *    documentation and/or other materials provided with the distribution.
1396f2e892SBill Paul  * 3. All advertising materials mentioning features or use of this software
1496f2e892SBill Paul  *    must display the following acknowledgement:
1596f2e892SBill Paul  *	This product includes software developed by Bill Paul.
1696f2e892SBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
1796f2e892SBill Paul  *    may be used to endorse or promote products derived from this software
1896f2e892SBill Paul  *    without specific prior written permission.
1996f2e892SBill Paul  *
2096f2e892SBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2196f2e892SBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2296f2e892SBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2396f2e892SBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2496f2e892SBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2596f2e892SBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2696f2e892SBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2796f2e892SBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2896f2e892SBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2996f2e892SBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3096f2e892SBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
3196f2e892SBill Paul  *
3296f2e892SBill Paul  * $FreeBSD$
3396f2e892SBill Paul  */
3496f2e892SBill Paul 
3596f2e892SBill Paul /*
3696f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
3796f2e892SBill Paul  * series chips and several workalikes including the following:
3896f2e892SBill Paul  *
39ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
4096f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
4196f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4296f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4396f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4496f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
4596f2e892SBill Paul  * ADMtek AN985 (www.admtek.com.tw)
4688d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
479ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
48feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
491d5e5310SBill Paul  * Abocom FE2500
501af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
5196f2e892SBill Paul  *
5296f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5396f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5496f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5596f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5696f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
5796f2e892SBill Paul  * instead of 512.
5896f2e892SBill Paul  *
5996f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6096f2e892SBill Paul  * Electrical Engineering Department
6196f2e892SBill Paul  * Columbia University, New York City
6296f2e892SBill Paul  */
6396f2e892SBill Paul 
6496f2e892SBill Paul /*
6596f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6696f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6796f2e892SBill Paul  * three kinds of media attachments:
6896f2e892SBill Paul  *
6996f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7096f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7196f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7296f2e892SBill Paul  * o 10baseT port.
7396f2e892SBill Paul  * o AUI/BNC port.
7496f2e892SBill Paul  *
7596f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7696f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7796f2e892SBill Paul  * autosensing configuration.
7896f2e892SBill Paul  *
7996f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8096f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8196f2e892SBill Paul  * handled separately due to its different register offsets and the
8296f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8396f2e892SBill Paul  * here, but I'm not thrilled about it.
8496f2e892SBill Paul  *
8596f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8696f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8796f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
8896f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
8996f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9096f2e892SBill Paul  */
9196f2e892SBill Paul 
9296f2e892SBill Paul #include <sys/param.h>
9396f2e892SBill Paul #include <sys/systm.h>
9496f2e892SBill Paul #include <sys/sockio.h>
9596f2e892SBill Paul #include <sys/mbuf.h>
9696f2e892SBill Paul #include <sys/malloc.h>
9796f2e892SBill Paul #include <sys/kernel.h>
9896f2e892SBill Paul #include <sys/socket.h>
9901faf54bSLuigi Rizzo #include <sys/sysctl.h>
10096f2e892SBill Paul 
10196f2e892SBill Paul #include <net/if.h>
10296f2e892SBill Paul #include <net/if_arp.h>
10396f2e892SBill Paul #include <net/ethernet.h>
10496f2e892SBill Paul #include <net/if_dl.h>
10596f2e892SBill Paul #include <net/if_media.h>
106db40c1aeSDoug Ambrisko #include <net/if_types.h>
107db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
10896f2e892SBill Paul 
10996f2e892SBill Paul #include <net/bpf.h>
11096f2e892SBill Paul 
11196f2e892SBill Paul #include <vm/vm.h>              /* for vtophys */
11296f2e892SBill Paul #include <vm/pmap.h>            /* for vtophys */
11396f2e892SBill Paul #include <machine/bus_pio.h>
11496f2e892SBill Paul #include <machine/bus_memio.h>
11596f2e892SBill Paul #include <machine/bus.h>
11696f2e892SBill Paul #include <machine/resource.h>
11796f2e892SBill Paul #include <sys/bus.h>
11896f2e892SBill Paul #include <sys/rman.h>
11996f2e892SBill Paul 
12096f2e892SBill Paul #include <dev/mii/mii.h>
12196f2e892SBill Paul #include <dev/mii/miivar.h>
12296f2e892SBill Paul 
12396f2e892SBill Paul #include <pci/pcireg.h>
12496f2e892SBill Paul #include <pci/pcivar.h>
12596f2e892SBill Paul 
12696f2e892SBill Paul #define DC_USEIOSPACE
1275c1cfac4SBill Paul #ifdef __alpha__
1285c1cfac4SBill Paul #define SRM_MEDIA
1295c1cfac4SBill Paul #endif
13096f2e892SBill Paul 
13196f2e892SBill Paul #include <pci/if_dcreg.h>
13296f2e892SBill Paul 
13395a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
13495a16455SPeter Wemm 
13596f2e892SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
13696f2e892SBill Paul #include "miibus_if.h"
13796f2e892SBill Paul 
13896f2e892SBill Paul #ifndef lint
13996f2e892SBill Paul static const char rcsid[] =
14096f2e892SBill Paul   "$FreeBSD$";
14196f2e892SBill Paul #endif
14296f2e892SBill Paul 
14396f2e892SBill Paul /*
14496f2e892SBill Paul  * Various supported device vendors/types and their names.
14596f2e892SBill Paul  */
14696f2e892SBill Paul static struct dc_type dc_devs[] = {
14796f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
14896f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
14996f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
15096f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
15196f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15296f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
15388d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15488d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
15596f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
15696f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
15796f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
15896f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
15996f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16096f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
16196f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16296f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
16396f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16496f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
16596f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16696f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
16796f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
16896f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
16996f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
17096f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17196f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17296f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
17396f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17479d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
17579d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17696f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
177ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
178ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
17996f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
18096f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
18196f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18296f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
18396f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18496f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1859ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1869ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
187fa167b8eSBill Paul 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN2242,
188fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
189feb78939SJonathan Chen     	{ DC_VENDORID_XIRCOM, DC_DEVICEID_X3201,
190feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
1911d5e5310SBill Paul 	{ DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500,
1921d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
1931af8bec7SBill Paul 	{ DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112,
1941af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
19596f2e892SBill Paul 	{ 0, 0, NULL }
19696f2e892SBill Paul };
19796f2e892SBill Paul 
19896f2e892SBill Paul static int dc_probe		__P((device_t));
19996f2e892SBill Paul static int dc_attach		__P((device_t));
20096f2e892SBill Paul static int dc_detach		__P((device_t));
20196f2e892SBill Paul static void dc_acpi		__P((device_t));
20296f2e892SBill Paul static struct dc_type *dc_devtype	__P((device_t));
20396f2e892SBill Paul static int dc_newbuf		__P((struct dc_softc *, int, struct mbuf *));
20496f2e892SBill Paul static int dc_encap		__P((struct dc_softc *, struct mbuf *,
20596f2e892SBill Paul 					u_int32_t *));
206fda39fd0SBill Paul static int dc_coal		__P((struct dc_softc *, struct mbuf **));
20796f2e892SBill Paul static void dc_pnic_rx_bug_war	__P((struct dc_softc *, int));
20873bf949cSBill Paul static int dc_rx_resync		__P((struct dc_softc *));
20996f2e892SBill Paul static void dc_rxeof		__P((struct dc_softc *));
21096f2e892SBill Paul static void dc_txeof		__P((struct dc_softc *));
21196f2e892SBill Paul static void dc_tick		__P((void *));
212d467c136SBill Paul static void dc_tx_underrun	__P((struct dc_softc *));
21396f2e892SBill Paul static void dc_intr		__P((void *));
21496f2e892SBill Paul static void dc_start		__P((struct ifnet *));
21596f2e892SBill Paul static int dc_ioctl		__P((struct ifnet *, u_long, caddr_t));
21696f2e892SBill Paul static void dc_init		__P((void *));
21796f2e892SBill Paul static void dc_stop		__P((struct dc_softc *));
21896f2e892SBill Paul static void dc_watchdog		__P((struct ifnet *));
21996f2e892SBill Paul static void dc_shutdown		__P((device_t));
22096f2e892SBill Paul static int dc_ifmedia_upd	__P((struct ifnet *));
22196f2e892SBill Paul static void dc_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
22296f2e892SBill Paul 
22396f2e892SBill Paul static void dc_delay		__P((struct dc_softc *));
22496f2e892SBill Paul static void dc_eeprom_idle	__P((struct dc_softc *));
22596f2e892SBill Paul static void dc_eeprom_putbyte	__P((struct dc_softc *, int));
22696f2e892SBill Paul static void dc_eeprom_getword	__P((struct dc_softc *, int, u_int16_t *));
22796f2e892SBill Paul static void dc_eeprom_getword_pnic
22896f2e892SBill Paul 				__P((struct dc_softc *, int, u_int16_t *));
229feb78939SJonathan Chen static void dc_eeprom_getword_xircom
230feb78939SJonathan Chen 				__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 *));
251feb78939SJonathan Chen static void dc_setfilt_xircom	__P((struct dc_softc *));
25296f2e892SBill Paul 
25396f2e892SBill Paul static void dc_setfilt		__P((struct dc_softc *));
25496f2e892SBill Paul 
25596f2e892SBill Paul static void dc_reset		__P((struct dc_softc *));
25696f2e892SBill Paul static int dc_list_rx_init	__P((struct dc_softc *));
25796f2e892SBill Paul static int dc_list_tx_init	__P((struct dc_softc *));
25896f2e892SBill Paul 
2595c1cfac4SBill Paul static void dc_parse_21143_srom	__P((struct dc_softc *));
2605c1cfac4SBill Paul static void dc_decode_leaf_sia	__P((struct dc_softc *,
2615c1cfac4SBill Paul 				    struct dc_eblock_sia *));
2625c1cfac4SBill Paul static void dc_decode_leaf_mii	__P((struct dc_softc *,
2635c1cfac4SBill Paul 				    struct dc_eblock_mii *));
2645c1cfac4SBill Paul static void dc_decode_leaf_sym	__P((struct dc_softc *,
2655c1cfac4SBill Paul 				    struct dc_eblock_sym *));
2665c1cfac4SBill Paul static void dc_apply_fixup	__P((struct dc_softc *, int));
2675c1cfac4SBill Paul 
26896f2e892SBill Paul #ifdef DC_USEIOSPACE
26996f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
27096f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
27196f2e892SBill Paul #else
27296f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
27396f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
27496f2e892SBill Paul #endif
27596f2e892SBill Paul 
27696f2e892SBill Paul static device_method_t dc_methods[] = {
27796f2e892SBill Paul 	/* Device interface */
27896f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
27996f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
28096f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
28196f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
28296f2e892SBill Paul 
28396f2e892SBill Paul 	/* bus interface */
28496f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
28596f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
28696f2e892SBill Paul 
28796f2e892SBill Paul 	/* MII interface */
28896f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
28996f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
29096f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
291f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
29296f2e892SBill Paul 
29396f2e892SBill Paul 	{ 0, 0 }
29496f2e892SBill Paul };
29596f2e892SBill Paul 
29696f2e892SBill Paul static driver_t dc_driver = {
29796f2e892SBill Paul 	"dc",
29896f2e892SBill Paul 	dc_methods,
29996f2e892SBill Paul 	sizeof(struct dc_softc)
30096f2e892SBill Paul };
30196f2e892SBill Paul 
30296f2e892SBill Paul static devclass_t dc_devclass;
30301faf54bSLuigi Rizzo #ifdef __i386__
30401faf54bSLuigi Rizzo static int dc_quick=1;
30501faf54bSLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW,
30601faf54bSLuigi Rizzo 	&dc_quick,0,"do not mdevget in dc driver");
30701faf54bSLuigi Rizzo #endif
30896f2e892SBill Paul 
309feb78939SJonathan Chen DRIVER_MODULE(if_dc, cardbus, dc_driver, dc_devclass, 0, 0);
31096f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
31196f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
31296f2e892SBill Paul 
31396f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
31496f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
31596f2e892SBill Paul 
31696f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
31796f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
31896f2e892SBill Paul 
31996f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
32096f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
32196f2e892SBill Paul 
322b50c6312SJonathan Lemon #define IS_MPSAFE 	0
323b50c6312SJonathan Lemon 
32496f2e892SBill Paul static void dc_delay(sc)
32596f2e892SBill Paul 	struct dc_softc		*sc;
32696f2e892SBill Paul {
32796f2e892SBill Paul 	int			idx;
32896f2e892SBill Paul 
32996f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
33096f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
33196f2e892SBill Paul }
33296f2e892SBill Paul 
33396f2e892SBill Paul static void dc_eeprom_idle(sc)
33496f2e892SBill Paul 	struct dc_softc		*sc;
33596f2e892SBill Paul {
33696f2e892SBill Paul 	register int		i;
33796f2e892SBill Paul 
33896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
33996f2e892SBill Paul 	dc_delay(sc);
34096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
34196f2e892SBill Paul 	dc_delay(sc);
34296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
34396f2e892SBill Paul 	dc_delay(sc);
34496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
34596f2e892SBill Paul 	dc_delay(sc);
34696f2e892SBill Paul 
34796f2e892SBill Paul 	for (i = 0; i < 25; i++) {
34896f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
34996f2e892SBill Paul 		dc_delay(sc);
35096f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
35196f2e892SBill Paul 		dc_delay(sc);
35296f2e892SBill Paul 	}
35396f2e892SBill Paul 
35496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
35596f2e892SBill Paul 	dc_delay(sc);
35696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
35796f2e892SBill Paul 	dc_delay(sc);
35896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
35996f2e892SBill Paul 
36096f2e892SBill Paul 	return;
36196f2e892SBill Paul }
36296f2e892SBill Paul 
36396f2e892SBill Paul /*
36496f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
36596f2e892SBill Paul  */
36696f2e892SBill Paul static void dc_eeprom_putbyte(sc, addr)
36796f2e892SBill Paul 	struct dc_softc		*sc;
36896f2e892SBill Paul 	int			addr;
36996f2e892SBill Paul {
37096f2e892SBill Paul 	register int		d, i;
37196f2e892SBill Paul 
37296f2e892SBill Paul 	/*
37396f2e892SBill Paul 	 * The AN985 has a 93C66 EEPROM on it instead of
37496f2e892SBill Paul 	 * a 93C46. It uses a different bit sequence for
37596f2e892SBill Paul 	 * specifying the "read" opcode.
37696f2e892SBill Paul 	 */
3771af8bec7SBill Paul 	if (DC_IS_CENTAUR(sc) || DC_IS_CONEXANT(sc))
37896f2e892SBill Paul 		d = addr | (DC_EECMD_READ << 2);
37996f2e892SBill Paul 	else
38096f2e892SBill Paul 		d = addr | DC_EECMD_READ;
38196f2e892SBill Paul 
38296f2e892SBill Paul 	/*
38396f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
38496f2e892SBill Paul 	 */
38596f2e892SBill Paul 	for (i = 0x400; i; i >>= 1) {
38696f2e892SBill Paul 		if (d & i) {
38796f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
38896f2e892SBill Paul 		} else {
38996f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
39096f2e892SBill Paul 		}
39196f2e892SBill Paul 		dc_delay(sc);
39296f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
39396f2e892SBill Paul 		dc_delay(sc);
39496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
39596f2e892SBill Paul 		dc_delay(sc);
39696f2e892SBill Paul 	}
39796f2e892SBill Paul 
39896f2e892SBill Paul 	return;
39996f2e892SBill Paul }
40096f2e892SBill Paul 
40196f2e892SBill Paul /*
40296f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
40396f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
40496f2e892SBill Paul  * the EEPROM.
40596f2e892SBill Paul  */
40696f2e892SBill Paul static void dc_eeprom_getword_pnic(sc, addr, dest)
40796f2e892SBill Paul 	struct dc_softc		*sc;
40896f2e892SBill Paul 	int			addr;
40996f2e892SBill Paul 	u_int16_t		*dest;
41096f2e892SBill Paul {
41196f2e892SBill Paul 	register int		i;
41296f2e892SBill Paul 	u_int32_t		r;
41396f2e892SBill Paul 
41496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
41596f2e892SBill Paul 
41696f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
41796f2e892SBill Paul 		DELAY(1);
41896f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
41996f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
42096f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
42196f2e892SBill Paul 			return;
42296f2e892SBill Paul 		}
42396f2e892SBill Paul 	}
42496f2e892SBill Paul 
42596f2e892SBill Paul 	return;
42696f2e892SBill Paul }
42796f2e892SBill Paul 
42896f2e892SBill Paul /*
42996f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
430feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
431feb78939SJonathan Chen  * the EEPROM, too.
432feb78939SJonathan Chen  */
433feb78939SJonathan Chen static void dc_eeprom_getword_xircom(sc, addr, dest)
434feb78939SJonathan Chen 	struct dc_softc		*sc;
435feb78939SJonathan Chen 	int			addr;
436feb78939SJonathan Chen 	u_int16_t		*dest;
437feb78939SJonathan Chen {
438feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
439feb78939SJonathan Chen 
440feb78939SJonathan Chen 	addr *= 2;
441feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
442feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff;
443feb78939SJonathan Chen 	addr += 1;
444feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
445feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff) << 8;
446feb78939SJonathan Chen 
447feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
448feb78939SJonathan Chen 	return;
449feb78939SJonathan Chen }
450feb78939SJonathan Chen 
451feb78939SJonathan Chen /*
452feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
45396f2e892SBill Paul  */
45496f2e892SBill Paul static void dc_eeprom_getword(sc, addr, dest)
45596f2e892SBill Paul 	struct dc_softc		*sc;
45696f2e892SBill Paul 	int			addr;
45796f2e892SBill Paul 	u_int16_t		*dest;
45896f2e892SBill Paul {
45996f2e892SBill Paul 	register int		i;
46096f2e892SBill Paul 	u_int16_t		word = 0;
46196f2e892SBill Paul 
46296f2e892SBill Paul 	/* Force EEPROM to idle state. */
46396f2e892SBill Paul 	dc_eeprom_idle(sc);
46496f2e892SBill Paul 
46596f2e892SBill Paul 	/* Enter EEPROM access mode. */
46696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
46796f2e892SBill Paul 	dc_delay(sc);
46896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
46996f2e892SBill Paul 	dc_delay(sc);
47096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
47196f2e892SBill Paul 	dc_delay(sc);
47296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
47396f2e892SBill Paul 	dc_delay(sc);
47496f2e892SBill Paul 
47596f2e892SBill Paul 	/*
47696f2e892SBill Paul 	 * Send address of word we want to read.
47796f2e892SBill Paul 	 */
47896f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
47996f2e892SBill Paul 
48096f2e892SBill Paul 	/*
48196f2e892SBill Paul 	 * Start reading bits from EEPROM.
48296f2e892SBill Paul 	 */
48396f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
48496f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48596f2e892SBill Paul 		dc_delay(sc);
48696f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
48796f2e892SBill Paul 			word |= i;
48896f2e892SBill Paul 		dc_delay(sc);
48996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
49096f2e892SBill Paul 		dc_delay(sc);
49196f2e892SBill Paul 	}
49296f2e892SBill Paul 
49396f2e892SBill Paul 	/* Turn off EEPROM access mode. */
49496f2e892SBill Paul 	dc_eeprom_idle(sc);
49596f2e892SBill Paul 
49696f2e892SBill Paul 	*dest = word;
49796f2e892SBill Paul 
49896f2e892SBill Paul 	return;
49996f2e892SBill Paul }
50096f2e892SBill Paul 
50196f2e892SBill Paul /*
50296f2e892SBill Paul  * Read a sequence of words from the EEPROM.
50396f2e892SBill Paul  */
50496f2e892SBill Paul static void dc_read_eeprom(sc, dest, off, cnt, swap)
50596f2e892SBill Paul 	struct dc_softc		*sc;
50696f2e892SBill Paul 	caddr_t			dest;
50796f2e892SBill Paul 	int			off;
50896f2e892SBill Paul 	int			cnt;
50996f2e892SBill Paul 	int			swap;
51096f2e892SBill Paul {
51196f2e892SBill Paul 	int			i;
51296f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
51396f2e892SBill Paul 
51496f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
51596f2e892SBill Paul 		if (DC_IS_PNIC(sc))
51696f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
517feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
518feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
51996f2e892SBill Paul 		else
52096f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
52196f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
52296f2e892SBill Paul 		if (swap)
52396f2e892SBill Paul 			*ptr = ntohs(word);
52496f2e892SBill Paul 		else
52596f2e892SBill Paul 			*ptr = word;
52696f2e892SBill Paul 	}
52796f2e892SBill Paul 
52896f2e892SBill Paul 	return;
52996f2e892SBill Paul }
53096f2e892SBill Paul 
53196f2e892SBill Paul /*
53296f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
53396f2e892SBill Paul  * Application Notes pp.19-21.
53496f2e892SBill Paul  */
53596f2e892SBill Paul /*
53696f2e892SBill Paul  * Write a bit to the MII bus.
53796f2e892SBill Paul  */
53896f2e892SBill Paul static void dc_mii_writebit(sc, bit)
53996f2e892SBill Paul 	struct dc_softc		*sc;
54096f2e892SBill Paul 	int			bit;
54196f2e892SBill Paul {
54296f2e892SBill Paul 	if (bit)
54396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
54496f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
54596f2e892SBill Paul 	else
54696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
54796f2e892SBill Paul 
54896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
54996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
55096f2e892SBill Paul 
55196f2e892SBill Paul 	return;
55296f2e892SBill Paul }
55396f2e892SBill Paul 
55496f2e892SBill Paul /*
55596f2e892SBill Paul  * Read a bit from the MII bus.
55696f2e892SBill Paul  */
55796f2e892SBill Paul static int dc_mii_readbit(sc)
55896f2e892SBill Paul 	struct dc_softc		*sc;
55996f2e892SBill Paul {
56096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
56196f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
56296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
56396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
56496f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
56596f2e892SBill Paul 		return(1);
56696f2e892SBill Paul 
56796f2e892SBill Paul 	return(0);
56896f2e892SBill Paul }
56996f2e892SBill Paul 
57096f2e892SBill Paul /*
57196f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
57296f2e892SBill Paul  */
57396f2e892SBill Paul static void dc_mii_sync(sc)
57496f2e892SBill Paul 	struct dc_softc		*sc;
57596f2e892SBill Paul {
57696f2e892SBill Paul 	register int		i;
57796f2e892SBill Paul 
57896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
57996f2e892SBill Paul 
58096f2e892SBill Paul 	for (i = 0; i < 32; i++)
58196f2e892SBill Paul 		dc_mii_writebit(sc, 1);
58296f2e892SBill Paul 
58396f2e892SBill Paul 	return;
58496f2e892SBill Paul }
58596f2e892SBill Paul 
58696f2e892SBill Paul /*
58796f2e892SBill Paul  * Clock a series of bits through the MII.
58896f2e892SBill Paul  */
58996f2e892SBill Paul static void dc_mii_send(sc, bits, cnt)
59096f2e892SBill Paul 	struct dc_softc		*sc;
59196f2e892SBill Paul 	u_int32_t		bits;
59296f2e892SBill Paul 	int			cnt;
59396f2e892SBill Paul {
59496f2e892SBill Paul 	int			i;
59596f2e892SBill Paul 
59696f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
59796f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
59896f2e892SBill Paul }
59996f2e892SBill Paul 
60096f2e892SBill Paul /*
60196f2e892SBill Paul  * Read an PHY register through the MII.
60296f2e892SBill Paul  */
60396f2e892SBill Paul static int dc_mii_readreg(sc, frame)
60496f2e892SBill Paul 	struct dc_softc		*sc;
60596f2e892SBill Paul 	struct dc_mii_frame	*frame;
60696f2e892SBill Paul 
60796f2e892SBill Paul {
608d1ce9105SBill Paul 	int			i, ack;
60996f2e892SBill Paul 
610d1ce9105SBill Paul 	DC_LOCK(sc);
61196f2e892SBill Paul 
61296f2e892SBill Paul 	/*
61396f2e892SBill Paul 	 * Set up frame for RX.
61496f2e892SBill Paul 	 */
61596f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
61696f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
61796f2e892SBill Paul 	frame->mii_turnaround = 0;
61896f2e892SBill Paul 	frame->mii_data = 0;
61996f2e892SBill Paul 
62096f2e892SBill Paul 	/*
62196f2e892SBill Paul 	 * Sync the PHYs.
62296f2e892SBill Paul 	 */
62396f2e892SBill Paul 	dc_mii_sync(sc);
62496f2e892SBill Paul 
62596f2e892SBill Paul 	/*
62696f2e892SBill Paul 	 * Send command/address info.
62796f2e892SBill Paul 	 */
62896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
62996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
63096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
63196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
63296f2e892SBill Paul 
63396f2e892SBill Paul #ifdef notdef
63496f2e892SBill Paul 	/* Idle bit */
63596f2e892SBill Paul 	dc_mii_writebit(sc, 1);
63696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
63796f2e892SBill Paul #endif
63896f2e892SBill Paul 
63996f2e892SBill Paul 	/* Check for ack */
64096f2e892SBill Paul 	ack = dc_mii_readbit(sc);
64196f2e892SBill Paul 
64296f2e892SBill Paul 	/*
64396f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
64496f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
64596f2e892SBill Paul 	 */
64696f2e892SBill Paul 	if (ack) {
64796f2e892SBill Paul 		for(i = 0; i < 16; i++) {
64896f2e892SBill Paul 			dc_mii_readbit(sc);
64996f2e892SBill Paul 		}
65096f2e892SBill Paul 		goto fail;
65196f2e892SBill Paul 	}
65296f2e892SBill Paul 
65396f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
65496f2e892SBill Paul 		if (!ack) {
65596f2e892SBill Paul 			if (dc_mii_readbit(sc))
65696f2e892SBill Paul 				frame->mii_data |= i;
65796f2e892SBill Paul 		}
65896f2e892SBill Paul 	}
65996f2e892SBill Paul 
66096f2e892SBill Paul fail:
66196f2e892SBill Paul 
66296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
66396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
66496f2e892SBill Paul 
665d1ce9105SBill Paul 	DC_UNLOCK(sc);
66696f2e892SBill Paul 
66796f2e892SBill Paul 	if (ack)
66896f2e892SBill Paul 		return(1);
66996f2e892SBill Paul 	return(0);
67096f2e892SBill Paul }
67196f2e892SBill Paul 
67296f2e892SBill Paul /*
67396f2e892SBill Paul  * Write to a PHY register through the MII.
67496f2e892SBill Paul  */
67596f2e892SBill Paul static int dc_mii_writereg(sc, frame)
67696f2e892SBill Paul 	struct dc_softc		*sc;
67796f2e892SBill Paul 	struct dc_mii_frame	*frame;
67896f2e892SBill Paul 
67996f2e892SBill Paul {
680d1ce9105SBill Paul 	DC_LOCK(sc);
68196f2e892SBill Paul 	/*
68296f2e892SBill Paul 	 * Set up frame for TX.
68396f2e892SBill Paul 	 */
68496f2e892SBill Paul 
68596f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
68696f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
68796f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
68896f2e892SBill Paul 
68996f2e892SBill Paul 	/*
69096f2e892SBill Paul 	 * Sync the PHYs.
69196f2e892SBill Paul 	 */
69296f2e892SBill Paul 	dc_mii_sync(sc);
69396f2e892SBill Paul 
69496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
69596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
69696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
69796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
69896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
69996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
70096f2e892SBill Paul 
70196f2e892SBill Paul 	/* Idle bit. */
70296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70496f2e892SBill Paul 
705d1ce9105SBill Paul 	DC_UNLOCK(sc);
70696f2e892SBill Paul 
70796f2e892SBill Paul 	return(0);
70896f2e892SBill Paul }
70996f2e892SBill Paul 
71096f2e892SBill Paul static int dc_miibus_readreg(dev, phy, reg)
71196f2e892SBill Paul 	device_t		dev;
71296f2e892SBill Paul 	int			phy, reg;
71396f2e892SBill Paul {
71496f2e892SBill Paul 	struct dc_mii_frame	frame;
71596f2e892SBill Paul 	struct dc_softc		*sc;
716c85c4667SBill Paul 	int			i, rval, phy_reg = 0;
71796f2e892SBill Paul 
71896f2e892SBill Paul 	sc = device_get_softc(dev);
71996f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
72096f2e892SBill Paul 
72196f2e892SBill Paul 	/*
72296f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
72396f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
72496f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
72596f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
72696f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
72796f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
72896f2e892SBill Paul 	 * that the PHY is at MII address 1.
72996f2e892SBill Paul 	 */
73096f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
73196f2e892SBill Paul 		return(0);
73296f2e892SBill Paul 
7331af8bec7SBill Paul 	/*
7341af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7351af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7361af8bec7SBill Paul 	 * so we only respond to correct one.
7371af8bec7SBill Paul 	 */
7381af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
7391af8bec7SBill Paul 		return(0);
7401af8bec7SBill Paul 
7415c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
74296f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
74396f2e892SBill Paul 			switch(reg) {
74496f2e892SBill Paul 			case MII_BMSR:
74596f2e892SBill Paul 			/*
74696f2e892SBill Paul 			 * Fake something to make the probe
74796f2e892SBill Paul 			 * code think there's a PHY here.
74896f2e892SBill Paul 			 */
74996f2e892SBill Paul 				return(BMSR_MEDIAMASK);
75096f2e892SBill Paul 				break;
75196f2e892SBill Paul 			case MII_PHYIDR1:
75296f2e892SBill Paul 				if (DC_IS_PNIC(sc))
75396f2e892SBill Paul 					return(DC_VENDORID_LO);
75496f2e892SBill Paul 				return(DC_VENDORID_DEC);
75596f2e892SBill Paul 				break;
75696f2e892SBill Paul 			case MII_PHYIDR2:
75796f2e892SBill Paul 				if (DC_IS_PNIC(sc))
75896f2e892SBill Paul 					return(DC_DEVICEID_82C168);
75996f2e892SBill Paul 				return(DC_DEVICEID_21143);
76096f2e892SBill Paul 				break;
76196f2e892SBill Paul 			default:
76296f2e892SBill Paul 				return(0);
76396f2e892SBill Paul 				break;
76496f2e892SBill Paul 			}
76596f2e892SBill Paul 		} else
76696f2e892SBill Paul 			return(0);
76796f2e892SBill Paul 	}
76896f2e892SBill Paul 
76996f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
77096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
77196f2e892SBill Paul 		    (phy << 23) | (reg << 18));
77296f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
77396f2e892SBill Paul 			DELAY(1);
77496f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
77596f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
77696f2e892SBill Paul 				rval &= 0xFFFF;
77796f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
77896f2e892SBill Paul 			}
77996f2e892SBill Paul 		}
78096f2e892SBill Paul 		return(0);
78196f2e892SBill Paul 	}
78296f2e892SBill Paul 
78396f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
78496f2e892SBill Paul 		switch(reg) {
78596f2e892SBill Paul 		case MII_BMCR:
78696f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
78796f2e892SBill Paul 			break;
78896f2e892SBill Paul 		case MII_BMSR:
78996f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
79096f2e892SBill Paul 			break;
79196f2e892SBill Paul 		case MII_PHYIDR1:
79296f2e892SBill Paul 			phy_reg = DC_AL_VENID;
79396f2e892SBill Paul 			break;
79496f2e892SBill Paul 		case MII_PHYIDR2:
79596f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
79696f2e892SBill Paul 			break;
79796f2e892SBill Paul 		case MII_ANAR:
79896f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
79996f2e892SBill Paul 			break;
80096f2e892SBill Paul 		case MII_ANLPAR:
80196f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
80296f2e892SBill Paul 			break;
80396f2e892SBill Paul 		case MII_ANER:
80496f2e892SBill Paul 			phy_reg = DC_AL_ANER;
80596f2e892SBill Paul 			break;
80696f2e892SBill Paul 		default:
80796f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
80896f2e892SBill Paul 			    sc->dc_unit, reg);
80996f2e892SBill Paul 			return(0);
81096f2e892SBill Paul 			break;
81196f2e892SBill Paul 		}
81296f2e892SBill Paul 
81396f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
81496f2e892SBill Paul 
81596f2e892SBill Paul 		if (rval == 0xFFFF)
81696f2e892SBill Paul 			return(0);
81796f2e892SBill Paul 		return(rval);
81896f2e892SBill Paul 	}
81996f2e892SBill Paul 
82096f2e892SBill Paul 	frame.mii_phyaddr = phy;
82196f2e892SBill Paul 	frame.mii_regaddr = reg;
822419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
823f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
824f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
825419146d9SBill Paul 	}
82696f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
827419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
828f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
82996f2e892SBill Paul 
83096f2e892SBill Paul 	return(frame.mii_data);
83196f2e892SBill Paul }
83296f2e892SBill Paul 
83396f2e892SBill Paul static int dc_miibus_writereg(dev, phy, reg, data)
83496f2e892SBill Paul 	device_t		dev;
83596f2e892SBill Paul 	int			phy, reg, data;
83696f2e892SBill Paul {
83796f2e892SBill Paul 	struct dc_softc		*sc;
83896f2e892SBill Paul 	struct dc_mii_frame	frame;
839c85c4667SBill Paul 	int			i, phy_reg = 0;
84096f2e892SBill Paul 
84196f2e892SBill Paul 	sc = device_get_softc(dev);
84296f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
84396f2e892SBill Paul 
84496f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
84596f2e892SBill Paul 		return(0);
84696f2e892SBill Paul 
8471af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
8481af8bec7SBill Paul 		return(0);
8491af8bec7SBill Paul 
85096f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
85196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
85296f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
85396f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
85496f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
85596f2e892SBill Paul 				break;
85696f2e892SBill Paul 		}
85796f2e892SBill Paul 		return(0);
85896f2e892SBill Paul 	}
85996f2e892SBill Paul 
86096f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
86196f2e892SBill Paul 		switch(reg) {
86296f2e892SBill Paul 		case MII_BMCR:
86396f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
86496f2e892SBill Paul 			break;
86596f2e892SBill Paul 		case MII_BMSR:
86696f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
86796f2e892SBill Paul 			break;
86896f2e892SBill Paul 		case MII_PHYIDR1:
86996f2e892SBill Paul 			phy_reg = DC_AL_VENID;
87096f2e892SBill Paul 			break;
87196f2e892SBill Paul 		case MII_PHYIDR2:
87296f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
87396f2e892SBill Paul 			break;
87496f2e892SBill Paul 		case MII_ANAR:
87596f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
87696f2e892SBill Paul 			break;
87796f2e892SBill Paul 		case MII_ANLPAR:
87896f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
87996f2e892SBill Paul 			break;
88096f2e892SBill Paul 		case MII_ANER:
88196f2e892SBill Paul 			phy_reg = DC_AL_ANER;
88296f2e892SBill Paul 			break;
88396f2e892SBill Paul 		default:
88496f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
88596f2e892SBill Paul 			    sc->dc_unit, reg);
88696f2e892SBill Paul 			return(0);
88796f2e892SBill Paul 			break;
88896f2e892SBill Paul 		}
88996f2e892SBill Paul 
89096f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
89196f2e892SBill Paul 		return(0);
89296f2e892SBill Paul 	}
89396f2e892SBill Paul 
89496f2e892SBill Paul 	frame.mii_phyaddr = phy;
89596f2e892SBill Paul 	frame.mii_regaddr = reg;
89696f2e892SBill Paul 	frame.mii_data = data;
89796f2e892SBill Paul 
898419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
899f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
900f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
901419146d9SBill Paul 	}
90296f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
903419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
904f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
90596f2e892SBill Paul 
90696f2e892SBill Paul 	return(0);
90796f2e892SBill Paul }
90896f2e892SBill Paul 
90996f2e892SBill Paul static void dc_miibus_statchg(dev)
91096f2e892SBill Paul 	device_t		dev;
91196f2e892SBill Paul {
91296f2e892SBill Paul 	struct dc_softc		*sc;
91396f2e892SBill Paul 	struct mii_data		*mii;
914f43d9309SBill Paul 	struct ifmedia		*ifm;
91596f2e892SBill Paul 
91696f2e892SBill Paul 	sc = device_get_softc(dev);
91796f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
91896f2e892SBill Paul 		return;
9195c1cfac4SBill Paul 
92096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
921f43d9309SBill Paul 	ifm = &mii->mii_media;
922f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
923f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
924f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
925f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
926f43d9309SBill Paul 	} else {
92796f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
92896f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
929f43d9309SBill Paul 	}
930f43d9309SBill Paul 
931f43d9309SBill Paul 	return;
932f43d9309SBill Paul }
933f43d9309SBill Paul 
934f43d9309SBill Paul /*
935f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
936f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
937f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
938f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
939f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
940f43d9309SBill Paul  * with it itself. *sigh*
941f43d9309SBill Paul  */
942f43d9309SBill Paul static void dc_miibus_mediainit(dev)
943f43d9309SBill Paul 	device_t		dev;
944f43d9309SBill Paul {
945f43d9309SBill Paul 	struct dc_softc		*sc;
946f43d9309SBill Paul 	struct mii_data		*mii;
947f43d9309SBill Paul 	struct ifmedia		*ifm;
948f43d9309SBill Paul 	int			rev;
949f43d9309SBill Paul 
950f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
951f43d9309SBill Paul 
952f43d9309SBill Paul 	sc = device_get_softc(dev);
953f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
954f43d9309SBill Paul 	ifm = &mii->mii_media;
955f43d9309SBill Paul 
956f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
957f43d9309SBill Paul 		ifmedia_add(ifm, IFM_ETHER|IFM_homePNA, 0, NULL);
95896f2e892SBill Paul 
95996f2e892SBill Paul 	return;
96096f2e892SBill Paul }
96196f2e892SBill Paul 
96296f2e892SBill Paul #define DC_POLY		0xEDB88320
96379d11e09SBill Paul #define DC_BITS_512	9
96479d11e09SBill Paul #define DC_BITS_128	7
96579d11e09SBill Paul #define DC_BITS_64	6
96696f2e892SBill Paul 
96796f2e892SBill Paul static u_int32_t dc_crc_le(sc, addr)
96896f2e892SBill Paul 	struct dc_softc		*sc;
96996f2e892SBill Paul 	caddr_t			addr;
97096f2e892SBill Paul {
97196f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
97296f2e892SBill Paul 
97396f2e892SBill Paul 	/* Compute CRC for the address value. */
97496f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
97596f2e892SBill Paul 
97696f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
97796f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
97896f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
97996f2e892SBill Paul 	}
98096f2e892SBill Paul 
98179d11e09SBill Paul 	/*
98279d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
98379d11e09SBill Paul 	 * chips is only 128 bits wide.
98479d11e09SBill Paul 	 */
98579d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
98679d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
98796f2e892SBill Paul 
98879d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
98979d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
99079d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
99179d11e09SBill Paul 
992feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
993feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
994feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
995feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
996feb78939SJonathan Chen 			return (crc & 0x0F) + (crc	& 0x70)*3 + (14 << 4);
997feb78939SJonathan Chen 		else
998feb78939SJonathan Chen 			return (crc & 0x1F) + ((crc>>1) & 0xF0)*3 + (12 << 4);
999feb78939SJonathan Chen 	}
1000feb78939SJonathan Chen 
100179d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
100296f2e892SBill Paul }
100396f2e892SBill Paul 
100496f2e892SBill Paul /*
100596f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
100696f2e892SBill Paul  */
100796f2e892SBill Paul static u_int32_t dc_crc_be(addr)
100896f2e892SBill Paul 	caddr_t			addr;
100996f2e892SBill Paul {
101096f2e892SBill Paul 	u_int32_t		crc, carry;
101196f2e892SBill Paul 	int			i, j;
101296f2e892SBill Paul 	u_int8_t		c;
101396f2e892SBill Paul 
101496f2e892SBill Paul 	/* Compute CRC for the address value. */
101596f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
101696f2e892SBill Paul 
101796f2e892SBill Paul 	for (i = 0; i < 6; i++) {
101896f2e892SBill Paul 		c = *(addr + i);
101996f2e892SBill Paul 		for (j = 0; j < 8; j++) {
102096f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
102196f2e892SBill Paul 			crc <<= 1;
102296f2e892SBill Paul 			c >>= 1;
102396f2e892SBill Paul 			if (carry)
102496f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
102596f2e892SBill Paul 		}
102696f2e892SBill Paul 	}
102796f2e892SBill Paul 
102896f2e892SBill Paul 	/* return the filter bit position */
102996f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
103096f2e892SBill Paul }
103196f2e892SBill Paul 
103296f2e892SBill Paul /*
103396f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
103496f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
103596f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
103696f2e892SBill Paul  *
103796f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
103896f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
103996f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
104096f2e892SBill Paul  * we need that too.
104196f2e892SBill Paul  */
104296f2e892SBill Paul void dc_setfilt_21143(sc)
104396f2e892SBill Paul 	struct dc_softc		*sc;
104496f2e892SBill Paul {
104596f2e892SBill Paul 	struct dc_desc		*sframe;
104696f2e892SBill Paul 	u_int32_t		h, *sp;
104796f2e892SBill Paul 	struct ifmultiaddr	*ifma;
104896f2e892SBill Paul 	struct ifnet		*ifp;
104996f2e892SBill Paul 	int			i;
105096f2e892SBill Paul 
105196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
105296f2e892SBill Paul 
105396f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
105496f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
105596f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
105696f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
105796f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
105896f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
105996f2e892SBill Paul 
106096f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
106196f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
106296f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
106396f2e892SBill Paul 
106496f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
106596f2e892SBill Paul 
106696f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
106796f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
106896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
106996f2e892SBill Paul 	else
107096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
107196f2e892SBill Paul 
107296f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
107396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
107496f2e892SBill Paul 	else
107596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
107696f2e892SBill Paul 
10776817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
107896f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
107996f2e892SBill Paul 			continue;
108096f2e892SBill Paul 		h = dc_crc_le(sc,
108196f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
108296f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
108396f2e892SBill Paul 	}
108496f2e892SBill Paul 
108596f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
108696f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
108796f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
108896f2e892SBill Paul 	}
108996f2e892SBill Paul 
109096f2e892SBill Paul 	/* Set our MAC address */
109196f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
109296f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
109396f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
109496f2e892SBill Paul 
109596f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
109696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
109796f2e892SBill Paul 
109896f2e892SBill Paul 	/*
109996f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
110096f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
110196f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
110296f2e892SBill Paul 	 * medicine.
110396f2e892SBill Paul 	 */
110496f2e892SBill Paul 	DELAY(10000);
110596f2e892SBill Paul 
110696f2e892SBill Paul 	ifp->if_timer = 5;
110796f2e892SBill Paul 
110896f2e892SBill Paul 	return;
110996f2e892SBill Paul }
111096f2e892SBill Paul 
111196f2e892SBill Paul void dc_setfilt_admtek(sc)
111296f2e892SBill Paul 	struct dc_softc		*sc;
111396f2e892SBill Paul {
111496f2e892SBill Paul 	struct ifnet		*ifp;
111596f2e892SBill Paul 	int			h = 0;
111696f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
111796f2e892SBill Paul 	struct ifmultiaddr	*ifma;
111896f2e892SBill Paul 
111996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
112096f2e892SBill Paul 
112196f2e892SBill Paul 	/* Init our MAC address */
112296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
112396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
112496f2e892SBill Paul 
112596f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
112696f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
112796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112896f2e892SBill Paul 	else
112996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
113096f2e892SBill Paul 
113196f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
113296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
113396f2e892SBill Paul 	else
113496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
113596f2e892SBill Paul 
113696f2e892SBill Paul 	/* first, zot all the existing hash bits */
113796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
113896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
113996f2e892SBill Paul 
114096f2e892SBill Paul 	/*
114196f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
114296f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
114396f2e892SBill Paul 	 */
114496f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
114596f2e892SBill Paul 		return;
114696f2e892SBill Paul 
114796f2e892SBill Paul 	/* now program new ones */
11486817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
114996f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
115096f2e892SBill Paul 			continue;
115196f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
115296f2e892SBill Paul 		if (h < 32)
115396f2e892SBill Paul 			hashes[0] |= (1 << h);
115496f2e892SBill Paul 		else
115596f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
115696f2e892SBill Paul 	}
115796f2e892SBill Paul 
115896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
115996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
116096f2e892SBill Paul 
116196f2e892SBill Paul 	return;
116296f2e892SBill Paul }
116396f2e892SBill Paul 
116496f2e892SBill Paul void dc_setfilt_asix(sc)
116596f2e892SBill Paul 	struct dc_softc		*sc;
116696f2e892SBill Paul {
116796f2e892SBill Paul 	struct ifnet		*ifp;
116896f2e892SBill Paul 	int			h = 0;
116996f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
117096f2e892SBill Paul 	struct ifmultiaddr	*ifma;
117196f2e892SBill Paul 
117296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
117396f2e892SBill Paul 
117496f2e892SBill Paul         /* Init our MAC address */
117596f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
117696f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
117796f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
117896f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
117996f2e892SBill Paul         CSR_WRITE_4(sc, DC_AX_FILTDATA,
118096f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
118196f2e892SBill Paul 
118296f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
118396f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
118496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
118596f2e892SBill Paul 	else
118696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
118796f2e892SBill Paul 
118896f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
118996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
119096f2e892SBill Paul 	else
119196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
119296f2e892SBill Paul 
119396f2e892SBill Paul 	/*
119496f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
119596f2e892SBill Paul 	 * of broadcast frames.
119696f2e892SBill Paul 	 */
119796f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
119896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
119996f2e892SBill Paul 	else
120096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
120196f2e892SBill Paul 
120296f2e892SBill Paul 	/* first, zot all the existing hash bits */
120396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
120496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
120596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
120696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
120796f2e892SBill Paul 
120896f2e892SBill Paul 	/*
120996f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
121096f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
121196f2e892SBill Paul 	 */
121296f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
121396f2e892SBill Paul 		return;
121496f2e892SBill Paul 
121596f2e892SBill Paul 	/* now program new ones */
12166817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
121796f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
121896f2e892SBill Paul 			continue;
121996f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
122096f2e892SBill Paul 		if (h < 32)
122196f2e892SBill Paul 			hashes[0] |= (1 << h);
122296f2e892SBill Paul 		else
122396f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
122496f2e892SBill Paul 	}
122596f2e892SBill Paul 
122696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
122796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
122896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
122996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
123096f2e892SBill Paul 
123196f2e892SBill Paul 	return;
123296f2e892SBill Paul }
123396f2e892SBill Paul 
1234feb78939SJonathan Chen void dc_setfilt_xircom(sc)
1235feb78939SJonathan Chen 	struct dc_softc		*sc;
1236feb78939SJonathan Chen {
1237feb78939SJonathan Chen 	struct dc_desc		*sframe;
1238feb78939SJonathan Chen 	u_int32_t		h, *sp;
1239feb78939SJonathan Chen 	struct ifmultiaddr	*ifma;
1240feb78939SJonathan Chen 	struct ifnet		*ifp;
1241feb78939SJonathan Chen 	int			i;
1242feb78939SJonathan Chen 
1243feb78939SJonathan Chen 	ifp = &sc->arpcom.ac_if;
1244feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
1245feb78939SJonathan Chen 
1246feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1247feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1248feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1249feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
1250feb78939SJonathan Chen 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
1251feb78939SJonathan Chen 	bzero((char *)sp, DC_SFRAME_LEN);
1252feb78939SJonathan Chen 
1253feb78939SJonathan Chen 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
1254feb78939SJonathan Chen 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
1255feb78939SJonathan Chen 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
1256feb78939SJonathan Chen 
1257feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
1258feb78939SJonathan Chen 
1259feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1260feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1261feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1262feb78939SJonathan Chen 	else
1263feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1264feb78939SJonathan Chen 
1265feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1266feb78939SJonathan Chen  		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1267feb78939SJonathan Chen 	else
1268feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1269feb78939SJonathan Chen 
12706817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1271feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1272feb78939SJonathan Chen 			continue;
12731d5e5310SBill Paul 		h = dc_crc_le(sc,
12741d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1275feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1276feb78939SJonathan Chen 	}
1277feb78939SJonathan Chen 
1278feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1279feb78939SJonathan Chen 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
1280feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1281feb78939SJonathan Chen 	}
1282feb78939SJonathan Chen 
1283feb78939SJonathan Chen 	/* Set our MAC address */
1284feb78939SJonathan Chen 	sp[0] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
1285feb78939SJonathan Chen 	sp[1] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
1286feb78939SJonathan Chen 	sp[2] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
1287feb78939SJonathan Chen 
1288feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1289feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1290feb78939SJonathan Chen 	ifp->if_flags |= IFF_RUNNING;
1291feb78939SJonathan Chen 	sframe->dc_status = DC_TXSTAT_OWN;
1292feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1293feb78939SJonathan Chen 
1294feb78939SJonathan Chen 	/*
1295feb78939SJonathan Chen 	 * wait some time...
1296feb78939SJonathan Chen 	 */
1297feb78939SJonathan Chen 	DELAY(1000);
1298feb78939SJonathan Chen 
1299feb78939SJonathan Chen 	ifp->if_timer = 5;
1300feb78939SJonathan Chen 
1301feb78939SJonathan Chen 	return;
1302feb78939SJonathan Chen }
1303feb78939SJonathan Chen 
130496f2e892SBill Paul static void dc_setfilt(sc)
130596f2e892SBill Paul 	struct dc_softc		*sc;
130696f2e892SBill Paul {
130796f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13081af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
130996f2e892SBill Paul 		dc_setfilt_21143(sc);
131096f2e892SBill Paul 
131196f2e892SBill Paul 	if (DC_IS_ASIX(sc))
131296f2e892SBill Paul 		dc_setfilt_asix(sc);
131396f2e892SBill Paul 
131496f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
131596f2e892SBill Paul 		dc_setfilt_admtek(sc);
131696f2e892SBill Paul 
1317feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1318feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
1319feb78939SJonathan Chen 
132096f2e892SBill Paul  	return;
132196f2e892SBill Paul }
132296f2e892SBill Paul 
132396f2e892SBill Paul /*
132496f2e892SBill Paul  * In order to fiddle with the
132596f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
132696f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
132796f2e892SBill Paul  */
132896f2e892SBill Paul static void dc_setcfg(sc, media)
132996f2e892SBill Paul 	struct dc_softc		*sc;
133096f2e892SBill Paul 	int			media;
133196f2e892SBill Paul {
133296f2e892SBill Paul 	int			i, restart = 0;
133396f2e892SBill Paul 	u_int32_t		isr;
133496f2e892SBill Paul 
133596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
133696f2e892SBill Paul 		return;
133796f2e892SBill Paul 
133896f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
133996f2e892SBill Paul 		restart = 1;
134096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
134196f2e892SBill Paul 
134296f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
134396f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1344d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
134596f2e892SBill Paul 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED)
134696f2e892SBill Paul 				break;
1347d467c136SBill Paul 			DELAY(10);
134896f2e892SBill Paul 		}
134996f2e892SBill Paul 
135096f2e892SBill Paul 		if (i == DC_TIMEOUT)
135196f2e892SBill Paul 			printf("dc%d: failed to force tx and "
135296f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
135396f2e892SBill Paul 	}
135496f2e892SBill Paul 
135596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1356042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1357042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
135896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
13598273d5f8SBill Paul 			int	watchdogreg;
13608273d5f8SBill Paul 
1361bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
13628273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
13638273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
13648273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
13658273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
13664c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1367bf645417SBill Paul 			} else {
1368bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1369bf645417SBill Paul 			}
137096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
137196f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
137296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
137396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
137496f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
137588d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
137696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
137796f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1378e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1379e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
138096f2e892SBill Paul 		} else {
138196f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
138296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
138396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
138496f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
138596f2e892SBill Paul 			}
1386318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1387318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1388318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
13895c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
13905c1cfac4SBill Paul 				dc_apply_fixup(sc,
13915c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
13925c1cfac4SBill Paul 				    IFM_100_TX|IFM_FDX : IFM_100_TX);
139396f2e892SBill Paul 		}
139496f2e892SBill Paul 	}
139596f2e892SBill Paul 
139696f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1397042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1398042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
139996f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14008273d5f8SBill Paul 			int	watchdogreg;
14018273d5f8SBill Paul 
14028273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
14034c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14048273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14058273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14068273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14078273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14084c2efe27SBill Paul 			} else {
14094c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14104c2efe27SBill Paul 			}
141196f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
141296f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
141396f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
141496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
141588d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
141696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
141796f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1418e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1419e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
142096f2e892SBill Paul 		} else {
142196f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142296f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
142396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
142496f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
142596f2e892SBill Paul 			}
142696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1427318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
142896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14295c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14305c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14315c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14325c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14335c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14345c1cfac4SBill Paul 				else
14355c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14365c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14375c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14385c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14395c1cfac4SBill Paul 				dc_apply_fixup(sc,
14405c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14415c1cfac4SBill Paul 				    IFM_10_T|IFM_FDX : IFM_10_T);
14425c1cfac4SBill Paul 				DELAY(20000);
14435c1cfac4SBill Paul 			}
144496f2e892SBill Paul 		}
144596f2e892SBill Paul 	}
144696f2e892SBill Paul 
1447f43d9309SBill Paul 	/*
1448f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1449f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1450f43d9309SBill Paul 	 * on the external MII port.
1451f43d9309SBill Paul 	 */
1452f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
1453f43d9309SBill Paul 		if (IFM_SUBTYPE(media) == IFM_homePNA) {
1454f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1455f43d9309SBill Paul 			sc->dc_link = 1;
1456f43d9309SBill Paul 		} else {
1457f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1458f43d9309SBill Paul 		}
1459f43d9309SBill Paul 	}
1460f43d9309SBill Paul 
146196f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
146296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
146396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
146496f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
146596f2e892SBill Paul 	} else {
146696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
146796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
146896f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
146996f2e892SBill Paul 	}
147096f2e892SBill Paul 
147196f2e892SBill Paul 	if (restart)
147296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
147396f2e892SBill Paul 
147496f2e892SBill Paul 	return;
147596f2e892SBill Paul }
147696f2e892SBill Paul 
147796f2e892SBill Paul static void dc_reset(sc)
147896f2e892SBill Paul 	struct dc_softc		*sc;
147996f2e892SBill Paul {
148096f2e892SBill Paul 	register int		i;
148196f2e892SBill Paul 
148296f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
148396f2e892SBill Paul 
148496f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
148596f2e892SBill Paul 		DELAY(10);
148696f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
148796f2e892SBill Paul 			break;
148896f2e892SBill Paul 	}
148996f2e892SBill Paul 
14901af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
14911d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
149296f2e892SBill Paul 		DELAY(10000);
149396f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
149496f2e892SBill Paul 		i = 0;
149596f2e892SBill Paul 	}
149696f2e892SBill Paul 
149796f2e892SBill Paul 	if (i == DC_TIMEOUT)
149896f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
149996f2e892SBill Paul 
150096f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
150196f2e892SBill Paul 	DELAY(1000);
150296f2e892SBill Paul 
150396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
150496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
150596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
150696f2e892SBill Paul 
150791cc2adbSBill Paul 	/*
150891cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
150991cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
151091cc2adbSBill Paul 	 * into a state where it will never come out of reset
151191cc2adbSBill Paul 	 * until we reset the whole chip again.
151291cc2adbSBill Paul 	 */
15135c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
151491cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15155c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15165c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15175c1cfac4SBill Paul 	}
151891cc2adbSBill Paul 
151996f2e892SBill Paul         return;
152096f2e892SBill Paul }
152196f2e892SBill Paul 
152296f2e892SBill Paul static struct dc_type *dc_devtype(dev)
152396f2e892SBill Paul 	device_t		dev;
152496f2e892SBill Paul {
152596f2e892SBill Paul 	struct dc_type		*t;
152696f2e892SBill Paul 	u_int32_t		rev;
152796f2e892SBill Paul 
152896f2e892SBill Paul 	t = dc_devs;
152996f2e892SBill Paul 
153096f2e892SBill Paul 	while(t->dc_name != NULL) {
153196f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
153296f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
153396f2e892SBill Paul 			/* Check the PCI revision */
153496f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
153596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
153696f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
153796f2e892SBill Paul 				t++;
153896f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
153996f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
154096f2e892SBill Paul 				t++;
154196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
154279d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
154379d11e09SBill Paul 				t++;
154479d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
154596f2e892SBill Paul 			    rev >= DC_REVISION_98725)
154696f2e892SBill Paul 				t++;
154796f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
154896f2e892SBill Paul 			    rev >= DC_REVISION_88141)
154996f2e892SBill Paul 				t++;
155096f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
155196f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
155296f2e892SBill Paul 				t++;
155388d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
155488d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
155588d739dcSBill Paul 				t++;
155696f2e892SBill Paul 			return(t);
155796f2e892SBill Paul 		}
155896f2e892SBill Paul 		t++;
155996f2e892SBill Paul 	}
156096f2e892SBill Paul 
156196f2e892SBill Paul 	return(NULL);
156296f2e892SBill Paul }
156396f2e892SBill Paul 
156496f2e892SBill Paul /*
156596f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
156696f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
156796f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
156896f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
156996f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
157096f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
157196f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
157296f2e892SBill Paul  */
157396f2e892SBill Paul static int dc_probe(dev)
157496f2e892SBill Paul 	device_t		dev;
157596f2e892SBill Paul {
157696f2e892SBill Paul 	struct dc_type		*t;
157796f2e892SBill Paul 
157896f2e892SBill Paul 	t = dc_devtype(dev);
157996f2e892SBill Paul 
158096f2e892SBill Paul 	if (t != NULL) {
158196f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
158296f2e892SBill Paul 		return(0);
158396f2e892SBill Paul 	}
158496f2e892SBill Paul 
158596f2e892SBill Paul 	return(ENXIO);
158696f2e892SBill Paul }
158796f2e892SBill Paul 
158896f2e892SBill Paul static void dc_acpi(dev)
158996f2e892SBill Paul 	device_t		dev;
159096f2e892SBill Paul {
159196f2e892SBill Paul 	int			unit;
159296f2e892SBill Paul 
159396f2e892SBill Paul 	unit = device_get_unit(dev);
159496f2e892SBill Paul 
159514a00c6cSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
159696f2e892SBill Paul 		u_int32_t		iobase, membase, irq;
159796f2e892SBill Paul 
159896f2e892SBill Paul 		/* Save important PCI config data. */
159996f2e892SBill Paul 		iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
160096f2e892SBill Paul 		membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
160196f2e892SBill Paul 		irq = pci_read_config(dev, DC_PCI_CFIT, 4);
160296f2e892SBill Paul 
160396f2e892SBill Paul 		/* Reset the power state. */
160496f2e892SBill Paul 		printf("dc%d: chip is in D%d power mode "
160514a00c6cSBill Paul 		    "-- setting to D0\n", unit,
160614a00c6cSBill Paul 		    pci_get_powerstate(dev));
160714a00c6cSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
160896f2e892SBill Paul 
160996f2e892SBill Paul 		/* Restore PCI config data. */
161096f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
161196f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
161296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFIT, irq, 4);
161396f2e892SBill Paul 	}
161414a00c6cSBill Paul 
161596f2e892SBill Paul 	return;
161696f2e892SBill Paul }
161796f2e892SBill Paul 
16185c1cfac4SBill Paul static void dc_apply_fixup(sc, media)
16195c1cfac4SBill Paul 	struct dc_softc		*sc;
16205c1cfac4SBill Paul 	int			media;
16215c1cfac4SBill Paul {
16225c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16235c1cfac4SBill Paul 	u_int8_t		*p;
16245c1cfac4SBill Paul 	int			i;
16255d801891SBill Paul 	u_int32_t		reg;
16265c1cfac4SBill Paul 
16275c1cfac4SBill Paul 	m = sc->dc_mi;
16285c1cfac4SBill Paul 
16295c1cfac4SBill Paul 	while (m != NULL) {
16305c1cfac4SBill Paul 		if (m->dc_media == media)
16315c1cfac4SBill Paul 			break;
16325c1cfac4SBill Paul 		m = m->dc_next;
16335c1cfac4SBill Paul 	}
16345c1cfac4SBill Paul 
16355c1cfac4SBill Paul 	if (m == NULL)
16365c1cfac4SBill Paul 		return;
16375c1cfac4SBill Paul 
16385c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16395c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16405c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16415c1cfac4SBill Paul 	}
16425c1cfac4SBill Paul 
16435c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16445c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16455c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16465c1cfac4SBill Paul 	}
16475c1cfac4SBill Paul 
16485c1cfac4SBill Paul 	return;
16495c1cfac4SBill Paul }
16505c1cfac4SBill Paul 
16515c1cfac4SBill Paul static void dc_decode_leaf_sia(sc, l)
16525c1cfac4SBill Paul 	struct dc_softc		*sc;
16535c1cfac4SBill Paul 	struct dc_eblock_sia	*l;
16545c1cfac4SBill Paul {
16555c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16565c1cfac4SBill Paul 
16575c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
16583019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
16595c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
16605c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
16615c1cfac4SBill Paul 
16625c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
16635c1cfac4SBill Paul 		m->dc_media = IFM_10_T|IFM_FDX;
16645c1cfac4SBill Paul 
16655c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
16665c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
16675c1cfac4SBill Paul 
16685c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
16695c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
16705c1cfac4SBill Paul 
16715c1cfac4SBill Paul 	m->dc_gp_len = 2;
16725c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
16735c1cfac4SBill Paul 
16745c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16755c1cfac4SBill Paul 	sc->dc_mi = m;
16765c1cfac4SBill Paul 
16775c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
16785c1cfac4SBill Paul 
16795c1cfac4SBill Paul 	return;
16805c1cfac4SBill Paul }
16815c1cfac4SBill Paul 
16825c1cfac4SBill Paul static void dc_decode_leaf_sym(sc, l)
16835c1cfac4SBill Paul 	struct dc_softc		*sc;
16845c1cfac4SBill Paul 	struct dc_eblock_sym	*l;
16855c1cfac4SBill Paul {
16865c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16875c1cfac4SBill Paul 
16885c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
16893019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
16905c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16915c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16925c1cfac4SBill Paul 
16935c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16945c1cfac4SBill Paul 		m->dc_media = IFM_100_TX|IFM_FDX;
16955c1cfac4SBill Paul 
16965c1cfac4SBill Paul 	m->dc_gp_len = 2;
16975c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
16985c1cfac4SBill Paul 
16995c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17005c1cfac4SBill Paul 	sc->dc_mi = m;
17015c1cfac4SBill Paul 
17025c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
17035c1cfac4SBill Paul 
17045c1cfac4SBill Paul 	return;
17055c1cfac4SBill Paul }
17065c1cfac4SBill Paul 
17075c1cfac4SBill Paul static void dc_decode_leaf_mii(sc, l)
17085c1cfac4SBill Paul 	struct dc_softc		*sc;
17095c1cfac4SBill Paul 	struct dc_eblock_mii	*l;
17105c1cfac4SBill Paul {
17115c1cfac4SBill Paul 	u_int8_t		*p;
17125c1cfac4SBill Paul 	struct dc_mediainfo	*m;
17135c1cfac4SBill Paul 
17145c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
17153019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
17165c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17175c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17185c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17195c1cfac4SBill Paul 
17205c1cfac4SBill Paul 	p = (u_int8_t *)l;
17215c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17225c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17235c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17245c1cfac4SBill Paul 	m->dc_reset_len = *p;
17255c1cfac4SBill Paul 	p++;
17265c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17275c1cfac4SBill Paul 
17285c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17295c1cfac4SBill Paul 	sc->dc_mi = m;
17305c1cfac4SBill Paul 
17315c1cfac4SBill Paul 	return;
17325c1cfac4SBill Paul }
17335c1cfac4SBill Paul 
17345c1cfac4SBill Paul static void dc_parse_21143_srom(sc)
17355c1cfac4SBill Paul 	struct dc_softc		*sc;
17365c1cfac4SBill Paul {
17375c1cfac4SBill Paul 	struct dc_leaf_hdr	*lhdr;
17385c1cfac4SBill Paul 	struct dc_eblock_hdr	*hdr;
17395c1cfac4SBill Paul 	int			i, loff;
17405c1cfac4SBill Paul 	char			*ptr;
17415c1cfac4SBill Paul 
17425c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17435c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17445c1cfac4SBill Paul 
17455c1cfac4SBill Paul 	ptr = (char *)lhdr;
17465c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17475c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17485c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17495c1cfac4SBill Paul 		switch(hdr->dc_type) {
17505c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17515c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17525c1cfac4SBill Paul 			break;
17535c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
17545c1cfac4SBill Paul 			dc_decode_leaf_sia(sc, (struct dc_eblock_sia *)hdr);
17555c1cfac4SBill Paul 			break;
17565c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
17575c1cfac4SBill Paul 			dc_decode_leaf_sym(sc, (struct dc_eblock_sym *)hdr);
17585c1cfac4SBill Paul 			break;
17595c1cfac4SBill Paul 		default:
17605c1cfac4SBill Paul 			/* Don't care. Yet. */
17615c1cfac4SBill Paul 			break;
17625c1cfac4SBill Paul 		}
17635c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17645c1cfac4SBill Paul 		ptr++;
17655c1cfac4SBill Paul 	}
17665c1cfac4SBill Paul 
17675c1cfac4SBill Paul 	return;
17685c1cfac4SBill Paul }
17695c1cfac4SBill Paul 
177096f2e892SBill Paul /*
177196f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
177296f2e892SBill Paul  * setup and ethernet/BPF attach.
177396f2e892SBill Paul  */
177496f2e892SBill Paul static int dc_attach(dev)
177596f2e892SBill Paul 	device_t		dev;
177696f2e892SBill Paul {
1777d1ce9105SBill Paul 	int			tmp = 0;
177896f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
177996f2e892SBill Paul 	u_int32_t		command;
178096f2e892SBill Paul 	struct dc_softc		*sc;
178196f2e892SBill Paul 	struct ifnet		*ifp;
178296f2e892SBill Paul 	u_int32_t		revision;
178396f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
178496f2e892SBill Paul 
178596f2e892SBill Paul 	sc = device_get_softc(dev);
178696f2e892SBill Paul 	unit = device_get_unit(dev);
178796f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
178896f2e892SBill Paul 
178908812b39SBosko Milekic 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_DEF | MTX_RECURSE);
1790031fc810SBill Paul 	DC_LOCK(sc);
1791031fc810SBill Paul 
179296f2e892SBill Paul 	/*
179396f2e892SBill Paul 	 * Handle power management nonsense.
179496f2e892SBill Paul 	 */
179596f2e892SBill Paul 	dc_acpi(dev);
179696f2e892SBill Paul 
179796f2e892SBill Paul 	/*
179896f2e892SBill Paul 	 * Map control/status registers.
179996f2e892SBill Paul 	 */
180007f65363SBill Paul 	pci_enable_busmaster(dev);
180175ff968cSBill Paul 	pci_enable_io(dev, SYS_RES_IOPORT);
180275ff968cSBill Paul 	pci_enable_io(dev, SYS_RES_MEMORY);
1803c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
180496f2e892SBill Paul 
180596f2e892SBill Paul #ifdef DC_USEIOSPACE
180696f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
180796f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
180896f2e892SBill Paul 		error = ENXIO;
180996f2e892SBill Paul 		goto fail;
181096f2e892SBill Paul 	}
181196f2e892SBill Paul #else
181296f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
181396f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
181496f2e892SBill Paul 		error = ENXIO;
181596f2e892SBill Paul 		goto fail;
181696f2e892SBill Paul 	}
181796f2e892SBill Paul #endif
181896f2e892SBill Paul 
181996f2e892SBill Paul 	rid = DC_RID;
182096f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
182196f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
182296f2e892SBill Paul 
182396f2e892SBill Paul 	if (sc->dc_res == NULL) {
182496f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
182596f2e892SBill Paul 		error = ENXIO;
182696f2e892SBill Paul 		goto fail;
182796f2e892SBill Paul 	}
182896f2e892SBill Paul 
182996f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
183096f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
183196f2e892SBill Paul 
183296f2e892SBill Paul 	/* Allocate interrupt */
183396f2e892SBill Paul 	rid = 0;
183496f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
183596f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
183696f2e892SBill Paul 
183796f2e892SBill Paul 	if (sc->dc_irq == NULL) {
183896f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
183996f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
184096f2e892SBill Paul 		error = ENXIO;
184196f2e892SBill Paul 		goto fail;
184296f2e892SBill Paul 	}
184396f2e892SBill Paul 
1844b50c6312SJonathan Lemon 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET |
1845b50c6312SJonathan Lemon 	    (IS_MPSAFE ? INTR_MPSAFE : 0),
184696f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
184796f2e892SBill Paul 
184896f2e892SBill Paul 	if (error) {
184996f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
185096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
185196f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
185296f2e892SBill Paul 		goto fail;
185396f2e892SBill Paul 	}
185496f2e892SBill Paul 
185596f2e892SBill Paul 	/* Need this info to decide on a chip type. */
185696f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
185796f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
185896f2e892SBill Paul 
185996f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
186096f2e892SBill Paul 	case DC_DEVICEID_21143:
186196f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
186296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1863042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
18645c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
18655c1cfac4SBill Paul 		dc_read_eeprom(sc, (caddr_t)&sc->dc_srom, 0, 512, 0);
186696f2e892SBill Paul 		break;
186796f2e892SBill Paul 	case DC_DEVICEID_DM9100:
186896f2e892SBill Paul 	case DC_DEVICEID_DM9102:
186996f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1870318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_INTR_ALWAYS;
1871318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_TX_STORENFWD;
187296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
18730a46b1dcSBill Paul 		/* Increase the latency timer value. */
18740a46b1dcSBill Paul 		command = pci_read_config(dev, DC_PCI_CFLT, 4);
18750a46b1dcSBill Paul 		command &= 0xFFFF00FF;
18760a46b1dcSBill Paul 		command |= 0x00008000;
18770a46b1dcSBill Paul 		pci_write_config(dev, DC_PCI_CFLT, command, 4);
187896f2e892SBill Paul 		break;
187996f2e892SBill Paul 	case DC_DEVICEID_AL981:
188096f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
188196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
188296f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
188396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
188496f2e892SBill Paul 		break;
188596f2e892SBill Paul 	case DC_DEVICEID_AN985:
188641fced74SPeter Wemm 	case DC_DEVICEID_FE2500:
1887fa167b8eSBill Paul 	case DC_DEVICEID_EN2242:
188896f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
188996f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
189096f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
189196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
189296f2e892SBill Paul 		break;
189396f2e892SBill Paul 	case DC_DEVICEID_98713:
189496f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
189596f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
189696f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
189796f2e892SBill Paul 		}
1898318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
189996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1900318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1901318b02fdSBill Paul 		}
1902318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
190396f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
190496f2e892SBill Paul 		break;
190596f2e892SBill Paul 	case DC_DEVICEID_987x5:
19069ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
190779d11e09SBill Paul 		/*
190879d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
190979d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
191079d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
191179d11e09SBill Paul 		 * get the right number of bits out of the
191279d11e09SBill Paul 		 * CRC routine.
191379d11e09SBill Paul 		 */
191479d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
191579d11e09SBill Paul 		    revision < DC_REVISION_98725)
191679d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
191796f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
191896f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1919318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
192096f2e892SBill Paul 		break;
1921ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1922ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1923ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1924ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
1925ead7cde9SBill Paul 		break;
192696f2e892SBill Paul 	case DC_DEVICEID_82C115:
192796f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
192879d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR|DC_128BIT_HASH;
1929318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
193096f2e892SBill Paul 		break;
193196f2e892SBill Paul 	case DC_DEVICEID_82C168:
193296f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
193391cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
193496f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
193596f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
193696f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
193796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
193896f2e892SBill Paul 		break;
193996f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
194096f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
194196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
194296f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
194396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
194496f2e892SBill Paul 		break;
1945feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
1946feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
19472dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
19482dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
1949feb78939SJonathan Chen 		/*
1950feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1951feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
19522dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
1953feb78939SJonathan Chen 		 */
1954feb78939SJonathan Chen 		break;
19551af8bec7SBill Paul 	case DC_DEVICEID_RS7112:
19561af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
19571af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
19581af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19591af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19601af8bec7SBill Paul 		dc_read_eeprom(sc, (caddr_t)&sc->dc_srom, 0, 256, 0);
19611af8bec7SBill Paul 		break;
196296f2e892SBill Paul 	default:
196396f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
196496f2e892SBill Paul 		    sc->dc_info->dc_did);
196596f2e892SBill Paul 		break;
196696f2e892SBill Paul 	}
196796f2e892SBill Paul 
196896f2e892SBill Paul 	/* Save the cache line size. */
196988d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
197088d739dcSBill Paul 		sc->dc_cachesize = 0;
197188d739dcSBill Paul 	else
197288d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
197388d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
197496f2e892SBill Paul 
197596f2e892SBill Paul 	/* Reset the adapter. */
197696f2e892SBill Paul 	dc_reset(sc);
197796f2e892SBill Paul 
197896f2e892SBill Paul 	/* Take 21143 out of snooze mode */
1979feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
198096f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
198196f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
198296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
198396f2e892SBill Paul 	}
198496f2e892SBill Paul 
198596f2e892SBill Paul 	/*
198696f2e892SBill Paul 	 * Try to learn something about the supported media.
198796f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
198896f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
198996f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
199096f2e892SBill Paul 	 * Intel 21143.
199196f2e892SBill Paul 	 */
19925c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
19935c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
19945c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
199596f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
199696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
199796f2e892SBill Paul 		else
199896f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
199996f2e892SBill Paul 	} else if (!sc->dc_pmode)
200096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
200196f2e892SBill Paul 
200296f2e892SBill Paul 	/*
200396f2e892SBill Paul 	 * Get station address from the EEPROM.
200496f2e892SBill Paul 	 */
200596f2e892SBill Paul 	switch(sc->dc_type) {
200696f2e892SBill Paul 	case DC_TYPE_98713:
200796f2e892SBill Paul 	case DC_TYPE_98713A:
200896f2e892SBill Paul 	case DC_TYPE_987x5:
200996f2e892SBill Paul 	case DC_TYPE_PNICII:
201096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
201196f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
201296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
201396f2e892SBill Paul 		break;
201496f2e892SBill Paul 	case DC_TYPE_PNIC:
201596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
201696f2e892SBill Paul 		break;
201796f2e892SBill Paul 	case DC_TYPE_DM9102:
201896f2e892SBill Paul 	case DC_TYPE_21143:
201996f2e892SBill Paul 	case DC_TYPE_ASIX:
202096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
202196f2e892SBill Paul 		break;
202296f2e892SBill Paul 	case DC_TYPE_AL981:
202396f2e892SBill Paul 	case DC_TYPE_AN985:
202496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
202596f2e892SBill Paul 		break;
20261af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20271af8bec7SBill Paul 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr, 6);
20281af8bec7SBill Paul 		break;
2029feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
2030feb78939SJonathan Chen 		dc_read_eeprom(sc, (caddr_t)&eaddr, 3, 3, 0);
2031feb78939SJonathan Chen 		break;
203296f2e892SBill Paul 	default:
203396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
203496f2e892SBill Paul 		break;
203596f2e892SBill Paul 	}
203696f2e892SBill Paul 
203796f2e892SBill Paul 	/*
203896f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
203996f2e892SBill Paul 	 */
204096f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
204196f2e892SBill Paul 
204296f2e892SBill Paul 	sc->dc_unit = unit;
204396f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
204496f2e892SBill Paul 
204596f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
204696f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
204796f2e892SBill Paul 
204896f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
204996f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
205096f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
205196f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
205296f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
205396f2e892SBill Paul 		error = ENXIO;
205496f2e892SBill Paul 		goto fail;
205596f2e892SBill Paul 	}
205696f2e892SBill Paul 
205796f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
205896f2e892SBill Paul 
205996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
206096f2e892SBill Paul 	ifp->if_softc = sc;
206196f2e892SBill Paul 	ifp->if_unit = unit;
206296f2e892SBill Paul 	ifp->if_name = "dc";
2063feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
206496f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
206596f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
206696f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
206796f2e892SBill Paul 	ifp->if_output = ether_output;
206896f2e892SBill Paul 	ifp->if_start = dc_start;
206996f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
207096f2e892SBill Paul 	ifp->if_init = dc_init;
207196f2e892SBill Paul 	ifp->if_baudrate = 10000000;
207296f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
207396f2e892SBill Paul 
207496f2e892SBill Paul 	/*
20755c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
20765c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
20775c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
20785c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
20795c1cfac4SBill Paul 	 * driver instead.
208096f2e892SBill Paul 	 */
20815c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
20825c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
20835c1cfac4SBill Paul 		tmp = sc->dc_pmode;
20845c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20855c1cfac4SBill Paul 	}
20865c1cfac4SBill Paul 
208796f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
208896f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
208996f2e892SBill Paul 
209096f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
20915c1cfac4SBill Paul 		sc->dc_pmode = tmp;
20925c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
209396f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2094042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
209596f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
209696f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
209778999dd1SBill Paul 		/*
209878999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
209978999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
210078999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
210178999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
210278999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
210378999dd1SBill Paul 		 */
210478999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
210578999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
210696f2e892SBill Paul 		error = 0;
210796f2e892SBill Paul 	}
210896f2e892SBill Paul 
210996f2e892SBill Paul 	if (error) {
211096f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
211196f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
211296f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
211396f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
211496f2e892SBill Paul 		error = ENXIO;
211596f2e892SBill Paul 		goto fail;
211696f2e892SBill Paul 	}
211796f2e892SBill Paul 
2118feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
2119feb78939SJonathan Chen 		/*
2120feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
2121feb78939SJonathan Chen 		 * can talk to the MII.
2122feb78939SJonathan Chen 		 */
2123feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2124feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2125feb78939SJonathan Chen 		DELAY(10);
2126feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2127feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2128feb78939SJonathan Chen 		DELAY(10);
2129feb78939SJonathan Chen 	}
2130feb78939SJonathan Chen 
213196f2e892SBill Paul 	/*
213221b8ebd9SArchie Cobbs 	 * Call MI attach routine.
213396f2e892SBill Paul 	 */
213421b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
2135db40c1aeSDoug Ambrisko 
2136db40c1aeSDoug Ambrisko 	/*
2137db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2138db40c1aeSDoug Ambrisko 	 */
2139db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2140db40c1aeSDoug Ambrisko 
2141b50c6312SJonathan Lemon 	callout_init(&sc->dc_stat_ch, IS_MPSAFE);
214296f2e892SBill Paul 
21435c1cfac4SBill Paul #ifdef SRM_MEDIA
2144510a809eSMike Smith         sc->dc_srm_media = 0;
2145510a809eSMike Smith 
2146510a809eSMike Smith 	/* Remember the SRM console media setting */
2147510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2148510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2149510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
2150510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2151510a809eSMike Smith 		case 3:
2152510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2153510a809eSMike Smith 			break;
2154510a809eSMike Smith 		case 4:
2155510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2156510a809eSMike Smith 			break;
2157510a809eSMike Smith 		case 5:
2158510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2159510a809eSMike Smith 			break;
2160510a809eSMike Smith 		case 6:
2161510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2162510a809eSMike Smith 			break;
2163510a809eSMike Smith 		}
2164510a809eSMike Smith 		if (sc->dc_srm_media)
2165510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2166510a809eSMike Smith 	}
2167510a809eSMike Smith #endif
2168510a809eSMike Smith 
2169d1ce9105SBill Paul 	DC_UNLOCK(sc);
2170d1ce9105SBill Paul 	return(0);
2171510a809eSMike Smith 
217296f2e892SBill Paul fail:
2173d1ce9105SBill Paul 	DC_UNLOCK(sc);
2174d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
217596f2e892SBill Paul 	return(error);
217696f2e892SBill Paul }
217796f2e892SBill Paul 
217896f2e892SBill Paul static int dc_detach(dev)
217996f2e892SBill Paul 	device_t		dev;
218096f2e892SBill Paul {
218196f2e892SBill Paul 	struct dc_softc		*sc;
218296f2e892SBill Paul 	struct ifnet		*ifp;
21835c1cfac4SBill Paul 	struct dc_mediainfo	*m;
218496f2e892SBill Paul 
218596f2e892SBill Paul 	sc = device_get_softc(dev);
2186d1ce9105SBill Paul 
2187d1ce9105SBill Paul 	DC_LOCK(sc);
2188d1ce9105SBill Paul 
218996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
219096f2e892SBill Paul 
219196f2e892SBill Paul 	dc_stop(sc);
219221b8ebd9SArchie Cobbs 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
219396f2e892SBill Paul 
219496f2e892SBill Paul 	bus_generic_detach(dev);
219596f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
219696f2e892SBill Paul 
219796f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
219896f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
219996f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
220096f2e892SBill Paul 
220196f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
220296f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
220396f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
220496f2e892SBill Paul 
22055c1cfac4SBill Paul 	while(sc->dc_mi != NULL) {
22065c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
22075c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
22085c1cfac4SBill Paul 		sc->dc_mi = m;
22095c1cfac4SBill Paul 	}
22105c1cfac4SBill Paul 
2211d1ce9105SBill Paul 	DC_UNLOCK(sc);
2212d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
221396f2e892SBill Paul 
221496f2e892SBill Paul 	return(0);
221596f2e892SBill Paul }
221696f2e892SBill Paul 
221796f2e892SBill Paul /*
221896f2e892SBill Paul  * Initialize the transmit descriptors.
221996f2e892SBill Paul  */
222096f2e892SBill Paul static int dc_list_tx_init(sc)
222196f2e892SBill Paul 	struct dc_softc		*sc;
222296f2e892SBill Paul {
222396f2e892SBill Paul 	struct dc_chain_data	*cd;
222496f2e892SBill Paul 	struct dc_list_data	*ld;
222501faf54bSLuigi Rizzo 	int			i, nexti;
222696f2e892SBill Paul 
222796f2e892SBill Paul 	cd = &sc->dc_cdata;
222896f2e892SBill Paul 	ld = sc->dc_ldata;
222996f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
223001faf54bSLuigi Rizzo 		nexti = (i == (DC_TX_LIST_CNT - 1)) ? 0 : i+1;
223101faf54bSLuigi Rizzo 		ld->dc_tx_list[i].dc_next = vtophys(&ld->dc_tx_list[nexti]);
223296f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
223396f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
223496f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
223596f2e892SBill Paul 	}
223696f2e892SBill Paul 
223796f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
223896f2e892SBill Paul 
223996f2e892SBill Paul 	return(0);
224096f2e892SBill Paul }
224196f2e892SBill Paul 
224296f2e892SBill Paul 
224396f2e892SBill Paul /*
224496f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
224596f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
224696f2e892SBill Paul  * points back to the first.
224796f2e892SBill Paul  */
224896f2e892SBill Paul static int dc_list_rx_init(sc)
224996f2e892SBill Paul 	struct dc_softc		*sc;
225096f2e892SBill Paul {
225196f2e892SBill Paul 	struct dc_chain_data	*cd;
225296f2e892SBill Paul 	struct dc_list_data	*ld;
225301faf54bSLuigi Rizzo 	int			i, nexti;
225496f2e892SBill Paul 
225596f2e892SBill Paul 	cd = &sc->dc_cdata;
225696f2e892SBill Paul 	ld = sc->dc_ldata;
225796f2e892SBill Paul 
225896f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
225996f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
226096f2e892SBill Paul 			return(ENOBUFS);
226101faf54bSLuigi Rizzo 		nexti = (i == (DC_RX_LIST_CNT - 1)) ? 0 : i+1;
226201faf54bSLuigi Rizzo 		ld->dc_rx_list[i].dc_next = vtophys(&ld->dc_rx_list[nexti]);
226396f2e892SBill Paul 	}
226496f2e892SBill Paul 
226596f2e892SBill Paul 	cd->dc_rx_prod = 0;
226696f2e892SBill Paul 
226796f2e892SBill Paul 	return(0);
226896f2e892SBill Paul }
226996f2e892SBill Paul 
227096f2e892SBill Paul /*
227196f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
227296f2e892SBill Paul  */
227396f2e892SBill Paul static int dc_newbuf(sc, i, m)
227496f2e892SBill Paul 	struct dc_softc		*sc;
227596f2e892SBill Paul 	int			i;
227696f2e892SBill Paul 	struct mbuf		*m;
227796f2e892SBill Paul {
227896f2e892SBill Paul 	struct mbuf		*m_new = NULL;
227996f2e892SBill Paul 	struct dc_desc		*c;
228096f2e892SBill Paul 
228196f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
228296f2e892SBill Paul 
228396f2e892SBill Paul 	if (m == NULL) {
228496f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
228540129585SLuigi Rizzo 		if (m_new == NULL)
228696f2e892SBill Paul 			return(ENOBUFS);
228796f2e892SBill Paul 
228896f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
228996f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
229096f2e892SBill Paul 			m_freem(m_new);
229196f2e892SBill Paul 			return(ENOBUFS);
229296f2e892SBill Paul 		}
229396f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
229496f2e892SBill Paul 	} else {
229596f2e892SBill Paul 		m_new = m;
229696f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
229796f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
229896f2e892SBill Paul 	}
229996f2e892SBill Paul 
230096f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
230196f2e892SBill Paul 
230296f2e892SBill Paul 	/*
230396f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
230496f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
230596f2e892SBill Paul 	 * 82c169 chips.
230696f2e892SBill Paul 	 */
230796f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
230896f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
230996f2e892SBill Paul 
231096f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
231196f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
231296f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
231396f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
231496f2e892SBill Paul 
231596f2e892SBill Paul 	return(0);
231696f2e892SBill Paul }
231796f2e892SBill Paul 
231896f2e892SBill Paul /*
231996f2e892SBill Paul  * Grrrrr.
232096f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
232196f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
232296f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
232396f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
232496f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
232596f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
232696f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
232796f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
232896f2e892SBill Paul  *
232996f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
233096f2e892SBill Paul  * Here's what we know:
233196f2e892SBill Paul  *
233296f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
233396f2e892SBill Paul  *   descriptors uploaded.
233496f2e892SBill Paul  *
233596f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
233696f2e892SBill Paul  *   total data upload.
233796f2e892SBill Paul  *
233896f2e892SBill Paul  * - We know the size of the desired received frame because it will be
233996f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
234096f2e892SBill Paul  *
234196f2e892SBill Paul  * Here's what we do:
234296f2e892SBill Paul  *
234396f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
234496f2e892SBill Paul  *   This means that we know that the buffer contents should be all
234596f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
234696f2e892SBill Paul  *
234796f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
234896f2e892SBill Paul  *   ethernet CRC at the end.
234996f2e892SBill Paul  *
235096f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
235196f2e892SBill Paul  *
235296f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
235396f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
235496f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
235596f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
235696f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
235796f2e892SBill Paul  *   we won't be fooled.
235896f2e892SBill Paul  *
235996f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
236096f2e892SBill Paul  *   that value from the current pointer location. This brings us
236196f2e892SBill Paul  *   to the start of the actual received packet.
236296f2e892SBill Paul  *
236396f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
236496f2e892SBill Paul  *   frame length.
236596f2e892SBill Paul  *
236696f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
236796f2e892SBill Paul  * the time.
236896f2e892SBill Paul  */
236996f2e892SBill Paul 
237096f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
237196f2e892SBill Paul static void dc_pnic_rx_bug_war(sc, idx)
237296f2e892SBill Paul 	struct dc_softc		*sc;
237396f2e892SBill Paul 	int			idx;
237496f2e892SBill Paul {
237596f2e892SBill Paul 	struct dc_desc		*cur_rx;
237696f2e892SBill Paul 	struct dc_desc		*c = NULL;
237796f2e892SBill Paul 	struct mbuf		*m = NULL;
237896f2e892SBill Paul 	unsigned char		*ptr;
237996f2e892SBill Paul 	int			i, total_len;
238096f2e892SBill Paul 	u_int32_t		rxstat = 0;
238196f2e892SBill Paul 
238296f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
238396f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
238496f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
238596f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
238696f2e892SBill Paul 
238796f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
238896f2e892SBill Paul 	while (1) {
238996f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
239096f2e892SBill Paul 		rxstat = c->dc_status;
239196f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
239296f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
239396f2e892SBill Paul 		ptr += DC_RXLEN;
239496f2e892SBill Paul 		/* If this is the last buffer, break out. */
239596f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
239696f2e892SBill Paul 			break;
239796f2e892SBill Paul 		dc_newbuf(sc, i, m);
239896f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
239996f2e892SBill Paul 	}
240096f2e892SBill Paul 
240196f2e892SBill Paul 	/* Find the length of the actual receive frame. */
240296f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
240396f2e892SBill Paul 
240496f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
240596f2e892SBill Paul 	while(*ptr == 0x00)
240696f2e892SBill Paul 		ptr--;
240796f2e892SBill Paul 
240896f2e892SBill Paul 	/* Round off. */
240996f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
241096f2e892SBill Paul 		ptr -= 1;
241196f2e892SBill Paul 
241296f2e892SBill Paul 	/* Now find the start of the frame. */
241396f2e892SBill Paul 	ptr -= total_len;
241496f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
241596f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
241696f2e892SBill Paul 
241796f2e892SBill Paul 	/*
241896f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
241996f2e892SBill Paul 	 * the status word to make it look like a successful
242096f2e892SBill Paul  	 * frame reception.
242196f2e892SBill Paul 	 */
242296f2e892SBill Paul 	dc_newbuf(sc, i, m);
242396f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
242496f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
242596f2e892SBill Paul 
242696f2e892SBill Paul 	return;
242796f2e892SBill Paul }
242896f2e892SBill Paul 
242996f2e892SBill Paul /*
243073bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
243173bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
243273bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
243373bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
243473bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
243573bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
243673bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
243773bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
243873bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
243973bf949cSBill Paul  */
244073bf949cSBill Paul static int dc_rx_resync(sc)
244173bf949cSBill Paul 	struct dc_softc		*sc;
244273bf949cSBill Paul {
244373bf949cSBill Paul 	int			i, pos;
244473bf949cSBill Paul 	struct dc_desc		*cur_rx;
244573bf949cSBill Paul 
244673bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
244773bf949cSBill Paul 
244873bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
244973bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
245073bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
245173bf949cSBill Paul 			break;
245273bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
245373bf949cSBill Paul 	}
245473bf949cSBill Paul 
245573bf949cSBill Paul 	/* If the ring really is empty, then just return. */
245673bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
245773bf949cSBill Paul 		return(0);
245873bf949cSBill Paul 
245973bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
246073bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
246173bf949cSBill Paul 
246273bf949cSBill Paul 	return(EAGAIN);
246373bf949cSBill Paul }
246473bf949cSBill Paul 
246573bf949cSBill Paul /*
246696f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
246796f2e892SBill Paul  * the higher level protocols.
246896f2e892SBill Paul  */
246996f2e892SBill Paul static void dc_rxeof(sc)
247096f2e892SBill Paul 	struct dc_softc		*sc;
247196f2e892SBill Paul {
247296f2e892SBill Paul         struct ether_header	*eh;
247396f2e892SBill Paul         struct mbuf		*m;
247496f2e892SBill Paul         struct ifnet		*ifp;
247596f2e892SBill Paul 	struct dc_desc		*cur_rx;
247696f2e892SBill Paul 	int			i, total_len = 0;
247796f2e892SBill Paul 	u_int32_t		rxstat;
247896f2e892SBill Paul 
247996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
248096f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
248196f2e892SBill Paul 
248296f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
248396f2e892SBill Paul 
2484e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2485e4fc250cSLuigi Rizzo 		if (ifp->if_ipending & IFF_POLLING) {
2486e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2487e4fc250cSLuigi Rizzo 				break;
2488e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2489e4fc250cSLuigi Rizzo 		}
2490e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
249196f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
249296f2e892SBill Paul 		rxstat = cur_rx->dc_status;
249396f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
249496f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
249596f2e892SBill Paul 
249696f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
249796f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
249896f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
249996f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
250096f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
250196f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
250296f2e892SBill Paul 					continue;
250396f2e892SBill Paul 				}
250496f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
250596f2e892SBill Paul 				rxstat = cur_rx->dc_status;
250696f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
250796f2e892SBill Paul 			}
250896f2e892SBill Paul 		}
250996f2e892SBill Paul 
251096f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
251196f2e892SBill Paul 
251296f2e892SBill Paul 		/*
251396f2e892SBill Paul 		 * If an error occurs, update stats, clear the
251496f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
251596f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2516db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
2517db40c1aeSDoug Ambrisko 		 * frames as errors since they could be vlans
251896f2e892SBill Paul 		 */
2519db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)){
2520db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2521db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2522db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2523db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
252496f2e892SBill Paul 				ifp->if_ierrors++;
252596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
252696f2e892SBill Paul 					ifp->if_collisions++;
252796f2e892SBill Paul 				dc_newbuf(sc, i, m);
252896f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
252996f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
253096f2e892SBill Paul 					continue;
253196f2e892SBill Paul 				} else {
253296f2e892SBill Paul 					dc_init(sc);
253396f2e892SBill Paul 					return;
253496f2e892SBill Paul 				}
253596f2e892SBill Paul 			}
2536db40c1aeSDoug Ambrisko 		}
253796f2e892SBill Paul 
253896f2e892SBill Paul 		/* No errors; receive the packet. */
253996f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
254001faf54bSLuigi Rizzo #ifdef __i386__
254101faf54bSLuigi Rizzo 		/*
254201faf54bSLuigi Rizzo 		 * On the x86 we do not have alignment problems, so try to
254301faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
254401faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
254501faf54bSLuigi Rizzo 		 * copy done in m_devget().
254601faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
254701faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
254801faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
254901faf54bSLuigi Rizzo 		 */
255001faf54bSLuigi Rizzo 		if (dc_quick && dc_newbuf(sc, i, NULL) == 0) {
255101faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
255201faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
255301faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
255401faf54bSLuigi Rizzo 		} else
255501faf54bSLuigi Rizzo #endif
255601faf54bSLuigi Rizzo 		{
255701faf54bSLuigi Rizzo 			struct mbuf *m0;
255896f2e892SBill Paul 
255901faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
256001faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
256196f2e892SBill Paul 			dc_newbuf(sc, i, m);
256296f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
256396f2e892SBill Paul 			if (m0 == NULL) {
256496f2e892SBill Paul 				ifp->if_ierrors++;
256596f2e892SBill Paul 				continue;
256696f2e892SBill Paul 			}
256796f2e892SBill Paul 			m = m0;
256801faf54bSLuigi Rizzo 		}
256996f2e892SBill Paul 
257096f2e892SBill Paul 		ifp->if_ipackets++;
257196f2e892SBill Paul 		eh = mtod(m, struct ether_header *);
257296f2e892SBill Paul 
257396f2e892SBill Paul 		/* Remove header from mbuf and pass it on. */
257496f2e892SBill Paul 		m_adj(m, sizeof(struct ether_header));
257596f2e892SBill Paul 		ether_input(ifp, eh, m);
257696f2e892SBill Paul 	}
257796f2e892SBill Paul 
257896f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
257996f2e892SBill Paul }
258096f2e892SBill Paul 
258196f2e892SBill Paul /*
258296f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
258396f2e892SBill Paul  * the list buffers.
258496f2e892SBill Paul  */
258596f2e892SBill Paul 
258696f2e892SBill Paul static void dc_txeof(sc)
258796f2e892SBill Paul 	struct dc_softc		*sc;
258896f2e892SBill Paul {
258996f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
259096f2e892SBill Paul 	struct ifnet		*ifp;
259196f2e892SBill Paul 	int			idx;
259296f2e892SBill Paul 
259396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
259496f2e892SBill Paul 
259596f2e892SBill Paul 	/* Clear the timeout timer. */
259696f2e892SBill Paul 	ifp->if_timer = 0;
259796f2e892SBill Paul 
259896f2e892SBill Paul 	/*
259996f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
260096f2e892SBill Paul 	 * frames that have been transmitted.
260196f2e892SBill Paul 	 */
260296f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
260396f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
260496f2e892SBill Paul 		u_int32_t		txstat;
260596f2e892SBill Paul 
260696f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
260796f2e892SBill Paul 		txstat = cur_tx->dc_status;
260896f2e892SBill Paul 
260996f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
261096f2e892SBill Paul 			break;
261196f2e892SBill Paul 
261296f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
261396f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
261496f2e892SBill Paul 			sc->dc_cdata.dc_tx_cnt--;
261596f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
261696f2e892SBill Paul 				/*
261796f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
261896f2e892SBill Paul 				 * that it will sometimes generate a TX
261996f2e892SBill Paul 				 * underrun error while DMAing the RX
262096f2e892SBill Paul 				 * filter setup frame. If we detect this,
262196f2e892SBill Paul 				 * we have to send the setup frame again,
262296f2e892SBill Paul 				 * or else the filter won't be programmed
262396f2e892SBill Paul 				 * correctly.
262496f2e892SBill Paul 				 */
262596f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
262696f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
262796f2e892SBill Paul 						dc_setfilt(sc);
262896f2e892SBill Paul 				}
262996f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
263096f2e892SBill Paul 			}
263196f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
263296f2e892SBill Paul 			continue;
263396f2e892SBill Paul 		}
263496f2e892SBill Paul 
263529a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2636feb78939SJonathan Chen 			/*
2637feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2638feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
263929a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
264029a2220aSBill Paul 			 * Who knows, but Conexant chips have the
264129a2220aSBill Paul 			 * same problem. Maybe they took lessons
264229a2220aSBill Paul 			 * from Xircom.
264329a2220aSBill Paul 			 */
2644feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2645feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2646feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
2647feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2648feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2649feb78939SJonathan Chen 		} else {
265096f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
265196f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
265296f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
265396f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
265496f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2655feb78939SJonathan Chen 		}
265696f2e892SBill Paul 
265796f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
265896f2e892SBill Paul 			ifp->if_oerrors++;
265996f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
266096f2e892SBill Paul 				ifp->if_collisions++;
266196f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
266296f2e892SBill Paul 				ifp->if_collisions++;
266396f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
266496f2e892SBill Paul 				dc_init(sc);
266596f2e892SBill Paul 				return;
266696f2e892SBill Paul 			}
266796f2e892SBill Paul 		}
266896f2e892SBill Paul 
266996f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
267096f2e892SBill Paul 
267196f2e892SBill Paul 		ifp->if_opackets++;
267296f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
267396f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
267496f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
267596f2e892SBill Paul 		}
267696f2e892SBill Paul 
267796f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
267896f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
267996f2e892SBill Paul 	}
268096f2e892SBill Paul 
268196f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
268296f2e892SBill Paul 	if (cur_tx != NULL)
268396f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
268496f2e892SBill Paul 
268596f2e892SBill Paul 	return;
268696f2e892SBill Paul }
268796f2e892SBill Paul 
268896f2e892SBill Paul static void dc_tick(xsc)
268996f2e892SBill Paul 	void			*xsc;
269096f2e892SBill Paul {
269196f2e892SBill Paul 	struct dc_softc		*sc;
269296f2e892SBill Paul 	struct mii_data		*mii;
269396f2e892SBill Paul 	struct ifnet		*ifp;
269496f2e892SBill Paul 	u_int32_t		r;
269596f2e892SBill Paul 
269696f2e892SBill Paul 	sc = xsc;
2697d1ce9105SBill Paul 	DC_LOCK(sc);
269896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
269996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
270096f2e892SBill Paul 
270196f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2702318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2703318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2704318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2705318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
270696f2e892SBill Paul 				sc->dc_link = 0;
2707318b02fdSBill Paul 				mii_mediachg(mii);
2708318b02fdSBill Paul 			}
2709318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2710318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2711318b02fdSBill Paul 				sc->dc_link = 0;
2712318b02fdSBill Paul 				mii_mediachg(mii);
2713318b02fdSBill Paul 			}
2714d675147eSBill Paul 			if (sc->dc_link == 0)
271596f2e892SBill Paul 				mii_tick(mii);
271696f2e892SBill Paul 		} else {
2717318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
271896f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2719042c8f6eSBill Paul 			    sc->dc_cdata.dc_tx_cnt == 0)
272096f2e892SBill Paul 				mii_tick(mii);
2721042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2722042c8f6eSBill Paul 					sc->dc_link = 0;
272396f2e892SBill Paul 		}
272496f2e892SBill Paul 	} else
272596f2e892SBill Paul 		mii_tick(mii);
272696f2e892SBill Paul 
272796f2e892SBill Paul 	/*
272896f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
272996f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
273096f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
273196f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
273296f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
273396f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
273496f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
273596f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
273696f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
273796f2e892SBill Paul 	 * a screeching halt for several seconds.
273896f2e892SBill Paul 	 *
273996f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
274096f2e892SBill Paul 	 * any packets until a link has been established. After the
274196f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
274296f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
274396f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
274496f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
274596f2e892SBill Paul 	 */
2746cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
274796f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
274896f2e892SBill Paul 		sc->dc_link++;
274996f2e892SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
275096f2e892SBill Paul 			dc_start(ifp);
275196f2e892SBill Paul 	}
275296f2e892SBill Paul 
2753318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2754b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2755318b02fdSBill Paul 	else
2756b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
275796f2e892SBill Paul 
2758d1ce9105SBill Paul 	DC_UNLOCK(sc);
275996f2e892SBill Paul 
276096f2e892SBill Paul 	return;
276196f2e892SBill Paul }
276296f2e892SBill Paul 
2763d467c136SBill Paul /*
2764d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2765d467c136SBill Paul  * or switch to store and forward mode if we have to.
2766d467c136SBill Paul  */
2767d467c136SBill Paul static void dc_tx_underrun(sc)
2768d467c136SBill Paul 	struct dc_softc		*sc;
2769d467c136SBill Paul {
2770d467c136SBill Paul 	u_int32_t		isr;
2771d467c136SBill Paul 	int			i;
2772d467c136SBill Paul 
2773d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
2774d467c136SBill Paul 		dc_init(sc);
2775d467c136SBill Paul 
2776d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
2777d467c136SBill Paul 		/*
2778d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
2779d467c136SBill Paul 		 * in order to change the transmit threshold or store
2780d467c136SBill Paul 		 * and forward state.
2781d467c136SBill Paul 		 */
2782d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2783d467c136SBill Paul 
2784d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
2785d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
2786d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
2787d467c136SBill Paul 				break;
2788d467c136SBill Paul 			DELAY(10);
2789d467c136SBill Paul 		}
2790d467c136SBill Paul 		if (i == DC_TIMEOUT) {
2791d467c136SBill Paul 			printf("dc%d: failed to force tx to idle state\n",
2792d467c136SBill Paul 			    sc->dc_unit);
2793d467c136SBill Paul 			dc_init(sc);
2794d467c136SBill Paul 		}
2795d467c136SBill Paul 	}
2796d467c136SBill Paul 
2797d467c136SBill Paul 	printf("dc%d: TX underrun -- ", sc->dc_unit);
2798d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
2799d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2800d467c136SBill Paul 		printf("using store and forward mode\n");
2801d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2802d467c136SBill Paul 	} else {
2803d467c136SBill Paul 		printf("increasing TX threshold\n");
2804d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2805d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2806d467c136SBill Paul 	}
2807d467c136SBill Paul 
2808d467c136SBill Paul 	if (DC_IS_INTEL(sc))
2809d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2810d467c136SBill Paul 
2811d467c136SBill Paul 	return;
2812d467c136SBill Paul }
2813d467c136SBill Paul 
2814e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2815e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
2816e4fc250cSLuigi Rizzo 
2817e4fc250cSLuigi Rizzo static void
2818e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2819e4fc250cSLuigi Rizzo {
2820e4fc250cSLuigi Rizzo 	struct	dc_softc *sc = ifp->if_softc;
2821e4fc250cSLuigi Rizzo 
2822e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
2823e4fc250cSLuigi Rizzo 		/* Re-enable interrupts. */
2824e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
2825e4fc250cSLuigi Rizzo 		return;
2826e4fc250cSLuigi Rizzo 	}
2827e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
2828e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
2829e4fc250cSLuigi Rizzo 	dc_txeof(sc);
2830e4fc250cSLuigi Rizzo 	if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE))
2831e4fc250cSLuigi Rizzo 		dc_start(ifp);
2832e4fc250cSLuigi Rizzo 
2833e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2834e4fc250cSLuigi Rizzo 		u_int32_t	status;
2835e4fc250cSLuigi Rizzo 
2836e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
2837e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF|
2838e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF|DC_ISR_TX_IDLE|DC_ISR_TX_UNDERRUN|
2839e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
2840e4fc250cSLuigi Rizzo 		if (!status)
2841e4fc250cSLuigi Rizzo 			return;
2842e4fc250cSLuigi Rizzo 		/* ack what we have */
2843e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
2844e4fc250cSLuigi Rizzo 
2845e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF)) {
2846e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
2847e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
2848e4fc250cSLuigi Rizzo 
2849e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
2850e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
2851e4fc250cSLuigi Rizzo 		}
2852e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
2853e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
2854e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
2855e4fc250cSLuigi Rizzo 
2856e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
2857e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
2858e4fc250cSLuigi Rizzo 
2859e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
2860e4fc250cSLuigi Rizzo 			printf("dc_poll: dc%d bus error\n", sc->dc_unit);
2861e4fc250cSLuigi Rizzo 			dc_reset(sc);
2862e4fc250cSLuigi Rizzo 			dc_init(sc);
2863e4fc250cSLuigi Rizzo 		}
2864e4fc250cSLuigi Rizzo 	}
2865e4fc250cSLuigi Rizzo }
2866e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
2867e4fc250cSLuigi Rizzo 
286896f2e892SBill Paul static void dc_intr(arg)
286996f2e892SBill Paul 	void			*arg;
287096f2e892SBill Paul {
287196f2e892SBill Paul 	struct dc_softc		*sc;
287296f2e892SBill Paul 	struct ifnet		*ifp;
287396f2e892SBill Paul 	u_int32_t		status;
287496f2e892SBill Paul 
287596f2e892SBill Paul 	sc = arg;
2876d2a1864bSWarner Losh 
2877d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
2878d2a1864bSWarner Losh 		return;
2879d2a1864bSWarner Losh 
2880d1ce9105SBill Paul 	DC_LOCK(sc);
288196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
2882e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2883e4fc250cSLuigi Rizzo 	if (ifp->if_ipending & IFF_POLLING)
2884e4fc250cSLuigi Rizzo 		goto done;
2885e4fc250cSLuigi Rizzo 	if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */
2886e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
2887e4fc250cSLuigi Rizzo 		goto done;
2888e4fc250cSLuigi Rizzo 	}
2889e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
289096f2e892SBill Paul 
2891d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
289296f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
289396f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
289496f2e892SBill Paul 			dc_stop(sc);
2895d1ce9105SBill Paul 		DC_UNLOCK(sc);
289696f2e892SBill Paul 		return;
289796f2e892SBill Paul 	}
289896f2e892SBill Paul 
289996f2e892SBill Paul 	/* Disable interrupts. */
290096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
290196f2e892SBill Paul 
2902feb78939SJonathan Chen 	while(((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
2903feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
290496f2e892SBill Paul 
290596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
290696f2e892SBill Paul 
290773bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
290873bf949cSBill Paul 			int		curpkts;
290973bf949cSBill Paul 			curpkts = ifp->if_ipackets;
291096f2e892SBill Paul 			dc_rxeof(sc);
291173bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
291273bf949cSBill Paul 				while(dc_rx_resync(sc))
291373bf949cSBill Paul 					dc_rxeof(sc);
291473bf949cSBill Paul 			}
291573bf949cSBill Paul 		}
291696f2e892SBill Paul 
291796f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
291896f2e892SBill Paul 			dc_txeof(sc);
291996f2e892SBill Paul 
292096f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
292196f2e892SBill Paul 			dc_txeof(sc);
292296f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
292396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
292496f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
292596f2e892SBill Paul 			}
292696f2e892SBill Paul 		}
292796f2e892SBill Paul 
2928d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
2929d467c136SBill Paul 			dc_tx_underrun(sc);
293096f2e892SBill Paul 
293196f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
293273bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
293373bf949cSBill Paul 			int		curpkts;
293473bf949cSBill Paul 			curpkts = ifp->if_ipackets;
293596f2e892SBill Paul 			dc_rxeof(sc);
293673bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
293773bf949cSBill Paul 				while(dc_rx_resync(sc))
293873bf949cSBill Paul 					dc_rxeof(sc);
293973bf949cSBill Paul 			}
294073bf949cSBill Paul 		}
294196f2e892SBill Paul 
294296f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
294396f2e892SBill Paul 			dc_reset(sc);
294496f2e892SBill Paul 			dc_init(sc);
294596f2e892SBill Paul 		}
294696f2e892SBill Paul 	}
294796f2e892SBill Paul 
294896f2e892SBill Paul 	/* Re-enable interrupts. */
294996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
295096f2e892SBill Paul 
295196f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
295296f2e892SBill Paul 		dc_start(ifp);
295396f2e892SBill Paul 
2954d9700bb5SBill Paul #ifdef DEVICE_POLLING
2955e4fc250cSLuigi Rizzo done:
2956d9700bb5SBill Paul #endif /* DEVICE_POLLING */
2957d9700bb5SBill Paul 
2958d1ce9105SBill Paul 	DC_UNLOCK(sc);
2959d1ce9105SBill Paul 
296096f2e892SBill Paul 	return;
296196f2e892SBill Paul }
296296f2e892SBill Paul 
296396f2e892SBill Paul /*
296496f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
296596f2e892SBill Paul  * pointers to the fragment pointers.
296696f2e892SBill Paul  */
296796f2e892SBill Paul static int dc_encap(sc, m_head, txidx)
296896f2e892SBill Paul 	struct dc_softc		*sc;
296996f2e892SBill Paul 	struct mbuf		*m_head;
297096f2e892SBill Paul 	u_int32_t		*txidx;
297196f2e892SBill Paul {
297296f2e892SBill Paul 	struct dc_desc		*f = NULL;
297396f2e892SBill Paul 	struct mbuf		*m;
297496f2e892SBill Paul 	int			frag, cur, cnt = 0;
297596f2e892SBill Paul 
297696f2e892SBill Paul 	/*
297796f2e892SBill Paul  	 * Start packing the mbufs in this chain into
297896f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
297996f2e892SBill Paul  	 * of fragments or hit the end of the mbuf chain.
298096f2e892SBill Paul 	 */
298196f2e892SBill Paul 	m = m_head;
298296f2e892SBill Paul 	cur = frag = *txidx;
298396f2e892SBill Paul 
298496f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
298596f2e892SBill Paul 		if (m->m_len != 0) {
298696f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
298796f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
298896f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
298996f2e892SBill Paul 					return(ENOBUFS);
299096f2e892SBill Paul 			}
299196f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
299296f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
299396f2e892SBill Paul 				return(ENOBUFS);
299496f2e892SBill Paul 
299596f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
299696f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
299796f2e892SBill Paul 			if (cnt == 0) {
299896f2e892SBill Paul 				f->dc_status = 0;
299996f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
300096f2e892SBill Paul 			} else
300196f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
300296f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
300396f2e892SBill Paul 			cur = frag;
300496f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
300596f2e892SBill Paul 			cnt++;
300696f2e892SBill Paul 		}
300796f2e892SBill Paul 	}
300896f2e892SBill Paul 
300996f2e892SBill Paul 	if (m != NULL)
301096f2e892SBill Paul 		return(ENOBUFS);
301196f2e892SBill Paul 
301296f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
301396f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
301496f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
301596f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
301696f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
301791cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
301891cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
301996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
302096f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
302196f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
302296f2e892SBill Paul 	*txidx = frag;
302396f2e892SBill Paul 
302496f2e892SBill Paul 	return(0);
302596f2e892SBill Paul }
302696f2e892SBill Paul 
302796f2e892SBill Paul /*
3028fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
3029fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
3030fda39fd0SBill Paul  * do scatter/gather correctly.
3031fda39fd0SBill Paul  */
3032fda39fd0SBill Paul static int dc_coal(sc, m_head)
3033fda39fd0SBill Paul 	struct dc_softc		*sc;
3034fda39fd0SBill Paul 	struct mbuf		**m_head;
3035fda39fd0SBill Paul {
3036fda39fd0SBill Paul         struct mbuf		*m_new, *m;
3037fda39fd0SBill Paul 
3038fda39fd0SBill Paul 	m = *m_head;
3039fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
304040129585SLuigi Rizzo 	if (m_new == NULL)
3041fda39fd0SBill Paul 		return(ENOBUFS);
3042fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
3043fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
3044fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
3045fda39fd0SBill Paul 			m_freem(m_new);
3046fda39fd0SBill Paul 			return(ENOBUFS);
3047fda39fd0SBill Paul 		}
3048fda39fd0SBill Paul 	}
3049fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
3050fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
3051fda39fd0SBill Paul 	m_freem(m);
3052fda39fd0SBill Paul 	*m_head = m_new;
3053fda39fd0SBill Paul 
3054fda39fd0SBill Paul 	return(0);
3055fda39fd0SBill Paul }
3056fda39fd0SBill Paul 
3057fda39fd0SBill Paul /*
305896f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
305996f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
306096f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
306196f2e892SBill Paul  * physical addresses.
306296f2e892SBill Paul  */
306396f2e892SBill Paul 
306496f2e892SBill Paul static void dc_start(ifp)
306596f2e892SBill Paul 	struct ifnet		*ifp;
306696f2e892SBill Paul {
306796f2e892SBill Paul 	struct dc_softc		*sc;
306896f2e892SBill Paul 	struct mbuf		*m_head = NULL;
306996f2e892SBill Paul 	int			idx;
307096f2e892SBill Paul 
307196f2e892SBill Paul 	sc = ifp->if_softc;
307296f2e892SBill Paul 
3073d1ce9105SBill Paul 	DC_LOCK(sc);
307496f2e892SBill Paul 
3075e7be9f9aSBill Paul 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10) {
3076d1ce9105SBill Paul 		DC_UNLOCK(sc);
307796f2e892SBill Paul 		return;
3078d1ce9105SBill Paul 	}
3079d1ce9105SBill Paul 
3080d1ce9105SBill Paul 	if (ifp->if_flags & IFF_OACTIVE) {
3081d1ce9105SBill Paul 		DC_UNLOCK(sc);
3082d1ce9105SBill Paul 		return;
3083d1ce9105SBill Paul 	}
308496f2e892SBill Paul 
308596f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
308696f2e892SBill Paul 
308796f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
308896f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
308996f2e892SBill Paul 		if (m_head == NULL)
309096f2e892SBill Paul 			break;
309196f2e892SBill Paul 
30922dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
30932dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
30942dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3095fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
3096fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
3097fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
3098fda39fd0SBill Paul 				break;
3099fda39fd0SBill Paul 			}
3100fda39fd0SBill Paul 		}
3101fda39fd0SBill Paul 
310296f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
310396f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
310496f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
310596f2e892SBill Paul 			break;
310696f2e892SBill Paul 		}
310796f2e892SBill Paul 
310896f2e892SBill Paul 		/*
310996f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
311096f2e892SBill Paul 		 * to him.
311196f2e892SBill Paul 		 */
311296f2e892SBill Paul 		if (ifp->if_bpf)
311396f2e892SBill Paul 			bpf_mtap(ifp, m_head);
31145c1cfac4SBill Paul 
31155c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
31165c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
31175c1cfac4SBill Paul 			break;
31185c1cfac4SBill Paul 		}
311996f2e892SBill Paul 	}
312096f2e892SBill Paul 
312196f2e892SBill Paul 	/* Transmit */
312296f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
312396f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
312496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
312596f2e892SBill Paul 
312696f2e892SBill Paul 	/*
312796f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
312896f2e892SBill Paul 	 */
312996f2e892SBill Paul 	ifp->if_timer = 5;
313096f2e892SBill Paul 
3131d1ce9105SBill Paul 	DC_UNLOCK(sc);
3132d1ce9105SBill Paul 
313396f2e892SBill Paul 	return;
313496f2e892SBill Paul }
313596f2e892SBill Paul 
313696f2e892SBill Paul static void dc_init(xsc)
313796f2e892SBill Paul 	void			*xsc;
313896f2e892SBill Paul {
313996f2e892SBill Paul 	struct dc_softc		*sc = xsc;
314096f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
314196f2e892SBill Paul 	struct mii_data		*mii;
314296f2e892SBill Paul 
3143d1ce9105SBill Paul 	DC_LOCK(sc);
314496f2e892SBill Paul 
314596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
314696f2e892SBill Paul 
314796f2e892SBill Paul 	/*
314896f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
314996f2e892SBill Paul 	 */
315096f2e892SBill Paul 	dc_stop(sc);
315196f2e892SBill Paul 	dc_reset(sc);
315296f2e892SBill Paul 
315396f2e892SBill Paul 	/*
315496f2e892SBill Paul 	 * Set cache alignment and burst length.
315596f2e892SBill Paul 	 */
315688d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
315796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
315896f2e892SBill Paul 	else
315996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
3160935fe010SLuigi Rizzo 	/*
3161935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3162935fe010SLuigi Rizzo 	 */
3163935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3164935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
316596f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
316696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
316796f2e892SBill Paul 	} else {
316896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
316996f2e892SBill Paul 	}
317096f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
317196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
317296f2e892SBill Paul 	switch(sc->dc_cachesize) {
317396f2e892SBill Paul 	case 32:
317496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
317596f2e892SBill Paul 		break;
317696f2e892SBill Paul 	case 16:
317796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
317896f2e892SBill Paul 		break;
317996f2e892SBill Paul 	case 8:
318096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
318196f2e892SBill Paul 		break;
318296f2e892SBill Paul 	case 0:
318396f2e892SBill Paul 	default:
318496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
318596f2e892SBill Paul 		break;
318696f2e892SBill Paul 	}
318796f2e892SBill Paul 
318896f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
318996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
319096f2e892SBill Paul 	else {
3191d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
319296f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
319396f2e892SBill Paul 		} else {
319496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
319596f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
319696f2e892SBill Paul 		}
319796f2e892SBill Paul 	}
319896f2e892SBill Paul 
319996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
320096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
320196f2e892SBill Paul 
320296f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
320396f2e892SBill Paul 		/*
320496f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
320596f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
320696f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
320796f2e892SBill Paul 		 * document the meaning of these bits so there's no way
320896f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
320996f2e892SBill Paul 		 * number all its own; the rest all use a different one.
321096f2e892SBill Paul 		 */
321196f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
321296f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
321396f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
321496f2e892SBill Paul 		else
321596f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
321696f2e892SBill Paul 	}
321796f2e892SBill Paul 
3218feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3219feb78939SJonathan Chen 		/*
3220feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3221feb78939SJonathan Chen 		 * can talk to the MII.
3222feb78939SJonathan Chen 		 */
3223feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3224feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3225feb78939SJonathan Chen 		DELAY(10);
3226feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3227feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3228feb78939SJonathan Chen 		DELAY(10);
3229feb78939SJonathan Chen 	}
3230feb78939SJonathan Chen 
323196f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3232d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
323396f2e892SBill Paul 
323496f2e892SBill Paul 	/* Init circular RX list. */
323596f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
323696f2e892SBill Paul 		printf("dc%d: initialization failed: no "
323796f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
323896f2e892SBill Paul 		dc_stop(sc);
3239d1ce9105SBill Paul 		DC_UNLOCK(sc);
324096f2e892SBill Paul 		return;
324196f2e892SBill Paul 	}
324296f2e892SBill Paul 
324396f2e892SBill Paul 	/*
324496f2e892SBill Paul 	 * Init tx descriptors.
324596f2e892SBill Paul 	 */
324696f2e892SBill Paul 	dc_list_tx_init(sc);
324796f2e892SBill Paul 
324896f2e892SBill Paul 	/*
324996f2e892SBill Paul 	 * Load the address of the RX list.
325096f2e892SBill Paul 	 */
325196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
325296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
325396f2e892SBill Paul 
325496f2e892SBill Paul 	/*
325596f2e892SBill Paul 	 * Enable interrupts.
325696f2e892SBill Paul 	 */
3257e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3258e4fc250cSLuigi Rizzo 	/*
3259e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3260e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3261e4fc250cSLuigi Rizzo 	 * after a reset.
3262e4fc250cSLuigi Rizzo 	 */
3263e4fc250cSLuigi Rizzo 	if (ifp->if_ipending & IFF_POLLING)
3264e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3265e4fc250cSLuigi Rizzo 	else
3266e4fc250cSLuigi Rizzo #endif
326796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
326896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
326996f2e892SBill Paul 
327096f2e892SBill Paul 	/* Enable transmitter. */
327196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
327296f2e892SBill Paul 
327396f2e892SBill Paul 	/*
3274918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3275918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3276918434c8SBill Paul 	 * link and activity indications.
3277918434c8SBill Paul 	 */
327878999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3279918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3280918434c8SBill Paul 		    DC_WDOG_CTLWREN|DC_WDOG_LINK|DC_WDOG_ACTIVITY);
328178999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3282918434c8SBill Paul 	}
3283918434c8SBill Paul 
3284918434c8SBill Paul 	/*
328596f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
328696f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
328796f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
328896f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
328996f2e892SBill Paul 	 */
329096f2e892SBill Paul 	dc_setfilt(sc);
329196f2e892SBill Paul 
329296f2e892SBill Paul 	/* Enable receiver. */
329396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
329496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
329596f2e892SBill Paul 
329696f2e892SBill Paul 	mii_mediachg(mii);
329796f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
329896f2e892SBill Paul 
329996f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
330096f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
330196f2e892SBill Paul 
3302857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
3303857fd445SBill Paul 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_homePNA)
3304857fd445SBill Paul 		sc->dc_link = 1;
3305857fd445SBill Paul 	else {
3306318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3307b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3308318b02fdSBill Paul 		else
3309b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3310857fd445SBill Paul 	}
331196f2e892SBill Paul 
33125c1cfac4SBill Paul #ifdef SRM_MEDIA
3313510a809eSMike Smith         if(sc->dc_srm_media) {
3314510a809eSMike Smith 		struct ifreq ifr;
3315510a809eSMike Smith 
3316510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
3317510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3318510a809eSMike Smith 		sc->dc_srm_media = 0;
3319510a809eSMike Smith 	}
3320510a809eSMike Smith #endif
3321d1ce9105SBill Paul 	DC_UNLOCK(sc);
332296f2e892SBill Paul 	return;
332396f2e892SBill Paul }
332496f2e892SBill Paul 
332596f2e892SBill Paul /*
332696f2e892SBill Paul  * Set media options.
332796f2e892SBill Paul  */
332896f2e892SBill Paul static int dc_ifmedia_upd(ifp)
332996f2e892SBill Paul 	struct ifnet		*ifp;
333096f2e892SBill Paul {
333196f2e892SBill Paul 	struct dc_softc		*sc;
333296f2e892SBill Paul 	struct mii_data		*mii;
3333f43d9309SBill Paul 	struct ifmedia		*ifm;
333496f2e892SBill Paul 
333596f2e892SBill Paul 	sc = ifp->if_softc;
333696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
333796f2e892SBill Paul 	mii_mediachg(mii);
3338f43d9309SBill Paul 	ifm = &mii->mii_media;
3339f43d9309SBill Paul 
3340f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
3341f43d9309SBill Paul 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA)
3342f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3343f43d9309SBill Paul 	else
334496f2e892SBill Paul 		sc->dc_link = 0;
334596f2e892SBill Paul 
334696f2e892SBill Paul 	return(0);
334796f2e892SBill Paul }
334896f2e892SBill Paul 
334996f2e892SBill Paul /*
335096f2e892SBill Paul  * Report current media status.
335196f2e892SBill Paul  */
335296f2e892SBill Paul static void dc_ifmedia_sts(ifp, ifmr)
335396f2e892SBill Paul 	struct ifnet		*ifp;
335496f2e892SBill Paul 	struct ifmediareq	*ifmr;
335596f2e892SBill Paul {
335696f2e892SBill Paul 	struct dc_softc		*sc;
335796f2e892SBill Paul 	struct mii_data		*mii;
3358f43d9309SBill Paul 	struct ifmedia		*ifm;
335996f2e892SBill Paul 
336096f2e892SBill Paul 	sc = ifp->if_softc;
336196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
336296f2e892SBill Paul 	mii_pollstat(mii);
3363f43d9309SBill Paul 	ifm = &mii->mii_media;
3364f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
3365f43d9309SBill Paul 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_homePNA) {
3366f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3367f43d9309SBill Paul 			ifmr->ifm_status = 0;
3368f43d9309SBill Paul 			return;
3369f43d9309SBill Paul 		}
3370f43d9309SBill Paul 	}
337196f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
337296f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
337396f2e892SBill Paul 
337496f2e892SBill Paul 	return;
337596f2e892SBill Paul }
337696f2e892SBill Paul 
337796f2e892SBill Paul static int dc_ioctl(ifp, command, data)
337896f2e892SBill Paul 	struct ifnet		*ifp;
337996f2e892SBill Paul 	u_long			command;
338096f2e892SBill Paul 	caddr_t			data;
338196f2e892SBill Paul {
338296f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
338396f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
338496f2e892SBill Paul 	struct mii_data		*mii;
3385d1ce9105SBill Paul 	int			error = 0;
338696f2e892SBill Paul 
3387d1ce9105SBill Paul 	DC_LOCK(sc);
338896f2e892SBill Paul 
338996f2e892SBill Paul 	switch(command) {
339096f2e892SBill Paul 	case SIOCSIFADDR:
339196f2e892SBill Paul 	case SIOCGIFADDR:
339296f2e892SBill Paul 	case SIOCSIFMTU:
339396f2e892SBill Paul 		error = ether_ioctl(ifp, command, data);
339496f2e892SBill Paul 		break;
339596f2e892SBill Paul 	case SIOCSIFFLAGS:
339696f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
339796f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
339896f2e892SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
339996f2e892SBill Paul 			    !(sc->dc_if_flags & IFF_PROMISC)) {
340096f2e892SBill Paul 				dc_setfilt(sc);
340196f2e892SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
340296f2e892SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
340396f2e892SBill Paul 			    sc->dc_if_flags & IFF_PROMISC) {
340496f2e892SBill Paul 				dc_setfilt(sc);
340596f2e892SBill Paul 			} else if (!(ifp->if_flags & IFF_RUNNING)) {
340696f2e892SBill Paul 				sc->dc_txthresh = 0;
340796f2e892SBill Paul 				dc_init(sc);
340896f2e892SBill Paul 			}
340996f2e892SBill Paul 		} else {
341096f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
341196f2e892SBill Paul 				dc_stop(sc);
341296f2e892SBill Paul 		}
341396f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
341496f2e892SBill Paul 		error = 0;
341596f2e892SBill Paul 		break;
341696f2e892SBill Paul 	case SIOCADDMULTI:
341796f2e892SBill Paul 	case SIOCDELMULTI:
341896f2e892SBill Paul 		dc_setfilt(sc);
341996f2e892SBill Paul 		error = 0;
342096f2e892SBill Paul 		break;
342196f2e892SBill Paul 	case SIOCGIFMEDIA:
342296f2e892SBill Paul 	case SIOCSIFMEDIA:
342396f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
342496f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
34255c1cfac4SBill Paul #ifdef SRM_MEDIA
3426510a809eSMike Smith 		if (sc->dc_srm_media)
3427510a809eSMike Smith 			sc->dc_srm_media = 0;
3428510a809eSMike Smith #endif
342996f2e892SBill Paul 		break;
343096f2e892SBill Paul 	default:
343196f2e892SBill Paul 		error = EINVAL;
343296f2e892SBill Paul 		break;
343396f2e892SBill Paul 	}
343496f2e892SBill Paul 
3435d1ce9105SBill Paul 	DC_UNLOCK(sc);
343696f2e892SBill Paul 
343796f2e892SBill Paul 	return(error);
343896f2e892SBill Paul }
343996f2e892SBill Paul 
344096f2e892SBill Paul static void dc_watchdog(ifp)
344196f2e892SBill Paul 	struct ifnet		*ifp;
344296f2e892SBill Paul {
344396f2e892SBill Paul 	struct dc_softc		*sc;
344496f2e892SBill Paul 
344596f2e892SBill Paul 	sc = ifp->if_softc;
344696f2e892SBill Paul 
3447d1ce9105SBill Paul 	DC_LOCK(sc);
3448d1ce9105SBill Paul 
344996f2e892SBill Paul 	ifp->if_oerrors++;
345096f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
345196f2e892SBill Paul 
345296f2e892SBill Paul 	dc_stop(sc);
345396f2e892SBill Paul 	dc_reset(sc);
345496f2e892SBill Paul 	dc_init(sc);
345596f2e892SBill Paul 
345696f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
345796f2e892SBill Paul 		dc_start(ifp);
345896f2e892SBill Paul 
3459d1ce9105SBill Paul 	DC_UNLOCK(sc);
3460d1ce9105SBill Paul 
346196f2e892SBill Paul 	return;
346296f2e892SBill Paul }
346396f2e892SBill Paul 
346496f2e892SBill Paul /*
346596f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
346696f2e892SBill Paul  * RX and TX lists.
346796f2e892SBill Paul  */
346896f2e892SBill Paul static void dc_stop(sc)
346996f2e892SBill Paul 	struct dc_softc		*sc;
347096f2e892SBill Paul {
347196f2e892SBill Paul 	register int		i;
347296f2e892SBill Paul 	struct ifnet		*ifp;
347396f2e892SBill Paul 
3474d1ce9105SBill Paul 	DC_LOCK(sc);
3475d1ce9105SBill Paul 
347696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
347796f2e892SBill Paul 	ifp->if_timer = 0;
347896f2e892SBill Paul 
3479b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
348096f2e892SBill Paul 
34813b3ec200SPeter Wemm 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3482e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3483e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
3484e4fc250cSLuigi Rizzo #endif
34853b3ec200SPeter Wemm 
348696f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
348796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
348896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
348996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
349096f2e892SBill Paul 	sc->dc_link = 0;
349196f2e892SBill Paul 
349296f2e892SBill Paul 	/*
349396f2e892SBill Paul 	 * Free data in the RX lists.
349496f2e892SBill Paul 	 */
349596f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
349696f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
349796f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
349896f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
349996f2e892SBill Paul 		}
350096f2e892SBill Paul 	}
350196f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
350296f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
350396f2e892SBill Paul 
350496f2e892SBill Paul 	/*
350596f2e892SBill Paul 	 * Free the TX list buffers.
350696f2e892SBill Paul 	 */
350796f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
350896f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
350996f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
351096f2e892SBill Paul 			    DC_TXCTL_SETUP) {
351196f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
351296f2e892SBill Paul 				continue;
351396f2e892SBill Paul 			}
351496f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
351596f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
351696f2e892SBill Paul 		}
351796f2e892SBill Paul 	}
351896f2e892SBill Paul 
351996f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
352096f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
352196f2e892SBill Paul 
3522d1ce9105SBill Paul 	DC_UNLOCK(sc);
3523d1ce9105SBill Paul 
352496f2e892SBill Paul 	return;
352596f2e892SBill Paul }
352696f2e892SBill Paul 
352796f2e892SBill Paul /*
352896f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
352996f2e892SBill Paul  * get confused by errant DMAs when rebooting.
353096f2e892SBill Paul  */
353196f2e892SBill Paul static void dc_shutdown(dev)
353296f2e892SBill Paul 	device_t		dev;
353396f2e892SBill Paul {
353496f2e892SBill Paul 	struct dc_softc		*sc;
353596f2e892SBill Paul 
353696f2e892SBill Paul 	sc = device_get_softc(dev);
353796f2e892SBill Paul 
353896f2e892SBill Paul 	dc_stop(sc);
353996f2e892SBill Paul 
354096f2e892SBill Paul 	return;
354196f2e892SBill Paul }
3542