xref: /freebsd/sys/dev/dc/if_dc.c (revision 4a80e74bf3448008643b410d409487fb96c0aec0)
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)
444c16d09eSWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek AN985
4588d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
469ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
47feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
481d5e5310SBill Paul  * Abocom FE2500
491af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
507eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5196f2e892SBill Paul  *
5296f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5396f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5496f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5596f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5696f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
5796f2e892SBill Paul  * instead of 512.
5896f2e892SBill Paul  *
5996f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6096f2e892SBill Paul  * Electrical Engineering Department
6196f2e892SBill Paul  * Columbia University, New York City
6296f2e892SBill Paul  */
6396f2e892SBill Paul 
6496f2e892SBill Paul /*
6596f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6696f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6796f2e892SBill Paul  * three kinds of media attachments:
6896f2e892SBill Paul  *
6996f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7096f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7196f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7296f2e892SBill Paul  * o 10baseT port.
7396f2e892SBill Paul  * o AUI/BNC port.
7496f2e892SBill Paul  *
7596f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7696f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7796f2e892SBill Paul  * autosensing configuration.
7896f2e892SBill Paul  *
7996f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8096f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8196f2e892SBill Paul  * handled separately due to its different register offsets and the
8296f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8396f2e892SBill Paul  * here, but I'm not thrilled about it.
8496f2e892SBill Paul  *
8596f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8696f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8796f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
8896f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
8996f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9096f2e892SBill Paul  */
9196f2e892SBill Paul 
928368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
938368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
948368cf8fSDavid E. O'Brien 
9596f2e892SBill Paul #include <sys/param.h>
96af4358c7SMaxime Henrion #include <sys/endian.h>
9796f2e892SBill Paul #include <sys/systm.h>
9896f2e892SBill Paul #include <sys/sockio.h>
9996f2e892SBill Paul #include <sys/mbuf.h>
10096f2e892SBill Paul #include <sys/malloc.h>
10196f2e892SBill Paul #include <sys/kernel.h>
10296f2e892SBill Paul #include <sys/socket.h>
10301faf54bSLuigi Rizzo #include <sys/sysctl.h>
10496f2e892SBill Paul 
10596f2e892SBill Paul #include <net/if.h>
10696f2e892SBill Paul #include <net/if_arp.h>
10796f2e892SBill Paul #include <net/ethernet.h>
10896f2e892SBill Paul #include <net/if_dl.h>
10996f2e892SBill Paul #include <net/if_media.h>
110db40c1aeSDoug Ambrisko #include <net/if_types.h>
111db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11296f2e892SBill Paul 
11396f2e892SBill Paul #include <net/bpf.h>
11496f2e892SBill Paul 
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 
12519b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
12619b7ffd1SWarner Losh #include <dev/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" },
160e351d778SMartin Blapp 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511,
161e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
162e351d778SMartin Blapp 	{ DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513,
163e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1644c16d09eSWarner Losh  	{ DC_VENDORID_ADMTEK, DC_DEVICEID_FA511,
1654c16d09eSWarner Losh  		"Netgear FA511 10/100BaseTX" },
16696f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16796f2e892SBill Paul 		"ASIX AX88140A 10/100BaseTX" },
16896f2e892SBill Paul 	{ DC_VENDORID_ASIX, DC_DEVICEID_AX88140A,
16996f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
17096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
17196f2e892SBill Paul 		"Macronix 98713 10/100BaseTX" },
17296f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98713,
17396f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
17496f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
17596f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17696f2e892SBill Paul 	{ DC_VENDORID_CP, DC_DEVICEID_98713_CP,
17796f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
17896f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
17996f2e892SBill Paul 		"Macronix 98715/98715A 10/100BaseTX" },
18096f2e892SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18179d11e09SBill Paul 		"Macronix 98715AEC-C 10/100BaseTX" },
18279d11e09SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_987x5,
18396f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
184ead7cde9SBill Paul 	{ DC_VENDORID_MX, DC_DEVICEID_98727,
185ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
18696f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C115,
18796f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
18896f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
18996f2e892SBill Paul 		"82c168 PNIC 10/100BaseTX" },
19096f2e892SBill Paul 	{ DC_VENDORID_LO, DC_DEVICEID_82C168,
19196f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1929ca710f6SJeroen Ruigrok van der Werven 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN1217,
1939ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
194fa167b8eSBill Paul 	{ DC_VENDORID_ACCTON, DC_DEVICEID_EN2242,
195fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
196feb78939SJonathan Chen 	{ DC_VENDORID_XIRCOM, DC_DEVICEID_X3201,
197feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
1981d5e5310SBill Paul 	{ DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500,
1991d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2001af8bec7SBill Paul 	{ DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112,
2011af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
202948c244dSWarner Losh 	{ DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX,
203948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
20497f91728SMIHIRA Sanpei Yoshiro 	{ DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T,
20597f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2067eac366bSMartin Blapp 	{ DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB,
2077eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
208e7b9ab3aSBill Paul 	{ DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120,
209e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
210e7b9ab3aSBill Paul 	{ DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130,
211e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
212e7b9ab3aSBill Paul 	{ DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130_FAKE,
213e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
21496f2e892SBill Paul 	{ 0, 0, NULL }
21596f2e892SBill Paul };
21696f2e892SBill Paul 
217e51a25f8SAlfred Perlstein static int dc_probe		(device_t);
218e51a25f8SAlfred Perlstein static int dc_attach		(device_t);
219e51a25f8SAlfred Perlstein static int dc_detach		(device_t);
220e8388e14SMitsuru IWASAKI static int dc_suspend		(device_t);
221e8388e14SMitsuru IWASAKI static int dc_resume		(device_t);
222b84e866aSWarner Losh #ifndef BURN_BRIDGES
223e51a25f8SAlfred Perlstein static void dc_acpi		(device_t);
224b84e866aSWarner Losh #endif
225e51a25f8SAlfred Perlstein static struct dc_type *dc_devtype	(device_t);
22656e5e7aeSMaxime Henrion static int dc_newbuf		(struct dc_softc *, int, int);
22756e5e7aeSMaxime Henrion static int dc_encap		(struct dc_softc *, struct mbuf *);
228e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war	(struct dc_softc *, int);
229e51a25f8SAlfred Perlstein static int dc_rx_resync		(struct dc_softc *);
230e51a25f8SAlfred Perlstein static void dc_rxeof		(struct dc_softc *);
231e51a25f8SAlfred Perlstein static void dc_txeof		(struct dc_softc *);
232e51a25f8SAlfred Perlstein static void dc_tick		(void *);
233e51a25f8SAlfred Perlstein static void dc_tx_underrun	(struct dc_softc *);
234e51a25f8SAlfred Perlstein static void dc_intr		(void *);
235e51a25f8SAlfred Perlstein static void dc_start		(struct ifnet *);
236e51a25f8SAlfred Perlstein static int dc_ioctl		(struct ifnet *, u_long, caddr_t);
237e51a25f8SAlfred Perlstein static void dc_init		(void *);
238e51a25f8SAlfred Perlstein static void dc_stop		(struct dc_softc *);
239e51a25f8SAlfred Perlstein static void dc_watchdog		(struct ifnet *);
240e51a25f8SAlfred Perlstein static void dc_shutdown		(device_t);
241e51a25f8SAlfred Perlstein static int dc_ifmedia_upd	(struct ifnet *);
242e51a25f8SAlfred Perlstein static void dc_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
24396f2e892SBill Paul 
244e51a25f8SAlfred Perlstein static void dc_delay		(struct dc_softc *);
245e51a25f8SAlfred Perlstein static void dc_eeprom_idle	(struct dc_softc *);
246e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte	(struct dc_softc *, int);
247e51a25f8SAlfred Perlstein static void dc_eeprom_getword	(struct dc_softc *, int, u_int16_t *);
24896f2e892SBill Paul static void dc_eeprom_getword_pnic
249e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
250feb78939SJonathan Chen static void dc_eeprom_getword_xircom
251e51a25f8SAlfred Perlstein 				(struct dc_softc *, int, u_int16_t *);
2523097aa70SWarner Losh static void dc_eeprom_width	(struct dc_softc *);
253e51a25f8SAlfred Perlstein static void dc_read_eeprom	(struct dc_softc *, caddr_t, int, int, int);
25496f2e892SBill Paul 
255e51a25f8SAlfred Perlstein static void dc_mii_writebit	(struct dc_softc *, int);
256e51a25f8SAlfred Perlstein static int dc_mii_readbit	(struct dc_softc *);
257e51a25f8SAlfred Perlstein static void dc_mii_sync		(struct dc_softc *);
258e51a25f8SAlfred Perlstein static void dc_mii_send		(struct dc_softc *, u_int32_t, int);
259e51a25f8SAlfred Perlstein static int dc_mii_readreg	(struct dc_softc *, struct dc_mii_frame *);
260e51a25f8SAlfred Perlstein static int dc_mii_writereg	(struct dc_softc *, struct dc_mii_frame *);
261e51a25f8SAlfred Perlstein static int dc_miibus_readreg	(device_t, int, int);
262e51a25f8SAlfred Perlstein static int dc_miibus_writereg	(device_t, int, int, int);
263e51a25f8SAlfred Perlstein static void dc_miibus_statchg	(device_t);
264e51a25f8SAlfred Perlstein static void dc_miibus_mediainit	(device_t);
26596f2e892SBill Paul 
266e51a25f8SAlfred Perlstein static void dc_setcfg		(struct dc_softc *, int);
267e51a25f8SAlfred Perlstein static u_int32_t dc_crc_le	(struct dc_softc *, caddr_t);
268e51a25f8SAlfred Perlstein static u_int32_t dc_crc_be	(caddr_t);
269e51a25f8SAlfred Perlstein static void dc_setfilt_21143	(struct dc_softc *);
270e51a25f8SAlfred Perlstein static void dc_setfilt_asix	(struct dc_softc *);
271e51a25f8SAlfred Perlstein static void dc_setfilt_admtek	(struct dc_softc *);
272e51a25f8SAlfred Perlstein static void dc_setfilt_xircom	(struct dc_softc *);
27396f2e892SBill Paul 
274e51a25f8SAlfred Perlstein static void dc_setfilt		(struct dc_softc *);
27596f2e892SBill Paul 
276e51a25f8SAlfred Perlstein static void dc_reset		(struct dc_softc *);
277e51a25f8SAlfred Perlstein static int dc_list_rx_init	(struct dc_softc *);
278e51a25f8SAlfred Perlstein static int dc_list_tx_init	(struct dc_softc *);
27996f2e892SBill Paul 
2803097aa70SWarner Losh static void dc_read_srom	(struct dc_softc *, int);
281e51a25f8SAlfred Perlstein static void dc_parse_21143_srom	(struct dc_softc *);
282e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia	(struct dc_softc *, struct dc_eblock_sia *);
283e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii	(struct dc_softc *, struct dc_eblock_mii *);
284e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym	(struct dc_softc *, struct dc_eblock_sym *);
285e51a25f8SAlfred Perlstein static void dc_apply_fixup	(struct dc_softc *, int);
2865c1cfac4SBill Paul 
28756e5e7aeSMaxime Henrion static void dc_dma_map_txbuf	(void *, bus_dma_segment_t *, int, bus_size_t,
28856e5e7aeSMaxime Henrion 				    int);
28956e5e7aeSMaxime Henrion static void dc_dma_map_rxbuf	(void *, bus_dma_segment_t *, int, bus_size_t,
29056e5e7aeSMaxime Henrion 				    int);
29156e5e7aeSMaxime Henrion 
29296f2e892SBill Paul #ifdef DC_USEIOSPACE
29396f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
29496f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
29596f2e892SBill Paul #else
29696f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
29796f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
29896f2e892SBill Paul #endif
29996f2e892SBill Paul 
30096f2e892SBill Paul static device_method_t dc_methods[] = {
30196f2e892SBill Paul 	/* Device interface */
30296f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
30396f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
30496f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
305e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
306e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
30796f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
30896f2e892SBill Paul 
30996f2e892SBill Paul 	/* bus interface */
31096f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
31196f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
31296f2e892SBill Paul 
31396f2e892SBill Paul 	/* MII interface */
31496f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
31596f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
31696f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
317f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
31896f2e892SBill Paul 
31996f2e892SBill Paul 	{ 0, 0 }
32096f2e892SBill Paul };
32196f2e892SBill Paul 
32296f2e892SBill Paul static driver_t dc_driver = {
32396f2e892SBill Paul 	"dc",
32496f2e892SBill Paul 	dc_methods,
32596f2e892SBill Paul 	sizeof(struct dc_softc)
32696f2e892SBill Paul };
32796f2e892SBill Paul 
32896f2e892SBill Paul static devclass_t dc_devclass;
32901faf54bSLuigi Rizzo #ifdef __i386__
33001faf54bSLuigi Rizzo static int dc_quick = 1;
331b3811c95SMaxime Henrion SYSCTL_INT(_hw, OID_AUTO, dc_quick, CTLFLAG_RW, &dc_quick, 0,
332b3811c95SMaxime Henrion     "do not mdevget in dc driver");
33301faf54bSLuigi Rizzo #endif
33496f2e892SBill Paul 
335f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, cardbus, dc_driver, dc_devclass, 0, 0);
336f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
33796f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
33896f2e892SBill Paul 
33996f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
34096f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
34196f2e892SBill Paul 
34296f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
34396f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
34496f2e892SBill Paul 
34596f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
34696f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
34796f2e892SBill Paul 
348b50c6312SJonathan Lemon #define IS_MPSAFE 	0
349b50c6312SJonathan Lemon 
350e3d2833aSAlfred Perlstein static void
3510934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
35296f2e892SBill Paul {
35396f2e892SBill Paul 	int idx;
35496f2e892SBill Paul 
35596f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
35696f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
35796f2e892SBill Paul }
35896f2e892SBill Paul 
3592c876e15SPoul-Henning Kamp static void
3600934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3613097aa70SWarner Losh {
3623097aa70SWarner Losh 	int i;
3633097aa70SWarner Losh 
3643097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3653097aa70SWarner Losh 	dc_eeprom_idle(sc);
3663097aa70SWarner Losh 
3673097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3683097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3693097aa70SWarner Losh 	dc_delay(sc);
3703097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3713097aa70SWarner Losh 	dc_delay(sc);
3723097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3733097aa70SWarner Losh 	dc_delay(sc);
3743097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3753097aa70SWarner Losh 	dc_delay(sc);
3763097aa70SWarner Losh 
3773097aa70SWarner Losh 	for (i = 3; i--;) {
3783097aa70SWarner Losh 		if (6 & (1 << i))
3793097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3803097aa70SWarner Losh 		else
3813097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3823097aa70SWarner Losh 		dc_delay(sc);
3833097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3843097aa70SWarner Losh 		dc_delay(sc);
3853097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3863097aa70SWarner Losh 		dc_delay(sc);
3873097aa70SWarner Losh 	}
3883097aa70SWarner Losh 
3893097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3903097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3913097aa70SWarner Losh 		dc_delay(sc);
3923097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3933097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3943097aa70SWarner Losh 			dc_delay(sc);
3953097aa70SWarner Losh 			break;
3963097aa70SWarner Losh 		}
3973097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3983097aa70SWarner Losh 		dc_delay(sc);
3993097aa70SWarner Losh 	}
4003097aa70SWarner Losh 
4013097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4023097aa70SWarner Losh 	dc_eeprom_idle(sc);
4033097aa70SWarner Losh 
4043097aa70SWarner Losh 	if (i < 4 || i > 12)
4053097aa70SWarner Losh 		sc->dc_romwidth = 6;
4063097aa70SWarner Losh 	else
4073097aa70SWarner Losh 		sc->dc_romwidth = i;
4083097aa70SWarner Losh 
4093097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4103097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4113097aa70SWarner Losh 	dc_delay(sc);
4123097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4133097aa70SWarner Losh 	dc_delay(sc);
4143097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4153097aa70SWarner Losh 	dc_delay(sc);
4163097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4173097aa70SWarner Losh 	dc_delay(sc);
4183097aa70SWarner Losh 
4193097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4203097aa70SWarner Losh 	dc_eeprom_idle(sc);
4213097aa70SWarner Losh }
4223097aa70SWarner Losh 
423e3d2833aSAlfred Perlstein static void
4240934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
42596f2e892SBill Paul {
4260934f18aSMaxime Henrion 	int i;
42796f2e892SBill Paul 
42896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
42996f2e892SBill Paul 	dc_delay(sc);
43096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
43196f2e892SBill Paul 	dc_delay(sc);
43296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43396f2e892SBill Paul 	dc_delay(sc);
43496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
43596f2e892SBill Paul 	dc_delay(sc);
43696f2e892SBill Paul 
43796f2e892SBill Paul 	for (i = 0; i < 25; i++) {
43896f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43996f2e892SBill Paul 		dc_delay(sc);
44096f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44196f2e892SBill Paul 		dc_delay(sc);
44296f2e892SBill Paul 	}
44396f2e892SBill Paul 
44496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44596f2e892SBill Paul 	dc_delay(sc);
44696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
44796f2e892SBill Paul 	dc_delay(sc);
44896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
44996f2e892SBill Paul }
45096f2e892SBill Paul 
45196f2e892SBill Paul /*
45296f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
45396f2e892SBill Paul  */
454e3d2833aSAlfred Perlstein static void
4550934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
45696f2e892SBill Paul {
4570934f18aSMaxime Henrion 	int d, i;
45896f2e892SBill Paul 
4593097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4603097aa70SWarner Losh 	for (i = 3; i--; ) {
4613097aa70SWarner Losh 		if (d & (1 << i))
4623097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
46396f2e892SBill Paul 		else
4643097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4653097aa70SWarner Losh 		dc_delay(sc);
4663097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4673097aa70SWarner Losh 		dc_delay(sc);
4683097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4693097aa70SWarner Losh 		dc_delay(sc);
4703097aa70SWarner Losh 	}
47196f2e892SBill Paul 
47296f2e892SBill Paul 	/*
47396f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
47496f2e892SBill Paul 	 */
4753097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4763097aa70SWarner Losh 		if (addr & (1 << i)) {
47796f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
47896f2e892SBill Paul 		} else {
47996f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
48096f2e892SBill Paul 		}
48196f2e892SBill Paul 		dc_delay(sc);
48296f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48396f2e892SBill Paul 		dc_delay(sc);
48496f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
48596f2e892SBill Paul 		dc_delay(sc);
48696f2e892SBill Paul 	}
48796f2e892SBill Paul }
48896f2e892SBill Paul 
48996f2e892SBill Paul /*
49096f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
49196f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
49296f2e892SBill Paul  * the EEPROM.
49396f2e892SBill Paul  */
494e3d2833aSAlfred Perlstein static void
4950934f18aSMaxime Henrion dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, u_int16_t *dest)
49696f2e892SBill Paul {
4970934f18aSMaxime Henrion 	int i;
49896f2e892SBill Paul 	u_int32_t r;
49996f2e892SBill Paul 
50096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
50196f2e892SBill Paul 
50296f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
50396f2e892SBill Paul 		DELAY(1);
50496f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
50596f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
50696f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
50796f2e892SBill Paul 			return;
50896f2e892SBill Paul 		}
50996f2e892SBill Paul 	}
51096f2e892SBill Paul }
51196f2e892SBill Paul 
51296f2e892SBill Paul /*
51396f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
514feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
515feb78939SJonathan Chen  * the EEPROM, too.
516feb78939SJonathan Chen  */
517e3d2833aSAlfred Perlstein static void
5180934f18aSMaxime Henrion dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, u_int16_t *dest)
519feb78939SJonathan Chen {
5200934f18aSMaxime Henrion 
521feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
522feb78939SJonathan Chen 
523feb78939SJonathan Chen 	addr *= 2;
524feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
525feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
526feb78939SJonathan Chen 	addr += 1;
527feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
528feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
529feb78939SJonathan Chen 
530feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
531feb78939SJonathan Chen }
532feb78939SJonathan Chen 
533feb78939SJonathan Chen /*
534feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
53596f2e892SBill Paul  */
536e3d2833aSAlfred Perlstein static void
5370934f18aSMaxime Henrion dc_eeprom_getword(struct dc_softc *sc, int addr, u_int16_t *dest)
53896f2e892SBill Paul {
5390934f18aSMaxime Henrion 	int i;
54096f2e892SBill Paul 	u_int16_t word = 0;
54196f2e892SBill Paul 
54296f2e892SBill Paul 	/* Force EEPROM to idle state. */
54396f2e892SBill Paul 	dc_eeprom_idle(sc);
54496f2e892SBill Paul 
54596f2e892SBill Paul 	/* Enter EEPROM access mode. */
54696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
54796f2e892SBill Paul 	dc_delay(sc);
54896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
54996f2e892SBill Paul 	dc_delay(sc);
55096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
55196f2e892SBill Paul 	dc_delay(sc);
55296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55396f2e892SBill Paul 	dc_delay(sc);
55496f2e892SBill Paul 
55596f2e892SBill Paul 	/*
55696f2e892SBill Paul 	 * Send address of word we want to read.
55796f2e892SBill Paul 	 */
55896f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
55996f2e892SBill Paul 
56096f2e892SBill Paul 	/*
56196f2e892SBill Paul 	 * Start reading bits from EEPROM.
56296f2e892SBill Paul 	 */
56396f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56496f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
56596f2e892SBill Paul 		dc_delay(sc);
56696f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
56796f2e892SBill Paul 			word |= i;
56896f2e892SBill Paul 		dc_delay(sc);
56996f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
57096f2e892SBill Paul 		dc_delay(sc);
57196f2e892SBill Paul 	}
57296f2e892SBill Paul 
57396f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57496f2e892SBill Paul 	dc_eeprom_idle(sc);
57596f2e892SBill Paul 
57696f2e892SBill Paul 	*dest = word;
57796f2e892SBill Paul }
57896f2e892SBill Paul 
57996f2e892SBill Paul /*
58096f2e892SBill Paul  * Read a sequence of words from the EEPROM.
58196f2e892SBill Paul  */
582e3d2833aSAlfred Perlstein static void
5830934f18aSMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int swap)
58496f2e892SBill Paul {
58596f2e892SBill Paul 	int i;
58696f2e892SBill Paul 	u_int16_t word = 0, *ptr;
58796f2e892SBill Paul 
58896f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
58996f2e892SBill Paul 		if (DC_IS_PNIC(sc))
59096f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
591feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
592feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59396f2e892SBill Paul 		else
59496f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
59596f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
59696f2e892SBill Paul 		if (swap)
59796f2e892SBill Paul 			*ptr = ntohs(word);
59896f2e892SBill Paul 		else
59996f2e892SBill Paul 			*ptr = word;
60096f2e892SBill Paul 	}
60196f2e892SBill Paul }
60296f2e892SBill Paul 
60396f2e892SBill Paul /*
60496f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
60596f2e892SBill Paul  * Application Notes pp.19-21.
60696f2e892SBill Paul  */
60796f2e892SBill Paul /*
60896f2e892SBill Paul  * Write a bit to the MII bus.
60996f2e892SBill Paul  */
610e3d2833aSAlfred Perlstein static void
6110934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
61296f2e892SBill Paul {
6130934f18aSMaxime Henrion 
61496f2e892SBill Paul 	if (bit)
61596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
61696f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE | DC_SIO_MII_DATAOUT);
61796f2e892SBill Paul 	else
61896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
61996f2e892SBill Paul 
62096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
62196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
62296f2e892SBill Paul }
62396f2e892SBill Paul 
62496f2e892SBill Paul /*
62596f2e892SBill Paul  * Read a bit from the MII bus.
62696f2e892SBill Paul  */
627e3d2833aSAlfred Perlstein static int
6280934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
62996f2e892SBill Paul {
6300934f18aSMaxime Henrion 
63196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR);
63296f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
63396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63596f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
63696f2e892SBill Paul 		return (1);
63796f2e892SBill Paul 
63896f2e892SBill Paul 	return (0);
63996f2e892SBill Paul }
64096f2e892SBill Paul 
64196f2e892SBill Paul /*
64296f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
64396f2e892SBill Paul  */
644e3d2833aSAlfred Perlstein static void
6450934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
64696f2e892SBill Paul {
6470934f18aSMaxime Henrion 	int i;
64896f2e892SBill Paul 
64996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
65096f2e892SBill Paul 
65196f2e892SBill Paul 	for (i = 0; i < 32; i++)
65296f2e892SBill Paul 		dc_mii_writebit(sc, 1);
65396f2e892SBill Paul }
65496f2e892SBill Paul 
65596f2e892SBill Paul /*
65696f2e892SBill Paul  * Clock a series of bits through the MII.
65796f2e892SBill Paul  */
658e3d2833aSAlfred Perlstein static void
6590934f18aSMaxime Henrion dc_mii_send(struct dc_softc *sc, u_int32_t bits, int cnt)
66096f2e892SBill Paul {
66196f2e892SBill Paul 	int i;
66296f2e892SBill Paul 
66396f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
66496f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
66596f2e892SBill Paul }
66696f2e892SBill Paul 
66796f2e892SBill Paul /*
66896f2e892SBill Paul  * Read an PHY register through the MII.
66996f2e892SBill Paul  */
670e3d2833aSAlfred Perlstein static int
6710934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
67296f2e892SBill Paul {
673d1ce9105SBill Paul 	int i, ack;
67496f2e892SBill Paul 
675d1ce9105SBill Paul 	DC_LOCK(sc);
67696f2e892SBill Paul 
67796f2e892SBill Paul 	/*
67896f2e892SBill Paul 	 * Set up frame for RX.
67996f2e892SBill Paul 	 */
68096f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
68196f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
68296f2e892SBill Paul 	frame->mii_turnaround = 0;
68396f2e892SBill Paul 	frame->mii_data = 0;
68496f2e892SBill Paul 
68596f2e892SBill Paul 	/*
68696f2e892SBill Paul 	 * Sync the PHYs.
68796f2e892SBill Paul 	 */
68896f2e892SBill Paul 	dc_mii_sync(sc);
68996f2e892SBill Paul 
69096f2e892SBill Paul 	/*
69196f2e892SBill Paul 	 * Send command/address info.
69296f2e892SBill Paul 	 */
69396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
69496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
69596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
69696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
69796f2e892SBill Paul 
69896f2e892SBill Paul #ifdef notdef
69996f2e892SBill Paul 	/* Idle bit */
70096f2e892SBill Paul 	dc_mii_writebit(sc, 1);
70196f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70296f2e892SBill Paul #endif
70396f2e892SBill Paul 
7040934f18aSMaxime Henrion 	/* Check for ack. */
70596f2e892SBill Paul 	ack = dc_mii_readbit(sc);
70696f2e892SBill Paul 
70796f2e892SBill Paul 	/*
70896f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
70996f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
71096f2e892SBill Paul 	 */
71196f2e892SBill Paul 	if (ack) {
7120934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
71396f2e892SBill Paul 			dc_mii_readbit(sc);
71496f2e892SBill Paul 		goto fail;
71596f2e892SBill Paul 	}
71696f2e892SBill Paul 
71796f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
71896f2e892SBill Paul 		if (!ack) {
71996f2e892SBill Paul 			if (dc_mii_readbit(sc))
72096f2e892SBill Paul 				frame->mii_data |= i;
72196f2e892SBill Paul 		}
72296f2e892SBill Paul 	}
72396f2e892SBill Paul 
72496f2e892SBill Paul fail:
72596f2e892SBill Paul 
72696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72896f2e892SBill Paul 
729d1ce9105SBill Paul 	DC_UNLOCK(sc);
73096f2e892SBill Paul 
73196f2e892SBill Paul 	if (ack)
73296f2e892SBill Paul 		return (1);
73396f2e892SBill Paul 	return (0);
73496f2e892SBill Paul }
73596f2e892SBill Paul 
73696f2e892SBill Paul /*
73796f2e892SBill Paul  * Write to a PHY register through the MII.
73896f2e892SBill Paul  */
739e3d2833aSAlfred Perlstein static int
7400934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
74196f2e892SBill Paul {
7420934f18aSMaxime Henrion 
743d1ce9105SBill Paul 	DC_LOCK(sc);
74496f2e892SBill Paul 	/*
74596f2e892SBill Paul 	 * Set up frame for TX.
74696f2e892SBill Paul 	 */
74796f2e892SBill Paul 
74896f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
74996f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
75096f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
75196f2e892SBill Paul 
75296f2e892SBill Paul 	/*
75396f2e892SBill Paul 	 * Sync the PHYs.
75496f2e892SBill Paul 	 */
75596f2e892SBill Paul 	dc_mii_sync(sc);
75696f2e892SBill Paul 
75796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
75896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
75996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
76096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
76196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
76296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
76396f2e892SBill Paul 
76496f2e892SBill Paul 	/* Idle bit. */
76596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
76696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
76796f2e892SBill Paul 
768d1ce9105SBill Paul 	DC_UNLOCK(sc);
76996f2e892SBill Paul 
77096f2e892SBill Paul 	return (0);
77196f2e892SBill Paul }
77296f2e892SBill Paul 
773e3d2833aSAlfred Perlstein static int
7740934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
77596f2e892SBill Paul {
77696f2e892SBill Paul 	struct dc_mii_frame frame;
77796f2e892SBill Paul 	struct dc_softc	 *sc;
778c85c4667SBill Paul 	int i, rval, phy_reg = 0;
77996f2e892SBill Paul 
78096f2e892SBill Paul 	sc = device_get_softc(dev);
7810934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
78296f2e892SBill Paul 
78396f2e892SBill Paul 	/*
78496f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
78596f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
78696f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
78796f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
78896f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
78996f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
79096f2e892SBill Paul 	 * that the PHY is at MII address 1.
79196f2e892SBill Paul 	 */
79296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
79396f2e892SBill Paul 		return (0);
79496f2e892SBill Paul 
7951af8bec7SBill Paul 	/*
7961af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7971af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7981af8bec7SBill Paul 	 * so we only respond to correct one.
7991af8bec7SBill Paul 	 */
8001af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
8011af8bec7SBill Paul 		return (0);
8021af8bec7SBill Paul 
8035c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
80496f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
80596f2e892SBill Paul 			switch (reg) {
80696f2e892SBill Paul 			case MII_BMSR:
80796f2e892SBill Paul 			/*
80896f2e892SBill Paul 			 * Fake something to make the probe
80996f2e892SBill Paul 			 * code think there's a PHY here.
81096f2e892SBill Paul 			 */
81196f2e892SBill Paul 				return (BMSR_MEDIAMASK);
81296f2e892SBill Paul 				break;
81396f2e892SBill Paul 			case MII_PHYIDR1:
81496f2e892SBill Paul 				if (DC_IS_PNIC(sc))
81596f2e892SBill Paul 					return (DC_VENDORID_LO);
81696f2e892SBill Paul 				return (DC_VENDORID_DEC);
81796f2e892SBill Paul 				break;
81896f2e892SBill Paul 			case MII_PHYIDR2:
81996f2e892SBill Paul 				if (DC_IS_PNIC(sc))
82096f2e892SBill Paul 					return (DC_DEVICEID_82C168);
82196f2e892SBill Paul 				return (DC_DEVICEID_21143);
82296f2e892SBill Paul 				break;
82396f2e892SBill Paul 			default:
82496f2e892SBill Paul 				return (0);
82596f2e892SBill Paul 				break;
82696f2e892SBill Paul 			}
82796f2e892SBill Paul 		} else
82896f2e892SBill Paul 			return (0);
82996f2e892SBill Paul 	}
83096f2e892SBill Paul 
83196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
83296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
83396f2e892SBill Paul 		    (phy << 23) | (reg << 18));
83496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
83596f2e892SBill Paul 			DELAY(1);
83696f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
83796f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
83896f2e892SBill Paul 				rval &= 0xFFFF;
83996f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
84096f2e892SBill Paul 			}
84196f2e892SBill Paul 		}
84296f2e892SBill Paul 		return (0);
84396f2e892SBill Paul 	}
84496f2e892SBill Paul 
84596f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
84696f2e892SBill Paul 		switch (reg) {
84796f2e892SBill Paul 		case MII_BMCR:
84896f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
84996f2e892SBill Paul 			break;
85096f2e892SBill Paul 		case MII_BMSR:
85196f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
85296f2e892SBill Paul 			break;
85396f2e892SBill Paul 		case MII_PHYIDR1:
85496f2e892SBill Paul 			phy_reg = DC_AL_VENID;
85596f2e892SBill Paul 			break;
85696f2e892SBill Paul 		case MII_PHYIDR2:
85796f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
85896f2e892SBill Paul 			break;
85996f2e892SBill Paul 		case MII_ANAR:
86096f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
86196f2e892SBill Paul 			break;
86296f2e892SBill Paul 		case MII_ANLPAR:
86396f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
86496f2e892SBill Paul 			break;
86596f2e892SBill Paul 		case MII_ANER:
86696f2e892SBill Paul 			phy_reg = DC_AL_ANER;
86796f2e892SBill Paul 			break;
86896f2e892SBill Paul 		default:
86996f2e892SBill Paul 			printf("dc%d: phy_read: bad phy register %x\n",
87096f2e892SBill Paul 			    sc->dc_unit, reg);
87196f2e892SBill Paul 			return (0);
87296f2e892SBill Paul 			break;
87396f2e892SBill Paul 		}
87496f2e892SBill Paul 
87596f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
87696f2e892SBill Paul 
87796f2e892SBill Paul 		if (rval == 0xFFFF)
87896f2e892SBill Paul 			return (0);
87996f2e892SBill Paul 		return (rval);
88096f2e892SBill Paul 	}
88196f2e892SBill Paul 
88296f2e892SBill Paul 	frame.mii_phyaddr = phy;
88396f2e892SBill Paul 	frame.mii_regaddr = reg;
884419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
885f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
886f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
887419146d9SBill Paul 	}
88896f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
889419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
890f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
89196f2e892SBill Paul 
89296f2e892SBill Paul 	return (frame.mii_data);
89396f2e892SBill Paul }
89496f2e892SBill Paul 
895e3d2833aSAlfred Perlstein static int
8960934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
89796f2e892SBill Paul {
89896f2e892SBill Paul 	struct dc_softc *sc;
89996f2e892SBill Paul 	struct dc_mii_frame frame;
900c85c4667SBill Paul 	int i, phy_reg = 0;
90196f2e892SBill Paul 
90296f2e892SBill Paul 	sc = device_get_softc(dev);
9030934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
90496f2e892SBill Paul 
90596f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
90696f2e892SBill Paul 		return (0);
90796f2e892SBill Paul 
9081af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
9091af8bec7SBill Paul 		return (0);
9101af8bec7SBill Paul 
91196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
91296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
91396f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
91496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
91596f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
91696f2e892SBill Paul 				break;
91796f2e892SBill Paul 		}
91896f2e892SBill Paul 		return (0);
91996f2e892SBill Paul 	}
92096f2e892SBill Paul 
92196f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
92296f2e892SBill Paul 		switch (reg) {
92396f2e892SBill Paul 		case MII_BMCR:
92496f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
92596f2e892SBill Paul 			break;
92696f2e892SBill Paul 		case MII_BMSR:
92796f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
92896f2e892SBill Paul 			break;
92996f2e892SBill Paul 		case MII_PHYIDR1:
93096f2e892SBill Paul 			phy_reg = DC_AL_VENID;
93196f2e892SBill Paul 			break;
93296f2e892SBill Paul 		case MII_PHYIDR2:
93396f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
93496f2e892SBill Paul 			break;
93596f2e892SBill Paul 		case MII_ANAR:
93696f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
93796f2e892SBill Paul 			break;
93896f2e892SBill Paul 		case MII_ANLPAR:
93996f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
94096f2e892SBill Paul 			break;
94196f2e892SBill Paul 		case MII_ANER:
94296f2e892SBill Paul 			phy_reg = DC_AL_ANER;
94396f2e892SBill Paul 			break;
94496f2e892SBill Paul 		default:
94596f2e892SBill Paul 			printf("dc%d: phy_write: bad phy register %x\n",
94696f2e892SBill Paul 			    sc->dc_unit, reg);
94796f2e892SBill Paul 			return (0);
94896f2e892SBill Paul 			break;
94996f2e892SBill Paul 		}
95096f2e892SBill Paul 
95196f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
95296f2e892SBill Paul 		return (0);
95396f2e892SBill Paul 	}
95496f2e892SBill Paul 
95596f2e892SBill Paul 	frame.mii_phyaddr = phy;
95696f2e892SBill Paul 	frame.mii_regaddr = reg;
95796f2e892SBill Paul 	frame.mii_data = data;
95896f2e892SBill Paul 
959419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
960f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
961f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
962419146d9SBill Paul 	}
96396f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
964419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
965f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
96696f2e892SBill Paul 
96796f2e892SBill Paul 	return (0);
96896f2e892SBill Paul }
96996f2e892SBill Paul 
970e3d2833aSAlfred Perlstein static void
9710934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
97296f2e892SBill Paul {
97396f2e892SBill Paul 	struct dc_softc *sc;
97496f2e892SBill Paul 	struct mii_data *mii;
975f43d9309SBill Paul 	struct ifmedia *ifm;
97696f2e892SBill Paul 
97796f2e892SBill Paul 	sc = device_get_softc(dev);
97896f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
97996f2e892SBill Paul 		return;
9805c1cfac4SBill Paul 
98196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
982f43d9309SBill Paul 	ifm = &mii->mii_media;
983f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
98445521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
985f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
986f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
987f43d9309SBill Paul 	} else {
98896f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
98996f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
990f43d9309SBill Paul 	}
991f43d9309SBill Paul }
992f43d9309SBill Paul 
993f43d9309SBill Paul /*
994f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
995f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
996f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
997f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
998f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
999f43d9309SBill Paul  * with it itself. *sigh*
1000f43d9309SBill Paul  */
1001e3d2833aSAlfred Perlstein static void
10020934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
1003f43d9309SBill Paul {
1004f43d9309SBill Paul 	struct dc_softc *sc;
1005f43d9309SBill Paul 	struct mii_data *mii;
1006f43d9309SBill Paul 	struct ifmedia *ifm;
1007f43d9309SBill Paul 	int rev;
1008f43d9309SBill Paul 
1009f43d9309SBill Paul 	rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
1010f43d9309SBill Paul 
1011f43d9309SBill Paul 	sc = device_get_softc(dev);
1012f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1013f43d9309SBill Paul 	ifm = &mii->mii_media;
1014f43d9309SBill Paul 
1015f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
101645521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
101796f2e892SBill Paul }
101896f2e892SBill Paul 
101996f2e892SBill Paul #define DC_POLY		0xEDB88320
102079d11e09SBill Paul #define DC_BITS_512	9
102179d11e09SBill Paul #define DC_BITS_128	7
102279d11e09SBill Paul #define DC_BITS_64	6
102396f2e892SBill Paul 
1024e3d2833aSAlfred Perlstein static u_int32_t
10250934f18aSMaxime Henrion dc_crc_le(struct dc_softc *sc, caddr_t addr)
102696f2e892SBill Paul {
102796f2e892SBill Paul 	u_int32_t idx, bit, data, crc;
102896f2e892SBill Paul 
102996f2e892SBill Paul 	/* Compute CRC for the address value. */
103096f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
103196f2e892SBill Paul 
103296f2e892SBill Paul 	for (idx = 0; idx < 6; idx++) {
103396f2e892SBill Paul 		for (data = *addr++, bit = 0; bit < 8; bit++, data >>= 1)
103496f2e892SBill Paul 			crc = (crc >> 1) ^ (((crc ^ data) & 1) ? DC_POLY : 0);
103596f2e892SBill Paul 	}
103696f2e892SBill Paul 
103779d11e09SBill Paul 	/*
103879d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
103979d11e09SBill Paul 	 * chips is only 128 bits wide.
104079d11e09SBill Paul 	 */
104179d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
104279d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
104396f2e892SBill Paul 
104479d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
104579d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
104679d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
104779d11e09SBill Paul 
1048feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1049feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1050feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1051feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10520934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1053feb78939SJonathan Chen 		else
10540934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10550934f18aSMaxime Henrion 			    (12 << 4));
1056feb78939SJonathan Chen 	}
1057feb78939SJonathan Chen 
105879d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
105996f2e892SBill Paul }
106096f2e892SBill Paul 
106196f2e892SBill Paul /*
106296f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
106396f2e892SBill Paul  */
1064e3d2833aSAlfred Perlstein static u_int32_t
10650934f18aSMaxime Henrion dc_crc_be(caddr_t addr)
106696f2e892SBill Paul {
106796f2e892SBill Paul 	u_int32_t crc, carry;
106896f2e892SBill Paul 	int i, j;
106996f2e892SBill Paul 	u_int8_t c;
107096f2e892SBill Paul 
107196f2e892SBill Paul 	/* Compute CRC for the address value. */
107296f2e892SBill Paul 	crc = 0xFFFFFFFF; /* initial value */
107396f2e892SBill Paul 
107496f2e892SBill Paul 	for (i = 0; i < 6; i++) {
107596f2e892SBill Paul 		c = *(addr + i);
107696f2e892SBill Paul 		for (j = 0; j < 8; j++) {
107796f2e892SBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
107896f2e892SBill Paul 			crc <<= 1;
107996f2e892SBill Paul 			c >>= 1;
108096f2e892SBill Paul 			if (carry)
108196f2e892SBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
108296f2e892SBill Paul 		}
108396f2e892SBill Paul 	}
108496f2e892SBill Paul 
10850934f18aSMaxime Henrion 	/* Return the filter bit position. */
108696f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
108796f2e892SBill Paul }
108896f2e892SBill Paul 
108996f2e892SBill Paul /*
109096f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
109196f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
109296f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
109396f2e892SBill Paul  *
109496f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
109596f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
109696f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
109796f2e892SBill Paul  * we need that too.
109896f2e892SBill Paul  */
10992c876e15SPoul-Henning Kamp static void
11000934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
110196f2e892SBill Paul {
110296f2e892SBill Paul 	struct dc_desc *sframe;
110396f2e892SBill Paul 	u_int32_t h, *sp;
110496f2e892SBill Paul 	struct ifmultiaddr *ifma;
110596f2e892SBill Paul 	struct ifnet *ifp;
110696f2e892SBill Paul 	int i;
110796f2e892SBill Paul 
110896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
110996f2e892SBill Paul 
111096f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
111196f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
111296f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
111396f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
111456e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
11150934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
111696f2e892SBill Paul 
1117af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1118af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1119af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
112096f2e892SBill Paul 
112156e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
112296f2e892SBill Paul 
112396f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
112496f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
112596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112696f2e892SBill Paul 	else
112796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
112896f2e892SBill Paul 
112996f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
113096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
113196f2e892SBill Paul 	else
113296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
113396f2e892SBill Paul 
11346817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
113596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
113696f2e892SBill Paul 			continue;
113796f2e892SBill Paul 		h = dc_crc_le(sc,
113896f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1139af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
114096f2e892SBill Paul 	}
114196f2e892SBill Paul 
114296f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1143868d8b62SMatthew N. Dodd 		h = dc_crc_le(sc, (caddr_t)ifp->if_broadcastaddr);
1144af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
114596f2e892SBill Paul 	}
114696f2e892SBill Paul 
114796f2e892SBill Paul 	/* Set our MAC address */
1148af4358c7SMaxime Henrion 	sp[39] = DC_SP_MAC(((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1149af4358c7SMaxime Henrion 	sp[40] = DC_SP_MAC(((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1150af4358c7SMaxime Henrion 	sp[41] = DC_SP_MAC(((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
115196f2e892SBill Paul 
1152af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
115396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
115496f2e892SBill Paul 
115596f2e892SBill Paul 	/*
115696f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
115796f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
115896f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
115996f2e892SBill Paul 	 * medicine.
116096f2e892SBill Paul 	 */
116196f2e892SBill Paul 	DELAY(10000);
116296f2e892SBill Paul 
116396f2e892SBill Paul 	ifp->if_timer = 5;
116496f2e892SBill Paul }
116596f2e892SBill Paul 
11662c876e15SPoul-Henning Kamp static void
11670934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
116896f2e892SBill Paul {
116996f2e892SBill Paul 	struct ifnet *ifp;
11700934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
117196f2e892SBill Paul 	int h = 0;
117296f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
117396f2e892SBill Paul 
117496f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
117596f2e892SBill Paul 
11760934f18aSMaxime Henrion 	/* Init our MAC address. */
117796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR0, *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
117896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_PAR1, *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
117996f2e892SBill Paul 
118096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
118196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
118296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
118396f2e892SBill Paul 	else
118496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
118596f2e892SBill Paul 
118696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
118796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
118896f2e892SBill Paul 	else
118996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
119096f2e892SBill Paul 
11910934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
119296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
119396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
119496f2e892SBill Paul 
119596f2e892SBill Paul 	/*
119696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
119796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
119896f2e892SBill Paul 	 */
119996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
120096f2e892SBill Paul 		return;
120196f2e892SBill Paul 
12020934f18aSMaxime Henrion 	/* Now program new ones. */
12036817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
120496f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
120596f2e892SBill Paul 			continue;
1206acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1207acc1bcccSMartin Blapp 			h = dc_crc_le(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1208acc1bcccSMartin Blapp 		else
120996f2e892SBill Paul 			h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
121096f2e892SBill Paul 		if (h < 32)
121196f2e892SBill Paul 			hashes[0] |= (1 << h);
121296f2e892SBill Paul 		else
121396f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
121496f2e892SBill Paul 	}
121596f2e892SBill Paul 
121696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
121796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
121896f2e892SBill Paul }
121996f2e892SBill Paul 
12202c876e15SPoul-Henning Kamp static void
12210934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
122296f2e892SBill Paul {
122396f2e892SBill Paul 	struct ifnet *ifp;
12240934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
122596f2e892SBill Paul 	int h = 0;
122696f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
122796f2e892SBill Paul 
122896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
122996f2e892SBill Paul 
123096f2e892SBill Paul 	/* Init our MAC address */
123196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
123296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
123396f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[0]));
123496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
123596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA,
123696f2e892SBill Paul 	    *(u_int32_t *)(&sc->arpcom.ac_enaddr[4]));
123796f2e892SBill Paul 
123896f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
123996f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
124096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
124196f2e892SBill Paul 	else
124296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
124396f2e892SBill Paul 
124496f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
124596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
124696f2e892SBill Paul 	else
124796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
124896f2e892SBill Paul 
124996f2e892SBill Paul 	/*
125096f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
125196f2e892SBill Paul 	 * of broadcast frames.
125296f2e892SBill Paul 	 */
125396f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
125496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
125596f2e892SBill Paul 	else
125696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
125796f2e892SBill Paul 
125896f2e892SBill Paul 	/* first, zot all the existing hash bits */
125996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
126096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
126196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
126296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
126396f2e892SBill Paul 
126496f2e892SBill Paul 	/*
126596f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
126696f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
126796f2e892SBill Paul 	 */
126896f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
126996f2e892SBill Paul 		return;
127096f2e892SBill Paul 
127196f2e892SBill Paul 	/* now program new ones */
12726817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
127396f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
127496f2e892SBill Paul 			continue;
127596f2e892SBill Paul 		h = dc_crc_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
127696f2e892SBill Paul 		if (h < 32)
127796f2e892SBill Paul 			hashes[0] |= (1 << h);
127896f2e892SBill Paul 		else
127996f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
128096f2e892SBill Paul 	}
128196f2e892SBill Paul 
128296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
128396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
128496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
128596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
128696f2e892SBill Paul }
128796f2e892SBill Paul 
12882c876e15SPoul-Henning Kamp static void
12890934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1290feb78939SJonathan Chen {
12910934f18aSMaxime Henrion 	struct ifnet *ifp;
12920934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1293feb78939SJonathan Chen 	struct dc_desc *sframe;
1294feb78939SJonathan Chen 	u_int32_t h, *sp;
1295feb78939SJonathan Chen 	int i;
1296feb78939SJonathan Chen 
1297feb78939SJonathan Chen 	ifp = &sc->arpcom.ac_if;
1298feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1299feb78939SJonathan Chen 
1300feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1301feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1302feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1303feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
130456e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
13050934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1306feb78939SJonathan Chen 
1307af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1308af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1309af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1310feb78939SJonathan Chen 
131156e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1312feb78939SJonathan Chen 
1313feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1314feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1315feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1316feb78939SJonathan Chen 	else
1317feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1318feb78939SJonathan Chen 
1319feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1320feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1321feb78939SJonathan Chen 	else
1322feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1323feb78939SJonathan Chen 
13246817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1325feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1326feb78939SJonathan Chen 			continue;
13271d5e5310SBill Paul 		h = dc_crc_le(sc,
13281d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1329af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1330feb78939SJonathan Chen 	}
1331feb78939SJonathan Chen 
1332feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1333868d8b62SMatthew N. Dodd 		h = dc_crc_le(sc, (caddr_t)ifp->if_broadcastaddr);
1334af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1335feb78939SJonathan Chen 	}
1336feb78939SJonathan Chen 
1337feb78939SJonathan Chen 	/* Set our MAC address */
1338af4358c7SMaxime Henrion 	sp[0] = DC_SP_MAC(((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1339af4358c7SMaxime Henrion 	sp[1] = DC_SP_MAC(((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1340af4358c7SMaxime Henrion 	sp[2] = DC_SP_MAC(((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1341feb78939SJonathan Chen 
1342feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1343feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1344feb78939SJonathan Chen 	ifp->if_flags |= IFF_RUNNING;
1345af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
1346feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1347feb78939SJonathan Chen 
1348feb78939SJonathan Chen 	/*
13490934f18aSMaxime Henrion 	 * Wait some time...
1350feb78939SJonathan Chen 	 */
1351feb78939SJonathan Chen 	DELAY(1000);
1352feb78939SJonathan Chen 
1353feb78939SJonathan Chen 	ifp->if_timer = 5;
1354feb78939SJonathan Chen }
1355feb78939SJonathan Chen 
1356e3d2833aSAlfred Perlstein static void
13570934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
135896f2e892SBill Paul {
13590934f18aSMaxime Henrion 
136096f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13611af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
136296f2e892SBill Paul 		dc_setfilt_21143(sc);
136396f2e892SBill Paul 
136496f2e892SBill Paul 	if (DC_IS_ASIX(sc))
136596f2e892SBill Paul 		dc_setfilt_asix(sc);
136696f2e892SBill Paul 
136796f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
136896f2e892SBill Paul 		dc_setfilt_admtek(sc);
136996f2e892SBill Paul 
1370feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1371feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
137296f2e892SBill Paul }
137396f2e892SBill Paul 
137496f2e892SBill Paul /*
13750934f18aSMaxime Henrion  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13760934f18aSMaxime Henrion  * the netconfig register, we first have to put the transmit and/or
13770934f18aSMaxime Henrion  * receive logic in the idle state.
137896f2e892SBill Paul  */
1379e3d2833aSAlfred Perlstein static void
13800934f18aSMaxime Henrion dc_setcfg(struct dc_softc *sc, int media)
138196f2e892SBill Paul {
13820934f18aSMaxime Henrion 	int i, restart = 0, watchdogreg;
138396f2e892SBill Paul 	u_int32_t isr;
138496f2e892SBill Paul 
138596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
138696f2e892SBill Paul 		return;
138796f2e892SBill Paul 
138896f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
138996f2e892SBill Paul 		restart = 1;
139096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
139196f2e892SBill Paul 
139296f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
139396f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1394d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1395351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1396351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
139796f2e892SBill Paul 				break;
1398d467c136SBill Paul 			DELAY(10);
139996f2e892SBill Paul 		}
140096f2e892SBill Paul 
140196f2e892SBill Paul 		if (i == DC_TIMEOUT)
140296f2e892SBill Paul 			printf("dc%d: failed to force tx and "
140396f2e892SBill Paul 				"rx to idle state\n", sc->dc_unit);
140496f2e892SBill Paul 	}
140596f2e892SBill Paul 
140696f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1407042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1408042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
140996f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1410bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14110934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14128273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14138273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14148273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14154c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1416bf645417SBill Paul 			} else {
1417bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1418bf645417SBill Paul 			}
141996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142096f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
142196f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
142296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142396f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
142488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1427e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1428e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
142996f2e892SBill Paul 		} else {
143096f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
143196f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
143296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
143396f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
143496f2e892SBill Paul 			}
1435318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1436318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1437318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14385c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14395c1cfac4SBill Paul 				dc_apply_fixup(sc,
14405c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14415c1cfac4SBill Paul 				    IFM_100_TX | IFM_FDX : IFM_100_TX);
144296f2e892SBill Paul 		}
144396f2e892SBill Paul 	}
144496f2e892SBill Paul 
144596f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1446042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1447042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
144896f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14490934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14504c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14518273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14528273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14538273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14548273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14554c2efe27SBill Paul 			} else {
14564c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14574c2efe27SBill Paul 			}
145896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
145996f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
146096f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
146196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146288d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
146396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
146496f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1465e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1466e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
146796f2e892SBill Paul 		} else {
146896f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
146996f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
147096f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
147196f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
147296f2e892SBill Paul 			}
147396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1474318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
147596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14765c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14775c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14785c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14795c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14805c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14815c1cfac4SBill Paul 				else
14825c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14835c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14845c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14855c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14865c1cfac4SBill Paul 				dc_apply_fixup(sc,
14875c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14885c1cfac4SBill Paul 				    IFM_10_T | IFM_FDX : IFM_10_T);
14895c1cfac4SBill Paul 				DELAY(20000);
14905c1cfac4SBill Paul 			}
149196f2e892SBill Paul 		}
149296f2e892SBill Paul 	}
149396f2e892SBill Paul 
1494f43d9309SBill Paul 	/*
1495f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1496f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1497f43d9309SBill Paul 	 * on the external MII port.
1498f43d9309SBill Paul 	 */
1499f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
150045521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1501f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1502f43d9309SBill Paul 			sc->dc_link = 1;
1503f43d9309SBill Paul 		} else {
1504f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1505f43d9309SBill Paul 		}
1506f43d9309SBill Paul 	}
1507f43d9309SBill Paul 
150896f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
150996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
151096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
151196f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
151296f2e892SBill Paul 	} else {
151396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
151496f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
151596f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
151696f2e892SBill Paul 	}
151796f2e892SBill Paul 
151896f2e892SBill Paul 	if (restart)
151996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
152096f2e892SBill Paul }
152196f2e892SBill Paul 
1522e3d2833aSAlfred Perlstein static void
15230934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
152496f2e892SBill Paul {
15250934f18aSMaxime Henrion 	int i;
152696f2e892SBill Paul 
152796f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152896f2e892SBill Paul 
152996f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
153096f2e892SBill Paul 		DELAY(10);
153196f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
153296f2e892SBill Paul 			break;
153396f2e892SBill Paul 	}
153496f2e892SBill Paul 
15351af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15361d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
153796f2e892SBill Paul 		DELAY(10000);
153896f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
153996f2e892SBill Paul 		i = 0;
154096f2e892SBill Paul 	}
154196f2e892SBill Paul 
154296f2e892SBill Paul 	if (i == DC_TIMEOUT)
154396f2e892SBill Paul 		printf("dc%d: reset never completed!\n", sc->dc_unit);
154496f2e892SBill Paul 
154596f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
154696f2e892SBill Paul 	DELAY(1000);
154796f2e892SBill Paul 
154896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
154996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
155096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
155196f2e892SBill Paul 
155291cc2adbSBill Paul 	/*
155391cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
155491cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
155591cc2adbSBill Paul 	 * into a state where it will never come out of reset
155691cc2adbSBill Paul 	 * until we reset the whole chip again.
155791cc2adbSBill Paul 	 */
15585c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
155991cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15605c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15615c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15625c1cfac4SBill Paul 	}
156396f2e892SBill Paul }
156496f2e892SBill Paul 
1565e3d2833aSAlfred Perlstein static struct dc_type *
15660934f18aSMaxime Henrion dc_devtype(device_t dev)
156796f2e892SBill Paul {
156896f2e892SBill Paul 	struct dc_type *t;
156996f2e892SBill Paul 	u_int32_t rev;
157096f2e892SBill Paul 
157196f2e892SBill Paul 	t = dc_devs;
157296f2e892SBill Paul 
157396f2e892SBill Paul 	while (t->dc_name != NULL) {
157496f2e892SBill Paul 		if ((pci_get_vendor(dev) == t->dc_vid) &&
157596f2e892SBill Paul 		    (pci_get_device(dev) == t->dc_did)) {
157696f2e892SBill Paul 			/* Check the PCI revision */
157796f2e892SBill Paul 			rev = pci_read_config(dev, DC_PCI_CFRV, 4) & 0xFF;
157896f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713 &&
157996f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
158096f2e892SBill Paul 				t++;
158196f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_98713_CP &&
158296f2e892SBill Paul 			    rev >= DC_REVISION_98713A)
158396f2e892SBill Paul 				t++;
158496f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
158579d11e09SBill Paul 			    rev >= DC_REVISION_98715AEC_C)
158679d11e09SBill Paul 				t++;
158779d11e09SBill Paul 			if (t->dc_did == DC_DEVICEID_987x5 &&
158896f2e892SBill Paul 			    rev >= DC_REVISION_98725)
158996f2e892SBill Paul 				t++;
159096f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_AX88140A &&
159196f2e892SBill Paul 			    rev >= DC_REVISION_88141)
159296f2e892SBill Paul 				t++;
159396f2e892SBill Paul 			if (t->dc_did == DC_DEVICEID_82C168 &&
159496f2e892SBill Paul 			    rev >= DC_REVISION_82C169)
159596f2e892SBill Paul 				t++;
159688d739dcSBill Paul 			if (t->dc_did == DC_DEVICEID_DM9102 &&
159788d739dcSBill Paul 			    rev >= DC_REVISION_DM9102A)
159888d739dcSBill Paul 				t++;
1599e7b9ab3aSBill Paul 			/*
1600e7b9ab3aSBill Paul 			 * The Microsoft MN-130 has a device ID of 0x0002,
1601e7b9ab3aSBill Paul 			 * which happens to be the same as the PNIC 82c168.
1602e7b9ab3aSBill Paul 			 * To keep dc_attach() from getting confused, we
1603e7b9ab3aSBill Paul 			 * pretend its ID is something different.
1604e7b9ab3aSBill Paul 			 * XXX: ideally, dc_attach() should be checking
1605e7b9ab3aSBill Paul 			 * vendorid+deviceid together to avoid such
1606e7b9ab3aSBill Paul 			 * collisions.
1607e7b9ab3aSBill Paul 			 */
1608e7b9ab3aSBill Paul 			if (t->dc_vid == DC_VENDORID_MICROSOFT &&
1609e7b9ab3aSBill Paul 			    t->dc_did == DC_DEVICEID_MSMN130)
1610e7b9ab3aSBill Paul 				t++;
161196f2e892SBill Paul 			return (t);
161296f2e892SBill Paul 		}
161396f2e892SBill Paul 		t++;
161496f2e892SBill Paul 	}
161596f2e892SBill Paul 
161696f2e892SBill Paul 	return (NULL);
161796f2e892SBill Paul }
161896f2e892SBill Paul 
161996f2e892SBill Paul /*
162096f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
162196f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
162296f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
162396f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
162496f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
162596f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
162696f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
162796f2e892SBill Paul  */
1628e3d2833aSAlfred Perlstein static int
16290934f18aSMaxime Henrion dc_probe(device_t dev)
163096f2e892SBill Paul {
163196f2e892SBill Paul 	struct dc_type *t;
163296f2e892SBill Paul 
163396f2e892SBill Paul 	t = dc_devtype(dev);
163496f2e892SBill Paul 
163596f2e892SBill Paul 	if (t != NULL) {
163696f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
163796f2e892SBill Paul 		return (0);
163896f2e892SBill Paul 	}
163996f2e892SBill Paul 
164096f2e892SBill Paul 	return (ENXIO);
164196f2e892SBill Paul }
164296f2e892SBill Paul 
1643b84e866aSWarner Losh #ifndef BURN_BRIDGES
1644e3d2833aSAlfred Perlstein static void
16450934f18aSMaxime Henrion dc_acpi(device_t dev)
164696f2e892SBill Paul {
164796f2e892SBill Paul 	int unit;
16480934f18aSMaxime Henrion 	u_int32_t iobase, membase, irq;
164996f2e892SBill Paul 
165096f2e892SBill Paul 	unit = device_get_unit(dev);
165196f2e892SBill Paul 
165214a00c6cSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
165396f2e892SBill Paul 		/* Save important PCI config data. */
165496f2e892SBill Paul 		iobase = pci_read_config(dev, DC_PCI_CFBIO, 4);
165596f2e892SBill Paul 		membase = pci_read_config(dev, DC_PCI_CFBMA, 4);
165696f2e892SBill Paul 		irq = pci_read_config(dev, DC_PCI_CFIT, 4);
165796f2e892SBill Paul 
165896f2e892SBill Paul 		/* Reset the power state. */
165996f2e892SBill Paul 		printf("dc%d: chip is in D%d power mode "
166014a00c6cSBill Paul 		    "-- setting to D0\n", unit,
166114a00c6cSBill Paul 		    pci_get_powerstate(dev));
166214a00c6cSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
166396f2e892SBill Paul 
166496f2e892SBill Paul 		/* Restore PCI config data. */
166596f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBIO, iobase, 4);
166696f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFBMA, membase, 4);
166796f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFIT, irq, 4);
166896f2e892SBill Paul 	}
166996f2e892SBill Paul }
1670b84e866aSWarner Losh #endif
167196f2e892SBill Paul 
1672e3d2833aSAlfred Perlstein static void
16730934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16745c1cfac4SBill Paul {
16755c1cfac4SBill Paul 	struct dc_mediainfo *m;
16765c1cfac4SBill Paul 	u_int8_t *p;
16775c1cfac4SBill Paul 	int i;
16785d801891SBill Paul 	u_int32_t reg;
16795c1cfac4SBill Paul 
16805c1cfac4SBill Paul 	m = sc->dc_mi;
16815c1cfac4SBill Paul 
16825c1cfac4SBill Paul 	while (m != NULL) {
16835c1cfac4SBill Paul 		if (m->dc_media == media)
16845c1cfac4SBill Paul 			break;
16855c1cfac4SBill Paul 		m = m->dc_next;
16865c1cfac4SBill Paul 	}
16875c1cfac4SBill Paul 
16885c1cfac4SBill Paul 	if (m == NULL)
16895c1cfac4SBill Paul 		return;
16905c1cfac4SBill Paul 
16915c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16925c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16935c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16945c1cfac4SBill Paul 	}
16955c1cfac4SBill Paul 
16965c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16975c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16985c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16995c1cfac4SBill Paul 	}
17005c1cfac4SBill Paul }
17015c1cfac4SBill Paul 
1702e3d2833aSAlfred Perlstein static void
17030934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
17045c1cfac4SBill Paul {
17055c1cfac4SBill Paul 	struct dc_mediainfo *m;
17065c1cfac4SBill Paul 
17070934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17085c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT)
17095c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
17105c1cfac4SBill Paul 
17115c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10BT_FDX)
17125c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
17135c1cfac4SBill Paul 
17145c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B2)
17155c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
17165c1cfac4SBill Paul 
17175c1cfac4SBill Paul 	if (l->dc_sia_code == DC_SIA_CODE_10B5)
17185c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
17195c1cfac4SBill Paul 
17205c1cfac4SBill Paul 	m->dc_gp_len = 2;
17215c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sia_gpio_ctl;
17225c1cfac4SBill Paul 
17235c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17245c1cfac4SBill Paul 	sc->dc_mi = m;
17255c1cfac4SBill Paul 
17265c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
17275c1cfac4SBill Paul }
17285c1cfac4SBill Paul 
1729e3d2833aSAlfred Perlstein static void
17300934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
17315c1cfac4SBill Paul {
17325c1cfac4SBill Paul 	struct dc_mediainfo *m;
17335c1cfac4SBill Paul 
17340934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17355c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
17365c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
17375c1cfac4SBill Paul 
17385c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
17395c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
17405c1cfac4SBill Paul 
17415c1cfac4SBill Paul 	m->dc_gp_len = 2;
17425c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
17435c1cfac4SBill Paul 
17445c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17455c1cfac4SBill Paul 	sc->dc_mi = m;
17465c1cfac4SBill Paul 
17475c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
17485c1cfac4SBill Paul }
17495c1cfac4SBill Paul 
1750e3d2833aSAlfred Perlstein static void
17510934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17525c1cfac4SBill Paul {
17535c1cfac4SBill Paul 	struct dc_mediainfo *m;
17540934f18aSMaxime Henrion 	u_int8_t *p;
17555c1cfac4SBill Paul 
17560934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17575c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17585c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17595c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17605c1cfac4SBill Paul 
17615c1cfac4SBill Paul 	p = (u_int8_t *)l;
17625c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17635c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17645c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17655c1cfac4SBill Paul 	m->dc_reset_len = *p;
17665c1cfac4SBill Paul 	p++;
17675c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17685c1cfac4SBill Paul 
17695c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17705c1cfac4SBill Paul 	sc->dc_mi = m;
17715c1cfac4SBill Paul }
17725c1cfac4SBill Paul 
17732c876e15SPoul-Henning Kamp static void
17740934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17753097aa70SWarner Losh {
17763097aa70SWarner Losh 	int size;
17773097aa70SWarner Losh 
17783097aa70SWarner Losh 	size = 2 << bits;
17793097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
17803097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
17813097aa70SWarner Losh }
17823097aa70SWarner Losh 
1783e3d2833aSAlfred Perlstein static void
17840934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17855c1cfac4SBill Paul {
17865c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17875c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
17880934f18aSMaxime Henrion 	int have_mii, i, loff;
17895c1cfac4SBill Paul 	char *ptr;
17905c1cfac4SBill Paul 
1791f956e0b3SMartin Blapp 	have_mii = 0;
17925c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17935c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17945c1cfac4SBill Paul 
17955c1cfac4SBill Paul 	ptr = (char *)lhdr;
17965c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1797f956e0b3SMartin Blapp 	/*
1798f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1799f956e0b3SMartin Blapp 	 */
1800f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1801f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1802f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1803f956e0b3SMartin Blapp 		    have_mii++;
1804f956e0b3SMartin Blapp 
1805f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1806f956e0b3SMartin Blapp 		ptr++;
1807f956e0b3SMartin Blapp 	}
1808f956e0b3SMartin Blapp 
1809f956e0b3SMartin Blapp 	/*
1810f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1811f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1812f956e0b3SMartin Blapp 	 */
1813f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1814f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
18155c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
18165c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
18175c1cfac4SBill Paul 		switch (hdr->dc_type) {
18185c1cfac4SBill Paul 		case DC_EBLOCK_MII:
18195c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
18205c1cfac4SBill Paul 			break;
18215c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1822f956e0b3SMartin Blapp 			if (! have_mii)
1823f956e0b3SMartin Blapp 				dc_decode_leaf_sia(sc,
1824f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
18255c1cfac4SBill Paul 			break;
18265c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1827f956e0b3SMartin Blapp 			if (! have_mii)
1828f956e0b3SMartin Blapp 				dc_decode_leaf_sym(sc,
1829f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
18305c1cfac4SBill Paul 			break;
18315c1cfac4SBill Paul 		default:
18325c1cfac4SBill Paul 			/* Don't care. Yet. */
18335c1cfac4SBill Paul 			break;
18345c1cfac4SBill Paul 		}
18355c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18365c1cfac4SBill Paul 		ptr++;
18375c1cfac4SBill Paul 	}
18385c1cfac4SBill Paul }
18395c1cfac4SBill Paul 
184056e5e7aeSMaxime Henrion static void
184156e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
184256e5e7aeSMaxime Henrion {
184356e5e7aeSMaxime Henrion 	u_int32_t *paddr;
184456e5e7aeSMaxime Henrion 
184556e5e7aeSMaxime Henrion 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
184656e5e7aeSMaxime Henrion 	paddr = arg;
184756e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
184856e5e7aeSMaxime Henrion }
184956e5e7aeSMaxime Henrion 
185096f2e892SBill Paul /*
185196f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
185296f2e892SBill Paul  * setup and ethernet/BPF attach.
185396f2e892SBill Paul  */
1854e3d2833aSAlfred Perlstein static int
18550934f18aSMaxime Henrion dc_attach(device_t dev)
185696f2e892SBill Paul {
1857d1ce9105SBill Paul 	int tmp = 0;
185896f2e892SBill Paul 	u_char eaddr[ETHER_ADDR_LEN];
185996f2e892SBill Paul 	u_int32_t command;
186096f2e892SBill Paul 	struct dc_softc *sc;
186196f2e892SBill Paul 	struct ifnet *ifp;
186296f2e892SBill Paul 	u_int32_t revision;
186396f2e892SBill Paul 	int unit, error = 0, rid, mac_offset;
186456e5e7aeSMaxime Henrion 	int i;
1865e7b01d07SWarner Losh 	u_int8_t *mac;
186696f2e892SBill Paul 
186796f2e892SBill Paul 	sc = device_get_softc(dev);
186896f2e892SBill Paul 	unit = device_get_unit(dev);
186996f2e892SBill Paul 
18706008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
18716008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
1872b84e866aSWarner Losh #ifndef BURN_BRIDGES
187396f2e892SBill Paul 	/*
187496f2e892SBill Paul 	 * Handle power management nonsense.
187596f2e892SBill Paul 	 */
187696f2e892SBill Paul 	dc_acpi(dev);
1877b84e866aSWarner Losh #endif
187896f2e892SBill Paul 	/*
187996f2e892SBill Paul 	 * Map control/status registers.
188096f2e892SBill Paul 	 */
188107f65363SBill Paul 	pci_enable_busmaster(dev);
188296f2e892SBill Paul 
188396f2e892SBill Paul 	rid = DC_RID;
188496f2e892SBill Paul 	sc->dc_res = bus_alloc_resource(dev, DC_RES, &rid,
188596f2e892SBill Paul 	    0, ~0, 1, RF_ACTIVE);
188696f2e892SBill Paul 
188796f2e892SBill Paul 	if (sc->dc_res == NULL) {
188896f2e892SBill Paul 		printf("dc%d: couldn't map ports/memory\n", unit);
188996f2e892SBill Paul 		error = ENXIO;
1890608654d4SNate Lawson 		goto fail;
189196f2e892SBill Paul 	}
189296f2e892SBill Paul 
189396f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
189496f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
189596f2e892SBill Paul 
18960934f18aSMaxime Henrion 	/* Allocate interrupt. */
189754f1f1d1SNate Lawson 	rid = 0;
189854f1f1d1SNate Lawson 	sc->dc_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
189954f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
190054f1f1d1SNate Lawson 
190154f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
190254f1f1d1SNate Lawson 		printf("dc%d: couldn't map interrupt\n", unit);
190354f1f1d1SNate Lawson 		error = ENXIO;
190454f1f1d1SNate Lawson 		goto fail;
190554f1f1d1SNate Lawson 	}
190654f1f1d1SNate Lawson 
190796f2e892SBill Paul 	/* Need this info to decide on a chip type. */
190896f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
190996f2e892SBill Paul 	revision = pci_read_config(dev, DC_PCI_CFRV, 4) & 0x000000FF;
191096f2e892SBill Paul 
19116d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
1912eecb3844SMartin Blapp 	if (sc->dc_info->dc_did != DC_DEVICEID_82C168 &&
1913eecb3844SMartin Blapp 	   sc->dc_info->dc_did != DC_DEVICEID_X3201)
1914eecb3844SMartin Blapp 		dc_eeprom_width(sc);
1915eecb3844SMartin Blapp 
191696f2e892SBill Paul 	switch (sc->dc_info->dc_did) {
191796f2e892SBill Paul 	case DC_DEVICEID_21143:
191896f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
191996f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1920042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19215c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
19223097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
192396f2e892SBill Paul 		break;
192438deb45fSTom Rhodes 	case DC_DEVICEID_DM9009:
192596f2e892SBill Paul 	case DC_DEVICEID_DM9100:
192696f2e892SBill Paul 	case DC_DEVICEID_DM9102:
192796f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1928318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1929318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
19307dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
19314a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
19320a46b1dcSBill Paul 		/* Increase the latency timer value. */
19330a46b1dcSBill Paul 		command = pci_read_config(dev, DC_PCI_CFLT, 4);
19340a46b1dcSBill Paul 		command &= 0xFFFF00FF;
19350a46b1dcSBill Paul 		command |= 0x00008000;
19360a46b1dcSBill Paul 		pci_write_config(dev, DC_PCI_CFLT, command, 4);
193796f2e892SBill Paul 		break;
193896f2e892SBill Paul 	case DC_DEVICEID_AL981:
193996f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
194096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
194196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
194296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19433097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
194496f2e892SBill Paul 		break;
194596f2e892SBill Paul 	case DC_DEVICEID_AN985:
1946e351d778SMartin Blapp 	case DC_DEVICEID_ADM9511:
1947e351d778SMartin Blapp 	case DC_DEVICEID_ADM9513:
19484c16d09eSWarner Losh 	case DC_DEVICEID_FA511:
194941fced74SPeter Wemm 	case DC_DEVICEID_FE2500:
1950fa167b8eSBill Paul 	case DC_DEVICEID_EN2242:
1951948c244dSWarner Losh 	case DC_DEVICEID_HAWKING_PN672TX:
19527eac366bSMartin Blapp 	case DC_DEVICEID_3CSOHOB:
1953e7b9ab3aSBill Paul 	case DC_DEVICEID_MSMN120:
1954e7b9ab3aSBill Paul 	case DC_DEVICEID_MSMN130_FAKE: /* XXX avoid collision with PNIC*/
195596f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
1956acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
195796f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
195896f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
195996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19603097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
196196f2e892SBill Paul 		break;
196296f2e892SBill Paul 	case DC_DEVICEID_98713:
196396f2e892SBill Paul 	case DC_DEVICEID_98713_CP:
196496f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
196596f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
196696f2e892SBill Paul 		}
1967318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
196896f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1969318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1970318b02fdSBill Paul 		}
1971318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
197296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
197396f2e892SBill Paul 		break;
197496f2e892SBill Paul 	case DC_DEVICEID_987x5:
19759ca710f6SJeroen Ruigrok van der Werven 	case DC_DEVICEID_EN1217:
197679d11e09SBill Paul 		/*
197779d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
197879d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
197979d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
198079d11e09SBill Paul 		 * get the right number of bits out of the
198179d11e09SBill Paul 		 * CRC routine.
198279d11e09SBill Paul 		 */
198379d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
198479d11e09SBill Paul 		    revision < DC_REVISION_98725)
198579d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
198696f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
198796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1988318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
198996f2e892SBill Paul 		break;
1990ead7cde9SBill Paul 	case DC_DEVICEID_98727:
1991ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1992ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1993ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1994ead7cde9SBill Paul 		break;
199596f2e892SBill Paul 	case DC_DEVICEID_82C115:
199696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
199779d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
1998318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
199996f2e892SBill Paul 		break;
200096f2e892SBill Paul 	case DC_DEVICEID_82C168:
200196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
200291cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
200396f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
200496f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
200596f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
200696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
200796f2e892SBill Paul 		break;
200896f2e892SBill Paul 	case DC_DEVICEID_AX88140A:
200996f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
201096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
201196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
201296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
201396f2e892SBill Paul 		break;
2014feb78939SJonathan Chen 	case DC_DEVICEID_X3201:
2015feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
20162dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
20172dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2018feb78939SJonathan Chen 		/*
2019feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2020feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
20212dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2022feb78939SJonathan Chen 		 */
20233097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2024feb78939SJonathan Chen 		break;
20251af8bec7SBill Paul 	case DC_DEVICEID_RS7112:
20261af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
20271af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
20281af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20291af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
20303097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
20311af8bec7SBill Paul 		break;
203296f2e892SBill Paul 	default:
203396f2e892SBill Paul 		printf("dc%d: unknown device: %x\n", sc->dc_unit,
203496f2e892SBill Paul 		    sc->dc_info->dc_did);
203596f2e892SBill Paul 		break;
203696f2e892SBill Paul 	}
203796f2e892SBill Paul 
203896f2e892SBill Paul 	/* Save the cache line size. */
203988d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
204088d739dcSBill Paul 		sc->dc_cachesize = 0;
204188d739dcSBill Paul 	else
204288d739dcSBill Paul 		sc->dc_cachesize = pci_read_config(dev,
204388d739dcSBill Paul 		    DC_PCI_CFLT, 4) & 0xFF;
204496f2e892SBill Paul 
204596f2e892SBill Paul 	/* Reset the adapter. */
204696f2e892SBill Paul 	dc_reset(sc);
204796f2e892SBill Paul 
204896f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2049feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
205096f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
205196f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
205296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
205396f2e892SBill Paul 	}
205496f2e892SBill Paul 
205596f2e892SBill Paul 	/*
205696f2e892SBill Paul 	 * Try to learn something about the supported media.
205796f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
205896f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
205996f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
206096f2e892SBill Paul 	 * Intel 21143.
206196f2e892SBill Paul 	 */
20625c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
20635c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
20645c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
206596f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
206696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
206796f2e892SBill Paul 		else
206896f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
206996f2e892SBill Paul 	} else if (!sc->dc_pmode)
207096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
207196f2e892SBill Paul 
207296f2e892SBill Paul 	/*
207396f2e892SBill Paul 	 * Get station address from the EEPROM.
207496f2e892SBill Paul 	 */
207596f2e892SBill Paul 	switch(sc->dc_type) {
207696f2e892SBill Paul 	case DC_TYPE_98713:
207796f2e892SBill Paul 	case DC_TYPE_98713A:
207896f2e892SBill Paul 	case DC_TYPE_987x5:
207996f2e892SBill Paul 	case DC_TYPE_PNICII:
208096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
208196f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
208296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
208396f2e892SBill Paul 		break;
208496f2e892SBill Paul 	case DC_TYPE_PNIC:
208596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
208696f2e892SBill Paul 		break;
208796f2e892SBill Paul 	case DC_TYPE_DM9102:
208896f2e892SBill Paul 	case DC_TYPE_21143:
208996f2e892SBill Paul 	case DC_TYPE_ASIX:
209096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
209196f2e892SBill Paul 		break;
209296f2e892SBill Paul 	case DC_TYPE_AL981:
209396f2e892SBill Paul 	case DC_TYPE_AN985:
20940934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_AL_EE_NODEADDR, &eaddr,
20953097aa70SWarner Losh 		    ETHER_ADDR_LEN);
209696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_AL_EE_NODEADDR, 3, 0);
209796f2e892SBill Paul 		break;
20981af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20990934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
21000934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
21011af8bec7SBill Paul 		break;
2102feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
21030934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2104e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2105e7b01d07SWarner Losh 		if (!mac) {
2106e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2107608654d4SNate Lawson 			error = ENXIO;
2108e7b01d07SWarner Losh 			goto fail;
2109e7b01d07SWarner Losh 		}
2110e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2111feb78939SJonathan Chen 		break;
211296f2e892SBill Paul 	default:
211396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
211496f2e892SBill Paul 		break;
211596f2e892SBill Paul 	}
211696f2e892SBill Paul 
211796f2e892SBill Paul 	/*
211896f2e892SBill Paul 	 * A 21143 or clone chip was detected. Inform the world.
211996f2e892SBill Paul 	 */
212096f2e892SBill Paul 	printf("dc%d: Ethernet address: %6D\n", unit, eaddr, ":");
212196f2e892SBill Paul 
212296f2e892SBill Paul 	sc->dc_unit = unit;
21230934f18aSMaxime Henrion 	bcopy(eaddr, &sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
212496f2e892SBill Paul 
212556e5e7aeSMaxime Henrion 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
212656e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
212756e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct dc_list_data), 1,
212856e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), 0, NULL, NULL, &sc->dc_ltag);
212956e5e7aeSMaxime Henrion 	if (error) {
213056e5e7aeSMaxime Henrion 		printf("dc%d: failed to allocate busdma tag\n", unit);
213156e5e7aeSMaxime Henrion 		error = ENXIO;
213256e5e7aeSMaxime Henrion 		goto fail;
213356e5e7aeSMaxime Henrion 	}
213456e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_ltag, (void **)&sc->dc_ldata,
2135aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->dc_lmap);
213656e5e7aeSMaxime Henrion 	if (error) {
213756e5e7aeSMaxime Henrion 		printf("dc%d: failed to allocate DMA safe memory\n", unit);
213856e5e7aeSMaxime Henrion 		error = ENXIO;
213956e5e7aeSMaxime Henrion 		goto fail;
214056e5e7aeSMaxime Henrion 	}
214156e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_ltag, sc->dc_lmap, sc->dc_ldata,
214256e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), dc_dma_map_addr, &sc->dc_laddr,
214356e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT);
214456e5e7aeSMaxime Henrion 	if (error) {
214556e5e7aeSMaxime Henrion 		printf("dc%d: cannot get address of the descriptors\n", unit);
214656e5e7aeSMaxime Henrion 		error = ENXIO;
214756e5e7aeSMaxime Henrion 		goto fail;
214856e5e7aeSMaxime Henrion 	}
214996f2e892SBill Paul 
215056e5e7aeSMaxime Henrion 	/*
215156e5e7aeSMaxime Henrion 	 * Allocate a busdma tag and DMA safe memory for the multicast
215256e5e7aeSMaxime Henrion 	 * setup frame.
215356e5e7aeSMaxime Henrion 	 */
215456e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
215556e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1,
215656e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 0, NULL, NULL, &sc->dc_stag);
215756e5e7aeSMaxime Henrion 	if (error) {
215856e5e7aeSMaxime Henrion 		printf("dc%d: failed to allocate busdma tag\n", unit);
215956e5e7aeSMaxime Henrion 		error = ENXIO;
216056e5e7aeSMaxime Henrion 		goto fail;
216156e5e7aeSMaxime Henrion 	}
216256e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
216356e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->dc_smap);
216456e5e7aeSMaxime Henrion 	if (error) {
216556e5e7aeSMaxime Henrion 		printf("dc%d: failed to allocate DMA safe memory\n", unit);
216656e5e7aeSMaxime Henrion 		error = ENXIO;
216756e5e7aeSMaxime Henrion 		goto fail;
216856e5e7aeSMaxime Henrion 	}
216956e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
217056e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
217156e5e7aeSMaxime Henrion 	if (error) {
217256e5e7aeSMaxime Henrion 		printf("dc%d: cannot get address of the descriptors\n", unit);
217396f2e892SBill Paul 		error = ENXIO;
217496f2e892SBill Paul 		goto fail;
217596f2e892SBill Paul 	}
217696f2e892SBill Paul 
217756e5e7aeSMaxime Henrion 	/* Allocate a busdma tag for mbufs. */
217856e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
217956e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * DC_TX_LIST_CNT,
218056e5e7aeSMaxime Henrion 	    DC_TX_LIST_CNT, MCLBYTES, 0, NULL, NULL, &sc->dc_mtag);
218156e5e7aeSMaxime Henrion 	if (error) {
218256e5e7aeSMaxime Henrion 		printf("dc%d: failed to allocate busdma tag\n", unit);
218356e5e7aeSMaxime Henrion 		error = ENXIO;
218456e5e7aeSMaxime Henrion 		goto fail;
218556e5e7aeSMaxime Henrion 	}
218656e5e7aeSMaxime Henrion 
218756e5e7aeSMaxime Henrion 	/* Create the TX/RX busdma maps. */
218856e5e7aeSMaxime Henrion 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
218956e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
219056e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_tx_map[i]);
219156e5e7aeSMaxime Henrion 		if (error) {
219256e5e7aeSMaxime Henrion 			printf("dc%d: failed to init TX ring\n", unit);
219356e5e7aeSMaxime Henrion 			error = ENXIO;
219456e5e7aeSMaxime Henrion 			goto fail;
219556e5e7aeSMaxime Henrion 		}
219656e5e7aeSMaxime Henrion 	}
219756e5e7aeSMaxime Henrion 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
219856e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
219956e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_rx_map[i]);
220056e5e7aeSMaxime Henrion 		if (error) {
220156e5e7aeSMaxime Henrion 			printf("dc%d: failed to init RX ring\n", unit);
220256e5e7aeSMaxime Henrion 			error = ENXIO;
220356e5e7aeSMaxime Henrion 			goto fail;
220456e5e7aeSMaxime Henrion 		}
220556e5e7aeSMaxime Henrion 	}
220656e5e7aeSMaxime Henrion 	error = bus_dmamap_create(sc->dc_mtag, 0, &sc->dc_sparemap);
220756e5e7aeSMaxime Henrion 	if (error) {
220856e5e7aeSMaxime Henrion 		printf("dc%d: failed to init RX ring\n", unit);
220956e5e7aeSMaxime Henrion 		error = ENXIO;
221056e5e7aeSMaxime Henrion 		goto fail;
221156e5e7aeSMaxime Henrion 	}
221296f2e892SBill Paul 
221396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
221496f2e892SBill Paul 	ifp->if_softc = sc;
221596f2e892SBill Paul 	ifp->if_unit = unit;
221696f2e892SBill Paul 	ifp->if_name = "dc";
2217feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
221896f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
221996f2e892SBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
222096f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
222196f2e892SBill Paul 	ifp->if_start = dc_start;
222296f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
222396f2e892SBill Paul 	ifp->if_init = dc_init;
222496f2e892SBill Paul 	ifp->if_baudrate = 10000000;
222596f2e892SBill Paul 	ifp->if_snd.ifq_maxlen = DC_TX_LIST_CNT - 1;
222696f2e892SBill Paul 
222796f2e892SBill Paul 	/*
22285c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
22295c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
22305c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
22315c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
22325c1cfac4SBill Paul 	 * driver instead.
223396f2e892SBill Paul 	 */
22345c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
22355c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
22365c1cfac4SBill Paul 		tmp = sc->dc_pmode;
22375c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
22385c1cfac4SBill Paul 	}
22395c1cfac4SBill Paul 
224096f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
224196f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
224296f2e892SBill Paul 
224396f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
22445c1cfac4SBill Paul 		sc->dc_pmode = tmp;
22455c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
224696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2247042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
224896f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
224996f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
225078999dd1SBill Paul 		/*
225178999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
225278999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
225378999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
225478999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
225578999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
225678999dd1SBill Paul 		 */
225778999dd1SBill Paul 		if (pci_read_config(dev, DC_PCI_CSID, 4) != 0x80281033)
225878999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
225996f2e892SBill Paul 		error = 0;
226096f2e892SBill Paul 	}
226196f2e892SBill Paul 
226296f2e892SBill Paul 	if (error) {
226396f2e892SBill Paul 		printf("dc%d: MII without any PHY!\n", sc->dc_unit);
226496f2e892SBill Paul 		goto fail;
226596f2e892SBill Paul 	}
226696f2e892SBill Paul 
2267feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
2268feb78939SJonathan Chen 		/*
2269feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
2270feb78939SJonathan Chen 		 * can talk to the MII.
2271feb78939SJonathan Chen 		 */
2272feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
2273feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2274feb78939SJonathan Chen 		DELAY(10);
2275feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
2276feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
2277feb78939SJonathan Chen 		DELAY(10);
2278feb78939SJonathan Chen 	}
2279feb78939SJonathan Chen 
2280028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2281028a8491SMartin Blapp 		/*
2282028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2283028a8491SMartin Blapp 		 */
2284028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2285028a8491SMartin Blapp 	}
2286028a8491SMartin Blapp 
228796f2e892SBill Paul 	/*
2288db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2289db40c1aeSDoug Ambrisko 	 */
2290db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
22919ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
2292db40c1aeSDoug Ambrisko 
2293c06eb4e2SSam Leffler 	callout_init(&sc->dc_stat_ch, IS_MPSAFE ? CALLOUT_MPSAFE : 0);
229496f2e892SBill Paul 
22955c1cfac4SBill Paul #ifdef SRM_MEDIA
2296510a809eSMike Smith 	sc->dc_srm_media = 0;
2297510a809eSMike Smith 
2298510a809eSMike Smith 	/* Remember the SRM console media setting */
2299510a809eSMike Smith 	if (DC_IS_INTEL(sc)) {
2300510a809eSMike Smith 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
2301510a809eSMike Smith 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
2302510a809eSMike Smith 		switch ((command >> 8) & 0xff) {
2303510a809eSMike Smith 		case 3:
2304510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T;
2305510a809eSMike Smith 			break;
2306510a809eSMike Smith 		case 4:
2307510a809eSMike Smith 			sc->dc_srm_media = IFM_10_T | IFM_FDX;
2308510a809eSMike Smith 			break;
2309510a809eSMike Smith 		case 5:
2310510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX;
2311510a809eSMike Smith 			break;
2312510a809eSMike Smith 		case 6:
2313510a809eSMike Smith 			sc->dc_srm_media = IFM_100_TX | IFM_FDX;
2314510a809eSMike Smith 			break;
2315510a809eSMike Smith 		}
2316510a809eSMike Smith 		if (sc->dc_srm_media)
2317510a809eSMike Smith 			sc->dc_srm_media |= IFM_ACTIVE | IFM_ETHER;
2318510a809eSMike Smith 	}
2319510a809eSMike Smith #endif
2320510a809eSMike Smith 
2321608654d4SNate Lawson 	/*
2322608654d4SNate Lawson 	 * Call MI attach routine.
2323608654d4SNate Lawson 	 */
2324608654d4SNate Lawson 	ether_ifattach(ifp, eaddr);
2325608654d4SNate Lawson 
232654f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2327608654d4SNate Lawson 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET |
2328608654d4SNate Lawson 	    (IS_MPSAFE ? INTR_MPSAFE : 0),
2329608654d4SNate Lawson 	    dc_intr, sc, &sc->dc_intrhand);
2330608654d4SNate Lawson 
2331608654d4SNate Lawson 	if (error) {
2332608654d4SNate Lawson 		printf("dc%d: couldn't set up irq\n", unit);
2333693f4477SNate Lawson 		ether_ifdetach(ifp);
233454f1f1d1SNate Lawson 		goto fail;
2335608654d4SNate Lawson 	}
2336510a809eSMike Smith 
233796f2e892SBill Paul fail:
233854f1f1d1SNate Lawson 	if (error)
233954f1f1d1SNate Lawson 		dc_detach(dev);
234096f2e892SBill Paul 	return (error);
234196f2e892SBill Paul }
234296f2e892SBill Paul 
2343693f4477SNate Lawson /*
2344693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2345693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2346693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2347693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2348693f4477SNate Lawson  * allocated.
2349693f4477SNate Lawson  */
2350e3d2833aSAlfred Perlstein static int
23510934f18aSMaxime Henrion dc_detach(device_t dev)
235296f2e892SBill Paul {
235396f2e892SBill Paul 	struct dc_softc *sc;
235496f2e892SBill Paul 	struct ifnet *ifp;
23555c1cfac4SBill Paul 	struct dc_mediainfo *m;
235656e5e7aeSMaxime Henrion 	int i;
235796f2e892SBill Paul 
235896f2e892SBill Paul 	sc = device_get_softc(dev);
235959f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2360d1ce9105SBill Paul 	DC_LOCK(sc);
2361d1ce9105SBill Paul 
236296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
236396f2e892SBill Paul 
2364693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2365214073e5SWarner Losh 	if (device_is_attached(dev)) {
236696f2e892SBill Paul 		dc_stop(sc);
23679ef8b520SSam Leffler 		ether_ifdetach(ifp);
2368693f4477SNate Lawson 	}
2369693f4477SNate Lawson 	if (sc->dc_miibus)
237096f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
237154f1f1d1SNate Lawson 	bus_generic_detach(dev);
237296f2e892SBill Paul 
237354f1f1d1SNate Lawson 	if (sc->dc_intrhand)
237496f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
237554f1f1d1SNate Lawson 	if (sc->dc_irq)
237696f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
237754f1f1d1SNate Lawson 	if (sc->dc_res)
237896f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
237996f2e892SBill Paul 
238056e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_sbuf != NULL)
238156e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf, sc->dc_smap);
238256e5e7aeSMaxime Henrion 	if (sc->dc_ldata != NULL)
238356e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_ltag, sc->dc_ldata, sc->dc_lmap);
238456e5e7aeSMaxime Henrion 	for (i = 0; i < DC_TX_LIST_CNT; i++)
238556e5e7aeSMaxime Henrion 		bus_dmamap_destroy(sc->dc_mtag, sc->dc_cdata.dc_tx_map[i]);
238656e5e7aeSMaxime Henrion 	for (i = 0; i < DC_RX_LIST_CNT; i++)
238756e5e7aeSMaxime Henrion 		bus_dmamap_destroy(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i]);
238856e5e7aeSMaxime Henrion 	bus_dmamap_destroy(sc->dc_mtag, sc->dc_sparemap);
238956e5e7aeSMaxime Henrion 	if (sc->dc_stag)
239056e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_stag);
239156e5e7aeSMaxime Henrion 	if (sc->dc_mtag)
239256e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_mtag);
239356e5e7aeSMaxime Henrion 	if (sc->dc_ltag)
239456e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_ltag);
239556e5e7aeSMaxime Henrion 
239696f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
239796f2e892SBill Paul 
23985c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
23995c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
24005c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
24015c1cfac4SBill Paul 		sc->dc_mi = m;
24025c1cfac4SBill Paul 	}
24037efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
24045c1cfac4SBill Paul 
2405d1ce9105SBill Paul 	DC_UNLOCK(sc);
2406d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
240796f2e892SBill Paul 
240896f2e892SBill Paul 	return (0);
240996f2e892SBill Paul }
241096f2e892SBill Paul 
241196f2e892SBill Paul /*
241296f2e892SBill Paul  * Initialize the transmit descriptors.
241396f2e892SBill Paul  */
2414e3d2833aSAlfred Perlstein static int
24150934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
241696f2e892SBill Paul {
241796f2e892SBill Paul 	struct dc_chain_data *cd;
241896f2e892SBill Paul 	struct dc_list_data *ld;
241901faf54bSLuigi Rizzo 	int i, nexti;
242096f2e892SBill Paul 
242196f2e892SBill Paul 	cd = &sc->dc_cdata;
242296f2e892SBill Paul 	ld = sc->dc_ldata;
242396f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2424b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2425b3811c95SMaxime Henrion 			nexti = 0;
2426b3811c95SMaxime Henrion 		else
2427b3811c95SMaxime Henrion 			nexti = i + 1;
2428af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
242996f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
243096f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
243196f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
243296f2e892SBill Paul 	}
243396f2e892SBill Paul 
243496f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
243556e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
243656e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
243796f2e892SBill Paul 	return (0);
243896f2e892SBill Paul }
243996f2e892SBill Paul 
244096f2e892SBill Paul 
244196f2e892SBill Paul /*
244296f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
244396f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
244496f2e892SBill Paul  * points back to the first.
244596f2e892SBill Paul  */
2446e3d2833aSAlfred Perlstein static int
24470934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
244896f2e892SBill Paul {
244996f2e892SBill Paul 	struct dc_chain_data *cd;
245096f2e892SBill Paul 	struct dc_list_data *ld;
245101faf54bSLuigi Rizzo 	int i, nexti;
245296f2e892SBill Paul 
245396f2e892SBill Paul 	cd = &sc->dc_cdata;
245496f2e892SBill Paul 	ld = sc->dc_ldata;
245596f2e892SBill Paul 
245696f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
245756e5e7aeSMaxime Henrion 		if (dc_newbuf(sc, i, 1) != 0)
245896f2e892SBill Paul 			return (ENOBUFS);
2459b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2460b3811c95SMaxime Henrion 			nexti = 0;
2461b3811c95SMaxime Henrion 		else
2462b3811c95SMaxime Henrion 			nexti = i + 1;
2463af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
246496f2e892SBill Paul 	}
246596f2e892SBill Paul 
246696f2e892SBill Paul 	cd->dc_rx_prod = 0;
246756e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
246856e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
246996f2e892SBill Paul 	return (0);
247096f2e892SBill Paul }
247196f2e892SBill Paul 
247256e5e7aeSMaxime Henrion static void
247356e5e7aeSMaxime Henrion dc_dma_map_rxbuf(arg, segs, nseg, mapsize, error)
247456e5e7aeSMaxime Henrion 	void *arg;
247556e5e7aeSMaxime Henrion 	bus_dma_segment_t *segs;
247656e5e7aeSMaxime Henrion 	int nseg;
247756e5e7aeSMaxime Henrion 	bus_size_t mapsize;
247856e5e7aeSMaxime Henrion 	int error;
247956e5e7aeSMaxime Henrion {
248056e5e7aeSMaxime Henrion 	struct dc_softc *sc;
248156e5e7aeSMaxime Henrion 	struct dc_desc *c;
248256e5e7aeSMaxime Henrion 
248356e5e7aeSMaxime Henrion 	sc = arg;
248456e5e7aeSMaxime Henrion 	c = &sc->dc_ldata->dc_rx_list[sc->dc_cdata.dc_rx_cur];
248556e5e7aeSMaxime Henrion 	if (error) {
248656e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_err = error;
248756e5e7aeSMaxime Henrion 		return;
248856e5e7aeSMaxime Henrion 	}
248956e5e7aeSMaxime Henrion 
249056e5e7aeSMaxime Henrion 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
249156e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_rx_err = 0;
2492af4358c7SMaxime Henrion 	c->dc_data = htole32(segs->ds_addr);
249356e5e7aeSMaxime Henrion }
249456e5e7aeSMaxime Henrion 
249596f2e892SBill Paul /*
249696f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
249796f2e892SBill Paul  */
2498e3d2833aSAlfred Perlstein static int
249956e5e7aeSMaxime Henrion dc_newbuf(struct dc_softc *sc, int i, int alloc)
250096f2e892SBill Paul {
250156e5e7aeSMaxime Henrion 	struct mbuf *m_new;
250256e5e7aeSMaxime Henrion 	bus_dmamap_t tmp;
250356e5e7aeSMaxime Henrion 	int error;
250496f2e892SBill Paul 
250556e5e7aeSMaxime Henrion 	if (alloc) {
250656e5e7aeSMaxime Henrion 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
250740129585SLuigi Rizzo 		if (m_new == NULL)
250896f2e892SBill Paul 			return (ENOBUFS);
250996f2e892SBill Paul 	} else {
251056e5e7aeSMaxime Henrion 		m_new = sc->dc_cdata.dc_rx_chain[i];
251196f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
251296f2e892SBill Paul 	}
251356e5e7aeSMaxime Henrion 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
251496f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
251596f2e892SBill Paul 
251696f2e892SBill Paul 	/*
251796f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
251896f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
251996f2e892SBill Paul 	 * 82c169 chips.
252096f2e892SBill Paul 	 */
252196f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
25220934f18aSMaxime Henrion 		bzero(mtod(m_new, char *), m_new->m_len);
252396f2e892SBill Paul 
252456e5e7aeSMaxime Henrion 	/* No need to remap the mbuf if we're reusing it. */
252556e5e7aeSMaxime Henrion 	if (alloc) {
252656e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_cur = i;
252756e5e7aeSMaxime Henrion 		error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_sparemap,
252856e5e7aeSMaxime Henrion 		    m_new, dc_dma_map_rxbuf, sc, 0);
252956e5e7aeSMaxime Henrion 		if (error) {
253056e5e7aeSMaxime Henrion 			m_freem(m_new);
253156e5e7aeSMaxime Henrion 			return (error);
253256e5e7aeSMaxime Henrion 		}
253356e5e7aeSMaxime Henrion 		if (sc->dc_cdata.dc_rx_err != 0) {
253456e5e7aeSMaxime Henrion 			m_freem(m_new);
253556e5e7aeSMaxime Henrion 			return (sc->dc_cdata.dc_rx_err);
253656e5e7aeSMaxime Henrion 		}
253756e5e7aeSMaxime Henrion 		bus_dmamap_unload(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i]);
253856e5e7aeSMaxime Henrion 		tmp = sc->dc_cdata.dc_rx_map[i];
253956e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
254056e5e7aeSMaxime Henrion 		sc->dc_sparemap = tmp;
254196f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = m_new;
254256e5e7aeSMaxime Henrion 	}
254396f2e892SBill Paul 
2544af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2545af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
254656e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
254756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
254856e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
254956e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
255096f2e892SBill Paul 	return (0);
255196f2e892SBill Paul }
255296f2e892SBill Paul 
255396f2e892SBill Paul /*
255496f2e892SBill Paul  * Grrrrr.
255596f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
255696f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
255796f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
255896f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
255996f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
256096f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
256196f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
256296f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
256396f2e892SBill Paul  *
256496f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
256596f2e892SBill Paul  * Here's what we know:
256696f2e892SBill Paul  *
256796f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
256896f2e892SBill Paul  *   descriptors uploaded.
256996f2e892SBill Paul  *
257096f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
257196f2e892SBill Paul  *   total data upload.
257296f2e892SBill Paul  *
257396f2e892SBill Paul  * - We know the size of the desired received frame because it will be
257496f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
257596f2e892SBill Paul  *
257696f2e892SBill Paul  * Here's what we do:
257796f2e892SBill Paul  *
257896f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
257996f2e892SBill Paul  *   This means that we know that the buffer contents should be all
258096f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
258196f2e892SBill Paul  *
258296f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
258396f2e892SBill Paul  *   ethernet CRC at the end.
258496f2e892SBill Paul  *
258596f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
258696f2e892SBill Paul  *
258796f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
258896f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
258996f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
259096f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
259196f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
259296f2e892SBill Paul  *   we won't be fooled.
259396f2e892SBill Paul  *
259496f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
259596f2e892SBill Paul  *   that value from the current pointer location. This brings us
259696f2e892SBill Paul  *   to the start of the actual received packet.
259796f2e892SBill Paul  *
259896f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
259996f2e892SBill Paul  *   frame length.
260096f2e892SBill Paul  *
260196f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
260296f2e892SBill Paul  * the time.
260396f2e892SBill Paul  */
260496f2e892SBill Paul 
260596f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2606e3d2833aSAlfred Perlstein static void
26070934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
260896f2e892SBill Paul {
260996f2e892SBill Paul 	struct dc_desc *cur_rx;
261096f2e892SBill Paul 	struct dc_desc *c = NULL;
261196f2e892SBill Paul 	struct mbuf *m = NULL;
261296f2e892SBill Paul 	unsigned char *ptr;
261396f2e892SBill Paul 	int i, total_len;
261496f2e892SBill Paul 	u_int32_t rxstat = 0;
261596f2e892SBill Paul 
261696f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
261796f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
261896f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
26191edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
262096f2e892SBill Paul 
262196f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
262296f2e892SBill Paul 	while (1) {
262396f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
2624af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
262596f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
262696f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
262796f2e892SBill Paul 		ptr += DC_RXLEN;
262896f2e892SBill Paul 		/* If this is the last buffer, break out. */
262996f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
263096f2e892SBill Paul 			break;
263156e5e7aeSMaxime Henrion 		dc_newbuf(sc, i, 0);
263296f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
263396f2e892SBill Paul 	}
263496f2e892SBill Paul 
263596f2e892SBill Paul 	/* Find the length of the actual receive frame. */
263696f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
263796f2e892SBill Paul 
263896f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
263996f2e892SBill Paul 	while (*ptr == 0x00)
264096f2e892SBill Paul 		ptr--;
264196f2e892SBill Paul 
264296f2e892SBill Paul 	/* Round off. */
264396f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
264496f2e892SBill Paul 		ptr -= 1;
264596f2e892SBill Paul 
264696f2e892SBill Paul 	/* Now find the start of the frame. */
264796f2e892SBill Paul 	ptr -= total_len;
264896f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
264996f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
265096f2e892SBill Paul 
265196f2e892SBill Paul 	/*
265296f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
265396f2e892SBill Paul 	 * the status word to make it look like a successful
265496f2e892SBill Paul 	 * frame reception.
265596f2e892SBill Paul 	 */
265656e5e7aeSMaxime Henrion 	dc_newbuf(sc, i, 0);
265796f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2658af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
265996f2e892SBill Paul }
266096f2e892SBill Paul 
266196f2e892SBill Paul /*
266273bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
266373bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
266473bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
266573bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
266673bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
266773bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
266873bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
266973bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
267073bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
267173bf949cSBill Paul  */
2672e3d2833aSAlfred Perlstein static int
26730934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
267473bf949cSBill Paul {
267573bf949cSBill Paul 	struct dc_desc *cur_rx;
26760934f18aSMaxime Henrion 	int i, pos;
267773bf949cSBill Paul 
267873bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
267973bf949cSBill Paul 
268073bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
268173bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
2682af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
268373bf949cSBill Paul 			break;
268473bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
268573bf949cSBill Paul 	}
268673bf949cSBill Paul 
268773bf949cSBill Paul 	/* If the ring really is empty, then just return. */
268873bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
268973bf949cSBill Paul 		return (0);
269073bf949cSBill Paul 
269173bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
269273bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
269373bf949cSBill Paul 
269473bf949cSBill Paul 	return (EAGAIN);
269573bf949cSBill Paul }
269673bf949cSBill Paul 
269773bf949cSBill Paul /*
269896f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
269996f2e892SBill Paul  * the higher level protocols.
270096f2e892SBill Paul  */
2701e3d2833aSAlfred Perlstein static void
27020934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
270396f2e892SBill Paul {
270496f2e892SBill Paul 	struct mbuf *m;
270596f2e892SBill Paul 	struct ifnet *ifp;
270696f2e892SBill Paul 	struct dc_desc *cur_rx;
270796f2e892SBill Paul 	int i, total_len = 0;
270896f2e892SBill Paul 	u_int32_t rxstat;
270996f2e892SBill Paul 
271096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
271196f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
271296f2e892SBill Paul 
271356e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
2714af4358c7SMaxime Henrion 	while (!(le32toh(sc->dc_ldata->dc_rx_list[i].dc_status) &
2715af4358c7SMaxime Henrion 	    DC_RXSTAT_OWN)) {
2716e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
271762f76486SMaxim Sobolev 		if (ifp->if_flags & IFF_POLLING) {
2718e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2719e4fc250cSLuigi Rizzo 				break;
2720e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2721e4fc250cSLuigi Rizzo 		}
27220934f18aSMaxime Henrion #endif
272396f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
2724af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
272596f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
272656e5e7aeSMaxime Henrion 		bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
272756e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
272896f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
272996f2e892SBill Paul 
273096f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
273196f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
273296f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
273396f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
273496f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
273596f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
273696f2e892SBill Paul 					continue;
273796f2e892SBill Paul 				}
273896f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2739af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
274096f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
274196f2e892SBill Paul 			}
274296f2e892SBill Paul 		}
274396f2e892SBill Paul 
274496f2e892SBill Paul 		/*
274596f2e892SBill Paul 		 * If an error occurs, update stats, clear the
274696f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
274796f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2748db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
27490934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
275096f2e892SBill Paul 		 */
2751db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2752db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2753db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2754db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2755db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
275696f2e892SBill Paul 				ifp->if_ierrors++;
275796f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
275896f2e892SBill Paul 					ifp->if_collisions++;
275956e5e7aeSMaxime Henrion 				dc_newbuf(sc, i, 0);
276096f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
276196f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
276296f2e892SBill Paul 					continue;
276396f2e892SBill Paul 				} else {
276496f2e892SBill Paul 					dc_init(sc);
276596f2e892SBill Paul 					return;
276696f2e892SBill Paul 				}
276796f2e892SBill Paul 			}
2768db40c1aeSDoug Ambrisko 		}
276996f2e892SBill Paul 
277096f2e892SBill Paul 		/* No errors; receive the packet. */
277196f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
277201faf54bSLuigi Rizzo #ifdef __i386__
277301faf54bSLuigi Rizzo 		/*
277401faf54bSLuigi Rizzo 		 * On the x86 we do not have alignment problems, so try to
277501faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
277601faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
277701faf54bSLuigi Rizzo 		 * copy done in m_devget().
277801faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
277901faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
278001faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
278101faf54bSLuigi Rizzo 		 */
278256e5e7aeSMaxime Henrion 		if (dc_quick && dc_newbuf(sc, i, 1) == 0) {
278301faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
278401faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
278501faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
278601faf54bSLuigi Rizzo 		} else
278701faf54bSLuigi Rizzo #endif
278801faf54bSLuigi Rizzo 		{
278901faf54bSLuigi Rizzo 			struct mbuf *m0;
279096f2e892SBill Paul 
279101faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
279201faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
279356e5e7aeSMaxime Henrion 			dc_newbuf(sc, i, 0);
279496f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
279596f2e892SBill Paul 			if (m0 == NULL) {
279696f2e892SBill Paul 				ifp->if_ierrors++;
279796f2e892SBill Paul 				continue;
279896f2e892SBill Paul 			}
279996f2e892SBill Paul 			m = m0;
280001faf54bSLuigi Rizzo 		}
280196f2e892SBill Paul 
280296f2e892SBill Paul 		ifp->if_ipackets++;
28039ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
280496f2e892SBill Paul 	}
280596f2e892SBill Paul 
280696f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
280796f2e892SBill Paul }
280896f2e892SBill Paul 
280996f2e892SBill Paul /*
281096f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
281196f2e892SBill Paul  * the list buffers.
281296f2e892SBill Paul  */
281396f2e892SBill Paul 
2814e3d2833aSAlfred Perlstein static void
28150934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
281696f2e892SBill Paul {
281796f2e892SBill Paul 	struct dc_desc *cur_tx = NULL;
281896f2e892SBill Paul 	struct ifnet *ifp;
281996f2e892SBill Paul 	int idx;
2820af4358c7SMaxime Henrion 	u_int32_t ctl, txstat;
282196f2e892SBill Paul 
282296f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
282396f2e892SBill Paul 
282496f2e892SBill Paul 	/*
282596f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
282696f2e892SBill Paul 	 * frames that have been transmitted.
282796f2e892SBill Paul 	 */
282856e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
282996f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
283096f2e892SBill Paul 	while (idx != sc->dc_cdata.dc_tx_prod) {
283196f2e892SBill Paul 
283296f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
2833af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2834af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
283596f2e892SBill Paul 
283696f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
283796f2e892SBill Paul 			break;
283896f2e892SBill Paul 
2839af4358c7SMaxime Henrion 		if (!(ctl & DC_TXCTL_FIRSTFRAG) || ctl & DC_TXCTL_SETUP) {
2840af4358c7SMaxime Henrion 			if (ctl & DC_TXCTL_SETUP) {
284196f2e892SBill Paul 				/*
284296f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
284396f2e892SBill Paul 				 * that it will sometimes generate a TX
284496f2e892SBill Paul 				 * underrun error while DMAing the RX
284596f2e892SBill Paul 				 * filter setup frame. If we detect this,
284696f2e892SBill Paul 				 * we have to send the setup frame again,
284796f2e892SBill Paul 				 * or else the filter won't be programmed
284896f2e892SBill Paul 				 * correctly.
284996f2e892SBill Paul 				 */
285096f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
285196f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
285296f2e892SBill Paul 						dc_setfilt(sc);
285396f2e892SBill Paul 				}
285496f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
285596f2e892SBill Paul 			}
2856bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
285796f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
285896f2e892SBill Paul 			continue;
285996f2e892SBill Paul 		}
286096f2e892SBill Paul 
286129a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2862feb78939SJonathan Chen 			/*
2863feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2864feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
286529a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
286629a2220aSBill Paul 			 * Who knows, but Conexant chips have the
286729a2220aSBill Paul 			 * same problem. Maybe they took lessons
286829a2220aSBill Paul 			 * from Xircom.
286929a2220aSBill Paul 			 */
2870feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2871feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2872feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2873feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2874feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2875feb78939SJonathan Chen 		} else {
287696f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
287796f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
287896f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
287996f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
288096f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2881feb78939SJonathan Chen 		}
288296f2e892SBill Paul 
288396f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
288496f2e892SBill Paul 			ifp->if_oerrors++;
288596f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
288696f2e892SBill Paul 				ifp->if_collisions++;
288796f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
288896f2e892SBill Paul 				ifp->if_collisions++;
288996f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
289096f2e892SBill Paul 				dc_init(sc);
289196f2e892SBill Paul 				return;
289296f2e892SBill Paul 			}
289396f2e892SBill Paul 		}
289496f2e892SBill Paul 
289596f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
289696f2e892SBill Paul 
289796f2e892SBill Paul 		ifp->if_opackets++;
289896f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
289956e5e7aeSMaxime Henrion 			bus_dmamap_sync(sc->dc_mtag,
290056e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx],
290156e5e7aeSMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
290256e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
290356e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx]);
290496f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
290596f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
290696f2e892SBill Paul 		}
290796f2e892SBill Paul 
290896f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
290996f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
291096f2e892SBill Paul 	}
291196f2e892SBill Paul 
2912bcb9ef4fSLuigi Rizzo 	if (idx != sc->dc_cdata.dc_tx_cons) {
29130934f18aSMaxime Henrion 	    	/* Some buffers have been freed. */
291496f2e892SBill Paul 		sc->dc_cdata.dc_tx_cons = idx;
291596f2e892SBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
2916bcb9ef4fSLuigi Rizzo 	}
2917bcb9ef4fSLuigi Rizzo 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
291896f2e892SBill Paul }
291996f2e892SBill Paul 
2920e3d2833aSAlfred Perlstein static void
29210934f18aSMaxime Henrion dc_tick(void *xsc)
292296f2e892SBill Paul {
292396f2e892SBill Paul 	struct dc_softc *sc;
292496f2e892SBill Paul 	struct mii_data *mii;
292596f2e892SBill Paul 	struct ifnet *ifp;
292696f2e892SBill Paul 	u_int32_t r;
292796f2e892SBill Paul 
292896f2e892SBill Paul 	sc = xsc;
2929d1ce9105SBill Paul 	DC_LOCK(sc);
293096f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
293196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
293296f2e892SBill Paul 
293396f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2934318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2935318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2936318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2937318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
293896f2e892SBill Paul 				sc->dc_link = 0;
2939318b02fdSBill Paul 				mii_mediachg(mii);
2940318b02fdSBill Paul 			}
2941318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2942318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2943318b02fdSBill Paul 				sc->dc_link = 0;
2944318b02fdSBill Paul 				mii_mediachg(mii);
2945318b02fdSBill Paul 			}
2946d675147eSBill Paul 			if (sc->dc_link == 0)
294796f2e892SBill Paul 				mii_tick(mii);
294896f2e892SBill Paul 		} else {
2949318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
295096f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2951259b8d84SMartin Blapp 			    sc->dc_cdata.dc_tx_cnt == 0) {
295296f2e892SBill Paul 				mii_tick(mii);
2953042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2954042c8f6eSBill Paul 					sc->dc_link = 0;
295596f2e892SBill Paul 			}
2956259b8d84SMartin Blapp 		}
295796f2e892SBill Paul 	} else
295896f2e892SBill Paul 		mii_tick(mii);
295996f2e892SBill Paul 
296096f2e892SBill Paul 	/*
296196f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
296296f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
296396f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
296496f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
296596f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
296696f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
296796f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
296896f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
296996f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
297096f2e892SBill Paul 	 * a screeching halt for several seconds.
297196f2e892SBill Paul 	 *
297296f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
297396f2e892SBill Paul 	 * any packets until a link has been established. After the
297496f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
297596f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
297696f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
297796f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
297896f2e892SBill Paul 	 */
2979cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
298096f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
298196f2e892SBill Paul 		sc->dc_link++;
298296f2e892SBill Paul 		if (ifp->if_snd.ifq_head != NULL)
298396f2e892SBill Paul 			dc_start(ifp);
298496f2e892SBill Paul 	}
298596f2e892SBill Paul 
2986318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2987b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2988318b02fdSBill Paul 	else
2989b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
299096f2e892SBill Paul 
2991d1ce9105SBill Paul 	DC_UNLOCK(sc);
299296f2e892SBill Paul }
299396f2e892SBill Paul 
2994d467c136SBill Paul /*
2995d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2996d467c136SBill Paul  * or switch to store and forward mode if we have to.
2997d467c136SBill Paul  */
2998e3d2833aSAlfred Perlstein static void
29990934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3000d467c136SBill Paul {
3001d467c136SBill Paul 	u_int32_t isr;
3002d467c136SBill Paul 	int i;
3003d467c136SBill Paul 
3004d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
3005d467c136SBill Paul 		dc_init(sc);
3006d467c136SBill Paul 
3007d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
3008d467c136SBill Paul 		/*
3009d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
3010d467c136SBill Paul 		 * in order to change the transmit threshold or store
3011d467c136SBill Paul 		 * and forward state.
3012d467c136SBill Paul 		 */
3013d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3014d467c136SBill Paul 
3015d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
3016d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
3017d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
3018d467c136SBill Paul 				break;
3019d467c136SBill Paul 			DELAY(10);
3020d467c136SBill Paul 		}
3021d467c136SBill Paul 		if (i == DC_TIMEOUT) {
3022d467c136SBill Paul 			printf("dc%d: failed to force tx to idle state\n",
3023d467c136SBill Paul 			    sc->dc_unit);
3024d467c136SBill Paul 			dc_init(sc);
3025d467c136SBill Paul 		}
3026d467c136SBill Paul 	}
3027d467c136SBill Paul 
3028d467c136SBill Paul 	printf("dc%d: TX underrun -- ", sc->dc_unit);
3029d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
3030d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
3031d467c136SBill Paul 		printf("using store and forward mode\n");
3032d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
3033d467c136SBill Paul 	} else {
3034d467c136SBill Paul 		printf("increasing TX threshold\n");
3035d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3036d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
3037d467c136SBill Paul 	}
3038d467c136SBill Paul 
3039d467c136SBill Paul 	if (DC_IS_INTEL(sc))
3040d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
3041d467c136SBill Paul }
3042d467c136SBill Paul 
3043e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3044e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3045e4fc250cSLuigi Rizzo 
3046e4fc250cSLuigi Rizzo static void
3047e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3048e4fc250cSLuigi Rizzo {
3049e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
3050e4fc250cSLuigi Rizzo 
3051e4fc250cSLuigi Rizzo 	if (cmd == POLL_DEREGISTER) { /* final call, enable interrupts */
3052e4fc250cSLuigi Rizzo 		/* Re-enable interrupts. */
3053e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
3054e4fc250cSLuigi Rizzo 		return;
3055e4fc250cSLuigi Rizzo 	}
3056e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
3057e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
3058e4fc250cSLuigi Rizzo 	dc_txeof(sc);
3059e4fc250cSLuigi Rizzo 	if (ifp->if_snd.ifq_head != NULL && !(ifp->if_flags & IFF_OACTIVE))
3060e4fc250cSLuigi Rizzo 		dc_start(ifp);
3061e4fc250cSLuigi Rizzo 
3062e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3063e4fc250cSLuigi Rizzo 		u_int32_t	status;
3064e4fc250cSLuigi Rizzo 
3065e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3066e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3067e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3068e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
3069e4fc250cSLuigi Rizzo 		if (!status)
3070e4fc250cSLuigi Rizzo 			return;
3071e4fc250cSLuigi Rizzo 		/* ack what we have */
3072e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3073e4fc250cSLuigi Rizzo 
3074e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3075e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3076e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3077e4fc250cSLuigi Rizzo 
3078e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3079e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3080e4fc250cSLuigi Rizzo 		}
3081e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3082e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3083e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3084e4fc250cSLuigi Rizzo 
3085e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3086e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3087e4fc250cSLuigi Rizzo 
3088e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
3089e4fc250cSLuigi Rizzo 			printf("dc_poll: dc%d bus error\n", sc->dc_unit);
3090e4fc250cSLuigi Rizzo 			dc_reset(sc);
3091e4fc250cSLuigi Rizzo 			dc_init(sc);
3092e4fc250cSLuigi Rizzo 		}
3093e4fc250cSLuigi Rizzo 	}
3094e4fc250cSLuigi Rizzo }
3095e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3096e4fc250cSLuigi Rizzo 
3097e3d2833aSAlfred Perlstein static void
30980934f18aSMaxime Henrion dc_intr(void *arg)
309996f2e892SBill Paul {
310096f2e892SBill Paul 	struct dc_softc *sc;
310196f2e892SBill Paul 	struct ifnet *ifp;
310296f2e892SBill Paul 	u_int32_t status;
310396f2e892SBill Paul 
310496f2e892SBill Paul 	sc = arg;
3105d2a1864bSWarner Losh 
31060934f18aSMaxime Henrion 	if (sc->suspended)
3107e8388e14SMitsuru IWASAKI 		return;
3108e8388e14SMitsuru IWASAKI 
3109d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
3110d2a1864bSWarner Losh 		return;
3111d2a1864bSWarner Losh 
3112d1ce9105SBill Paul 	DC_LOCK(sc);
311396f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
3114e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
311562f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
3116e4fc250cSLuigi Rizzo 		goto done;
3117e4fc250cSLuigi Rizzo 	if (ether_poll_register(dc_poll, ifp)) { /* ok, disable interrupts */
3118e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3119e4fc250cSLuigi Rizzo 		goto done;
3120e4fc250cSLuigi Rizzo 	}
31210934f18aSMaxime Henrion #endif
312296f2e892SBill Paul 
3123d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
312496f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
312596f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
312696f2e892SBill Paul 			dc_stop(sc);
3127d1ce9105SBill Paul 		DC_UNLOCK(sc);
312896f2e892SBill Paul 		return;
312996f2e892SBill Paul 	}
313096f2e892SBill Paul 
313196f2e892SBill Paul 	/* Disable interrupts. */
313296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
313396f2e892SBill Paul 
3134feb78939SJonathan Chen 	while (((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS)
3135feb78939SJonathan Chen 	      && status != 0xFFFFFFFF) {
313696f2e892SBill Paul 
313796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
313896f2e892SBill Paul 
313973bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
314073bf949cSBill Paul 			int		curpkts;
314173bf949cSBill Paul 			curpkts = ifp->if_ipackets;
314296f2e892SBill Paul 			dc_rxeof(sc);
314373bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
314473bf949cSBill Paul 				while (dc_rx_resync(sc))
314573bf949cSBill Paul 					dc_rxeof(sc);
314673bf949cSBill Paul 			}
314773bf949cSBill Paul 		}
314896f2e892SBill Paul 
314996f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
315096f2e892SBill Paul 			dc_txeof(sc);
315196f2e892SBill Paul 
315296f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
315396f2e892SBill Paul 			dc_txeof(sc);
315496f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
315596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
315696f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
315796f2e892SBill Paul 			}
315896f2e892SBill Paul 		}
315996f2e892SBill Paul 
3160d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3161d467c136SBill Paul 			dc_tx_underrun(sc);
316296f2e892SBill Paul 
316396f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
316473bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
316573bf949cSBill Paul 			int		curpkts;
316673bf949cSBill Paul 			curpkts = ifp->if_ipackets;
316796f2e892SBill Paul 			dc_rxeof(sc);
316873bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
316973bf949cSBill Paul 				while (dc_rx_resync(sc))
317073bf949cSBill Paul 					dc_rxeof(sc);
317173bf949cSBill Paul 			}
317273bf949cSBill Paul 		}
317396f2e892SBill Paul 
317496f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
317596f2e892SBill Paul 			dc_reset(sc);
317696f2e892SBill Paul 			dc_init(sc);
317796f2e892SBill Paul 		}
317896f2e892SBill Paul 	}
317996f2e892SBill Paul 
318096f2e892SBill Paul 	/* Re-enable interrupts. */
318196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
318296f2e892SBill Paul 
318396f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
318496f2e892SBill Paul 		dc_start(ifp);
318596f2e892SBill Paul 
3186d9700bb5SBill Paul #ifdef DEVICE_POLLING
3187e4fc250cSLuigi Rizzo done:
31880934f18aSMaxime Henrion #endif
3189d9700bb5SBill Paul 
3190d1ce9105SBill Paul 	DC_UNLOCK(sc);
319196f2e892SBill Paul }
319296f2e892SBill Paul 
319356e5e7aeSMaxime Henrion static void
319456e5e7aeSMaxime Henrion dc_dma_map_txbuf(arg, segs, nseg, mapsize, error)
319556e5e7aeSMaxime Henrion 	void *arg;
319656e5e7aeSMaxime Henrion 	bus_dma_segment_t *segs;
319756e5e7aeSMaxime Henrion 	int nseg;
319856e5e7aeSMaxime Henrion 	bus_size_t mapsize;
319956e5e7aeSMaxime Henrion 	int error;
320056e5e7aeSMaxime Henrion {
320156e5e7aeSMaxime Henrion 	struct dc_softc *sc;
320256e5e7aeSMaxime Henrion 	struct dc_desc *f;
320356e5e7aeSMaxime Henrion 	int cur, first, frag, i;
320456e5e7aeSMaxime Henrion 
320556e5e7aeSMaxime Henrion 	sc = arg;
320656e5e7aeSMaxime Henrion 	if (error) {
320756e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_tx_err = error;
320856e5e7aeSMaxime Henrion 		return;
320956e5e7aeSMaxime Henrion 	}
321056e5e7aeSMaxime Henrion 
321156e5e7aeSMaxime Henrion 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
321256e5e7aeSMaxime Henrion 	for (i = 0; i < nseg; i++) {
321356e5e7aeSMaxime Henrion 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
321456e5e7aeSMaxime Henrion 		    (frag == (DC_TX_LIST_CNT - 1)) &&
321556e5e7aeSMaxime Henrion 		    (first != sc->dc_cdata.dc_tx_first)) {
321656e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
321756e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[first]);
321856e5e7aeSMaxime Henrion 			sc->dc_cdata.dc_tx_err = ENOBUFS;
321956e5e7aeSMaxime Henrion 			return;
322056e5e7aeSMaxime Henrion 		}
322156e5e7aeSMaxime Henrion 
322256e5e7aeSMaxime Henrion 		f = &sc->dc_ldata->dc_tx_list[frag];
3223af4358c7SMaxime Henrion 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
322456e5e7aeSMaxime Henrion 		if (i == 0) {
322556e5e7aeSMaxime Henrion 			f->dc_status = 0;
3226af4358c7SMaxime Henrion 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
322756e5e7aeSMaxime Henrion 		} else
3228af4358c7SMaxime Henrion 			f->dc_status = htole32(DC_TXSTAT_OWN);
3229af4358c7SMaxime Henrion 		f->dc_data = htole32(segs[i].ds_addr);
323056e5e7aeSMaxime Henrion 		cur = frag;
323156e5e7aeSMaxime Henrion 		DC_INC(frag, DC_TX_LIST_CNT);
323256e5e7aeSMaxime Henrion 	}
323356e5e7aeSMaxime Henrion 
323456e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_err = 0;
323556e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_prod = frag;
323656e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_cnt += nseg;
3237af4358c7SMaxime Henrion 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
323856e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
3239af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[first].dc_ctl |=
3240af4358c7SMaxime Henrion 		    htole32(DC_TXCTL_FINT);
324156e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
3242af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
324356e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
3244af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3245af4358c7SMaxime Henrion 	sc->dc_ldata->dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
324656e5e7aeSMaxime Henrion }
324756e5e7aeSMaxime Henrion 
324896f2e892SBill Paul /*
324996f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
325096f2e892SBill Paul  * pointers to the fragment pointers.
325196f2e892SBill Paul  */
3252e3d2833aSAlfred Perlstein static int
325356e5e7aeSMaxime Henrion dc_encap(struct dc_softc *sc, struct mbuf *m_head)
325496f2e892SBill Paul {
325596f2e892SBill Paul 	struct mbuf *m;
325656e5e7aeSMaxime Henrion 	int error, idx, chainlen = 0;
3257cda97c50SMike Silbersack 
3258cda97c50SMike Silbersack 	/*
3259cda97c50SMike Silbersack 	 * If there's no way we can send any packets, return now.
3260cda97c50SMike Silbersack 	 */
3261cda97c50SMike Silbersack 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt < 6)
3262cda97c50SMike Silbersack 		return (ENOBUFS);
3263cda97c50SMike Silbersack 
3264cda97c50SMike Silbersack 	/*
3265cda97c50SMike Silbersack 	 * Count the number of frags in this chain to see if
3266cda97c50SMike Silbersack 	 * we need to m_defrag.  Since the descriptor list is shared
3267cda97c50SMike Silbersack 	 * by all packets, we'll m_defrag long chains so that they
3268cda97c50SMike Silbersack 	 * do not use up the entire list, even if they would fit.
3269cda97c50SMike Silbersack 	 */
3270cda97c50SMike Silbersack 	for (m = m_head; m != NULL; m = m->m_next)
3271cda97c50SMike Silbersack 		chainlen++;
3272cda97c50SMike Silbersack 
3273cda97c50SMike Silbersack 	if ((chainlen > DC_TX_LIST_CNT / 4) ||
3274cda97c50SMike Silbersack 	    ((DC_TX_LIST_CNT - (chainlen + sc->dc_cdata.dc_tx_cnt)) < 6)) {
3275cda97c50SMike Silbersack 		m = m_defrag(m_head, M_DONTWAIT);
3276cda97c50SMike Silbersack 		if (m == NULL)
3277cda97c50SMike Silbersack 			return (ENOBUFS);
3278cda97c50SMike Silbersack 		m_head = m;
3279cda97c50SMike Silbersack 	}
328096f2e892SBill Paul 
328196f2e892SBill Paul 	/*
328296f2e892SBill Paul 	 * Start packing the mbufs in this chain into
328396f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
328496f2e892SBill Paul 	 * of fragments or hit the end of the mbuf chain.
328596f2e892SBill Paul 	 */
328656e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
328756e5e7aeSMaxime Henrion 	error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
328856e5e7aeSMaxime Henrion 	    m_head, dc_dma_map_txbuf, sc, 0);
328956e5e7aeSMaxime Henrion 	if (error)
329056e5e7aeSMaxime Henrion 		return (error);
329156e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_tx_err != 0)
329256e5e7aeSMaxime Henrion 		return (sc->dc_cdata.dc_tx_err);
329356e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[idx] = m_head;
329456e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
329556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE);
329656e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
329756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
329896f2e892SBill Paul 	return (0);
329996f2e892SBill Paul }
330096f2e892SBill Paul 
330196f2e892SBill Paul /*
330296f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
330396f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
330496f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
330596f2e892SBill Paul  * physical addresses.
330696f2e892SBill Paul  */
330796f2e892SBill Paul 
3308e3d2833aSAlfred Perlstein static void
33090934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
331096f2e892SBill Paul {
331196f2e892SBill Paul 	struct dc_softc *sc;
3312cda97c50SMike Silbersack 	struct mbuf *m_head = NULL, *m;
331396f2e892SBill Paul 	int idx;
331496f2e892SBill Paul 
331596f2e892SBill Paul 	sc = ifp->if_softc;
331696f2e892SBill Paul 
3317d1ce9105SBill Paul 	DC_LOCK(sc);
331896f2e892SBill Paul 
3319e7be9f9aSBill Paul 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10) {
3320d1ce9105SBill Paul 		DC_UNLOCK(sc);
332196f2e892SBill Paul 		return;
3322d1ce9105SBill Paul 	}
3323d1ce9105SBill Paul 
3324d1ce9105SBill Paul 	if (ifp->if_flags & IFF_OACTIVE) {
3325d1ce9105SBill Paul 		DC_UNLOCK(sc);
3326d1ce9105SBill Paul 		return;
3327d1ce9105SBill Paul 	}
332896f2e892SBill Paul 
332956e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
333096f2e892SBill Paul 
333196f2e892SBill Paul 	while (sc->dc_cdata.dc_tx_chain[idx] == NULL) {
333296f2e892SBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
333396f2e892SBill Paul 		if (m_head == NULL)
333496f2e892SBill Paul 			break;
333596f2e892SBill Paul 
33362dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
33372dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
33382dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3339cda97c50SMike Silbersack 			m = m_defrag(m_head, M_DONTWAIT);
3340cda97c50SMike Silbersack 			if (m == NULL) {
3341fda39fd0SBill Paul 				IF_PREPEND(&ifp->if_snd, m_head);
3342fda39fd0SBill Paul 				ifp->if_flags |= IFF_OACTIVE;
3343fda39fd0SBill Paul 				break;
3344cda97c50SMike Silbersack 			} else {
3345cda97c50SMike Silbersack 				m_head = m;
3346fda39fd0SBill Paul 			}
3347fda39fd0SBill Paul 		}
3348fda39fd0SBill Paul 
334956e5e7aeSMaxime Henrion 		if (dc_encap(sc, m_head)) {
335096f2e892SBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
335196f2e892SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
335296f2e892SBill Paul 			break;
335396f2e892SBill Paul 		}
335456e5e7aeSMaxime Henrion 		idx = sc->dc_cdata.dc_tx_prod;
335596f2e892SBill Paul 
335696f2e892SBill Paul 		/*
335796f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
335896f2e892SBill Paul 		 * to him.
335996f2e892SBill Paul 		 */
33609ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
33615c1cfac4SBill Paul 
33625c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
33635c1cfac4SBill Paul 			ifp->if_flags |= IFF_OACTIVE;
33645c1cfac4SBill Paul 			break;
33655c1cfac4SBill Paul 		}
336696f2e892SBill Paul 	}
336796f2e892SBill Paul 
336896f2e892SBill Paul 	/* Transmit */
336996f2e892SBill Paul 	if (!(sc->dc_flags & DC_TX_POLL))
337096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
337196f2e892SBill Paul 
337296f2e892SBill Paul 	/*
337396f2e892SBill Paul 	 * Set a timeout in case the chip goes out to lunch.
337496f2e892SBill Paul 	 */
337596f2e892SBill Paul 	ifp->if_timer = 5;
337696f2e892SBill Paul 
3377d1ce9105SBill Paul 	DC_UNLOCK(sc);
337896f2e892SBill Paul }
337996f2e892SBill Paul 
3380e3d2833aSAlfred Perlstein static void
33810934f18aSMaxime Henrion dc_init(void *xsc)
338296f2e892SBill Paul {
338396f2e892SBill Paul 	struct dc_softc *sc = xsc;
338496f2e892SBill Paul 	struct ifnet *ifp = &sc->arpcom.ac_if;
338596f2e892SBill Paul 	struct mii_data *mii;
338696f2e892SBill Paul 
3387d1ce9105SBill Paul 	DC_LOCK(sc);
338896f2e892SBill Paul 
338996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
339096f2e892SBill Paul 
339196f2e892SBill Paul 	/*
339296f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
339396f2e892SBill Paul 	 */
339496f2e892SBill Paul 	dc_stop(sc);
339596f2e892SBill Paul 	dc_reset(sc);
339696f2e892SBill Paul 
339796f2e892SBill Paul 	/*
339896f2e892SBill Paul 	 * Set cache alignment and burst length.
339996f2e892SBill Paul 	 */
340088d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
340196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
340296f2e892SBill Paul 	else
340396f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3404935fe010SLuigi Rizzo 	/*
3405935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3406935fe010SLuigi Rizzo 	 */
3407935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3408935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
340996f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
341096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
341196f2e892SBill Paul 	} else {
341296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
341396f2e892SBill Paul 	}
341496f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
341596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
341696f2e892SBill Paul 	switch(sc->dc_cachesize) {
341796f2e892SBill Paul 	case 32:
341896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
341996f2e892SBill Paul 		break;
342096f2e892SBill Paul 	case 16:
342196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
342296f2e892SBill Paul 		break;
342396f2e892SBill Paul 	case 8:
342496f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
342596f2e892SBill Paul 		break;
342696f2e892SBill Paul 	case 0:
342796f2e892SBill Paul 	default:
342896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
342996f2e892SBill Paul 		break;
343096f2e892SBill Paul 	}
343196f2e892SBill Paul 
343296f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
343396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
343496f2e892SBill Paul 	else {
3435d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
343696f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
343796f2e892SBill Paul 		} else {
343896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
343996f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
344096f2e892SBill Paul 		}
344196f2e892SBill Paul 	}
344296f2e892SBill Paul 
344396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
344496f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
344596f2e892SBill Paul 
344696f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
344796f2e892SBill Paul 		/*
344896f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
344996f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
345096f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
345196f2e892SBill Paul 		 * document the meaning of these bits so there's no way
345296f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
345396f2e892SBill Paul 		 * number all its own; the rest all use a different one.
345496f2e892SBill Paul 		 */
345596f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
345696f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
345796f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
345896f2e892SBill Paul 		else
345996f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
346096f2e892SBill Paul 	}
346196f2e892SBill Paul 
3462feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3463feb78939SJonathan Chen 		/*
3464feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3465feb78939SJonathan Chen 		 * can talk to the MII.
3466feb78939SJonathan Chen 		 */
3467feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3468feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3469feb78939SJonathan Chen 		DELAY(10);
3470feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3471feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3472feb78939SJonathan Chen 		DELAY(10);
3473feb78939SJonathan Chen 	}
3474feb78939SJonathan Chen 
347596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3476d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
347796f2e892SBill Paul 
347896f2e892SBill Paul 	/* Init circular RX list. */
347996f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
348096f2e892SBill Paul 		printf("dc%d: initialization failed: no "
348196f2e892SBill Paul 		    "memory for rx buffers\n", sc->dc_unit);
348296f2e892SBill Paul 		dc_stop(sc);
3483d1ce9105SBill Paul 		DC_UNLOCK(sc);
348496f2e892SBill Paul 		return;
348596f2e892SBill Paul 	}
348696f2e892SBill Paul 
348796f2e892SBill Paul 	/*
348856e5e7aeSMaxime Henrion 	 * Init TX descriptors.
348996f2e892SBill Paul 	 */
349096f2e892SBill Paul 	dc_list_tx_init(sc);
349196f2e892SBill Paul 
349296f2e892SBill Paul 	/*
349396f2e892SBill Paul 	 * Load the address of the RX list.
349496f2e892SBill Paul 	 */
349556e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
349656e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
349796f2e892SBill Paul 
349896f2e892SBill Paul 	/*
349996f2e892SBill Paul 	 * Enable interrupts.
350096f2e892SBill Paul 	 */
3501e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3502e4fc250cSLuigi Rizzo 	/*
3503e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3504e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3505e4fc250cSLuigi Rizzo 	 * after a reset.
3506e4fc250cSLuigi Rizzo 	 */
350762f76486SMaxim Sobolev 	if (ifp->if_flags & IFF_POLLING)
3508e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3509e4fc250cSLuigi Rizzo 	else
3510e4fc250cSLuigi Rizzo #endif
351196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
351296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
351396f2e892SBill Paul 
351496f2e892SBill Paul 	/* Enable transmitter. */
351596f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
351696f2e892SBill Paul 
351796f2e892SBill Paul 	/*
3518918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3519918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3520918434c8SBill Paul 	 * link and activity indications.
3521918434c8SBill Paul 	 */
352278999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3523918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3524918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
352578999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3526918434c8SBill Paul 	}
3527918434c8SBill Paul 
3528918434c8SBill Paul 	/*
352996f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
353096f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
353196f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
353296f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
353396f2e892SBill Paul 	 */
353496f2e892SBill Paul 	dc_setfilt(sc);
353596f2e892SBill Paul 
353696f2e892SBill Paul 	/* Enable receiver. */
353796f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
353896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
353996f2e892SBill Paul 
354096f2e892SBill Paul 	mii_mediachg(mii);
354196f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
354296f2e892SBill Paul 
354396f2e892SBill Paul 	ifp->if_flags |= IFF_RUNNING;
354496f2e892SBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
354596f2e892SBill Paul 
3546857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
354745521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3548857fd445SBill Paul 		sc->dc_link = 1;
3549857fd445SBill Paul 	else {
3550318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3551b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3552318b02fdSBill Paul 		else
3553b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3554857fd445SBill Paul 	}
355596f2e892SBill Paul 
35565c1cfac4SBill Paul #ifdef SRM_MEDIA
3557510a809eSMike Smith 	if(sc->dc_srm_media) {
3558510a809eSMike Smith 		struct ifreq ifr;
3559510a809eSMike Smith 
3560510a809eSMike Smith 		ifr.ifr_media = sc->dc_srm_media;
3561510a809eSMike Smith 		ifmedia_ioctl(ifp, &ifr, &mii->mii_media, SIOCSIFMEDIA);
3562510a809eSMike Smith 		sc->dc_srm_media = 0;
3563510a809eSMike Smith 	}
3564510a809eSMike Smith #endif
3565d1ce9105SBill Paul 	DC_UNLOCK(sc);
356696f2e892SBill Paul }
356796f2e892SBill Paul 
356896f2e892SBill Paul /*
356996f2e892SBill Paul  * Set media options.
357096f2e892SBill Paul  */
3571e3d2833aSAlfred Perlstein static int
35720934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
357396f2e892SBill Paul {
357496f2e892SBill Paul 	struct dc_softc *sc;
357596f2e892SBill Paul 	struct mii_data *mii;
3576f43d9309SBill Paul 	struct ifmedia *ifm;
357796f2e892SBill Paul 
357896f2e892SBill Paul 	sc = ifp->if_softc;
357996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
358096f2e892SBill Paul 	mii_mediachg(mii);
3581f43d9309SBill Paul 	ifm = &mii->mii_media;
3582f43d9309SBill Paul 
3583f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
358445521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3585f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3586f43d9309SBill Paul 	else
358796f2e892SBill Paul 		sc->dc_link = 0;
358896f2e892SBill Paul 
358996f2e892SBill Paul 	return (0);
359096f2e892SBill Paul }
359196f2e892SBill Paul 
359296f2e892SBill Paul /*
359396f2e892SBill Paul  * Report current media status.
359496f2e892SBill Paul  */
3595e3d2833aSAlfred Perlstein static void
35960934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
359796f2e892SBill Paul {
359896f2e892SBill Paul 	struct dc_softc *sc;
359996f2e892SBill Paul 	struct mii_data *mii;
3600f43d9309SBill Paul 	struct ifmedia *ifm;
360196f2e892SBill Paul 
360296f2e892SBill Paul 	sc = ifp->if_softc;
360396f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
360496f2e892SBill Paul 	mii_pollstat(mii);
3605f43d9309SBill Paul 	ifm = &mii->mii_media;
3606f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
360745521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3608f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3609f43d9309SBill Paul 			ifmr->ifm_status = 0;
3610f43d9309SBill Paul 			return;
3611f43d9309SBill Paul 		}
3612f43d9309SBill Paul 	}
361396f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
361496f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
361596f2e892SBill Paul }
361696f2e892SBill Paul 
3617e3d2833aSAlfred Perlstein static int
36180934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
361996f2e892SBill Paul {
362096f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
362196f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
362296f2e892SBill Paul 	struct mii_data *mii;
3623d1ce9105SBill Paul 	int error = 0;
362496f2e892SBill Paul 
3625d1ce9105SBill Paul 	DC_LOCK(sc);
362696f2e892SBill Paul 
362796f2e892SBill Paul 	switch (command) {
362896f2e892SBill Paul 	case SIOCSIFFLAGS:
362996f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
36305d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
36315d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
36325d6dfbbbSLuigi Rizzo 
36335d6dfbbbSLuigi Rizzo 			if (ifp->if_flags & IFF_RUNNING) {
36345d6dfbbbSLuigi Rizzo 				if (need_setfilt)
363596f2e892SBill Paul 					dc_setfilt(sc);
36365d6dfbbbSLuigi Rizzo 			} else {
363796f2e892SBill Paul 				sc->dc_txthresh = 0;
363896f2e892SBill Paul 				dc_init(sc);
363996f2e892SBill Paul 			}
364096f2e892SBill Paul 		} else {
364196f2e892SBill Paul 			if (ifp->if_flags & IFF_RUNNING)
364296f2e892SBill Paul 				dc_stop(sc);
364396f2e892SBill Paul 		}
364496f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
364596f2e892SBill Paul 		error = 0;
364696f2e892SBill Paul 		break;
364796f2e892SBill Paul 	case SIOCADDMULTI:
364896f2e892SBill Paul 	case SIOCDELMULTI:
364996f2e892SBill Paul 		dc_setfilt(sc);
365096f2e892SBill Paul 		error = 0;
365196f2e892SBill Paul 		break;
365296f2e892SBill Paul 	case SIOCGIFMEDIA:
365396f2e892SBill Paul 	case SIOCSIFMEDIA:
365496f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
365596f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
36565c1cfac4SBill Paul #ifdef SRM_MEDIA
3657510a809eSMike Smith 		if (sc->dc_srm_media)
3658510a809eSMike Smith 			sc->dc_srm_media = 0;
3659510a809eSMike Smith #endif
366096f2e892SBill Paul 		break;
366196f2e892SBill Paul 	default:
36629ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
366396f2e892SBill Paul 		break;
366496f2e892SBill Paul 	}
366596f2e892SBill Paul 
3666d1ce9105SBill Paul 	DC_UNLOCK(sc);
366796f2e892SBill Paul 
366896f2e892SBill Paul 	return (error);
366996f2e892SBill Paul }
367096f2e892SBill Paul 
3671e3d2833aSAlfred Perlstein static void
36720934f18aSMaxime Henrion dc_watchdog(struct ifnet *ifp)
367396f2e892SBill Paul {
367496f2e892SBill Paul 	struct dc_softc *sc;
367596f2e892SBill Paul 
367696f2e892SBill Paul 	sc = ifp->if_softc;
367796f2e892SBill Paul 
3678d1ce9105SBill Paul 	DC_LOCK(sc);
3679d1ce9105SBill Paul 
368096f2e892SBill Paul 	ifp->if_oerrors++;
368196f2e892SBill Paul 	printf("dc%d: watchdog timeout\n", sc->dc_unit);
368296f2e892SBill Paul 
368396f2e892SBill Paul 	dc_stop(sc);
368496f2e892SBill Paul 	dc_reset(sc);
368596f2e892SBill Paul 	dc_init(sc);
368696f2e892SBill Paul 
368796f2e892SBill Paul 	if (ifp->if_snd.ifq_head != NULL)
368896f2e892SBill Paul 		dc_start(ifp);
368996f2e892SBill Paul 
3690d1ce9105SBill Paul 	DC_UNLOCK(sc);
369196f2e892SBill Paul }
369296f2e892SBill Paul 
369396f2e892SBill Paul /*
369496f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
369596f2e892SBill Paul  * RX and TX lists.
369696f2e892SBill Paul  */
3697e3d2833aSAlfred Perlstein static void
36980934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
369996f2e892SBill Paul {
370096f2e892SBill Paul 	struct ifnet *ifp;
3701b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3702b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3703b3811c95SMaxime Henrion 	int i;
3704af4358c7SMaxime Henrion 	u_int32_t ctl;
370596f2e892SBill Paul 
3706d1ce9105SBill Paul 	DC_LOCK(sc);
3707d1ce9105SBill Paul 
370896f2e892SBill Paul 	ifp = &sc->arpcom.ac_if;
370996f2e892SBill Paul 	ifp->if_timer = 0;
3710b3811c95SMaxime Henrion 	ld = sc->dc_ldata;
3711b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
371296f2e892SBill Paul 
3713b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
371496f2e892SBill Paul 
37153b3ec200SPeter Wemm 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
3716e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3717e4fc250cSLuigi Rizzo 	ether_poll_deregister(ifp);
3718e4fc250cSLuigi Rizzo #endif
37193b3ec200SPeter Wemm 
372096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
372196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
372296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
372396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
372496f2e892SBill Paul 	sc->dc_link = 0;
372596f2e892SBill Paul 
372696f2e892SBill Paul 	/*
372796f2e892SBill Paul 	 * Free data in the RX lists.
372896f2e892SBill Paul 	 */
372996f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3730b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
373156e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
373256e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
373396f2e892SBill Paul 		}
373496f2e892SBill Paul 	}
3735b3811c95SMaxime Henrion 	bzero(&ld->dc_rx_list, sizeof(ld->dc_rx_list));
373696f2e892SBill Paul 
373796f2e892SBill Paul 	/*
373896f2e892SBill Paul 	 * Free the TX list buffers.
373996f2e892SBill Paul 	 */
374096f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3741b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
3742af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
3743af4358c7SMaxime Henrion 			if ((ctl & DC_TXCTL_SETUP) ||
3744af4358c7SMaxime Henrion 			    !(ctl & DC_TXCTL_FIRSTFRAG)) {
3745b3811c95SMaxime Henrion 				cd->dc_tx_chain[i] = NULL;
374696f2e892SBill Paul 				continue;
374796f2e892SBill Paul 			}
374856e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag, cd->dc_tx_map[i]);
374956e5e7aeSMaxime Henrion 			m_freem(cd->dc_tx_chain[i]);
3750b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
375196f2e892SBill Paul 		}
375296f2e892SBill Paul 	}
3753b3811c95SMaxime Henrion 	bzero(&ld->dc_tx_list, sizeof(ld->dc_tx_list));
375496f2e892SBill Paul 
3755d1ce9105SBill Paul 	DC_UNLOCK(sc);
375696f2e892SBill Paul }
375796f2e892SBill Paul 
375896f2e892SBill Paul /*
3759e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3760e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3761e8388e14SMitsuru IWASAKI  * resume.
3762e8388e14SMitsuru IWASAKI  */
3763e3d2833aSAlfred Perlstein static int
37640934f18aSMaxime Henrion dc_suspend(device_t dev)
3765e8388e14SMitsuru IWASAKI {
3766e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
37670934f18aSMaxime Henrion 	int i, s;
3768e8388e14SMitsuru IWASAKI 
3769e8388e14SMitsuru IWASAKI 	s = splimp();
3770e8388e14SMitsuru IWASAKI 
3771e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3772e8388e14SMitsuru IWASAKI 
3773e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3774e8388e14SMitsuru IWASAKI 
3775e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3776e8388e14SMitsuru IWASAKI 		sc->saved_maps[i] = pci_read_config(dev, PCIR_MAPS + i * 4, 4);
3777e8388e14SMitsuru IWASAKI 	sc->saved_biosaddr = pci_read_config(dev, PCIR_BIOS, 4);
3778e8388e14SMitsuru IWASAKI 	sc->saved_intline = pci_read_config(dev, PCIR_INTLINE, 1);
3779e8388e14SMitsuru IWASAKI 	sc->saved_cachelnsz = pci_read_config(dev, PCIR_CACHELNSZ, 1);
3780e8388e14SMitsuru IWASAKI 	sc->saved_lattimer = pci_read_config(dev, PCIR_LATTIMER, 1);
3781e8388e14SMitsuru IWASAKI 
3782e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3783e8388e14SMitsuru IWASAKI 
3784e8388e14SMitsuru IWASAKI 	splx(s);
3785e8388e14SMitsuru IWASAKI 	return (0);
3786e8388e14SMitsuru IWASAKI }
3787e8388e14SMitsuru IWASAKI 
3788e8388e14SMitsuru IWASAKI /*
3789e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3790e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3791e8388e14SMitsuru IWASAKI  * appropriate.
3792e8388e14SMitsuru IWASAKI  */
3793e3d2833aSAlfred Perlstein static int
37940934f18aSMaxime Henrion dc_resume(device_t dev)
3795e8388e14SMitsuru IWASAKI {
3796e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3797e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
37980934f18aSMaxime Henrion 	int i, s;
3799e8388e14SMitsuru IWASAKI 
3800e8388e14SMitsuru IWASAKI 	s = splimp();
3801e8388e14SMitsuru IWASAKI 
3802e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3803e8388e14SMitsuru IWASAKI 	ifp = &sc->arpcom.ac_if;
3804b84e866aSWarner Losh #ifndef BURN_BRIDGES
3805e8388e14SMitsuru IWASAKI 	dc_acpi(dev);
3806b84e866aSWarner Losh #endif
3807e8388e14SMitsuru IWASAKI 	/* better way to do this? */
3808e8388e14SMitsuru IWASAKI 	for (i = 0; i < 5; i++)
3809e8388e14SMitsuru IWASAKI 		pci_write_config(dev, PCIR_MAPS + i * 4, sc->saved_maps[i], 4);
3810e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_BIOS, sc->saved_biosaddr, 4);
3811e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_INTLINE, sc->saved_intline, 1);
3812e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_CACHELNSZ, sc->saved_cachelnsz, 1);
3813e8388e14SMitsuru IWASAKI 	pci_write_config(dev, PCIR_LATTIMER, sc->saved_lattimer, 1);
3814e8388e14SMitsuru IWASAKI 
3815e8388e14SMitsuru IWASAKI 	/* reenable busmastering */
3816e8388e14SMitsuru IWASAKI 	pci_enable_busmaster(dev);
3817e8388e14SMitsuru IWASAKI 	pci_enable_io(dev, DC_RES);
3818e8388e14SMitsuru IWASAKI 
3819e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3820e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3821e8388e14SMitsuru IWASAKI 		dc_init(sc);
3822e8388e14SMitsuru IWASAKI 
3823e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3824e8388e14SMitsuru IWASAKI 
3825e8388e14SMitsuru IWASAKI 	splx(s);
3826e8388e14SMitsuru IWASAKI 	return (0);
3827e8388e14SMitsuru IWASAKI }
3828e8388e14SMitsuru IWASAKI 
3829e8388e14SMitsuru IWASAKI /*
383096f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
383196f2e892SBill Paul  * get confused by errant DMAs when rebooting.
383296f2e892SBill Paul  */
3833e3d2833aSAlfred Perlstein static void
38340934f18aSMaxime Henrion dc_shutdown(device_t dev)
383596f2e892SBill Paul {
383696f2e892SBill Paul 	struct dc_softc *sc;
383796f2e892SBill Paul 
383896f2e892SBill Paul 	sc = device_get_softc(dev);
383996f2e892SBill Paul 
384096f2e892SBill Paul 	dc_stop(sc);
384196f2e892SBill Paul }
3842