xref: /freebsd/sys/dev/dc/if_dc.c (revision 351267c1866c459f90ef4fa34d10fdbc95e8b058)
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 
198e51a25f8SAlfred Perlstein static int dc_probe		(device_t);
199e51a25f8SAlfred Perlstein static int dc_attach		(device_t);
200e51a25f8SAlfred Perlstein static int dc_detach		(device_t);
201e8388e14SMitsuru IWASAKI static int dc_suspend		(device_t);
202e8388e14SMitsuru IWASAKI static int dc_resume		(device_t);
203e51a25f8SAlfred Perlstein static void dc_acpi		(device_t);
204e51a25f8SAlfred Perlstein static struct dc_type *dc_devtype	(device_t);
205e51a25f8SAlfred Perlstein static int dc_newbuf		(struct dc_softc *, int, struct mbuf *);
206e51a25f8SAlfred Perlstein static int dc_encap		(struct dc_softc *, struct mbuf *, u_int32_t *);
207e51a25f8SAlfred Perlstein static int dc_coal		(struct dc_softc *, struct mbuf **);
208e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war	(struct dc_softc *, int);
209e51a25f8SAlfred Perlstein static int dc_rx_resync		(struct dc_softc *);
210e51a25f8SAlfred Perlstein static void dc_rxeof		(struct dc_softc *);
211e51a25f8SAlfred Perlstein static void dc_txeof		(struct dc_softc *);
212e51a25f8SAlfred Perlstein static void dc_tick		(void *);
213e51a25f8SAlfred Perlstein static void dc_tx_underrun	(struct dc_softc *);
214e51a25f8SAlfred Perlstein static void dc_intr		(void *);
215e51a25f8SAlfred Perlstein static void dc_start		(struct ifnet *);
216e51a25f8SAlfred Perlstein static int dc_ioctl		(struct ifnet *, u_long, caddr_t);
217e51a25f8SAlfred Perlstein static void dc_init		(void *);
218e51a25f8SAlfred Perlstein static void dc_stop		(struct dc_softc *);
219e51a25f8SAlfred Perlstein static void dc_watchdog		(struct ifnet *);
220e51a25f8SAlfred Perlstein static void dc_shutdown		(device_t);
221e51a25f8SAlfred Perlstein static int dc_ifmedia_upd	(struct ifnet *);
222e51a25f8SAlfred Perlstein static void dc_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
22396f2e892SBill Paul 
224e51a25f8SAlfred Perlstein static void dc_delay		(struct dc_softc *);
225e51a25f8SAlfred Perlstein static void dc_eeprom_idle	(struct dc_softc *);
226e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte	(struct dc_softc *, int);
227e51a25f8SAlfred Perlstein static void dc_eeprom_getword	(struct dc_softc *, int, u_int16_t *);
22896f2e892SBill Paul static void dc_eeprom_getword_pnic
229e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
230feb78939SJonathan Chen static void dc_eeprom_getword_xircom
231e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
232e51a25f8SAlfred Perlstein static void dc_read_eeprom	(struct dc_softc *, caddr_t, int, int, int);
23396f2e892SBill Paul 
234e51a25f8SAlfred Perlstein static void dc_mii_writebit	(struct dc_softc *, int);
235e51a25f8SAlfred Perlstein static int dc_mii_readbit	(struct dc_softc *);
236e51a25f8SAlfred Perlstein static void dc_mii_sync		(struct dc_softc *);
237e51a25f8SAlfred Perlstein static void dc_mii_send		(struct dc_softc *, u_int32_t, int);
238e51a25f8SAlfred Perlstein static int dc_mii_readreg	(struct dc_softc *, struct dc_mii_frame *);
239e51a25f8SAlfred Perlstein static int dc_mii_writereg	(struct dc_softc *, struct dc_mii_frame *);
240e51a25f8SAlfred Perlstein static int dc_miibus_readreg	(device_t, int, int);
241e51a25f8SAlfred Perlstein static int dc_miibus_writereg	(device_t, int, int, int);
242e51a25f8SAlfred Perlstein static void dc_miibus_statchg	(device_t);
243e51a25f8SAlfred Perlstein static void dc_miibus_mediainit	(device_t);
24496f2e892SBill Paul 
245e51a25f8SAlfred Perlstein static void dc_setcfg		(struct dc_softc *, int);
246e51a25f8SAlfred Perlstein static u_int32_t dc_crc_le	(struct dc_softc *, caddr_t);
247e51a25f8SAlfred Perlstein static u_int32_t dc_crc_be	(caddr_t);
248e51a25f8SAlfred Perlstein static void dc_setfilt_21143	(struct dc_softc *);
249e51a25f8SAlfred Perlstein static void dc_setfilt_asix	(struct dc_softc *);
250e51a25f8SAlfred Perlstein static void dc_setfilt_admtek	(struct dc_softc *);
251e51a25f8SAlfred Perlstein static void dc_setfilt_xircom	(struct dc_softc *);
25296f2e892SBill Paul 
253e51a25f8SAlfred Perlstein static void dc_setfilt		(struct dc_softc *);
25496f2e892SBill Paul 
255e51a25f8SAlfred Perlstein static void dc_reset		(struct dc_softc *);
256e51a25f8SAlfred Perlstein static int dc_list_rx_init	(struct dc_softc *);
257e51a25f8SAlfred Perlstein static int dc_list_tx_init	(struct dc_softc *);
25896f2e892SBill Paul 
259e51a25f8SAlfred Perlstein static void dc_parse_21143_srom	(struct dc_softc *);
260e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia	(struct dc_softc *, struct dc_eblock_sia *);
261e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii	(struct dc_softc *, struct dc_eblock_mii *);
262e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym	(struct dc_softc *, struct dc_eblock_sym *);
263e51a25f8SAlfred Perlstein static void dc_apply_fixup	(struct dc_softc *, int);
2645c1cfac4SBill Paul 
26596f2e892SBill Paul #ifdef DC_USEIOSPACE
26696f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
26796f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
26896f2e892SBill Paul #else
26996f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
27096f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
27196f2e892SBill Paul #endif
27296f2e892SBill Paul 
27396f2e892SBill Paul static device_method_t dc_methods[] = {
27496f2e892SBill Paul 	/* Device interface */
27596f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
27696f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
27796f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
278e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
279e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
28096f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
28196f2e892SBill Paul 
28296f2e892SBill Paul 	/* bus interface */
28396f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
28496f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
28596f2e892SBill Paul 
28696f2e892SBill Paul 	/* MII interface */
28796f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
28896f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
28996f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
290f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
29196f2e892SBill Paul 
29296f2e892SBill Paul 	{ 0, 0 }
29396f2e892SBill Paul };
29496f2e892SBill Paul 
29596f2e892SBill Paul static driver_t dc_driver = {
29696f2e892SBill Paul 	"dc",
29796f2e892SBill Paul 	dc_methods,
29896f2e892SBill Paul 	sizeof(struct dc_softc)
29996f2e892SBill Paul };
30096f2e892SBill Paul 
30196f2e892SBill Paul static devclass_t dc_devclass;
30201faf54bSLuigi Rizzo #ifdef __i386__
30301faf54bSLuigi Rizzo static int dc_quick=1;
30401faf54bSLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW,
30501faf54bSLuigi Rizzo 	&dc_quick,0,"do not mdevget in dc driver");
30601faf54bSLuigi Rizzo #endif
30796f2e892SBill Paul 
308feb78939SJonathan Chen DRIVER_MODULE(if_dc, cardbus, dc_driver, dc_devclass, 0, 0);
30996f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
31096f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
31196f2e892SBill Paul 
31296f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
31396f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
31496f2e892SBill Paul 
31596f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
31696f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
31796f2e892SBill Paul 
31896f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
31996f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
32096f2e892SBill Paul 
321b50c6312SJonathan Lemon #define IS_MPSAFE 	0
322b50c6312SJonathan Lemon 
323e3d2833aSAlfred Perlstein static void
324e3d2833aSAlfred Perlstein 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 
333e3d2833aSAlfred Perlstein static void
334e3d2833aSAlfred Perlstein dc_eeprom_idle(sc)
33596f2e892SBill Paul 	struct dc_softc		*sc;
33696f2e892SBill Paul {
33796f2e892SBill Paul 	register int		i;
33896f2e892SBill Paul 
33996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
34096f2e892SBill Paul 	dc_delay(sc);
34196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
34296f2e892SBill Paul 	dc_delay(sc);
34396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
34496f2e892SBill Paul 	dc_delay(sc);
34596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
34696f2e892SBill Paul 	dc_delay(sc);
34796f2e892SBill Paul 
34896f2e892SBill Paul 	for (i = 0; i < 25; i++) {
34996f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
35096f2e892SBill Paul 		dc_delay(sc);
35196f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
35296f2e892SBill Paul 		dc_delay(sc);
35396f2e892SBill Paul 	}
35496f2e892SBill Paul 
35596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
35696f2e892SBill Paul 	dc_delay(sc);
35796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
35896f2e892SBill Paul 	dc_delay(sc);
35996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
36096f2e892SBill Paul 
36196f2e892SBill Paul 	return;
36296f2e892SBill Paul }
36396f2e892SBill Paul 
36496f2e892SBill Paul /*
36596f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
36696f2e892SBill Paul  */
367e3d2833aSAlfred Perlstein static void
368e3d2833aSAlfred Perlstein dc_eeprom_putbyte(sc, addr)
36996f2e892SBill Paul 	struct dc_softc		*sc;
37096f2e892SBill Paul 	int			addr;
37196f2e892SBill Paul {
37296f2e892SBill Paul 	register int		d, i;
37396f2e892SBill Paul 
37496f2e892SBill Paul 	/*
37596f2e892SBill Paul 	 * The AN985 has a 93C66 EEPROM on it instead of
37696f2e892SBill Paul 	 * a 93C46. It uses a different bit sequence for
37796f2e892SBill Paul 	 * specifying the "read" opcode.
37896f2e892SBill Paul 	 */
3791af8bec7SBill Paul 	if (DC_IS_CENTAUR(sc) || DC_IS_CONEXANT(sc))
38096f2e892SBill Paul 		d = addr | (DC_EECMD_READ << 2);
38196f2e892SBill Paul 	else
38296f2e892SBill Paul 		d = addr | DC_EECMD_READ;
38396f2e892SBill Paul 
38496f2e892SBill Paul 	/*
38596f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
38696f2e892SBill Paul 	 */
38796f2e892SBill Paul 	for (i = 0x400; i; i >>= 1) {
38896f2e892SBill Paul 		if (d & i) {
38996f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
39096f2e892SBill Paul 		} else {
39196f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
39296f2e892SBill Paul 		}
39396f2e892SBill Paul 		dc_delay(sc);
39496f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
39596f2e892SBill Paul 		dc_delay(sc);
39696f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
39796f2e892SBill Paul 		dc_delay(sc);
39896f2e892SBill Paul 	}
39996f2e892SBill Paul 
40096f2e892SBill Paul 	return;
40196f2e892SBill Paul }
40296f2e892SBill Paul 
40396f2e892SBill Paul /*
40496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
40596f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
40696f2e892SBill Paul  * the EEPROM.
40796f2e892SBill Paul  */
408e3d2833aSAlfred Perlstein static void
409e3d2833aSAlfred Perlstein dc_eeprom_getword_pnic(sc, addr, dest)
41096f2e892SBill Paul 	struct dc_softc		*sc;
41196f2e892SBill Paul 	int			addr;
41296f2e892SBill Paul 	u_int16_t		*dest;
41396f2e892SBill Paul {
41496f2e892SBill Paul 	register int		i;
41596f2e892SBill Paul 	u_int32_t		r;
41696f2e892SBill Paul 
41796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
41896f2e892SBill Paul 
41996f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
42096f2e892SBill Paul 		DELAY(1);
42196f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
42296f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
42396f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
42496f2e892SBill Paul 			return;
42596f2e892SBill Paul 		}
42696f2e892SBill Paul 	}
42796f2e892SBill Paul 
42896f2e892SBill Paul 	return;
42996f2e892SBill Paul }
43096f2e892SBill Paul 
43196f2e892SBill Paul /*
43296f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
433feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
434feb78939SJonathan Chen  * the EEPROM, too.
435feb78939SJonathan Chen  */
436e3d2833aSAlfred Perlstein static void
437e3d2833aSAlfred Perlstein dc_eeprom_getword_xircom(sc, addr, dest)
438feb78939SJonathan Chen 	struct dc_softc		*sc;
439feb78939SJonathan Chen 	int			addr;
440feb78939SJonathan Chen 	u_int16_t		*dest;
441feb78939SJonathan Chen {
442feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
443feb78939SJonathan Chen 
444feb78939SJonathan Chen 	addr *= 2;
445feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
446feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff;
447feb78939SJonathan Chen 	addr += 1;
448feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
449feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff) << 8;
450feb78939SJonathan Chen 
451feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
452feb78939SJonathan Chen 	return;
453feb78939SJonathan Chen }
454feb78939SJonathan Chen 
455feb78939SJonathan Chen /*
456feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
45796f2e892SBill Paul  */
458e3d2833aSAlfred Perlstein static void
459e3d2833aSAlfred Perlstein dc_eeprom_getword(sc, addr, dest)
46096f2e892SBill Paul 	struct dc_softc		*sc;
46196f2e892SBill Paul 	int			addr;
46296f2e892SBill Paul 	u_int16_t		*dest;
46396f2e892SBill Paul {
46496f2e892SBill Paul 	register int		i;
46596f2e892SBill Paul 	u_int16_t		word = 0;
46696f2e892SBill Paul 
46796f2e892SBill Paul 	/* Force EEPROM to idle state. */
46896f2e892SBill Paul 	dc_eeprom_idle(sc);
46996f2e892SBill Paul 
47096f2e892SBill Paul 	/* Enter EEPROM access mode. */
47196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
47296f2e892SBill Paul 	dc_delay(sc);
47396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
47496f2e892SBill Paul 	dc_delay(sc);
47596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
47696f2e892SBill Paul 	dc_delay(sc);
47796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
47896f2e892SBill Paul 	dc_delay(sc);
47996f2e892SBill Paul 
48096f2e892SBill Paul 	/*
48196f2e892SBill Paul 	 * Send address of word we want to read.
48296f2e892SBill Paul 	 */
48396f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
48496f2e892SBill Paul 
48596f2e892SBill Paul 	/*
48696f2e892SBill Paul 	 * Start reading bits from EEPROM.
48796f2e892SBill Paul 	 */
48896f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
48996f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
49096f2e892SBill Paul 		dc_delay(sc);
49196f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
49296f2e892SBill Paul 			word |= i;
49396f2e892SBill Paul 		dc_delay(sc);
49496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
49596f2e892SBill Paul 		dc_delay(sc);
49696f2e892SBill Paul 	}
49796f2e892SBill Paul 
49896f2e892SBill Paul 	/* Turn off EEPROM access mode. */
49996f2e892SBill Paul 	dc_eeprom_idle(sc);
50096f2e892SBill Paul 
50196f2e892SBill Paul 	*dest = word;
50296f2e892SBill Paul 
50396f2e892SBill Paul 	return;
50496f2e892SBill Paul }
50596f2e892SBill Paul 
50696f2e892SBill Paul /*
50796f2e892SBill Paul  * Read a sequence of words from the EEPROM.
50896f2e892SBill Paul  */
509e3d2833aSAlfred Perlstein static void
510e3d2833aSAlfred Perlstein dc_read_eeprom(sc, dest, off, cnt, swap)
51196f2e892SBill Paul 	struct dc_softc		*sc;
51296f2e892SBill Paul 	caddr_t			dest;
51396f2e892SBill Paul 	int			off;
51496f2e892SBill Paul 	int			cnt;
51596f2e892SBill Paul 	int			swap;
51696f2e892SBill Paul {
51796f2e892SBill Paul 	int			i;
51896f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
51996f2e892SBill Paul 
52096f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
52196f2e892SBill Paul 		if (DC_IS_PNIC(sc))
52296f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
523feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
524feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
52596f2e892SBill Paul 		else
52696f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
52796f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
52896f2e892SBill Paul 		if (swap)
52996f2e892SBill Paul 			*ptr = ntohs(word);
53096f2e892SBill Paul 		else
53196f2e892SBill Paul 			*ptr = word;
53296f2e892SBill Paul 	}
53396f2e892SBill Paul 
53496f2e892SBill Paul 	return;
53596f2e892SBill Paul }
53696f2e892SBill Paul 
53796f2e892SBill Paul /*
53896f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
53996f2e892SBill Paul  * Application Notes pp.19-21.
54096f2e892SBill Paul  */
54196f2e892SBill Paul /*
54296f2e892SBill Paul  * Write a bit to the MII bus.
54396f2e892SBill Paul  */
544e3d2833aSAlfred Perlstein static void
545e3d2833aSAlfred Perlstein dc_mii_writebit(sc, bit)
54696f2e892SBill Paul 	struct dc_softc		*sc;
54796f2e892SBill Paul 	int			bit;
54896f2e892SBill Paul {
54996f2e892SBill Paul 	if (bit)
55096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
55196f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
55296f2e892SBill Paul 	else
55396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
55496f2e892SBill Paul 
55596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
55696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
55796f2e892SBill Paul 
55896f2e892SBill Paul 	return;
55996f2e892SBill Paul }
56096f2e892SBill Paul 
56196f2e892SBill Paul /*
56296f2e892SBill Paul  * Read a bit from the MII bus.
56396f2e892SBill Paul  */
564e3d2833aSAlfred Perlstein static int
565e3d2833aSAlfred Perlstein dc_mii_readbit(sc)
56696f2e892SBill Paul 	struct dc_softc		*sc;
56796f2e892SBill Paul {
56896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
56996f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
57096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
57196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
57296f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
57396f2e892SBill Paul 		return(1);
57496f2e892SBill Paul 
57596f2e892SBill Paul 	return(0);
57696f2e892SBill Paul }
57796f2e892SBill Paul 
57896f2e892SBill Paul /*
57996f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
58096f2e892SBill Paul  */
581e3d2833aSAlfred Perlstein static void
582e3d2833aSAlfred Perlstein dc_mii_sync(sc)
58396f2e892SBill Paul 	struct dc_softc		*sc;
58496f2e892SBill Paul {
58596f2e892SBill Paul 	register int		i;
58696f2e892SBill Paul 
58796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
58896f2e892SBill Paul 
58996f2e892SBill Paul 	for (i = 0; i < 32; i++)
59096f2e892SBill Paul 		dc_mii_writebit(sc, 1);
59196f2e892SBill Paul 
59296f2e892SBill Paul 	return;
59396f2e892SBill Paul }
59496f2e892SBill Paul 
59596f2e892SBill Paul /*
59696f2e892SBill Paul  * Clock a series of bits through the MII.
59796f2e892SBill Paul  */
598e3d2833aSAlfred Perlstein static void
599e3d2833aSAlfred Perlstein dc_mii_send(sc, bits, cnt)
60096f2e892SBill Paul 	struct dc_softc		*sc;
60196f2e892SBill Paul 	u_int32_t		bits;
60296f2e892SBill Paul 	int			cnt;
60396f2e892SBill Paul {
60496f2e892SBill Paul 	int			i;
60596f2e892SBill Paul 
60696f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
60796f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
60896f2e892SBill Paul }
60996f2e892SBill Paul 
61096f2e892SBill Paul /*
61196f2e892SBill Paul  * Read an PHY register through the MII.
61296f2e892SBill Paul  */
613e3d2833aSAlfred Perlstein static int
614e3d2833aSAlfred Perlstein dc_mii_readreg(sc, frame)
61596f2e892SBill Paul 	struct dc_softc		*sc;
61696f2e892SBill Paul 	struct dc_mii_frame	*frame;
61796f2e892SBill Paul 
61896f2e892SBill Paul {
619d1ce9105SBill Paul 	int			i, ack;
62096f2e892SBill Paul 
621d1ce9105SBill Paul 	DC_LOCK(sc);
62296f2e892SBill Paul 
62396f2e892SBill Paul 	/*
62496f2e892SBill Paul 	 * Set up frame for RX.
62596f2e892SBill Paul 	 */
62696f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
62796f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
62896f2e892SBill Paul 	frame->mii_turnaround = 0;
62996f2e892SBill Paul 	frame->mii_data = 0;
63096f2e892SBill Paul 
63196f2e892SBill Paul 	/*
63296f2e892SBill Paul 	 * Sync the PHYs.
63396f2e892SBill Paul 	 */
63496f2e892SBill Paul 	dc_mii_sync(sc);
63596f2e892SBill Paul 
63696f2e892SBill Paul 	/*
63796f2e892SBill Paul 	 * Send command/address info.
63896f2e892SBill Paul 	 */
63996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
64096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
64196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
64296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
64396f2e892SBill Paul 
64496f2e892SBill Paul #ifdef notdef
64596f2e892SBill Paul 	/* Idle bit */
64696f2e892SBill Paul 	dc_mii_writebit(sc, 1);
64796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
64896f2e892SBill Paul #endif
64996f2e892SBill Paul 
65096f2e892SBill Paul 	/* Check for ack */
65196f2e892SBill Paul 	ack = dc_mii_readbit(sc);
65296f2e892SBill Paul 
65396f2e892SBill Paul 	/*
65496f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
65596f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
65696f2e892SBill Paul 	 */
65796f2e892SBill Paul 	if (ack) {
65896f2e892SBill Paul 		for(i = 0; i < 16; i++) {
65996f2e892SBill Paul 			dc_mii_readbit(sc);
66096f2e892SBill Paul 		}
66196f2e892SBill Paul 		goto fail;
66296f2e892SBill Paul 	}
66396f2e892SBill Paul 
66496f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
66596f2e892SBill Paul 		if (!ack) {
66696f2e892SBill Paul 			if (dc_mii_readbit(sc))
66796f2e892SBill Paul 				frame->mii_data |= i;
66896f2e892SBill Paul 		}
66996f2e892SBill Paul 	}
67096f2e892SBill Paul 
67196f2e892SBill Paul fail:
67296f2e892SBill Paul 
67396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
67496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
67596f2e892SBill Paul 
676d1ce9105SBill Paul 	DC_UNLOCK(sc);
67796f2e892SBill Paul 
67896f2e892SBill Paul 	if (ack)
67996f2e892SBill Paul 		return(1);
68096f2e892SBill Paul 	return(0);
68196f2e892SBill Paul }
68296f2e892SBill Paul 
68396f2e892SBill Paul /*
68496f2e892SBill Paul  * Write to a PHY register through the MII.
68596f2e892SBill Paul  */
686e3d2833aSAlfred Perlstein static int
687e3d2833aSAlfred Perlstein dc_mii_writereg(sc, frame)
68896f2e892SBill Paul 	struct dc_softc		*sc;
68996f2e892SBill Paul 	struct dc_mii_frame	*frame;
69096f2e892SBill Paul 
69196f2e892SBill Paul {
692d1ce9105SBill Paul 	DC_LOCK(sc);
69396f2e892SBill Paul 	/*
69496f2e892SBill Paul 	 * Set up frame for TX.
69596f2e892SBill Paul 	 */
69696f2e892SBill Paul 
69796f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
69896f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
69996f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
70096f2e892SBill Paul 
70196f2e892SBill Paul 	/*
70296f2e892SBill Paul 	 * Sync the PHYs.
70396f2e892SBill Paul 	 */
70496f2e892SBill Paul 	dc_mii_sync(sc);
70596f2e892SBill Paul 
70696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
70796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
70896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
70996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
71096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
71196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
71296f2e892SBill Paul 
71396f2e892SBill Paul 	/* Idle bit. */
71496f2e892SBill Paul 	dc_mii_writebit(sc, 0);
71596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
71696f2e892SBill Paul 
717d1ce9105SBill Paul 	DC_UNLOCK(sc);
71896f2e892SBill Paul 
71996f2e892SBill Paul 	return(0);
72096f2e892SBill Paul }
72196f2e892SBill Paul 
722e3d2833aSAlfred Perlstein static int
723e3d2833aSAlfred Perlstein dc_miibus_readreg(dev, phy, reg)
72496f2e892SBill Paul 	device_t		dev;
72596f2e892SBill Paul 	int			phy, reg;
72696f2e892SBill Paul {
72796f2e892SBill Paul 	struct dc_mii_frame	frame;
72896f2e892SBill Paul 	struct dc_softc		*sc;
729c85c4667SBill Paul 	int			i, rval, phy_reg = 0;
73096f2e892SBill Paul 
73196f2e892SBill Paul 	sc = device_get_softc(dev);
73296f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
73396f2e892SBill Paul 
73496f2e892SBill Paul 	/*
73596f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
73696f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
73796f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
73896f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
73996f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
74096f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
74196f2e892SBill Paul 	 * that the PHY is at MII address 1.
74296f2e892SBill Paul 	 */
74396f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
74496f2e892SBill Paul 		return(0);
74596f2e892SBill Paul 
7461af8bec7SBill Paul 	/*
7471af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7481af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7491af8bec7SBill Paul 	 * so we only respond to correct one.
7501af8bec7SBill Paul 	 */
7511af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
7521af8bec7SBill Paul 		return(0);
7531af8bec7SBill Paul 
7545c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
75596f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
75696f2e892SBill Paul 			switch(reg) {
75796f2e892SBill Paul 			case MII_BMSR:
75896f2e892SBill Paul 			/*
75996f2e892SBill Paul 			 * Fake something to make the probe
76096f2e892SBill Paul 			 * code think there's a PHY here.
76196f2e892SBill Paul 			 */
76296f2e892SBill Paul 				return(BMSR_MEDIAMASK);
76396f2e892SBill Paul 				break;
76496f2e892SBill Paul 			case MII_PHYIDR1:
76596f2e892SBill Paul 				if (DC_IS_PNIC(sc))
76696f2e892SBill Paul 					return(DC_VENDORID_LO);
76796f2e892SBill Paul 				return(DC_VENDORID_DEC);
76896f2e892SBill Paul 				break;
76996f2e892SBill Paul 			case MII_PHYIDR2:
77096f2e892SBill Paul 				if (DC_IS_PNIC(sc))
77196f2e892SBill Paul 					return(DC_DEVICEID_82C168);
77296f2e892SBill Paul 				return(DC_DEVICEID_21143);
77396f2e892SBill Paul 				break;
77496f2e892SBill Paul 			default:
77596f2e892SBill Paul 				return(0);
77696f2e892SBill Paul 				break;
77796f2e892SBill Paul 			}
77896f2e892SBill Paul 		} else
77996f2e892SBill Paul 			return(0);
78096f2e892SBill Paul 	}
78196f2e892SBill Paul 
78296f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
78396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
78496f2e892SBill Paul 		    (phy << 23) | (reg << 18));
78596f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
78696f2e892SBill Paul 			DELAY(1);
78796f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
78896f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
78996f2e892SBill Paul 				rval &= 0xFFFF;
79096f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
79196f2e892SBill Paul 			}
79296f2e892SBill Paul 		}
79396f2e892SBill Paul 		return(0);
79496f2e892SBill Paul 	}
79596f2e892SBill Paul 
79696f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
79796f2e892SBill Paul 		switch(reg) {
79896f2e892SBill Paul 		case MII_BMCR:
79996f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
80096f2e892SBill Paul 			break;
80196f2e892SBill Paul 		case MII_BMSR:
80296f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
80396f2e892SBill Paul 			break;
80496f2e892SBill Paul 		case MII_PHYIDR1:
80596f2e892SBill Paul 			phy_reg = DC_AL_VENID;
80696f2e892SBill Paul 			break;
80796f2e892SBill Paul 		case MII_PHYIDR2:
80896f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
80996f2e892SBill Paul 			break;
81096f2e892SBill Paul 		case MII_ANAR:
81196f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
81296f2e892SBill Paul 			break;
81396f2e892SBill Paul 		case MII_ANLPAR:
81496f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
81596f2e892SBill Paul 			break;
81696f2e892SBill Paul 		case MII_ANER:
81796f2e892SBill Paul 			phy_reg = DC_AL_ANER;
81896f2e892SBill Paul 			break;
81996f2e892SBill Paul 		default:
82096f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
82196f2e892SBill Paul 			    sc->dc_unit, reg);
82296f2e892SBill Paul 			return(0);
82396f2e892SBill Paul 			break;
82496f2e892SBill Paul 		}
82596f2e892SBill Paul 
82696f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
82796f2e892SBill Paul 
82896f2e892SBill Paul 		if (rval == 0xFFFF)
82996f2e892SBill Paul 			return(0);
83096f2e892SBill Paul 		return(rval);
83196f2e892SBill Paul 	}
83296f2e892SBill Paul 
83396f2e892SBill Paul 	frame.mii_phyaddr = phy;
83496f2e892SBill Paul 	frame.mii_regaddr = reg;
835419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
836f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
837f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
838419146d9SBill Paul 	}
83996f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
840419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
841f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
84296f2e892SBill Paul 
84396f2e892SBill Paul 	return(frame.mii_data);
84496f2e892SBill Paul }
84596f2e892SBill Paul 
846e3d2833aSAlfred Perlstein static int
847e3d2833aSAlfred Perlstein dc_miibus_writereg(dev, phy, reg, data)
84896f2e892SBill Paul 	device_t		dev;
84996f2e892SBill Paul 	int			phy, reg, data;
85096f2e892SBill Paul {
85196f2e892SBill Paul 	struct dc_softc		*sc;
85296f2e892SBill Paul 	struct dc_mii_frame	frame;
853c85c4667SBill Paul 	int			i, phy_reg = 0;
85496f2e892SBill Paul 
85596f2e892SBill Paul 	sc = device_get_softc(dev);
85696f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
85796f2e892SBill Paul 
85896f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
85996f2e892SBill Paul 		return(0);
86096f2e892SBill Paul 
8611af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
8621af8bec7SBill Paul 		return(0);
8631af8bec7SBill Paul 
86496f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
86596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
86696f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
86796f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
86896f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
86996f2e892SBill Paul 				break;
87096f2e892SBill Paul 		}
87196f2e892SBill Paul 		return(0);
87296f2e892SBill Paul 	}
87396f2e892SBill Paul 
87496f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
87596f2e892SBill Paul 		switch(reg) {
87696f2e892SBill Paul 		case MII_BMCR:
87796f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
87896f2e892SBill Paul 			break;
87996f2e892SBill Paul 		case MII_BMSR:
88096f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
88196f2e892SBill Paul 			break;
88296f2e892SBill Paul 		case MII_PHYIDR1:
88396f2e892SBill Paul 			phy_reg = DC_AL_VENID;
88496f2e892SBill Paul 			break;
88596f2e892SBill Paul 		case MII_PHYIDR2:
88696f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
88796f2e892SBill Paul 			break;
88896f2e892SBill Paul 		case MII_ANAR:
88996f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
89096f2e892SBill Paul 			break;
89196f2e892SBill Paul 		case MII_ANLPAR:
89296f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
89396f2e892SBill Paul 			break;
89496f2e892SBill Paul 		case MII_ANER:
89596f2e892SBill Paul 			phy_reg = DC_AL_ANER;
89696f2e892SBill Paul 			break;
89796f2e892SBill Paul 		default:
89896f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
89996f2e892SBill Paul 			    sc->dc_unit, reg);
90096f2e892SBill Paul 			return(0);
90196f2e892SBill Paul 			break;
90296f2e892SBill Paul 		}
90396f2e892SBill Paul 
90496f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
90596f2e892SBill Paul 		return(0);
90696f2e892SBill Paul 	}
90796f2e892SBill Paul 
90896f2e892SBill Paul 	frame.mii_phyaddr = phy;
90996f2e892SBill Paul 	frame.mii_regaddr = reg;
91096f2e892SBill Paul 	frame.mii_data = data;
91196f2e892SBill Paul 
912419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
913f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
914f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
915419146d9SBill Paul 	}
91696f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
917419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
918f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
91996f2e892SBill Paul 
92096f2e892SBill Paul 	return(0);
92196f2e892SBill Paul }
92296f2e892SBill Paul 
923e3d2833aSAlfred Perlstein static void
924e3d2833aSAlfred Perlstein dc_miibus_statchg(dev)
92596f2e892SBill Paul 	device_t		dev;
92696f2e892SBill Paul {
92796f2e892SBill Paul 	struct dc_softc		*sc;
92896f2e892SBill Paul 	struct mii_data		*mii;
929f43d9309SBill Paul 	struct ifmedia		*ifm;
93096f2e892SBill Paul 
93196f2e892SBill Paul 	sc = device_get_softc(dev);
93296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
93396f2e892SBill Paul 		return;
9345c1cfac4SBill Paul 
93596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
936f43d9309SBill Paul 	ifm = &mii->mii_media;
937f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
93845521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
939f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
940f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
941f43d9309SBill Paul 	} else {
94296f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
94396f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
944f43d9309SBill Paul 	}
945f43d9309SBill Paul 
946f43d9309SBill Paul 	return;
947f43d9309SBill Paul }
948f43d9309SBill Paul 
949f43d9309SBill Paul /*
950f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
951f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
952f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
953f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
954f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
955f43d9309SBill Paul  * with it itself. *sigh*
956f43d9309SBill Paul  */
957e3d2833aSAlfred Perlstein static void
958e3d2833aSAlfred Perlstein dc_miibus_mediainit(dev)
959f43d9309SBill Paul 	device_t		dev;
960f43d9309SBill Paul {
961f43d9309SBill Paul 	struct dc_softc		*sc;
962f43d9309SBill Paul 	struct mii_data		*mii;
963f43d9309SBill Paul 	struct ifmedia		*ifm;
964f43d9309SBill Paul 	int			rev;
965f43d9309SBill Paul 
966f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
967f43d9309SBill Paul 
968f43d9309SBill Paul 	sc = device_get_softc(dev);
969f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
970f43d9309SBill Paul 	ifm = &mii->mii_media;
971f43d9309SBill Paul 
972f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
97345521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER|IFM_HPNA_1, 0, NULL);
97496f2e892SBill Paul 
97596f2e892SBill Paul 	return;
97696f2e892SBill Paul }
97796f2e892SBill Paul 
97896f2e892SBill Paul #define DC_POLY		0xEDB88320
97979d11e09SBill Paul #define DC_BITS_512	9
98079d11e09SBill Paul #define DC_BITS_128	7
98179d11e09SBill Paul #define DC_BITS_64	6
98296f2e892SBill Paul 
983e3d2833aSAlfred Perlstein static u_int32_t
984e3d2833aSAlfred Perlstein dc_crc_le(sc, addr)
98596f2e892SBill Paul 	struct dc_softc		*sc;
98696f2e892SBill Paul 	caddr_t			addr;
98796f2e892SBill Paul {
98896f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
98996f2e892SBill Paul 
99096f2e892SBill Paul 	/* Compute CRC for the address value. */
99196f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
99296f2e892SBill Paul 
99396f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
99496f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
99596f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
99696f2e892SBill Paul 	}
99796f2e892SBill Paul 
99879d11e09SBill Paul 	/*
99979d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
100079d11e09SBill Paul 	 * chips is only 128 bits wide.
100179d11e09SBill Paul 	 */
100279d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
100379d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
100496f2e892SBill Paul 
100579d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
100679d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
100779d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
100879d11e09SBill Paul 
1009feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1010feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1011feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1012feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
1013feb78939SJonathan Chen 			return (crc & 0x0F) + (crc	& 0x70)*3 + (14 << 4);
1014feb78939SJonathan Chen 		else
1015feb78939SJonathan Chen 			return (crc & 0x1F) + ((crc>>1) & 0xF0)*3 + (12 << 4);
1016feb78939SJonathan Chen 	}
1017feb78939SJonathan Chen 
101879d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
101996f2e892SBill Paul }
102096f2e892SBill Paul 
102196f2e892SBill Paul /*
102296f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
102396f2e892SBill Paul  */
1024e3d2833aSAlfred Perlstein static u_int32_t
1025e3d2833aSAlfred Perlstein dc_crc_be(addr)
102696f2e892SBill Paul 	caddr_t			addr;
102796f2e892SBill Paul {
102896f2e892SBill Paul 	u_int32_t		crc, carry;
102996f2e892SBill Paul 	int			i, j;
103096f2e892SBill Paul 	u_int8_t		c;
103196f2e892SBill Paul 
103296f2e892SBill Paul 	/* Compute CRC for the address value. */
103396f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
103496f2e892SBill Paul 
103596f2e892SBill Paul 	for (i = 0; i < 6; i++) {
103696f2e892SBill Paul 		c = *(addr + i);
103796f2e892SBill Paul 		for (j = 0; j < 8; j++) {
103896f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
103996f2e892SBill Paul 			crc <<= 1;
104096f2e892SBill Paul 			c >>= 1;
104196f2e892SBill Paul 			if (carry)
104296f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
104396f2e892SBill Paul 		}
104496f2e892SBill Paul 	}
104596f2e892SBill Paul 
104696f2e892SBill Paul 	/* return the filter bit position */
104796f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
104896f2e892SBill Paul }
104996f2e892SBill Paul 
105096f2e892SBill Paul /*
105196f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
105296f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
105396f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
105496f2e892SBill Paul  *
105596f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
105696f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
105796f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
105896f2e892SBill Paul  * we need that too.
105996f2e892SBill Paul  */
1060e3d2833aSAlfred Perlstein void
1061e3d2833aSAlfred Perlstein dc_setfilt_21143(sc)
106296f2e892SBill Paul 	struct dc_softc		*sc;
106396f2e892SBill Paul {
106496f2e892SBill Paul 	struct dc_desc		*sframe;
106596f2e892SBill Paul 	u_int32_t		h, *sp;
106696f2e892SBill Paul 	struct ifmultiaddr	*ifma;
106796f2e892SBill Paul 	struct ifnet		*ifp;
106896f2e892SBill Paul 	int			i;
106996f2e892SBill Paul 
107096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
107196f2e892SBill Paul 
107296f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
107396f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
107496f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
107596f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
107696f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
107796f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
107896f2e892SBill Paul 
107996f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
108096f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
108196f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
108296f2e892SBill Paul 
108396f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
108496f2e892SBill Paul 
108596f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
108696f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
108796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
108896f2e892SBill Paul 	else
108996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
109096f2e892SBill Paul 
109196f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
109296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
109396f2e892SBill Paul 	else
109496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
109596f2e892SBill Paul 
10966817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
109796f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
109896f2e892SBill Paul 			continue;
109996f2e892SBill Paul 		h = dc_crc_le(sc,
110096f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
110196f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
110296f2e892SBill Paul 	}
110396f2e892SBill Paul 
110496f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
110596f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
110696f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
110796f2e892SBill Paul 	}
110896f2e892SBill Paul 
110996f2e892SBill Paul 	/* Set our MAC address */
111096f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
111196f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
111296f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
111396f2e892SBill Paul 
111496f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
111596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
111696f2e892SBill Paul 
111796f2e892SBill Paul 	/*
111896f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
111996f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
112096f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
112196f2e892SBill Paul 	 * medicine.
112296f2e892SBill Paul 	 */
112396f2e892SBill Paul 	DELAY(10000);
112496f2e892SBill Paul 
112596f2e892SBill Paul 	ifp->if_timer = 5;
112696f2e892SBill Paul 
112796f2e892SBill Paul 	return;
112896f2e892SBill Paul }
112996f2e892SBill Paul 
1130e3d2833aSAlfred Perlstein void
1131e3d2833aSAlfred Perlstein dc_setfilt_admtek(sc)
113296f2e892SBill Paul 	struct dc_softc		*sc;
113396f2e892SBill Paul {
113496f2e892SBill Paul 	struct ifnet		*ifp;
113596f2e892SBill Paul 	int			h = 0;
113696f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
113796f2e892SBill Paul 	struct ifmultiaddr	*ifma;
113896f2e892SBill Paul 
113996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
114096f2e892SBill Paul 
114196f2e892SBill Paul 	/* Init our MAC address */
114296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
114396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
114496f2e892SBill Paul 
114596f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
114696f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
114796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
114896f2e892SBill Paul 	else
114996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
115096f2e892SBill Paul 
115196f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
115296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
115396f2e892SBill Paul 	else
115496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
115596f2e892SBill Paul 
115696f2e892SBill Paul 	/* first, zot all the existing hash bits */
115796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
115896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
115996f2e892SBill Paul 
116096f2e892SBill Paul 	/*
116196f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
116296f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
116396f2e892SBill Paul 	 */
116496f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
116596f2e892SBill Paul 		return;
116696f2e892SBill Paul 
116796f2e892SBill Paul 	/* now program new ones */
11686817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
116996f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
117096f2e892SBill Paul 			continue;
117196f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
117296f2e892SBill Paul 		if (h < 32)
117396f2e892SBill Paul 			hashes[0] |= (1 << h);
117496f2e892SBill Paul 		else
117596f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
117696f2e892SBill Paul 	}
117796f2e892SBill Paul 
117896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
117996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
118096f2e892SBill Paul 
118196f2e892SBill Paul 	return;
118296f2e892SBill Paul }
118396f2e892SBill Paul 
1184e3d2833aSAlfred Perlstein void
1185e3d2833aSAlfred Perlstein dc_setfilt_asix(sc)
118696f2e892SBill Paul 	struct dc_softc		*sc;
118796f2e892SBill Paul {
118896f2e892SBill Paul 	struct ifnet		*ifp;
118996f2e892SBill Paul 	int			h = 0;
119096f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
119196f2e892SBill Paul 	struct ifmultiaddr	*ifma;
119296f2e892SBill Paul 
119396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
119496f2e892SBill Paul 
119596f2e892SBill Paul 	/* Init our MAC address */
119696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
119796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
119896f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
119996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
120096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
120196f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
120296f2e892SBill Paul 
120396f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
120496f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
120596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
120696f2e892SBill Paul 	else
120796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
120896f2e892SBill Paul 
120996f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
121096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
121196f2e892SBill Paul 	else
121296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
121396f2e892SBill Paul 
121496f2e892SBill Paul 	/*
121596f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
121696f2e892SBill Paul 	 * of broadcast frames.
121796f2e892SBill Paul 	 */
121896f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
121996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
122096f2e892SBill Paul 	else
122196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
122296f2e892SBill Paul 
122396f2e892SBill Paul 	/* first, zot all the existing hash bits */
122496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
122596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
122696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
122796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
122896f2e892SBill Paul 
122996f2e892SBill Paul 	/*
123096f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
123196f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
123296f2e892SBill Paul 	 */
123396f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
123496f2e892SBill Paul 		return;
123596f2e892SBill Paul 
123696f2e892SBill Paul 	/* now program new ones */
12376817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
123896f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
123996f2e892SBill Paul 			continue;
124096f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
124196f2e892SBill Paul 		if (h < 32)
124296f2e892SBill Paul 			hashes[0] |= (1 << h);
124396f2e892SBill Paul 		else
124496f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
124596f2e892SBill Paul 	}
124696f2e892SBill Paul 
124796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
124896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
124996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
125096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
125196f2e892SBill Paul 
125296f2e892SBill Paul 	return;
125396f2e892SBill Paul }
125496f2e892SBill Paul 
1255e3d2833aSAlfred Perlstein void
1256e3d2833aSAlfred Perlstein dc_setfilt_xircom(sc)
1257feb78939SJonathan Chen 	struct dc_softc		*sc;
1258feb78939SJonathan Chen {
1259feb78939SJonathan Chen 	struct dc_desc		*sframe;
1260feb78939SJonathan Chen 	u_int32_t		h, *sp;
1261feb78939SJonathan Chen 	struct ifmultiaddr	*ifma;
1262feb78939SJonathan Chen 	struct ifnet		*ifp;
1263feb78939SJonathan Chen 	int			i;
1264feb78939SJonathan Chen 
1265feb78939SJonathan Chen 	ifp = &sc->arpcom.ac_if;
1266feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
1267feb78939SJonathan Chen 
1268feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1269feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1270feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1271feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
1272feb78939SJonathan Chen 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
1273feb78939SJonathan Chen 	bzero((char *)sp, DC_SFRAME_LEN);
1274feb78939SJonathan Chen 
1275feb78939SJonathan Chen 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
1276feb78939SJonathan Chen 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
1277feb78939SJonathan Chen 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
1278feb78939SJonathan Chen 
1279feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
1280feb78939SJonathan Chen 
1281feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1282feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1283feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1284feb78939SJonathan Chen 	else
1285feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1286feb78939SJonathan Chen 
1287feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1288feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1289feb78939SJonathan Chen 	else
1290feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1291feb78939SJonathan Chen 
12926817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1293feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1294feb78939SJonathan Chen 			continue;
12951d5e5310SBill Paul 		h = dc_crc_le(sc,
12961d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1297feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1298feb78939SJonathan Chen 	}
1299feb78939SJonathan Chen 
1300feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1301feb78939SJonathan Chen 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
1302feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1303feb78939SJonathan Chen 	}
1304feb78939SJonathan Chen 
1305feb78939SJonathan Chen 	/* Set our MAC address */
1306feb78939SJonathan Chen 	sp[0] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
1307feb78939SJonathan Chen 	sp[1] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
1308feb78939SJonathan Chen 	sp[2] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
1309feb78939SJonathan Chen 
1310feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1311feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1312feb78939SJonathan Chen 	ifp->if_flags |= IFF_RUNNING;
1313feb78939SJonathan Chen 	sframe->dc_status = DC_TXSTAT_OWN;
1314feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1315feb78939SJonathan Chen 
1316feb78939SJonathan Chen 	/*
1317feb78939SJonathan Chen 	 * wait some time...
1318feb78939SJonathan Chen 	 */
1319feb78939SJonathan Chen 	DELAY(1000);
1320feb78939SJonathan Chen 
1321feb78939SJonathan Chen 	ifp->if_timer = 5;
1322feb78939SJonathan Chen 
1323feb78939SJonathan Chen 	return;
1324feb78939SJonathan Chen }
1325feb78939SJonathan Chen 
1326e3d2833aSAlfred Perlstein static void
1327e3d2833aSAlfred Perlstein dc_setfilt(sc)
132896f2e892SBill Paul 	struct dc_softc		*sc;
132996f2e892SBill Paul {
133096f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13311af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
133296f2e892SBill Paul 		dc_setfilt_21143(sc);
133396f2e892SBill Paul 
133496f2e892SBill Paul 	if (DC_IS_ASIX(sc))
133596f2e892SBill Paul 		dc_setfilt_asix(sc);
133696f2e892SBill Paul 
133796f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
133896f2e892SBill Paul 		dc_setfilt_admtek(sc);
133996f2e892SBill Paul 
1340feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1341feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
1342feb78939SJonathan Chen 
134396f2e892SBill Paul 	return;
134496f2e892SBill Paul }
134596f2e892SBill Paul 
134696f2e892SBill Paul /*
134796f2e892SBill Paul  * In order to fiddle with the
134896f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
134996f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
135096f2e892SBill Paul  */
1351e3d2833aSAlfred Perlstein static void
1352e3d2833aSAlfred Perlstein dc_setcfg(sc, media)
135396f2e892SBill Paul 	struct dc_softc		*sc;
135496f2e892SBill Paul 	int			media;
135596f2e892SBill Paul {
135696f2e892SBill Paul 	int			i, restart = 0;
135796f2e892SBill Paul 	u_int32_t		isr;
135896f2e892SBill Paul 
135996f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
136096f2e892SBill Paul 		return;
136196f2e892SBill Paul 
136296f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
136396f2e892SBill Paul 		restart = 1;
136496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
136596f2e892SBill Paul 
136696f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
136796f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1368d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1369351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1370351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
137196f2e892SBill Paul 				break;
1372d467c136SBill Paul 			DELAY(10);
137396f2e892SBill Paul 		}
137496f2e892SBill Paul 
137596f2e892SBill Paul 		if (i == DC_TIMEOUT)
137696f2e892SBill Paul 			printf("dc%d: failed to force tx and "
137796f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
137896f2e892SBill Paul 	}
137996f2e892SBill Paul 
138096f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1381042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1382042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
138396f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
13848273d5f8SBill Paul 			int	watchdogreg;
13858273d5f8SBill Paul 
1386bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
13878273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
13888273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
13898273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
13908273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
13914c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1392bf645417SBill Paul 			} else {
1393bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1394bf645417SBill Paul 			}
139596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
139696f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
139796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
139896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
139996f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
140088d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
140196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
140296f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1403e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1404e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
140596f2e892SBill Paul 		} else {
140696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
140796f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
140896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
140996f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
141096f2e892SBill Paul 			}
1411318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1412318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1413318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14145c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14155c1cfac4SBill Paul 				dc_apply_fixup(sc,
14165c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14175c1cfac4SBill Paul 				    IFM_100_TX|IFM_FDX : IFM_100_TX);
141896f2e892SBill Paul 		}
141996f2e892SBill Paul 	}
142096f2e892SBill Paul 
142196f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1422042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1423042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
142496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14258273d5f8SBill Paul 			int	watchdogreg;
14268273d5f8SBill Paul 
14278273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
14284c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14298273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14308273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14318273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14328273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14334c2efe27SBill Paul 			} else {
14344c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14354c2efe27SBill Paul 			}
143696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
143796f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
143896f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
143996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
144088d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
144196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
144296f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1443e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1444e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
144596f2e892SBill Paul 		} else {
144696f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
144796f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
144896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
144996f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
145096f2e892SBill Paul 			}
145196f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1452318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14545c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14555c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14565c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14575c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14585c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14595c1cfac4SBill Paul 				else
14605c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14615c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14625c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14635c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14645c1cfac4SBill Paul 				dc_apply_fixup(sc,
14655c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14665c1cfac4SBill Paul 				    IFM_10_T|IFM_FDX : IFM_10_T);
14675c1cfac4SBill Paul 				DELAY(20000);
14685c1cfac4SBill Paul 			}
146996f2e892SBill Paul 		}
147096f2e892SBill Paul 	}
147196f2e892SBill Paul 
1472f43d9309SBill Paul 	/*
1473f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1474f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1475f43d9309SBill Paul 	 * on the external MII port.
1476f43d9309SBill Paul 	 */
1477f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
147845521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1479f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1480f43d9309SBill Paul 			sc->dc_link = 1;
1481f43d9309SBill Paul 		} else {
1482f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1483f43d9309SBill Paul 		}
1484f43d9309SBill Paul 	}
1485f43d9309SBill Paul 
14867e346229SMartin Blapp 	if (DC_IS_ADMTEK(sc))
14877e346229SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
14887e346229SMartin Blapp 
148996f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149296f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149396f2e892SBill Paul 	} else {
149496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149696f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149796f2e892SBill Paul 	}
149896f2e892SBill Paul 
149996f2e892SBill Paul 	if (restart)
150096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
150196f2e892SBill Paul 
150296f2e892SBill Paul 	return;
150396f2e892SBill Paul }
150496f2e892SBill Paul 
1505e3d2833aSAlfred Perlstein static void
1506e3d2833aSAlfred Perlstein dc_reset(sc)
150796f2e892SBill Paul 	struct dc_softc		*sc;
150896f2e892SBill Paul {
150996f2e892SBill Paul 	register int		i;
151096f2e892SBill Paul 
151196f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
151296f2e892SBill Paul 
151396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151496f2e892SBill Paul 		DELAY(10);
151596f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
151696f2e892SBill Paul 			break;
151796f2e892SBill Paul 	}
151896f2e892SBill Paul 
15191af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15201d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
152196f2e892SBill Paul 		DELAY(10000);
152296f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152396f2e892SBill Paul 		i = 0;
152496f2e892SBill Paul 	}
152596f2e892SBill Paul 
152696f2e892SBill Paul 	if (i == DC_TIMEOUT)
152796f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
152896f2e892SBill Paul 
152996f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
153096f2e892SBill Paul 	DELAY(1000);
153196f2e892SBill Paul 
153296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
153596f2e892SBill Paul 
153691cc2adbSBill Paul 	/*
153791cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
153891cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
153991cc2adbSBill Paul 	 * into a state where it will never come out of reset
154091cc2adbSBill Paul 	 * until we reset the whole chip again.
154191cc2adbSBill Paul 	 */
15425c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154391cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15445c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15455c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15465c1cfac4SBill Paul 	}
154791cc2adbSBill Paul 
154896f2e892SBill Paul 	return;
154996f2e892SBill Paul }
155096f2e892SBill Paul 
1551e3d2833aSAlfred Perlstein static struct dc_type *
1552e3d2833aSAlfred Perlstein dc_devtype(dev)
155396f2e892SBill Paul 	device_t		dev;
155496f2e892SBill Paul {
155596f2e892SBill Paul 	struct dc_type		*t;
155696f2e892SBill Paul 	u_int32_t		rev;
155796f2e892SBill Paul 
155896f2e892SBill Paul 	t = dc_devs;
155996f2e892SBill Paul 
156096f2e892SBill Paul 	while(t->dc_name != NULL) {
156196f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
156296f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
156396f2e892SBill Paul 			/* Check the PCI revision */
156496f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
156596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
156696f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
156796f2e892SBill Paul 				t++;
156896f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
156996f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
157096f2e892SBill Paul 				t++;
157196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
157279d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
157379d11e09SBill Paul 				t++;
157479d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
157596f2e892SBill Paul 			    rev >= DC_REVISION_98725)
157696f2e892SBill Paul 				t++;
157796f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
157896f2e892SBill Paul 			    rev >= DC_REVISION_88141)
157996f2e892SBill Paul 				t++;
158096f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
158196f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
158296f2e892SBill Paul 				t++;
158388d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
158488d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
158588d739dcSBill Paul 				t++;
158696f2e892SBill Paul 			return(t);
158796f2e892SBill Paul 		}
158896f2e892SBill Paul 		t++;
158996f2e892SBill Paul 	}
159096f2e892SBill Paul 
159196f2e892SBill Paul 	return(NULL);
159296f2e892SBill Paul }
159396f2e892SBill Paul 
159496f2e892SBill Paul /*
159596f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
159696f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
159796f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
159896f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
159996f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
160096f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
160196f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
160296f2e892SBill Paul  */
1603e3d2833aSAlfred Perlstein static int
1604e3d2833aSAlfred Perlstein dc_probe(dev)
160596f2e892SBill Paul 	device_t		dev;
160696f2e892SBill Paul {
160796f2e892SBill Paul 	struct dc_type		*t;
160896f2e892SBill Paul 
160996f2e892SBill Paul 	t = dc_devtype(dev);
161096f2e892SBill Paul 
161196f2e892SBill Paul 	if (t != NULL) {
161296f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
161396f2e892SBill Paul 		return(0);
161496f2e892SBill Paul 	}
161596f2e892SBill Paul 
161696f2e892SBill Paul 	return(ENXIO);
161796f2e892SBill Paul }
161896f2e892SBill Paul 
1619e3d2833aSAlfred Perlstein static void
1620e3d2833aSAlfred Perlstein dc_acpi(dev)
162196f2e892SBill Paul 	device_t		dev;
162296f2e892SBill Paul {
162396f2e892SBill Paul 	int			unit;
162496f2e892SBill Paul 
162596f2e892SBill Paul 	unit = device_get_unit(dev);
162696f2e892SBill Paul 
162714a00c6cSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
162896f2e892SBill Paul 		u_int32_t		iobase, membase, irq;
162996f2e892SBill Paul 
163096f2e892SBill Paul 		/* Save important PCI config data. */
163196f2e892SBill Paul 		iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
163296f2e892SBill Paul 		membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
163396f2e892SBill Paul 		irq = pci_read_config(dev, DC_PCI_CFIT, 4);
163496f2e892SBill Paul 
163596f2e892SBill Paul 		/* Reset the power state. */
163696f2e892SBill Paul 		printf("dc%d: chip is in D%d power mode "
163714a00c6cSBill Paul 		    "-- setting to D0\n", unit,
163814a00c6cSBill Paul 		    pci_get_powerstate(dev));
163914a00c6cSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
164096f2e892SBill Paul 
164196f2e892SBill Paul 		/* Restore PCI config data. */
164296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
164396f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
164496f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFIT, irq, 4);
164596f2e892SBill Paul 	}
164614a00c6cSBill Paul 
164796f2e892SBill Paul 	return;
164896f2e892SBill Paul }
164996f2e892SBill Paul 
1650e3d2833aSAlfred Perlstein static void
1651e3d2833aSAlfred Perlstein dc_apply_fixup(sc, media)
16525c1cfac4SBill Paul 	struct dc_softc		*sc;
16535c1cfac4SBill Paul 	int			media;
16545c1cfac4SBill Paul {
16555c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16565c1cfac4SBill Paul 	u_int8_t		*p;
16575c1cfac4SBill Paul 	int			i;
16585d801891SBill Paul 	u_int32_t		reg;
16595c1cfac4SBill Paul 
16605c1cfac4SBill Paul 	m = sc->dc_mi;
16615c1cfac4SBill Paul 
16625c1cfac4SBill Paul 	while (m != NULL) {
16635c1cfac4SBill Paul 		if (m->dc_media == media)
16645c1cfac4SBill Paul 			break;
16655c1cfac4SBill Paul 		m = m->dc_next;
16665c1cfac4SBill Paul 	}
16675c1cfac4SBill Paul 
16685c1cfac4SBill Paul 	if (m == NULL)
16695c1cfac4SBill Paul 		return;
16705c1cfac4SBill Paul 
16715c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16725c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16735c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16745c1cfac4SBill Paul 	}
16755c1cfac4SBill Paul 
16765c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16775c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16785c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16795c1cfac4SBill Paul 	}
16805c1cfac4SBill Paul 
16815c1cfac4SBill Paul 	return;
16825c1cfac4SBill Paul }
16835c1cfac4SBill Paul 
1684e3d2833aSAlfred Perlstein static void
1685e3d2833aSAlfred Perlstein dc_decode_leaf_sia(sc, l)
16865c1cfac4SBill Paul 	struct dc_softc		*sc;
16875c1cfac4SBill Paul 	struct dc_eblock_sia	*l;
16885c1cfac4SBill Paul {
16895c1cfac4SBill Paul 	struct dc_mediainfo	*m;
16905c1cfac4SBill Paul 
16915c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
16923019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
16935c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
16945c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
16955c1cfac4SBill Paul 
16965c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
16975c1cfac4SBill Paul 		m->dc_media = IFM_10_T|IFM_FDX;
16985c1cfac4SBill Paul 
16995c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
17005c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
17015c1cfac4SBill Paul 
17025c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
17035c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
17045c1cfac4SBill Paul 
17055c1cfac4SBill Paul 	m->dc_gp_len = 2;
17065c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
17075c1cfac4SBill Paul 
17085c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17095c1cfac4SBill Paul 	sc->dc_mi = m;
17105c1cfac4SBill Paul 
17115c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
17125c1cfac4SBill Paul 
17135c1cfac4SBill Paul 	return;
17145c1cfac4SBill Paul }
17155c1cfac4SBill Paul 
1716e3d2833aSAlfred Perlstein static void
1717e3d2833aSAlfred Perlstein dc_decode_leaf_sym(sc, l)
17185c1cfac4SBill Paul 	struct dc_softc		*sc;
17195c1cfac4SBill Paul 	struct dc_eblock_sym	*l;
17205c1cfac4SBill Paul {
17215c1cfac4SBill Paul 	struct dc_mediainfo	*m;
17225c1cfac4SBill Paul 
17235c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
17243019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
17255c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
17265c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
17275c1cfac4SBill Paul 
17285c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
17295c1cfac4SBill Paul 		m->dc_media = IFM_100_TX|IFM_FDX;
17305c1cfac4SBill Paul 
17315c1cfac4SBill Paul 	m->dc_gp_len = 2;
17325c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
17335c1cfac4SBill Paul 
17345c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17355c1cfac4SBill Paul 	sc->dc_mi = m;
17365c1cfac4SBill Paul 
17375c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
17385c1cfac4SBill Paul 
17395c1cfac4SBill Paul 	return;
17405c1cfac4SBill Paul }
17415c1cfac4SBill Paul 
1742e3d2833aSAlfred Perlstein static void
1743e3d2833aSAlfred Perlstein dc_decode_leaf_mii(sc, l)
17445c1cfac4SBill Paul 	struct dc_softc		*sc;
17455c1cfac4SBill Paul 	struct dc_eblock_mii	*l;
17465c1cfac4SBill Paul {
17475c1cfac4SBill Paul 	u_int8_t		*p;
17485c1cfac4SBill Paul 	struct dc_mediainfo	*m;
17495c1cfac4SBill Paul 
17505c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
17513019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
17525c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17535c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17545c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17555c1cfac4SBill Paul 
17565c1cfac4SBill Paul 	p = (u_int8_t *)l;
17575c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17585c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17595c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17605c1cfac4SBill Paul 	m->dc_reset_len = *p;
17615c1cfac4SBill Paul 	p++;
17625c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17635c1cfac4SBill Paul 
17645c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17655c1cfac4SBill Paul 	sc->dc_mi = m;
17665c1cfac4SBill Paul 
17675c1cfac4SBill Paul 	return;
17685c1cfac4SBill Paul }
17695c1cfac4SBill Paul 
1770e3d2833aSAlfred Perlstein static void
1771e3d2833aSAlfred Perlstein dc_parse_21143_srom(sc)
17725c1cfac4SBill Paul 	struct dc_softc		*sc;
17735c1cfac4SBill Paul {
17745c1cfac4SBill Paul 	struct dc_leaf_hdr	*lhdr;
17755c1cfac4SBill Paul 	struct dc_eblock_hdr	*hdr;
17765c1cfac4SBill Paul 	int			i, loff;
17775c1cfac4SBill Paul 	char			*ptr;
17785c1cfac4SBill Paul 
17795c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17805c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17815c1cfac4SBill Paul 
17825c1cfac4SBill Paul 	ptr = (char *)lhdr;
17835c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17845c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17855c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17865c1cfac4SBill Paul 		switch(hdr->dc_type) {
17875c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17885c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17895c1cfac4SBill Paul 			break;
17905c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
17915c1cfac4SBill Paul 			dc_decode_leaf_sia(sc, (struct dc_eblock_sia *)hdr);
17925c1cfac4SBill Paul 			break;
17935c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
17945c1cfac4SBill Paul 			dc_decode_leaf_sym(sc, (struct dc_eblock_sym *)hdr);
17955c1cfac4SBill Paul 			break;
17965c1cfac4SBill Paul 		default:
17975c1cfac4SBill Paul 			/* Don't care. Yet. */
17985c1cfac4SBill Paul 			break;
17995c1cfac4SBill Paul 		}
18005c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18015c1cfac4SBill Paul 		ptr++;
18025c1cfac4SBill Paul 	}
18035c1cfac4SBill Paul 
18045c1cfac4SBill Paul 	return;
18055c1cfac4SBill Paul }
18065c1cfac4SBill Paul 
180796f2e892SBill Paul /*
180896f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
180996f2e892SBill Paul  * setup and ethernet/BPF attach.
181096f2e892SBill Paul  */
1811e3d2833aSAlfred Perlstein static int
1812e3d2833aSAlfred Perlstein dc_attach(dev)
181396f2e892SBill Paul 	device_t		dev;
181496f2e892SBill Paul {
1815d1ce9105SBill Paul 	int			tmp = 0;
181696f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
181796f2e892SBill Paul 	u_int32_t		command;
181896f2e892SBill Paul 	struct dc_softc		*sc;
181996f2e892SBill Paul 	struct ifnet		*ifp;
182096f2e892SBill Paul 	u_int32_t		revision;
182196f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
182296f2e892SBill Paul 
182396f2e892SBill Paul 	sc = device_get_softc(dev);
182496f2e892SBill Paul 	unit = device_get_unit(dev);
182596f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
182696f2e892SBill Paul 
18276008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
18286008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
1829031fc810SBill Paul 
183096f2e892SBill Paul 	/*
183196f2e892SBill Paul 	 * Handle power management nonsense.
183296f2e892SBill Paul 	 */
183396f2e892SBill Paul 	dc_acpi(dev);
183496f2e892SBill Paul 
183596f2e892SBill Paul 	/*
183696f2e892SBill Paul 	 * Map control/status registers.
183796f2e892SBill Paul 	 */
183807f65363SBill Paul 	pci_enable_busmaster(dev);
183975ff968cSBill Paul 	pci_enable_io(dev, SYS_RES_IOPORT);
184075ff968cSBill Paul 	pci_enable_io(dev, SYS_RES_MEMORY);
1841c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
184296f2e892SBill Paul 
184396f2e892SBill Paul #ifdef DC_USEIOSPACE
184496f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
184596f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
184696f2e892SBill Paul 		error = ENXIO;
18479ebe64caSPoul-Henning Kamp 		goto fail_nolock;
184896f2e892SBill Paul 	}
184996f2e892SBill Paul #else
185096f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
185196f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
185296f2e892SBill Paul 		error = ENXIO;
18539ebe64caSPoul-Henning Kamp 		goto fail_nolock;
185496f2e892SBill Paul 	}
185596f2e892SBill Paul #endif
185696f2e892SBill Paul 
185796f2e892SBill Paul 	rid = DC_RID;
185896f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
185996f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
186096f2e892SBill Paul 
186196f2e892SBill Paul 	if (sc->dc_res == NULL) {
186296f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
186396f2e892SBill Paul 		error = ENXIO;
18649ebe64caSPoul-Henning Kamp 		goto fail_nolock;
186596f2e892SBill Paul 	}
186696f2e892SBill Paul 
186796f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
186896f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
186996f2e892SBill Paul 
187096f2e892SBill Paul 	/* Allocate interrupt */
187196f2e892SBill Paul 	rid = 0;
187296f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
187396f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
187496f2e892SBill Paul 
187596f2e892SBill Paul 	if (sc->dc_irq == NULL) {
187696f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
187796f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
187896f2e892SBill Paul 		error = ENXIO;
18799ebe64caSPoul-Henning Kamp 		goto fail_nolock;
188096f2e892SBill Paul 	}
188196f2e892SBill Paul 
1882b50c6312SJonathan Lemon 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET |
1883b50c6312SJonathan Lemon 	    (IS_MPSAFE ? INTR_MPSAFE : 0),
188496f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
188596f2e892SBill Paul 
188696f2e892SBill Paul 	if (error) {
188796f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
188896f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
188996f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
18909ebe64caSPoul-Henning Kamp 		goto fail_nolock;
189196f2e892SBill Paul 	}
18929ebe64caSPoul-Henning Kamp 	DC_LOCK(sc);
189396f2e892SBill Paul 
189496f2e892SBill Paul 	/* Need this info to decide on a chip type. */
189596f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
189696f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
189796f2e892SBill Paul 
189896f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
189996f2e892SBill Paul 	case DC_DEVICEID_21143:
190096f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
190196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1902042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19035c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
19045c1cfac4SBill Paul 		dc_read_eeprom(sc, (caddr_t)&sc->dc_srom, 0, 512, 0);
190596f2e892SBill Paul 		break;
190696f2e892SBill Paul 	case DC_DEVICEID_DM9100:
190796f2e892SBill Paul 	case DC_DEVICEID_DM9102:
190896f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1909318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_INTR_ALWAYS;
1910318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_TX_STORENFWD;
191196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19120a46b1dcSBill Paul 		/* Increase the latency timer value. */
19130a46b1dcSBill Paul 		command = pci_read_config(dev, DC_PCI_CFLT, 4);
19140a46b1dcSBill Paul 		command &= 0xFFFF00FF;
19150a46b1dcSBill Paul 		command |= 0x00008000;
19160a46b1dcSBill Paul 		pci_write_config(dev, DC_PCI_CFLT, command, 4);
191796f2e892SBill Paul 		break;
191896f2e892SBill Paul 	case DC_DEVICEID_AL981:
191996f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
192096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
192196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
192296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
192396f2e892SBill Paul 		break;
192496f2e892SBill Paul 	case DC_DEVICEID_AN985:
192541fced74SPeter Wemm 	case DC_DEVICEID_FE2500:
1926fa167b8eSBill Paul 	case DC_DEVICEID_EN2242:
192796f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
192896f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
192996f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
193096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
193196f2e892SBill Paul 		break;
193296f2e892SBill Paul 	case DC_DEVICEID_98713:
193396f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
193496f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
193596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
193696f2e892SBill Paul 		}
1937318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
193896f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1939318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1940318b02fdSBill Paul 		}
1941318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
194296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
194396f2e892SBill Paul 		break;
194496f2e892SBill Paul 	case DC_DEVICEID_987x5:
19459ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
194679d11e09SBill Paul 		/*
194779d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
194879d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
194979d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
195079d11e09SBill Paul 		 * get the right number of bits out of the
195179d11e09SBill Paul 		 * CRC routine.
195279d11e09SBill Paul 		 */
195379d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
195479d11e09SBill Paul 		    revision < DC_REVISION_98725)
195579d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
195696f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
195796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1958318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
195996f2e892SBill Paul 		break;
1960ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1961ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1962ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1963ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
1964ead7cde9SBill Paul 		break;
196596f2e892SBill Paul 	case DC_DEVICEID_82C115:
196696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
196779d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR|DC_128BIT_HASH;
1968318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
196996f2e892SBill Paul 		break;
197096f2e892SBill Paul 	case DC_DEVICEID_82C168:
197196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
197291cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
197396f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
197496f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
197596f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
197696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
197796f2e892SBill Paul 		break;
197896f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
197996f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
198096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
198196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
198296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
198396f2e892SBill Paul 		break;
1984feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
1985feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
19862dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
19872dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
1988feb78939SJonathan Chen 		/*
1989feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1990feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
19912dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
1992feb78939SJonathan Chen 		 */
1993feb78939SJonathan Chen 		break;
19941af8bec7SBill Paul 	case DC_DEVICEID_RS7112:
19951af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
19961af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
19971af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19981af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19991af8bec7SBill Paul 		dc_read_eeprom(sc, (caddr_t)&sc->dc_srom, 0, 256, 0);
20001af8bec7SBill Paul 		break;
200196f2e892SBill Paul 	default:
200296f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
200396f2e892SBill Paul 		    sc->dc_info->dc_did);
200496f2e892SBill Paul 		break;
200596f2e892SBill Paul 	}
200696f2e892SBill Paul 
200796f2e892SBill Paul 	/* Save the cache line size. */
200888d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
200988d739dcSBill Paul 		sc->dc_cachesize = 0;
201088d739dcSBill Paul 	else
201188d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
201288d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
201396f2e892SBill Paul 
201496f2e892SBill Paul 	/* Reset the adapter. */
201596f2e892SBill Paul 	dc_reset(sc);
201696f2e892SBill Paul 
201796f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2018feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
201996f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
202096f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
202196f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
202296f2e892SBill Paul 	}
202396f2e892SBill Paul 
202496f2e892SBill Paul 	/*
202596f2e892SBill Paul 	 * Try to learn something about the supported media.
202696f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
202796f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
202896f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
202996f2e892SBill Paul 	 * Intel 21143.
203096f2e892SBill Paul 	 */
20315c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
20325c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
20335c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
203496f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
203596f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
203696f2e892SBill Paul 		else
203796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
203896f2e892SBill Paul 	} else if (!sc->dc_pmode)
203996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
204096f2e892SBill Paul 
204196f2e892SBill Paul 	/*
204296f2e892SBill Paul 	 * Get station address from the EEPROM.
204396f2e892SBill Paul 	 */
204496f2e892SBill Paul 	switch(sc->dc_type) {
204596f2e892SBill Paul 	case DC_TYPE_98713:
204696f2e892SBill Paul 	case DC_TYPE_98713A:
204796f2e892SBill Paul 	case DC_TYPE_987x5:
204896f2e892SBill Paul 	case DC_TYPE_PNICII:
204996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
205096f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
205196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
205296f2e892SBill Paul 		break;
205396f2e892SBill Paul 	case DC_TYPE_PNIC:
205496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
205596f2e892SBill Paul 		break;
205696f2e892SBill Paul 	case DC_TYPE_DM9102:
205796f2e892SBill Paul 	case DC_TYPE_21143:
205896f2e892SBill Paul 	case DC_TYPE_ASIX:
205996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
206096f2e892SBill Paul 		break;
206196f2e892SBill Paul 	case DC_TYPE_AL981:
206296f2e892SBill Paul 	case DC_TYPE_AN985:
206396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
206496f2e892SBill Paul 		break;
20651af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20661af8bec7SBill Paul 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr, 6);
20671af8bec7SBill Paul 		break;
2068feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
2069feb78939SJonathan Chen 		dc_read_eeprom(sc, (caddr_t)&eaddr, 3, 3, 0);
2070feb78939SJonathan Chen 		break;
207196f2e892SBill Paul 	default:
207296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
207396f2e892SBill Paul 		break;
207496f2e892SBill Paul 	}
207596f2e892SBill Paul 
207696f2e892SBill Paul 	/*
207796f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
207896f2e892SBill Paul 	 */
207996f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
208096f2e892SBill Paul 
208196f2e892SBill Paul 	sc->dc_unit = unit;
208296f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
208396f2e892SBill Paul 
208496f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
208596f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
208696f2e892SBill Paul 
208796f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
208896f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
208996f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
209096f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
209196f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
209296f2e892SBill Paul 		error = ENXIO;
209396f2e892SBill Paul 		goto fail;
209496f2e892SBill Paul 	}
209596f2e892SBill Paul 
209696f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
209796f2e892SBill Paul 
209896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
209996f2e892SBill Paul 	ifp->if_softc = sc;
210096f2e892SBill Paul 	ifp->if_unit = unit;
210196f2e892SBill Paul 	ifp->if_name = "dc";
2102feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
210396f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
210496f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
210596f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
210696f2e892SBill Paul 	ifp->if_output = ether_output;
210796f2e892SBill Paul 	ifp->if_start = dc_start;
210896f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
210996f2e892SBill Paul 	ifp->if_init = dc_init;
211096f2e892SBill Paul 	ifp->if_baudrate = 10000000;
211196f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
211296f2e892SBill Paul 
211396f2e892SBill Paul 	/*
21145c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
21155c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
21165c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
21175c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
21185c1cfac4SBill Paul 	 * driver instead.
211996f2e892SBill Paul 	 */
21205c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
21215c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
21225c1cfac4SBill Paul 		tmp = sc->dc_pmode;
21235c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
21245c1cfac4SBill Paul 	}
21255c1cfac4SBill Paul 
212696f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
212796f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
212896f2e892SBill Paul 
212996f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
21305c1cfac4SBill Paul 		sc->dc_pmode = tmp;
21315c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
213296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2133042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
213496f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
213596f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
213678999dd1SBill Paul 		/*
213778999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
213878999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
213978999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
214078999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
214178999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
214278999dd1SBill Paul 		 */
214378999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
214478999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
214596f2e892SBill Paul 		error = 0;
214696f2e892SBill Paul 	}
214796f2e892SBill Paul 
214896f2e892SBill Paul 	if (error) {
214996f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
215096f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
215196f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
215296f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
215396f2e892SBill Paul 		error = ENXIO;
215496f2e892SBill Paul 		goto fail;
215596f2e892SBill Paul 	}
215696f2e892SBill Paul 
2157feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
2158feb78939SJonathan Chen 		/*
2159feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
2160feb78939SJonathan Chen 		 * can talk to the MII.
2161feb78939SJonathan Chen 		 */
2162feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2163feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2164feb78939SJonathan Chen 		DELAY(10);
2165feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2166feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2167feb78939SJonathan Chen 		DELAY(10);
2168feb78939SJonathan Chen 	}
2169feb78939SJonathan Chen 
217096f2e892SBill Paul 	/*
217121b8ebd9SArchie Cobbs 	 * Call MI attach routine.
217296f2e892SBill Paul 	 */
217321b8ebd9SArchie Cobbs 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
2174db40c1aeSDoug Ambrisko 
2175db40c1aeSDoug Ambrisko 	/*
2176db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2177db40c1aeSDoug Ambrisko 	 */
2178db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
2179db40c1aeSDoug Ambrisko 
2180b50c6312SJonathan Lemon 	callout_init(&sc->dc_stat_ch, IS_MPSAFE);
218196f2e892SBill Paul 
21825c1cfac4SBill Paul #ifdef SRM_MEDIA
2183510a809eSMike Smith 	sc->dc_srm_media = 0;
2184510a809eSMike Smith 
2185510a809eSMike Smith 	/* Remember the SRM console media setting */
2186510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2187510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2188510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
2189510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2190510a809eSMike Smith 		case 3:
2191510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2192510a809eSMike Smith 			break;
2193510a809eSMike Smith 		case 4:
2194510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2195510a809eSMike Smith 			break;
2196510a809eSMike Smith 		case 5:
2197510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2198510a809eSMike Smith 			break;
2199510a809eSMike Smith 		case 6:
2200510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2201510a809eSMike Smith 			break;
2202510a809eSMike Smith 		}
2203510a809eSMike Smith 		if (sc->dc_srm_media)
2204510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2205510a809eSMike Smith 	}
2206510a809eSMike Smith #endif
2207510a809eSMike Smith 
2208d1ce9105SBill Paul 	DC_UNLOCK(sc);
2209d1ce9105SBill Paul 	return(0);
2210510a809eSMike Smith 
221196f2e892SBill Paul fail:
2212d1ce9105SBill Paul 	DC_UNLOCK(sc);
22139ebe64caSPoul-Henning Kamp fail_nolock:
2214d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
221596f2e892SBill Paul 	return(error);
221696f2e892SBill Paul }
221796f2e892SBill Paul 
2218e3d2833aSAlfred Perlstein static int
2219e3d2833aSAlfred Perlstein dc_detach(dev)
222096f2e892SBill Paul 	device_t		dev;
222196f2e892SBill Paul {
222296f2e892SBill Paul 	struct dc_softc		*sc;
222396f2e892SBill Paul 	struct ifnet		*ifp;
22245c1cfac4SBill Paul 	struct dc_mediainfo	*m;
222596f2e892SBill Paul 
222696f2e892SBill Paul 	sc = device_get_softc(dev);
2227d1ce9105SBill Paul 
2228d1ce9105SBill Paul 	DC_LOCK(sc);
2229d1ce9105SBill Paul 
223096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
223196f2e892SBill Paul 
223296f2e892SBill Paul 	dc_stop(sc);
223321b8ebd9SArchie Cobbs 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
223496f2e892SBill Paul 
223596f2e892SBill Paul 	bus_generic_detach(dev);
223696f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
223796f2e892SBill Paul 
223896f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
223996f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
224096f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
224196f2e892SBill Paul 
224296f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
224396f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
224496f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
224596f2e892SBill Paul 
22465c1cfac4SBill Paul 	while(sc->dc_mi != NULL) {
22475c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
22485c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
22495c1cfac4SBill Paul 		sc->dc_mi = m;
22505c1cfac4SBill Paul 	}
22515c1cfac4SBill Paul 
2252d1ce9105SBill Paul 	DC_UNLOCK(sc);
2253d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
225496f2e892SBill Paul 
225596f2e892SBill Paul 	return(0);
225696f2e892SBill Paul }
225796f2e892SBill Paul 
225896f2e892SBill Paul /*
225996f2e892SBill Paul  * Initialize the transmit descriptors.
226096f2e892SBill Paul  */
2261e3d2833aSAlfred Perlstein static int
2262e3d2833aSAlfred Perlstein dc_list_tx_init(sc)
226396f2e892SBill Paul 	struct dc_softc		*sc;
226496f2e892SBill Paul {
226596f2e892SBill Paul 	struct dc_chain_data	*cd;
226696f2e892SBill Paul 	struct dc_list_data	*ld;
226701faf54bSLuigi Rizzo 	int			i, nexti;
226896f2e892SBill Paul 
226996f2e892SBill Paul 	cd = &sc->dc_cdata;
227096f2e892SBill Paul 	ld = sc->dc_ldata;
227196f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
227201faf54bSLuigi Rizzo 		nexti = (i == (DC_TX_LIST_CNT - 1)) ? 0 : i+1;
227301faf54bSLuigi Rizzo 		ld->dc_tx_list[i].dc_next = vtophys(&ld->dc_tx_list[nexti]);
227496f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
227596f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
227696f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
227796f2e892SBill Paul 	}
227896f2e892SBill Paul 
227996f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
228096f2e892SBill Paul 
228196f2e892SBill Paul 	return(0);
228296f2e892SBill Paul }
228396f2e892SBill Paul 
228496f2e892SBill Paul 
228596f2e892SBill Paul /*
228696f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
228796f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
228896f2e892SBill Paul  * points back to the first.
228996f2e892SBill Paul  */
2290e3d2833aSAlfred Perlstein static int
2291e3d2833aSAlfred Perlstein dc_list_rx_init(sc)
229296f2e892SBill Paul 	struct dc_softc		*sc;
229396f2e892SBill Paul {
229496f2e892SBill Paul 	struct dc_chain_data	*cd;
229596f2e892SBill Paul 	struct dc_list_data	*ld;
229601faf54bSLuigi Rizzo 	int			i, nexti;
229796f2e892SBill Paul 
229896f2e892SBill Paul 	cd = &sc->dc_cdata;
229996f2e892SBill Paul 	ld = sc->dc_ldata;
230096f2e892SBill Paul 
230196f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
230296f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
230396f2e892SBill Paul 			return(ENOBUFS);
230401faf54bSLuigi Rizzo 		nexti = (i == (DC_RX_LIST_CNT - 1)) ? 0 : i+1;
230501faf54bSLuigi Rizzo 		ld->dc_rx_list[i].dc_next = vtophys(&ld->dc_rx_list[nexti]);
230696f2e892SBill Paul 	}
230796f2e892SBill Paul 
230896f2e892SBill Paul 	cd->dc_rx_prod = 0;
230996f2e892SBill Paul 
231096f2e892SBill Paul 	return(0);
231196f2e892SBill Paul }
231296f2e892SBill Paul 
231396f2e892SBill Paul /*
231496f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
231596f2e892SBill Paul  */
2316e3d2833aSAlfred Perlstein static int
2317e3d2833aSAlfred Perlstein dc_newbuf(sc, i, m)
231896f2e892SBill Paul 	struct dc_softc		*sc;
231996f2e892SBill Paul 	int			i;
232096f2e892SBill Paul 	struct mbuf		*m;
232196f2e892SBill Paul {
232296f2e892SBill Paul 	struct mbuf		*m_new = NULL;
232396f2e892SBill Paul 	struct dc_desc		*c;
232496f2e892SBill Paul 
232596f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
232696f2e892SBill Paul 
232796f2e892SBill Paul 	if (m == NULL) {
232896f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
232940129585SLuigi Rizzo 		if (m_new == NULL)
233096f2e892SBill Paul 			return(ENOBUFS);
233196f2e892SBill Paul 
233296f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
233396f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
233496f2e892SBill Paul 			m_freem(m_new);
233596f2e892SBill Paul 			return(ENOBUFS);
233696f2e892SBill Paul 		}
233796f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
233896f2e892SBill Paul 	} else {
233996f2e892SBill Paul 		m_new = m;
234096f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
234196f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
234296f2e892SBill Paul 	}
234396f2e892SBill Paul 
234496f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
234596f2e892SBill Paul 
234696f2e892SBill Paul 	/*
234796f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
234896f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
234996f2e892SBill Paul 	 * 82c169 chips.
235096f2e892SBill Paul 	 */
235196f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
235296f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
235396f2e892SBill Paul 
235496f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
235596f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
235696f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
235796f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
235896f2e892SBill Paul 
235996f2e892SBill Paul 	return(0);
236096f2e892SBill Paul }
236196f2e892SBill Paul 
236296f2e892SBill Paul /*
236396f2e892SBill Paul  * Grrrrr.
236496f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
236596f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
236696f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
236796f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
236896f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
236996f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
237096f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
237196f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
237296f2e892SBill Paul  *
237396f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
237496f2e892SBill Paul  * Here's what we know:
237596f2e892SBill Paul  *
237696f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
237796f2e892SBill Paul  *   descriptors uploaded.
237896f2e892SBill Paul  *
237996f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
238096f2e892SBill Paul  *   total data upload.
238196f2e892SBill Paul  *
238296f2e892SBill Paul  * - We know the size of the desired received frame because it will be
238396f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
238496f2e892SBill Paul  *
238596f2e892SBill Paul  * Here's what we do:
238696f2e892SBill Paul  *
238796f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
238896f2e892SBill Paul  *   This means that we know that the buffer contents should be all
238996f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
239096f2e892SBill Paul  *
239196f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
239296f2e892SBill Paul  *   ethernet CRC at the end.
239396f2e892SBill Paul  *
239496f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
239596f2e892SBill Paul  *
239696f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
239796f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
239896f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
239996f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
240096f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
240196f2e892SBill Paul  *   we won't be fooled.
240296f2e892SBill Paul  *
240396f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
240496f2e892SBill Paul  *   that value from the current pointer location. This brings us
240596f2e892SBill Paul  *   to the start of the actual received packet.
240696f2e892SBill Paul  *
240796f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
240896f2e892SBill Paul  *   frame length.
240996f2e892SBill Paul  *
241096f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
241196f2e892SBill Paul  * the time.
241296f2e892SBill Paul  */
241396f2e892SBill Paul 
241496f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
2415e3d2833aSAlfred Perlstein static void
2416e3d2833aSAlfred Perlstein dc_pnic_rx_bug_war(sc, idx)
241796f2e892SBill Paul 	struct dc_softc		*sc;
241896f2e892SBill Paul 	int			idx;
241996f2e892SBill Paul {
242096f2e892SBill Paul 	struct dc_desc		*cur_rx;
242196f2e892SBill Paul 	struct dc_desc		*c = NULL;
242296f2e892SBill Paul 	struct mbuf		*m = NULL;
242396f2e892SBill Paul 	unsigned char		*ptr;
242496f2e892SBill Paul 	int			i, total_len;
242596f2e892SBill Paul 	u_int32_t		rxstat = 0;
242696f2e892SBill Paul 
242796f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
242896f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
242996f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
243096f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
243196f2e892SBill Paul 
243296f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
243396f2e892SBill Paul 	while (1) {
243496f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
243596f2e892SBill Paul 		rxstat = c->dc_status;
243696f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
243796f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
243896f2e892SBill Paul 		ptr += DC_RXLEN;
243996f2e892SBill Paul 		/* If this is the last buffer, break out. */
244096f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
244196f2e892SBill Paul 			break;
244296f2e892SBill Paul 		dc_newbuf(sc, i, m);
244396f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
244496f2e892SBill Paul 	}
244596f2e892SBill Paul 
244696f2e892SBill Paul 	/* Find the length of the actual receive frame. */
244796f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
244896f2e892SBill Paul 
244996f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
245096f2e892SBill Paul 	while(*ptr == 0x00)
245196f2e892SBill Paul 		ptr--;
245296f2e892SBill Paul 
245396f2e892SBill Paul 	/* Round off. */
245496f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
245596f2e892SBill Paul 		ptr -= 1;
245696f2e892SBill Paul 
245796f2e892SBill Paul 	/* Now find the start of the frame. */
245896f2e892SBill Paul 	ptr -= total_len;
245996f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
246096f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
246196f2e892SBill Paul 
246296f2e892SBill Paul 	/*
246396f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
246496f2e892SBill Paul 	 * the status word to make it look like a successful
246596f2e892SBill Paul 	 * frame reception.
246696f2e892SBill Paul 	 */
246796f2e892SBill Paul 	dc_newbuf(sc, i, m);
246896f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
246996f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
247096f2e892SBill Paul 
247196f2e892SBill Paul 	return;
247296f2e892SBill Paul }
247396f2e892SBill Paul 
247496f2e892SBill Paul /*
247573bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
247673bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
247773bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
247873bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
247973bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
248073bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
248173bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
248273bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
248373bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
248473bf949cSBill Paul  */
2485e3d2833aSAlfred Perlstein static int
2486e3d2833aSAlfred Perlstein dc_rx_resync(sc)
248773bf949cSBill Paul 	struct dc_softc		*sc;
248873bf949cSBill Paul {
248973bf949cSBill Paul 	int			i, pos;
249073bf949cSBill Paul 	struct dc_desc		*cur_rx;
249173bf949cSBill Paul 
249273bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
249373bf949cSBill Paul 
249473bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
249573bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
249673bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
249773bf949cSBill Paul 			break;
249873bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
249973bf949cSBill Paul 	}
250073bf949cSBill Paul 
250173bf949cSBill Paul 	/* If the ring really is empty, then just return. */
250273bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
250373bf949cSBill Paul 		return(0);
250473bf949cSBill Paul 
250573bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
250673bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
250773bf949cSBill Paul 
250873bf949cSBill Paul 	return(EAGAIN);
250973bf949cSBill Paul }
251073bf949cSBill Paul 
251173bf949cSBill Paul /*
251296f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
251396f2e892SBill Paul  * the higher level protocols.
251496f2e892SBill Paul  */
2515e3d2833aSAlfred Perlstein static void
2516e3d2833aSAlfred Perlstein dc_rxeof(sc)
251796f2e892SBill Paul 	struct dc_softc		*sc;
251896f2e892SBill Paul {
251996f2e892SBill Paul 	struct ether_header	*eh;
252096f2e892SBill Paul 	struct mbuf		*m;
252196f2e892SBill Paul 	struct ifnet		*ifp;
252296f2e892SBill Paul 	struct dc_desc		*cur_rx;
252396f2e892SBill Paul 	int			i, total_len = 0;
252496f2e892SBill Paul 	u_int32_t		rxstat;
252596f2e892SBill Paul 
252696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
252796f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
252896f2e892SBill Paul 
252996f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
253096f2e892SBill Paul 
2531e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
253262f76486SMaxim Sobolev 		if (ifp->if_flags & IFF_POLLING) {
2533e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2534e4fc250cSLuigi Rizzo 				break;
2535e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2536e4fc250cSLuigi Rizzo 		}
2537e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
253896f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
253996f2e892SBill Paul 		rxstat = cur_rx->dc_status;
254096f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
254196f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
254296f2e892SBill Paul 
254396f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
254496f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
254596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
254696f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
254796f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
254896f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
254996f2e892SBill Paul 					continue;
255096f2e892SBill Paul 				}
255196f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
255296f2e892SBill Paul 				rxstat = cur_rx->dc_status;
255396f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
255496f2e892SBill Paul 			}
255596f2e892SBill Paul 		}
255696f2e892SBill Paul 
255796f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
255896f2e892SBill Paul 
255996f2e892SBill Paul 		/*
256096f2e892SBill Paul 		 * If an error occurs, update stats, clear the
256196f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
256296f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2563db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
2564db40c1aeSDoug Ambrisko 		 * frames as errors since they could be vlans
256596f2e892SBill Paul 		 */
2566db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)){
2567db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2568db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2569db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2570db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
257196f2e892SBill Paul 				ifp->if_ierrors++;
257296f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
257396f2e892SBill Paul 					ifp->if_collisions++;
257496f2e892SBill Paul 				dc_newbuf(sc, i, m);
257596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
257696f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
257796f2e892SBill Paul 					continue;
257896f2e892SBill Paul 				} else {
257996f2e892SBill Paul 					dc_init(sc);
258096f2e892SBill Paul 					return;
258196f2e892SBill Paul 				}
258296f2e892SBill Paul 			}
2583db40c1aeSDoug Ambrisko 		}
258496f2e892SBill Paul 
258596f2e892SBill Paul 		/* No errors; receive the packet. */
258696f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
258701faf54bSLuigi Rizzo #ifdef __i386__
258801faf54bSLuigi Rizzo 		/*
258901faf54bSLuigi Rizzo 		 * On the x86 we do not have alignment problems, so try to
259001faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
259101faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
259201faf54bSLuigi Rizzo 		 * copy done in m_devget().
259301faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
259401faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
259501faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
259601faf54bSLuigi Rizzo 		 */
259701faf54bSLuigi Rizzo 		if (dc_quick && dc_newbuf(sc, i, NULL) == 0) {
259801faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
259901faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
260001faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
260101faf54bSLuigi Rizzo 		} else
260201faf54bSLuigi Rizzo #endif
260301faf54bSLuigi Rizzo 		{
260401faf54bSLuigi Rizzo 			struct mbuf *m0;
260596f2e892SBill Paul 
260601faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
260701faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
260896f2e892SBill Paul 			dc_newbuf(sc, i, m);
260996f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
261096f2e892SBill Paul 			if (m0 == NULL) {
261196f2e892SBill Paul 				ifp->if_ierrors++;
261296f2e892SBill Paul 				continue;
261396f2e892SBill Paul 			}
261496f2e892SBill Paul 			m = m0;
261501faf54bSLuigi Rizzo 		}
261696f2e892SBill Paul 
261796f2e892SBill Paul 		ifp->if_ipackets++;
261896f2e892SBill Paul 		eh = mtod(m, struct ether_header *);
261996f2e892SBill Paul 
262096f2e892SBill Paul 		/* Remove header from mbuf and pass it on. */
262196f2e892SBill Paul 		m_adj(m, sizeof(struct ether_header));
262296f2e892SBill Paul 		ether_input(ifp, eh, m);
262396f2e892SBill Paul 	}
262496f2e892SBill Paul 
262596f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
262696f2e892SBill Paul }
262796f2e892SBill Paul 
262896f2e892SBill Paul /*
262996f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
263096f2e892SBill Paul  * the list buffers.
263196f2e892SBill Paul  */
263296f2e892SBill Paul 
2633e3d2833aSAlfred Perlstein static void
2634e3d2833aSAlfred Perlstein dc_txeof(sc)
263596f2e892SBill Paul 	struct dc_softc		*sc;
263696f2e892SBill Paul {
263796f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
263896f2e892SBill Paul 	struct ifnet		*ifp;
263996f2e892SBill Paul 	int			idx;
264096f2e892SBill Paul 
264196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
264296f2e892SBill Paul 
264396f2e892SBill Paul 	/*
264496f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
264596f2e892SBill Paul 	 * frames that have been transmitted.
264696f2e892SBill Paul 	 */
264796f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
264896f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
264996f2e892SBill Paul 		u_int32_t		txstat;
265096f2e892SBill Paul 
265196f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
265296f2e892SBill Paul 		txstat = cur_tx->dc_status;
265396f2e892SBill Paul 
265496f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
265596f2e892SBill Paul 			break;
265696f2e892SBill Paul 
265796f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
265896f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
265996f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
266096f2e892SBill Paul 				/*
266196f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
266296f2e892SBill Paul 				 * that it will sometimes generate a TX
266396f2e892SBill Paul 				 * underrun error while DMAing the RX
266496f2e892SBill Paul 				 * filter setup frame. If we detect this,
266596f2e892SBill Paul 				 * we have to send the setup frame again,
266696f2e892SBill Paul 				 * or else the filter won't be programmed
266796f2e892SBill Paul 				 * correctly.
266896f2e892SBill Paul 				 */
266996f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
267096f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
267196f2e892SBill Paul 						dc_setfilt(sc);
267296f2e892SBill Paul 				}
267396f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
267496f2e892SBill Paul 			}
2675bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
267696f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
267796f2e892SBill Paul 			continue;
267896f2e892SBill Paul 		}
267996f2e892SBill Paul 
268029a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2681feb78939SJonathan Chen 			/*
2682feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2683feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
268429a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
268529a2220aSBill Paul 			 * Who knows, but Conexant chips have the
268629a2220aSBill Paul 			 * same problem. Maybe they took lessons
268729a2220aSBill Paul 			 * from Xircom.
268829a2220aSBill Paul 			 */
2689feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2690feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2691feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
2692feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2693feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2694feb78939SJonathan Chen 		} else {
269596f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
269696f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
269796f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
269896f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
269996f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2700feb78939SJonathan Chen 		}
270196f2e892SBill Paul 
270296f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
270396f2e892SBill Paul 			ifp->if_oerrors++;
270496f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
270596f2e892SBill Paul 				ifp->if_collisions++;
270696f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
270796f2e892SBill Paul 				ifp->if_collisions++;
270896f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
270996f2e892SBill Paul 				dc_init(sc);
271096f2e892SBill Paul 				return;
271196f2e892SBill Paul 			}
271296f2e892SBill Paul 		}
271396f2e892SBill Paul 
271496f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
271596f2e892SBill Paul 
271696f2e892SBill Paul 		ifp->if_opackets++;
271796f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
271896f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
271996f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
272096f2e892SBill Paul 		}
272196f2e892SBill Paul 
272296f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
272396f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
272496f2e892SBill Paul 	}
272596f2e892SBill Paul 
2726bcb9ef4fSLuigi Rizzo 	if (idx != sc->dc_cdata.dc_tx_cons) {
2727bcb9ef4fSLuigi Rizzo 	    	/* some buffers have been freed */
272896f2e892SBill Paul 		sc->dc_cdata.dc_tx_cons = idx;
272996f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
2730bcb9ef4fSLuigi Rizzo 	}
2731bcb9ef4fSLuigi Rizzo 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
273296f2e892SBill Paul 
273396f2e892SBill Paul 	return;
273496f2e892SBill Paul }
273596f2e892SBill Paul 
2736e3d2833aSAlfred Perlstein static void
2737e3d2833aSAlfred Perlstein dc_tick(xsc)
273896f2e892SBill Paul 	void			*xsc;
273996f2e892SBill Paul {
274096f2e892SBill Paul 	struct dc_softc		*sc;
274196f2e892SBill Paul 	struct mii_data		*mii;
274296f2e892SBill Paul 	struct ifnet		*ifp;
274396f2e892SBill Paul 	u_int32_t		r;
274496f2e892SBill Paul 
274596f2e892SBill Paul 	sc = xsc;
2746d1ce9105SBill Paul 	DC_LOCK(sc);
274796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
274896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
274996f2e892SBill Paul 
275096f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2751318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2752318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2753318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2754318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
275596f2e892SBill Paul 				sc->dc_link = 0;
2756318b02fdSBill Paul 				mii_mediachg(mii);
2757318b02fdSBill Paul 			}
2758318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2759318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2760318b02fdSBill Paul 				sc->dc_link = 0;
2761318b02fdSBill Paul 				mii_mediachg(mii);
2762318b02fdSBill Paul 			}
2763d675147eSBill Paul 			if (sc->dc_link == 0)
276496f2e892SBill Paul 				mii_tick(mii);
276596f2e892SBill Paul 		} else {
2766318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
276796f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2768042c8f6eSBill Paul 			    sc->dc_cdata.dc_tx_cnt == 0)
276996f2e892SBill Paul 				mii_tick(mii);
2770042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2771042c8f6eSBill Paul 					sc->dc_link = 0;
277296f2e892SBill Paul 		}
277396f2e892SBill Paul 	} else
277496f2e892SBill Paul 		mii_tick(mii);
277596f2e892SBill Paul 
277696f2e892SBill Paul 	/*
277796f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
277896f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
277996f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
278096f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
278196f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
278296f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
278396f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
278496f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
278596f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
278696f2e892SBill Paul 	 * a screeching halt for several seconds.
278796f2e892SBill Paul 	 *
278896f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
278996f2e892SBill Paul 	 * any packets until a link has been established. After the
279096f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
279196f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
279296f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
279396f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
279496f2e892SBill Paul 	 */
2795cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
279696f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
279796f2e892SBill Paul 		sc->dc_link++;
279896f2e892SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
279996f2e892SBill Paul 			dc_start(ifp);
280096f2e892SBill Paul 	}
280196f2e892SBill Paul 
2802318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2803b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2804318b02fdSBill Paul 	else
2805b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
280696f2e892SBill Paul 
2807d1ce9105SBill Paul 	DC_UNLOCK(sc);
280896f2e892SBill Paul 
280996f2e892SBill Paul 	return;
281096f2e892SBill Paul }
281196f2e892SBill Paul 
2812d467c136SBill Paul /*
2813d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2814d467c136SBill Paul  * or switch to store and forward mode if we have to.
2815d467c136SBill Paul  */
2816e3d2833aSAlfred Perlstein static void
2817e3d2833aSAlfred Perlstein dc_tx_underrun(sc)
2818d467c136SBill Paul 	struct dc_softc		*sc;
2819d467c136SBill Paul {
2820d467c136SBill Paul 	u_int32_t		isr;
2821d467c136SBill Paul 	int			i;
2822d467c136SBill Paul 
2823d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
2824d467c136SBill Paul 		dc_init(sc);
2825d467c136SBill Paul 
2826d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
2827d467c136SBill Paul 		/*
2828d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
2829d467c136SBill Paul 		 * in order to change the transmit threshold or store
2830d467c136SBill Paul 		 * and forward state.
2831d467c136SBill Paul 		 */
2832d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2833d467c136SBill Paul 
2834d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
2835d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
2836d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
2837d467c136SBill Paul 				break;
2838d467c136SBill Paul 			DELAY(10);
2839d467c136SBill Paul 		}
2840d467c136SBill Paul 		if (i == DC_TIMEOUT) {
2841d467c136SBill Paul 			printf("dc%d: failed to force tx to idle state\n",
2842d467c136SBill Paul 			    sc->dc_unit);
2843d467c136SBill Paul 			dc_init(sc);
2844d467c136SBill Paul 		}
2845d467c136SBill Paul 	}
2846d467c136SBill Paul 
2847d467c136SBill Paul 	printf("dc%d: TX underrun -- ", sc->dc_unit);
2848d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
2849d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2850d467c136SBill Paul 		printf("using store and forward mode\n");
2851d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2852d467c136SBill Paul 	} else {
2853d467c136SBill Paul 		printf("increasing TX threshold\n");
2854d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2855d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2856d467c136SBill Paul 	}
2857d467c136SBill Paul 
2858d467c136SBill Paul 	if (DC_IS_INTEL(sc))
2859d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2860d467c136SBill Paul 
2861d467c136SBill Paul 	return;
2862d467c136SBill Paul }
2863d467c136SBill Paul 
2864e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2865e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
2866e4fc250cSLuigi Rizzo 
2867e4fc250cSLuigi Rizzo static void
2868e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2869e4fc250cSLuigi Rizzo {
2870e4fc250cSLuigi Rizzo 	struct	dc_softc *sc = ifp->if_softc;
2871e4fc250cSLuigi Rizzo 
2872e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
2873e4fc250cSLuigi Rizzo 		/* Re-enable interrupts. */
2874e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
2875e4fc250cSLuigi Rizzo 		return;
2876e4fc250cSLuigi Rizzo 	}
2877e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
2878e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
2879e4fc250cSLuigi Rizzo 	dc_txeof(sc);
2880e4fc250cSLuigi Rizzo 	if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE))
2881e4fc250cSLuigi Rizzo 		dc_start(ifp);
2882e4fc250cSLuigi Rizzo 
2883e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2884e4fc250cSLuigi Rizzo 		u_int32_t	status;
2885e4fc250cSLuigi Rizzo 
2886e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
2887e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF|
2888e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF|DC_ISR_TX_IDLE|DC_ISR_TX_UNDERRUN|
2889e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
2890e4fc250cSLuigi Rizzo 		if (!status)
2891e4fc250cSLuigi Rizzo 			return;
2892e4fc250cSLuigi Rizzo 		/* ack what we have */
2893e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
2894e4fc250cSLuigi Rizzo 
2895e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF)) {
2896e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
2897e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
2898e4fc250cSLuigi Rizzo 
2899e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
2900e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
2901e4fc250cSLuigi Rizzo 		}
2902e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
2903e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
2904e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
2905e4fc250cSLuigi Rizzo 
2906e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
2907e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
2908e4fc250cSLuigi Rizzo 
2909e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
2910e4fc250cSLuigi Rizzo 			printf("dc_poll: dc%d bus error\n", sc->dc_unit);
2911e4fc250cSLuigi Rizzo 			dc_reset(sc);
2912e4fc250cSLuigi Rizzo 			dc_init(sc);
2913e4fc250cSLuigi Rizzo 		}
2914e4fc250cSLuigi Rizzo 	}
2915e4fc250cSLuigi Rizzo }
2916e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
2917e4fc250cSLuigi Rizzo 
2918e3d2833aSAlfred Perlstein static void
2919e3d2833aSAlfred Perlstein dc_intr(arg)
292096f2e892SBill Paul 	void			*arg;
292196f2e892SBill Paul {
292296f2e892SBill Paul 	struct dc_softc		*sc;
292396f2e892SBill Paul 	struct ifnet		*ifp;
292496f2e892SBill Paul 	u_int32_t		status;
292596f2e892SBill Paul 
292696f2e892SBill Paul 	sc = arg;
2927d2a1864bSWarner Losh 
2928e8388e14SMitsuru IWASAKI 	if (sc->suspended) {
2929e8388e14SMitsuru IWASAKI 		return;
2930e8388e14SMitsuru IWASAKI 	}
2931e8388e14SMitsuru IWASAKI 
2932d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
2933d2a1864bSWarner Losh 		return;
2934d2a1864bSWarner Losh 
2935d1ce9105SBill Paul 	DC_LOCK(sc);
293696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
2937e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
293862f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
2939e4fc250cSLuigi Rizzo 		goto done;
2940e4fc250cSLuigi Rizzo 	if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */
2941e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
2942e4fc250cSLuigi Rizzo 		goto done;
2943e4fc250cSLuigi Rizzo 	}
2944e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
294596f2e892SBill Paul 
2946d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
294796f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
294896f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
294996f2e892SBill Paul 			dc_stop(sc);
2950d1ce9105SBill Paul 		DC_UNLOCK(sc);
295196f2e892SBill Paul 		return;
295296f2e892SBill Paul 	}
295396f2e892SBill Paul 
295496f2e892SBill Paul 	/* Disable interrupts. */
295596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
295696f2e892SBill Paul 
2957feb78939SJonathan Chen 	while(((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
2958feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
295996f2e892SBill Paul 
296096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
296196f2e892SBill Paul 
296273bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
296373bf949cSBill Paul 			int		curpkts;
296473bf949cSBill Paul 			curpkts = ifp->if_ipackets;
296596f2e892SBill Paul 			dc_rxeof(sc);
296673bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
296773bf949cSBill Paul 				while(dc_rx_resync(sc))
296873bf949cSBill Paul 					dc_rxeof(sc);
296973bf949cSBill Paul 			}
297073bf949cSBill Paul 		}
297196f2e892SBill Paul 
297296f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
297396f2e892SBill Paul 			dc_txeof(sc);
297496f2e892SBill Paul 
297596f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
297696f2e892SBill Paul 			dc_txeof(sc);
297796f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
297896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
297996f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
298096f2e892SBill Paul 			}
298196f2e892SBill Paul 		}
298296f2e892SBill Paul 
2983d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
2984d467c136SBill Paul 			dc_tx_underrun(sc);
298596f2e892SBill Paul 
298696f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
298773bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
298873bf949cSBill Paul 			int		curpkts;
298973bf949cSBill Paul 			curpkts = ifp->if_ipackets;
299096f2e892SBill Paul 			dc_rxeof(sc);
299173bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
299273bf949cSBill Paul 				while(dc_rx_resync(sc))
299373bf949cSBill Paul 					dc_rxeof(sc);
299473bf949cSBill Paul 			}
299573bf949cSBill Paul 		}
299696f2e892SBill Paul 
299796f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
299896f2e892SBill Paul 			dc_reset(sc);
299996f2e892SBill Paul 			dc_init(sc);
300096f2e892SBill Paul 		}
300196f2e892SBill Paul 	}
300296f2e892SBill Paul 
300396f2e892SBill Paul 	/* Re-enable interrupts. */
300496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
300596f2e892SBill Paul 
300696f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
300796f2e892SBill Paul 		dc_start(ifp);
300896f2e892SBill Paul 
3009d9700bb5SBill Paul #ifdef DEVICE_POLLING
3010e4fc250cSLuigi Rizzo done:
3011d9700bb5SBill Paul #endif /* DEVICE_POLLING */
3012d9700bb5SBill Paul 
3013d1ce9105SBill Paul 	DC_UNLOCK(sc);
3014d1ce9105SBill Paul 
301596f2e892SBill Paul 	return;
301696f2e892SBill Paul }
301796f2e892SBill Paul 
301896f2e892SBill Paul /*
301996f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
302096f2e892SBill Paul  * pointers to the fragment pointers.
302196f2e892SBill Paul  */
3022e3d2833aSAlfred Perlstein static int
3023e3d2833aSAlfred Perlstein dc_encap(sc, m_head, txidx)
302496f2e892SBill Paul 	struct dc_softc		*sc;
302596f2e892SBill Paul 	struct mbuf		*m_head;
302696f2e892SBill Paul 	u_int32_t		*txidx;
302796f2e892SBill Paul {
302896f2e892SBill Paul 	struct dc_desc		*f = NULL;
302996f2e892SBill Paul 	struct mbuf		*m;
303096f2e892SBill Paul 	int			frag, cur, cnt = 0;
303196f2e892SBill Paul 
303296f2e892SBill Paul 	/*
303396f2e892SBill Paul 	 * Start packing the mbufs in this chain into
303496f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
303596f2e892SBill Paul 	 * of fragments or hit the end of the mbuf chain.
303696f2e892SBill Paul 	 */
303796f2e892SBill Paul 	m = m_head;
303896f2e892SBill Paul 	cur = frag = *txidx;
303996f2e892SBill Paul 
304096f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
304196f2e892SBill Paul 		if (m->m_len != 0) {
304296f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
304396f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
304496f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
304596f2e892SBill Paul 					return(ENOBUFS);
304696f2e892SBill Paul 			}
304796f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
304896f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
304996f2e892SBill Paul 				return(ENOBUFS);
305096f2e892SBill Paul 
305196f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
305296f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
305396f2e892SBill Paul 			if (cnt == 0) {
305496f2e892SBill Paul 				f->dc_status = 0;
305596f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
305696f2e892SBill Paul 			} else
305796f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
305896f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
305996f2e892SBill Paul 			cur = frag;
306096f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
306196f2e892SBill Paul 			cnt++;
306296f2e892SBill Paul 		}
306396f2e892SBill Paul 	}
306496f2e892SBill Paul 
306596f2e892SBill Paul 	if (m != NULL)
306696f2e892SBill Paul 		return(ENOBUFS);
306796f2e892SBill Paul 
306896f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
306996f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
307096f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
307196f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
307296f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
307391cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
307491cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
307596f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
307696f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
307796f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
307896f2e892SBill Paul 	*txidx = frag;
307996f2e892SBill Paul 
308096f2e892SBill Paul 	return(0);
308196f2e892SBill Paul }
308296f2e892SBill Paul 
308396f2e892SBill Paul /*
3084fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
3085fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
3086fda39fd0SBill Paul  * do scatter/gather correctly.
3087fda39fd0SBill Paul  */
3088e3d2833aSAlfred Perlstein static int
3089e3d2833aSAlfred Perlstein dc_coal(sc, m_head)
3090fda39fd0SBill Paul 	struct dc_softc		*sc;
3091fda39fd0SBill Paul 	struct mbuf		**m_head;
3092fda39fd0SBill Paul {
3093fda39fd0SBill Paul 	struct mbuf		*m_new, *m;
3094fda39fd0SBill Paul 
3095fda39fd0SBill Paul 	m = *m_head;
3096fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
309740129585SLuigi Rizzo 	if (m_new == NULL)
3098fda39fd0SBill Paul 		return(ENOBUFS);
3099fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
3100fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
3101fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
3102fda39fd0SBill Paul 			m_freem(m_new);
3103fda39fd0SBill Paul 			return(ENOBUFS);
3104fda39fd0SBill Paul 		}
3105fda39fd0SBill Paul 	}
3106fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
3107fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
3108fda39fd0SBill Paul 	m_freem(m);
3109fda39fd0SBill Paul 	*m_head = m_new;
3110fda39fd0SBill Paul 
3111fda39fd0SBill Paul 	return(0);
3112fda39fd0SBill Paul }
3113fda39fd0SBill Paul 
3114fda39fd0SBill Paul /*
311596f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
311696f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
311796f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
311896f2e892SBill Paul  * physical addresses.
311996f2e892SBill Paul  */
312096f2e892SBill Paul 
3121e3d2833aSAlfred Perlstein static void
3122e3d2833aSAlfred Perlstein dc_start(ifp)
312396f2e892SBill Paul 	struct ifnet		*ifp;
312496f2e892SBill Paul {
312596f2e892SBill Paul 	struct dc_softc		*sc;
312696f2e892SBill Paul 	struct mbuf		*m_head = NULL;
312796f2e892SBill Paul 	int			idx;
312896f2e892SBill Paul 
312996f2e892SBill Paul 	sc = ifp->if_softc;
313096f2e892SBill Paul 
3131d1ce9105SBill Paul 	DC_LOCK(sc);
313296f2e892SBill Paul 
3133e7be9f9aSBill Paul 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10) {
3134d1ce9105SBill Paul 		DC_UNLOCK(sc);
313596f2e892SBill Paul 		return;
3136d1ce9105SBill Paul 	}
3137d1ce9105SBill Paul 
3138d1ce9105SBill Paul 	if (ifp->if_flags & IFF_OACTIVE) {
3139d1ce9105SBill Paul 		DC_UNLOCK(sc);
3140d1ce9105SBill Paul 		return;
3141d1ce9105SBill Paul 	}
314296f2e892SBill Paul 
314396f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
314496f2e892SBill Paul 
314596f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
314696f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
314796f2e892SBill Paul 		if (m_head == NULL)
314896f2e892SBill Paul 			break;
314996f2e892SBill Paul 
31502dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
31512dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
31522dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3153fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
3154fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
3155fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
3156fda39fd0SBill Paul 				break;
3157fda39fd0SBill Paul 			}
3158fda39fd0SBill Paul 		}
3159fda39fd0SBill Paul 
316096f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
316196f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
316296f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
316396f2e892SBill Paul 			break;
316496f2e892SBill Paul 		}
316596f2e892SBill Paul 
316696f2e892SBill Paul 		/*
316796f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
316896f2e892SBill Paul 		 * to him.
316996f2e892SBill Paul 		 */
317096f2e892SBill Paul 		if (ifp->if_bpf)
317196f2e892SBill Paul 			bpf_mtap(ifp, m_head);
31725c1cfac4SBill Paul 
31735c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
31745c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
31755c1cfac4SBill Paul 			break;
31765c1cfac4SBill Paul 		}
317796f2e892SBill Paul 	}
317896f2e892SBill Paul 
317996f2e892SBill Paul 	/* Transmit */
318096f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
318196f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
318296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
318396f2e892SBill Paul 
318496f2e892SBill Paul 	/*
318596f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
318696f2e892SBill Paul 	 */
318796f2e892SBill Paul 	ifp->if_timer = 5;
318896f2e892SBill Paul 
3189d1ce9105SBill Paul 	DC_UNLOCK(sc);
3190d1ce9105SBill Paul 
319196f2e892SBill Paul 	return;
319296f2e892SBill Paul }
319396f2e892SBill Paul 
3194e3d2833aSAlfred Perlstein static void
3195e3d2833aSAlfred Perlstein dc_init(xsc)
319696f2e892SBill Paul 	void			*xsc;
319796f2e892SBill Paul {
319896f2e892SBill Paul 	struct dc_softc		*sc = xsc;
319996f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
320096f2e892SBill Paul 	struct mii_data		*mii;
320196f2e892SBill Paul 
3202d1ce9105SBill Paul 	DC_LOCK(sc);
320396f2e892SBill Paul 
320496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
320596f2e892SBill Paul 
320696f2e892SBill Paul 	/*
320796f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
320896f2e892SBill Paul 	 */
320996f2e892SBill Paul 	dc_stop(sc);
321096f2e892SBill Paul 	dc_reset(sc);
321196f2e892SBill Paul 
321296f2e892SBill Paul 	/*
321396f2e892SBill Paul 	 * Set cache alignment and burst length.
321496f2e892SBill Paul 	 */
321588d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
321696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
321796f2e892SBill Paul 	else
321896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
3219935fe010SLuigi Rizzo 	/*
3220935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3221935fe010SLuigi Rizzo 	 */
3222935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3223935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
322496f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
322596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
322696f2e892SBill Paul 	} else {
322796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
322896f2e892SBill Paul 	}
322996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
323096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
323196f2e892SBill Paul 	switch(sc->dc_cachesize) {
323296f2e892SBill Paul 	case 32:
323396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
323496f2e892SBill Paul 		break;
323596f2e892SBill Paul 	case 16:
323696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
323796f2e892SBill Paul 		break;
323896f2e892SBill Paul 	case 8:
323996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
324096f2e892SBill Paul 		break;
324196f2e892SBill Paul 	case 0:
324296f2e892SBill Paul 	default:
324396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
324496f2e892SBill Paul 		break;
324596f2e892SBill Paul 	}
324696f2e892SBill Paul 
324796f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
324896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
324996f2e892SBill Paul 	else {
3250d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
325196f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
325296f2e892SBill Paul 		} else {
325396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
325496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
325596f2e892SBill Paul 		}
325696f2e892SBill Paul 	}
325796f2e892SBill Paul 
325896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
325996f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
326096f2e892SBill Paul 
326196f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
326296f2e892SBill Paul 		/*
326396f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
326496f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
326596f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
326696f2e892SBill Paul 		 * document the meaning of these bits so there's no way
326796f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
326896f2e892SBill Paul 		 * number all its own; the rest all use a different one.
326996f2e892SBill Paul 		 */
327096f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
327196f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
327296f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
327396f2e892SBill Paul 		else
327496f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
327596f2e892SBill Paul 	}
327696f2e892SBill Paul 
3277feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3278feb78939SJonathan Chen 		/*
3279feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3280feb78939SJonathan Chen 		 * can talk to the MII.
3281feb78939SJonathan Chen 		 */
3282feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3283feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3284feb78939SJonathan Chen 		DELAY(10);
3285feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3286feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3287feb78939SJonathan Chen 		DELAY(10);
3288feb78939SJonathan Chen 	}
3289feb78939SJonathan Chen 
329096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3291d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
329296f2e892SBill Paul 
329396f2e892SBill Paul 	/* Init circular RX list. */
329496f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
329596f2e892SBill Paul 		printf("dc%d: initialization failed: no "
329696f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
329796f2e892SBill Paul 		dc_stop(sc);
3298d1ce9105SBill Paul 		DC_UNLOCK(sc);
329996f2e892SBill Paul 		return;
330096f2e892SBill Paul 	}
330196f2e892SBill Paul 
330296f2e892SBill Paul 	/*
330396f2e892SBill Paul 	 * Init tx descriptors.
330496f2e892SBill Paul 	 */
330596f2e892SBill Paul 	dc_list_tx_init(sc);
330696f2e892SBill Paul 
330796f2e892SBill Paul 	/*
330896f2e892SBill Paul 	 * Load the address of the RX list.
330996f2e892SBill Paul 	 */
331096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
331196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
331296f2e892SBill Paul 
331396f2e892SBill Paul 	/*
331496f2e892SBill Paul 	 * Enable interrupts.
331596f2e892SBill Paul 	 */
3316e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3317e4fc250cSLuigi Rizzo 	/*
3318e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3319e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3320e4fc250cSLuigi Rizzo 	 * after a reset.
3321e4fc250cSLuigi Rizzo 	 */
332262f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
3323e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3324e4fc250cSLuigi Rizzo 	else
3325e4fc250cSLuigi Rizzo #endif
332696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
332796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
332896f2e892SBill Paul 
332996f2e892SBill Paul 	/* Enable transmitter. */
333096f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
333196f2e892SBill Paul 
333296f2e892SBill Paul 	/*
3333918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3334918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3335918434c8SBill Paul 	 * link and activity indications.
3336918434c8SBill Paul 	 */
333778999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3338918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3339918434c8SBill Paul 		    DC_WDOG_CTLWREN|DC_WDOG_LINK|DC_WDOG_ACTIVITY);
334078999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3341918434c8SBill Paul 	}
3342918434c8SBill Paul 
3343918434c8SBill Paul 	/*
334496f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
334596f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
334696f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
334796f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
334896f2e892SBill Paul 	 */
334996f2e892SBill Paul 	dc_setfilt(sc);
335096f2e892SBill Paul 
335196f2e892SBill Paul 	/* Enable receiver. */
335296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
335396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
335496f2e892SBill Paul 
335596f2e892SBill Paul 	mii_mediachg(mii);
335696f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
335796f2e892SBill Paul 
335896f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
335996f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
336096f2e892SBill Paul 
3361857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
336245521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3363857fd445SBill Paul 		sc->dc_link = 1;
3364857fd445SBill Paul 	else {
3365318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3366b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3367318b02fdSBill Paul 		else
3368b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3369857fd445SBill Paul 	}
337096f2e892SBill Paul 
33715c1cfac4SBill Paul #ifdef SRM_MEDIA
3372510a809eSMike Smith 	if(sc->dc_srm_media) {
3373510a809eSMike Smith 		struct ifreq ifr;
3374510a809eSMike Smith 
3375510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
3376510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3377510a809eSMike Smith 		sc->dc_srm_media = 0;
3378510a809eSMike Smith 	}
3379510a809eSMike Smith #endif
3380d1ce9105SBill Paul 	DC_UNLOCK(sc);
338196f2e892SBill Paul 	return;
338296f2e892SBill Paul }
338396f2e892SBill Paul 
338496f2e892SBill Paul /*
338596f2e892SBill Paul  * Set media options.
338696f2e892SBill Paul  */
3387e3d2833aSAlfred Perlstein static int
3388e3d2833aSAlfred Perlstein dc_ifmedia_upd(ifp)
338996f2e892SBill Paul 	struct ifnet		*ifp;
339096f2e892SBill Paul {
339196f2e892SBill Paul 	struct dc_softc		*sc;
339296f2e892SBill Paul 	struct mii_data		*mii;
3393f43d9309SBill Paul 	struct ifmedia		*ifm;
339496f2e892SBill Paul 
339596f2e892SBill Paul 	sc = ifp->if_softc;
339696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
339796f2e892SBill Paul 	mii_mediachg(mii);
3398f43d9309SBill Paul 	ifm = &mii->mii_media;
3399f43d9309SBill Paul 
3400f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
340145521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3402f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3403f43d9309SBill Paul 	else
340496f2e892SBill Paul 		sc->dc_link = 0;
340596f2e892SBill Paul 
340696f2e892SBill Paul 	return(0);
340796f2e892SBill Paul }
340896f2e892SBill Paul 
340996f2e892SBill Paul /*
341096f2e892SBill Paul  * Report current media status.
341196f2e892SBill Paul  */
3412e3d2833aSAlfred Perlstein static void
3413e3d2833aSAlfred Perlstein dc_ifmedia_sts(ifp, ifmr)
341496f2e892SBill Paul 	struct ifnet		*ifp;
341596f2e892SBill Paul 	struct ifmediareq	*ifmr;
341696f2e892SBill Paul {
341796f2e892SBill Paul 	struct dc_softc		*sc;
341896f2e892SBill Paul 	struct mii_data		*mii;
3419f43d9309SBill Paul 	struct ifmedia		*ifm;
342096f2e892SBill Paul 
342196f2e892SBill Paul 	sc = ifp->if_softc;
342296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
342396f2e892SBill Paul 	mii_pollstat(mii);
3424f43d9309SBill Paul 	ifm = &mii->mii_media;
3425f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
342645521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3427f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3428f43d9309SBill Paul 			ifmr->ifm_status = 0;
3429f43d9309SBill Paul 			return;
3430f43d9309SBill Paul 		}
3431f43d9309SBill Paul 	}
343296f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
343396f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
343496f2e892SBill Paul 
343596f2e892SBill Paul 	return;
343696f2e892SBill Paul }
343796f2e892SBill Paul 
3438e3d2833aSAlfred Perlstein static int
3439e3d2833aSAlfred Perlstein dc_ioctl(ifp, command, data)
344096f2e892SBill Paul 	struct ifnet		*ifp;
344196f2e892SBill Paul 	u_long			command;
344296f2e892SBill Paul 	caddr_t			data;
344396f2e892SBill Paul {
344496f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
344596f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
344696f2e892SBill Paul 	struct mii_data		*mii;
3447d1ce9105SBill Paul 	int			error = 0;
344896f2e892SBill Paul 
3449d1ce9105SBill Paul 	DC_LOCK(sc);
345096f2e892SBill Paul 
345196f2e892SBill Paul 	switch(command) {
345296f2e892SBill Paul 	case SIOCSIFADDR:
345396f2e892SBill Paul 	case SIOCGIFADDR:
345496f2e892SBill Paul 	case SIOCSIFMTU:
345596f2e892SBill Paul 		error = ether_ioctl(ifp, command, data);
345696f2e892SBill Paul 		break;
345796f2e892SBill Paul 	case SIOCSIFFLAGS:
345896f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
345996f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
346096f2e892SBill Paul 			    ifp->if_flags & IFF_PROMISC &&
346196f2e892SBill Paul 			    !(sc->dc_if_flags & IFF_PROMISC)) {
346296f2e892SBill Paul 				dc_setfilt(sc);
346396f2e892SBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
346496f2e892SBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
346596f2e892SBill Paul 			    sc->dc_if_flags & IFF_PROMISC) {
346696f2e892SBill Paul 				dc_setfilt(sc);
346796f2e892SBill Paul 			} else if (!(ifp->if_flags & IFF_RUNNING)) {
346896f2e892SBill Paul 				sc->dc_txthresh = 0;
346996f2e892SBill Paul 				dc_init(sc);
347096f2e892SBill Paul 			}
347196f2e892SBill Paul 		} else {
347296f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
347396f2e892SBill Paul 				dc_stop(sc);
347496f2e892SBill Paul 		}
347596f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
347696f2e892SBill Paul 		error = 0;
347796f2e892SBill Paul 		break;
347896f2e892SBill Paul 	case SIOCADDMULTI:
347996f2e892SBill Paul 	case SIOCDELMULTI:
348096f2e892SBill Paul 		dc_setfilt(sc);
348196f2e892SBill Paul 		error = 0;
348296f2e892SBill Paul 		break;
348396f2e892SBill Paul 	case SIOCGIFMEDIA:
348496f2e892SBill Paul 	case SIOCSIFMEDIA:
348596f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
348696f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
34875c1cfac4SBill Paul #ifdef SRM_MEDIA
3488510a809eSMike Smith 		if (sc->dc_srm_media)
3489510a809eSMike Smith 			sc->dc_srm_media = 0;
3490510a809eSMike Smith #endif
349196f2e892SBill Paul 		break;
349296f2e892SBill Paul 	default:
349396f2e892SBill Paul 		error = EINVAL;
349496f2e892SBill Paul 		break;
349596f2e892SBill Paul 	}
349696f2e892SBill Paul 
3497d1ce9105SBill Paul 	DC_UNLOCK(sc);
349896f2e892SBill Paul 
349996f2e892SBill Paul 	return(error);
350096f2e892SBill Paul }
350196f2e892SBill Paul 
3502e3d2833aSAlfred Perlstein static void
3503e3d2833aSAlfred Perlstein dc_watchdog(ifp)
350496f2e892SBill Paul 	struct ifnet		*ifp;
350596f2e892SBill Paul {
350696f2e892SBill Paul 	struct dc_softc		*sc;
350796f2e892SBill Paul 
350896f2e892SBill Paul 	sc = ifp->if_softc;
350996f2e892SBill Paul 
3510d1ce9105SBill Paul 	DC_LOCK(sc);
3511d1ce9105SBill Paul 
351296f2e892SBill Paul 	ifp->if_oerrors++;
351396f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
351496f2e892SBill Paul 
351596f2e892SBill Paul 	dc_stop(sc);
351696f2e892SBill Paul 	dc_reset(sc);
351796f2e892SBill Paul 	dc_init(sc);
351896f2e892SBill Paul 
351996f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
352096f2e892SBill Paul 		dc_start(ifp);
352196f2e892SBill Paul 
3522d1ce9105SBill Paul 	DC_UNLOCK(sc);
3523d1ce9105SBill Paul 
352496f2e892SBill Paul 	return;
352596f2e892SBill Paul }
352696f2e892SBill Paul 
352796f2e892SBill Paul /*
352896f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
352996f2e892SBill Paul  * RX and TX lists.
353096f2e892SBill Paul  */
3531e3d2833aSAlfred Perlstein static void
3532e3d2833aSAlfred Perlstein dc_stop(sc)
353396f2e892SBill Paul 	struct dc_softc		*sc;
353496f2e892SBill Paul {
353596f2e892SBill Paul 	register int		i;
353696f2e892SBill Paul 	struct ifnet		*ifp;
353796f2e892SBill Paul 
3538d1ce9105SBill Paul 	DC_LOCK(sc);
3539d1ce9105SBill Paul 
354096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
354196f2e892SBill Paul 	ifp->if_timer = 0;
354296f2e892SBill Paul 
3543b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
354496f2e892SBill Paul 
35453b3ec200SPeter Wemm 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3546e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3547e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
3548e4fc250cSLuigi Rizzo #endif
35493b3ec200SPeter Wemm 
355096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
355196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
355296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
355396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
355496f2e892SBill Paul 	sc->dc_link = 0;
355596f2e892SBill Paul 
355696f2e892SBill Paul 	/*
355796f2e892SBill Paul 	 * Free data in the RX lists.
355896f2e892SBill Paul 	 */
355996f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
356096f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
356196f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
356296f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
356396f2e892SBill Paul 		}
356496f2e892SBill Paul 	}
356596f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
356696f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
356796f2e892SBill Paul 
356896f2e892SBill Paul 	/*
356996f2e892SBill Paul 	 * Free the TX list buffers.
357096f2e892SBill Paul 	 */
357196f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
357296f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
357396f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
357496f2e892SBill Paul 			    DC_TXCTL_SETUP) {
357596f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
357696f2e892SBill Paul 				continue;
357796f2e892SBill Paul 			}
357896f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
357996f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
358096f2e892SBill Paul 		}
358196f2e892SBill Paul 	}
358296f2e892SBill Paul 
358396f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
358496f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
358596f2e892SBill Paul 
3586d1ce9105SBill Paul 	DC_UNLOCK(sc);
3587d1ce9105SBill Paul 
358896f2e892SBill Paul 	return;
358996f2e892SBill Paul }
359096f2e892SBill Paul 
359196f2e892SBill Paul /*
3592e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3593e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3594e8388e14SMitsuru IWASAKI  * resume.
3595e8388e14SMitsuru IWASAKI  */
3596e3d2833aSAlfred Perlstein static int
3597e3d2833aSAlfred Perlstein dc_suspend(dev)
3598e8388e14SMitsuru IWASAKI 	device_t		dev;
3599e8388e14SMitsuru IWASAKI {
3600e8388e14SMitsuru IWASAKI 	register int		i;
3601e8388e14SMitsuru IWASAKI 	int			s;
3602e8388e14SMitsuru IWASAKI 	struct dc_softc		*sc;
3603e8388e14SMitsuru IWASAKI 
3604e8388e14SMitsuru IWASAKI 	s = splimp();
3605e8388e14SMitsuru IWASAKI 
3606e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3607e8388e14SMitsuru IWASAKI 
3608e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3609e8388e14SMitsuru IWASAKI 
3610e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3611e8388e14SMitsuru IWASAKI 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
3612e8388e14SMitsuru IWASAKI 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
3613e8388e14SMitsuru IWASAKI 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
3614e8388e14SMitsuru IWASAKI 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3615e8388e14SMitsuru IWASAKI 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
3616e8388e14SMitsuru IWASAKI 
3617e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3618e8388e14SMitsuru IWASAKI 
3619e8388e14SMitsuru IWASAKI 	splx(s);
3620e8388e14SMitsuru IWASAKI 	return (0);
3621e8388e14SMitsuru IWASAKI }
3622e8388e14SMitsuru IWASAKI 
3623e8388e14SMitsuru IWASAKI /*
3624e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3625e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3626e8388e14SMitsuru IWASAKI  * appropriate.
3627e8388e14SMitsuru IWASAKI  */
3628e3d2833aSAlfred Perlstein static int
3629e3d2833aSAlfred Perlstein dc_resume(dev)
3630e8388e14SMitsuru IWASAKI 	device_t		dev;
3631e8388e14SMitsuru IWASAKI {
3632e8388e14SMitsuru IWASAKI 	register int		i;
3633e8388e14SMitsuru IWASAKI 	int			s;
3634e8388e14SMitsuru IWASAKI 	struct dc_softc		*sc;
3635e8388e14SMitsuru IWASAKI 	struct ifnet		*ifp;
3636e8388e14SMitsuru IWASAKI 
3637e8388e14SMitsuru IWASAKI 	s = splimp();
3638e8388e14SMitsuru IWASAKI 
3639e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3640e8388e14SMitsuru IWASAKI 	ifp = &sc->arpcom.ac_if;
3641e8388e14SMitsuru IWASAKI 
3642e8388e14SMitsuru IWASAKI 	dc_acpi(dev);
3643e8388e14SMitsuru IWASAKI 
3644e8388e14SMitsuru IWASAKI 	/* better way to do this? */
3645e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3646e8388e14SMitsuru IWASAKI 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
3647e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
3648e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
3649e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
3650e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
3651e8388e14SMitsuru IWASAKI 
3652e8388e14SMitsuru IWASAKI 	/* reenable busmastering */
3653e8388e14SMitsuru IWASAKI 	pci_enable_busmaster(dev);
3654e8388e14SMitsuru IWASAKI 	pci_enable_io(dev, DC_RES);
3655e8388e14SMitsuru IWASAKI 
3656e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3657e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3658e8388e14SMitsuru IWASAKI 		dc_init(sc);
3659e8388e14SMitsuru IWASAKI 
3660e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3661e8388e14SMitsuru IWASAKI 
3662e8388e14SMitsuru IWASAKI 	splx(s);
3663e8388e14SMitsuru IWASAKI 	return (0);
3664e8388e14SMitsuru IWASAKI }
3665e8388e14SMitsuru IWASAKI 
3666e8388e14SMitsuru IWASAKI /*
366796f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
366896f2e892SBill Paul  * get confused by errant DMAs when rebooting.
366996f2e892SBill Paul  */
3670e3d2833aSAlfred Perlstein static void
3671e3d2833aSAlfred Perlstein dc_shutdown(dev)
367296f2e892SBill Paul 	device_t		dev;
367396f2e892SBill Paul {
367496f2e892SBill Paul 	struct dc_softc		*sc;
367596f2e892SBill Paul 
367696f2e892SBill Paul 	sc = device_get_softc(dev);
367796f2e892SBill Paul 
367896f2e892SBill Paul 	dc_stop(sc);
367996f2e892SBill Paul 
368096f2e892SBill Paul 	return;
368196f2e892SBill Paul }
3682