xref: /freebsd/sys/dev/dc/if_dc.c (revision 1edc4c46b896ac39289ed85566c32f4535d2f757)
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 
3396f2e892SBill Paul /*
3496f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
3596f2e892SBill Paul  * series chips and several workalikes including the following:
3696f2e892SBill Paul  *
37ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
3896f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
3996f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4096f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4196f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4296f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
4396f2e892SBill Paul  * ADMtek AN985 (www.admtek.com.tw)
4488d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
459ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
46feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
471d5e5310SBill Paul  * Abocom FE2500
481af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
497eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5096f2e892SBill Paul  *
5196f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5296f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5396f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5496f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5596f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
5696f2e892SBill Paul  * instead of 512.
5796f2e892SBill Paul  *
5896f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
5996f2e892SBill Paul  * Electrical Engineering Department
6096f2e892SBill Paul  * Columbia University, New York City
6196f2e892SBill Paul  */
6296f2e892SBill Paul 
6396f2e892SBill Paul /*
6496f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6596f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6696f2e892SBill Paul  * three kinds of media attachments:
6796f2e892SBill Paul  *
6896f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
6996f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7096f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7196f2e892SBill Paul  * o 10baseT port.
7296f2e892SBill Paul  * o AUI/BNC port.
7396f2e892SBill Paul  *
7496f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7596f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7696f2e892SBill Paul  * autosensing configuration.
7796f2e892SBill Paul  *
7896f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
7996f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8096f2e892SBill Paul  * handled separately due to its different register offsets and the
8196f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8296f2e892SBill Paul  * here, but I'm not thrilled about it.
8396f2e892SBill Paul  *
8496f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8596f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8696f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
8796f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
8896f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
8996f2e892SBill Paul  */
9096f2e892SBill Paul 
918368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
928368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
938368cf8fSDavid E. O'Brien 
9496f2e892SBill Paul #include <sys/param.h>
9596f2e892SBill Paul #include <sys/systm.h>
9696f2e892SBill Paul #include <sys/sockio.h>
9796f2e892SBill Paul #include <sys/mbuf.h>
9896f2e892SBill Paul #include <sys/malloc.h>
9996f2e892SBill Paul #include <sys/kernel.h>
10096f2e892SBill Paul #include <sys/socket.h>
10101faf54bSLuigi Rizzo #include <sys/sysctl.h>
10296f2e892SBill Paul 
10396f2e892SBill Paul #include <net/if.h>
10496f2e892SBill Paul #include <net/if_arp.h>
10596f2e892SBill Paul #include <net/ethernet.h>
10696f2e892SBill Paul #include <net/if_dl.h>
10796f2e892SBill Paul #include <net/if_media.h>
108db40c1aeSDoug Ambrisko #include <net/if_types.h>
109db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11096f2e892SBill Paul 
11196f2e892SBill Paul #include <net/bpf.h>
11296f2e892SBill Paul 
11396f2e892SBill Paul #include <vm/vm.h>              /* for vtophys */
11496f2e892SBill Paul #include <vm/pmap.h>            /* for vtophys */
11596f2e892SBill Paul #include <machine/bus_pio.h>
11696f2e892SBill Paul #include <machine/bus_memio.h>
11796f2e892SBill Paul #include <machine/bus.h>
11896f2e892SBill Paul #include <machine/resource.h>
11996f2e892SBill Paul #include <sys/bus.h>
12096f2e892SBill Paul #include <sys/rman.h>
12196f2e892SBill Paul 
12296f2e892SBill Paul #include <dev/mii/mii.h>
12396f2e892SBill Paul #include <dev/mii/miivar.h>
12496f2e892SBill Paul 
12596f2e892SBill Paul #include <pci/pcireg.h>
12696f2e892SBill Paul #include <pci/pcivar.h>
12796f2e892SBill Paul 
12896f2e892SBill Paul #define DC_USEIOSPACE
1295c1cfac4SBill Paul #ifdef __alpha__
1305c1cfac4SBill Paul #define SRM_MEDIA
1315c1cfac4SBill Paul #endif
13296f2e892SBill Paul 
13396f2e892SBill Paul #include <pci/if_dcreg.h>
13496f2e892SBill Paul 
135f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
136f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
13795a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
13895a16455SPeter Wemm 
13996f2e892SBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
14096f2e892SBill Paul #include "miibus_if.h"
14196f2e892SBill Paul 
14296f2e892SBill Paul /*
14396f2e892SBill Paul  * Various supported device vendors/types and their names.
14496f2e892SBill Paul  */
14596f2e892SBill Paul static struct dc_type dc_devs[] = {
14696f2e892SBill Paul 	{ DC_VENDORID_DEC, DC_DEVICEID_21143,
14796f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
14838deb45fSTom Rhodes 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009,
14938deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
15096f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100,
15196f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
15296f2e892SBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15396f2e892SBill Paul 		"Davicom DM9102 10/100BaseTX" },
15488d739dcSBill Paul 	{ DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102,
15588d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
15696f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AL981,
15796f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
15896f2e892SBill Paul 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_AN985,
15996f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
16096f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16196f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
16296f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16396f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
16496f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16596f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
16696f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
16796f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
16896f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
16996f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17096f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
17196f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17296f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17396f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
17496f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17579d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
17679d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17796f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
178ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
179ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
18096f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
18196f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
18296f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18396f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
18496f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18596f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1869ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1879ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
188fa167b8eSBill Paul 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN2242,
189fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
190feb78939SJonathan Chen 	{ DC_VENDORID_XIRCOM, DC_DEVICEID_X3201,
191feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
1921d5e5310SBill Paul 	{ DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500,
1931d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
1941af8bec7SBill Paul 	{ DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112,
1951af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
196948c244dSWarner Losh 	{ DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX,
197948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
19897f91728SMIHIRA Sanpei Yoshiro 	{ DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T,
19997f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2007eac366bSMartin Blapp 	{ DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB,
2017eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
20296f2e892SBill Paul 	{ 0, 0, NULL }
20396f2e892SBill Paul };
20496f2e892SBill Paul 
205e51a25f8SAlfred Perlstein static int dc_probe		(device_t);
206e51a25f8SAlfred Perlstein static int dc_attach		(device_t);
207e51a25f8SAlfred Perlstein static int dc_detach		(device_t);
208e8388e14SMitsuru IWASAKI static int dc_suspend		(device_t);
209e8388e14SMitsuru IWASAKI static int dc_resume		(device_t);
210e51a25f8SAlfred Perlstein static void dc_acpi		(device_t);
211e51a25f8SAlfred Perlstein static struct dc_type *dc_devtype	(device_t);
212e51a25f8SAlfred Perlstein static int dc_newbuf		(struct dc_softc *, int, struct mbuf *);
213e51a25f8SAlfred Perlstein static int dc_encap		(struct dc_softc *, struct mbuf *, u_int32_t *);
214e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war	(struct dc_softc *, int);
215e51a25f8SAlfred Perlstein static int dc_rx_resync		(struct dc_softc *);
216e51a25f8SAlfred Perlstein static void dc_rxeof		(struct dc_softc *);
217e51a25f8SAlfred Perlstein static void dc_txeof		(struct dc_softc *);
218e51a25f8SAlfred Perlstein static void dc_tick		(void *);
219e51a25f8SAlfred Perlstein static void dc_tx_underrun	(struct dc_softc *);
220e51a25f8SAlfred Perlstein static void dc_intr		(void *);
221e51a25f8SAlfred Perlstein static void dc_start		(struct ifnet *);
222e51a25f8SAlfred Perlstein static int dc_ioctl		(struct ifnet *, u_long, caddr_t);
223e51a25f8SAlfred Perlstein static void dc_init		(void *);
224e51a25f8SAlfred Perlstein static void dc_stop		(struct dc_softc *);
225e51a25f8SAlfred Perlstein static void dc_watchdog		(struct ifnet *);
226e51a25f8SAlfred Perlstein static void dc_shutdown		(device_t);
227e51a25f8SAlfred Perlstein static int dc_ifmedia_upd	(struct ifnet *);
228e51a25f8SAlfred Perlstein static void dc_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
22996f2e892SBill Paul 
230e51a25f8SAlfred Perlstein static void dc_delay		(struct dc_softc *);
231e51a25f8SAlfred Perlstein static void dc_eeprom_idle	(struct dc_softc *);
232e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte	(struct dc_softc *, int);
233e51a25f8SAlfred Perlstein static void dc_eeprom_getword	(struct dc_softc *, int, u_int16_t *);
23496f2e892SBill Paul static void dc_eeprom_getword_pnic
235e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
236feb78939SJonathan Chen static void dc_eeprom_getword_xircom
237e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
2383097aa70SWarner Losh static void dc_eeprom_width	(struct dc_softc *);
239e51a25f8SAlfred Perlstein static void dc_read_eeprom	(struct dc_softc *, caddr_t, int, int, int);
24096f2e892SBill Paul 
241e51a25f8SAlfred Perlstein static void dc_mii_writebit	(struct dc_softc *, int);
242e51a25f8SAlfred Perlstein static int dc_mii_readbit	(struct dc_softc *);
243e51a25f8SAlfred Perlstein static void dc_mii_sync		(struct dc_softc *);
244e51a25f8SAlfred Perlstein static void dc_mii_send		(struct dc_softc *, u_int32_t, int);
245e51a25f8SAlfred Perlstein static int dc_mii_readreg	(struct dc_softc *, struct dc_mii_frame *);
246e51a25f8SAlfred Perlstein static int dc_mii_writereg	(struct dc_softc *, struct dc_mii_frame *);
247e51a25f8SAlfred Perlstein static int dc_miibus_readreg	(device_t, int, int);
248e51a25f8SAlfred Perlstein static int dc_miibus_writereg	(device_t, int, int, int);
249e51a25f8SAlfred Perlstein static void dc_miibus_statchg	(device_t);
250e51a25f8SAlfred Perlstein static void dc_miibus_mediainit	(device_t);
25196f2e892SBill Paul 
252e51a25f8SAlfred Perlstein static void dc_setcfg		(struct dc_softc *, int);
253e51a25f8SAlfred Perlstein static u_int32_t dc_crc_le	(struct dc_softc *, caddr_t);
254e51a25f8SAlfred Perlstein static u_int32_t dc_crc_be	(caddr_t);
255e51a25f8SAlfred Perlstein static void dc_setfilt_21143	(struct dc_softc *);
256e51a25f8SAlfred Perlstein static void dc_setfilt_asix	(struct dc_softc *);
257e51a25f8SAlfred Perlstein static void dc_setfilt_admtek	(struct dc_softc *);
258e51a25f8SAlfred Perlstein static void dc_setfilt_xircom	(struct dc_softc *);
25996f2e892SBill Paul 
260e51a25f8SAlfred Perlstein static void dc_setfilt		(struct dc_softc *);
26196f2e892SBill Paul 
262e51a25f8SAlfred Perlstein static void dc_reset		(struct dc_softc *);
263e51a25f8SAlfred Perlstein static int dc_list_rx_init	(struct dc_softc *);
264e51a25f8SAlfred Perlstein static int dc_list_tx_init	(struct dc_softc *);
26596f2e892SBill Paul 
2663097aa70SWarner Losh static void dc_read_srom	(struct dc_softc *, int);
267e51a25f8SAlfred Perlstein static void dc_parse_21143_srom	(struct dc_softc *);
268e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia	(struct dc_softc *, struct dc_eblock_sia *);
269e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii	(struct dc_softc *, struct dc_eblock_mii *);
270e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym	(struct dc_softc *, struct dc_eblock_sym *);
271e51a25f8SAlfred Perlstein static void dc_apply_fixup	(struct dc_softc *, int);
2725c1cfac4SBill Paul 
27396f2e892SBill Paul #ifdef DC_USEIOSPACE
27496f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
27596f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
27696f2e892SBill Paul #else
27796f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
27896f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
27996f2e892SBill Paul #endif
28096f2e892SBill Paul 
28196f2e892SBill Paul static device_method_t dc_methods[] = {
28296f2e892SBill Paul 	/* Device interface */
28396f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
28496f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
28596f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
286e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
287e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
28896f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
28996f2e892SBill Paul 
29096f2e892SBill Paul 	/* bus interface */
29196f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
29296f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
29396f2e892SBill Paul 
29496f2e892SBill Paul 	/* MII interface */
29596f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
29696f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
29796f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
298f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
29996f2e892SBill Paul 
30096f2e892SBill Paul 	{ 0, 0 }
30196f2e892SBill Paul };
30296f2e892SBill Paul 
30396f2e892SBill Paul static driver_t dc_driver = {
30496f2e892SBill Paul 	"dc",
30596f2e892SBill Paul 	dc_methods,
30696f2e892SBill Paul 	sizeof(struct dc_softc)
30796f2e892SBill Paul };
30896f2e892SBill Paul 
30996f2e892SBill Paul static devclass_t dc_devclass;
31001faf54bSLuigi Rizzo #ifdef __i386__
31101faf54bSLuigi Rizzo static int dc_quick = 1;
31201faf54bSLuigi Rizzo SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW,
31301faf54bSLuigi Rizzo 	&dc_quick,0,"do not mdevget in dc driver");
31401faf54bSLuigi Rizzo #endif
31596f2e892SBill Paul 
316f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, cardbus, dc_driver, dc_devclass, 0, 0);
317f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
31896f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
31996f2e892SBill Paul 
32096f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
32196f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
32296f2e892SBill Paul 
32396f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
32496f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
32596f2e892SBill Paul 
32696f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
32796f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
32896f2e892SBill Paul 
329b50c6312SJonathan Lemon #define IS_MPSAFE 	0
330b50c6312SJonathan Lemon 
331e3d2833aSAlfred Perlstein static void
3320934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
33396f2e892SBill Paul {
33496f2e892SBill Paul 	int idx;
33596f2e892SBill Paul 
33696f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
33796f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
33896f2e892SBill Paul }
33996f2e892SBill Paul 
3402c876e15SPoul-Henning Kamp static void
3410934f18aSMaxime Henrion dc_eeprom_width(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
4050934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
40696f2e892SBill Paul {
4070934f18aSMaxime Henrion 	int i;
40896f2e892SBill Paul 
40996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
41096f2e892SBill Paul 	dc_delay(sc);
41196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
41296f2e892SBill Paul 	dc_delay(sc);
41396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
41496f2e892SBill Paul 	dc_delay(sc);
41596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
41696f2e892SBill Paul 	dc_delay(sc);
41796f2e892SBill Paul 
41896f2e892SBill Paul 	for (i = 0; i < 25; i++) {
41996f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42096f2e892SBill Paul 		dc_delay(sc);
42196f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42296f2e892SBill Paul 		dc_delay(sc);
42396f2e892SBill Paul 	}
42496f2e892SBill Paul 
42596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
42696f2e892SBill Paul 	dc_delay(sc);
42796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
42896f2e892SBill Paul 	dc_delay(sc);
42996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
43096f2e892SBill Paul }
43196f2e892SBill Paul 
43296f2e892SBill Paul /*
43396f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
43496f2e892SBill Paul  */
435e3d2833aSAlfred Perlstein static void
4360934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
43796f2e892SBill Paul {
4380934f18aSMaxime Henrion 	int d, i;
43996f2e892SBill Paul 
4403097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4413097aa70SWarner Losh 	for (i = 3; i--; ) {
4423097aa70SWarner Losh 		if (d & (1 << i))
4433097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
44496f2e892SBill Paul 		else
4453097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4463097aa70SWarner Losh 		dc_delay(sc);
4473097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4483097aa70SWarner Losh 		dc_delay(sc);
4493097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4503097aa70SWarner Losh 		dc_delay(sc);
4513097aa70SWarner Losh 	}
45296f2e892SBill Paul 
45396f2e892SBill Paul 	/*
45496f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
45596f2e892SBill Paul 	 */
4563097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4573097aa70SWarner Losh 		if (addr & (1 << i)) {
45896f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
45996f2e892SBill Paul 		} else {
46096f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
46196f2e892SBill Paul 		}
46296f2e892SBill Paul 		dc_delay(sc);
46396f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
46496f2e892SBill Paul 		dc_delay(sc);
46596f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
46696f2e892SBill Paul 		dc_delay(sc);
46796f2e892SBill Paul 	}
46896f2e892SBill Paul }
46996f2e892SBill Paul 
47096f2e892SBill Paul /*
47196f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
47296f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
47396f2e892SBill Paul  * the EEPROM.
47496f2e892SBill Paul  */
475e3d2833aSAlfred Perlstein static void
4760934f18aSMaxime Henrion dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, u_int16_t *dest)
47796f2e892SBill Paul {
4780934f18aSMaxime Henrion 	int i;
47996f2e892SBill Paul 	u_int32_t r;
48096f2e892SBill Paul 
48196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
48296f2e892SBill Paul 
48396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
48496f2e892SBill Paul 		DELAY(1);
48596f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
48696f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
48796f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
48896f2e892SBill Paul 			return;
48996f2e892SBill Paul 		}
49096f2e892SBill Paul 	}
49196f2e892SBill Paul }
49296f2e892SBill Paul 
49396f2e892SBill Paul /*
49496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
495feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
496feb78939SJonathan Chen  * the EEPROM, too.
497feb78939SJonathan Chen  */
498e3d2833aSAlfred Perlstein static void
4990934f18aSMaxime Henrion dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, u_int16_t *dest)
500feb78939SJonathan Chen {
5010934f18aSMaxime Henrion 
502feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
503feb78939SJonathan Chen 
504feb78939SJonathan Chen 	addr *= 2;
505feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
506feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
507feb78939SJonathan Chen 	addr += 1;
508feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
509feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
510feb78939SJonathan Chen 
511feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
512feb78939SJonathan Chen }
513feb78939SJonathan Chen 
514feb78939SJonathan Chen /*
515feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
51696f2e892SBill Paul  */
517e3d2833aSAlfred Perlstein static void
5180934f18aSMaxime Henrion dc_eeprom_getword(struct dc_softc *sc, int addr, u_int16_t *dest)
51996f2e892SBill Paul {
5200934f18aSMaxime Henrion 	int i;
52196f2e892SBill Paul 	u_int16_t word = 0;
52296f2e892SBill Paul 
52396f2e892SBill Paul 	/* Force EEPROM to idle state. */
52496f2e892SBill Paul 	dc_eeprom_idle(sc);
52596f2e892SBill Paul 
52696f2e892SBill Paul 	/* Enter EEPROM access mode. */
52796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
52896f2e892SBill Paul 	dc_delay(sc);
52996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
53096f2e892SBill Paul 	dc_delay(sc);
53196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
53296f2e892SBill Paul 	dc_delay(sc);
53396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
53496f2e892SBill Paul 	dc_delay(sc);
53596f2e892SBill Paul 
53696f2e892SBill Paul 	/*
53796f2e892SBill Paul 	 * Send address of word we want to read.
53896f2e892SBill Paul 	 */
53996f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
54096f2e892SBill Paul 
54196f2e892SBill Paul 	/*
54296f2e892SBill Paul 	 * Start reading bits from EEPROM.
54396f2e892SBill Paul 	 */
54496f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
54596f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
54696f2e892SBill Paul 		dc_delay(sc);
54796f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
54896f2e892SBill Paul 			word |= i;
54996f2e892SBill Paul 		dc_delay(sc);
55096f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
55196f2e892SBill Paul 		dc_delay(sc);
55296f2e892SBill Paul 	}
55396f2e892SBill Paul 
55496f2e892SBill Paul 	/* Turn off EEPROM access mode. */
55596f2e892SBill Paul 	dc_eeprom_idle(sc);
55696f2e892SBill Paul 
55796f2e892SBill Paul 	*dest = word;
55896f2e892SBill Paul }
55996f2e892SBill Paul 
56096f2e892SBill Paul /*
56196f2e892SBill Paul  * Read a sequence of words from the EEPROM.
56296f2e892SBill Paul  */
563e3d2833aSAlfred Perlstein static void
5640934f18aSMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int swap)
56596f2e892SBill Paul {
56696f2e892SBill Paul 	int i;
56796f2e892SBill Paul 	u_int16_t word = 0, *ptr;
56896f2e892SBill Paul 
56996f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
57096f2e892SBill Paul 		if (DC_IS_PNIC(sc))
57196f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
572feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
573feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
57496f2e892SBill Paul 		else
57596f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
57696f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
57796f2e892SBill Paul 		if (swap)
57896f2e892SBill Paul 			*ptr = ntohs(word);
57996f2e892SBill Paul 		else
58096f2e892SBill Paul 			*ptr = word;
58196f2e892SBill Paul 	}
58296f2e892SBill Paul }
58396f2e892SBill Paul 
58496f2e892SBill Paul /*
58596f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
58696f2e892SBill Paul  * Application Notes pp.19-21.
58796f2e892SBill Paul  */
58896f2e892SBill Paul /*
58996f2e892SBill Paul  * Write a bit to the MII bus.
59096f2e892SBill Paul  */
591e3d2833aSAlfred Perlstein static void
5920934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
59396f2e892SBill Paul {
5940934f18aSMaxime Henrion 
59596f2e892SBill Paul 	if (bit)
59696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
59796f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE | DC_SIO_MII_DATAOUT);
59896f2e892SBill Paul 	else
59996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
60096f2e892SBill Paul 
60196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
60296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
60396f2e892SBill Paul }
60496f2e892SBill Paul 
60596f2e892SBill Paul /*
60696f2e892SBill Paul  * Read a bit from the MII bus.
60796f2e892SBill Paul  */
608e3d2833aSAlfred Perlstein static int
6090934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
61096f2e892SBill Paul {
6110934f18aSMaxime Henrion 
61296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR);
61396f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
61496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
61596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
61696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
61796f2e892SBill Paul 		return (1);
61896f2e892SBill Paul 
61996f2e892SBill Paul 	return (0);
62096f2e892SBill Paul }
62196f2e892SBill Paul 
62296f2e892SBill Paul /*
62396f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
62496f2e892SBill Paul  */
625e3d2833aSAlfred Perlstein static void
6260934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
62796f2e892SBill Paul {
6280934f18aSMaxime Henrion 	int i;
62996f2e892SBill Paul 
63096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
63196f2e892SBill Paul 
63296f2e892SBill Paul 	for (i = 0; i < 32; i++)
63396f2e892SBill Paul 		dc_mii_writebit(sc, 1);
63496f2e892SBill Paul }
63596f2e892SBill Paul 
63696f2e892SBill Paul /*
63796f2e892SBill Paul  * Clock a series of bits through the MII.
63896f2e892SBill Paul  */
639e3d2833aSAlfred Perlstein static void
6400934f18aSMaxime Henrion dc_mii_send(struct dc_softc *sc, u_int32_t bits, int cnt)
64196f2e892SBill Paul {
64296f2e892SBill Paul 	int i;
64396f2e892SBill Paul 
64496f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
64596f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
64696f2e892SBill Paul }
64796f2e892SBill Paul 
64896f2e892SBill Paul /*
64996f2e892SBill Paul  * Read an PHY register through the MII.
65096f2e892SBill Paul  */
651e3d2833aSAlfred Perlstein static int
6520934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
65396f2e892SBill Paul {
654d1ce9105SBill Paul 	int i, ack;
65596f2e892SBill Paul 
656d1ce9105SBill Paul 	DC_LOCK(sc);
65796f2e892SBill Paul 
65896f2e892SBill Paul 	/*
65996f2e892SBill Paul 	 * Set up frame for RX.
66096f2e892SBill Paul 	 */
66196f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
66296f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
66396f2e892SBill Paul 	frame->mii_turnaround = 0;
66496f2e892SBill Paul 	frame->mii_data = 0;
66596f2e892SBill Paul 
66696f2e892SBill Paul 	/*
66796f2e892SBill Paul 	 * Sync the PHYs.
66896f2e892SBill Paul 	 */
66996f2e892SBill Paul 	dc_mii_sync(sc);
67096f2e892SBill Paul 
67196f2e892SBill Paul 	/*
67296f2e892SBill Paul 	 * Send command/address info.
67396f2e892SBill Paul 	 */
67496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
67596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
67696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
67796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
67896f2e892SBill Paul 
67996f2e892SBill Paul #ifdef notdef
68096f2e892SBill Paul 	/* Idle bit */
68196f2e892SBill Paul 	dc_mii_writebit(sc, 1);
68296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
68396f2e892SBill Paul #endif
68496f2e892SBill Paul 
6850934f18aSMaxime Henrion 	/* Check for ack. */
68696f2e892SBill Paul 	ack = dc_mii_readbit(sc);
68796f2e892SBill Paul 
68896f2e892SBill Paul 	/*
68996f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
69096f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
69196f2e892SBill Paul 	 */
69296f2e892SBill Paul 	if (ack) {
6930934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
69496f2e892SBill Paul 			dc_mii_readbit(sc);
69596f2e892SBill Paul 		goto fail;
69696f2e892SBill Paul 	}
69796f2e892SBill Paul 
69896f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
69996f2e892SBill Paul 		if (!ack) {
70096f2e892SBill Paul 			if (dc_mii_readbit(sc))
70196f2e892SBill Paul 				frame->mii_data |= i;
70296f2e892SBill Paul 		}
70396f2e892SBill Paul 	}
70496f2e892SBill Paul 
70596f2e892SBill Paul fail:
70696f2e892SBill Paul 
70796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70996f2e892SBill Paul 
710d1ce9105SBill Paul 	DC_UNLOCK(sc);
71196f2e892SBill Paul 
71296f2e892SBill Paul 	if (ack)
71396f2e892SBill Paul 		return (1);
71496f2e892SBill Paul 	return (0);
71596f2e892SBill Paul }
71696f2e892SBill Paul 
71796f2e892SBill Paul /*
71896f2e892SBill Paul  * Write to a PHY register through the MII.
71996f2e892SBill Paul  */
720e3d2833aSAlfred Perlstein static int
7210934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
72296f2e892SBill Paul {
7230934f18aSMaxime Henrion 
724d1ce9105SBill Paul 	DC_LOCK(sc);
72596f2e892SBill Paul 	/*
72696f2e892SBill Paul 	 * Set up frame for TX.
72796f2e892SBill Paul 	 */
72896f2e892SBill Paul 
72996f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
73096f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
73196f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
73296f2e892SBill Paul 
73396f2e892SBill Paul 	/*
73496f2e892SBill Paul 	 * Sync the PHYs.
73596f2e892SBill Paul 	 */
73696f2e892SBill Paul 	dc_mii_sync(sc);
73796f2e892SBill Paul 
73896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
73996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
74096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
74196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
74296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
74396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
74496f2e892SBill Paul 
74596f2e892SBill Paul 	/* Idle bit. */
74696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
74796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
74896f2e892SBill Paul 
749d1ce9105SBill Paul 	DC_UNLOCK(sc);
75096f2e892SBill Paul 
75196f2e892SBill Paul 	return (0);
75296f2e892SBill Paul }
75396f2e892SBill Paul 
754e3d2833aSAlfred Perlstein static int
7550934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
75696f2e892SBill Paul {
75796f2e892SBill Paul 	struct dc_mii_frame frame;
75896f2e892SBill Paul 	struct dc_softc	 *sc;
759c85c4667SBill Paul 	int i, rval, phy_reg = 0;
76096f2e892SBill Paul 
76196f2e892SBill Paul 	sc = device_get_softc(dev);
7620934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
76396f2e892SBill Paul 
76496f2e892SBill Paul 	/*
76596f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
76696f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
76796f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
76896f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
76996f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
77096f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
77196f2e892SBill Paul 	 * that the PHY is at MII address 1.
77296f2e892SBill Paul 	 */
77396f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
77496f2e892SBill Paul 		return (0);
77596f2e892SBill Paul 
7761af8bec7SBill Paul 	/*
7771af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7781af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7791af8bec7SBill Paul 	 * so we only respond to correct one.
7801af8bec7SBill Paul 	 */
7811af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
7821af8bec7SBill Paul 		return (0);
7831af8bec7SBill Paul 
7845c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
78596f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
78696f2e892SBill Paul 			switch (reg) {
78796f2e892SBill Paul 			case MII_BMSR:
78896f2e892SBill Paul 			/*
78996f2e892SBill Paul 			 * Fake something to make the probe
79096f2e892SBill Paul 			 * code think there's a PHY here.
79196f2e892SBill Paul 			 */
79296f2e892SBill Paul 				return (BMSR_MEDIAMASK);
79396f2e892SBill Paul 				break;
79496f2e892SBill Paul 			case MII_PHYIDR1:
79596f2e892SBill Paul 				if (DC_IS_PNIC(sc))
79696f2e892SBill Paul 					return (DC_VENDORID_LO);
79796f2e892SBill Paul 				return (DC_VENDORID_DEC);
79896f2e892SBill Paul 				break;
79996f2e892SBill Paul 			case MII_PHYIDR2:
80096f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80196f2e892SBill Paul 					return (DC_DEVICEID_82C168);
80296f2e892SBill Paul 				return (DC_DEVICEID_21143);
80396f2e892SBill Paul 				break;
80496f2e892SBill Paul 			default:
80596f2e892SBill Paul 				return (0);
80696f2e892SBill Paul 				break;
80796f2e892SBill Paul 			}
80896f2e892SBill Paul 		} else
80996f2e892SBill Paul 			return (0);
81096f2e892SBill Paul 	}
81196f2e892SBill Paul 
81296f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
81396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
81496f2e892SBill Paul 		    (phy << 23) | (reg << 18));
81596f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
81696f2e892SBill Paul 			DELAY(1);
81796f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
81896f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
81996f2e892SBill Paul 				rval &= 0xFFFF;
82096f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
82196f2e892SBill Paul 			}
82296f2e892SBill Paul 		}
82396f2e892SBill Paul 		return (0);
82496f2e892SBill Paul 	}
82596f2e892SBill Paul 
82696f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
82796f2e892SBill Paul 		switch (reg) {
82896f2e892SBill Paul 		case MII_BMCR:
82996f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
83096f2e892SBill Paul 			break;
83196f2e892SBill Paul 		case MII_BMSR:
83296f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
83396f2e892SBill Paul 			break;
83496f2e892SBill Paul 		case MII_PHYIDR1:
83596f2e892SBill Paul 			phy_reg = DC_AL_VENID;
83696f2e892SBill Paul 			break;
83796f2e892SBill Paul 		case MII_PHYIDR2:
83896f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
83996f2e892SBill Paul 			break;
84096f2e892SBill Paul 		case MII_ANAR:
84196f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
84296f2e892SBill Paul 			break;
84396f2e892SBill Paul 		case MII_ANLPAR:
84496f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
84596f2e892SBill Paul 			break;
84696f2e892SBill Paul 		case MII_ANER:
84796f2e892SBill Paul 			phy_reg = DC_AL_ANER;
84896f2e892SBill Paul 			break;
84996f2e892SBill Paul 		default:
85096f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
85196f2e892SBill Paul 			    sc->dc_unit, reg);
85296f2e892SBill Paul 			return (0);
85396f2e892SBill Paul 			break;
85496f2e892SBill Paul 		}
85596f2e892SBill Paul 
85696f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
85796f2e892SBill Paul 
85896f2e892SBill Paul 		if (rval == 0xFFFF)
85996f2e892SBill Paul 			return (0);
86096f2e892SBill Paul 		return (rval);
86196f2e892SBill Paul 	}
86296f2e892SBill Paul 
86396f2e892SBill Paul 	frame.mii_phyaddr = phy;
86496f2e892SBill Paul 	frame.mii_regaddr = reg;
865419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
866f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
867f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
868419146d9SBill Paul 	}
86996f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
870419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
871f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
87296f2e892SBill Paul 
87396f2e892SBill Paul 	return (frame.mii_data);
87496f2e892SBill Paul }
87596f2e892SBill Paul 
876e3d2833aSAlfred Perlstein static int
8770934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
87896f2e892SBill Paul {
87996f2e892SBill Paul 	struct dc_softc *sc;
88096f2e892SBill Paul 	struct dc_mii_frame frame;
881c85c4667SBill Paul 	int i, phy_reg = 0;
88296f2e892SBill Paul 
88396f2e892SBill Paul 	sc = device_get_softc(dev);
8840934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
88596f2e892SBill Paul 
88696f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
88796f2e892SBill Paul 		return (0);
88896f2e892SBill Paul 
8891af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
8901af8bec7SBill Paul 		return (0);
8911af8bec7SBill Paul 
89296f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
89396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
89496f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
89596f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
89696f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
89796f2e892SBill Paul 				break;
89896f2e892SBill Paul 		}
89996f2e892SBill Paul 		return (0);
90096f2e892SBill Paul 	}
90196f2e892SBill Paul 
90296f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
90396f2e892SBill Paul 		switch (reg) {
90496f2e892SBill Paul 		case MII_BMCR:
90596f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
90696f2e892SBill Paul 			break;
90796f2e892SBill Paul 		case MII_BMSR:
90896f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
90996f2e892SBill Paul 			break;
91096f2e892SBill Paul 		case MII_PHYIDR1:
91196f2e892SBill Paul 			phy_reg = DC_AL_VENID;
91296f2e892SBill Paul 			break;
91396f2e892SBill Paul 		case MII_PHYIDR2:
91496f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
91596f2e892SBill Paul 			break;
91696f2e892SBill Paul 		case MII_ANAR:
91796f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
91896f2e892SBill Paul 			break;
91996f2e892SBill Paul 		case MII_ANLPAR:
92096f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
92196f2e892SBill Paul 			break;
92296f2e892SBill Paul 		case MII_ANER:
92396f2e892SBill Paul 			phy_reg = DC_AL_ANER;
92496f2e892SBill Paul 			break;
92596f2e892SBill Paul 		default:
92696f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
92796f2e892SBill Paul 			    sc->dc_unit, reg);
92896f2e892SBill Paul 			return (0);
92996f2e892SBill Paul 			break;
93096f2e892SBill Paul 		}
93196f2e892SBill Paul 
93296f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
93396f2e892SBill Paul 		return (0);
93496f2e892SBill Paul 	}
93596f2e892SBill Paul 
93696f2e892SBill Paul 	frame.mii_phyaddr = phy;
93796f2e892SBill Paul 	frame.mii_regaddr = reg;
93896f2e892SBill Paul 	frame.mii_data = data;
93996f2e892SBill Paul 
940419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
941f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
942f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
943419146d9SBill Paul 	}
94496f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
945419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
946f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
94796f2e892SBill Paul 
94896f2e892SBill Paul 	return (0);
94996f2e892SBill Paul }
95096f2e892SBill Paul 
951e3d2833aSAlfred Perlstein static void
9520934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
95396f2e892SBill Paul {
95496f2e892SBill Paul 	struct dc_softc *sc;
95596f2e892SBill Paul 	struct mii_data *mii;
956f43d9309SBill Paul 	struct ifmedia *ifm;
95796f2e892SBill Paul 
95896f2e892SBill Paul 	sc = device_get_softc(dev);
95996f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
96096f2e892SBill Paul 		return;
9615c1cfac4SBill Paul 
96296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
963f43d9309SBill Paul 	ifm = &mii->mii_media;
964f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
96545521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
966f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
967f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
968f43d9309SBill Paul 	} else {
96996f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
97096f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
971f43d9309SBill Paul 	}
972f43d9309SBill Paul }
973f43d9309SBill Paul 
974f43d9309SBill Paul /*
975f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
976f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
977f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
978f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
979f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
980f43d9309SBill Paul  * with it itself. *sigh*
981f43d9309SBill Paul  */
982e3d2833aSAlfred Perlstein static void
9830934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
984f43d9309SBill Paul {
985f43d9309SBill Paul 	struct dc_softc *sc;
986f43d9309SBill Paul 	struct mii_data *mii;
987f43d9309SBill Paul 	struct ifmedia *ifm;
988f43d9309SBill Paul 	int rev;
989f43d9309SBill Paul 
990f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
991f43d9309SBill Paul 
992f43d9309SBill Paul 	sc = device_get_softc(dev);
993f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
994f43d9309SBill Paul 	ifm = &mii->mii_media;
995f43d9309SBill Paul 
996f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
99745521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
99896f2e892SBill Paul }
99996f2e892SBill Paul 
100096f2e892SBill Paul #define DC_POLY		0xEDB88320
100179d11e09SBill Paul #define DC_BITS_512	9
100279d11e09SBill Paul #define DC_BITS_128	7
100379d11e09SBill Paul #define DC_BITS_64	6
100496f2e892SBill Paul 
1005e3d2833aSAlfred Perlstein static u_int32_t
10060934f18aSMaxime Henrion dc_crc_le(struct dc_softc *sc, caddr_t addr)
100796f2e892SBill Paul {
100896f2e892SBill Paul 	u_int32_t idx, bit, data, crc;
100996f2e892SBill Paul 
101096f2e892SBill Paul 	/* Compute CRC for the address value. */
101196f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
101296f2e892SBill Paul 
101396f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
101496f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
101596f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
101696f2e892SBill Paul 	}
101796f2e892SBill Paul 
101879d11e09SBill Paul 	/*
101979d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
102079d11e09SBill Paul 	 * chips is only 128 bits wide.
102179d11e09SBill Paul 	 */
102279d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
102379d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
102496f2e892SBill Paul 
102579d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
102679d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
102779d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
102879d11e09SBill Paul 
1029feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1030feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1031feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1032feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10330934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1034feb78939SJonathan Chen 		else
10350934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10360934f18aSMaxime Henrion 			    (12 << 4));
1037feb78939SJonathan Chen 	}
1038feb78939SJonathan Chen 
103979d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
104096f2e892SBill Paul }
104196f2e892SBill Paul 
104296f2e892SBill Paul /*
104396f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
104496f2e892SBill Paul  */
1045e3d2833aSAlfred Perlstein static u_int32_t
10460934f18aSMaxime Henrion dc_crc_be(caddr_t addr)
104796f2e892SBill Paul {
104896f2e892SBill Paul 	u_int32_t crc, carry;
104996f2e892SBill Paul 	int i, j;
105096f2e892SBill Paul 	u_int8_t c;
105196f2e892SBill Paul 
105296f2e892SBill Paul 	/* Compute CRC for the address value. */
105396f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
105496f2e892SBill Paul 
105596f2e892SBill Paul 	for (i = 0; i < 6; i++) {
105696f2e892SBill Paul 		c = *(addr + i);
105796f2e892SBill Paul 		for (j = 0; j < 8; j++) {
105896f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
105996f2e892SBill Paul 			crc <<= 1;
106096f2e892SBill Paul 			c >>= 1;
106196f2e892SBill Paul 			if (carry)
106296f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
106396f2e892SBill Paul 		}
106496f2e892SBill Paul 	}
106596f2e892SBill Paul 
10660934f18aSMaxime Henrion 	/* Return the filter bit position. */
106796f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
106896f2e892SBill Paul }
106996f2e892SBill Paul 
107096f2e892SBill Paul /*
107196f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
107296f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
107396f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
107496f2e892SBill Paul  *
107596f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
107696f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
107796f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
107896f2e892SBill Paul  * we need that too.
107996f2e892SBill Paul  */
10802c876e15SPoul-Henning Kamp static void
10810934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
108296f2e892SBill Paul {
108396f2e892SBill Paul 	struct dc_desc *sframe;
108496f2e892SBill Paul 	u_int32_t h, *sp;
108596f2e892SBill Paul 	struct ifmultiaddr *ifma;
108696f2e892SBill Paul 	struct ifnet *ifp;
108796f2e892SBill Paul 	int i;
108896f2e892SBill Paul 
108996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
109096f2e892SBill Paul 
109196f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
109296f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
109396f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
109496f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
109596f2e892SBill Paul 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
10960934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
109796f2e892SBill Paul 
109896f2e892SBill Paul 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
109996f2e892SBill Paul 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
110096f2e892SBill Paul 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
110196f2e892SBill Paul 
110296f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
110396f2e892SBill Paul 
110496f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
110596f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
110696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110796f2e892SBill Paul 	else
110896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110996f2e892SBill Paul 
111096f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
111196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111296f2e892SBill Paul 	else
111396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111496f2e892SBill Paul 
11156817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
111696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
111796f2e892SBill Paul 			continue;
111896f2e892SBill Paul 		h = dc_crc_le(sc,
111996f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
112096f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
112196f2e892SBill Paul 	}
112296f2e892SBill Paul 
112396f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1124868d8b62SMatthew N. Dodd 		h = dc_crc_le(sc, (caddr_t)ifp->if_broadcastaddr);
112596f2e892SBill Paul 		sp[h >> 4] |= 1 << (h & 0xF);
112696f2e892SBill Paul 	}
112796f2e892SBill Paul 
112896f2e892SBill Paul 	/* Set our MAC address */
112996f2e892SBill Paul 	sp[39] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
113096f2e892SBill Paul 	sp[40] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
113196f2e892SBill Paul 	sp[41] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
113296f2e892SBill Paul 
113396f2e892SBill Paul 	sframe->dc_status = DC_TXSTAT_OWN;
113496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
113596f2e892SBill Paul 
113696f2e892SBill Paul 	/*
113796f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
113896f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
113996f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
114096f2e892SBill Paul 	 * medicine.
114196f2e892SBill Paul 	 */
114296f2e892SBill Paul 	DELAY(10000);
114396f2e892SBill Paul 
114496f2e892SBill Paul 	ifp->if_timer = 5;
114596f2e892SBill Paul }
114696f2e892SBill Paul 
11472c876e15SPoul-Henning Kamp static void
11480934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
114996f2e892SBill Paul {
115096f2e892SBill Paul 	struct ifnet *ifp;
11510934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
115296f2e892SBill Paul 	int h = 0;
115396f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
115496f2e892SBill Paul 
115596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
115696f2e892SBill Paul 
11570934f18aSMaxime Henrion 	/* Init our MAC address. */
115896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
115996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
116096f2e892SBill Paul 
116196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
116296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
116396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116496f2e892SBill Paul 	else
116596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116696f2e892SBill Paul 
116796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
116896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116996f2e892SBill Paul 	else
117096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
117196f2e892SBill Paul 
11720934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
117396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
117496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
117596f2e892SBill Paul 
117696f2e892SBill Paul 	/*
117796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
117896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
117996f2e892SBill Paul 	 */
118096f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
118196f2e892SBill Paul 		return;
118296f2e892SBill Paul 
11830934f18aSMaxime Henrion 	/* Now program new ones. */
11846817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
118596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
118696f2e892SBill Paul 			continue;
1187acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1188acc1bcccSMartin Blapp 			h = dc_crc_le(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1189acc1bcccSMartin Blapp 		else
119096f2e892SBill Paul 			h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
119196f2e892SBill Paul 		if (h < 32)
119296f2e892SBill Paul 			hashes[0] |= (1 << h);
119396f2e892SBill Paul 		else
119496f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
119596f2e892SBill Paul 	}
119696f2e892SBill Paul 
119796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
119896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
119996f2e892SBill Paul }
120096f2e892SBill Paul 
12012c876e15SPoul-Henning Kamp static void
12020934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
120396f2e892SBill Paul {
120496f2e892SBill Paul 	struct ifnet *ifp;
12050934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
120696f2e892SBill Paul 	int h = 0;
120796f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
120896f2e892SBill Paul 
120996f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
121096f2e892SBill Paul 
121196f2e892SBill Paul 	/* Init our MAC address */
121296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
121396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
121496f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
121596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
121696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
121796f2e892SBill Paul 	    *(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 	/*
123196f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
123296f2e892SBill Paul 	 * of broadcast frames.
123396f2e892SBill Paul 	 */
123496f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
123596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
123696f2e892SBill Paul 	else
123796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
123896f2e892SBill Paul 
123996f2e892SBill Paul 	/* first, zot all the existing hash bits */
124096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
124196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
124396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124496f2e892SBill Paul 
124596f2e892SBill Paul 	/*
124696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
124796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
124896f2e892SBill Paul 	 */
124996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
125096f2e892SBill Paul 		return;
125196f2e892SBill Paul 
125296f2e892SBill Paul 	/* now program new ones */
12536817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
125496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
125596f2e892SBill Paul 			continue;
125696f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
125796f2e892SBill Paul 		if (h < 32)
125896f2e892SBill Paul 			hashes[0] |= (1 << h);
125996f2e892SBill Paul 		else
126096f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
126196f2e892SBill Paul 	}
126296f2e892SBill Paul 
126396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
126496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
126596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
126696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
126796f2e892SBill Paul }
126896f2e892SBill Paul 
12692c876e15SPoul-Henning Kamp static void
12700934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1271feb78939SJonathan Chen {
12720934f18aSMaxime Henrion 	struct ifnet *ifp;
12730934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1274feb78939SJonathan Chen 	struct dc_desc *sframe;
1275feb78939SJonathan Chen 	u_int32_t h, *sp;
1276feb78939SJonathan Chen 	int i;
1277feb78939SJonathan Chen 
1278feb78939SJonathan Chen 	ifp = &sc->arpcom.ac_if;
1279feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1280feb78939SJonathan Chen 
1281feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1282feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1283feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1284feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
1285feb78939SJonathan Chen 	sp = (u_int32_t *)&sc->dc_cdata.dc_sbuf;
12860934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1287feb78939SJonathan Chen 
1288feb78939SJonathan Chen 	sframe->dc_data = vtophys(&sc->dc_cdata.dc_sbuf);
1289feb78939SJonathan Chen 	sframe->dc_ctl = DC_SFRAME_LEN | DC_TXCTL_SETUP | DC_TXCTL_TLINK |
1290feb78939SJonathan Chen 	    DC_FILTER_HASHPERF | DC_TXCTL_FINT;
1291feb78939SJonathan Chen 
1292feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)&sc->dc_cdata.dc_sbuf;
1293feb78939SJonathan Chen 
1294feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1295feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1296feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1297feb78939SJonathan Chen 	else
1298feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1299feb78939SJonathan Chen 
1300feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1301feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1302feb78939SJonathan Chen 	else
1303feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1304feb78939SJonathan Chen 
13056817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1306feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1307feb78939SJonathan Chen 			continue;
13081d5e5310SBill Paul 		h = dc_crc_le(sc,
13091d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1310feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1311feb78939SJonathan Chen 	}
1312feb78939SJonathan Chen 
1313feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1314868d8b62SMatthew N. Dodd 		h = dc_crc_le(sc, (caddr_t)ifp->if_broadcastaddr);
1315feb78939SJonathan Chen 		sp[h >> 4] |= 1 << (h & 0xF);
1316feb78939SJonathan Chen 	}
1317feb78939SJonathan Chen 
1318feb78939SJonathan Chen 	/* Set our MAC address */
1319feb78939SJonathan Chen 	sp[0] = ((u_int16_t *)sc->arpcom.ac_enaddr)[0];
1320feb78939SJonathan Chen 	sp[1] = ((u_int16_t *)sc->arpcom.ac_enaddr)[1];
1321feb78939SJonathan Chen 	sp[2] = ((u_int16_t *)sc->arpcom.ac_enaddr)[2];
1322feb78939SJonathan Chen 
1323feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1324feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1325feb78939SJonathan Chen 	ifp->if_flags |= IFF_RUNNING;
1326feb78939SJonathan Chen 	sframe->dc_status = DC_TXSTAT_OWN;
1327feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1328feb78939SJonathan Chen 
1329feb78939SJonathan Chen 	/*
13300934f18aSMaxime Henrion 	 * Wait some time...
1331feb78939SJonathan Chen 	 */
1332feb78939SJonathan Chen 	DELAY(1000);
1333feb78939SJonathan Chen 
1334feb78939SJonathan Chen 	ifp->if_timer = 5;
1335feb78939SJonathan Chen }
1336feb78939SJonathan Chen 
1337e3d2833aSAlfred Perlstein static void
13380934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
133996f2e892SBill Paul {
13400934f18aSMaxime Henrion 
134196f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13421af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
134396f2e892SBill Paul 		dc_setfilt_21143(sc);
134496f2e892SBill Paul 
134596f2e892SBill Paul 	if (DC_IS_ASIX(sc))
134696f2e892SBill Paul 		dc_setfilt_asix(sc);
134796f2e892SBill Paul 
134896f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
134996f2e892SBill Paul 		dc_setfilt_admtek(sc);
135096f2e892SBill Paul 
1351feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1352feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
135396f2e892SBill Paul }
135496f2e892SBill Paul 
135596f2e892SBill Paul /*
13560934f18aSMaxime Henrion  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13570934f18aSMaxime Henrion  * the netconfig register, we first have to put the transmit and/or
13580934f18aSMaxime Henrion  * receive logic in the idle state.
135996f2e892SBill Paul  */
1360e3d2833aSAlfred Perlstein static void
13610934f18aSMaxime Henrion dc_setcfg(struct dc_softc *sc, int media)
136296f2e892SBill Paul {
13630934f18aSMaxime Henrion 	int i, restart = 0, watchdogreg;
136496f2e892SBill Paul 	u_int32_t isr;
136596f2e892SBill Paul 
136696f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
136796f2e892SBill Paul 		return;
136896f2e892SBill Paul 
136996f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
137096f2e892SBill Paul 		restart = 1;
137196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
137296f2e892SBill Paul 
137396f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
137496f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1375d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1376351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1377351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
137896f2e892SBill Paul 				break;
1379d467c136SBill Paul 			DELAY(10);
138096f2e892SBill Paul 		}
138196f2e892SBill Paul 
138296f2e892SBill Paul 		if (i == DC_TIMEOUT)
138396f2e892SBill Paul 			printf("dc%d: failed to force tx and "
138496f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
138596f2e892SBill Paul 	}
138696f2e892SBill Paul 
138796f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1388042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1389042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
139096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1391bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
13920934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
13938273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
13948273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
13958273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
13964c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1397bf645417SBill Paul 			} else {
1398bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1399bf645417SBill Paul 			}
140096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
140196f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
140296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
140396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
140496f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
140588d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
140696f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
140796f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1408e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1409e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
141096f2e892SBill Paul 		} else {
141196f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
141296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
141396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
141496f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
141596f2e892SBill Paul 			}
1416318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1417318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1418318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14195c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14205c1cfac4SBill Paul 				dc_apply_fixup(sc,
14215c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14225c1cfac4SBill Paul 				    IFM_100_TX | IFM_FDX : IFM_100_TX);
142396f2e892SBill Paul 		}
142496f2e892SBill Paul 	}
142596f2e892SBill Paul 
142696f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1427042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1428042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
142996f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14300934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14314c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14328273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14338273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14348273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14358273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14364c2efe27SBill Paul 			} else {
14374c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14384c2efe27SBill Paul 			}
143996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
144096f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
144196f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
144296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
144388d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
144496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
144596f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1446e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1447e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
144896f2e892SBill Paul 		} else {
144996f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
145096f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
145196f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
145296f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
145396f2e892SBill Paul 			}
145496f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1455318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14575c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14585c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14595c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14605c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14615c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14625c1cfac4SBill Paul 				else
14635c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14645c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14655c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14665c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14675c1cfac4SBill Paul 				dc_apply_fixup(sc,
14685c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14695c1cfac4SBill Paul 				    IFM_10_T | IFM_FDX : IFM_10_T);
14705c1cfac4SBill Paul 				DELAY(20000);
14715c1cfac4SBill Paul 			}
147296f2e892SBill Paul 		}
147396f2e892SBill Paul 	}
147496f2e892SBill Paul 
1475f43d9309SBill Paul 	/*
1476f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1477f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1478f43d9309SBill Paul 	 * on the external MII port.
1479f43d9309SBill Paul 	 */
1480f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
148145521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1482f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1483f43d9309SBill Paul 			sc->dc_link = 1;
1484f43d9309SBill Paul 		} else {
1485f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1486f43d9309SBill Paul 		}
1487f43d9309SBill Paul 	}
1488f43d9309SBill Paul 
148996f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
149096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149296f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149396f2e892SBill Paul 	} else {
149496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
149596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
149696f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
149796f2e892SBill Paul 	}
149896f2e892SBill Paul 
149996f2e892SBill Paul 	if (restart)
150096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
150196f2e892SBill Paul }
150296f2e892SBill Paul 
1503e3d2833aSAlfred Perlstein static void
15040934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
150596f2e892SBill Paul {
15060934f18aSMaxime Henrion 	int i;
150796f2e892SBill Paul 
150896f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
150996f2e892SBill Paul 
151096f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
151196f2e892SBill Paul 		DELAY(10);
151296f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
151396f2e892SBill Paul 			break;
151496f2e892SBill Paul 	}
151596f2e892SBill Paul 
15161af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15171d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
151896f2e892SBill Paul 		DELAY(10000);
151996f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152096f2e892SBill Paul 		i = 0;
152196f2e892SBill Paul 	}
152296f2e892SBill Paul 
152396f2e892SBill Paul 	if (i == DC_TIMEOUT)
152496f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
152596f2e892SBill Paul 
152696f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
152796f2e892SBill Paul 	DELAY(1000);
152896f2e892SBill Paul 
152996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
153096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
153196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
153296f2e892SBill Paul 
153391cc2adbSBill Paul 	/*
153491cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
153591cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
153691cc2adbSBill Paul 	 * into a state where it will never come out of reset
153791cc2adbSBill Paul 	 * until we reset the whole chip again.
153891cc2adbSBill Paul 	 */
15395c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
154091cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15415c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15425c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15435c1cfac4SBill Paul 	}
154496f2e892SBill Paul }
154596f2e892SBill Paul 
1546e3d2833aSAlfred Perlstein static struct dc_type *
15470934f18aSMaxime Henrion dc_devtype(device_t dev)
154896f2e892SBill Paul {
154996f2e892SBill Paul 	struct dc_type *t;
155096f2e892SBill Paul 	u_int32_t rev;
155196f2e892SBill Paul 
155296f2e892SBill Paul 	t = dc_devs;
155396f2e892SBill Paul 
155496f2e892SBill Paul 	while (t->dc_name != NULL) {
155596f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
155696f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
155796f2e892SBill Paul 			/* Check the PCI revision */
155896f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
155996f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
156096f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
156196f2e892SBill Paul 				t++;
156296f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
156396f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
156496f2e892SBill Paul 				t++;
156596f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
156679d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
156779d11e09SBill Paul 				t++;
156879d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
156996f2e892SBill Paul 			    rev >= DC_REVISION_98725)
157096f2e892SBill Paul 				t++;
157196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
157296f2e892SBill Paul 			    rev >= DC_REVISION_88141)
157396f2e892SBill Paul 				t++;
157496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
157596f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
157696f2e892SBill Paul 				t++;
157788d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
157888d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
157988d739dcSBill Paul 				t++;
158096f2e892SBill Paul 			return (t);
158196f2e892SBill Paul 		}
158296f2e892SBill Paul 		t++;
158396f2e892SBill Paul 	}
158496f2e892SBill Paul 
158596f2e892SBill Paul 	return (NULL);
158696f2e892SBill Paul }
158796f2e892SBill Paul 
158896f2e892SBill Paul /*
158996f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
159096f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
159196f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
159296f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
159396f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
159496f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
159596f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
159696f2e892SBill Paul  */
1597e3d2833aSAlfred Perlstein static int
15980934f18aSMaxime Henrion dc_probe(device_t dev)
159996f2e892SBill Paul {
160096f2e892SBill Paul 	struct dc_type *t;
160196f2e892SBill Paul 
160296f2e892SBill Paul 	t = dc_devtype(dev);
160396f2e892SBill Paul 
160496f2e892SBill Paul 	if (t != NULL) {
160596f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
160696f2e892SBill Paul 		return (0);
160796f2e892SBill Paul 	}
160896f2e892SBill Paul 
160996f2e892SBill Paul 	return (ENXIO);
161096f2e892SBill Paul }
161196f2e892SBill Paul 
1612e3d2833aSAlfred Perlstein static void
16130934f18aSMaxime Henrion dc_acpi(device_t dev)
161496f2e892SBill Paul {
161596f2e892SBill Paul 	int unit;
16160934f18aSMaxime Henrion 	u_int32_t iobase, membase, irq;
161796f2e892SBill Paul 
161896f2e892SBill Paul 	unit = device_get_unit(dev);
161996f2e892SBill Paul 
162014a00c6cSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
162196f2e892SBill Paul 		/* Save important PCI config data. */
162296f2e892SBill Paul 		iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
162396f2e892SBill Paul 		membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
162496f2e892SBill Paul 		irq = pci_read_config(dev, DC_PCI_CFIT, 4);
162596f2e892SBill Paul 
162696f2e892SBill Paul 		/* Reset the power state. */
162796f2e892SBill Paul 		printf("dc%d: chip is in D%d power mode "
162814a00c6cSBill Paul 		    "-- setting to D0\n", unit,
162914a00c6cSBill Paul 		    pci_get_powerstate(dev));
163014a00c6cSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
163196f2e892SBill Paul 
163296f2e892SBill Paul 		/* Restore PCI config data. */
163396f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
163496f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
163596f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFIT, irq, 4);
163696f2e892SBill Paul 	}
163796f2e892SBill Paul }
163896f2e892SBill Paul 
1639e3d2833aSAlfred Perlstein static void
16400934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16415c1cfac4SBill Paul {
16425c1cfac4SBill Paul 	struct dc_mediainfo *m;
16435c1cfac4SBill Paul 	u_int8_t *p;
16445c1cfac4SBill Paul 	int i;
16455d801891SBill Paul 	u_int32_t reg;
16465c1cfac4SBill Paul 
16475c1cfac4SBill Paul 	m = sc->dc_mi;
16485c1cfac4SBill Paul 
16495c1cfac4SBill Paul 	while (m != NULL) {
16505c1cfac4SBill Paul 		if (m->dc_media == media)
16515c1cfac4SBill Paul 			break;
16525c1cfac4SBill Paul 		m = m->dc_next;
16535c1cfac4SBill Paul 	}
16545c1cfac4SBill Paul 
16555c1cfac4SBill Paul 	if (m == NULL)
16565c1cfac4SBill Paul 		return;
16575c1cfac4SBill Paul 
16585c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16595c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16605c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16615c1cfac4SBill Paul 	}
16625c1cfac4SBill Paul 
16635c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16645c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16655c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16665c1cfac4SBill Paul 	}
16675c1cfac4SBill Paul }
16685c1cfac4SBill Paul 
1669e3d2833aSAlfred Perlstein static void
16700934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16715c1cfac4SBill Paul {
16725c1cfac4SBill Paul 	struct dc_mediainfo *m;
16735c1cfac4SBill Paul 
16740934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
16755c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
16765c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
16775c1cfac4SBill Paul 
16785c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
16795c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
16805c1cfac4SBill Paul 
16815c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
16825c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
16835c1cfac4SBill Paul 
16845c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
16855c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
16865c1cfac4SBill Paul 
16875c1cfac4SBill Paul 	m->dc_gp_len = 2;
16885c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
16895c1cfac4SBill Paul 
16905c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16915c1cfac4SBill Paul 	sc->dc_mi = m;
16925c1cfac4SBill Paul 
16935c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
16945c1cfac4SBill Paul }
16955c1cfac4SBill Paul 
1696e3d2833aSAlfred Perlstein static void
16970934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16985c1cfac4SBill Paul {
16995c1cfac4SBill Paul 	struct dc_mediainfo *m;
17005c1cfac4SBill Paul 
17010934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17025c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
17035c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
17045c1cfac4SBill Paul 
17055c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
17065c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
17075c1cfac4SBill Paul 
17085c1cfac4SBill Paul 	m->dc_gp_len = 2;
17095c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
17105c1cfac4SBill Paul 
17115c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17125c1cfac4SBill Paul 	sc->dc_mi = m;
17135c1cfac4SBill Paul 
17145c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
17155c1cfac4SBill Paul }
17165c1cfac4SBill Paul 
1717e3d2833aSAlfred Perlstein static void
17180934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17195c1cfac4SBill Paul {
17205c1cfac4SBill Paul 	struct dc_mediainfo *m;
17210934f18aSMaxime Henrion 	u_int8_t *p;
17225c1cfac4SBill Paul 
17230934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17245c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17255c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17265c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17275c1cfac4SBill Paul 
17285c1cfac4SBill Paul 	p = (u_int8_t *)l;
17295c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17305c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17315c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17325c1cfac4SBill Paul 	m->dc_reset_len = *p;
17335c1cfac4SBill Paul 	p++;
17345c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17355c1cfac4SBill Paul 
17365c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17375c1cfac4SBill Paul 	sc->dc_mi = m;
17385c1cfac4SBill Paul }
17395c1cfac4SBill Paul 
17402c876e15SPoul-Henning Kamp static void
17410934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17423097aa70SWarner Losh {
17433097aa70SWarner Losh 	int size;
17443097aa70SWarner Losh 
17453097aa70SWarner Losh 	size = 2 << bits;
17463097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
17473097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
17483097aa70SWarner Losh }
17493097aa70SWarner Losh 
1750e3d2833aSAlfred Perlstein static void
17510934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17525c1cfac4SBill Paul {
17535c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17545c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
17550934f18aSMaxime Henrion 	int have_mii, i, loff;
17565c1cfac4SBill Paul 	char *ptr;
17575c1cfac4SBill Paul 
1758f956e0b3SMartin Blapp 	have_mii = 0;
17595c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17605c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17615c1cfac4SBill Paul 
17625c1cfac4SBill Paul 	ptr = (char *)lhdr;
17635c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1764f956e0b3SMartin Blapp 	/*
1765f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1766f956e0b3SMartin Blapp 	 */
1767f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1768f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1769f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1770f956e0b3SMartin Blapp 		    have_mii++;
1771f956e0b3SMartin Blapp 
1772f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1773f956e0b3SMartin Blapp 		ptr++;
1774f956e0b3SMartin Blapp 	}
1775f956e0b3SMartin Blapp 
1776f956e0b3SMartin Blapp 	/*
1777f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1778f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1779f956e0b3SMartin Blapp 	 */
1780f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1781f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17825c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17835c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17845c1cfac4SBill Paul 		switch (hdr->dc_type) {
17855c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17865c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17875c1cfac4SBill Paul 			break;
17885c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1789f956e0b3SMartin Blapp 			if (! have_mii)
1790f956e0b3SMartin Blapp 				dc_decode_leaf_sia(sc,
1791f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17925c1cfac4SBill Paul 			break;
17935c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1794f956e0b3SMartin Blapp 			if (! have_mii)
1795f956e0b3SMartin Blapp 				dc_decode_leaf_sym(sc,
1796f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17975c1cfac4SBill Paul 			break;
17985c1cfac4SBill Paul 		default:
17995c1cfac4SBill Paul 			/* Don't care. Yet. */
18005c1cfac4SBill Paul 			break;
18015c1cfac4SBill Paul 		}
18025c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18035c1cfac4SBill Paul 		ptr++;
18045c1cfac4SBill Paul 	}
18055c1cfac4SBill Paul }
18065c1cfac4SBill Paul 
180796f2e892SBill Paul /*
180896f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
180996f2e892SBill Paul  * setup and ethernet/BPF attach.
181096f2e892SBill Paul  */
1811e3d2833aSAlfred Perlstein static int
18120934f18aSMaxime Henrion dc_attach(device_t dev)
181396f2e892SBill Paul {
1814d1ce9105SBill Paul 	int tmp = 0;
181596f2e892SBill Paul 	u_char eaddr[ETHER_ADDR_LEN];
181696f2e892SBill Paul 	u_int32_t command;
181796f2e892SBill Paul 	struct dc_softc *sc;
181896f2e892SBill Paul 	struct ifnet *ifp;
181996f2e892SBill Paul 	u_int32_t revision;
182096f2e892SBill Paul 	int unit, error = 0, rid, mac_offset;
1821e7b01d07SWarner Losh 	u_int8_t *mac;
182296f2e892SBill Paul 
182396f2e892SBill Paul 	sc = device_get_softc(dev);
182496f2e892SBill Paul 	unit = device_get_unit(dev);
182596f2e892SBill Paul 
18266008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
18276008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
1828031fc810SBill Paul 
182996f2e892SBill Paul 	/*
183096f2e892SBill Paul 	 * Handle power management nonsense.
183196f2e892SBill Paul 	 */
183296f2e892SBill Paul 	dc_acpi(dev);
183396f2e892SBill Paul 
183496f2e892SBill Paul 	/*
183596f2e892SBill Paul 	 * Map control/status registers.
183696f2e892SBill Paul 	 */
183707f65363SBill Paul 	pci_enable_busmaster(dev);
183896f2e892SBill Paul 
183996f2e892SBill Paul 	rid = DC_RID;
184096f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
184196f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
184296f2e892SBill Paul 
184396f2e892SBill Paul 	if (sc->dc_res == NULL) {
184496f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
184596f2e892SBill Paul 		error = ENXIO;
1846608654d4SNate Lawson 		goto fail;
184796f2e892SBill Paul 	}
184896f2e892SBill Paul 
184996f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
185096f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
185196f2e892SBill Paul 
18520934f18aSMaxime Henrion 	/* Allocate interrupt. */
185354f1f1d1SNate Lawson 	rid = 0;
185454f1f1d1SNate Lawson 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
185554f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
185654f1f1d1SNate Lawson 
185754f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
185854f1f1d1SNate Lawson 		printf("dc%d: couldn't map interrupt\n", unit);
185954f1f1d1SNate Lawson 		error = ENXIO;
186054f1f1d1SNate Lawson 		goto fail;
186154f1f1d1SNate Lawson 	}
186254f1f1d1SNate Lawson 
186396f2e892SBill Paul 	/* Need this info to decide on a chip type. */
186496f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
186596f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
186696f2e892SBill Paul 
18676d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
1868eecb3844SMartin Blapp 	if (sc->dc_info->dc_did != DC_DEVICEID_82C168 &&
1869eecb3844SMartin Blapp 	   sc->dc_info->dc_did != DC_DEVICEID_X3201)
1870eecb3844SMartin Blapp 		dc_eeprom_width(sc);
1871eecb3844SMartin Blapp 
187296f2e892SBill Paul 	switch (sc->dc_info->dc_did) {
187396f2e892SBill Paul 	case DC_DEVICEID_21143:
187496f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
187596f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1876042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
18775c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
18783097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
187996f2e892SBill Paul 		break;
188038deb45fSTom Rhodes 	case DC_DEVICEID_DM9009:
188196f2e892SBill Paul 	case DC_DEVICEID_DM9100:
188296f2e892SBill Paul 	case DC_DEVICEID_DM9102:
188396f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1884318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1885318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
188696f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
18870a46b1dcSBill Paul 		/* Increase the latency timer value. */
18880a46b1dcSBill Paul 		command = pci_read_config(dev, DC_PCI_CFLT, 4);
18890a46b1dcSBill Paul 		command &= 0xFFFF00FF;
18900a46b1dcSBill Paul 		command |= 0x00008000;
18910a46b1dcSBill Paul 		pci_write_config(dev, DC_PCI_CFLT, command, 4);
189296f2e892SBill Paul 		break;
189396f2e892SBill Paul 	case DC_DEVICEID_AL981:
189496f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
189596f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
189696f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
189796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
18983097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
189996f2e892SBill Paul 		break;
190096f2e892SBill Paul 	case DC_DEVICEID_AN985:
190141fced74SPeter Wemm 	case DC_DEVICEID_FE2500:
1902fa167b8eSBill Paul 	case DC_DEVICEID_EN2242:
1903948c244dSWarner Losh 	case DC_DEVICEID_HAWKING_PN672TX:
19047eac366bSMartin Blapp 	case DC_DEVICEID_3CSOHOB:
190596f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
1906acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
190796f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
190896f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
190996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19103097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
191196f2e892SBill Paul 		break;
191296f2e892SBill Paul 	case DC_DEVICEID_98713:
191396f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
191496f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
191596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
191696f2e892SBill Paul 		}
1917318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
191896f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1919318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1920318b02fdSBill Paul 		}
1921318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
192296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
192396f2e892SBill Paul 		break;
192496f2e892SBill Paul 	case DC_DEVICEID_987x5:
19259ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
192679d11e09SBill Paul 		/*
192779d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
192879d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
192979d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
193079d11e09SBill Paul 		 * get the right number of bits out of the
193179d11e09SBill Paul 		 * CRC routine.
193279d11e09SBill Paul 		 */
193379d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
193479d11e09SBill Paul 		    revision < DC_REVISION_98725)
193579d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
193696f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
193796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1938318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
193996f2e892SBill Paul 		break;
1940ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1941ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1942ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1943ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1944ead7cde9SBill Paul 		break;
194596f2e892SBill Paul 	case DC_DEVICEID_82C115:
194696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
194779d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
1948318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
194996f2e892SBill Paul 		break;
195096f2e892SBill Paul 	case DC_DEVICEID_82C168:
195196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
195291cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
195396f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
195496f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
195596f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
195696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
195796f2e892SBill Paul 		break;
195896f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
195996f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
196096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
196196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
196296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
196396f2e892SBill Paul 		break;
1964feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
1965feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
19662dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
19672dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
1968feb78939SJonathan Chen 		/*
1969feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1970feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
19712dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
1972feb78939SJonathan Chen 		 */
19733097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
1974feb78939SJonathan Chen 		break;
19751af8bec7SBill Paul 	case DC_DEVICEID_RS7112:
19761af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
19771af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
19781af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19791af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19803097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
19811af8bec7SBill Paul 		break;
198296f2e892SBill Paul 	default:
198396f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
198496f2e892SBill Paul 		    sc->dc_info->dc_did);
198596f2e892SBill Paul 		break;
198696f2e892SBill Paul 	}
198796f2e892SBill Paul 
198896f2e892SBill Paul 	/* Save the cache line size. */
198988d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
199088d739dcSBill Paul 		sc->dc_cachesize = 0;
199188d739dcSBill Paul 	else
199288d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
199388d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
199496f2e892SBill Paul 
199596f2e892SBill Paul 	/* Reset the adapter. */
199696f2e892SBill Paul 	dc_reset(sc);
199796f2e892SBill Paul 
199896f2e892SBill Paul 	/* Take 21143 out of snooze mode */
1999feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
200096f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
200196f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
200296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
200396f2e892SBill Paul 	}
200496f2e892SBill Paul 
200596f2e892SBill Paul 	/*
200696f2e892SBill Paul 	 * Try to learn something about the supported media.
200796f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
200896f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
200996f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
201096f2e892SBill Paul 	 * Intel 21143.
201196f2e892SBill Paul 	 */
20125c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
20135c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
20145c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
201596f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
201696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
201796f2e892SBill Paul 		else
201896f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
201996f2e892SBill Paul 	} else if (!sc->dc_pmode)
202096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
202196f2e892SBill Paul 
202296f2e892SBill Paul 	/*
202396f2e892SBill Paul 	 * Get station address from the EEPROM.
202496f2e892SBill Paul 	 */
202596f2e892SBill Paul 	switch(sc->dc_type) {
202696f2e892SBill Paul 	case DC_TYPE_98713:
202796f2e892SBill Paul 	case DC_TYPE_98713A:
202896f2e892SBill Paul 	case DC_TYPE_987x5:
202996f2e892SBill Paul 	case DC_TYPE_PNICII:
203096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
203196f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
203296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
203396f2e892SBill Paul 		break;
203496f2e892SBill Paul 	case DC_TYPE_PNIC:
203596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
203696f2e892SBill Paul 		break;
203796f2e892SBill Paul 	case DC_TYPE_DM9102:
203896f2e892SBill Paul 	case DC_TYPE_21143:
203996f2e892SBill Paul 	case DC_TYPE_ASIX:
204096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
204196f2e892SBill Paul 		break;
204296f2e892SBill Paul 	case DC_TYPE_AL981:
204396f2e892SBill Paul 	case DC_TYPE_AN985:
20440934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_AL_EE_NODEADDR, &eaddr,
20453097aa70SWarner Losh 		    ETHER_ADDR_LEN);
204696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
204796f2e892SBill Paul 		break;
20481af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20490934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
20500934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
20511af8bec7SBill Paul 		break;
2052feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
20530934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2054e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2055e7b01d07SWarner Losh 		if (!mac) {
2056e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2057608654d4SNate Lawson 			error = ENXIO;
2058e7b01d07SWarner Losh 			goto fail;
2059e7b01d07SWarner Losh 		}
2060e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2061feb78939SJonathan Chen 		break;
206296f2e892SBill Paul 	default:
206396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
206496f2e892SBill Paul 		break;
206596f2e892SBill Paul 	}
206696f2e892SBill Paul 
206796f2e892SBill Paul 	/*
206896f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
206996f2e892SBill Paul 	 */
207096f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
207196f2e892SBill Paul 
207296f2e892SBill Paul 	sc->dc_unit = unit;
20730934f18aSMaxime Henrion 	bcopy(eaddr, &sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
207496f2e892SBill Paul 
207596f2e892SBill Paul 	sc->dc_ldata = contigmalloc(sizeof(struct dc_list_data), M_DEVBUF,
207696f2e892SBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
207796f2e892SBill Paul 
207896f2e892SBill Paul 	if (sc->dc_ldata == NULL) {
207996f2e892SBill Paul 		printf("dc%d: no memory for list buffers!\n", unit);
208096f2e892SBill Paul 		error = ENXIO;
208196f2e892SBill Paul 		goto fail;
208296f2e892SBill Paul 	}
208396f2e892SBill Paul 
208496f2e892SBill Paul 	bzero(sc->dc_ldata, sizeof(struct dc_list_data));
208596f2e892SBill Paul 
208696f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
208796f2e892SBill Paul 	ifp->if_softc = sc;
208896f2e892SBill Paul 	ifp->if_unit = unit;
208996f2e892SBill Paul 	ifp->if_name = "dc";
2090feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
209196f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
209296f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
209396f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
209496f2e892SBill Paul 	ifp->if_start = dc_start;
209596f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
209696f2e892SBill Paul 	ifp->if_init = dc_init;
209796f2e892SBill Paul 	ifp->if_baudrate = 10000000;
209896f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
209996f2e892SBill Paul 
210096f2e892SBill Paul 	/*
21015c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
21025c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
21035c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
21045c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
21055c1cfac4SBill Paul 	 * driver instead.
210696f2e892SBill Paul 	 */
21075c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
21085c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
21095c1cfac4SBill Paul 		tmp = sc->dc_pmode;
21105c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
21115c1cfac4SBill Paul 	}
21125c1cfac4SBill Paul 
211396f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
211496f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
211596f2e892SBill Paul 
211696f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
21175c1cfac4SBill Paul 		sc->dc_pmode = tmp;
21185c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
211996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2120042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
212196f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
212296f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
212378999dd1SBill Paul 		/*
212478999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
212578999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
212678999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
212778999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
212878999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
212978999dd1SBill Paul 		 */
213078999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
213178999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
213296f2e892SBill Paul 		error = 0;
213396f2e892SBill Paul 	}
213496f2e892SBill Paul 
213596f2e892SBill Paul 	if (error) {
213696f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
213796f2e892SBill Paul 		goto fail;
213896f2e892SBill Paul 	}
213996f2e892SBill Paul 
2140feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
2141feb78939SJonathan Chen 		/*
2142feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
2143feb78939SJonathan Chen 		 * can talk to the MII.
2144feb78939SJonathan Chen 		 */
2145feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2146feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2147feb78939SJonathan Chen 		DELAY(10);
2148feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2149feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2150feb78939SJonathan Chen 		DELAY(10);
2151feb78939SJonathan Chen 	}
2152feb78939SJonathan Chen 
2153028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2154028a8491SMartin Blapp 		/*
2155028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2156028a8491SMartin Blapp 		 */
2157028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2158028a8491SMartin Blapp 	}
2159028a8491SMartin Blapp 
216096f2e892SBill Paul 	/*
2161db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2162db40c1aeSDoug Ambrisko 	 */
2163db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
21649ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
2165db40c1aeSDoug Ambrisko 
2166b50c6312SJonathan Lemon 	callout_init(&sc->dc_stat_ch, IS_MPSAFE);
216796f2e892SBill Paul 
21685c1cfac4SBill Paul #ifdef SRM_MEDIA
2169510a809eSMike Smith 	sc->dc_srm_media = 0;
2170510a809eSMike Smith 
2171510a809eSMike Smith 	/* Remember the SRM console media setting */
2172510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2173510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2174510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
2175510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2176510a809eSMike Smith 		case 3:
2177510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2178510a809eSMike Smith 			break;
2179510a809eSMike Smith 		case 4:
2180510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2181510a809eSMike Smith 			break;
2182510a809eSMike Smith 		case 5:
2183510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2184510a809eSMike Smith 			break;
2185510a809eSMike Smith 		case 6:
2186510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2187510a809eSMike Smith 			break;
2188510a809eSMike Smith 		}
2189510a809eSMike Smith 		if (sc->dc_srm_media)
2190510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2191510a809eSMike Smith 	}
2192510a809eSMike Smith #endif
2193510a809eSMike Smith 
2194608654d4SNate Lawson 	/*
2195608654d4SNate Lawson 	 * Call MI attach routine.
2196608654d4SNate Lawson 	 */
2197608654d4SNate Lawson 	ether_ifattach(ifp, eaddr);
2198608654d4SNate Lawson 
219954f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2200608654d4SNate Lawson 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET |
2201608654d4SNate Lawson 	    (IS_MPSAFE ? INTR_MPSAFE : 0),
2202608654d4SNate Lawson 	    dc_intr, sc, &sc->dc_intrhand);
2203608654d4SNate Lawson 
2204608654d4SNate Lawson 	if (error) {
2205608654d4SNate Lawson 		printf("dc%d: couldn't set up irq\n", unit);
2206693f4477SNate Lawson 		ether_ifdetach(ifp);
220754f1f1d1SNate Lawson 		goto fail;
2208608654d4SNate Lawson 	}
2209510a809eSMike Smith 
221096f2e892SBill Paul fail:
221154f1f1d1SNate Lawson 	if (error)
221254f1f1d1SNate Lawson 		dc_detach(dev);
221396f2e892SBill Paul 	return (error);
221496f2e892SBill Paul }
221596f2e892SBill Paul 
2216693f4477SNate Lawson /*
2217693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2218693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2219693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2220693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2221693f4477SNate Lawson  * allocated.
2222693f4477SNate Lawson  */
2223e3d2833aSAlfred Perlstein static int
22240934f18aSMaxime Henrion dc_detach(device_t dev)
222596f2e892SBill Paul {
222696f2e892SBill Paul 	struct dc_softc *sc;
222796f2e892SBill Paul 	struct ifnet *ifp;
22285c1cfac4SBill Paul 	struct dc_mediainfo *m;
222996f2e892SBill Paul 
223096f2e892SBill Paul 	sc = device_get_softc(dev);
223159f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2232d1ce9105SBill Paul 	DC_LOCK(sc);
2233d1ce9105SBill Paul 
223496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
223596f2e892SBill Paul 
2236693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2237214073e5SWarner Losh 	if (device_is_attached(dev)) {
223896f2e892SBill Paul 		dc_stop(sc);
22399ef8b520SSam Leffler 		ether_ifdetach(ifp);
2240693f4477SNate Lawson 	}
2241693f4477SNate Lawson 	if (sc->dc_miibus)
224296f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
224354f1f1d1SNate Lawson 	bus_generic_detach(dev);
224496f2e892SBill Paul 
224554f1f1d1SNate Lawson 	if (sc->dc_intrhand)
224696f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
224754f1f1d1SNate Lawson 	if (sc->dc_irq)
224896f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
224954f1f1d1SNate Lawson 	if (sc->dc_res)
225096f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
225196f2e892SBill Paul 
225254f1f1d1SNate Lawson 	if (sc->dc_ldata)
225396f2e892SBill Paul 		contigfree(sc->dc_ldata, sizeof(struct dc_list_data), M_DEVBUF);
225496f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
225596f2e892SBill Paul 
22565c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
22575c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
22585c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
22595c1cfac4SBill Paul 		sc->dc_mi = m;
22605c1cfac4SBill Paul 	}
22617efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
22625c1cfac4SBill Paul 
2263d1ce9105SBill Paul 	DC_UNLOCK(sc);
2264d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
226596f2e892SBill Paul 
226696f2e892SBill Paul 	return (0);
226796f2e892SBill Paul }
226896f2e892SBill Paul 
226996f2e892SBill Paul /*
227096f2e892SBill Paul  * Initialize the transmit descriptors.
227196f2e892SBill Paul  */
2272e3d2833aSAlfred Perlstein static int
22730934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
227496f2e892SBill Paul {
227596f2e892SBill Paul 	struct dc_chain_data *cd;
227696f2e892SBill Paul 	struct dc_list_data *ld;
227701faf54bSLuigi Rizzo 	int i, nexti;
227896f2e892SBill Paul 
227996f2e892SBill Paul 	cd = &sc->dc_cdata;
228096f2e892SBill Paul 	ld = sc->dc_ldata;
228196f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
228201faf54bSLuigi Rizzo 		nexti = (i == (DC_TX_LIST_CNT - 1)) ? 0 : i+1;
228301faf54bSLuigi Rizzo 		ld->dc_tx_list[i].dc_next = vtophys(&ld->dc_tx_list[nexti]);
228496f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
228596f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
228696f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
228796f2e892SBill Paul 	}
228896f2e892SBill Paul 
228996f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
229096f2e892SBill Paul 
229196f2e892SBill Paul 	return (0);
229296f2e892SBill Paul }
229396f2e892SBill Paul 
229496f2e892SBill Paul 
229596f2e892SBill Paul /*
229696f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
229796f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
229896f2e892SBill Paul  * points back to the first.
229996f2e892SBill Paul  */
2300e3d2833aSAlfred Perlstein static int
23010934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
230296f2e892SBill Paul {
230396f2e892SBill Paul 	struct dc_chain_data *cd;
230496f2e892SBill Paul 	struct dc_list_data *ld;
230501faf54bSLuigi Rizzo 	int i, nexti;
230696f2e892SBill Paul 
230796f2e892SBill Paul 	cd = &sc->dc_cdata;
230896f2e892SBill Paul 	ld = sc->dc_ldata;
230996f2e892SBill Paul 
231096f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
231196f2e892SBill Paul 		if (dc_newbuf(sc, i, NULL) == ENOBUFS)
231296f2e892SBill Paul 			return (ENOBUFS);
231301faf54bSLuigi Rizzo 		nexti = (i == (DC_RX_LIST_CNT - 1)) ? 0 : i+1;
231401faf54bSLuigi Rizzo 		ld->dc_rx_list[i].dc_next = vtophys(&ld->dc_rx_list[nexti]);
231596f2e892SBill Paul 	}
231696f2e892SBill Paul 
231796f2e892SBill Paul 	cd->dc_rx_prod = 0;
231896f2e892SBill Paul 
231996f2e892SBill Paul 	return (0);
232096f2e892SBill Paul }
232196f2e892SBill Paul 
232296f2e892SBill Paul /*
232396f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
232496f2e892SBill Paul  */
2325e3d2833aSAlfred Perlstein static int
23260934f18aSMaxime Henrion dc_newbuf(struct dc_softc *sc, int i, struct mbuf *m)
232796f2e892SBill Paul {
232896f2e892SBill Paul 	struct mbuf *m_new = NULL;
232996f2e892SBill Paul 	struct dc_desc *c;
233096f2e892SBill Paul 
233196f2e892SBill Paul 	c = &sc->dc_ldata->dc_rx_list[i];
233296f2e892SBill Paul 
233396f2e892SBill Paul 	if (m == NULL) {
2334a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
233540129585SLuigi Rizzo 		if (m_new == NULL)
233696f2e892SBill Paul 			return (ENOBUFS);
233796f2e892SBill Paul 
2338a163d034SWarner Losh 		MCLGET(m_new, M_DONTWAIT);
233996f2e892SBill Paul 		if (!(m_new->m_flags & M_EXT)) {
234096f2e892SBill Paul 			m_freem(m_new);
234196f2e892SBill Paul 			return (ENOBUFS);
234296f2e892SBill Paul 		}
234396f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
234496f2e892SBill Paul 	} else {
234596f2e892SBill Paul 		m_new = m;
234696f2e892SBill Paul 		m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
234796f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
234896f2e892SBill Paul 	}
234996f2e892SBill Paul 
235096f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
235196f2e892SBill Paul 
235296f2e892SBill Paul 	/*
235396f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
235496f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
235596f2e892SBill Paul 	 * 82c169 chips.
235696f2e892SBill Paul 	 */
235796f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
23580934f18aSMaxime Henrion 		bzero(mtod(m_new, char *), m_new->m_len);
235996f2e892SBill Paul 
236096f2e892SBill Paul 	sc->dc_cdata.dc_rx_chain[i] = m_new;
236196f2e892SBill Paul 	c->dc_data = vtophys(mtod(m_new, caddr_t));
236296f2e892SBill Paul 	c->dc_ctl = DC_RXCTL_RLINK | DC_RXLEN;
236396f2e892SBill Paul 	c->dc_status = DC_RXSTAT_OWN;
236496f2e892SBill Paul 
236596f2e892SBill Paul 	return (0);
236696f2e892SBill Paul }
236796f2e892SBill Paul 
236896f2e892SBill Paul /*
236996f2e892SBill Paul  * Grrrrr.
237096f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
237196f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
237296f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
237396f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
237496f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
237596f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
237696f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
237796f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
237896f2e892SBill Paul  *
237996f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
238096f2e892SBill Paul  * Here's what we know:
238196f2e892SBill Paul  *
238296f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
238396f2e892SBill Paul  *   descriptors uploaded.
238496f2e892SBill Paul  *
238596f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
238696f2e892SBill Paul  *   total data upload.
238796f2e892SBill Paul  *
238896f2e892SBill Paul  * - We know the size of the desired received frame because it will be
238996f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
239096f2e892SBill Paul  *
239196f2e892SBill Paul  * Here's what we do:
239296f2e892SBill Paul  *
239396f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
239496f2e892SBill Paul  *   This means that we know that the buffer contents should be all
239596f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
239696f2e892SBill Paul  *
239796f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
239896f2e892SBill Paul  *   ethernet CRC at the end.
239996f2e892SBill Paul  *
240096f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
240196f2e892SBill Paul  *
240296f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
240396f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
240496f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
240596f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
240696f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
240796f2e892SBill Paul  *   we won't be fooled.
240896f2e892SBill Paul  *
240996f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
241096f2e892SBill Paul  *   that value from the current pointer location. This brings us
241196f2e892SBill Paul  *   to the start of the actual received packet.
241296f2e892SBill Paul  *
241396f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
241496f2e892SBill Paul  *   frame length.
241596f2e892SBill Paul  *
241696f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
241796f2e892SBill Paul  * the time.
241896f2e892SBill Paul  */
241996f2e892SBill Paul 
242096f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2421e3d2833aSAlfred Perlstein static void
24220934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
242396f2e892SBill Paul {
242496f2e892SBill Paul 	struct dc_desc *cur_rx;
242596f2e892SBill Paul 	struct dc_desc *c = NULL;
242696f2e892SBill Paul 	struct mbuf *m = NULL;
242796f2e892SBill Paul 	unsigned char *ptr;
242896f2e892SBill Paul 	int i, total_len;
242996f2e892SBill Paul 	u_int32_t rxstat = 0;
243096f2e892SBill Paul 
243196f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
243296f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
243396f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
24341edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
243596f2e892SBill Paul 
243696f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
243796f2e892SBill Paul 	while (1) {
243896f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
243996f2e892SBill Paul 		rxstat = c->dc_status;
244096f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
244196f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
244296f2e892SBill Paul 		ptr += DC_RXLEN;
244396f2e892SBill Paul 		/* If this is the last buffer, break out. */
244496f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
244596f2e892SBill Paul 			break;
244696f2e892SBill Paul 		dc_newbuf(sc, i, m);
244796f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
244896f2e892SBill Paul 	}
244996f2e892SBill Paul 
245096f2e892SBill Paul 	/* Find the length of the actual receive frame. */
245196f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
245296f2e892SBill Paul 
245396f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
245496f2e892SBill Paul 	while (*ptr == 0x00)
245596f2e892SBill Paul 		ptr--;
245696f2e892SBill Paul 
245796f2e892SBill Paul 	/* Round off. */
245896f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
245996f2e892SBill Paul 		ptr -= 1;
246096f2e892SBill Paul 
246196f2e892SBill Paul 	/* Now find the start of the frame. */
246296f2e892SBill Paul 	ptr -= total_len;
246396f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
246496f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
246596f2e892SBill Paul 
246696f2e892SBill Paul 	/*
246796f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
246896f2e892SBill Paul 	 * the status word to make it look like a successful
246996f2e892SBill Paul 	 * frame reception.
247096f2e892SBill Paul 	 */
247196f2e892SBill Paul 	dc_newbuf(sc, i, m);
247296f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
247396f2e892SBill Paul 	cur_rx->dc_status = rxstat | DC_RXSTAT_FIRSTFRAG;
247496f2e892SBill Paul }
247596f2e892SBill Paul 
247696f2e892SBill Paul /*
247773bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
247873bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
247973bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
248073bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
248173bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
248273bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
248373bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
248473bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
248573bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
248673bf949cSBill Paul  */
2487e3d2833aSAlfred Perlstein static int
24880934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
248973bf949cSBill Paul {
249073bf949cSBill Paul 	struct dc_desc *cur_rx;
24910934f18aSMaxime Henrion 	int i, pos;
249273bf949cSBill Paul 
249373bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
249473bf949cSBill Paul 
249573bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
249673bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
249773bf949cSBill Paul 		if (!(cur_rx->dc_status & DC_RXSTAT_OWN))
249873bf949cSBill Paul 			break;
249973bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
250073bf949cSBill Paul 	}
250173bf949cSBill Paul 
250273bf949cSBill Paul 	/* If the ring really is empty, then just return. */
250373bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
250473bf949cSBill Paul 		return (0);
250573bf949cSBill Paul 
250673bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
250773bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
250873bf949cSBill Paul 
250973bf949cSBill Paul 	return (EAGAIN);
251073bf949cSBill Paul }
251173bf949cSBill Paul 
251273bf949cSBill Paul /*
251396f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
251496f2e892SBill Paul  * the higher level protocols.
251596f2e892SBill Paul  */
2516e3d2833aSAlfred Perlstein static void
25170934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
251896f2e892SBill Paul {
251996f2e892SBill Paul 	struct mbuf *m;
252096f2e892SBill Paul 	struct ifnet *ifp;
252196f2e892SBill Paul 	struct dc_desc *cur_rx;
252296f2e892SBill Paul 	int i, total_len = 0;
252396f2e892SBill Paul 	u_int32_t rxstat;
252496f2e892SBill Paul 
252596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
252696f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
252796f2e892SBill Paul 
252896f2e892SBill Paul 	while (!(sc->dc_ldata->dc_rx_list[i].dc_status & DC_RXSTAT_OWN)) {
2529e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
253062f76486SMaxim Sobolev 		if (ifp->if_flags & IFF_POLLING) {
2531e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2532e4fc250cSLuigi Rizzo 				break;
2533e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2534e4fc250cSLuigi Rizzo 		}
25350934f18aSMaxime Henrion #endif
253696f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
253796f2e892SBill Paul 		rxstat = cur_rx->dc_status;
253896f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
253996f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
254096f2e892SBill Paul 
254196f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
254296f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
254396f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
254496f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
254596f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
254696f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
254796f2e892SBill Paul 					continue;
254896f2e892SBill Paul 				}
254996f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
255096f2e892SBill Paul 				rxstat = cur_rx->dc_status;
255196f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
255296f2e892SBill Paul 			}
255396f2e892SBill Paul 		}
255496f2e892SBill Paul 
255596f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = NULL;
255696f2e892SBill Paul 
255796f2e892SBill Paul 		/*
255896f2e892SBill Paul 		 * If an error occurs, update stats, clear the
255996f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
256096f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2561db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
25620934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
256396f2e892SBill Paul 		 */
2564db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2565db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2566db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2567db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2568db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
256996f2e892SBill Paul 				ifp->if_ierrors++;
257096f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
257196f2e892SBill Paul 					ifp->if_collisions++;
257296f2e892SBill Paul 				dc_newbuf(sc, i, m);
257396f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
257496f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
257596f2e892SBill Paul 					continue;
257696f2e892SBill Paul 				} else {
257796f2e892SBill Paul 					dc_init(sc);
257896f2e892SBill Paul 					return;
257996f2e892SBill Paul 				}
258096f2e892SBill Paul 			}
2581db40c1aeSDoug Ambrisko 		}
258296f2e892SBill Paul 
258396f2e892SBill Paul 		/* No errors; receive the packet. */
258496f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
258501faf54bSLuigi Rizzo #ifdef __i386__
258601faf54bSLuigi Rizzo 		/*
258701faf54bSLuigi Rizzo 		 * On the x86 we do not have alignment problems, so try to
258801faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
258901faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
259001faf54bSLuigi Rizzo 		 * copy done in m_devget().
259101faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
259201faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
259301faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
259401faf54bSLuigi Rizzo 		 */
259501faf54bSLuigi Rizzo 		if (dc_quick && dc_newbuf(sc, i, NULL) == 0) {
259601faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
259701faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
259801faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
259901faf54bSLuigi Rizzo 		} else
260001faf54bSLuigi Rizzo #endif
260101faf54bSLuigi Rizzo 		{
260201faf54bSLuigi Rizzo 			struct mbuf *m0;
260396f2e892SBill Paul 
260401faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
260501faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
260696f2e892SBill Paul 			dc_newbuf(sc, i, m);
260796f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
260896f2e892SBill Paul 			if (m0 == NULL) {
260996f2e892SBill Paul 				ifp->if_ierrors++;
261096f2e892SBill Paul 				continue;
261196f2e892SBill Paul 			}
261296f2e892SBill Paul 			m = m0;
261301faf54bSLuigi Rizzo 		}
261496f2e892SBill Paul 
261596f2e892SBill Paul 		ifp->if_ipackets++;
26169ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
261796f2e892SBill Paul 	}
261896f2e892SBill Paul 
261996f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
262096f2e892SBill Paul }
262196f2e892SBill Paul 
262296f2e892SBill Paul /*
262396f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
262496f2e892SBill Paul  * the list buffers.
262596f2e892SBill Paul  */
262696f2e892SBill Paul 
2627e3d2833aSAlfred Perlstein static void
26280934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
262996f2e892SBill Paul {
263096f2e892SBill Paul 	struct dc_desc *cur_tx = NULL;
263196f2e892SBill Paul 	struct ifnet *ifp;
263296f2e892SBill Paul 	int idx;
26330934f18aSMaxime Henrion 	u_int32_t txstat;
263496f2e892SBill Paul 
263596f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
263696f2e892SBill Paul 
263796f2e892SBill Paul 	/*
263896f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
263996f2e892SBill Paul 	 * frames that have been transmitted.
264096f2e892SBill Paul 	 */
264196f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
264296f2e892SBill Paul 	while (idx != sc->dc_cdata.dc_tx_prod) {
264396f2e892SBill Paul 
264496f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
264596f2e892SBill Paul 		txstat = cur_tx->dc_status;
264696f2e892SBill Paul 
264796f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
264896f2e892SBill Paul 			break;
264996f2e892SBill Paul 
265096f2e892SBill Paul 		if (!(cur_tx->dc_ctl & DC_TXCTL_LASTFRAG) ||
265196f2e892SBill Paul 		    cur_tx->dc_ctl & DC_TXCTL_SETUP) {
265296f2e892SBill Paul 			if (cur_tx->dc_ctl & DC_TXCTL_SETUP) {
265396f2e892SBill Paul 				/*
265496f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
265596f2e892SBill Paul 				 * that it will sometimes generate a TX
265696f2e892SBill Paul 				 * underrun error while DMAing the RX
265796f2e892SBill Paul 				 * filter setup frame. If we detect this,
265896f2e892SBill Paul 				 * we have to send the setup frame again,
265996f2e892SBill Paul 				 * or else the filter won't be programmed
266096f2e892SBill Paul 				 * correctly.
266196f2e892SBill Paul 				 */
266296f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
266396f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
266496f2e892SBill Paul 						dc_setfilt(sc);
266596f2e892SBill Paul 				}
266696f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
266796f2e892SBill Paul 			}
2668bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
266996f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
267096f2e892SBill Paul 			continue;
267196f2e892SBill Paul 		}
267296f2e892SBill Paul 
267329a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2674feb78939SJonathan Chen 			/*
2675feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2676feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
267729a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
267829a2220aSBill Paul 			 * Who knows, but Conexant chips have the
267929a2220aSBill Paul 			 * same problem. Maybe they took lessons
268029a2220aSBill Paul 			 * from Xircom.
268129a2220aSBill Paul 			 */
2682feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2683feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2684feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2685feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2686feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2687feb78939SJonathan Chen 		} else {
268896f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
268996f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
269096f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
269196f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
269296f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2693feb78939SJonathan Chen 		}
269496f2e892SBill Paul 
269596f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
269696f2e892SBill Paul 			ifp->if_oerrors++;
269796f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
269896f2e892SBill Paul 				ifp->if_collisions++;
269996f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
270096f2e892SBill Paul 				ifp->if_collisions++;
270196f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
270296f2e892SBill Paul 				dc_init(sc);
270396f2e892SBill Paul 				return;
270496f2e892SBill Paul 			}
270596f2e892SBill Paul 		}
270696f2e892SBill Paul 
270796f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
270896f2e892SBill Paul 
270996f2e892SBill Paul 		ifp->if_opackets++;
271096f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
271196f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
271296f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
271396f2e892SBill Paul 		}
271496f2e892SBill Paul 
271596f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
271696f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
271796f2e892SBill Paul 	}
271896f2e892SBill Paul 
2719bcb9ef4fSLuigi Rizzo 	if (idx != sc->dc_cdata.dc_tx_cons) {
27200934f18aSMaxime Henrion 	    	/* Some buffers have been freed. */
272196f2e892SBill Paul 		sc->dc_cdata.dc_tx_cons = idx;
272296f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
2723bcb9ef4fSLuigi Rizzo 	}
2724bcb9ef4fSLuigi Rizzo 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
272596f2e892SBill Paul }
272696f2e892SBill Paul 
2727e3d2833aSAlfred Perlstein static void
27280934f18aSMaxime Henrion dc_tick(void *xsc)
272996f2e892SBill Paul {
273096f2e892SBill Paul 	struct dc_softc *sc;
273196f2e892SBill Paul 	struct mii_data *mii;
273296f2e892SBill Paul 	struct ifnet *ifp;
273396f2e892SBill Paul 	u_int32_t r;
273496f2e892SBill Paul 
273596f2e892SBill Paul 	sc = xsc;
2736d1ce9105SBill Paul 	DC_LOCK(sc);
273796f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
273896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
273996f2e892SBill Paul 
274096f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2741318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2742318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2743318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2744318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
274596f2e892SBill Paul 				sc->dc_link = 0;
2746318b02fdSBill Paul 				mii_mediachg(mii);
2747318b02fdSBill Paul 			}
2748318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2749318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2750318b02fdSBill Paul 				sc->dc_link = 0;
2751318b02fdSBill Paul 				mii_mediachg(mii);
2752318b02fdSBill Paul 			}
2753d675147eSBill Paul 			if (sc->dc_link == 0)
275496f2e892SBill Paul 				mii_tick(mii);
275596f2e892SBill Paul 		} else {
2756318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
275796f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2758259b8d84SMartin Blapp 			    sc->dc_cdata.dc_tx_cnt == 0) {
275996f2e892SBill Paul 				mii_tick(mii);
2760042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2761042c8f6eSBill Paul 					sc->dc_link = 0;
276296f2e892SBill Paul 			}
2763259b8d84SMartin Blapp 		}
276496f2e892SBill Paul 	} else
276596f2e892SBill Paul 		mii_tick(mii);
276696f2e892SBill Paul 
276796f2e892SBill Paul 	/*
276896f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
276996f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
277096f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
277196f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
277296f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
277396f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
277496f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
277596f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
277696f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
277796f2e892SBill Paul 	 * a screeching halt for several seconds.
277896f2e892SBill Paul 	 *
277996f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
278096f2e892SBill Paul 	 * any packets until a link has been established. After the
278196f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
278296f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
278396f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
278496f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
278596f2e892SBill Paul 	 */
2786cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
278796f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
278896f2e892SBill Paul 		sc->dc_link++;
278996f2e892SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
279096f2e892SBill Paul 			dc_start(ifp);
279196f2e892SBill Paul 	}
279296f2e892SBill Paul 
2793318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2794b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2795318b02fdSBill Paul 	else
2796b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
279796f2e892SBill Paul 
2798d1ce9105SBill Paul 	DC_UNLOCK(sc);
279996f2e892SBill Paul }
280096f2e892SBill Paul 
2801d467c136SBill Paul /*
2802d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2803d467c136SBill Paul  * or switch to store and forward mode if we have to.
2804d467c136SBill Paul  */
2805e3d2833aSAlfred Perlstein static void
28060934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
2807d467c136SBill Paul {
2808d467c136SBill Paul 	u_int32_t isr;
2809d467c136SBill Paul 	int i;
2810d467c136SBill Paul 
2811d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
2812d467c136SBill Paul 		dc_init(sc);
2813d467c136SBill Paul 
2814d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
2815d467c136SBill Paul 		/*
2816d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
2817d467c136SBill Paul 		 * in order to change the transmit threshold or store
2818d467c136SBill Paul 		 * and forward state.
2819d467c136SBill Paul 		 */
2820d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2821d467c136SBill Paul 
2822d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
2823d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
2824d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
2825d467c136SBill Paul 				break;
2826d467c136SBill Paul 			DELAY(10);
2827d467c136SBill Paul 		}
2828d467c136SBill Paul 		if (i == DC_TIMEOUT) {
2829d467c136SBill Paul 			printf("dc%d: failed to force tx to idle state\n",
2830d467c136SBill Paul 			    sc->dc_unit);
2831d467c136SBill Paul 			dc_init(sc);
2832d467c136SBill Paul 		}
2833d467c136SBill Paul 	}
2834d467c136SBill Paul 
2835d467c136SBill Paul 	printf("dc%d: TX underrun -- ", sc->dc_unit);
2836d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
2837d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2838d467c136SBill Paul 		printf("using store and forward mode\n");
2839d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2840d467c136SBill Paul 	} else {
2841d467c136SBill Paul 		printf("increasing TX threshold\n");
2842d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2843d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2844d467c136SBill Paul 	}
2845d467c136SBill Paul 
2846d467c136SBill Paul 	if (DC_IS_INTEL(sc))
2847d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2848d467c136SBill Paul }
2849d467c136SBill Paul 
2850e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2851e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
2852e4fc250cSLuigi Rizzo 
2853e4fc250cSLuigi Rizzo static void
2854e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2855e4fc250cSLuigi Rizzo {
2856e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
2857e4fc250cSLuigi Rizzo 
2858e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
2859e4fc250cSLuigi Rizzo 		/* Re-enable interrupts. */
2860e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
2861e4fc250cSLuigi Rizzo 		return;
2862e4fc250cSLuigi Rizzo 	}
2863e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
2864e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
2865e4fc250cSLuigi Rizzo 	dc_txeof(sc);
2866e4fc250cSLuigi Rizzo 	if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE))
2867e4fc250cSLuigi Rizzo 		dc_start(ifp);
2868e4fc250cSLuigi Rizzo 
2869e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2870e4fc250cSLuigi Rizzo 		u_int32_t	status;
2871e4fc250cSLuigi Rizzo 
2872e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
2873e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
2874e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
2875e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
2876e4fc250cSLuigi Rizzo 		if (!status)
2877e4fc250cSLuigi Rizzo 			return;
2878e4fc250cSLuigi Rizzo 		/* ack what we have */
2879e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
2880e4fc250cSLuigi Rizzo 
2881e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
2882e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
2883e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
2884e4fc250cSLuigi Rizzo 
2885e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
2886e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
2887e4fc250cSLuigi Rizzo 		}
2888e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
2889e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
2890e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
2891e4fc250cSLuigi Rizzo 
2892e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
2893e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
2894e4fc250cSLuigi Rizzo 
2895e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
2896e4fc250cSLuigi Rizzo 			printf("dc_poll: dc%d bus error\n", sc->dc_unit);
2897e4fc250cSLuigi Rizzo 			dc_reset(sc);
2898e4fc250cSLuigi Rizzo 			dc_init(sc);
2899e4fc250cSLuigi Rizzo 		}
2900e4fc250cSLuigi Rizzo 	}
2901e4fc250cSLuigi Rizzo }
2902e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
2903e4fc250cSLuigi Rizzo 
2904e3d2833aSAlfred Perlstein static void
29050934f18aSMaxime Henrion dc_intr(void *arg)
290696f2e892SBill Paul {
290796f2e892SBill Paul 	struct dc_softc *sc;
290896f2e892SBill Paul 	struct ifnet *ifp;
290996f2e892SBill Paul 	u_int32_t status;
291096f2e892SBill Paul 
291196f2e892SBill Paul 	sc = arg;
2912d2a1864bSWarner Losh 
29130934f18aSMaxime Henrion 	if (sc->suspended)
2914e8388e14SMitsuru IWASAKI 		return;
2915e8388e14SMitsuru IWASAKI 
2916d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
2917d2a1864bSWarner Losh 		return;
2918d2a1864bSWarner Losh 
2919d1ce9105SBill Paul 	DC_LOCK(sc);
292096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
2921e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
292262f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
2923e4fc250cSLuigi Rizzo 		goto done;
2924e4fc250cSLuigi Rizzo 	if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */
2925e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
2926e4fc250cSLuigi Rizzo 		goto done;
2927e4fc250cSLuigi Rizzo 	}
29280934f18aSMaxime Henrion #endif
292996f2e892SBill Paul 
2930d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
293196f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
293296f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
293396f2e892SBill Paul 			dc_stop(sc);
2934d1ce9105SBill Paul 		DC_UNLOCK(sc);
293596f2e892SBill Paul 		return;
293696f2e892SBill Paul 	}
293796f2e892SBill Paul 
293896f2e892SBill Paul 	/* Disable interrupts. */
293996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
294096f2e892SBill Paul 
2941feb78939SJonathan Chen 	while (((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
2942feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
294396f2e892SBill Paul 
294496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
294596f2e892SBill Paul 
294673bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
294773bf949cSBill Paul 			int		curpkts;
294873bf949cSBill Paul 			curpkts = ifp->if_ipackets;
294996f2e892SBill Paul 			dc_rxeof(sc);
295073bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
295173bf949cSBill Paul 				while (dc_rx_resync(sc))
295273bf949cSBill Paul 					dc_rxeof(sc);
295373bf949cSBill Paul 			}
295473bf949cSBill Paul 		}
295596f2e892SBill Paul 
295696f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
295796f2e892SBill Paul 			dc_txeof(sc);
295896f2e892SBill Paul 
295996f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
296096f2e892SBill Paul 			dc_txeof(sc);
296196f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
296296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
296396f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
296496f2e892SBill Paul 			}
296596f2e892SBill Paul 		}
296696f2e892SBill Paul 
2967d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
2968d467c136SBill Paul 			dc_tx_underrun(sc);
296996f2e892SBill Paul 
297096f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
297173bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
297273bf949cSBill Paul 			int		curpkts;
297373bf949cSBill Paul 			curpkts = ifp->if_ipackets;
297496f2e892SBill Paul 			dc_rxeof(sc);
297573bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
297673bf949cSBill Paul 				while (dc_rx_resync(sc))
297773bf949cSBill Paul 					dc_rxeof(sc);
297873bf949cSBill Paul 			}
297973bf949cSBill Paul 		}
298096f2e892SBill Paul 
298196f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
298296f2e892SBill Paul 			dc_reset(sc);
298396f2e892SBill Paul 			dc_init(sc);
298496f2e892SBill Paul 		}
298596f2e892SBill Paul 	}
298696f2e892SBill Paul 
298796f2e892SBill Paul 	/* Re-enable interrupts. */
298896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
298996f2e892SBill Paul 
299096f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
299196f2e892SBill Paul 		dc_start(ifp);
299296f2e892SBill Paul 
2993d9700bb5SBill Paul #ifdef DEVICE_POLLING
2994e4fc250cSLuigi Rizzo done:
29950934f18aSMaxime Henrion #endif
2996d9700bb5SBill Paul 
2997d1ce9105SBill Paul 	DC_UNLOCK(sc);
299896f2e892SBill Paul }
299996f2e892SBill Paul 
300096f2e892SBill Paul /*
300196f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
300296f2e892SBill Paul  * pointers to the fragment pointers.
300396f2e892SBill Paul  */
3004e3d2833aSAlfred Perlstein static int
30050934f18aSMaxime Henrion dc_encap(struct dc_softc *sc, struct mbuf *m_head, u_int32_t *txidx)
300696f2e892SBill Paul {
300796f2e892SBill Paul 	struct dc_desc *f = NULL;
300896f2e892SBill Paul 	struct mbuf *m;
3009cda97c50SMike Silbersack 	int frag, cur, cnt = 0, chainlen = 0;
3010cda97c50SMike Silbersack 
3011cda97c50SMike Silbersack 	/*
3012cda97c50SMike Silbersack 	 * If there's no way we can send any packets, return now.
3013cda97c50SMike Silbersack 	 */
3014cda97c50SMike Silbersack 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt < 6)
3015cda97c50SMike Silbersack 		return (ENOBUFS);
3016cda97c50SMike Silbersack 
3017cda97c50SMike Silbersack 	/*
3018cda97c50SMike Silbersack 	 * Count the number of frags in this chain to see if
3019cda97c50SMike Silbersack 	 * we need to m_defrag.  Since the descriptor list is shared
3020cda97c50SMike Silbersack 	 * by all packets, we'll m_defrag long chains so that they
3021cda97c50SMike Silbersack 	 * do not use up the entire list, even if they would fit.
3022cda97c50SMike Silbersack 	 */
3023cda97c50SMike Silbersack 
3024cda97c50SMike Silbersack 	for (m = m_head; m != NULL; m = m->m_next)
3025cda97c50SMike Silbersack 		chainlen++;
3026cda97c50SMike Silbersack 
3027cda97c50SMike Silbersack 	if ((chainlen > DC_TX_LIST_CNT / 4) ||
3028cda97c50SMike Silbersack 	    ((DC_TX_LIST_CNT - (chainlen + sc->dc_cdata.dc_tx_cnt)) < 6)) {
3029cda97c50SMike Silbersack 		m = m_defrag(m_head, M_DONTWAIT);
3030cda97c50SMike Silbersack 		if (m == NULL)
3031cda97c50SMike Silbersack 			return (ENOBUFS);
3032cda97c50SMike Silbersack 		m_head = m;
3033cda97c50SMike Silbersack 	}
303496f2e892SBill Paul 
303596f2e892SBill Paul 	/*
303696f2e892SBill Paul 	 * Start packing the mbufs in this chain into
303796f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
303896f2e892SBill Paul 	 * of fragments or hit the end of the mbuf chain.
303996f2e892SBill Paul 	 */
304096f2e892SBill Paul 	m = m_head;
304196f2e892SBill Paul 	cur = frag = *txidx;
304296f2e892SBill Paul 
304396f2e892SBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
304496f2e892SBill Paul 		if (m->m_len != 0) {
304596f2e892SBill Paul 			if (sc->dc_flags & DC_TX_ADMTEK_WAR) {
304696f2e892SBill Paul 				if (*txidx != sc->dc_cdata.dc_tx_prod &&
304796f2e892SBill Paul 				    frag == (DC_TX_LIST_CNT - 1))
304896f2e892SBill Paul 					return (ENOBUFS);
304996f2e892SBill Paul 			}
305096f2e892SBill Paul 			if ((DC_TX_LIST_CNT -
305196f2e892SBill Paul 			    (sc->dc_cdata.dc_tx_cnt + cnt)) < 5)
305296f2e892SBill Paul 				return (ENOBUFS);
305396f2e892SBill Paul 
305496f2e892SBill Paul 			f = &sc->dc_ldata->dc_tx_list[frag];
305596f2e892SBill Paul 			f->dc_ctl = DC_TXCTL_TLINK | m->m_len;
305696f2e892SBill Paul 			if (cnt == 0) {
305796f2e892SBill Paul 				f->dc_status = 0;
305896f2e892SBill Paul 				f->dc_ctl |= DC_TXCTL_FIRSTFRAG;
305996f2e892SBill Paul 			} else
306096f2e892SBill Paul 				f->dc_status = DC_TXSTAT_OWN;
306196f2e892SBill Paul 			f->dc_data = vtophys(mtod(m, vm_offset_t));
306296f2e892SBill Paul 			cur = frag;
306396f2e892SBill Paul 			DC_INC(frag, DC_TX_LIST_CNT);
306496f2e892SBill Paul 			cnt++;
306596f2e892SBill Paul 		}
306696f2e892SBill Paul 	}
306796f2e892SBill Paul 
306896f2e892SBill Paul 	if (m != NULL)
306996f2e892SBill Paul 		return (ENOBUFS);
307096f2e892SBill Paul 
307196f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt += cnt;
307296f2e892SBill Paul 	sc->dc_cdata.dc_tx_chain[cur] = m_head;
307396f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_LASTFRAG;
307496f2e892SBill Paul 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
307596f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[*txidx].dc_ctl |= DC_TXCTL_FINT;
307691cc2adbSBill Paul 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
307791cc2adbSBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
307896f2e892SBill Paul 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
307996f2e892SBill Paul 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= DC_TXCTL_FINT;
308096f2e892SBill Paul 	sc->dc_ldata->dc_tx_list[*txidx].dc_status = DC_TXSTAT_OWN;
308196f2e892SBill Paul 	*txidx = frag;
308296f2e892SBill Paul 
308396f2e892SBill Paul 	return (0);
308496f2e892SBill Paul }
308596f2e892SBill Paul 
308696f2e892SBill Paul /*
308796f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
308896f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
308996f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
309096f2e892SBill Paul  * physical addresses.
309196f2e892SBill Paul  */
309296f2e892SBill Paul 
3093e3d2833aSAlfred Perlstein static void
30940934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
309596f2e892SBill Paul {
309696f2e892SBill Paul 	struct dc_softc *sc;
3097cda97c50SMike Silbersack 	struct mbuf *m_head = NULL, *m;
309896f2e892SBill Paul 	int idx;
309996f2e892SBill Paul 
310096f2e892SBill Paul 	sc = ifp->if_softc;
310196f2e892SBill Paul 
3102d1ce9105SBill Paul 	DC_LOCK(sc);
310396f2e892SBill Paul 
3104e7be9f9aSBill Paul 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10) {
3105d1ce9105SBill Paul 		DC_UNLOCK(sc);
310696f2e892SBill Paul 		return;
3107d1ce9105SBill Paul 	}
3108d1ce9105SBill Paul 
3109d1ce9105SBill Paul 	if (ifp->if_flags & IFF_OACTIVE) {
3110d1ce9105SBill Paul 		DC_UNLOCK(sc);
3111d1ce9105SBill Paul 		return;
3112d1ce9105SBill Paul 	}
311396f2e892SBill Paul 
311496f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_prod;
311596f2e892SBill Paul 
311696f2e892SBill Paul 	while (sc->dc_cdata.dc_tx_chain[idx] == NULL) {
311796f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
311896f2e892SBill Paul 		if (m_head == NULL)
311996f2e892SBill Paul 			break;
312096f2e892SBill Paul 
31212dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
31222dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
31232dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3124cda97c50SMike Silbersack 			m = m_defrag(m_head, M_DONTWAIT);
3125cda97c50SMike Silbersack 			if (m == NULL) {
3126fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
3127fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
3128fda39fd0SBill Paul 				break;
3129cda97c50SMike Silbersack 			} else {
3130cda97c50SMike Silbersack 				m_head = m;
3131fda39fd0SBill Paul 			}
3132fda39fd0SBill Paul 		}
3133fda39fd0SBill Paul 
313496f2e892SBill Paul 		if (dc_encap(sc, m_head, &idx)) {
313596f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
313696f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
313796f2e892SBill Paul 			break;
313896f2e892SBill Paul 		}
313996f2e892SBill Paul 
314096f2e892SBill Paul 		/*
314196f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
314296f2e892SBill Paul 		 * to him.
314396f2e892SBill Paul 		 */
31449ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
31455c1cfac4SBill Paul 
31465c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
31475c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
31485c1cfac4SBill Paul 			break;
31495c1cfac4SBill Paul 		}
315096f2e892SBill Paul 	}
315196f2e892SBill Paul 
315296f2e892SBill Paul 	/* Transmit */
315396f2e892SBill Paul 	sc->dc_cdata.dc_tx_prod = idx;
315496f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
315596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
315696f2e892SBill Paul 
315796f2e892SBill Paul 	/*
315896f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
315996f2e892SBill Paul 	 */
316096f2e892SBill Paul 	ifp->if_timer = 5;
316196f2e892SBill Paul 
3162d1ce9105SBill Paul 	DC_UNLOCK(sc);
316396f2e892SBill Paul }
316496f2e892SBill Paul 
3165e3d2833aSAlfred Perlstein static void
31660934f18aSMaxime Henrion dc_init(void *xsc)
316796f2e892SBill Paul {
316896f2e892SBill Paul 	struct dc_softc *sc = xsc;
316996f2e892SBill Paul 	struct ifnet *ifp = &sc->arpcom.ac_if;
317096f2e892SBill Paul 	struct mii_data *mii;
317196f2e892SBill Paul 
3172d1ce9105SBill Paul 	DC_LOCK(sc);
317396f2e892SBill Paul 
317496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
317596f2e892SBill Paul 
317696f2e892SBill Paul 	/*
317796f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
317896f2e892SBill Paul 	 */
317996f2e892SBill Paul 	dc_stop(sc);
318096f2e892SBill Paul 	dc_reset(sc);
318196f2e892SBill Paul 
318296f2e892SBill Paul 	/*
318396f2e892SBill Paul 	 * Set cache alignment and burst length.
318496f2e892SBill Paul 	 */
318588d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
318696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
318796f2e892SBill Paul 	else
318896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3189935fe010SLuigi Rizzo 	/*
3190935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3191935fe010SLuigi Rizzo 	 */
3192935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3193935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
319496f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
319596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
319696f2e892SBill Paul 	} else {
319796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
319896f2e892SBill Paul 	}
319996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
320096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
320196f2e892SBill Paul 	switch(sc->dc_cachesize) {
320296f2e892SBill Paul 	case 32:
320396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
320496f2e892SBill Paul 		break;
320596f2e892SBill Paul 	case 16:
320696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
320796f2e892SBill Paul 		break;
320896f2e892SBill Paul 	case 8:
320996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
321096f2e892SBill Paul 		break;
321196f2e892SBill Paul 	case 0:
321296f2e892SBill Paul 	default:
321396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
321496f2e892SBill Paul 		break;
321596f2e892SBill Paul 	}
321696f2e892SBill Paul 
321796f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
321896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
321996f2e892SBill Paul 	else {
3220d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
322196f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
322296f2e892SBill Paul 		} else {
322396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
322496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
322596f2e892SBill Paul 		}
322696f2e892SBill Paul 	}
322796f2e892SBill Paul 
322896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
322996f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
323096f2e892SBill Paul 
323196f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
323296f2e892SBill Paul 		/*
323396f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
323496f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
323596f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
323696f2e892SBill Paul 		 * document the meaning of these bits so there's no way
323796f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
323896f2e892SBill Paul 		 * number all its own; the rest all use a different one.
323996f2e892SBill Paul 		 */
324096f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
324196f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
324296f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
324396f2e892SBill Paul 		else
324496f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
324596f2e892SBill Paul 	}
324696f2e892SBill Paul 
3247feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3248feb78939SJonathan Chen 		/*
3249feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3250feb78939SJonathan Chen 		 * can talk to the MII.
3251feb78939SJonathan Chen 		 */
3252feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3253feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3254feb78939SJonathan Chen 		DELAY(10);
3255feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3256feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3257feb78939SJonathan Chen 		DELAY(10);
3258feb78939SJonathan Chen 	}
3259feb78939SJonathan Chen 
326096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3261d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
326296f2e892SBill Paul 
326396f2e892SBill Paul 	/* Init circular RX list. */
326496f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
326596f2e892SBill Paul 		printf("dc%d: initialization failed: no "
326696f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
326796f2e892SBill Paul 		dc_stop(sc);
3268d1ce9105SBill Paul 		DC_UNLOCK(sc);
326996f2e892SBill Paul 		return;
327096f2e892SBill Paul 	}
327196f2e892SBill Paul 
327296f2e892SBill Paul 	/*
327396f2e892SBill Paul 	 * Init tx descriptors.
327496f2e892SBill Paul 	 */
327596f2e892SBill Paul 	dc_list_tx_init(sc);
327696f2e892SBill Paul 
327796f2e892SBill Paul 	/*
327896f2e892SBill Paul 	 * Load the address of the RX list.
327996f2e892SBill Paul 	 */
328096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, vtophys(&sc->dc_ldata->dc_rx_list[0]));
328196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, vtophys(&sc->dc_ldata->dc_tx_list[0]));
328296f2e892SBill Paul 
328396f2e892SBill Paul 	/*
328496f2e892SBill Paul 	 * Enable interrupts.
328596f2e892SBill Paul 	 */
3286e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3287e4fc250cSLuigi Rizzo 	/*
3288e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3289e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3290e4fc250cSLuigi Rizzo 	 * after a reset.
3291e4fc250cSLuigi Rizzo 	 */
329262f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
3293e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3294e4fc250cSLuigi Rizzo 	else
3295e4fc250cSLuigi Rizzo #endif
329696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
329796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
329896f2e892SBill Paul 
329996f2e892SBill Paul 	/* Enable transmitter. */
330096f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
330196f2e892SBill Paul 
330296f2e892SBill Paul 	/*
3303918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3304918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3305918434c8SBill Paul 	 * link and activity indications.
3306918434c8SBill Paul 	 */
330778999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3308918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3309918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
331078999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3311918434c8SBill Paul 	}
3312918434c8SBill Paul 
3313918434c8SBill Paul 	/*
331496f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
331596f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
331696f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
331796f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
331896f2e892SBill Paul 	 */
331996f2e892SBill Paul 	dc_setfilt(sc);
332096f2e892SBill Paul 
332196f2e892SBill Paul 	/* Enable receiver. */
332296f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
332396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
332496f2e892SBill Paul 
332596f2e892SBill Paul 	mii_mediachg(mii);
332696f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
332796f2e892SBill Paul 
332896f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
332996f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
333096f2e892SBill Paul 
3331857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
333245521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3333857fd445SBill Paul 		sc->dc_link = 1;
3334857fd445SBill Paul 	else {
3335318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3336b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3337318b02fdSBill Paul 		else
3338b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3339857fd445SBill Paul 	}
334096f2e892SBill Paul 
33415c1cfac4SBill Paul #ifdef SRM_MEDIA
3342510a809eSMike Smith 	if(sc->dc_srm_media) {
3343510a809eSMike Smith 		struct ifreq ifr;
3344510a809eSMike Smith 
3345510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
3346510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3347510a809eSMike Smith 		sc->dc_srm_media = 0;
3348510a809eSMike Smith 	}
3349510a809eSMike Smith #endif
3350d1ce9105SBill Paul 	DC_UNLOCK(sc);
335196f2e892SBill Paul }
335296f2e892SBill Paul 
335396f2e892SBill Paul /*
335496f2e892SBill Paul  * Set media options.
335596f2e892SBill Paul  */
3356e3d2833aSAlfred Perlstein static int
33570934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
335896f2e892SBill Paul {
335996f2e892SBill Paul 	struct dc_softc *sc;
336096f2e892SBill Paul 	struct mii_data *mii;
3361f43d9309SBill Paul 	struct ifmedia *ifm;
336296f2e892SBill Paul 
336396f2e892SBill Paul 	sc = ifp->if_softc;
336496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
336596f2e892SBill Paul 	mii_mediachg(mii);
3366f43d9309SBill Paul 	ifm = &mii->mii_media;
3367f43d9309SBill Paul 
3368f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
336945521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3370f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3371f43d9309SBill Paul 	else
337296f2e892SBill Paul 		sc->dc_link = 0;
337396f2e892SBill Paul 
337496f2e892SBill Paul 	return (0);
337596f2e892SBill Paul }
337696f2e892SBill Paul 
337796f2e892SBill Paul /*
337896f2e892SBill Paul  * Report current media status.
337996f2e892SBill Paul  */
3380e3d2833aSAlfred Perlstein static void
33810934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
338296f2e892SBill Paul {
338396f2e892SBill Paul 	struct dc_softc *sc;
338496f2e892SBill Paul 	struct mii_data *mii;
3385f43d9309SBill Paul 	struct ifmedia *ifm;
338696f2e892SBill Paul 
338796f2e892SBill Paul 	sc = ifp->if_softc;
338896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
338996f2e892SBill Paul 	mii_pollstat(mii);
3390f43d9309SBill Paul 	ifm = &mii->mii_media;
3391f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
339245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3393f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3394f43d9309SBill Paul 			ifmr->ifm_status = 0;
3395f43d9309SBill Paul 			return;
3396f43d9309SBill Paul 		}
3397f43d9309SBill Paul 	}
339896f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
339996f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
340096f2e892SBill Paul }
340196f2e892SBill Paul 
3402e3d2833aSAlfred Perlstein static int
34030934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
340496f2e892SBill Paul {
340596f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
340696f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
340796f2e892SBill Paul 	struct mii_data *mii;
3408d1ce9105SBill Paul 	int error = 0;
340996f2e892SBill Paul 
3410d1ce9105SBill Paul 	DC_LOCK(sc);
341196f2e892SBill Paul 
341296f2e892SBill Paul 	switch (command) {
341396f2e892SBill Paul 	case SIOCSIFFLAGS:
341496f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
34155d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
34165d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
34175d6dfbbbSLuigi Rizzo 
34185d6dfbbbSLuigi Rizzo 			if (ifp->if_flags & IFF_RUNNING) {
34195d6dfbbbSLuigi Rizzo 				if (need_setfilt)
342096f2e892SBill Paul 					dc_setfilt(sc);
34215d6dfbbbSLuigi Rizzo 			} else {
342296f2e892SBill Paul 				sc->dc_txthresh = 0;
342396f2e892SBill Paul 				dc_init(sc);
342496f2e892SBill Paul 			}
342596f2e892SBill Paul 		} else {
342696f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
342796f2e892SBill Paul 				dc_stop(sc);
342896f2e892SBill Paul 		}
342996f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
343096f2e892SBill Paul 		error = 0;
343196f2e892SBill Paul 		break;
343296f2e892SBill Paul 	case SIOCADDMULTI:
343396f2e892SBill Paul 	case SIOCDELMULTI:
343496f2e892SBill Paul 		dc_setfilt(sc);
343596f2e892SBill Paul 		error = 0;
343696f2e892SBill Paul 		break;
343796f2e892SBill Paul 	case SIOCGIFMEDIA:
343896f2e892SBill Paul 	case SIOCSIFMEDIA:
343996f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
344096f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
34415c1cfac4SBill Paul #ifdef SRM_MEDIA
3442510a809eSMike Smith 		if (sc->dc_srm_media)
3443510a809eSMike Smith 			sc->dc_srm_media = 0;
3444510a809eSMike Smith #endif
344596f2e892SBill Paul 		break;
344696f2e892SBill Paul 	default:
34479ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
344896f2e892SBill Paul 		break;
344996f2e892SBill Paul 	}
345096f2e892SBill Paul 
3451d1ce9105SBill Paul 	DC_UNLOCK(sc);
345296f2e892SBill Paul 
345396f2e892SBill Paul 	return (error);
345496f2e892SBill Paul }
345596f2e892SBill Paul 
3456e3d2833aSAlfred Perlstein static void
34570934f18aSMaxime Henrion dc_watchdog(struct ifnet *ifp)
345896f2e892SBill Paul {
345996f2e892SBill Paul 	struct dc_softc *sc;
346096f2e892SBill Paul 
346196f2e892SBill Paul 	sc = ifp->if_softc;
346296f2e892SBill Paul 
3463d1ce9105SBill Paul 	DC_LOCK(sc);
3464d1ce9105SBill Paul 
346596f2e892SBill Paul 	ifp->if_oerrors++;
346696f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
346796f2e892SBill Paul 
346896f2e892SBill Paul 	dc_stop(sc);
346996f2e892SBill Paul 	dc_reset(sc);
347096f2e892SBill Paul 	dc_init(sc);
347196f2e892SBill Paul 
347296f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
347396f2e892SBill Paul 		dc_start(ifp);
347496f2e892SBill Paul 
3475d1ce9105SBill Paul 	DC_UNLOCK(sc);
347696f2e892SBill Paul }
347796f2e892SBill Paul 
347896f2e892SBill Paul /*
347996f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
348096f2e892SBill Paul  * RX and TX lists.
348196f2e892SBill Paul  */
3482e3d2833aSAlfred Perlstein static void
34830934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
348496f2e892SBill Paul {
34850934f18aSMaxime Henrion 	int i;
348696f2e892SBill Paul 	struct ifnet *ifp;
348796f2e892SBill Paul 
3488d1ce9105SBill Paul 	DC_LOCK(sc);
3489d1ce9105SBill Paul 
349096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
349196f2e892SBill Paul 	ifp->if_timer = 0;
349296f2e892SBill Paul 
3493b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
349496f2e892SBill Paul 
34953b3ec200SPeter Wemm 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3496e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3497e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
3498e4fc250cSLuigi Rizzo #endif
34993b3ec200SPeter Wemm 
350096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
350196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
350296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
350396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
350496f2e892SBill Paul 	sc->dc_link = 0;
350596f2e892SBill Paul 
350696f2e892SBill Paul 	/*
350796f2e892SBill Paul 	 * Free data in the RX lists.
350896f2e892SBill Paul 	 */
350996f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
351096f2e892SBill Paul 		if (sc->dc_cdata.dc_rx_chain[i] != NULL) {
351196f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_rx_chain[i]);
351296f2e892SBill Paul 			sc->dc_cdata.dc_rx_chain[i] = NULL;
351396f2e892SBill Paul 		}
351496f2e892SBill Paul 	}
35150934f18aSMaxime Henrion 	bzero(&sc->dc_ldata->dc_rx_list, sizeof(sc->dc_ldata->dc_rx_list));
351696f2e892SBill Paul 
351796f2e892SBill Paul 	/*
351896f2e892SBill Paul 	 * Free the TX list buffers.
351996f2e892SBill Paul 	 */
352096f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
352196f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[i] != NULL) {
352296f2e892SBill Paul 			if (sc->dc_ldata->dc_tx_list[i].dc_ctl &
352396f2e892SBill Paul 			    DC_TXCTL_SETUP) {
352496f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[i] = NULL;
352596f2e892SBill Paul 				continue;
352696f2e892SBill Paul 			}
352796f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[i]);
352896f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[i] = NULL;
352996f2e892SBill Paul 		}
353096f2e892SBill Paul 	}
353196f2e892SBill Paul 
35320934f18aSMaxime Henrion 	bzero(&sc->dc_ldata->dc_tx_list, sizeof(sc->dc_ldata->dc_tx_list));
353396f2e892SBill Paul 
3534d1ce9105SBill Paul 	DC_UNLOCK(sc);
353596f2e892SBill Paul }
353696f2e892SBill Paul 
353796f2e892SBill Paul /*
3538e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3539e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3540e8388e14SMitsuru IWASAKI  * resume.
3541e8388e14SMitsuru IWASAKI  */
3542e3d2833aSAlfred Perlstein static int
35430934f18aSMaxime Henrion dc_suspend(device_t dev)
3544e8388e14SMitsuru IWASAKI {
3545e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
35460934f18aSMaxime Henrion 	int i, s;
3547e8388e14SMitsuru IWASAKI 
3548e8388e14SMitsuru IWASAKI 	s = splimp();
3549e8388e14SMitsuru IWASAKI 
3550e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3551e8388e14SMitsuru IWASAKI 
3552e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3553e8388e14SMitsuru IWASAKI 
3554e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3555e8388e14SMitsuru IWASAKI 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
3556e8388e14SMitsuru IWASAKI 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
3557e8388e14SMitsuru IWASAKI 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
3558e8388e14SMitsuru IWASAKI 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3559e8388e14SMitsuru IWASAKI 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
3560e8388e14SMitsuru IWASAKI 
3561e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3562e8388e14SMitsuru IWASAKI 
3563e8388e14SMitsuru IWASAKI 	splx(s);
3564e8388e14SMitsuru IWASAKI 	return (0);
3565e8388e14SMitsuru IWASAKI }
3566e8388e14SMitsuru IWASAKI 
3567e8388e14SMitsuru IWASAKI /*
3568e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3569e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3570e8388e14SMitsuru IWASAKI  * appropriate.
3571e8388e14SMitsuru IWASAKI  */
3572e3d2833aSAlfred Perlstein static int
35730934f18aSMaxime Henrion dc_resume(device_t dev)
3574e8388e14SMitsuru IWASAKI {
3575e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3576e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
35770934f18aSMaxime Henrion 	int i, s;
3578e8388e14SMitsuru IWASAKI 
3579e8388e14SMitsuru IWASAKI 	s = splimp();
3580e8388e14SMitsuru IWASAKI 
3581e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3582e8388e14SMitsuru IWASAKI 	ifp = &sc->arpcom.ac_if;
3583e8388e14SMitsuru IWASAKI 
3584e8388e14SMitsuru IWASAKI 	dc_acpi(dev);
3585e8388e14SMitsuru IWASAKI 
3586e8388e14SMitsuru IWASAKI 	/* better way to do this? */
3587e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3588e8388e14SMitsuru IWASAKI 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
3589e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
3590e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
3591e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
3592e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
3593e8388e14SMitsuru IWASAKI 
3594e8388e14SMitsuru IWASAKI 	/* reenable busmastering */
3595e8388e14SMitsuru IWASAKI 	pci_enable_busmaster(dev);
3596e8388e14SMitsuru IWASAKI 	pci_enable_io(dev, DC_RES);
3597e8388e14SMitsuru IWASAKI 
3598e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3599e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3600e8388e14SMitsuru IWASAKI 		dc_init(sc);
3601e8388e14SMitsuru IWASAKI 
3602e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3603e8388e14SMitsuru IWASAKI 
3604e8388e14SMitsuru IWASAKI 	splx(s);
3605e8388e14SMitsuru IWASAKI 	return (0);
3606e8388e14SMitsuru IWASAKI }
3607e8388e14SMitsuru IWASAKI 
3608e8388e14SMitsuru IWASAKI /*
360996f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
361096f2e892SBill Paul  * get confused by errant DMAs when rebooting.
361196f2e892SBill Paul  */
3612e3d2833aSAlfred Perlstein static void
36130934f18aSMaxime Henrion dc_shutdown(device_t dev)
361496f2e892SBill Paul {
361596f2e892SBill Paul 	struct dc_softc *sc;
361696f2e892SBill Paul 
361796f2e892SBill Paul 	sc = device_get_softc(dev);
361896f2e892SBill Paul 
361996f2e892SBill Paul 	dc_stop(sc);
362096f2e892SBill Paul }
3621