xref: /freebsd/sys/dev/dc/if_dc.c (revision 948c244d942abcea8e9a4dee0e60c5bb611d358c)
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" },
14938deb45fSTom Rhodes 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009,
15038deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
15196f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
15296f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
15396f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15496f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
15588d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15688d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
15796f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
15896f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
15996f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
16096f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
16196f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16296f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
16396f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16496f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
16596f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16696f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
16796f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16896f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
16996f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
17096f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17196f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
17296f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17396f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17496f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
17596f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17679d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
17779d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17896f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
179ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
180ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
18196f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
18296f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
18396f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18496f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
18596f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18696f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1879ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1889ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
189fa167b8eSBill Paul 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN2242,
190fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
191feb78939SJonathan Chen 	{ DC_VENDORID_XIRCOM, DC_DEVICEID_X3201,
192feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
1931d5e5310SBill Paul 	{ DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500,
1941d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
1951af8bec7SBill Paul 	{ DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112,
1961af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
197948c244dSWarner Losh 	{ DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX,
198948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
19996f2e892SBill Paul 	{ 0, 0, NULL }
20096f2e892SBill Paul };
20196f2e892SBill Paul 
202e51a25f8SAlfred Perlstein static int dc_probe		(device_t);
203e51a25f8SAlfred Perlstein static int dc_attach		(device_t);
204e51a25f8SAlfred Perlstein static int dc_detach		(device_t);
205e8388e14SMitsuru IWASAKI static int dc_suspend		(device_t);
206e8388e14SMitsuru IWASAKI static int dc_resume		(device_t);
207e51a25f8SAlfred Perlstein static void dc_acpi		(device_t);
208e51a25f8SAlfred Perlstein static struct dc_type *dc_devtype	(device_t);
209e51a25f8SAlfred Perlstein static int dc_newbuf		(struct dc_softc *, int, struct mbuf *);
210e51a25f8SAlfred Perlstein static int dc_encap		(struct dc_softc *, struct mbuf *, u_int32_t *);
211e51a25f8SAlfred Perlstein static int dc_coal		(struct dc_softc *, struct mbuf **);
212e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war	(struct dc_softc *, int);
213e51a25f8SAlfred Perlstein static int dc_rx_resync		(struct dc_softc *);
214e51a25f8SAlfred Perlstein static void dc_rxeof		(struct dc_softc *);
215e51a25f8SAlfred Perlstein static void dc_txeof		(struct dc_softc *);
216e51a25f8SAlfred Perlstein static void dc_tick		(void *);
217e51a25f8SAlfred Perlstein static void dc_tx_underrun	(struct dc_softc *);
218e51a25f8SAlfred Perlstein static void dc_intr		(void *);
219e51a25f8SAlfred Perlstein static void dc_start		(struct ifnet *);
220e51a25f8SAlfred Perlstein static int dc_ioctl		(struct ifnet *, u_long, caddr_t);
221e51a25f8SAlfred Perlstein static void dc_init		(void *);
222e51a25f8SAlfred Perlstein static void dc_stop		(struct dc_softc *);
223e51a25f8SAlfred Perlstein static void dc_watchdog		(struct ifnet *);
224e51a25f8SAlfred Perlstein static void dc_shutdown		(device_t);
225e51a25f8SAlfred Perlstein static int dc_ifmedia_upd	(struct ifnet *);
226e51a25f8SAlfred Perlstein static void dc_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
22796f2e892SBill Paul 
228e51a25f8SAlfred Perlstein static void dc_delay		(struct dc_softc *);
229e51a25f8SAlfred Perlstein static void dc_eeprom_idle	(struct dc_softc *);
230e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte	(struct dc_softc *, int);
231e51a25f8SAlfred Perlstein static void dc_eeprom_getword	(struct dc_softc *, int, u_int16_t *);
23296f2e892SBill Paul static void dc_eeprom_getword_pnic
233e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
234feb78939SJonathan Chen static void dc_eeprom_getword_xircom
235e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
2363097aa70SWarner Losh static void dc_eeprom_width	(struct dc_softc *);
237e51a25f8SAlfred Perlstein static void dc_read_eeprom	(struct dc_softc *, caddr_t, int, int, int);
23896f2e892SBill Paul 
239e51a25f8SAlfred Perlstein static void dc_mii_writebit	(struct dc_softc *, int);
240e51a25f8SAlfred Perlstein static int dc_mii_readbit	(struct dc_softc *);
241e51a25f8SAlfred Perlstein static void dc_mii_sync		(struct dc_softc *);
242e51a25f8SAlfred Perlstein static void dc_mii_send		(struct dc_softc *, u_int32_t, int);
243e51a25f8SAlfred Perlstein static int dc_mii_readreg	(struct dc_softc *, struct dc_mii_frame *);
244e51a25f8SAlfred Perlstein static int dc_mii_writereg	(struct dc_softc *, struct dc_mii_frame *);
245e51a25f8SAlfred Perlstein static int dc_miibus_readreg	(device_t, int, int);
246e51a25f8SAlfred Perlstein static int dc_miibus_writereg	(device_t, int, int, int);
247e51a25f8SAlfred Perlstein static void dc_miibus_statchg	(device_t);
248e51a25f8SAlfred Perlstein static void dc_miibus_mediainit	(device_t);
24996f2e892SBill Paul 
250e51a25f8SAlfred Perlstein static void dc_setcfg		(struct dc_softc *, int);
251e51a25f8SAlfred Perlstein static u_int32_t dc_crc_le	(struct dc_softc *, caddr_t);
252e51a25f8SAlfred Perlstein static u_int32_t dc_crc_be	(caddr_t);
253e51a25f8SAlfred Perlstein static void dc_setfilt_21143	(struct dc_softc *);
254e51a25f8SAlfred Perlstein static void dc_setfilt_asix	(struct dc_softc *);
255e51a25f8SAlfred Perlstein static void dc_setfilt_admtek	(struct dc_softc *);
256e51a25f8SAlfred Perlstein static void dc_setfilt_xircom	(struct dc_softc *);
25796f2e892SBill Paul 
258e51a25f8SAlfred Perlstein static void dc_setfilt		(struct dc_softc *);
25996f2e892SBill Paul 
260e51a25f8SAlfred Perlstein static void dc_reset		(struct dc_softc *);
261e51a25f8SAlfred Perlstein static int dc_list_rx_init	(struct dc_softc *);
262e51a25f8SAlfred Perlstein static int dc_list_tx_init	(struct dc_softc *);
26396f2e892SBill Paul 
2643097aa70SWarner Losh static void dc_read_srom	(struct dc_softc *, int);
265e51a25f8SAlfred Perlstein static void dc_parse_21143_srom	(struct dc_softc *);
266e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia	(struct dc_softc *, struct dc_eblock_sia *);
267e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii	(struct dc_softc *, struct dc_eblock_mii *);
268e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym	(struct dc_softc *, struct dc_eblock_sym *);
269e51a25f8SAlfred Perlstein static void dc_apply_fixup	(struct dc_softc *, int);
2705c1cfac4SBill Paul 
27196f2e892SBill Paul #ifdef DC_USEIOSPACE
27296f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
27396f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
27496f2e892SBill Paul #else
27596f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
27696f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
27796f2e892SBill Paul #endif
27896f2e892SBill Paul 
27996f2e892SBill Paul static device_method_t dc_methods[] = {
28096f2e892SBill Paul 	/* Device interface */
28196f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
28296f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
28396f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
284e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
285e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
28696f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
28796f2e892SBill Paul 
28896f2e892SBill Paul 	/* bus interface */
28996f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
29096f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
29196f2e892SBill Paul 
29296f2e892SBill Paul 	/* MII interface */
29396f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
29496f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
29596f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
296f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
29796f2e892SBill Paul 
29896f2e892SBill Paul 	{ 0, 0 }
29996f2e892SBill Paul };
30096f2e892SBill Paul 
30196f2e892SBill Paul static driver_t dc_driver = {
30296f2e892SBill Paul 	"dc",
30396f2e892SBill Paul 	dc_methods,
30496f2e892SBill Paul 	sizeof(struct dc_softc)
30596f2e892SBill Paul };
30696f2e892SBill Paul 
30796f2e892SBill Paul static devclass_t dc_devclass;
30801faf54bSLuigi Rizzo #ifdef __i386__
30901faf54bSLuigi Rizzo static int dc_quick=1;
31001faf54bSLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW,
31101faf54bSLuigi Rizzo 	&dc_quick,0,"do not mdevget in dc driver");
31201faf54bSLuigi Rizzo #endif
31396f2e892SBill Paul 
314feb78939SJonathan Chen DRIVER_MODULE(if_dc, cardbus, dc_driver, dc_devclass, 0, 0);
31596f2e892SBill Paul DRIVER_MODULE(if_dc, pci, dc_driver, dc_devclass, 0, 0);
31696f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
31796f2e892SBill Paul 
31896f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
31996f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
32096f2e892SBill Paul 
32196f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
32296f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
32396f2e892SBill Paul 
32496f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
32596f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
32696f2e892SBill Paul 
327b50c6312SJonathan Lemon #define IS_MPSAFE 	0
328b50c6312SJonathan Lemon 
329e3d2833aSAlfred Perlstein static void
330e3d2833aSAlfred Perlstein dc_delay(sc)
33196f2e892SBill Paul 	struct dc_softc		*sc;
33296f2e892SBill Paul {
33396f2e892SBill Paul 	int			idx;
33496f2e892SBill Paul 
33596f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
33696f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
33796f2e892SBill Paul }
33896f2e892SBill Paul 
3392c876e15SPoul-Henning Kamp static void
3402c876e15SPoul-Henning Kamp dc_eeprom_width(sc)
3413097aa70SWarner Losh 	struct dc_softc		*sc;
3423097aa70SWarner Losh {
3433097aa70SWarner Losh 	int i;
3443097aa70SWarner Losh 
3453097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3463097aa70SWarner Losh 	dc_eeprom_idle(sc);
3473097aa70SWarner Losh 
3483097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3493097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3503097aa70SWarner Losh 	dc_delay(sc);
3513097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3523097aa70SWarner Losh 	dc_delay(sc);
3533097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3543097aa70SWarner Losh 	dc_delay(sc);
3553097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3563097aa70SWarner Losh 	dc_delay(sc);
3573097aa70SWarner Losh 
3583097aa70SWarner Losh 	for (i = 3; i--;) {
3593097aa70SWarner Losh 		if (6 & (1 << i))
3603097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3613097aa70SWarner Losh 		else
3623097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3633097aa70SWarner Losh 		dc_delay(sc);
3643097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3653097aa70SWarner Losh 		dc_delay(sc);
3663097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3673097aa70SWarner Losh 		dc_delay(sc);
3683097aa70SWarner Losh 	}
3693097aa70SWarner Losh 
3703097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3713097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3723097aa70SWarner Losh 		dc_delay(sc);
3733097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3743097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3753097aa70SWarner Losh 			dc_delay(sc);
3763097aa70SWarner Losh 			break;
3773097aa70SWarner Losh 		}
3783097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3793097aa70SWarner Losh 		dc_delay(sc);
3803097aa70SWarner Losh 	}
3813097aa70SWarner Losh 
3823097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
3833097aa70SWarner Losh 	dc_eeprom_idle(sc);
3843097aa70SWarner Losh 
3853097aa70SWarner Losh 	if (i < 4 || i > 12)
3863097aa70SWarner Losh 		sc->dc_romwidth = 6;
3873097aa70SWarner Losh 	else
3883097aa70SWarner Losh 		sc->dc_romwidth = i;
3893097aa70SWarner Losh 
3903097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3913097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3923097aa70SWarner Losh 	dc_delay(sc);
3933097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3943097aa70SWarner Losh 	dc_delay(sc);
3953097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3963097aa70SWarner Losh 	dc_delay(sc);
3973097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3983097aa70SWarner Losh 	dc_delay(sc);
3993097aa70SWarner Losh 
4003097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4013097aa70SWarner Losh 	dc_eeprom_idle(sc);
4023097aa70SWarner Losh }
4033097aa70SWarner Losh 
404e3d2833aSAlfred Perlstein static void
405e3d2833aSAlfred Perlstein dc_eeprom_idle(sc)
40696f2e892SBill Paul 	struct dc_softc		*sc;
40796f2e892SBill Paul {
40896f2e892SBill Paul 	register int		i;
40996f2e892SBill Paul 
41096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
41196f2e892SBill Paul 	dc_delay(sc);
41296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
41396f2e892SBill Paul 	dc_delay(sc);
41496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
41596f2e892SBill Paul 	dc_delay(sc);
41696f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
41796f2e892SBill Paul 	dc_delay(sc);
41896f2e892SBill Paul 
41996f2e892SBill Paul 	for (i = 0; i < 25; i++) {
42096f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42196f2e892SBill Paul 		dc_delay(sc);
42296f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42396f2e892SBill Paul 		dc_delay(sc);
42496f2e892SBill Paul 	}
42596f2e892SBill Paul 
42696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42796f2e892SBill Paul 	dc_delay(sc);
42896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
42996f2e892SBill Paul 	dc_delay(sc);
43096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
43196f2e892SBill Paul 
43296f2e892SBill Paul 	return;
43396f2e892SBill Paul }
43496f2e892SBill Paul 
43596f2e892SBill Paul /*
43696f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
43796f2e892SBill Paul  */
438e3d2833aSAlfred Perlstein static void
439e3d2833aSAlfred Perlstein dc_eeprom_putbyte(sc, addr)
44096f2e892SBill Paul 	struct dc_softc		*sc;
44196f2e892SBill Paul 	int			addr;
44296f2e892SBill Paul {
44396f2e892SBill Paul 	register int		d, i;
44496f2e892SBill Paul 
4453097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4463097aa70SWarner Losh 	for (i = 3; i--; ) {
4473097aa70SWarner Losh 		if (d & (1 << i))
4483097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
44996f2e892SBill Paul 		else
4503097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4513097aa70SWarner Losh 		dc_delay(sc);
4523097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4533097aa70SWarner Losh 		dc_delay(sc);
4543097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4553097aa70SWarner Losh 		dc_delay(sc);
4563097aa70SWarner Losh 	}
45796f2e892SBill Paul 
45896f2e892SBill Paul 	/*
45996f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
46096f2e892SBill Paul 	 */
4613097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4623097aa70SWarner Losh 		if (addr & (1 << i)) {
46396f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
46496f2e892SBill Paul 		} else {
46596f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
46696f2e892SBill Paul 		}
46796f2e892SBill Paul 		dc_delay(sc);
46896f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
46996f2e892SBill Paul 		dc_delay(sc);
47096f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
47196f2e892SBill Paul 		dc_delay(sc);
47296f2e892SBill Paul 	}
47396f2e892SBill Paul 
47496f2e892SBill Paul 	return;
47596f2e892SBill Paul }
47696f2e892SBill Paul 
47796f2e892SBill Paul /*
47896f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
47996f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
48096f2e892SBill Paul  * the EEPROM.
48196f2e892SBill Paul  */
482e3d2833aSAlfred Perlstein static void
483e3d2833aSAlfred Perlstein dc_eeprom_getword_pnic(sc, addr, dest)
48496f2e892SBill Paul 	struct dc_softc		*sc;
48596f2e892SBill Paul 	int			addr;
48696f2e892SBill Paul 	u_int16_t		*dest;
48796f2e892SBill Paul {
48896f2e892SBill Paul 	register int		i;
48996f2e892SBill Paul 	u_int32_t		r;
49096f2e892SBill Paul 
49196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ|addr);
49296f2e892SBill Paul 
49396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
49496f2e892SBill Paul 		DELAY(1);
49596f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
49696f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
49796f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
49896f2e892SBill Paul 			return;
49996f2e892SBill Paul 		}
50096f2e892SBill Paul 	}
50196f2e892SBill Paul 
50296f2e892SBill Paul 	return;
50396f2e892SBill Paul }
50496f2e892SBill Paul 
50596f2e892SBill Paul /*
50696f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
507feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
508feb78939SJonathan Chen  * the EEPROM, too.
509feb78939SJonathan Chen  */
510e3d2833aSAlfred Perlstein static void
511e3d2833aSAlfred Perlstein dc_eeprom_getword_xircom(sc, addr, dest)
512feb78939SJonathan Chen 	struct dc_softc		*sc;
513feb78939SJonathan Chen 	int			addr;
514feb78939SJonathan Chen 	u_int16_t		*dest;
515feb78939SJonathan Chen {
516feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
517feb78939SJonathan Chen 
518feb78939SJonathan Chen 	addr *= 2;
519feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
520feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff;
521feb78939SJonathan Chen 	addr += 1;
522feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
523feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO)&0xff) << 8;
524feb78939SJonathan Chen 
525feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
526feb78939SJonathan Chen 	return;
527feb78939SJonathan Chen }
528feb78939SJonathan Chen 
529feb78939SJonathan Chen /*
530feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
53196f2e892SBill Paul  */
532e3d2833aSAlfred Perlstein static void
533e3d2833aSAlfred Perlstein dc_eeprom_getword(sc, addr, dest)
53496f2e892SBill Paul 	struct dc_softc		*sc;
53596f2e892SBill Paul 	int			addr;
53696f2e892SBill Paul 	u_int16_t		*dest;
53796f2e892SBill Paul {
53896f2e892SBill Paul 	register int		i;
53996f2e892SBill Paul 	u_int16_t		word = 0;
54096f2e892SBill Paul 
54196f2e892SBill Paul 	/* Force EEPROM to idle state. */
54296f2e892SBill Paul 	dc_eeprom_idle(sc);
54396f2e892SBill Paul 
54496f2e892SBill Paul 	/* Enter EEPROM access mode. */
54596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
54696f2e892SBill Paul 	dc_delay(sc);
54796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
54896f2e892SBill Paul 	dc_delay(sc);
54996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
55096f2e892SBill Paul 	dc_delay(sc);
55196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55296f2e892SBill Paul 	dc_delay(sc);
55396f2e892SBill Paul 
55496f2e892SBill Paul 	/*
55596f2e892SBill Paul 	 * Send address of word we want to read.
55696f2e892SBill Paul 	 */
55796f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
55896f2e892SBill Paul 
55996f2e892SBill Paul 	/*
56096f2e892SBill Paul 	 * Start reading bits from EEPROM.
56196f2e892SBill Paul 	 */
56296f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56396f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
56496f2e892SBill Paul 		dc_delay(sc);
56596f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
56696f2e892SBill Paul 			word |= i;
56796f2e892SBill Paul 		dc_delay(sc);
56896f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
56996f2e892SBill Paul 		dc_delay(sc);
57096f2e892SBill Paul 	}
57196f2e892SBill Paul 
57296f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57396f2e892SBill Paul 	dc_eeprom_idle(sc);
57496f2e892SBill Paul 
57596f2e892SBill Paul 	*dest = word;
57696f2e892SBill Paul 
57796f2e892SBill Paul 	return;
57896f2e892SBill Paul }
57996f2e892SBill Paul 
58096f2e892SBill Paul /*
58196f2e892SBill Paul  * Read a sequence of words from the EEPROM.
58296f2e892SBill Paul  */
583e3d2833aSAlfred Perlstein static void
584e3d2833aSAlfred Perlstein dc_read_eeprom(sc, dest, off, cnt, swap)
58596f2e892SBill Paul 	struct dc_softc		*sc;
58696f2e892SBill Paul 	caddr_t			dest;
58796f2e892SBill Paul 	int			off;
58896f2e892SBill Paul 	int			cnt;
58996f2e892SBill Paul 	int			swap;
59096f2e892SBill Paul {
59196f2e892SBill Paul 	int			i;
59296f2e892SBill Paul 	u_int16_t		word = 0, *ptr;
59396f2e892SBill Paul 
59496f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
59596f2e892SBill Paul 		if (DC_IS_PNIC(sc))
59696f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
597feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
598feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59996f2e892SBill Paul 		else
60096f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
60196f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
60296f2e892SBill Paul 		if (swap)
60396f2e892SBill Paul 			*ptr = ntohs(word);
60496f2e892SBill Paul 		else
60596f2e892SBill Paul 			*ptr = word;
60696f2e892SBill Paul 	}
60796f2e892SBill Paul 
60896f2e892SBill Paul 	return;
60996f2e892SBill Paul }
61096f2e892SBill Paul 
61196f2e892SBill Paul /*
61296f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
61396f2e892SBill Paul  * Application Notes pp.19-21.
61496f2e892SBill Paul  */
61596f2e892SBill Paul /*
61696f2e892SBill Paul  * Write a bit to the MII bus.
61796f2e892SBill Paul  */
618e3d2833aSAlfred Perlstein static void
619e3d2833aSAlfred Perlstein dc_mii_writebit(sc, bit)
62096f2e892SBill Paul 	struct dc_softc		*sc;
62196f2e892SBill Paul 	int			bit;
62296f2e892SBill Paul {
62396f2e892SBill Paul 	if (bit)
62496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
62596f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE|DC_SIO_MII_DATAOUT);
62696f2e892SBill Paul 	else
62796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
62896f2e892SBill Paul 
62996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63196f2e892SBill Paul 
63296f2e892SBill Paul 	return;
63396f2e892SBill Paul }
63496f2e892SBill Paul 
63596f2e892SBill Paul /*
63696f2e892SBill Paul  * Read a bit from the MII bus.
63796f2e892SBill Paul  */
638e3d2833aSAlfred Perlstein static int
639e3d2833aSAlfred Perlstein dc_mii_readbit(sc)
64096f2e892SBill Paul 	struct dc_softc		*sc;
64196f2e892SBill Paul {
64296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ|DC_SIO_MII_DIR);
64396f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
64496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
64596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
64696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
64796f2e892SBill Paul 		return(1);
64896f2e892SBill Paul 
64996f2e892SBill Paul 	return(0);
65096f2e892SBill Paul }
65196f2e892SBill Paul 
65296f2e892SBill Paul /*
65396f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
65496f2e892SBill Paul  */
655e3d2833aSAlfred Perlstein static void
656e3d2833aSAlfred Perlstein dc_mii_sync(sc)
65796f2e892SBill Paul 	struct dc_softc		*sc;
65896f2e892SBill Paul {
65996f2e892SBill Paul 	register int		i;
66096f2e892SBill Paul 
66196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
66296f2e892SBill Paul 
66396f2e892SBill Paul 	for (i = 0; i < 32; i++)
66496f2e892SBill Paul 		dc_mii_writebit(sc, 1);
66596f2e892SBill Paul 
66696f2e892SBill Paul 	return;
66796f2e892SBill Paul }
66896f2e892SBill Paul 
66996f2e892SBill Paul /*
67096f2e892SBill Paul  * Clock a series of bits through the MII.
67196f2e892SBill Paul  */
672e3d2833aSAlfred Perlstein static void
673e3d2833aSAlfred Perlstein dc_mii_send(sc, bits, cnt)
67496f2e892SBill Paul 	struct dc_softc		*sc;
67596f2e892SBill Paul 	u_int32_t		bits;
67696f2e892SBill Paul 	int			cnt;
67796f2e892SBill Paul {
67896f2e892SBill Paul 	int			i;
67996f2e892SBill Paul 
68096f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
68196f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
68296f2e892SBill Paul }
68396f2e892SBill Paul 
68496f2e892SBill Paul /*
68596f2e892SBill Paul  * Read an PHY register through the MII.
68696f2e892SBill Paul  */
687e3d2833aSAlfred Perlstein static int
688e3d2833aSAlfred Perlstein dc_mii_readreg(sc, frame)
68996f2e892SBill Paul 	struct dc_softc		*sc;
69096f2e892SBill Paul 	struct dc_mii_frame	*frame;
69196f2e892SBill Paul 
69296f2e892SBill Paul {
693d1ce9105SBill Paul 	int			i, ack;
69496f2e892SBill Paul 
695d1ce9105SBill Paul 	DC_LOCK(sc);
69696f2e892SBill Paul 
69796f2e892SBill Paul 	/*
69896f2e892SBill Paul 	 * Set up frame for RX.
69996f2e892SBill Paul 	 */
70096f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
70196f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
70296f2e892SBill Paul 	frame->mii_turnaround = 0;
70396f2e892SBill Paul 	frame->mii_data = 0;
70496f2e892SBill Paul 
70596f2e892SBill Paul 	/*
70696f2e892SBill Paul 	 * Sync the PHYs.
70796f2e892SBill Paul 	 */
70896f2e892SBill Paul 	dc_mii_sync(sc);
70996f2e892SBill Paul 
71096f2e892SBill Paul 	/*
71196f2e892SBill Paul 	 * Send command/address info.
71296f2e892SBill Paul 	 */
71396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
71496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
71596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
71696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
71796f2e892SBill Paul 
71896f2e892SBill Paul #ifdef notdef
71996f2e892SBill Paul 	/* Idle bit */
72096f2e892SBill Paul 	dc_mii_writebit(sc, 1);
72196f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72296f2e892SBill Paul #endif
72396f2e892SBill Paul 
72496f2e892SBill Paul 	/* Check for ack */
72596f2e892SBill Paul 	ack = dc_mii_readbit(sc);
72696f2e892SBill Paul 
72796f2e892SBill Paul 	/*
72896f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
72996f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
73096f2e892SBill Paul 	 */
73196f2e892SBill Paul 	if (ack) {
73296f2e892SBill Paul 		for(i = 0; i < 16; i++) {
73396f2e892SBill Paul 			dc_mii_readbit(sc);
73496f2e892SBill Paul 		}
73596f2e892SBill Paul 		goto fail;
73696f2e892SBill Paul 	}
73796f2e892SBill Paul 
73896f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
73996f2e892SBill Paul 		if (!ack) {
74096f2e892SBill Paul 			if (dc_mii_readbit(sc))
74196f2e892SBill Paul 				frame->mii_data |= i;
74296f2e892SBill Paul 		}
74396f2e892SBill Paul 	}
74496f2e892SBill Paul 
74596f2e892SBill Paul fail:
74696f2e892SBill Paul 
74796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
74896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
74996f2e892SBill Paul 
750d1ce9105SBill Paul 	DC_UNLOCK(sc);
75196f2e892SBill Paul 
75296f2e892SBill Paul 	if (ack)
75396f2e892SBill Paul 		return(1);
75496f2e892SBill Paul 	return(0);
75596f2e892SBill Paul }
75696f2e892SBill Paul 
75796f2e892SBill Paul /*
75896f2e892SBill Paul  * Write to a PHY register through the MII.
75996f2e892SBill Paul  */
760e3d2833aSAlfred Perlstein static int
761e3d2833aSAlfred Perlstein dc_mii_writereg(sc, frame)
76296f2e892SBill Paul 	struct dc_softc		*sc;
76396f2e892SBill Paul 	struct dc_mii_frame	*frame;
76496f2e892SBill Paul 
76596f2e892SBill Paul {
766d1ce9105SBill Paul 	DC_LOCK(sc);
76796f2e892SBill Paul 	/*
76896f2e892SBill Paul 	 * Set up frame for TX.
76996f2e892SBill Paul 	 */
77096f2e892SBill Paul 
77196f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
77296f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
77396f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
77496f2e892SBill Paul 
77596f2e892SBill Paul 	/*
77696f2e892SBill Paul 	 * Sync the PHYs.
77796f2e892SBill Paul 	 */
77896f2e892SBill Paul 	dc_mii_sync(sc);
77996f2e892SBill Paul 
78096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
78196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
78296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
78396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
78496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
78596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
78696f2e892SBill Paul 
78796f2e892SBill Paul 	/* Idle bit. */
78896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
78996f2e892SBill Paul 	dc_mii_writebit(sc, 0);
79096f2e892SBill Paul 
791d1ce9105SBill Paul 	DC_UNLOCK(sc);
79296f2e892SBill Paul 
79396f2e892SBill Paul 	return(0);
79496f2e892SBill Paul }
79596f2e892SBill Paul 
796e3d2833aSAlfred Perlstein static int
797e3d2833aSAlfred Perlstein dc_miibus_readreg(dev, phy, reg)
79896f2e892SBill Paul 	device_t		dev;
79996f2e892SBill Paul 	int			phy, reg;
80096f2e892SBill Paul {
80196f2e892SBill Paul 	struct dc_mii_frame	frame;
80296f2e892SBill Paul 	struct dc_softc		*sc;
803c85c4667SBill Paul 	int			i, rval, phy_reg = 0;
80496f2e892SBill Paul 
80596f2e892SBill Paul 	sc = device_get_softc(dev);
80696f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
80796f2e892SBill Paul 
80896f2e892SBill Paul 	/*
80996f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
81096f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
81196f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
81296f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
81396f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
81496f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
81596f2e892SBill Paul 	 * that the PHY is at MII address 1.
81696f2e892SBill Paul 	 */
81796f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
81896f2e892SBill Paul 		return(0);
81996f2e892SBill Paul 
8201af8bec7SBill Paul 	/*
8211af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
8221af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
8231af8bec7SBill Paul 	 * so we only respond to correct one.
8241af8bec7SBill Paul 	 */
8251af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
8261af8bec7SBill Paul 		return(0);
8271af8bec7SBill Paul 
8285c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
82996f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
83096f2e892SBill Paul 			switch(reg) {
83196f2e892SBill Paul 			case MII_BMSR:
83296f2e892SBill Paul 			/*
83396f2e892SBill Paul 			 * Fake something to make the probe
83496f2e892SBill Paul 			 * code think there's a PHY here.
83596f2e892SBill Paul 			 */
83696f2e892SBill Paul 				return(BMSR_MEDIAMASK);
83796f2e892SBill Paul 				break;
83896f2e892SBill Paul 			case MII_PHYIDR1:
83996f2e892SBill Paul 				if (DC_IS_PNIC(sc))
84096f2e892SBill Paul 					return(DC_VENDORID_LO);
84196f2e892SBill Paul 				return(DC_VENDORID_DEC);
84296f2e892SBill Paul 				break;
84396f2e892SBill Paul 			case MII_PHYIDR2:
84496f2e892SBill Paul 				if (DC_IS_PNIC(sc))
84596f2e892SBill Paul 					return(DC_DEVICEID_82C168);
84696f2e892SBill Paul 				return(DC_DEVICEID_21143);
84796f2e892SBill Paul 				break;
84896f2e892SBill Paul 			default:
84996f2e892SBill Paul 				return(0);
85096f2e892SBill Paul 				break;
85196f2e892SBill Paul 			}
85296f2e892SBill Paul 		} else
85396f2e892SBill Paul 			return(0);
85496f2e892SBill Paul 	}
85596f2e892SBill Paul 
85696f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
85796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
85896f2e892SBill Paul 		    (phy << 23) | (reg << 18));
85996f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
86096f2e892SBill Paul 			DELAY(1);
86196f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
86296f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
86396f2e892SBill Paul 				rval &= 0xFFFF;
86496f2e892SBill Paul 				return(rval == 0xFFFF ? 0 : rval);
86596f2e892SBill Paul 			}
86696f2e892SBill Paul 		}
86796f2e892SBill Paul 		return(0);
86896f2e892SBill Paul 	}
86996f2e892SBill Paul 
87096f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
87196f2e892SBill Paul 		switch(reg) {
87296f2e892SBill Paul 		case MII_BMCR:
87396f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
87496f2e892SBill Paul 			break;
87596f2e892SBill Paul 		case MII_BMSR:
87696f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
87796f2e892SBill Paul 			break;
87896f2e892SBill Paul 		case MII_PHYIDR1:
87996f2e892SBill Paul 			phy_reg = DC_AL_VENID;
88096f2e892SBill Paul 			break;
88196f2e892SBill Paul 		case MII_PHYIDR2:
88296f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
88396f2e892SBill Paul 			break;
88496f2e892SBill Paul 		case MII_ANAR:
88596f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
88696f2e892SBill Paul 			break;
88796f2e892SBill Paul 		case MII_ANLPAR:
88896f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
88996f2e892SBill Paul 			break;
89096f2e892SBill Paul 		case MII_ANER:
89196f2e892SBill Paul 			phy_reg = DC_AL_ANER;
89296f2e892SBill Paul 			break;
89396f2e892SBill Paul 		default:
89496f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
89596f2e892SBill Paul 			    sc->dc_unit, reg);
89696f2e892SBill Paul 			return(0);
89796f2e892SBill Paul 			break;
89896f2e892SBill Paul 		}
89996f2e892SBill Paul 
90096f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
90196f2e892SBill Paul 
90296f2e892SBill Paul 		if (rval == 0xFFFF)
90396f2e892SBill Paul 			return(0);
90496f2e892SBill Paul 		return(rval);
90596f2e892SBill Paul 	}
90696f2e892SBill Paul 
90796f2e892SBill Paul 	frame.mii_phyaddr = phy;
90896f2e892SBill Paul 	frame.mii_regaddr = reg;
909419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
910f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
911f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
912419146d9SBill Paul 	}
91396f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
914419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
915f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
91696f2e892SBill Paul 
91796f2e892SBill Paul 	return(frame.mii_data);
91896f2e892SBill Paul }
91996f2e892SBill Paul 
920e3d2833aSAlfred Perlstein static int
921e3d2833aSAlfred Perlstein dc_miibus_writereg(dev, phy, reg, data)
92296f2e892SBill Paul 	device_t		dev;
92396f2e892SBill Paul 	int			phy, reg, data;
92496f2e892SBill Paul {
92596f2e892SBill Paul 	struct dc_softc		*sc;
92696f2e892SBill Paul 	struct dc_mii_frame	frame;
927c85c4667SBill Paul 	int			i, phy_reg = 0;
92896f2e892SBill Paul 
92996f2e892SBill Paul 	sc = device_get_softc(dev);
93096f2e892SBill Paul 	bzero((char *)&frame, sizeof(frame));
93196f2e892SBill Paul 
93296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
93396f2e892SBill Paul 		return(0);
93496f2e892SBill Paul 
9351af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
9361af8bec7SBill Paul 		return(0);
9371af8bec7SBill Paul 
93896f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
93996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
94096f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
94196f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
94296f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
94396f2e892SBill Paul 				break;
94496f2e892SBill Paul 		}
94596f2e892SBill Paul 		return(0);
94696f2e892SBill Paul 	}
94796f2e892SBill Paul 
94896f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
94996f2e892SBill Paul 		switch(reg) {
95096f2e892SBill Paul 		case MII_BMCR:
95196f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
95296f2e892SBill Paul 			break;
95396f2e892SBill Paul 		case MII_BMSR:
95496f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
95596f2e892SBill Paul 			break;
95696f2e892SBill Paul 		case MII_PHYIDR1:
95796f2e892SBill Paul 			phy_reg = DC_AL_VENID;
95896f2e892SBill Paul 			break;
95996f2e892SBill Paul 		case MII_PHYIDR2:
96096f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
96196f2e892SBill Paul 			break;
96296f2e892SBill Paul 		case MII_ANAR:
96396f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
96496f2e892SBill Paul 			break;
96596f2e892SBill Paul 		case MII_ANLPAR:
96696f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
96796f2e892SBill Paul 			break;
96896f2e892SBill Paul 		case MII_ANER:
96996f2e892SBill Paul 			phy_reg = DC_AL_ANER;
97096f2e892SBill Paul 			break;
97196f2e892SBill Paul 		default:
97296f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
97396f2e892SBill Paul 			    sc->dc_unit, reg);
97496f2e892SBill Paul 			return(0);
97596f2e892SBill Paul 			break;
97696f2e892SBill Paul 		}
97796f2e892SBill Paul 
97896f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
97996f2e892SBill Paul 		return(0);
98096f2e892SBill Paul 	}
98196f2e892SBill Paul 
98296f2e892SBill Paul 	frame.mii_phyaddr = phy;
98396f2e892SBill Paul 	frame.mii_regaddr = reg;
98496f2e892SBill Paul 	frame.mii_data = data;
98596f2e892SBill Paul 
986419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
987f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
988f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
989419146d9SBill Paul 	}
99096f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
991419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
992f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
99396f2e892SBill Paul 
99496f2e892SBill Paul 	return(0);
99596f2e892SBill Paul }
99696f2e892SBill Paul 
997e3d2833aSAlfred Perlstein static void
998e3d2833aSAlfred Perlstein dc_miibus_statchg(dev)
99996f2e892SBill Paul 	device_t		dev;
100096f2e892SBill Paul {
100196f2e892SBill Paul 	struct dc_softc		*sc;
100296f2e892SBill Paul 	struct mii_data		*mii;
1003f43d9309SBill Paul 	struct ifmedia		*ifm;
100496f2e892SBill Paul 
100596f2e892SBill Paul 	sc = device_get_softc(dev);
100696f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
100796f2e892SBill Paul 		return;
10085c1cfac4SBill Paul 
100996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1010f43d9309SBill Paul 	ifm = &mii->mii_media;
1011f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
101245521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
1013f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
1014f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
1015f43d9309SBill Paul 	} else {
101696f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
101796f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
1018f43d9309SBill Paul 	}
1019f43d9309SBill Paul 
1020f43d9309SBill Paul 	return;
1021f43d9309SBill Paul }
1022f43d9309SBill Paul 
1023f43d9309SBill Paul /*
1024f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
1025f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
1026f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
1027f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
1028f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
1029f43d9309SBill Paul  * with it itself. *sigh*
1030f43d9309SBill Paul  */
1031e3d2833aSAlfred Perlstein static void
1032e3d2833aSAlfred Perlstein dc_miibus_mediainit(dev)
1033f43d9309SBill Paul 	device_t		dev;
1034f43d9309SBill Paul {
1035f43d9309SBill Paul 	struct dc_softc		*sc;
1036f43d9309SBill Paul 	struct mii_data		*mii;
1037f43d9309SBill Paul 	struct ifmedia		*ifm;
1038f43d9309SBill Paul 	int			rev;
1039f43d9309SBill Paul 
1040f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
1041f43d9309SBill Paul 
1042f43d9309SBill Paul 	sc = device_get_softc(dev);
1043f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1044f43d9309SBill Paul 	ifm = &mii->mii_media;
1045f43d9309SBill Paul 
1046f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
104745521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER|IFM_HPNA_1, 0, NULL);
104896f2e892SBill Paul 
104996f2e892SBill Paul 	return;
105096f2e892SBill Paul }
105196f2e892SBill Paul 
105296f2e892SBill Paul #define DC_POLY		0xEDB88320
105379d11e09SBill Paul #define DC_BITS_512	9
105479d11e09SBill Paul #define DC_BITS_128	7
105579d11e09SBill Paul #define DC_BITS_64	6
105696f2e892SBill Paul 
1057e3d2833aSAlfred Perlstein static u_int32_t
1058e3d2833aSAlfred Perlstein dc_crc_le(sc, addr)
105996f2e892SBill Paul 	struct dc_softc		*sc;
106096f2e892SBill Paul 	caddr_t			addr;
106196f2e892SBill Paul {
106296f2e892SBill Paul 	u_int32_t		idx, bit, data, crc;
106396f2e892SBill Paul 
106496f2e892SBill Paul 	/* Compute CRC for the address value. */
106596f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
106696f2e892SBill Paul 
106796f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
106896f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
106996f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
107096f2e892SBill Paul 	}
107196f2e892SBill Paul 
107279d11e09SBill Paul 	/*
107379d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
107479d11e09SBill Paul 	 * chips is only 128 bits wide.
107579d11e09SBill Paul 	 */
107679d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
107779d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
107896f2e892SBill Paul 
107979d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
108079d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
108179d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
108279d11e09SBill Paul 
1083feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1084feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1085feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1086feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
1087feb78939SJonathan Chen 			return (crc & 0x0F) + (crc	& 0x70)*3 + (14 << 4);
1088feb78939SJonathan Chen 		else
1089feb78939SJonathan Chen 			return (crc & 0x1F) + ((crc>>1) & 0xF0)*3 + (12 << 4);
1090feb78939SJonathan Chen 	}
1091feb78939SJonathan Chen 
109279d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
109396f2e892SBill Paul }
109496f2e892SBill Paul 
109596f2e892SBill Paul /*
109696f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
109796f2e892SBill Paul  */
1098e3d2833aSAlfred Perlstein static u_int32_t
1099e3d2833aSAlfred Perlstein dc_crc_be(addr)
110096f2e892SBill Paul 	caddr_t			addr;
110196f2e892SBill Paul {
110296f2e892SBill Paul 	u_int32_t		crc, carry;
110396f2e892SBill Paul 	int			i, j;
110496f2e892SBill Paul 	u_int8_t		c;
110596f2e892SBill Paul 
110696f2e892SBill Paul 	/* Compute CRC for the address value. */
110796f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
110896f2e892SBill Paul 
110996f2e892SBill Paul 	for (i = 0; i < 6; i++) {
111096f2e892SBill Paul 		c = *(addr + i);
111196f2e892SBill Paul 		for (j = 0; j < 8; j++) {
111296f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
111396f2e892SBill Paul 			crc <<= 1;
111496f2e892SBill Paul 			c >>= 1;
111596f2e892SBill Paul 			if (carry)
111696f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
111796f2e892SBill Paul 		}
111896f2e892SBill Paul 	}
111996f2e892SBill Paul 
112096f2e892SBill Paul 	/* return the filter bit position */
112196f2e892SBill Paul 	return((crc >> 26) & 0x0000003F);
112296f2e892SBill Paul }
112396f2e892SBill Paul 
112496f2e892SBill Paul /*
112596f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
112696f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
112796f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
112896f2e892SBill Paul  *
112996f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
113096f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
113196f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
113296f2e892SBill Paul  * we need that too.
113396f2e892SBill Paul  */
11342c876e15SPoul-Henning Kamp static void
1135e3d2833aSAlfred Perlstein dc_setfilt_21143(sc)
113696f2e892SBill Paul 	struct dc_softc		*sc;
113796f2e892SBill Paul {
113896f2e892SBill Paul 	struct dc_desc		*sframe;
113996f2e892SBill Paul 	u_int32_t		h, *sp;
114096f2e892SBill Paul 	struct ifmultiaddr	*ifma;
114196f2e892SBill Paul 	struct ifnet		*ifp;
114296f2e892SBill Paul 	int			i;
114396f2e892SBill Paul 
114496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
114596f2e892SBill Paul 
114696f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
114796f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
114896f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
114996f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
115096f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
115196f2e892SBill Paul 	bzero((char *)sp, DC_SFRAME_LEN);
115296f2e892SBill Paul 
115396f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
115496f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
115596f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
115696f2e892SBill Paul 
115796f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
115896f2e892SBill Paul 
115996f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
116096f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
116196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116296f2e892SBill Paul 	else
116396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116496f2e892SBill Paul 
116596f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
116696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116796f2e892SBill Paul 	else
116896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116996f2e892SBill Paul 
11706817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
117196f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
117296f2e892SBill Paul 			continue;
117396f2e892SBill Paul 		h = dc_crc_le(sc,
117496f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
117596f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
117696f2e892SBill Paul 	}
117796f2e892SBill Paul 
117896f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
117996f2e892SBill Paul 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
118096f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
118196f2e892SBill Paul 	}
118296f2e892SBill Paul 
118396f2e892SBill Paul 	/* Set our MAC address */
118496f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
118596f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
118696f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
118796f2e892SBill Paul 
118896f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
118996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
119096f2e892SBill Paul 
119196f2e892SBill Paul 	/*
119296f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
119396f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
119496f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
119596f2e892SBill Paul 	 * medicine.
119696f2e892SBill Paul 	 */
119796f2e892SBill Paul 	DELAY(10000);
119896f2e892SBill Paul 
119996f2e892SBill Paul 	ifp->if_timer = 5;
120096f2e892SBill Paul 
120196f2e892SBill Paul 	return;
120296f2e892SBill Paul }
120396f2e892SBill Paul 
12042c876e15SPoul-Henning Kamp static void
1205e3d2833aSAlfred Perlstein dc_setfilt_admtek(sc)
120696f2e892SBill Paul 	struct dc_softc		*sc;
120796f2e892SBill Paul {
120896f2e892SBill Paul 	struct ifnet		*ifp;
120996f2e892SBill Paul 	int			h = 0;
121096f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
121196f2e892SBill Paul 	struct ifmultiaddr	*ifma;
121296f2e892SBill Paul 
121396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
121496f2e892SBill Paul 
121596f2e892SBill Paul 	/* Init our MAC address */
121696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
121796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
121896f2e892SBill Paul 
121996f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
122096f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
122196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122296f2e892SBill Paul 	else
122396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122496f2e892SBill Paul 
122596f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
122696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
122796f2e892SBill Paul 	else
122896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
122996f2e892SBill Paul 
123096f2e892SBill Paul 	/* first, zot all the existing hash bits */
123196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
123296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
123396f2e892SBill Paul 
123496f2e892SBill Paul 	/*
123596f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
123696f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
123796f2e892SBill Paul 	 */
123896f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
123996f2e892SBill Paul 		return;
124096f2e892SBill Paul 
124196f2e892SBill Paul 	/* now program new ones */
12426817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
124396f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
124496f2e892SBill Paul 			continue;
124596f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
124696f2e892SBill Paul 		if (h < 32)
124796f2e892SBill Paul 			hashes[0] |= (1 << h);
124896f2e892SBill Paul 		else
124996f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
125096f2e892SBill Paul 	}
125196f2e892SBill Paul 
125296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
125396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
125496f2e892SBill Paul 
125596f2e892SBill Paul 	return;
125696f2e892SBill Paul }
125796f2e892SBill Paul 
12582c876e15SPoul-Henning Kamp static void
1259e3d2833aSAlfred Perlstein dc_setfilt_asix(sc)
126096f2e892SBill Paul 	struct dc_softc		*sc;
126196f2e892SBill Paul {
126296f2e892SBill Paul 	struct ifnet		*ifp;
126396f2e892SBill Paul 	int			h = 0;
126496f2e892SBill Paul 	u_int32_t		hashes[2] = { 0, 0 };
126596f2e892SBill Paul 	struct ifmultiaddr	*ifma;
126696f2e892SBill Paul 
126796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
126896f2e892SBill Paul 
126996f2e892SBill Paul 	/* Init our MAC address */
127096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
127196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
127296f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
127396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
127496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
127596f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
127696f2e892SBill Paul 
127796f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
127896f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
127996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
128096f2e892SBill Paul 	else
128196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
128296f2e892SBill Paul 
128396f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
128496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
128596f2e892SBill Paul 	else
128696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
128796f2e892SBill Paul 
128896f2e892SBill Paul 	/*
128996f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
129096f2e892SBill Paul 	 * of broadcast frames.
129196f2e892SBill Paul 	 */
129296f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
129396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
129496f2e892SBill Paul 	else
129596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
129696f2e892SBill Paul 
129796f2e892SBill Paul 	/* first, zot all the existing hash bits */
129896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
129996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
130096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
130196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
130296f2e892SBill Paul 
130396f2e892SBill Paul 	/*
130496f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
130596f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
130696f2e892SBill Paul 	 */
130796f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC|IFF_ALLMULTI))
130896f2e892SBill Paul 		return;
130996f2e892SBill Paul 
131096f2e892SBill Paul 	/* now program new ones */
13116817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
131296f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
131396f2e892SBill Paul 			continue;
131496f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
131596f2e892SBill Paul 		if (h < 32)
131696f2e892SBill Paul 			hashes[0] |= (1 << h);
131796f2e892SBill Paul 		else
131896f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
131996f2e892SBill Paul 	}
132096f2e892SBill Paul 
132196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
132296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
132396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
132496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
132596f2e892SBill Paul 
132696f2e892SBill Paul 	return;
132796f2e892SBill Paul }
132896f2e892SBill Paul 
13292c876e15SPoul-Henning Kamp static void
1330e3d2833aSAlfred Perlstein dc_setfilt_xircom(sc)
1331feb78939SJonathan Chen 	struct dc_softc		*sc;
1332feb78939SJonathan Chen {
1333feb78939SJonathan Chen 	struct dc_desc		*sframe;
1334feb78939SJonathan Chen 	u_int32_t		h, *sp;
1335feb78939SJonathan Chen 	struct ifmultiaddr	*ifma;
1336feb78939SJonathan Chen 	struct ifnet		*ifp;
1337feb78939SJonathan Chen 	int			i;
1338feb78939SJonathan Chen 
1339feb78939SJonathan Chen 	ifp = &sc->arpcom.ac_if;
1340feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
1341feb78939SJonathan Chen 
1342feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1343feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1344feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1345feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
1346feb78939SJonathan Chen 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
1347feb78939SJonathan Chen 	bzero((char *)sp, DC_SFRAME_LEN);
1348feb78939SJonathan Chen 
1349feb78939SJonathan Chen 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
1350feb78939SJonathan Chen 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
1351feb78939SJonathan Chen 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
1352feb78939SJonathan Chen 
1353feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
1354feb78939SJonathan Chen 
1355feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1356feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1357feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1358feb78939SJonathan Chen 	else
1359feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1360feb78939SJonathan Chen 
1361feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1362feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1363feb78939SJonathan Chen 	else
1364feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1365feb78939SJonathan Chen 
13666817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1367feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1368feb78939SJonathan Chen 			continue;
13691d5e5310SBill Paul 		h = dc_crc_le(sc,
13701d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1371feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1372feb78939SJonathan Chen 	}
1373feb78939SJonathan Chen 
1374feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1375feb78939SJonathan Chen 		h = dc_crc_le(sc, (caddr_t)&etherbroadcastaddr);
1376feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1377feb78939SJonathan Chen 	}
1378feb78939SJonathan Chen 
1379feb78939SJonathan Chen 	/* Set our MAC address */
1380feb78939SJonathan Chen 	sp[0] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
1381feb78939SJonathan Chen 	sp[1] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
1382feb78939SJonathan Chen 	sp[2] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
1383feb78939SJonathan Chen 
1384feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1385feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1386feb78939SJonathan Chen 	ifp->if_flags |= IFF_RUNNING;
1387feb78939SJonathan Chen 	sframe->dc_status = DC_TXSTAT_OWN;
1388feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1389feb78939SJonathan Chen 
1390feb78939SJonathan Chen 	/*
1391feb78939SJonathan Chen 	 * wait some time...
1392feb78939SJonathan Chen 	 */
1393feb78939SJonathan Chen 	DELAY(1000);
1394feb78939SJonathan Chen 
1395feb78939SJonathan Chen 	ifp->if_timer = 5;
1396feb78939SJonathan Chen 
1397feb78939SJonathan Chen 	return;
1398feb78939SJonathan Chen }
1399feb78939SJonathan Chen 
1400e3d2833aSAlfred Perlstein static void
1401e3d2833aSAlfred Perlstein dc_setfilt(sc)
140296f2e892SBill Paul 	struct dc_softc		*sc;
140396f2e892SBill Paul {
140496f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
14051af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
140696f2e892SBill Paul 		dc_setfilt_21143(sc);
140796f2e892SBill Paul 
140896f2e892SBill Paul 	if (DC_IS_ASIX(sc))
140996f2e892SBill Paul 		dc_setfilt_asix(sc);
141096f2e892SBill Paul 
141196f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
141296f2e892SBill Paul 		dc_setfilt_admtek(sc);
141396f2e892SBill Paul 
1414feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1415feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
1416feb78939SJonathan Chen 
141796f2e892SBill Paul 	return;
141896f2e892SBill Paul }
141996f2e892SBill Paul 
142096f2e892SBill Paul /*
142196f2e892SBill Paul  * In order to fiddle with the
142296f2e892SBill Paul  * 'full-duplex' and '100Mbps' bits in the netconfig register, we
142396f2e892SBill Paul  * first have to put the transmit and/or receive logic in the idle state.
142496f2e892SBill Paul  */
1425e3d2833aSAlfred Perlstein static void
1426e3d2833aSAlfred Perlstein dc_setcfg(sc, media)
142796f2e892SBill Paul 	struct dc_softc		*sc;
142896f2e892SBill Paul 	int			media;
142996f2e892SBill Paul {
143096f2e892SBill Paul 	int			i, restart = 0;
143196f2e892SBill Paul 	u_int32_t		isr;
143296f2e892SBill Paul 
143396f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
143496f2e892SBill Paul 		return;
143596f2e892SBill Paul 
143696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON)) {
143796f2e892SBill Paul 		restart = 1;
143896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON|DC_NETCFG_RX_ON));
143996f2e892SBill Paul 
144096f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
144196f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1442d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1443351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1444351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
144596f2e892SBill Paul 				break;
1446d467c136SBill Paul 			DELAY(10);
144796f2e892SBill Paul 		}
144896f2e892SBill Paul 
144996f2e892SBill Paul 		if (i == DC_TIMEOUT)
145096f2e892SBill Paul 			printf("dc%d: failed to force tx and "
145196f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
145296f2e892SBill Paul 	}
145396f2e892SBill Paul 
145496f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1455042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1456042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
145796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14588273d5f8SBill Paul 			int	watchdogreg;
14598273d5f8SBill Paul 
1460bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14618273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
14628273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14638273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14648273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14654c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1466bf645417SBill Paul 			} else {
1467bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1468bf645417SBill Paul 			}
146996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
147096f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
147196f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
147296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
147396f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
147488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
147596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
147696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1477e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1478e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
147996f2e892SBill Paul 		} else {
148096f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
148196f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
148296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
148396f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
148496f2e892SBill Paul 			}
1485318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1486318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1487318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14885c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14895c1cfac4SBill Paul 				dc_apply_fixup(sc,
14905c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14915c1cfac4SBill Paul 				    IFM_100_TX|IFM_FDX : IFM_100_TX);
149296f2e892SBill Paul 		}
149396f2e892SBill Paul 	}
149496f2e892SBill Paul 
149596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1496042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1497042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
149896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14998273d5f8SBill Paul 			int	watchdogreg;
15008273d5f8SBill Paul 
15018273d5f8SBill Paul 			/* there's a write enable bit here that reads as 1 */
15024c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
15038273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
15048273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
15058273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
15068273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
15074c2efe27SBill Paul 			} else {
15084c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
15094c2efe27SBill Paul 			}
151096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS|
151196f2e892SBill Paul 			    DC_NETCFG_PORTSEL|DC_NETCFG_SCRAMBLER));
151296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
151396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
151488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
151596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
151696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1517e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1518e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
151996f2e892SBill Paul 		} else {
152096f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
152196f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
152296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
152396f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
152496f2e892SBill Paul 			}
152596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1526318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
152796f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
15285c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
15295c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
15305c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
15315c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
15325c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
15335c1cfac4SBill Paul 				else
15345c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
15355c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15365c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
15375c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
15385c1cfac4SBill Paul 				dc_apply_fixup(sc,
15395c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
15405c1cfac4SBill Paul 				    IFM_10_T|IFM_FDX : IFM_10_T);
15415c1cfac4SBill Paul 				DELAY(20000);
15425c1cfac4SBill Paul 			}
154396f2e892SBill Paul 		}
154496f2e892SBill Paul 	}
154596f2e892SBill Paul 
1546f43d9309SBill Paul 	/*
1547f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1548f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1549f43d9309SBill Paul 	 * on the external MII port.
1550f43d9309SBill Paul 	 */
1551f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
155245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1553f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1554f43d9309SBill Paul 			sc->dc_link = 1;
1555f43d9309SBill Paul 		} else {
1556f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1557f43d9309SBill Paul 		}
1558f43d9309SBill Paul 	}
1559f43d9309SBill Paul 
15607e346229SMartin Blapp 	if (DC_IS_ADMTEK(sc))
15617e346229SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
15627e346229SMartin Blapp 
156396f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
156496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
156596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
156696f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
156796f2e892SBill Paul 	} else {
156896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
156996f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
157096f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
157196f2e892SBill Paul 	}
157296f2e892SBill Paul 
157396f2e892SBill Paul 	if (restart)
157496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON|DC_NETCFG_RX_ON);
157596f2e892SBill Paul 
157696f2e892SBill Paul 	return;
157796f2e892SBill Paul }
157896f2e892SBill Paul 
1579e3d2833aSAlfred Perlstein static void
1580e3d2833aSAlfred Perlstein dc_reset(sc)
158196f2e892SBill Paul 	struct dc_softc		*sc;
158296f2e892SBill Paul {
158396f2e892SBill Paul 	register int		i;
158496f2e892SBill Paul 
158596f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
158696f2e892SBill Paul 
158796f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
158896f2e892SBill Paul 		DELAY(10);
158996f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
159096f2e892SBill Paul 			break;
159196f2e892SBill Paul 	}
159296f2e892SBill Paul 
15931af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15941d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
159596f2e892SBill Paul 		DELAY(10000);
159696f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
159796f2e892SBill Paul 		i = 0;
159896f2e892SBill Paul 	}
159996f2e892SBill Paul 
160096f2e892SBill Paul 	if (i == DC_TIMEOUT)
160196f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
160296f2e892SBill Paul 
160396f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
160496f2e892SBill Paul 	DELAY(1000);
160596f2e892SBill Paul 
160696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
160796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
160896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
160996f2e892SBill Paul 
161091cc2adbSBill Paul 	/*
161191cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
161291cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
161391cc2adbSBill Paul 	 * into a state where it will never come out of reset
161491cc2adbSBill Paul 	 * until we reset the whole chip again.
161591cc2adbSBill Paul 	 */
16165c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
161791cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
16185c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
16195c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
16205c1cfac4SBill Paul 	}
162191cc2adbSBill Paul 
162296f2e892SBill Paul 	return;
162396f2e892SBill Paul }
162496f2e892SBill Paul 
1625e3d2833aSAlfred Perlstein static struct dc_type *
1626e3d2833aSAlfred Perlstein dc_devtype(dev)
162796f2e892SBill Paul 	device_t		dev;
162896f2e892SBill Paul {
162996f2e892SBill Paul 	struct dc_type		*t;
163096f2e892SBill Paul 	u_int32_t		rev;
163196f2e892SBill Paul 
163296f2e892SBill Paul 	t = dc_devs;
163396f2e892SBill Paul 
163496f2e892SBill Paul 	while(t->dc_name != NULL) {
163596f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
163696f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
163796f2e892SBill Paul 			/* Check the PCI revision */
163896f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
163996f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
164096f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
164196f2e892SBill Paul 				t++;
164296f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
164396f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
164496f2e892SBill Paul 				t++;
164596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
164679d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
164779d11e09SBill Paul 				t++;
164879d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
164996f2e892SBill Paul 			    rev >= DC_REVISION_98725)
165096f2e892SBill Paul 				t++;
165196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
165296f2e892SBill Paul 			    rev >= DC_REVISION_88141)
165396f2e892SBill Paul 				t++;
165496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
165596f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
165696f2e892SBill Paul 				t++;
165788d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
165888d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
165988d739dcSBill Paul 				t++;
166096f2e892SBill Paul 			return(t);
166196f2e892SBill Paul 		}
166296f2e892SBill Paul 		t++;
166396f2e892SBill Paul 	}
166496f2e892SBill Paul 
166596f2e892SBill Paul 	return(NULL);
166696f2e892SBill Paul }
166796f2e892SBill Paul 
166896f2e892SBill Paul /*
166996f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
167096f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
167196f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
167296f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
167396f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
167496f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
167596f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
167696f2e892SBill Paul  */
1677e3d2833aSAlfred Perlstein static int
1678e3d2833aSAlfred Perlstein dc_probe(dev)
167996f2e892SBill Paul 	device_t		dev;
168096f2e892SBill Paul {
168196f2e892SBill Paul 	struct dc_type		*t;
168296f2e892SBill Paul 
168396f2e892SBill Paul 	t = dc_devtype(dev);
168496f2e892SBill Paul 
168596f2e892SBill Paul 	if (t != NULL) {
168696f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
168796f2e892SBill Paul 		return(0);
168896f2e892SBill Paul 	}
168996f2e892SBill Paul 
169096f2e892SBill Paul 	return(ENXIO);
169196f2e892SBill Paul }
169296f2e892SBill Paul 
1693e3d2833aSAlfred Perlstein static void
1694e3d2833aSAlfred Perlstein dc_acpi(dev)
169596f2e892SBill Paul 	device_t		dev;
169696f2e892SBill Paul {
169796f2e892SBill Paul 	int			unit;
169896f2e892SBill Paul 
169996f2e892SBill Paul 	unit = device_get_unit(dev);
170096f2e892SBill Paul 
170114a00c6cSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
170296f2e892SBill Paul 		u_int32_t		iobase, membase, irq;
170396f2e892SBill Paul 
170496f2e892SBill Paul 		/* Save important PCI config data. */
170596f2e892SBill Paul 		iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
170696f2e892SBill Paul 		membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
170796f2e892SBill Paul 		irq = pci_read_config(dev, DC_PCI_CFIT, 4);
170896f2e892SBill Paul 
170996f2e892SBill Paul 		/* Reset the power state. */
171096f2e892SBill Paul 		printf("dc%d: chip is in D%d power mode "
171114a00c6cSBill Paul 		    "-- setting to D0\n", unit,
171214a00c6cSBill Paul 		    pci_get_powerstate(dev));
171314a00c6cSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
171496f2e892SBill Paul 
171596f2e892SBill Paul 		/* Restore PCI config data. */
171696f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
171796f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
171896f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFIT, irq, 4);
171996f2e892SBill Paul 	}
172014a00c6cSBill Paul 
172196f2e892SBill Paul 	return;
172296f2e892SBill Paul }
172396f2e892SBill Paul 
1724e3d2833aSAlfred Perlstein static void
1725e3d2833aSAlfred Perlstein dc_apply_fixup(sc, media)
17265c1cfac4SBill Paul 	struct dc_softc		*sc;
17275c1cfac4SBill Paul 	int			media;
17285c1cfac4SBill Paul {
17295c1cfac4SBill Paul 	struct dc_mediainfo	*m;
17305c1cfac4SBill Paul 	u_int8_t		*p;
17315c1cfac4SBill Paul 	int			i;
17325d801891SBill Paul 	u_int32_t		reg;
17335c1cfac4SBill Paul 
17345c1cfac4SBill Paul 	m = sc->dc_mi;
17355c1cfac4SBill Paul 
17365c1cfac4SBill Paul 	while (m != NULL) {
17375c1cfac4SBill Paul 		if (m->dc_media == media)
17385c1cfac4SBill Paul 			break;
17395c1cfac4SBill Paul 		m = m->dc_next;
17405c1cfac4SBill Paul 	}
17415c1cfac4SBill Paul 
17425c1cfac4SBill Paul 	if (m == NULL)
17435c1cfac4SBill Paul 		return;
17445c1cfac4SBill Paul 
17455c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
17465c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
17475c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
17485c1cfac4SBill Paul 	}
17495c1cfac4SBill Paul 
17505c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
17515c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
17525c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
17535c1cfac4SBill Paul 	}
17545c1cfac4SBill Paul 
17555c1cfac4SBill Paul 	return;
17565c1cfac4SBill Paul }
17575c1cfac4SBill Paul 
1758e3d2833aSAlfred Perlstein static void
1759e3d2833aSAlfred Perlstein dc_decode_leaf_sia(sc, l)
17605c1cfac4SBill Paul 	struct dc_softc		*sc;
17615c1cfac4SBill Paul 	struct dc_eblock_sia	*l;
17625c1cfac4SBill Paul {
17635c1cfac4SBill Paul 	struct dc_mediainfo	*m;
17645c1cfac4SBill Paul 
17655c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
17663019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
17675c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
17685c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
17695c1cfac4SBill Paul 
17705c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
17715c1cfac4SBill Paul 		m->dc_media = IFM_10_T|IFM_FDX;
17725c1cfac4SBill Paul 
17735c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
17745c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
17755c1cfac4SBill Paul 
17765c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
17775c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
17785c1cfac4SBill Paul 
17795c1cfac4SBill Paul 	m->dc_gp_len = 2;
17805c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
17815c1cfac4SBill Paul 
17825c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17835c1cfac4SBill Paul 	sc->dc_mi = m;
17845c1cfac4SBill Paul 
17855c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
17865c1cfac4SBill Paul 
17875c1cfac4SBill Paul 	return;
17885c1cfac4SBill Paul }
17895c1cfac4SBill Paul 
1790e3d2833aSAlfred Perlstein static void
1791e3d2833aSAlfred Perlstein dc_decode_leaf_sym(sc, l)
17925c1cfac4SBill Paul 	struct dc_softc		*sc;
17935c1cfac4SBill Paul 	struct dc_eblock_sym	*l;
17945c1cfac4SBill Paul {
17955c1cfac4SBill Paul 	struct dc_mediainfo	*m;
17965c1cfac4SBill Paul 
17975c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
17983019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
17995c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
18005c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
18015c1cfac4SBill Paul 
18025c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
18035c1cfac4SBill Paul 		m->dc_media = IFM_100_TX|IFM_FDX;
18045c1cfac4SBill Paul 
18055c1cfac4SBill Paul 	m->dc_gp_len = 2;
18065c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
18075c1cfac4SBill Paul 
18085c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
18095c1cfac4SBill Paul 	sc->dc_mi = m;
18105c1cfac4SBill Paul 
18115c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
18125c1cfac4SBill Paul 
18135c1cfac4SBill Paul 	return;
18145c1cfac4SBill Paul }
18155c1cfac4SBill Paul 
1816e3d2833aSAlfred Perlstein static void
1817e3d2833aSAlfred Perlstein dc_decode_leaf_mii(sc, l)
18185c1cfac4SBill Paul 	struct dc_softc		*sc;
18195c1cfac4SBill Paul 	struct dc_eblock_mii	*l;
18205c1cfac4SBill Paul {
18215c1cfac4SBill Paul 	u_int8_t		*p;
18225c1cfac4SBill Paul 	struct dc_mediainfo	*m;
18235c1cfac4SBill Paul 
18245c1cfac4SBill Paul 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT);
18253019f2bfSBill Paul 	bzero(m, sizeof(struct dc_mediainfo));
18265c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
18275c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
18285c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
18295c1cfac4SBill Paul 
18305c1cfac4SBill Paul 	p = (u_int8_t *)l;
18315c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
18325c1cfac4SBill Paul 	m->dc_gp_ptr = p;
18335c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
18345c1cfac4SBill Paul 	m->dc_reset_len = *p;
18355c1cfac4SBill Paul 	p++;
18365c1cfac4SBill Paul 	m->dc_reset_ptr = p;
18375c1cfac4SBill Paul 
18385c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
18395c1cfac4SBill Paul 	sc->dc_mi = m;
18405c1cfac4SBill Paul 
18415c1cfac4SBill Paul 	return;
18425c1cfac4SBill Paul }
18435c1cfac4SBill Paul 
18442c876e15SPoul-Henning Kamp static void
18452c876e15SPoul-Henning Kamp dc_read_srom(sc, bits)
18463097aa70SWarner Losh 	struct dc_softc		*sc;
18473097aa70SWarner Losh 	int			bits;
18483097aa70SWarner Losh {
18493097aa70SWarner Losh 	int size;
18503097aa70SWarner Losh 
18513097aa70SWarner Losh 	size = 2 << bits;
18523097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
18533097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
18543097aa70SWarner Losh }
18553097aa70SWarner Losh 
1856e3d2833aSAlfred Perlstein static void
1857e3d2833aSAlfred Perlstein dc_parse_21143_srom(sc)
18585c1cfac4SBill Paul 	struct dc_softc		*sc;
18595c1cfac4SBill Paul {
18605c1cfac4SBill Paul 	struct dc_leaf_hdr	*lhdr;
18615c1cfac4SBill Paul 	struct dc_eblock_hdr	*hdr;
18625c1cfac4SBill Paul 	int			i, loff;
18635c1cfac4SBill Paul 	char			*ptr;
18645c1cfac4SBill Paul 
18655c1cfac4SBill Paul 	loff = sc->dc_srom[27];
18665c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
18675c1cfac4SBill Paul 
18685c1cfac4SBill Paul 	ptr = (char *)lhdr;
18695c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
18705c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
18715c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
18725c1cfac4SBill Paul 		switch(hdr->dc_type) {
18735c1cfac4SBill Paul 		case DC_EBLOCK_MII:
18745c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
18755c1cfac4SBill Paul 			break;
18765c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
18775c1cfac4SBill Paul 			dc_decode_leaf_sia(sc, (struct dc_eblock_sia *)hdr);
18785c1cfac4SBill Paul 			break;
18795c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
18805c1cfac4SBill Paul 			dc_decode_leaf_sym(sc, (struct dc_eblock_sym *)hdr);
18815c1cfac4SBill Paul 			break;
18825c1cfac4SBill Paul 		default:
18835c1cfac4SBill Paul 			/* Don't care. Yet. */
18845c1cfac4SBill Paul 			break;
18855c1cfac4SBill Paul 		}
18865c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18875c1cfac4SBill Paul 		ptr++;
18885c1cfac4SBill Paul 	}
18895c1cfac4SBill Paul 
18905c1cfac4SBill Paul 	return;
18915c1cfac4SBill Paul }
18925c1cfac4SBill Paul 
189396f2e892SBill Paul /*
189496f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
189596f2e892SBill Paul  * setup and ethernet/BPF attach.
189696f2e892SBill Paul  */
1897e3d2833aSAlfred Perlstein static int
1898e3d2833aSAlfred Perlstein dc_attach(dev)
189996f2e892SBill Paul 	device_t		dev;
190096f2e892SBill Paul {
1901d1ce9105SBill Paul 	int			tmp = 0;
190296f2e892SBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
190396f2e892SBill Paul 	u_int32_t		command;
190496f2e892SBill Paul 	struct dc_softc		*sc;
190596f2e892SBill Paul 	struct ifnet		*ifp;
190696f2e892SBill Paul 	u_int32_t		revision;
190796f2e892SBill Paul 	int			unit, error = 0, rid, mac_offset;
1908e7b01d07SWarner Losh 	u_int8_t		*mac;
190996f2e892SBill Paul 
191096f2e892SBill Paul 	sc = device_get_softc(dev);
191196f2e892SBill Paul 	unit = device_get_unit(dev);
191296f2e892SBill Paul 	bzero(sc, sizeof(struct dc_softc));
191396f2e892SBill Paul 
19146008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
19156008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
1916031fc810SBill Paul 
191796f2e892SBill Paul 	/*
191896f2e892SBill Paul 	 * Handle power management nonsense.
191996f2e892SBill Paul 	 */
192096f2e892SBill Paul 	dc_acpi(dev);
192196f2e892SBill Paul 
192296f2e892SBill Paul 	/*
192396f2e892SBill Paul 	 * Map control/status registers.
192496f2e892SBill Paul 	 */
192507f65363SBill Paul 	pci_enable_busmaster(dev);
192675ff968cSBill Paul 	pci_enable_io(dev, SYS_RES_IOPORT);
192775ff968cSBill Paul 	pci_enable_io(dev, SYS_RES_MEMORY);
1928c48cc9ceSPeter Wemm 	command = pci_read_config(dev, PCIR_COMMAND, 4);
192996f2e892SBill Paul 
193096f2e892SBill Paul #ifdef DC_USEIOSPACE
193196f2e892SBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
193296f2e892SBill Paul 		printf("dc%d: failed to enable I/O ports!\n", unit);
193396f2e892SBill Paul 		error = ENXIO;
19349ebe64caSPoul-Henning Kamp 		goto fail_nolock;
193596f2e892SBill Paul 	}
193696f2e892SBill Paul #else
193796f2e892SBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
193896f2e892SBill Paul 		printf("dc%d: failed to enable memory mapping!\n", unit);
193996f2e892SBill Paul 		error = ENXIO;
19409ebe64caSPoul-Henning Kamp 		goto fail_nolock;
194196f2e892SBill Paul 	}
194296f2e892SBill Paul #endif
194396f2e892SBill Paul 
194496f2e892SBill Paul 	rid = DC_RID;
194596f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
194696f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
194796f2e892SBill Paul 
194896f2e892SBill Paul 	if (sc->dc_res == NULL) {
194996f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
195096f2e892SBill Paul 		error = ENXIO;
19519ebe64caSPoul-Henning Kamp 		goto fail_nolock;
195296f2e892SBill Paul 	}
195396f2e892SBill Paul 
195496f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
195596f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
195696f2e892SBill Paul 
195796f2e892SBill Paul 	/* Allocate interrupt */
195896f2e892SBill Paul 	rid = 0;
195996f2e892SBill Paul 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
196096f2e892SBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
196196f2e892SBill Paul 
196296f2e892SBill Paul 	if (sc->dc_irq == NULL) {
196396f2e892SBill Paul 		printf("dc%d: couldn't map interrupt\n", unit);
196496f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
196596f2e892SBill Paul 		error = ENXIO;
19669ebe64caSPoul-Henning Kamp 		goto fail_nolock;
196796f2e892SBill Paul 	}
196896f2e892SBill Paul 
1969b50c6312SJonathan Lemon 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET |
1970b50c6312SJonathan Lemon 	    (IS_MPSAFE ? INTR_MPSAFE : 0),
197196f2e892SBill Paul 	    dc_intr, sc, &sc->dc_intrhand);
197296f2e892SBill Paul 
197396f2e892SBill Paul 	if (error) {
197496f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
197596f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
197696f2e892SBill Paul 		printf("dc%d: couldn't set up irq\n", unit);
19779ebe64caSPoul-Henning Kamp 		goto fail_nolock;
197896f2e892SBill Paul 	}
19799ebe64caSPoul-Henning Kamp 	DC_LOCK(sc);
198096f2e892SBill Paul 
198196f2e892SBill Paul 	/* Need this info to decide on a chip type. */
198296f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
198396f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
198496f2e892SBill Paul 
198596f2e892SBill Paul 	switch(sc->dc_info->dc_did) {
198696f2e892SBill Paul 	case DC_DEVICEID_21143:
198796f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
198896f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
1989042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19905c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
19913097aa70SWarner Losh 		dc_eeprom_width(sc);
19923097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
199396f2e892SBill Paul 		break;
199438deb45fSTom Rhodes 	case DC_DEVICEID_DM9009:
199596f2e892SBill Paul 	case DC_DEVICEID_DM9100:
199696f2e892SBill Paul 	case DC_DEVICEID_DM9102:
199796f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1998318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE|DC_TX_INTR_ALWAYS;
1999318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_TX_STORENFWD;
200096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20010a46b1dcSBill Paul 		/* Increase the latency timer value. */
20020a46b1dcSBill Paul 		command = pci_read_config(dev, DC_PCI_CFLT, 4);
20030a46b1dcSBill Paul 		command &= 0xFFFF00FF;
20040a46b1dcSBill Paul 		command |= 0x00008000;
20050a46b1dcSBill Paul 		pci_write_config(dev, DC_PCI_CFLT, command, 4);
200696f2e892SBill Paul 		break;
200796f2e892SBill Paul 	case DC_DEVICEID_AL981:
200896f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
200996f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
201096f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
201196f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20123097aa70SWarner Losh 		dc_eeprom_width(sc);
20133097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
201496f2e892SBill Paul 		break;
201596f2e892SBill Paul 	case DC_DEVICEID_AN985:
201641fced74SPeter Wemm 	case DC_DEVICEID_FE2500:
2017fa167b8eSBill Paul 	case DC_DEVICEID_EN2242:
2018948c244dSWarner Losh 	case DC_DEVICEID_HAWKING_PN672TX:
201996f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
202096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
202196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
202296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20233097aa70SWarner Losh 		dc_eeprom_width(sc);
20243097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
202596f2e892SBill Paul 		break;
202696f2e892SBill Paul 	case DC_DEVICEID_98713:
202796f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
202896f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
202996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
203096f2e892SBill Paul 		}
2031318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
203296f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2033318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2034318b02fdSBill Paul 		}
2035318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
203696f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
203796f2e892SBill Paul 		break;
203896f2e892SBill Paul 	case DC_DEVICEID_987x5:
20399ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
204079d11e09SBill Paul 		/*
204179d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
204279d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
204379d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
204479d11e09SBill Paul 		 * get the right number of bits out of the
204579d11e09SBill Paul 		 * CRC routine.
204679d11e09SBill Paul 		 */
204779d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
204879d11e09SBill Paul 		    revision < DC_REVISION_98725)
204979d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
205096f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
205196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
2052318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
205396f2e892SBill Paul 		break;
2054ead7cde9SBill Paul 	case DC_DEVICEID_98727:
2055ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2056ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR;
2057ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
2058ead7cde9SBill Paul 		break;
205996f2e892SBill Paul 	case DC_DEVICEID_82C115:
206096f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
206179d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL|DC_TX_USE_TX_INTR|DC_128BIT_HASH;
2062318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL|DC_21143_NWAY;
206396f2e892SBill Paul 		break;
206496f2e892SBill Paul 	case DC_DEVICEID_82C168:
206596f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
206691cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD|DC_TX_INTR_ALWAYS;
206796f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
206896f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
206996f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
207096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
207196f2e892SBill Paul 		break;
207296f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
207396f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
207496f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR|DC_TX_INTR_FIRSTFRAG;
207596f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
207696f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
207796f2e892SBill Paul 		break;
2078feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
2079feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
20802dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
20812dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2082feb78939SJonathan Chen 		/*
2083feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2084feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
20852dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2086feb78939SJonathan Chen 		 */
20873097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2088feb78939SJonathan Chen 		break;
20891af8bec7SBill Paul 	case DC_DEVICEID_RS7112:
20901af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
20911af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
20921af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20931af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20943097aa70SWarner Losh 		dc_eeprom_width(sc);
20953097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
20961af8bec7SBill Paul 		break;
209796f2e892SBill Paul 	default:
209896f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
209996f2e892SBill Paul 		    sc->dc_info->dc_did);
210096f2e892SBill Paul 		break;
210196f2e892SBill Paul 	}
210296f2e892SBill Paul 
210396f2e892SBill Paul 	/* Save the cache line size. */
210488d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
210588d739dcSBill Paul 		sc->dc_cachesize = 0;
210688d739dcSBill Paul 	else
210788d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
210888d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
210996f2e892SBill Paul 
211096f2e892SBill Paul 	/* Reset the adapter. */
211196f2e892SBill Paul 	dc_reset(sc);
211296f2e892SBill Paul 
211396f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2114feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
211596f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
211696f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
211796f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
211896f2e892SBill Paul 	}
211996f2e892SBill Paul 
212096f2e892SBill Paul 	/*
212196f2e892SBill Paul 	 * Try to learn something about the supported media.
212296f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
212396f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
212496f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
212596f2e892SBill Paul 	 * Intel 21143.
212696f2e892SBill Paul 	 */
21275c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
21285c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
21295c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
213096f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
213196f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
213296f2e892SBill Paul 		else
213396f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
213496f2e892SBill Paul 	} else if (!sc->dc_pmode)
213596f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
213696f2e892SBill Paul 
213796f2e892SBill Paul 	/*
213896f2e892SBill Paul 	 * Get station address from the EEPROM.
213996f2e892SBill Paul 	 */
214096f2e892SBill Paul 	switch(sc->dc_type) {
214196f2e892SBill Paul 	case DC_TYPE_98713:
214296f2e892SBill Paul 	case DC_TYPE_98713A:
214396f2e892SBill Paul 	case DC_TYPE_987x5:
214496f2e892SBill Paul 	case DC_TYPE_PNICII:
214596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
214696f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
214796f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
214896f2e892SBill Paul 		break;
214996f2e892SBill Paul 	case DC_TYPE_PNIC:
215096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
215196f2e892SBill Paul 		break;
215296f2e892SBill Paul 	case DC_TYPE_DM9102:
215396f2e892SBill Paul 	case DC_TYPE_21143:
215496f2e892SBill Paul 	case DC_TYPE_ASIX:
215596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
215696f2e892SBill Paul 		break;
215796f2e892SBill Paul 	case DC_TYPE_AL981:
215896f2e892SBill Paul 	case DC_TYPE_AN985:
21593097aa70SWarner Losh 		bcopy(&sc->dc_srom[DC_AL_EE_NODEADDR], (caddr_t)&eaddr,
21603097aa70SWarner Losh 		    ETHER_ADDR_LEN);
216196f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
216296f2e892SBill Paul 		break;
21631af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
21641af8bec7SBill Paul 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr, 6);
21651af8bec7SBill Paul 		break;
2166feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
2167e7b01d07SWarner Losh 		/* The MAC comes from the CIS */
2168e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2169e7b01d07SWarner Losh 		if (!mac) {
2170e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2171e7b01d07SWarner Losh 			goto fail;
2172e7b01d07SWarner Losh 		}
2173e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2174feb78939SJonathan Chen 		break;
217596f2e892SBill Paul 	default:
217696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
217796f2e892SBill Paul 		break;
217896f2e892SBill Paul 	}
217996f2e892SBill Paul 
218096f2e892SBill Paul 	/*
218196f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
218296f2e892SBill Paul 	 */
218396f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
218496f2e892SBill Paul 
218596f2e892SBill Paul 	sc->dc_unit = unit;
218696f2e892SBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
218796f2e892SBill Paul 
218896f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
218996f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
219096f2e892SBill Paul 
219196f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
219296f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
219396f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
219496f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
219596f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
219696f2e892SBill Paul 		error = ENXIO;
219796f2e892SBill Paul 		goto fail;
219896f2e892SBill Paul 	}
219996f2e892SBill Paul 
220096f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
220196f2e892SBill Paul 
220296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
220396f2e892SBill Paul 	ifp->if_softc = sc;
220496f2e892SBill Paul 	ifp->if_unit = unit;
220596f2e892SBill Paul 	ifp->if_name = "dc";
2206feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
220796f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
220896f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
220996f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
221096f2e892SBill Paul 	ifp->if_output = ether_output;
221196f2e892SBill Paul 	ifp->if_start = dc_start;
221296f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
221396f2e892SBill Paul 	ifp->if_init = dc_init;
221496f2e892SBill Paul 	ifp->if_baudrate = 10000000;
221596f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
221696f2e892SBill Paul 
221796f2e892SBill Paul 	/*
22185c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
22195c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
22205c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
22215c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
22225c1cfac4SBill Paul 	 * driver instead.
222396f2e892SBill Paul 	 */
22245c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
22255c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
22265c1cfac4SBill Paul 		tmp = sc->dc_pmode;
22275c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
22285c1cfac4SBill Paul 	}
22295c1cfac4SBill Paul 
223096f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
223196f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
223296f2e892SBill Paul 
223396f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
22345c1cfac4SBill Paul 		sc->dc_pmode = tmp;
22355c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
223696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2237042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
223896f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
223996f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
224078999dd1SBill Paul 		/*
224178999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
224278999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
224378999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
224478999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
224578999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
224678999dd1SBill Paul 		 */
224778999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
224878999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
224996f2e892SBill Paul 		error = 0;
225096f2e892SBill Paul 	}
225196f2e892SBill Paul 
225296f2e892SBill Paul 	if (error) {
225396f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
225496f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
225596f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
225696f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
225796f2e892SBill Paul 		error = ENXIO;
225896f2e892SBill Paul 		goto fail;
225996f2e892SBill Paul 	}
226096f2e892SBill Paul 
2261feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
2262feb78939SJonathan Chen 		/*
2263feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
2264feb78939SJonathan Chen 		 * can talk to the MII.
2265feb78939SJonathan Chen 		 */
2266feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2267feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2268feb78939SJonathan Chen 		DELAY(10);
2269feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2270feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2271feb78939SJonathan Chen 		DELAY(10);
2272feb78939SJonathan Chen 	}
2273feb78939SJonathan Chen 
227496f2e892SBill Paul 	/*
227521b8ebd9SArchie Cobbs 	 * Call MI attach routine.
227696f2e892SBill Paul 	 */
22779ef8b520SSam Leffler 	ether_ifattach(ifp, eaddr);
2278db40c1aeSDoug Ambrisko 
2279db40c1aeSDoug Ambrisko 	/*
2280db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2281db40c1aeSDoug Ambrisko 	 */
2282db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
22839ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
2284db40c1aeSDoug Ambrisko 
2285b50c6312SJonathan Lemon 	callout_init(&sc->dc_stat_ch, IS_MPSAFE);
228696f2e892SBill Paul 
22875c1cfac4SBill Paul #ifdef SRM_MEDIA
2288510a809eSMike Smith 	sc->dc_srm_media = 0;
2289510a809eSMike Smith 
2290510a809eSMike Smith 	/* Remember the SRM console media setting */
2291510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2292510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2293510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE|DC_CFDD_SLEEP_MODE);
2294510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2295510a809eSMike Smith 		case 3:
2296510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2297510a809eSMike Smith 			break;
2298510a809eSMike Smith 		case 4:
2299510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2300510a809eSMike Smith 			break;
2301510a809eSMike Smith 		case 5:
2302510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2303510a809eSMike Smith 			break;
2304510a809eSMike Smith 		case 6:
2305510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2306510a809eSMike Smith 			break;
2307510a809eSMike Smith 		}
2308510a809eSMike Smith 		if (sc->dc_srm_media)
2309510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2310510a809eSMike Smith 	}
2311510a809eSMike Smith #endif
2312510a809eSMike Smith 
2313d1ce9105SBill Paul 	DC_UNLOCK(sc);
2314d1ce9105SBill Paul 	return(0);
2315510a809eSMike Smith 
231696f2e892SBill Paul fail:
2317d1ce9105SBill Paul 	DC_UNLOCK(sc);
23189ebe64caSPoul-Henning Kamp fail_nolock:
2319d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
232096f2e892SBill Paul 	return(error);
232196f2e892SBill Paul }
232296f2e892SBill Paul 
2323e3d2833aSAlfred Perlstein static int
2324e3d2833aSAlfred Perlstein dc_detach(dev)
232596f2e892SBill Paul 	device_t		dev;
232696f2e892SBill Paul {
232796f2e892SBill Paul 	struct dc_softc		*sc;
232896f2e892SBill Paul 	struct ifnet		*ifp;
23295c1cfac4SBill Paul 	struct dc_mediainfo	*m;
233096f2e892SBill Paul 
233196f2e892SBill Paul 	sc = device_get_softc(dev);
2332d1ce9105SBill Paul 
2333d1ce9105SBill Paul 	DC_LOCK(sc);
2334d1ce9105SBill Paul 
233596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
233696f2e892SBill Paul 
233796f2e892SBill Paul 	dc_stop(sc);
23389ef8b520SSam Leffler 	ether_ifdetach(ifp);
233996f2e892SBill Paul 
234096f2e892SBill Paul 	bus_generic_detach(dev);
234196f2e892SBill Paul 	device_delete_child(dev, sc->dc_miibus);
234296f2e892SBill Paul 
234396f2e892SBill Paul 	bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
234496f2e892SBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
234596f2e892SBill Paul 	bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
234696f2e892SBill Paul 
234796f2e892SBill Paul 	contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
234896f2e892SBill Paul 	if (sc->dc_pnic_rx_buf != NULL)
234996f2e892SBill Paul 		free(sc->dc_pnic_rx_buf, M_DEVBUF);
235096f2e892SBill Paul 
23515c1cfac4SBill Paul 	while(sc->dc_mi != NULL) {
23525c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
23535c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
23545c1cfac4SBill Paul 		sc->dc_mi = m;
23555c1cfac4SBill Paul 	}
23567efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
23575c1cfac4SBill Paul 
2358d1ce9105SBill Paul 	DC_UNLOCK(sc);
2359d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
236096f2e892SBill Paul 
236196f2e892SBill Paul 	return(0);
236296f2e892SBill Paul }
236396f2e892SBill Paul 
236496f2e892SBill Paul /*
236596f2e892SBill Paul  * Initialize the transmit descriptors.
236696f2e892SBill Paul  */
2367e3d2833aSAlfred Perlstein static int
2368e3d2833aSAlfred Perlstein dc_list_tx_init(sc)
236996f2e892SBill Paul 	struct dc_softc		*sc;
237096f2e892SBill Paul {
237196f2e892SBill Paul 	struct dc_chain_data	*cd;
237296f2e892SBill Paul 	struct dc_list_data	*ld;
237301faf54bSLuigi Rizzo 	int			i, nexti;
237496f2e892SBill Paul 
237596f2e892SBill Paul 	cd = &sc->dc_cdata;
237696f2e892SBill Paul 	ld = sc->dc_ldata;
237796f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
237801faf54bSLuigi Rizzo 		nexti = (i == (DC_TX_LIST_CNT - 1)) ? 0 : i+1;
237901faf54bSLuigi Rizzo 		ld->dc_tx_list[i].dc_next = vtophys(&ld->dc_tx_list[nexti]);
238096f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
238196f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
238296f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
238396f2e892SBill Paul 	}
238496f2e892SBill Paul 
238596f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
238696f2e892SBill Paul 
238796f2e892SBill Paul 	return(0);
238896f2e892SBill Paul }
238996f2e892SBill Paul 
239096f2e892SBill Paul 
239196f2e892SBill Paul /*
239296f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
239396f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
239496f2e892SBill Paul  * points back to the first.
239596f2e892SBill Paul  */
2396e3d2833aSAlfred Perlstein static int
2397e3d2833aSAlfred Perlstein dc_list_rx_init(sc)
239896f2e892SBill Paul 	struct dc_softc		*sc;
239996f2e892SBill Paul {
240096f2e892SBill Paul 	struct dc_chain_data	*cd;
240196f2e892SBill Paul 	struct dc_list_data	*ld;
240201faf54bSLuigi Rizzo 	int			i, nexti;
240396f2e892SBill Paul 
240496f2e892SBill Paul 	cd = &sc->dc_cdata;
240596f2e892SBill Paul 	ld = sc->dc_ldata;
240696f2e892SBill Paul 
240796f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
240896f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
240996f2e892SBill Paul 			return(ENOBUFS);
241001faf54bSLuigi Rizzo 		nexti = (i == (DC_RX_LIST_CNT - 1)) ? 0 : i+1;
241101faf54bSLuigi Rizzo 		ld->dc_rx_list[i].dc_next = vtophys(&ld->dc_rx_list[nexti]);
241296f2e892SBill Paul 	}
241396f2e892SBill Paul 
241496f2e892SBill Paul 	cd->dc_rx_prod = 0;
241596f2e892SBill Paul 
241696f2e892SBill Paul 	return(0);
241796f2e892SBill Paul }
241896f2e892SBill Paul 
241996f2e892SBill Paul /*
242096f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
242196f2e892SBill Paul  */
2422e3d2833aSAlfred Perlstein static int
2423e3d2833aSAlfred Perlstein dc_newbuf(sc, i, m)
242496f2e892SBill Paul 	struct dc_softc		*sc;
242596f2e892SBill Paul 	int			i;
242696f2e892SBill Paul 	struct mbuf		*m;
242796f2e892SBill Paul {
242896f2e892SBill Paul 	struct mbuf		*m_new = NULL;
242996f2e892SBill Paul 	struct dc_desc		*c;
243096f2e892SBill Paul 
243196f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
243296f2e892SBill Paul 
243396f2e892SBill Paul 	if (m == NULL) {
243496f2e892SBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
243540129585SLuigi Rizzo 		if (m_new == NULL)
243696f2e892SBill Paul 			return(ENOBUFS);
243796f2e892SBill Paul 
243896f2e892SBill Paul 		MCLGET(m_new, M_DONTWAIT);
243996f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
244096f2e892SBill Paul 			m_freem(m_new);
244196f2e892SBill Paul 			return(ENOBUFS);
244296f2e892SBill Paul 		}
244396f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
244496f2e892SBill Paul 	} else {
244596f2e892SBill Paul 		m_new = m;
244696f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
244796f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
244896f2e892SBill Paul 	}
244996f2e892SBill Paul 
245096f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
245196f2e892SBill Paul 
245296f2e892SBill Paul 	/*
245396f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
245496f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
245596f2e892SBill Paul 	 * 82c169 chips.
245696f2e892SBill Paul 	 */
245796f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
245896f2e892SBill Paul 		bzero((char *)mtod(m_new, char *), m_new->m_len);
245996f2e892SBill Paul 
246096f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
246196f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
246296f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
246396f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
246496f2e892SBill Paul 
246596f2e892SBill Paul 	return(0);
246696f2e892SBill Paul }
246796f2e892SBill Paul 
246896f2e892SBill Paul /*
246996f2e892SBill Paul  * Grrrrr.
247096f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
247196f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
247296f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
247396f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
247496f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
247596f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
247696f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
247796f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
247896f2e892SBill Paul  *
247996f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
248096f2e892SBill Paul  * Here's what we know:
248196f2e892SBill Paul  *
248296f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
248396f2e892SBill Paul  *   descriptors uploaded.
248496f2e892SBill Paul  *
248596f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
248696f2e892SBill Paul  *   total data upload.
248796f2e892SBill Paul  *
248896f2e892SBill Paul  * - We know the size of the desired received frame because it will be
248996f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
249096f2e892SBill Paul  *
249196f2e892SBill Paul  * Here's what we do:
249296f2e892SBill Paul  *
249396f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
249496f2e892SBill Paul  *   This means that we know that the buffer contents should be all
249596f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
249696f2e892SBill Paul  *
249796f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
249896f2e892SBill Paul  *   ethernet CRC at the end.
249996f2e892SBill Paul  *
250096f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
250196f2e892SBill Paul  *
250296f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
250396f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
250496f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
250596f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
250696f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
250796f2e892SBill Paul  *   we won't be fooled.
250896f2e892SBill Paul  *
250996f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
251096f2e892SBill Paul  *   that value from the current pointer location. This brings us
251196f2e892SBill Paul  *   to the start of the actual received packet.
251296f2e892SBill Paul  *
251396f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
251496f2e892SBill Paul  *   frame length.
251596f2e892SBill Paul  *
251696f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
251796f2e892SBill Paul  * the time.
251896f2e892SBill Paul  */
251996f2e892SBill Paul 
252096f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG|DC_RXSTAT_LASTFRAG)
2521e3d2833aSAlfred Perlstein static void
2522e3d2833aSAlfred Perlstein dc_pnic_rx_bug_war(sc, idx)
252396f2e892SBill Paul 	struct dc_softc		*sc;
252496f2e892SBill Paul 	int			idx;
252596f2e892SBill Paul {
252696f2e892SBill Paul 	struct dc_desc		*cur_rx;
252796f2e892SBill Paul 	struct dc_desc		*c = NULL;
252896f2e892SBill Paul 	struct mbuf		*m = NULL;
252996f2e892SBill Paul 	unsigned char		*ptr;
253096f2e892SBill Paul 	int			i, total_len;
253196f2e892SBill Paul 	u_int32_t		rxstat = 0;
253296f2e892SBill Paul 
253396f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
253496f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
253596f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
253696f2e892SBill Paul 	bzero(ptr, sizeof(DC_RXLEN * 5));
253796f2e892SBill Paul 
253896f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
253996f2e892SBill Paul 	while (1) {
254096f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
254196f2e892SBill Paul 		rxstat = c->dc_status;
254296f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
254396f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
254496f2e892SBill Paul 		ptr += DC_RXLEN;
254596f2e892SBill Paul 		/* If this is the last buffer, break out. */
254696f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
254796f2e892SBill Paul 			break;
254896f2e892SBill Paul 		dc_newbuf(sc, i, m);
254996f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
255096f2e892SBill Paul 	}
255196f2e892SBill Paul 
255296f2e892SBill Paul 	/* Find the length of the actual receive frame. */
255396f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
255496f2e892SBill Paul 
255596f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
255696f2e892SBill Paul 	while(*ptr == 0x00)
255796f2e892SBill Paul 		ptr--;
255896f2e892SBill Paul 
255996f2e892SBill Paul 	/* Round off. */
256096f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
256196f2e892SBill Paul 		ptr -= 1;
256296f2e892SBill Paul 
256396f2e892SBill Paul 	/* Now find the start of the frame. */
256496f2e892SBill Paul 	ptr -= total_len;
256596f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
256696f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
256796f2e892SBill Paul 
256896f2e892SBill Paul 	/*
256996f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
257096f2e892SBill Paul 	 * the status word to make it look like a successful
257196f2e892SBill Paul 	 * frame reception.
257296f2e892SBill Paul 	 */
257396f2e892SBill Paul 	dc_newbuf(sc, i, m);
257496f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
257596f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
257696f2e892SBill Paul 
257796f2e892SBill Paul 	return;
257896f2e892SBill Paul }
257996f2e892SBill Paul 
258096f2e892SBill Paul /*
258173bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
258273bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
258373bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
258473bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
258573bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
258673bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
258773bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
258873bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
258973bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
259073bf949cSBill Paul  */
2591e3d2833aSAlfred Perlstein static int
2592e3d2833aSAlfred Perlstein dc_rx_resync(sc)
259373bf949cSBill Paul 	struct dc_softc		*sc;
259473bf949cSBill Paul {
259573bf949cSBill Paul 	int			i, pos;
259673bf949cSBill Paul 	struct dc_desc		*cur_rx;
259773bf949cSBill Paul 
259873bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
259973bf949cSBill Paul 
260073bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
260173bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
260273bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
260373bf949cSBill Paul 			break;
260473bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
260573bf949cSBill Paul 	}
260673bf949cSBill Paul 
260773bf949cSBill Paul 	/* If the ring really is empty, then just return. */
260873bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
260973bf949cSBill Paul 		return(0);
261073bf949cSBill Paul 
261173bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
261273bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
261373bf949cSBill Paul 
261473bf949cSBill Paul 	return(EAGAIN);
261573bf949cSBill Paul }
261673bf949cSBill Paul 
261773bf949cSBill Paul /*
261896f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
261996f2e892SBill Paul  * the higher level protocols.
262096f2e892SBill Paul  */
2621e3d2833aSAlfred Perlstein static void
2622e3d2833aSAlfred Perlstein dc_rxeof(sc)
262396f2e892SBill Paul 	struct dc_softc		*sc;
262496f2e892SBill Paul {
262596f2e892SBill Paul 	struct mbuf		*m;
262696f2e892SBill Paul 	struct ifnet		*ifp;
262796f2e892SBill Paul 	struct dc_desc		*cur_rx;
262896f2e892SBill Paul 	int			i, total_len = 0;
262996f2e892SBill Paul 	u_int32_t		rxstat;
263096f2e892SBill Paul 
263196f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
263296f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
263396f2e892SBill Paul 
263496f2e892SBill Paul 	while(!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
263596f2e892SBill Paul 
2636e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
263762f76486SMaxim Sobolev 		if (ifp->if_flags & IFF_POLLING) {
2638e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2639e4fc250cSLuigi Rizzo 				break;
2640e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2641e4fc250cSLuigi Rizzo 		}
2642e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
264396f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
264496f2e892SBill Paul 		rxstat = cur_rx->dc_status;
264596f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
264696f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
264796f2e892SBill Paul 
264896f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
264996f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
265096f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
265196f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
265296f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
265396f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
265496f2e892SBill Paul 					continue;
265596f2e892SBill Paul 				}
265696f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
265796f2e892SBill Paul 				rxstat = cur_rx->dc_status;
265896f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
265996f2e892SBill Paul 			}
266096f2e892SBill Paul 		}
266196f2e892SBill Paul 
266296f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
266396f2e892SBill Paul 
266496f2e892SBill Paul 		/*
266596f2e892SBill Paul 		 * If an error occurs, update stats, clear the
266696f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
266796f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2668db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
2669db40c1aeSDoug Ambrisko 		 * frames as errors since they could be vlans
267096f2e892SBill Paul 		 */
2671db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)){
2672db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2673db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2674db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2675db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
267696f2e892SBill Paul 				ifp->if_ierrors++;
267796f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
267896f2e892SBill Paul 					ifp->if_collisions++;
267996f2e892SBill Paul 				dc_newbuf(sc, i, m);
268096f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
268196f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
268296f2e892SBill Paul 					continue;
268396f2e892SBill Paul 				} else {
268496f2e892SBill Paul 					dc_init(sc);
268596f2e892SBill Paul 					return;
268696f2e892SBill Paul 				}
268796f2e892SBill Paul 			}
2688db40c1aeSDoug Ambrisko 		}
268996f2e892SBill Paul 
269096f2e892SBill Paul 		/* No errors; receive the packet. */
269196f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
269201faf54bSLuigi Rizzo #ifdef __i386__
269301faf54bSLuigi Rizzo 		/*
269401faf54bSLuigi Rizzo 		 * On the x86 we do not have alignment problems, so try to
269501faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
269601faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
269701faf54bSLuigi Rizzo 		 * copy done in m_devget().
269801faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
269901faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
270001faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
270101faf54bSLuigi Rizzo 		 */
270201faf54bSLuigi Rizzo 		if (dc_quick && dc_newbuf(sc, i, NULL) == 0) {
270301faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
270401faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
270501faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
270601faf54bSLuigi Rizzo 		} else
270701faf54bSLuigi Rizzo #endif
270801faf54bSLuigi Rizzo 		{
270901faf54bSLuigi Rizzo 			struct mbuf *m0;
271096f2e892SBill Paul 
271101faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
271201faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
271396f2e892SBill Paul 			dc_newbuf(sc, i, m);
271496f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
271596f2e892SBill Paul 			if (m0 == NULL) {
271696f2e892SBill Paul 				ifp->if_ierrors++;
271796f2e892SBill Paul 				continue;
271896f2e892SBill Paul 			}
271996f2e892SBill Paul 			m = m0;
272001faf54bSLuigi Rizzo 		}
272196f2e892SBill Paul 
272296f2e892SBill Paul 		ifp->if_ipackets++;
27239ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
272496f2e892SBill Paul 	}
272596f2e892SBill Paul 
272696f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
272796f2e892SBill Paul }
272896f2e892SBill Paul 
272996f2e892SBill Paul /*
273096f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
273196f2e892SBill Paul  * the list buffers.
273296f2e892SBill Paul  */
273396f2e892SBill Paul 
2734e3d2833aSAlfred Perlstein static void
2735e3d2833aSAlfred Perlstein dc_txeof(sc)
273696f2e892SBill Paul 	struct dc_softc		*sc;
273796f2e892SBill Paul {
273896f2e892SBill Paul 	struct dc_desc		*cur_tx = NULL;
273996f2e892SBill Paul 	struct ifnet		*ifp;
274096f2e892SBill Paul 	int			idx;
274196f2e892SBill Paul 
274296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
274396f2e892SBill Paul 
274496f2e892SBill Paul 	/*
274596f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
274696f2e892SBill Paul 	 * frames that have been transmitted.
274796f2e892SBill Paul 	 */
274896f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
274996f2e892SBill Paul 	while(idx != sc->dc_cdata.dc_tx_prod) {
275096f2e892SBill Paul 		u_int32_t		txstat;
275196f2e892SBill Paul 
275296f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
275396f2e892SBill Paul 		txstat = cur_tx->dc_status;
275496f2e892SBill Paul 
275596f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
275696f2e892SBill Paul 			break;
275796f2e892SBill Paul 
275896f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
275996f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
276096f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
276196f2e892SBill Paul 				/*
276296f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
276396f2e892SBill Paul 				 * that it will sometimes generate a TX
276496f2e892SBill Paul 				 * underrun error while DMAing the RX
276596f2e892SBill Paul 				 * filter setup frame. If we detect this,
276696f2e892SBill Paul 				 * we have to send the setup frame again,
276796f2e892SBill Paul 				 * or else the filter won't be programmed
276896f2e892SBill Paul 				 * correctly.
276996f2e892SBill Paul 				 */
277096f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
277196f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
277296f2e892SBill Paul 						dc_setfilt(sc);
277396f2e892SBill Paul 				}
277496f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
277596f2e892SBill Paul 			}
2776bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
277796f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
277896f2e892SBill Paul 			continue;
277996f2e892SBill Paul 		}
278096f2e892SBill Paul 
278129a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2782feb78939SJonathan Chen 			/*
2783feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2784feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
278529a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
278629a2220aSBill Paul 			 * Who knows, but Conexant chips have the
278729a2220aSBill Paul 			 * same problem. Maybe they took lessons
278829a2220aSBill Paul 			 * from Xircom.
278929a2220aSBill Paul 			 */
2790feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2791feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2792feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
2793feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2794feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2795feb78939SJonathan Chen 		} else {
279696f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
279796f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
279896f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM|
279996f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER|DC_TXSTAT_CARRLOST)))
280096f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2801feb78939SJonathan Chen 		}
280296f2e892SBill Paul 
280396f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
280496f2e892SBill Paul 			ifp->if_oerrors++;
280596f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
280696f2e892SBill Paul 				ifp->if_collisions++;
280796f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
280896f2e892SBill Paul 				ifp->if_collisions++;
280996f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
281096f2e892SBill Paul 				dc_init(sc);
281196f2e892SBill Paul 				return;
281296f2e892SBill Paul 			}
281396f2e892SBill Paul 		}
281496f2e892SBill Paul 
281596f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
281696f2e892SBill Paul 
281796f2e892SBill Paul 		ifp->if_opackets++;
281896f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
281996f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
282096f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
282196f2e892SBill Paul 		}
282296f2e892SBill Paul 
282396f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
282496f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
282596f2e892SBill Paul 	}
282696f2e892SBill Paul 
2827bcb9ef4fSLuigi Rizzo 	if (idx != sc->dc_cdata.dc_tx_cons) {
2828bcb9ef4fSLuigi Rizzo 	    	/* some buffers have been freed */
282996f2e892SBill Paul 		sc->dc_cdata.dc_tx_cons = idx;
283096f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
2831bcb9ef4fSLuigi Rizzo 	}
2832bcb9ef4fSLuigi Rizzo 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
283396f2e892SBill Paul 
283496f2e892SBill Paul 	return;
283596f2e892SBill Paul }
283696f2e892SBill Paul 
2837e3d2833aSAlfred Perlstein static void
2838e3d2833aSAlfred Perlstein dc_tick(xsc)
283996f2e892SBill Paul 	void			*xsc;
284096f2e892SBill Paul {
284196f2e892SBill Paul 	struct dc_softc		*sc;
284296f2e892SBill Paul 	struct mii_data		*mii;
284396f2e892SBill Paul 	struct ifnet		*ifp;
284496f2e892SBill Paul 	u_int32_t		r;
284596f2e892SBill Paul 
284696f2e892SBill Paul 	sc = xsc;
2847d1ce9105SBill Paul 	DC_LOCK(sc);
284896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
284996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
285096f2e892SBill Paul 
285196f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2852318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2853318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2854318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2855318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
285696f2e892SBill Paul 				sc->dc_link = 0;
2857318b02fdSBill Paul 				mii_mediachg(mii);
2858318b02fdSBill Paul 			}
2859318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2860318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2861318b02fdSBill Paul 				sc->dc_link = 0;
2862318b02fdSBill Paul 				mii_mediachg(mii);
2863318b02fdSBill Paul 			}
2864d675147eSBill Paul 			if (sc->dc_link == 0)
286596f2e892SBill Paul 				mii_tick(mii);
286696f2e892SBill Paul 		} else {
2867318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
286896f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2869042c8f6eSBill Paul 			    sc->dc_cdata.dc_tx_cnt == 0)
287096f2e892SBill Paul 				mii_tick(mii);
2871042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2872042c8f6eSBill Paul 					sc->dc_link = 0;
287396f2e892SBill Paul 		}
287496f2e892SBill Paul 	} else
287596f2e892SBill Paul 		mii_tick(mii);
287696f2e892SBill Paul 
287796f2e892SBill Paul 	/*
287896f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
287996f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
288096f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
288196f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
288296f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
288396f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
288496f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
288596f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
288696f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
288796f2e892SBill Paul 	 * a screeching halt for several seconds.
288896f2e892SBill Paul 	 *
288996f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
289096f2e892SBill Paul 	 * any packets until a link has been established. After the
289196f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
289296f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
289396f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
289496f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
289596f2e892SBill Paul 	 */
2896cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
289796f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
289896f2e892SBill Paul 		sc->dc_link++;
289996f2e892SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
290096f2e892SBill Paul 			dc_start(ifp);
290196f2e892SBill Paul 	}
290296f2e892SBill Paul 
2903318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2904b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2905318b02fdSBill Paul 	else
2906b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
290796f2e892SBill Paul 
2908d1ce9105SBill Paul 	DC_UNLOCK(sc);
290996f2e892SBill Paul 
291096f2e892SBill Paul 	return;
291196f2e892SBill Paul }
291296f2e892SBill Paul 
2913d467c136SBill Paul /*
2914d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2915d467c136SBill Paul  * or switch to store and forward mode if we have to.
2916d467c136SBill Paul  */
2917e3d2833aSAlfred Perlstein static void
2918e3d2833aSAlfred Perlstein dc_tx_underrun(sc)
2919d467c136SBill Paul 	struct dc_softc		*sc;
2920d467c136SBill Paul {
2921d467c136SBill Paul 	u_int32_t		isr;
2922d467c136SBill Paul 	int			i;
2923d467c136SBill Paul 
2924d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
2925d467c136SBill Paul 		dc_init(sc);
2926d467c136SBill Paul 
2927d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
2928d467c136SBill Paul 		/*
2929d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
2930d467c136SBill Paul 		 * in order to change the transmit threshold or store
2931d467c136SBill Paul 		 * and forward state.
2932d467c136SBill Paul 		 */
2933d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2934d467c136SBill Paul 
2935d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
2936d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
2937d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
2938d467c136SBill Paul 				break;
2939d467c136SBill Paul 			DELAY(10);
2940d467c136SBill Paul 		}
2941d467c136SBill Paul 		if (i == DC_TIMEOUT) {
2942d467c136SBill Paul 			printf("dc%d: failed to force tx to idle state\n",
2943d467c136SBill Paul 			    sc->dc_unit);
2944d467c136SBill Paul 			dc_init(sc);
2945d467c136SBill Paul 		}
2946d467c136SBill Paul 	}
2947d467c136SBill Paul 
2948d467c136SBill Paul 	printf("dc%d: TX underrun -- ", sc->dc_unit);
2949d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
2950d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2951d467c136SBill Paul 		printf("using store and forward mode\n");
2952d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2953d467c136SBill Paul 	} else {
2954d467c136SBill Paul 		printf("increasing TX threshold\n");
2955d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2956d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2957d467c136SBill Paul 	}
2958d467c136SBill Paul 
2959d467c136SBill Paul 	if (DC_IS_INTEL(sc))
2960d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2961d467c136SBill Paul 
2962d467c136SBill Paul 	return;
2963d467c136SBill Paul }
2964d467c136SBill Paul 
2965e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2966e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
2967e4fc250cSLuigi Rizzo 
2968e4fc250cSLuigi Rizzo static void
2969e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2970e4fc250cSLuigi Rizzo {
2971e4fc250cSLuigi Rizzo 	struct	dc_softc *sc = ifp->if_softc;
2972e4fc250cSLuigi Rizzo 
2973e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
2974e4fc250cSLuigi Rizzo 		/* Re-enable interrupts. */
2975e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
2976e4fc250cSLuigi Rizzo 		return;
2977e4fc250cSLuigi Rizzo 	}
2978e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
2979e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
2980e4fc250cSLuigi Rizzo 	dc_txeof(sc);
2981e4fc250cSLuigi Rizzo 	if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE))
2982e4fc250cSLuigi Rizzo 		dc_start(ifp);
2983e4fc250cSLuigi Rizzo 
2984e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2985e4fc250cSLuigi Rizzo 		u_int32_t	status;
2986e4fc250cSLuigi Rizzo 
2987e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
2988e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF|
2989e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF|DC_ISR_TX_IDLE|DC_ISR_TX_UNDERRUN|
2990e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
2991e4fc250cSLuigi Rizzo 		if (!status)
2992e4fc250cSLuigi Rizzo 			return;
2993e4fc250cSLuigi Rizzo 		/* ack what we have */
2994e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
2995e4fc250cSLuigi Rizzo 
2996e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO|DC_ISR_RX_NOBUF)) {
2997e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
2998e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
2999e4fc250cSLuigi Rizzo 
3000e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3001e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3002e4fc250cSLuigi Rizzo 		}
3003e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3004e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3005e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3006e4fc250cSLuigi Rizzo 
3007e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3008e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3009e4fc250cSLuigi Rizzo 
3010e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
3011e4fc250cSLuigi Rizzo 			printf("dc_poll: dc%d bus error\n", sc->dc_unit);
3012e4fc250cSLuigi Rizzo 			dc_reset(sc);
3013e4fc250cSLuigi Rizzo 			dc_init(sc);
3014e4fc250cSLuigi Rizzo 		}
3015e4fc250cSLuigi Rizzo 	}
3016e4fc250cSLuigi Rizzo }
3017e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3018e4fc250cSLuigi Rizzo 
3019e3d2833aSAlfred Perlstein static void
3020e3d2833aSAlfred Perlstein dc_intr(arg)
302196f2e892SBill Paul 	void			*arg;
302296f2e892SBill Paul {
302396f2e892SBill Paul 	struct dc_softc		*sc;
302496f2e892SBill Paul 	struct ifnet		*ifp;
302596f2e892SBill Paul 	u_int32_t		status;
302696f2e892SBill Paul 
302796f2e892SBill Paul 	sc = arg;
3028d2a1864bSWarner Losh 
3029e8388e14SMitsuru IWASAKI 	if (sc->suspended) {
3030e8388e14SMitsuru IWASAKI 		return;
3031e8388e14SMitsuru IWASAKI 	}
3032e8388e14SMitsuru IWASAKI 
3033d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
3034d2a1864bSWarner Losh 		return;
3035d2a1864bSWarner Losh 
3036d1ce9105SBill Paul 	DC_LOCK(sc);
303796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
3038e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
303962f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
3040e4fc250cSLuigi Rizzo 		goto done;
3041e4fc250cSLuigi Rizzo 	if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */
3042e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3043e4fc250cSLuigi Rizzo 		goto done;
3044e4fc250cSLuigi Rizzo 	}
3045e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
304696f2e892SBill Paul 
3047d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
304896f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
304996f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
305096f2e892SBill Paul 			dc_stop(sc);
3051d1ce9105SBill Paul 		DC_UNLOCK(sc);
305296f2e892SBill Paul 		return;
305396f2e892SBill Paul 	}
305496f2e892SBill Paul 
305596f2e892SBill Paul 	/* Disable interrupts. */
305696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
305796f2e892SBill Paul 
3058feb78939SJonathan Chen 	while(((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
3059feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
306096f2e892SBill Paul 
306196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
306296f2e892SBill Paul 
306373bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
306473bf949cSBill Paul 			int		curpkts;
306573bf949cSBill Paul 			curpkts = ifp->if_ipackets;
306696f2e892SBill Paul 			dc_rxeof(sc);
306773bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
306873bf949cSBill Paul 				while(dc_rx_resync(sc))
306973bf949cSBill Paul 					dc_rxeof(sc);
307073bf949cSBill Paul 			}
307173bf949cSBill Paul 		}
307296f2e892SBill Paul 
307396f2e892SBill Paul 		if (status & (DC_ISR_TX_OK|DC_ISR_TX_NOBUF))
307496f2e892SBill Paul 			dc_txeof(sc);
307596f2e892SBill Paul 
307696f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
307796f2e892SBill Paul 			dc_txeof(sc);
307896f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
307996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
308096f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
308196f2e892SBill Paul 			}
308296f2e892SBill Paul 		}
308396f2e892SBill Paul 
3084d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3085d467c136SBill Paul 			dc_tx_underrun(sc);
308696f2e892SBill Paul 
308796f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
308873bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
308973bf949cSBill Paul 			int		curpkts;
309073bf949cSBill Paul 			curpkts = ifp->if_ipackets;
309196f2e892SBill Paul 			dc_rxeof(sc);
309273bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
309373bf949cSBill Paul 				while(dc_rx_resync(sc))
309473bf949cSBill Paul 					dc_rxeof(sc);
309573bf949cSBill Paul 			}
309673bf949cSBill Paul 		}
309796f2e892SBill Paul 
309896f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
309996f2e892SBill Paul 			dc_reset(sc);
310096f2e892SBill Paul 			dc_init(sc);
310196f2e892SBill Paul 		}
310296f2e892SBill Paul 	}
310396f2e892SBill Paul 
310496f2e892SBill Paul 	/* Re-enable interrupts. */
310596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
310696f2e892SBill Paul 
310796f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
310896f2e892SBill Paul 		dc_start(ifp);
310996f2e892SBill Paul 
3110d9700bb5SBill Paul #ifdef DEVICE_POLLING
3111e4fc250cSLuigi Rizzo done:
3112d9700bb5SBill Paul #endif /* DEVICE_POLLING */
3113d9700bb5SBill Paul 
3114d1ce9105SBill Paul 	DC_UNLOCK(sc);
3115d1ce9105SBill Paul 
311696f2e892SBill Paul 	return;
311796f2e892SBill Paul }
311896f2e892SBill Paul 
311996f2e892SBill Paul /*
312096f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
312196f2e892SBill Paul  * pointers to the fragment pointers.
312296f2e892SBill Paul  */
3123e3d2833aSAlfred Perlstein static int
3124e3d2833aSAlfred Perlstein dc_encap(sc, m_head, txidx)
312596f2e892SBill Paul 	struct dc_softc		*sc;
312696f2e892SBill Paul 	struct mbuf		*m_head;
312796f2e892SBill Paul 	u_int32_t		*txidx;
312896f2e892SBill Paul {
312996f2e892SBill Paul 	struct dc_desc		*f = NULL;
313096f2e892SBill Paul 	struct mbuf		*m;
313196f2e892SBill Paul 	int			frag, cur, cnt = 0;
313296f2e892SBill Paul 
313396f2e892SBill Paul 	/*
313496f2e892SBill Paul 	 * Start packing the mbufs in this chain into
313596f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
313696f2e892SBill Paul 	 * of fragments or hit the end of the mbuf chain.
313796f2e892SBill Paul 	 */
313896f2e892SBill Paul 	m = m_head;
313996f2e892SBill Paul 	cur = frag = *txidx;
314096f2e892SBill Paul 
314196f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
314296f2e892SBill Paul 		if (m->m_len != 0) {
314396f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
314496f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
314596f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
314696f2e892SBill Paul 					return(ENOBUFS);
314796f2e892SBill Paul 			}
314896f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
314996f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
315096f2e892SBill Paul 				return(ENOBUFS);
315196f2e892SBill Paul 
315296f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
315396f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
315496f2e892SBill Paul 			if (cnt == 0) {
315596f2e892SBill Paul 				f->dc_status = 0;
315696f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
315796f2e892SBill Paul 			} else
315896f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
315996f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
316096f2e892SBill Paul 			cur = frag;
316196f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
316296f2e892SBill Paul 			cnt++;
316396f2e892SBill Paul 		}
316496f2e892SBill Paul 	}
316596f2e892SBill Paul 
316696f2e892SBill Paul 	if (m != NULL)
316796f2e892SBill Paul 		return(ENOBUFS);
316896f2e892SBill Paul 
316996f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
317096f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
317196f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
317296f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
317396f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
317491cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
317591cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
317696f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
317796f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
317896f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
317996f2e892SBill Paul 	*txidx = frag;
318096f2e892SBill Paul 
318196f2e892SBill Paul 	return(0);
318296f2e892SBill Paul }
318396f2e892SBill Paul 
318496f2e892SBill Paul /*
3185fda39fd0SBill Paul  * Coalesce an mbuf chain into a single mbuf cluster buffer.
3186fda39fd0SBill Paul  * Needed for some really badly behaved chips that just can't
3187fda39fd0SBill Paul  * do scatter/gather correctly.
3188fda39fd0SBill Paul  */
3189e3d2833aSAlfred Perlstein static int
3190e3d2833aSAlfred Perlstein dc_coal(sc, m_head)
3191fda39fd0SBill Paul 	struct dc_softc		*sc;
3192fda39fd0SBill Paul 	struct mbuf		**m_head;
3193fda39fd0SBill Paul {
3194fda39fd0SBill Paul 	struct mbuf		*m_new, *m;
3195fda39fd0SBill Paul 
3196fda39fd0SBill Paul 	m = *m_head;
3197fda39fd0SBill Paul 	MGETHDR(m_new, M_DONTWAIT, MT_DATA);
319840129585SLuigi Rizzo 	if (m_new == NULL)
3199fda39fd0SBill Paul 		return(ENOBUFS);
3200fda39fd0SBill Paul 	if (m->m_pkthdr.len > MHLEN) {
3201fda39fd0SBill Paul 		MCLGET(m_new, M_DONTWAIT);
3202fda39fd0SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
3203fda39fd0SBill Paul 			m_freem(m_new);
3204fda39fd0SBill Paul 			return(ENOBUFS);
3205fda39fd0SBill Paul 		}
3206fda39fd0SBill Paul 	}
3207fda39fd0SBill Paul 	m_copydata(m, 0, m->m_pkthdr.len, mtod(m_new, caddr_t));
3208fda39fd0SBill Paul 	m_new->m_pkthdr.len = m_new->m_len = m->m_pkthdr.len;
3209fda39fd0SBill Paul 	m_freem(m);
3210fda39fd0SBill Paul 	*m_head = m_new;
3211fda39fd0SBill Paul 
3212fda39fd0SBill Paul 	return(0);
3213fda39fd0SBill Paul }
3214fda39fd0SBill Paul 
3215fda39fd0SBill Paul /*
321696f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
321796f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
321896f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
321996f2e892SBill Paul  * physical addresses.
322096f2e892SBill Paul  */
322196f2e892SBill Paul 
3222e3d2833aSAlfred Perlstein static void
3223e3d2833aSAlfred Perlstein dc_start(ifp)
322496f2e892SBill Paul 	struct ifnet		*ifp;
322596f2e892SBill Paul {
322696f2e892SBill Paul 	struct dc_softc		*sc;
322796f2e892SBill Paul 	struct mbuf		*m_head = NULL;
322896f2e892SBill Paul 	int			idx;
322996f2e892SBill Paul 
323096f2e892SBill Paul 	sc = ifp->if_softc;
323196f2e892SBill Paul 
3232d1ce9105SBill Paul 	DC_LOCK(sc);
323396f2e892SBill Paul 
3234e7be9f9aSBill Paul 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10) {
3235d1ce9105SBill Paul 		DC_UNLOCK(sc);
323696f2e892SBill Paul 		return;
3237d1ce9105SBill Paul 	}
3238d1ce9105SBill Paul 
3239d1ce9105SBill Paul 	if (ifp->if_flags & IFF_OACTIVE) {
3240d1ce9105SBill Paul 		DC_UNLOCK(sc);
3241d1ce9105SBill Paul 		return;
3242d1ce9105SBill Paul 	}
324396f2e892SBill Paul 
324496f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
324596f2e892SBill Paul 
324696f2e892SBill Paul 	while(sc->dc_cdata.dc_tx_chain[idx] == NULL) {
324796f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
324896f2e892SBill Paul 		if (m_head == NULL)
324996f2e892SBill Paul 			break;
325096f2e892SBill Paul 
32512dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
32522dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
32532dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3254fda39fd0SBill Paul 			if (dc_coal(sc, &m_head)) {
3255fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
3256fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
3257fda39fd0SBill Paul 				break;
3258fda39fd0SBill Paul 			}
3259fda39fd0SBill Paul 		}
3260fda39fd0SBill Paul 
326196f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
326296f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
326396f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
326496f2e892SBill Paul 			break;
326596f2e892SBill Paul 		}
326696f2e892SBill Paul 
326796f2e892SBill Paul 		/*
326896f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
326996f2e892SBill Paul 		 * to him.
327096f2e892SBill Paul 		 */
32719ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
32725c1cfac4SBill Paul 
32735c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
32745c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
32755c1cfac4SBill Paul 			break;
32765c1cfac4SBill Paul 		}
327796f2e892SBill Paul 	}
327896f2e892SBill Paul 
327996f2e892SBill Paul 	/* Transmit */
328096f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
328196f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
328296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
328396f2e892SBill Paul 
328496f2e892SBill Paul 	/*
328596f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
328696f2e892SBill Paul 	 */
328796f2e892SBill Paul 	ifp->if_timer = 5;
328896f2e892SBill Paul 
3289d1ce9105SBill Paul 	DC_UNLOCK(sc);
3290d1ce9105SBill Paul 
329196f2e892SBill Paul 	return;
329296f2e892SBill Paul }
329396f2e892SBill Paul 
3294e3d2833aSAlfred Perlstein static void
3295e3d2833aSAlfred Perlstein dc_init(xsc)
329696f2e892SBill Paul 	void			*xsc;
329796f2e892SBill Paul {
329896f2e892SBill Paul 	struct dc_softc		*sc = xsc;
329996f2e892SBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
330096f2e892SBill Paul 	struct mii_data		*mii;
330196f2e892SBill Paul 
3302d1ce9105SBill Paul 	DC_LOCK(sc);
330396f2e892SBill Paul 
330496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
330596f2e892SBill Paul 
330696f2e892SBill Paul 	/*
330796f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
330896f2e892SBill Paul 	 */
330996f2e892SBill Paul 	dc_stop(sc);
331096f2e892SBill Paul 	dc_reset(sc);
331196f2e892SBill Paul 
331296f2e892SBill Paul 	/*
331396f2e892SBill Paul 	 * Set cache alignment and burst length.
331496f2e892SBill Paul 	 */
331588d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
331696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
331796f2e892SBill Paul 	else
331896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME|DC_BUSCTL_MRLE);
3319935fe010SLuigi Rizzo 	/*
3320935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3321935fe010SLuigi Rizzo 	 */
3322935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3323935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
332496f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
332596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
332696f2e892SBill Paul 	} else {
332796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
332896f2e892SBill Paul 	}
332996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
333096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
333196f2e892SBill Paul 	switch(sc->dc_cachesize) {
333296f2e892SBill Paul 	case 32:
333396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
333496f2e892SBill Paul 		break;
333596f2e892SBill Paul 	case 16:
333696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
333796f2e892SBill Paul 		break;
333896f2e892SBill Paul 	case 8:
333996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
334096f2e892SBill Paul 		break;
334196f2e892SBill Paul 	case 0:
334296f2e892SBill Paul 	default:
334396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
334496f2e892SBill Paul 		break;
334596f2e892SBill Paul 	}
334696f2e892SBill Paul 
334796f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
334896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
334996f2e892SBill Paul 	else {
3350d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
335196f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
335296f2e892SBill Paul 		} else {
335396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
335496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
335596f2e892SBill Paul 		}
335696f2e892SBill Paul 	}
335796f2e892SBill Paul 
335896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
335996f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
336096f2e892SBill Paul 
336196f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
336296f2e892SBill Paul 		/*
336396f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
336496f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
336596f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
336696f2e892SBill Paul 		 * document the meaning of these bits so there's no way
336796f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
336896f2e892SBill Paul 		 * number all its own; the rest all use a different one.
336996f2e892SBill Paul 		 */
337096f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
337196f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
337296f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
337396f2e892SBill Paul 		else
337496f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
337596f2e892SBill Paul 	}
337696f2e892SBill Paul 
3377feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3378feb78939SJonathan Chen 		/*
3379feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3380feb78939SJonathan Chen 		 * can talk to the MII.
3381feb78939SJonathan Chen 		 */
3382feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3383feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3384feb78939SJonathan Chen 		DELAY(10);
3385feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3386feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3387feb78939SJonathan Chen 		DELAY(10);
3388feb78939SJonathan Chen 	}
3389feb78939SJonathan Chen 
339096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3391d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
339296f2e892SBill Paul 
339396f2e892SBill Paul 	/* Init circular RX list. */
339496f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
339596f2e892SBill Paul 		printf("dc%d: initialization failed: no "
339696f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
339796f2e892SBill Paul 		dc_stop(sc);
3398d1ce9105SBill Paul 		DC_UNLOCK(sc);
339996f2e892SBill Paul 		return;
340096f2e892SBill Paul 	}
340196f2e892SBill Paul 
340296f2e892SBill Paul 	/*
340396f2e892SBill Paul 	 * Init tx descriptors.
340496f2e892SBill Paul 	 */
340596f2e892SBill Paul 	dc_list_tx_init(sc);
340696f2e892SBill Paul 
340796f2e892SBill Paul 	/*
340896f2e892SBill Paul 	 * Load the address of the RX list.
340996f2e892SBill Paul 	 */
341096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
341196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
341296f2e892SBill Paul 
341396f2e892SBill Paul 	/*
341496f2e892SBill Paul 	 * Enable interrupts.
341596f2e892SBill Paul 	 */
3416e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3417e4fc250cSLuigi Rizzo 	/*
3418e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3419e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3420e4fc250cSLuigi Rizzo 	 * after a reset.
3421e4fc250cSLuigi Rizzo 	 */
342262f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
3423e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3424e4fc250cSLuigi Rizzo 	else
3425e4fc250cSLuigi Rizzo #endif
342696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
342796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
342896f2e892SBill Paul 
342996f2e892SBill Paul 	/* Enable transmitter. */
343096f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
343196f2e892SBill Paul 
343296f2e892SBill Paul 	/*
3433918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3434918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3435918434c8SBill Paul 	 * link and activity indications.
3436918434c8SBill Paul 	 */
343778999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3438918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3439918434c8SBill Paul 		    DC_WDOG_CTLWREN|DC_WDOG_LINK|DC_WDOG_ACTIVITY);
344078999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3441918434c8SBill Paul 	}
3442918434c8SBill Paul 
3443918434c8SBill Paul 	/*
344496f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
344596f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
344696f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
344796f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
344896f2e892SBill Paul 	 */
344996f2e892SBill Paul 	dc_setfilt(sc);
345096f2e892SBill Paul 
345196f2e892SBill Paul 	/* Enable receiver. */
345296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
345396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
345496f2e892SBill Paul 
345596f2e892SBill Paul 	mii_mediachg(mii);
345696f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
345796f2e892SBill Paul 
345896f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
345996f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
346096f2e892SBill Paul 
3461857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
346245521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3463857fd445SBill Paul 		sc->dc_link = 1;
3464857fd445SBill Paul 	else {
3465318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3466b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3467318b02fdSBill Paul 		else
3468b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3469857fd445SBill Paul 	}
347096f2e892SBill Paul 
34715c1cfac4SBill Paul #ifdef SRM_MEDIA
3472510a809eSMike Smith 	if(sc->dc_srm_media) {
3473510a809eSMike Smith 		struct ifreq ifr;
3474510a809eSMike Smith 
3475510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
3476510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3477510a809eSMike Smith 		sc->dc_srm_media = 0;
3478510a809eSMike Smith 	}
3479510a809eSMike Smith #endif
3480d1ce9105SBill Paul 	DC_UNLOCK(sc);
348196f2e892SBill Paul 	return;
348296f2e892SBill Paul }
348396f2e892SBill Paul 
348496f2e892SBill Paul /*
348596f2e892SBill Paul  * Set media options.
348696f2e892SBill Paul  */
3487e3d2833aSAlfred Perlstein static int
3488e3d2833aSAlfred Perlstein dc_ifmedia_upd(ifp)
348996f2e892SBill Paul 	struct ifnet		*ifp;
349096f2e892SBill Paul {
349196f2e892SBill Paul 	struct dc_softc		*sc;
349296f2e892SBill Paul 	struct mii_data		*mii;
3493f43d9309SBill Paul 	struct ifmedia		*ifm;
349496f2e892SBill Paul 
349596f2e892SBill Paul 	sc = ifp->if_softc;
349696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
349796f2e892SBill Paul 	mii_mediachg(mii);
3498f43d9309SBill Paul 	ifm = &mii->mii_media;
3499f43d9309SBill Paul 
3500f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
350145521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3502f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3503f43d9309SBill Paul 	else
350496f2e892SBill Paul 		sc->dc_link = 0;
350596f2e892SBill Paul 
350696f2e892SBill Paul 	return(0);
350796f2e892SBill Paul }
350896f2e892SBill Paul 
350996f2e892SBill Paul /*
351096f2e892SBill Paul  * Report current media status.
351196f2e892SBill Paul  */
3512e3d2833aSAlfred Perlstein static void
3513e3d2833aSAlfred Perlstein dc_ifmedia_sts(ifp, ifmr)
351496f2e892SBill Paul 	struct ifnet		*ifp;
351596f2e892SBill Paul 	struct ifmediareq	*ifmr;
351696f2e892SBill Paul {
351796f2e892SBill Paul 	struct dc_softc		*sc;
351896f2e892SBill Paul 	struct mii_data		*mii;
3519f43d9309SBill Paul 	struct ifmedia		*ifm;
352096f2e892SBill Paul 
352196f2e892SBill Paul 	sc = ifp->if_softc;
352296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
352396f2e892SBill Paul 	mii_pollstat(mii);
3524f43d9309SBill Paul 	ifm = &mii->mii_media;
3525f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
352645521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3527f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3528f43d9309SBill Paul 			ifmr->ifm_status = 0;
3529f43d9309SBill Paul 			return;
3530f43d9309SBill Paul 		}
3531f43d9309SBill Paul 	}
353296f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
353396f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
353496f2e892SBill Paul 
353596f2e892SBill Paul 	return;
353696f2e892SBill Paul }
353796f2e892SBill Paul 
3538e3d2833aSAlfred Perlstein static int
3539e3d2833aSAlfred Perlstein dc_ioctl(ifp, command, data)
354096f2e892SBill Paul 	struct ifnet		*ifp;
354196f2e892SBill Paul 	u_long			command;
354296f2e892SBill Paul 	caddr_t			data;
354396f2e892SBill Paul {
354496f2e892SBill Paul 	struct dc_softc		*sc = ifp->if_softc;
354596f2e892SBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
354696f2e892SBill Paul 	struct mii_data		*mii;
3547d1ce9105SBill Paul 	int			error = 0;
354896f2e892SBill Paul 
3549d1ce9105SBill Paul 	DC_LOCK(sc);
355096f2e892SBill Paul 
355196f2e892SBill Paul 	switch(command) {
355296f2e892SBill Paul 	case SIOCSIFFLAGS:
355396f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
35545d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
35555d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
35565d6dfbbbSLuigi Rizzo 
35575d6dfbbbSLuigi Rizzo 			if (ifp->if_flags & IFF_RUNNING) {
35585d6dfbbbSLuigi Rizzo 				if (need_setfilt)
355996f2e892SBill Paul 					dc_setfilt(sc);
35605d6dfbbbSLuigi Rizzo 			} else {
356196f2e892SBill Paul 				sc->dc_txthresh = 0;
356296f2e892SBill Paul 				dc_init(sc);
356396f2e892SBill Paul 			}
356496f2e892SBill Paul 		} else {
356596f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
356696f2e892SBill Paul 				dc_stop(sc);
356796f2e892SBill Paul 		}
356896f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
356996f2e892SBill Paul 		error = 0;
357096f2e892SBill Paul 		break;
357196f2e892SBill Paul 	case SIOCADDMULTI:
357296f2e892SBill Paul 	case SIOCDELMULTI:
357396f2e892SBill Paul 		dc_setfilt(sc);
357496f2e892SBill Paul 		error = 0;
357596f2e892SBill Paul 		break;
357696f2e892SBill Paul 	case SIOCGIFMEDIA:
357796f2e892SBill Paul 	case SIOCSIFMEDIA:
357896f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
357996f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
35805c1cfac4SBill Paul #ifdef SRM_MEDIA
3581510a809eSMike Smith 		if (sc->dc_srm_media)
3582510a809eSMike Smith 			sc->dc_srm_media = 0;
3583510a809eSMike Smith #endif
358496f2e892SBill Paul 		break;
358596f2e892SBill Paul 	default:
35869ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
358796f2e892SBill Paul 		break;
358896f2e892SBill Paul 	}
358996f2e892SBill Paul 
3590d1ce9105SBill Paul 	DC_UNLOCK(sc);
359196f2e892SBill Paul 
359296f2e892SBill Paul 	return(error);
359396f2e892SBill Paul }
359496f2e892SBill Paul 
3595e3d2833aSAlfred Perlstein static void
3596e3d2833aSAlfred Perlstein dc_watchdog(ifp)
359796f2e892SBill Paul 	struct ifnet		*ifp;
359896f2e892SBill Paul {
359996f2e892SBill Paul 	struct dc_softc		*sc;
360096f2e892SBill Paul 
360196f2e892SBill Paul 	sc = ifp->if_softc;
360296f2e892SBill Paul 
3603d1ce9105SBill Paul 	DC_LOCK(sc);
3604d1ce9105SBill Paul 
360596f2e892SBill Paul 	ifp->if_oerrors++;
360696f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
360796f2e892SBill Paul 
360896f2e892SBill Paul 	dc_stop(sc);
360996f2e892SBill Paul 	dc_reset(sc);
361096f2e892SBill Paul 	dc_init(sc);
361196f2e892SBill Paul 
361296f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
361396f2e892SBill Paul 		dc_start(ifp);
361496f2e892SBill Paul 
3615d1ce9105SBill Paul 	DC_UNLOCK(sc);
3616d1ce9105SBill Paul 
361796f2e892SBill Paul 	return;
361896f2e892SBill Paul }
361996f2e892SBill Paul 
362096f2e892SBill Paul /*
362196f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
362296f2e892SBill Paul  * RX and TX lists.
362396f2e892SBill Paul  */
3624e3d2833aSAlfred Perlstein static void
3625e3d2833aSAlfred Perlstein dc_stop(sc)
362696f2e892SBill Paul 	struct dc_softc		*sc;
362796f2e892SBill Paul {
362896f2e892SBill Paul 	register int		i;
362996f2e892SBill Paul 	struct ifnet		*ifp;
363096f2e892SBill Paul 
3631d1ce9105SBill Paul 	DC_LOCK(sc);
3632d1ce9105SBill Paul 
363396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
363496f2e892SBill Paul 	ifp->if_timer = 0;
363596f2e892SBill Paul 
3636b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
363796f2e892SBill Paul 
36383b3ec200SPeter Wemm 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3639e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3640e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
3641e4fc250cSLuigi Rizzo #endif
36423b3ec200SPeter Wemm 
364396f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON|DC_NETCFG_TX_ON));
364496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
364596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
364696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
364796f2e892SBill Paul 	sc->dc_link = 0;
364896f2e892SBill Paul 
364996f2e892SBill Paul 	/*
365096f2e892SBill Paul 	 * Free data in the RX lists.
365196f2e892SBill Paul 	 */
365296f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
365396f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
365496f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
365596f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
365696f2e892SBill Paul 		}
365796f2e892SBill Paul 	}
365896f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_rx_list,
365996f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_rx_list));
366096f2e892SBill Paul 
366196f2e892SBill Paul 	/*
366296f2e892SBill Paul 	 * Free the TX list buffers.
366396f2e892SBill Paul 	 */
366496f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
366596f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
366696f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
366796f2e892SBill Paul 			    DC_TXCTL_SETUP) {
366896f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
366996f2e892SBill Paul 				continue;
367096f2e892SBill Paul 			}
367196f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
367296f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
367396f2e892SBill Paul 		}
367496f2e892SBill Paul 	}
367596f2e892SBill Paul 
367696f2e892SBill Paul 	bzero((char *)&sc->dc_ldata->dc_tx_list,
367796f2e892SBill Paul 		sizeof(sc->dc_ldata->dc_tx_list));
367896f2e892SBill Paul 
3679d1ce9105SBill Paul 	DC_UNLOCK(sc);
3680d1ce9105SBill Paul 
368196f2e892SBill Paul 	return;
368296f2e892SBill Paul }
368396f2e892SBill Paul 
368496f2e892SBill Paul /*
3685e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3686e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3687e8388e14SMitsuru IWASAKI  * resume.
3688e8388e14SMitsuru IWASAKI  */
3689e3d2833aSAlfred Perlstein static int
3690e3d2833aSAlfred Perlstein dc_suspend(dev)
3691e8388e14SMitsuru IWASAKI 	device_t		dev;
3692e8388e14SMitsuru IWASAKI {
3693e8388e14SMitsuru IWASAKI 	register int		i;
3694e8388e14SMitsuru IWASAKI 	int			s;
3695e8388e14SMitsuru IWASAKI 	struct dc_softc		*sc;
3696e8388e14SMitsuru IWASAKI 
3697e8388e14SMitsuru IWASAKI 	s = splimp();
3698e8388e14SMitsuru IWASAKI 
3699e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3700e8388e14SMitsuru IWASAKI 
3701e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3702e8388e14SMitsuru IWASAKI 
3703e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3704e8388e14SMitsuru IWASAKI 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
3705e8388e14SMitsuru IWASAKI 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
3706e8388e14SMitsuru IWASAKI 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
3707e8388e14SMitsuru IWASAKI 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3708e8388e14SMitsuru IWASAKI 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
3709e8388e14SMitsuru IWASAKI 
3710e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3711e8388e14SMitsuru IWASAKI 
3712e8388e14SMitsuru IWASAKI 	splx(s);
3713e8388e14SMitsuru IWASAKI 	return (0);
3714e8388e14SMitsuru IWASAKI }
3715e8388e14SMitsuru IWASAKI 
3716e8388e14SMitsuru IWASAKI /*
3717e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3718e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3719e8388e14SMitsuru IWASAKI  * appropriate.
3720e8388e14SMitsuru IWASAKI  */
3721e3d2833aSAlfred Perlstein static int
3722e3d2833aSAlfred Perlstein dc_resume(dev)
3723e8388e14SMitsuru IWASAKI 	device_t		dev;
3724e8388e14SMitsuru IWASAKI {
3725e8388e14SMitsuru IWASAKI 	register int		i;
3726e8388e14SMitsuru IWASAKI 	int			s;
3727e8388e14SMitsuru IWASAKI 	struct dc_softc		*sc;
3728e8388e14SMitsuru IWASAKI 	struct ifnet		*ifp;
3729e8388e14SMitsuru IWASAKI 
3730e8388e14SMitsuru IWASAKI 	s = splimp();
3731e8388e14SMitsuru IWASAKI 
3732e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3733e8388e14SMitsuru IWASAKI 	ifp = &sc->arpcom.ac_if;
3734e8388e14SMitsuru IWASAKI 
3735e8388e14SMitsuru IWASAKI 	dc_acpi(dev);
3736e8388e14SMitsuru IWASAKI 
3737e8388e14SMitsuru IWASAKI 	/* better way to do this? */
3738e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3739e8388e14SMitsuru IWASAKI 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
3740e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
3741e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
3742e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
3743e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
3744e8388e14SMitsuru IWASAKI 
3745e8388e14SMitsuru IWASAKI 	/* reenable busmastering */
3746e8388e14SMitsuru IWASAKI 	pci_enable_busmaster(dev);
3747e8388e14SMitsuru IWASAKI 	pci_enable_io(dev, DC_RES);
3748e8388e14SMitsuru IWASAKI 
3749e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3750e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3751e8388e14SMitsuru IWASAKI 		dc_init(sc);
3752e8388e14SMitsuru IWASAKI 
3753e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3754e8388e14SMitsuru IWASAKI 
3755e8388e14SMitsuru IWASAKI 	splx(s);
3756e8388e14SMitsuru IWASAKI 	return (0);
3757e8388e14SMitsuru IWASAKI }
3758e8388e14SMitsuru IWASAKI 
3759e8388e14SMitsuru IWASAKI /*
376096f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
376196f2e892SBill Paul  * get confused by errant DMAs when rebooting.
376296f2e892SBill Paul  */
3763e3d2833aSAlfred Perlstein static void
3764e3d2833aSAlfred Perlstein dc_shutdown(dev)
376596f2e892SBill Paul 	device_t		dev;
376696f2e892SBill Paul {
376796f2e892SBill Paul 	struct dc_softc		*sc;
376896f2e892SBill Paul 
376996f2e892SBill Paul 	sc = device_get_softc(dev);
377096f2e892SBill Paul 
377196f2e892SBill Paul 	dc_stop(sc);
377296f2e892SBill Paul 
377396f2e892SBill Paul 	return;
377496f2e892SBill Paul }
3775