xref: /freebsd/sys/dev/dc/if_dc.c (revision 432120f2bb656eb8c96a9efe3dccbb76713fb3e3)
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>
107432120f2SMarius Strobl #include <sys/types.h>
10896f2e892SBill Paul 
10996f2e892SBill Paul #include <net/if.h>
11096f2e892SBill Paul #include <net/if_arp.h>
11196f2e892SBill Paul #include <net/ethernet.h>
11296f2e892SBill Paul #include <net/if_dl.h>
11396f2e892SBill Paul #include <net/if_media.h>
114db40c1aeSDoug Ambrisko #include <net/if_types.h>
115db40c1aeSDoug Ambrisko #include <net/if_vlan_var.h>
11696f2e892SBill Paul 
11796f2e892SBill Paul #include <net/bpf.h>
11896f2e892SBill Paul 
11996f2e892SBill Paul #include <machine/bus.h>
12096f2e892SBill Paul #include <machine/resource.h>
12196f2e892SBill Paul #include <sys/bus.h>
12296f2e892SBill Paul #include <sys/rman.h>
12396f2e892SBill Paul 
12496f2e892SBill Paul #include <dev/mii/mii.h>
12596f2e892SBill Paul #include <dev/mii/miivar.h>
12696f2e892SBill Paul 
12719b7ffd1SWarner Losh #include <dev/pci/pcireg.h>
12819b7ffd1SWarner Losh #include <dev/pci/pcivar.h>
12996f2e892SBill Paul 
13096f2e892SBill Paul #define DC_USEIOSPACE
13196f2e892SBill Paul 
1326a3033a8SWarner Losh #include <dev/dc/if_dcreg.h>
13396f2e892SBill Paul 
134ec6a7299SMaxime Henrion #ifdef __sparc64__
135ec6a7299SMaxime Henrion #include <dev/ofw/openfirm.h>
136ec6a7299SMaxime Henrion #include <machine/ofw_machdep.h>
137ec6a7299SMaxime Henrion #endif
138ec6a7299SMaxime Henrion 
139f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, pci, 1, 1, 1);
140f246e4a1SMatthew N. Dodd MODULE_DEPEND(dc, ether, 1, 1, 1);
14195a16455SPeter Wemm MODULE_DEPEND(dc, miibus, 1, 1, 1);
14295a16455SPeter Wemm 
143919ccba7SWarner Losh /*
144919ccba7SWarner Losh  * "device miibus" is required in kernel config.  See GENERIC if you get
145919ccba7SWarner Losh  * errors here.
146919ccba7SWarner Losh  */
14796f2e892SBill Paul #include "miibus_if.h"
14896f2e892SBill Paul 
14996f2e892SBill Paul /*
15096f2e892SBill Paul  * Various supported device vendors/types and their names.
15196f2e892SBill Paul  */
15296f2e892SBill Paul static struct dc_type dc_devs[] = {
1531e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143), 0,
15496f2e892SBill Paul 		"Intel 21143 10/100BaseTX" },
1551e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009), 0,
15638deb45fSTom Rhodes 		"Davicom DM9009 10/100BaseTX" },
1571e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100), 0,
15896f2e892SBill Paul 		"Davicom DM9100 10/100BaseTX" },
1591e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), DC_REVISION_DM9102A,
16088d739dcSBill Paul 		"Davicom DM9102A 10/100BaseTX" },
1611e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102), 0,
1621e2e70b1SJohn Baldwin 		"Davicom DM9102 10/100BaseTX" },
1631e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981), 0,
16496f2e892SBill Paul 		"ADMtek AL981 10/100BaseTX" },
1651e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
16696f2e892SBill Paul 		"ADMtek AN985 10/100BaseTX" },
1671e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
168e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1691e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
170e351d778SMartin Blapp 		"ADMtek ADM9513 10/100BaseTX" },
1711e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_FA511), 0,
1724c16d09eSWarner Losh 		"Netgear FA511 10/100BaseTX" },
1731e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), DC_REVISION_88141,
17496f2e892SBill Paul 		"ASIX AX88141 10/100BaseTX" },
1751e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A), 0,
1761e2e70b1SJohn Baldwin 		"ASIX AX88140A 10/100BaseTX" },
1771e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), DC_REVISION_98713A,
17896f2e892SBill Paul 		"Macronix 98713A 10/100BaseTX" },
1791e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713), 0,
1801e2e70b1SJohn Baldwin 		"Macronix 98713 10/100BaseTX" },
1811e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), DC_REVISION_98713A,
18296f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1831e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP), 0,
18496f2e892SBill Paul 		"Compex RL100-TX 10/100BaseTX" },
1851e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98725,
18696f2e892SBill Paul 		"Macronix 98725 10/100BaseTX" },
1871e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), DC_REVISION_98715AEC_C,
1881e2e70b1SJohn Baldwin 		"Macronix 98715AEC-C 10/100BaseTX" },
1891e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5), 0,
1901e2e70b1SJohn Baldwin 		"Macronix 98715/98715A 10/100BaseTX" },
1911e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727), 0,
192ead7cde9SBill Paul 		"Macronix 98727/98732 10/100BaseTX" },
1931e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115), 0,
19496f2e892SBill Paul 		"LC82C115 PNIC II 10/100BaseTX" },
1951e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), DC_REVISION_82C169,
19696f2e892SBill Paul 		"82c169 PNIC 10/100BaseTX" },
1971e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168), 0,
1981e2e70b1SJohn Baldwin 		"82c168 PNIC 10/100BaseTX" },
1991e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217), 0,
2009ca710f6SJeroen Ruigrok van der Werven 		"Accton EN1217 10/100BaseTX" },
2011e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242), 0,
202fa167b8eSBill Paul 		"Accton EN2242 MiniPCI 10/100BaseTX" },
2031e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201), 0,
204feb78939SJonathan Chen 	  	"Xircom X3201 10/100BaseTX" },
2051e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD), 0,
2069be0993cSJohn Baldwin 		"Neteasy DRP-32TXD Cardbus 10/100" },
2071e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500), 0,
2081d5e5310SBill Paul 		"Abocom FE2500 10/100BaseTX" },
2091e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX), 0,
210773c505fSMIHIRA Sanpei Yoshiro 		"Abocom FE2500MX 10/100BaseTX" },
2111e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112), 0,
2121af8bec7SBill Paul 		"Conexant LANfinity MiniPCI 10/100BaseTX" },
2131e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX), 0,
214948c244dSWarner Losh 		"Hawking CB102 CardBus 10/100" },
2151e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T), 0,
21697f91728SMIHIRA Sanpei Yoshiro 		"PlaneX FNW-3602-T CardBus 10/100" },
2171e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB), 0,
2187eac366bSMartin Blapp 		"3Com OfficeConnect 10/100B" },
2191e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120), 0,
220e7b9ab3aSBill Paul 		"Microsoft MN-120 CardBus 10/100" },
2211e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130), 0,
222e7b9ab3aSBill Paul 		"Microsoft MN-130 10/100" },
22317762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08), 0,
22417762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22517762569SGleb Smirnoff 	{ DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09), 0,
22617762569SGleb Smirnoff 		"Linksys PCMPC200 CardBus 10/100" },
22796f2e892SBill Paul 	{ 0, 0, NULL }
22896f2e892SBill Paul };
22996f2e892SBill Paul 
230e51a25f8SAlfred Perlstein static int dc_probe(device_t);
231e51a25f8SAlfred Perlstein static int dc_attach(device_t);
232e51a25f8SAlfred Perlstein static int dc_detach(device_t);
233e8388e14SMitsuru IWASAKI static int dc_suspend(device_t);
234e8388e14SMitsuru IWASAKI static int dc_resume(device_t);
235e51a25f8SAlfred Perlstein static struct dc_type *dc_devtype(device_t);
23656e5e7aeSMaxime Henrion static int dc_newbuf(struct dc_softc *, int, int);
237a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
238e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
239e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
240e51a25f8SAlfred Perlstein static void dc_rxeof(struct dc_softc *);
241e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
242e51a25f8SAlfred Perlstein static void dc_tick(void *);
243e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
244e51a25f8SAlfred Perlstein static void dc_intr(void *);
245e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
246c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
247e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
248e51a25f8SAlfred Perlstein static void dc_init(void *);
249c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
250e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
251e51a25f8SAlfred Perlstein static void dc_watchdog(struct ifnet *);
252e51a25f8SAlfred Perlstein static void dc_shutdown(device_t);
253e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
254e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
25596f2e892SBill Paul 
256e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
257e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
258e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
259e51a25f8SAlfred Perlstein static void dc_eeprom_getword(struct dc_softc *, int, u_int16_t *);
260d24ae19dSWarner Losh static void dc_eeprom_getword_pnic(struct dc_softc *, int, u_int16_t *);
261d24ae19dSWarner Losh static void dc_eeprom_getword_xircom(struct dc_softc *, int, u_int16_t *);
2623097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
263e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
26496f2e892SBill Paul 
265e51a25f8SAlfred Perlstein static void dc_mii_writebit(struct dc_softc *, int);
266e51a25f8SAlfred Perlstein static int dc_mii_readbit(struct dc_softc *);
267e51a25f8SAlfred Perlstein static void dc_mii_sync(struct dc_softc *);
268e51a25f8SAlfred Perlstein static void dc_mii_send(struct dc_softc *, u_int32_t, int);
269e51a25f8SAlfred Perlstein static int dc_mii_readreg(struct dc_softc *, struct dc_mii_frame *);
270e51a25f8SAlfred Perlstein static int dc_mii_writereg(struct dc_softc *, struct dc_mii_frame *);
271e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
272e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
273e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
274e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
27596f2e892SBill Paul 
276e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2773373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2783373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
279e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
280e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
281e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
282e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
28396f2e892SBill Paul 
284e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
28596f2e892SBill Paul 
286e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
287e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
288e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
28996f2e892SBill Paul 
2903097aa70SWarner Losh static void dc_read_srom(struct dc_softc *, int);
291e51a25f8SAlfred Perlstein static void dc_parse_21143_srom(struct dc_softc *);
292e51a25f8SAlfred Perlstein static void dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
293e51a25f8SAlfred Perlstein static void dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
294e51a25f8SAlfred Perlstein static void dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
295e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
2965c1cfac4SBill Paul 
297d24ae19dSWarner Losh static void dc_dma_map_txbuf(void *, bus_dma_segment_t *, int, bus_size_t, int);
298d24ae19dSWarner Losh static void dc_dma_map_rxbuf(void *, bus_dma_segment_t *, int, bus_size_t, int);
29956e5e7aeSMaxime Henrion 
30096f2e892SBill Paul #ifdef DC_USEIOSPACE
30196f2e892SBill Paul #define DC_RES			SYS_RES_IOPORT
30296f2e892SBill Paul #define DC_RID			DC_PCI_CFBIO
30396f2e892SBill Paul #else
30496f2e892SBill Paul #define DC_RES			SYS_RES_MEMORY
30596f2e892SBill Paul #define DC_RID			DC_PCI_CFBMA
30696f2e892SBill Paul #endif
30796f2e892SBill Paul 
30896f2e892SBill Paul static device_method_t dc_methods[] = {
30996f2e892SBill Paul 	/* Device interface */
31096f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
31196f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
31296f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
313e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
314e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
31596f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
31696f2e892SBill Paul 
31796f2e892SBill Paul 	/* bus interface */
31896f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
31996f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
32096f2e892SBill Paul 
32196f2e892SBill Paul 	/* MII interface */
32296f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
32396f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
32496f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
325f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
32696f2e892SBill Paul 
32796f2e892SBill Paul 	{ 0, 0 }
32896f2e892SBill Paul };
32996f2e892SBill Paul 
33096f2e892SBill Paul static driver_t dc_driver = {
33196f2e892SBill Paul 	"dc",
33296f2e892SBill Paul 	dc_methods,
33396f2e892SBill Paul 	sizeof(struct dc_softc)
33496f2e892SBill Paul };
33596f2e892SBill Paul 
33696f2e892SBill Paul static devclass_t dc_devclass;
33796f2e892SBill Paul 
338347934faSWarner Losh DRIVER_MODULE(dc, cardbus, dc_driver, dc_devclass, 0, 0);
339f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
34096f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
34196f2e892SBill Paul 
34296f2e892SBill Paul #define DC_SETBIT(sc, reg, x)				\
34396f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
34496f2e892SBill Paul 
34596f2e892SBill Paul #define DC_CLRBIT(sc, reg, x)				\
34696f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
34796f2e892SBill Paul 
34896f2e892SBill Paul #define SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
34996f2e892SBill Paul #define SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
35096f2e892SBill Paul 
351e3d2833aSAlfred Perlstein static void
3520934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
35396f2e892SBill Paul {
35496f2e892SBill Paul 	int idx;
35596f2e892SBill Paul 
35696f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
35796f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
35896f2e892SBill Paul }
35996f2e892SBill Paul 
3602c876e15SPoul-Henning Kamp static void
3610934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3623097aa70SWarner Losh {
3633097aa70SWarner Losh 	int i;
3643097aa70SWarner Losh 
3653097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3663097aa70SWarner Losh 	dc_eeprom_idle(sc);
3673097aa70SWarner Losh 
3683097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3693097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3703097aa70SWarner Losh 	dc_delay(sc);
3713097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3723097aa70SWarner Losh 	dc_delay(sc);
3733097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3743097aa70SWarner Losh 	dc_delay(sc);
3753097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3763097aa70SWarner Losh 	dc_delay(sc);
3773097aa70SWarner Losh 
3783097aa70SWarner Losh 	for (i = 3; i--;) {
3793097aa70SWarner Losh 		if (6 & (1 << i))
3803097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3813097aa70SWarner Losh 		else
3823097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3833097aa70SWarner Losh 		dc_delay(sc);
3843097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3853097aa70SWarner Losh 		dc_delay(sc);
3863097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3873097aa70SWarner Losh 		dc_delay(sc);
3883097aa70SWarner Losh 	}
3893097aa70SWarner Losh 
3903097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3913097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3923097aa70SWarner Losh 		dc_delay(sc);
3933097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3943097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3953097aa70SWarner Losh 			dc_delay(sc);
3963097aa70SWarner Losh 			break;
3973097aa70SWarner Losh 		}
3983097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3993097aa70SWarner Losh 		dc_delay(sc);
4003097aa70SWarner Losh 	}
4013097aa70SWarner Losh 
4023097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4033097aa70SWarner Losh 	dc_eeprom_idle(sc);
4043097aa70SWarner Losh 
4053097aa70SWarner Losh 	if (i < 4 || i > 12)
4063097aa70SWarner Losh 		sc->dc_romwidth = 6;
4073097aa70SWarner Losh 	else
4083097aa70SWarner Losh 		sc->dc_romwidth = i;
4093097aa70SWarner Losh 
4103097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4113097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4123097aa70SWarner Losh 	dc_delay(sc);
4133097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4143097aa70SWarner Losh 	dc_delay(sc);
4153097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4163097aa70SWarner Losh 	dc_delay(sc);
4173097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4183097aa70SWarner Losh 	dc_delay(sc);
4193097aa70SWarner Losh 
4203097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4213097aa70SWarner Losh 	dc_eeprom_idle(sc);
4223097aa70SWarner Losh }
4233097aa70SWarner Losh 
424e3d2833aSAlfred Perlstein static void
4250934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
42696f2e892SBill Paul {
4270934f18aSMaxime Henrion 	int i;
42896f2e892SBill Paul 
42996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
43096f2e892SBill Paul 	dc_delay(sc);
43196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
43296f2e892SBill Paul 	dc_delay(sc);
43396f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43496f2e892SBill Paul 	dc_delay(sc);
43596f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
43696f2e892SBill Paul 	dc_delay(sc);
43796f2e892SBill Paul 
43896f2e892SBill Paul 	for (i = 0; i < 25; i++) {
43996f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44096f2e892SBill Paul 		dc_delay(sc);
44196f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44296f2e892SBill Paul 		dc_delay(sc);
44396f2e892SBill Paul 	}
44496f2e892SBill Paul 
44596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44696f2e892SBill Paul 	dc_delay(sc);
44796f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
44896f2e892SBill Paul 	dc_delay(sc);
44996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
45096f2e892SBill Paul }
45196f2e892SBill Paul 
45296f2e892SBill Paul /*
45396f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
45496f2e892SBill Paul  */
455e3d2833aSAlfred Perlstein static void
4560934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
45796f2e892SBill Paul {
4580934f18aSMaxime Henrion 	int d, i;
45996f2e892SBill Paul 
4603097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4613097aa70SWarner Losh 	for (i = 3; i--; ) {
4623097aa70SWarner Losh 		if (d & (1 << i))
4633097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
46496f2e892SBill Paul 		else
4653097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4663097aa70SWarner Losh 		dc_delay(sc);
4673097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4683097aa70SWarner Losh 		dc_delay(sc);
4693097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4703097aa70SWarner Losh 		dc_delay(sc);
4713097aa70SWarner Losh 	}
47296f2e892SBill Paul 
47396f2e892SBill Paul 	/*
47496f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
47596f2e892SBill Paul 	 */
4763097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4773097aa70SWarner Losh 		if (addr & (1 << i)) {
47896f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
47996f2e892SBill Paul 		} else {
48096f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
48196f2e892SBill Paul 		}
48296f2e892SBill Paul 		dc_delay(sc);
48396f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48496f2e892SBill Paul 		dc_delay(sc);
48596f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
48696f2e892SBill Paul 		dc_delay(sc);
48796f2e892SBill Paul 	}
48896f2e892SBill Paul }
48996f2e892SBill Paul 
49096f2e892SBill Paul /*
49196f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
49296f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
49396f2e892SBill Paul  * the EEPROM.
49496f2e892SBill Paul  */
495e3d2833aSAlfred Perlstein static void
4960934f18aSMaxime Henrion dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, u_int16_t *dest)
49796f2e892SBill Paul {
4980934f18aSMaxime Henrion 	int i;
49996f2e892SBill Paul 	u_int32_t r;
50096f2e892SBill Paul 
50196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
50296f2e892SBill Paul 
50396f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
50496f2e892SBill Paul 		DELAY(1);
50596f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
50696f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
50796f2e892SBill Paul 			*dest = (u_int16_t)(r & 0xFFFF);
50896f2e892SBill Paul 			return;
50996f2e892SBill Paul 		}
51096f2e892SBill Paul 	}
51196f2e892SBill Paul }
51296f2e892SBill Paul 
51396f2e892SBill Paul /*
51496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
515feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
516feb78939SJonathan Chen  * the EEPROM, too.
517feb78939SJonathan Chen  */
518e3d2833aSAlfred Perlstein static void
5190934f18aSMaxime Henrion dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, u_int16_t *dest)
520feb78939SJonathan Chen {
5210934f18aSMaxime Henrion 
522feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
523feb78939SJonathan Chen 
524feb78939SJonathan Chen 	addr *= 2;
525feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
526feb78939SJonathan Chen 	*dest = (u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
527feb78939SJonathan Chen 	addr += 1;
528feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
529feb78939SJonathan Chen 	*dest |= ((u_int16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
530feb78939SJonathan Chen 
531feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
532feb78939SJonathan Chen }
533feb78939SJonathan Chen 
534feb78939SJonathan Chen /*
535feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
53696f2e892SBill Paul  */
537e3d2833aSAlfred Perlstein static void
5380934f18aSMaxime Henrion dc_eeprom_getword(struct dc_softc *sc, int addr, u_int16_t *dest)
53996f2e892SBill Paul {
5400934f18aSMaxime Henrion 	int i;
54196f2e892SBill Paul 	u_int16_t word = 0;
54296f2e892SBill Paul 
54396f2e892SBill Paul 	/* Force EEPROM to idle state. */
54496f2e892SBill Paul 	dc_eeprom_idle(sc);
54596f2e892SBill Paul 
54696f2e892SBill Paul 	/* Enter EEPROM access mode. */
54796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
54896f2e892SBill Paul 	dc_delay(sc);
54996f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
55096f2e892SBill Paul 	dc_delay(sc);
55196f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
55296f2e892SBill Paul 	dc_delay(sc);
55396f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55496f2e892SBill Paul 	dc_delay(sc);
55596f2e892SBill Paul 
55696f2e892SBill Paul 	/*
55796f2e892SBill Paul 	 * Send address of word we want to read.
55896f2e892SBill Paul 	 */
55996f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
56096f2e892SBill Paul 
56196f2e892SBill Paul 	/*
56296f2e892SBill Paul 	 * Start reading bits from EEPROM.
56396f2e892SBill Paul 	 */
56496f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56596f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
56696f2e892SBill Paul 		dc_delay(sc);
56796f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
56896f2e892SBill Paul 			word |= i;
56996f2e892SBill Paul 		dc_delay(sc);
57096f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
57196f2e892SBill Paul 		dc_delay(sc);
57296f2e892SBill Paul 	}
57396f2e892SBill Paul 
57496f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57596f2e892SBill Paul 	dc_eeprom_idle(sc);
57696f2e892SBill Paul 
57796f2e892SBill Paul 	*dest = word;
57896f2e892SBill Paul }
57996f2e892SBill Paul 
58096f2e892SBill Paul /*
58196f2e892SBill Paul  * Read a sequence of words from the EEPROM.
58296f2e892SBill Paul  */
583e3d2833aSAlfred Perlstein static void
5848c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
58596f2e892SBill Paul {
58696f2e892SBill Paul 	int i;
58796f2e892SBill Paul 	u_int16_t word = 0, *ptr;
58896f2e892SBill Paul 
58996f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
59096f2e892SBill Paul 		if (DC_IS_PNIC(sc))
59196f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
592feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
593feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59496f2e892SBill Paul 		else
59596f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
59696f2e892SBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
5978c7ff1f3SMaxime Henrion 		if (be)
5988c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
59996f2e892SBill Paul 		else
6008c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
60196f2e892SBill Paul 	}
60296f2e892SBill Paul }
60396f2e892SBill Paul 
60496f2e892SBill Paul /*
60596f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
60696f2e892SBill Paul  * Application Notes pp.19-21.
60796f2e892SBill Paul  */
60896f2e892SBill Paul /*
60996f2e892SBill Paul  * Write a bit to the MII bus.
61096f2e892SBill Paul  */
611e3d2833aSAlfred Perlstein static void
6120934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
61396f2e892SBill Paul {
6140934f18aSMaxime Henrion 
61596f2e892SBill Paul 	if (bit)
61696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO,
61796f2e892SBill Paul 		    DC_SIO_ROMCTL_WRITE | DC_SIO_MII_DATAOUT);
61896f2e892SBill Paul 	else
61996f2e892SBill Paul 		CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
62096f2e892SBill Paul 
62196f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
62296f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
62396f2e892SBill Paul }
62496f2e892SBill Paul 
62596f2e892SBill Paul /*
62696f2e892SBill Paul  * Read a bit from the MII bus.
62796f2e892SBill Paul  */
628e3d2833aSAlfred Perlstein static int
6290934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
63096f2e892SBill Paul {
6310934f18aSMaxime Henrion 
63296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR);
63396f2e892SBill Paul 	CSR_READ_4(sc, DC_SIO);
63496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63596f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_MII_CLK);
63696f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
63796f2e892SBill Paul 		return (1);
63896f2e892SBill Paul 
63996f2e892SBill Paul 	return (0);
64096f2e892SBill Paul }
64196f2e892SBill Paul 
64296f2e892SBill Paul /*
64396f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
64496f2e892SBill Paul  */
645e3d2833aSAlfred Perlstein static void
6460934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
64796f2e892SBill Paul {
6480934f18aSMaxime Henrion 	int i;
64996f2e892SBill Paul 
65096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
65196f2e892SBill Paul 
65296f2e892SBill Paul 	for (i = 0; i < 32; i++)
65396f2e892SBill Paul 		dc_mii_writebit(sc, 1);
65496f2e892SBill Paul }
65596f2e892SBill Paul 
65696f2e892SBill Paul /*
65796f2e892SBill Paul  * Clock a series of bits through the MII.
65896f2e892SBill Paul  */
659e3d2833aSAlfred Perlstein static void
6600934f18aSMaxime Henrion dc_mii_send(struct dc_softc *sc, u_int32_t bits, int cnt)
66196f2e892SBill Paul {
66296f2e892SBill Paul 	int i;
66396f2e892SBill Paul 
66496f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
66596f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
66696f2e892SBill Paul }
66796f2e892SBill Paul 
66896f2e892SBill Paul /*
66996f2e892SBill Paul  * Read an PHY register through the MII.
67096f2e892SBill Paul  */
671e3d2833aSAlfred Perlstein static int
6720934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
67396f2e892SBill Paul {
674d1ce9105SBill Paul 	int i, ack;
67596f2e892SBill Paul 
67696f2e892SBill Paul 	/*
67796f2e892SBill Paul 	 * Set up frame for RX.
67896f2e892SBill Paul 	 */
67996f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
68096f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
68196f2e892SBill Paul 	frame->mii_turnaround = 0;
68296f2e892SBill Paul 	frame->mii_data = 0;
68396f2e892SBill Paul 
68496f2e892SBill Paul 	/*
68596f2e892SBill Paul 	 * Sync the PHYs.
68696f2e892SBill Paul 	 */
68796f2e892SBill Paul 	dc_mii_sync(sc);
68896f2e892SBill Paul 
68996f2e892SBill Paul 	/*
69096f2e892SBill Paul 	 * Send command/address info.
69196f2e892SBill Paul 	 */
69296f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
69396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
69496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
69596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
69696f2e892SBill Paul 
69796f2e892SBill Paul #ifdef notdef
69896f2e892SBill Paul 	/* Idle bit */
69996f2e892SBill Paul 	dc_mii_writebit(sc, 1);
70096f2e892SBill Paul 	dc_mii_writebit(sc, 0);
70196f2e892SBill Paul #endif
70296f2e892SBill Paul 
7030934f18aSMaxime Henrion 	/* Check for ack. */
70496f2e892SBill Paul 	ack = dc_mii_readbit(sc);
70596f2e892SBill Paul 
70696f2e892SBill Paul 	/*
70796f2e892SBill Paul 	 * Now try reading data bits. If the ack failed, we still
70896f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
70996f2e892SBill Paul 	 */
71096f2e892SBill Paul 	if (ack) {
7110934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
71296f2e892SBill Paul 			dc_mii_readbit(sc);
71396f2e892SBill Paul 		goto fail;
71496f2e892SBill Paul 	}
71596f2e892SBill Paul 
71696f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
71796f2e892SBill Paul 		if (!ack) {
71896f2e892SBill Paul 			if (dc_mii_readbit(sc))
71996f2e892SBill Paul 				frame->mii_data |= i;
72096f2e892SBill Paul 		}
72196f2e892SBill Paul 	}
72296f2e892SBill Paul 
72396f2e892SBill Paul fail:
72496f2e892SBill Paul 
72596f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72696f2e892SBill Paul 	dc_mii_writebit(sc, 0);
72796f2e892SBill Paul 
72896f2e892SBill Paul 	if (ack)
72996f2e892SBill Paul 		return (1);
73096f2e892SBill Paul 	return (0);
73196f2e892SBill Paul }
73296f2e892SBill Paul 
73396f2e892SBill Paul /*
73496f2e892SBill Paul  * Write to a PHY register through the MII.
73596f2e892SBill Paul  */
736e3d2833aSAlfred Perlstein static int
7370934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
73896f2e892SBill Paul {
7390934f18aSMaxime Henrion 
74096f2e892SBill Paul 	/*
74196f2e892SBill Paul 	 * Set up frame for TX.
74296f2e892SBill Paul 	 */
74396f2e892SBill Paul 
74496f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
74596f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
74696f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
74796f2e892SBill Paul 
74896f2e892SBill Paul 	/*
74996f2e892SBill Paul 	 * Sync the PHYs.
75096f2e892SBill Paul 	 */
75196f2e892SBill Paul 	dc_mii_sync(sc);
75296f2e892SBill Paul 
75396f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
75496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
75596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
75696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
75796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
75896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
75996f2e892SBill Paul 
76096f2e892SBill Paul 	/* Idle bit. */
76196f2e892SBill Paul 	dc_mii_writebit(sc, 0);
76296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
76396f2e892SBill Paul 
76496f2e892SBill Paul 	return (0);
76596f2e892SBill Paul }
76696f2e892SBill Paul 
767e3d2833aSAlfred Perlstein static int
7680934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
76996f2e892SBill Paul {
77096f2e892SBill Paul 	struct dc_mii_frame frame;
77196f2e892SBill Paul 	struct dc_softc	 *sc;
772c85c4667SBill Paul 	int i, rval, phy_reg = 0;
77396f2e892SBill Paul 
77496f2e892SBill Paul 	sc = device_get_softc(dev);
7750934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
77696f2e892SBill Paul 
77796f2e892SBill Paul 	/*
77896f2e892SBill Paul 	 * Note: both the AL981 and AN985 have internal PHYs,
77996f2e892SBill Paul 	 * however the AL981 provides direct access to the PHY
78096f2e892SBill Paul 	 * registers while the AN985 uses a serial MII interface.
78196f2e892SBill Paul 	 * The AN985's MII interface is also buggy in that you
78296f2e892SBill Paul 	 * can read from any MII address (0 to 31), but only address 1
78396f2e892SBill Paul 	 * behaves normally. To deal with both cases, we pretend
78496f2e892SBill Paul 	 * that the PHY is at MII address 1.
78596f2e892SBill Paul 	 */
78696f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
78796f2e892SBill Paul 		return (0);
78896f2e892SBill Paul 
7891af8bec7SBill Paul 	/*
7901af8bec7SBill Paul 	 * Note: the ukphy probes of the RS7112 report a PHY at
7911af8bec7SBill Paul 	 * MII address 0 (possibly HomePNA?) and 1 (ethernet)
7921af8bec7SBill Paul 	 * so we only respond to correct one.
7931af8bec7SBill Paul 	 */
7941af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
7951af8bec7SBill Paul 		return (0);
7961af8bec7SBill Paul 
7975c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
79896f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
79996f2e892SBill Paul 			switch (reg) {
80096f2e892SBill Paul 			case MII_BMSR:
80196f2e892SBill Paul 			/*
80296f2e892SBill Paul 			 * Fake something to make the probe
80396f2e892SBill Paul 			 * code think there's a PHY here.
80496f2e892SBill Paul 			 */
80596f2e892SBill Paul 				return (BMSR_MEDIAMASK);
80696f2e892SBill Paul 				break;
80796f2e892SBill Paul 			case MII_PHYIDR1:
80896f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80996f2e892SBill Paul 					return (DC_VENDORID_LO);
81096f2e892SBill Paul 				return (DC_VENDORID_DEC);
81196f2e892SBill Paul 				break;
81296f2e892SBill Paul 			case MII_PHYIDR2:
81396f2e892SBill Paul 				if (DC_IS_PNIC(sc))
81496f2e892SBill Paul 					return (DC_DEVICEID_82C168);
81596f2e892SBill Paul 				return (DC_DEVICEID_21143);
81696f2e892SBill Paul 				break;
81796f2e892SBill Paul 			default:
81896f2e892SBill Paul 				return (0);
81996f2e892SBill Paul 				break;
82096f2e892SBill Paul 			}
82196f2e892SBill Paul 		} else
82296f2e892SBill Paul 			return (0);
82396f2e892SBill Paul 	}
82496f2e892SBill Paul 
82596f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
82696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
82796f2e892SBill Paul 		    (phy << 23) | (reg << 18));
82896f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
82996f2e892SBill Paul 			DELAY(1);
83096f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
83196f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
83296f2e892SBill Paul 				rval &= 0xFFFF;
83396f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
83496f2e892SBill Paul 			}
83596f2e892SBill Paul 		}
83696f2e892SBill Paul 		return (0);
83796f2e892SBill Paul 	}
83896f2e892SBill Paul 
83996f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
84096f2e892SBill Paul 		switch (reg) {
84196f2e892SBill Paul 		case MII_BMCR:
84296f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
84396f2e892SBill Paul 			break;
84496f2e892SBill Paul 		case MII_BMSR:
84596f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
84696f2e892SBill Paul 			break;
84796f2e892SBill Paul 		case MII_PHYIDR1:
84896f2e892SBill Paul 			phy_reg = DC_AL_VENID;
84996f2e892SBill Paul 			break;
85096f2e892SBill Paul 		case MII_PHYIDR2:
85196f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
85296f2e892SBill Paul 			break;
85396f2e892SBill Paul 		case MII_ANAR:
85496f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
85596f2e892SBill Paul 			break;
85696f2e892SBill Paul 		case MII_ANLPAR:
85796f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
85896f2e892SBill Paul 			break;
85996f2e892SBill Paul 		case MII_ANER:
86096f2e892SBill Paul 			phy_reg = DC_AL_ANER;
86196f2e892SBill Paul 			break;
86296f2e892SBill Paul 		default:
86322f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
86422f6205dSJohn Baldwin 			    reg);
86596f2e892SBill Paul 			return (0);
86696f2e892SBill Paul 			break;
86796f2e892SBill Paul 		}
86896f2e892SBill Paul 
86996f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
87096f2e892SBill Paul 
87196f2e892SBill Paul 		if (rval == 0xFFFF)
87296f2e892SBill Paul 			return (0);
87396f2e892SBill Paul 		return (rval);
87496f2e892SBill Paul 	}
87596f2e892SBill Paul 
87696f2e892SBill Paul 	frame.mii_phyaddr = phy;
87796f2e892SBill Paul 	frame.mii_regaddr = reg;
878419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
879f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
880f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
881419146d9SBill Paul 	}
88296f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
883419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
884f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
88596f2e892SBill Paul 
88696f2e892SBill Paul 	return (frame.mii_data);
88796f2e892SBill Paul }
88896f2e892SBill Paul 
889e3d2833aSAlfred Perlstein static int
8900934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
89196f2e892SBill Paul {
89296f2e892SBill Paul 	struct dc_softc *sc;
89396f2e892SBill Paul 	struct dc_mii_frame frame;
894c85c4667SBill Paul 	int i, phy_reg = 0;
89596f2e892SBill Paul 
89696f2e892SBill Paul 	sc = device_get_softc(dev);
8970934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
89896f2e892SBill Paul 
89996f2e892SBill Paul 	if (DC_IS_ADMTEK(sc) && phy != DC_ADMTEK_PHYADDR)
90096f2e892SBill Paul 		return (0);
90196f2e892SBill Paul 
9021af8bec7SBill Paul 	if (DC_IS_CONEXANT(sc) && phy != DC_CONEXANT_PHYADDR)
9031af8bec7SBill Paul 		return (0);
9041af8bec7SBill Paul 
90596f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
90696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
90796f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
90896f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
90996f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
91096f2e892SBill Paul 				break;
91196f2e892SBill Paul 		}
91296f2e892SBill Paul 		return (0);
91396f2e892SBill Paul 	}
91496f2e892SBill Paul 
91596f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
91696f2e892SBill Paul 		switch (reg) {
91796f2e892SBill Paul 		case MII_BMCR:
91896f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
91996f2e892SBill Paul 			break;
92096f2e892SBill Paul 		case MII_BMSR:
92196f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
92296f2e892SBill Paul 			break;
92396f2e892SBill Paul 		case MII_PHYIDR1:
92496f2e892SBill Paul 			phy_reg = DC_AL_VENID;
92596f2e892SBill Paul 			break;
92696f2e892SBill Paul 		case MII_PHYIDR2:
92796f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
92896f2e892SBill Paul 			break;
92996f2e892SBill Paul 		case MII_ANAR:
93096f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
93196f2e892SBill Paul 			break;
93296f2e892SBill Paul 		case MII_ANLPAR:
93396f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
93496f2e892SBill Paul 			break;
93596f2e892SBill Paul 		case MII_ANER:
93696f2e892SBill Paul 			phy_reg = DC_AL_ANER;
93796f2e892SBill Paul 			break;
93896f2e892SBill Paul 		default:
93922f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
94022f6205dSJohn Baldwin 			    reg);
94196f2e892SBill Paul 			return (0);
94296f2e892SBill Paul 			break;
94396f2e892SBill Paul 		}
94496f2e892SBill Paul 
94596f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
94696f2e892SBill Paul 		return (0);
94796f2e892SBill Paul 	}
94896f2e892SBill Paul 
94996f2e892SBill Paul 	frame.mii_phyaddr = phy;
95096f2e892SBill Paul 	frame.mii_regaddr = reg;
95196f2e892SBill Paul 	frame.mii_data = data;
95296f2e892SBill Paul 
953419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
954f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
955f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
956419146d9SBill Paul 	}
95796f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
958419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
959f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
96096f2e892SBill Paul 
96196f2e892SBill Paul 	return (0);
96296f2e892SBill Paul }
96396f2e892SBill Paul 
964e3d2833aSAlfred Perlstein static void
9650934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
96696f2e892SBill Paul {
96796f2e892SBill Paul 	struct dc_softc *sc;
96896f2e892SBill Paul 	struct mii_data *mii;
969f43d9309SBill Paul 	struct ifmedia *ifm;
97096f2e892SBill Paul 
97196f2e892SBill Paul 	sc = device_get_softc(dev);
97296f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
97396f2e892SBill Paul 		return;
9745c1cfac4SBill Paul 
97596f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
976f43d9309SBill Paul 	ifm = &mii->mii_media;
977f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
97845521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
979f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
980f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
981f43d9309SBill Paul 	} else {
98296f2e892SBill Paul 		dc_setcfg(sc, mii->mii_media_active);
98396f2e892SBill Paul 		sc->dc_if_media = mii->mii_media_active;
984f43d9309SBill Paul 	}
985f43d9309SBill Paul }
986f43d9309SBill Paul 
987f43d9309SBill Paul /*
988f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
989f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
990f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
991f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
992f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
993f43d9309SBill Paul  * with it itself. *sigh*
994f43d9309SBill Paul  */
995e3d2833aSAlfred Perlstein static void
9960934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
997f43d9309SBill Paul {
998f43d9309SBill Paul 	struct dc_softc *sc;
999f43d9309SBill Paul 	struct mii_data *mii;
1000f43d9309SBill Paul 	struct ifmedia *ifm;
1001f43d9309SBill Paul 	int rev;
1002f43d9309SBill Paul 
10031e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
1004f43d9309SBill Paul 
1005f43d9309SBill Paul 	sc = device_get_softc(dev);
1006f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1007f43d9309SBill Paul 	ifm = &mii->mii_media;
1008f43d9309SBill Paul 
1009f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
101045521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
101196f2e892SBill Paul }
101296f2e892SBill Paul 
101379d11e09SBill Paul #define DC_BITS_512	9
101479d11e09SBill Paul #define DC_BITS_128	7
101579d11e09SBill Paul #define DC_BITS_64	6
101696f2e892SBill Paul 
10173373489bSWarner Losh static uint32_t
10183373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
101996f2e892SBill Paul {
10203373489bSWarner Losh 	uint32_t crc;
102196f2e892SBill Paul 
102296f2e892SBill Paul 	/* Compute CRC for the address value. */
10230e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
102496f2e892SBill Paul 
102579d11e09SBill Paul 	/*
102679d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
102779d11e09SBill Paul 	 * chips is only 128 bits wide.
102879d11e09SBill Paul 	 */
102979d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
103079d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
103196f2e892SBill Paul 
103279d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
103379d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
103479d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
103579d11e09SBill Paul 
1036feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1037feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1038feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1039feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10400934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1041feb78939SJonathan Chen 		else
10420934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10430934f18aSMaxime Henrion 			    (12 << 4));
1044feb78939SJonathan Chen 	}
1045feb78939SJonathan Chen 
104679d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
104796f2e892SBill Paul }
104896f2e892SBill Paul 
104996f2e892SBill Paul /*
105096f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
105196f2e892SBill Paul  */
10523373489bSWarner Losh static uint32_t
10533373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
105496f2e892SBill Paul {
10550e939c0cSChristian Weisgerber 	uint32_t crc;
105696f2e892SBill Paul 
105796f2e892SBill Paul 	/* Compute CRC for the address value. */
10580e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
105996f2e892SBill Paul 
10600934f18aSMaxime Henrion 	/* Return the filter bit position. */
106196f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
106296f2e892SBill Paul }
106396f2e892SBill Paul 
106496f2e892SBill Paul /*
106596f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
106696f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
106796f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
106896f2e892SBill Paul  *
106996f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
107096f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
107196f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
107296f2e892SBill Paul  * we need that too.
107396f2e892SBill Paul  */
10742c876e15SPoul-Henning Kamp static void
10750934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
107696f2e892SBill Paul {
10778df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
107896f2e892SBill Paul 	struct dc_desc *sframe;
107996f2e892SBill Paul 	u_int32_t h, *sp;
108096f2e892SBill Paul 	struct ifmultiaddr *ifma;
108196f2e892SBill Paul 	struct ifnet *ifp;
108296f2e892SBill Paul 	int i;
108396f2e892SBill Paul 
1084fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
108596f2e892SBill Paul 
108696f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
108796f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
108896f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
108996f2e892SBill Paul 	sframe = &sc->dc_ldata->dc_tx_list[i];
109056e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
10910934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
109296f2e892SBill Paul 
1093af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1094af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1095af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
109696f2e892SBill Paul 
109756e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
109896f2e892SBill Paul 
109996f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
110096f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
110196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110296f2e892SBill Paul 	else
110396f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110496f2e892SBill Paul 
110596f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
110696f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
110796f2e892SBill Paul 	else
110896f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
110996f2e892SBill Paul 
111013b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
11116817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
111296f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
111396f2e892SBill Paul 			continue;
1114aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
111596f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1116af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
111796f2e892SBill Paul 	}
111813b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
111996f2e892SBill Paul 
112096f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1121aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1122af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
112396f2e892SBill Paul 	}
112496f2e892SBill Paul 
11258df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
11268df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11278df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
11288df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
11298df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
113096f2e892SBill Paul 
1131af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
113296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
113396f2e892SBill Paul 
113496f2e892SBill Paul 	/*
113596f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
113696f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
113796f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
113896f2e892SBill Paul 	 * medicine.
113996f2e892SBill Paul 	 */
114096f2e892SBill Paul 	DELAY(10000);
114196f2e892SBill Paul 
114296f2e892SBill Paul 	ifp->if_timer = 5;
114396f2e892SBill Paul }
114496f2e892SBill Paul 
11452c876e15SPoul-Henning Kamp static void
11460934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
114796f2e892SBill Paul {
11488df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
114996f2e892SBill Paul 	struct ifnet *ifp;
11500934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
115196f2e892SBill Paul 	int h = 0;
115296f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
115396f2e892SBill Paul 
1154fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
115596f2e892SBill Paul 
11560934f18aSMaxime Henrion 	/* Init our MAC address. */
11578df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11588df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[0]);
11598df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[1]);
116096f2e892SBill Paul 
116196f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
116296f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
116396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116496f2e892SBill Paul 	else
116596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
116696f2e892SBill Paul 
116796f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
116896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
116996f2e892SBill Paul 	else
117096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
117196f2e892SBill Paul 
11720934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
117396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
117496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
117596f2e892SBill Paul 
117696f2e892SBill Paul 	/*
117796f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
117896f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
117996f2e892SBill Paul 	 */
118096f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
118196f2e892SBill Paul 		return;
118296f2e892SBill Paul 
11830934f18aSMaxime Henrion 	/* Now program new ones. */
118413b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
11856817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
118696f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
118796f2e892SBill Paul 			continue;
1188acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1189aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1190aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1191acc1bcccSMartin Blapp 		else
1192aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1193aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
119496f2e892SBill Paul 		if (h < 32)
119596f2e892SBill Paul 			hashes[0] |= (1 << h);
119696f2e892SBill Paul 		else
119796f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
119896f2e892SBill Paul 	}
119913b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
120096f2e892SBill Paul 
120196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
120296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
120396f2e892SBill Paul }
120496f2e892SBill Paul 
12052c876e15SPoul-Henning Kamp static void
12060934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
120796f2e892SBill Paul {
12088df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
120996f2e892SBill Paul 	struct ifnet *ifp;
12100934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
121196f2e892SBill Paul 	int h = 0;
121296f2e892SBill Paul 	u_int32_t hashes[2] = { 0, 0 };
121396f2e892SBill Paul 
1214fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
121596f2e892SBill Paul 
12168df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
12178df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
121896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
12198df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
122096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
12218df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
122296f2e892SBill Paul 
122396f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
122496f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
122596f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122696f2e892SBill Paul 	else
122796f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
122896f2e892SBill Paul 
122996f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
123096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
123196f2e892SBill Paul 	else
123296f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
123396f2e892SBill Paul 
123496f2e892SBill Paul 	/*
123596f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
123696f2e892SBill Paul 	 * of broadcast frames.
123796f2e892SBill Paul 	 */
123896f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
123996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
124096f2e892SBill Paul 	else
124196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
124296f2e892SBill Paul 
124396f2e892SBill Paul 	/* first, zot all the existing hash bits */
124496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
124596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
124796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
124896f2e892SBill Paul 
124996f2e892SBill Paul 	/*
125096f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
125196f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
125296f2e892SBill Paul 	 */
125396f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
125496f2e892SBill Paul 		return;
125596f2e892SBill Paul 
125696f2e892SBill Paul 	/* now program new ones */
125713b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
12586817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
125996f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
126096f2e892SBill Paul 			continue;
1261aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
126296f2e892SBill Paul 		if (h < 32)
126396f2e892SBill Paul 			hashes[0] |= (1 << h);
126496f2e892SBill Paul 		else
126596f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
126696f2e892SBill Paul 	}
126713b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
126896f2e892SBill Paul 
126996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
127096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
127196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
127296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
127396f2e892SBill Paul }
127496f2e892SBill Paul 
12752c876e15SPoul-Henning Kamp static void
12760934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1277feb78939SJonathan Chen {
12788df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12790934f18aSMaxime Henrion 	struct ifnet *ifp;
12800934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1281feb78939SJonathan Chen 	struct dc_desc *sframe;
1282feb78939SJonathan Chen 	u_int32_t h, *sp;
1283feb78939SJonathan Chen 	int i;
1284feb78939SJonathan Chen 
1285fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1286feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1287feb78939SJonathan Chen 
1288feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1289feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1290feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
1291feb78939SJonathan Chen 	sframe = &sc->dc_ldata->dc_tx_list[i];
129256e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
12930934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1294feb78939SJonathan Chen 
1295af4358c7SMaxime Henrion 	sframe->dc_data = htole32(sc->dc_saddr);
1296af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1297af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1298feb78939SJonathan Chen 
129956e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1300feb78939SJonathan Chen 
1301feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1302feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1303feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1304feb78939SJonathan Chen 	else
1305feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1306feb78939SJonathan Chen 
1307feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1308feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1309feb78939SJonathan Chen 	else
1310feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1311feb78939SJonathan Chen 
131213b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
13136817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1314feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1315feb78939SJonathan Chen 			continue;
1316aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13171d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1318af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1319feb78939SJonathan Chen 	}
132013b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
1321feb78939SJonathan Chen 
1322feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1323aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1324af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1325feb78939SJonathan Chen 	}
1326feb78939SJonathan Chen 
13278df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13288df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13298df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13308df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13318df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1332feb78939SJonathan Chen 
1333feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1334feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
133513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
1336af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
1337feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1338feb78939SJonathan Chen 
1339feb78939SJonathan Chen 	/*
13400934f18aSMaxime Henrion 	 * Wait some time...
1341feb78939SJonathan Chen 	 */
1342feb78939SJonathan Chen 	DELAY(1000);
1343feb78939SJonathan Chen 
1344feb78939SJonathan Chen 	ifp->if_timer = 5;
1345feb78939SJonathan Chen }
1346feb78939SJonathan Chen 
1347e3d2833aSAlfred Perlstein static void
13480934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
134996f2e892SBill Paul {
13500934f18aSMaxime Henrion 
135196f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13521af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
135396f2e892SBill Paul 		dc_setfilt_21143(sc);
135496f2e892SBill Paul 
135596f2e892SBill Paul 	if (DC_IS_ASIX(sc))
135696f2e892SBill Paul 		dc_setfilt_asix(sc);
135796f2e892SBill Paul 
135896f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
135996f2e892SBill Paul 		dc_setfilt_admtek(sc);
136096f2e892SBill Paul 
1361feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1362feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
136396f2e892SBill Paul }
136496f2e892SBill Paul 
136596f2e892SBill Paul /*
13660934f18aSMaxime Henrion  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
13670934f18aSMaxime Henrion  * the netconfig register, we first have to put the transmit and/or
13680934f18aSMaxime Henrion  * receive logic in the idle state.
136996f2e892SBill Paul  */
1370e3d2833aSAlfred Perlstein static void
13710934f18aSMaxime Henrion dc_setcfg(struct dc_softc *sc, int media)
137296f2e892SBill Paul {
13730934f18aSMaxime Henrion 	int i, restart = 0, watchdogreg;
137496f2e892SBill Paul 	u_int32_t isr;
137596f2e892SBill Paul 
137696f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_NONE)
137796f2e892SBill Paul 		return;
137896f2e892SBill Paul 
137996f2e892SBill Paul 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
138096f2e892SBill Paul 		restart = 1;
138196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
138296f2e892SBill Paul 
138396f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
138496f2e892SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
1385d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE &&
1386351267c1SMartin Blapp 			    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1387351267c1SMartin Blapp 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
138896f2e892SBill Paul 				break;
1389d467c136SBill Paul 			DELAY(10);
139096f2e892SBill Paul 		}
139196f2e892SBill Paul 
1392432120f2SMarius Strobl 		if (i == DC_TIMEOUT) {
1393432120f2SMarius Strobl 			if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13946b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
1395432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
1396432120f2SMarius Strobl 				    __func__);
1397432120f2SMarius Strobl 			if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1398432120f2SMarius Strobl 			    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1399432120f2SMarius Strobl 			    !(DC_IS_CENTAUR(sc) || DC_IS_CONEXANT(sc) ||
1400432120f2SMarius Strobl 			    (DC_IS_DAVICOM(sc) && pci_get_revid(sc->dc_dev) >=
1401432120f2SMarius Strobl 			    DC_REVISION_DM9102A)))
1402432120f2SMarius Strobl 				device_printf(sc->dc_dev,
1403432120f2SMarius Strobl 				    "%s: failed to force rx to idle state\n",
1404432120f2SMarius Strobl 				    __func__);
1405432120f2SMarius Strobl 		}
140696f2e892SBill Paul 	}
140796f2e892SBill Paul 
140896f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1409042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1410042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
141196f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1412bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14130934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14148273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14158273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14168273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14174c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1418bf645417SBill Paul 			} else {
1419bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1420bf645417SBill Paul 			}
142196f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142296f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
142396f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
142496f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
142596f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
142688d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
142796f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
142896f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1429e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1430e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
143196f2e892SBill Paul 		} else {
143296f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
143396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
143496f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
143596f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
143696f2e892SBill Paul 			}
1437318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1438318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1439318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14405c1cfac4SBill Paul 			if (DC_IS_INTEL(sc))
14415c1cfac4SBill Paul 				dc_apply_fixup(sc,
14425c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14435c1cfac4SBill Paul 				    IFM_100_TX | IFM_FDX : IFM_100_TX);
144496f2e892SBill Paul 		}
144596f2e892SBill Paul 	}
144696f2e892SBill Paul 
144796f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1448042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1449042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
145096f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14510934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14524c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14538273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14548273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14558273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14568273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14574c2efe27SBill Paul 			} else {
14584c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14594c2efe27SBill Paul 			}
146096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
146196f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
146296f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
146396f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146488d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
146596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
146696f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
1467e99285a4SBill Paul 			if (DC_IS_INTEL(sc))
1468e99285a4SBill Paul 				dc_apply_fixup(sc, IFM_AUTO);
146996f2e892SBill Paul 		} else {
147096f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
147196f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
147296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
147396f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
147496f2e892SBill Paul 			}
147596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1476318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
147796f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14785c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14795c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14805c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14815c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14825c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14835c1cfac4SBill Paul 				else
14845c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14855c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14865c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14875c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
14885c1cfac4SBill Paul 				dc_apply_fixup(sc,
14895c1cfac4SBill Paul 				    (media & IFM_GMASK) == IFM_FDX ?
14905c1cfac4SBill Paul 				    IFM_10_T | IFM_FDX : IFM_10_T);
14915c1cfac4SBill Paul 				DELAY(20000);
14925c1cfac4SBill Paul 			}
149396f2e892SBill Paul 		}
149496f2e892SBill Paul 	}
149596f2e892SBill Paul 
1496f43d9309SBill Paul 	/*
1497f43d9309SBill Paul 	 * If this is a Davicom DM9102A card with a DM9801 HomePNA
1498f43d9309SBill Paul 	 * PHY and we want HomePNA mode, set the portsel bit to turn
1499f43d9309SBill Paul 	 * on the external MII port.
1500f43d9309SBill Paul 	 */
1501f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
150245521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(media) == IFM_HPNA_1) {
1503f43d9309SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1504f43d9309SBill Paul 			sc->dc_link = 1;
1505f43d9309SBill Paul 		} else {
1506f43d9309SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1507f43d9309SBill Paul 		}
1508f43d9309SBill Paul 	}
1509f43d9309SBill Paul 
151096f2e892SBill Paul 	if ((media & IFM_GMASK) == IFM_FDX) {
151196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
151296f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
151396f2e892SBill Paul 			DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
151496f2e892SBill Paul 	} else {
151596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_FULLDUPLEX);
151696f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_SYM && DC_IS_PNIC(sc))
151796f2e892SBill Paul 			DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_DUPLEX);
151896f2e892SBill Paul 	}
151996f2e892SBill Paul 
152096f2e892SBill Paul 	if (restart)
152196f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON | DC_NETCFG_RX_ON);
152296f2e892SBill Paul }
152396f2e892SBill Paul 
1524e3d2833aSAlfred Perlstein static void
15250934f18aSMaxime Henrion dc_reset(struct dc_softc *sc)
152696f2e892SBill Paul {
15270934f18aSMaxime Henrion 	int i;
152896f2e892SBill Paul 
152996f2e892SBill Paul 	DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
153096f2e892SBill Paul 
153196f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
153296f2e892SBill Paul 		DELAY(10);
153396f2e892SBill Paul 		if (!(CSR_READ_4(sc, DC_BUSCTL) & DC_BUSCTL_RESET))
153496f2e892SBill Paul 			break;
153596f2e892SBill Paul 	}
153696f2e892SBill Paul 
15371af8bec7SBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_ADMTEK(sc) || DC_IS_CONEXANT(sc) ||
15381d5e5310SBill Paul 	    DC_IS_XIRCOM(sc) || DC_IS_INTEL(sc)) {
153996f2e892SBill Paul 		DELAY(10000);
154096f2e892SBill Paul 		DC_CLRBIT(sc, DC_BUSCTL, DC_BUSCTL_RESET);
154196f2e892SBill Paul 		i = 0;
154296f2e892SBill Paul 	}
154396f2e892SBill Paul 
154496f2e892SBill Paul 	if (i == DC_TIMEOUT)
15456b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev, "reset never completed!\n");
154696f2e892SBill Paul 
154796f2e892SBill Paul 	/* Wait a little while for the chip to get its brains in order. */
154896f2e892SBill Paul 	DELAY(1000);
154996f2e892SBill Paul 
155096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
155196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_BUSCTL, 0x00000000);
155296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_NETCFG, 0x00000000);
155396f2e892SBill Paul 
155491cc2adbSBill Paul 	/*
155591cc2adbSBill Paul 	 * Bring the SIA out of reset. In some cases, it looks
155691cc2adbSBill Paul 	 * like failing to unreset the SIA soon enough gets it
155791cc2adbSBill Paul 	 * into a state where it will never come out of reset
155891cc2adbSBill Paul 	 * until we reset the whole chip again.
155991cc2adbSBill Paul 	 */
15605c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
156191cc2adbSBill Paul 		DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
15625c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_10BTCTRL, 0);
15635c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15645c1cfac4SBill Paul 	}
156596f2e892SBill Paul }
156696f2e892SBill Paul 
1567e3d2833aSAlfred Perlstein static struct dc_type *
15680934f18aSMaxime Henrion dc_devtype(device_t dev)
156996f2e892SBill Paul {
157096f2e892SBill Paul 	struct dc_type *t;
15711e2e70b1SJohn Baldwin 	u_int32_t devid;
15721e2e70b1SJohn Baldwin 	u_int8_t rev;
157396f2e892SBill Paul 
157496f2e892SBill Paul 	t = dc_devs;
15751e2e70b1SJohn Baldwin 	devid = pci_get_devid(dev);
15761e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
157796f2e892SBill Paul 
157896f2e892SBill Paul 	while (t->dc_name != NULL) {
15791e2e70b1SJohn Baldwin 		if (devid == t->dc_devid && rev >= t->dc_minrev)
158096f2e892SBill Paul 			return (t);
158196f2e892SBill Paul 		t++;
158296f2e892SBill Paul 	}
158396f2e892SBill Paul 
158496f2e892SBill Paul 	return (NULL);
158596f2e892SBill Paul }
158696f2e892SBill Paul 
158796f2e892SBill Paul /*
158896f2e892SBill Paul  * Probe for a 21143 or clone chip. Check the PCI vendor and device
158996f2e892SBill Paul  * IDs against our list and return a device name if we find a match.
159096f2e892SBill Paul  * We do a little bit of extra work to identify the exact type of
159196f2e892SBill Paul  * chip. The MX98713 and MX98713A have the same PCI vendor/device ID,
159296f2e892SBill Paul  * but different revision IDs. The same is true for 98715/98715A
159396f2e892SBill Paul  * chips and the 98725, as well as the ASIX and ADMtek chips. In some
159496f2e892SBill Paul  * cases, the exact chip revision affects driver behavior.
159596f2e892SBill Paul  */
1596e3d2833aSAlfred Perlstein static int
15970934f18aSMaxime Henrion dc_probe(device_t dev)
159896f2e892SBill Paul {
159996f2e892SBill Paul 	struct dc_type *t;
160096f2e892SBill Paul 
160196f2e892SBill Paul 	t = dc_devtype(dev);
160296f2e892SBill Paul 
160396f2e892SBill Paul 	if (t != NULL) {
160496f2e892SBill Paul 		device_set_desc(dev, t->dc_name);
1605d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
160696f2e892SBill Paul 	}
160796f2e892SBill Paul 
160896f2e892SBill Paul 	return (ENXIO);
160996f2e892SBill Paul }
161096f2e892SBill Paul 
1611e3d2833aSAlfred Perlstein static void
16120934f18aSMaxime Henrion dc_apply_fixup(struct dc_softc *sc, int media)
16135c1cfac4SBill Paul {
16145c1cfac4SBill Paul 	struct dc_mediainfo *m;
16155c1cfac4SBill Paul 	u_int8_t *p;
16165c1cfac4SBill Paul 	int i;
16175d801891SBill Paul 	u_int32_t reg;
16185c1cfac4SBill Paul 
16195c1cfac4SBill Paul 	m = sc->dc_mi;
16205c1cfac4SBill Paul 
16215c1cfac4SBill Paul 	while (m != NULL) {
16225c1cfac4SBill Paul 		if (m->dc_media == media)
16235c1cfac4SBill Paul 			break;
16245c1cfac4SBill Paul 		m = m->dc_next;
16255c1cfac4SBill Paul 	}
16265c1cfac4SBill Paul 
16275c1cfac4SBill Paul 	if (m == NULL)
16285c1cfac4SBill Paul 		return;
16295c1cfac4SBill Paul 
16305c1cfac4SBill Paul 	for (i = 0, p = m->dc_reset_ptr; i < m->dc_reset_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 	for (i = 0, p = m->dc_gp_ptr; i < m->dc_gp_len; i++, p += 2) {
16365c1cfac4SBill Paul 		reg = (p[0] | (p[1] << 8)) << 16;
16375c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, reg);
16385c1cfac4SBill Paul 	}
16395c1cfac4SBill Paul }
16405c1cfac4SBill Paul 
1641e3d2833aSAlfred Perlstein static void
16420934f18aSMaxime Henrion dc_decode_leaf_sia(struct dc_softc *sc, struct dc_eblock_sia *l)
16435c1cfac4SBill Paul {
16445c1cfac4SBill Paul 	struct dc_mediainfo *m;
16455c1cfac4SBill Paul 
16460934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
164787f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
164887f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16495c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
165087f4fa15SMartin Blapp 		break;
165187f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16525c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
165387f4fa15SMartin Blapp 		break;
165487f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16555c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
165687f4fa15SMartin Blapp 		break;
165787f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16585c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
165987f4fa15SMartin Blapp 		break;
166087f4fa15SMartin Blapp 	default:
166187f4fa15SMartin Blapp 		break;
166287f4fa15SMartin Blapp 	}
16635c1cfac4SBill Paul 
166487f4fa15SMartin Blapp 	/*
166587f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
166687f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
166787f4fa15SMartin Blapp 	 * supply Media Specific Data.
166887f4fa15SMartin Blapp 	 */
166987f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16705c1cfac4SBill Paul 		m->dc_gp_len = 2;
167187f4fa15SMartin Blapp 		m->dc_gp_ptr =
167287f4fa15SMartin Blapp 		(u_int8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
167387f4fa15SMartin Blapp 	} else {
167487f4fa15SMartin Blapp 		m->dc_gp_len = 2;
167587f4fa15SMartin Blapp 		m->dc_gp_ptr =
167687f4fa15SMartin Blapp 		(u_int8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
167787f4fa15SMartin Blapp 	}
16785c1cfac4SBill Paul 
16795c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16805c1cfac4SBill Paul 	sc->dc_mi = m;
16815c1cfac4SBill Paul 
16825c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
16835c1cfac4SBill Paul }
16845c1cfac4SBill Paul 
1685e3d2833aSAlfred Perlstein static void
16860934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16875c1cfac4SBill Paul {
16885c1cfac4SBill Paul 	struct dc_mediainfo *m;
16895c1cfac4SBill Paul 
16900934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
16915c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
16925c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
16935c1cfac4SBill Paul 
16945c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
16955c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
16965c1cfac4SBill Paul 
16975c1cfac4SBill Paul 	m->dc_gp_len = 2;
16985c1cfac4SBill Paul 	m->dc_gp_ptr = (u_int8_t *)&l->dc_sym_gpio_ctl;
16995c1cfac4SBill Paul 
17005c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17015c1cfac4SBill Paul 	sc->dc_mi = m;
17025c1cfac4SBill Paul 
17035c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
17045c1cfac4SBill Paul }
17055c1cfac4SBill Paul 
1706e3d2833aSAlfred Perlstein static void
17070934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17085c1cfac4SBill Paul {
17095c1cfac4SBill Paul 	struct dc_mediainfo *m;
17100934f18aSMaxime Henrion 	u_int8_t *p;
17115c1cfac4SBill Paul 
17120934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
17135c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17145c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17155c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17165c1cfac4SBill Paul 
17175c1cfac4SBill Paul 	p = (u_int8_t *)l;
17185c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17195c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17205c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17215c1cfac4SBill Paul 	m->dc_reset_len = *p;
17225c1cfac4SBill Paul 	p++;
17235c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17245c1cfac4SBill Paul 
17255c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17265c1cfac4SBill Paul 	sc->dc_mi = m;
17275c1cfac4SBill Paul }
17285c1cfac4SBill Paul 
17292c876e15SPoul-Henning Kamp static void
17300934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17313097aa70SWarner Losh {
17323097aa70SWarner Losh 	int size;
17333097aa70SWarner Losh 
17343097aa70SWarner Losh 	size = 2 << bits;
17353097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
17363097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
17373097aa70SWarner Losh }
17383097aa70SWarner Losh 
1739e3d2833aSAlfred Perlstein static void
17400934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17415c1cfac4SBill Paul {
17425c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17435c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
17440934f18aSMaxime Henrion 	int have_mii, i, loff;
17455c1cfac4SBill Paul 	char *ptr;
17465c1cfac4SBill Paul 
1747f956e0b3SMartin Blapp 	have_mii = 0;
17485c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17495c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17505c1cfac4SBill Paul 
17515c1cfac4SBill Paul 	ptr = (char *)lhdr;
17525c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1753f956e0b3SMartin Blapp 	/*
1754f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1755f956e0b3SMartin Blapp 	 */
1756f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1757f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1758f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1759f956e0b3SMartin Blapp 		    have_mii++;
1760f956e0b3SMartin Blapp 
1761f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1762f956e0b3SMartin Blapp 		ptr++;
1763f956e0b3SMartin Blapp 	}
1764f956e0b3SMartin Blapp 
1765f956e0b3SMartin Blapp 	/*
1766f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1767f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1768f956e0b3SMartin Blapp 	 */
1769f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1770f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
17715c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17725c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17735c1cfac4SBill Paul 		switch (hdr->dc_type) {
17745c1cfac4SBill Paul 		case DC_EBLOCK_MII:
17755c1cfac4SBill Paul 			dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17765c1cfac4SBill Paul 			break;
17775c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1778f956e0b3SMartin Blapp 			if (! have_mii)
1779f956e0b3SMartin Blapp 				dc_decode_leaf_sia(sc,
1780f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
17815c1cfac4SBill Paul 			break;
17825c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1783f956e0b3SMartin Blapp 			if (! have_mii)
1784f956e0b3SMartin Blapp 				dc_decode_leaf_sym(sc,
1785f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
17865c1cfac4SBill Paul 			break;
17875c1cfac4SBill Paul 		default:
17885c1cfac4SBill Paul 			/* Don't care. Yet. */
17895c1cfac4SBill Paul 			break;
17905c1cfac4SBill Paul 		}
17915c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
17925c1cfac4SBill Paul 		ptr++;
17935c1cfac4SBill Paul 	}
17945c1cfac4SBill Paul }
17955c1cfac4SBill Paul 
179656e5e7aeSMaxime Henrion static void
179756e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
179856e5e7aeSMaxime Henrion {
179956e5e7aeSMaxime Henrion 	u_int32_t *paddr;
180056e5e7aeSMaxime Henrion 
180156e5e7aeSMaxime Henrion 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
180256e5e7aeSMaxime Henrion 	paddr = arg;
180356e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
180456e5e7aeSMaxime Henrion }
180556e5e7aeSMaxime Henrion 
180696f2e892SBill Paul /*
180796f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
180896f2e892SBill Paul  * setup and ethernet/BPF attach.
180996f2e892SBill Paul  */
1810e3d2833aSAlfred Perlstein static int
18110934f18aSMaxime Henrion dc_attach(device_t dev)
181296f2e892SBill Paul {
1813d1ce9105SBill Paul 	int tmp = 0;
18148df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
181596f2e892SBill Paul 	u_int32_t command;
181696f2e892SBill Paul 	struct dc_softc *sc;
181796f2e892SBill Paul 	struct ifnet *ifp;
181896f2e892SBill Paul 	u_int32_t revision;
181922f6205dSJohn Baldwin 	int error = 0, rid, mac_offset;
182056e5e7aeSMaxime Henrion 	int i;
1821e7b01d07SWarner Losh 	u_int8_t *mac;
182296f2e892SBill Paul 
182396f2e892SBill Paul 	sc = device_get_softc(dev);
18246b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
182596f2e892SBill Paul 
18266008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
1827c8b27acaSJohn Baldwin 	    MTX_DEF);
1828c3e7434fSWarner Losh 
182996f2e892SBill Paul 	/*
183096f2e892SBill Paul 	 * Map control/status registers.
183196f2e892SBill Paul 	 */
183207f65363SBill Paul 	pci_enable_busmaster(dev);
183396f2e892SBill Paul 
183496f2e892SBill Paul 	rid = DC_RID;
18355f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
183696f2e892SBill Paul 
183796f2e892SBill Paul 	if (sc->dc_res == NULL) {
183822f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
183996f2e892SBill Paul 		error = ENXIO;
1840608654d4SNate Lawson 		goto fail;
184196f2e892SBill Paul 	}
184296f2e892SBill Paul 
184396f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
184496f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
184596f2e892SBill Paul 
18460934f18aSMaxime Henrion 	/* Allocate interrupt. */
184754f1f1d1SNate Lawson 	rid = 0;
18485f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
184954f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
185054f1f1d1SNate Lawson 
185154f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
185222f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
185354f1f1d1SNate Lawson 		error = ENXIO;
185454f1f1d1SNate Lawson 		goto fail;
185554f1f1d1SNate Lawson 	}
185654f1f1d1SNate Lawson 
185796f2e892SBill Paul 	/* Need this info to decide on a chip type. */
185896f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
18591e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
186096f2e892SBill Paul 
18616d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
18621e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
18631e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
18641e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
18651e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
1866eecb3844SMartin Blapp 		dc_eeprom_width(sc);
1867eecb3844SMartin Blapp 
18681e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
18691e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
187096f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
187196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1872042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
18735c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
18743097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
187596f2e892SBill Paul 		break;
18761e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
18771e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
18781e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
187996f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
1880318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
1881318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
18827dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
18834a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
18841e2e70b1SJohn Baldwin 
18850a46b1dcSBill Paul 		/* Increase the latency timer value. */
18861e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
188796f2e892SBill Paul 		break;
18881e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
188996f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
189096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
189196f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
189296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
18933097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
189496f2e892SBill Paul 		break;
18951e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
18961e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
18971e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
18981e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
18991e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_FA511):
19001e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
19011e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
19021e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
19031e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
19041e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
19051e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
19061e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
19071e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
190817762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
190917762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
191096f2e892SBill Paul 		sc->dc_type = DC_TYPE_AN985;
1911acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
191296f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
191396f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
191496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
1915129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
191696f2e892SBill Paul 		break;
19171e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
19181e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
191996f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
192096f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
192196f2e892SBill Paul 		}
1922318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
192396f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
1924318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
1925318b02fdSBill Paul 		}
1926318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
192796f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
192896f2e892SBill Paul 		break;
19291e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
19301e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
193179d11e09SBill Paul 		/*
193279d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
193379d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
193479d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
193579d11e09SBill Paul 		 * get the right number of bits out of the
193679d11e09SBill Paul 		 * CRC routine.
193779d11e09SBill Paul 		 */
193879d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
193979d11e09SBill Paul 		    revision < DC_REVISION_98725)
194079d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
194196f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
194296f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1943318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
194496f2e892SBill Paul 		break;
19451e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
1946ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
1947ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
1948ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
1949ead7cde9SBill Paul 		break;
19501e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
195196f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
195279d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
1953318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
195496f2e892SBill Paul 		break;
19551e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
195696f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
195791cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
195896f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
195996f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
196096f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
196196f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
196296f2e892SBill Paul 		break;
19631e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
196496f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
196596f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
196696f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
196796f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
196896f2e892SBill Paul 		break;
19691e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
1970feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
19712dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
19722dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
1973feb78939SJonathan Chen 		/*
1974feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
1975feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
19762dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
1977feb78939SJonathan Chen 		 */
19783097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
1979feb78939SJonathan Chen 		break;
19801e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
19811af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
19821af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
19831af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
19841af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
19853097aa70SWarner Losh 		dc_read_srom(sc, sc->dc_romwidth);
19861af8bec7SBill Paul 		break;
198796f2e892SBill Paul 	default:
19881e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
19891e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
199096f2e892SBill Paul 		break;
199196f2e892SBill Paul 	}
199296f2e892SBill Paul 
199396f2e892SBill Paul 	/* Save the cache line size. */
199488d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
199588d739dcSBill Paul 		sc->dc_cachesize = 0;
199688d739dcSBill Paul 	else
19971e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
199896f2e892SBill Paul 
199996f2e892SBill Paul 	/* Reset the adapter. */
200096f2e892SBill Paul 	dc_reset(sc);
200196f2e892SBill Paul 
200296f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2003feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
200496f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
200596f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
200696f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
200796f2e892SBill Paul 	}
200896f2e892SBill Paul 
200996f2e892SBill Paul 	/*
201096f2e892SBill Paul 	 * Try to learn something about the supported media.
201196f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
201296f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
201396f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
201496f2e892SBill Paul 	 * Intel 21143.
201596f2e892SBill Paul 	 */
20165c1cfac4SBill Paul 	if (DC_IS_INTEL(sc))
20175c1cfac4SBill Paul 		dc_parse_21143_srom(sc);
20185c1cfac4SBill Paul 	else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
201996f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
202096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
202196f2e892SBill Paul 		else
202296f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
202396f2e892SBill Paul 	} else if (!sc->dc_pmode)
202496f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
202596f2e892SBill Paul 
202696f2e892SBill Paul 	/*
202796f2e892SBill Paul 	 * Get station address from the EEPROM.
202896f2e892SBill Paul 	 */
202996f2e892SBill Paul 	switch(sc->dc_type) {
203096f2e892SBill Paul 	case DC_TYPE_98713:
203196f2e892SBill Paul 	case DC_TYPE_98713A:
203296f2e892SBill Paul 	case DC_TYPE_987x5:
203396f2e892SBill Paul 	case DC_TYPE_PNICII:
203496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
203596f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
203696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
203796f2e892SBill Paul 		break;
203896f2e892SBill Paul 	case DC_TYPE_PNIC:
203996f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
204096f2e892SBill Paul 		break;
204196f2e892SBill Paul 	case DC_TYPE_DM9102:
2042ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2043ec6a7299SMaxime Henrion #ifdef __sparc64__
2044ec6a7299SMaxime Henrion 		/*
2045ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2046802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2047ec6a7299SMaxime Henrion 		 */
2048802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
20498069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2050ec6a7299SMaxime Henrion #endif
2051ec6a7299SMaxime Henrion 		break;
205296f2e892SBill Paul 	case DC_TYPE_21143:
205396f2e892SBill Paul 	case DC_TYPE_ASIX:
205496f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
205596f2e892SBill Paul 		break;
205696f2e892SBill Paul 	case DC_TYPE_AL981:
205796f2e892SBill Paul 	case DC_TYPE_AN985:
20588df1ebe9SMarcel Moolenaar 		eaddr[0] = CSR_READ_4(sc, DC_AL_PAR0);
20598df1ebe9SMarcel Moolenaar 		eaddr[1] = CSR_READ_4(sc, DC_AL_PAR1);
206096f2e892SBill Paul 		break;
20611af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
20620934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
20630934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
20641af8bec7SBill Paul 		break;
2065feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
20660934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2067e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2068e7b01d07SWarner Losh 		if (!mac) {
2069e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2070608654d4SNate Lawson 			error = ENXIO;
2071e7b01d07SWarner Losh 			goto fail;
2072e7b01d07SWarner Losh 		}
2073e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2074feb78939SJonathan Chen 		break;
207596f2e892SBill Paul 	default:
207696f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
207796f2e892SBill Paul 		break;
207896f2e892SBill Paul 	}
207996f2e892SBill Paul 
208056e5e7aeSMaxime Henrion 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
208156e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
208256e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, sizeof(struct dc_list_data), 1,
208356e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), 0, NULL, NULL, &sc->dc_ltag);
208456e5e7aeSMaxime Henrion 	if (error) {
208522f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
208656e5e7aeSMaxime Henrion 		error = ENXIO;
208756e5e7aeSMaxime Henrion 		goto fail;
208856e5e7aeSMaxime Henrion 	}
208956e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_ltag, (void **)&sc->dc_ldata,
2090aafb3ebbSMaxime Henrion 	    BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->dc_lmap);
209156e5e7aeSMaxime Henrion 	if (error) {
209222f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate DMA safe memory\n");
209356e5e7aeSMaxime Henrion 		error = ENXIO;
209456e5e7aeSMaxime Henrion 		goto fail;
209556e5e7aeSMaxime Henrion 	}
209656e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_ltag, sc->dc_lmap, sc->dc_ldata,
209756e5e7aeSMaxime Henrion 	    sizeof(struct dc_list_data), dc_dma_map_addr, &sc->dc_laddr,
209856e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT);
209956e5e7aeSMaxime Henrion 	if (error) {
210022f6205dSJohn Baldwin 		device_printf(dev, "cannot get address of the descriptors\n");
210156e5e7aeSMaxime Henrion 		error = ENXIO;
210256e5e7aeSMaxime Henrion 		goto fail;
210356e5e7aeSMaxime Henrion 	}
210496f2e892SBill Paul 
210556e5e7aeSMaxime Henrion 	/*
210656e5e7aeSMaxime Henrion 	 * Allocate a busdma tag and DMA safe memory for the multicast
210756e5e7aeSMaxime Henrion 	 * setup frame.
210856e5e7aeSMaxime Henrion 	 */
210956e5e7aeSMaxime Henrion 	error = bus_dma_tag_create(NULL, PAGE_SIZE, 0, BUS_SPACE_MAXADDR_32BIT,
211056e5e7aeSMaxime Henrion 	    BUS_SPACE_MAXADDR, NULL, NULL, DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1,
211156e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 0, NULL, NULL, &sc->dc_stag);
211256e5e7aeSMaxime Henrion 	if (error) {
211322f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
211456e5e7aeSMaxime Henrion 		error = ENXIO;
211556e5e7aeSMaxime Henrion 		goto fail;
211656e5e7aeSMaxime Henrion 	}
211756e5e7aeSMaxime Henrion 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
211856e5e7aeSMaxime Henrion 	    BUS_DMA_NOWAIT, &sc->dc_smap);
211956e5e7aeSMaxime Henrion 	if (error) {
212022f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate DMA safe memory\n");
212156e5e7aeSMaxime Henrion 		error = ENXIO;
212256e5e7aeSMaxime Henrion 		goto fail;
212356e5e7aeSMaxime Henrion 	}
212456e5e7aeSMaxime Henrion 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
212556e5e7aeSMaxime Henrion 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
212656e5e7aeSMaxime Henrion 	if (error) {
212722f6205dSJohn Baldwin 		device_printf(dev, "cannot get address of the descriptors\n");
212896f2e892SBill Paul 		error = ENXIO;
212996f2e892SBill Paul 		goto fail;
213096f2e892SBill Paul 	}
213196f2e892SBill Paul 
213256e5e7aeSMaxime Henrion 	/* Allocate a busdma tag for mbufs. */
2133c1b677aaSScott Long 	error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
2134ab0d8702SScott Long 	    BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, DC_TX_LIST_CNT, MCLBYTES,
2135c1b677aaSScott Long 	    0, NULL, NULL, &sc->dc_mtag);
213656e5e7aeSMaxime Henrion 	if (error) {
213722f6205dSJohn Baldwin 		device_printf(dev, "failed to allocate busdma tag\n");
213856e5e7aeSMaxime Henrion 		error = ENXIO;
213956e5e7aeSMaxime Henrion 		goto fail;
214056e5e7aeSMaxime Henrion 	}
214156e5e7aeSMaxime Henrion 
214256e5e7aeSMaxime Henrion 	/* Create the TX/RX busdma maps. */
214356e5e7aeSMaxime Henrion 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
214456e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
214556e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_tx_map[i]);
214656e5e7aeSMaxime Henrion 		if (error) {
214722f6205dSJohn Baldwin 			device_printf(dev, "failed to init TX ring\n");
214856e5e7aeSMaxime Henrion 			error = ENXIO;
214956e5e7aeSMaxime Henrion 			goto fail;
215056e5e7aeSMaxime Henrion 		}
215156e5e7aeSMaxime Henrion 	}
215256e5e7aeSMaxime Henrion 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
215356e5e7aeSMaxime Henrion 		error = bus_dmamap_create(sc->dc_mtag, 0,
215456e5e7aeSMaxime Henrion 		    &sc->dc_cdata.dc_rx_map[i]);
215556e5e7aeSMaxime Henrion 		if (error) {
215622f6205dSJohn Baldwin 			device_printf(dev, "failed to init RX ring\n");
215756e5e7aeSMaxime Henrion 			error = ENXIO;
215856e5e7aeSMaxime Henrion 			goto fail;
215956e5e7aeSMaxime Henrion 		}
216056e5e7aeSMaxime Henrion 	}
216156e5e7aeSMaxime Henrion 	error = bus_dmamap_create(sc->dc_mtag, 0, &sc->dc_sparemap);
216256e5e7aeSMaxime Henrion 	if (error) {
216322f6205dSJohn Baldwin 		device_printf(dev, "failed to init RX ring\n");
216456e5e7aeSMaxime Henrion 		error = ENXIO;
216556e5e7aeSMaxime Henrion 		goto fail;
216656e5e7aeSMaxime Henrion 	}
216796f2e892SBill Paul 
2168fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2169fc74a9f9SBrooks Davis 	if (ifp == NULL) {
217022f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2171fc74a9f9SBrooks Davis 		error = ENOSPC;
2172fc74a9f9SBrooks Davis 		goto fail;
2173fc74a9f9SBrooks Davis 	}
217496f2e892SBill Paul 	ifp->if_softc = sc;
21759bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
2176feb78939SJonathan Chen 	/* XXX: bleah, MTU gets overwritten in ether_ifattach() */
217796f2e892SBill Paul 	ifp->if_mtu = ETHERMTU;
21783d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
217996f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
218096f2e892SBill Paul 	ifp->if_start = dc_start;
218196f2e892SBill Paul 	ifp->if_watchdog = dc_watchdog;
218296f2e892SBill Paul 	ifp->if_init = dc_init;
2183cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2184cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2185cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
218696f2e892SBill Paul 
218796f2e892SBill Paul 	/*
21885c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
21895c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
21905c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
21915c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
21925c1cfac4SBill Paul 	 * driver instead.
219396f2e892SBill Paul 	 */
21945c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
21955c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
21965c1cfac4SBill Paul 		tmp = sc->dc_pmode;
21975c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
21985c1cfac4SBill Paul 	}
21995c1cfac4SBill Paul 
22006d431b17SWarner Losh 	/*
22016d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
22026d431b17SWarner Losh 	 * to the MII.  This needs to be done before mii_phy_probe so that
22036d431b17SWarner Losh 	 * we can actually see them.
22046d431b17SWarner Losh 	 */
22056d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
22066d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
22076d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
22086d431b17SWarner Losh 		DELAY(10);
22096d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
22106d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
22116d431b17SWarner Losh 		DELAY(10);
22126d431b17SWarner Losh 	}
22136d431b17SWarner Losh 
221496f2e892SBill Paul 	error = mii_phy_probe(dev, &sc->dc_miibus,
221596f2e892SBill Paul 	    dc_ifmedia_upd, dc_ifmedia_sts);
221696f2e892SBill Paul 
221796f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
22185c1cfac4SBill Paul 		sc->dc_pmode = tmp;
22195c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
222096f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2221042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
222296f2e892SBill Paul 		mii_phy_probe(dev, &sc->dc_miibus,
222396f2e892SBill Paul 		    dc_ifmedia_upd, dc_ifmedia_sts);
222478999dd1SBill Paul 		/*
222578999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
222678999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
222778999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
222878999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
222978999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
223078999dd1SBill Paul 		 */
22311e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
22321e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
223378999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
223496f2e892SBill Paul 		error = 0;
223596f2e892SBill Paul 	}
223696f2e892SBill Paul 
223796f2e892SBill Paul 	if (error) {
223822f6205dSJohn Baldwin 		device_printf(dev, "MII without any PHY!\n");
223996f2e892SBill Paul 		goto fail;
224096f2e892SBill Paul 	}
224196f2e892SBill Paul 
2242028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2243028a8491SMartin Blapp 		/*
2244028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2245028a8491SMartin Blapp 		 */
2246028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2247028a8491SMartin Blapp 	}
2248028a8491SMartin Blapp 
224996f2e892SBill Paul 	/*
2250db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2251db40c1aeSDoug Ambrisko 	 */
2252db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
22539ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
225440929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2255e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2256e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2257e695984eSRuslan Ermilov #endif
2258db40c1aeSDoug Ambrisko 
2259c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
226096f2e892SBill Paul 
2261608654d4SNate Lawson 	/*
2262608654d4SNate Lawson 	 * Call MI attach routine.
2263608654d4SNate Lawson 	 */
22648df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2265608654d4SNate Lawson 
226654f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2267c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2268608654d4SNate Lawson 	    dc_intr, sc, &sc->dc_intrhand);
2269608654d4SNate Lawson 
2270608654d4SNate Lawson 	if (error) {
227122f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2272693f4477SNate Lawson 		ether_ifdetach(ifp);
227354f1f1d1SNate Lawson 		goto fail;
2274608654d4SNate Lawson 	}
2275510a809eSMike Smith 
227696f2e892SBill Paul fail:
227754f1f1d1SNate Lawson 	if (error)
227854f1f1d1SNate Lawson 		dc_detach(dev);
227996f2e892SBill Paul 	return (error);
228096f2e892SBill Paul }
228196f2e892SBill Paul 
2282693f4477SNate Lawson /*
2283693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2284693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2285693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2286693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2287693f4477SNate Lawson  * allocated.
2288693f4477SNate Lawson  */
2289e3d2833aSAlfred Perlstein static int
22900934f18aSMaxime Henrion dc_detach(device_t dev)
229196f2e892SBill Paul {
229296f2e892SBill Paul 	struct dc_softc *sc;
229396f2e892SBill Paul 	struct ifnet *ifp;
22945c1cfac4SBill Paul 	struct dc_mediainfo *m;
229556e5e7aeSMaxime Henrion 	int i;
229696f2e892SBill Paul 
229796f2e892SBill Paul 	sc = device_get_softc(dev);
229859f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2299d1ce9105SBill Paul 
2300fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
230196f2e892SBill Paul 
230240929967SGleb Smirnoff #ifdef DEVICE_POLLING
230340929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
230440929967SGleb Smirnoff 		ether_poll_deregister(ifp);
230540929967SGleb Smirnoff #endif
230640929967SGleb Smirnoff 
2307693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2308214073e5SWarner Losh 	if (device_is_attached(dev)) {
2309c8b27acaSJohn Baldwin 		DC_LOCK(sc);
231096f2e892SBill Paul 		dc_stop(sc);
2311c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2312c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
23139ef8b520SSam Leffler 		ether_ifdetach(ifp);
2314693f4477SNate Lawson 	}
2315693f4477SNate Lawson 	if (sc->dc_miibus)
231696f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
231754f1f1d1SNate Lawson 	bus_generic_detach(dev);
231896f2e892SBill Paul 
231954f1f1d1SNate Lawson 	if (sc->dc_intrhand)
232096f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
232154f1f1d1SNate Lawson 	if (sc->dc_irq)
232296f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
232354f1f1d1SNate Lawson 	if (sc->dc_res)
232496f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
232596f2e892SBill Paul 
23266a3033a8SWarner Losh 	if (ifp)
23276a3033a8SWarner Losh 		if_free(ifp);
23286a3033a8SWarner Losh 
232956e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_sbuf != NULL)
233056e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf, sc->dc_smap);
233156e5e7aeSMaxime Henrion 	if (sc->dc_ldata != NULL)
233256e5e7aeSMaxime Henrion 		bus_dmamem_free(sc->dc_ltag, sc->dc_ldata, sc->dc_lmap);
23334f867c2dSGiorgos Keramidas 	if (sc->dc_mtag) {
233456e5e7aeSMaxime Henrion 		for (i = 0; i < DC_TX_LIST_CNT; i++)
23354f867c2dSGiorgos Keramidas 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
23364f867c2dSGiorgos Keramidas 				bus_dmamap_destroy(sc->dc_mtag,
23374f867c2dSGiorgos Keramidas 				    sc->dc_cdata.dc_tx_map[i]);
233856e5e7aeSMaxime Henrion 		for (i = 0; i < DC_RX_LIST_CNT; i++)
23394f867c2dSGiorgos Keramidas 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
23404f867c2dSGiorgos Keramidas 				bus_dmamap_destroy(sc->dc_mtag,
23414f867c2dSGiorgos Keramidas 				    sc->dc_cdata.dc_rx_map[i]);
234256e5e7aeSMaxime Henrion 		bus_dmamap_destroy(sc->dc_mtag, sc->dc_sparemap);
23434f867c2dSGiorgos Keramidas 	}
234456e5e7aeSMaxime Henrion 	if (sc->dc_stag)
234556e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_stag);
234656e5e7aeSMaxime Henrion 	if (sc->dc_mtag)
234756e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_mtag);
234856e5e7aeSMaxime Henrion 	if (sc->dc_ltag)
234956e5e7aeSMaxime Henrion 		bus_dma_tag_destroy(sc->dc_ltag);
235056e5e7aeSMaxime Henrion 
235196f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
235296f2e892SBill Paul 
23535c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
23545c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
23555c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
23565c1cfac4SBill Paul 		sc->dc_mi = m;
23575c1cfac4SBill Paul 	}
23587efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
23595c1cfac4SBill Paul 
2360d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
236196f2e892SBill Paul 
236296f2e892SBill Paul 	return (0);
236396f2e892SBill Paul }
236496f2e892SBill Paul 
236596f2e892SBill Paul /*
236696f2e892SBill Paul  * Initialize the transmit descriptors.
236796f2e892SBill Paul  */
2368e3d2833aSAlfred Perlstein static int
23690934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
237096f2e892SBill Paul {
237196f2e892SBill Paul 	struct dc_chain_data *cd;
237296f2e892SBill Paul 	struct dc_list_data *ld;
237301faf54bSLuigi Rizzo 	int i, nexti;
237496f2e892SBill Paul 
237596f2e892SBill Paul 	cd = &sc->dc_cdata;
237696f2e892SBill Paul 	ld = sc->dc_ldata;
237796f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2378b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2379b3811c95SMaxime Henrion 			nexti = 0;
2380b3811c95SMaxime Henrion 		else
2381b3811c95SMaxime Henrion 			nexti = i + 1;
2382af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
238396f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
238496f2e892SBill Paul 		ld->dc_tx_list[i].dc_data = 0;
238596f2e892SBill Paul 		ld->dc_tx_list[i].dc_ctl = 0;
238696f2e892SBill Paul 	}
238796f2e892SBill Paul 
238896f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
238956e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
239056e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
239196f2e892SBill Paul 	return (0);
239296f2e892SBill Paul }
239396f2e892SBill Paul 
239496f2e892SBill Paul 
239596f2e892SBill Paul /*
239696f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
239796f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
239896f2e892SBill Paul  * points back to the first.
239996f2e892SBill Paul  */
2400e3d2833aSAlfred Perlstein static int
24010934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
240296f2e892SBill Paul {
240396f2e892SBill Paul 	struct dc_chain_data *cd;
240496f2e892SBill Paul 	struct dc_list_data *ld;
240501faf54bSLuigi Rizzo 	int i, nexti;
240696f2e892SBill Paul 
240796f2e892SBill Paul 	cd = &sc->dc_cdata;
240896f2e892SBill Paul 	ld = sc->dc_ldata;
240996f2e892SBill Paul 
241096f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
241156e5e7aeSMaxime Henrion 		if (dc_newbuf(sc, i, 1) != 0)
241296f2e892SBill Paul 			return (ENOBUFS);
2413b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2414b3811c95SMaxime Henrion 			nexti = 0;
2415b3811c95SMaxime Henrion 		else
2416b3811c95SMaxime Henrion 			nexti = i + 1;
2417af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
241896f2e892SBill Paul 	}
241996f2e892SBill Paul 
242096f2e892SBill Paul 	cd->dc_rx_prod = 0;
242156e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
242256e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
242396f2e892SBill Paul 	return (0);
242496f2e892SBill Paul }
242596f2e892SBill Paul 
242656e5e7aeSMaxime Henrion static void
242756e5e7aeSMaxime Henrion dc_dma_map_rxbuf(arg, segs, nseg, mapsize, error)
242856e5e7aeSMaxime Henrion 	void *arg;
242956e5e7aeSMaxime Henrion 	bus_dma_segment_t *segs;
243056e5e7aeSMaxime Henrion 	int nseg;
243156e5e7aeSMaxime Henrion 	bus_size_t mapsize;
243256e5e7aeSMaxime Henrion 	int error;
243356e5e7aeSMaxime Henrion {
243456e5e7aeSMaxime Henrion 	struct dc_softc *sc;
243556e5e7aeSMaxime Henrion 	struct dc_desc *c;
243656e5e7aeSMaxime Henrion 
243756e5e7aeSMaxime Henrion 	sc = arg;
243856e5e7aeSMaxime Henrion 	c = &sc->dc_ldata->dc_rx_list[sc->dc_cdata.dc_rx_cur];
243956e5e7aeSMaxime Henrion 	if (error) {
244056e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_err = error;
244156e5e7aeSMaxime Henrion 		return;
244256e5e7aeSMaxime Henrion 	}
244356e5e7aeSMaxime Henrion 
244456e5e7aeSMaxime Henrion 	KASSERT(nseg == 1, ("wrong number of segments, should be 1"));
244556e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_rx_err = 0;
2446af4358c7SMaxime Henrion 	c->dc_data = htole32(segs->ds_addr);
244756e5e7aeSMaxime Henrion }
244856e5e7aeSMaxime Henrion 
244996f2e892SBill Paul /*
245096f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
245196f2e892SBill Paul  */
2452e3d2833aSAlfred Perlstein static int
245356e5e7aeSMaxime Henrion dc_newbuf(struct dc_softc *sc, int i, int alloc)
245496f2e892SBill Paul {
245556e5e7aeSMaxime Henrion 	struct mbuf *m_new;
245656e5e7aeSMaxime Henrion 	bus_dmamap_t tmp;
245756e5e7aeSMaxime Henrion 	int error;
245896f2e892SBill Paul 
245956e5e7aeSMaxime Henrion 	if (alloc) {
246056e5e7aeSMaxime Henrion 		m_new = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
246140129585SLuigi Rizzo 		if (m_new == NULL)
246296f2e892SBill Paul 			return (ENOBUFS);
246396f2e892SBill Paul 	} else {
246456e5e7aeSMaxime Henrion 		m_new = sc->dc_cdata.dc_rx_chain[i];
246596f2e892SBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
246696f2e892SBill Paul 	}
246756e5e7aeSMaxime Henrion 	m_new->m_len = m_new->m_pkthdr.len = MCLBYTES;
246896f2e892SBill Paul 	m_adj(m_new, sizeof(u_int64_t));
246996f2e892SBill Paul 
247096f2e892SBill Paul 	/*
247196f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
247296f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
247396f2e892SBill Paul 	 * 82c169 chips.
247496f2e892SBill Paul 	 */
247596f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
24760934f18aSMaxime Henrion 		bzero(mtod(m_new, char *), m_new->m_len);
247796f2e892SBill Paul 
247856e5e7aeSMaxime Henrion 	/* No need to remap the mbuf if we're reusing it. */
247956e5e7aeSMaxime Henrion 	if (alloc) {
248056e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_cur = i;
248156e5e7aeSMaxime Henrion 		error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_sparemap,
248256e5e7aeSMaxime Henrion 		    m_new, dc_dma_map_rxbuf, sc, 0);
248356e5e7aeSMaxime Henrion 		if (error) {
248456e5e7aeSMaxime Henrion 			m_freem(m_new);
248556e5e7aeSMaxime Henrion 			return (error);
248656e5e7aeSMaxime Henrion 		}
248756e5e7aeSMaxime Henrion 		if (sc->dc_cdata.dc_rx_err != 0) {
248856e5e7aeSMaxime Henrion 			m_freem(m_new);
248956e5e7aeSMaxime Henrion 			return (sc->dc_cdata.dc_rx_err);
249056e5e7aeSMaxime Henrion 		}
249156e5e7aeSMaxime Henrion 		bus_dmamap_unload(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i]);
249256e5e7aeSMaxime Henrion 		tmp = sc->dc_cdata.dc_rx_map[i];
249356e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
249456e5e7aeSMaxime Henrion 		sc->dc_sparemap = tmp;
249596f2e892SBill Paul 		sc->dc_cdata.dc_rx_chain[i] = m_new;
249656e5e7aeSMaxime Henrion 	}
249796f2e892SBill Paul 
2498af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
2499af4358c7SMaxime Henrion 	sc->dc_ldata->dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
250056e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
250156e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
250256e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
250356e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
250496f2e892SBill Paul 	return (0);
250596f2e892SBill Paul }
250696f2e892SBill Paul 
250796f2e892SBill Paul /*
250896f2e892SBill Paul  * Grrrrr.
250996f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
251096f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
251196f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
251296f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
251396f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
251496f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
251596f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
251696f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
251796f2e892SBill Paul  *
251896f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
251996f2e892SBill Paul  * Here's what we know:
252096f2e892SBill Paul  *
252196f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
252296f2e892SBill Paul  *   descriptors uploaded.
252396f2e892SBill Paul  *
252496f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
252596f2e892SBill Paul  *   total data upload.
252696f2e892SBill Paul  *
252796f2e892SBill Paul  * - We know the size of the desired received frame because it will be
252896f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
252996f2e892SBill Paul  *
253096f2e892SBill Paul  * Here's what we do:
253196f2e892SBill Paul  *
253296f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
253396f2e892SBill Paul  *   This means that we know that the buffer contents should be all
253496f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
253596f2e892SBill Paul  *
253696f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
253796f2e892SBill Paul  *   ethernet CRC at the end.
253896f2e892SBill Paul  *
253996f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
254096f2e892SBill Paul  *
254196f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
254296f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
254396f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
254496f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
254596f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
254696f2e892SBill Paul  *   we won't be fooled.
254796f2e892SBill Paul  *
254896f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
254996f2e892SBill Paul  *   that value from the current pointer location. This brings us
255096f2e892SBill Paul  *   to the start of the actual received packet.
255196f2e892SBill Paul  *
255296f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
255396f2e892SBill Paul  *   frame length.
255496f2e892SBill Paul  *
255596f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
255696f2e892SBill Paul  * the time.
255796f2e892SBill Paul  */
255896f2e892SBill Paul 
255996f2e892SBill Paul #define DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2560e3d2833aSAlfred Perlstein static void
25610934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
256296f2e892SBill Paul {
256396f2e892SBill Paul 	struct dc_desc *cur_rx;
256496f2e892SBill Paul 	struct dc_desc *c = NULL;
256596f2e892SBill Paul 	struct mbuf *m = NULL;
256696f2e892SBill Paul 	unsigned char *ptr;
256796f2e892SBill Paul 	int i, total_len;
256896f2e892SBill Paul 	u_int32_t rxstat = 0;
256996f2e892SBill Paul 
257096f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
257196f2e892SBill Paul 	cur_rx = &sc->dc_ldata->dc_rx_list[idx];
257296f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
25731edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
257496f2e892SBill Paul 
257596f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
257696f2e892SBill Paul 	while (1) {
257796f2e892SBill Paul 		c = &sc->dc_ldata->dc_rx_list[i];
2578af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
257996f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
258096f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
258196f2e892SBill Paul 		ptr += DC_RXLEN;
258296f2e892SBill Paul 		/* If this is the last buffer, break out. */
258396f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
258496f2e892SBill Paul 			break;
258556e5e7aeSMaxime Henrion 		dc_newbuf(sc, i, 0);
258696f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
258796f2e892SBill Paul 	}
258896f2e892SBill Paul 
258996f2e892SBill Paul 	/* Find the length of the actual receive frame. */
259096f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
259196f2e892SBill Paul 
259296f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
259396f2e892SBill Paul 	while (*ptr == 0x00)
259496f2e892SBill Paul 		ptr--;
259596f2e892SBill Paul 
259696f2e892SBill Paul 	/* Round off. */
259796f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
259896f2e892SBill Paul 		ptr -= 1;
259996f2e892SBill Paul 
260096f2e892SBill Paul 	/* Now find the start of the frame. */
260196f2e892SBill Paul 	ptr -= total_len;
260296f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
260396f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
260496f2e892SBill Paul 
260596f2e892SBill Paul 	/*
260696f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
260796f2e892SBill Paul 	 * the status word to make it look like a successful
260896f2e892SBill Paul 	 * frame reception.
260996f2e892SBill Paul 	 */
261056e5e7aeSMaxime Henrion 	dc_newbuf(sc, i, 0);
261196f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2612af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
261396f2e892SBill Paul }
261496f2e892SBill Paul 
261596f2e892SBill Paul /*
261673bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
261773bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
261873bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
261973bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
262073bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
262173bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
262273bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
262373bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
262473bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
262573bf949cSBill Paul  */
2626e3d2833aSAlfred Perlstein static int
26270934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
262873bf949cSBill Paul {
262973bf949cSBill Paul 	struct dc_desc *cur_rx;
26300934f18aSMaxime Henrion 	int i, pos;
263173bf949cSBill Paul 
263273bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
263373bf949cSBill Paul 
263473bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
263573bf949cSBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[pos];
2636af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
263773bf949cSBill Paul 			break;
263873bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
263973bf949cSBill Paul 	}
264073bf949cSBill Paul 
264173bf949cSBill Paul 	/* If the ring really is empty, then just return. */
264273bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
264373bf949cSBill Paul 		return (0);
264473bf949cSBill Paul 
264573bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
264673bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
264773bf949cSBill Paul 
264873bf949cSBill Paul 	return (EAGAIN);
264973bf949cSBill Paul }
265073bf949cSBill Paul 
265173bf949cSBill Paul /*
265296f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
265396f2e892SBill Paul  * the higher level protocols.
265496f2e892SBill Paul  */
2655e3d2833aSAlfred Perlstein static void
26560934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
265796f2e892SBill Paul {
2658432120f2SMarius Strobl 	struct mbuf *m, *m0;
265996f2e892SBill Paul 	struct ifnet *ifp;
266096f2e892SBill Paul 	struct dc_desc *cur_rx;
266196f2e892SBill Paul 	int i, total_len = 0;
266296f2e892SBill Paul 	u_int32_t rxstat;
266396f2e892SBill Paul 
26645120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
26655120abbfSSam Leffler 
2666fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
266796f2e892SBill Paul 	i = sc->dc_cdata.dc_rx_prod;
266896f2e892SBill Paul 
266956e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
2670af4358c7SMaxime Henrion 	while (!(le32toh(sc->dc_ldata->dc_rx_list[i].dc_status) &
2671af4358c7SMaxime Henrion 	    DC_RXSTAT_OWN)) {
2672e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
267340929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2674e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2675e4fc250cSLuigi Rizzo 				break;
2676e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2677e4fc250cSLuigi Rizzo 		}
26780934f18aSMaxime Henrion #endif
267996f2e892SBill Paul 		cur_rx = &sc->dc_ldata->dc_rx_list[i];
2680af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
268196f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
268256e5e7aeSMaxime Henrion 		bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_rx_map[i],
268356e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
268496f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
268596f2e892SBill Paul 
268696f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
268796f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
268896f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
268996f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
269096f2e892SBill Paul 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0) {
269196f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
269296f2e892SBill Paul 					continue;
269396f2e892SBill Paul 				}
269496f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2695af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
269696f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
269796f2e892SBill Paul 			}
269896f2e892SBill Paul 		}
269996f2e892SBill Paul 
270096f2e892SBill Paul 		/*
270196f2e892SBill Paul 		 * If an error occurs, update stats, clear the
270296f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
270396f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2704db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
27050934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
270696f2e892SBill Paul 		 */
2707db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2708db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2709db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2710db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2711db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
271296f2e892SBill Paul 				ifp->if_ierrors++;
271396f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
271496f2e892SBill Paul 					ifp->if_collisions++;
271556e5e7aeSMaxime Henrion 				dc_newbuf(sc, i, 0);
271696f2e892SBill Paul 				if (rxstat & DC_RXSTAT_CRCERR) {
271796f2e892SBill Paul 					DC_INC(i, DC_RX_LIST_CNT);
271896f2e892SBill Paul 					continue;
271996f2e892SBill Paul 				} else {
2720c8b27acaSJohn Baldwin 					dc_init_locked(sc);
272196f2e892SBill Paul 					return;
272296f2e892SBill Paul 				}
272396f2e892SBill Paul 			}
2724db40c1aeSDoug Ambrisko 		}
272596f2e892SBill Paul 
272696f2e892SBill Paul 		/* No errors; receive the packet. */
272796f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2728432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
272901faf54bSLuigi Rizzo 		/*
2730432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
273101faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
273201faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
273301faf54bSLuigi Rizzo 		 * copy done in m_devget().
273401faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
273501faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
273601faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
273701faf54bSLuigi Rizzo 		 */
2738432120f2SMarius Strobl 		if (dc_newbuf(sc, i, 1) == 0) {
273901faf54bSLuigi Rizzo 			m->m_pkthdr.rcvif = ifp;
274001faf54bSLuigi Rizzo 			m->m_pkthdr.len = m->m_len = total_len;
274101faf54bSLuigi Rizzo 			DC_INC(i, DC_RX_LIST_CNT);
274201faf54bSLuigi Rizzo 		} else
274301faf54bSLuigi Rizzo #endif
274401faf54bSLuigi Rizzo 		{
274501faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
274601faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
274756e5e7aeSMaxime Henrion 			dc_newbuf(sc, i, 0);
274896f2e892SBill Paul 			DC_INC(i, DC_RX_LIST_CNT);
274996f2e892SBill Paul 			if (m0 == NULL) {
275096f2e892SBill Paul 				ifp->if_ierrors++;
275196f2e892SBill Paul 				continue;
275296f2e892SBill Paul 			}
275396f2e892SBill Paul 			m = m0;
275401faf54bSLuigi Rizzo 		}
275596f2e892SBill Paul 
275696f2e892SBill Paul 		ifp->if_ipackets++;
27575120abbfSSam Leffler 		DC_UNLOCK(sc);
27589ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
27595120abbfSSam Leffler 		DC_LOCK(sc);
276096f2e892SBill Paul 	}
276196f2e892SBill Paul 
276296f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
276396f2e892SBill Paul }
276496f2e892SBill Paul 
276596f2e892SBill Paul /*
276696f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
276796f2e892SBill Paul  * the list buffers.
276896f2e892SBill Paul  */
276996f2e892SBill Paul 
2770e3d2833aSAlfred Perlstein static void
27710934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
277296f2e892SBill Paul {
277396f2e892SBill Paul 	struct dc_desc *cur_tx = NULL;
277496f2e892SBill Paul 	struct ifnet *ifp;
277596f2e892SBill Paul 	int idx;
2776af4358c7SMaxime Henrion 	u_int32_t ctl, txstat;
277796f2e892SBill Paul 
2778fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
277996f2e892SBill Paul 
278096f2e892SBill Paul 	/*
278196f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
278296f2e892SBill Paul 	 * frames that have been transmitted.
278396f2e892SBill Paul 	 */
278456e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap, BUS_DMASYNC_POSTREAD);
278596f2e892SBill Paul 	idx = sc->dc_cdata.dc_tx_cons;
278696f2e892SBill Paul 	while (idx != sc->dc_cdata.dc_tx_prod) {
278796f2e892SBill Paul 
278896f2e892SBill Paul 		cur_tx = &sc->dc_ldata->dc_tx_list[idx];
2789af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2790af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
279196f2e892SBill Paul 
279296f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
279396f2e892SBill Paul 			break;
279496f2e892SBill Paul 
27954ff4a9beSDon Lewis 		if (!(ctl & DC_TXCTL_LASTFRAG) || ctl & DC_TXCTL_SETUP) {
2796af4358c7SMaxime Henrion 			if (ctl & DC_TXCTL_SETUP) {
279796f2e892SBill Paul 				/*
279896f2e892SBill Paul 				 * Yes, the PNIC is so brain damaged
279996f2e892SBill Paul 				 * that it will sometimes generate a TX
280096f2e892SBill Paul 				 * underrun error while DMAing the RX
280196f2e892SBill Paul 				 * filter setup frame. If we detect this,
280296f2e892SBill Paul 				 * we have to send the setup frame again,
280396f2e892SBill Paul 				 * or else the filter won't be programmed
280496f2e892SBill Paul 				 * correctly.
280596f2e892SBill Paul 				 */
280696f2e892SBill Paul 				if (DC_IS_PNIC(sc)) {
280796f2e892SBill Paul 					if (txstat & DC_TXSTAT_ERRSUM)
280896f2e892SBill Paul 						dc_setfilt(sc);
280996f2e892SBill Paul 				}
281096f2e892SBill Paul 				sc->dc_cdata.dc_tx_chain[idx] = NULL;
281196f2e892SBill Paul 			}
2812bcb9ef4fSLuigi Rizzo 			sc->dc_cdata.dc_tx_cnt--;
281396f2e892SBill Paul 			DC_INC(idx, DC_TX_LIST_CNT);
281496f2e892SBill Paul 			continue;
281596f2e892SBill Paul 		}
281696f2e892SBill Paul 
281729a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
2818feb78939SJonathan Chen 			/*
2819feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
2820feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
282129a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
282229a2220aSBill Paul 			 * Who knows, but Conexant chips have the
282329a2220aSBill Paul 			 * same problem. Maybe they took lessons
282429a2220aSBill Paul 			 * from Xircom.
282529a2220aSBill Paul 			 */
2826feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
2827feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
2828feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
2829feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
2830feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
2831feb78939SJonathan Chen 		} else {
283296f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
283396f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
283496f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
283596f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
283696f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
2837feb78939SJonathan Chen 		}
283896f2e892SBill Paul 
283996f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
284096f2e892SBill Paul 			ifp->if_oerrors++;
284196f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
284296f2e892SBill Paul 				ifp->if_collisions++;
284396f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
284496f2e892SBill Paul 				ifp->if_collisions++;
284596f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
2846c8b27acaSJohn Baldwin 				dc_init_locked(sc);
284796f2e892SBill Paul 				return;
284896f2e892SBill Paul 			}
284996f2e892SBill Paul 		}
285096f2e892SBill Paul 
285196f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
285296f2e892SBill Paul 
285396f2e892SBill Paul 		ifp->if_opackets++;
285496f2e892SBill Paul 		if (sc->dc_cdata.dc_tx_chain[idx] != NULL) {
285556e5e7aeSMaxime Henrion 			bus_dmamap_sync(sc->dc_mtag,
285656e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx],
285756e5e7aeSMaxime Henrion 			    BUS_DMASYNC_POSTWRITE);
285856e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
285956e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[idx]);
286096f2e892SBill Paul 			m_freem(sc->dc_cdata.dc_tx_chain[idx]);
286196f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
286296f2e892SBill Paul 		}
286396f2e892SBill Paul 
286496f2e892SBill Paul 		sc->dc_cdata.dc_tx_cnt--;
286596f2e892SBill Paul 		DC_INC(idx, DC_TX_LIST_CNT);
286696f2e892SBill Paul 	}
286796f2e892SBill Paul 
2868bcb9ef4fSLuigi Rizzo 	if (idx != sc->dc_cdata.dc_tx_cons) {
28690934f18aSMaxime Henrion 	    	/* Some buffers have been freed. */
287096f2e892SBill Paul 		sc->dc_cdata.dc_tx_cons = idx;
287113f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2872bcb9ef4fSLuigi Rizzo 	}
2873bcb9ef4fSLuigi Rizzo 	ifp->if_timer = (sc->dc_cdata.dc_tx_cnt == 0) ? 0 : 5;
287496f2e892SBill Paul }
287596f2e892SBill Paul 
2876e3d2833aSAlfred Perlstein static void
28770934f18aSMaxime Henrion dc_tick(void *xsc)
287896f2e892SBill Paul {
287996f2e892SBill Paul 	struct dc_softc *sc;
288096f2e892SBill Paul 	struct mii_data *mii;
288196f2e892SBill Paul 	struct ifnet *ifp;
288296f2e892SBill Paul 	u_int32_t r;
288396f2e892SBill Paul 
288496f2e892SBill Paul 	sc = xsc;
2885c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
2886fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
288796f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
288896f2e892SBill Paul 
288996f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
2890318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
2891318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
2892318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2893318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
289496f2e892SBill Paul 				sc->dc_link = 0;
2895318b02fdSBill Paul 				mii_mediachg(mii);
2896318b02fdSBill Paul 			}
2897318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
2898318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
2899318b02fdSBill Paul 				sc->dc_link = 0;
2900318b02fdSBill Paul 				mii_mediachg(mii);
2901318b02fdSBill Paul 			}
2902d675147eSBill Paul 			if (sc->dc_link == 0)
290396f2e892SBill Paul 				mii_tick(mii);
290496f2e892SBill Paul 		} else {
2905318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_ISR);
290696f2e892SBill Paul 			if ((r & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT &&
2907259b8d84SMartin Blapp 			    sc->dc_cdata.dc_tx_cnt == 0) {
290896f2e892SBill Paul 				mii_tick(mii);
2909042c8f6eSBill Paul 				if (!(mii->mii_media_status & IFM_ACTIVE))
2910042c8f6eSBill Paul 					sc->dc_link = 0;
291196f2e892SBill Paul 			}
2912259b8d84SMartin Blapp 		}
291396f2e892SBill Paul 	} else
291496f2e892SBill Paul 		mii_tick(mii);
291596f2e892SBill Paul 
291696f2e892SBill Paul 	/*
291796f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
291896f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
291996f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
292096f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
292196f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
292296f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
292396f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
292496f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
292596f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
292696f2e892SBill Paul 	 * a screeching halt for several seconds.
292796f2e892SBill Paul 	 *
292896f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
292996f2e892SBill Paul 	 * any packets until a link has been established. After the
293096f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
293196f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
293296f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
293396f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
293496f2e892SBill Paul 	 */
2935cd62a9cbSJonathan Lemon 	if (!sc->dc_link && mii->mii_media_status & IFM_ACTIVE &&
293696f2e892SBill Paul 	    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
293796f2e892SBill Paul 		sc->dc_link++;
2938cbaf877fSBrian Feldman 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2939c8b27acaSJohn Baldwin 			dc_start_locked(ifp);
294096f2e892SBill Paul 	}
294196f2e892SBill Paul 
2942318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
2943b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
2944318b02fdSBill Paul 	else
2945b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
294696f2e892SBill Paul }
294796f2e892SBill Paul 
2948d467c136SBill Paul /*
2949d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
2950d467c136SBill Paul  * or switch to store and forward mode if we have to.
2951d467c136SBill Paul  */
2952e3d2833aSAlfred Perlstein static void
29530934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
2954d467c136SBill Paul {
2955d467c136SBill Paul 	u_int32_t isr;
2956d467c136SBill Paul 	int i;
2957d467c136SBill Paul 
2958d467c136SBill Paul 	if (DC_IS_DAVICOM(sc))
2959c8b27acaSJohn Baldwin 		dc_init_locked(sc);
2960d467c136SBill Paul 
2961d467c136SBill Paul 	if (DC_IS_INTEL(sc)) {
2962d467c136SBill Paul 		/*
2963d467c136SBill Paul 		 * The real 21143 requires that the transmitter be idle
2964d467c136SBill Paul 		 * in order to change the transmit threshold or store
2965d467c136SBill Paul 		 * and forward state.
2966d467c136SBill Paul 		 */
2967d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2968d467c136SBill Paul 
2969d467c136SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
2970d467c136SBill Paul 			isr = CSR_READ_4(sc, DC_ISR);
2971d467c136SBill Paul 			if (isr & DC_ISR_TX_IDLE)
2972d467c136SBill Paul 				break;
2973d467c136SBill Paul 			DELAY(10);
2974d467c136SBill Paul 		}
2975d467c136SBill Paul 		if (i == DC_TIMEOUT) {
29766b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
2977432120f2SMarius Strobl 			    "%s: failed to force tx to idle state\n",
2978432120f2SMarius Strobl 			    __func__);
2979c8b27acaSJohn Baldwin 			dc_init_locked(sc);
2980d467c136SBill Paul 		}
2981d467c136SBill Paul 	}
2982d467c136SBill Paul 
29836b9f5c94SGleb Smirnoff 	device_printf(sc->dc_dev, "TX underrun -- ");
2984d467c136SBill Paul 	sc->dc_txthresh += DC_TXTHRESH_INC;
2985d467c136SBill Paul 	if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
2986d467c136SBill Paul 		printf("using store and forward mode\n");
2987d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
2988d467c136SBill Paul 	} else {
2989d467c136SBill Paul 		printf("increasing TX threshold\n");
2990d467c136SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
2991d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
2992d467c136SBill Paul 	}
2993d467c136SBill Paul 
2994d467c136SBill Paul 	if (DC_IS_INTEL(sc))
2995d467c136SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
2996d467c136SBill Paul }
2997d467c136SBill Paul 
2998e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
2999e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3000e4fc250cSLuigi Rizzo 
3001e4fc250cSLuigi Rizzo static void
3002e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3003e4fc250cSLuigi Rizzo {
3004e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
3005e4fc250cSLuigi Rizzo 
300640929967SGleb Smirnoff 	DC_LOCK(sc);
300740929967SGleb Smirnoff 
300840929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
300940929967SGleb Smirnoff 		DC_UNLOCK(sc);
3010e4fc250cSLuigi Rizzo 		return;
3011e4fc250cSLuigi Rizzo 	}
301240929967SGleb Smirnoff 
3013e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
3014e4fc250cSLuigi Rizzo 	dc_rxeof(sc);
3015e4fc250cSLuigi Rizzo 	dc_txeof(sc);
301613f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
301713f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3018c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3019e4fc250cSLuigi Rizzo 
3020e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3021e4fc250cSLuigi Rizzo 		u_int32_t	status;
3022e4fc250cSLuigi Rizzo 
3023e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3024e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3025e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3026e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
30275120abbfSSam Leffler 		if (!status) {
30285120abbfSSam Leffler 			DC_UNLOCK(sc);
3029e4fc250cSLuigi Rizzo 			return;
30305120abbfSSam Leffler 		}
3031e4fc250cSLuigi Rizzo 		/* ack what we have */
3032e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3033e4fc250cSLuigi Rizzo 
3034e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3035e4fc250cSLuigi Rizzo 			u_int32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3036e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3037e4fc250cSLuigi Rizzo 
3038e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3039e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3040e4fc250cSLuigi Rizzo 		}
3041e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3042e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3043e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3044e4fc250cSLuigi Rizzo 
3045e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3046e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3047e4fc250cSLuigi Rizzo 
3048e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
30496b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
3050e4fc250cSLuigi Rizzo 			dc_reset(sc);
3051c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3052e4fc250cSLuigi Rizzo 		}
3053e4fc250cSLuigi Rizzo 	}
30545120abbfSSam Leffler 	DC_UNLOCK(sc);
3055e4fc250cSLuigi Rizzo }
3056e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3057e4fc250cSLuigi Rizzo 
3058e3d2833aSAlfred Perlstein static void
30590934f18aSMaxime Henrion dc_intr(void *arg)
306096f2e892SBill Paul {
306196f2e892SBill Paul 	struct dc_softc *sc;
306296f2e892SBill Paul 	struct ifnet *ifp;
306396f2e892SBill Paul 	u_int32_t status;
306496f2e892SBill Paul 
306596f2e892SBill Paul 	sc = arg;
3066d2a1864bSWarner Losh 
30670934f18aSMaxime Henrion 	if (sc->suspended)
3068e8388e14SMitsuru IWASAKI 		return;
3069e8388e14SMitsuru IWASAKI 
3070d2a1864bSWarner Losh 	if ((CSR_READ_4(sc, DC_ISR) & DC_INTRS) == 0)
3071d2a1864bSWarner Losh 		return;
3072d2a1864bSWarner Losh 
3073d1ce9105SBill Paul 	DC_LOCK(sc);
3074fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3075e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
307640929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
307740929967SGleb Smirnoff 		DC_UNLOCK(sc);
307840929967SGleb Smirnoff 		return;
3079e4fc250cSLuigi Rizzo 	}
30800934f18aSMaxime Henrion #endif
308196f2e892SBill Paul 
3082d88a358cSLuigi Rizzo 	/* Suppress unwanted interrupts */
308396f2e892SBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
308496f2e892SBill Paul 		if (CSR_READ_4(sc, DC_ISR) & DC_INTRS)
308596f2e892SBill Paul 			dc_stop(sc);
3086d1ce9105SBill Paul 		DC_UNLOCK(sc);
308796f2e892SBill Paul 		return;
308896f2e892SBill Paul 	}
308996f2e892SBill Paul 
309096f2e892SBill Paul 	/* Disable interrupts. */
309196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
309296f2e892SBill Paul 
30937ed2454cSGleb Smirnoff 	while (((status = CSR_READ_4(sc, DC_ISR)) & DC_INTRS) &&
30947ed2454cSGleb Smirnoff 	    status != 0xFFFFFFFF &&
30955108cc56SGleb Smirnoff 	    (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
309696f2e892SBill Paul 
309796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
309896f2e892SBill Paul 
309973bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
310073bf949cSBill Paul 			int		curpkts;
310173bf949cSBill Paul 			curpkts = ifp->if_ipackets;
310296f2e892SBill Paul 			dc_rxeof(sc);
310373bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
310473bf949cSBill Paul 				while (dc_rx_resync(sc))
310573bf949cSBill Paul 					dc_rxeof(sc);
310673bf949cSBill Paul 			}
310773bf949cSBill Paul 		}
310896f2e892SBill Paul 
310996f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
311096f2e892SBill Paul 			dc_txeof(sc);
311196f2e892SBill Paul 
311296f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
311396f2e892SBill Paul 			dc_txeof(sc);
311496f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
311596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
311696f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
311796f2e892SBill Paul 			}
311896f2e892SBill Paul 		}
311996f2e892SBill Paul 
3120d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3121d467c136SBill Paul 			dc_tx_underrun(sc);
312296f2e892SBill Paul 
312396f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
312473bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
312573bf949cSBill Paul 			int		curpkts;
312673bf949cSBill Paul 			curpkts = ifp->if_ipackets;
312796f2e892SBill Paul 			dc_rxeof(sc);
312873bf949cSBill Paul 			if (curpkts == ifp->if_ipackets) {
312973bf949cSBill Paul 				while (dc_rx_resync(sc))
313073bf949cSBill Paul 					dc_rxeof(sc);
313173bf949cSBill Paul 			}
313273bf949cSBill Paul 		}
313396f2e892SBill Paul 
313496f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
313596f2e892SBill Paul 			dc_reset(sc);
3136c8b27acaSJohn Baldwin 			dc_init_locked(sc);
313796f2e892SBill Paul 		}
313896f2e892SBill Paul 	}
313996f2e892SBill Paul 
314096f2e892SBill Paul 	/* Re-enable interrupts. */
314196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
314296f2e892SBill Paul 
3143cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3144c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
314596f2e892SBill Paul 
3146d1ce9105SBill Paul 	DC_UNLOCK(sc);
314796f2e892SBill Paul }
314896f2e892SBill Paul 
314956e5e7aeSMaxime Henrion static void
315056e5e7aeSMaxime Henrion dc_dma_map_txbuf(arg, segs, nseg, mapsize, error)
315156e5e7aeSMaxime Henrion 	void *arg;
315256e5e7aeSMaxime Henrion 	bus_dma_segment_t *segs;
315356e5e7aeSMaxime Henrion 	int nseg;
315456e5e7aeSMaxime Henrion 	bus_size_t mapsize;
315556e5e7aeSMaxime Henrion 	int error;
315656e5e7aeSMaxime Henrion {
315756e5e7aeSMaxime Henrion 	struct dc_softc *sc;
315856e5e7aeSMaxime Henrion 	struct dc_desc *f;
315956e5e7aeSMaxime Henrion 	int cur, first, frag, i;
316056e5e7aeSMaxime Henrion 
316156e5e7aeSMaxime Henrion 	sc = arg;
316256e5e7aeSMaxime Henrion 	if (error) {
316356e5e7aeSMaxime Henrion 		sc->dc_cdata.dc_tx_err = error;
316456e5e7aeSMaxime Henrion 		return;
316556e5e7aeSMaxime Henrion 	}
316656e5e7aeSMaxime Henrion 
316756e5e7aeSMaxime Henrion 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
316856e5e7aeSMaxime Henrion 	for (i = 0; i < nseg; i++) {
316956e5e7aeSMaxime Henrion 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
317056e5e7aeSMaxime Henrion 		    (frag == (DC_TX_LIST_CNT - 1)) &&
317156e5e7aeSMaxime Henrion 		    (first != sc->dc_cdata.dc_tx_first)) {
317256e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag,
317356e5e7aeSMaxime Henrion 			    sc->dc_cdata.dc_tx_map[first]);
317456e5e7aeSMaxime Henrion 			sc->dc_cdata.dc_tx_err = ENOBUFS;
317556e5e7aeSMaxime Henrion 			return;
317656e5e7aeSMaxime Henrion 		}
317756e5e7aeSMaxime Henrion 
317856e5e7aeSMaxime Henrion 		f = &sc->dc_ldata->dc_tx_list[frag];
3179af4358c7SMaxime Henrion 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
318056e5e7aeSMaxime Henrion 		if (i == 0) {
318156e5e7aeSMaxime Henrion 			f->dc_status = 0;
3182af4358c7SMaxime Henrion 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
318356e5e7aeSMaxime Henrion 		} else
3184af4358c7SMaxime Henrion 			f->dc_status = htole32(DC_TXSTAT_OWN);
3185af4358c7SMaxime Henrion 		f->dc_data = htole32(segs[i].ds_addr);
318656e5e7aeSMaxime Henrion 		cur = frag;
318756e5e7aeSMaxime Henrion 		DC_INC(frag, DC_TX_LIST_CNT);
318856e5e7aeSMaxime Henrion 	}
318956e5e7aeSMaxime Henrion 
319056e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_err = 0;
319156e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_prod = frag;
319256e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_cnt += nseg;
3193af4358c7SMaxime Henrion 	sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
31944ff4a9beSDon Lewis 	sc->dc_cdata.dc_tx_chain[cur] = sc->dc_cdata.dc_tx_mapping;
319556e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
3196af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[first].dc_ctl |=
3197af4358c7SMaxime Henrion 		    htole32(DC_TXCTL_FINT);
319856e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
3199af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
320056e5e7aeSMaxime Henrion 	if (sc->dc_flags & DC_TX_USE_TX_INTR && sc->dc_cdata.dc_tx_cnt > 64)
3201af4358c7SMaxime Henrion 		sc->dc_ldata->dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
3202af4358c7SMaxime Henrion 	sc->dc_ldata->dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
320356e5e7aeSMaxime Henrion }
320456e5e7aeSMaxime Henrion 
320596f2e892SBill Paul /*
320696f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
320796f2e892SBill Paul  * pointers to the fragment pointers.
320896f2e892SBill Paul  */
3209e3d2833aSAlfred Perlstein static int
3210a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
321196f2e892SBill Paul {
321296f2e892SBill Paul 	struct mbuf *m;
321356e5e7aeSMaxime Henrion 	int error, idx, chainlen = 0;
3214cda97c50SMike Silbersack 
3215cda97c50SMike Silbersack 	/*
3216cda97c50SMike Silbersack 	 * If there's no way we can send any packets, return now.
3217cda97c50SMike Silbersack 	 */
3218cda97c50SMike Silbersack 	if (DC_TX_LIST_CNT - sc->dc_cdata.dc_tx_cnt < 6)
3219cda97c50SMike Silbersack 		return (ENOBUFS);
3220cda97c50SMike Silbersack 
3221cda97c50SMike Silbersack 	/*
3222cda97c50SMike Silbersack 	 * Count the number of frags in this chain to see if
3223cda97c50SMike Silbersack 	 * we need to m_defrag.  Since the descriptor list is shared
3224cda97c50SMike Silbersack 	 * by all packets, we'll m_defrag long chains so that they
3225cda97c50SMike Silbersack 	 * do not use up the entire list, even if they would fit.
3226cda97c50SMike Silbersack 	 */
3227a10c0e45SMike Silbersack 	for (m = *m_head; m != NULL; m = m->m_next)
3228cda97c50SMike Silbersack 		chainlen++;
3229cda97c50SMike Silbersack 
3230cda97c50SMike Silbersack 	if ((chainlen > DC_TX_LIST_CNT / 4) ||
3231cda97c50SMike Silbersack 	    ((DC_TX_LIST_CNT - (chainlen + sc->dc_cdata.dc_tx_cnt)) < 6)) {
3232a10c0e45SMike Silbersack 		m = m_defrag(*m_head, M_DONTWAIT);
3233cda97c50SMike Silbersack 		if (m == NULL)
3234cda97c50SMike Silbersack 			return (ENOBUFS);
3235a10c0e45SMike Silbersack 		*m_head = m;
3236cda97c50SMike Silbersack 	}
323796f2e892SBill Paul 
323896f2e892SBill Paul 	/*
323996f2e892SBill Paul 	 * Start packing the mbufs in this chain into
324096f2e892SBill Paul 	 * the fragment pointers. Stop when we run out
324196f2e892SBill Paul 	 * of fragments or hit the end of the mbuf chain.
324296f2e892SBill Paul 	 */
324356e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
32444ff4a9beSDon Lewis 	sc->dc_cdata.dc_tx_mapping = *m_head;
324556e5e7aeSMaxime Henrion 	error = bus_dmamap_load_mbuf(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
3246a10c0e45SMike Silbersack 	    *m_head, dc_dma_map_txbuf, sc, 0);
324756e5e7aeSMaxime Henrion 	if (error)
324856e5e7aeSMaxime Henrion 		return (error);
324956e5e7aeSMaxime Henrion 	if (sc->dc_cdata.dc_tx_err != 0)
325056e5e7aeSMaxime Henrion 		return (sc->dc_cdata.dc_tx_err);
325156e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_mtag, sc->dc_cdata.dc_tx_map[idx],
325256e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE);
325356e5e7aeSMaxime Henrion 	bus_dmamap_sync(sc->dc_ltag, sc->dc_lmap,
325456e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
325596f2e892SBill Paul 	return (0);
325696f2e892SBill Paul }
325796f2e892SBill Paul 
325896f2e892SBill Paul /*
325996f2e892SBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
326096f2e892SBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
326196f2e892SBill Paul  * copy of the pointers since the transmit list fragment pointers are
326296f2e892SBill Paul  * physical addresses.
326396f2e892SBill Paul  */
326496f2e892SBill Paul 
3265e3d2833aSAlfred Perlstein static void
32660934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
326796f2e892SBill Paul {
326896f2e892SBill Paul 	struct dc_softc *sc;
3269c8b27acaSJohn Baldwin 
3270c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3271c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3272c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3273c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3274c8b27acaSJohn Baldwin }
3275c8b27acaSJohn Baldwin 
3276c8b27acaSJohn Baldwin static void
3277c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3278c8b27acaSJohn Baldwin {
3279c8b27acaSJohn Baldwin 	struct dc_softc *sc;
3280cda97c50SMike Silbersack 	struct mbuf *m_head = NULL, *m;
3281cbaf877fSBrian Feldman 	unsigned int queued = 0;
328296f2e892SBill Paul 	int idx;
328396f2e892SBill Paul 
328496f2e892SBill Paul 	sc = ifp->if_softc;
328596f2e892SBill Paul 
3286c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
328796f2e892SBill Paul 
3288c8b27acaSJohn Baldwin 	if (!sc->dc_link && ifp->if_snd.ifq_len < 10)
328996f2e892SBill Paul 		return;
3290d1ce9105SBill Paul 
3291c8b27acaSJohn Baldwin 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
3292d1ce9105SBill Paul 		return;
329396f2e892SBill Paul 
329456e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
329596f2e892SBill Paul 
329696f2e892SBill Paul 	while (sc->dc_cdata.dc_tx_chain[idx] == NULL) {
3297cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
329896f2e892SBill Paul 		if (m_head == NULL)
329996f2e892SBill Paul 			break;
330096f2e892SBill Paul 
33012dfc960aSLuigi Rizzo 		if (sc->dc_flags & DC_TX_COALESCE &&
33022dfc960aSLuigi Rizzo 		    (m_head->m_next != NULL ||
33032dfc960aSLuigi Rizzo 		     sc->dc_flags & DC_TX_ALIGN)) {
3304cda97c50SMike Silbersack 			m = m_defrag(m_head, M_DONTWAIT);
3305cda97c50SMike Silbersack 			if (m == NULL) {
3306cbaf877fSBrian Feldman 				IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
330713f4c340SRobert Watson 				ifp->if_drv_flags |= IFF_DRV_OACTIVE;
3308fda39fd0SBill Paul 				break;
3309cda97c50SMike Silbersack 			} else {
3310cda97c50SMike Silbersack 				m_head = m;
3311fda39fd0SBill Paul 			}
3312fda39fd0SBill Paul 		}
3313fda39fd0SBill Paul 
3314a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
3315cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
331613f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
331796f2e892SBill Paul 			break;
331896f2e892SBill Paul 		}
331956e5e7aeSMaxime Henrion 		idx = sc->dc_cdata.dc_tx_prod;
332096f2e892SBill Paul 
3321cbaf877fSBrian Feldman 		queued++;
332296f2e892SBill Paul 		/*
332396f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
332496f2e892SBill Paul 		 * to him.
332596f2e892SBill Paul 		 */
33269ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
33275c1cfac4SBill Paul 
33285c1cfac4SBill Paul 		if (sc->dc_flags & DC_TX_ONE) {
332913f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
33305c1cfac4SBill Paul 			break;
33315c1cfac4SBill Paul 		}
333296f2e892SBill Paul 	}
333396f2e892SBill Paul 
3334cbaf877fSBrian Feldman 	if (queued > 0) {
333596f2e892SBill Paul 		/* Transmit */
333696f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
333796f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
333896f2e892SBill Paul 
333996f2e892SBill Paul 		/*
334096f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
334196f2e892SBill Paul 		 */
334296f2e892SBill Paul 		ifp->if_timer = 5;
3343cbaf877fSBrian Feldman 	}
334496f2e892SBill Paul }
334596f2e892SBill Paul 
3346e3d2833aSAlfred Perlstein static void
33470934f18aSMaxime Henrion dc_init(void *xsc)
334896f2e892SBill Paul {
334996f2e892SBill Paul 	struct dc_softc *sc = xsc;
3350c8b27acaSJohn Baldwin 
3351c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3352c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3353c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3354c8b27acaSJohn Baldwin }
3355c8b27acaSJohn Baldwin 
3356c8b27acaSJohn Baldwin static void
3357c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3358c8b27acaSJohn Baldwin {
3359fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
336096f2e892SBill Paul 	struct mii_data *mii;
336196f2e892SBill Paul 
3362c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
336396f2e892SBill Paul 
336496f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
336596f2e892SBill Paul 
336696f2e892SBill Paul 	/*
336796f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
336896f2e892SBill Paul 	 */
336996f2e892SBill Paul 	dc_stop(sc);
337096f2e892SBill Paul 	dc_reset(sc);
337196f2e892SBill Paul 
337296f2e892SBill Paul 	/*
337396f2e892SBill Paul 	 * Set cache alignment and burst length.
337496f2e892SBill Paul 	 */
337588d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
337696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
337796f2e892SBill Paul 	else
337896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3379935fe010SLuigi Rizzo 	/*
3380935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3381935fe010SLuigi Rizzo 	 */
3382935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3383935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
338496f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
338596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
338696f2e892SBill Paul 	} else {
338796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
338896f2e892SBill Paul 	}
338996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
339096f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
339196f2e892SBill Paul 	switch(sc->dc_cachesize) {
339296f2e892SBill Paul 	case 32:
339396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
339496f2e892SBill Paul 		break;
339596f2e892SBill Paul 	case 16:
339696f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
339796f2e892SBill Paul 		break;
339896f2e892SBill Paul 	case 8:
339996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
340096f2e892SBill Paul 		break;
340196f2e892SBill Paul 	case 0:
340296f2e892SBill Paul 	default:
340396f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
340496f2e892SBill Paul 		break;
340596f2e892SBill Paul 	}
340696f2e892SBill Paul 
340796f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
340896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
340996f2e892SBill Paul 	else {
3410d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
341196f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
341296f2e892SBill Paul 		} else {
341396f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
341496f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
341596f2e892SBill Paul 		}
341696f2e892SBill Paul 	}
341796f2e892SBill Paul 
341896f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
341996f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
342096f2e892SBill Paul 
342196f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
342296f2e892SBill Paul 		/*
342396f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
342496f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
342596f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
342696f2e892SBill Paul 		 * document the meaning of these bits so there's no way
342796f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
342896f2e892SBill Paul 		 * number all its own; the rest all use a different one.
342996f2e892SBill Paul 		 */
343096f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
343196f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
343296f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
343396f2e892SBill Paul 		else
343496f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
343596f2e892SBill Paul 	}
343696f2e892SBill Paul 
3437feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3438feb78939SJonathan Chen 		/*
3439feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3440feb78939SJonathan Chen 		 * can talk to the MII.
3441feb78939SJonathan Chen 		 */
3442feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3443feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3444feb78939SJonathan Chen 		DELAY(10);
3445feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3446feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3447feb78939SJonathan Chen 		DELAY(10);
3448feb78939SJonathan Chen 	}
3449feb78939SJonathan Chen 
345096f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3451d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
345296f2e892SBill Paul 
345396f2e892SBill Paul 	/* Init circular RX list. */
345496f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
34556b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
345622f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
345796f2e892SBill Paul 		dc_stop(sc);
345896f2e892SBill Paul 		return;
345996f2e892SBill Paul 	}
346096f2e892SBill Paul 
346196f2e892SBill Paul 	/*
346256e5e7aeSMaxime Henrion 	 * Init TX descriptors.
346396f2e892SBill Paul 	 */
346496f2e892SBill Paul 	dc_list_tx_init(sc);
346596f2e892SBill Paul 
346696f2e892SBill Paul 	/*
346796f2e892SBill Paul 	 * Load the address of the RX list.
346896f2e892SBill Paul 	 */
346956e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
347056e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
347196f2e892SBill Paul 
347296f2e892SBill Paul 	/*
347396f2e892SBill Paul 	 * Enable interrupts.
347496f2e892SBill Paul 	 */
3475e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3476e4fc250cSLuigi Rizzo 	/*
3477e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3478e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3479e4fc250cSLuigi Rizzo 	 * after a reset.
3480e4fc250cSLuigi Rizzo 	 */
348140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3482e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3483e4fc250cSLuigi Rizzo 	else
3484e4fc250cSLuigi Rizzo #endif
348596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
348696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
348796f2e892SBill Paul 
348896f2e892SBill Paul 	/* Enable transmitter. */
348996f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
349096f2e892SBill Paul 
349196f2e892SBill Paul 	/*
3492918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3493918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3494918434c8SBill Paul 	 * link and activity indications.
3495918434c8SBill Paul 	 */
349678999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3497918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3498918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
349978999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3500918434c8SBill Paul 	}
3501918434c8SBill Paul 
3502918434c8SBill Paul 	/*
350396f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
350496f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
350596f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
350696f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
350796f2e892SBill Paul 	 */
350896f2e892SBill Paul 	dc_setfilt(sc);
350996f2e892SBill Paul 
351096f2e892SBill Paul 	/* Enable receiver. */
351196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
351296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
351396f2e892SBill Paul 
351496f2e892SBill Paul 	mii_mediachg(mii);
351596f2e892SBill Paul 	dc_setcfg(sc, sc->dc_if_media);
351696f2e892SBill Paul 
351713f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
351813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
351996f2e892SBill Paul 
3520857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
352145521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3522857fd445SBill Paul 		sc->dc_link = 1;
3523857fd445SBill Paul 	else {
3524318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3525b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3526318b02fdSBill Paul 		else
3527b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3528857fd445SBill Paul 	}
352996f2e892SBill Paul }
353096f2e892SBill Paul 
353196f2e892SBill Paul /*
353296f2e892SBill Paul  * Set media options.
353396f2e892SBill Paul  */
3534e3d2833aSAlfred Perlstein static int
35350934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
353696f2e892SBill Paul {
353796f2e892SBill Paul 	struct dc_softc *sc;
353896f2e892SBill Paul 	struct mii_data *mii;
3539f43d9309SBill Paul 	struct ifmedia *ifm;
354096f2e892SBill Paul 
354196f2e892SBill Paul 	sc = ifp->if_softc;
354296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3543c8b27acaSJohn Baldwin 	DC_LOCK(sc);
354496f2e892SBill Paul 	mii_mediachg(mii);
3545f43d9309SBill Paul 	ifm = &mii->mii_media;
3546f43d9309SBill Paul 
3547f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
354845521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3549f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3550f43d9309SBill Paul 	else
355196f2e892SBill Paul 		sc->dc_link = 0;
3552c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
355396f2e892SBill Paul 
355496f2e892SBill Paul 	return (0);
355596f2e892SBill Paul }
355696f2e892SBill Paul 
355796f2e892SBill Paul /*
355896f2e892SBill Paul  * Report current media status.
355996f2e892SBill Paul  */
3560e3d2833aSAlfred Perlstein static void
35610934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
356296f2e892SBill Paul {
356396f2e892SBill Paul 	struct dc_softc *sc;
356496f2e892SBill Paul 	struct mii_data *mii;
3565f43d9309SBill Paul 	struct ifmedia *ifm;
356696f2e892SBill Paul 
356796f2e892SBill Paul 	sc = ifp->if_softc;
356896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3569c8b27acaSJohn Baldwin 	DC_LOCK(sc);
357096f2e892SBill Paul 	mii_pollstat(mii);
3571f43d9309SBill Paul 	ifm = &mii->mii_media;
3572f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
357345521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3574f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3575f43d9309SBill Paul 			ifmr->ifm_status = 0;
3576432120f2SMarius Strobl 			DC_UNLOCK(sc);
3577f43d9309SBill Paul 			return;
3578f43d9309SBill Paul 		}
3579f43d9309SBill Paul 	}
358096f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
358196f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3582c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
358396f2e892SBill Paul }
358496f2e892SBill Paul 
3585e3d2833aSAlfred Perlstein static int
35860934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
358796f2e892SBill Paul {
358896f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
358996f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
359096f2e892SBill Paul 	struct mii_data *mii;
3591d1ce9105SBill Paul 	int error = 0;
359296f2e892SBill Paul 
359396f2e892SBill Paul 	switch (command) {
359496f2e892SBill Paul 	case SIOCSIFFLAGS:
3595c8b27acaSJohn Baldwin 		DC_LOCK(sc);
359696f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
35975d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
35985d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
35995d6dfbbbSLuigi Rizzo 
360013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
36015d6dfbbbSLuigi Rizzo 				if (need_setfilt)
360296f2e892SBill Paul 					dc_setfilt(sc);
36035d6dfbbbSLuigi Rizzo 			} else {
360496f2e892SBill Paul 				sc->dc_txthresh = 0;
3605c8b27acaSJohn Baldwin 				dc_init_locked(sc);
360696f2e892SBill Paul 			}
360796f2e892SBill Paul 		} else {
360813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
360996f2e892SBill Paul 				dc_stop(sc);
361096f2e892SBill Paul 		}
361196f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3612c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
361396f2e892SBill Paul 		error = 0;
361496f2e892SBill Paul 		break;
361596f2e892SBill Paul 	case SIOCADDMULTI:
361696f2e892SBill Paul 	case SIOCDELMULTI:
3617c8b27acaSJohn Baldwin 		DC_LOCK(sc);
361896f2e892SBill Paul 		dc_setfilt(sc);
3619c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
362096f2e892SBill Paul 		error = 0;
362196f2e892SBill Paul 		break;
362296f2e892SBill Paul 	case SIOCGIFMEDIA:
362396f2e892SBill Paul 	case SIOCSIFMEDIA:
362496f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
362596f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
362696f2e892SBill Paul 		break;
3627e695984eSRuslan Ermilov 	case SIOCSIFCAP:
362840929967SGleb Smirnoff #ifdef DEVICE_POLLING
362940929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
363040929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
363140929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
363240929967SGleb Smirnoff 			if (error)
363340929967SGleb Smirnoff 				return(error);
3634c8b27acaSJohn Baldwin 			DC_LOCK(sc);
363540929967SGleb Smirnoff 			/* Disable interrupts */
363640929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
363740929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3638c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
363940929967SGleb Smirnoff 			return (error);
364040929967SGleb Smirnoff 
364140929967SGleb Smirnoff 		}
364240929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
364340929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
364440929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
364540929967SGleb Smirnoff 			/* Enable interrupts. */
364640929967SGleb Smirnoff 			DC_LOCK(sc);
364740929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
364840929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
364940929967SGleb Smirnoff 			DC_UNLOCK(sc);
365040929967SGleb Smirnoff 			return (error);
365140929967SGleb Smirnoff 		}
365240929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3653e695984eSRuslan Ermilov 		break;
365496f2e892SBill Paul 	default:
36559ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
365696f2e892SBill Paul 		break;
365796f2e892SBill Paul 	}
365896f2e892SBill Paul 
365996f2e892SBill Paul 	return (error);
366096f2e892SBill Paul }
366196f2e892SBill Paul 
3662e3d2833aSAlfred Perlstein static void
36630934f18aSMaxime Henrion dc_watchdog(struct ifnet *ifp)
366496f2e892SBill Paul {
366596f2e892SBill Paul 	struct dc_softc *sc;
366696f2e892SBill Paul 
366796f2e892SBill Paul 	sc = ifp->if_softc;
366896f2e892SBill Paul 
3669d1ce9105SBill Paul 	DC_LOCK(sc);
3670d1ce9105SBill Paul 
367196f2e892SBill Paul 	ifp->if_oerrors++;
367222f6205dSJohn Baldwin 	if_printf(ifp, "watchdog timeout\n");
367396f2e892SBill Paul 
367496f2e892SBill Paul 	dc_stop(sc);
367596f2e892SBill Paul 	dc_reset(sc);
3676c8b27acaSJohn Baldwin 	dc_init_locked(sc);
367796f2e892SBill Paul 
3678cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3679c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
368096f2e892SBill Paul 
3681d1ce9105SBill Paul 	DC_UNLOCK(sc);
368296f2e892SBill Paul }
368396f2e892SBill Paul 
368496f2e892SBill Paul /*
368596f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
368696f2e892SBill Paul  * RX and TX lists.
368796f2e892SBill Paul  */
3688e3d2833aSAlfred Perlstein static void
36890934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
369096f2e892SBill Paul {
369196f2e892SBill Paul 	struct ifnet *ifp;
3692b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3693b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3694b3811c95SMaxime Henrion 	int i;
3695af4358c7SMaxime Henrion 	u_int32_t ctl;
369696f2e892SBill Paul 
3697c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3698d1ce9105SBill Paul 
3699fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
370096f2e892SBill Paul 	ifp->if_timer = 0;
3701b3811c95SMaxime Henrion 	ld = sc->dc_ldata;
3702b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
370396f2e892SBill Paul 
3704b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
370596f2e892SBill Paul 
370613f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
37073b3ec200SPeter Wemm 
370896f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
370996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
371096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
371196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
371296f2e892SBill Paul 	sc->dc_link = 0;
371396f2e892SBill Paul 
371496f2e892SBill Paul 	/*
371596f2e892SBill Paul 	 * Free data in the RX lists.
371696f2e892SBill Paul 	 */
371796f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3718b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
371956e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
372056e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
372196f2e892SBill Paul 		}
372296f2e892SBill Paul 	}
3723b3811c95SMaxime Henrion 	bzero(&ld->dc_rx_list, sizeof(ld->dc_rx_list));
372496f2e892SBill Paul 
372596f2e892SBill Paul 	/*
372696f2e892SBill Paul 	 * Free the TX list buffers.
372796f2e892SBill Paul 	 */
372896f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3729b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
3730af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
3731af4358c7SMaxime Henrion 			if ((ctl & DC_TXCTL_SETUP) ||
37324ff4a9beSDon Lewis 			    !(ctl & DC_TXCTL_LASTFRAG)) {
3733b3811c95SMaxime Henrion 				cd->dc_tx_chain[i] = NULL;
373496f2e892SBill Paul 				continue;
373596f2e892SBill Paul 			}
373656e5e7aeSMaxime Henrion 			bus_dmamap_unload(sc->dc_mtag, cd->dc_tx_map[i]);
373756e5e7aeSMaxime Henrion 			m_freem(cd->dc_tx_chain[i]);
3738b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
373996f2e892SBill Paul 		}
374096f2e892SBill Paul 	}
3741b3811c95SMaxime Henrion 	bzero(&ld->dc_tx_list, sizeof(ld->dc_tx_list));
374296f2e892SBill Paul }
374396f2e892SBill Paul 
374496f2e892SBill Paul /*
3745e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3746e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3747e8388e14SMitsuru IWASAKI  * resume.
3748e8388e14SMitsuru IWASAKI  */
3749e3d2833aSAlfred Perlstein static int
37500934f18aSMaxime Henrion dc_suspend(device_t dev)
3751e8388e14SMitsuru IWASAKI {
3752e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3753e8388e14SMitsuru IWASAKI 
3754e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3755c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3756e8388e14SMitsuru IWASAKI 	dc_stop(sc);
3757e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
3758c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3759e8388e14SMitsuru IWASAKI 
3760e8388e14SMitsuru IWASAKI 	return (0);
3761e8388e14SMitsuru IWASAKI }
3762e8388e14SMitsuru IWASAKI 
3763e8388e14SMitsuru IWASAKI /*
3764e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
3765e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
3766e8388e14SMitsuru IWASAKI  * appropriate.
3767e8388e14SMitsuru IWASAKI  */
3768e3d2833aSAlfred Perlstein static int
37690934f18aSMaxime Henrion dc_resume(device_t dev)
3770e8388e14SMitsuru IWASAKI {
3771e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
3772e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
3773e8388e14SMitsuru IWASAKI 
3774e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
3775fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3776e8388e14SMitsuru IWASAKI 
3777e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
3778c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3779e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
3780c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3781e8388e14SMitsuru IWASAKI 
3782e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
3783c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3784e8388e14SMitsuru IWASAKI 
3785e8388e14SMitsuru IWASAKI 	return (0);
3786e8388e14SMitsuru IWASAKI }
3787e8388e14SMitsuru IWASAKI 
3788e8388e14SMitsuru IWASAKI /*
378996f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
379096f2e892SBill Paul  * get confused by errant DMAs when rebooting.
379196f2e892SBill Paul  */
3792e3d2833aSAlfred Perlstein static void
37930934f18aSMaxime Henrion dc_shutdown(device_t dev)
379496f2e892SBill Paul {
379596f2e892SBill Paul 	struct dc_softc *sc;
379696f2e892SBill Paul 
379796f2e892SBill Paul 	sc = device_get_softc(dev);
379896f2e892SBill Paul 
3799c8b27acaSJohn Baldwin 	DC_LOCK(sc);
380096f2e892SBill Paul 	dc_stop(sc);
3801c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
380296f2e892SBill Paul }
3803