xref: /freebsd/sys/dev/dc/if_dc.c (revision d0d67284f8dd998545e70830b375a9fa90e55255)
160727d8bSWarner Losh /*-
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 
334dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
344dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
354dc52c32SDavid E. O'Brien 
3696f2e892SBill Paul /*
3796f2e892SBill Paul  * DEC "tulip" clone ethernet driver. Supports the DEC/Intel 21143
3896f2e892SBill Paul  * series chips and several workalikes including the following:
3996f2e892SBill Paul  *
40ead7cde9SBill Paul  * Macronix 98713/98715/98725/98727/98732 PMAC (www.macronix.com)
4196f2e892SBill Paul  * Macronix/Lite-On 82c115 PNIC II (www.macronix.com)
4296f2e892SBill Paul  * Lite-On 82c168/82c169 PNIC (www.litecom.com)
4396f2e892SBill Paul  * ASIX Electronics AX88140A (www.asix.com.tw)
4496f2e892SBill Paul  * ASIX Electronics AX88141 (www.asix.com.tw)
4596f2e892SBill Paul  * ADMtek AL981 (www.admtek.com.tw)
4696f2e892SBill Paul  * ADMtek AN985 (www.admtek.com.tw)
474c16d09eSWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek AN985
4888d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
499ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
50feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
511d5e5310SBill Paul  * Abocom FE2500
521af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
537eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5496f2e892SBill Paul  *
5596f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5696f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5796f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5896f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
5996f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
6096f2e892SBill Paul  * instead of 512.
6196f2e892SBill Paul  *
6296f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6396f2e892SBill Paul  * Electrical Engineering Department
6496f2e892SBill Paul  * Columbia University, New York City
6596f2e892SBill Paul  */
6696f2e892SBill Paul /*
6796f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6896f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
6996f2e892SBill Paul  * three kinds of media attachments:
7096f2e892SBill Paul  *
7196f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7296f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7396f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7496f2e892SBill Paul  * o 10baseT port.
7596f2e892SBill Paul  * o AUI/BNC port.
7696f2e892SBill Paul  *
7796f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7896f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
7996f2e892SBill Paul  * autosensing configuration.
8096f2e892SBill Paul  *
8196f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8296f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8396f2e892SBill Paul  * handled separately due to its different register offsets and the
8496f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8596f2e892SBill Paul  * here, but I'm not thrilled about it.
8696f2e892SBill Paul  *
8796f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8896f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
8996f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
9096f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
9196f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9296f2e892SBill Paul  */
9396f2e892SBill Paul 
94f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
95f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
96f0796cd2SGleb Smirnoff #endif
97f0796cd2SGleb Smirnoff 
9896f2e892SBill Paul #include <sys/param.h>
99af4358c7SMaxime Henrion #include <sys/endian.h>
10096f2e892SBill Paul #include <sys/systm.h>
10196f2e892SBill Paul #include <sys/sockio.h>
10296f2e892SBill Paul #include <sys/mbuf.h>
10396f2e892SBill Paul #include <sys/malloc.h>
10496f2e892SBill Paul #include <sys/kernel.h>
105f11d01c3SPoul-Henning Kamp #include <sys/module.h>
10696f2e892SBill Paul #include <sys/socket.h>
10796f2e892SBill Paul 
10896f2e892SBill Paul #include <net/if.h>
10996f2e892SBill Paul #include <net/if_arp.h>
11096f2e892SBill Paul #include <net/ethernet.h>
11196f2e892SBill Paul #include <net/if_dl.h>
11296f2e892SBill Paul #include <net/if_media.h>
113db40c1aeSDoug Ambrisko #include <net/if_types.h>
114db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11596f2e892SBill Paul 
11696f2e892SBill Paul #include <net/bpf.h>
11796f2e892SBill Paul 
11896f2e892SBill Paul #include <machine/bus.h>
11996f2e892SBill Paul #include <machine/resource.h>
12096f2e892SBill Paul #include <sys/bus.h>
12196f2e892SBill Paul #include <sys/rman.h>
12296f2e892SBill Paul 
12396f2e892SBill Paul #include <dev/mii/mii.h>
12496f2e892SBill Paul #include <dev/mii/miivar.h>
12596f2e892SBill Paul 
12619b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
12719b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
12896f2e892SBill Paul 
12996f2e892SBill Paul #define DC_USEIOSPACE
13096f2e892SBill Paul 
1316a3033a8SWarner Losh #include <dev/dc/if_dcreg.h>
13296f2e892SBill Paul 
133ec6a7299SMaxime Henrion #ifdef __sparc64__
134ec6a7299SMaxime Henrion #include <dev/ofw/openfirm.h>
135ec6a7299SMaxime Henrion #include <machine/ofw_machdep.h>
136ec6a7299SMaxime Henrion #endif
137ec6a7299SMaxime Henrion 
138f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
139f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
14095a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14195a16455SPeter Wemm 
142919ccba7SWarner Losh /*
143919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
144919ccba7SWarner Losh  * errors here.
145919ccba7SWarner Losh  */
14696f2e892SBill Paul #include "miibus_if.h"
14796f2e892SBill Paul 
14896f2e892SBill Paul /*
14996f2e892SBill Paul  * Various supported device vendors/types and their names.
15096f2e892SBill Paul  */
151ebc284ccSMarius Strobl static const struct dc_type dc_devs[] = {
1521e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
15396f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
1541e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
15538deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
1561e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
15796f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
1581e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
15988d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
1601e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
1611e2e70b1SJohn Baldwin 		"Davicom DM9102 10/100BaseTX" },
1621e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
16396f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
1641e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
16596f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
1661e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
167e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1681e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
169e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1701e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_FA511), 0,
1714c16d09eSWarner Losh 		"Netgear FA511 10/100BaseTX" },
1721e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
17396f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
1741e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
1751e2e70b1SJohn Baldwin 		"ASIX AX88140A 10/100BaseTX" },
1761e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
17796f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
1781e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
1791e2e70b1SJohn Baldwin 		"Macronix 98713 10/100BaseTX" },
1801e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
18196f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1821e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
18396f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1841e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
18596f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
1861e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
1871e2e70b1SJohn Baldwin 		"Macronix 98715AEC-C 10/100BaseTX" },
1881e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
1891e2e70b1SJohn Baldwin 		"Macronix 98715/98715A 10/100BaseTX" },
1901e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
191ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
1921e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
19396f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
1941e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
19596f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1961e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
1971e2e70b1SJohn Baldwin 		"82c168 PNIC 10/100BaseTX" },
1981e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
1999ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
2001e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
201fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
2021e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
203feb78939SJonathan Chen 		"Xircom X3201 10/100BaseTX" },
2041e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
2059be0993cSJohn Baldwin 		"Neteasy DRP-32TXD Cardbus 10/100" },
2061e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
2071d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2081e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
209773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2101e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
2111af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
2121e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
213948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
2141e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
21597f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2161e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
2177eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
2181e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
219e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
2201e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
221e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22217762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
22317762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22417762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
22517762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22696f2e892SBill Paul 	{ 0, 0, NULL }
22796f2e892SBill Paul };
22896f2e892SBill Paul 
229e51a25f8SAlfred Perlstein static int dc_probe(device_t);
230e51a25f8SAlfred Perlstein static int dc_attach(device_t);
231e51a25f8SAlfred Perlstein static int dc_detach(device_t);
232e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
233e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
234ebc284ccSMarius Strobl static const struct dc_type *dc_devtype(device_t);
23556e5e7aeSMaxime Henrion static int dc_newbuf(struct dc_softc *, int, int);
236a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
237e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
238e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
239e51a25f8SAlfred Perlstein static void dc_rxeof(struct dc_softc *);
240e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
241e51a25f8SAlfred Perlstein static void dc_tick(void *);
242e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
243e51a25f8SAlfred Perlstein static void dc_intr(void *);
244e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
245c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
246e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
247e51a25f8SAlfred Perlstein static void dc_init(void *);
248c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
249e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
250b1d16143SMarius Strobl static void dc_watchdog(void *);
2516a087a87SPyun YongHyeon static int dc_shutdown(device_t);
252e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
253e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
25496f2e892SBill Paul 
255e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
256e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
257e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
258e51a25f8SAlfred Perlstein static void dc_eeprom_getword(struct dc_softc *, int, u_int16_t *);
259d24ae19dSWarner Losh static void dc_eeprom_getword_pnic(struct dc_softc *, int, u_int16_t *);
260d24ae19dSWarner Losh static void dc_eeprom_getword_xircom(struct dc_softc *, int, u_int16_t *);
2613097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
262e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
26396f2e892SBill Paul 
264e51a25f8SAlfred Perlstein static void dc_mii_writebit(struct dc_softc *, int);
265e51a25f8SAlfred Perlstein static int dc_mii_readbit(struct dc_softc *);
266e51a25f8SAlfred Perlstein static void dc_mii_sync(struct dc_softc *);
267e51a25f8SAlfred Perlstein static void dc_mii_send(struct dc_softc *, u_int32_t, int);
268e51a25f8SAlfred Perlstein static int dc_mii_readreg(struct dc_softc *, struct dc_mii_frame *);
269e51a25f8SAlfred Perlstein static int dc_mii_writereg(struct dc_softc *, struct dc_mii_frame *);
270e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
271e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
272e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
273e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
27496f2e892SBill Paul 
275e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2763373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2773373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
278e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
279e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
280e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
281e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
28296f2e892SBill Paul 
283e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
28496f2e892SBill Paul 
285e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
286e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
287e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
28896f2e892SBill Paul 
2893097aa70SWarner Losh static void dc_read_srom(struct dc_softc *, int);
290e51a25f8SAlfred Perlstein static void dc_parse_21143_srom(struct dc_softc *);
291e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
292e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
293e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
294e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
2955c1cfac4SBill Paul 
29696f2e892SBill Paul #ifdef DC_USEIOSPACE
29796f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
29896f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
29996f2e892SBill Paul #else
30096f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
30196f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
30296f2e892SBill Paul #endif
30396f2e892SBill Paul 
30496f2e892SBill Paul static device_method_t dc_methods[] = {
30596f2e892SBill Paul 	/* Device interface */
30696f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
30796f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
30896f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
309e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
310e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
31196f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
31296f2e892SBill Paul 
31396f2e892SBill Paul 	/* bus interface */
31496f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
31596f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
31696f2e892SBill Paul 
31796f2e892SBill Paul 	/* MII interface */
31896f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
31996f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
32096f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
321f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
32296f2e892SBill Paul 
32396f2e892SBill Paul 	{ 0, 0 }
32496f2e892SBill Paul };
32596f2e892SBill Paul 
32696f2e892SBill Paul static driver_t dc_driver = {
32796f2e892SBill Paul 	"dc",
32896f2e892SBill Paul 	dc_methods,
32996f2e892SBill Paul 	sizeof(struct dc_softc)
33096f2e892SBill Paul };
33196f2e892SBill Paul 
33296f2e892SBill Paul static devclass_t dc_devclass;
33396f2e892SBill Paul 
334347934faSWarner Losh DRIVER_MODULE(dc, cardbus, dc_driver, dc_devclass, 0, 0);
335f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
33696f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
33796f2e892SBill Paul 
33896f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
33996f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
34096f2e892SBill Paul 
34196f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
34296f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
34396f2e892SBill Paul 
34496f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
34596f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
34696f2e892SBill Paul 
347e3d2833aSAlfred Perlstein static void
3480934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
34996f2e892SBill Paul {
35096f2e892SBill Paul 	int idx;
35196f2e892SBill Paul 
35296f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
35396f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
35496f2e892SBill Paul }
35596f2e892SBill Paul 
3562c876e15SPoul-Henning Kamp static void
3570934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3583097aa70SWarner Losh {
3593097aa70SWarner Losh 	int i;
3603097aa70SWarner Losh 
3613097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3623097aa70SWarner Losh 	dc_eeprom_idle(sc);
3633097aa70SWarner Losh 
3643097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3653097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3663097aa70SWarner Losh 	dc_delay(sc);
3673097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3683097aa70SWarner Losh 	dc_delay(sc);
3693097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3703097aa70SWarner Losh 	dc_delay(sc);
3713097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3723097aa70SWarner Losh 	dc_delay(sc);
3733097aa70SWarner Losh 
3743097aa70SWarner Losh 	for (i = 3; i--;) {
3753097aa70SWarner Losh 		if (6 & (1 << i))
3763097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3773097aa70SWarner Losh 		else
3783097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3793097aa70SWarner Losh 		dc_delay(sc);
3803097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3813097aa70SWarner Losh 		dc_delay(sc);
3823097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3833097aa70SWarner Losh 		dc_delay(sc);
3843097aa70SWarner Losh 	}
3853097aa70SWarner Losh 
3863097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3873097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3883097aa70SWarner Losh 		dc_delay(sc);
3893097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3903097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3913097aa70SWarner Losh 			dc_delay(sc);
3923097aa70SWarner Losh 			break;
3933097aa70SWarner Losh 		}
3943097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3953097aa70SWarner Losh 		dc_delay(sc);
3963097aa70SWarner Losh 	}
3973097aa70SWarner Losh 
3983097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
3993097aa70SWarner Losh 	dc_eeprom_idle(sc);
4003097aa70SWarner Losh 
4013097aa70SWarner Losh 	if (i < 4 || i > 12)
4023097aa70SWarner Losh 		sc->dc_romwidth = 6;
4033097aa70SWarner Losh 	else
4043097aa70SWarner Losh 		sc->dc_romwidth = i;
4053097aa70SWarner Losh 
4063097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4073097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4083097aa70SWarner Losh 	dc_delay(sc);
4093097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4103097aa70SWarner Losh 	dc_delay(sc);
4113097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4123097aa70SWarner Losh 	dc_delay(sc);
4133097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4143097aa70SWarner Losh 	dc_delay(sc);
4153097aa70SWarner Losh 
4163097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4173097aa70SWarner Losh 	dc_eeprom_idle(sc);
4183097aa70SWarner Losh }
4193097aa70SWarner Losh 
420e3d2833aSAlfred Perlstein static void
4210934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
42296f2e892SBill Paul {
4230934f18aSMaxime Henrion 	int i;
42496f2e892SBill Paul 
42596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
42696f2e892SBill Paul 	dc_delay(sc);
42796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
42896f2e892SBill Paul 	dc_delay(sc);
42996f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43096f2e892SBill Paul 	dc_delay(sc);
43196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
43296f2e892SBill Paul 	dc_delay(sc);
43396f2e892SBill Paul 
43496f2e892SBill Paul 	for (i = 0; i < 25; i++) {
43596f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43696f2e892SBill Paul 		dc_delay(sc);
43796f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43896f2e892SBill Paul 		dc_delay(sc);
43996f2e892SBill Paul 	}
44096f2e892SBill Paul 
44196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44296f2e892SBill Paul 	dc_delay(sc);
44396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
44496f2e892SBill Paul 	dc_delay(sc);
44596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
44696f2e892SBill Paul }
44796f2e892SBill Paul 
44896f2e892SBill Paul /*
44996f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
45096f2e892SBill Paul  */
451e3d2833aSAlfred Perlstein static void
4520934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
45396f2e892SBill Paul {
4540934f18aSMaxime Henrion 	int d, i;
45596f2e892SBill Paul 
4563097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4573097aa70SWarner Losh 	for (i = 3; i--; ) {
4583097aa70SWarner Losh 		if (d & (1 << i))
4593097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
46096f2e892SBill Paul 		else
4613097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4623097aa70SWarner Losh 		dc_delay(sc);
4633097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4643097aa70SWarner Losh 		dc_delay(sc);
4653097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4663097aa70SWarner Losh 		dc_delay(sc);
4673097aa70SWarner Losh 	}
46896f2e892SBill Paul 
46996f2e892SBill Paul 	/*
47096f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
47196f2e892SBill Paul 	 */
4723097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4733097aa70SWarner Losh 		if (addr & (1 << i)) {
47496f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
47596f2e892SBill Paul 		} else {
47696f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
47796f2e892SBill Paul 		}
47896f2e892SBill Paul 		dc_delay(sc);
47996f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48096f2e892SBill Paul 		dc_delay(sc);
48196f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
48296f2e892SBill Paul 		dc_delay(sc);
48396f2e892SBill Paul 	}
48496f2e892SBill Paul }
48596f2e892SBill Paul 
48696f2e892SBill Paul /*
48796f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
48896f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
48996f2e892SBill Paul  * the EEPROM.
49096f2e892SBill Paul  */
491e3d2833aSAlfred Perlstein static void
4920934f18aSMaxime Henrion dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, u_int16_t *dest)
49396f2e892SBill Paul {
4940934f18aSMaxime Henrion 	int i;
49596f2e892SBill Paul 	u_int32_t r;
49696f2e892SBill Paul 
49796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
49896f2e892SBill Paul 
49996f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
50096f2e892SBill Paul 		DELAY(1);
50196f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
50296f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
50396f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
50496f2e892SBill Paul 			return;
50596f2e892SBill Paul 		}
50696f2e892SBill Paul 	}
50796f2e892SBill Paul }
50896f2e892SBill Paul 
50996f2e892SBill Paul /*
51096f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
511feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
512feb78939SJonathan Chen  * the EEPROM, too.
513feb78939SJonathan Chen  */
514e3d2833aSAlfred Perlstein static void
5150934f18aSMaxime Henrion dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, u_int16_t *dest)
516feb78939SJonathan Chen {
5170934f18aSMaxime Henrion 
518feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
519feb78939SJonathan Chen 
520feb78939SJonathan Chen 	addr *= 2;
521feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
522feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
523feb78939SJonathan Chen 	addr += 1;
524feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
525feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
526feb78939SJonathan Chen 
527feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
528feb78939SJonathan Chen }
529feb78939SJonathan Chen 
530feb78939SJonathan Chen /*
531feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
53296f2e892SBill Paul  */
533e3d2833aSAlfred Perlstein static void
5340934f18aSMaxime Henrion dc_eeprom_getword(struct dc_softc *sc, int addr, u_int16_t *dest)
53596f2e892SBill Paul {
5360934f18aSMaxime Henrion 	int i;
53796f2e892SBill Paul 	u_int16_t word = 0;
53896f2e892SBill Paul 
53996f2e892SBill Paul 	/* Force EEPROM to idle state. */
54096f2e892SBill Paul 	dc_eeprom_idle(sc);
54196f2e892SBill Paul 
54296f2e892SBill Paul 	/* Enter EEPROM access mode. */
54396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
54496f2e892SBill Paul 	dc_delay(sc);
54596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
54696f2e892SBill Paul 	dc_delay(sc);
54796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
54896f2e892SBill Paul 	dc_delay(sc);
54996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55096f2e892SBill Paul 	dc_delay(sc);
55196f2e892SBill Paul 
55296f2e892SBill Paul 	/*
55396f2e892SBill Paul 	 * Send address of word we want to read.
55496f2e892SBill Paul 	 */
55596f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
55696f2e892SBill Paul 
55796f2e892SBill Paul 	/*
55896f2e892SBill Paul 	 * Start reading bits from EEPROM.
55996f2e892SBill Paul 	 */
56096f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56196f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
56296f2e892SBill Paul 		dc_delay(sc);
56396f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
56496f2e892SBill Paul 			word |= i;
56596f2e892SBill Paul 		dc_delay(sc);
56696f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
56796f2e892SBill Paul 		dc_delay(sc);
56896f2e892SBill Paul 	}
56996f2e892SBill Paul 
57096f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57196f2e892SBill Paul 	dc_eeprom_idle(sc);
57296f2e892SBill Paul 
57396f2e892SBill Paul 	*dest = word;
57496f2e892SBill Paul }
57596f2e892SBill Paul 
57696f2e892SBill Paul /*
57796f2e892SBill Paul  * Read a sequence of words from the EEPROM.
57896f2e892SBill Paul  */
579e3d2833aSAlfred Perlstein static void
5808c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
58196f2e892SBill Paul {
58296f2e892SBill Paul 	int i;
58396f2e892SBill Paul 	u_int16_t word = 0, *ptr;
58496f2e892SBill Paul 
58596f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
58696f2e892SBill Paul 		if (DC_IS_PNIC(sc))
58796f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
588feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
589feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59096f2e892SBill Paul 		else
59196f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
59296f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
5938c7ff1f3SMaxime Henrion 		if (be)
5948c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
59596f2e892SBill Paul 		else
5968c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
59796f2e892SBill Paul 	}
59896f2e892SBill Paul }
59996f2e892SBill Paul 
60096f2e892SBill Paul /*
60196f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
60296f2e892SBill Paul  * Application Notes pp.19-21.
60396f2e892SBill Paul  */
60496f2e892SBill Paul /*
60596f2e892SBill Paul  * Write a bit to the MII bus.
60696f2e892SBill Paul  */
607e3d2833aSAlfred Perlstein static void
6080934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
60996f2e892SBill Paul {
6100934f18aSMaxime Henrion 
61196f2e892SBill Paul 	if (bit)
61296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
61396f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE | DC_SIO_MII_DATAOUT);
61496f2e892SBill Paul 	else
61596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
61696f2e892SBill Paul 
61796f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
61896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
61996f2e892SBill Paul }
62096f2e892SBill Paul 
62196f2e892SBill Paul /*
62296f2e892SBill Paul  * Read a bit from the MII bus.
62396f2e892SBill Paul  */
624e3d2833aSAlfred Perlstein static int
6250934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
62696f2e892SBill Paul {
6270934f18aSMaxime Henrion 
62896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR);
62996f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
63096f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63296f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
63396f2e892SBill Paul 		return (1);
63496f2e892SBill Paul 
63596f2e892SBill Paul 	return (0);
63696f2e892SBill Paul }
63796f2e892SBill Paul 
63896f2e892SBill Paul /*
63996f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
64096f2e892SBill Paul  */
641e3d2833aSAlfred Perlstein static void
6420934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
64396f2e892SBill Paul {
6440934f18aSMaxime Henrion 	int i;
64596f2e892SBill Paul 
64696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
64796f2e892SBill Paul 
64896f2e892SBill Paul 	for (i = 0; i < 32; i++)
64996f2e892SBill Paul 		dc_mii_writebit(sc, 1);
65096f2e892SBill Paul }
65196f2e892SBill Paul 
65296f2e892SBill Paul /*
65396f2e892SBill Paul  * Clock a series of bits through the MII.
65496f2e892SBill Paul  */
655e3d2833aSAlfred Perlstein static void
6560934f18aSMaxime Henrion dc_mii_send(struct dc_softc *sc, u_int32_t bits, int cnt)
65796f2e892SBill Paul {
65896f2e892SBill Paul 	int i;
65996f2e892SBill Paul 
66096f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
66196f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
66296f2e892SBill Paul }
66396f2e892SBill Paul 
66496f2e892SBill Paul /*
66596f2e892SBill Paul  * Read an PHY register through the MII.
66696f2e892SBill Paul  */
667e3d2833aSAlfred Perlstein static int
6680934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
66996f2e892SBill Paul {
670d1ce9105SBill Paul 	int i, ack;
67196f2e892SBill Paul 
67296f2e892SBill Paul 	/*
67396f2e892SBill Paul 	 * Set up frame for RX.
67496f2e892SBill Paul 	 */
67596f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
67696f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
67796f2e892SBill Paul 	frame->mii_turnaround = 0;
67896f2e892SBill Paul 	frame->mii_data = 0;
67996f2e892SBill Paul 
68096f2e892SBill Paul 	/*
68196f2e892SBill Paul 	 * Sync the PHYs.
68296f2e892SBill Paul 	 */
68396f2e892SBill Paul 	dc_mii_sync(sc);
68496f2e892SBill Paul 
68596f2e892SBill Paul 	/*
68696f2e892SBill Paul 	 * Send command/address info.
68796f2e892SBill Paul 	 */
68896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
68996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
69096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
69196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
69296f2e892SBill Paul 
69396f2e892SBill Paul #ifdef notdef
69496f2e892SBill Paul 	/* Idle bit */
69596f2e892SBill Paul 	dc_mii_writebit(sc, 1);
69696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
69796f2e892SBill Paul #endif
69896f2e892SBill Paul 
6990934f18aSMaxime Henrion 	/* Check for ack. */
70096f2e892SBill Paul 	ack = dc_mii_readbit(sc);
70196f2e892SBill Paul 
70296f2e892SBill Paul 	/*
70396f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
70496f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
70596f2e892SBill Paul 	 */
70696f2e892SBill Paul 	if (ack) {
7070934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
70896f2e892SBill Paul 			dc_mii_readbit(sc);
70996f2e892SBill Paul 		goto fail;
71096f2e892SBill Paul 	}
71196f2e892SBill Paul 
71296f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
71396f2e892SBill Paul 		if (!ack) {
71496f2e892SBill Paul 			if (dc_mii_readbit(sc))
71596f2e892SBill Paul 				frame->mii_data |= i;
71696f2e892SBill Paul 		}
71796f2e892SBill Paul 	}
71896f2e892SBill Paul 
71996f2e892SBill Paul fail:
72096f2e892SBill Paul 
72196f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72396f2e892SBill Paul 
72496f2e892SBill Paul 	if (ack)
72596f2e892SBill Paul 		return (1);
72696f2e892SBill Paul 	return (0);
72796f2e892SBill Paul }
72896f2e892SBill Paul 
72996f2e892SBill Paul /*
73096f2e892SBill Paul  * Write to a PHY register through the MII.
73196f2e892SBill Paul  */
732e3d2833aSAlfred Perlstein static int
7330934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
73496f2e892SBill Paul {
7350934f18aSMaxime Henrion 
73696f2e892SBill Paul 	/*
73796f2e892SBill Paul 	 * Set up frame for TX.
73896f2e892SBill Paul 	 */
73996f2e892SBill Paul 
74096f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
74196f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
74296f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
74396f2e892SBill Paul 
74496f2e892SBill Paul 	/*
74596f2e892SBill Paul 	 * Sync the PHYs.
74696f2e892SBill Paul 	 */
74796f2e892SBill Paul 	dc_mii_sync(sc);
74896f2e892SBill Paul 
74996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
75096f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
75196f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
75296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
75396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
75496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
75596f2e892SBill Paul 
75696f2e892SBill Paul 	/* Idle bit. */
75796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
75896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
75996f2e892SBill Paul 
76096f2e892SBill Paul 	return (0);
76196f2e892SBill Paul }
76296f2e892SBill Paul 
763e3d2833aSAlfred Perlstein static int
7640934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
76596f2e892SBill Paul {
76696f2e892SBill Paul 	struct dc_mii_frame frame;
76796f2e892SBill Paul 	struct dc_softc	 *sc;
768c85c4667SBill Paul 	int i, rval, phy_reg = 0;
76996f2e892SBill Paul 
77096f2e892SBill Paul 	sc = device_get_softc(dev);
7710934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
77296f2e892SBill Paul 
77396f2e892SBill Paul 	/*
77496f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
77596f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
77696f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
77796f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
77896f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
77996f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
78096f2e892SBill Paul 	 * that the PHY is at MII address 1.
78196f2e892SBill Paul 	 */
78296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
78396f2e892SBill Paul 		return (0);
78496f2e892SBill Paul 
7851af8bec7SBill Paul 	/*
7861af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7871af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7881af8bec7SBill Paul 	 * so we only respond to correct one.
7891af8bec7SBill Paul 	 */
7901af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
7911af8bec7SBill Paul 		return (0);
7921af8bec7SBill Paul 
7935c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
79496f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
79596f2e892SBill Paul 			switch (reg) {
79696f2e892SBill Paul 			case MII_BMSR:
79796f2e892SBill Paul 			/*
79896f2e892SBill Paul 			 * Fake something to make the probe
79996f2e892SBill Paul 			 * code think there's a PHY here.
80096f2e892SBill Paul 			 */
80196f2e892SBill Paul 				return (BMSR_MEDIAMASK);
80296f2e892SBill Paul 				break;
80396f2e892SBill Paul 			case MII_PHYIDR1:
80496f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80596f2e892SBill Paul 					return (DC_VENDORID_LO);
80696f2e892SBill Paul 				return (DC_VENDORID_DEC);
80796f2e892SBill Paul 				break;
80896f2e892SBill Paul 			case MII_PHYIDR2:
80996f2e892SBill Paul 				if (DC_IS_PNIC(sc))
81096f2e892SBill Paul 					return (DC_DEVICEID_82C168);
81196f2e892SBill Paul 				return (DC_DEVICEID_21143);
81296f2e892SBill Paul 				break;
81396f2e892SBill Paul 			default:
81496f2e892SBill Paul 				return (0);
81596f2e892SBill Paul 				break;
81696f2e892SBill Paul 			}
81796f2e892SBill Paul 		} else
81896f2e892SBill Paul 			return (0);
81996f2e892SBill Paul 	}
82096f2e892SBill Paul 
82196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
82296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
82396f2e892SBill Paul 		    (phy << 23) | (reg << 18));
82496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
82596f2e892SBill Paul 			DELAY(1);
82696f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
82796f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
82896f2e892SBill Paul 				rval &= 0xFFFF;
82996f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
83096f2e892SBill Paul 			}
83196f2e892SBill Paul 		}
83296f2e892SBill Paul 		return (0);
83396f2e892SBill Paul 	}
83496f2e892SBill Paul 
83596f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
83696f2e892SBill Paul 		switch (reg) {
83796f2e892SBill Paul 		case MII_BMCR:
83896f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
83996f2e892SBill Paul 			break;
84096f2e892SBill Paul 		case MII_BMSR:
84196f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
84296f2e892SBill Paul 			break;
84396f2e892SBill Paul 		case MII_PHYIDR1:
84496f2e892SBill Paul 			phy_reg = DC_AL_VENID;
84596f2e892SBill Paul 			break;
84696f2e892SBill Paul 		case MII_PHYIDR2:
84796f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
84896f2e892SBill Paul 			break;
84996f2e892SBill Paul 		case MII_ANAR:
85096f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
85196f2e892SBill Paul 			break;
85296f2e892SBill Paul 		case MII_ANLPAR:
85396f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
85496f2e892SBill Paul 			break;
85596f2e892SBill Paul 		case MII_ANER:
85696f2e892SBill Paul 			phy_reg = DC_AL_ANER;
85796f2e892SBill Paul 			break;
85896f2e892SBill Paul 		default:
85922f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
86022f6205dSJohn Baldwin 			    reg);
86196f2e892SBill Paul 			return (0);
86296f2e892SBill Paul 			break;
86396f2e892SBill Paul 		}
86496f2e892SBill Paul 
86596f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
86696f2e892SBill Paul 
86796f2e892SBill Paul 		if (rval == 0xFFFF)
86896f2e892SBill Paul 			return (0);
86996f2e892SBill Paul 		return (rval);
87096f2e892SBill Paul 	}
87196f2e892SBill Paul 
87296f2e892SBill Paul 	frame.mii_phyaddr = phy;
87396f2e892SBill Paul 	frame.mii_regaddr = reg;
874419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
875f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
876f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
877419146d9SBill Paul 	}
87896f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
879419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
880f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
88196f2e892SBill Paul 
88296f2e892SBill Paul 	return (frame.mii_data);
88396f2e892SBill Paul }
88496f2e892SBill Paul 
885e3d2833aSAlfred Perlstein static int
8860934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
88796f2e892SBill Paul {
88896f2e892SBill Paul 	struct dc_softc *sc;
88996f2e892SBill Paul 	struct dc_mii_frame frame;
890c85c4667SBill Paul 	int i, phy_reg = 0;
89196f2e892SBill Paul 
89296f2e892SBill Paul 	sc = device_get_softc(dev);
8930934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
89496f2e892SBill Paul 
89596f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
89696f2e892SBill Paul 		return (0);
89796f2e892SBill Paul 
8981af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
8991af8bec7SBill Paul 		return (0);
9001af8bec7SBill Paul 
90196f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
90296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
90396f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
90496f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
90596f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
90696f2e892SBill Paul 				break;
90796f2e892SBill Paul 		}
90896f2e892SBill Paul 		return (0);
90996f2e892SBill Paul 	}
91096f2e892SBill Paul 
91196f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
91296f2e892SBill Paul 		switch (reg) {
91396f2e892SBill Paul 		case MII_BMCR:
91496f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
91596f2e892SBill Paul 			break;
91696f2e892SBill Paul 		case MII_BMSR:
91796f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
91896f2e892SBill Paul 			break;
91996f2e892SBill Paul 		case MII_PHYIDR1:
92096f2e892SBill Paul 			phy_reg = DC_AL_VENID;
92196f2e892SBill Paul 			break;
92296f2e892SBill Paul 		case MII_PHYIDR2:
92396f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
92496f2e892SBill Paul 			break;
92596f2e892SBill Paul 		case MII_ANAR:
92696f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
92796f2e892SBill Paul 			break;
92896f2e892SBill Paul 		case MII_ANLPAR:
92996f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
93096f2e892SBill Paul 			break;
93196f2e892SBill Paul 		case MII_ANER:
93296f2e892SBill Paul 			phy_reg = DC_AL_ANER;
93396f2e892SBill Paul 			break;
93496f2e892SBill Paul 		default:
93522f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
93622f6205dSJohn Baldwin 			    reg);
93796f2e892SBill Paul 			return (0);
93896f2e892SBill Paul 			break;
93996f2e892SBill Paul 		}
94096f2e892SBill Paul 
94196f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
94296f2e892SBill Paul 		return (0);
94396f2e892SBill Paul 	}
94496f2e892SBill Paul 
94596f2e892SBill Paul 	frame.mii_phyaddr = phy;
94696f2e892SBill Paul 	frame.mii_regaddr = reg;
94796f2e892SBill Paul 	frame.mii_data = data;
94896f2e892SBill Paul 
949419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
950f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
951f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
952419146d9SBill Paul 	}
95396f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
954419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
955f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
95696f2e892SBill Paul 
95796f2e892SBill Paul 	return (0);
95896f2e892SBill Paul }
95996f2e892SBill Paul 
960e3d2833aSAlfred Perlstein static void
9610934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
96296f2e892SBill Paul {
96396f2e892SBill Paul 	struct dc_softc *sc;
96496f2e892SBill Paul 	struct mii_data *mii;
965f43d9309SBill Paul 	struct ifmedia *ifm;
96696f2e892SBill Paul 
96796f2e892SBill Paul 	sc = device_get_softc(dev);
96896f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
96996f2e892SBill Paul 		return;
9705c1cfac4SBill Paul 
97196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
972f43d9309SBill Paul 	ifm = &mii->mii_media;
973f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
97445521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
975f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
976f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
977f43d9309SBill Paul 	} else {
97896f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
97996f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
980f43d9309SBill Paul 	}
981f43d9309SBill Paul }
982f43d9309SBill Paul 
983f43d9309SBill Paul /*
984f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
985f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
986f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
987f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
988f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
989f43d9309SBill Paul  * with it itself. *sigh*
990f43d9309SBill Paul  */
991e3d2833aSAlfred Perlstein static void
9920934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
993f43d9309SBill Paul {
994f43d9309SBill Paul 	struct dc_softc *sc;
995f43d9309SBill Paul 	struct mii_data *mii;
996f43d9309SBill Paul 	struct ifmedia *ifm;
997f43d9309SBill Paul 	int rev;
998f43d9309SBill Paul 
9991e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
1000f43d9309SBill Paul 
1001f43d9309SBill Paul 	sc = device_get_softc(dev);
1002f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1003f43d9309SBill Paul 	ifm = &mii->mii_media;
1004f43d9309SBill Paul 
1005f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
100645521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
100796f2e892SBill Paul }
100896f2e892SBill Paul 
100979d11e09SBill Paul #define DC_BITS_512	9
101079d11e09SBill Paul #define DC_BITS_128	7
101179d11e09SBill Paul #define DC_BITS_64	6
101296f2e892SBill Paul 
10133373489bSWarner Losh static uint32_t
10143373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
101596f2e892SBill Paul {
10163373489bSWarner Losh 	uint32_t crc;
101796f2e892SBill Paul 
101896f2e892SBill Paul 	/* Compute CRC for the address value. */
10190e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
102096f2e892SBill Paul 
102179d11e09SBill Paul 	/*
102279d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
102379d11e09SBill Paul 	 * chips is only 128 bits wide.
102479d11e09SBill Paul 	 */
102579d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
102679d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
102796f2e892SBill Paul 
102879d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
102979d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
103079d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
103179d11e09SBill Paul 
1032feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1033feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1034feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1035feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10360934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1037feb78939SJonathan Chen 		else
10380934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10390934f18aSMaxime Henrion 			    (12 << 4));
1040feb78939SJonathan Chen 	}
1041feb78939SJonathan Chen 
104279d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
104396f2e892SBill Paul }
104496f2e892SBill Paul 
104596f2e892SBill Paul /*
104696f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
104796f2e892SBill Paul  */
10483373489bSWarner Losh static uint32_t
10493373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
105096f2e892SBill Paul {
10510e939c0cSChristian Weisgerber 	uint32_t crc;
105296f2e892SBill Paul 
105396f2e892SBill Paul 	/* Compute CRC for the address value. */
10540e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
105596f2e892SBill Paul 
10560934f18aSMaxime Henrion 	/* Return the filter bit position. */
105796f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
105896f2e892SBill Paul }
105996f2e892SBill Paul 
106096f2e892SBill Paul /*
106196f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
106296f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
106396f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
106496f2e892SBill Paul  *
106596f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
106696f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
106796f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
106896f2e892SBill Paul  * we need that too.
106996f2e892SBill Paul  */
10702c876e15SPoul-Henning Kamp static void
10710934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
107296f2e892SBill Paul {
10738df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
107496f2e892SBill Paul 	struct dc_desc *sframe;
107596f2e892SBill Paul 	u_int32_t h, *sp;
107696f2e892SBill Paul 	struct ifmultiaddr *ifma;
107796f2e892SBill Paul 	struct ifnet *ifp;
107896f2e892SBill Paul 	int i;
107996f2e892SBill Paul 
1080fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
108196f2e892SBill Paul 
108296f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
108396f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
108496f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
108596f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
108656e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
10870934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
108896f2e892SBill Paul 
1089af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1090af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1091af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
109296f2e892SBill Paul 
109356e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
109496f2e892SBill Paul 
109596f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
109696f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
109796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
109896f2e892SBill Paul 	else
109996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110096f2e892SBill Paul 
110196f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
110296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
110396f2e892SBill Paul 	else
110496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
110596f2e892SBill Paul 
110613b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
11076817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
110896f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
110996f2e892SBill Paul 			continue;
1110aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
111196f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1112af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
111396f2e892SBill Paul 	}
111413b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
111596f2e892SBill Paul 
111696f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1117aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1118af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
111996f2e892SBill Paul 	}
112096f2e892SBill Paul 
11218df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
11228df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11238df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
11248df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
11258df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
112696f2e892SBill Paul 
1127af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
112896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
112996f2e892SBill Paul 
113096f2e892SBill Paul 	/*
113196f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
113296f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
113396f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
113496f2e892SBill Paul 	 * medicine.
113596f2e892SBill Paul 	 */
113696f2e892SBill Paul 	DELAY(10000);
113796f2e892SBill Paul 
1138b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
113996f2e892SBill Paul }
114096f2e892SBill Paul 
11412c876e15SPoul-Henning Kamp static void
11420934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
114396f2e892SBill Paul {
11442e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
114596f2e892SBill Paul 	struct ifnet *ifp;
11460934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
114796f2e892SBill Paul 	int h = 0;
114896f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
114996f2e892SBill Paul 
1150fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
115196f2e892SBill Paul 
11520934f18aSMaxime Henrion 	/* Init our MAC address. */
11538df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11542e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
11552e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
11562e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
115796f2e892SBill Paul 
115896f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
115996f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
116096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116196f2e892SBill Paul 	else
116296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116396f2e892SBill Paul 
116496f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
116596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116696f2e892SBill Paul 	else
116796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116896f2e892SBill Paul 
11690934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
117096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
117196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
117296f2e892SBill Paul 
117396f2e892SBill Paul 	/*
117496f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
117596f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
117696f2e892SBill Paul 	 */
117796f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
117896f2e892SBill Paul 		return;
117996f2e892SBill Paul 
11800934f18aSMaxime Henrion 	/* Now program new ones. */
118113b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
11826817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
118396f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
118496f2e892SBill Paul 			continue;
1185acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1186aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1187aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1188acc1bcccSMartin Blapp 		else
1189aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1190aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
119196f2e892SBill Paul 		if (h < 32)
119296f2e892SBill Paul 			hashes[0] |= (1 << h);
119396f2e892SBill Paul 		else
119496f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
119596f2e892SBill Paul 	}
119613b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
119796f2e892SBill Paul 
119896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
119996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
120096f2e892SBill Paul }
120196f2e892SBill Paul 
12022c876e15SPoul-Henning Kamp static void
12030934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
120496f2e892SBill Paul {
12058df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
120696f2e892SBill Paul 	struct ifnet *ifp;
12070934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
120896f2e892SBill Paul 	int h = 0;
120996f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
121096f2e892SBill Paul 
1211fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
121296f2e892SBill Paul 
12138df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
12148df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
121596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
12168df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
121796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
12188df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
121996f2e892SBill Paul 
122096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
122196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
122296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122396f2e892SBill Paul 	else
122496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122596f2e892SBill Paul 
122696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
122796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
122896f2e892SBill Paul 	else
122996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
123096f2e892SBill Paul 
123196f2e892SBill Paul 	/*
123296f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
123396f2e892SBill Paul 	 * of broadcast frames.
123496f2e892SBill Paul 	 */
123596f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
123696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
123796f2e892SBill Paul 	else
123896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
123996f2e892SBill Paul 
124096f2e892SBill Paul 	/* first, zot all the existing hash bits */
124196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
124296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
124496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124596f2e892SBill Paul 
124696f2e892SBill Paul 	/*
124796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
124896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
124996f2e892SBill Paul 	 */
125096f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
125196f2e892SBill Paul 		return;
125296f2e892SBill Paul 
125396f2e892SBill Paul 	/* now program new ones */
125413b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
12556817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
125696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
125796f2e892SBill Paul 			continue;
1258aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
125996f2e892SBill Paul 		if (h < 32)
126096f2e892SBill Paul 			hashes[0] |= (1 << h);
126196f2e892SBill Paul 		else
126296f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
126396f2e892SBill Paul 	}
126413b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
126596f2e892SBill Paul 
126696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
126796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
126896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
126996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
127096f2e892SBill Paul }
127196f2e892SBill Paul 
12722c876e15SPoul-Henning Kamp static void
12730934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1274feb78939SJonathan Chen {
12758df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12760934f18aSMaxime Henrion 	struct ifnet *ifp;
12770934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1278feb78939SJonathan Chen 	struct dc_desc *sframe;
1279feb78939SJonathan Chen 	u_int32_t h, *sp;
1280feb78939SJonathan Chen 	int i;
1281feb78939SJonathan Chen 
1282fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1283feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1284feb78939SJonathan Chen 
1285feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1286feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1287feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1288feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
128956e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12900934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1291feb78939SJonathan Chen 
1292af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1293af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1294af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1295feb78939SJonathan Chen 
129656e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1297feb78939SJonathan Chen 
1298feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1299feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1300feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1301feb78939SJonathan Chen 	else
1302feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1303feb78939SJonathan Chen 
1304feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1305feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1306feb78939SJonathan Chen 	else
1307feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1308feb78939SJonathan Chen 
130913b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
13106817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1311feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1312feb78939SJonathan Chen 			continue;
1313aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13141d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1315af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1316feb78939SJonathan Chen 	}
131713b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
1318feb78939SJonathan Chen 
1319feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1320aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1321af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1322feb78939SJonathan Chen 	}
1323feb78939SJonathan Chen 
13248df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13258df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13268df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13278df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13288df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1329feb78939SJonathan Chen 
1330feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1331feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
133213f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1333af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
1334feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1335feb78939SJonathan Chen 
1336feb78939SJonathan Chen 	/*
13370934f18aSMaxime Henrion 	 * Wait some time...
1338feb78939SJonathan Chen 	 */
1339feb78939SJonathan Chen 	DELAY(1000);
1340feb78939SJonathan Chen 
1341b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1342feb78939SJonathan Chen }
1343feb78939SJonathan Chen 
1344e3d2833aSAlfred Perlstein static void
13450934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
134696f2e892SBill Paul {
13470934f18aSMaxime Henrion 
134896f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13491af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
135096f2e892SBill Paul 		dc_setfilt_21143(sc);
135196f2e892SBill Paul 
135296f2e892SBill Paul 	if (DC_IS_ASIX(sc))
135396f2e892SBill Paul 		dc_setfilt_asix(sc);
135496f2e892SBill Paul 
135596f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
135696f2e892SBill Paul 		dc_setfilt_admtek(sc);
135796f2e892SBill Paul 
1358feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1359feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
136096f2e892SBill Paul }
136196f2e892SBill Paul 
136296f2e892SBill Paul /*
13630934f18aSMaxime Henrion  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13640934f18aSMaxime Henrion  * the netconfig register, we first have to put the transmit and/or
13650934f18aSMaxime Henrion  * receive logic in the idle state.
136696f2e892SBill Paul  */
1367e3d2833aSAlfred Perlstein static void
13680934f18aSMaxime Henrion dc_setcfg(struct dc_softc *sc, int media)
136996f2e892SBill Paul {
13700934f18aSMaxime Henrion 	int i, restart = 0, watchdogreg;
137196f2e892SBill Paul 	u_int32_t isr;
137296f2e892SBill Paul 
137396f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
137496f2e892SBill Paul 		return;
137596f2e892SBill Paul 
137696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
137796f2e892SBill Paul 		restart = 1;
137896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
137996f2e892SBill Paul 
138096f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
138196f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1382d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1383351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1384351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
138596f2e892SBill Paul 				break;
1386d467c136SBill Paul 			DELAY(10);
138796f2e892SBill Paul 		}
138896f2e892SBill Paul 
1389432120f2SMarius Strobl 		if (i == DC_TIMEOUT) {
1390432120f2SMarius Strobl 			if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13916b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
1392432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
1393432120f2SMarius Strobl 				    __func__);
1394432120f2SMarius Strobl 			if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1395432120f2SMarius Strobl 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1396d0d67284SMarius Strobl 			    !DC_HAS_BROKEN_RXSTATE(sc))
1397432120f2SMarius Strobl 				device_printf(sc->dc_dev,
1398432120f2SMarius Strobl 				    "%s: failed to force rx to idle state\n",
1399432120f2SMarius Strobl 				    __func__);
1400432120f2SMarius Strobl 		}
140196f2e892SBill Paul 	}
140296f2e892SBill Paul 
140396f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1404042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1405042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
140696f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1407bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14080934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14098273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14108273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14118273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14124c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1413bf645417SBill Paul 			} else {
1414bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1415bf645417SBill Paul 			}
141696f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
141796f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
141896f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
141996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142096f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
142188d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142296f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142396f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1424e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1425e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
142696f2e892SBill Paul 		} else {
142796f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
142896f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
142996f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
143096f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
143196f2e892SBill Paul 			}
1432318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1433318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1434318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14355c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14365c1cfac4SBill Paul 				dc_apply_fixup(sc,
14375c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14385c1cfac4SBill Paul 				    IFM_100_TX | IFM_FDX : IFM_100_TX);
143996f2e892SBill Paul 		}
144096f2e892SBill Paul 	}
144196f2e892SBill Paul 
144296f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1443042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1444042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
144596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14460934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14474c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14488273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14498273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14508273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14518273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14524c2efe27SBill Paul 			} else {
14534c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14544c2efe27SBill Paul 			}
145596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
145696f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
145796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
145896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
145988d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
146096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
146196f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1462e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1463e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
146496f2e892SBill Paul 		} else {
146596f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
146696f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
146796f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
146896f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
146996f2e892SBill Paul 			}
147096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1471318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
147296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14735c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14745c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14755c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14765c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14775c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14785c1cfac4SBill Paul 				else
14795c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14805c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14815c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14825c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14835c1cfac4SBill Paul 				dc_apply_fixup(sc,
14845c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14855c1cfac4SBill Paul 				    IFM_10_T | IFM_FDX : IFM_10_T);
14865c1cfac4SBill Paul 				DELAY(20000);
14875c1cfac4SBill Paul 			}
148896f2e892SBill Paul 		}
148996f2e892SBill Paul 	}
149096f2e892SBill Paul 
1491f43d9309SBill Paul 	/*
1492f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1493f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1494f43d9309SBill Paul 	 * on the external MII port.
1495f43d9309SBill Paul 	 */
1496f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
149745521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1498f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1499f43d9309SBill Paul 			sc->dc_link = 1;
1500f43d9309SBill Paul 		} else {
1501f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1502f43d9309SBill Paul 		}
1503f43d9309SBill Paul 	}
1504f43d9309SBill Paul 
150596f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
150696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
150796f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
150896f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
150996f2e892SBill Paul 	} else {
151096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
151196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
151296f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
151396f2e892SBill Paul 	}
151496f2e892SBill Paul 
151596f2e892SBill Paul 	if (restart)
151696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
151796f2e892SBill Paul }
151896f2e892SBill Paul 
1519e3d2833aSAlfred Perlstein static void
15200934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
152196f2e892SBill Paul {
15220934f18aSMaxime Henrion 	int i;
152396f2e892SBill Paul 
152496f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
152596f2e892SBill Paul 
152696f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
152796f2e892SBill Paul 		DELAY(10);
152896f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
152996f2e892SBill Paul 			break;
153096f2e892SBill Paul 	}
153196f2e892SBill Paul 
15321af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15331d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
153496f2e892SBill Paul 		DELAY(10000);
153596f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
153696f2e892SBill Paul 		i = 0;
153796f2e892SBill Paul 	}
153896f2e892SBill Paul 
153996f2e892SBill Paul 	if (i == DC_TIMEOUT)
15406b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
154196f2e892SBill Paul 
154296f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
154396f2e892SBill Paul 	DELAY(1000);
154496f2e892SBill Paul 
154596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
154696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
154796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
154896f2e892SBill Paul 
154991cc2adbSBill Paul 	/*
155091cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
155191cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
155291cc2adbSBill Paul 	 * into a state where it will never come out of reset
155391cc2adbSBill Paul 	 * until we reset the whole chip again.
155491cc2adbSBill Paul 	 */
15555c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
155691cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15575c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15585c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15595c1cfac4SBill Paul 	}
156096f2e892SBill Paul }
156196f2e892SBill Paul 
1562ebc284ccSMarius Strobl static const struct dc_type *
15630934f18aSMaxime Henrion dc_devtype(device_t dev)
156496f2e892SBill Paul {
1565ebc284ccSMarius Strobl 	const struct dc_type *t;
15661e2e70b1SJohn Baldwin 	u_int32_t devid;
15671e2e70b1SJohn Baldwin 	u_int8_t rev;
156896f2e892SBill Paul 
156996f2e892SBill Paul 	t = dc_devs;
15701e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15711e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
157296f2e892SBill Paul 
157396f2e892SBill Paul 	while (t->dc_name != NULL) {
15741e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
157596f2e892SBill Paul 			return (t);
157696f2e892SBill Paul 		t++;
157796f2e892SBill Paul 	}
157896f2e892SBill Paul 
157996f2e892SBill Paul 	return (NULL);
158096f2e892SBill Paul }
158196f2e892SBill Paul 
158296f2e892SBill Paul /*
158396f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
158496f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
158596f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
158696f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
158796f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
158896f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
158996f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
159096f2e892SBill Paul  */
1591e3d2833aSAlfred Perlstein static int
15920934f18aSMaxime Henrion dc_probe(device_t dev)
159396f2e892SBill Paul {
1594ebc284ccSMarius Strobl 	const struct dc_type *t;
159596f2e892SBill Paul 
159696f2e892SBill Paul 	t = dc_devtype(dev);
159796f2e892SBill Paul 
159896f2e892SBill Paul 	if (t != NULL) {
159996f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1600d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
160196f2e892SBill Paul 	}
160296f2e892SBill Paul 
160396f2e892SBill Paul 	return (ENXIO);
160496f2e892SBill Paul }
160596f2e892SBill Paul 
1606e3d2833aSAlfred Perlstein static void
16070934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16085c1cfac4SBill Paul {
16095c1cfac4SBill Paul 	struct dc_mediainfo *m;
16105c1cfac4SBill Paul 	u_int8_t *p;
16115c1cfac4SBill Paul 	int i;
16125d801891SBill Paul 	u_int32_t reg;
16135c1cfac4SBill Paul 
16145c1cfac4SBill Paul 	m = sc->dc_mi;
16155c1cfac4SBill Paul 
16165c1cfac4SBill Paul 	while (m != NULL) {
16175c1cfac4SBill Paul 		if (m->dc_media == media)
16185c1cfac4SBill Paul 			break;
16195c1cfac4SBill Paul 		m = m->dc_next;
16205c1cfac4SBill Paul 	}
16215c1cfac4SBill Paul 
16225c1cfac4SBill Paul 	if (m == NULL)
16235c1cfac4SBill Paul 		return;
16245c1cfac4SBill Paul 
16255c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_len; i++, p += 2) {
16265c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16275c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16285c1cfac4SBill Paul 	}
16295c1cfac4SBill Paul 
16305c1cfac4SBill Paul 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16315c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16325c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16335c1cfac4SBill Paul 	}
16345c1cfac4SBill Paul }
16355c1cfac4SBill Paul 
1636e3d2833aSAlfred Perlstein static void
16370934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16385c1cfac4SBill Paul {
16395c1cfac4SBill Paul 	struct dc_mediainfo *m;
16405c1cfac4SBill Paul 
16410934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
164287f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
164387f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16445c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
164587f4fa15SMartin Blapp 		break;
164687f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16475c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
164887f4fa15SMartin Blapp 		break;
164987f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16505c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
165187f4fa15SMartin Blapp 		break;
165287f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16535c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
165487f4fa15SMartin Blapp 		break;
165587f4fa15SMartin Blapp 	default:
165687f4fa15SMartin Blapp 		break;
165787f4fa15SMartin Blapp 	}
16585c1cfac4SBill Paul 
165987f4fa15SMartin Blapp 	/*
166087f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
166187f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
166287f4fa15SMartin Blapp 	 * supply Media Specific Data.
166387f4fa15SMartin Blapp 	 */
166487f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16655c1cfac4SBill Paul 		m->dc_gp_len = 2;
166687f4fa15SMartin Blapp 		m->dc_gp_ptr =
166787f4fa15SMartin Blapp 		(u_int8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
166887f4fa15SMartin Blapp 	} else {
166987f4fa15SMartin Blapp 		m->dc_gp_len = 2;
167087f4fa15SMartin Blapp 		m->dc_gp_ptr =
167187f4fa15SMartin Blapp 		(u_int8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
167287f4fa15SMartin Blapp 	}
16735c1cfac4SBill Paul 
16745c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16755c1cfac4SBill Paul 	sc->dc_mi = m;
16765c1cfac4SBill Paul 
16775c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
16785c1cfac4SBill Paul }
16795c1cfac4SBill Paul 
1680e3d2833aSAlfred Perlstein static void
16810934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16825c1cfac4SBill Paul {
16835c1cfac4SBill Paul 	struct dc_mediainfo *m;
16845c1cfac4SBill Paul 
16850934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
16865c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16875c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16885c1cfac4SBill Paul 
16895c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16905c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16915c1cfac4SBill Paul 
16925c1cfac4SBill Paul 	m->dc_gp_len = 2;
16935c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
16945c1cfac4SBill Paul 
16955c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16965c1cfac4SBill Paul 	sc->dc_mi = m;
16975c1cfac4SBill Paul 
16985c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
16995c1cfac4SBill Paul }
17005c1cfac4SBill Paul 
1701e3d2833aSAlfred Perlstein static void
17020934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17035c1cfac4SBill Paul {
17045c1cfac4SBill Paul 	struct dc_mediainfo *m;
17050934f18aSMaxime Henrion 	u_int8_t *p;
17065c1cfac4SBill Paul 
17070934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17085c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17095c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17105c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17115c1cfac4SBill Paul 
17125c1cfac4SBill Paul 	p = (u_int8_t *)l;
17135c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17145c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17155c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17165c1cfac4SBill Paul 	m->dc_reset_len = *p;
17175c1cfac4SBill Paul 	p++;
17185c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17195c1cfac4SBill Paul 
17205c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17215c1cfac4SBill Paul 	sc->dc_mi = m;
17225c1cfac4SBill Paul }
17235c1cfac4SBill Paul 
17242c876e15SPoul-Henning Kamp static void
17250934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17263097aa70SWarner Losh {
17273097aa70SWarner Losh 	int size;
17283097aa70SWarner Losh 
17293097aa70SWarner Losh 	size = 2 << bits;
17303097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
17313097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
17323097aa70SWarner Losh }
17333097aa70SWarner Losh 
1734e3d2833aSAlfred Perlstein static void
17350934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17365c1cfac4SBill Paul {
17375c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17385c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
17390934f18aSMaxime Henrion 	int have_mii, i, loff;
17405c1cfac4SBill Paul 	char *ptr;
17415c1cfac4SBill Paul 
1742f956e0b3SMartin Blapp 	have_mii = 0;
17435c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17445c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17455c1cfac4SBill Paul 
17465c1cfac4SBill Paul 	ptr = (char *)lhdr;
17475c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1748f956e0b3SMartin Blapp 	/*
1749f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1750f956e0b3SMartin Blapp 	 */
1751f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1752f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1753f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1754f956e0b3SMartin Blapp 		    have_mii++;
1755f956e0b3SMartin Blapp 
1756f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1757f956e0b3SMartin Blapp 		ptr++;
1758f956e0b3SMartin Blapp 	}
1759f956e0b3SMartin Blapp 
1760f956e0b3SMartin Blapp 	/*
1761f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1762f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1763f956e0b3SMartin Blapp 	 */
1764f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1765f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17665c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17675c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17685c1cfac4SBill Paul 		switch (hdr->dc_type) {
17695c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17705c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17715c1cfac4SBill Paul 			break;
17725c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1773f956e0b3SMartin Blapp 			if (! have_mii)
1774f956e0b3SMartin Blapp 				dc_decode_leaf_sia(sc,
1775f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17765c1cfac4SBill Paul 			break;
17775c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1778f956e0b3SMartin Blapp 			if (! have_mii)
1779f956e0b3SMartin Blapp 				dc_decode_leaf_sym(sc,
1780f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17815c1cfac4SBill Paul 			break;
17825c1cfac4SBill Paul 		default:
17835c1cfac4SBill Paul 			/* Don't care. Yet. */
17845c1cfac4SBill Paul 			break;
17855c1cfac4SBill Paul 		}
17865c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17875c1cfac4SBill Paul 		ptr++;
17885c1cfac4SBill Paul 	}
17895c1cfac4SBill Paul }
17905c1cfac4SBill Paul 
179156e5e7aeSMaxime Henrion static void
179256e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
179356e5e7aeSMaxime Henrion {
179456e5e7aeSMaxime Henrion 	u_int32_t *paddr;
179556e5e7aeSMaxime Henrion 
1796ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1797ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
179856e5e7aeSMaxime Henrion 	paddr = arg;
179956e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
180056e5e7aeSMaxime Henrion }
180156e5e7aeSMaxime Henrion 
180296f2e892SBill Paul /*
180396f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
180496f2e892SBill Paul  * setup and ethernet/BPF attach.
180596f2e892SBill Paul  */
1806e3d2833aSAlfred Perlstein static int
18070934f18aSMaxime Henrion dc_attach(device_t dev)
180896f2e892SBill Paul {
1809d1ce9105SBill Paul 	int tmp = 0;
18108df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
181196f2e892SBill Paul 	u_int32_t command;
181296f2e892SBill Paul 	struct dc_softc *sc;
181396f2e892SBill Paul 	struct ifnet *ifp;
18142e3d4b79SPyun YongHyeon 	u_int32_t reg, revision;
181522f6205dSJohn Baldwin 	int error = 0, rid, mac_offset;
181656e5e7aeSMaxime Henrion 	int i;
1817e7b01d07SWarner Losh 	u_int8_t *mac;
181896f2e892SBill Paul 
181996f2e892SBill Paul 	sc = device_get_softc(dev);
18206b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
182196f2e892SBill Paul 
18226008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1823c8b27acaSJohn Baldwin 	    MTX_DEF);
1824c3e7434fSWarner Losh 
182596f2e892SBill Paul 	/*
182696f2e892SBill Paul 	 * Map control/status registers.
182796f2e892SBill Paul 	 */
182807f65363SBill Paul 	pci_enable_busmaster(dev);
182996f2e892SBill Paul 
183096f2e892SBill Paul 	rid = DC_RID;
18315f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
183296f2e892SBill Paul 
183396f2e892SBill Paul 	if (sc->dc_res == NULL) {
183422f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
183596f2e892SBill Paul 		error = ENXIO;
1836608654d4SNate Lawson 		goto fail;
183796f2e892SBill Paul 	}
183896f2e892SBill Paul 
183996f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
184096f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
184196f2e892SBill Paul 
18420934f18aSMaxime Henrion 	/* Allocate interrupt. */
184354f1f1d1SNate Lawson 	rid = 0;
18445f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
184554f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
184654f1f1d1SNate Lawson 
184754f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
184822f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
184954f1f1d1SNate Lawson 		error = ENXIO;
185054f1f1d1SNate Lawson 		goto fail;
185154f1f1d1SNate Lawson 	}
185254f1f1d1SNate Lawson 
185396f2e892SBill Paul 	/* Need this info to decide on a chip type. */
185496f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
18551e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
185696f2e892SBill Paul 
18576d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
18581e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
18591e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
18601e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
18611e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
1862eecb3844SMartin Blapp 		dc_eeprom_width(sc);
1863eecb3844SMartin Blapp 
18641e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
18651e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
186696f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
186796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1868042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
18695c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
18703097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
187196f2e892SBill Paul 		break;
18721e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
18731e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
18741e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
187596f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1876318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1877318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
18787dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
18794a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
18801e2e70b1SJohn Baldwin 
18810a46b1dcSBill Paul 		/* Increase the latency timer value. */
18821e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
188396f2e892SBill Paul 		break;
18841e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
188596f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
188696f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
188796f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
188896f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
18893097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
189096f2e892SBill Paul 		break;
18911e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
18921e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
18931e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
18941e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
18951e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_FA511):
18961e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
18971e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
18981e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
18991e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
19001e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
19011e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
19021e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
19031e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
190417762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
190517762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
190696f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
1907acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
190896f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
190996f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
191096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
1911129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
191296f2e892SBill Paul 		break;
19131e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
19141e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
191596f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
191696f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
191796f2e892SBill Paul 		}
1918318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
191996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1920318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1921318b02fdSBill Paul 		}
1922318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
192396f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
192496f2e892SBill Paul 		break;
19251e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
19261e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
192779d11e09SBill Paul 		/*
192879d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
192979d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
193079d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
193179d11e09SBill Paul 		 * get the right number of bits out of the
193279d11e09SBill Paul 		 * CRC routine.
193379d11e09SBill Paul 		 */
193479d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
193579d11e09SBill Paul 		    revision < DC_REVISION_98725)
193679d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
193796f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
193896f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1939318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
194096f2e892SBill Paul 		break;
19411e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
1942ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1943ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1944ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1945ead7cde9SBill Paul 		break;
19461e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
194796f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
194879d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
1949318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
195096f2e892SBill Paul 		break;
19511e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
195296f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
195391cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
195496f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
195596f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
195696f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
195796f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
195896f2e892SBill Paul 		break;
19591e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
196096f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
196196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
196296f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
196396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
196496f2e892SBill Paul 		break;
19651e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
1966feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
19672dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
19682dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
1969feb78939SJonathan Chen 		/*
1970feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1971feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
19722dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
1973feb78939SJonathan Chen 		 */
19743097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
1975feb78939SJonathan Chen 		break;
19761e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
19771af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
19781af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
19791af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19801af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19813097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
19821af8bec7SBill Paul 		break;
198396f2e892SBill Paul 	default:
19841e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
19851e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
198696f2e892SBill Paul 		break;
198796f2e892SBill Paul 	}
198896f2e892SBill Paul 
198996f2e892SBill Paul 	/* Save the cache line size. */
199088d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
199188d739dcSBill Paul 		sc->dc_cachesize = 0;
199288d739dcSBill Paul 	else
19931e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
199496f2e892SBill Paul 
199596f2e892SBill Paul 	/* Reset the adapter. */
199696f2e892SBill Paul 	dc_reset(sc);
199796f2e892SBill Paul 
199896f2e892SBill Paul 	/* Take 21143 out of snooze mode */
1999feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
200096f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
200196f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
200296f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
200396f2e892SBill Paul 	}
200496f2e892SBill Paul 
200596f2e892SBill Paul 	/*
200696f2e892SBill Paul 	 * Try to learn something about the supported media.
200796f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
200896f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
200996f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
201096f2e892SBill Paul 	 * Intel 21143.
201196f2e892SBill Paul 	 */
20125c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
20135c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
20145c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
201596f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
201696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
201796f2e892SBill Paul 		else
201896f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
201996f2e892SBill Paul 	} else if (!sc->dc_pmode)
202096f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
202196f2e892SBill Paul 
202296f2e892SBill Paul 	/*
202396f2e892SBill Paul 	 * Get station address from the EEPROM.
202496f2e892SBill Paul 	 */
202596f2e892SBill Paul 	switch(sc->dc_type) {
202696f2e892SBill Paul 	case DC_TYPE_98713:
202796f2e892SBill Paul 	case DC_TYPE_98713A:
202896f2e892SBill Paul 	case DC_TYPE_987x5:
202996f2e892SBill Paul 	case DC_TYPE_PNICII:
203096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
203196f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
203296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
203396f2e892SBill Paul 		break;
203496f2e892SBill Paul 	case DC_TYPE_PNIC:
203596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
203696f2e892SBill Paul 		break;
203796f2e892SBill Paul 	case DC_TYPE_DM9102:
2038ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2039ec6a7299SMaxime Henrion #ifdef __sparc64__
2040ec6a7299SMaxime Henrion 		/*
2041ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2042802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2043ec6a7299SMaxime Henrion 		 */
2044802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
20458069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2046ec6a7299SMaxime Henrion #endif
2047ec6a7299SMaxime Henrion 		break;
204896f2e892SBill Paul 	case DC_TYPE_21143:
204996f2e892SBill Paul 	case DC_TYPE_ASIX:
205096f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
205196f2e892SBill Paul 		break;
205296f2e892SBill Paul 	case DC_TYPE_AL981:
205396f2e892SBill Paul 	case DC_TYPE_AN985:
20542e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
20552e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
20562e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
20572e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
20582e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
20592e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
20602e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
20612e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
20622e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
206396f2e892SBill Paul 		break;
20641af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20650934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
20660934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
20671af8bec7SBill Paul 		break;
2068feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
20690934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2070e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2071e7b01d07SWarner Losh 		if (!mac) {
2072e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2073608654d4SNate Lawson 			error = ENXIO;
2074e7b01d07SWarner Losh 			goto fail;
2075e7b01d07SWarner Losh 		}
2076e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2077feb78939SJonathan Chen 		break;
207896f2e892SBill Paul 	default:
207996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
208096f2e892SBill Paul 		break;
208196f2e892SBill Paul 	}
208296f2e892SBill Paul 
208356e5e7aeSMaxime Henrion 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
2084b1d16143SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), PAGE_SIZE, 0,
2085b1d16143SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
2086b1d16143SMarius Strobl 	    sizeof(struct dc_list_data), 1, sizeof(struct dc_list_data),
2087b1d16143SMarius Strobl 	    0, NULL, NULL, &sc->dc_ltag);
208856e5e7aeSMaxime Henrion 	if (error) {
208922f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
209056e5e7aeSMaxime Henrion 		error = ENXIO;
209156e5e7aeSMaxime Henrion 		goto fail;
209256e5e7aeSMaxime Henrion 	}
209356e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_ltag, (void **)&sc->dc_ldata,
2094aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->dc_lmap);
209556e5e7aeSMaxime Henrion 	if (error) {
209622f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate DMA safe memory\n");
209756e5e7aeSMaxime Henrion 		error = ENXIO;
209856e5e7aeSMaxime Henrion 		goto fail;
209956e5e7aeSMaxime Henrion 	}
210056e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_ltag, sc->dc_lmap, sc->dc_ldata,
210156e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), dc_dma_map_addr, &sc->dc_laddr,
210256e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT);
210356e5e7aeSMaxime Henrion 	if (error) {
210422f6205dSJohn Baldwin 		device_printf(dev, "cannot get address of the descriptors\n");
210556e5e7aeSMaxime Henrion 		error = ENXIO;
210656e5e7aeSMaxime Henrion 		goto fail;
210756e5e7aeSMaxime Henrion 	}
210896f2e892SBill Paul 
210956e5e7aeSMaxime Henrion 	/*
211056e5e7aeSMaxime Henrion 	 * Allocate a busdma tag and DMA safe memory for the multicast
211156e5e7aeSMaxime Henrion 	 * setup frame.
211256e5e7aeSMaxime Henrion 	 */
2113b1d16143SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), PAGE_SIZE, 0,
2114b1d16143SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
2115b1d16143SMarius Strobl 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
2116b1d16143SMarius Strobl 	    0, NULL, NULL, &sc->dc_stag);
211756e5e7aeSMaxime Henrion 	if (error) {
211822f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
211956e5e7aeSMaxime Henrion 		error = ENXIO;
212056e5e7aeSMaxime Henrion 		goto fail;
212156e5e7aeSMaxime Henrion 	}
212256e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
212356e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->dc_smap);
212456e5e7aeSMaxime Henrion 	if (error) {
212522f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate DMA safe memory\n");
212656e5e7aeSMaxime Henrion 		error = ENXIO;
212756e5e7aeSMaxime Henrion 		goto fail;
212856e5e7aeSMaxime Henrion 	}
212956e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
213056e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
213156e5e7aeSMaxime Henrion 	if (error) {
213222f6205dSJohn Baldwin 		device_printf(dev, "cannot get address of the descriptors\n");
213396f2e892SBill Paul 		error = ENXIO;
213496f2e892SBill Paul 		goto fail;
213596f2e892SBill Paul 	}
213696f2e892SBill Paul 
213756e5e7aeSMaxime Henrion 	/* Allocate a busdma tag for mbufs. */
2138b1d16143SMarius Strobl 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
2139b1d16143SMarius Strobl 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
2140ebc284ccSMarius Strobl 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
2141c1b677aaSScott Long 	    0, NULL, NULL, &sc->dc_mtag);
214256e5e7aeSMaxime Henrion 	if (error) {
214322f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
214456e5e7aeSMaxime Henrion 		error = ENXIO;
214556e5e7aeSMaxime Henrion 		goto fail;
214656e5e7aeSMaxime Henrion 	}
214756e5e7aeSMaxime Henrion 
214856e5e7aeSMaxime Henrion 	/* Create the TX/RX busdma maps. */
214956e5e7aeSMaxime Henrion 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
215056e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
215156e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_tx_map[i]);
215256e5e7aeSMaxime Henrion 		if (error) {
215322f6205dSJohn Baldwin 			device_printf(dev, "failed to init TX ring\n");
215456e5e7aeSMaxime Henrion 			error = ENXIO;
215556e5e7aeSMaxime Henrion 			goto fail;
215656e5e7aeSMaxime Henrion 		}
215756e5e7aeSMaxime Henrion 	}
215856e5e7aeSMaxime Henrion 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
215956e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
216056e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_rx_map[i]);
216156e5e7aeSMaxime Henrion 		if (error) {
216222f6205dSJohn Baldwin 			device_printf(dev, "failed to init RX ring\n");
216356e5e7aeSMaxime Henrion 			error = ENXIO;
216456e5e7aeSMaxime Henrion 			goto fail;
216556e5e7aeSMaxime Henrion 		}
216656e5e7aeSMaxime Henrion 	}
216756e5e7aeSMaxime Henrion 	error = bus_dmamap_create(sc->dc_mtag, 0, &sc->dc_sparemap);
216856e5e7aeSMaxime Henrion 	if (error) {
216922f6205dSJohn Baldwin 		device_printf(dev, "failed to init RX ring\n");
217056e5e7aeSMaxime Henrion 		error = ENXIO;
217156e5e7aeSMaxime Henrion 		goto fail;
217256e5e7aeSMaxime Henrion 	}
217396f2e892SBill Paul 
2174fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2175fc74a9f9SBrooks Davis 	if (ifp == NULL) {
217622f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2177fc74a9f9SBrooks Davis 		error = ENOSPC;
2178fc74a9f9SBrooks Davis 		goto fail;
2179fc74a9f9SBrooks Davis 	}
218096f2e892SBill Paul 	ifp->if_softc = sc;
21819bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
21823d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
218396f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
218496f2e892SBill Paul 	ifp->if_start = dc_start;
218596f2e892SBill Paul 	ifp->if_init = dc_init;
2186cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2187cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2188cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
218996f2e892SBill Paul 
219096f2e892SBill Paul 	/*
21915c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
21925c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
21935c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
21945c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
21955c1cfac4SBill Paul 	 * driver instead.
219696f2e892SBill Paul 	 */
21975c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
21985c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
21995c1cfac4SBill Paul 		tmp = sc->dc_pmode;
22005c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
22015c1cfac4SBill Paul 	}
22025c1cfac4SBill Paul 
22036d431b17SWarner Losh 	/*
22046d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
22056d431b17SWarner Losh 	 * to the MII.  This needs to be done before mii_phy_probe so that
22066d431b17SWarner Losh 	 * we can actually see them.
22076d431b17SWarner Losh 	 */
22086d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
22096d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
22106d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
22116d431b17SWarner Losh 		DELAY(10);
22126d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
22136d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
22146d431b17SWarner Losh 		DELAY(10);
22156d431b17SWarner Losh 	}
22166d431b17SWarner Losh 
221796f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
221896f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
221996f2e892SBill Paul 
222096f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
22215c1cfac4SBill Paul 		sc->dc_pmode = tmp;
22225c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
222396f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2224042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
222596f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
222696f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
222778999dd1SBill Paul 		/*
222878999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
222978999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
223078999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
223178999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
223278999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
223378999dd1SBill Paul 		 */
22341e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
22351e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
223678999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
223796f2e892SBill Paul 		error = 0;
223896f2e892SBill Paul 	}
223996f2e892SBill Paul 
224096f2e892SBill Paul 	if (error) {
224122f6205dSJohn Baldwin 		device_printf(dev, "MII without any PHY!\n");
224296f2e892SBill Paul 		goto fail;
224396f2e892SBill Paul 	}
224496f2e892SBill Paul 
2245028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2246028a8491SMartin Blapp 		/*
2247028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2248028a8491SMartin Blapp 		 */
2249028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2250028a8491SMartin Blapp 	}
2251028a8491SMartin Blapp 
225296f2e892SBill Paul 	/*
2253db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2254db40c1aeSDoug Ambrisko 	 */
2255db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
22569ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
225740929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2258e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2259e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2260e695984eSRuslan Ermilov #endif
2261db40c1aeSDoug Ambrisko 
2262c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2263b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
226496f2e892SBill Paul 
2265608654d4SNate Lawson 	/*
2266608654d4SNate Lawson 	 * Call MI attach routine.
2267608654d4SNate Lawson 	 */
22688df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2269608654d4SNate Lawson 
227054f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2271c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2272ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2273608654d4SNate Lawson 
2274608654d4SNate Lawson 	if (error) {
227522f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2276693f4477SNate Lawson 		ether_ifdetach(ifp);
227754f1f1d1SNate Lawson 		goto fail;
2278608654d4SNate Lawson 	}
2279510a809eSMike Smith 
228096f2e892SBill Paul fail:
228154f1f1d1SNate Lawson 	if (error)
228254f1f1d1SNate Lawson 		dc_detach(dev);
228396f2e892SBill Paul 	return (error);
228496f2e892SBill Paul }
228596f2e892SBill Paul 
2286693f4477SNate Lawson /*
2287693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2288693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2289693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2290693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2291693f4477SNate Lawson  * allocated.
2292693f4477SNate Lawson  */
2293e3d2833aSAlfred Perlstein static int
22940934f18aSMaxime Henrion dc_detach(device_t dev)
229596f2e892SBill Paul {
229696f2e892SBill Paul 	struct dc_softc *sc;
229796f2e892SBill Paul 	struct ifnet *ifp;
22985c1cfac4SBill Paul 	struct dc_mediainfo *m;
229956e5e7aeSMaxime Henrion 	int i;
230096f2e892SBill Paul 
230196f2e892SBill Paul 	sc = device_get_softc(dev);
230259f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2303d1ce9105SBill Paul 
2304fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
230596f2e892SBill Paul 
230640929967SGleb Smirnoff #ifdef DEVICE_POLLING
230740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
230840929967SGleb Smirnoff 		ether_poll_deregister(ifp);
230940929967SGleb Smirnoff #endif
231040929967SGleb Smirnoff 
2311693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2312214073e5SWarner Losh 	if (device_is_attached(dev)) {
2313c8b27acaSJohn Baldwin 		DC_LOCK(sc);
231496f2e892SBill Paul 		dc_stop(sc);
2315c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2316c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2317b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
23189ef8b520SSam Leffler 		ether_ifdetach(ifp);
2319693f4477SNate Lawson 	}
2320693f4477SNate Lawson 	if (sc->dc_miibus)
232196f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
232254f1f1d1SNate Lawson 	bus_generic_detach(dev);
232396f2e892SBill Paul 
232454f1f1d1SNate Lawson 	if (sc->dc_intrhand)
232596f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
232654f1f1d1SNate Lawson 	if (sc->dc_irq)
232796f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
232854f1f1d1SNate Lawson 	if (sc->dc_res)
232996f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
233096f2e892SBill Paul 
23316a3033a8SWarner Losh 	if (ifp)
23326a3033a8SWarner Losh 		if_free(ifp);
23336a3033a8SWarner Losh 
233456e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_sbuf != NULL)
233556e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf, sc->dc_smap);
233656e5e7aeSMaxime Henrion 	if (sc->dc_ldata != NULL)
233756e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_ltag, sc->dc_ldata, sc->dc_lmap);
23384f867c2dSGiorgos Keramidas 	if (sc->dc_mtag) {
233956e5e7aeSMaxime Henrion 		for (i = 0; i < DC_TX_LIST_CNT; i++)
23404f867c2dSGiorgos Keramidas 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
23414f867c2dSGiorgos Keramidas 				bus_dmamap_destroy(sc->dc_mtag,
23424f867c2dSGiorgos Keramidas 				    sc->dc_cdata.dc_tx_map[i]);
234356e5e7aeSMaxime Henrion 		for (i = 0; i < DC_RX_LIST_CNT; i++)
23444f867c2dSGiorgos Keramidas 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
23454f867c2dSGiorgos Keramidas 				bus_dmamap_destroy(sc->dc_mtag,
23464f867c2dSGiorgos Keramidas 				    sc->dc_cdata.dc_rx_map[i]);
234756e5e7aeSMaxime Henrion 		bus_dmamap_destroy(sc->dc_mtag, sc->dc_sparemap);
23484f867c2dSGiorgos Keramidas 	}
234956e5e7aeSMaxime Henrion 	if (sc->dc_stag)
235056e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_stag);
235156e5e7aeSMaxime Henrion 	if (sc->dc_mtag)
235256e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_mtag);
235356e5e7aeSMaxime Henrion 	if (sc->dc_ltag)
235456e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_ltag);
235556e5e7aeSMaxime Henrion 
235696f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
235796f2e892SBill Paul 
23585c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
23595c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
23605c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
23615c1cfac4SBill Paul 		sc->dc_mi = m;
23625c1cfac4SBill Paul 	}
23637efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
23645c1cfac4SBill Paul 
2365d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
236696f2e892SBill Paul 
236796f2e892SBill Paul 	return (0);
236896f2e892SBill Paul }
236996f2e892SBill Paul 
237096f2e892SBill Paul /*
237196f2e892SBill Paul  * Initialize the transmit descriptors.
237296f2e892SBill Paul  */
2373e3d2833aSAlfred Perlstein static int
23740934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
237596f2e892SBill Paul {
237696f2e892SBill Paul 	struct dc_chain_data *cd;
237796f2e892SBill Paul 	struct dc_list_data *ld;
237801faf54bSLuigi Rizzo 	int i, nexti;
237996f2e892SBill Paul 
238096f2e892SBill Paul 	cd = &sc->dc_cdata;
238196f2e892SBill Paul 	ld = sc->dc_ldata;
238296f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2383b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2384b3811c95SMaxime Henrion 			nexti = 0;
2385b3811c95SMaxime Henrion 		else
2386b3811c95SMaxime Henrion 			nexti = i + 1;
2387af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
238896f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
238996f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
239096f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
239196f2e892SBill Paul 	}
239296f2e892SBill Paul 
239396f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
239456e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
239556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
239696f2e892SBill Paul 	return (0);
239796f2e892SBill Paul }
239896f2e892SBill Paul 
239996f2e892SBill Paul 
240096f2e892SBill Paul /*
240196f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
240296f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
240396f2e892SBill Paul  * points back to the first.
240496f2e892SBill Paul  */
2405e3d2833aSAlfred Perlstein static int
24060934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
240796f2e892SBill Paul {
240896f2e892SBill Paul 	struct dc_chain_data *cd;
240996f2e892SBill Paul 	struct dc_list_data *ld;
241001faf54bSLuigi Rizzo 	int i, nexti;
241196f2e892SBill Paul 
241296f2e892SBill Paul 	cd = &sc->dc_cdata;
241396f2e892SBill Paul 	ld = sc->dc_ldata;
241496f2e892SBill Paul 
241596f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
241656e5e7aeSMaxime Henrion 		if (dc_newbuf(sc, i, 1) != 0)
241796f2e892SBill Paul 			return (ENOBUFS);
2418b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2419b3811c95SMaxime Henrion 			nexti = 0;
2420b3811c95SMaxime Henrion 		else
2421b3811c95SMaxime Henrion 			nexti = i + 1;
2422af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
242396f2e892SBill Paul 	}
242496f2e892SBill Paul 
242596f2e892SBill Paul 	cd->dc_rx_prod = 0;
242656e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
242756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
242896f2e892SBill Paul 	return (0);
242996f2e892SBill Paul }
243096f2e892SBill Paul 
243196f2e892SBill Paul /*
243296f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
243396f2e892SBill Paul  */
2434e3d2833aSAlfred Perlstein static int
243556e5e7aeSMaxime Henrion dc_newbuf(struct dc_softc *sc, int i, int alloc)
243696f2e892SBill Paul {
243756e5e7aeSMaxime Henrion 	struct mbuf *m_new;
243856e5e7aeSMaxime Henrion 	bus_dmamap_t tmp;
243982a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
244082a67a70SMarius Strobl 	int error, nseg;
244196f2e892SBill Paul 
244256e5e7aeSMaxime Henrion 	if (alloc) {
244356e5e7aeSMaxime Henrion 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
244440129585SLuigi Rizzo 		if (m_new == NULL)
244596f2e892SBill Paul 			return (ENOBUFS);
244696f2e892SBill Paul 	} else {
244756e5e7aeSMaxime Henrion 		m_new = sc->dc_cdata.dc_rx_chain[i];
244896f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
244996f2e892SBill Paul 	}
245056e5e7aeSMaxime Henrion 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
245196f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
245296f2e892SBill Paul 
245396f2e892SBill Paul 	/*
245496f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
245596f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
245696f2e892SBill Paul 	 * 82c169 chips.
245796f2e892SBill Paul 	 */
245896f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
24590934f18aSMaxime Henrion 		bzero(mtod(m_new, char *), m_new->m_len);
246096f2e892SBill Paul 
246156e5e7aeSMaxime Henrion 	/* No need to remap the mbuf if we're reusing it. */
246256e5e7aeSMaxime Henrion 	if (alloc) {
246382a67a70SMarius Strobl 		error = bus_dmamap_load_mbuf_sg(sc->dc_mtag, sc->dc_sparemap,
246482a67a70SMarius Strobl 		    m_new, segs, &nseg, 0);
246556e5e7aeSMaxime Henrion 		if (error) {
246656e5e7aeSMaxime Henrion 			m_freem(m_new);
246756e5e7aeSMaxime Henrion 			return (error);
246856e5e7aeSMaxime Henrion 		}
2469ebc284ccSMarius Strobl 		KASSERT(nseg == 1,
2470ebc284ccSMarius Strobl 		    ("%s: wrong number of segments (%d)", __func__, nseg));
247182a67a70SMarius Strobl 		sc->dc_ldata->dc_rx_list[i].dc_data = htole32(segs->ds_addr);
247256e5e7aeSMaxime Henrion 		bus_dmamap_unload(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i]);
247356e5e7aeSMaxime Henrion 		tmp = sc->dc_cdata.dc_rx_map[i];
247456e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
247556e5e7aeSMaxime Henrion 		sc->dc_sparemap = tmp;
247696f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = m_new;
247756e5e7aeSMaxime Henrion 	}
247896f2e892SBill Paul 
2479af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2480af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
248156e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
248256e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
248356e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
248456e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
248596f2e892SBill Paul 	return (0);
248696f2e892SBill Paul }
248796f2e892SBill Paul 
248896f2e892SBill Paul /*
248996f2e892SBill Paul  * Grrrrr.
249096f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
249196f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
249296f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
249396f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
249496f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
249596f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
249696f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
249796f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
249896f2e892SBill Paul  *
249996f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
250096f2e892SBill Paul  * Here's what we know:
250196f2e892SBill Paul  *
250296f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
250396f2e892SBill Paul  *   descriptors uploaded.
250496f2e892SBill Paul  *
250596f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
250696f2e892SBill Paul  *   total data upload.
250796f2e892SBill Paul  *
250896f2e892SBill Paul  * - We know the size of the desired received frame because it will be
250996f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
251096f2e892SBill Paul  *
251196f2e892SBill Paul  * Here's what we do:
251296f2e892SBill Paul  *
251396f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
251496f2e892SBill Paul  *   This means that we know that the buffer contents should be all
251596f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
251696f2e892SBill Paul  *
251796f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
251896f2e892SBill Paul  *   ethernet CRC at the end.
251996f2e892SBill Paul  *
252096f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
252196f2e892SBill Paul  *
252296f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
252396f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
252496f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
252596f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
252696f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
252796f2e892SBill Paul  *   we won't be fooled.
252896f2e892SBill Paul  *
252996f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
253096f2e892SBill Paul  *   that value from the current pointer location. This brings us
253196f2e892SBill Paul  *   to the start of the actual received packet.
253296f2e892SBill Paul  *
253396f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
253496f2e892SBill Paul  *   frame length.
253596f2e892SBill Paul  *
253696f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
253796f2e892SBill Paul  * the time.
253896f2e892SBill Paul  */
253996f2e892SBill Paul 
254096f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2541e3d2833aSAlfred Perlstein static void
25420934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
254396f2e892SBill Paul {
254496f2e892SBill Paul 	struct dc_desc *cur_rx;
254596f2e892SBill Paul 	struct dc_desc *c = NULL;
254696f2e892SBill Paul 	struct mbuf *m = NULL;
254796f2e892SBill Paul 	unsigned char *ptr;
254896f2e892SBill Paul 	int i, total_len;
254996f2e892SBill Paul 	u_int32_t rxstat = 0;
255096f2e892SBill Paul 
255196f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
255296f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
255396f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
25541edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
255596f2e892SBill Paul 
255696f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
255796f2e892SBill Paul 	while (1) {
255896f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
2559af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
256096f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
256196f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
256296f2e892SBill Paul 		ptr += DC_RXLEN;
256396f2e892SBill Paul 		/* If this is the last buffer, break out. */
256496f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
256596f2e892SBill Paul 			break;
256656e5e7aeSMaxime Henrion 		dc_newbuf(sc, i, 0);
256796f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
256896f2e892SBill Paul 	}
256996f2e892SBill Paul 
257096f2e892SBill Paul 	/* Find the length of the actual receive frame. */
257196f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
257296f2e892SBill Paul 
257396f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
257496f2e892SBill Paul 	while (*ptr == 0x00)
257596f2e892SBill Paul 		ptr--;
257696f2e892SBill Paul 
257796f2e892SBill Paul 	/* Round off. */
257896f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
257996f2e892SBill Paul 		ptr -= 1;
258096f2e892SBill Paul 
258196f2e892SBill Paul 	/* Now find the start of the frame. */
258296f2e892SBill Paul 	ptr -= total_len;
258396f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
258496f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
258596f2e892SBill Paul 
258696f2e892SBill Paul 	/*
258796f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
258896f2e892SBill Paul 	 * the status word to make it look like a successful
258996f2e892SBill Paul 	 * frame reception.
259096f2e892SBill Paul 	 */
259156e5e7aeSMaxime Henrion 	dc_newbuf(sc, i, 0);
259296f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2593af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
259496f2e892SBill Paul }
259596f2e892SBill Paul 
259696f2e892SBill Paul /*
259773bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
259873bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
259973bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
260073bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
260173bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
260273bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
260373bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
260473bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
260573bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
260673bf949cSBill Paul  */
2607e3d2833aSAlfred Perlstein static int
26080934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
260973bf949cSBill Paul {
261073bf949cSBill Paul 	struct dc_desc *cur_rx;
26110934f18aSMaxime Henrion 	int i, pos;
261273bf949cSBill Paul 
261373bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
261473bf949cSBill Paul 
261573bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
261673bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
2617af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
261873bf949cSBill Paul 			break;
261973bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
262073bf949cSBill Paul 	}
262173bf949cSBill Paul 
262273bf949cSBill Paul 	/* If the ring really is empty, then just return. */
262373bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
262473bf949cSBill Paul 		return (0);
262573bf949cSBill Paul 
262673bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
262773bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
262873bf949cSBill Paul 
262973bf949cSBill Paul 	return (EAGAIN);
263073bf949cSBill Paul }
263173bf949cSBill Paul 
263273bf949cSBill Paul /*
263396f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
263496f2e892SBill Paul  * the higher level protocols.
263596f2e892SBill Paul  */
2636e3d2833aSAlfred Perlstein static void
26370934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
263896f2e892SBill Paul {
2639432120f2SMarius Strobl 	struct mbuf *m, *m0;
264096f2e892SBill Paul 	struct ifnet *ifp;
264196f2e892SBill Paul 	struct dc_desc *cur_rx;
264296f2e892SBill Paul 	int i, total_len = 0;
264396f2e892SBill Paul 	u_int32_t rxstat;
264496f2e892SBill Paul 
26455120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
26465120abbfSSam Leffler 
2647fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
264896f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
264996f2e892SBill Paul 
265056e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
2651af4358c7SMaxime Henrion 	while (!(le32toh(sc->dc_ldata->dc_rx_list[i].dc_status) &
2652af4358c7SMaxime Henrion 	    DC_RXSTAT_OWN)) {
2653e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
265440929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2655e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2656e4fc250cSLuigi Rizzo 				break;
2657e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2658e4fc250cSLuigi Rizzo 		}
26590934f18aSMaxime Henrion #endif
266096f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
2661af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
266296f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
266356e5e7aeSMaxime Henrion 		bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
266456e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
266596f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
266696f2e892SBill Paul 
266796f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
266896f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
266996f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
267096f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
267196f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
267296f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
267396f2e892SBill Paul 					continue;
267496f2e892SBill Paul 				}
267596f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2676af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
267796f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
267896f2e892SBill Paul 			}
267996f2e892SBill Paul 		}
268096f2e892SBill Paul 
268196f2e892SBill Paul 		/*
268296f2e892SBill Paul 		 * If an error occurs, update stats, clear the
268396f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
268496f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2685db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
26860934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
268796f2e892SBill Paul 		 */
2688db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2689db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2690db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2691db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2692db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
269396f2e892SBill Paul 				ifp->if_ierrors++;
269496f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
269596f2e892SBill Paul 					ifp->if_collisions++;
269656e5e7aeSMaxime Henrion 				dc_newbuf(sc, i, 0);
269796f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
269896f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
269996f2e892SBill Paul 					continue;
270096f2e892SBill Paul 				} else {
2701c8b27acaSJohn Baldwin 					dc_init_locked(sc);
270296f2e892SBill Paul 					return;
270396f2e892SBill Paul 				}
270496f2e892SBill Paul 			}
2705db40c1aeSDoug Ambrisko 		}
270696f2e892SBill Paul 
270796f2e892SBill Paul 		/* No errors; receive the packet. */
270896f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2709432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
271001faf54bSLuigi Rizzo 		/*
2711432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
271201faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
271301faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
271401faf54bSLuigi Rizzo 		 * copy done in m_devget().
271501faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
271601faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
271701faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
271801faf54bSLuigi Rizzo 		 */
2719432120f2SMarius Strobl 		if (dc_newbuf(sc, i, 1) == 0) {
272001faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
272101faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
272201faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
272301faf54bSLuigi Rizzo 		} else
272401faf54bSLuigi Rizzo #endif
272501faf54bSLuigi Rizzo 		{
272601faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
272701faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
272856e5e7aeSMaxime Henrion 			dc_newbuf(sc, i, 0);
272996f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
273096f2e892SBill Paul 			if (m0 == NULL) {
273196f2e892SBill Paul 				ifp->if_ierrors++;
273296f2e892SBill Paul 				continue;
273396f2e892SBill Paul 			}
273496f2e892SBill Paul 			m = m0;
273501faf54bSLuigi Rizzo 		}
273696f2e892SBill Paul 
273796f2e892SBill Paul 		ifp->if_ipackets++;
27385120abbfSSam Leffler 		DC_UNLOCK(sc);
27399ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
27405120abbfSSam Leffler 		DC_LOCK(sc);
274196f2e892SBill Paul 	}
274296f2e892SBill Paul 
274396f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
274496f2e892SBill Paul }
274596f2e892SBill Paul 
274696f2e892SBill Paul /*
274796f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
274896f2e892SBill Paul  * the list buffers.
274996f2e892SBill Paul  */
2750e3d2833aSAlfred Perlstein static void
27510934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
275296f2e892SBill Paul {
275396f2e892SBill Paul 	struct dc_desc *cur_tx = NULL;
275496f2e892SBill Paul 	struct ifnet *ifp;
275596f2e892SBill Paul 	int idx;
2756af4358c7SMaxime Henrion 	u_int32_t ctl, txstat;
275796f2e892SBill Paul 
2758fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
275996f2e892SBill Paul 
276096f2e892SBill Paul 	/*
276196f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
276296f2e892SBill Paul 	 * frames that have been transmitted.
276396f2e892SBill Paul 	 */
276456e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
276596f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
276696f2e892SBill Paul 	while (idx != sc->dc_cdata.dc_tx_prod) {
276796f2e892SBill Paul 
276896f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
2769af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2770af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
277196f2e892SBill Paul 
277296f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
277396f2e892SBill Paul 			break;
277496f2e892SBill Paul 
27754ff4a9beSDon Lewis 		if (!(ctl & DC_TXCTL_LASTFRAG) || ctl & DC_TXCTL_SETUP) {
2776af4358c7SMaxime Henrion 			if (ctl & DC_TXCTL_SETUP) {
277796f2e892SBill Paul 				/*
277896f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
277996f2e892SBill Paul 				 * that it will sometimes generate a TX
278096f2e892SBill Paul 				 * underrun error while DMAing the RX
278196f2e892SBill Paul 				 * filter setup frame. If we detect this,
278296f2e892SBill Paul 				 * we have to send the setup frame again,
278396f2e892SBill Paul 				 * or else the filter won't be programmed
278496f2e892SBill Paul 				 * correctly.
278596f2e892SBill Paul 				 */
278696f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
278796f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
278896f2e892SBill Paul 						dc_setfilt(sc);
278996f2e892SBill Paul 				}
279096f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
279196f2e892SBill Paul 			}
2792bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
279396f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
279496f2e892SBill Paul 			continue;
279596f2e892SBill Paul 		}
279696f2e892SBill Paul 
279729a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2798feb78939SJonathan Chen 			/*
2799feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2800feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
280129a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
280229a2220aSBill Paul 			 * Who knows, but Conexant chips have the
280329a2220aSBill Paul 			 * same problem. Maybe they took lessons
280429a2220aSBill Paul 			 * from Xircom.
280529a2220aSBill Paul 			 */
2806feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2807feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2808feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2809feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2810feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2811feb78939SJonathan Chen 		} else {
281296f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
281396f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
281496f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
281596f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
281696f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2817feb78939SJonathan Chen 		}
281896f2e892SBill Paul 
281996f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
282096f2e892SBill Paul 			ifp->if_oerrors++;
282196f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
282296f2e892SBill Paul 				ifp->if_collisions++;
282396f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
282496f2e892SBill Paul 				ifp->if_collisions++;
282596f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
2826c8b27acaSJohn Baldwin 				dc_init_locked(sc);
282796f2e892SBill Paul 				return;
282896f2e892SBill Paul 			}
282996f2e892SBill Paul 		}
283096f2e892SBill Paul 
283196f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
283296f2e892SBill Paul 
283396f2e892SBill Paul 		ifp->if_opackets++;
283496f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
283556e5e7aeSMaxime Henrion 			bus_dmamap_sync(sc->dc_mtag,
283656e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx],
283756e5e7aeSMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
283856e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
283956e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx]);
284096f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
284196f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
284296f2e892SBill Paul 		}
284396f2e892SBill Paul 
284496f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
284596f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
284696f2e892SBill Paul 	}
284796f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
284882a67a70SMarius Strobl 
284982a67a70SMarius Strobl 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_RSVD)
285013f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
285182a67a70SMarius Strobl 
28523e0e6726SMarius Strobl 	if (sc->dc_cdata.dc_tx_cnt == 0)
28533e0e6726SMarius Strobl 		sc->dc_wdog_timer = 0;
285496f2e892SBill Paul }
285596f2e892SBill Paul 
2856e3d2833aSAlfred Perlstein static void
28570934f18aSMaxime Henrion dc_tick(void *xsc)
285896f2e892SBill Paul {
285996f2e892SBill Paul 	struct dc_softc *sc;
286096f2e892SBill Paul 	struct mii_data *mii;
286196f2e892SBill Paul 	struct ifnet *ifp;
286296f2e892SBill Paul 	u_int32_t r;
286396f2e892SBill Paul 
286496f2e892SBill Paul 	sc = xsc;
2865c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
2866fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
286796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
286896f2e892SBill Paul 
286996f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2870318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2871318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2872318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2873318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
287496f2e892SBill Paul 				sc->dc_link = 0;
2875318b02fdSBill Paul 				mii_mediachg(mii);
2876318b02fdSBill Paul 			}
2877318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2878318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2879318b02fdSBill Paul 				sc->dc_link = 0;
2880318b02fdSBill Paul 				mii_mediachg(mii);
2881318b02fdSBill Paul 			}
2882d675147eSBill Paul 			if (sc->dc_link == 0)
288396f2e892SBill Paul 				mii_tick(mii);
288496f2e892SBill Paul 		} else {
2885d0d67284SMarius Strobl 			/*
2886d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
2887d0d67284SMarius Strobl 			 * have to bite the bullet...
2888d0d67284SMarius Strobl 			 */
2889d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
2890d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
2891259b8d84SMartin Blapp 			    sc->dc_cdata.dc_tx_cnt == 0) {
289296f2e892SBill Paul 				mii_tick(mii);
2893042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2894042c8f6eSBill Paul 					sc->dc_link = 0;
289596f2e892SBill Paul 			}
2896259b8d84SMartin Blapp 		}
289796f2e892SBill Paul 	} else
289896f2e892SBill Paul 		mii_tick(mii);
289996f2e892SBill Paul 
290096f2e892SBill Paul 	/*
290196f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
290296f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
290396f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
290496f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
290596f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
290696f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
290796f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
290896f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
290996f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
291096f2e892SBill Paul 	 * a screeching halt for several seconds.
291196f2e892SBill Paul 	 *
291296f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
291396f2e892SBill Paul 	 * any packets until a link has been established. After the
291496f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
291596f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
291696f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
291796f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
291896f2e892SBill Paul 	 */
2919cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
292096f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
292196f2e892SBill Paul 		sc->dc_link++;
2922cbaf877fSBrian Feldman 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2923c8b27acaSJohn Baldwin 			dc_start_locked(ifp);
292496f2e892SBill Paul 	}
292596f2e892SBill Paul 
2926318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2927b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2928318b02fdSBill Paul 	else
2929b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
293096f2e892SBill Paul }
293196f2e892SBill Paul 
2932d467c136SBill Paul /*
2933d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2934d467c136SBill Paul  * or switch to store and forward mode if we have to.
2935d467c136SBill Paul  */
2936e3d2833aSAlfred Perlstein static void
29370934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
2938d467c136SBill Paul {
2939d467c136SBill Paul 	u_int32_t isr;
2940d467c136SBill Paul 	int i;
2941d467c136SBill Paul 
2942d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
2943c8b27acaSJohn Baldwin 		dc_init_locked(sc);
2944d467c136SBill Paul 
2945d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
2946d467c136SBill Paul 		/*
2947d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
2948d467c136SBill Paul 		 * in order to change the transmit threshold or store
2949d467c136SBill Paul 		 * and forward state.
2950d467c136SBill Paul 		 */
2951d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2952d467c136SBill Paul 
2953d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
2954d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
2955d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
2956d467c136SBill Paul 				break;
2957d467c136SBill Paul 			DELAY(10);
2958d467c136SBill Paul 		}
2959d467c136SBill Paul 		if (i == DC_TIMEOUT) {
29606b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
2961432120f2SMarius Strobl 			    "%s: failed to force tx to idle state\n",
2962432120f2SMarius Strobl 			    __func__);
2963c8b27acaSJohn Baldwin 			dc_init_locked(sc);
2964d467c136SBill Paul 		}
2965d467c136SBill Paul 	}
2966d467c136SBill Paul 
29676b9f5c94SGleb Smirnoff 	device_printf(sc->dc_dev, "TX underrun -- ");
2968d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
2969d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2970d467c136SBill Paul 		printf("using store and forward mode\n");
2971d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2972d467c136SBill Paul 	} else {
2973d467c136SBill Paul 		printf("increasing TX threshold\n");
2974d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2975d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2976d467c136SBill Paul 	}
2977d467c136SBill Paul 
2978d467c136SBill Paul 	if (DC_IS_INTEL(sc))
2979d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2980d467c136SBill Paul }
2981d467c136SBill Paul 
2982e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2983e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
2984e4fc250cSLuigi Rizzo 
2985e4fc250cSLuigi Rizzo static void
2986e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2987e4fc250cSLuigi Rizzo {
2988e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
2989e4fc250cSLuigi Rizzo 
299040929967SGleb Smirnoff 	DC_LOCK(sc);
299140929967SGleb Smirnoff 
299240929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
299340929967SGleb Smirnoff 		DC_UNLOCK(sc);
2994e4fc250cSLuigi Rizzo 		return;
2995e4fc250cSLuigi Rizzo 	}
299640929967SGleb Smirnoff 
2997e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
2998e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
2999e4fc250cSLuigi Rizzo 	dc_txeof(sc);
300013f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
300113f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3002c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3003e4fc250cSLuigi Rizzo 
3004e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3005e4fc250cSLuigi Rizzo 		u_int32_t	status;
3006e4fc250cSLuigi Rizzo 
3007e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3008e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3009e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3010e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
30115120abbfSSam Leffler 		if (!status) {
30125120abbfSSam Leffler 			DC_UNLOCK(sc);
3013e4fc250cSLuigi Rizzo 			return;
30145120abbfSSam Leffler 		}
3015e4fc250cSLuigi Rizzo 		/* ack what we have */
3016e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3017e4fc250cSLuigi Rizzo 
3018e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3019e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3020e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3021e4fc250cSLuigi Rizzo 
3022e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3023e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3024e4fc250cSLuigi Rizzo 		}
3025e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3026e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3027e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3028e4fc250cSLuigi Rizzo 
3029e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3030e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3031e4fc250cSLuigi Rizzo 
3032e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
30336b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
3034e4fc250cSLuigi Rizzo 			dc_reset(sc);
3035c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3036e4fc250cSLuigi Rizzo 		}
3037e4fc250cSLuigi Rizzo 	}
30385120abbfSSam Leffler 	DC_UNLOCK(sc);
3039e4fc250cSLuigi Rizzo }
3040e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3041e4fc250cSLuigi Rizzo 
3042e3d2833aSAlfred Perlstein static void
30430934f18aSMaxime Henrion dc_intr(void *arg)
304496f2e892SBill Paul {
304596f2e892SBill Paul 	struct dc_softc *sc;
304696f2e892SBill Paul 	struct ifnet *ifp;
304796f2e892SBill Paul 	u_int32_t status;
304896f2e892SBill Paul 
304996f2e892SBill Paul 	sc = arg;
3050d2a1864bSWarner Losh 
30510934f18aSMaxime Henrion 	if (sc->suspended)
3052e8388e14SMitsuru IWASAKI 		return;
3053e8388e14SMitsuru IWASAKI 
3054d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
3055d2a1864bSWarner Losh 		return;
3056d2a1864bSWarner Losh 
3057d1ce9105SBill Paul 	DC_LOCK(sc);
3058fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3059e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
306040929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
306140929967SGleb Smirnoff 		DC_UNLOCK(sc);
306240929967SGleb Smirnoff 		return;
3063e4fc250cSLuigi Rizzo 	}
30640934f18aSMaxime Henrion #endif
306596f2e892SBill Paul 
3066d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
306796f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
306896f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
306996f2e892SBill Paul 			dc_stop(sc);
3070d1ce9105SBill Paul 		DC_UNLOCK(sc);
307196f2e892SBill Paul 		return;
307296f2e892SBill Paul 	}
307396f2e892SBill Paul 
307496f2e892SBill Paul 	/* Disable interrupts. */
307596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
307696f2e892SBill Paul 
30777ed2454cSGleb Smirnoff 	while (((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) &&
30787ed2454cSGleb Smirnoff 	    status != 0xFFFFFFFF &&
30795108cc56SGleb Smirnoff 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
308096f2e892SBill Paul 
308196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
308296f2e892SBill Paul 
308373bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
308473bf949cSBill Paul 			int		curpkts;
308573bf949cSBill Paul 			curpkts = ifp->if_ipackets;
308696f2e892SBill Paul 			dc_rxeof(sc);
308773bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
308873bf949cSBill Paul 				while (dc_rx_resync(sc))
308973bf949cSBill Paul 					dc_rxeof(sc);
309073bf949cSBill Paul 			}
309173bf949cSBill Paul 		}
309296f2e892SBill Paul 
309396f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
309496f2e892SBill Paul 			dc_txeof(sc);
309596f2e892SBill Paul 
309696f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
309796f2e892SBill Paul 			dc_txeof(sc);
309896f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
309996f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
310096f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
310196f2e892SBill Paul 			}
310296f2e892SBill Paul 		}
310396f2e892SBill Paul 
3104d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3105d467c136SBill Paul 			dc_tx_underrun(sc);
310696f2e892SBill Paul 
310796f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
310873bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
310973bf949cSBill Paul 			int		curpkts;
311073bf949cSBill Paul 			curpkts = ifp->if_ipackets;
311196f2e892SBill Paul 			dc_rxeof(sc);
311273bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
311373bf949cSBill Paul 				while (dc_rx_resync(sc))
311473bf949cSBill Paul 					dc_rxeof(sc);
311573bf949cSBill Paul 			}
311673bf949cSBill Paul 		}
311796f2e892SBill Paul 
311896f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
311996f2e892SBill Paul 			dc_reset(sc);
3120c8b27acaSJohn Baldwin 			dc_init_locked(sc);
312196f2e892SBill Paul 		}
312296f2e892SBill Paul 	}
312396f2e892SBill Paul 
312496f2e892SBill Paul 	/* Re-enable interrupts. */
312596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
312696f2e892SBill Paul 
3127cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3128c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
312996f2e892SBill Paul 
3130d1ce9105SBill Paul 	DC_UNLOCK(sc);
313196f2e892SBill Paul }
313296f2e892SBill Paul 
313396f2e892SBill Paul /*
313496f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
313596f2e892SBill Paul  * pointers to the fragment pointers.
313696f2e892SBill Paul  */
3137e3d2833aSAlfred Perlstein static int
3138a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
313996f2e892SBill Paul {
3140ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
3141ebc284ccSMarius Strobl 	struct dc_desc *f;
314296f2e892SBill Paul 	struct mbuf *m;
3143993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3144cda97c50SMike Silbersack 
3145cda97c50SMike Silbersack 	/*
3146cda97c50SMike Silbersack 	 * If there's no way we can send any packets, return now.
3147cda97c50SMike Silbersack 	 */
314882a67a70SMarius Strobl 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_RSVD)
3149cda97c50SMike Silbersack 		return (ENOBUFS);
3150cda97c50SMike Silbersack 
3151993a741aSMarius Strobl 	m = NULL;
3152993a741aSMarius Strobl 	defragged = 0;
3153993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3154993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3155993a741aSMarius Strobl 		m = m_defrag(*m_head, M_DONTWAIT);
3156993a741aSMarius Strobl 		defragged = 1;
3157993a741aSMarius Strobl 	} else {
3158cda97c50SMike Silbersack 		/*
3159993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3160993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3161993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3162cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3163cda97c50SMike Silbersack 		 */
3164993a741aSMarius Strobl 		i = 0;
3165a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3166993a741aSMarius Strobl 			i++;
3167993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3168993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3169993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3170993a741aSMarius Strobl 			m = m_collapse(*m_head, M_DONTWAIT, DC_MAXFRAGS);
3171993a741aSMarius Strobl 			defragged = 1;
3172993a741aSMarius Strobl 		}
3173993a741aSMarius Strobl 	}
3174993a741aSMarius Strobl 	if (defragged != 0) {
317582a67a70SMarius Strobl 		if (m == NULL) {
317682a67a70SMarius Strobl 			m_freem(*m_head);
317782a67a70SMarius Strobl 			*m_head = NULL;
3178cda97c50SMike Silbersack 			return (ENOBUFS);
317982a67a70SMarius Strobl 		}
3180a10c0e45SMike Silbersack 		*m_head = m;
3181cda97c50SMike Silbersack 	}
3182993a741aSMarius Strobl 
318356e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
3184ebc284ccSMarius Strobl 	error = bus_dmamap_load_mbuf_sg(sc->dc_mtag,
3185ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3186ebc284ccSMarius Strobl 	if (error == EFBIG) {
3187993a741aSMarius Strobl 		if (defragged != 0 || (m = m_collapse(*m_head, M_DONTWAIT,
3188993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3189ebc284ccSMarius Strobl 			m_freem(*m_head);
319082a67a70SMarius Strobl 			*m_head = NULL;
3191993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
319282a67a70SMarius Strobl 		}
3193ebc284ccSMarius Strobl 		*m_head = m;
3194ebc284ccSMarius Strobl 		error = bus_dmamap_load_mbuf_sg(sc->dc_mtag,
3195ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3196ebc284ccSMarius Strobl 		if (error != 0) {
3197ebc284ccSMarius Strobl 			m_freem(*m_head);
3198ebc284ccSMarius Strobl 			*m_head = NULL;
3199ebc284ccSMarius Strobl 			return (error);
320082a67a70SMarius Strobl 		}
3201ebc284ccSMarius Strobl 	} else if (error != 0)
3202ebc284ccSMarius Strobl 		return (error);
3203ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3204ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3205ebc284ccSMarius Strobl 	if (nseg == 0) {
3206ebc284ccSMarius Strobl 		m_freem(*m_head);
3207ebc284ccSMarius Strobl 		*m_head = NULL;
3208ebc284ccSMarius Strobl 		return (EIO);
3209ebc284ccSMarius Strobl 	}
3210ebc284ccSMarius Strobl 
3211ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3212ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3213ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3214ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3215ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
3216ebc284ccSMarius Strobl 			bus_dmamap_unload(sc->dc_mtag,
3217ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3218ebc284ccSMarius Strobl 			m_freem(*m_head);
3219ebc284ccSMarius Strobl 			*m_head = NULL;
3220ebc284ccSMarius Strobl 			return (ENOBUFS);
3221ebc284ccSMarius Strobl 		}
3222ebc284ccSMarius Strobl 
3223ebc284ccSMarius Strobl 		f = &sc->dc_ldata->dc_tx_list[frag];
3224ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3225ebc284ccSMarius Strobl 		if (i == 0) {
3226ebc284ccSMarius Strobl 			f->dc_status = 0;
3227ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3228ebc284ccSMarius Strobl 		} else
3229ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
3230ebc284ccSMarius Strobl 		f->dc_data = htole32(segs[i].ds_addr);
3231ebc284ccSMarius Strobl 		cur = frag;
3232ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3233ebc284ccSMarius Strobl 	}
3234ebc284ccSMarius Strobl 
3235ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3236ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3237ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
3238ebc284ccSMarius Strobl 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3239ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
3240ebc284ccSMarius Strobl 		sc->dc_ldata->dc_tx_list[first].dc_ctl |=
3241ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3242ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
3243ebc284ccSMarius Strobl 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3244ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
3245ebc284ccSMarius Strobl 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3246ebc284ccSMarius Strobl 	sc->dc_ldata->dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3247ebc284ccSMarius Strobl 
324856e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
324956e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE);
325056e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
325156e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
325296f2e892SBill Paul 	return (0);
325396f2e892SBill Paul }
325496f2e892SBill Paul 
3255e3d2833aSAlfred Perlstein static void
32560934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
325796f2e892SBill Paul {
325896f2e892SBill Paul 	struct dc_softc *sc;
3259c8b27acaSJohn Baldwin 
3260c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3261c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3262c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3263c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3264c8b27acaSJohn Baldwin }
3265c8b27acaSJohn Baldwin 
3266ebc284ccSMarius Strobl /*
3267ebc284ccSMarius Strobl  * Main transmit routine
3268ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3269ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3270ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3271ebc284ccSMarius Strobl  * addresses.
3272ebc284ccSMarius Strobl  */
3273c8b27acaSJohn Baldwin static void
3274c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3275c8b27acaSJohn Baldwin {
3276c8b27acaSJohn Baldwin 	struct dc_softc *sc;
327782a67a70SMarius Strobl 	struct mbuf *m_head = NULL;
3278cbaf877fSBrian Feldman 	unsigned int queued = 0;
327996f2e892SBill Paul 	int idx;
328096f2e892SBill Paul 
328196f2e892SBill Paul 	sc = ifp->if_softc;
328296f2e892SBill Paul 
3283c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
328496f2e892SBill Paul 
3285c8b27acaSJohn Baldwin 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10)
328696f2e892SBill Paul 		return;
3287d1ce9105SBill Paul 
3288c8b27acaSJohn Baldwin 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
3289d1ce9105SBill Paul 		return;
329096f2e892SBill Paul 
329156e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
329296f2e892SBill Paul 
329396f2e892SBill Paul 	while (sc->dc_cdata.dc_tx_chain[idx] == NULL) {
3294cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
329596f2e892SBill Paul 		if (m_head == NULL)
329696f2e892SBill Paul 			break;
329796f2e892SBill Paul 
3298a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
329982a67a70SMarius Strobl 			if (m_head == NULL)
330082a67a70SMarius Strobl 				break;
3301cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
330213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
330396f2e892SBill Paul 			break;
330496f2e892SBill Paul 		}
330556e5e7aeSMaxime Henrion 		idx = sc->dc_cdata.dc_tx_prod;
330696f2e892SBill Paul 
3307cbaf877fSBrian Feldman 		queued++;
330896f2e892SBill Paul 		/*
330996f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
331096f2e892SBill Paul 		 * to him.
331196f2e892SBill Paul 		 */
33129ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
33135c1cfac4SBill Paul 
33145c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
331513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
33165c1cfac4SBill Paul 			break;
33175c1cfac4SBill Paul 		}
331896f2e892SBill Paul 	}
331996f2e892SBill Paul 
3320cbaf877fSBrian Feldman 	if (queued > 0) {
332196f2e892SBill Paul 		/* Transmit */
332296f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
332396f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
332496f2e892SBill Paul 
332596f2e892SBill Paul 		/*
332696f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
332796f2e892SBill Paul 		 */
3328b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3329cbaf877fSBrian Feldman 	}
333096f2e892SBill Paul }
333196f2e892SBill Paul 
3332e3d2833aSAlfred Perlstein static void
33330934f18aSMaxime Henrion dc_init(void *xsc)
333496f2e892SBill Paul {
333596f2e892SBill Paul 	struct dc_softc *sc = xsc;
3336c8b27acaSJohn Baldwin 
3337c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3338c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3339c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3340c8b27acaSJohn Baldwin }
3341c8b27acaSJohn Baldwin 
3342c8b27acaSJohn Baldwin static void
3343c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3344c8b27acaSJohn Baldwin {
3345fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
334696f2e892SBill Paul 	struct mii_data *mii;
334796f2e892SBill Paul 
3348c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
334996f2e892SBill Paul 
335096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
335196f2e892SBill Paul 
335296f2e892SBill Paul 	/*
335396f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
335496f2e892SBill Paul 	 */
335596f2e892SBill Paul 	dc_stop(sc);
335696f2e892SBill Paul 	dc_reset(sc);
335796f2e892SBill Paul 
335896f2e892SBill Paul 	/*
335996f2e892SBill Paul 	 * Set cache alignment and burst length.
336096f2e892SBill Paul 	 */
336188d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
336296f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
336396f2e892SBill Paul 	else
336496f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3365935fe010SLuigi Rizzo 	/*
3366935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3367935fe010SLuigi Rizzo 	 */
3368935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3369935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
337096f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
337196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
337296f2e892SBill Paul 	} else {
337396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
337496f2e892SBill Paul 	}
337596f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
337696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
337796f2e892SBill Paul 	switch(sc->dc_cachesize) {
337896f2e892SBill Paul 	case 32:
337996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
338096f2e892SBill Paul 		break;
338196f2e892SBill Paul 	case 16:
338296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
338396f2e892SBill Paul 		break;
338496f2e892SBill Paul 	case 8:
338596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
338696f2e892SBill Paul 		break;
338796f2e892SBill Paul 	case 0:
338896f2e892SBill Paul 	default:
338996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
339096f2e892SBill Paul 		break;
339196f2e892SBill Paul 	}
339296f2e892SBill Paul 
339396f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
339496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
339596f2e892SBill Paul 	else {
3396d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
339796f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
339896f2e892SBill Paul 		} else {
339996f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
340096f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
340196f2e892SBill Paul 		}
340296f2e892SBill Paul 	}
340396f2e892SBill Paul 
340496f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
340596f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
340696f2e892SBill Paul 
340796f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
340896f2e892SBill Paul 		/*
340996f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
341096f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
341196f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
341296f2e892SBill Paul 		 * document the meaning of these bits so there's no way
341396f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
341496f2e892SBill Paul 		 * number all its own; the rest all use a different one.
341596f2e892SBill Paul 		 */
341696f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
341796f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
341896f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
341996f2e892SBill Paul 		else
342096f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
342196f2e892SBill Paul 	}
342296f2e892SBill Paul 
3423feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3424feb78939SJonathan Chen 		/*
3425feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3426feb78939SJonathan Chen 		 * can talk to the MII.
3427feb78939SJonathan Chen 		 */
3428feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3429feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3430feb78939SJonathan Chen 		DELAY(10);
3431feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3432feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3433feb78939SJonathan Chen 		DELAY(10);
3434feb78939SJonathan Chen 	}
3435feb78939SJonathan Chen 
343696f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3437d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
343896f2e892SBill Paul 
343996f2e892SBill Paul 	/* Init circular RX list. */
344096f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
34416b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
344222f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
344396f2e892SBill Paul 		dc_stop(sc);
344496f2e892SBill Paul 		return;
344596f2e892SBill Paul 	}
344696f2e892SBill Paul 
344796f2e892SBill Paul 	/*
344856e5e7aeSMaxime Henrion 	 * Init TX descriptors.
344996f2e892SBill Paul 	 */
345096f2e892SBill Paul 	dc_list_tx_init(sc);
345196f2e892SBill Paul 
345296f2e892SBill Paul 	/*
345396f2e892SBill Paul 	 * Load the address of the RX list.
345496f2e892SBill Paul 	 */
345556e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
345656e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
345796f2e892SBill Paul 
345896f2e892SBill Paul 	/*
345996f2e892SBill Paul 	 * Enable interrupts.
346096f2e892SBill Paul 	 */
3461e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3462e4fc250cSLuigi Rizzo 	/*
3463e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3464e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3465e4fc250cSLuigi Rizzo 	 * after a reset.
3466e4fc250cSLuigi Rizzo 	 */
346740929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3468e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3469e4fc250cSLuigi Rizzo 	else
3470e4fc250cSLuigi Rizzo #endif
347196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
347296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
347396f2e892SBill Paul 
347496f2e892SBill Paul 	/* Enable transmitter. */
347596f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
347696f2e892SBill Paul 
347796f2e892SBill Paul 	/*
3478918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3479918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3480918434c8SBill Paul 	 * link and activity indications.
3481918434c8SBill Paul 	 */
348278999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3483918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3484918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
348578999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3486918434c8SBill Paul 	}
3487918434c8SBill Paul 
3488918434c8SBill Paul 	/*
348996f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
349096f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
349196f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
349296f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
349396f2e892SBill Paul 	 */
349496f2e892SBill Paul 	dc_setfilt(sc);
349596f2e892SBill Paul 
349696f2e892SBill Paul 	/* Enable receiver. */
349796f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
349896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
349996f2e892SBill Paul 
350096f2e892SBill Paul 	mii_mediachg(mii);
350196f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
350296f2e892SBill Paul 
350313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
350413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
350596f2e892SBill Paul 
3506857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
350745521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3508857fd445SBill Paul 		sc->dc_link = 1;
3509857fd445SBill Paul 	else {
3510318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3511b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3512318b02fdSBill Paul 		else
3513b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3514857fd445SBill Paul 	}
3515b1d16143SMarius Strobl 
3516b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3517b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
351896f2e892SBill Paul }
351996f2e892SBill Paul 
352096f2e892SBill Paul /*
352196f2e892SBill Paul  * Set media options.
352296f2e892SBill Paul  */
3523e3d2833aSAlfred Perlstein static int
35240934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
352596f2e892SBill Paul {
352696f2e892SBill Paul 	struct dc_softc *sc;
352796f2e892SBill Paul 	struct mii_data *mii;
3528f43d9309SBill Paul 	struct ifmedia *ifm;
352996f2e892SBill Paul 
353096f2e892SBill Paul 	sc = ifp->if_softc;
353196f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3532c8b27acaSJohn Baldwin 	DC_LOCK(sc);
353396f2e892SBill Paul 	mii_mediachg(mii);
3534f43d9309SBill Paul 	ifm = &mii->mii_media;
3535f43d9309SBill Paul 
3536f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
353745521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3538f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3539f43d9309SBill Paul 	else
354096f2e892SBill Paul 		sc->dc_link = 0;
3541c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
354296f2e892SBill Paul 
354396f2e892SBill Paul 	return (0);
354496f2e892SBill Paul }
354596f2e892SBill Paul 
354696f2e892SBill Paul /*
354796f2e892SBill Paul  * Report current media status.
354896f2e892SBill Paul  */
3549e3d2833aSAlfred Perlstein static void
35500934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
355196f2e892SBill Paul {
355296f2e892SBill Paul 	struct dc_softc *sc;
355396f2e892SBill Paul 	struct mii_data *mii;
3554f43d9309SBill Paul 	struct ifmedia *ifm;
355596f2e892SBill Paul 
355696f2e892SBill Paul 	sc = ifp->if_softc;
355796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3558c8b27acaSJohn Baldwin 	DC_LOCK(sc);
355996f2e892SBill Paul 	mii_pollstat(mii);
3560f43d9309SBill Paul 	ifm = &mii->mii_media;
3561f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
356245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3563f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3564f43d9309SBill Paul 			ifmr->ifm_status = 0;
3565432120f2SMarius Strobl 			DC_UNLOCK(sc);
3566f43d9309SBill Paul 			return;
3567f43d9309SBill Paul 		}
3568f43d9309SBill Paul 	}
356996f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
357096f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3571c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
357296f2e892SBill Paul }
357396f2e892SBill Paul 
3574e3d2833aSAlfred Perlstein static int
35750934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
357696f2e892SBill Paul {
357796f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
357896f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
357996f2e892SBill Paul 	struct mii_data *mii;
3580d1ce9105SBill Paul 	int error = 0;
358196f2e892SBill Paul 
358296f2e892SBill Paul 	switch (command) {
358396f2e892SBill Paul 	case SIOCSIFFLAGS:
3584c8b27acaSJohn Baldwin 		DC_LOCK(sc);
358596f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
35865d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
35875d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
35885d6dfbbbSLuigi Rizzo 
358913f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
35905d6dfbbbSLuigi Rizzo 				if (need_setfilt)
359196f2e892SBill Paul 					dc_setfilt(sc);
35925d6dfbbbSLuigi Rizzo 			} else {
359396f2e892SBill Paul 				sc->dc_txthresh = 0;
3594c8b27acaSJohn Baldwin 				dc_init_locked(sc);
359596f2e892SBill Paul 			}
359696f2e892SBill Paul 		} else {
359713f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
359896f2e892SBill Paul 				dc_stop(sc);
359996f2e892SBill Paul 		}
360096f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3601c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
360296f2e892SBill Paul 		error = 0;
360396f2e892SBill Paul 		break;
360496f2e892SBill Paul 	case SIOCADDMULTI:
360596f2e892SBill Paul 	case SIOCDELMULTI:
3606c8b27acaSJohn Baldwin 		DC_LOCK(sc);
360796f2e892SBill Paul 		dc_setfilt(sc);
3608c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
360996f2e892SBill Paul 		error = 0;
361096f2e892SBill Paul 		break;
361196f2e892SBill Paul 	case SIOCGIFMEDIA:
361296f2e892SBill Paul 	case SIOCSIFMEDIA:
361396f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
361496f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
361596f2e892SBill Paul 		break;
3616e695984eSRuslan Ermilov 	case SIOCSIFCAP:
361740929967SGleb Smirnoff #ifdef DEVICE_POLLING
361840929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
361940929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
362040929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
362140929967SGleb Smirnoff 			if (error)
362240929967SGleb Smirnoff 				return(error);
3623c8b27acaSJohn Baldwin 			DC_LOCK(sc);
362440929967SGleb Smirnoff 			/* Disable interrupts */
362540929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
362640929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3627c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
362840929967SGleb Smirnoff 			return (error);
362940929967SGleb Smirnoff 		}
363040929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
363140929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
363240929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
363340929967SGleb Smirnoff 			/* Enable interrupts. */
363440929967SGleb Smirnoff 			DC_LOCK(sc);
363540929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
363640929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
363740929967SGleb Smirnoff 			DC_UNLOCK(sc);
363840929967SGleb Smirnoff 			return (error);
363940929967SGleb Smirnoff 		}
364040929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3641e695984eSRuslan Ermilov 		break;
364296f2e892SBill Paul 	default:
36439ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
364496f2e892SBill Paul 		break;
364596f2e892SBill Paul 	}
364696f2e892SBill Paul 
364796f2e892SBill Paul 	return (error);
364896f2e892SBill Paul }
364996f2e892SBill Paul 
3650e3d2833aSAlfred Perlstein static void
3651b1d16143SMarius Strobl dc_watchdog(void *xsc)
365296f2e892SBill Paul {
3653b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3654b1d16143SMarius Strobl 	struct ifnet *ifp;
365596f2e892SBill Paul 
3656b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
365796f2e892SBill Paul 
3658b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3659b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3660b1d16143SMarius Strobl 		return;
3661b1d16143SMarius Strobl 	}
3662d1ce9105SBill Paul 
3663b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
366496f2e892SBill Paul 	ifp->if_oerrors++;
3665b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
366696f2e892SBill Paul 
366796f2e892SBill Paul 	dc_stop(sc);
366896f2e892SBill Paul 	dc_reset(sc);
3669c8b27acaSJohn Baldwin 	dc_init_locked(sc);
367096f2e892SBill Paul 
3671cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3672c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
367396f2e892SBill Paul }
367496f2e892SBill Paul 
367596f2e892SBill Paul /*
367696f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
367796f2e892SBill Paul  * RX and TX lists.
367896f2e892SBill Paul  */
3679e3d2833aSAlfred Perlstein static void
36800934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
368196f2e892SBill Paul {
368296f2e892SBill Paul 	struct ifnet *ifp;
3683b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3684b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3685b3811c95SMaxime Henrion 	int i;
3686af4358c7SMaxime Henrion 	u_int32_t ctl;
368796f2e892SBill Paul 
3688c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3689d1ce9105SBill Paul 
3690fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3691b3811c95SMaxime Henrion 	ld = sc->dc_ldata;
3692b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
369396f2e892SBill Paul 
3694b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3695b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3696b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
369796f2e892SBill Paul 
369813f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
36993b3ec200SPeter Wemm 
370096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
370196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
370296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
370396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
370496f2e892SBill Paul 	sc->dc_link = 0;
370596f2e892SBill Paul 
370696f2e892SBill Paul 	/*
370796f2e892SBill Paul 	 * Free data in the RX lists.
370896f2e892SBill Paul 	 */
370996f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3710b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
371156e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
371256e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
371396f2e892SBill Paul 		}
371496f2e892SBill Paul 	}
3715b3811c95SMaxime Henrion 	bzero(&ld->dc_rx_list, sizeof(ld->dc_rx_list));
371696f2e892SBill Paul 
371796f2e892SBill Paul 	/*
371896f2e892SBill Paul 	 * Free the TX list buffers.
371996f2e892SBill Paul 	 */
372096f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3721b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
3722af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
3723af4358c7SMaxime Henrion 			if ((ctl & DC_TXCTL_SETUP) ||
37244ff4a9beSDon Lewis 			    !(ctl & DC_TXCTL_LASTFRAG)) {
3725b3811c95SMaxime Henrion 				cd->dc_tx_chain[i] = NULL;
372696f2e892SBill Paul 				continue;
372796f2e892SBill Paul 			}
372856e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag, cd->dc_tx_map[i]);
372956e5e7aeSMaxime Henrion 			m_freem(cd->dc_tx_chain[i]);
3730b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
373196f2e892SBill Paul 		}
373296f2e892SBill Paul 	}
3733b3811c95SMaxime Henrion 	bzero(&ld->dc_tx_list, sizeof(ld->dc_tx_list));
373496f2e892SBill Paul }
373596f2e892SBill Paul 
373696f2e892SBill Paul /*
3737e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3738e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3739e8388e14SMitsuru IWASAKI  * resume.
3740e8388e14SMitsuru IWASAKI  */
3741e3d2833aSAlfred Perlstein static int
37420934f18aSMaxime Henrion dc_suspend(device_t dev)
3743e8388e14SMitsuru IWASAKI {
3744e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3745e8388e14SMitsuru IWASAKI 
3746e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3747c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3748e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3749e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3750c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3751e8388e14SMitsuru IWASAKI 
3752e8388e14SMitsuru IWASAKI 	return (0);
3753e8388e14SMitsuru IWASAKI }
3754e8388e14SMitsuru IWASAKI 
3755e8388e14SMitsuru IWASAKI /*
3756e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3757e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3758e8388e14SMitsuru IWASAKI  * appropriate.
3759e8388e14SMitsuru IWASAKI  */
3760e3d2833aSAlfred Perlstein static int
37610934f18aSMaxime Henrion dc_resume(device_t dev)
3762e8388e14SMitsuru IWASAKI {
3763e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3764e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
3765e8388e14SMitsuru IWASAKI 
3766e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3767fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3768e8388e14SMitsuru IWASAKI 
3769e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3770c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3771e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3772c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3773e8388e14SMitsuru IWASAKI 
3774e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3775c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3776e8388e14SMitsuru IWASAKI 
3777e8388e14SMitsuru IWASAKI 	return (0);
3778e8388e14SMitsuru IWASAKI }
3779e8388e14SMitsuru IWASAKI 
3780e8388e14SMitsuru IWASAKI /*
378196f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
378296f2e892SBill Paul  * get confused by errant DMAs when rebooting.
378396f2e892SBill Paul  */
37846a087a87SPyun YongHyeon static int
37850934f18aSMaxime Henrion dc_shutdown(device_t dev)
378696f2e892SBill Paul {
378796f2e892SBill Paul 	struct dc_softc *sc;
378896f2e892SBill Paul 
378996f2e892SBill Paul 	sc = device_get_softc(dev);
379096f2e892SBill Paul 
3791c8b27acaSJohn Baldwin 	DC_LOCK(sc);
379296f2e892SBill Paul 	dc_stop(sc);
3793c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
37946a087a87SPyun YongHyeon 
37956a087a87SPyun YongHyeon 	return (0);
379696f2e892SBill Paul }
3797