xref: /freebsd/sys/dev/dc/if_dc.c (revision cb94db27d21aa3d25b8b74522eb0444d7f86fa6f)
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)
46593a1aeaSMartin Blapp  * ADMtek AN983 (www.admtek.com.tw)
47a2d61e43SWarner Losh  * ADMtek CardBus AN985 (www.admtek.com.tw)
48a2d61e43SWarner Losh  * Netgear FA511 (www.netgear.com) Appears to be rebadged ADMTek CardBus AN985
4988d739dcSBill Paul  * Davicom DM9100, DM9102, DM9102A (www.davicom8.com)
509ca710f6SJeroen Ruigrok van der Werven  * Accton EN1217 (www.accton.com)
51feb78939SJonathan Chen  * Xircom X3201 (www.xircom.com)
521d5e5310SBill Paul  * Abocom FE2500
531af8bec7SBill Paul  * Conexant LANfinity (www.conexant.com)
547eac366bSMartin Blapp  * 3Com OfficeConnect 10/100B 3CSOHO100B (www.3com.com)
5596f2e892SBill Paul  *
5696f2e892SBill Paul  * Datasheets for the 21143 are available at developer.intel.com.
5796f2e892SBill Paul  * Datasheets for the clone parts can be found at their respective sites.
5896f2e892SBill Paul  * (Except for the PNIC; see www.freebsd.org/~wpaul/PNIC/pnic.ps.gz.)
5996f2e892SBill Paul  * The PNIC II is essentially a Macronix 98715A chip; the only difference
6096f2e892SBill Paul  * worth noting is that its multicast hash table is only 128 bits wide
6196f2e892SBill Paul  * instead of 512.
6296f2e892SBill Paul  *
6396f2e892SBill Paul  * Written by Bill Paul <wpaul@ee.columbia.edu>
6496f2e892SBill Paul  * Electrical Engineering Department
6596f2e892SBill Paul  * Columbia University, New York City
6696f2e892SBill Paul  */
6796f2e892SBill Paul /*
6896f2e892SBill Paul  * The Intel 21143 is the successor to the DEC 21140. It is basically
6996f2e892SBill Paul  * the same as the 21140 but with a few new features. The 21143 supports
7096f2e892SBill Paul  * three kinds of media attachments:
7196f2e892SBill Paul  *
7296f2e892SBill Paul  * o MII port, for 10Mbps and 100Mbps support and NWAY
7396f2e892SBill Paul  *   autonegotiation provided by an external PHY.
7496f2e892SBill Paul  * o SYM port, for symbol mode 100Mbps support.
7596f2e892SBill Paul  * o 10baseT port.
7696f2e892SBill Paul  * o AUI/BNC port.
7796f2e892SBill Paul  *
7896f2e892SBill Paul  * The 100Mbps SYM port and 10baseT port can be used together in
7996f2e892SBill Paul  * combination with the internal NWAY support to create a 10/100
8096f2e892SBill Paul  * autosensing configuration.
8196f2e892SBill Paul  *
8296f2e892SBill Paul  * Note that not all tulip workalikes are handled in this driver: we only
8396f2e892SBill Paul  * deal with those which are relatively well behaved. The Winbond is
8496f2e892SBill Paul  * handled separately due to its different register offsets and the
8596f2e892SBill Paul  * special handling needed for its various bugs. The PNIC is handled
8696f2e892SBill Paul  * here, but I'm not thrilled about it.
8796f2e892SBill Paul  *
8896f2e892SBill Paul  * All of the workalike chips use some form of MII transceiver support
8996f2e892SBill Paul  * with the exception of the Macronix chips, which also have a SYM port.
9096f2e892SBill Paul  * The ASIX AX88140A is also documented to have a SYM port, but all
9196f2e892SBill Paul  * the cards I've seen use an MII transceiver, probably because the
9296f2e892SBill Paul  * AX88140A doesn't support internal NWAY.
9396f2e892SBill Paul  */
9496f2e892SBill Paul 
95f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
96f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
97f0796cd2SGleb Smirnoff #endif
98f0796cd2SGleb Smirnoff 
9996f2e892SBill Paul #include <sys/param.h>
100af4358c7SMaxime Henrion #include <sys/endian.h>
10196f2e892SBill Paul #include <sys/systm.h>
10296f2e892SBill Paul #include <sys/sockio.h>
10396f2e892SBill Paul #include <sys/mbuf.h>
10496f2e892SBill Paul #include <sys/malloc.h>
10596f2e892SBill Paul #include <sys/kernel.h>
106f11d01c3SPoul-Henning Kamp #include <sys/module.h>
10796f2e892SBill Paul #include <sys/socket.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  */
152ebc284ccSMarius Strobl static const 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" },
165593a1aeaSMartin Blapp 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983), 0,
166593a1aeaSMartin Blapp 		"ADMtek AN983 10/100BaseTX" },
1671e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985), 0,
168a2d61e43SWarner Losh 		"ADMtek AN985 CardBus 10/100BaseTX or clone" },
1691e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511), 0,
170e351d778SMartin Blapp 		"ADMtek ADM9511 10/100BaseTX" },
1711e2e70b1SJohn Baldwin 	{ DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513), 0,
172e351d778SMartin Blapp 		"ADMtek ADM9513 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);
235ebc284ccSMarius Strobl static const struct dc_type *dc_devtype(device_t);
2365f14ee23SPyun YongHyeon static void dc_discard_rxbuf(struct dc_softc *, int);
2375f14ee23SPyun YongHyeon static int dc_newbuf(struct dc_softc *, int);
238a10c0e45SMike Silbersack static int dc_encap(struct dc_softc *, struct mbuf **);
239e51a25f8SAlfred Perlstein static void dc_pnic_rx_bug_war(struct dc_softc *, int);
240e51a25f8SAlfred Perlstein static int dc_rx_resync(struct dc_softc *);
2411abcdbd1SAttilio Rao static int dc_rxeof(struct dc_softc *);
242e51a25f8SAlfred Perlstein static void dc_txeof(struct dc_softc *);
243e51a25f8SAlfred Perlstein static void dc_tick(void *);
244e51a25f8SAlfred Perlstein static void dc_tx_underrun(struct dc_softc *);
245e51a25f8SAlfred Perlstein static void dc_intr(void *);
246e51a25f8SAlfred Perlstein static void dc_start(struct ifnet *);
247c8b27acaSJohn Baldwin static void dc_start_locked(struct ifnet *);
248e51a25f8SAlfred Perlstein static int dc_ioctl(struct ifnet *, u_long, caddr_t);
249e51a25f8SAlfred Perlstein static void dc_init(void *);
250c8b27acaSJohn Baldwin static void dc_init_locked(struct dc_softc *);
251e51a25f8SAlfred Perlstein static void dc_stop(struct dc_softc *);
252b1d16143SMarius Strobl static void dc_watchdog(void *);
2536a087a87SPyun YongHyeon static int dc_shutdown(device_t);
254e51a25f8SAlfred Perlstein static int dc_ifmedia_upd(struct ifnet *);
255e51a25f8SAlfred Perlstein static void dc_ifmedia_sts(struct ifnet *, struct ifmediareq *);
25696f2e892SBill Paul 
2575f14ee23SPyun YongHyeon static int dc_dma_alloc(struct dc_softc *);
2585f14ee23SPyun YongHyeon static void dc_dma_free(struct dc_softc *);
2595f14ee23SPyun YongHyeon static void dc_dma_map_addr(void *, bus_dma_segment_t *, int, int);
2605f14ee23SPyun YongHyeon 
261e51a25f8SAlfred Perlstein static void dc_delay(struct dc_softc *);
262e51a25f8SAlfred Perlstein static void dc_eeprom_idle(struct dc_softc *);
263e51a25f8SAlfred Perlstein static void dc_eeprom_putbyte(struct dc_softc *, int);
264ee320f98SPyun YongHyeon static void dc_eeprom_getword(struct dc_softc *, int, uint16_t *);
265ee320f98SPyun YongHyeon static void dc_eeprom_getword_pnic(struct dc_softc *, int, uint16_t *);
266ee320f98SPyun YongHyeon static void dc_eeprom_getword_xircom(struct dc_softc *, int, uint16_t *);
2673097aa70SWarner Losh static void dc_eeprom_width(struct dc_softc *);
268e51a25f8SAlfred Perlstein static void dc_read_eeprom(struct dc_softc *, caddr_t, int, int, int);
26996f2e892SBill Paul 
270e51a25f8SAlfred Perlstein static void dc_mii_writebit(struct dc_softc *, int);
271e51a25f8SAlfred Perlstein static int dc_mii_readbit(struct dc_softc *);
272e51a25f8SAlfred Perlstein static void dc_mii_sync(struct dc_softc *);
273ee320f98SPyun YongHyeon static void dc_mii_send(struct dc_softc *, uint32_t, int);
274e51a25f8SAlfred Perlstein static int dc_mii_readreg(struct dc_softc *, struct dc_mii_frame *);
275e51a25f8SAlfred Perlstein static int dc_mii_writereg(struct dc_softc *, struct dc_mii_frame *);
276e51a25f8SAlfred Perlstein static int dc_miibus_readreg(device_t, int, int);
277e51a25f8SAlfred Perlstein static int dc_miibus_writereg(device_t, int, int, int);
278e51a25f8SAlfred Perlstein static void dc_miibus_statchg(device_t);
279e51a25f8SAlfred Perlstein static void dc_miibus_mediainit(device_t);
28096f2e892SBill Paul 
281e51a25f8SAlfred Perlstein static void dc_setcfg(struct dc_softc *, int);
2821da7683aSPyun YongHyeon static void dc_netcfg_wait(struct dc_softc *);
2833373489bSWarner Losh static uint32_t dc_mchash_le(struct dc_softc *, const uint8_t *);
2843373489bSWarner Losh static uint32_t dc_mchash_be(const uint8_t *);
285e51a25f8SAlfred Perlstein static void dc_setfilt_21143(struct dc_softc *);
286e51a25f8SAlfred Perlstein static void dc_setfilt_asix(struct dc_softc *);
287e51a25f8SAlfred Perlstein static void dc_setfilt_admtek(struct dc_softc *);
288e51a25f8SAlfred Perlstein static void dc_setfilt_xircom(struct dc_softc *);
28996f2e892SBill Paul 
290e51a25f8SAlfred Perlstein static void dc_setfilt(struct dc_softc *);
29196f2e892SBill Paul 
292e51a25f8SAlfred Perlstein static void dc_reset(struct dc_softc *);
293e51a25f8SAlfred Perlstein static int dc_list_rx_init(struct dc_softc *);
294e51a25f8SAlfred Perlstein static int dc_list_tx_init(struct dc_softc *);
29596f2e892SBill Paul 
296abe4e865SPyun YongHyeon static int dc_read_srom(struct dc_softc *, int);
297abe4e865SPyun YongHyeon static int dc_parse_21143_srom(struct dc_softc *);
298abe4e865SPyun YongHyeon static int dc_decode_leaf_sia(struct dc_softc *, struct dc_eblock_sia *);
299abe4e865SPyun YongHyeon static int dc_decode_leaf_mii(struct dc_softc *, struct dc_eblock_mii *);
300abe4e865SPyun YongHyeon static int dc_decode_leaf_sym(struct dc_softc *, struct dc_eblock_sym *);
301e51a25f8SAlfred Perlstein static void dc_apply_fixup(struct dc_softc *, int);
30239d76ed6SPyun YongHyeon static int dc_check_multiport(struct dc_softc *);
3035c1cfac4SBill Paul 
30496f2e892SBill Paul #ifdef DC_USEIOSPACE
30596f2e892SBill Paul #define	DC_RES			SYS_RES_IOPORT
30696f2e892SBill Paul #define	DC_RID			DC_PCI_CFBIO
30796f2e892SBill Paul #else
30896f2e892SBill Paul #define	DC_RES			SYS_RES_MEMORY
30996f2e892SBill Paul #define	DC_RID			DC_PCI_CFBMA
31096f2e892SBill Paul #endif
31196f2e892SBill Paul 
31296f2e892SBill Paul static device_method_t dc_methods[] = {
31396f2e892SBill Paul 	/* Device interface */
31496f2e892SBill Paul 	DEVMETHOD(device_probe,		dc_probe),
31596f2e892SBill Paul 	DEVMETHOD(device_attach,	dc_attach),
31696f2e892SBill Paul 	DEVMETHOD(device_detach,	dc_detach),
317e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_suspend,	dc_suspend),
318e8388e14SMitsuru IWASAKI 	DEVMETHOD(device_resume,	dc_resume),
31996f2e892SBill Paul 	DEVMETHOD(device_shutdown,	dc_shutdown),
32096f2e892SBill Paul 
32196f2e892SBill Paul 	/* bus interface */
32296f2e892SBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
32396f2e892SBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
32496f2e892SBill Paul 
32596f2e892SBill Paul 	/* MII interface */
32696f2e892SBill Paul 	DEVMETHOD(miibus_readreg,	dc_miibus_readreg),
32796f2e892SBill Paul 	DEVMETHOD(miibus_writereg,	dc_miibus_writereg),
32896f2e892SBill Paul 	DEVMETHOD(miibus_statchg,	dc_miibus_statchg),
329f43d9309SBill Paul 	DEVMETHOD(miibus_mediainit,	dc_miibus_mediainit),
33096f2e892SBill Paul 
33196f2e892SBill Paul 	{ 0, 0 }
33296f2e892SBill Paul };
33396f2e892SBill Paul 
33496f2e892SBill Paul static driver_t dc_driver = {
33596f2e892SBill Paul 	"dc",
33696f2e892SBill Paul 	dc_methods,
33796f2e892SBill Paul 	sizeof(struct dc_softc)
33896f2e892SBill Paul };
33996f2e892SBill Paul 
34096f2e892SBill Paul static devclass_t dc_devclass;
34196f2e892SBill Paul 
342f246e4a1SMatthew N. Dodd DRIVER_MODULE(dc, pci, dc_driver, dc_devclass, 0, 0);
34396f2e892SBill Paul DRIVER_MODULE(miibus, dc, miibus_driver, miibus_devclass, 0, 0);
34496f2e892SBill Paul 
34596f2e892SBill Paul #define	DC_SETBIT(sc, reg, x)				\
34696f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) | (x))
34796f2e892SBill Paul 
34896f2e892SBill Paul #define	DC_CLRBIT(sc, reg, x)				\
34996f2e892SBill Paul 	CSR_WRITE_4(sc, reg, CSR_READ_4(sc, reg) & ~(x))
35096f2e892SBill Paul 
35196f2e892SBill Paul #define	SIO_SET(x)	DC_SETBIT(sc, DC_SIO, (x))
35296f2e892SBill Paul #define	SIO_CLR(x)	DC_CLRBIT(sc, DC_SIO, (x))
35396f2e892SBill Paul 
354e3d2833aSAlfred Perlstein static void
3550934f18aSMaxime Henrion dc_delay(struct dc_softc *sc)
35696f2e892SBill Paul {
35796f2e892SBill Paul 	int idx;
35896f2e892SBill Paul 
35996f2e892SBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
36096f2e892SBill Paul 		CSR_READ_4(sc, DC_BUSCTL);
36196f2e892SBill Paul }
36296f2e892SBill Paul 
3632c876e15SPoul-Henning Kamp static void
3640934f18aSMaxime Henrion dc_eeprom_width(struct dc_softc *sc)
3653097aa70SWarner Losh {
3663097aa70SWarner Losh 	int i;
3673097aa70SWarner Losh 
3683097aa70SWarner Losh 	/* Force EEPROM to idle state. */
3693097aa70SWarner Losh 	dc_eeprom_idle(sc);
3703097aa70SWarner Losh 
3713097aa70SWarner Losh 	/* Enter EEPROM access mode. */
3723097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
3733097aa70SWarner Losh 	dc_delay(sc);
3743097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
3753097aa70SWarner Losh 	dc_delay(sc);
3763097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3773097aa70SWarner Losh 	dc_delay(sc);
3783097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
3793097aa70SWarner Losh 	dc_delay(sc);
3803097aa70SWarner Losh 
3813097aa70SWarner Losh 	for (i = 3; i--;) {
3823097aa70SWarner Losh 		if (6 & (1 << i))
3833097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3843097aa70SWarner Losh 		else
3853097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
3863097aa70SWarner Losh 		dc_delay(sc);
3873097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3883097aa70SWarner Losh 		dc_delay(sc);
3893097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3903097aa70SWarner Losh 		dc_delay(sc);
3913097aa70SWarner Losh 	}
3923097aa70SWarner Losh 
3933097aa70SWarner Losh 	for (i = 1; i <= 12; i++) {
3943097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3953097aa70SWarner Losh 		dc_delay(sc);
3963097aa70SWarner Losh 		if (!(CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)) {
3973097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
3983097aa70SWarner Losh 			dc_delay(sc);
3993097aa70SWarner Losh 			break;
4003097aa70SWarner Losh 		}
4013097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4023097aa70SWarner Losh 		dc_delay(sc);
4033097aa70SWarner Losh 	}
4043097aa70SWarner Losh 
4053097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4063097aa70SWarner Losh 	dc_eeprom_idle(sc);
4073097aa70SWarner Losh 
4083097aa70SWarner Losh 	if (i < 4 || i > 12)
4093097aa70SWarner Losh 		sc->dc_romwidth = 6;
4103097aa70SWarner Losh 	else
4113097aa70SWarner Losh 		sc->dc_romwidth = i;
4123097aa70SWarner Losh 
4133097aa70SWarner Losh 	/* Enter EEPROM access mode. */
4143097aa70SWarner Losh 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
4153097aa70SWarner Losh 	dc_delay(sc);
4163097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
4173097aa70SWarner Losh 	dc_delay(sc);
4183097aa70SWarner Losh 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4193097aa70SWarner Losh 	dc_delay(sc);
4203097aa70SWarner Losh 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
4213097aa70SWarner Losh 	dc_delay(sc);
4223097aa70SWarner Losh 
4233097aa70SWarner Losh 	/* Turn off EEPROM access mode. */
4243097aa70SWarner Losh 	dc_eeprom_idle(sc);
4253097aa70SWarner Losh }
4263097aa70SWarner Losh 
427e3d2833aSAlfred Perlstein static void
4280934f18aSMaxime Henrion dc_eeprom_idle(struct dc_softc *sc)
42996f2e892SBill Paul {
4300934f18aSMaxime Henrion 	int i;
43196f2e892SBill Paul 
43296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
43396f2e892SBill Paul 	dc_delay(sc);
43496f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_ROMCTL_READ);
43596f2e892SBill Paul 	dc_delay(sc);
43696f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
43796f2e892SBill Paul 	dc_delay(sc);
43896f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
43996f2e892SBill Paul 	dc_delay(sc);
44096f2e892SBill Paul 
44196f2e892SBill Paul 	for (i = 0; i < 25; i++) {
44296f2e892SBill Paul 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44396f2e892SBill Paul 		dc_delay(sc);
44496f2e892SBill Paul 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44596f2e892SBill Paul 		dc_delay(sc);
44696f2e892SBill Paul 	}
44796f2e892SBill Paul 
44896f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
44996f2e892SBill Paul 	dc_delay(sc);
45096f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CS);
45196f2e892SBill Paul 	dc_delay(sc);
45296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, 0x00000000);
45396f2e892SBill Paul }
45496f2e892SBill Paul 
45596f2e892SBill Paul /*
45696f2e892SBill Paul  * Send a read command and address to the EEPROM, check for ACK.
45796f2e892SBill Paul  */
458e3d2833aSAlfred Perlstein static void
4590934f18aSMaxime Henrion dc_eeprom_putbyte(struct dc_softc *sc, int addr)
46096f2e892SBill Paul {
4610934f18aSMaxime Henrion 	int d, i;
46296f2e892SBill Paul 
4633097aa70SWarner Losh 	d = DC_EECMD_READ >> 6;
4643097aa70SWarner Losh 	for (i = 3; i--; ) {
4653097aa70SWarner Losh 		if (d & (1 << i))
4663097aa70SWarner Losh 			DC_SETBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
46796f2e892SBill Paul 		else
4683097aa70SWarner Losh 			DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_DATAIN);
4693097aa70SWarner Losh 		dc_delay(sc);
4703097aa70SWarner Losh 		DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4713097aa70SWarner Losh 		dc_delay(sc);
4723097aa70SWarner Losh 		DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
4733097aa70SWarner Losh 		dc_delay(sc);
4743097aa70SWarner Losh 	}
47596f2e892SBill Paul 
47696f2e892SBill Paul 	/*
47796f2e892SBill Paul 	 * Feed in each bit and strobe the clock.
47896f2e892SBill Paul 	 */
4793097aa70SWarner Losh 	for (i = sc->dc_romwidth; i--;) {
4803097aa70SWarner Losh 		if (addr & (1 << i)) {
48196f2e892SBill Paul 			SIO_SET(DC_SIO_EE_DATAIN);
48296f2e892SBill Paul 		} else {
48396f2e892SBill Paul 			SIO_CLR(DC_SIO_EE_DATAIN);
48496f2e892SBill Paul 		}
48596f2e892SBill Paul 		dc_delay(sc);
48696f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
48796f2e892SBill Paul 		dc_delay(sc);
48896f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
48996f2e892SBill Paul 		dc_delay(sc);
49096f2e892SBill Paul 	}
49196f2e892SBill Paul }
49296f2e892SBill Paul 
49396f2e892SBill Paul /*
49496f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
49596f2e892SBill Paul  * The PNIC 82c168/82c169 has its own non-standard way to read
49696f2e892SBill Paul  * the EEPROM.
49796f2e892SBill Paul  */
498e3d2833aSAlfred Perlstein static void
499ee320f98SPyun YongHyeon dc_eeprom_getword_pnic(struct dc_softc *sc, int addr, uint16_t *dest)
50096f2e892SBill Paul {
5010934f18aSMaxime Henrion 	int i;
502ee320f98SPyun YongHyeon 	uint32_t r;
50396f2e892SBill Paul 
50496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_PN_SIOCTL, DC_PN_EEOPCODE_READ | addr);
50596f2e892SBill Paul 
50696f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
50796f2e892SBill Paul 		DELAY(1);
50896f2e892SBill Paul 		r = CSR_READ_4(sc, DC_SIO);
50996f2e892SBill Paul 		if (!(r & DC_PN_SIOCTL_BUSY)) {
510ee320f98SPyun YongHyeon 			*dest = (uint16_t)(r & 0xFFFF);
51196f2e892SBill Paul 			return;
51296f2e892SBill Paul 		}
51396f2e892SBill Paul 	}
51496f2e892SBill Paul }
51596f2e892SBill Paul 
51696f2e892SBill Paul /*
51796f2e892SBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
518feb78939SJonathan Chen  * The Xircom X3201 has its own non-standard way to read
519feb78939SJonathan Chen  * the EEPROM, too.
520feb78939SJonathan Chen  */
521e3d2833aSAlfred Perlstein static void
522ee320f98SPyun YongHyeon dc_eeprom_getword_xircom(struct dc_softc *sc, int addr, uint16_t *dest)
523feb78939SJonathan Chen {
5240934f18aSMaxime Henrion 
525feb78939SJonathan Chen 	SIO_SET(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
526feb78939SJonathan Chen 
527feb78939SJonathan Chen 	addr *= 2;
528feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
529ee320f98SPyun YongHyeon 	*dest = (uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff;
530feb78939SJonathan Chen 	addr += 1;
531feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_ROM, addr | 0x160);
532ee320f98SPyun YongHyeon 	*dest |= ((uint16_t)CSR_READ_4(sc, DC_SIO) & 0xff) << 8;
533feb78939SJonathan Chen 
534feb78939SJonathan Chen 	SIO_CLR(DC_SIO_ROMSEL | DC_SIO_ROMCTL_READ);
535feb78939SJonathan Chen }
536feb78939SJonathan Chen 
537feb78939SJonathan Chen /*
538feb78939SJonathan Chen  * Read a word of data stored in the EEPROM at address 'addr.'
53996f2e892SBill Paul  */
540e3d2833aSAlfred Perlstein static void
541ee320f98SPyun YongHyeon dc_eeprom_getword(struct dc_softc *sc, int addr, uint16_t *dest)
54296f2e892SBill Paul {
5430934f18aSMaxime Henrion 	int i;
544ee320f98SPyun YongHyeon 	uint16_t word = 0;
54596f2e892SBill Paul 
54696f2e892SBill Paul 	/* Force EEPROM to idle state. */
54796f2e892SBill Paul 	dc_eeprom_idle(sc);
54896f2e892SBill Paul 
54996f2e892SBill Paul 	/* Enter EEPROM access mode. */
55096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_EESEL);
55196f2e892SBill Paul 	dc_delay(sc);
55296f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO,  DC_SIO_ROMCTL_READ);
55396f2e892SBill Paul 	dc_delay(sc);
55496f2e892SBill Paul 	DC_CLRBIT(sc, DC_SIO, DC_SIO_EE_CLK);
55596f2e892SBill Paul 	dc_delay(sc);
55696f2e892SBill Paul 	DC_SETBIT(sc, DC_SIO, DC_SIO_EE_CS);
55796f2e892SBill Paul 	dc_delay(sc);
55896f2e892SBill Paul 
55996f2e892SBill Paul 	/*
56096f2e892SBill Paul 	 * Send address of word we want to read.
56196f2e892SBill Paul 	 */
56296f2e892SBill Paul 	dc_eeprom_putbyte(sc, addr);
56396f2e892SBill Paul 
56496f2e892SBill Paul 	/*
56596f2e892SBill Paul 	 * Start reading bits from EEPROM.
56696f2e892SBill Paul 	 */
56796f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
56896f2e892SBill Paul 		SIO_SET(DC_SIO_EE_CLK);
56996f2e892SBill Paul 		dc_delay(sc);
57096f2e892SBill Paul 		if (CSR_READ_4(sc, DC_SIO) & DC_SIO_EE_DATAOUT)
57196f2e892SBill Paul 			word |= i;
57296f2e892SBill Paul 		dc_delay(sc);
57396f2e892SBill Paul 		SIO_CLR(DC_SIO_EE_CLK);
57496f2e892SBill Paul 		dc_delay(sc);
57596f2e892SBill Paul 	}
57696f2e892SBill Paul 
57796f2e892SBill Paul 	/* Turn off EEPROM access mode. */
57896f2e892SBill Paul 	dc_eeprom_idle(sc);
57996f2e892SBill Paul 
58096f2e892SBill Paul 	*dest = word;
58196f2e892SBill Paul }
58296f2e892SBill Paul 
58396f2e892SBill Paul /*
58496f2e892SBill Paul  * Read a sequence of words from the EEPROM.
58596f2e892SBill Paul  */
586e3d2833aSAlfred Perlstein static void
5878c7ff1f3SMaxime Henrion dc_read_eeprom(struct dc_softc *sc, caddr_t dest, int off, int cnt, int be)
58896f2e892SBill Paul {
58996f2e892SBill Paul 	int i;
590ee320f98SPyun YongHyeon 	uint16_t word = 0, *ptr;
59196f2e892SBill Paul 
59296f2e892SBill Paul 	for (i = 0; i < cnt; i++) {
59396f2e892SBill Paul 		if (DC_IS_PNIC(sc))
59496f2e892SBill Paul 			dc_eeprom_getword_pnic(sc, off + i, &word);
595feb78939SJonathan Chen 		else if (DC_IS_XIRCOM(sc))
596feb78939SJonathan Chen 			dc_eeprom_getword_xircom(sc, off + i, &word);
59796f2e892SBill Paul 		else
59896f2e892SBill Paul 			dc_eeprom_getword(sc, off + i, &word);
599ee320f98SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
6008c7ff1f3SMaxime Henrion 		if (be)
6018c7ff1f3SMaxime Henrion 			*ptr = be16toh(word);
60296f2e892SBill Paul 		else
6038c7ff1f3SMaxime Henrion 			*ptr = le16toh(word);
60496f2e892SBill Paul 	}
60596f2e892SBill Paul }
60696f2e892SBill Paul 
60796f2e892SBill Paul /*
60896f2e892SBill Paul  * The following two routines are taken from the Macronix 98713
60996f2e892SBill Paul  * Application Notes pp.19-21.
61096f2e892SBill Paul  */
61196f2e892SBill Paul /*
61296f2e892SBill Paul  * Write a bit to the MII bus.
61396f2e892SBill Paul  */
614e3d2833aSAlfred Perlstein static void
6150934f18aSMaxime Henrion dc_mii_writebit(struct dc_softc *sc, int bit)
61696f2e892SBill Paul {
61715578119SMarius Strobl 	uint32_t reg;
6180934f18aSMaxime Henrion 
61915578119SMarius Strobl 	reg = DC_SIO_ROMCTL_WRITE | (bit != 0 ? DC_SIO_MII_DATAOUT : 0);
62015578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
62115578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
62215578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
62315578119SMarius Strobl 	DELAY(1);
62496f2e892SBill Paul 
62515578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg | DC_SIO_MII_CLK);
62615578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
62715578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
62815578119SMarius Strobl 	DELAY(1);
62915578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
63015578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
63115578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
63215578119SMarius Strobl 	DELAY(1);
63396f2e892SBill Paul }
63496f2e892SBill Paul 
63596f2e892SBill Paul /*
63696f2e892SBill Paul  * Read a bit from the MII bus.
63796f2e892SBill Paul  */
638e3d2833aSAlfred Perlstein static int
6390934f18aSMaxime Henrion dc_mii_readbit(struct dc_softc *sc)
64096f2e892SBill Paul {
64115578119SMarius Strobl 	uint32_t reg;
6420934f18aSMaxime Henrion 
64315578119SMarius Strobl 	reg = DC_SIO_ROMCTL_READ | DC_SIO_MII_DIR;
64415578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
64515578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
64615578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
64715578119SMarius Strobl 	DELAY(1);
64815578119SMarius Strobl 	(void)CSR_READ_4(sc, DC_SIO);
64915578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg | DC_SIO_MII_CLK);
65015578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65115578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65215578119SMarius Strobl 	DELAY(1);
65315578119SMarius Strobl 	CSR_WRITE_4(sc, DC_SIO, reg);
65415578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
65515578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
65615578119SMarius Strobl 	DELAY(1);
65796f2e892SBill Paul 	if (CSR_READ_4(sc, DC_SIO) & DC_SIO_MII_DATAIN)
65896f2e892SBill Paul 		return (1);
65996f2e892SBill Paul 
66096f2e892SBill Paul 	return (0);
66196f2e892SBill Paul }
66296f2e892SBill Paul 
66396f2e892SBill Paul /*
66496f2e892SBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
66596f2e892SBill Paul  */
666e3d2833aSAlfred Perlstein static void
6670934f18aSMaxime Henrion dc_mii_sync(struct dc_softc *sc)
66896f2e892SBill Paul {
6690934f18aSMaxime Henrion 	int i;
67096f2e892SBill Paul 
67196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_SIO, DC_SIO_ROMCTL_WRITE);
67215578119SMarius Strobl 	CSR_BARRIER_4(sc, DC_SIO,
67315578119SMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
67415578119SMarius Strobl 	DELAY(1);
67596f2e892SBill Paul 
67696f2e892SBill Paul 	for (i = 0; i < 32; i++)
67796f2e892SBill Paul 		dc_mii_writebit(sc, 1);
67896f2e892SBill Paul }
67996f2e892SBill Paul 
68096f2e892SBill Paul /*
68196f2e892SBill Paul  * Clock a series of bits through the MII.
68296f2e892SBill Paul  */
683e3d2833aSAlfred Perlstein static void
684ee320f98SPyun YongHyeon dc_mii_send(struct dc_softc *sc, uint32_t bits, int cnt)
68596f2e892SBill Paul {
68696f2e892SBill Paul 	int i;
68796f2e892SBill Paul 
68896f2e892SBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1)
68996f2e892SBill Paul 		dc_mii_writebit(sc, bits & i);
69096f2e892SBill Paul }
69196f2e892SBill Paul 
69296f2e892SBill Paul /*
69396f2e892SBill Paul  * Read an PHY register through the MII.
69496f2e892SBill Paul  */
695e3d2833aSAlfred Perlstein static int
6960934f18aSMaxime Henrion dc_mii_readreg(struct dc_softc *sc, struct dc_mii_frame *frame)
69796f2e892SBill Paul {
69815578119SMarius Strobl 	int i;
69996f2e892SBill Paul 
70096f2e892SBill Paul 	/*
70196f2e892SBill Paul 	 * Set up frame for RX.
70296f2e892SBill Paul 	 */
70396f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
70496f2e892SBill Paul 	frame->mii_opcode = DC_MII_READOP;
70596f2e892SBill Paul 
70696f2e892SBill Paul 	/*
70796f2e892SBill Paul 	 * Sync the PHYs.
70896f2e892SBill Paul 	 */
70996f2e892SBill Paul 	dc_mii_sync(sc);
71096f2e892SBill Paul 
71196f2e892SBill Paul 	/*
71296f2e892SBill Paul 	 * Send command/address info.
71396f2e892SBill Paul 	 */
71496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
71596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
71696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
71796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
71896f2e892SBill Paul 
71996f2e892SBill Paul 	/*
72015578119SMarius Strobl 	 * Now try reading data bits.  If the turnaround failed, we still
72196f2e892SBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
72296f2e892SBill Paul 	 */
72315578119SMarius Strobl 	frame->mii_turnaround = dc_mii_readbit(sc);
72415578119SMarius Strobl 	if (frame->mii_turnaround != 0) {
7250934f18aSMaxime Henrion 		for (i = 0; i < 16; i++)
72696f2e892SBill Paul 			dc_mii_readbit(sc);
72796f2e892SBill Paul 		goto fail;
72896f2e892SBill Paul 	}
72996f2e892SBill Paul 	for (i = 0x8000; i; i >>= 1) {
73096f2e892SBill Paul 		if (dc_mii_readbit(sc))
73196f2e892SBill Paul 			frame->mii_data |= i;
73296f2e892SBill Paul 	}
73396f2e892SBill Paul 
73496f2e892SBill Paul fail:
73596f2e892SBill Paul 
73615578119SMarius Strobl 	/* Clock the idle bits. */
73796f2e892SBill Paul 	dc_mii_writebit(sc, 0);
73896f2e892SBill Paul 	dc_mii_writebit(sc, 0);
73996f2e892SBill Paul 
74015578119SMarius Strobl 	if (frame->mii_turnaround != 0)
74196f2e892SBill Paul 		return (1);
74296f2e892SBill Paul 	return (0);
74396f2e892SBill Paul }
74496f2e892SBill Paul 
74596f2e892SBill Paul /*
74696f2e892SBill Paul  * Write to a PHY register through the MII.
74796f2e892SBill Paul  */
748e3d2833aSAlfred Perlstein static int
7490934f18aSMaxime Henrion dc_mii_writereg(struct dc_softc *sc, struct dc_mii_frame *frame)
75096f2e892SBill Paul {
7510934f18aSMaxime Henrion 
75296f2e892SBill Paul 	/*
75396f2e892SBill Paul 	 * Set up frame for TX.
75496f2e892SBill Paul 	 */
75596f2e892SBill Paul 	frame->mii_stdelim = DC_MII_STARTDELIM;
75696f2e892SBill Paul 	frame->mii_opcode = DC_MII_WRITEOP;
75796f2e892SBill Paul 	frame->mii_turnaround = DC_MII_TURNAROUND;
75896f2e892SBill Paul 
75996f2e892SBill Paul 	/*
76096f2e892SBill Paul 	 * Sync the PHYs.
76196f2e892SBill Paul 	 */
76296f2e892SBill Paul 	dc_mii_sync(sc);
76396f2e892SBill Paul 
76496f2e892SBill Paul 	dc_mii_send(sc, frame->mii_stdelim, 2);
76596f2e892SBill Paul 	dc_mii_send(sc, frame->mii_opcode, 2);
76696f2e892SBill Paul 	dc_mii_send(sc, frame->mii_phyaddr, 5);
76796f2e892SBill Paul 	dc_mii_send(sc, frame->mii_regaddr, 5);
76896f2e892SBill Paul 	dc_mii_send(sc, frame->mii_turnaround, 2);
76996f2e892SBill Paul 	dc_mii_send(sc, frame->mii_data, 16);
77096f2e892SBill Paul 
77115578119SMarius Strobl 	/* Clock the idle bits. */
77296f2e892SBill Paul 	dc_mii_writebit(sc, 0);
77396f2e892SBill Paul 	dc_mii_writebit(sc, 0);
77496f2e892SBill Paul 
77596f2e892SBill Paul 	return (0);
77696f2e892SBill Paul }
77796f2e892SBill Paul 
778e3d2833aSAlfred Perlstein static int
7790934f18aSMaxime Henrion dc_miibus_readreg(device_t dev, int phy, int reg)
78096f2e892SBill Paul {
78196f2e892SBill Paul 	struct dc_mii_frame frame;
78296f2e892SBill Paul 	struct dc_softc	 *sc;
783c85c4667SBill Paul 	int i, rval, phy_reg = 0;
78496f2e892SBill Paul 
78596f2e892SBill Paul 	sc = device_get_softc(dev);
7860934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
78796f2e892SBill Paul 
7885c1cfac4SBill Paul 	if (sc->dc_pmode != DC_PMODE_MII) {
78996f2e892SBill Paul 		if (phy == (MII_NPHY - 1)) {
79096f2e892SBill Paul 			switch (reg) {
79196f2e892SBill Paul 			case MII_BMSR:
79296f2e892SBill Paul 			/*
79396f2e892SBill Paul 			 * Fake something to make the probe
79496f2e892SBill Paul 			 * code think there's a PHY here.
79596f2e892SBill Paul 			 */
79696f2e892SBill Paul 				return (BMSR_MEDIAMASK);
79796f2e892SBill Paul 				break;
79896f2e892SBill Paul 			case MII_PHYIDR1:
79996f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80096f2e892SBill Paul 					return (DC_VENDORID_LO);
80196f2e892SBill Paul 				return (DC_VENDORID_DEC);
80296f2e892SBill Paul 				break;
80396f2e892SBill Paul 			case MII_PHYIDR2:
80496f2e892SBill Paul 				if (DC_IS_PNIC(sc))
80596f2e892SBill Paul 					return (DC_DEVICEID_82C168);
80696f2e892SBill Paul 				return (DC_DEVICEID_21143);
80796f2e892SBill Paul 				break;
80896f2e892SBill Paul 			default:
80996f2e892SBill Paul 				return (0);
81096f2e892SBill Paul 				break;
81196f2e892SBill Paul 			}
81296f2e892SBill Paul 		} else
81396f2e892SBill Paul 			return (0);
81496f2e892SBill Paul 	}
81596f2e892SBill Paul 
81696f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
81796f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_READ |
81896f2e892SBill Paul 		    (phy << 23) | (reg << 18));
81996f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
82096f2e892SBill Paul 			DELAY(1);
82196f2e892SBill Paul 			rval = CSR_READ_4(sc, DC_PN_MII);
82296f2e892SBill Paul 			if (!(rval & DC_PN_MII_BUSY)) {
82396f2e892SBill Paul 				rval &= 0xFFFF;
82496f2e892SBill Paul 				return (rval == 0xFFFF ? 0 : rval);
82596f2e892SBill Paul 			}
82696f2e892SBill Paul 		}
82796f2e892SBill Paul 		return (0);
82896f2e892SBill Paul 	}
82996f2e892SBill Paul 
83096f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
83196f2e892SBill Paul 		switch (reg) {
83296f2e892SBill Paul 		case MII_BMCR:
83396f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
83496f2e892SBill Paul 			break;
83596f2e892SBill Paul 		case MII_BMSR:
83696f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
83796f2e892SBill Paul 			break;
83896f2e892SBill Paul 		case MII_PHYIDR1:
83996f2e892SBill Paul 			phy_reg = DC_AL_VENID;
84096f2e892SBill Paul 			break;
84196f2e892SBill Paul 		case MII_PHYIDR2:
84296f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
84396f2e892SBill Paul 			break;
84496f2e892SBill Paul 		case MII_ANAR:
84596f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
84696f2e892SBill Paul 			break;
84796f2e892SBill Paul 		case MII_ANLPAR:
84896f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
84996f2e892SBill Paul 			break;
85096f2e892SBill Paul 		case MII_ANER:
85196f2e892SBill Paul 			phy_reg = DC_AL_ANER;
85296f2e892SBill Paul 			break;
85396f2e892SBill Paul 		default:
85422f6205dSJohn Baldwin 			device_printf(dev, "phy_read: bad phy register %x\n",
85522f6205dSJohn Baldwin 			    reg);
85696f2e892SBill Paul 			return (0);
85796f2e892SBill Paul 			break;
85896f2e892SBill Paul 		}
85996f2e892SBill Paul 
86096f2e892SBill Paul 		rval = CSR_READ_4(sc, phy_reg) & 0x0000FFFF;
86196f2e892SBill Paul 
86296f2e892SBill Paul 		if (rval == 0xFFFF)
86396f2e892SBill Paul 			return (0);
86496f2e892SBill Paul 		return (rval);
86596f2e892SBill Paul 	}
86696f2e892SBill Paul 
86796f2e892SBill Paul 	frame.mii_phyaddr = phy;
86896f2e892SBill Paul 	frame.mii_regaddr = reg;
869419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
870f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
871f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
872419146d9SBill Paul 	}
87396f2e892SBill Paul 	dc_mii_readreg(sc, &frame);
874419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
875f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
87696f2e892SBill Paul 
87796f2e892SBill Paul 	return (frame.mii_data);
87896f2e892SBill Paul }
87996f2e892SBill Paul 
880e3d2833aSAlfred Perlstein static int
8810934f18aSMaxime Henrion dc_miibus_writereg(device_t dev, int phy, int reg, int data)
88296f2e892SBill Paul {
88396f2e892SBill Paul 	struct dc_softc *sc;
88496f2e892SBill Paul 	struct dc_mii_frame frame;
885c85c4667SBill Paul 	int i, phy_reg = 0;
88696f2e892SBill Paul 
88796f2e892SBill Paul 	sc = device_get_softc(dev);
8880934f18aSMaxime Henrion 	bzero(&frame, sizeof(frame));
88996f2e892SBill Paul 
89096f2e892SBill Paul 	if (DC_IS_PNIC(sc)) {
89196f2e892SBill Paul 		CSR_WRITE_4(sc, DC_PN_MII, DC_PN_MIIOPCODE_WRITE |
89296f2e892SBill Paul 		    (phy << 23) | (reg << 10) | data);
89396f2e892SBill Paul 		for (i = 0; i < DC_TIMEOUT; i++) {
89496f2e892SBill Paul 			if (!(CSR_READ_4(sc, DC_PN_MII) & DC_PN_MII_BUSY))
89596f2e892SBill Paul 				break;
89696f2e892SBill Paul 		}
89796f2e892SBill Paul 		return (0);
89896f2e892SBill Paul 	}
89996f2e892SBill Paul 
90096f2e892SBill Paul 	if (DC_IS_COMET(sc)) {
90196f2e892SBill Paul 		switch (reg) {
90296f2e892SBill Paul 		case MII_BMCR:
90396f2e892SBill Paul 			phy_reg = DC_AL_BMCR;
90496f2e892SBill Paul 			break;
90596f2e892SBill Paul 		case MII_BMSR:
90696f2e892SBill Paul 			phy_reg = DC_AL_BMSR;
90796f2e892SBill Paul 			break;
90896f2e892SBill Paul 		case MII_PHYIDR1:
90996f2e892SBill Paul 			phy_reg = DC_AL_VENID;
91096f2e892SBill Paul 			break;
91196f2e892SBill Paul 		case MII_PHYIDR2:
91296f2e892SBill Paul 			phy_reg = DC_AL_DEVID;
91396f2e892SBill Paul 			break;
91496f2e892SBill Paul 		case MII_ANAR:
91596f2e892SBill Paul 			phy_reg = DC_AL_ANAR;
91696f2e892SBill Paul 			break;
91796f2e892SBill Paul 		case MII_ANLPAR:
91896f2e892SBill Paul 			phy_reg = DC_AL_LPAR;
91996f2e892SBill Paul 			break;
92096f2e892SBill Paul 		case MII_ANER:
92196f2e892SBill Paul 			phy_reg = DC_AL_ANER;
92296f2e892SBill Paul 			break;
92396f2e892SBill Paul 		default:
92422f6205dSJohn Baldwin 			device_printf(dev, "phy_write: bad phy register %x\n",
92522f6205dSJohn Baldwin 			    reg);
92696f2e892SBill Paul 			return (0);
92796f2e892SBill Paul 			break;
92896f2e892SBill Paul 		}
92996f2e892SBill Paul 
93096f2e892SBill Paul 		CSR_WRITE_4(sc, phy_reg, data);
93196f2e892SBill Paul 		return (0);
93296f2e892SBill Paul 	}
93396f2e892SBill Paul 
93496f2e892SBill Paul 	frame.mii_phyaddr = phy;
93596f2e892SBill Paul 	frame.mii_regaddr = reg;
93696f2e892SBill Paul 	frame.mii_data = data;
93796f2e892SBill Paul 
938419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713) {
939f43d9309SBill Paul 		phy_reg = CSR_READ_4(sc, DC_NETCFG);
940f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg & ~DC_NETCFG_PORTSEL);
941419146d9SBill Paul 	}
94296f2e892SBill Paul 	dc_mii_writereg(sc, &frame);
943419146d9SBill Paul 	if (sc->dc_type == DC_TYPE_98713)
944f43d9309SBill Paul 		CSR_WRITE_4(sc, DC_NETCFG, phy_reg);
94596f2e892SBill Paul 
94696f2e892SBill Paul 	return (0);
94796f2e892SBill Paul }
94896f2e892SBill Paul 
949e3d2833aSAlfred Perlstein static void
9500934f18aSMaxime Henrion dc_miibus_statchg(device_t dev)
95196f2e892SBill Paul {
95296f2e892SBill Paul 	struct dc_softc *sc;
953d314ebf5SPyun YongHyeon 	struct ifnet *ifp;
95496f2e892SBill Paul 	struct mii_data *mii;
955f43d9309SBill Paul 	struct ifmedia *ifm;
95696f2e892SBill Paul 
95796f2e892SBill Paul 	sc = device_get_softc(dev);
9585c1cfac4SBill Paul 
95996f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
960d314ebf5SPyun YongHyeon 	ifp = sc->dc_ifp;
961d314ebf5SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
962d314ebf5SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
963d314ebf5SPyun YongHyeon 		return;
964d314ebf5SPyun YongHyeon 
965f43d9309SBill Paul 	ifm = &mii->mii_media;
966f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) &&
96745521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
968f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
969f43d9309SBill Paul 		sc->dc_if_media = ifm->ifm_media;
970d314ebf5SPyun YongHyeon 		return;
971f43d9309SBill Paul 	}
972d314ebf5SPyun YongHyeon 
973d314ebf5SPyun YongHyeon 	sc->dc_link = 0;
974d314ebf5SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
975d314ebf5SPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
976d314ebf5SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
977d314ebf5SPyun YongHyeon 		case IFM_10_T:
978d314ebf5SPyun YongHyeon 		case IFM_100_TX:
979d314ebf5SPyun YongHyeon 			sc->dc_link = 1;
980d314ebf5SPyun YongHyeon 			break;
981d314ebf5SPyun YongHyeon 		default:
982d314ebf5SPyun YongHyeon 			break;
983d314ebf5SPyun YongHyeon 		}
984d314ebf5SPyun YongHyeon 	}
985d314ebf5SPyun YongHyeon 	if (sc->dc_link == 0)
986d314ebf5SPyun YongHyeon 		return;
987d314ebf5SPyun YongHyeon 
988d314ebf5SPyun YongHyeon 	sc->dc_if_media = mii->mii_media_active;
989d314ebf5SPyun YongHyeon 	if (DC_IS_ADMTEK(sc))
990d314ebf5SPyun YongHyeon 		return;
991d314ebf5SPyun YongHyeon 	dc_setcfg(sc, mii->mii_media_active);
992f43d9309SBill Paul }
993f43d9309SBill Paul 
994f43d9309SBill Paul /*
995f43d9309SBill Paul  * Special support for DM9102A cards with HomePNA PHYs. Note:
996f43d9309SBill Paul  * with the Davicom DM9102A/DM9801 eval board that I have, it seems
997f43d9309SBill Paul  * to be impossible to talk to the management interface of the DM9801
998f43d9309SBill Paul  * PHY (its MDIO pin is not connected to anything). Consequently,
999f43d9309SBill Paul  * the driver has to just 'know' about the additional mode and deal
1000f43d9309SBill Paul  * with it itself. *sigh*
1001f43d9309SBill Paul  */
1002e3d2833aSAlfred Perlstein static void
10030934f18aSMaxime Henrion dc_miibus_mediainit(device_t dev)
1004f43d9309SBill Paul {
1005f43d9309SBill Paul 	struct dc_softc *sc;
1006f43d9309SBill Paul 	struct mii_data *mii;
1007f43d9309SBill Paul 	struct ifmedia *ifm;
1008f43d9309SBill Paul 	int rev;
1009f43d9309SBill Paul 
10101e2e70b1SJohn Baldwin 	rev = pci_get_revid(dev);
1011f43d9309SBill Paul 
1012f43d9309SBill Paul 	sc = device_get_softc(dev);
1013f43d9309SBill Paul 	mii = device_get_softc(sc->dc_miibus);
1014f43d9309SBill Paul 	ifm = &mii->mii_media;
1015f43d9309SBill Paul 
1016f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc) && rev >= DC_REVISION_DM9102A)
101745521525SPoul-Henning Kamp 		ifmedia_add(ifm, IFM_ETHER | IFM_HPNA_1, 0, NULL);
101896f2e892SBill Paul }
101996f2e892SBill Paul 
102079d11e09SBill Paul #define	DC_BITS_512	9
102179d11e09SBill Paul #define	DC_BITS_128	7
102279d11e09SBill Paul #define	DC_BITS_64	6
102396f2e892SBill Paul 
10243373489bSWarner Losh static uint32_t
10253373489bSWarner Losh dc_mchash_le(struct dc_softc *sc, const uint8_t *addr)
102696f2e892SBill Paul {
10273373489bSWarner Losh 	uint32_t crc;
102896f2e892SBill Paul 
102996f2e892SBill Paul 	/* Compute CRC for the address value. */
10300e939c0cSChristian Weisgerber 	crc = ether_crc32_le(addr, ETHER_ADDR_LEN);
103196f2e892SBill Paul 
103279d11e09SBill Paul 	/*
103379d11e09SBill Paul 	 * The hash table on the PNIC II and the MX98715AEC-C/D/E
103479d11e09SBill Paul 	 * chips is only 128 bits wide.
103579d11e09SBill Paul 	 */
103679d11e09SBill Paul 	if (sc->dc_flags & DC_128BIT_HASH)
103779d11e09SBill Paul 		return (crc & ((1 << DC_BITS_128) - 1));
103896f2e892SBill Paul 
103979d11e09SBill Paul 	/* The hash table on the MX98715BEC is only 64 bits wide. */
104079d11e09SBill Paul 	if (sc->dc_flags & DC_64BIT_HASH)
104179d11e09SBill Paul 		return (crc & ((1 << DC_BITS_64) - 1));
104279d11e09SBill Paul 
1043feb78939SJonathan Chen 	/* Xircom's hash filtering table is different (read: weird) */
1044feb78939SJonathan Chen 	/* Xircom uses the LEAST significant bits */
1045feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
1046feb78939SJonathan Chen 		if ((crc & 0x180) == 0x180)
10470934f18aSMaxime Henrion 			return ((crc & 0x0F) + (crc & 0x70) * 3 + (14 << 4));
1048feb78939SJonathan Chen 		else
10490934f18aSMaxime Henrion 			return ((crc & 0x1F) + ((crc >> 1) & 0xF0) * 3 +
10500934f18aSMaxime Henrion 			    (12 << 4));
1051feb78939SJonathan Chen 	}
1052feb78939SJonathan Chen 
105379d11e09SBill Paul 	return (crc & ((1 << DC_BITS_512) - 1));
105496f2e892SBill Paul }
105596f2e892SBill Paul 
105696f2e892SBill Paul /*
105796f2e892SBill Paul  * Calculate CRC of a multicast group address, return the lower 6 bits.
105896f2e892SBill Paul  */
10593373489bSWarner Losh static uint32_t
10603373489bSWarner Losh dc_mchash_be(const uint8_t *addr)
106196f2e892SBill Paul {
10620e939c0cSChristian Weisgerber 	uint32_t crc;
106396f2e892SBill Paul 
106496f2e892SBill Paul 	/* Compute CRC for the address value. */
10650e939c0cSChristian Weisgerber 	crc = ether_crc32_be(addr, ETHER_ADDR_LEN);
106696f2e892SBill Paul 
10670934f18aSMaxime Henrion 	/* Return the filter bit position. */
106896f2e892SBill Paul 	return ((crc >> 26) & 0x0000003F);
106996f2e892SBill Paul }
107096f2e892SBill Paul 
107196f2e892SBill Paul /*
107296f2e892SBill Paul  * 21143-style RX filter setup routine. Filter programming is done by
107396f2e892SBill Paul  * downloading a special setup frame into the TX engine. 21143, Macronix,
107496f2e892SBill Paul  * PNIC, PNIC II and Davicom chips are programmed this way.
107596f2e892SBill Paul  *
107696f2e892SBill Paul  * We always program the chip using 'hash perfect' mode, i.e. one perfect
107796f2e892SBill Paul  * address (our node address) and a 512-bit hash filter for multicast
107896f2e892SBill Paul  * frames. We also sneak the broadcast address into the hash filter since
107996f2e892SBill Paul  * we need that too.
108096f2e892SBill Paul  */
10812c876e15SPoul-Henning Kamp static void
10820934f18aSMaxime Henrion dc_setfilt_21143(struct dc_softc *sc)
108396f2e892SBill Paul {
10848df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
108596f2e892SBill Paul 	struct dc_desc *sframe;
1086ee320f98SPyun YongHyeon 	uint32_t h, *sp;
108796f2e892SBill Paul 	struct ifmultiaddr *ifma;
108896f2e892SBill Paul 	struct ifnet *ifp;
108996f2e892SBill Paul 	int i;
109096f2e892SBill Paul 
1091fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
109296f2e892SBill Paul 
109396f2e892SBill Paul 	i = sc->dc_cdata.dc_tx_prod;
109496f2e892SBill Paul 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
109596f2e892SBill Paul 	sc->dc_cdata.dc_tx_cnt++;
10965f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
109756e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
10980934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
109996f2e892SBill Paul 
11005f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1101af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1102af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
110396f2e892SBill Paul 
110456e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
110596f2e892SBill Paul 
110696f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
110796f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
110896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
110996f2e892SBill Paul 	else
111096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
111196f2e892SBill Paul 
111296f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
111396f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111496f2e892SBill Paul 	else
111596f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
111696f2e892SBill Paul 
1117eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11186817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
111996f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
112096f2e892SBill Paul 			continue;
1121aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
112296f2e892SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1123af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
112496f2e892SBill Paul 	}
1125eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
112696f2e892SBill Paul 
112796f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1128aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1129af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
113096f2e892SBill Paul 	}
113196f2e892SBill Paul 
11328df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
11338df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11348df1ebe9SMarcel Moolenaar 	sp[39] = DC_SP_MAC(eaddr[0]);
11358df1ebe9SMarcel Moolenaar 	sp[40] = DC_SP_MAC(eaddr[1]);
11368df1ebe9SMarcel Moolenaar 	sp[41] = DC_SP_MAC(eaddr[2]);
113796f2e892SBill Paul 
1138af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
11395f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
114096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
114196f2e892SBill Paul 
114296f2e892SBill Paul 	/*
114396f2e892SBill Paul 	 * The PNIC takes an exceedingly long time to process its
114496f2e892SBill Paul 	 * setup frame; wait 10ms after posting the setup frame
114596f2e892SBill Paul 	 * before proceeding, just so it has time to swallow its
114696f2e892SBill Paul 	 * medicine.
114796f2e892SBill Paul 	 */
114896f2e892SBill Paul 	DELAY(10000);
114996f2e892SBill Paul 
1150b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
115196f2e892SBill Paul }
115296f2e892SBill Paul 
11532c876e15SPoul-Henning Kamp static void
11540934f18aSMaxime Henrion dc_setfilt_admtek(struct dc_softc *sc)
115596f2e892SBill Paul {
11562e3d4b79SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
115796f2e892SBill Paul 	struct ifnet *ifp;
11580934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
115996f2e892SBill Paul 	int h = 0;
1160ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
116196f2e892SBill Paul 
1162fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
116396f2e892SBill Paul 
11640934f18aSMaxime Henrion 	/* Init our MAC address. */
11658df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
11662e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR0, eaddr[3] << 24 | eaddr[2] << 16 |
11672e3d4b79SPyun YongHyeon 	    eaddr[1] << 8 | eaddr[0]);
11682e3d4b79SPyun YongHyeon 	CSR_WRITE_4(sc, DC_AL_PAR1, eaddr[5] << 8 | eaddr[4]);
116996f2e892SBill Paul 
117096f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
117196f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
117296f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
117396f2e892SBill Paul 	else
117496f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
117596f2e892SBill Paul 
117696f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
117796f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
117896f2e892SBill Paul 	else
117996f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
118096f2e892SBill Paul 
11810934f18aSMaxime Henrion 	/* First, zot all the existing hash bits. */
118296f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, 0);
118396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, 0);
118496f2e892SBill Paul 
118596f2e892SBill Paul 	/*
118696f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
118796f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
118896f2e892SBill Paul 	 */
118996f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
119096f2e892SBill Paul 		return;
119196f2e892SBill Paul 
11920934f18aSMaxime Henrion 	/* Now program new ones. */
1193eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
11946817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
119596f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
119696f2e892SBill Paul 			continue;
1197acc1bcccSMartin Blapp 		if (DC_IS_CENTAUR(sc))
1198aa825502SDavid E. O'Brien 			h = dc_mchash_le(sc,
1199aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1200acc1bcccSMartin Blapp 		else
1201aa825502SDavid E. O'Brien 			h = dc_mchash_be(
1202aa825502SDavid E. O'Brien 			    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
120396f2e892SBill Paul 		if (h < 32)
120496f2e892SBill Paul 			hashes[0] |= (1 << h);
120596f2e892SBill Paul 		else
120696f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
120796f2e892SBill Paul 	}
1208eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
120996f2e892SBill Paul 
121096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR0, hashes[0]);
121196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AL_MAR1, hashes[1]);
121296f2e892SBill Paul }
121396f2e892SBill Paul 
12142c876e15SPoul-Henning Kamp static void
12150934f18aSMaxime Henrion dc_setfilt_asix(struct dc_softc *sc)
121696f2e892SBill Paul {
12178df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
121896f2e892SBill Paul 	struct ifnet *ifp;
12190934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
122096f2e892SBill Paul 	int h = 0;
1221ee320f98SPyun YongHyeon 	uint32_t hashes[2] = { 0, 0 };
122296f2e892SBill Paul 
1223fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
122496f2e892SBill Paul 
12258df1ebe9SMarcel Moolenaar 	/* Init our MAC address. */
12268df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
122796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR0);
12288df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[0]);
122996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_PAR1);
12308df1ebe9SMarcel Moolenaar 	CSR_WRITE_4(sc, DC_AX_FILTDATA, eaddr[1]);
123196f2e892SBill Paul 
123296f2e892SBill Paul 	/* If we want promiscuous mode, set the allframes bit. */
123396f2e892SBill Paul 	if (ifp->if_flags & IFF_PROMISC)
123496f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
123596f2e892SBill Paul 	else
123696f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
123796f2e892SBill Paul 
123896f2e892SBill Paul 	if (ifp->if_flags & IFF_ALLMULTI)
123996f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
124096f2e892SBill Paul 	else
124196f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
124296f2e892SBill Paul 
124396f2e892SBill Paul 	/*
124496f2e892SBill Paul 	 * The ASIX chip has a special bit to enable reception
124596f2e892SBill Paul 	 * of broadcast frames.
124696f2e892SBill Paul 	 */
124796f2e892SBill Paul 	if (ifp->if_flags & IFF_BROADCAST)
124896f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
124996f2e892SBill Paul 	else
125096f2e892SBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_AX_NETCFG_RX_BROAD);
125196f2e892SBill Paul 
125296f2e892SBill Paul 	/* first, zot all the existing hash bits */
125396f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
125496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
125596f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
125696f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, 0);
125796f2e892SBill Paul 
125896f2e892SBill Paul 	/*
125996f2e892SBill Paul 	 * If we're already in promisc or allmulti mode, we
126096f2e892SBill Paul 	 * don't have to bother programming the multicast filter.
126196f2e892SBill Paul 	 */
126296f2e892SBill Paul 	if (ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI))
126396f2e892SBill Paul 		return;
126496f2e892SBill Paul 
126596f2e892SBill Paul 	/* now program new ones */
1266eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
12676817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
126896f2e892SBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
126996f2e892SBill Paul 			continue;
1270aa825502SDavid E. O'Brien 		h = dc_mchash_be(LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
127196f2e892SBill Paul 		if (h < 32)
127296f2e892SBill Paul 			hashes[0] |= (1 << h);
127396f2e892SBill Paul 		else
127496f2e892SBill Paul 			hashes[1] |= (1 << (h - 32));
127596f2e892SBill Paul 	}
1276eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
127796f2e892SBill Paul 
127896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR0);
127996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[0]);
128096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTIDX, DC_AX_FILTIDX_MAR1);
128196f2e892SBill Paul 	CSR_WRITE_4(sc, DC_AX_FILTDATA, hashes[1]);
128296f2e892SBill Paul }
128396f2e892SBill Paul 
12842c876e15SPoul-Henning Kamp static void
12850934f18aSMaxime Henrion dc_setfilt_xircom(struct dc_softc *sc)
1286feb78939SJonathan Chen {
12878df1ebe9SMarcel Moolenaar 	uint16_t eaddr[(ETHER_ADDR_LEN+1)/2];
12880934f18aSMaxime Henrion 	struct ifnet *ifp;
12890934f18aSMaxime Henrion 	struct ifmultiaddr *ifma;
1290feb78939SJonathan Chen 	struct dc_desc *sframe;
1291ee320f98SPyun YongHyeon 	uint32_t h, *sp;
1292feb78939SJonathan Chen 	int i;
1293feb78939SJonathan Chen 
1294fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
1295feb78939SJonathan Chen 	DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
1296feb78939SJonathan Chen 
1297feb78939SJonathan Chen 	i = sc->dc_cdata.dc_tx_prod;
1298feb78939SJonathan Chen 	DC_INC(sc->dc_cdata.dc_tx_prod, DC_TX_LIST_CNT);
1299feb78939SJonathan Chen 	sc->dc_cdata.dc_tx_cnt++;
13005f14ee23SPyun YongHyeon 	sframe = &sc->dc_ldata.dc_tx_list[i];
130156e5e7aeSMaxime Henrion 	sp = sc->dc_cdata.dc_sbuf;
13020934f18aSMaxime Henrion 	bzero(sp, DC_SFRAME_LEN);
1303feb78939SJonathan Chen 
13045f14ee23SPyun YongHyeon 	sframe->dc_data = htole32(DC_ADDR_LO(sc->dc_saddr));
1305af4358c7SMaxime Henrion 	sframe->dc_ctl = htole32(DC_SFRAME_LEN | DC_TXCTL_SETUP |
1306af4358c7SMaxime Henrion 	    DC_TXCTL_TLINK | DC_FILTER_HASHPERF | DC_TXCTL_FINT);
1307feb78939SJonathan Chen 
130856e5e7aeSMaxime Henrion 	sc->dc_cdata.dc_tx_chain[i] = (struct mbuf *)sc->dc_cdata.dc_sbuf;
1309feb78939SJonathan Chen 
1310feb78939SJonathan Chen 	/* If we want promiscuous mode, set the allframes bit. */
1311feb78939SJonathan Chen 	if (ifp->if_flags & IFF_PROMISC)
1312feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1313feb78939SJonathan Chen 	else
1314feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_PROMISC);
1315feb78939SJonathan Chen 
1316feb78939SJonathan Chen 	if (ifp->if_flags & IFF_ALLMULTI)
1317feb78939SJonathan Chen 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1318feb78939SJonathan Chen 	else
1319feb78939SJonathan Chen 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_RX_ALLMULTI);
1320feb78939SJonathan Chen 
1321eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
13226817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1323feb78939SJonathan Chen 		if (ifma->ifma_addr->sa_family != AF_LINK)
1324feb78939SJonathan Chen 			continue;
1325aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc,
13261d5e5310SBill Paul 		    LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
1327af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1328feb78939SJonathan Chen 	}
1329eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
1330feb78939SJonathan Chen 
1331feb78939SJonathan Chen 	if (ifp->if_flags & IFF_BROADCAST) {
1332aa825502SDavid E. O'Brien 		h = dc_mchash_le(sc, ifp->if_broadcastaddr);
1333af4358c7SMaxime Henrion 		sp[h >> 4] |= htole32(1 << (h & 0xF));
1334feb78939SJonathan Chen 	}
1335feb78939SJonathan Chen 
13368df1ebe9SMarcel Moolenaar 	/* Set our MAC address. */
13378df1ebe9SMarcel Moolenaar 	bcopy(IF_LLADDR(sc->dc_ifp), eaddr, ETHER_ADDR_LEN);
13388df1ebe9SMarcel Moolenaar 	sp[0] = DC_SP_MAC(eaddr[0]);
13398df1ebe9SMarcel Moolenaar 	sp[1] = DC_SP_MAC(eaddr[1]);
13408df1ebe9SMarcel Moolenaar 	sp[2] = DC_SP_MAC(eaddr[2]);
1341feb78939SJonathan Chen 
1342feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
1343feb78939SJonathan Chen 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
1344af4358c7SMaxime Henrion 	sframe->dc_status = htole32(DC_TXSTAT_OWN);
13455f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_stag, sc->dc_smap, BUS_DMASYNC_PREWRITE);
1346feb78939SJonathan Chen 	CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
1347feb78939SJonathan Chen 
1348feb78939SJonathan Chen 	/*
13490934f18aSMaxime Henrion 	 * Wait some time...
1350feb78939SJonathan Chen 	 */
1351feb78939SJonathan Chen 	DELAY(1000);
1352feb78939SJonathan Chen 
1353b1d16143SMarius Strobl 	sc->dc_wdog_timer = 5;
1354feb78939SJonathan Chen }
1355feb78939SJonathan Chen 
1356e3d2833aSAlfred Perlstein static void
13570934f18aSMaxime Henrion dc_setfilt(struct dc_softc *sc)
135896f2e892SBill Paul {
13590934f18aSMaxime Henrion 
136096f2e892SBill Paul 	if (DC_IS_INTEL(sc) || DC_IS_MACRONIX(sc) || DC_IS_PNIC(sc) ||
13611af8bec7SBill Paul 	    DC_IS_PNICII(sc) || DC_IS_DAVICOM(sc) || DC_IS_CONEXANT(sc))
136296f2e892SBill Paul 		dc_setfilt_21143(sc);
136396f2e892SBill Paul 
136496f2e892SBill Paul 	if (DC_IS_ASIX(sc))
136596f2e892SBill Paul 		dc_setfilt_asix(sc);
136696f2e892SBill Paul 
136796f2e892SBill Paul 	if (DC_IS_ADMTEK(sc))
136896f2e892SBill Paul 		dc_setfilt_admtek(sc);
136996f2e892SBill Paul 
1370feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc))
1371feb78939SJonathan Chen 		dc_setfilt_xircom(sc);
137296f2e892SBill Paul }
137396f2e892SBill Paul 
1374e3d2833aSAlfred Perlstein static void
13751da7683aSPyun YongHyeon dc_netcfg_wait(struct dc_softc *sc)
137696f2e892SBill Paul {
1377ee320f98SPyun YongHyeon 	uint32_t isr;
13781da7683aSPyun YongHyeon 	int i;
137996f2e892SBill Paul 
138096f2e892SBill Paul 	for (i = 0; i < DC_TIMEOUT; i++) {
138196f2e892SBill Paul 		isr = CSR_READ_4(sc, DC_ISR);
1382d467c136SBill Paul 		if (isr & DC_ISR_TX_IDLE &&
1383351267c1SMartin Blapp 		    ((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1384351267c1SMartin Blapp 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT))
138596f2e892SBill Paul 			break;
1386d467c136SBill Paul 		DELAY(10);
138796f2e892SBill Paul 	}
13887a6fab66SWarner Losh 	if (i == DC_TIMEOUT && bus_child_present(sc->dc_dev)) {
1389432120f2SMarius Strobl 		if (!(isr & DC_ISR_TX_IDLE) && !DC_IS_ASIX(sc))
13906b9f5c94SGleb Smirnoff 			device_printf(sc->dc_dev,
13911da7683aSPyun YongHyeon 			    "%s: failed to force tx to idle state\n", __func__);
1392432120f2SMarius Strobl 		if (!((isr & DC_ISR_RX_STATE) == DC_RXSTATE_STOPPED ||
1393432120f2SMarius Strobl 		    (isr & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
1394d0d67284SMarius Strobl 		    !DC_HAS_BROKEN_RXSTATE(sc))
1395432120f2SMarius Strobl 			device_printf(sc->dc_dev,
13961da7683aSPyun YongHyeon 			    "%s: failed to force rx to idle state\n", __func__);
1397432120f2SMarius Strobl 	}
139896f2e892SBill Paul }
139996f2e892SBill Paul 
14001da7683aSPyun YongHyeon /*
14011da7683aSPyun YongHyeon  * In order to fiddle with the 'full-duplex' and '100Mbps' bits in
14021da7683aSPyun YongHyeon  * the netconfig register, we first have to put the transmit and/or
14031da7683aSPyun YongHyeon  * receive logic in the idle state.
14041da7683aSPyun YongHyeon  */
14051da7683aSPyun YongHyeon static void
14061da7683aSPyun YongHyeon dc_setcfg(struct dc_softc *sc, int media)
14071da7683aSPyun YongHyeon {
14081da7683aSPyun YongHyeon 	int restart = 0, watchdogreg;
14091da7683aSPyun YongHyeon 
14101da7683aSPyun YongHyeon 	if (IFM_SUBTYPE(media) == IFM_NONE)
14111da7683aSPyun YongHyeon 		return;
14121da7683aSPyun YongHyeon 
14131da7683aSPyun YongHyeon 	if (CSR_READ_4(sc, DC_NETCFG) & (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON)) {
14141da7683aSPyun YongHyeon 		restart = 1;
14151da7683aSPyun YongHyeon 		DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_TX_ON | DC_NETCFG_RX_ON));
14161da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
14171da7683aSPyun YongHyeon 	}
14181da7683aSPyun YongHyeon 
141996f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_100_TX) {
1420042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1421042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
142296f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
1423bf645417SBill Paul 			if (DC_IS_INTEL(sc)) {
14240934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14258273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14268273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14278273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14284c2efe27SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
1429bf645417SBill Paul 			} else {
1430bf645417SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
1431bf645417SBill Paul 			}
143296f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
143396f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
143496f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
143596f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
143696f2e892SBill Paul 				    DC_NETCFG_SCRAMBLER));
143788d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
143896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
143996f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
144096f2e892SBill Paul 		} else {
144196f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
144296f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_SPEEDSEL);
144396f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
144496f2e892SBill Paul 				DC_SETBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
144596f2e892SBill Paul 			}
1446318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1447318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
1448318b02fdSBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
144996f2e892SBill Paul 		}
145096f2e892SBill Paul 	}
145196f2e892SBill Paul 
145296f2e892SBill Paul 	if (IFM_SUBTYPE(media) == IFM_10_T) {
1453042c8f6eSBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_SPEEDSEL);
1454042c8f6eSBill Paul 		DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_HEARTBEAT);
145596f2e892SBill Paul 		if (sc->dc_pmode == DC_PMODE_MII) {
14560934f18aSMaxime Henrion 			/* There's a write enable bit here that reads as 1. */
14574c2efe27SBill Paul 			if (DC_IS_INTEL(sc)) {
14588273d5f8SBill Paul 				watchdogreg = CSR_READ_4(sc, DC_WATCHDOG);
14598273d5f8SBill Paul 				watchdogreg &= ~DC_WDOG_CTLWREN;
14608273d5f8SBill Paul 				watchdogreg |= DC_WDOG_JABBERDIS;
14618273d5f8SBill Paul 				CSR_WRITE_4(sc, DC_WATCHDOG, watchdogreg);
14624c2efe27SBill Paul 			} else {
14634c2efe27SBill Paul 				DC_SETBIT(sc, DC_WATCHDOG, DC_WDOG_JABBERDIS);
14644c2efe27SBill Paul 			}
146596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, (DC_NETCFG_PCS |
146696f2e892SBill Paul 			    DC_NETCFG_PORTSEL | DC_NETCFG_SCRAMBLER));
146796f2e892SBill Paul 			if (sc->dc_type == DC_TYPE_98713)
146896f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
146988d739dcSBill Paul 			if (!DC_IS_DAVICOM(sc))
147096f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
147196f2e892SBill Paul 			DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
147296f2e892SBill Paul 		} else {
147396f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
147496f2e892SBill Paul 				DC_PN_GPIO_CLRBIT(sc, DC_PN_GPIO_SPEEDSEL);
147596f2e892SBill Paul 				DC_PN_GPIO_SETBIT(sc, DC_PN_GPIO_100TX_LOOP);
147696f2e892SBill Paul 				DC_CLRBIT(sc, DC_PN_NWAY, DC_PN_NWAY_SPEEDSEL);
147796f2e892SBill Paul 			}
147896f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PORTSEL);
1479318b02fdSBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_PCS);
148096f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_SCRAMBLER);
14815c1cfac4SBill Paul 			if (DC_IS_INTEL(sc)) {
14825c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_SIARESET, DC_SIA_RESET);
14835c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL, 0xFFFF);
14845c1cfac4SBill Paul 				if ((media & IFM_GMASK) == IFM_FDX)
14855c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3D);
14865c1cfac4SBill Paul 				else
14875c1cfac4SBill Paul 					DC_SETBIT(sc, DC_10BTCTRL, 0x7F3F);
14885c1cfac4SBill Paul 				DC_SETBIT(sc, DC_SIARESET, DC_SIA_RESET);
14895c1cfac4SBill Paul 				DC_CLRBIT(sc, DC_10BTCTRL,
14905c1cfac4SBill Paul 				    DC_TCTL_AUTONEGENBL);
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);
1562d314ebf5SPyun YongHyeon 		CSR_WRITE_4(sc, DC_10BTCTRL, 0xFFFFFFFF);
15635c1cfac4SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
15645c1cfac4SBill Paul 	}
156596f2e892SBill Paul }
156696f2e892SBill Paul 
1567ebc284ccSMarius Strobl static const struct dc_type *
15680934f18aSMaxime Henrion dc_devtype(device_t dev)
156996f2e892SBill Paul {
1570ebc284ccSMarius Strobl 	const struct dc_type *t;
1571ee320f98SPyun YongHyeon 	uint32_t devid;
1572ee320f98SPyun YongHyeon 	uint8_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 {
1599ebc284ccSMarius Strobl 	const 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;
1615ee320f98SPyun YongHyeon 	uint8_t *p;
16165c1cfac4SBill Paul 	int i;
1617ee320f98SPyun YongHyeon 	uint32_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 
1641abe4e865SPyun YongHyeon static int
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);
1647abe4e865SPyun YongHyeon 	if (m == NULL) {
1648abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1649abe4e865SPyun YongHyeon 		return (ENOMEM);
1650abe4e865SPyun YongHyeon 	}
165187f4fa15SMartin Blapp 	switch (l->dc_sia_code & ~DC_SIA_CODE_EXT) {
165287f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT:
16535c1cfac4SBill Paul 		m->dc_media = IFM_10_T;
165487f4fa15SMartin Blapp 		break;
165587f4fa15SMartin Blapp 	case DC_SIA_CODE_10BT_FDX:
16565c1cfac4SBill Paul 		m->dc_media = IFM_10_T | IFM_FDX;
165787f4fa15SMartin Blapp 		break;
165887f4fa15SMartin Blapp 	case DC_SIA_CODE_10B2:
16595c1cfac4SBill Paul 		m->dc_media = IFM_10_2;
166087f4fa15SMartin Blapp 		break;
166187f4fa15SMartin Blapp 	case DC_SIA_CODE_10B5:
16625c1cfac4SBill Paul 		m->dc_media = IFM_10_5;
166387f4fa15SMartin Blapp 		break;
166487f4fa15SMartin Blapp 	default:
166587f4fa15SMartin Blapp 		break;
166687f4fa15SMartin Blapp 	}
16675c1cfac4SBill Paul 
166887f4fa15SMartin Blapp 	/*
166987f4fa15SMartin Blapp 	 * We need to ignore CSR13, CSR14, CSR15 for SIA mode.
167087f4fa15SMartin Blapp 	 * Things apparently already work for cards that do
167187f4fa15SMartin Blapp 	 * supply Media Specific Data.
167287f4fa15SMartin Blapp 	 */
167387f4fa15SMartin Blapp 	if (l->dc_sia_code & DC_SIA_CODE_EXT) {
16745c1cfac4SBill Paul 		m->dc_gp_len = 2;
167587f4fa15SMartin Blapp 		m->dc_gp_ptr =
1676ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_ext.dc_sia_gpio_ctl;
167787f4fa15SMartin Blapp 	} else {
167887f4fa15SMartin Blapp 		m->dc_gp_len = 2;
167987f4fa15SMartin Blapp 		m->dc_gp_ptr =
1680ee320f98SPyun YongHyeon 		(uint8_t *)&l->dc_un.dc_sia_noext.dc_sia_gpio_ctl;
168187f4fa15SMartin Blapp 	}
16825c1cfac4SBill Paul 
16835c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
16845c1cfac4SBill Paul 	sc->dc_mi = m;
16855c1cfac4SBill Paul 
16865c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SIA;
1687abe4e865SPyun YongHyeon 	return (0);
16885c1cfac4SBill Paul }
16895c1cfac4SBill Paul 
1690abe4e865SPyun YongHyeon static int
16910934f18aSMaxime Henrion dc_decode_leaf_sym(struct dc_softc *sc, struct dc_eblock_sym *l)
16925c1cfac4SBill Paul {
16935c1cfac4SBill Paul 	struct dc_mediainfo *m;
16945c1cfac4SBill Paul 
16950934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1696abe4e865SPyun YongHyeon 	if (m == NULL) {
1697abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1698abe4e865SPyun YongHyeon 		return (ENOMEM);
1699abe4e865SPyun YongHyeon 	}
17005c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT)
17015c1cfac4SBill Paul 		m->dc_media = IFM_100_TX;
17025c1cfac4SBill Paul 
17035c1cfac4SBill Paul 	if (l->dc_sym_code == DC_SYM_CODE_100BT_FDX)
17045c1cfac4SBill Paul 		m->dc_media = IFM_100_TX | IFM_FDX;
17055c1cfac4SBill Paul 
17065c1cfac4SBill Paul 	m->dc_gp_len = 2;
1707ee320f98SPyun YongHyeon 	m->dc_gp_ptr = (uint8_t *)&l->dc_sym_gpio_ctl;
17085c1cfac4SBill Paul 
17095c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17105c1cfac4SBill Paul 	sc->dc_mi = m;
17115c1cfac4SBill Paul 
17125c1cfac4SBill Paul 	sc->dc_pmode = DC_PMODE_SYM;
1713abe4e865SPyun YongHyeon 	return (0);
17145c1cfac4SBill Paul }
17155c1cfac4SBill Paul 
1716abe4e865SPyun YongHyeon static int
17170934f18aSMaxime Henrion dc_decode_leaf_mii(struct dc_softc *sc, struct dc_eblock_mii *l)
17185c1cfac4SBill Paul {
17195c1cfac4SBill Paul 	struct dc_mediainfo *m;
1720ee320f98SPyun YongHyeon 	uint8_t *p;
17215c1cfac4SBill Paul 
17220934f18aSMaxime Henrion 	m = malloc(sizeof(struct dc_mediainfo), M_DEVBUF, M_NOWAIT | M_ZERO);
1723abe4e865SPyun YongHyeon 	if (m == NULL) {
1724abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate mediainfo\n");
1725abe4e865SPyun YongHyeon 		return (ENOMEM);
1726abe4e865SPyun YongHyeon 	}
17275c1cfac4SBill Paul 	/* We abuse IFM_AUTO to represent MII. */
17285c1cfac4SBill Paul 	m->dc_media = IFM_AUTO;
17295c1cfac4SBill Paul 	m->dc_gp_len = l->dc_gpr_len;
17305c1cfac4SBill Paul 
1731ee320f98SPyun YongHyeon 	p = (uint8_t *)l;
17325c1cfac4SBill Paul 	p += sizeof(struct dc_eblock_mii);
17335c1cfac4SBill Paul 	m->dc_gp_ptr = p;
17345c1cfac4SBill Paul 	p += 2 * l->dc_gpr_len;
17355c1cfac4SBill Paul 	m->dc_reset_len = *p;
17365c1cfac4SBill Paul 	p++;
17375c1cfac4SBill Paul 	m->dc_reset_ptr = p;
17385c1cfac4SBill Paul 
17395c1cfac4SBill Paul 	m->dc_next = sc->dc_mi;
17405c1cfac4SBill Paul 	sc->dc_mi = m;
1741abe4e865SPyun YongHyeon 	return (0);
17425c1cfac4SBill Paul }
17435c1cfac4SBill Paul 
1744abe4e865SPyun YongHyeon static int
17450934f18aSMaxime Henrion dc_read_srom(struct dc_softc *sc, int bits)
17463097aa70SWarner Losh {
17473097aa70SWarner Losh 	int size;
17483097aa70SWarner Losh 
1749abe4e865SPyun YongHyeon 	size = DC_ROM_SIZE(bits);
17503097aa70SWarner Losh 	sc->dc_srom = malloc(size, M_DEVBUF, M_NOWAIT);
1751abe4e865SPyun YongHyeon 	if (sc->dc_srom == NULL) {
1752abe4e865SPyun YongHyeon 		device_printf(sc->dc_dev, "Could not allocate SROM buffer\n");
1753abe4e865SPyun YongHyeon 		return (ENOMEM);
1754abe4e865SPyun YongHyeon 	}
17553097aa70SWarner Losh 	dc_read_eeprom(sc, (caddr_t)sc->dc_srom, 0, (size / 2), 0);
1756abe4e865SPyun YongHyeon 	return (0);
17573097aa70SWarner Losh }
17583097aa70SWarner Losh 
1759abe4e865SPyun YongHyeon static int
17600934f18aSMaxime Henrion dc_parse_21143_srom(struct dc_softc *sc)
17615c1cfac4SBill Paul {
17625c1cfac4SBill Paul 	struct dc_leaf_hdr *lhdr;
17635c1cfac4SBill Paul 	struct dc_eblock_hdr *hdr;
1764abe4e865SPyun YongHyeon 	int error, have_mii, i, loff;
17655c1cfac4SBill Paul 	char *ptr;
17665c1cfac4SBill Paul 
1767f956e0b3SMartin Blapp 	have_mii = 0;
17685c1cfac4SBill Paul 	loff = sc->dc_srom[27];
17695c1cfac4SBill Paul 	lhdr = (struct dc_leaf_hdr *)&(sc->dc_srom[loff]);
17705c1cfac4SBill Paul 
17715c1cfac4SBill Paul 	ptr = (char *)lhdr;
17725c1cfac4SBill Paul 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1773f956e0b3SMartin Blapp 	/*
1774f956e0b3SMartin Blapp 	 * Look if we got a MII media block.
1775f956e0b3SMartin Blapp 	 */
1776f956e0b3SMartin Blapp 	for (i = 0; i < lhdr->dc_mcnt; i++) {
1777f956e0b3SMartin Blapp 		hdr = (struct dc_eblock_hdr *)ptr;
1778f956e0b3SMartin Blapp 		if (hdr->dc_type == DC_EBLOCK_MII)
1779f956e0b3SMartin Blapp 		    have_mii++;
1780f956e0b3SMartin Blapp 
1781f956e0b3SMartin Blapp 		ptr += (hdr->dc_len & 0x7F);
1782f956e0b3SMartin Blapp 		ptr++;
1783f956e0b3SMartin Blapp 	}
1784f956e0b3SMartin Blapp 
1785f956e0b3SMartin Blapp 	/*
1786f956e0b3SMartin Blapp 	 * Do the same thing again. Only use SIA and SYM media
1787f956e0b3SMartin Blapp 	 * blocks if no MII media block is available.
1788f956e0b3SMartin Blapp 	 */
1789f956e0b3SMartin Blapp 	ptr = (char *)lhdr;
1790f956e0b3SMartin Blapp 	ptr += sizeof(struct dc_leaf_hdr) - 1;
1791abe4e865SPyun YongHyeon 	error = 0;
17925c1cfac4SBill Paul 	for (i = 0; i < lhdr->dc_mcnt; i++) {
17935c1cfac4SBill Paul 		hdr = (struct dc_eblock_hdr *)ptr;
17945c1cfac4SBill Paul 		switch (hdr->dc_type) {
17955c1cfac4SBill Paul 		case DC_EBLOCK_MII:
1796abe4e865SPyun YongHyeon 			error = dc_decode_leaf_mii(sc, (struct dc_eblock_mii *)hdr);
17975c1cfac4SBill Paul 			break;
17985c1cfac4SBill Paul 		case DC_EBLOCK_SIA:
1799f956e0b3SMartin Blapp 			if (! have_mii)
1800abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sia(sc,
1801f956e0b3SMartin Blapp 				    (struct dc_eblock_sia *)hdr);
18025c1cfac4SBill Paul 			break;
18035c1cfac4SBill Paul 		case DC_EBLOCK_SYM:
1804f956e0b3SMartin Blapp 			if (! have_mii)
1805abe4e865SPyun YongHyeon 				error = dc_decode_leaf_sym(sc,
1806f956e0b3SMartin Blapp 				    (struct dc_eblock_sym *)hdr);
18075c1cfac4SBill Paul 			break;
18085c1cfac4SBill Paul 		default:
18095c1cfac4SBill Paul 			/* Don't care. Yet. */
18105c1cfac4SBill Paul 			break;
18115c1cfac4SBill Paul 		}
18125c1cfac4SBill Paul 		ptr += (hdr->dc_len & 0x7F);
18135c1cfac4SBill Paul 		ptr++;
18145c1cfac4SBill Paul 	}
1815abe4e865SPyun YongHyeon 	return (error);
18165c1cfac4SBill Paul }
18175c1cfac4SBill Paul 
181856e5e7aeSMaxime Henrion static void
181956e5e7aeSMaxime Henrion dc_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
182056e5e7aeSMaxime Henrion {
18215f14ee23SPyun YongHyeon 	bus_addr_t *paddr;
182256e5e7aeSMaxime Henrion 
1823ebc284ccSMarius Strobl 	KASSERT(nseg == 1,
1824ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
182556e5e7aeSMaxime Henrion 	paddr = arg;
182656e5e7aeSMaxime Henrion 	*paddr = segs->ds_addr;
182756e5e7aeSMaxime Henrion }
182856e5e7aeSMaxime Henrion 
18295f14ee23SPyun YongHyeon static int
18305f14ee23SPyun YongHyeon dc_dma_alloc(struct dc_softc *sc)
18315f14ee23SPyun YongHyeon {
18325f14ee23SPyun YongHyeon 	int error, i;
18335f14ee23SPyun YongHyeon 
18345f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(sc->dc_dev), 1, 0,
18355f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL,
18365f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
18375f14ee23SPyun YongHyeon 	    NULL, NULL, &sc->dc_ptag);
18385f14ee23SPyun YongHyeon 	if (error) {
18395f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18405f14ee23SPyun YongHyeon 		    "failed to allocate parent DMA tag\n");
18415f14ee23SPyun YongHyeon 		goto fail;
18425f14ee23SPyun YongHyeon 	}
18435f14ee23SPyun YongHyeon 
18445f14ee23SPyun YongHyeon 	/* Allocate a busdma tag and DMA safe memory for TX/RX descriptors. */
18455f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18465f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_RX_LIST_SZ, 1,
18475f14ee23SPyun YongHyeon 	    DC_RX_LIST_SZ, 0, NULL, NULL, &sc->dc_rx_ltag);
18485f14ee23SPyun YongHyeon 	if (error) {
18495f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX list DMA tag\n");
18505f14ee23SPyun YongHyeon 		goto fail;
18515f14ee23SPyun YongHyeon 	}
18525f14ee23SPyun YongHyeon 
18535f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
18545f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL, DC_TX_LIST_SZ, 1,
18555f14ee23SPyun YongHyeon 	    DC_TX_LIST_SZ, 0, NULL, NULL, &sc->dc_tx_ltag);
18565f14ee23SPyun YongHyeon 	if (error) {
18575f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX list DMA tag\n");
18585f14ee23SPyun YongHyeon 		goto fail;
18595f14ee23SPyun YongHyeon 	}
18605f14ee23SPyun YongHyeon 
18615f14ee23SPyun YongHyeon 	/* RX descriptor list. */
18625f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_rx_ltag,
18635f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_rx_list, BUS_DMA_NOWAIT |
18645f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_rx_lmap);
18655f14ee23SPyun YongHyeon 	if (error) {
18665f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18675f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for RX list\n");
18685f14ee23SPyun YongHyeon 		goto fail;
18695f14ee23SPyun YongHyeon 	}
18705f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_rx_ltag, sc->dc_rx_lmap,
18715f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_rx_list, DC_RX_LIST_SZ, dc_dma_map_addr,
18725f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_rx_list_paddr, BUS_DMA_NOWAIT);
18735f14ee23SPyun YongHyeon 	if (error) {
18745f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18755f14ee23SPyun YongHyeon 		    "failed to load DMA'able memory for RX list\n");
18765f14ee23SPyun YongHyeon 		goto fail;
18775f14ee23SPyun YongHyeon 	}
18785f14ee23SPyun YongHyeon 	/* TX descriptor list. */
18795f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_tx_ltag,
18805f14ee23SPyun YongHyeon 	    (void **)&sc->dc_ldata.dc_tx_list, BUS_DMA_NOWAIT |
18815f14ee23SPyun YongHyeon 	    BUS_DMA_ZERO | BUS_DMA_COHERENT, &sc->dc_tx_lmap);
18825f14ee23SPyun YongHyeon 	if (error) {
18835f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18845f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for TX list\n");
18855f14ee23SPyun YongHyeon 		goto fail;
18865f14ee23SPyun YongHyeon 	}
18875f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_tx_ltag, sc->dc_tx_lmap,
18885f14ee23SPyun YongHyeon 	    sc->dc_ldata.dc_tx_list, DC_TX_LIST_SZ, dc_dma_map_addr,
18895f14ee23SPyun YongHyeon 	    &sc->dc_ldata.dc_tx_list_paddr, BUS_DMA_NOWAIT);
18905f14ee23SPyun YongHyeon 	if (error) {
18915f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
18925f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for TX list\n");
18935f14ee23SPyun YongHyeon 		goto fail;
18945f14ee23SPyun YongHyeon 	}
18955f14ee23SPyun YongHyeon 
18965f14ee23SPyun YongHyeon 	/*
18975f14ee23SPyun YongHyeon 	 * Allocate a busdma tag and DMA safe memory for the multicast
18985f14ee23SPyun YongHyeon 	 * setup frame.
18995f14ee23SPyun YongHyeon 	 */
19005f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_LIST_ALIGN, 0,
19015f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19025f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN + DC_MIN_FRAMELEN, 1, DC_SFRAME_LEN + DC_MIN_FRAMELEN,
19035f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_stag);
19045f14ee23SPyun YongHyeon 	if (error) {
19055f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19065f14ee23SPyun YongHyeon 		    "failed to create DMA tag for setup frame\n");
19075f14ee23SPyun YongHyeon 		goto fail;
19085f14ee23SPyun YongHyeon 	}
19095f14ee23SPyun YongHyeon 	error = bus_dmamem_alloc(sc->dc_stag, (void **)&sc->dc_cdata.dc_sbuf,
19105f14ee23SPyun YongHyeon 	    BUS_DMA_NOWAIT, &sc->dc_smap);
19115f14ee23SPyun YongHyeon 	if (error) {
19125f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19135f14ee23SPyun YongHyeon 		    "failed to allocate DMA'able memory for setup frame\n");
19145f14ee23SPyun YongHyeon 		goto fail;
19155f14ee23SPyun YongHyeon 	}
19165f14ee23SPyun YongHyeon 	error = bus_dmamap_load(sc->dc_stag, sc->dc_smap, sc->dc_cdata.dc_sbuf,
19175f14ee23SPyun YongHyeon 	    DC_SFRAME_LEN, dc_dma_map_addr, &sc->dc_saddr, BUS_DMA_NOWAIT);
19185f14ee23SPyun YongHyeon 	if (error) {
19195f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19205f14ee23SPyun YongHyeon 		    "cannot load DMA'able memory for setup frame\n");
19215f14ee23SPyun YongHyeon 		goto fail;
19225f14ee23SPyun YongHyeon 	}
19235f14ee23SPyun YongHyeon 
19245f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for RX mbufs. */
19255f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, DC_RXBUF_ALIGN, 0,
19265f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19275f14ee23SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->dc_rx_mtag);
19285f14ee23SPyun YongHyeon 	if (error) {
19295f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create RX mbuf tag\n");
19305f14ee23SPyun YongHyeon 		goto fail;
19315f14ee23SPyun YongHyeon 	}
19325f14ee23SPyun YongHyeon 
19335f14ee23SPyun YongHyeon 	/* Allocate a busdma tag for TX mbufs. */
19345f14ee23SPyun YongHyeon 	error = bus_dma_tag_create(sc->dc_ptag, 1, 0,
19355f14ee23SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
19365f14ee23SPyun YongHyeon 	    MCLBYTES * DC_MAXFRAGS, DC_MAXFRAGS, MCLBYTES,
19375f14ee23SPyun YongHyeon 	    0, NULL, NULL, &sc->dc_tx_mtag);
19385f14ee23SPyun YongHyeon 	if (error) {
19395f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev, "failed to create TX mbuf tag\n");
19405f14ee23SPyun YongHyeon 		goto fail;
19415f14ee23SPyun YongHyeon 	}
19425f14ee23SPyun YongHyeon 
19435f14ee23SPyun YongHyeon 	/* Create the TX/RX busdma maps. */
19445f14ee23SPyun YongHyeon 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
19455f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_tx_mtag, 0,
19465f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_tx_map[i]);
19475f14ee23SPyun YongHyeon 		if (error) {
19485f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19495f14ee23SPyun YongHyeon 			    "failed to create TX mbuf dmamap\n");
19505f14ee23SPyun YongHyeon 			goto fail;
19515f14ee23SPyun YongHyeon 		}
19525f14ee23SPyun YongHyeon 	}
19535f14ee23SPyun YongHyeon 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
19545f14ee23SPyun YongHyeon 		error = bus_dmamap_create(sc->dc_rx_mtag, 0,
19555f14ee23SPyun YongHyeon 		    &sc->dc_cdata.dc_rx_map[i]);
19565f14ee23SPyun YongHyeon 		if (error) {
19575f14ee23SPyun YongHyeon 			device_printf(sc->dc_dev,
19585f14ee23SPyun YongHyeon 			    "failed to create RX mbuf dmamap\n");
19595f14ee23SPyun YongHyeon 			goto fail;
19605f14ee23SPyun YongHyeon 		}
19615f14ee23SPyun YongHyeon 	}
19625f14ee23SPyun YongHyeon 	error = bus_dmamap_create(sc->dc_rx_mtag, 0, &sc->dc_sparemap);
19635f14ee23SPyun YongHyeon 	if (error) {
19645f14ee23SPyun YongHyeon 		device_printf(sc->dc_dev,
19655f14ee23SPyun YongHyeon 		    "failed to create spare RX mbuf dmamap\n");
19665f14ee23SPyun YongHyeon 		goto fail;
19675f14ee23SPyun YongHyeon 	}
19685f14ee23SPyun YongHyeon 
19695f14ee23SPyun YongHyeon fail:
19705f14ee23SPyun YongHyeon 	return (error);
19715f14ee23SPyun YongHyeon }
19725f14ee23SPyun YongHyeon 
19735f14ee23SPyun YongHyeon static void
19745f14ee23SPyun YongHyeon dc_dma_free(struct dc_softc *sc)
19755f14ee23SPyun YongHyeon {
19765f14ee23SPyun YongHyeon 	int i;
19775f14ee23SPyun YongHyeon 
19785f14ee23SPyun YongHyeon 	/* RX buffers. */
19795f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19805f14ee23SPyun YongHyeon 		for (i = 0; i < DC_RX_LIST_CNT; i++) {
19815f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_rx_map[i] != NULL)
19825f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_rx_mtag,
19835f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_rx_map[i]);
19845f14ee23SPyun YongHyeon 		}
19855f14ee23SPyun YongHyeon 		if (sc->dc_sparemap != NULL)
19865f14ee23SPyun YongHyeon 			bus_dmamap_destroy(sc->dc_rx_mtag, sc->dc_sparemap);
19875f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_mtag);
19885f14ee23SPyun YongHyeon 	}
19895f14ee23SPyun YongHyeon 
19905f14ee23SPyun YongHyeon 	/* TX buffers. */
19915f14ee23SPyun YongHyeon 	if (sc->dc_rx_mtag != NULL) {
19925f14ee23SPyun YongHyeon 		for (i = 0; i < DC_TX_LIST_CNT; i++) {
19935f14ee23SPyun YongHyeon 			if (sc->dc_cdata.dc_tx_map[i] != NULL)
19945f14ee23SPyun YongHyeon 				bus_dmamap_destroy(sc->dc_tx_mtag,
19955f14ee23SPyun YongHyeon 				    sc->dc_cdata.dc_tx_map[i]);
19965f14ee23SPyun YongHyeon 		}
19975f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_mtag);
19985f14ee23SPyun YongHyeon 	}
19995f14ee23SPyun YongHyeon 
20005f14ee23SPyun YongHyeon 	/* RX descriptor list. */
20015f14ee23SPyun YongHyeon 	if (sc->dc_rx_ltag) {
20025f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL)
20035f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_ltag, sc->dc_rx_lmap);
20045f14ee23SPyun YongHyeon 		if (sc->dc_rx_lmap != NULL && sc->dc_ldata.dc_rx_list != NULL)
20055f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_rx_ltag, sc->dc_ldata.dc_rx_list,
20065f14ee23SPyun YongHyeon 			    sc->dc_rx_lmap);
20075f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_rx_ltag);
20085f14ee23SPyun YongHyeon 	}
20095f14ee23SPyun YongHyeon 
20105f14ee23SPyun YongHyeon 	/* TX descriptor list. */
20115f14ee23SPyun YongHyeon 	if (sc->dc_tx_ltag) {
20125f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL)
20135f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_ltag, sc->dc_tx_lmap);
20145f14ee23SPyun YongHyeon 		if (sc->dc_tx_lmap != NULL && sc->dc_ldata.dc_tx_list != NULL)
20155f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_tx_ltag, sc->dc_ldata.dc_tx_list,
20165f14ee23SPyun YongHyeon 			    sc->dc_tx_lmap);
20175f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_tx_ltag);
20185f14ee23SPyun YongHyeon 	}
20195f14ee23SPyun YongHyeon 
20205f14ee23SPyun YongHyeon 	/* multicast setup frame. */
20215f14ee23SPyun YongHyeon 	if (sc->dc_stag) {
20225f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL)
20235f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_stag, sc->dc_smap);
20245f14ee23SPyun YongHyeon 		if (sc->dc_smap != NULL && sc->dc_cdata.dc_sbuf != NULL)
20255f14ee23SPyun YongHyeon 			bus_dmamem_free(sc->dc_stag, sc->dc_cdata.dc_sbuf,
20265f14ee23SPyun YongHyeon 			    sc->dc_smap);
20275f14ee23SPyun YongHyeon 		bus_dma_tag_destroy(sc->dc_stag);
20285f14ee23SPyun YongHyeon 	}
20295f14ee23SPyun YongHyeon }
20305f14ee23SPyun YongHyeon 
203196f2e892SBill Paul /*
203296f2e892SBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
203396f2e892SBill Paul  * setup and ethernet/BPF attach.
203496f2e892SBill Paul  */
2035e3d2833aSAlfred Perlstein static int
20360934f18aSMaxime Henrion dc_attach(device_t dev)
203796f2e892SBill Paul {
20388df1ebe9SMarcel Moolenaar 	uint32_t eaddr[(ETHER_ADDR_LEN+3)/4];
2039ee320f98SPyun YongHyeon 	uint32_t command;
204096f2e892SBill Paul 	struct dc_softc *sc;
204196f2e892SBill Paul 	struct ifnet *ifp;
2042b289c607SPyun YongHyeon 	struct dc_mediainfo *m;
2043ee320f98SPyun YongHyeon 	uint32_t reg, revision;
20445f14ee23SPyun YongHyeon 	int error, mac_offset, phy, rid, tmp;
2045ee320f98SPyun YongHyeon 	uint8_t *mac;
204696f2e892SBill Paul 
204796f2e892SBill Paul 	sc = device_get_softc(dev);
20486b9f5c94SGleb Smirnoff 	sc->dc_dev = dev;
204996f2e892SBill Paul 
20506008862bSJohn Baldwin 	mtx_init(&sc->dc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
2051c8b27acaSJohn Baldwin 	    MTX_DEF);
2052c3e7434fSWarner Losh 
205396f2e892SBill Paul 	/*
205496f2e892SBill Paul 	 * Map control/status registers.
205596f2e892SBill Paul 	 */
205607f65363SBill Paul 	pci_enable_busmaster(dev);
205796f2e892SBill Paul 
205896f2e892SBill Paul 	rid = DC_RID;
20595f96beb9SNate Lawson 	sc->dc_res = bus_alloc_resource_any(dev, DC_RES, &rid, RF_ACTIVE);
206096f2e892SBill Paul 
206196f2e892SBill Paul 	if (sc->dc_res == NULL) {
206222f6205dSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
206396f2e892SBill Paul 		error = ENXIO;
2064608654d4SNate Lawson 		goto fail;
206596f2e892SBill Paul 	}
206696f2e892SBill Paul 
206796f2e892SBill Paul 	sc->dc_btag = rman_get_bustag(sc->dc_res);
206896f2e892SBill Paul 	sc->dc_bhandle = rman_get_bushandle(sc->dc_res);
206996f2e892SBill Paul 
20700934f18aSMaxime Henrion 	/* Allocate interrupt. */
207154f1f1d1SNate Lawson 	rid = 0;
20725f96beb9SNate Lawson 	sc->dc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
207354f1f1d1SNate Lawson 	    RF_SHAREABLE | RF_ACTIVE);
207454f1f1d1SNate Lawson 
207554f1f1d1SNate Lawson 	if (sc->dc_irq == NULL) {
207622f6205dSJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
207754f1f1d1SNate Lawson 		error = ENXIO;
207854f1f1d1SNate Lawson 		goto fail;
207954f1f1d1SNate Lawson 	}
208054f1f1d1SNate Lawson 
208196f2e892SBill Paul 	/* Need this info to decide on a chip type. */
208296f2e892SBill Paul 	sc->dc_info = dc_devtype(dev);
20831e2e70b1SJohn Baldwin 	revision = pci_get_revid(dev);
208496f2e892SBill Paul 
2085abe4e865SPyun YongHyeon 	error = 0;
20866d0dd931SWarner Losh 	/* Get the eeprom width, but PNIC and XIRCOM have diff eeprom */
20871e2e70b1SJohn Baldwin 	if (sc->dc_info->dc_devid !=
20881e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168) &&
20891e2e70b1SJohn Baldwin 	    sc->dc_info->dc_devid !=
20901e2e70b1SJohn Baldwin 	    DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201))
2091eecb3844SMartin Blapp 		dc_eeprom_width(sc);
2092eecb3844SMartin Blapp 
20931e2e70b1SJohn Baldwin 	switch (sc->dc_info->dc_devid) {
20941e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DEC, DC_DEVICEID_21143):
209596f2e892SBill Paul 		sc->dc_type = DC_TYPE_21143;
209696f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2097042c8f6eSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
20985c1cfac4SBill Paul 		/* Save EEPROM contents so we can parse them later. */
2099abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2100abe4e865SPyun YongHyeon 		if (error != 0)
2101abe4e865SPyun YongHyeon 			goto fail;
210296f2e892SBill Paul 		break;
21031e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9009):
21041e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9100):
21051e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DAVICOM, DC_DEVICEID_DM9102):
210696f2e892SBill Paul 		sc->dc_type = DC_TYPE_DM9102;
2107318a72d7SBill Paul 		sc->dc_flags |= DC_TX_COALESCE | DC_TX_INTR_ALWAYS;
2108318a72d7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_TX_STORENFWD;
21097dfdc26cSMartin Blapp 		sc->dc_flags |= DC_TX_ALIGN;
21104a80e74bSMartin Blapp 		sc->dc_pmode = DC_PMODE_MII;
21111e2e70b1SJohn Baldwin 
21120a46b1dcSBill Paul 		/* Increase the latency timer value. */
21131e2e70b1SJohn Baldwin 		pci_write_config(dev, PCIR_LATTIMER, 0x80, 1);
211496f2e892SBill Paul 		break;
21151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AL981):
211696f2e892SBill Paul 		sc->dc_type = DC_TYPE_AL981;
211796f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
211896f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
211996f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2120abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2121abe4e865SPyun YongHyeon 		if (error != 0)
2122abe4e865SPyun YongHyeon 			goto fail;
212396f2e892SBill Paul 		break;
2124593a1aeaSMartin Blapp 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN983):
21251e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_AN985):
21261e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9511):
21271e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ADMTEK, DC_DEVICEID_ADM9513):
21281e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_DLINK, DC_DEVICEID_DRP32TXD):
21291e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500):
21301e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ABOCOM, DC_DEVICEID_FE2500MX):
21311e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN2242):
21321e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_HAWKING, DC_DEVICEID_HAWKING_PN672TX):
21331e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_PLANEX, DC_DEVICEID_FNW3602T):
21341e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_3COM, DC_DEVICEID_3CSOHOB):
21351e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN120):
21361e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MICROSOFT, DC_DEVICEID_MSMN130):
213717762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB08):
213817762569SGleb Smirnoff 	case DC_DEVID(DC_VENDORID_LINKSYS, DC_DEVICEID_PCMPC200_AB09):
2139593a1aeaSMartin Blapp 		sc->dc_type = DC_TYPE_AN983;
2140acc1bcccSMartin Blapp 		sc->dc_flags |= DC_64BIT_HASH;
214196f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR;
214296f2e892SBill Paul 		sc->dc_flags |= DC_TX_ADMTEK_WAR;
214396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2144129eaf79SMartin Blapp 		/* Don't read SROM for - auto-loaded on reset */
214596f2e892SBill Paul 		break;
21461e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98713):
21471e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CP, DC_DEVICEID_98713_CP):
214896f2e892SBill Paul 		if (revision < DC_REVISION_98713A) {
214996f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713;
215096f2e892SBill Paul 		}
2151318b02fdSBill Paul 		if (revision >= DC_REVISION_98713A) {
215296f2e892SBill Paul 			sc->dc_type = DC_TYPE_98713A;
2153318b02fdSBill Paul 			sc->dc_flags |= DC_21143_NWAY;
2154318b02fdSBill Paul 		}
2155318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
215696f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
215796f2e892SBill Paul 		break;
21581e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_987x5):
21591e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ACCTON, DC_DEVICEID_EN1217):
216079d11e09SBill Paul 		/*
216179d11e09SBill Paul 		 * Macronix MX98715AEC-C/D/E parts have only a
216279d11e09SBill Paul 		 * 128-bit hash table. We need to deal with these
216379d11e09SBill Paul 		 * in the same manner as the PNIC II so that we
216479d11e09SBill Paul 		 * get the right number of bits out of the
216579d11e09SBill Paul 		 * CRC routine.
216679d11e09SBill Paul 		 */
216779d11e09SBill Paul 		if (revision >= DC_REVISION_98715AEC_C &&
216879d11e09SBill Paul 		    revision < DC_REVISION_98725)
216979d11e09SBill Paul 			sc->dc_flags |= DC_128BIT_HASH;
217096f2e892SBill Paul 		sc->dc_type = DC_TYPE_987x5;
217196f2e892SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2172318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
217396f2e892SBill Paul 		break;
21741e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_MX, DC_DEVICEID_98727):
2175ead7cde9SBill Paul 		sc->dc_type = DC_TYPE_987x5;
2176ead7cde9SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR;
2177ead7cde9SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
2178ead7cde9SBill Paul 		break;
21791e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C115):
218096f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNICII;
218179d11e09SBill Paul 		sc->dc_flags |= DC_TX_POLL | DC_TX_USE_TX_INTR | DC_128BIT_HASH;
2182318b02fdSBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL | DC_21143_NWAY;
218396f2e892SBill Paul 		break;
21841e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_LO, DC_DEVICEID_82C168):
218596f2e892SBill Paul 		sc->dc_type = DC_TYPE_PNIC;
218691cc2adbSBill Paul 		sc->dc_flags |= DC_TX_STORENFWD | DC_TX_INTR_ALWAYS;
218796f2e892SBill Paul 		sc->dc_flags |= DC_PNIC_RX_BUG_WAR;
218896f2e892SBill Paul 		sc->dc_pnic_rx_buf = malloc(DC_RXLEN * 5, M_DEVBUF, M_NOWAIT);
2189abe4e865SPyun YongHyeon 		if (sc->dc_pnic_rx_buf == NULL) {
2190abe4e865SPyun YongHyeon 			device_printf(sc->dc_dev,
2191abe4e865SPyun YongHyeon 			    "Could not allocate PNIC RX buffer\n");
2192abe4e865SPyun YongHyeon 			error = ENOMEM;
2193abe4e865SPyun YongHyeon 			goto fail;
2194abe4e865SPyun YongHyeon 		}
219596f2e892SBill Paul 		if (revision < DC_REVISION_82C169)
219696f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
219796f2e892SBill Paul 		break;
21981e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_ASIX, DC_DEVICEID_AX88140A):
219996f2e892SBill Paul 		sc->dc_type = DC_TYPE_ASIX;
220096f2e892SBill Paul 		sc->dc_flags |= DC_TX_USE_TX_INTR | DC_TX_INTR_FIRSTFRAG;
220196f2e892SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
220296f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
220396f2e892SBill Paul 		break;
22041e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_XIRCOM, DC_DEVICEID_X3201):
2205feb78939SJonathan Chen 		sc->dc_type = DC_TYPE_XIRCOM;
22062dfc960aSLuigi Rizzo 		sc->dc_flags |= DC_TX_INTR_ALWAYS | DC_TX_COALESCE |
22072dfc960aSLuigi Rizzo 				DC_TX_ALIGN;
2208feb78939SJonathan Chen 		/*
2209feb78939SJonathan Chen 		 * We don't actually need to coalesce, but we're doing
2210feb78939SJonathan Chen 		 * it to obtain a double word aligned buffer.
22112dfc960aSLuigi Rizzo 		 * The DC_TX_COALESCE flag is required.
2212feb78939SJonathan Chen 		 */
22133097aa70SWarner Losh 		sc->dc_pmode = DC_PMODE_MII;
2214feb78939SJonathan Chen 		break;
22151e2e70b1SJohn Baldwin 	case DC_DEVID(DC_VENDORID_CONEXANT, DC_DEVICEID_RS7112):
22161af8bec7SBill Paul 		sc->dc_type = DC_TYPE_CONEXANT;
22171af8bec7SBill Paul 		sc->dc_flags |= DC_TX_INTR_ALWAYS;
22181af8bec7SBill Paul 		sc->dc_flags |= DC_REDUCED_MII_POLL;
22191af8bec7SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
2220abe4e865SPyun YongHyeon 		error = dc_read_srom(sc, sc->dc_romwidth);
2221abe4e865SPyun YongHyeon 		if (error != 0)
2222abe4e865SPyun YongHyeon 			goto fail;
22231af8bec7SBill Paul 		break;
222496f2e892SBill Paul 	default:
22251e2e70b1SJohn Baldwin 		device_printf(dev, "unknown device: %x\n",
22261e2e70b1SJohn Baldwin 		    sc->dc_info->dc_devid);
222796f2e892SBill Paul 		break;
222896f2e892SBill Paul 	}
222996f2e892SBill Paul 
223096f2e892SBill Paul 	/* Save the cache line size. */
223188d739dcSBill Paul 	if (DC_IS_DAVICOM(sc))
223288d739dcSBill Paul 		sc->dc_cachesize = 0;
223388d739dcSBill Paul 	else
22341e2e70b1SJohn Baldwin 		sc->dc_cachesize = pci_get_cachelnsz(dev);
223596f2e892SBill Paul 
223696f2e892SBill Paul 	/* Reset the adapter. */
223796f2e892SBill Paul 	dc_reset(sc);
223896f2e892SBill Paul 
223996f2e892SBill Paul 	/* Take 21143 out of snooze mode */
2240feb78939SJonathan Chen 	if (DC_IS_INTEL(sc) || DC_IS_XIRCOM(sc)) {
224196f2e892SBill Paul 		command = pci_read_config(dev, DC_PCI_CFDD, 4);
224296f2e892SBill Paul 		command &= ~(DC_CFDD_SNOOZE_MODE | DC_CFDD_SLEEP_MODE);
224396f2e892SBill Paul 		pci_write_config(dev, DC_PCI_CFDD, command, 4);
224496f2e892SBill Paul 	}
224596f2e892SBill Paul 
224696f2e892SBill Paul 	/*
224796f2e892SBill Paul 	 * Try to learn something about the supported media.
224896f2e892SBill Paul 	 * We know that ASIX and ADMtek and Davicom devices
224996f2e892SBill Paul 	 * will *always* be using MII media, so that's a no-brainer.
225096f2e892SBill Paul 	 * The tricky ones are the Macronix/PNIC II and the
225196f2e892SBill Paul 	 * Intel 21143.
225296f2e892SBill Paul 	 */
2253abe4e865SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
2254abe4e865SPyun YongHyeon 		error = dc_parse_21143_srom(sc);
2255abe4e865SPyun YongHyeon 		if (error != 0)
2256abe4e865SPyun YongHyeon 			goto fail;
2257abe4e865SPyun YongHyeon 	} else if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
225896f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
225996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_MII;
226096f2e892SBill Paul 		else
226196f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
226296f2e892SBill Paul 	} else if (!sc->dc_pmode)
226396f2e892SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
226496f2e892SBill Paul 
226596f2e892SBill Paul 	/*
226696f2e892SBill Paul 	 * Get station address from the EEPROM.
226796f2e892SBill Paul 	 */
226896f2e892SBill Paul 	switch(sc->dc_type) {
226996f2e892SBill Paul 	case DC_TYPE_98713:
227096f2e892SBill Paul 	case DC_TYPE_98713A:
227196f2e892SBill Paul 	case DC_TYPE_987x5:
227296f2e892SBill Paul 	case DC_TYPE_PNICII:
227396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&mac_offset,
227496f2e892SBill Paul 		    (DC_EE_NODEADDR_OFFSET / 2), 1, 0);
227596f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, (mac_offset / 2), 3, 0);
227696f2e892SBill Paul 		break;
227796f2e892SBill Paul 	case DC_TYPE_PNIC:
227896f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, 0, 3, 1);
227996f2e892SBill Paul 		break;
228096f2e892SBill Paul 	case DC_TYPE_DM9102:
2281ec6a7299SMaxime Henrion 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
2282ec6a7299SMaxime Henrion #ifdef __sparc64__
2283ec6a7299SMaxime Henrion 		/*
2284ec6a7299SMaxime Henrion 		 * If this is an onboard dc(4) the station address read from
2285802cab03SMarius Strobl 		 * the EEPROM is all zero and we have to get it from the FCode.
2286ec6a7299SMaxime Henrion 		 */
2287802cab03SMarius Strobl 		if (eaddr[0] == 0 && (eaddr[1] & ~0xffff) == 0)
22888069c79dSRuslan Ermilov 			OF_getetheraddr(dev, (caddr_t)&eaddr);
2289ec6a7299SMaxime Henrion #endif
2290ec6a7299SMaxime Henrion 		break;
229196f2e892SBill Paul 	case DC_TYPE_21143:
229296f2e892SBill Paul 	case DC_TYPE_ASIX:
229396f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
229496f2e892SBill Paul 		break;
229596f2e892SBill Paul 	case DC_TYPE_AL981:
2296593a1aeaSMartin Blapp 	case DC_TYPE_AN983:
22972e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR0);
22982e3d4b79SPyun YongHyeon 		mac = (uint8_t *)&eaddr[0];
22992e3d4b79SPyun YongHyeon 		mac[0] = (reg >> 0) & 0xff;
23002e3d4b79SPyun YongHyeon 		mac[1] = (reg >> 8) & 0xff;
23012e3d4b79SPyun YongHyeon 		mac[2] = (reg >> 16) & 0xff;
23022e3d4b79SPyun YongHyeon 		mac[3] = (reg >> 24) & 0xff;
23032e3d4b79SPyun YongHyeon 		reg = CSR_READ_4(sc, DC_AL_PAR1);
23042e3d4b79SPyun YongHyeon 		mac[4] = (reg >> 0) & 0xff;
23052e3d4b79SPyun YongHyeon 		mac[5] = (reg >> 8) & 0xff;
230696f2e892SBill Paul 		break;
23071af8bec7SBill Paul 	case DC_TYPE_CONEXANT:
23080934f18aSMaxime Henrion 		bcopy(sc->dc_srom + DC_CONEXANT_EE_NODEADDR, &eaddr,
23090934f18aSMaxime Henrion 		    ETHER_ADDR_LEN);
23101af8bec7SBill Paul 		break;
2311feb78939SJonathan Chen 	case DC_TYPE_XIRCOM:
23120934f18aSMaxime Henrion 		/* The MAC comes from the CIS. */
2313e7b01d07SWarner Losh 		mac = pci_get_ether(dev);
2314e7b01d07SWarner Losh 		if (!mac) {
2315e7b01d07SWarner Losh 			device_printf(dev, "No station address in CIS!\n");
2316608654d4SNate Lawson 			error = ENXIO;
2317e7b01d07SWarner Losh 			goto fail;
2318e7b01d07SWarner Losh 		}
2319e7b01d07SWarner Losh 		bcopy(mac, eaddr, ETHER_ADDR_LEN);
2320feb78939SJonathan Chen 		break;
232196f2e892SBill Paul 	default:
232296f2e892SBill Paul 		dc_read_eeprom(sc, (caddr_t)&eaddr, DC_EE_NODEADDR, 3, 0);
232396f2e892SBill Paul 		break;
232496f2e892SBill Paul 	}
232596f2e892SBill Paul 
232639d76ed6SPyun YongHyeon 	bcopy(eaddr, sc->dc_eaddr, sizeof(eaddr));
232739d76ed6SPyun YongHyeon 	/*
232839d76ed6SPyun YongHyeon 	 * If we still have invalid station address, see whether we can
232939d76ed6SPyun YongHyeon 	 * find station address for chip 0.  Some multi-port controllers
233039d76ed6SPyun YongHyeon 	 * just store station address for chip 0 if they have a shared
233139d76ed6SPyun YongHyeon 	 * SROM.
233239d76ed6SPyun YongHyeon 	 */
233339d76ed6SPyun YongHyeon 	if ((sc->dc_eaddr[0] == 0 && (sc->dc_eaddr[1] & ~0xffff) == 0) ||
233439d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[0] == 0xffffffff &&
233539d76ed6SPyun YongHyeon 	    (sc->dc_eaddr[1] & 0xffff) == 0xffff)) {
2336b289c607SPyun YongHyeon 		error = dc_check_multiport(sc);
2337b289c607SPyun YongHyeon 		if (error == 0) {
233839d76ed6SPyun YongHyeon 			bcopy(sc->dc_eaddr, eaddr, sizeof(eaddr));
2339b289c607SPyun YongHyeon 			/* Extract media information. */
2340b289c607SPyun YongHyeon 			if (DC_IS_INTEL(sc) && sc->dc_srom != NULL) {
2341b289c607SPyun YongHyeon 				while (sc->dc_mi != NULL) {
2342b289c607SPyun YongHyeon 					m = sc->dc_mi->dc_next;
2343b289c607SPyun YongHyeon 					free(sc->dc_mi, M_DEVBUF);
2344b289c607SPyun YongHyeon 					sc->dc_mi = m;
2345b289c607SPyun YongHyeon 				}
2346b289c607SPyun YongHyeon 				error = dc_parse_21143_srom(sc);
2347b289c607SPyun YongHyeon 				if (error != 0)
2348b289c607SPyun YongHyeon 					goto fail;
2349b289c607SPyun YongHyeon 			}
2350b289c607SPyun YongHyeon 		} else if (error == ENOMEM)
2351b289c607SPyun YongHyeon 			goto fail;
2352b289c607SPyun YongHyeon 		else
2353b289c607SPyun YongHyeon 			error = 0;
235439d76ed6SPyun YongHyeon 	}
235539d76ed6SPyun YongHyeon 
23565f14ee23SPyun YongHyeon 	if ((error = dc_dma_alloc(sc)) != 0)
235756e5e7aeSMaxime Henrion 		goto fail;
235896f2e892SBill Paul 
2359fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp = if_alloc(IFT_ETHER);
2360fc74a9f9SBrooks Davis 	if (ifp == NULL) {
236122f6205dSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
2362fc74a9f9SBrooks Davis 		error = ENOSPC;
2363fc74a9f9SBrooks Davis 		goto fail;
2364fc74a9f9SBrooks Davis 	}
236596f2e892SBill Paul 	ifp->if_softc = sc;
23669bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
23673d57a2e5SBrian Feldman 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
236896f2e892SBill Paul 	ifp->if_ioctl = dc_ioctl;
236996f2e892SBill Paul 	ifp->if_start = dc_start;
237096f2e892SBill Paul 	ifp->if_init = dc_init;
2371cbaf877fSBrian Feldman 	IFQ_SET_MAXLEN(&ifp->if_snd, DC_TX_LIST_CNT - 1);
2372cbaf877fSBrian Feldman 	ifp->if_snd.ifq_drv_maxlen = DC_TX_LIST_CNT - 1;
2373cbaf877fSBrian Feldman 	IFQ_SET_READY(&ifp->if_snd);
237496f2e892SBill Paul 
237596f2e892SBill Paul 	/*
23765c1cfac4SBill Paul 	 * Do MII setup. If this is a 21143, check for a PHY on the
23775c1cfac4SBill Paul 	 * MII bus after applying any necessary fixups to twiddle the
23785c1cfac4SBill Paul 	 * GPIO bits. If we don't end up finding a PHY, restore the
23795c1cfac4SBill Paul 	 * old selection (SIA only or SIA/SYM) and attach the dcphy
23805c1cfac4SBill Paul 	 * driver instead.
238196f2e892SBill Paul 	 */
23828e5d93dbSMarius Strobl 	tmp = 0;
23835c1cfac4SBill Paul 	if (DC_IS_INTEL(sc)) {
23845c1cfac4SBill Paul 		dc_apply_fixup(sc, IFM_AUTO);
23855c1cfac4SBill Paul 		tmp = sc->dc_pmode;
23865c1cfac4SBill Paul 		sc->dc_pmode = DC_PMODE_MII;
23875c1cfac4SBill Paul 	}
23885c1cfac4SBill Paul 
23896d431b17SWarner Losh 	/*
23906d431b17SWarner Losh 	 * Setup General Purpose port mode and data so the tulip can talk
23918e5d93dbSMarius Strobl 	 * to the MII.  This needs to be done before mii_attach so that
23926d431b17SWarner Losh 	 * we can actually see them.
23936d431b17SWarner Losh 	 */
23946d431b17SWarner Losh 	if (DC_IS_XIRCOM(sc)) {
23956d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
23966d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
23976d431b17SWarner Losh 		DELAY(10);
23986d431b17SWarner Losh 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
23996d431b17SWarner Losh 		    DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
24006d431b17SWarner Losh 		DELAY(10);
24016d431b17SWarner Losh 	}
24026d431b17SWarner Losh 
24038e5d93dbSMarius Strobl 	phy = MII_PHY_ANY;
24048e5d93dbSMarius Strobl 	/*
24058e5d93dbSMarius Strobl 	 * Note: both the AL981 and AN983 have internal PHYs, however the
24068e5d93dbSMarius Strobl 	 * AL981 provides direct access to the PHY registers while the AN983
24078e5d93dbSMarius Strobl 	 * uses a serial MII interface. The AN983's MII interface is also
24088e5d93dbSMarius Strobl 	 * buggy in that you can read from any MII address (0 to 31), but
24098e5d93dbSMarius Strobl 	 * only address 1 behaves normally. To deal with both cases, we
24108e5d93dbSMarius Strobl 	 * pretend that the PHY is at MII address 1.
24118e5d93dbSMarius Strobl 	 */
24128e5d93dbSMarius Strobl 	if (DC_IS_ADMTEK(sc))
24138e5d93dbSMarius Strobl 		phy = DC_ADMTEK_PHYADDR;
24148e5d93dbSMarius Strobl 
24158e5d93dbSMarius Strobl 	/*
24168e5d93dbSMarius Strobl 	 * Note: the ukphy probes of the RS7112 report a PHY at MII address
24178e5d93dbSMarius Strobl 	 * 0 (possibly HomePNA?) and 1 (ethernet) so we only respond to the
24188e5d93dbSMarius Strobl 	 * correct one.
24198e5d93dbSMarius Strobl 	 */
24208e5d93dbSMarius Strobl 	if (DC_IS_CONEXANT(sc))
24218e5d93dbSMarius Strobl 		phy = DC_CONEXANT_PHYADDR;
24228e5d93dbSMarius Strobl 
24238e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24248e5d93dbSMarius Strobl 	    dc_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, 0);
242596f2e892SBill Paul 
242696f2e892SBill Paul 	if (error && DC_IS_INTEL(sc)) {
24275c1cfac4SBill Paul 		sc->dc_pmode = tmp;
24285c1cfac4SBill Paul 		if (sc->dc_pmode != DC_PMODE_SIA)
242996f2e892SBill Paul 			sc->dc_pmode = DC_PMODE_SYM;
2430042c8f6eSBill Paul 		sc->dc_flags |= DC_21143_NWAY;
24318e5d93dbSMarius Strobl 		mii_attach(dev, &sc->dc_miibus, ifp, dc_ifmedia_upd,
24328e5d93dbSMarius Strobl 		    dc_ifmedia_sts, BMSR_DEFCAPMASK, MII_PHY_ANY,
24338e5d93dbSMarius Strobl 		    MII_OFFSET_ANY, 0);
243478999dd1SBill Paul 		/*
243578999dd1SBill Paul 		 * For non-MII cards, we need to have the 21143
243678999dd1SBill Paul 		 * drive the LEDs. Except there are some systems
243778999dd1SBill Paul 		 * like the NEC VersaPro NoteBook PC which have no
243878999dd1SBill Paul 		 * LEDs, and twiddling these bits has adverse effects
243978999dd1SBill Paul 		 * on them. (I.e. you suddenly can't get a link.)
244078999dd1SBill Paul 		 */
24411e2e70b1SJohn Baldwin 		if (!(pci_get_subvendor(dev) == 0x1033 &&
24421e2e70b1SJohn Baldwin 		    pci_get_subdevice(dev) == 0x8028))
244378999dd1SBill Paul 			sc->dc_flags |= DC_TULIP_LEDS;
244496f2e892SBill Paul 		error = 0;
244596f2e892SBill Paul 	}
244696f2e892SBill Paul 
244796f2e892SBill Paul 	if (error) {
24488e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
244996f2e892SBill Paul 		goto fail;
245096f2e892SBill Paul 	}
245196f2e892SBill Paul 
2452028a8491SMartin Blapp 	if (DC_IS_ADMTEK(sc)) {
2453028a8491SMartin Blapp 		/*
2454028a8491SMartin Blapp 		 * Set automatic TX underrun recovery for the ADMtek chips
2455028a8491SMartin Blapp 		 */
2456028a8491SMartin Blapp 		DC_SETBIT(sc, DC_AL_CR, DC_AL_CR_ATUR);
2457028a8491SMartin Blapp 	}
2458028a8491SMartin Blapp 
245996f2e892SBill Paul 	/*
2460db40c1aeSDoug Ambrisko 	 * Tell the upper layer(s) we support long frames.
2461db40c1aeSDoug Ambrisko 	 */
2462db40c1aeSDoug Ambrisko 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
24639ef8b520SSam Leffler 	ifp->if_capabilities |= IFCAP_VLAN_MTU;
246440929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
2465e695984eSRuslan Ermilov #ifdef DEVICE_POLLING
2466e695984eSRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
2467e695984eSRuslan Ermilov #endif
2468db40c1aeSDoug Ambrisko 
2469c8b27acaSJohn Baldwin 	callout_init_mtx(&sc->dc_stat_ch, &sc->dc_mtx, 0);
2470b1d16143SMarius Strobl 	callout_init_mtx(&sc->dc_wdog_ch, &sc->dc_mtx, 0);
247196f2e892SBill Paul 
2472608654d4SNate Lawson 	/*
2473608654d4SNate Lawson 	 * Call MI attach routine.
2474608654d4SNate Lawson 	 */
24758df1ebe9SMarcel Moolenaar 	ether_ifattach(ifp, (caddr_t)eaddr);
2476608654d4SNate Lawson 
247754f1f1d1SNate Lawson 	/* Hook interrupt last to avoid having to lock softc */
2478c8b27acaSJohn Baldwin 	error = bus_setup_intr(dev, sc->dc_irq, INTR_TYPE_NET | INTR_MPSAFE,
2479ef544f63SPaolo Pisati 	    NULL, dc_intr, sc, &sc->dc_intrhand);
2480608654d4SNate Lawson 
2481608654d4SNate Lawson 	if (error) {
248222f6205dSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
2483693f4477SNate Lawson 		ether_ifdetach(ifp);
248454f1f1d1SNate Lawson 		goto fail;
2485608654d4SNate Lawson 	}
2486510a809eSMike Smith 
248796f2e892SBill Paul fail:
248854f1f1d1SNate Lawson 	if (error)
248954f1f1d1SNate Lawson 		dc_detach(dev);
249096f2e892SBill Paul 	return (error);
249196f2e892SBill Paul }
249296f2e892SBill Paul 
2493693f4477SNate Lawson /*
2494693f4477SNate Lawson  * Shutdown hardware and free up resources. This can be called any
2495693f4477SNate Lawson  * time after the mutex has been initialized. It is called in both
2496693f4477SNate Lawson  * the error case in attach and the normal detach case so it needs
2497693f4477SNate Lawson  * to be careful about only freeing resources that have actually been
2498693f4477SNate Lawson  * allocated.
2499693f4477SNate Lawson  */
2500e3d2833aSAlfred Perlstein static int
25010934f18aSMaxime Henrion dc_detach(device_t dev)
250296f2e892SBill Paul {
250396f2e892SBill Paul 	struct dc_softc *sc;
250496f2e892SBill Paul 	struct ifnet *ifp;
25055c1cfac4SBill Paul 	struct dc_mediainfo *m;
250696f2e892SBill Paul 
250796f2e892SBill Paul 	sc = device_get_softc(dev);
250859f47d29SJohn Baldwin 	KASSERT(mtx_initialized(&sc->dc_mtx), ("dc mutex not initialized"));
2509d1ce9105SBill Paul 
2510fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
251196f2e892SBill Paul 
251240929967SGleb Smirnoff #ifdef DEVICE_POLLING
251340929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
251440929967SGleb Smirnoff 		ether_poll_deregister(ifp);
251540929967SGleb Smirnoff #endif
251640929967SGleb Smirnoff 
2517693f4477SNate Lawson 	/* These should only be active if attach succeeded */
2518214073e5SWarner Losh 	if (device_is_attached(dev)) {
2519c8b27acaSJohn Baldwin 		DC_LOCK(sc);
252096f2e892SBill Paul 		dc_stop(sc);
2521c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
2522c8b27acaSJohn Baldwin 		callout_drain(&sc->dc_stat_ch);
2523b1d16143SMarius Strobl 		callout_drain(&sc->dc_wdog_ch);
25249ef8b520SSam Leffler 		ether_ifdetach(ifp);
2525693f4477SNate Lawson 	}
2526693f4477SNate Lawson 	if (sc->dc_miibus)
252796f2e892SBill Paul 		device_delete_child(dev, sc->dc_miibus);
252854f1f1d1SNate Lawson 	bus_generic_detach(dev);
252996f2e892SBill Paul 
253054f1f1d1SNate Lawson 	if (sc->dc_intrhand)
253196f2e892SBill Paul 		bus_teardown_intr(dev, sc->dc_irq, sc->dc_intrhand);
253254f1f1d1SNate Lawson 	if (sc->dc_irq)
253396f2e892SBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->dc_irq);
253454f1f1d1SNate Lawson 	if (sc->dc_res)
253596f2e892SBill Paul 		bus_release_resource(dev, DC_RES, DC_RID, sc->dc_res);
253696f2e892SBill Paul 
25376a3033a8SWarner Losh 	if (ifp)
25386a3033a8SWarner Losh 		if_free(ifp);
25396a3033a8SWarner Losh 
25405f14ee23SPyun YongHyeon 	dc_dma_free(sc);
254156e5e7aeSMaxime Henrion 
254296f2e892SBill Paul 	free(sc->dc_pnic_rx_buf, M_DEVBUF);
254396f2e892SBill Paul 
25445c1cfac4SBill Paul 	while (sc->dc_mi != NULL) {
25455c1cfac4SBill Paul 		m = sc->dc_mi->dc_next;
25465c1cfac4SBill Paul 		free(sc->dc_mi, M_DEVBUF);
25475c1cfac4SBill Paul 		sc->dc_mi = m;
25485c1cfac4SBill Paul 	}
25497efff076SWarner Losh 	free(sc->dc_srom, M_DEVBUF);
25505c1cfac4SBill Paul 
2551d1ce9105SBill Paul 	mtx_destroy(&sc->dc_mtx);
255296f2e892SBill Paul 
255396f2e892SBill Paul 	return (0);
255496f2e892SBill Paul }
255596f2e892SBill Paul 
255696f2e892SBill Paul /*
255796f2e892SBill Paul  * Initialize the transmit descriptors.
255896f2e892SBill Paul  */
2559e3d2833aSAlfred Perlstein static int
25600934f18aSMaxime Henrion dc_list_tx_init(struct dc_softc *sc)
256196f2e892SBill Paul {
256296f2e892SBill Paul 	struct dc_chain_data *cd;
256396f2e892SBill Paul 	struct dc_list_data *ld;
256401faf54bSLuigi Rizzo 	int i, nexti;
256596f2e892SBill Paul 
256696f2e892SBill Paul 	cd = &sc->dc_cdata;
25675f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
256896f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
2569b3811c95SMaxime Henrion 		if (i == DC_TX_LIST_CNT - 1)
2570b3811c95SMaxime Henrion 			nexti = 0;
2571b3811c95SMaxime Henrion 		else
2572b3811c95SMaxime Henrion 			nexti = i + 1;
257352c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_status = 0;
257452c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_ctl = 0;
257552c43a47SPyun YongHyeon 		ld->dc_tx_list[i].dc_data = 0;
2576af4358c7SMaxime Henrion 		ld->dc_tx_list[i].dc_next = htole32(DC_TXDESC(sc, nexti));
257796f2e892SBill Paul 		cd->dc_tx_chain[i] = NULL;
257896f2e892SBill Paul 	}
257996f2e892SBill Paul 
258096f2e892SBill Paul 	cd->dc_tx_prod = cd->dc_tx_cons = cd->dc_tx_cnt = 0;
258106d23883SPyun YongHyeon 	cd->dc_tx_pkts = 0;
25825f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
258356e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
258496f2e892SBill Paul 	return (0);
258596f2e892SBill Paul }
258696f2e892SBill Paul 
258796f2e892SBill Paul 
258896f2e892SBill Paul /*
258996f2e892SBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
259096f2e892SBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
259196f2e892SBill Paul  * points back to the first.
259296f2e892SBill Paul  */
2593e3d2833aSAlfred Perlstein static int
25940934f18aSMaxime Henrion dc_list_rx_init(struct dc_softc *sc)
259596f2e892SBill Paul {
259696f2e892SBill Paul 	struct dc_chain_data *cd;
259796f2e892SBill Paul 	struct dc_list_data *ld;
259801faf54bSLuigi Rizzo 	int i, nexti;
259996f2e892SBill Paul 
260096f2e892SBill Paul 	cd = &sc->dc_cdata;
26015f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
260296f2e892SBill Paul 
260396f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
26045f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0)
260596f2e892SBill Paul 			return (ENOBUFS);
2606b3811c95SMaxime Henrion 		if (i == DC_RX_LIST_CNT - 1)
2607b3811c95SMaxime Henrion 			nexti = 0;
2608b3811c95SMaxime Henrion 		else
2609b3811c95SMaxime Henrion 			nexti = i + 1;
2610af4358c7SMaxime Henrion 		ld->dc_rx_list[i].dc_next = htole32(DC_RXDESC(sc, nexti));
261196f2e892SBill Paul 	}
261296f2e892SBill Paul 
261396f2e892SBill Paul 	cd->dc_rx_prod = 0;
26145f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
261556e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
261696f2e892SBill Paul 	return (0);
261796f2e892SBill Paul }
261896f2e892SBill Paul 
261996f2e892SBill Paul /*
262096f2e892SBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
262196f2e892SBill Paul  */
2622e3d2833aSAlfred Perlstein static int
26235f14ee23SPyun YongHyeon dc_newbuf(struct dc_softc *sc, int i)
262496f2e892SBill Paul {
26255f14ee23SPyun YongHyeon 	struct mbuf *m;
26265f14ee23SPyun YongHyeon 	bus_dmamap_t map;
262782a67a70SMarius Strobl 	bus_dma_segment_t segs[1];
262882a67a70SMarius Strobl 	int error, nseg;
262996f2e892SBill Paul 
26305f14ee23SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
26315f14ee23SPyun YongHyeon 	if (m == NULL)
263296f2e892SBill Paul 		return (ENOBUFS);
26335f14ee23SPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
26345f14ee23SPyun YongHyeon 	m_adj(m, sizeof(u_int64_t));
263596f2e892SBill Paul 
263696f2e892SBill Paul 	/*
263796f2e892SBill Paul 	 * If this is a PNIC chip, zero the buffer. This is part
263896f2e892SBill Paul 	 * of the workaround for the receive bug in the 82c168 and
263996f2e892SBill Paul 	 * 82c169 chips.
264096f2e892SBill Paul 	 */
264196f2e892SBill Paul 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR)
26425f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
264396f2e892SBill Paul 
26445f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_rx_mtag, sc->dc_sparemap,
26455f14ee23SPyun YongHyeon 	    m, segs, &nseg, 0);
264656e5e7aeSMaxime Henrion 	if (error) {
26475f14ee23SPyun YongHyeon 		m_freem(m);
264856e5e7aeSMaxime Henrion 		return (error);
264956e5e7aeSMaxime Henrion 	}
26505f14ee23SPyun YongHyeon 	KASSERT(nseg == 1, ("%s: wrong number of segments (%d)", __func__,
26515f14ee23SPyun YongHyeon 	    nseg));
26525f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_rx_chain[i] != NULL)
26535f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i]);
265496f2e892SBill Paul 
26555f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_rx_map[i];
26565f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_map[i] = sc->dc_sparemap;
26575f14ee23SPyun YongHyeon 	sc->dc_sparemap = map;
26585f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_rx_chain[i] = m;
26595f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
266056e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREREAD);
26615f14ee23SPyun YongHyeon 
26625f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
26635f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_data =
26645f14ee23SPyun YongHyeon 	    htole32(DC_ADDR_LO(segs[0].ds_addr));
26655f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
26665f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
266756e5e7aeSMaxime Henrion 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
266896f2e892SBill Paul 	return (0);
266996f2e892SBill Paul }
267096f2e892SBill Paul 
267196f2e892SBill Paul /*
267296f2e892SBill Paul  * Grrrrr.
267396f2e892SBill Paul  * The PNIC chip has a terrible bug in it that manifests itself during
267496f2e892SBill Paul  * periods of heavy activity. The exact mode of failure if difficult to
267596f2e892SBill Paul  * pinpoint: sometimes it only happens in promiscuous mode, sometimes it
267696f2e892SBill Paul  * will happen on slow machines. The bug is that sometimes instead of
267796f2e892SBill Paul  * uploading one complete frame during reception, it uploads what looks
267896f2e892SBill Paul  * like the entire contents of its FIFO memory. The frame we want is at
267996f2e892SBill Paul  * the end of the whole mess, but we never know exactly how much data has
268096f2e892SBill Paul  * been uploaded, so salvaging the frame is hard.
268196f2e892SBill Paul  *
268296f2e892SBill Paul  * There is only one way to do it reliably, and it's disgusting.
268396f2e892SBill Paul  * Here's what we know:
268496f2e892SBill Paul  *
268596f2e892SBill Paul  * - We know there will always be somewhere between one and three extra
268696f2e892SBill Paul  *   descriptors uploaded.
268796f2e892SBill Paul  *
268896f2e892SBill Paul  * - We know the desired received frame will always be at the end of the
268996f2e892SBill Paul  *   total data upload.
269096f2e892SBill Paul  *
269196f2e892SBill Paul  * - We know the size of the desired received frame because it will be
269296f2e892SBill Paul  *   provided in the length field of the status word in the last descriptor.
269396f2e892SBill Paul  *
269496f2e892SBill Paul  * Here's what we do:
269596f2e892SBill Paul  *
269696f2e892SBill Paul  * - When we allocate buffers for the receive ring, we bzero() them.
269796f2e892SBill Paul  *   This means that we know that the buffer contents should be all
269896f2e892SBill Paul  *   zeros, except for data uploaded by the chip.
269996f2e892SBill Paul  *
270096f2e892SBill Paul  * - We also force the PNIC chip to upload frames that include the
270196f2e892SBill Paul  *   ethernet CRC at the end.
270296f2e892SBill Paul  *
270396f2e892SBill Paul  * - We gather all of the bogus frame data into a single buffer.
270496f2e892SBill Paul  *
270596f2e892SBill Paul  * - We then position a pointer at the end of this buffer and scan
270696f2e892SBill Paul  *   backwards until we encounter the first non-zero byte of data.
270796f2e892SBill Paul  *   This is the end of the received frame. We know we will encounter
270896f2e892SBill Paul  *   some data at the end of the frame because the CRC will always be
270996f2e892SBill Paul  *   there, so even if the sender transmits a packet of all zeros,
271096f2e892SBill Paul  *   we won't be fooled.
271196f2e892SBill Paul  *
271296f2e892SBill Paul  * - We know the size of the actual received frame, so we subtract
271396f2e892SBill Paul  *   that value from the current pointer location. This brings us
271496f2e892SBill Paul  *   to the start of the actual received packet.
271596f2e892SBill Paul  *
271696f2e892SBill Paul  * - We copy this into an mbuf and pass it on, along with the actual
271796f2e892SBill Paul  *   frame length.
271896f2e892SBill Paul  *
271996f2e892SBill Paul  * The performance hit is tremendous, but it beats dropping frames all
272096f2e892SBill Paul  * the time.
272196f2e892SBill Paul  */
272296f2e892SBill Paul 
272396f2e892SBill Paul #define	DC_WHOLEFRAME	(DC_RXSTAT_FIRSTFRAG | DC_RXSTAT_LASTFRAG)
2724e3d2833aSAlfred Perlstein static void
27250934f18aSMaxime Henrion dc_pnic_rx_bug_war(struct dc_softc *sc, int idx)
272696f2e892SBill Paul {
272796f2e892SBill Paul 	struct dc_desc *cur_rx;
272896f2e892SBill Paul 	struct dc_desc *c = NULL;
272996f2e892SBill Paul 	struct mbuf *m = NULL;
273096f2e892SBill Paul 	unsigned char *ptr;
273196f2e892SBill Paul 	int i, total_len;
2732ee320f98SPyun YongHyeon 	uint32_t rxstat = 0;
273396f2e892SBill Paul 
273496f2e892SBill Paul 	i = sc->dc_pnic_rx_bug_save;
27355f14ee23SPyun YongHyeon 	cur_rx = &sc->dc_ldata.dc_rx_list[idx];
273696f2e892SBill Paul 	ptr = sc->dc_pnic_rx_buf;
27371edc4c46SMaxime Henrion 	bzero(ptr, DC_RXLEN * 5);
273896f2e892SBill Paul 
273996f2e892SBill Paul 	/* Copy all the bytes from the bogus buffers. */
274096f2e892SBill Paul 	while (1) {
27415f14ee23SPyun YongHyeon 		c = &sc->dc_ldata.dc_rx_list[i];
2742af4358c7SMaxime Henrion 		rxstat = le32toh(c->dc_status);
274396f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
274496f2e892SBill Paul 		bcopy(mtod(m, char *), ptr, DC_RXLEN);
274596f2e892SBill Paul 		ptr += DC_RXLEN;
274696f2e892SBill Paul 		/* If this is the last buffer, break out. */
274796f2e892SBill Paul 		if (i == idx || rxstat & DC_RXSTAT_LASTFRAG)
274896f2e892SBill Paul 			break;
27495f14ee23SPyun YongHyeon 		dc_discard_rxbuf(sc, i);
275096f2e892SBill Paul 		DC_INC(i, DC_RX_LIST_CNT);
275196f2e892SBill Paul 	}
275296f2e892SBill Paul 
275396f2e892SBill Paul 	/* Find the length of the actual receive frame. */
275496f2e892SBill Paul 	total_len = DC_RXBYTES(rxstat);
275596f2e892SBill Paul 
275696f2e892SBill Paul 	/* Scan backwards until we hit a non-zero byte. */
275796f2e892SBill Paul 	while (*ptr == 0x00)
275896f2e892SBill Paul 		ptr--;
275996f2e892SBill Paul 
276096f2e892SBill Paul 	/* Round off. */
276196f2e892SBill Paul 	if ((uintptr_t)(ptr) & 0x3)
276296f2e892SBill Paul 		ptr -= 1;
276396f2e892SBill Paul 
276496f2e892SBill Paul 	/* Now find the start of the frame. */
276596f2e892SBill Paul 	ptr -= total_len;
276696f2e892SBill Paul 	if (ptr < sc->dc_pnic_rx_buf)
276796f2e892SBill Paul 		ptr = sc->dc_pnic_rx_buf;
276896f2e892SBill Paul 
276996f2e892SBill Paul 	/*
277096f2e892SBill Paul 	 * Now copy the salvaged frame to the last mbuf and fake up
277196f2e892SBill Paul 	 * the status word to make it look like a successful
277296f2e892SBill Paul 	 * frame reception.
277396f2e892SBill Paul 	 */
277496f2e892SBill Paul 	bcopy(ptr, mtod(m, char *), total_len);
2775af4358c7SMaxime Henrion 	cur_rx->dc_status = htole32(rxstat | DC_RXSTAT_FIRSTFRAG);
277696f2e892SBill Paul }
277796f2e892SBill Paul 
277896f2e892SBill Paul /*
277973bf949cSBill Paul  * This routine searches the RX ring for dirty descriptors in the
278073bf949cSBill Paul  * event that the rxeof routine falls out of sync with the chip's
278173bf949cSBill Paul  * current descriptor pointer. This may happen sometimes as a result
278273bf949cSBill Paul  * of a "no RX buffer available" condition that happens when the chip
278373bf949cSBill Paul  * consumes all of the RX buffers before the driver has a chance to
278473bf949cSBill Paul  * process the RX ring. This routine may need to be called more than
278573bf949cSBill Paul  * once to bring the driver back in sync with the chip, however we
278673bf949cSBill Paul  * should still be getting RX DONE interrupts to drive the search
278773bf949cSBill Paul  * for new packets in the RX ring, so we should catch up eventually.
278873bf949cSBill Paul  */
2789e3d2833aSAlfred Perlstein static int
27900934f18aSMaxime Henrion dc_rx_resync(struct dc_softc *sc)
279173bf949cSBill Paul {
279273bf949cSBill Paul 	struct dc_desc *cur_rx;
27930934f18aSMaxime Henrion 	int i, pos;
279473bf949cSBill Paul 
279573bf949cSBill Paul 	pos = sc->dc_cdata.dc_rx_prod;
279673bf949cSBill Paul 
279773bf949cSBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
27985f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[pos];
2799af4358c7SMaxime Henrion 		if (!(le32toh(cur_rx->dc_status) & DC_RXSTAT_OWN))
280073bf949cSBill Paul 			break;
280173bf949cSBill Paul 		DC_INC(pos, DC_RX_LIST_CNT);
280273bf949cSBill Paul 	}
280373bf949cSBill Paul 
280473bf949cSBill Paul 	/* If the ring really is empty, then just return. */
280573bf949cSBill Paul 	if (i == DC_RX_LIST_CNT)
280673bf949cSBill Paul 		return (0);
280773bf949cSBill Paul 
280873bf949cSBill Paul 	/* We've fallen behing the chip: catch it. */
280973bf949cSBill Paul 	sc->dc_cdata.dc_rx_prod = pos;
281073bf949cSBill Paul 
281173bf949cSBill Paul 	return (EAGAIN);
281273bf949cSBill Paul }
281373bf949cSBill Paul 
28145f14ee23SPyun YongHyeon static void
28155f14ee23SPyun YongHyeon dc_discard_rxbuf(struct dc_softc *sc, int i)
28165f14ee23SPyun YongHyeon {
28175f14ee23SPyun YongHyeon 	struct mbuf *m;
28185f14ee23SPyun YongHyeon 
28195f14ee23SPyun YongHyeon 	if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
28205f14ee23SPyun YongHyeon 		m = sc->dc_cdata.dc_rx_chain[i];
28215f14ee23SPyun YongHyeon 		bzero(mtod(m, char *), m->m_len);
28225f14ee23SPyun YongHyeon 	}
28235f14ee23SPyun YongHyeon 
28245f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_ctl = htole32(DC_RXCTL_RLINK | DC_RXLEN);
28255f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_rx_list[i].dc_status = htole32(DC_RXSTAT_OWN);
28265f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_PREREAD |
28275f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
28285f14ee23SPyun YongHyeon }
28295f14ee23SPyun YongHyeon 
283073bf949cSBill Paul /*
283196f2e892SBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
283296f2e892SBill Paul  * the higher level protocols.
283396f2e892SBill Paul  */
28341abcdbd1SAttilio Rao static int
28350934f18aSMaxime Henrion dc_rxeof(struct dc_softc *sc)
283696f2e892SBill Paul {
28375f14ee23SPyun YongHyeon 	struct mbuf *m;
283896f2e892SBill Paul 	struct ifnet *ifp;
283996f2e892SBill Paul 	struct dc_desc *cur_rx;
28401abcdbd1SAttilio Rao 	int i, total_len, rx_npkts;
2841ee320f98SPyun YongHyeon 	uint32_t rxstat;
284296f2e892SBill Paul 
28435120abbfSSam Leffler 	DC_LOCK_ASSERT(sc);
28445120abbfSSam Leffler 
2845fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
28461abcdbd1SAttilio Rao 	rx_npkts = 0;
284796f2e892SBill Paul 
28485f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap, BUS_DMASYNC_POSTREAD |
28495f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
28505f14ee23SPyun YongHyeon 	for (i = sc->dc_cdata.dc_rx_prod;
28515f14ee23SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
28525f14ee23SPyun YongHyeon 	    DC_INC(i, DC_RX_LIST_CNT)) {
2853e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
285440929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
2855e4fc250cSLuigi Rizzo 			if (sc->rxcycles <= 0)
2856e4fc250cSLuigi Rizzo 				break;
2857e4fc250cSLuigi Rizzo 			sc->rxcycles--;
2858e4fc250cSLuigi Rizzo 		}
28590934f18aSMaxime Henrion #endif
28605f14ee23SPyun YongHyeon 		cur_rx = &sc->dc_ldata.dc_rx_list[i];
2861af4358c7SMaxime Henrion 		rxstat = le32toh(cur_rx->dc_status);
28625f14ee23SPyun YongHyeon 		if ((rxstat & DC_RXSTAT_OWN) != 0)
28635f14ee23SPyun YongHyeon 			break;
286496f2e892SBill Paul 		m = sc->dc_cdata.dc_rx_chain[i];
28655f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_rx_mtag, sc->dc_cdata.dc_rx_map[i],
286656e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTREAD);
286796f2e892SBill Paul 		total_len = DC_RXBYTES(rxstat);
2868848a02fcSPyun YongHyeon 		rx_npkts++;
286996f2e892SBill Paul 
287096f2e892SBill Paul 		if (sc->dc_flags & DC_PNIC_RX_BUG_WAR) {
287196f2e892SBill Paul 			if ((rxstat & DC_WHOLEFRAME) != DC_WHOLEFRAME) {
287296f2e892SBill Paul 				if (rxstat & DC_RXSTAT_FIRSTFRAG)
287396f2e892SBill Paul 					sc->dc_pnic_rx_bug_save = i;
28745f14ee23SPyun YongHyeon 				if ((rxstat & DC_RXSTAT_LASTFRAG) == 0)
287596f2e892SBill Paul 					continue;
287696f2e892SBill Paul 				dc_pnic_rx_bug_war(sc, i);
2877af4358c7SMaxime Henrion 				rxstat = le32toh(cur_rx->dc_status);
287896f2e892SBill Paul 				total_len = DC_RXBYTES(rxstat);
287996f2e892SBill Paul 			}
288096f2e892SBill Paul 		}
288196f2e892SBill Paul 
288296f2e892SBill Paul 		/*
288396f2e892SBill Paul 		 * If an error occurs, update stats, clear the
288496f2e892SBill Paul 		 * status word and leave the mbuf cluster in place:
288596f2e892SBill Paul 		 * it should simply get re-used next time this descriptor
2886db40c1aeSDoug Ambrisko 		 * comes up in the ring.  However, don't report long
28870934f18aSMaxime Henrion 		 * frames as errors since they could be vlans.
288896f2e892SBill Paul 		 */
2889db40c1aeSDoug Ambrisko 		if ((rxstat & DC_RXSTAT_RXERR)) {
2890db40c1aeSDoug Ambrisko 			if (!(rxstat & DC_RXSTAT_GIANT) ||
2891db40c1aeSDoug Ambrisko 			    (rxstat & (DC_RXSTAT_CRCERR | DC_RXSTAT_DRIBBLE |
2892db40c1aeSDoug Ambrisko 				       DC_RXSTAT_MIIERE | DC_RXSTAT_COLLSEEN |
2893db40c1aeSDoug Ambrisko 				       DC_RXSTAT_RUNT   | DC_RXSTAT_DE))) {
289496f2e892SBill Paul 				ifp->if_ierrors++;
289596f2e892SBill Paul 				if (rxstat & DC_RXSTAT_COLLSEEN)
289696f2e892SBill Paul 					ifp->if_collisions++;
28975f14ee23SPyun YongHyeon 				dc_discard_rxbuf(sc, i);
28985f14ee23SPyun YongHyeon 				if (rxstat & DC_RXSTAT_CRCERR)
289996f2e892SBill Paul 					continue;
29005f14ee23SPyun YongHyeon 				else {
29018f382a1fSPyun YongHyeon 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2902c8b27acaSJohn Baldwin 					dc_init_locked(sc);
29031abcdbd1SAttilio Rao 					return (rx_npkts);
290496f2e892SBill Paul 				}
290596f2e892SBill Paul 			}
2906db40c1aeSDoug Ambrisko 		}
290796f2e892SBill Paul 
290896f2e892SBill Paul 		/* No errors; receive the packet. */
290996f2e892SBill Paul 		total_len -= ETHER_CRC_LEN;
2910432120f2SMarius Strobl #ifdef __NO_STRICT_ALIGNMENT
291101faf54bSLuigi Rizzo 		/*
2912432120f2SMarius Strobl 		 * On architectures without alignment problems we try to
291301faf54bSLuigi Rizzo 		 * allocate a new buffer for the receive ring, and pass up
291401faf54bSLuigi Rizzo 		 * the one where the packet is already, saving the expensive
291501faf54bSLuigi Rizzo 		 * copy done in m_devget().
291601faf54bSLuigi Rizzo 		 * If we are on an architecture with alignment problems, or
291701faf54bSLuigi Rizzo 		 * if the allocation fails, then use m_devget and leave the
291801faf54bSLuigi Rizzo 		 * existing buffer in the receive ring.
291901faf54bSLuigi Rizzo 		 */
29205f14ee23SPyun YongHyeon 		if (dc_newbuf(sc, i) != 0) {
29215f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
29225f14ee23SPyun YongHyeon 			ifp->if_iqdrops++;
29235f14ee23SPyun YongHyeon 			continue;
29245f14ee23SPyun YongHyeon 		}
292501faf54bSLuigi Rizzo 		m->m_pkthdr.rcvif = ifp;
292601faf54bSLuigi Rizzo 		m->m_pkthdr.len = m->m_len = total_len;
29275f14ee23SPyun YongHyeon #else
292801faf54bSLuigi Rizzo 		{
29295f14ee23SPyun YongHyeon 			struct mbuf *m0;
29305f14ee23SPyun YongHyeon 
293101faf54bSLuigi Rizzo 			m0 = m_devget(mtod(m, char *), total_len,
293201faf54bSLuigi Rizzo 				ETHER_ALIGN, ifp, NULL);
29335f14ee23SPyun YongHyeon 			dc_discard_rxbuf(sc, i);
293496f2e892SBill Paul 			if (m0 == NULL) {
29355f14ee23SPyun YongHyeon 				ifp->if_iqdrops++;
293696f2e892SBill Paul 				continue;
293796f2e892SBill Paul 			}
293896f2e892SBill Paul 			m = m0;
293901faf54bSLuigi Rizzo 		}
29405f14ee23SPyun YongHyeon #endif
294196f2e892SBill Paul 
294296f2e892SBill Paul 		ifp->if_ipackets++;
29435120abbfSSam Leffler 		DC_UNLOCK(sc);
29449ef8b520SSam Leffler 		(*ifp->if_input)(ifp, m);
29455120abbfSSam Leffler 		DC_LOCK(sc);
294696f2e892SBill Paul 	}
294796f2e892SBill Paul 
294896f2e892SBill Paul 	sc->dc_cdata.dc_rx_prod = i;
29491abcdbd1SAttilio Rao 	return (rx_npkts);
295096f2e892SBill Paul }
295196f2e892SBill Paul 
295296f2e892SBill Paul /*
295396f2e892SBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
295496f2e892SBill Paul  * the list buffers.
295596f2e892SBill Paul  */
2956e3d2833aSAlfred Perlstein static void
29570934f18aSMaxime Henrion dc_txeof(struct dc_softc *sc)
295896f2e892SBill Paul {
29595f14ee23SPyun YongHyeon 	struct dc_desc *cur_tx;
296096f2e892SBill Paul 	struct ifnet *ifp;
29615f14ee23SPyun YongHyeon 	int idx, setup;
2962ee320f98SPyun YongHyeon 	uint32_t ctl, txstat;
296396f2e892SBill Paul 
296406d23883SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt == 0)
296506d23883SPyun YongHyeon 		return;
296606d23883SPyun YongHyeon 
2967fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
296896f2e892SBill Paul 
296996f2e892SBill Paul 	/*
297096f2e892SBill Paul 	 * Go through our tx list and free mbufs for those
297196f2e892SBill Paul 	 * frames that have been transmitted.
297296f2e892SBill Paul 	 */
2973*cb94db27SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap, BUS_DMASYNC_POSTREAD |
29745f14ee23SPyun YongHyeon 	    BUS_DMASYNC_POSTWRITE);
29755f14ee23SPyun YongHyeon 	setup = 0;
29765f14ee23SPyun YongHyeon 	for (idx = sc->dc_cdata.dc_tx_cons; idx != sc->dc_cdata.dc_tx_prod;
29775f14ee23SPyun YongHyeon 	    DC_INC(idx, DC_TX_LIST_CNT), sc->dc_cdata.dc_tx_cnt--) {
29785f14ee23SPyun YongHyeon 		cur_tx = &sc->dc_ldata.dc_tx_list[idx];
2979af4358c7SMaxime Henrion 		txstat = le32toh(cur_tx->dc_status);
2980af4358c7SMaxime Henrion 		ctl = le32toh(cur_tx->dc_ctl);
298196f2e892SBill Paul 
298296f2e892SBill Paul 		if (txstat & DC_TXSTAT_OWN)
298396f2e892SBill Paul 			break;
298496f2e892SBill Paul 
29855f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_chain[idx] == NULL)
29865f14ee23SPyun YongHyeon 			continue;
29875f14ee23SPyun YongHyeon 
2988af4358c7SMaxime Henrion 		if (ctl & DC_TXCTL_SETUP) {
29895f14ee23SPyun YongHyeon 			cur_tx->dc_ctl = htole32(ctl & ~DC_TXCTL_SETUP);
29905f14ee23SPyun YongHyeon 			setup++;
29915f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
29925f14ee23SPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
299396f2e892SBill Paul 			/*
299496f2e892SBill Paul 			 * Yes, the PNIC is so brain damaged
299596f2e892SBill Paul 			 * that it will sometimes generate a TX
299696f2e892SBill Paul 			 * underrun error while DMAing the RX
299796f2e892SBill Paul 			 * filter setup frame. If we detect this,
299896f2e892SBill Paul 			 * we have to send the setup frame again,
299996f2e892SBill Paul 			 * or else the filter won't be programmed
300096f2e892SBill Paul 			 * correctly.
300196f2e892SBill Paul 			 */
300296f2e892SBill Paul 			if (DC_IS_PNIC(sc)) {
300396f2e892SBill Paul 				if (txstat & DC_TXSTAT_ERRSUM)
300496f2e892SBill Paul 					dc_setfilt(sc);
300596f2e892SBill Paul 			}
300696f2e892SBill Paul 			sc->dc_cdata.dc_tx_chain[idx] = NULL;
300796f2e892SBill Paul 			continue;
300896f2e892SBill Paul 		}
300996f2e892SBill Paul 
301029a2220aSBill Paul 		if (DC_IS_XIRCOM(sc) || DC_IS_CONEXANT(sc)) {
3011feb78939SJonathan Chen 			/*
3012feb78939SJonathan Chen 			 * XXX: Why does my Xircom taunt me so?
3013feb78939SJonathan Chen 			 * For some reason it likes setting the CARRLOST flag
301429a2220aSBill Paul 			 * even when the carrier is there. wtf?!?
301529a2220aSBill Paul 			 * Who knows, but Conexant chips have the
301629a2220aSBill Paul 			 * same problem. Maybe they took lessons
301729a2220aSBill Paul 			 * from Xircom.
301829a2220aSBill Paul 			 */
3019feb78939SJonathan Chen 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
3020feb78939SJonathan Chen 			    sc->dc_pmode == DC_PMODE_MII &&
3021feb78939SJonathan Chen 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
3022feb78939SJonathan Chen 			    DC_TXSTAT_NOCARRIER)))
3023feb78939SJonathan Chen 				txstat &= ~DC_TXSTAT_ERRSUM;
3024feb78939SJonathan Chen 		} else {
302596f2e892SBill Paul 			if (/*sc->dc_type == DC_TYPE_21143 &&*/
302696f2e892SBill Paul 			    sc->dc_pmode == DC_PMODE_MII &&
302796f2e892SBill Paul 			    ((txstat & 0xFFFF) & ~(DC_TXSTAT_ERRSUM |
302896f2e892SBill Paul 			    DC_TXSTAT_NOCARRIER | DC_TXSTAT_CARRLOST)))
302996f2e892SBill Paul 				txstat &= ~DC_TXSTAT_ERRSUM;
3030feb78939SJonathan Chen 		}
303196f2e892SBill Paul 
303296f2e892SBill Paul 		if (txstat & DC_TXSTAT_ERRSUM) {
303396f2e892SBill Paul 			ifp->if_oerrors++;
303496f2e892SBill Paul 			if (txstat & DC_TXSTAT_EXCESSCOLL)
303596f2e892SBill Paul 				ifp->if_collisions++;
303696f2e892SBill Paul 			if (txstat & DC_TXSTAT_LATECOLL)
303796f2e892SBill Paul 				ifp->if_collisions++;
303896f2e892SBill Paul 			if (!(txstat & DC_TXSTAT_UNDERRUN)) {
30398f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3040c8b27acaSJohn Baldwin 				dc_init_locked(sc);
304196f2e892SBill Paul 				return;
304296f2e892SBill Paul 			}
304352c43a47SPyun YongHyeon 		} else
304452c43a47SPyun YongHyeon 			ifp->if_opackets++;
304596f2e892SBill Paul 		ifp->if_collisions += (txstat & DC_TXSTAT_COLLCNT) >> 3;
304696f2e892SBill Paul 
30475f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
304856e5e7aeSMaxime Henrion 		    BUS_DMASYNC_POSTWRITE);
30495f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
305096f2e892SBill Paul 		m_freem(sc->dc_cdata.dc_tx_chain[idx]);
305196f2e892SBill Paul 		sc->dc_cdata.dc_tx_chain[idx] = NULL;
305296f2e892SBill Paul 	}
305396f2e892SBill Paul 	sc->dc_cdata.dc_tx_cons = idx;
305482a67a70SMarius Strobl 
30555f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt <= DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
305613f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
30573e0e6726SMarius Strobl 		if (sc->dc_cdata.dc_tx_cnt == 0)
30583e0e6726SMarius Strobl 			sc->dc_wdog_timer = 0;
305996f2e892SBill Paul 	}
30605f14ee23SPyun YongHyeon 	if (setup > 0)
30615f14ee23SPyun YongHyeon 		bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
30625f14ee23SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
30635f14ee23SPyun YongHyeon }
306496f2e892SBill Paul 
3065e3d2833aSAlfred Perlstein static void
30660934f18aSMaxime Henrion dc_tick(void *xsc)
306796f2e892SBill Paul {
306896f2e892SBill Paul 	struct dc_softc *sc;
306996f2e892SBill Paul 	struct mii_data *mii;
307096f2e892SBill Paul 	struct ifnet *ifp;
3071ee320f98SPyun YongHyeon 	uint32_t r;
307296f2e892SBill Paul 
307396f2e892SBill Paul 	sc = xsc;
3074c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3075fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
307696f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
307796f2e892SBill Paul 
307806d23883SPyun YongHyeon 	/*
307906d23883SPyun YongHyeon 	 * Reclaim transmitted frames for controllers that do
308006d23883SPyun YongHyeon 	 * not generate TX completion interrupt for every frame.
308106d23883SPyun YongHyeon 	 */
308206d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR)
308306d23883SPyun YongHyeon 		dc_txeof(sc);
308406d23883SPyun YongHyeon 
308596f2e892SBill Paul 	if (sc->dc_flags & DC_REDUCED_MII_POLL) {
3086318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY) {
3087318b02fdSBill Paul 			r = CSR_READ_4(sc, DC_10BTSTAT);
3088318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3089318b02fdSBill Paul 			    IFM_100_TX && (r & DC_TSTAT_LS100)) {
309096f2e892SBill Paul 				sc->dc_link = 0;
3091318b02fdSBill Paul 				mii_mediachg(mii);
3092318b02fdSBill Paul 			}
3093318b02fdSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) ==
3094318b02fdSBill Paul 			    IFM_10_T && (r & DC_TSTAT_LS10)) {
3095318b02fdSBill Paul 				sc->dc_link = 0;
3096318b02fdSBill Paul 				mii_mediachg(mii);
3097318b02fdSBill Paul 			}
3098d675147eSBill Paul 			if (sc->dc_link == 0)
309996f2e892SBill Paul 				mii_tick(mii);
310096f2e892SBill Paul 		} else {
3101d0d67284SMarius Strobl 			/*
3102d0d67284SMarius Strobl 			 * For NICs which never report DC_RXSTATE_WAIT, we
3103d0d67284SMarius Strobl 			 * have to bite the bullet...
3104d0d67284SMarius Strobl 			 */
3105d0d67284SMarius Strobl 			if ((DC_HAS_BROKEN_RXSTATE(sc) || (CSR_READ_4(sc,
3106d0d67284SMarius Strobl 			    DC_ISR) & DC_ISR_RX_STATE) == DC_RXSTATE_WAIT) &&
3107d314ebf5SPyun YongHyeon 			    sc->dc_cdata.dc_tx_cnt == 0)
310896f2e892SBill Paul 				mii_tick(mii);
3109259b8d84SMartin Blapp 		}
311096f2e892SBill Paul 	} else
311196f2e892SBill Paul 		mii_tick(mii);
311296f2e892SBill Paul 
311396f2e892SBill Paul 	/*
311496f2e892SBill Paul 	 * When the init routine completes, we expect to be able to send
311596f2e892SBill Paul 	 * packets right away, and in fact the network code will send a
311696f2e892SBill Paul 	 * gratuitous ARP the moment the init routine marks the interface
311796f2e892SBill Paul 	 * as running. However, even though the MAC may have been initialized,
311896f2e892SBill Paul 	 * there may be a delay of a few seconds before the PHY completes
311996f2e892SBill Paul 	 * autonegotiation and the link is brought up. Any transmissions
312096f2e892SBill Paul 	 * made during that delay will be lost. Dealing with this is tricky:
312196f2e892SBill Paul 	 * we can't just pause in the init routine while waiting for the
312296f2e892SBill Paul 	 * PHY to come ready since that would bring the whole system to
312396f2e892SBill Paul 	 * a screeching halt for several seconds.
312496f2e892SBill Paul 	 *
312596f2e892SBill Paul 	 * What we do here is prevent the TX start routine from sending
312696f2e892SBill Paul 	 * any packets until a link has been established. After the
312796f2e892SBill Paul 	 * interface has been initialized, the tick routine will poll
312896f2e892SBill Paul 	 * the state of the PHY until the IFM_ACTIVE flag is set. Until
312996f2e892SBill Paul 	 * that time, packets will stay in the send queue, and once the
313096f2e892SBill Paul 	 * link comes up, they will be flushed out to the wire.
313196f2e892SBill Paul 	 */
3132d314ebf5SPyun YongHyeon 	if (sc->dc_link != 0 && !IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3133c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
313496f2e892SBill Paul 
3135318b02fdSBill Paul 	if (sc->dc_flags & DC_21143_NWAY && !sc->dc_link)
3136b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3137318b02fdSBill Paul 	else
3138b50c6312SJonathan Lemon 		callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
313996f2e892SBill Paul }
314096f2e892SBill Paul 
3141d467c136SBill Paul /*
3142d467c136SBill Paul  * A transmit underrun has occurred.  Back off the transmit threshold,
3143d467c136SBill Paul  * or switch to store and forward mode if we have to.
3144d467c136SBill Paul  */
3145e3d2833aSAlfred Perlstein static void
31460934f18aSMaxime Henrion dc_tx_underrun(struct dc_softc *sc)
3147d467c136SBill Paul {
3148d9efae03SPyun YongHyeon 	uint32_t netcfg, isr;
3149d9efae03SPyun YongHyeon 	int i, reinit;
3150d467c136SBill Paul 
3151d9efae03SPyun YongHyeon 	reinit = 0;
3152d9efae03SPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
3153d9efae03SPyun YongHyeon 	device_printf(sc->dc_dev, "TX underrun -- ");
3154d9efae03SPyun YongHyeon 	if ((sc->dc_flags & DC_TX_STORENFWD) == 0) {
3155d9efae03SPyun YongHyeon 		if (sc->dc_txthresh + DC_TXTHRESH_INC > DC_TXTHRESH_MAX) {
3156d9efae03SPyun YongHyeon 			printf("using store and forward mode\n");
3157d9efae03SPyun YongHyeon 			netcfg |= DC_NETCFG_STORENFWD;
3158d9efae03SPyun YongHyeon 		} else {
3159d9efae03SPyun YongHyeon 			printf("increasing TX threshold\n");
3160d9efae03SPyun YongHyeon 			sc->dc_txthresh += DC_TXTHRESH_INC;
3161d9efae03SPyun YongHyeon 			netcfg &= ~DC_NETCFG_TX_THRESH;
3162d9efae03SPyun YongHyeon 			netcfg |= sc->dc_txthresh;
31638f382a1fSPyun YongHyeon 		}
3164d467c136SBill Paul 
3165d467c136SBill Paul 		if (DC_IS_INTEL(sc)) {
3166d467c136SBill Paul 			/*
3167d467c136SBill Paul 			 * The real 21143 requires that the transmitter be idle
3168d467c136SBill Paul 			 * in order to change the transmit threshold or store
3169d467c136SBill Paul 			 * and forward state.
3170d467c136SBill Paul 			 */
3171d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg & ~DC_NETCFG_TX_ON);
3172d467c136SBill Paul 
3173d467c136SBill Paul 			for (i = 0; i < DC_TIMEOUT; i++) {
3174d467c136SBill Paul 				isr = CSR_READ_4(sc, DC_ISR);
3175d467c136SBill Paul 				if (isr & DC_ISR_TX_IDLE)
3176d467c136SBill Paul 					break;
3177d467c136SBill Paul 				DELAY(10);
3178d467c136SBill Paul 			}
3179d467c136SBill Paul 			if (i == DC_TIMEOUT) {
31806b9f5c94SGleb Smirnoff 				device_printf(sc->dc_dev,
3181432120f2SMarius Strobl 				    "%s: failed to force tx to idle state\n",
3182432120f2SMarius Strobl 				    __func__);
3183d9efae03SPyun YongHyeon 				reinit++;
3184d9efae03SPyun YongHyeon 			}
3185d9efae03SPyun YongHyeon 		}
3186d9efae03SPyun YongHyeon 	} else {
3187d9efae03SPyun YongHyeon 		printf("resetting\n");
3188d9efae03SPyun YongHyeon 		reinit++;
3189d9efae03SPyun YongHyeon 	}
3190d9efae03SPyun YongHyeon 
3191d9efae03SPyun YongHyeon 	if (reinit == 0) {
3192d9efae03SPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG, netcfg);
3193d9efae03SPyun YongHyeon 		if (DC_IS_INTEL(sc))
3194d9efae03SPyun YongHyeon 			CSR_WRITE_4(sc, DC_NETCFG, netcfg | DC_NETCFG_TX_ON);
3195d9efae03SPyun YongHyeon 	} else {
31968f382a1fSPyun YongHyeon 		sc->dc_ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3197c8b27acaSJohn Baldwin 		dc_init_locked(sc);
3198d467c136SBill Paul 	}
3199d467c136SBill Paul }
3200d467c136SBill Paul 
3201e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3202e4fc250cSLuigi Rizzo static poll_handler_t dc_poll;
3203e4fc250cSLuigi Rizzo 
32041abcdbd1SAttilio Rao static int
3205e4fc250cSLuigi Rizzo dc_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
3206e4fc250cSLuigi Rizzo {
3207e4fc250cSLuigi Rizzo 	struct dc_softc *sc = ifp->if_softc;
32081abcdbd1SAttilio Rao 	int rx_npkts = 0;
3209e4fc250cSLuigi Rizzo 
321040929967SGleb Smirnoff 	DC_LOCK(sc);
321140929967SGleb Smirnoff 
321240929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
321340929967SGleb Smirnoff 		DC_UNLOCK(sc);
32141abcdbd1SAttilio Rao 		return (rx_npkts);
3215e4fc250cSLuigi Rizzo 	}
321640929967SGleb Smirnoff 
3217e4fc250cSLuigi Rizzo 	sc->rxcycles = count;
32181abcdbd1SAttilio Rao 	rx_npkts = dc_rxeof(sc);
3219e4fc250cSLuigi Rizzo 	dc_txeof(sc);
322013f4c340SRobert Watson 	if (!IFQ_IS_EMPTY(&ifp->if_snd) &&
322113f4c340SRobert Watson 	    !(ifp->if_drv_flags & IFF_DRV_OACTIVE))
3222c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
3223e4fc250cSLuigi Rizzo 
3224e4fc250cSLuigi Rizzo 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
3225ee320f98SPyun YongHyeon 		uint32_t	status;
3226e4fc250cSLuigi Rizzo 
3227e4fc250cSLuigi Rizzo 		status = CSR_READ_4(sc, DC_ISR);
3228e4fc250cSLuigi Rizzo 		status &= (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF |
3229e4fc250cSLuigi Rizzo 			DC_ISR_TX_NOBUF | DC_ISR_TX_IDLE | DC_ISR_TX_UNDERRUN |
3230e4fc250cSLuigi Rizzo 			DC_ISR_BUS_ERR);
32315120abbfSSam Leffler 		if (!status) {
32325120abbfSSam Leffler 			DC_UNLOCK(sc);
32331abcdbd1SAttilio Rao 			return (rx_npkts);
32345120abbfSSam Leffler 		}
3235e4fc250cSLuigi Rizzo 		/* ack what we have */
3236e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_ISR, status);
3237e4fc250cSLuigi Rizzo 
3238e4fc250cSLuigi Rizzo 		if (status & (DC_ISR_RX_WATDOGTIMEO | DC_ISR_RX_NOBUF)) {
3239ee320f98SPyun YongHyeon 			uint32_t r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
3240e4fc250cSLuigi Rizzo 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3241e4fc250cSLuigi Rizzo 
3242e4fc250cSLuigi Rizzo 			if (dc_rx_resync(sc))
3243e4fc250cSLuigi Rizzo 				dc_rxeof(sc);
3244e4fc250cSLuigi Rizzo 		}
3245e4fc250cSLuigi Rizzo 		/* restart transmit unit if necessary */
3246e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_IDLE && sc->dc_cdata.dc_tx_cnt)
3247e4fc250cSLuigi Rizzo 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
3248e4fc250cSLuigi Rizzo 
3249e4fc250cSLuigi Rizzo 		if (status & DC_ISR_TX_UNDERRUN)
3250e4fc250cSLuigi Rizzo 			dc_tx_underrun(sc);
3251e4fc250cSLuigi Rizzo 
3252e4fc250cSLuigi Rizzo 		if (status & DC_ISR_BUS_ERR) {
32536b9f5c94SGleb Smirnoff 			if_printf(ifp, "%s: bus error\n", __func__);
32548f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3255c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3256e4fc250cSLuigi Rizzo 		}
3257e4fc250cSLuigi Rizzo 	}
32585120abbfSSam Leffler 	DC_UNLOCK(sc);
32591abcdbd1SAttilio Rao 	return (rx_npkts);
3260e4fc250cSLuigi Rizzo }
3261e4fc250cSLuigi Rizzo #endif /* DEVICE_POLLING */
3262e4fc250cSLuigi Rizzo 
3263e3d2833aSAlfred Perlstein static void
32640934f18aSMaxime Henrion dc_intr(void *arg)
326596f2e892SBill Paul {
326696f2e892SBill Paul 	struct dc_softc *sc;
326796f2e892SBill Paul 	struct ifnet *ifp;
3268ee320f98SPyun YongHyeon 	uint32_t r, status;
3269848a02fcSPyun YongHyeon 	int n;
327096f2e892SBill Paul 
327196f2e892SBill Paul 	sc = arg;
3272d2a1864bSWarner Losh 
32730934f18aSMaxime Henrion 	if (sc->suspended)
3274e8388e14SMitsuru IWASAKI 		return;
3275e8388e14SMitsuru IWASAKI 
3276d1ce9105SBill Paul 	DC_LOCK(sc);
3277a84b4e80SPyun YongHyeon 	status = CSR_READ_4(sc, DC_ISR);
3278a84b4e80SPyun YongHyeon 	if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0) {
3279a84b4e80SPyun YongHyeon 		DC_UNLOCK(sc);
3280a84b4e80SPyun YongHyeon 		return;
3281a84b4e80SPyun YongHyeon 	}
3282fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
3283e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
328440929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
328540929967SGleb Smirnoff 		DC_UNLOCK(sc);
328640929967SGleb Smirnoff 		return;
3287e4fc250cSLuigi Rizzo 	}
32880934f18aSMaxime Henrion #endif
328996f2e892SBill Paul 	/* Disable interrupts. */
329096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
329196f2e892SBill Paul 
3292a84b4e80SPyun YongHyeon 	for (n = 16; n > 0; n--) {
3293a84b4e80SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
3294a84b4e80SPyun YongHyeon 			break;
3295a84b4e80SPyun YongHyeon 		/* Ack interrupts. */
329696f2e892SBill Paul 		CSR_WRITE_4(sc, DC_ISR, status);
329796f2e892SBill Paul 
329873bf949cSBill Paul 		if (status & DC_ISR_RX_OK) {
3299848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
330073bf949cSBill Paul 				while (dc_rx_resync(sc))
330173bf949cSBill Paul 					dc_rxeof(sc);
330273bf949cSBill Paul 			}
330373bf949cSBill Paul 		}
330496f2e892SBill Paul 
330596f2e892SBill Paul 		if (status & (DC_ISR_TX_OK | DC_ISR_TX_NOBUF))
330696f2e892SBill Paul 			dc_txeof(sc);
330796f2e892SBill Paul 
330896f2e892SBill Paul 		if (status & DC_ISR_TX_IDLE) {
330996f2e892SBill Paul 			dc_txeof(sc);
331096f2e892SBill Paul 			if (sc->dc_cdata.dc_tx_cnt) {
331196f2e892SBill Paul 				DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
331296f2e892SBill Paul 				CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
331396f2e892SBill Paul 			}
331496f2e892SBill Paul 		}
331596f2e892SBill Paul 
3316d467c136SBill Paul 		if (status & DC_ISR_TX_UNDERRUN)
3317d467c136SBill Paul 			dc_tx_underrun(sc);
331896f2e892SBill Paul 
331996f2e892SBill Paul 		if ((status & DC_ISR_RX_WATDOGTIMEO)
332073bf949cSBill Paul 		    || (status & DC_ISR_RX_NOBUF)) {
332126b40a65SPyun YongHyeon 			r = CSR_READ_4(sc, DC_FRAMESDISCARDED);
332226b40a65SPyun YongHyeon 			ifp->if_ierrors += (r & 0xffff) + ((r >> 17) & 0x7ff);
3323848a02fcSPyun YongHyeon 			if (dc_rxeof(sc) == 0) {
332473bf949cSBill Paul 				while (dc_rx_resync(sc))
332573bf949cSBill Paul 					dc_rxeof(sc);
332673bf949cSBill Paul 			}
332773bf949cSBill Paul 		}
332896f2e892SBill Paul 
3329a84b4e80SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3330a84b4e80SPyun YongHyeon 			dc_start_locked(ifp);
3331a84b4e80SPyun YongHyeon 
333296f2e892SBill Paul 		if (status & DC_ISR_BUS_ERR) {
33338f382a1fSPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3334c8b27acaSJohn Baldwin 			dc_init_locked(sc);
3335a84b4e80SPyun YongHyeon 			DC_UNLOCK(sc);
3336a84b4e80SPyun YongHyeon 			return;
333796f2e892SBill Paul 		}
3338a84b4e80SPyun YongHyeon 		status = CSR_READ_4(sc, DC_ISR);
3339a84b4e80SPyun YongHyeon 		if (status == 0xFFFFFFFF || (status & DC_INTRS) == 0)
3340a84b4e80SPyun YongHyeon 			break;
334196f2e892SBill Paul 	}
334296f2e892SBill Paul 
334396f2e892SBill Paul 	/* Re-enable interrupts. */
3344a84b4e80SPyun YongHyeon 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
334596f2e892SBill Paul 		CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
334696f2e892SBill Paul 
3347d1ce9105SBill Paul 	DC_UNLOCK(sc);
334896f2e892SBill Paul }
334996f2e892SBill Paul 
335096f2e892SBill Paul /*
335196f2e892SBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
335296f2e892SBill Paul  * pointers to the fragment pointers.
335396f2e892SBill Paul  */
3354e3d2833aSAlfred Perlstein static int
3355a10c0e45SMike Silbersack dc_encap(struct dc_softc *sc, struct mbuf **m_head)
335696f2e892SBill Paul {
3357ebc284ccSMarius Strobl 	bus_dma_segment_t segs[DC_MAXFRAGS];
33585f14ee23SPyun YongHyeon 	bus_dmamap_t map;
3359ebc284ccSMarius Strobl 	struct dc_desc *f;
336096f2e892SBill Paul 	struct mbuf *m;
3361993a741aSMarius Strobl 	int cur, defragged, error, first, frag, i, idx, nseg;
3362cda97c50SMike Silbersack 
3363993a741aSMarius Strobl 	m = NULL;
3364993a741aSMarius Strobl 	defragged = 0;
3365993a741aSMarius Strobl 	if (sc->dc_flags & DC_TX_COALESCE &&
3366993a741aSMarius Strobl 	    ((*m_head)->m_next != NULL || sc->dc_flags & DC_TX_ALIGN)) {
3367993a741aSMarius Strobl 		m = m_defrag(*m_head, M_DONTWAIT);
3368993a741aSMarius Strobl 		defragged = 1;
3369993a741aSMarius Strobl 	} else {
3370cda97c50SMike Silbersack 		/*
3371993a741aSMarius Strobl 		 * Count the number of frags in this chain to see if we
3372993a741aSMarius Strobl 		 * need to m_collapse.  Since the descriptor list is shared
3373993a741aSMarius Strobl 		 * by all packets, we'll m_collapse long chains so that they
3374cda97c50SMike Silbersack 		 * do not use up the entire list, even if they would fit.
3375cda97c50SMike Silbersack 		 */
3376993a741aSMarius Strobl 		i = 0;
3377a10c0e45SMike Silbersack 		for (m = *m_head; m != NULL; m = m->m_next)
3378993a741aSMarius Strobl 			i++;
3379993a741aSMarius Strobl 		if (i > DC_TX_LIST_CNT / 4 ||
3380993a741aSMarius Strobl 		    DC_TX_LIST_CNT - i + sc->dc_cdata.dc_tx_cnt <=
3381993a741aSMarius Strobl 		    DC_TX_LIST_RSVD) {
3382993a741aSMarius Strobl 			m = m_collapse(*m_head, M_DONTWAIT, DC_MAXFRAGS);
3383993a741aSMarius Strobl 			defragged = 1;
3384993a741aSMarius Strobl 		}
3385993a741aSMarius Strobl 	}
3386993a741aSMarius Strobl 	if (defragged != 0) {
338782a67a70SMarius Strobl 		if (m == NULL) {
338882a67a70SMarius Strobl 			m_freem(*m_head);
338982a67a70SMarius Strobl 			*m_head = NULL;
3390cda97c50SMike Silbersack 			return (ENOBUFS);
339182a67a70SMarius Strobl 		}
3392a10c0e45SMike Silbersack 		*m_head = m;
3393cda97c50SMike Silbersack 	}
3394993a741aSMarius Strobl 
339556e5e7aeSMaxime Henrion 	idx = sc->dc_cdata.dc_tx_prod;
33965f14ee23SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3397ebc284ccSMarius Strobl 	    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3398ebc284ccSMarius Strobl 	if (error == EFBIG) {
3399993a741aSMarius Strobl 		if (defragged != 0 || (m = m_collapse(*m_head, M_DONTWAIT,
3400993a741aSMarius Strobl 		    DC_MAXFRAGS)) == NULL) {
3401ebc284ccSMarius Strobl 			m_freem(*m_head);
340282a67a70SMarius Strobl 			*m_head = NULL;
3403993a741aSMarius Strobl 			return (defragged != 0 ? error : ENOBUFS);
340482a67a70SMarius Strobl 		}
3405ebc284ccSMarius Strobl 		*m_head = m;
34065f14ee23SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->dc_tx_mtag,
3407ebc284ccSMarius Strobl 		    sc->dc_cdata.dc_tx_map[idx], *m_head, segs, &nseg, 0);
3408ebc284ccSMarius Strobl 		if (error != 0) {
3409ebc284ccSMarius Strobl 			m_freem(*m_head);
3410ebc284ccSMarius Strobl 			*m_head = NULL;
3411ebc284ccSMarius Strobl 			return (error);
341282a67a70SMarius Strobl 		}
3413ebc284ccSMarius Strobl 	} else if (error != 0)
3414ebc284ccSMarius Strobl 		return (error);
3415ebc284ccSMarius Strobl 	KASSERT(nseg <= DC_MAXFRAGS,
3416ebc284ccSMarius Strobl 	    ("%s: wrong number of segments (%d)", __func__, nseg));
3417ebc284ccSMarius Strobl 	if (nseg == 0) {
3418ebc284ccSMarius Strobl 		m_freem(*m_head);
3419ebc284ccSMarius Strobl 		*m_head = NULL;
3420ebc284ccSMarius Strobl 		return (EIO);
3421ebc284ccSMarius Strobl 	}
3422ebc284ccSMarius Strobl 
34235f14ee23SPyun YongHyeon 	/* Check descriptor overruns. */
34245f14ee23SPyun YongHyeon 	if (sc->dc_cdata.dc_tx_cnt + nseg > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
34255f14ee23SPyun YongHyeon 		bus_dmamap_unload(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx]);
34265f14ee23SPyun YongHyeon 		return (ENOBUFS);
34275f14ee23SPyun YongHyeon 	}
34285f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_mtag, sc->dc_cdata.dc_tx_map[idx],
34295f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
34305f14ee23SPyun YongHyeon 
3431ebc284ccSMarius Strobl 	first = cur = frag = sc->dc_cdata.dc_tx_prod;
3432ebc284ccSMarius Strobl 	for (i = 0; i < nseg; i++) {
3433ebc284ccSMarius Strobl 		if ((sc->dc_flags & DC_TX_ADMTEK_WAR) &&
3434ebc284ccSMarius Strobl 		    (frag == (DC_TX_LIST_CNT - 1)) &&
3435ebc284ccSMarius Strobl 		    (first != sc->dc_cdata.dc_tx_first)) {
34365f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_tx_mtag,
3437ebc284ccSMarius Strobl 			    sc->dc_cdata.dc_tx_map[first]);
3438ebc284ccSMarius Strobl 			m_freem(*m_head);
3439ebc284ccSMarius Strobl 			*m_head = NULL;
3440ebc284ccSMarius Strobl 			return (ENOBUFS);
3441ebc284ccSMarius Strobl 		}
3442ebc284ccSMarius Strobl 
34435f14ee23SPyun YongHyeon 		f = &sc->dc_ldata.dc_tx_list[frag];
3444ebc284ccSMarius Strobl 		f->dc_ctl = htole32(DC_TXCTL_TLINK | segs[i].ds_len);
3445ebc284ccSMarius Strobl 		if (i == 0) {
3446ebc284ccSMarius Strobl 			f->dc_status = 0;
3447ebc284ccSMarius Strobl 			f->dc_ctl |= htole32(DC_TXCTL_FIRSTFRAG);
3448ebc284ccSMarius Strobl 		} else
3449ebc284ccSMarius Strobl 			f->dc_status = htole32(DC_TXSTAT_OWN);
34505f14ee23SPyun YongHyeon 		f->dc_data = htole32(DC_ADDR_LO(segs[i].ds_addr));
3451ebc284ccSMarius Strobl 		cur = frag;
3452ebc284ccSMarius Strobl 		DC_INC(frag, DC_TX_LIST_CNT);
3453ebc284ccSMarius Strobl 	}
3454ebc284ccSMarius Strobl 
3455ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_prod = frag;
3456ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_cnt += nseg;
3457ebc284ccSMarius Strobl 	sc->dc_cdata.dc_tx_chain[cur] = *m_head;
34585f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_LASTFRAG);
3459ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_FIRSTFRAG)
34605f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[first].dc_ctl |=
3461ebc284ccSMarius Strobl 		    htole32(DC_TXCTL_FINT);
3462ebc284ccSMarius Strobl 	if (sc->dc_flags & DC_TX_INTR_ALWAYS)
34635f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
346406d23883SPyun YongHyeon 	if (sc->dc_flags & DC_TX_USE_TX_INTR &&
346506d23883SPyun YongHyeon 	    ++sc->dc_cdata.dc_tx_pkts >= 8) {
346606d23883SPyun YongHyeon 		sc->dc_cdata.dc_tx_pkts = 0;
34675f14ee23SPyun YongHyeon 		sc->dc_ldata.dc_tx_list[cur].dc_ctl |= htole32(DC_TXCTL_FINT);
346806d23883SPyun YongHyeon 	}
34695f14ee23SPyun YongHyeon 	sc->dc_ldata.dc_tx_list[first].dc_status = htole32(DC_TXSTAT_OWN);
3470ebc284ccSMarius Strobl 
34715f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
34725f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
34735f14ee23SPyun YongHyeon 
34745f14ee23SPyun YongHyeon 	/*
34755f14ee23SPyun YongHyeon 	 * Swap the last and the first dmamaps to ensure the map for
34765f14ee23SPyun YongHyeon 	 * this transmission is placed at the last descriptor.
34775f14ee23SPyun YongHyeon 	 */
34785f14ee23SPyun YongHyeon 	map = sc->dc_cdata.dc_tx_map[cur];
34795f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[cur] = sc->dc_cdata.dc_tx_map[first];
34805f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_map[first] = map;
34815f14ee23SPyun YongHyeon 
348296f2e892SBill Paul 	return (0);
348396f2e892SBill Paul }
348496f2e892SBill Paul 
3485e3d2833aSAlfred Perlstein static void
34860934f18aSMaxime Henrion dc_start(struct ifnet *ifp)
348796f2e892SBill Paul {
348896f2e892SBill Paul 	struct dc_softc *sc;
3489c8b27acaSJohn Baldwin 
3490c8b27acaSJohn Baldwin 	sc = ifp->if_softc;
3491c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3492c8b27acaSJohn Baldwin 	dc_start_locked(ifp);
3493c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3494c8b27acaSJohn Baldwin }
3495c8b27acaSJohn Baldwin 
3496ebc284ccSMarius Strobl /*
3497ebc284ccSMarius Strobl  * Main transmit routine
3498ebc284ccSMarius Strobl  * To avoid having to do mbuf copies, we put pointers to the mbuf data
3499ebc284ccSMarius Strobl  * regions directly in the transmit lists.  We also save a copy of the
3500ebc284ccSMarius Strobl  * pointers since the transmit list fragment pointers are physical
3501ebc284ccSMarius Strobl  * addresses.
3502ebc284ccSMarius Strobl  */
3503c8b27acaSJohn Baldwin static void
3504c8b27acaSJohn Baldwin dc_start_locked(struct ifnet *ifp)
3505c8b27acaSJohn Baldwin {
3506c8b27acaSJohn Baldwin 	struct dc_softc *sc;
35075f14ee23SPyun YongHyeon 	struct mbuf *m_head;
35085f14ee23SPyun YongHyeon 	int queued;
350996f2e892SBill Paul 
351096f2e892SBill Paul 	sc = ifp->if_softc;
351196f2e892SBill Paul 
3512c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
351396f2e892SBill Paul 
351476d40c85SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
351576d40c85SPyun YongHyeon 	    IFF_DRV_RUNNING || sc->dc_link == 0)
3516d1ce9105SBill Paul 		return;
351796f2e892SBill Paul 
35185f14ee23SPyun YongHyeon 	sc->dc_cdata.dc_tx_first = sc->dc_cdata.dc_tx_prod;
351996f2e892SBill Paul 
35205f14ee23SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
35215f14ee23SPyun YongHyeon 		/*
35225f14ee23SPyun YongHyeon 		 * If there's no way we can send any packets, return now.
35235f14ee23SPyun YongHyeon 		 */
35245f14ee23SPyun YongHyeon 		if (sc->dc_cdata.dc_tx_cnt > DC_TX_LIST_CNT - DC_TX_LIST_RSVD) {
35255f14ee23SPyun YongHyeon 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
35265f14ee23SPyun YongHyeon 			break;
35275f14ee23SPyun YongHyeon 		}
3528cbaf877fSBrian Feldman 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
352996f2e892SBill Paul 		if (m_head == NULL)
353096f2e892SBill Paul 			break;
353196f2e892SBill Paul 
3532a10c0e45SMike Silbersack 		if (dc_encap(sc, &m_head)) {
353382a67a70SMarius Strobl 			if (m_head == NULL)
353482a67a70SMarius Strobl 				break;
3535cbaf877fSBrian Feldman 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
353613f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
353796f2e892SBill Paul 			break;
353896f2e892SBill Paul 		}
353996f2e892SBill Paul 
3540cbaf877fSBrian Feldman 		queued++;
354196f2e892SBill Paul 		/*
354296f2e892SBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
354396f2e892SBill Paul 		 * to him.
354496f2e892SBill Paul 		 */
35459ef8b520SSam Leffler 		BPF_MTAP(ifp, m_head);
354696f2e892SBill Paul 	}
354796f2e892SBill Paul 
3548cbaf877fSBrian Feldman 	if (queued > 0) {
354996f2e892SBill Paul 		/* Transmit */
355096f2e892SBill Paul 		if (!(sc->dc_flags & DC_TX_POLL))
355196f2e892SBill Paul 			CSR_WRITE_4(sc, DC_TXSTART, 0xFFFFFFFF);
355296f2e892SBill Paul 
355396f2e892SBill Paul 		/*
355496f2e892SBill Paul 		 * Set a timeout in case the chip goes out to lunch.
355596f2e892SBill Paul 		 */
3556b1d16143SMarius Strobl 		sc->dc_wdog_timer = 5;
3557cbaf877fSBrian Feldman 	}
355896f2e892SBill Paul }
355996f2e892SBill Paul 
3560e3d2833aSAlfred Perlstein static void
35610934f18aSMaxime Henrion dc_init(void *xsc)
356296f2e892SBill Paul {
356396f2e892SBill Paul 	struct dc_softc *sc = xsc;
3564c8b27acaSJohn Baldwin 
3565c8b27acaSJohn Baldwin 	DC_LOCK(sc);
3566c8b27acaSJohn Baldwin 	dc_init_locked(sc);
3567c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
3568c8b27acaSJohn Baldwin }
3569c8b27acaSJohn Baldwin 
3570c8b27acaSJohn Baldwin static void
3571c8b27acaSJohn Baldwin dc_init_locked(struct dc_softc *sc)
3572c8b27acaSJohn Baldwin {
3573fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->dc_ifp;
357496f2e892SBill Paul 	struct mii_data *mii;
3575d314ebf5SPyun YongHyeon 	struct ifmedia *ifm;
357696f2e892SBill Paul 
3577c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
357896f2e892SBill Paul 
35798f382a1fSPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
35808f382a1fSPyun YongHyeon 		return;
35818f382a1fSPyun YongHyeon 
358296f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
358396f2e892SBill Paul 
358496f2e892SBill Paul 	/*
358596f2e892SBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
358696f2e892SBill Paul 	 */
358796f2e892SBill Paul 	dc_stop(sc);
358896f2e892SBill Paul 	dc_reset(sc);
3589d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc)) {
3590d314ebf5SPyun YongHyeon 		ifm = &mii->mii_media;
3591d314ebf5SPyun YongHyeon 		dc_apply_fixup(sc, ifm->ifm_media);
3592d314ebf5SPyun YongHyeon 	}
359396f2e892SBill Paul 
359496f2e892SBill Paul 	/*
359596f2e892SBill Paul 	 * Set cache alignment and burst length.
359696f2e892SBill Paul 	 */
359788d739dcSBill Paul 	if (DC_IS_ASIX(sc) || DC_IS_DAVICOM(sc))
359896f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, 0);
359996f2e892SBill Paul 	else
360096f2e892SBill Paul 		CSR_WRITE_4(sc, DC_BUSCTL, DC_BUSCTL_MRME | DC_BUSCTL_MRLE);
3601935fe010SLuigi Rizzo 	/*
3602935fe010SLuigi Rizzo 	 * Evenly share the bus between receive and transmit process.
3603935fe010SLuigi Rizzo 	 */
3604935fe010SLuigi Rizzo 	if (DC_IS_INTEL(sc))
3605935fe010SLuigi Rizzo 		DC_SETBIT(sc, DC_BUSCTL, DC_BUSCTL_ARBITRATION);
360696f2e892SBill Paul 	if (DC_IS_DAVICOM(sc) || DC_IS_INTEL(sc)) {
360796f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_USECA);
360896f2e892SBill Paul 	} else {
360996f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_BURSTLEN_16LONG);
361096f2e892SBill Paul 	}
361196f2e892SBill Paul 	if (sc->dc_flags & DC_TX_POLL)
361296f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_TXPOLL_1);
361396f2e892SBill Paul 	switch(sc->dc_cachesize) {
361496f2e892SBill Paul 	case 32:
361596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_32LONG);
361696f2e892SBill Paul 		break;
361796f2e892SBill Paul 	case 16:
361896f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_16LONG);
361996f2e892SBill Paul 		break;
362096f2e892SBill Paul 	case 8:
362196f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_8LONG);
362296f2e892SBill Paul 		break;
362396f2e892SBill Paul 	case 0:
362496f2e892SBill Paul 	default:
362596f2e892SBill Paul 		DC_SETBIT(sc, DC_BUSCTL, DC_CACHEALIGN_NONE);
362696f2e892SBill Paul 		break;
362796f2e892SBill Paul 	}
362896f2e892SBill Paul 
362996f2e892SBill Paul 	if (sc->dc_flags & DC_TX_STORENFWD)
363096f2e892SBill Paul 		DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
363196f2e892SBill Paul 	else {
3632d467c136SBill Paul 		if (sc->dc_txthresh > DC_TXTHRESH_MAX) {
363396f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
363496f2e892SBill Paul 		} else {
363596f2e892SBill Paul 			DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_STORENFWD);
363696f2e892SBill Paul 			DC_SETBIT(sc, DC_NETCFG, sc->dc_txthresh);
363796f2e892SBill Paul 		}
363896f2e892SBill Paul 	}
363996f2e892SBill Paul 
364096f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_NO_RXCRC);
364196f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_BACKOFF);
364296f2e892SBill Paul 
364396f2e892SBill Paul 	if (DC_IS_MACRONIX(sc) || DC_IS_PNICII(sc)) {
364496f2e892SBill Paul 		/*
364596f2e892SBill Paul 		 * The app notes for the 98713 and 98715A say that
364696f2e892SBill Paul 		 * in order to have the chips operate properly, a magic
364796f2e892SBill Paul 		 * number must be written to CSR16. Macronix does not
364896f2e892SBill Paul 		 * document the meaning of these bits so there's no way
364996f2e892SBill Paul 		 * to know exactly what they do. The 98713 has a magic
365096f2e892SBill Paul 		 * number all its own; the rest all use a different one.
365196f2e892SBill Paul 		 */
365296f2e892SBill Paul 		DC_CLRBIT(sc, DC_MX_MAGICPACKET, 0xFFFF0000);
365396f2e892SBill Paul 		if (sc->dc_type == DC_TYPE_98713)
365496f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98713);
365596f2e892SBill Paul 		else
365696f2e892SBill Paul 			DC_SETBIT(sc, DC_MX_MAGICPACKET, DC_MX_MAGIC_98715);
365796f2e892SBill Paul 	}
365896f2e892SBill Paul 
3659feb78939SJonathan Chen 	if (DC_IS_XIRCOM(sc)) {
3660feb78939SJonathan Chen 		/*
3661feb78939SJonathan Chen 		 * setup General Purpose Port mode and data so the tulip
3662feb78939SJonathan Chen 		 * can talk to the MII.
3663feb78939SJonathan Chen 		 */
3664feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_WRITE_EN | DC_SIAGP_INT1_EN |
3665feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3666feb78939SJonathan Chen 		DELAY(10);
3667feb78939SJonathan Chen 		CSR_WRITE_4(sc, DC_SIAGP, DC_SIAGP_INT1_EN |
3668feb78939SJonathan Chen 			   DC_SIAGP_MD_GP2_OUTPUT | DC_SIAGP_MD_GP0_OUTPUT);
3669feb78939SJonathan Chen 		DELAY(10);
3670feb78939SJonathan Chen 	}
3671feb78939SJonathan Chen 
367296f2e892SBill Paul 	DC_CLRBIT(sc, DC_NETCFG, DC_NETCFG_TX_THRESH);
3673d467c136SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_TXTHRESH_MIN);
367496f2e892SBill Paul 
367596f2e892SBill Paul 	/* Init circular RX list. */
367696f2e892SBill Paul 	if (dc_list_rx_init(sc) == ENOBUFS) {
36776b9f5c94SGleb Smirnoff 		device_printf(sc->dc_dev,
367822f6205dSJohn Baldwin 		    "initialization failed: no memory for rx buffers\n");
367996f2e892SBill Paul 		dc_stop(sc);
368096f2e892SBill Paul 		return;
368196f2e892SBill Paul 	}
368296f2e892SBill Paul 
368396f2e892SBill Paul 	/*
368456e5e7aeSMaxime Henrion 	 * Init TX descriptors.
368596f2e892SBill Paul 	 */
368696f2e892SBill Paul 	dc_list_tx_init(sc);
368796f2e892SBill Paul 
368896f2e892SBill Paul 	/*
368996f2e892SBill Paul 	 * Load the address of the RX list.
369096f2e892SBill Paul 	 */
369156e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_RXADDR, DC_RXDESC(sc, 0));
369256e5e7aeSMaxime Henrion 	CSR_WRITE_4(sc, DC_TXADDR, DC_TXDESC(sc, 0));
369396f2e892SBill Paul 
369496f2e892SBill Paul 	/*
369596f2e892SBill Paul 	 * Enable interrupts.
369696f2e892SBill Paul 	 */
3697e4fc250cSLuigi Rizzo #ifdef DEVICE_POLLING
3698e4fc250cSLuigi Rizzo 	/*
3699e4fc250cSLuigi Rizzo 	 * ... but only if we are not polling, and make sure they are off in
3700e4fc250cSLuigi Rizzo 	 * the case of polling. Some cards (e.g. fxp) turn interrupts on
3701e4fc250cSLuigi Rizzo 	 * after a reset.
3702e4fc250cSLuigi Rizzo 	 */
370340929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3704e4fc250cSLuigi Rizzo 		CSR_WRITE_4(sc, DC_IMR, 0x00000000);
3705e4fc250cSLuigi Rizzo 	else
3706e4fc250cSLuigi Rizzo #endif
370796f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
370896f2e892SBill Paul 	CSR_WRITE_4(sc, DC_ISR, 0xFFFFFFFF);
370996f2e892SBill Paul 
371096f2e892SBill Paul 	/* Enable transmitter. */
371196f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_TX_ON);
371296f2e892SBill Paul 
371396f2e892SBill Paul 	/*
3714918434c8SBill Paul 	 * If this is an Intel 21143 and we're not using the
3715918434c8SBill Paul 	 * MII port, program the LED control pins so we get
3716918434c8SBill Paul 	 * link and activity indications.
3717918434c8SBill Paul 	 */
371878999dd1SBill Paul 	if (sc->dc_flags & DC_TULIP_LEDS) {
3719918434c8SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG,
3720918434c8SBill Paul 		    DC_WDOG_CTLWREN | DC_WDOG_LINK | DC_WDOG_ACTIVITY);
372178999dd1SBill Paul 		CSR_WRITE_4(sc, DC_WATCHDOG, 0);
3722918434c8SBill Paul 	}
3723918434c8SBill Paul 
3724918434c8SBill Paul 	/*
372596f2e892SBill Paul 	 * Load the RX/multicast filter. We do this sort of late
372696f2e892SBill Paul 	 * because the filter programming scheme on the 21143 and
372796f2e892SBill Paul 	 * some clones requires DMAing a setup frame via the TX
372896f2e892SBill Paul 	 * engine, and we need the transmitter enabled for that.
372996f2e892SBill Paul 	 */
373096f2e892SBill Paul 	dc_setfilt(sc);
373196f2e892SBill Paul 
373296f2e892SBill Paul 	/* Enable receiver. */
373396f2e892SBill Paul 	DC_SETBIT(sc, DC_NETCFG, DC_NETCFG_RX_ON);
373496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXSTART, 0xFFFFFFFF);
373596f2e892SBill Paul 
373613f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
373713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
373896f2e892SBill Paul 
3739d314ebf5SPyun YongHyeon 	mii_mediachg(mii);
3740d314ebf5SPyun YongHyeon 	dc_setcfg(sc, sc->dc_if_media);
3741d314ebf5SPyun YongHyeon 
374226b40a65SPyun YongHyeon 	/* Clear missed frames and overflow counter. */
374326b40a65SPyun YongHyeon 	CSR_READ_4(sc, DC_FRAMESDISCARDED);
374426b40a65SPyun YongHyeon 
3745857fd445SBill Paul 	/* Don't start the ticker if this is a homePNA link. */
374645521525SPoul-Henning Kamp 	if (IFM_SUBTYPE(mii->mii_media.ifm_media) == IFM_HPNA_1)
3747857fd445SBill Paul 		sc->dc_link = 1;
3748857fd445SBill Paul 	else {
3749318b02fdSBill Paul 		if (sc->dc_flags & DC_21143_NWAY)
3750b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz/10, dc_tick, sc);
3751318b02fdSBill Paul 		else
3752b50c6312SJonathan Lemon 			callout_reset(&sc->dc_stat_ch, hz, dc_tick, sc);
3753857fd445SBill Paul 	}
3754b1d16143SMarius Strobl 
3755b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
3756b1d16143SMarius Strobl 	callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
375796f2e892SBill Paul }
375896f2e892SBill Paul 
375996f2e892SBill Paul /*
376096f2e892SBill Paul  * Set media options.
376196f2e892SBill Paul  */
3762e3d2833aSAlfred Perlstein static int
37630934f18aSMaxime Henrion dc_ifmedia_upd(struct ifnet *ifp)
376496f2e892SBill Paul {
376596f2e892SBill Paul 	struct dc_softc *sc;
376696f2e892SBill Paul 	struct mii_data *mii;
3767f43d9309SBill Paul 	struct ifmedia *ifm;
376896f2e892SBill Paul 
376996f2e892SBill Paul 	sc = ifp->if_softc;
377096f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3771c8b27acaSJohn Baldwin 	DC_LOCK(sc);
377296f2e892SBill Paul 	mii_mediachg(mii);
3773f43d9309SBill Paul 	ifm = &mii->mii_media;
3774f43d9309SBill Paul 
3775d314ebf5SPyun YongHyeon 	if (DC_IS_INTEL(sc))
3776d314ebf5SPyun YongHyeon 		dc_setcfg(sc, ifm->ifm_media);
3777d314ebf5SPyun YongHyeon 	else if (DC_IS_DAVICOM(sc) &&
377845521525SPoul-Henning Kamp 	    IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1)
3779f43d9309SBill Paul 		dc_setcfg(sc, ifm->ifm_media);
3780f43d9309SBill Paul 	else
378196f2e892SBill Paul 		sc->dc_link = 0;
3782c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
378396f2e892SBill Paul 
378496f2e892SBill Paul 	return (0);
378596f2e892SBill Paul }
378696f2e892SBill Paul 
378796f2e892SBill Paul /*
378896f2e892SBill Paul  * Report current media status.
378996f2e892SBill Paul  */
3790e3d2833aSAlfred Perlstein static void
37910934f18aSMaxime Henrion dc_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
379296f2e892SBill Paul {
379396f2e892SBill Paul 	struct dc_softc *sc;
379496f2e892SBill Paul 	struct mii_data *mii;
3795f43d9309SBill Paul 	struct ifmedia *ifm;
379696f2e892SBill Paul 
379796f2e892SBill Paul 	sc = ifp->if_softc;
379896f2e892SBill Paul 	mii = device_get_softc(sc->dc_miibus);
3799c8b27acaSJohn Baldwin 	DC_LOCK(sc);
380096f2e892SBill Paul 	mii_pollstat(mii);
3801f43d9309SBill Paul 	ifm = &mii->mii_media;
3802f43d9309SBill Paul 	if (DC_IS_DAVICOM(sc)) {
380345521525SPoul-Henning Kamp 		if (IFM_SUBTYPE(ifm->ifm_media) == IFM_HPNA_1) {
3804f43d9309SBill Paul 			ifmr->ifm_active = ifm->ifm_media;
3805f43d9309SBill Paul 			ifmr->ifm_status = 0;
3806432120f2SMarius Strobl 			DC_UNLOCK(sc);
3807f43d9309SBill Paul 			return;
3808f43d9309SBill Paul 		}
3809f43d9309SBill Paul 	}
381096f2e892SBill Paul 	ifmr->ifm_active = mii->mii_media_active;
381196f2e892SBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3812c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
381396f2e892SBill Paul }
381496f2e892SBill Paul 
3815e3d2833aSAlfred Perlstein static int
38160934f18aSMaxime Henrion dc_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
381796f2e892SBill Paul {
381896f2e892SBill Paul 	struct dc_softc *sc = ifp->if_softc;
381996f2e892SBill Paul 	struct ifreq *ifr = (struct ifreq *)data;
382096f2e892SBill Paul 	struct mii_data *mii;
3821d1ce9105SBill Paul 	int error = 0;
382296f2e892SBill Paul 
382396f2e892SBill Paul 	switch (command) {
382496f2e892SBill Paul 	case SIOCSIFFLAGS:
3825c8b27acaSJohn Baldwin 		DC_LOCK(sc);
382696f2e892SBill Paul 		if (ifp->if_flags & IFF_UP) {
38275d6dfbbbSLuigi Rizzo 			int need_setfilt = (ifp->if_flags ^ sc->dc_if_flags) &
38285d6dfbbbSLuigi Rizzo 				(IFF_PROMISC | IFF_ALLMULTI);
38295d6dfbbbSLuigi Rizzo 
383013f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
38315d6dfbbbSLuigi Rizzo 				if (need_setfilt)
383296f2e892SBill Paul 					dc_setfilt(sc);
38335d6dfbbbSLuigi Rizzo 			} else {
38348f382a1fSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3835c8b27acaSJohn Baldwin 				dc_init_locked(sc);
383696f2e892SBill Paul 			}
383796f2e892SBill Paul 		} else {
383813f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
383996f2e892SBill Paul 				dc_stop(sc);
384096f2e892SBill Paul 		}
384196f2e892SBill Paul 		sc->dc_if_flags = ifp->if_flags;
3842c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
384396f2e892SBill Paul 		break;
384496f2e892SBill Paul 	case SIOCADDMULTI:
384596f2e892SBill Paul 	case SIOCDELMULTI:
3846c8b27acaSJohn Baldwin 		DC_LOCK(sc);
384724507bc1SPyun YongHyeon 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
384896f2e892SBill Paul 			dc_setfilt(sc);
3849c8b27acaSJohn Baldwin 		DC_UNLOCK(sc);
385096f2e892SBill Paul 		break;
385196f2e892SBill Paul 	case SIOCGIFMEDIA:
385296f2e892SBill Paul 	case SIOCSIFMEDIA:
385396f2e892SBill Paul 		mii = device_get_softc(sc->dc_miibus);
385496f2e892SBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
385596f2e892SBill Paul 		break;
3856e695984eSRuslan Ermilov 	case SIOCSIFCAP:
385740929967SGleb Smirnoff #ifdef DEVICE_POLLING
385840929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
385940929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
386040929967SGleb Smirnoff 			error = ether_poll_register(dc_poll, ifp);
386140929967SGleb Smirnoff 			if (error)
386240929967SGleb Smirnoff 				return(error);
3863c8b27acaSJohn Baldwin 			DC_LOCK(sc);
386440929967SGleb Smirnoff 			/* Disable interrupts */
386540929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, 0x00000000);
386640929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
3867c8b27acaSJohn Baldwin 			DC_UNLOCK(sc);
386840929967SGleb Smirnoff 			return (error);
386940929967SGleb Smirnoff 		}
387040929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
387140929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
387240929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
387340929967SGleb Smirnoff 			/* Enable interrupts. */
387440929967SGleb Smirnoff 			DC_LOCK(sc);
387540929967SGleb Smirnoff 			CSR_WRITE_4(sc, DC_IMR, DC_INTRS);
387640929967SGleb Smirnoff 			ifp->if_capenable &= ~IFCAP_POLLING;
387740929967SGleb Smirnoff 			DC_UNLOCK(sc);
387840929967SGleb Smirnoff 			return (error);
387940929967SGleb Smirnoff 		}
388040929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3881e695984eSRuslan Ermilov 		break;
388296f2e892SBill Paul 	default:
38839ef8b520SSam Leffler 		error = ether_ioctl(ifp, command, data);
388496f2e892SBill Paul 		break;
388596f2e892SBill Paul 	}
388696f2e892SBill Paul 
388796f2e892SBill Paul 	return (error);
388896f2e892SBill Paul }
388996f2e892SBill Paul 
3890e3d2833aSAlfred Perlstein static void
3891b1d16143SMarius Strobl dc_watchdog(void *xsc)
389296f2e892SBill Paul {
3893b1d16143SMarius Strobl 	struct dc_softc *sc = xsc;
3894b1d16143SMarius Strobl 	struct ifnet *ifp;
389596f2e892SBill Paul 
3896b1d16143SMarius Strobl 	DC_LOCK_ASSERT(sc);
389796f2e892SBill Paul 
3898b1d16143SMarius Strobl 	if (sc->dc_wdog_timer == 0 || --sc->dc_wdog_timer != 0) {
3899b1d16143SMarius Strobl 		callout_reset(&sc->dc_wdog_ch, hz, dc_watchdog, sc);
3900b1d16143SMarius Strobl 		return;
3901b1d16143SMarius Strobl 	}
3902d1ce9105SBill Paul 
3903b1d16143SMarius Strobl 	ifp = sc->dc_ifp;
390496f2e892SBill Paul 	ifp->if_oerrors++;
3905b1d16143SMarius Strobl 	device_printf(sc->dc_dev, "watchdog timeout\n");
390696f2e892SBill Paul 
39078f382a1fSPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3908c8b27acaSJohn Baldwin 	dc_init_locked(sc);
390996f2e892SBill Paul 
3910cbaf877fSBrian Feldman 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3911c8b27acaSJohn Baldwin 		dc_start_locked(ifp);
391296f2e892SBill Paul }
391396f2e892SBill Paul 
391496f2e892SBill Paul /*
391596f2e892SBill Paul  * Stop the adapter and free any mbufs allocated to the
391696f2e892SBill Paul  * RX and TX lists.
391796f2e892SBill Paul  */
3918e3d2833aSAlfred Perlstein static void
39190934f18aSMaxime Henrion dc_stop(struct dc_softc *sc)
392096f2e892SBill Paul {
392196f2e892SBill Paul 	struct ifnet *ifp;
3922b3811c95SMaxime Henrion 	struct dc_list_data *ld;
3923b3811c95SMaxime Henrion 	struct dc_chain_data *cd;
3924b3811c95SMaxime Henrion 	int i;
39251da7683aSPyun YongHyeon 	uint32_t ctl, netcfg;
392696f2e892SBill Paul 
3927c8b27acaSJohn Baldwin 	DC_LOCK_ASSERT(sc);
3928d1ce9105SBill Paul 
3929fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
39305f14ee23SPyun YongHyeon 	ld = &sc->dc_ldata;
3931b3811c95SMaxime Henrion 	cd = &sc->dc_cdata;
393296f2e892SBill Paul 
3933b50c6312SJonathan Lemon 	callout_stop(&sc->dc_stat_ch);
3934b1d16143SMarius Strobl 	callout_stop(&sc->dc_wdog_ch);
3935b1d16143SMarius Strobl 	sc->dc_wdog_timer = 0;
39361da7683aSPyun YongHyeon 	sc->dc_link = 0;
393796f2e892SBill Paul 
393813f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
39393b3ec200SPeter Wemm 
39401da7683aSPyun YongHyeon 	netcfg = CSR_READ_4(sc, DC_NETCFG);
39411da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39421da7683aSPyun YongHyeon 		CSR_WRITE_4(sc, DC_NETCFG,
39431da7683aSPyun YongHyeon 		   netcfg & ~(DC_NETCFG_RX_ON | DC_NETCFG_TX_ON));
394496f2e892SBill Paul 	CSR_WRITE_4(sc, DC_IMR, 0x00000000);
39451da7683aSPyun YongHyeon 	/* Wait the completion of TX/RX SM. */
39461da7683aSPyun YongHyeon 	if (netcfg & (DC_NETCFG_RX_ON | DC_NETCFG_TX_ON))
39471da7683aSPyun YongHyeon 		dc_netcfg_wait(sc);
39481da7683aSPyun YongHyeon 
394996f2e892SBill Paul 	CSR_WRITE_4(sc, DC_TXADDR, 0x00000000);
395096f2e892SBill Paul 	CSR_WRITE_4(sc, DC_RXADDR, 0x00000000);
395196f2e892SBill Paul 
395296f2e892SBill Paul 	/*
395396f2e892SBill Paul 	 * Free data in the RX lists.
395496f2e892SBill Paul 	 */
395596f2e892SBill Paul 	for (i = 0; i < DC_RX_LIST_CNT; i++) {
3956b3811c95SMaxime Henrion 		if (cd->dc_rx_chain[i] != NULL) {
39575f14ee23SPyun YongHyeon 			bus_dmamap_sync(sc->dc_rx_mtag,
39585f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i], BUS_DMASYNC_POSTREAD);
39595f14ee23SPyun YongHyeon 			bus_dmamap_unload(sc->dc_rx_mtag,
39605f14ee23SPyun YongHyeon 			    cd->dc_rx_map[i]);
396156e5e7aeSMaxime Henrion 			m_freem(cd->dc_rx_chain[i]);
396256e5e7aeSMaxime Henrion 			cd->dc_rx_chain[i] = NULL;
396396f2e892SBill Paul 		}
396496f2e892SBill Paul 	}
39655f14ee23SPyun YongHyeon 	bzero(ld->dc_rx_list, DC_RX_LIST_SZ);
39665f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_rx_ltag, sc->dc_rx_lmap,
39675f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
396896f2e892SBill Paul 
396996f2e892SBill Paul 	/*
397096f2e892SBill Paul 	 * Free the TX list buffers.
397196f2e892SBill Paul 	 */
397296f2e892SBill Paul 	for (i = 0; i < DC_TX_LIST_CNT; i++) {
3973b3811c95SMaxime Henrion 		if (cd->dc_tx_chain[i] != NULL) {
3974af4358c7SMaxime Henrion 			ctl = le32toh(ld->dc_tx_list[i].dc_ctl);
39755f14ee23SPyun YongHyeon 			if (ctl & DC_TXCTL_SETUP) {
39765f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_stag, sc->dc_smap,
39775f14ee23SPyun YongHyeon 				    BUS_DMASYNC_POSTWRITE);
39785f14ee23SPyun YongHyeon 			} else {
39795f14ee23SPyun YongHyeon 				bus_dmamap_sync(sc->dc_tx_mtag,
39805f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i], BUS_DMASYNC_POSTWRITE);
39815f14ee23SPyun YongHyeon 				bus_dmamap_unload(sc->dc_tx_mtag,
39825f14ee23SPyun YongHyeon 				    cd->dc_tx_map[i]);
398356e5e7aeSMaxime Henrion 				m_freem(cd->dc_tx_chain[i]);
39845f14ee23SPyun YongHyeon 			}
3985b3811c95SMaxime Henrion 			cd->dc_tx_chain[i] = NULL;
398696f2e892SBill Paul 		}
398796f2e892SBill Paul 	}
39885f14ee23SPyun YongHyeon 	bzero(ld->dc_tx_list, DC_TX_LIST_SZ);
39895f14ee23SPyun YongHyeon 	bus_dmamap_sync(sc->dc_tx_ltag, sc->dc_tx_lmap,
39905f14ee23SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
399196f2e892SBill Paul }
399296f2e892SBill Paul 
399396f2e892SBill Paul /*
3994e8388e14SMitsuru IWASAKI  * Device suspend routine.  Stop the interface and save some PCI
3995e8388e14SMitsuru IWASAKI  * settings in case the BIOS doesn't restore them properly on
3996e8388e14SMitsuru IWASAKI  * resume.
3997e8388e14SMitsuru IWASAKI  */
3998e3d2833aSAlfred Perlstein static int
39990934f18aSMaxime Henrion dc_suspend(device_t dev)
4000e8388e14SMitsuru IWASAKI {
4001e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4002e8388e14SMitsuru IWASAKI 
4003e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4004c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4005e8388e14SMitsuru IWASAKI 	dc_stop(sc);
4006e8388e14SMitsuru IWASAKI 	sc->suspended = 1;
4007c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4008e8388e14SMitsuru IWASAKI 
4009e8388e14SMitsuru IWASAKI 	return (0);
4010e8388e14SMitsuru IWASAKI }
4011e8388e14SMitsuru IWASAKI 
4012e8388e14SMitsuru IWASAKI /*
4013e8388e14SMitsuru IWASAKI  * Device resume routine.  Restore some PCI settings in case the BIOS
4014e8388e14SMitsuru IWASAKI  * doesn't, re-enable busmastering, and restart the interface if
4015e8388e14SMitsuru IWASAKI  * appropriate.
4016e8388e14SMitsuru IWASAKI  */
4017e3d2833aSAlfred Perlstein static int
40180934f18aSMaxime Henrion dc_resume(device_t dev)
4019e8388e14SMitsuru IWASAKI {
4020e8388e14SMitsuru IWASAKI 	struct dc_softc *sc;
4021e8388e14SMitsuru IWASAKI 	struct ifnet *ifp;
4022e8388e14SMitsuru IWASAKI 
4023e8388e14SMitsuru IWASAKI 	sc = device_get_softc(dev);
4024fc74a9f9SBrooks Davis 	ifp = sc->dc_ifp;
4025e8388e14SMitsuru IWASAKI 
4026e8388e14SMitsuru IWASAKI 	/* reinitialize interface if necessary */
4027c8b27acaSJohn Baldwin 	DC_LOCK(sc);
4028e8388e14SMitsuru IWASAKI 	if (ifp->if_flags & IFF_UP)
4029c8b27acaSJohn Baldwin 		dc_init_locked(sc);
4030e8388e14SMitsuru IWASAKI 
4031e8388e14SMitsuru IWASAKI 	sc->suspended = 0;
4032c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
4033e8388e14SMitsuru IWASAKI 
4034e8388e14SMitsuru IWASAKI 	return (0);
4035e8388e14SMitsuru IWASAKI }
4036e8388e14SMitsuru IWASAKI 
4037e8388e14SMitsuru IWASAKI /*
403896f2e892SBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
403996f2e892SBill Paul  * get confused by errant DMAs when rebooting.
404096f2e892SBill Paul  */
40416a087a87SPyun YongHyeon static int
40420934f18aSMaxime Henrion dc_shutdown(device_t dev)
404396f2e892SBill Paul {
404496f2e892SBill Paul 	struct dc_softc *sc;
404596f2e892SBill Paul 
404696f2e892SBill Paul 	sc = device_get_softc(dev);
404796f2e892SBill Paul 
4048c8b27acaSJohn Baldwin 	DC_LOCK(sc);
404996f2e892SBill Paul 	dc_stop(sc);
4050c8b27acaSJohn Baldwin 	DC_UNLOCK(sc);
40516a087a87SPyun YongHyeon 
40526a087a87SPyun YongHyeon 	return (0);
405396f2e892SBill Paul }
405439d76ed6SPyun YongHyeon 
405539d76ed6SPyun YongHyeon static int
405639d76ed6SPyun YongHyeon dc_check_multiport(struct dc_softc *sc)
405739d76ed6SPyun YongHyeon {
405839d76ed6SPyun YongHyeon 	struct dc_softc *dsc;
405939d76ed6SPyun YongHyeon 	devclass_t dc;
406039d76ed6SPyun YongHyeon 	device_t child;
406139d76ed6SPyun YongHyeon 	uint8_t *eaddr;
406239d76ed6SPyun YongHyeon 	int unit;
406339d76ed6SPyun YongHyeon 
406439d76ed6SPyun YongHyeon 	dc = devclass_find("dc");
406539d76ed6SPyun YongHyeon 	for (unit = 0; unit < devclass_get_maxunit(dc); unit++) {
406639d76ed6SPyun YongHyeon 		child = devclass_get_device(dc, unit);
406739d76ed6SPyun YongHyeon 		if (child == NULL)
406839d76ed6SPyun YongHyeon 			continue;
406939d76ed6SPyun YongHyeon 		if (child == sc->dc_dev)
407039d76ed6SPyun YongHyeon 			continue;
407139d76ed6SPyun YongHyeon 		if (device_get_parent(child) != device_get_parent(sc->dc_dev))
407239d76ed6SPyun YongHyeon 			continue;
407339d76ed6SPyun YongHyeon 		if (unit > device_get_unit(sc->dc_dev))
407439d76ed6SPyun YongHyeon 			continue;
4075b289c607SPyun YongHyeon 		if (device_is_attached(child) == 0)
4076b289c607SPyun YongHyeon 			continue;
407739d76ed6SPyun YongHyeon 		dsc = device_get_softc(child);
4078b289c607SPyun YongHyeon 		device_printf(sc->dc_dev,
4079b289c607SPyun YongHyeon 		    "Using station address of %s as base\n",
408039d76ed6SPyun YongHyeon 		    device_get_nameunit(child));
408139d76ed6SPyun YongHyeon 		bcopy(dsc->dc_eaddr, sc->dc_eaddr, ETHER_ADDR_LEN);
408239d76ed6SPyun YongHyeon 		eaddr = (uint8_t *)sc->dc_eaddr;
408339d76ed6SPyun YongHyeon 		eaddr[5]++;
4084b289c607SPyun YongHyeon 		/* Prepare SROM to parse again. */
4085b289c607SPyun YongHyeon 		if (DC_IS_INTEL(sc) && dsc->dc_srom != NULL &&
4086b289c607SPyun YongHyeon 		    sc->dc_romwidth != 0) {
4087b289c607SPyun YongHyeon 			free(sc->dc_srom, M_DEVBUF);
4088b289c607SPyun YongHyeon 			sc->dc_romwidth = dsc->dc_romwidth;
4089b289c607SPyun YongHyeon 			sc->dc_srom = malloc(DC_ROM_SIZE(sc->dc_romwidth),
4090b289c607SPyun YongHyeon 			    M_DEVBUF, M_NOWAIT);
4091b289c607SPyun YongHyeon 			if (sc->dc_srom == NULL) {
4092b289c607SPyun YongHyeon 				device_printf(sc->dc_dev,
4093b289c607SPyun YongHyeon 				    "Could not allocate SROM buffer\n");
4094b289c607SPyun YongHyeon 				return (ENOMEM);
4095b289c607SPyun YongHyeon 			}
4096b289c607SPyun YongHyeon 			bcopy(dsc->dc_srom, sc->dc_srom,
4097b289c607SPyun YongHyeon 			    DC_ROM_SIZE(sc->dc_romwidth));
4098b289c607SPyun YongHyeon 		}
409939d76ed6SPyun YongHyeon 		return (0);
410039d76ed6SPyun YongHyeon 	}
410139d76ed6SPyun YongHyeon 	return (ENOENT);
410239d76ed6SPyun YongHyeon }
4103